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