]> git.draconx.ca Git - cdecl99.git/blob - src/gen-cmdlist.awk
Use gperf to implement command selection.
[cdecl99.git] / src / gen-cmdlist.awk
1 #!/bin/awk -f
2 #
3 # Copyright © 2021 Nick Bowler
4 #
5 # Hackjob to try and find all the relevant wordlist items from the gperf
6 # output, in order to produce (at runtime) a list of those commands, in
7 # the same order as they are listed in the gperf input file.
8 #
9 # The output is a definition of the object-like macro CMD_SEQ, which
10 # can be used to initialize an array with static storage duration.
11 # Each element of the array represents offsets into the wordlist table,
12 # in sequence.
13 #
14 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
15 # This is free software: you are free to do what the fuck you want to.
16 # There is NO WARRANTY, to the extent permitted by law.
17
18 END {
19   print "/*"
20   if (FILENAME) {
21     print " * Automatically generated by gen-cmdlist.awk from " FILENAME
22   } else {
23     print " * Automatically generated by gen-cmdlist.awk"
24   }
25   print " * Do not edit."
26   print " */"
27 }
28
29 BEGIN { maxline = 0 }
30
31 $1 == "char" && (id = get_stringpool_id($2)) {
32   sub(/[^"]*"/, "", $2)
33   sub(/".*/, "", $2)
34   pool[id] = $2
35 }
36
37 $1 ~ /^#line/ {
38   line = $2
39   if (line > maxline) {
40     maxline = line
41   }
42 }
43
44 (id = get_stringpool_id($0)) && $0 ~ "cmd_" pool[id] {
45   sub(/^stringpool_str/, "", id)
46   indices[line] = id
47 }
48
49 END {
50   seq = ""
51   for (i = 1; i <= maxline; i++) {
52     if (i in indices) {
53       if (seq) {
54         seq = seq ", " indices[i]
55       } else {
56         seq = indices[i]
57       }
58     }
59   }
60
61   print "#define CMD_SEQ { " seq " }"
62 }
63
64 function get_stringpool_id(s)
65 {
66   if (sub(/.*stringpool_str/, "stringpool_str", s) && sub(/[,[].*/, "", s)) {
67     return s
68   }
69
70   return ""
71 }