home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / ut-c.lbr / TR.CZ / TR.C
Encoding:
C/C++ Source or Header  |  1993-10-25  |  3.4 KB  |  125 lines

  1. /* tr.c -- UTOOL. Transliterate characters
  2.  
  3.      author: David H. Wolen
  4.      last change: 12/31/82
  5.  
  6.      usage:    tr src dest
  7.                tr #A-Z# a-z   (change case)
  8.                tr " \t\n" \n  (make one word per line)
  9.                tr !\n         (delete everything but newlines)
  10.                tr 0-9 9       (change digit strings to single digit 9)
  11.  
  12.  
  13.      input:    STDIN
  14.      output:   STDOUT
  15.  
  16.      notes:    (1) - denotes range
  17.                (2) ! denotes "all but"
  18.                (3) \n (newline) \t (tab) \b (backspace) \r (carr ret)
  19.                    \f (form feed) \p (#) \q (")
  20.                (4) if dest is absent, all chars represented by src are
  21.                    deleted
  22.                (5) if dest is shorter than src, all chars of src that 
  23.                    would map to or beyond last char in dest are mapped
  24.                    to last char in dest.  Adjacent instances of such 
  25.                    chars in src cause a single instance of last char
  26.                    in dest to be output.
  27.                (6) use " " to enclose blanks in src and dest
  28.                (7) use # # to enclose capital letters in src and dest
  29.  
  30.  
  31.      linkage:  a:clink tr -f dio -ca (uses deff3.crl)
  32. */
  33.  
  34. #include "a:bdscio.h"
  35. #include "dio.h"
  36.  
  37. #define  NEGATE '!'
  38. #define  ESCAPE '\\'
  39.  
  40. main(argc,argv)
  41. int  argc;
  42. char *argv[];
  43. {
  44.      int  c, i, lastto, allbut, squash;
  45.      char arg[MAXLINE], fromset[MAXLINE], toset[MAXLINE];
  46.  
  47.      dioinit(&argc,argv);
  48.  
  49.      if(argc <= 1)  error("usage: tr from to");
  50.  
  51.      rcarg(arg,*++argv);
  52.  
  53.      allbut=(arg[0] == NEGATE);
  54.      if(allbut)
  55.           i=1;
  56.      else
  57.           i=0;
  58.  
  59.      if(!makset(arg,i,fromset,MAXLINE))
  60.           error("tr: from set too large");
  61.  
  62.      if(argc == 2)       /* empty toset */
  63.           toset[0]='\0';
  64.      else
  65.           {rcarg(arg,*++argv);
  66.           if(!makset(arg,0,toset,MAXLINE))
  67.                error("tr: to set too large");
  68.           }
  69.  
  70.      if(strlen(fromset) < strlen(toset))
  71.           error("tr: from shorter than to");
  72.  
  73.      lastto=strlen(toset)  -1;
  74.      squash=(strlen(fromset) > lastto +1) || allbut;
  75.  
  76.      do
  77.           {i=xindex(fromset,(c=getchar()),allbut,lastto);
  78.           if(squash && i >= lastto && lastto >= 0)
  79.                {putchar(toset[lastto]);
  80.                do
  81.                     i=xindex(fromset,(c=getchar()),allbut,lastto);
  82.                     while(i >= lastto);
  83.                }
  84.           if(c != EOF)
  85.                {if(i >= 0 && lastto >= 0)    /* translate */
  86.                     putchar(toset[i]);
  87.                else if(i < 0)                /* copy */
  88.                     putchar(c);
  89.                }                             /* else delete */
  90.           }
  91.           while(c != EOF);
  92.  
  93.      dioflush();
  94. }
  95.  
  96.  
  97.  
  98. /* makset -- make set from inset[k] in outset. Returns FALSE for no room */
  99. makset(inset,k,outset,maxset)
  100. char *inset, *outset;
  101. int  k, maxset;
  102. {
  103.      int  j, kk;
  104.  
  105.      j=0;
  106.      kk=k;
  107.      dodash('\0',inset,&kk,outset,&j,maxset);
  108.      return(addstr('\0',outset,&j,maxset));
  109. }
  110.  
  111.  
  112.  
  113. /* xindex -- conditionally invert value from cindex */
  114. xindex(inset,c,allbut,lastto)
  115. char *inset;
  116. int  allbut, lastto, c;
  117. {
  118.      if(c == EOF)  return(-1);
  119.      if(!allbut)  return(cindex(inset,c));
  120.  
  121.      if(cindex(inset,c) >= 0)  return(-1);
  122.      else
  123.           return(lastto+1);
  124. }
  125.