fake-gperf.awk revision 103516
1#!/usr/bin/awk -f
2#
3# This file is in the public domain.  Written by Garrett A. Wollman,
4# 2002-09-17.
5#
6# $FreeBSD: head/usr.bin/getconf/fake-gperf.awk 103516 2002-09-18 03:15:25Z wollman $
7#
8BEGIN {
9  state = 0;
10}
11/^%{$/ && state == 0 {
12  state = 1;
13  next;
14}
15/^%}$/ && state == 1 {
16  state = 0;
17  next;
18}
19state == 1 { print; next; }
20/^%%$/ && state == 0 {
21  state = 2;
22  print "#include <stddef.h>";
23  print "#include <string.h>";
24  print "static const struct map {";
25  print "\tconst char *name;";
26  print "\tint key;";
27  print "} wordlist[] = {";
28  next;
29}
30/^%%$/ && state == 2 {
31  state = 3;
32  print "\t{ NULL }";
33  print "};";
34  print "#define\tNWORDS\t(sizeof(wordlist)/sizeof(wordlist[0]))";
35  print "static const struct map *";
36  print "in_word_set(const char *word, unsigned int len)";
37  print "{";
38  print "\tconst struct map *mp;";
39  print "";
40  print "\tfor (mp = wordlist; mp < &wordlist[NWORDS]; mp++) {";
41  print "\t\tif (strcmp(word, mp->name) == 0)";
42  print "\t\t\treturn (mp);";
43  print "\t}";
44  print "\treturn (NULL);";
45  print "}";
46  print "";
47  next;
48}
49state == 2 && NF == 2 {
50  name = substr($1, 1, length($1) - 1);
51  printf "\t{ \"%s\", %s },\n", name, $2;
52  next;
53}
54state == 3 { print; next; }
55{
56				# eat anything not matched.
57}
58