home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / page.c < prev    next >
Text File  |  1979-12-31  |  2KB  |  123 lines

  1. /* page.c:    UNIX more clone */
  2.  
  3. /*
  4.     Paginates text files to screen.  Wildcards and pathnames are
  5.     processed correctly.  At the end of each screen-painting, a
  6.     keypress of 'n' will skip to the next file to be displayed.
  7.     The key 'q' will return to DOS.  Hitting the return key will
  8.     scroll down one line.  Any other keypress will cause the next
  9.     screen of the current file to be displayed.  Can also be used
  10.     as a filter.
  11.  
  12. -----
  13.     (c) Chuck Allison, 1985
  14. -----
  15.  
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #define screen_size 25
  21. #define ESC 27
  22. #define MAXLINE 256
  23. #define MAXFILES 150
  24. extern char getkey();        /* ..get keystroke through BIOS.. */
  25.  
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     FILE *fp;
  32.     int maxarg = MAXFILES, i, xargc, rc;
  33.     char *xargv[MAXFILES];
  34.  
  35.     if (argc == 1)
  36.         more(stdin,"(stdin)");
  37.     else
  38.     {
  39.         /* ..expand wildcards, if any.. */
  40.         xargc = exargs("",argc,argv,xargv,maxarg);
  41.  
  42.         for (i = 0; i < xargc; ++i)
  43.             if ((fp = fopen(xargv[i],"r")) == NULL)
  44.                 fprintf(stderr,"can't open: %s\n",xargv[i]);
  45.             else
  46.             {
  47.                 rc = more(fp,xargv[i]);
  48.                 fclose(fp);
  49.                 /* ..pause between files.. */
  50.                 if (rc == EOF && i < xargc-1)
  51.                     pause();
  52.             }
  53.     }
  54. }
  55.  
  56.  
  57. int more(f,name)
  58. FILE *f;
  59. char *name;
  60. {
  61.     int lc, key;
  62.     char line[MAXLINE];
  63.  
  64.     /* ..print filename.. */
  65.     filename(name);
  66.     lc = 2;
  67.  
  68.     while (fgets(line,MAXLINE-1,f) != NULL)
  69.         if (lc <= screen_size-1)
  70.         {
  71.             /* ..continue scroll.. */
  72.             fputs(line,stderr);
  73.             ++lc;
  74.         }
  75.         else if ((key = tolower(pause())) == 'n')
  76.             /* ..stop - get next file, if any.. */
  77.             return key;
  78.         else if (key == '\r')
  79.             /* ..scroll 1 line only.. */
  80.             fputs(line,stderr);
  81.         else
  82.         {
  83.             /* ..start next screen.. */
  84.             filename(name);
  85.             fputs(line,stderr);
  86.             lc = 3;
  87.         }
  88.  
  89.     return EOF;
  90. }
  91.  
  92.  
  93. filename(s)        /* ..clear screen and show current file name.. */
  94. char *s;
  95. {
  96.     fprintf(stderr,"%c[2J%c[7m%-20.20s%c[0m\n",ESC,ESC,s,ESC);
  97. }
  98.  
  99. int pause()
  100. {
  101.     int key;
  102.  
  103.     /* ..position cursor.. */
  104.     fprintf(stderr,"%c[25;1H",ESC);    
  105.  
  106.     /* ..print prompt.. */
  107.     fprintf(stderr,"%c[7mmore...%c[0m",ESC,ESC);
  108.     fflush(stderr);
  109.  
  110.     /* ..get keypress.. */
  111.     key = tolower(getkey());
  112.  
  113.     /* ..wipe prompt.. */
  114.     fprintf(stderr,"\r       \r");
  115.     fflush(stderr);
  116.  
  117.     /* ..quit if requested.. */
  118.     if (key == 'q')
  119.         exit(0);
  120.  
  121.     return(key);
  122. }
  123.