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