home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2994 / main.c < prev    next >
C/C++ Source or Header  |  1991-03-05  |  5KB  |  276 lines

  1. /*
  2.  * Entry point, initialization, miscellaneous routines.
  3.  */
  4.  
  5. #include "less.h"
  6. #include "position.h"
  7.  
  8. public int    ispipe;
  9. public char *    every_first_cmd = NULL;
  10. public int    new_file;
  11. public int    is_tty;
  12. public IFILE    curr_ifile = NULL_IFILE;
  13. public IFILE    old_ifile = NULL_IFILE;
  14. public struct scrpos initial_scrpos;
  15. public int    any_display = 0;
  16. public int    scroll;
  17. public char *    progname;
  18. public int    quitting;
  19.  
  20. extern int    file;
  21. extern int    quit_at_eof;
  22. extern int    hit_eof;
  23. extern int    cbufs;
  24. extern int    errmsgs;
  25. extern int    screen_trashed;
  26. extern int    force_open;
  27.  
  28. #if LOGFILE
  29. public int    logfile = -1;
  30. public int    force_logfile = 0;
  31. public char *    namelogfile = NULL;
  32. #endif
  33.  
  34. #if EDITOR
  35. public char *    editor;
  36. public char *    editproto;
  37. #endif
  38.  
  39. #if TAGS
  40. extern char *    tagfile;
  41. extern char *    tagpattern;
  42. extern int    tagoption;
  43. #endif
  44.  
  45.  
  46.  
  47. /*
  48.  * Entry point.
  49.  */
  50. main(argc, argv)
  51.     int argc;
  52.     char *argv[];
  53. {
  54.     IFILE h;
  55.     int nofiles;
  56.     extern char *getenv();
  57.  
  58.     progname = *argv++;
  59.  
  60.     /*
  61.      * Process command line arguments and LESS environment arguments.
  62.      * Command line arguments override environment arguments.
  63.      */
  64.     init_prompt();
  65.     init_charset();
  66.     init_option();
  67.     scan_option(getenv("LESS"));
  68.  
  69. #define    isoptstring(s)    (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
  70.     while (--argc > 0 && (isoptstring(argv[0]) || isoptpending()))
  71.         scan_option(*argv++);
  72. #undef isoptstring
  73.  
  74.     if (isoptpending())
  75.     {
  76.         /*
  77.          * Last command line option was a flag requiring a
  78.          * following string, but there was no following string.
  79.          */
  80.         nopendopt();
  81.         quit(0);
  82.     }
  83.  
  84. #if USERFILE
  85.     /*
  86.      * Try to use the lesskey file "$HOME/.less".
  87.      */
  88.     add_hometable();
  89. #endif
  90. #if EDITOR
  91.     editor = getenv("EDITOR");
  92.     if (editor == NULL || *editor == '\0')
  93.         editor = EDIT_PGM;
  94.     editproto = getenv("LESSEDIT");
  95.     if (editproto == NULL || *editproto == '\0')
  96.         editproto = "%E ?lm+%lm. %f";
  97. #endif
  98.  
  99.     /*
  100.      * Set up terminal, etc.
  101.      */
  102.     is_tty = isatty(1);
  103.     if (!is_tty)
  104.     {
  105.         /*
  106.          * Output is not a tty.
  107.          * Just copy the input file(s) to output.
  108.          */
  109.         if (argc <= 0)
  110.         {
  111.             if (edit("-", 0) == 0)
  112.                 cat_file();
  113.         } else
  114.         {
  115.             while (--argc >= 0)
  116.             {
  117.                 if (edit(*argv++, 0) == 0)
  118.                     cat_file();
  119.             }
  120.         }
  121.         quit(0);
  122.     }
  123.  
  124.     /*
  125.      * Call get_ifile with all the command line filenames
  126.      * to "register" them with the ifile system.
  127.      */
  128.     h = NULL_IFILE;
  129.     while (--argc >= 0)
  130.         h = get_ifile(*argv++, h);
  131.  
  132.     init_mark();
  133.     raw_mode(1);
  134.     get_term();
  135.     open_getchr();
  136.  
  137.     init_signals(1);
  138.  
  139.     /*
  140.      * Select the first file to examine.
  141.      */
  142. #if TAGS
  143.     if (tagoption)
  144.     {
  145.         /*
  146.          * A -t option was given.
  147.          * Verify that no filenames were also given.
  148.          * Edit the file selected by the "tags" search,
  149.          * and search for the proper line in the file.
  150.          */
  151.         if (nifile() > 0)
  152.         {
  153.             error("No filenames allowed with -t option", NULL_PARG);
  154.             quit(1);
  155.         }
  156.         if (tagfile == NULL)
  157.             quit(1);
  158.         if (edit(tagfile, 0) || tagsearch())
  159.             quit(1);
  160.     } else
  161. #endif
  162.     if (nifile() == 0)
  163.         nofiles = edit("-", 0);    /* Standard input */
  164.     else 
  165.         nofiles = edit_first();
  166.  
  167.     if (nofiles)
  168.     {
  169.         quit(1);
  170.         /*NOTREACHED*/
  171.     }
  172.  
  173.     init();
  174.     commands();
  175.     quit(0);
  176.     /*NOTREACHED*/
  177. }
  178.  
  179. /*
  180.  * Copy a string, truncating to the specified length if necessary.
  181.  * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
  182.  */
  183.     public void
  184. strtcpy(to, from, len)
  185.     char *to;
  186.     char *from;
  187.     unsigned int len;
  188. {
  189.     strncpy(to, from, len);
  190.     to[len-1] = '\0';
  191. }
  192.  
  193. /*
  194.  * Copy a string to a "safe" place
  195.  * (that is, to a buffer allocated by calloc).
  196.  */
  197.     public char *
  198. save(s)
  199.     char *s;
  200. {
  201.     register char *p;
  202.  
  203.     p = (char *) ecalloc(strlen(s)+1, sizeof(char));
  204.     strcpy(p, s);
  205.     return (p);
  206. }
  207.  
  208.     public VOID_POINTER
  209. ecalloc(count, size)
  210.     int count;
  211.     unsigned int size;
  212. {
  213.     register VOID_POINTER p;
  214.  
  215.     p = calloc(count, size);
  216.     if (p != NULL)
  217.         return (p);
  218.     error("Cannot allocate memory", NULL_PARG);
  219.     quit(1);
  220.     /*NOTREACHED*/
  221. }
  222.  
  223. /*
  224.  * Skip leading spaces in a string.
  225.  */
  226.     public char *
  227. skipsp(s)
  228.     register char *s;
  229. {
  230.     while (*s == ' ' || *s == '\t')    
  231.         s++;
  232.     return (s);
  233. }
  234.  
  235. /*
  236.  * Exit the program.
  237.  */
  238.     public void
  239. quit(status)
  240.     int status;
  241. {
  242.     static int save_status;
  243.  
  244.     /*
  245.      * Put cursor at bottom left corner, clear the line,
  246.      * reset the terminal modes, and exit.
  247.      */
  248.     if (status < 0)
  249.         status = save_status;
  250.     else
  251.         save_status = status;
  252.     quitting = 1;
  253. #if LOGFILE
  254.     end_logfile();
  255. #endif
  256.     if (any_display)
  257.     {
  258.         lower_left();
  259.         clear_eol();
  260.     }
  261.     deinit();
  262.     flush();
  263.     raw_mode(0);
  264. #if __MSDOS__
  265.     restore_screen();
  266.     /* 
  267.      * If we don't close 2, we get some garbage from
  268.      * 2's buffer when it flushes automatically.
  269.      * I cannot track this one down  RB
  270.      * The same bug shows up if we use ^C^C to abort.
  271.      */
  272.     close(2);
  273. #endif
  274.     exit(status);
  275. }
  276.