home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / trn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-11  |  1.8 KB  |  66 lines

  1. /*
  2. ** trn.c -- map characters
  3. **
  4. ** Copyright 1982 J. E. Hendrix.  All rights reserved.
  5. */
  6. #include <stdio.h>
  7. #include "tools.h"
  8. #define NOCCARGC
  9. #define MAXARG 80
  10. #define MAXSET 127
  11. main(argc, argv) int argc, *argv; {
  12.   char arg[MAXARG+1], c, from[MAXSET+1], to[MAXSET+1];
  13.   int allbut, collap, i, lastto, error;
  14.   auxbuf(stdin, 4096);
  15.   error=NO;
  16.   if(getarg(1, arg, MAXARG, argc, argv)==EOF) error=YES;
  17.   if(((arg[0]=='-')&(arg[1]<=' '))|(error==YES)) {
  18.     fputs("usage: TRN [~]from [to]\n", stderr);
  19.     abort(7);
  20.     }
  21.   if(arg[0]==NOT) {
  22.     allbut=YES;
  23.     if(makset(arg, 1, from, MAXSET)==NO)
  24.       error("from-list too large");
  25.     }
  26.   else {
  27.     allbut=NO;
  28.     if(makset(arg, 0, from, MAXSET)==NO)
  29.       error("from-list too large");
  30.     }
  31.   if(getarg(2, arg, MAXARG, argc, argv)==EOF) to[0]=NULL;
  32.   else if(makset(arg, 0, to, MAXSET)==NO)
  33.     error("to-list too large");
  34.   lastto=strlen(to)-1;
  35.   if((strlen(from)>(lastto+1))|(allbut==YES)) collap=YES;
  36.   else collap=NO;
  37.   while(1) {
  38.     poll(YES);
  39.     i=xindex(from, c=fgetc(stdin), allbut, lastto);
  40.     if((collap==YES)&(i>=lastto)&(lastto>-1)) { /* collapse */
  41.       if(fputc(to[lastto], stdout)==EOF)
  42.         error("output error");
  43.       while(1) {
  44.         i=xindex(from, c=fgetc(stdin), allbut, lastto);
  45.         if(i<lastto) break;
  46.         }
  47.       }
  48.     if(c==EOF) break;
  49.     if((i>-1)&(lastto>-1)) {            /* translate */
  50.       if(fputc(to[i], stdout)==EOF)
  51.         error("output error");
  52.       }
  53.     else if(i<0) {                       /* copy */
  54.       if(fputc(c, stdout)==EOF)
  55.         error("output error");
  56.       }
  57.     }                                    /* delete */
  58.   fclose(stdout);
  59.   }
  60. #include "pat.c"
  61. #include "error.c"
  62. #include "index.c"
  63. #include "xindex.c"
  64. #include "makset.c"
  65.  
  66.