home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / pct.arc / PAGE.C < prev    next >
Text File  |  1986-03-29  |  3KB  |  150 lines

  1. /* page.c:    paginates text to screen */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <xdir.h>
  6.  
  7. #define SCR_SIZ 25
  8. #define MAXLINE 256
  9. #define TABSPACE 8
  10. #define MAXFILES 150
  11. #define cls() fputs("\033[2J\033[H",stderr)
  12. #define clear_line() fputs("\033[2K",stderr)
  13. #define setcur(row,col) fprintf(stderr,"\033[%d;%dH",row,col)
  14. #define bold() fputs("\033[1m",stderr)
  15. #define reset() fputs("\033[0m",stderr)
  16. #define min(x,y) (x < y) ? x : y
  17.  
  18. extern char *trim_fspec();
  19. int tabstops[MAXLINE];
  20. char prompt();
  21. FILE *screen;
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     int rc, nfiles;
  28.     register i;
  29.     char temp[MAXLINE], trim_temp[MAXLINE];
  30.     struct file_info *xfiles[MAXFILES];
  31.  
  32.     settabs();
  33.     screen = fopen("con","w");
  34.  
  35.     if (argc == 1)
  36.     {
  37.         page((struct file_info *) NULL);
  38.         exit();
  39.     }
  40.  
  41.     /* ..expand wildcard filespecs.. */
  42.     nfiles = xdir(argc,argv,xfiles,MAXFILES,NORMAL);
  43.  
  44.     for (i = 0; i < nfiles; ++i)
  45.         if (freopen(xfiles[i]->name,"r",stdin) != NULL)
  46.         {
  47.             rc = page(xfiles[i]);
  48.  
  49.             if (i < nfiles-1 && rc == EOF)
  50.         {
  51.         strcpy(temp,xfiles[i]->name);
  52.             strcat(temp,"  (Next file: ");
  53.         strcat(temp,trim_fspec(trim_temp,xfiles[i+1]->name,30));
  54.         strcat(temp,")");
  55.         prompt(temp,0);
  56.         }
  57.         }
  58.         else if (i < nfiles-1)
  59.         {
  60.             fprintf(screen,"can't open %s\nHit any key...",xfiles[i]->name);
  61.             fflush(screen);
  62.             getcnb();
  63.         }
  64. }
  65.  
  66. int page(f)
  67. struct file_info *f;
  68. {
  69.     register count;
  70.     long bytes_read, file_size;
  71.     char line[MAXLINE], temp[MAXLINE], fname[MAXLINE];
  72.  
  73.     cls();
  74.     if (f)
  75.     {
  76.         file_size = f->size;
  77.         trim_fspec(fname,f->name,30);
  78.     }
  79.     else
  80.         strcpy(fname,"(stdin)");
  81.  
  82.     bytes_read = 0;
  83.  
  84.     for (count = 1; fgets(temp,MAXLINE-1,stdin) != NULL; ++count)
  85.     {
  86.         bytes_read += strlen(temp) + 1;
  87.         line[strlen(temp)-1] = '\0';    /* ..zap newline.. */
  88.     detab(line,temp);
  89.         fprintf(screen,"%-.*s\n",min(79,strlen(line)),line);
  90.         fflush(screen);
  91.         if (count % (SCR_SIZ-1) == 0)
  92.         {
  93.             switch(prompt(fname,(int) ((float) bytes_read/file_size * 100)))
  94.             {
  95.                 case '\015':
  96.                     --count;
  97.             break;
  98.                 case 'n':
  99.                     return 0;
  100.             }
  101.         }
  102.     }
  103.     return EOF;
  104. }
  105.  
  106. int prompt(fname,per_cent_read)
  107. char *fname;
  108. int per_cent_read;
  109. {
  110.     int c;
  111.  
  112.     setcur(SCR_SIZ,1);
  113.     bold();
  114.     fprintf(screen,"%s",fname);
  115.     if (per_cent_read > 0)
  116.         fprintf(screen," (%d%%)",per_cent_read);
  117.     fputs(" ...\r",screen);
  118.     fflush(screen);
  119.     reset();
  120.     c = tolower(getcnb());
  121.     clear_line();
  122.     if (c == 'q' || c == '\003')
  123.     exit();
  124.     return c;
  125. }
  126.     
  127. detab(t,s)
  128. char *s, *t;
  129. {
  130.     register i, j;
  131.  
  132.     for (i = 0, j = 0; j < MAXLINE-1 && i < strlen(s)-1; ++i)
  133.     if (s[i] == '\t')
  134.         do
  135.         t[j++] = ' ';
  136.         while (!tabstops[j]);
  137.     else if (!iscntrl(s[i]))
  138.         t[j++] = s[i];
  139.     t[j] = '\0';
  140. }
  141.  
  142. settabs()
  143. {
  144.     register i;
  145.  
  146.     tabstops[0] = 0;
  147.     for (i = 1; i < MAXLINE; ++i)
  148.     tabstops[i] = (i%TABSPACE == 0);
  149. }
  150.