home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s2 / tr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  2.3 KB  |  145 lines

  1. int dflag 0;
  2. int sflag 0;
  3. int cflag 0;
  4. int save 0;
  5. char code[256];
  6. char squeez[256];
  7. char vect[256];
  8. struct string { int last, max, rep; char *p; } string1, string2;
  9. int inbuf[259];
  10.  
  11. main(argc,argv)
  12. char **argv;
  13. {
  14.     int i, j;
  15.     int c, d;
  16.     char *compl;
  17.     extern fout;
  18.  
  19.     string1.last = string2.last = 0;
  20.     string1.max = string2.max = 0;
  21.     string1.rep = string2.rep = 0;
  22.     string1.p = string2.p = "";
  23.  
  24.     if(--argc>0) {
  25.         argv++;
  26.         if(*argv[0]=='-'&&argv[0][1]!=0) {
  27.             while(*++argv[0])
  28.                 switch(*argv[0]) {
  29.                 case 'c':
  30.                     cflag++;
  31.                     continue;
  32.                 case 'd':
  33.                     dflag++;
  34.                     continue;
  35.                 case 's':
  36.                     sflag++;
  37.                     continue;
  38.                 }
  39.             argc--;
  40.             argv++;
  41.         }
  42.     }
  43.     if(argc>0) string1.p = argv[0];
  44.     if(argc>1) string2.p = argv[1];
  45.     for(i=0; i<256; i++)
  46.         code[i] = vect[i] = 0;
  47.     if(cflag) {
  48.         while(c = next(&string1))
  49.             vect[c&0377] = 1;
  50.         j = 0;
  51.         for(i=1; i<256; i++)
  52.             if(vect[i]==0) vect[j++] = i;
  53.         vect[j] = 0;
  54.         compl = vect;
  55.     }
  56.     for(i=0; i<256; i++)
  57.         squeez[i] = 0;
  58.     for(;;){
  59.         if(cflag) c = *compl++;
  60.         else c = next(&string1);
  61.         if(c==0) break;
  62.         d = next(&string2);
  63.         if(d==0) d = c;
  64.         code[c&0377] = d;
  65.         squeez[d&0377] = 1;
  66.     }
  67.     while(d = next(&string2))
  68.         squeez[d&0377] = 1;
  69.     squeez[0] = 1;
  70.     for(i=0;i<256;i++) {
  71.         if(code[i]==0) code[i] = i;
  72.         else if(dflag) code[i] = 0;
  73.     }
  74.  
  75.     inbuf[0] = 0;
  76.     fout = dup(1);
  77.     close(1);
  78.     while((c=getc(inbuf)) >=0 ) {
  79.         if(c == 0) continue;
  80.         if(c = code[c&0377]&0377)
  81.             if(!sflag || c!=save || !squeez[c&0377])
  82.                 putchar(save = c);
  83.     }
  84.     flush();
  85. }
  86.  
  87. next(s)
  88. struct string *s;
  89. {
  90.     int a, b, c, n;
  91.     int base;
  92.  
  93.     if(--s->rep > 0) return(s->last);
  94.     if(s->last < s->max) return(++s->last);
  95.     if(*s->p=='[') {
  96.         nextc(s);
  97.         s->last = a = nextc(s);
  98.         s->max = 0;
  99.         switch(nextc(s)) {
  100.         case '-':
  101.             b = nextc(s);
  102.             if(b<a || *s->p++!=']')
  103.                 goto error;
  104.             s->max = b;
  105.             return(a);
  106.         case '*':
  107.             base = (*s->p=='0')?8:10;
  108.             n = 0;
  109.             while((c = *s->p)>='0' && c<'0'+base) {
  110.                 n = base*n + c - '0';
  111.                 s->p++;
  112.             }
  113.             if(*s->p++!=']') goto error;
  114.             if(n==0) n = 1000;
  115.             s->rep = n;
  116.             return(a);
  117.         default:
  118.         error:
  119.             write(1,"Bad string\n",11);
  120.             exit();
  121.         }
  122.     }
  123.     return(nextc(s));
  124. }
  125.  
  126. nextc(s)
  127. struct string *s;
  128. {
  129.     int c, i, n;
  130.  
  131.     c = *s->p++;
  132.     if(c=='\\') {
  133.         i = n = 0;
  134.         while(i<3 && (c = *s->p)>='0' && c<='7') {
  135.             n = n*8 + c - '0';
  136.             i++;
  137.             s->p++;
  138.         }
  139.         if(i>0) c = n;
  140.         else c = *s->p++;
  141.     }
  142.     if(c==0) *--s->p = 0;
  143.     return(c&0377);
  144. }
  145.