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

  1.  
  2. /*
  3. ** chg.c -- change occurrences of "from" to "to"
  4. **
  5. ** Copyright 1982 J. E. Hendrix.  All rights reserved.
  6. */
  7. #include <stdio.h>
  8. #include "tools.h"
  9. #define NOCCARGC
  10. #define MAXARG 49
  11. #define MAXLIN1 (MAXLINE+1)
  12. char lin[MAXLIN1], new[MAXLIN1], pat[MAXPAT], sub[MAXPAT];
  13. char arg[MAXARG];
  14. int i, k, lastn, n;
  15. main(argc, argv) int argc, *argv; {
  16.   if((getarg(1, arg, MAXARG, argc, argv)==EOF)
  17.     |((arg[0]=='-')&(arg[1]==0)))
  18.     error("usage: CHG pattern [replacement]\n");
  19.   if(makpat(arg, 0, NULL, pat)==ERR)
  20.     error("pattern too long\n");
  21.   if(getarg(2, arg, MAXARG, argc, argv)==EOF)
  22.     arg[0]=NULL;
  23.   if(maksub(arg, 0, NULL, sub)==ERR)
  24.     error("replacement too long\n");
  25.   auxbuf(stdin, 4096);  /** alloc aux buffer to stdin **/
  26.   while(fgets(lin, MAXLIN1, stdin)!=NULL) {
  27.     poll(YES);
  28.     lastn = -1;
  29.     i=0;  k=0;
  30.     trim(lin);
  31.     while(YES) {
  32.       n=amatch(lin, i, pat);
  33.       if((n>=0)&(lastn!=n)) {   /** replace matched text **/
  34.         catsub(lin, i, n, sub, new, &k, MAXLIN1);
  35.         lastn=n;
  36.         }
  37.       if(lin[i]==NULL) break;
  38.       if((n==-1)|(n==i)) {      /** no match or null match **/
  39.         addset(lin[i], new, &k, MAXLIN1);
  40.         ++i;
  41.         }
  42.       else i=n;                 /** skip matched text **/
  43.       }
  44.     if(addset(NULL, new, &k, MAXLIN1)==NO) {
  45.       k=MAXLIN1-1;
  46.       addset(NULL, new, &k, MAXLIN1);
  47.       sout("\7line truncated: ", stderr);
  48.       lout(new, stderr);
  49.       }
  50.     lout(new, stdout);
  51.     }
  52.   fclose(stdout);
  53.   }
  54.  
  55. #include "pat.c"
  56. #include "maksub.c"
  57. #include "catsub.c"
  58. #include "index.c"
  59. #include "error.c"
  60. #include "out.c"
  61. #include "trim.c"
  62.  
  63.