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