summaryrefslogtreecommitdiff
path: root/appl/cmd/install/proto2list.b
blob: b5997c15138e8be4702c0e16b66db0379385e32b (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
#	
# Copyright © 2001 Vita Nuova (Holdings) Limited.  All rights reserved.	
#

implement Proto2list;

# make a version list suitable for SDS from a series of proto files

include "sys.m";
	sys : Sys;
include "draw.m";
include "bufio.m";
	bufio : Bufio;
	Iobuf : import bufio;
include "crc.m";
	crcm : Crc;
include "proto.m";
	proto : Proto;
include "protocaller.m";
	protocaller : Protocaller;

WARN, ERROR, FATAL : import Protocaller;

Proto2list: module
{
	init : fn(ctxt: ref Draw->Context, argv: list of string);
	protofile: fn(new : string, old : string, d : ref Sys->Dir);
	protoerr: fn(lev : int, line : int, err : string);
};

stderr: ref Sys->FD;
protof: string;

Element: type (string, string);

List: adt{
	as: array of Element;
	n: int;
	init: fn(l: self ref List);
	add: fn(l: self ref List, e: Element);
	end: fn(l: self ref List): array of Element;
};

flist: ref List;

List.init(l: self ref List)
{
	l.as = array[1024] of Element;
	l.n = 0;
}

List.add(l: self ref List, e: Element)
{
	if(l.n == len l.as)
		l.as = (array[2*l.n] of Element)[0:] = l.as;
	l.as[l.n++] = e;
}

List.end(l: self ref List): array of Element
{
	return l.as[0: l.n];
}

usage()
{
	sys->fprint(stderr, "Usage: proto2list protofile ...\n");
	exit;
}

init(nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;
	crcm = load Crc Crc->PATH;
	proto = load Proto Proto->PATH;
	protocaller = load Protocaller "$self";
	stderr = sys->fildes(2);
	root := "/";
	flist = ref List;
	flist.init();
	for(argv = tl argv; argv != nil; argv = tl argv){
		protof = hd argv;
		proto->rdproto(hd argv, root, protocaller);
	}
	fs := flist.end();
	sort(fs);
	fs = uniq(fs);
	out(fs);
}

protofile(new : string, old : string, nil : ref Sys->Dir)
{
	if(new == old)
		new = "-";
	flist.add((old, new));
}

out(fs: array of Element)
{
	nf := len fs;
	for(i := 0; i < nf; i++){
		(f, g) := fs[i];
		(ok, d) := sys->stat(f);
		if (ok < 0) {
			sys->fprint(stderr, "cannot open %s\n", f);
			continue;
		}
		if (d.mode & Sys->DMDIR)
			d.length = big 0;
		sys->print("%s	%s	%d	%d	%d	%d	%d\n", f, g, int d.length, d.mode, d.mtime, crc(f, d), 0);
	}
}

protoerr(lev : int, line : int, err : string)
{
	s := "line " + string line + " : " + err;
	case lev {
		WARN => warn(s);
		ERROR => error(s);
		FATAL => fatal(s);
	}
}

crc(f : string, d: Sys->Dir) : int
{
	crcs := crcm->init(0, int 16rffffffff);
	if (d.mode & Sys->DMDIR)
		return 0;
	fd := sys->open(f, Sys->OREAD);
	if (fd == nil) {
		sys->fprint(stderr, "cannot open %s\n", f);
		return 0;
	}
	crc := 0;
	buf := array[Sys->ATOMICIO] of byte;
	for (;;) {
		nr := sys->read(fd, buf, len buf);
		if (nr < 0) {
			sys->fprint(stderr, "bad read on %s : %r\n", f);
			return 0;
		}
		if (nr <= 0)
			break;
		crc = crcm->crc(crcs, buf, nr);
	}
	crcm->reset(crcs);
	return crc;
}

sort(a: array of Element)
{
	mergesort(a, array[len a] of Element);
}
	
mergesort(a, b: array of Element)
{
	r := len a;
	if (r > 1) {
		m := (r-1)/2 + 1;
		mergesort(a[0:m], b[0:m]);
		mergesort(a[m:], b[m:]);
		b[0:] = a;
		for ((i, j, k) := (0, m, 0); i < m && j < r; k++) {
			if (b[i].t0 > b[j].t0)
				a[k] = b[j++];
			else
				a[k] = b[i++];
		}
		if (i < m)
			a[k:] = b[i:m];
		else if (j < r)
			a[k:] = b[j:r];
	}
}


uniq(a: array of Element): array of Element
{
	m := n := len a;
	for(i := 0; i < n-1; ){
		if(a[i].t0 == a[i+1].t0){
			if(a[i].t1 != a[i+1].t1)
				warn(sys->sprint("duplicate %s(%s %s)", a[i].t0, a[i].t1, a[i+1].t1));
			a[i+1:] = a[i+2: n--];
		}
		else
			i++;
	}
	if(n == m)
		return a;
	return a[0: n];
}
		
error(s: string)
{
	sys->fprint(stderr, "%s: %s\n", protof, s);
	exit;
}

fatal(s: string)
{
	sys->fprint(stderr, "fatal: %s\n", s);
	exit;
}
 
warn(s: string)
{
	sys->fprint(stderr, "%s: %s\n", protof, s);
}