keyword-gen.c revision 330106
1258945Sroberto/*
2258945Sroberto * keyword-gen.c -- generate keyword scanner finite state machine and
3258945Sroberto *		    keyword_text array.
4280849Scy *
5280849Scy * This program is run to generate ntp_keyword.h
6280849Scy * After making a change here, two output files should be committed at
7280849Scy * the same time as keyword-gen.c:
8280849Scy *	ntp_keyword.h
9280849Scy *	keyword-gen-utd
10280849Scy *
11280849Scy * keyword-gen-utd is a sentinel used by Makefile.am to avoid compiling
12280849Scy * keyword_gen.c and generating ntp_keyword.h if the input keyword-gen.c
13280849Scy * has not changed.  This is not solely an optimization, it also breaks
14280849Scy * a dependency chain that otherwise would cause programs to be compiled
15280849Scy * when running "make dist" or "make distdir".  We want these to package
16280849Scy * the existing source without building anything but a tarball.  See
17280849Scy * [Bug 1470].
18258945Sroberto */
19258945Sroberto#include <config.h>
20258945Sroberto#include <stdio.h>
21258945Sroberto#include <stdlib.h>
22258945Sroberto#include <time.h>
23258945Sroberto
24258945Sroberto#include <ntp_stdlib.h>
25258945Sroberto#include <ntp_config.h>
26258945Sroberto#include <lib_strbuf.h>
27258945Sroberto#include "ntp_scanner.h"
28258945Sroberto#include "ntp_parser.h"
29258945Sroberto
30258945Sroberto
31258945Sroberto/* Define a structure to hold a (keyword, token) pair */
32258945Srobertostruct key_tok {
33258945Sroberto	char *	key;		/* Keyword */
34280849Scy	u_short	token;		/* Associated Token */
35258945Sroberto	follby	followedby;	/* nonzero indicates the next token(s)
36258945Sroberto				   forced to be string(s) */
37258945Sroberto};
38258945Sroberto
39258945Srobertostruct key_tok ntp_keywords[] = {
40258945Sroberto{ "...",		T_Ellipsis,		FOLLBY_TOKEN },
41280849Scy{ "allpeers",		T_Allpeers,		FOLLBY_TOKEN },
42258945Sroberto{ "automax",		T_Automax,		FOLLBY_TOKEN },
43258945Sroberto{ "broadcast",		T_Broadcast,		FOLLBY_STRING },
44258945Sroberto{ "broadcastclient",	T_Broadcastclient,	FOLLBY_TOKEN },
45258945Sroberto{ "broadcastdelay",	T_Broadcastdelay,	FOLLBY_TOKEN },
46280849Scy{ "ctl",		T_Ctl,			FOLLBY_TOKEN },
47258945Sroberto{ "disable",		T_Disable,		FOLLBY_TOKEN },
48258945Sroberto{ "driftfile",		T_Driftfile,		FOLLBY_STRING },
49285169Scy{ "dscp",		T_Dscp,			FOLLBY_TOKEN },
50258945Sroberto{ "enable",		T_Enable,		FOLLBY_TOKEN },
51258945Sroberto{ "end",		T_End,			FOLLBY_TOKEN },
52258945Sroberto{ "filegen",		T_Filegen,		FOLLBY_TOKEN },
53258945Sroberto{ "fudge",		T_Fudge,		FOLLBY_STRING },
54280849Scy{ "io",			T_Io,			FOLLBY_TOKEN },
55258945Sroberto{ "includefile",	T_Includefile,		FOLLBY_STRING },
56258945Sroberto{ "leapfile",		T_Leapfile,		FOLLBY_STRING },
57285169Scy{ "leapsmearinterval",	T_Leapsmearinterval,	FOLLBY_TOKEN },
58258945Sroberto{ "logconfig",		T_Logconfig,		FOLLBY_STRINGS_TO_EOC },
59258945Sroberto{ "logfile",		T_Logfile,		FOLLBY_STRING },
60258945Sroberto{ "manycastclient",	T_Manycastclient,	FOLLBY_STRING },
61258945Sroberto{ "manycastserver",	T_Manycastserver,	FOLLBY_STRINGS_TO_EOC },
62280849Scy{ "mem",		T_Mem,			FOLLBY_TOKEN },
63258945Sroberto{ "multicastclient",	T_Multicastclient,	FOLLBY_STRINGS_TO_EOC },
64258945Sroberto{ "peer",		T_Peer,			FOLLBY_STRING },
65258945Sroberto{ "phone",		T_Phone,		FOLLBY_STRINGS_TO_EOC },
66258945Sroberto{ "pidfile",		T_Pidfile,		FOLLBY_STRING },
67258945Sroberto{ "pool",		T_Pool,			FOLLBY_STRING },
68258945Sroberto{ "discard",		T_Discard,		FOLLBY_TOKEN },
69280849Scy{ "reset",		T_Reset,		FOLLBY_TOKEN },
70258945Sroberto{ "restrict",		T_Restrict,		FOLLBY_TOKEN },
71280849Scy{ "rlimit",		T_Rlimit,		FOLLBY_TOKEN },
72258945Sroberto{ "server",		T_Server,		FOLLBY_STRING },
73258945Sroberto{ "setvar",		T_Setvar,		FOLLBY_STRING },
74258945Sroberto{ "statistics",		T_Statistics,		FOLLBY_TOKEN },
75258945Sroberto{ "statsdir",		T_Statsdir,		FOLLBY_STRING },
76280849Scy{ "sys",		T_Sys,			FOLLBY_TOKEN },
77258945Sroberto{ "tick",		T_Tick,			FOLLBY_TOKEN },
78280849Scy{ "timer",		T_Timer,		FOLLBY_TOKEN },
79258945Sroberto{ "tinker",		T_Tinker,		FOLLBY_TOKEN },
80258945Sroberto{ "tos",		T_Tos,			FOLLBY_TOKEN },
81258945Sroberto{ "trap",		T_Trap,			FOLLBY_STRING },
82258945Sroberto{ "unconfig",		T_Unconfig,		FOLLBY_STRING },
83258945Sroberto{ "unpeer",		T_Unpeer,		FOLLBY_STRING },
84258945Sroberto/* authentication_command */
85258945Sroberto{ "controlkey",		T_ControlKey,		FOLLBY_TOKEN },
86258945Sroberto{ "crypto",		T_Crypto,		FOLLBY_TOKEN },
87258945Sroberto{ "keys",		T_Keys,			FOLLBY_STRING },
88258945Sroberto{ "keysdir",		T_Keysdir,		FOLLBY_STRING },
89258945Sroberto{ "ntpsigndsocket",	T_NtpSignDsocket,	FOLLBY_STRING },
90258945Sroberto{ "requestkey",		T_Requestkey,		FOLLBY_TOKEN },
91258945Sroberto{ "revoke",		T_Revoke,		FOLLBY_TOKEN },
92258945Sroberto{ "trustedkey",		T_Trustedkey,		FOLLBY_TOKEN },
93258945Sroberto/* IPv4/IPv6 protocol override flag */
94258945Sroberto{ "-4",			T_Ipv4_flag,		FOLLBY_TOKEN },
95258945Sroberto{ "-6",			T_Ipv6_flag,		FOLLBY_TOKEN },
96258945Sroberto/* option */
97258945Sroberto{ "autokey",		T_Autokey,		FOLLBY_TOKEN },
98258945Sroberto{ "burst",		T_Burst,		FOLLBY_TOKEN },
99258945Sroberto{ "iburst",		T_Iburst,		FOLLBY_TOKEN },
100258945Sroberto{ "key",		T_Key,			FOLLBY_TOKEN },
101258945Sroberto{ "maxpoll",		T_Maxpoll,		FOLLBY_TOKEN },
102280849Scy{ "mdnstries",		T_Mdnstries,		FOLLBY_TOKEN },
103258945Sroberto{ "minpoll",		T_Minpoll,		FOLLBY_TOKEN },
104258945Sroberto{ "mode",		T_Mode,			FOLLBY_TOKEN },
105258945Sroberto{ "noselect",		T_Noselect,		FOLLBY_TOKEN },
106258945Sroberto{ "preempt",		T_Preempt,		FOLLBY_TOKEN },
107258945Sroberto{ "true",		T_True,			FOLLBY_TOKEN },
108258945Sroberto{ "prefer",		T_Prefer,		FOLLBY_TOKEN },
109258945Sroberto{ "ttl",		T_Ttl,			FOLLBY_TOKEN },
110258945Sroberto{ "version",		T_Version,		FOLLBY_TOKEN },
111258945Sroberto{ "xleave",		T_Xleave,		FOLLBY_TOKEN },
112258945Sroberto/* crypto_command */
113258945Sroberto{ "host",		T_Host,			FOLLBY_STRING },
114258945Sroberto{ "ident",		T_Ident,		FOLLBY_STRING },
115258945Sroberto{ "pw",			T_Pw,			FOLLBY_STRING },
116258945Sroberto{ "randfile",		T_Randfile,		FOLLBY_STRING },
117258945Sroberto{ "digest",		T_Digest,		FOLLBY_STRING },
118258945Sroberto/*** MONITORING COMMANDS ***/
119258945Sroberto/* stat */
120258945Sroberto{ "clockstats",		T_Clockstats,		FOLLBY_TOKEN },
121258945Sroberto{ "cryptostats",	T_Cryptostats,		FOLLBY_TOKEN },
122258945Sroberto{ "loopstats",		T_Loopstats,		FOLLBY_TOKEN },
123258945Sroberto{ "peerstats",		T_Peerstats,		FOLLBY_TOKEN },
124258945Sroberto{ "rawstats",		T_Rawstats,		FOLLBY_TOKEN },
125258945Sroberto{ "sysstats", 		T_Sysstats,		FOLLBY_TOKEN },
126258945Sroberto{ "protostats",		T_Protostats,		FOLLBY_TOKEN },
127258945Sroberto{ "timingstats",	T_Timingstats,		FOLLBY_TOKEN },
128258945Sroberto/* filegen_option */
129258945Sroberto{ "file",		T_File,			FOLLBY_STRING },
130258945Sroberto{ "link",		T_Link,			FOLLBY_TOKEN },
131258945Sroberto{ "nolink",		T_Nolink,		FOLLBY_TOKEN },
132258945Sroberto{ "type",		T_Type,			FOLLBY_TOKEN },
133258945Sroberto/* filegen_type */
134258945Sroberto{ "age",		T_Age,			FOLLBY_TOKEN },
135258945Sroberto{ "day",		T_Day,			FOLLBY_TOKEN },
136258945Sroberto{ "month",		T_Month,		FOLLBY_TOKEN },
137258945Sroberto{ "none",		T_None,			FOLLBY_TOKEN },
138258945Sroberto{ "pid",		T_Pid,			FOLLBY_TOKEN },
139258945Sroberto{ "week",		T_Week,			FOLLBY_TOKEN },
140258945Sroberto{ "year",		T_Year,			FOLLBY_TOKEN },
141258945Sroberto/*** ORPHAN MODE COMMANDS ***/
142258945Sroberto/* tos_option */
143258945Sroberto{ "minclock",		T_Minclock,		FOLLBY_TOKEN },
144258945Sroberto{ "maxclock",		T_Maxclock,		FOLLBY_TOKEN },
145258945Sroberto{ "minsane",		T_Minsane,		FOLLBY_TOKEN },
146258945Sroberto{ "floor",		T_Floor,		FOLLBY_TOKEN },
147258945Sroberto{ "ceiling",		T_Ceiling,		FOLLBY_TOKEN },
148258945Sroberto{ "cohort",		T_Cohort,		FOLLBY_TOKEN },
149258945Sroberto{ "mindist",		T_Mindist,		FOLLBY_TOKEN },
150258945Sroberto{ "maxdist",		T_Maxdist,		FOLLBY_TOKEN },
151309007Sdelphij{ "bcpollbstep",	T_Bcpollbstep,		FOLLBY_TOKEN },
152258945Sroberto{ "beacon",		T_Beacon,		FOLLBY_TOKEN },
153258945Sroberto{ "orphan",		T_Orphan,		FOLLBY_TOKEN },
154280849Scy{ "orphanwait",		T_Orphanwait,		FOLLBY_TOKEN },
155280849Scy{ "nonvolatile",	T_Nonvolatile,		FOLLBY_TOKEN },
156330106Sdelphij{ "basedate",		T_Basedate,		FOLLBY_STRING },
157258945Sroberto/* access_control_flag */
158258945Sroberto{ "default",		T_Default,		FOLLBY_TOKEN },
159280849Scy{ "source",		T_Source,		FOLLBY_TOKEN },
160330106Sdelphij{ "epeer",		T_Epeer,		FOLLBY_TOKEN },
161330106Sdelphij{ "noepeer",		T_Noepeer,		FOLLBY_TOKEN },
162258945Sroberto{ "flake",		T_Flake,		FOLLBY_TOKEN },
163258945Sroberto{ "ignore",		T_Ignore,		FOLLBY_TOKEN },
164330106Sdelphij{ "ippeerlimit",	T_Ippeerlimit,		FOLLBY_TOKEN },
165258945Sroberto{ "limited",		T_Limited,		FOLLBY_TOKEN },
166258945Sroberto{ "mssntp",		T_Mssntp,		FOLLBY_TOKEN },
167258945Sroberto{ "kod",		T_Kod,			FOLLBY_TOKEN },
168258945Sroberto{ "lowpriotrap",	T_Lowpriotrap,		FOLLBY_TOKEN },
169258945Sroberto{ "mask",		T_Mask,			FOLLBY_TOKEN },
170258945Sroberto{ "nomodify",		T_Nomodify,		FOLLBY_TOKEN },
171280849Scy{ "nomrulist",		T_Nomrulist,		FOLLBY_TOKEN },
172258945Sroberto{ "nopeer",		T_Nopeer,		FOLLBY_TOKEN },
173258945Sroberto{ "noquery",		T_Noquery,		FOLLBY_TOKEN },
174258945Sroberto{ "noserve",		T_Noserve,		FOLLBY_TOKEN },
175258945Sroberto{ "notrap",		T_Notrap,		FOLLBY_TOKEN },
176258945Sroberto{ "notrust",		T_Notrust,		FOLLBY_TOKEN },
177258945Sroberto{ "ntpport",		T_Ntpport,		FOLLBY_TOKEN },
178258945Sroberto/* discard_option */
179258945Sroberto{ "average",		T_Average,		FOLLBY_TOKEN },
180258945Sroberto{ "minimum",		T_Minimum,		FOLLBY_TOKEN },
181258945Sroberto{ "monitor",		T_Monitor,		FOLLBY_TOKEN },
182280849Scy/* mru_option */
183280849Scy{ "incalloc",		T_Incalloc,		FOLLBY_TOKEN },
184280849Scy{ "incmem",		T_Incmem,		FOLLBY_TOKEN },
185280849Scy{ "initalloc",		T_Initalloc,		FOLLBY_TOKEN },
186280849Scy{ "initmem",		T_Initmem,		FOLLBY_TOKEN },
187280849Scy{ "mindepth",		T_Mindepth,		FOLLBY_TOKEN },
188280849Scy{ "maxage",		T_Maxage,		FOLLBY_TOKEN },
189280849Scy{ "maxdepth",		T_Maxdepth,		FOLLBY_TOKEN },
190280849Scy{ "maxmem",		T_Maxmem,		FOLLBY_TOKEN },
191280849Scy{ "mru",		T_Mru,			FOLLBY_TOKEN },
192258945Sroberto/* fudge_factor */
193280849Scy{ "abbrev",		T_Abbrev,		FOLLBY_STRING },
194258945Sroberto{ "flag1",		T_Flag1,		FOLLBY_TOKEN },
195258945Sroberto{ "flag2",		T_Flag2,		FOLLBY_TOKEN },
196258945Sroberto{ "flag3",		T_Flag3,		FOLLBY_TOKEN },
197258945Sroberto{ "flag4",		T_Flag4,		FOLLBY_TOKEN },
198258945Sroberto{ "refid",		T_Refid,		FOLLBY_STRING },
199258945Sroberto{ "stratum",		T_Stratum,		FOLLBY_TOKEN },
200258945Sroberto{ "time1",		T_Time1,		FOLLBY_TOKEN },
201258945Sroberto{ "time2",		T_Time2,		FOLLBY_TOKEN },
202258945Sroberto/* system_option */
203258945Sroberto{ "auth",		T_Auth,			FOLLBY_TOKEN },
204258945Sroberto{ "bclient",		T_Bclient,		FOLLBY_TOKEN },
205258945Sroberto{ "calibrate",		T_Calibrate,		FOLLBY_TOKEN },
206258945Sroberto{ "kernel",		T_Kernel,		FOLLBY_TOKEN },
207301247Sdelphij{ "mode7",		T_Mode7,		FOLLBY_TOKEN },
208258945Sroberto{ "ntp",		T_Ntp,			FOLLBY_TOKEN },
209301247Sdelphij{ "peer_clear_digest_early",	T_PCEdigest,	FOLLBY_TOKEN },
210258945Sroberto{ "stats",		T_Stats,		FOLLBY_TOKEN },
211294554Sdelphij{ "unpeer_crypto_early",	T_UEcrypto,	FOLLBY_TOKEN },
212294554Sdelphij{ "unpeer_crypto_nak_early",	T_UEcryptonak,	FOLLBY_TOKEN },
213294554Sdelphij{ "unpeer_digest_early",	T_UEdigest,	FOLLBY_TOKEN },
214280849Scy/* rlimit_option */
215280849Scy{ "memlock",		T_Memlock,		FOLLBY_TOKEN },
216280849Scy{ "stacksize",		T_Stacksize,		FOLLBY_TOKEN },
217280849Scy{ "filenum",		T_Filenum,		FOLLBY_TOKEN },
218258945Sroberto/* tinker_option */
219258945Sroberto{ "step",		T_Step,			FOLLBY_TOKEN },
220282408Scy{ "stepback",		T_Stepback,		FOLLBY_TOKEN },
221282408Scy{ "stepfwd",		T_Stepfwd,		FOLLBY_TOKEN },
222258945Sroberto{ "panic",		T_Panic,		FOLLBY_TOKEN },
223258945Sroberto{ "dispersion",		T_Dispersion,		FOLLBY_TOKEN },
224258945Sroberto{ "stepout",		T_Stepout,		FOLLBY_TOKEN },
225258945Sroberto{ "allan",		T_Allan,		FOLLBY_TOKEN },
226258945Sroberto{ "huffpuff",		T_Huffpuff,		FOLLBY_TOKEN },
227258945Sroberto{ "freq",		T_Freq,			FOLLBY_TOKEN },
228258945Sroberto/* miscellaneous_command */
229258945Sroberto{ "port",		T_Port,			FOLLBY_TOKEN },
230258945Sroberto{ "interface",		T_Interface,		FOLLBY_TOKEN },
231258945Sroberto{ "saveconfigdir",	T_Saveconfigdir,	FOLLBY_STRING },
232258945Sroberto/* interface_command (ignore and interface already defined) */
233258945Sroberto{ "nic",		T_Nic,			FOLLBY_TOKEN },
234258945Sroberto{ "all",		T_All,			FOLLBY_TOKEN },
235258945Sroberto{ "ipv4",		T_Ipv4,			FOLLBY_TOKEN },
236258945Sroberto{ "ipv6",		T_Ipv6,			FOLLBY_TOKEN },
237258945Sroberto{ "wildcard",		T_Wildcard,		FOLLBY_TOKEN },
238258945Sroberto{ "listen",		T_Listen,		FOLLBY_TOKEN },
239258945Sroberto{ "drop",		T_Drop,			FOLLBY_TOKEN },
240258945Sroberto/* simulator commands */
241258945Sroberto{ "simulate",		T_Simulate,		FOLLBY_TOKEN },
242258945Sroberto{ "simulation_duration",T_Sim_Duration,		FOLLBY_TOKEN },
243258945Sroberto{ "beep_delay",		T_Beep_Delay,		FOLLBY_TOKEN },
244258945Sroberto{ "duration",		T_Duration,		FOLLBY_TOKEN },
245258945Sroberto{ "server_offset",	T_Server_Offset,	FOLLBY_TOKEN },
246258945Sroberto{ "freq_offset",	T_Freq_Offset,		FOLLBY_TOKEN },
247258945Sroberto{ "wander",		T_Wander,		FOLLBY_TOKEN },
248258945Sroberto{ "jitter",		T_Jitter,		FOLLBY_TOKEN },
249258945Sroberto{ "prop_delay",		T_Prop_Delay,		FOLLBY_TOKEN },
250258945Sroberto{ "proc_delay",		T_Proc_Delay,		FOLLBY_TOKEN },
251258945Sroberto};
252258945Sroberto
253258945Srobertotypedef struct big_scan_state_tag {
254258945Sroberto	char	ch;		/* Character this state matches on */
255258945Sroberto	char	followedby;	/* Forces next token(s) to T_String */
256258945Sroberto	u_short	finishes_token;	/* nonzero ID if last keyword char */
257258945Sroberto	u_short	match_next_s;	/* next state to check matching ch */
258258945Sroberto	u_short	other_next_s;	/* next state to check if not ch */
259258945Sroberto} big_scan_state;
260258945Sroberto
261258945Sroberto/*
262258945Sroberto * Note: to increase MAXSTATES beyond 2048, be aware it is currently
263258945Sroberto * crammed into 11 bits in scan_state form.  Raising to 4096 would be
264258945Sroberto * relatively easy by storing the followedby value in a separate
265258945Sroberto * array with one entry per token, and shrinking the char value to
266258945Sroberto * 7 bits to free a bit for accepting/non-accepting.  More than 4096
267258945Sroberto * states will require expanding scan_state beyond 32 bits each.
268258945Sroberto */
269280849Scy#define MAXSTATES	2048
270280849Scy#define MAX_TOK_LEN	63
271258945Sroberto
272258945Srobertoconst char *	current_keyword;/* for error reporting */
273258945Srobertobig_scan_state	sst[MAXSTATES];	/* scanner FSM state entries */
274280849Scyu_short		sst_highwater;	/* next entry index to consider */
275258945Srobertochar *		symb[1024];	/* map token ID to symbolic name */
276258945Sroberto
277258945Sroberto/* for libntp */
278258945Srobertoconst char *	progname = "keyword-gen";
279258945Sroberto
280258945Srobertoint		main			(int, char **);
281258945Srobertostatic void	generate_preamble	(void);
282258945Srobertostatic void	generate_fsm		(void);
283258945Srobertostatic void	generate_token_text	(void);
284280849Scystatic u_short	create_keyword_scanner	(void);
285280849Scystatic u_short	create_scan_states	(char *, u_short, follby, u_short);
286280849Scyint		compare_key_tok_id	(const void *, const void *);
287280849Scyint		compare_key_tok_text	(const void *, const void *);
288258945Srobertovoid		populate_symb		(char *);
289280849Scyconst char *	symbname		(u_short);
290258945Sroberto
291258945Sroberto
292258945Srobertoint main(int argc, char **argv)
293258945Sroberto{
294258945Sroberto	if (argc < 2) {
295258945Sroberto		fprintf(stderr, "Usage:\n%s t_header.h\n", argv[0]);
296258945Sroberto		exit(1);
297258945Sroberto	}
298280849Scy	debug = 1;
299280849Scy
300258945Sroberto	populate_symb(argv[1]);
301258945Sroberto
302258945Sroberto	generate_preamble();
303258945Sroberto	generate_token_text();
304258945Sroberto	generate_fsm();
305258945Sroberto
306258945Sroberto	return 0;
307258945Sroberto}
308258945Sroberto
309258945Sroberto
310258945Srobertostatic void
311258945Srobertogenerate_preamble(void)
312258945Sroberto{
313258945Sroberto	time_t now;
314258945Sroberto	char timestamp[128];
315258945Sroberto	char preamble[] =
316258945Sroberto"/*\n"
317258945Sroberto" * ntp_keyword.h\n"
318258945Sroberto" * \n"
319258945Sroberto" * NOTE: edit this file with caution, it is generated by keyword-gen.c\n"
320258945Sroberto" *\t Generated %s UTC	  diff_ignore_line\n"
321258945Sroberto" *\n"
322258945Sroberto" */\n"
323258945Sroberto"#include \"ntp_scanner.h\"\n"
324258945Sroberto"#include \"ntp_parser.h\"\n"
325258945Sroberto"\n";
326258945Sroberto
327258945Sroberto	time(&now);
328258945Sroberto	if (!strftime(timestamp, sizeof(timestamp),
329258945Sroberto		      "%Y-%m-%d %H:%M:%S", gmtime(&now)))
330258945Sroberto		timestamp[0] = '\0';
331258945Sroberto
332258945Sroberto	printf(preamble, timestamp);
333258945Sroberto}
334258945Sroberto
335258945Sroberto
336258945Srobertostatic void
337258945Srobertogenerate_fsm(void)
338258945Sroberto{
339280849Scy	char rprefix[MAX_TOK_LEN + 1];
340280849Scy	char prefix[MAX_TOK_LEN + 1];
341280849Scy	char token_id_comment[16 + MAX_TOK_LEN + 1];
342280849Scy	size_t prefix_len;
343280849Scy	char *p;
344280849Scy	char *r;
345280849Scy	u_short initial_state;
346280849Scy	u_short this_state;
347280849Scy	u_short state;
348280849Scy	u_short i;
349280849Scy	u_short token;
350258945Sroberto
351285169Scy	/*
352258945Sroberto	 * Sort ntp_keywords in alphabetical keyword order.  This is
353258945Sroberto	 * not necessary, but minimizes nonfunctional changes in the
354258945Sroberto	 * generated finite state machine when keywords are modified.
355258945Sroberto	 */
356258945Sroberto	qsort(ntp_keywords, COUNTOF(ntp_keywords),
357258945Sroberto	      sizeof(ntp_keywords[0]), compare_key_tok_text);
358258945Sroberto
359258945Sroberto	/*
360285169Scy	 * To save space, reserve the state array entry matching each
361258945Sroberto	 * token number for its terminal state, so the token identifier
362258945Sroberto	 * does not need to be stored in each state, but can be
363258945Sroberto	 * recovered trivially.  To mark the entry reserved,
364258945Sroberto	 * finishes_token is nonzero.
365258945Sroberto	 */
366258945Sroberto
367258945Sroberto	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
368258945Sroberto		token = ntp_keywords[i].token;
369258945Sroberto		if (1 > token || token >= COUNTOF(sst)) {
370258945Sroberto			fprintf(stderr,
371258945Sroberto				"keyword-gen sst[%u] too small "
372258945Sroberto				"for keyword '%s' id %d\n",
373280849Scy				(int)COUNTOF(sst),
374258945Sroberto				ntp_keywords[i].key,
375258945Sroberto				token);
376258945Sroberto			exit(4);
377258945Sroberto		}
378258945Sroberto		sst[token].finishes_token = token;
379258945Sroberto	}
380258945Sroberto
381258945Sroberto	initial_state = create_keyword_scanner();
382258945Sroberto
383258945Sroberto	fprintf(stderr,
384258945Sroberto		"%d keywords consumed %d states of %d max.\n",
385258945Sroberto		(int)COUNTOF(ntp_keywords),
386258945Sroberto		sst_highwater - 1,
387258945Sroberto		(int)COUNTOF(sst) - 1);
388258945Sroberto
389258945Sroberto	printf("#define SCANNER_INIT_S %d\n\n", initial_state);
390258945Sroberto
391258945Sroberto	printf("const scan_state sst[%d] = {\n"
392258945Sroberto	       "/*SS_T( ch,\tf-by, match, other ),\t\t\t\t */\n"
393258945Sroberto	       "  0,\t\t\t\t      /* %5d %-17s */\n",
394258945Sroberto	       sst_highwater,
395258945Sroberto	       0, "");
396258945Sroberto
397258945Sroberto	for (i = 1; i < sst_highwater; i++) {
398258945Sroberto
399258945Sroberto		/* verify fields will fit */
400258945Sroberto		if (sst[i].followedby & ~0x3) {
401258945Sroberto			fprintf(stderr,
402258945Sroberto				"keyword-gen internal error "
403258945Sroberto				"sst[%d].followedby %d too big\n",
404258945Sroberto				i, sst[i].followedby);
405258945Sroberto			exit(7);
406258945Sroberto		}
407258945Sroberto
408258945Sroberto		if (sst_highwater <= sst[i].match_next_s
409258945Sroberto		    || sst[i].match_next_s & ~0x7ff) {
410258945Sroberto			fprintf(stderr,
411258945Sroberto				"keyword-gen internal error "
412258945Sroberto				"sst[%d].match_next_s %d too big\n",
413258945Sroberto				i, sst[i].match_next_s);
414258945Sroberto			exit(8);
415258945Sroberto		}
416258945Sroberto
417258945Sroberto		if (sst_highwater <= sst[i].other_next_s
418258945Sroberto		    || sst[i].other_next_s & ~0x7ff) {
419258945Sroberto			fprintf(stderr,
420258945Sroberto				"keyword-gen internal error "
421258945Sroberto				"sst[%d].other_next_s %d too big\n",
422258945Sroberto				i, sst[i].other_next_s);
423258945Sroberto			exit(9);
424258945Sroberto		}
425258945Sroberto
426280849Scy		if (sst[i].finishes_token) {
427285169Scy			snprintf(token_id_comment,
428258945Sroberto				 sizeof(token_id_comment), "%5d %-17s",
429258945Sroberto				 i, symbname(sst[i].finishes_token));
430258945Sroberto			if (i != sst[i].finishes_token) {
431258945Sroberto				fprintf(stderr,
432258945Sroberto					"keyword-gen internal error "
433258945Sroberto					"entry %d finishes token %d\n",
434258945Sroberto					i, sst[i].finishes_token);
435258945Sroberto				exit(5);
436258945Sroberto			}
437280849Scy		} else {
438280849Scy		/*
439280849Scy		 * Determine the keyword prefix that leads to this
440280849Scy		 * state.  This is expensive but keyword-gen is run
441280849Scy		 * only when it changes.  Distributing keyword-gen-utd
442280849Scy		 * achieves that, which is why it must be committed
443280849Scy		 * at the same time as keyword-gen.c and ntp_keyword.h.
444280849Scy		 *
445280849Scy		 * Scan the state array iteratively looking for a state
446280849Scy		 * which leads to the current one, collecting matching
447280849Scy		 * characters along the way.  There is only one such
448280849Scy		 * path back to the starting state given the way our
449280849Scy		 * scanner state machine is built and the practice of
450280849Scy		 * using the spelling of the keyword as its T_* token
451280849Scy		 * identifier, which results in never having two
452280849Scy		 * spellings result in the same T_* value.
453280849Scy		 */
454280849Scy			prefix_len = 0;
455280849Scy			this_state = i;
456280849Scy			do {
457280849Scy				for (state = 1; state < sst_highwater; state++)
458280849Scy					if (sst[state].other_next_s == this_state) {
459280849Scy						this_state = state;
460280849Scy						break;
461280849Scy					} else if (sst[state].match_next_s == this_state) {
462280849Scy						this_state = state;
463280849Scy						rprefix[prefix_len] = sst[state].ch;
464280849Scy						prefix_len++;
465280849Scy						break;
466280849Scy					}
467280849Scy			} while (this_state != initial_state);
468280849Scy
469280849Scy			if (prefix_len) {
470280849Scy				/* reverse rprefix into prefix */
471280849Scy				p = prefix + prefix_len;
472280849Scy				r = rprefix;
473280849Scy				while (r < rprefix + prefix_len)
474280849Scy					*--p = *r++;
475280849Scy			}
476280849Scy			prefix[prefix_len] = '\0';
477280849Scy
478280849Scy			snprintf(token_id_comment,
479280849Scy				 sizeof(token_id_comment), "%5d %-17s",
480285169Scy				 i, (initial_state == i)
481285169Scy					? "[initial state]"
482280849Scy					: prefix);
483258945Sroberto		}
484258945Sroberto
485258945Sroberto		printf("  S_ST( '%c',\t%d,    %5u, %5u )%s /* %s */\n",
486258945Sroberto		       sst[i].ch,
487258945Sroberto		       sst[i].followedby,
488258945Sroberto		       sst[i].match_next_s,
489258945Sroberto		       sst[i].other_next_s,
490258945Sroberto		       (i + 1 < sst_highwater)
491258945Sroberto			   ? ","
492258945Sroberto			   : " ",
493258945Sroberto		       token_id_comment);
494258945Sroberto	}
495258945Sroberto
496258945Sroberto	printf("};\n\n");
497258945Sroberto}
498258945Sroberto
499258945Sroberto
500258945Sroberto/* Define a function to create the states of the scanner. This function
501258945Sroberto * is used by the create_keyword_scanner function below.
502258945Sroberto *
503258945Sroberto * This function takes a suffix of a keyword, the token to be returned on
504258945Sroberto * recognizing the complete keyword, and any pre-existing state that exists
505258945Sroberto * for some other keyword that has the same prefix as the current one.
506258945Sroberto */
507280849Scystatic u_short
508258945Srobertocreate_scan_states(
509285169Scy	char *	text,
510285169Scy	u_short	token,
511258945Sroberto	follby	followedby,
512280849Scy	u_short	prev_state
513258945Sroberto	)
514258945Sroberto{
515280849Scy	u_short my_state;
516280849Scy	u_short return_state;
517280849Scy	u_short prev_char_s;
518280849Scy	u_short curr_char_s;
519258945Sroberto
520258945Sroberto	return_state = prev_state;
521258945Sroberto	curr_char_s = prev_state;
522258945Sroberto	prev_char_s = 0;
523258945Sroberto
524285169Scy	/* Find the correct position to insert the state.
525258945Sroberto	 * All states should be in alphabetical order
526258945Sroberto	 */
527258945Sroberto	while (curr_char_s && (text[0] < sst[curr_char_s].ch)) {
528258945Sroberto		prev_char_s = curr_char_s;
529258945Sroberto		curr_char_s = sst[curr_char_s].other_next_s;
530258945Sroberto	}
531258945Sroberto
532285169Scy	/*
533258945Sroberto	 * Check if a previously seen keyword has the same prefix as
534258945Sroberto	 * the current keyword.  If so, simply use the state for that
535258945Sroberto	 * keyword as my_state, otherwise, allocate a new state.
536258945Sroberto	 */
537258945Sroberto	if (curr_char_s && (text[0] == sst[curr_char_s].ch)) {
538258945Sroberto		my_state = curr_char_s;
539258945Sroberto		if ('\0' == text[1]) {
540258945Sroberto			fprintf(stderr,
541258945Sroberto				"Duplicate entries for keyword '%s' in"
542258945Sroberto				" keyword_gen.c ntp_keywords[].\n",
543258945Sroberto				current_keyword);
544258945Sroberto			exit(2);
545258945Sroberto		}
546258945Sroberto	} else {
547258945Sroberto		do
548258945Sroberto			my_state = sst_highwater++;
549258945Sroberto		while (my_state < COUNTOF(sst)
550258945Sroberto		       && sst[my_state].finishes_token);
551258945Sroberto		if (my_state >= COUNTOF(sst)) {
552258945Sroberto			fprintf(stderr,
553258945Sroberto				"fatal, keyword scanner state array "
554258945Sroberto				"sst[%d] is too small, modify\n"
555258945Sroberto				"keyword-gen.c to increase.\n",
556258945Sroberto				(int)COUNTOF(sst));
557258945Sroberto			exit(3);
558258945Sroberto		}
559258945Sroberto		/* Store the next character of the keyword */
560285169Scy		sst[my_state].ch = text[0];
561258945Sroberto		sst[my_state].other_next_s = curr_char_s;
562258945Sroberto		sst[my_state].followedby = FOLLBY_NON_ACCEPTING;
563258945Sroberto
564258945Sroberto		if (prev_char_s)
565258945Sroberto			sst[prev_char_s].other_next_s = my_state;
566258945Sroberto		else
567258945Sroberto			return_state = my_state;
568258945Sroberto	}
569258945Sroberto
570258945Sroberto	/* Check if the next character is '\0'.
571258945Sroberto	 * If yes, we are done with the recognition and this is an accepting
572258945Sroberto	 * state.
573258945Sroberto	 * If not, we need to continue scanning
574258945Sroberto	 */
575258945Sroberto	if ('\0' == text[1]) {
576258945Sroberto		sst[my_state].finishes_token = (u_short)token;
577258945Sroberto		sst[my_state].followedby = (char)followedby;
578258945Sroberto
579258945Sroberto		if (sst[token].finishes_token != (u_short)token) {
580258945Sroberto			fprintf(stderr,
581258945Sroberto				"fatal, sst[%d] not reserved for %s.\n",
582258945Sroberto				token, symbname(token));
583258945Sroberto			exit(6);
584258945Sroberto		}
585258945Sroberto		/* relocate so token id is sst[] index */
586258945Sroberto		if (my_state != token) {
587258945Sroberto			sst[token] = sst[my_state];
588280849Scy			ZERO(sst[my_state]);
589258945Sroberto			do
590258945Sroberto				sst_highwater--;
591258945Sroberto			while (sst[sst_highwater].finishes_token);
592258945Sroberto			my_state = token;
593258945Sroberto			if (prev_char_s)
594258945Sroberto				sst[prev_char_s].other_next_s = my_state;
595258945Sroberto			else
596258945Sroberto				return_state = my_state;
597258945Sroberto		}
598258945Sroberto	} else
599285169Scy		sst[my_state].match_next_s =
600258945Sroberto		    create_scan_states(
601258945Sroberto			&text[1],
602258945Sroberto			token,
603258945Sroberto			followedby,
604258945Sroberto			sst[my_state].match_next_s);
605258945Sroberto
606258945Sroberto	return return_state;
607258945Sroberto}
608258945Sroberto
609258945Sroberto
610258945Sroberto/* Define a function that takes a list of (keyword, token) values and
611258945Sroberto * creates a keywords scanner out of it.
612258945Sroberto */
613258945Sroberto
614280849Scystatic u_short
615258945Srobertocreate_keyword_scanner(void)
616258945Sroberto{
617280849Scy	u_short scanner;
618280849Scy	u_short i;
619258945Sroberto
620258945Sroberto	sst_highwater = 1;	/* index 0 invalid, unused */
621258945Sroberto	scanner = 0;
622258945Sroberto
623258945Sroberto	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
624258945Sroberto		current_keyword = ntp_keywords[i].key;
625258945Sroberto		scanner =
626258945Sroberto		    create_scan_states(
627285169Scy			ntp_keywords[i].key,
628285169Scy			ntp_keywords[i].token,
629258945Sroberto			ntp_keywords[i].followedby,
630258945Sroberto			scanner);
631258945Sroberto	}
632258945Sroberto
633258945Sroberto	return scanner;
634258945Sroberto}
635258945Sroberto
636258945Sroberto
637258945Srobertostatic void
638258945Srobertogenerate_token_text(void)
639258945Sroberto{
640280849Scy	u_short lowest_id;
641280849Scy	u_short highest_id;
642280849Scy	u_short id_count;
643280849Scy	u_short id;
644280849Scy	u_short i;
645258945Sroberto
646258945Sroberto	/* sort ntp_keywords in token ID order */
647258945Sroberto	qsort(ntp_keywords, COUNTOF(ntp_keywords),
648258945Sroberto	      sizeof(ntp_keywords[0]), compare_key_tok_id);
649258945Sroberto
650258945Sroberto	lowest_id = ntp_keywords[0].token;
651258945Sroberto	highest_id = ntp_keywords[COUNTOF(ntp_keywords) - 1].token;
652258945Sroberto	id_count = highest_id - lowest_id + 1;
653258945Sroberto
654258945Sroberto	printf("#define LOWEST_KEYWORD_ID %d\n\n", lowest_id);
655258945Sroberto
656258945Sroberto	printf("const char * const keyword_text[%d] = {", id_count);
657258945Sroberto
658258945Sroberto	id = lowest_id;
659258945Sroberto	i = 0;
660258945Sroberto	while (i < COUNTOF(ntp_keywords)) {
661258945Sroberto		while (id < ntp_keywords[i].token) {
662258945Sroberto			printf(",\n\t/* %-5d %5d %20s */\tNULL",
663258945Sroberto			       id - lowest_id, id, symbname(id));
664258945Sroberto			id++;
665258945Sroberto		}
666258945Sroberto		if (i > 0)
667258945Sroberto			printf(",");
668258945Sroberto		printf("\n\t/* %-5d %5d %20s */\t\"%s\"",
669285169Scy		       id - lowest_id, id, symbname(id),
670258945Sroberto		       ntp_keywords[i].key);
671258945Sroberto		i++;
672258945Sroberto		id++;
673258945Sroberto	}
674258945Sroberto
675258945Sroberto	printf("\n};\n\n");
676258945Sroberto}
677258945Sroberto
678285169Scy
679258945Srobertoint
680258945Srobertocompare_key_tok_id(
681280849Scy	const void *a1,
682280849Scy	const void *a2
683258945Sroberto	)
684258945Sroberto{
685280849Scy	const struct key_tok *p1 = a1;
686280849Scy	const struct key_tok *p2 = a2;
687258945Sroberto
688258945Sroberto	if (p1->token == p2->token)
689258945Sroberto		return 0;
690258945Sroberto
691258945Sroberto	if (p1->token < p2->token)
692258945Sroberto		return -1;
693258945Sroberto	else
694258945Sroberto		return 1;
695258945Sroberto}
696258945Sroberto
697258945Sroberto
698258945Srobertoint
699258945Srobertocompare_key_tok_text(
700280849Scy	const void *a1,
701280849Scy	const void *a2
702258945Sroberto	)
703258945Sroberto{
704280849Scy	const struct key_tok *p1 = a1;
705280849Scy	const struct key_tok *p2 = a2;
706258945Sroberto
707258945Sroberto	return strcmp(p1->key, p2->key);
708258945Sroberto}
709258945Sroberto
710258945Sroberto
711258945Sroberto/*
712258945Sroberto * populate_symb() - populate symb[] lookup array with symbolic token
713258945Sroberto *		     names such that symb[T_Age] == "T_Age", etc.
714258945Sroberto */
715258945Srobertovoid
716258945Srobertopopulate_symb(
717258945Sroberto	char *header_file
718258945Sroberto	)
719258945Sroberto{
720258945Sroberto	FILE *	yh;
721280849Scy	char	line[2 * MAX_TOK_LEN];
722280849Scy	char	name[2 * MAX_TOK_LEN];
723258945Sroberto	int	token;
724258945Sroberto
725258945Sroberto	yh = fopen(header_file, "r");
726258945Sroberto	if (NULL == yh) {
727258945Sroberto		perror("unable to open yacc/bison header file");
728258945Sroberto		exit(4);
729258945Sroberto	}
730258945Sroberto
731258945Sroberto	while (NULL != fgets(line, sizeof(line), yh))
732258945Sroberto		if (2 == sscanf(line, "#define %s %d", name, &token)
733258945Sroberto		    && 'T' == name[0] && '_' == name[1] && token >= 0
734280849Scy		    && token < COUNTOF(symb)) {
735258945Sroberto
736258945Sroberto			symb[token] = estrdup(name);
737280849Scy			if (strlen(name) > MAX_TOK_LEN) {
738280849Scy				fprintf(stderr,
739280849Scy					"MAX_TOK_LEN %d too small for '%s'\n"
740280849Scy					"Edit keyword-gen.c to raise.\n",
741280849Scy					MAX_TOK_LEN, name);
742280849Scy				exit(10);
743280849Scy			}
744280849Scy		}
745258945Sroberto	fclose(yh);
746258945Sroberto}
747258945Sroberto
748258945Sroberto
749258945Srobertoconst char *
750258945Srobertosymbname(
751280849Scy	u_short token
752258945Sroberto	)
753258945Sroberto{
754258945Sroberto	char *name;
755258945Sroberto
756280849Scy	if (token < COUNTOF(symb) && symb[token] != NULL) {
757280849Scy		name = symb[token];
758280849Scy	} else {
759280849Scy		LIB_GETBUF(name);
760280849Scy		snprintf(name, LIB_BUFLENGTH, "%d", token);
761285169Scy	}
762258945Sroberto
763258945Sroberto	return name;
764258945Sroberto}
765