xref: /plan9/sys/src/cmd/upas/unesc/unesc.c (revision 7ad596ff0f87e7b1bf4052ceeca38bcec4a5e832)
1 /*
2  *	upas/unesc - interpret =?foo?bar?=char?= escapes
3  */
4 #include <u.h>
5 #include <libc.h>
6 #include <bio.h>
7 
8 int
hex(int c)9 hex(int c)
10 {
11 	if('0' <= c && c <= '9')
12 		return c - '0';
13 	if('a' <= c && c <= 'f')
14 		return c - 'a' + 10;
15 	if('A' <= c && c <= 'F')
16 		return c - 'A' + 10;
17 	return 0;
18 }
19 
20 void
main(void)21 main(void)
22 {
23 	int c;
24 	Biobuf bin, bout;
25 
26 	Binit(&bin,  0, OREAD);
27 	Binit(&bout, 1, OWRITE);
28 	while((c = Bgetc(&bin)) != Beof)
29 		if(c != '=')
30 			Bputc(&bout, c);
31 		else if((c = Bgetc(&bin)) != '?'){
32 			Bputc(&bout, '=');
33 			Bputc(&bout, c);
34 		} else {
35 			while((c = Bgetc(&bin)) != Beof && c != '?')
36 				continue;		/* consume foo */
37 			while((c = Bgetc(&bin)) != Beof && c != '?')
38 				continue;		/* consume bar */
39 			while((c = Bgetc(&bin)) != Beof && c != '?'){
40 				if(c == '='){
41 					c  = hex(Bgetc(&bin)) << 4;
42 					c |= hex(Bgetc(&bin));
43 				}
44 				Bputc(&bout, c);
45 			}
46 			c = Bgetc(&bin);		/* consume '=' */
47 			if (c != '=')
48 				Bungetc(&bin);
49 		}
50 	Bterm(&bout);
51 	exits(0);
52 }
53