xref: /inferno-os/emu/port/mkroot (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1$AWK '
2BEGIN{
3		if (ARGC < 2)
4			exit "usage";
5
6		conf = ARGV[1];
7		infernoroot = ENVIRON["ROOT"];
8		init = ENVIRON["INIT"];
9		data2c = ENVIRON["DATA2C"];
10		if(data2c == "")
11			data2c = "data2c"
12		nroot = 0;
13}
14/^$/{
15		next;
16}
17/^#/{
18		next;
19}
20collect && /^[^	\t]/{
21		collect = 0;
22}
23collect && section ~ "root"{
24		dst[nroot] = $1;
25		if (NF > 1)
26			src[nroot] = infernoroot $2;
27		else if (dst[nroot] == "/osinit.dis")
28			src[nroot] = infernoroot "/dis/" init ".dis";
29		else
30			src[nroot] = infernoroot $1;
31		for(i=0; i<nroot; i++)
32			if(dst[i] == dst[nroot])
33				break;
34		if(i == nroot)
35			nroot++;
36}
37$0 ~ /^[^ \t]/{
38		if($0 ~ "(code|dev|ether|ip|lib|link|mod|misc|port|root|vga)"){
39			section = $0;
40			collect = 1;
41		}
42		next;
43}
44END{
45		rootdata = conf ".root.c";
46		system("rm -f " rootdata);
47		print("/* Generated by mkroot */") >rootdata;
48		close(rootdata);
49		isdir[0] = 1;
50		dotdot[0] = 0;
51		qid = 1;
52		for (i = 0; i < nroot; i++) {
53			ncomp = split(dst[i], comp, "/");
54			if (comp[1] != "" || ncomp < 2)
55				continue;
56			q = 0;
57			for (j = 2; j <= ncomp; j++) {
58				key = q "/" comp[j];
59				if (walk[key] == 0) {
60					walk[key] = qid;
61					dotdot[qid] = q;
62					q = qid++;
63					name[q] = comp[j];
64					if (j < ncomp)
65						isdir[q] = 1;
66				}
67				else
68					q = walk[key];
69			}
70			if (system("test -d " src[i]) == 0)
71				isdir[q] = 1;
72			else {
73				if (system(data2c " root" q " <" src[i] " >>" rootdata) != 0)
74					exit 1;
75				print("extern unsigned char root" q "code[];");
76				print("extern int root" q "len;");
77			}
78		}
79
80		x = 1;
81		sort[0] = 0;
82		unsort[0] = 0;
83		for (q = 0; q < qid; q++) {
84			if (isdir[q]) {
85				nchild[q] = 0;
86				for (q2 = 1; q2 < qid; q2++) {
87					if (dotdot[q2] == q) {
88						if (nchild[q]++ == 0)
89							child0[q] = x;
90						sort[q2] = x++;
91						unsort[sort[q2]] = q2;
92					}
93				}
94			}
95		}
96
97		print("int rootmaxq = " qid ";");
98
99		print("Dirtab roottab[" qid "] = {");
100		for (oq = 0; oq < qid; oq++) {
101			q = unsort[oq];
102			if (!isdir[q])
103				print("\t\"" name[q] "\",\t{" oq ", 0, QTFILE},\t", "0,\t0444,");
104			else
105				print("\t\"" name[q] "\",\t{" oq ", 0, QTDIR},\t", "0,\t0555,");
106		}
107		print("};");
108
109		print("Rootdata rootdata[" qid "] = {");
110		for (oq = 0; oq < qid; oq++) {
111			q = unsort[oq];
112			if (!isdir[q])
113				print("\t" sort[dotdot[q]] ",\t", "root" q "code,\t", "0,\t", "&root" q "len,");
114			else if (nchild[q])
115				print("\t" sort[dotdot[q]] ",\t", "&roottab[" child0[q] "],\t", nchild[q] ",\tnil,");
116			else
117				print("\t" sort[dotdot[q]] ",\t", "nil,\t", "0,\t", "nil,");
118		}
119		print("};");
120}
121' $1 >$1.root.h
122