| 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. *) |
| 8 | module Opt; |
| 9 | import Out, Sys, Rt, Str, Buf, Heap, Files; |
| 10 | |
| 11 | type |
| 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 | |
| 29 | var 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. *) |
| 32 | proc optionOf(r: Row; s: []U8): (BOOL, U8, []U8, []U8); |
| 33 | begin |
| 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, "", ""; |
| 40 | end; |
| 41 | |
| 42 | proc say(s: []U8); |
| 43 | begin |
| 44 | Buf.str(ADR(text), s); |
| 45 | end; |
| 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. *) |
| 49 | proc find(p: ^Parser; c: U8; n: []U8): INT; |
| 50 | var i: INT; is: BOOL; letter: U8; long, value: []U8; |
| 51 | begin |
| 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; |
| 57 | end; |
| 58 | |
| 59 | (* The next option of p, by getopt's rules. *) |
| 60 | proc step(p: ^Parser): Step; |
| 61 | var w, n: []U8; i, eq: INT; is: BOOL; letter: U8; long, value: []U8; |
| 62 | begin |
| 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; |
| 120 | end; |
| 121 | |
| 122 | (* The words of the scope's name, "remote.add" as "remote add". *) |
| 123 | proc scopeWords(p: ^Parser; b: ^Buf.Buf); |
| 124 | var i: INT; |
| 125 | begin |
| 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; |
| 129 | end; |
| 130 | |
| 131 | (* The scope's synopsis lines, as the help and the mistakes begin. *) |
| 132 | proc synopsis(p: ^Parser; b: ^Buf.Buf); |
| 133 | var i: INT; first: BOOL; |
| 134 | begin |
| 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; |
| 154 | end; |
| 155 | |
| 156 | (* The left column of a help line for an option. *) |
| 157 | proc left(b: ^Buf.Buf; letter: U8; long, value: []U8); |
| 158 | begin |
| 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; |
| 168 | end; |
| 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. *) |
| 173 | proc printHelp(p: ^Parser); |
| 174 | var out, line: Buf.Buf; width, i, k, round: INT; help: []U8; |
| 175 | begin |
| 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); |
| 211 | end; |
| 212 | |
| 213 | (* "name: sentence" on standard error, the synopsis after it if wanted, |
| 214 | and the end, with status. *) |
| 215 | proc complain(p: ^Parser; sentence: []U8; withSynopsis: BOOL; status: INT); |
| 216 | var b: Buf.Buf; |
| 217 | begin |
| 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); |
| 227 | end; |
| 228 | |
| 229 | (* -h or --help among the scope's options, before the program sees any: |
| 230 | read ahead by a copy of the parser *) |
| 231 | proc lookForHelp(p: ^Parser); |
| 232 | var q: Parser; s: Step; |
| 233 | begin |
| 234 | q := p^; |
| 235 | repeat |
| 236 | s := step(ADR(q)); |
| 237 | if s = aHelp then printHelp(p); end; |
| 238 | until s = theEnd; |
| 239 | end; |
| 240 | |
| 241 | proc decimal(b: ^Buf.Buf; n: I64); |
| 242 | var d: [20]U8; i: INT; u: U64; |
| 243 | begin |
| 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)]); |
| 249 | end; |
| 250 | |
| 251 | (* The table checked before a word is read: a mistake in it is the |
| 252 | program's, status 1. *) |
| 253 | proc checkTable(p: ^Parser); |
| 254 | var i, j, k: INT; is, other: BOOL; letter, l2: U8; long, value, l3, v3, why: []U8; b: Buf.Buf; |
| 255 | begin |
| 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; |
| 285 | end; |
| 286 | |
| 287 | (* the arguments from 1 on, by table *) |
| 288 | proc init*(p: ^Parser; name: []U8; table: []Row); |
| 289 | begin |
| 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); |
| 300 | end; |
| 301 | |
| 302 | (* the next option; false when they end *) |
| 303 | proc next*(p: ^Parser): BOOL; |
| 304 | begin |
| 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; |
| 312 | end; |
| 313 | |
| 314 | (* the next operand; false when there are no more *) |
| 315 | proc operand*(p: ^Parser): ([]U8, BOOL); |
| 316 | begin |
| 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; |
| 322 | end; |
| 323 | |
| 324 | (* subcommand name's options, from the word after it *) |
| 325 | proc scope*(p: ^Parser; name: []U8); |
| 326 | begin |
| 327 | p^.scope := name; |
| 328 | p^.ended := false; |
| 329 | p^.pos := 0; |
| 330 | lookForHelp(p); |
| 331 | end; |
| 332 | |
| 333 | (* the value just read, a number from lo to hi *) |
| 334 | proc number*(p: ^Parser; lo, hi: I64): I64; |
| 335 | var n: I64; ok: BOOL; b: Buf.Buf; i: INT; is: BOOL; letter: U8; long, value: []U8; |
| 336 | begin |
| 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; |
| 354 | end; |
| 355 | |
| 356 | (* the program's own mistake *) |
| 357 | proc fail*(p: ^Parser; text: []U8); |
| 358 | begin |
| 359 | complain(p, text, false, 2); |
| 360 | end; |
| 361 | |
| 362 | end Opt. |