raz / sbcl / lib / Opt.raz

362 lines · 11.8 KiB · raw · history · last change 55bc6c0d

1(* A command line read through one table, by getopt's rules with GNU's
2 long options; the help and the mistakes are made from the table.
3
4 The parser walks the arguments with arg, the next word, and pos, the
5 next letter in a group such as -lw (0 outside one). Each step is read
6 by step, which never ends the program: init and scope use it to look
7 ahead for -h and --help, and next to read the options one by one. *)
8module Opt;
9import Out, Sys, Rt, Str, Buf, Heap, Files;
10
11type
12 Row* = variant
13 usage(scope, line: []U8); (* a synopsis line, without "usage: " *)
14 sub(scope, name, help: []U8); (* a subcommand *)
15 option(scope: []U8; letter: U8; long, value, help: []U8);
16 end;
17 Parser* = record
18 letter*: U8; (* the option just read *)
19 value*: []U8; (* its value; "" for a flag *)
20 long*: BOOL; (* it was written by its long name *)
21 name: []U8; (* the program's *)
22 table: []Row;
23 scope: []U8;
24 arg, pos: INT;
25 ended: BOOL; (* the options have ended: what is left are operands *)
26 end;
27 Step = (anOption, theEnd, aHelp, aMistake); (* what step read *)
28
29var text: Buf.Buf; (* a mistake's sentence, as step makes it *)
30
31(* What an option row says, if row r is an option of scope s. *)
32proc optionOf(r: Row; s: []U8): (BOOL, U8, []U8, []U8);
33begin
34 match r of
35 | option(scope, letter, long, value, help):
36 if scope = s then return true, letter, long, value; end;
37 | else:
38 end;
39 return false, 0, "", "";
40end;
41
42proc say(s: []U8);
43begin
44 Buf.str(ADR(text), s);
45end;
46
47(* The option of p's scope with the letter c (or, if c is 0, the long
48 name n): its row, or -1. *)
49proc find(p: ^Parser; c: U8; n: []U8): INT;
50var i: INT; is: BOOL; letter: U8; long, value: []U8;
51begin
52 for i := 0 to LEN(p^.table) - 1 do
53 is, letter, long, value := optionOf(p^.table[i], p^.scope);
54 if is and ((c # 0 and letter = c) or (c = 0 and LEN(n) > 0 and long = n)) then return i; end;
55 end;
56 return -1;
57end;
58
59(* The next option of p, by getopt's rules. *)
60proc step(p: ^Parser): Step;
61var w, n: []U8; i, eq: INT; is: BOOL; letter: U8; long, value: []U8;
62begin
63 Buf.reset(ADR(text));
64 p^.value := "";
65 p^.long := false;
66 if p^.ended or p^.arg >= ARGC() then return theEnd; end;
67 w := Files.arg(p^.arg);
68 if p^.pos = 0 then
69 if w = "--" then INC(p^.arg); p^.ended := true; return theEnd; end;
70 if LEN(w) < 2 or w[0] # '-' then return theEnd; end;
71 if w[1] = '-' then
72 (* --name, --name=value or --name value *)
73 INC(p^.arg);
74 eq := 2;
75 while eq < LEN(w) and w[eq] # '=' do INC(eq); end;
76 n := w[2..eq];
77 if n = "help" then return aHelp; end;
78 i := find(p, 0, n);
79 if i < 0 then say("unknown option --"); say(n); return aMistake; end;
80 is, letter, long, value := optionOf(p^.table[i], p^.scope);
81 p^.letter := letter;
82 p^.long := true;
83 if LEN(value) = 0 then
84 if eq < LEN(w) then say("--"); say(n); say(" takes no value"); return aMistake; end;
85 elsif eq < LEN(w) then
86 p^.value := w[eq + 1..LEN(w)];
87 elsif p^.arg < ARGC() then
88 p^.value := Files.arg(p^.arg);
89 INC(p^.arg);
90 else
91 say("--"); say(n); say(" needs a "); say(value); return aMistake;
92 end;
93 return anOption;
94 end;
95 p^.pos := 1;
96 end;
97 (* a letter of a group *)
98 letter := w[p^.pos];
99 INC(p^.pos);
100 if p^.pos = LEN(w) then p^.pos := 0; INC(p^.arg); end;
101 if letter = 'h' then return aHelp; end;
102 i := find(p, letter, "");
103 if i < 0 or letter < '!' then say("unknown option -"); Buf.add(ADR(text), letter); return aMistake; end;
104 is, letter, long, value := optionOf(p^.table[i], p^.scope);
105 p^.letter := letter;
106 if LEN(value) > 0 then
107 (* the rest of the word, or the whole next word *)
108 if p^.pos > 0 then
109 p^.value := w[p^.pos..LEN(w)];
110 p^.pos := 0;
111 INC(p^.arg);
112 elsif p^.arg < ARGC() then
113 p^.value := Files.arg(p^.arg);
114 INC(p^.arg);
115 else
116 say("-"); Buf.add(ADR(text), letter); say(" needs a "); say(value); return aMistake;
117 end;
118 end;
119 return anOption;
120end;
121
122(* The words of the scope's name, "remote.add" as "remote add". *)
123proc scopeWords(p: ^Parser; b: ^Buf.Buf);
124var i: INT;
125begin
126 for i := 0 to LEN(p^.scope) - 1 do
127 if p^.scope[i] = '.' then Buf.add(b, ' '); else Buf.add(b, p^.scope[i]); end;
128 end;
129end;
130
131(* The scope's synopsis lines, as the help and the mistakes begin. *)
132proc synopsis(p: ^Parser; b: ^Buf.Buf);
133var i: INT; first: BOOL;
134begin
135 first := true;
136 for i := 0 to LEN(p^.table) - 1 do
137 match p^.table[i] of
138 | usage(scope, line):
139 if scope = p^.scope then
140 if first then Buf.str(b, "usage: "); else Buf.str(b, " "); end;
141 Buf.str(b, line);
142 Buf.add(b, '\n');
143 first := false;
144 end;
145 | else:
146 end;
147 end;
148 if first then
149 Buf.str(b, "usage: ");
150 Buf.str(b, p^.name);
151 if LEN(p^.scope) > 0 then Buf.add(b, ' '); scopeWords(p, b); end;
152 Buf.add(b, '\n');
153 end;
154end;
155
156(* The left column of a help line for an option. *)
157proc left(b: ^Buf.Buf; letter: U8; long, value: []U8);
158begin
159 Buf.str(b, " ");
160 if letter >= '!' then
161 Buf.add(b, '-'); Buf.add(b, letter);
162 if LEN(long) > 0 then Buf.str(b, ", "); end;
163 else
164 Buf.str(b, " ");
165 end;
166 if LEN(long) > 0 then Buf.str(b, "--"); Buf.str(b, long); end;
167 if LEN(value) > 0 then Buf.add(b, ' '); Buf.str(b, value); end;
168end;
169
170(* The help: the synopsis, a blank line, and each subcommand and option
171 of the scope, and the help itself, each with its line of help, the
172 helps in a column as far right as the longest left column needs. *)
173proc printHelp(p: ^Parser);
174var out, line: Buf.Buf; width, i, k, round: INT; help: []U8;
175begin
176 Buf.init(ADR(out), Heap.heap);
177 Buf.init(ADR(line), Heap.heap);
178 synopsis(p, ADR(out));
179 Buf.add(ADR(out), '\n');
180 (* round 0 measures the left column, round 1 writes the lines *)
181 for round := 0 to 1 do
182 for i := 0 to LEN(p^.table) do
183 Buf.reset(ADR(line));
184 help := "";
185 if i = LEN(p^.table) then
186 left(ADR(line), 'h', "help", "");
187 help := "print this and exit";
188 else
189 match p^.table[i] of
190 | sub(scope, name, h):
191 if scope = p^.scope then Buf.str(ADR(line), " "); Buf.str(ADR(line), name); help := h; end;
192 | option(scope, letter, long, value, h):
193 if scope = p^.scope then left(ADR(line), letter, long, value); help := h; end;
194 | else:
195 end;
196 end;
197 if line.len > 0 then
198 if round = 0 then
199 if line.len > width then width := line.len; end;
200 else
201 Buf.str(ADR(out), Buf.bytes(ADR(line)));
202 for k := line.len to width + 2 do Buf.add(ADR(out), ' '); end;
203 Buf.str(ADR(out), help);
204 Buf.add(ADR(out), '\n');
205 end;
206 end;
207 end;
208 end;
209 Out.str(Buf.bytes(ADR(out)));
210 Rt.exit(0);
211end;
212
213(* "name: sentence" on standard error, the synopsis after it if wanted,
214 and the end, with status. *)
215proc complain(p: ^Parser; sentence: []U8; withSynopsis: BOOL; status: INT);
216var b: Buf.Buf;
217begin
218 Buf.init(ADR(b), Heap.heap);
219 Buf.str(ADR(b), p^.name);
220 Buf.str(ADR(b), ": ");
221 Buf.str(ADR(b), sentence);
222 Buf.add(ADR(b), '\n');
223 if withSynopsis then synopsis(p, ADR(b)); end;
224 Out.flush;
225 _ := Sys.write(Sys.stderr, Buf.bytes(ADR(b)));
226 Rt.exit(status);
227end;
228
229(* -h or --help among the scope's options, before the program sees any:
230 read ahead by a copy of the parser *)
231proc lookForHelp(p: ^Parser);
232var q: Parser; s: Step;
233begin
234 q := p^;
235 repeat
236 s := step(ADR(q));
237 if s = aHelp then printHelp(p); end;
238 until s = theEnd;
239end;
240
241proc decimal(b: ^Buf.Buf; n: I64);
242var d: [20]U8; i: INT; u: U64;
243begin
244 u := U64(n);
245 if n < 0 then Buf.add(b, '-'); u := 0 - u; end;
246 i := LEN(d);
247 repeat DEC(i); d[i] := U8(u mod 10) + '0'; u := u div 10; until u = 0;
248 Buf.str(b, d[i..LEN(d)]);
249end;
250
251(* The table checked before a word is read: a mistake in it is the
252 program's, status 1. *)
253proc checkTable(p: ^Parser);
254var i, j, k: INT; is, other: BOOL; letter, l2: U8; long, value, l3, v3, why: []U8; b: Buf.Buf;
255begin
256 for i := 0 to LEN(p^.table) - 1 do
257 why := "";
258 match p^.table[i] of
259 | option(scope, letter, long, value, help):
260 if letter = 0 and LEN(long) = 0 then why := "the option has neither a letter nor a long name";
261 elsif letter = '-' then why := "the letter is -";
262 elsif letter = 'h' or long = "help" then why := "-h and --help are Opt's";
263 else
264 for k := 0 to LEN(long) - 1 do
265 if long[k] = '=' then why := "the long name has = in it"; end;
266 end;
267 for j := 0 to i - 1 do
268 other, l2, l3, v3 := optionOf(p^.table[j], scope);
269 if other and ((letter # 0 and l2 = letter) or (LEN(long) > 0 and l3 = long)) then
270 why := "the option is in the scope already";
271 end;
272 end;
273 end;
274 | else:
275 end;
276 if LEN(why) > 0 then
277 Buf.init(ADR(b), Heap.heap);
278 Buf.str(ADR(b), "option table, row ");
279 decimal(ADR(b), I64(i + 1));
280 Buf.str(ADR(b), ": ");
281 Buf.str(ADR(b), why);
282 complain(p, Buf.bytes(ADR(b)), false, 1);
283 end;
284 end;
285end;
286
287(* the arguments from 1 on, by table *)
288proc init*(p: ^Parser; name: []U8; table: []Row);
289begin
290 if text.len = 0 then Buf.init(ADR(text), Heap.heap); end;
291 p^.letter := 0; p^.value := ""; p^.long := false;
292 p^.name := name;
293 p^.table := table;
294 p^.scope := "";
295 p^.arg := 1;
296 p^.pos := 0;
297 p^.ended := false;
298 checkTable(p);
299 lookForHelp(p);
300end;
301
302(* the next option; false when they end *)
303proc next*(p: ^Parser): BOOL;
304begin
305 match step(p) of
306 | anOption: return true;
307 | theEnd: return false;
308 | aHelp: printHelp(p);
309 | aMistake: complain(p, Buf.bytes(ADR(text)), true, 2);
310 end;
311 return false;
312end;
313
314(* the next operand; false when there are no more *)
315proc operand*(p: ^Parser): ([]U8, BOOL);
316begin
317 while next(p) do end;
318 p^.ended := true;
319 if p^.arg >= ARGC() then return "", false; end;
320 INC(p^.arg);
321 return Files.arg(p^.arg - 1), true;
322end;
323
324(* subcommand name's options, from the word after it *)
325proc scope*(p: ^Parser; name: []U8);
326begin
327 p^.scope := name;
328 p^.ended := false;
329 p^.pos := 0;
330 lookForHelp(p);
331end;
332
333(* the value just read, a number from lo to hi *)
334proc number*(p: ^Parser; lo, hi: I64): I64;
335var n: I64; ok: BOOL; b: Buf.Buf; i: INT; is: BOOL; letter: U8; long, value: []U8;
336begin
337 n, ok := Str.toInt(p^.value);
338 if ok and n >= lo and n <= hi then return n; end;
339 Buf.init(ADR(b), Heap.heap);
340 i := find(p, p^.letter, "");
341 is, letter, long, value := optionOf(p^.table[i], p^.scope);
342 if p^.long then Buf.str(ADR(b), "--"); Buf.str(ADR(b), long);
343 else Buf.add(ADR(b), '-'); Buf.add(ADR(b), letter);
344 end;
345 Buf.str(ADR(b), " wants a number from ");
346 decimal(ADR(b), lo);
347 Buf.str(ADR(b), " to ");
348 decimal(ADR(b), hi);
349 Buf.str(ADR(b), ", not '");
350 Buf.str(ADR(b), p^.value);
351 Buf.str(ADR(b), "'");
352 complain(p, Buf.bytes(ADR(b)), true, 2);
353 return 0;
354end;
355
356(* the program's own mistake *)
357proc fail*(p: ^Parser; text: []U8);
358begin
359 complain(p, text, false, 2);
360end;
361
362end Opt.