4 typedef struct PCB /* Control block controlling specification parse */
6 char *base; /* start of specification */
7 char *current; /* current parse point */
8 long last; /* last Rune returned */
9 long final; /* final Rune in a span */
12 uchar bits[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
14 #define SETBIT(a, c) ((a)[(c)/8] |= bits[(c)&07])
15 #define CLEARBIT(a,c) ((a)[(c)/8] &= ~bits[(c)&07])
16 #define BITSET(a,c) ((a)[(c)/8] & bits[(c)&07])
18 #define MAXRUNE 0xFFFF
20 uchar f[(MAXRUNE+1)/8];
21 uchar t[(MAXRUNE+1)/8];
31 void complement(void);
37 char *getrune(char*, Rune*);
38 void Pinit(Pcb*, char*);
40 int readrune(int, long*);
42 void writerune(int, Rune);
45 main(int argc, char **argv)
48 case 's': sflag++; break;
49 case 'd': dflag++; break;
50 case 'c': cflag++; break;
51 default: error("bad option");
54 Pinit(&pfrom, argv[0]);
60 if ((sflag && argc != 2) || (!sflag && argc != 1))
79 memset((char *) f, 0xff, sizeof f);
80 while ((c = canon(&pfrom)) >= 0)
83 while ((c = canon(&pfrom)) >= 0)
87 while ((c = canon(&pto)) >= 0)
92 while (readrune(0, &c) > 0) {
93 if(!BITSET(f, c) && (c != last || !BITSET(t,c))) {
95 writerune(1, (Rune) c);
106 long from, to, lastc, high;
110 while ((from = canon(&pfrom)) >= 0) {
111 if (from > high) high = from;
114 while ((to = canon(&pto)) > 0) {
115 if (to > high) high = to;
119 if ((p = (Rune *) malloc((high+1)*sizeof(Rune))) == 0)
120 error("can't allocate memory");
121 for (i = 0; i <= high; i++){
123 if ((to = canon(&pto)) < 0)
132 while (readrune(0, &from) > 0) {
137 if (from != lastc || !BITSET(t,from)) {
139 writerune(1, (Rune) from);
144 while (readrune(0, &from) > 0){
149 writerune(1, (Rune) from);
160 long from, to, lastc, high;
164 while ((from = canon(&pfrom)) >= 0)
165 if (from > high) high = from;
167 if ((p = (Rune *) malloc((high+1)*sizeof(Rune))) == 0)
168 error("can't allocate memory");
169 for (i = 0; i <= high; i++)
171 while ((from = canon(&pfrom)) >= 0) {
172 if ((to = canon(&pto)) < 0)
175 if (BITSET(f,from) && p[from] != to)
176 error("ambiguous translation");
181 while ((to = canon(&pto)) >= 0) {
186 while (readrune(0, &from) > 0) {
189 if (from != lastc || !BITSET(t,from)) {
191 writerune(1, (Rune) from);
196 while (readrune(0, &from) > 0) {
199 writerune(1, (Rune) from);
206 readrune(int fd, long *rp)
211 static char buf[4096];
218 memcpy(buf, buf+j, n-j);
220 n = read(fd, &buf[i], sizeof(buf)-i);
229 if (fullrune(&buf[j], i-j))
232 chartorune(&r, &buf[j]);
238 writerune(int fd, Rune r)
245 n = runetochar(buf, (Rune*)&r);
246 if (wptr+n >= wbuf+sizeof(wbuf))
248 memcpy(wptr, buf, n);
255 if (wptr && wptr > wbuf)
256 if (write(fd, wbuf, wptr-wbuf) != wptr-wbuf)
257 error("write error");
262 getrune(char *s, Rune *rp)
268 s += chartorune(rp, s);
269 if((r = *rp) == '\\' && *s){
273 for (i = 0; i < 4; i++) {
275 s += chartorune(&r, s);
276 if ('0' <= r && r <= '9')
278 else if ('a' <= r && r <= 'f')
279 n = 16*n + r - 'a' + 10;
280 else if ('A' <= r && r <= 'F')
281 n = 16*n + r - 'A' + 10;
290 for(i = 0; i < 3; i++) {
292 s += chartorune(&r, s);
293 if('0' <= r && r <= '7')
319 if (p->last < p->final)
323 if (*p->current == '\0')
325 if(*p->current == '-' && p->last >= 0 && p->current[1]){
326 p->current = getrune(p->current+1, &r);
328 error ("Invalid range specification");
334 p->current = getrune(p->current, &r);
340 Pinit(Pcb *p, char *cp)
342 p->current = p->base = cp;
343 p->last = p->final = -1;
348 p->current = p->base;
349 p->last = p->final = -1;
354 fprint(2, "%s: %s\n", argv0, s);