home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / lst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-09-12  |  3.6 KB  |  155 lines

  1. /*
  2. ** lst.c -- list text in columns on pages
  3. */
  4. #include <stdio.h>
  5. #include "tools.h"
  6. #define NOCCARGC
  7. int
  8.   fd,     /* file descriptor for input */
  9.   eof,    /* end of file if YES */
  10.   cols,   /* number of cols */
  11.   cwidth, /* col width */
  12.   pwidth, /* page width */
  13.   plength,/* page length */
  14.   blanks, /* blank lines processed if YES */
  15.   numbers,/* number lines if > 0 */
  16.   pause;  /* pause before each page if YES */
  17. main(argc, argv) int argc, *argv; {
  18.   if(!isatty(stdout)) {        /* set default dimensions */
  19.     pause=NO;
  20.     pwidth=PTRWIDE-1;
  21.     plength=PTRHIGH-PTRSKIP-PTRHDR;  /* page body height */
  22.     }
  23.   else {
  24.     pause=YES;
  25.     pwidth=CRTWIDE-1;
  26.     plength=CRTHIGH-1;               /* allow for prompts */
  27.     }
  28.   blanks=YES;
  29.   eof=NO;
  30.   cols=1;
  31.   numbers=1;
  32.   fd=stdin;
  33.   doargs(argc, argv);
  34.   cwidth=pwidth/cols;
  35.   while(eof==NO) {
  36.     if(pause) {
  37.       fputs("waiting... ", stderr);
  38.       fgetc(stderr);
  39.       }
  40.     eof=column(fd, stdout, cwidth, cols, plength, &numbers);
  41.     }
  42.   fclose(stdout);
  43.   }
  44. doargs(argc, argv) int argc, *argv; {
  45.   int i, j, k, err;
  46.   char arg[MAXFN];
  47.   err=NO;
  48.   i=0;
  49.   while(getarg(++i, arg, MAXFN, argc, argv) != EOF) {
  50.     if(arg[0] != '-') {
  51.       if(!(fd=fopen(arg, "r"))) {err=YES; break;}
  52.       continue;
  53.       }
  54.     if(same(arg[1], 'c')) {
  55.       if((j=utoi(arg+2, &cols)) < 1) {err=YES; break;}
  56.       if((cols < 1)|(arg[j+2] > ' ')) {err=YES; break;}
  57.       continue;
  58.       }
  59.     if(same(arg[1], 'n')) {
  60.       if(arg[3] <= ' ') {
  61.         if(same(arg[2], 'b')) {
  62.           blanks=NO;
  63.           continue;
  64.           }
  65.         if(same(arg[2], 'p')) {
  66.           pause=NO;
  67.           continue;
  68.           }
  69.         if(same(arg[2], 'n')) {
  70.           numbers=0;
  71.           continue;
  72.           }
  73.         err=YES;
  74.         }
  75.       }
  76.     if(same(arg[1], 'p')) {
  77.       if((j=utoi(arg+3, &k)) > 0) {
  78.         if((k > 0)&(arg[j+3] <= ' ')) {
  79.           if(same(arg[2], 'l')) {
  80.             plength=k;
  81.             continue;
  82.             }
  83.           if(same(arg[2], 'w')) {
  84.             pwidth=k;
  85.             continue;
  86.             }
  87.           }
  88.         }
  89.       }
  90.     err=YES;
  91.     }
  92.   if(err) {
  93.     fputs("usage: LST [file] [-C#] [-PW#] [-PL#] [-NB] [-NN] [-NP]\n",
  94.           stderr);
  95.     abort(7);
  96.     }
  97.   }
  98. column(in, out, cwidth, cols, plength, number)
  99.    int in, out, cwidth, cols, plength, *number; {
  100.    int eof, lines, colcnt, lwidth, i, bufsz, linecnt;
  101.   char *ptr, *stop, *eptr, *nexteptr, *buf, *bend;
  102.   bufsz=cols*(cwidth+1)*plength;
  103.   ptr=buf=malloc(bufsz);
  104.   bend=buf+bufsz;
  105.   eof=NO;
  106.   while(ptr < bend) {
  107.     poll(YES);
  108.     if(*number) {
  109.       i=4;
  110.       itou(*number, ptr, -i);
  111.       ptr[i++]=' ';
  112.       }
  113.     else i=0;
  114.     if(fgets(ptr+i, cwidth+1-i, in)==NULL) {
  115.       eof=YES;
  116.       break;
  117.       }
  118.     trim(ptr+i);
  119.     if((blanks==NO) && (ptr[i]==NULL)) continue;
  120.     if(*number) ++(*number);
  121.     ptr=ptr+cwidth+1;
  122.     }
  123.   stop=ptr;
  124.   ptr=buf;
  125.   lwidth=cols*(cwidth+1);
  126.   lines=(stop-buf)/lwidth;
  127.   if((stop-buf)%lwidth) ++lines;
  128.   if(lines==0) return eof;
  129.   linecnt=lines;
  130.   while(linecnt--) {
  131.     poll(YES);
  132.     eptr=ptr;
  133.     colcnt=cols;
  134.     while(colcnt--) {
  135.       sout(eptr, out);
  136.       if((nexteptr=eptr+(lines*(cwidth+1))) >= stop) break;
  137.       if(colcnt > 0) {
  138.         if((i=strlen(eptr)) < cwidth) {
  139.           i=cwidth-i;
  140.           while(i--) cout(' ', out);
  141.           }
  142.         }
  143.       eptr=nexteptr;
  144.       }
  145.     cout('\n', out);
  146.     ptr=ptr+cwidth+1;
  147.     }
  148.   free(buf);
  149.   return eof;
  150.   }
  151. #include "out.c"
  152. #include "same.c"
  153. #include "trim.c"
  154.  
  155.