xref: /freebsd/usr.bin/getconf/fake-gperf.awk (revision 02e9120893770924227138ba49df1edb3896112a)
1#!/usr/bin/awk -f
2BEGIN {
3  state = 0;
4  struct_seen = "";
5}
6/^%{$/ && state == 0 {
7  state = 1;
8  next;
9}
10/^%}$/ && state == 1 {
11  state = 0;
12  next;
13}
14state == 1 { print; next; }
15/^struct/ && state == 0 {
16  print;
17  struct_seen = $2;
18  next;
19}
20/^%%$/ && state == 0 {
21  state = 2;
22  if (struct_seen !~ /^$/) {
23    print "static const struct", struct_seen, "wordlist[] = {";
24  } else {
25    print "static const struct map {";
26    print "\tconst char *name;";
27    print "\tint key;";
28    print "\tint valid;";
29    print "} wordlist[] = {";
30    struct_seen = "map";
31  }
32  next;
33}
34/^%%$/ && state == 2 {
35  state = 3;
36  print "\t{ NULL, 0, 0 }";
37  print "};";
38  print "#include <sys/param.h>";
39  print "#define\tNWORDS\t(nitems(wordlist) - 1)";
40  print "static const struct map *";
41  print "in_word_set(const char *word)";
42  print "{";
43  print "\tconst struct", struct_seen, "*mp;";
44  print "";
45  print "\tfor (mp = wordlist; mp < &wordlist[NWORDS]; mp++) {";
46  print "\t\tif (strcmp(word, mp->name) == 0)";
47  print "\t\t\treturn (mp);";
48  print "\t}";
49  print "\treturn (NULL);";
50  print "}";
51  print "";
52  next;
53}
54state == 2 && NF == 2 {
55  name = substr($1, 1, length($1) - 1);
56  printf "#ifdef %s\n", $2;
57  printf "\t{ \"%s\", %s, 1 },\n", name, $2;
58  print "#else";
59  printf "\t{ \"%s\", 0, 0 },\n", name, $2;
60  print "#endif"
61  next;
62}
63state == 3 { print; next; }
64{
65				# eat anything not matched.
66}
67