summaryrefslogtreecommitdiff
path: root/appl/cmd/tr.b
blob: 202ace53b2dfa7a89563944f0ab2fb515a4a13f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
implement Tr;

include "sys.m";
	sys: Sys;

include "draw.m";

include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "arg.m";
	arg: Arg;

Tr: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

Pcb: adt {	# Control block controlling specification parse
	spec: string;	# specification string
	end:	int;	# its length
	current:	int;	# current parse point
	last:	int;	# last Rune returned
	final:	int;	# final Rune in a span

	new:	fn(nil: string): ref Pcb;
	rewind:	fn(nil: self ref Pcb);
	getc:	fn(nil: self ref Pcb): int;
	canon:	fn(nil: self ref Pcb): int;
};

bits := array [] of { byte 1, byte 2, byte 4, byte 8, byte 16, byte 32, byte 64, byte 128 };

SETBIT(a: array of byte, c: int)
{
	a[c>>3] |= bits[c & 7];
}

CLEARBIT(a: array of byte, c: int)
{
	a[c>>3] &= ~bits[c & 7];
}

BITSET(a: array of byte, c: int): int
{
	return int (a[c>>3] & bits[c&7]);
}

MAXRUNE: con 16rFFFF;

f := array[(MAXRUNE+1)/8] of byte;
t := array[(MAXRUNE+1)/8] of byte;

pto, pfrom: ref Pcb;

cflag := 0;
dflag := 0;
sflag := 0;
stderr: ref Sys->FD;

ib: ref Iobuf;
ob: ref Iobuf;

usage()
{
	sys->fprint(stderr, "Usage: tr [-sdc] [from-set [to-set]]\n");
	raise "fail: usage";
}

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);
	bufio = load Bufio Bufio->PATH;
	arg = load Arg Arg->PATH;
	arg->init(args);
	while((c := arg->opt()) != 0)
		case c {
		's' => sflag = 1;
		'd' => dflag = 1;
		'c' => cflag = 1;
		* => usage();
	}
	args = arg->argv();
	argc := len args;
	if(args != nil){
		pfrom = Pcb.new(hd args);
		args = tl args;
	}
	if(args != nil){
		pto = Pcb.new(hd args);
		args = tl args;
	}
	if(args != nil)
		usage();
	ib = bufio->fopen(sys->fildes(0), Bufio->OREAD);
	ob = bufio->fopen(sys->fildes(1), Bufio->OWRITE);
	if(dflag) {
		if(sflag && argc != 2 || !sflag && argc != 1)
			usage();
		delete();
	} else {
		if(argc != 2)
			usage();
		if(cflag)
			complement();
		else
			translit();
	}
	if(ob.flush() == Bufio->ERROR)
		error(sys->sprint("write error: %r"));
}

delete()
{
	if (cflag) {
		for(i := 0; i < len f; i++)
			f[i] = byte 16rFF;
		while ((c := pfrom.canon()) >= 0)
			CLEARBIT(f, c);
	} else {
		while ((c := pfrom.canon()) >= 0)
			SETBIT(f, c);
	}
	if (sflag) {
		while ((c := pto.canon()) >= 0)
			SETBIT(t, c);
	}

	last := MAXRUNE+1;
	while ((c := ib.getc()) >= 0) {
		if(!BITSET(f, c) && (c != last || !BITSET(t,c))) {
			last = c;
			ob.putc(c);
		}
	}
}

complement()
{
	lastc := 0;
	high := 0;
	while ((from := pfrom.canon()) >= 0) {
		if (from > high)
			high = from;
		SETBIT(f, from);
	}
	while ((cto := pto.canon()) >= 0) {
		if (cto > high)
			high = cto;
		SETBIT(t,cto);
	}
	pto.rewind();
	p := array[high+1] of int;
	for (i := 0; i <= high; i++){
		if (!BITSET(f,i)) {
			if ((cto = pto.canon()) < 0)
				cto = lastc;
			else
				lastc = cto;
			p[i] = cto;
		} else
			p[i] = i;
	}
	if (sflag){
		lastc = MAXRUNE+1;
		while ((from = ib.getc()) >= 0) {
			if (from > high)
				from = cto;
			else
				from = p[from];
			if (from != lastc || !BITSET(t,from)) {
				lastc = from;
				ob.putc(from);
			}
		}
	} else {
		while ((from = ib.getc()) >= 0){
			if (from > high)
				from = cto;
			else
				from = p[from];
			ob.putc(from);
		}
	}
}

translit()
{
	lastc := 0;
	high := 0;
	while ((from := pfrom.canon()) >= 0)
		if (from > high)
			high = from;
	pfrom.rewind();
	p := array[high+1] of int;
	for (i := 0; i <= high; i++)
		p[i] = i;
	while ((from = pfrom.canon()) >= 0) {
		if ((cto := pto.canon()) < 0)
			cto = lastc;
		else
			lastc = cto;
		if (BITSET(f,from) && p[from] != cto)
			error("ambiguous translation");
		SETBIT(f,from);
		p[from] = cto;
		SETBIT(t,cto);
	}
	while ((cto := pto.canon()) >= 0)
		SETBIT(t,cto);
	if (sflag){
		lastc = MAXRUNE+1;
		while ((from = ib.getc()) >= 0) {
			if (from <= high)
				from = p[from];
			if (from != lastc || !BITSET(t,from)) {
				lastc = from;
				ob.putc(from);
			}
		}
				
	} else {
		while ((from = ib.getc()) >= 0) {
			if (from <= high)
				from = p[from];
			ob.putc(from);
		}
	}
}

Pcb.new(s: string): ref Pcb
{
	return ref Pcb(s, len s, 0, -1, -1);
}

Pcb.rewind(p: self ref Pcb)
{
	p.current = 0;
	p.last = p.final = -1;
}

Pcb.getc(p: self ref Pcb): int
{
	if(p.current >= p.end)
		return -1;
	s := p.current;
	r := p.spec[s++];
	if(r == '\\' && s < p.end){
		n := 0;
		if ((r = p.spec[s]) == 'x') {
			s++;
			for (i := 0; i < 4 && s < p.end; i++) {
				p.current = s;
				r = p.spec[s++];
				if ('0' <= r && r <= '9')
					n = 16*n + r - '0';
				else if ('a' <= r && r <= 'f')
					n = 16*n + r - 'a' + 10;
				else if ('A' <= r && r <= 'F')
					n = 16*n + r - 'A' + 10;
				else {
					if (i == 0)
						return 'x';
					return n;
				}
			}
			r = n;
		} else {
			for(i := 0; i < 3 && s < p.end; i++) {
				p.current = s;
				r = p.spec[s++];
				if('0' <= r && r <= '7')
					n = 8*n + r - '0';
				else {
					if (i == 0)
						return r;
					return n;
				}
			}
			if(n > 0377)
				error("char>0377");
			r = n;
		}
	}
	p.current = s;
	return r;
}

Pcb.canon(p: self ref Pcb): int
{
	if (p.final >= 0) {
		if (p.last < p.final)
			return ++p.last;
		p.final = -1;
	}
	if (p.current >= p.end)
		return -1;
	if(p.spec[p.current] == '-' && p.last >= 0 && p.current+1 < p.end){
		p.current++;
		r := p.getc();
		if (r < p.last)
			error ("Invalid range specification");
		if (r > p.last) {
			p.final = r;
			return ++p.last;
		}
	}
	r := p.getc();
	p.last = r;
	return p.last;
}

error(s: string)
{
	sys->fprint(stderr, "tr: %s\n", s);
	raise "fail: error";
}