home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / less / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-27  |  5.8 KB  |  324 lines

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