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