xref: /inferno-os/appl/acme/acme/edit/src/a.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Aa;
2
3include "sys.m";
4include "draw.m";
5include "bufio.m";
6
7sys : Sys;
8bufio : Bufio;
9
10ORDWR, FD, open, read, write, seek, sprint, fprint, fildes, byte2char : import sys;
11Iobuf : import bufio;
12
13Aa : module {
14	init : fn(ctxt : ref Draw->Context, argl : list of string);
15};
16
17stdin, stderr : ref FD;
18
19init(nil : ref Draw->Context, argl : list of string)
20{
21	sys = load Sys Sys->PATH;
22	bufio = load Bufio Bufio->PATH;
23	stdin = fildes(0);
24	stderr = fildes(2);
25	main(argl);
26}
27
28include "findfile.b";
29
30prog := "a";
31bin : ref Iobuf;
32
33main(argv : list of string)
34{
35	afd, cfd, dfd : ref FD;
36	i, id : int;
37	nf, n, seq, rlen : int;
38	f, tf : array of File;
39	buf, s : string;
40
41	if(len argv != 2){
42		fprint(stderr, "usage: %s 'replacement'\n", prog);
43		exit;
44	}
45
46include "input.b";
47
48	# sort back to original order, backwards
49	qsort(f, nf, BSCMP);
50
51	# change
52	id = -1;
53	afd = nil;
54	cfd = nil;
55	dfd = nil;
56	ab := array of byte hd tl argv;
57	rlen = len ab;
58	for(i=0; i<nf; i++){
59		if(f[i].ok == 0)
60			continue;
61		if(f[i].id != id){
62			if(id > 0){
63				afd = nil;
64				cfd = nil;
65				dfd = nil;
66			}
67			id = f[i].id;
68			buf = sprint("/mnt/acme/%d/addr", id);
69			afd = open(buf, ORDWR);
70			if(afd == nil)
71				rerror(buf);
72			buf = sprint("/mnt/acme/%d/data", id);
73			dfd = open(buf, ORDWR);
74			if(dfd == nil)
75				rerror(buf);
76			buf = sprint("/mnt/acme/%d/ctl", id);
77			cfd = open(buf, ORDWR);
78			if(cfd == nil)
79				rerror(buf);
80			if(write(cfd, array of byte "mark\nnomark\n", 12) != 12)
81				rerror("setting nomark");
82		}
83		if(fprint(afd, "#%d", f[i].q1) < 0)
84			rerror("writing address");
85		if(write(dfd, ab, rlen) != rlen)
86			rerror("writing replacement");
87	}
88	exit;
89}
90