home *** CD-ROM | disk | FTP | other *** search
- /* s - change string1 into string2
- * usage: s string1 [string2]
- * special chars : / - (by itself) null
- * ? - question mark matches any char
- * @ matches any letter
- * # matches any number (0-9)
- * !@ or !# means not a letter (or number)
- * ^ matches first char on a line.
- * $ matches last char on a line (newline).
- * /c character c
- * %c control character c
- * /n - newline
- * /t - tab
- * /f - form feed
- * /" - single quote (usually stripped by dos)
- * if string 2 is null, string 1 is removed from the input
- * ex:
- * s abc /n/ - changes all occurences of 'abc' into
- * a newline followed by a space
- * s "abc" " /n " does the same thing.
- * s c?t r?t changes cat to rat, cot to rot, etc.
- * s ?@# #?@ changes +A4 to 4+A , and DA2 to D2A, etc.
- * added control characters ver 4.0
- * Fri Aug 21 11:38:28 1987
- */
-
- #include <ctype.h>
-
- #include <stdio.h>
- #include <copywrit.h>
- #define VER "4.0\0"
- #define WILD '\376' /* ascii 254 */
- #define NUMBER '\375'
- #define LETTER '\374'
- #define NOTLETTER '\373'
- #define NOTNUMBER '\372'
- #define BEGINLINE '\371'
- #define ENDLINE '\370'
- #define STRMAX 255
- #define FALSE 0
- #define TRUE !FALSE
- #define NUMWILD 7
- /* globals */
- int i,j=0; char c;
- int numwild1,numwild2;
- char from[STRMAX], to[STRMAX]; char temp[STRMAX];
- /* command line parsing routine */
- /* sinput - change /n into newline, etc */
- /* also count wildcard chars */
- sinput(s)
- char s[];
- {
-
- char t[STRMAX]; /* better not be a string longer than STRMAX */
- int i=0; int j=0; int flag;
- int numwild=0;
- while( i < strlen(s))
- {
- flag=FALSE;
- if (i>= strlen(s)) break;
- /* handle negation op */
- if (s[i]=='!' && i<strlen(s))
- /* negation operator */
- {
- switch (s[++i])
- {
- case '#' :t[j++]=NOTNUMBER;i++;numwild++ ;break;
- case '@' :t[j++]=NOTLETTER;i++;numwild++; break;
-
- default: fprintf(stderr,"sinput: ! ignored\n");
- break;
-
- }
- }
- /* handle control chars */
- if (s[i]=='%' && i<strlen(s))
- {
- t[j++] = s[++i] - 'A'+1; /* make into control character */
- t[j] = NULL;
- i++; /* ready for next char */
- continue;
- }
- if (s[i]!='!')
- {
- if (s[i]=='?')
- { t[j++]=WILD; t[j]=NULL; numwild++ ; i++; }
- /* beginline */
- else if (s[i]=='^')
- { t[j++]=BEGINLINE; t[j]=NULL; numwild++ ; i++; }
- /* endline - any char before a newline */
- else if (s[i]=='$')
- { t[j++]=ENDLINE; t[j]=NULL; numwild++; i++; }
- else /* letter wildcard */
- if (s[i]=='@')
- { t[j++]=LETTER; t[j]=NULL; numwild++ ; i++; }
- else
- if (s[i]=='#')
- { t[j++]=NUMBER; t[j]=NULL; numwild++ ; i++; }
- else
- if (s[i] != '/') { t[j++]=s[i++]; t[j]=NULL; }
- else
- {
- flag=(s[i]=='/');
- if (flag)
- {
- i++;
- switch (s[i]) {
- case 'n' : { t[j]='\n';j++; i++; } break;
- case 't' : { t[j]='\t';j++; i++; } break;
- case 'f' : { t[j]='\014';j++; i++; } break;
- case NULL: { t[j]=' '; j++; i++; } break;
- default : { t[j]=s[i]; j++; i++; } break;
- }
-
- }
- t[j]=NULL;
- }
- }
- }
- strncpy(s,t,strlen(t));
- s[strlen(t)]=NULL;
- /* printf("%s\n",s); */
- return(numwild);
- }
- /* skip to the r+1th wildcard of type w in s */
- skip(r,s,w)
- int r; char s[]; char w;
- {
- int found;
- int cnt=0,i, j;
- if (r>numwild2) r=0;
- found=0;
- j=0;
- while (j<=r)
- {
- for (i=0; i<strlen(s); i++)
- {
- if (s[i]==w)
- { found=1;
- j++;
- }
- if (j>r) break;
- }
- if (!found) return(-1);
- }
-
- return(i);
- }
- /* firstflag is true if this is the first char read. */
- int firstflag=1;
-
- ismatch(c,oldc,d)
- char c; char oldc; char d;
- { /* see if its an endline (non-newline before a newline) */
- if (d==ENDLINE && (!firstflag && c=='\n' && oldc !='\n'))
- return(1);
- if (d==BEGINLINE &&
- ((c !='\n' && oldc == '\n') || firstflag)) return(1);
- return(c==d | d==WILD | (d==LETTER && isalpha(c))
- | (d==NUMBER && isdigit(c)) |(d==NOTLETTER && !isalpha(c))
- | (d==NOTNUMBER && !isdigit(c)));
-
- }
-
- iswild(c)
- char c;
- {
- return(c==WILD | c==LETTER | c==NUMBER
- | c==NOTNUMBER | c==NOTLETTER | c==BEGINLINE | c==ENDLINE);
- }
- /* sum the first n elts of r */
-
- sum(q,n)
- int q[]; int n;
- { int j,s;
- s=0;
- for (j=1; j<=n;j++)
- s += q[j];
- return(s);
- }
- replace(i)
- int i;
- { int j=0,p; int q[NUMWILD]; int k=0; int f;
- int index;
- /* q1 = # of '?' repaced
- * q2 = # of '@' replaced
- * q3 = # of '#' replaced
- * q4 = # of '!@'(not letter)
- * q5 = # of '!#'(not number)
- * q6 = # of '^' (beginline)
- * q7 = # of '$' (endline)
- */
-
- for (j=0;j<=NUMWILD;j++)
- {
- q[j]=0;
- }
- f=0;
- for (j=0; j < strlen(to) ; j++)
-
- if (iswild(to[j]) && (sum(q,NUMWILD) < numwild2))
- { /* if we found a matching wildcard , replace */
- /* it, otherwise delete the wild char from 'to' */
- index=0;
- switch (to[j])
- {
- case WILD : index=1; break;
- case LETTER: index=2; break;
- case NUMBER: index=3; break;
- case NOTLETTER: index=4; break;
- case NOTNUMBER: index=5; break;
- case BEGINLINE: index=6; break;
- case ENDLINE: index=7; break;
- }
- k=skip(q[index],from,to[j]); q[index]++;
- if (k>=0) to[j]=temp[k];
- else /* remove the non-matching wild char */
- for (k=j-- ; k< (strlen(to)); to[k]=to[k+1],
- k++);
- if (sum(q,NUMWILD)>=numwild2) break;
- /* now done with translation */
- }
- }
-
- /* the firstflag flag is global */
- translat()
- {
- char tmp[STRMAX];
- int i,j,k,l;
- char c=' ';
- char oldc; /* last char read */
- int flag=1; int lastmatch;
- i=0; oldc=c;
- while (c != EOF)
- {
- lastmatch=0;
- strncpy(tmp,to,strlen(to)); tmp[strlen(to)]=NULL;
- if (flag) c=getchar(); else flag=1;
- if (c==EOF) {
- printf("%s",temp);
- break;
- }
- temp[i]=c; temp[i+1]=NULL;
-
- if (ismatch(c,oldc,from[i]))
- { /* first see if there is a matching 'to' wildcard */
- /* and replace it */
- replace(i);
- lastmatch=1;
- i++;
- if (i == strlen(from)) { i=0;
- printf("%s",to); flag=1;}
- }
- else
-
- /* not a match. see if it matches first char. */
- { /* at least one char has matched so far */
- if ((ismatch(c,oldc,from[0])) && i>0 )
- { flag=0; temp[i]=NULL;
- }
- printf("%s",temp);
- i=0;
- }
- strncpy(to,tmp,strlen(tmp));
- if (firstflag==1) firstflag=0;
- oldc=c;
- }
- }
- main(argc,argv)
-
- int argc;
- char *argv[];
- { char *copyright = COPYRIGHT;
-
- fprintf(stderr,"%s\n",copyright);
- if (argc<2)
- {
- printf("usage - %s [pattern1 pattern2]\n",argv[0]);
- printf("vers %s\n",VER);
- exit(0);
- }
- i=strlen(argv[1]);
- strncpy(from,argv[1],i);
- from[i]=NULL;
-
- numwild1=sinput(from);
- if (argc==2) /* replace from by null */
- to[0]=NULL;
-
- else {
- i=strlen(argv[2]);
- strncpy(to,argv[2],i);
- to[i]=NULL;
- numwild2 = sinput(to);
- }
- /* printf("%d and %d ?\n",numwild1,numwild2); */
- translat();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-