home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / most423.zip / main.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  11KB  |  333 lines

  1. #define __MOST_VERSION__ "4.23"
  2. #define __MOST_USAGE__ "most [-bstvw1] [+line] [+/string] [+[C][D]] filename"
  3. /*
  4.                Most ---- a more/less paging type program.
  5.  
  6.    Copyright (C) 1991, 1993 by John E. Davis. (davis@amy.tch.harvard.edu)
  7.  
  8.                    The following disclaimer from GNU emacs.
  9.  
  10.                                  NO WARRANTY
  11.  
  12.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY NO
  13. WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT WHEN
  14. OTHERWISE STATED IN WRITING, JOHN E. DAVIS AND/OR OTHER PARTIES PROVIDE THIS
  15. PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND
  18. PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE,
  19. YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  20.  
  21.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL JOHN E. DAVIS, AND/OR ANY
  22. OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW,
  23. BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  24. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  25. INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
  26. RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE
  27. PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS PROGRAM, EVEN IF YOU HAVE
  28. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER
  29. PARTY.
  30.  
  31.                         GENERAL PUBLIC LICENSE TO COPY
  32.  
  33.   1. You may copy and distribute verbatim copies of this source file as you
  34. receive it, in any medium, provided that you conspicuously and appropriately
  35. publish on each copy a valid copyright notice "Copyright (C) 1990 John E.
  36. Davis"; and include following the copyright notice a verbatim copy of the
  37. above disclaimer of warranty and of this License.  You may charge a
  38. distribution fee for the physical act of transferring a copy.
  39.  
  40.   2. You may modify your copy or copies of this source file or
  41. any portion of it, and copy and distribute such modifications under
  42. the terms of Paragraph 1 above, provided that you also do the following:
  43.  
  44.     a) cause the modified files to carry prominent notices stating
  45.     that you changed the files and the date of any change; and
  46.  
  47.     b) cause the whole of any work that you distribute or publish,
  48.     that in whole or in part contains or is a derivative of this
  49.     program or any part thereof, to be licensed at no charge to all
  50.     third parties on terms identical to those contained in this
  51.     License Agreement (except that you may choose to grant more extensive
  52.     warranty protection to some or all third parties, at your option).
  53.  
  54.     c) You may charge a distribution fee for the physical act of
  55.     transferring a copy, and you may at your option offer warranty
  56.     protection in exchange for a fee.
  57.  
  58. Mere aggregation of another unrelated program with this program (or its
  59. derivative) on a volume of a storage or distribution medium does not bring
  60. the other program under the scope of these terms.
  61.  
  62.   3. You may copy and distribute this program (or a portion or derivative
  63. of it, under Paragraph 2) in object code or executable form under the terms
  64. of Paragraphs 1 and 2 above provided that you also do one of the following:
  65.  
  66.     a) accompany it with the complete corresponding machine-readable
  67.     source code, which must be distributed under the terms of
  68.     Paragraphs 1 and 2 above; or,
  69.  
  70.     b) accompany it with a written offer, valid for at least three
  71.     years, to give any third party free (except for a nominal
  72.     shipping charge) a complete machine-readable copy of the
  73.     corresponding source code, to be distributed under the terms of
  74.     Paragraphs 1 and 2 above; or,
  75.  
  76.     c) accompany it with the information you received as to where the
  77.     corresponding source code may be obtained.  (This alternative is
  78.     allowed only for noncommercial distribution and only if you
  79.     received the program in object code or executable form alone.)
  80.  
  81. For an executable file, complete source code means all the source code for
  82. all modules it contains; but, as a special exception, it need not include
  83. source code for modules which are standard libraries that accompany the
  84. operating system on which the executable file runs.
  85.  
  86.   4. You may not copy, sublicense, distribute or transfer this program
  87. except as expressly provided under this License Agreement.  Any attempt
  88. otherwise to copy, sublicense, distribute or transfer this program is void and
  89. your rights to use the program under this License agreement shall be
  90. automatically terminated.  However, parties who have received computer
  91. software programs from you with this License Agreement will not have
  92. their licenses terminated so long as such parties remain in full compliance.
  93.  
  94. In other words, you are welcome to use, share and improve this program.
  95. You are forbidden to forbid anyone else to use, share and improve
  96. what you give them.   Help stamp out software-hoarding!  */
  97.  
  98. char *MOST_VERSION = __MOST_VERSION__ ;
  99. #include <stdio.h>
  100. #include "externs.h"
  101. #include "window.h"
  102. #include "buffer.h"
  103. #include "file.h"
  104. #include "sysdep.h"
  105. #include "most.h"
  106.  
  107. int MOST_K_OPT = 0;    /* Display 8 bit unformatted (Kanji) */
  108. int  STARTING_LINE;
  109. char *MOST_PROGRAM;    /* Program Name (argv[0]) */
  110.  
  111. #include <ctype.h>
  112.  
  113. #ifdef VMS
  114. #include <stdlib.h>
  115. #ifndef isalpha
  116. #define isalpha(x) \
  117.       (((x >= 'A') && (x <= 'Z'))||((x >= 'a') && (x <= 'z')) ? 1 : 0)
  118. #endif
  119. #endif
  120.  
  121. #include "display.h"
  122.  
  123. void do_extended_switches(char *str)
  124. {
  125.     int i;
  126.     char ch,numstr[7];
  127.     
  128.     i = 0;
  129.     ch = *(++str);
  130.     if ( ch == '/')
  131.       {
  132.           strcpy(SEARCH_STR,++str);
  133.       }
  134.     else if (ch >= '0' && ch <= '9')
  135.       {
  136.           while (ch >= '0' && ch <= '9')
  137.             {
  138.                 numstr[i++] = ch;
  139.                 ch = *(++str);
  140.             }
  141.           numstr[i] = '\0';
  142.           (void) sscanf(numstr,"%d",&STARTING_LINE);
  143.       }
  144.     else if (isalpha(ch))
  145.       {
  146.           while (isalpha(ch))
  147.             {
  148.                 if ((ch == 'C') || (ch == 'c'))
  149.                   CASE_SENSITIVE = 1;
  150.                 else if ((ch == 'D') || (ch == 'd'))
  151.                   MOST_D_OPT = 1;   /* delete file mode */
  152.                 ch = *(++str);
  153.             }
  154.       }
  155.     
  156.     else
  157.       {
  158.           fprintf(stdout,"switch '+%s' not valid.\n",str);
  159.           exit(0);
  160.       }
  161. }
  162.  
  163. /* if non-zero, assume terminal is only a generic vt100 */
  164. static int assume_vt100 = 0;
  165.  
  166. void do_switches(char *str)
  167. {
  168.  
  169.     while (*str != '\0')
  170.       {
  171.           switch (*str++)
  172.             {
  173.               case 'a':
  174.               case 'A':
  175.                 MOST_A_OPT = 1; break;  /* check to see if file is binary */
  176.               case 's':
  177.               case 'S':
  178.                 SQUEEZE_LINES = 1; break;
  179.               case 'c':
  180.               case 'C':
  181.                 MOST_C_OPT = 1; break;
  182.               case 'V':
  183.               case 'v':
  184.                 MOST_V_OPT = 1;  /* verbose-- convert control chars to '^' 'ch' */
  185.                 break;
  186.               case 'W':
  187.               case 'w':  MOST_W_OPT = 1; break;
  188.               case 'K':
  189.               case 'k':  MOST_K_OPT = 1; break;
  190.               case 'B':
  191.               case 'b':
  192.                 MOST_B_OPT = 1;  /* Binary display 8 bit */
  193.                 break;
  194.               case 't':
  195.               case 'T': /* expand tabs to '^I'; meaningful only with 'v' */
  196.                 MOST_T_OPT = 1;
  197.                 break;
  198.               case 'L':
  199.               case 'l':
  200.                 MOST_L_OPT = 1;  /* use ^L to clear screen */
  201.                 break;
  202.          case '1': assume_vt100 = 1;
  203.             }
  204.       }
  205. }
  206.  
  207. void exit_error(char *s)
  208. {
  209.    reset_tty();
  210.    reset_display();
  211.    fprintf(stderr, "%s\n", s);
  212.    exit(1);
  213. }
  214.  
  215. void play_cat()
  216. {
  217.    char buf[2048];
  218.    int n;
  219.    
  220.    while ((n = fread(buf, 1, 2048, stdin)) > 0)
  221.      {
  222.     fwrite (buf, 1, n, stdout);
  223.      }
  224.    exit(0);
  225. }
  226.  
  227.  
  228. extern char *getenv();
  229. int main(int argc, char *argv[])
  230. {
  231.     char file[255], *switches;
  232.     int file_i = 0, quit,i,piped,a_opt;
  233.     unsigned long context;
  234.  
  235. #ifdef VMS
  236.    char filename[256];
  237. #else
  238.    int j;
  239. #endif
  240.  
  241.    
  242.     extern int isatty(int);
  243.     extern void sysinit(void);
  244.  
  245.     MOST_PROGRAM = argv[0];
  246.     piped = 0;
  247.     switches = getenv("MOST_SWITCHES");
  248.     if (switches !=  NULL)  do_switches(switches);
  249.           
  250.     i = 1;
  251.     if (argc > 1)
  252.       {
  253.           quit = 0;
  254.           while ((!quit) && (i < argc))
  255.             {
  256.                 if (argv[i][0] == '-')
  257.                   do_switches(argv[i++]);
  258.                 else if (argv[i][0] == '+')
  259.                   do_extended_switches(argv[i++]);
  260.                 else quit = 1;
  261.             }
  262.       }
  263.  
  264.     
  265.     if (i == argc)
  266.       {
  267.           if (isatty(0))   /* 1 if stdin is a terminal, 0 otherwise */
  268.             {
  269.                 fprintf(stderr,"MOST(%s) Usage: %s\n",MOST_VERSION,__MOST_USAGE__);
  270.                         exit(0);
  271.             }
  272.           /* assume input is from stdin */
  273.           file[0] = '\0';  /* tells most this is stdin */
  274.           piped = 1;
  275.           MOST_A_OPT = 0;  /* in this version since we cannot do an fseek */
  276.      if (!isatty(fileno(stdout))) play_cat();
  277.       }
  278.     else
  279.       strcpy(file,argv[i]);
  280.  
  281.     NUM_FILES = 0;
  282.     context = 0;
  283.  
  284.     init_tty();
  285. #ifdef unix
  286.     get_terminfo();           /* set up esc sequences etc... */
  287. #else
  288.    set_term_vtxxx(assume_vt100);
  289. #endif
  290.    
  291.     if (MOST_B_OPT) MOST_A_OPT = 0;   /* explicit b overrides a */
  292.     a_opt = MOST_A_OPT;
  293.  
  294.     if (!piped)
  295.       {
  296.           file_i = i;
  297. #ifdef VMS
  298.           while(i < argc)
  299.             {
  300.                 if (argv[i][0] == '.') strcpy(file,"*"); else *file = 0;
  301.                 strcat(file,unix2vms(argv[i++]));
  302.                 while (expand_file_name(file,filename))
  303.                   {
  304.                       FILE_RING[NUM_FILES] = (char*) MALLOC(strlen(filename) + 1);
  305.                       strcpy(FILE_RING[NUM_FILES++], filename);
  306.                   }
  307.             }
  308.      if (NUM_FILES) strcpy(file,FILE_RING[0]);
  309.      else fputs("%%MOST-W-NOFILES, no files found\n", stderr);
  310. #else
  311.           j = 0;
  312.           NUM_FILES = argc - i;
  313.           while (i < argc)
  314.             {
  315.                 FILE_RING[j++] = argv[i++];
  316.             }
  317. #endif
  318.       }
  319.  
  320.     if (NUM_FILES) quit = 0; else quit = 1;
  321.     if (!quit || piped) most(file,STARTING_LINE);
  322.     if (quit) fprintf(stderr,"File %s not found\n", argv[file_i]);
  323.  
  324.     reset_tty();
  325.     reset_display();
  326. #ifndef  VMS
  327.     tt_putchar('\n');
  328. #endif    
  329.     exit(0);
  330.     return (0);
  331. }
  332.  
  333.