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