/* tr - like unix tr ** THIS PROGRAM MUST BE COMPILED WITH THE EXTENDED PREPROCESSOR ** IE: cc TR -p by jim mckim this program may be used in several ways. By example: tr A-Z a-z output will convert upper case to lower case tr -c !-~ ? output will convert unprintable characters to question marks tr -cs !-~ \40 output will convert unprintable characters to spaces and squeeze out extra spaces tr -d \177-\377 #include #define TTSIZE 257 #define isodigit(c) ((c)>='0' && (c)<='7') TellHow() { fputs("usage: tr [-flags] string1 string2 output\n", stderr); fputs("where flags are:\n",stderr); fputs("c compliment string1 (e.g. all NOT in string1)\n",stderr); fputs("d delete if in string1\n",stderr); fputs("s squeeze out duplicates if in string2\n",stderr); fputs("r raw (no cr/lf conversion) (MSDOS only)\n",stderr); exit(-1); } Interpret(input,output) char *input, *output; { int startc, endc; int i; int range_flag; /* true when '-' has been seen */ int got_next; /* true when char after '-' has been seen */ *output = range_flag = got_next = 0; while (*input) { if (*input == '\\') { ++input; if (isodigit(*input)) { /* got an octal constant */ i = *input - '0'; if (isodigit(*(input+1))) { i = i*8 + *++input - '0'; if (isodigit(*(input+1))) i = i*8 + *++input - '0'; } *output++ = i; if (range_flag) got_next = 1; } else { /* got a \c type character */ *output++ = *input; if (range_flag) got_next = 1; } } else if (*input == '-') { /* got a range */ range_flag = 1; } else { /* got a normal character */ *output++ = *input; if (range_flag) got_next = 1; } ++input; if (range_flag && got_next) { startc = *(output-2); endc = *(output-1); output -= 2; for (i=startc; i<=endc; ++i) *output++ = i; range_flag = got_next = 0; } } /* end while */ *output = '\0'; } main(argc,argv) int argc; char *argv[]; { char incode[TTSIZE], outcode[TTSIZE], trtbl[TTSIZE]; char inset[TTSIZE]; char inch, outch, old_outch; int i, j, iarg; char c_flag, d_flag, s_flag; char *iap; iarg = 1; c_flag = d_flag = s_flag = 0; /* get the switches */ while ((iarg <= argc) && (*argv[iarg] == '-')) { iap = argv[iarg++] + 1; /* point past '-' */ while (*iap) switch (*iap++) { case 'c': /* Compliment string */ c_flag = 1; break; case 'd': /* Delete */ d_flag = 1; break; case 's': /* Squeeze out duplicates */ s_flag = 1; break; case 'r': /* Raw I/O mode */ *stdin |= F_BINARY; *stdout |= F_BINARY; break; default: TellHow(); } } if (iarg+1 >= argc) { TellHow(); } for (i=0; i