home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 110_01 / tabify.c < prev    next >
Text File  |  1984-03-03  |  3KB  |  137 lines

  1.  
  2. /*
  3.     Tabify.c    written by Leor Zolman
  4.  
  5.     This filter takes sequences of spaces in a file and turns
  6.     them, whenever possible, into tabs. Usage:
  7.  
  8.         A>tabify oldfile [newfile] <cr>
  9.  
  10.     Quoted strings are not modified. Likewise, the remainder of the
  11.     line is not modified. The \<NL> escape for line continuation
  12.     in C is recognized and handled accordingly.
  13.  
  14. */
  15. /* Changed 6-10-81 CAF newfile optional, eliminated crt list */
  16. /* Changed 8-5-81 CAF Quote terminated at \n */
  17. /* Changed 9-8-81 CAF Strip parity rubouts nulls */
  18.  
  19. #include "a:bdscio.h"
  20. #define stdout 1
  21.  
  22. main(argc,argv)
  23. char **argv;
  24. {
  25.     int scount, column, ifd, ofd, i;
  26.     int lastc, c, tabwidth;
  27.     FILE ibuf,outfil, *obuf;
  28.  
  29.     tabwidth= 8;
  30.     if(argc<2) {
  31. usage:
  32.         puts("usage: tabify [-tabwidth] oldfile [newfile]");
  33.         exit();
  34.     }
  35.     if(argv[1][0] == '-') {
  36.         tabwidth= atoi( &argv[1][1]);
  37.         argv++;
  38.         if( --argc < 2)
  39.             goto usage;
  40.     }
  41.     ifd = fopen(argv[1],ibuf);
  42.     if(argc>2) {
  43.         obuf= outfil;
  44.         ofd = fcreat(argv[2],obuf);
  45.     }
  46.     else {
  47.         obuf=stdout;
  48.         ofd=0;
  49.     }
  50.     if (ifd == ERROR || ofd == ERROR) {
  51.         puts("Can't open file(s)");
  52.         exit();
  53.     }
  54.  
  55.     scount = column = 0;
  56.  
  57.     do {
  58.         c = getc(ibuf);
  59.         if (c == ERROR)
  60.             break;
  61.         switch(c &=0177) {
  62.          case '\n':
  63.              putc1('\r', obuf);
  64.             putc1(c,obuf);
  65.             column = scount = 0;
  66.         case 0:
  67.         case 0177:
  68.         case '\r':
  69.             break;
  70.         case ' ':
  71.             column++; scount++;
  72.             if (!(column%tabwidth)) {
  73.                 if (scount > 1)
  74.                     putc1('\t',obuf);
  75.                 else
  76.                     putc1(' ',obuf);
  77.                 scount = 0;
  78.             }
  79.             break;
  80.         case '\t':
  81.             scount = 0;
  82.             column += (tabwidth-column%tabwidth);
  83.             putc1('\t',obuf);
  84.             break;
  85.         case '"':
  86.             putc1('"',obuf);
  87.             do {
  88.                 lastc=c;
  89.                 c = getc(ibuf);
  90.                 if (c == ERROR || c==CPMEOF) {
  91.                     puts("Quote error.");
  92.                     break;
  93.                 }
  94.                 putc1(c&=0177,obuf);
  95.             } while (c != '"' && (c != '\n' || lastc=='\\'));
  96.             while (c != '\n') {
  97.                 c = getc(ibuf);
  98.                 if(c==EOF || c==CPMEOF)
  99.                     break;
  100.                 putc1(c&=0177,obuf);
  101.             }
  102.             column = scount = 0;
  103.             break;
  104.         case 0x1a:
  105.             break;
  106.         default:
  107.             for (i=0; i<scount; i++)
  108.                 putc1(' ',obuf);
  109.             scount = 0;
  110.             /* don't count spaces for control characters */
  111.             if(c >= ' ')
  112.                 column++;
  113.             putc1 (c,obuf);
  114.         }
  115.     } while (c != CPMEOF && c != EOF);
  116.  
  117.     putc1(CPMEOF, obuf);
  118.     fflush(obuf);
  119.     fclose(ibuf);
  120.     fclose(obuf);
  121. }
  122.  
  123. putc1(c,buf)
  124. char c;
  125. {
  126. /*
  127.     putchar(c);
  128. */
  129.     if (putc(c,buf) < 0) {
  130.         puts("putc just retured an error!");
  131.         exit();
  132.     }
  133. }
  134.  
  135. e
  136.                     putc1(' ',obuf);
  137.                 scount =