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