Home | History | Annotate | Line # | Download | only in ntpd
keyword-gen.c revision 1.13
      1 /*	$NetBSD: keyword-gen.c,v 1.13 2020/05/25 20:47:25 christos Exp $	*/
      2 
      3 /*
      4  * keyword-gen.c -- generate keyword scanner finite state machine and
      5  *		    keyword_text array.
      6  *
      7  * This program is run to generate ntp_keyword.h
      8  * After making a change here, two output files should be committed at
      9  * the same time as keyword-gen.c:
     10  *	ntp_keyword.h
     11  *	keyword-gen-utd
     12  *
     13  * keyword-gen-utd is a sentinel used by Makefile.am to avoid compiling
     14  * keyword_gen.c and generating ntp_keyword.h if the input keyword-gen.c
     15  * has not changed.  This is not solely an optimization, it also breaks
     16  * a dependency chain that otherwise would cause programs to be compiled
     17  * when running "make dist" or "make distdir".  We want these to package
     18  * the existing source without building anything but a tarball.  See
     19  * [Bug 1470].
     20  */
     21 #include <config.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <time.h>
     25 
     26 #include <ntp_stdlib.h>
     27 #include <ntp_config.h>
     28 #include <lib_strbuf.h>
     29 #include "ntp_scanner.h"
     30 #include "ntp_parser.h"
     31 
     32 
     33 /* Define a structure to hold a (keyword, token) pair */
     34 struct key_tok {
     35 	char *	key;		/* Keyword */
     36 	u_short	token;		/* Associated Token */
     37 	follby	followedby;	/* nonzero indicates the next token(s)
     38 				   forced to be string(s) */
     39 };
     40 
     41 struct key_tok ntp_keywords[] = {
     42 { "...",		T_Ellipsis,		FOLLBY_TOKEN },
     43 { "allpeers",		T_Allpeers,		FOLLBY_TOKEN },
     44 { "automax",		T_Automax,		FOLLBY_TOKEN },
     45 { "broadcast",		T_Broadcast,		FOLLBY_STRING },
     46 { "broadcastclient",	T_Broadcastclient,	FOLLBY_TOKEN },
     47 { "broadcastdelay",	T_Broadcastdelay,	FOLLBY_TOKEN },
     48 { "checkhash",		T_Checkhash,		FOLLBY_TOKEN },
     49 { "ctl",		T_Ctl,			FOLLBY_TOKEN },
     50 { "disable",		T_Disable,		FOLLBY_TOKEN },
     51 { "driftfile",		T_Driftfile,		FOLLBY_STRING },
     52 { "dscp",		T_Dscp,			FOLLBY_TOKEN },
     53 { "enable",		T_Enable,		FOLLBY_TOKEN },
     54 { "end",		T_End,			FOLLBY_TOKEN },
     55 { "filegen",		T_Filegen,		FOLLBY_TOKEN },
     56 { "fudge",		T_Fudge,		FOLLBY_STRING },
     57 { "ignorehash",		T_Ignorehash,		FOLLBY_TOKEN },
     58 { "io",			T_Io,			FOLLBY_TOKEN },
     59 { "includefile",	T_Includefile,		FOLLBY_STRING },
     60 { "leapfile",		T_Leapfile,		FOLLBY_STRING },
     61 { "leapsmearinterval",	T_Leapsmearinterval,	FOLLBY_TOKEN },
     62 { "logconfig",		T_Logconfig,		FOLLBY_STRINGS_TO_EOC },
     63 { "logfile",		T_Logfile,		FOLLBY_STRING },
     64 { "manycastclient",	T_Manycastclient,	FOLLBY_STRING },
     65 { "manycastserver",	T_Manycastserver,	FOLLBY_STRINGS_TO_EOC },
     66 { "mem",		T_Mem,			FOLLBY_TOKEN },
     67 { "multicastclient",	T_Multicastclient,	FOLLBY_STRINGS_TO_EOC },
     68 { "peer",		T_Peer,			FOLLBY_STRING },
     69 { "phone",		T_Phone,		FOLLBY_STRINGS_TO_EOC },
     70 { "pidfile",		T_Pidfile,		FOLLBY_STRING },
     71 { "pollskewlist",	T_PollSkewList,		FOLLBY_TOKEN },
     72 { "pool",		T_Pool,			FOLLBY_STRING },
     73 { "discard",		T_Discard,		FOLLBY_TOKEN },
     74 { "reset",		T_Reset,		FOLLBY_TOKEN },
     75 { "restrict",		T_Restrict,		FOLLBY_TOKEN },
     76 { "rlimit",		T_Rlimit,		FOLLBY_TOKEN },
     77 { "server",		T_Server,		FOLLBY_STRING },
     78 { "serverresponse",	T_Serverresponse,	FOLLBY_TOKEN },
     79 { "fuzz",		T_Fuzz,			FOLLBY_TOKEN },
     80 { "poll",		T_Poll,			FOLLBY_TOKEN },
     81 { "setvar",		T_Setvar,		FOLLBY_STRING },
     82 { "statistics",		T_Statistics,		FOLLBY_TOKEN },
     83 { "statsdir",		T_Statsdir,		FOLLBY_STRING },
     84 { "sys",		T_Sys,			FOLLBY_TOKEN },
     85 { "tick",		T_Tick,			FOLLBY_TOKEN },
     86 { "timer",		T_Timer,		FOLLBY_TOKEN },
     87 { "tinker",		T_Tinker,		FOLLBY_TOKEN },
     88 { "tos",		T_Tos,			FOLLBY_TOKEN },
     89 { "trap",		T_Trap,			FOLLBY_STRING },
     90 { "unconfig",		T_Unconfig,		FOLLBY_STRING },
     91 { "unpeer",		T_Unpeer,		FOLLBY_STRING },
     92 { "xmtnonce",		T_Xmtnonce,		FOLLBY_TOKEN },
     93 /* authentication_command */
     94 { "controlkey",		T_ControlKey,		FOLLBY_TOKEN },
     95 { "crypto",		T_Crypto,		FOLLBY_TOKEN },
     96 { "keys",		T_Keys,			FOLLBY_STRING },
     97 { "keysdir",		T_Keysdir,		FOLLBY_STRING },
     98 { "ntpsigndsocket",	T_NtpSignDsocket,	FOLLBY_STRING },
     99 { "requestkey",		T_Requestkey,		FOLLBY_TOKEN },
    100 { "revoke",		T_Revoke,		FOLLBY_TOKEN },
    101 { "trustedkey",		T_Trustedkey,		FOLLBY_TOKEN },
    102 /* IPv4/IPv6 protocol override flag */
    103 { "-4",			T_Ipv4_flag,		FOLLBY_TOKEN },
    104 { "-6",			T_Ipv6_flag,		FOLLBY_TOKEN },
    105 /* option */
    106 { "autokey",		T_Autokey,		FOLLBY_TOKEN },
    107 { "burst",		T_Burst,		FOLLBY_TOKEN },
    108 { "iburst",		T_Iburst,		FOLLBY_TOKEN },
    109 { "key",		T_Key,			FOLLBY_TOKEN },
    110 { "maxpoll",		T_Maxpoll,		FOLLBY_TOKEN },
    111 { "mdnstries",		T_Mdnstries,		FOLLBY_TOKEN },
    112 { "minpoll",		T_Minpoll,		FOLLBY_TOKEN },
    113 { "mode",		T_Mode,			FOLLBY_TOKEN },
    114 { "noselect",		T_Noselect,		FOLLBY_TOKEN },
    115 { "preempt",		T_Preempt,		FOLLBY_TOKEN },
    116 { "true",		T_True,			FOLLBY_TOKEN },
    117 { "prefer",		T_Prefer,		FOLLBY_TOKEN },
    118 { "ttl",		T_Ttl,			FOLLBY_TOKEN },
    119 { "version",		T_Version,		FOLLBY_TOKEN },
    120 { "xleave",		T_Xleave,		FOLLBY_TOKEN },
    121 /* crypto_command */
    122 { "host",		T_Host,			FOLLBY_STRING },
    123 { "ident",		T_Ident,		FOLLBY_STRING },
    124 { "pw",			T_Pw,			FOLLBY_STRING },
    125 { "randfile",		T_Randfile,		FOLLBY_STRING },
    126 { "digest",		T_Digest,		FOLLBY_STRING },
    127 /*** MONITORING COMMANDS ***/
    128 /* stat */
    129 { "clockstats",		T_Clockstats,		FOLLBY_TOKEN },
    130 { "cryptostats",	T_Cryptostats,		FOLLBY_TOKEN },
    131 { "loopstats",		T_Loopstats,		FOLLBY_TOKEN },
    132 { "peerstats",		T_Peerstats,		FOLLBY_TOKEN },
    133 { "rawstats",		T_Rawstats,		FOLLBY_TOKEN },
    134 { "sysstats", 		T_Sysstats,		FOLLBY_TOKEN },
    135 { "protostats",		T_Protostats,		FOLLBY_TOKEN },
    136 { "timingstats",	T_Timingstats,		FOLLBY_TOKEN },
    137 /* filegen_option */
    138 { "file",		T_File,			FOLLBY_STRING },
    139 { "link",		T_Link,			FOLLBY_TOKEN },
    140 { "nolink",		T_Nolink,		FOLLBY_TOKEN },
    141 { "type",		T_Type,			FOLLBY_TOKEN },
    142 /* filegen_type */
    143 { "age",		T_Age,			FOLLBY_TOKEN },
    144 { "day",		T_Day,			FOLLBY_TOKEN },
    145 { "month",		T_Month,		FOLLBY_TOKEN },
    146 { "none",		T_None,			FOLLBY_TOKEN },
    147 { "pid",		T_Pid,			FOLLBY_TOKEN },
    148 { "week",		T_Week,			FOLLBY_TOKEN },
    149 { "year",		T_Year,			FOLLBY_TOKEN },
    150 /*** ORPHAN MODE COMMANDS ***/
    151 /* tos_option */
    152 { "minclock",		T_Minclock,		FOLLBY_TOKEN },
    153 { "maxclock",		T_Maxclock,		FOLLBY_TOKEN },
    154 { "minsane",		T_Minsane,		FOLLBY_TOKEN },
    155 { "floor",		T_Floor,		FOLLBY_TOKEN },
    156 { "ceiling",		T_Ceiling,		FOLLBY_TOKEN },
    157 { "cohort",		T_Cohort,		FOLLBY_TOKEN },
    158 { "mindist",		T_Mindist,		FOLLBY_TOKEN },
    159 { "maxdist",		T_Maxdist,		FOLLBY_TOKEN },
    160 { "bcpollbstep",	T_Bcpollbstep,		FOLLBY_TOKEN },
    161 { "beacon",		T_Beacon,		FOLLBY_TOKEN },
    162 { "orphan",		T_Orphan,		FOLLBY_TOKEN },
    163 { "orphanwait",		T_Orphanwait,		FOLLBY_TOKEN },
    164 { "nonvolatile",	T_Nonvolatile,		FOLLBY_TOKEN },
    165 { "basedate",		T_Basedate,		FOLLBY_STRING },
    166 /* access_control_flag */
    167 { "default",		T_Default,		FOLLBY_TOKEN },
    168 { "source",		T_Source,		FOLLBY_TOKEN },
    169 { "epeer",		T_Epeer,		FOLLBY_TOKEN },
    170 { "noepeer",		T_Noepeer,		FOLLBY_TOKEN },
    171 { "flake",		T_Flake,		FOLLBY_TOKEN },
    172 { "ignore",		T_Ignore,		FOLLBY_TOKEN },
    173 { "ippeerlimit",	T_Ippeerlimit,		FOLLBY_TOKEN },
    174 { "limited",		T_Limited,		FOLLBY_TOKEN },
    175 { "mssntp",		T_Mssntp,		FOLLBY_TOKEN },
    176 { "kod",		T_Kod,			FOLLBY_TOKEN },
    177 { "lowpriotrap",	T_Lowpriotrap,		FOLLBY_TOKEN },
    178 { "mask",		T_Mask,			FOLLBY_TOKEN },
    179 { "nomodify",		T_Nomodify,		FOLLBY_TOKEN },
    180 { "nomrulist",		T_Nomrulist,		FOLLBY_TOKEN },
    181 { "nopeer",		T_Nopeer,		FOLLBY_TOKEN },
    182 { "noquery",		T_Noquery,		FOLLBY_TOKEN },
    183 { "noserve",		T_Noserve,		FOLLBY_TOKEN },
    184 { "notrap",		T_Notrap,		FOLLBY_TOKEN },
    185 { "notrust",		T_Notrust,		FOLLBY_TOKEN },
    186 { "ntpport",		T_Ntpport,		FOLLBY_TOKEN },
    187 /* discard_option */
    188 { "average",		T_Average,		FOLLBY_TOKEN },
    189 { "minimum",		T_Minimum,		FOLLBY_TOKEN },
    190 { "monitor",		T_Monitor,		FOLLBY_TOKEN },
    191 /* mru_option */
    192 { "incalloc",		T_Incalloc,		FOLLBY_TOKEN },
    193 { "incmem",		T_Incmem,		FOLLBY_TOKEN },
    194 { "initalloc",		T_Initalloc,		FOLLBY_TOKEN },
    195 { "initmem",		T_Initmem,		FOLLBY_TOKEN },
    196 { "mindepth",		T_Mindepth,		FOLLBY_TOKEN },
    197 { "maxage",		T_Maxage,		FOLLBY_TOKEN },
    198 { "maxdepth",		T_Maxdepth,		FOLLBY_TOKEN },
    199 { "maxmem",		T_Maxmem,		FOLLBY_TOKEN },
    200 { "mru",		T_Mru,			FOLLBY_TOKEN },
    201 /* fudge_factor */
    202 { "abbrev",		T_Abbrev,		FOLLBY_STRING },
    203 { "flag1",		T_Flag1,		FOLLBY_TOKEN },
    204 { "flag2",		T_Flag2,		FOLLBY_TOKEN },
    205 { "flag3",		T_Flag3,		FOLLBY_TOKEN },
    206 { "flag4",		T_Flag4,		FOLLBY_TOKEN },
    207 { "refid",		T_Refid,		FOLLBY_STRING },
    208 { "stratum",		T_Stratum,		FOLLBY_TOKEN },
    209 { "time1",		T_Time1,		FOLLBY_TOKEN },
    210 { "time2",		T_Time2,		FOLLBY_TOKEN },
    211 { "minjitter",		T_Minjitter,		FOLLBY_TOKEN },
    212 /* system_option */
    213 { "auth",		T_Auth,			FOLLBY_TOKEN },
    214 { "bclient",		T_Bclient,		FOLLBY_TOKEN },
    215 { "calibrate",		T_Calibrate,		FOLLBY_TOKEN },
    216 { "kernel",		T_Kernel,		FOLLBY_TOKEN },
    217 { "mode7",		T_Mode7,		FOLLBY_TOKEN },
    218 { "ntp",		T_Ntp,			FOLLBY_TOKEN },
    219 { "peer_clear_digest_early",	T_PCEdigest,	FOLLBY_TOKEN },
    220 { "stats",		T_Stats,		FOLLBY_TOKEN },
    221 { "unpeer_crypto_early",	T_UEcrypto,	FOLLBY_TOKEN },
    222 { "unpeer_crypto_nak_early",	T_UEcryptonak,	FOLLBY_TOKEN },
    223 { "unpeer_digest_early",	T_UEdigest,	FOLLBY_TOKEN },
    224 /* rlimit_option */
    225 { "memlock",		T_Memlock,		FOLLBY_TOKEN },
    226 { "stacksize",		T_Stacksize,		FOLLBY_TOKEN },
    227 { "filenum",		T_Filenum,		FOLLBY_TOKEN },
    228 /* tinker_option */
    229 { "step",		T_Step,			FOLLBY_TOKEN },
    230 { "stepback",		T_Stepback,		FOLLBY_TOKEN },
    231 { "stepfwd",		T_Stepfwd,		FOLLBY_TOKEN },
    232 { "panic",		T_Panic,		FOLLBY_TOKEN },
    233 { "dispersion",		T_Dispersion,		FOLLBY_TOKEN },
    234 { "stepout",		T_Stepout,		FOLLBY_TOKEN },
    235 { "allan",		T_Allan,		FOLLBY_TOKEN },
    236 { "huffpuff",		T_Huffpuff,		FOLLBY_TOKEN },
    237 { "freq",		T_Freq,			FOLLBY_TOKEN },
    238 /* miscellaneous_command */
    239 { "port",		T_Port,			FOLLBY_TOKEN },
    240 { "interface",		T_Interface,		FOLLBY_TOKEN },
    241 { "saveconfigdir",	T_Saveconfigdir,	FOLLBY_STRING },
    242 /* interface_command (ignore and interface already defined) */
    243 { "nic",		T_Nic,			FOLLBY_TOKEN },
    244 { "all",		T_All,			FOLLBY_TOKEN },
    245 { "ipv4",		T_Ipv4,			FOLLBY_TOKEN },
    246 { "ipv6",		T_Ipv6,			FOLLBY_TOKEN },
    247 { "wildcard",		T_Wildcard,		FOLLBY_TOKEN },
    248 { "listen",		T_Listen,		FOLLBY_TOKEN },
    249 { "drop",		T_Drop,			FOLLBY_TOKEN },
    250 /* simulator commands */
    251 { "simulate",		T_Simulate,		FOLLBY_TOKEN },
    252 { "simulation_duration",T_Sim_Duration,		FOLLBY_TOKEN },
    253 { "beep_delay",		T_Beep_Delay,		FOLLBY_TOKEN },
    254 { "duration",		T_Duration,		FOLLBY_TOKEN },
    255 { "server_offset",	T_Server_Offset,	FOLLBY_TOKEN },
    256 { "freq_offset",	T_Freq_Offset,		FOLLBY_TOKEN },
    257 { "wander",		T_Wander,		FOLLBY_TOKEN },
    258 { "jitter",		T_Jitter,		FOLLBY_TOKEN },
    259 { "prop_delay",		T_Prop_Delay,		FOLLBY_TOKEN },
    260 { "proc_delay",		T_Proc_Delay,		FOLLBY_TOKEN },
    261 };
    262 
    263 typedef struct big_scan_state_tag {
    264 	char	ch;		/* Character this state matches on */
    265 	char	followedby;	/* Forces next token(s) to T_String */
    266 	u_short	finishes_token;	/* nonzero ID if last keyword char */
    267 	u_short	match_next_s;	/* next state to check matching ch */
    268 	u_short	other_next_s;	/* next state to check if not ch */
    269 } big_scan_state;
    270 
    271 /*
    272  * Note: to increase MAXSTATES beyond 2048, be aware it is currently
    273  * crammed into 11 bits in scan_state form.  Raising to 4096 would be
    274  * relatively easy by storing the followedby value in a separate
    275  * array with one entry per token, and shrinking the char value to
    276  * 7 bits to free a bit for accepting/non-accepting.  More than 4096
    277  * states will require expanding scan_state beyond 32 bits each.
    278  */
    279 #define MAXSTATES	2048
    280 #define MAX_TOK_LEN	63
    281 
    282 const char *	current_keyword;/* for error reporting */
    283 big_scan_state	sst[MAXSTATES];	/* scanner FSM state entries */
    284 u_short		sst_highwater;	/* next entry index to consider */
    285 char *		symb[1024];	/* map token ID to symbolic name */
    286 
    287 /* for libntp */
    288 const char *	progname = "keyword-gen";
    289 
    290 int		main			(int, char **);
    291 static void	generate_preamble	(void);
    292 static void	generate_fsm		(void);
    293 static void	generate_token_text	(void);
    294 static u_short	create_keyword_scanner	(void);
    295 static u_short	create_scan_states	(char *, u_short, follby, u_short);
    296 int		compare_key_tok_id	(const void *, const void *);
    297 int		compare_key_tok_text	(const void *, const void *);
    298 void		populate_symb		(char *);
    299 const char *	symbname		(u_short);
    300 
    301 
    302 int main(int argc, char **argv)
    303 {
    304 	if (argc < 2) {
    305 		fprintf(stderr, "Usage:\n%s t_header.h\n", argv[0]);
    306 		exit(1);
    307 	}
    308 	debug = 1;
    309 
    310 	populate_symb(argv[1]);
    311 
    312 	generate_preamble();
    313 	generate_token_text();
    314 	generate_fsm();
    315 
    316 	return 0;
    317 }
    318 
    319 
    320 static void
    321 generate_preamble(void)
    322 {
    323 	time_t now;
    324 	char timestamp[128];
    325 	char preamble[] =
    326 "/*\n"
    327 " * ntp_keyword.h\n"
    328 " * \n"
    329 " * NOTE: edit this file with caution, it is generated by keyword-gen.c\n"
    330 " *\t Generated %s UTC	  diff_ignore_line\n"
    331 " *\n"
    332 " */\n"
    333 "#include \"ntp_scanner.h\"\n"
    334 "#include \"ntp_parser.h\"\n"
    335 "\n";
    336 
    337 	time(&now);
    338 	if (!strftime(timestamp, sizeof(timestamp),
    339 		      "%Y-%m-%d %H:%M:%S", gmtime(&now)))
    340 		timestamp[0] = '\0';
    341 
    342 	printf(preamble, timestamp);
    343 }
    344 
    345 
    346 static void
    347 generate_fsm(void)
    348 {
    349 	char rprefix[MAX_TOK_LEN + 1];
    350 	char prefix[MAX_TOK_LEN + 1];
    351 	char token_id_comment[16 + MAX_TOK_LEN + 1];
    352 	size_t prefix_len;
    353 	char *p;
    354 	char *r;
    355 	u_short initial_state;
    356 	u_short this_state;
    357 	u_short state;
    358 	u_short i;
    359 	u_short token;
    360 
    361 	/*
    362 	 * Sort ntp_keywords in alphabetical keyword order.  This is
    363 	 * not necessary, but minimizes nonfunctional changes in the
    364 	 * generated finite state machine when keywords are modified.
    365 	 */
    366 	qsort(ntp_keywords, COUNTOF(ntp_keywords),
    367 	      sizeof(ntp_keywords[0]), compare_key_tok_text);
    368 
    369 	/*
    370 	 * To save space, reserve the state array entry matching each
    371 	 * token number for its terminal state, so the token identifier
    372 	 * does not need to be stored in each state, but can be
    373 	 * recovered trivially.  To mark the entry reserved,
    374 	 * finishes_token is nonzero.
    375 	 */
    376 
    377 	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
    378 		token = ntp_keywords[i].token;
    379 		if (1 > token || token >= COUNTOF(sst)) {
    380 			fprintf(stderr,
    381 				"keyword-gen sst[%u] too small "
    382 				"for keyword '%s' id %d\n",
    383 				(int)COUNTOF(sst),
    384 				ntp_keywords[i].key,
    385 				token);
    386 			exit(4);
    387 		}
    388 		sst[token].finishes_token = token;
    389 	}
    390 
    391 	initial_state = create_keyword_scanner();
    392 
    393 	fprintf(stderr,
    394 		"%d keywords consumed %d states of %d max.\n",
    395 		(int)COUNTOF(ntp_keywords),
    396 		sst_highwater - 1,
    397 		(int)COUNTOF(sst) - 1);
    398 
    399 	printf("#define SCANNER_INIT_S %d\n\n", initial_state);
    400 
    401 	printf("const scan_state sst[%d] = {\n"
    402 	       "/*SS_T( ch,\tf-by, match, other ),\t\t\t\t */\n"
    403 	       "  0,\t\t\t\t      /* %5d %-17s */\n",
    404 	       sst_highwater,
    405 	       0, "");
    406 
    407 	for (i = 1; i < sst_highwater; i++) {
    408 
    409 		/* verify fields will fit */
    410 		if (sst[i].followedby & ~0x3) {
    411 			fprintf(stderr,
    412 				"keyword-gen internal error "
    413 				"sst[%d].followedby %d too big\n",
    414 				i, sst[i].followedby);
    415 			exit(7);
    416 		}
    417 
    418 		if (sst_highwater <= sst[i].match_next_s
    419 		    || sst[i].match_next_s & ~0x7ff) {
    420 			fprintf(stderr,
    421 				"keyword-gen internal error "
    422 				"sst[%d].match_next_s %d too big\n",
    423 				i, sst[i].match_next_s);
    424 			exit(8);
    425 		}
    426 
    427 		if (sst_highwater <= sst[i].other_next_s
    428 		    || sst[i].other_next_s & ~0x7ff) {
    429 			fprintf(stderr,
    430 				"keyword-gen internal error "
    431 				"sst[%d].other_next_s %d too big\n",
    432 				i, sst[i].other_next_s);
    433 			exit(9);
    434 		}
    435 
    436 		if (sst[i].finishes_token) {
    437 			snprintf(token_id_comment,
    438 				 sizeof(token_id_comment), "%5d %-17s",
    439 				 i, symbname(sst[i].finishes_token));
    440 			if (i != sst[i].finishes_token) {
    441 				fprintf(stderr,
    442 					"keyword-gen internal error "
    443 					"entry %d finishes token %d\n",
    444 					i, sst[i].finishes_token);
    445 				exit(5);
    446 			}
    447 		} else {
    448 		/*
    449 		 * Determine the keyword prefix that leads to this
    450 		 * state.  This is expensive but keyword-gen is run
    451 		 * only when it changes.  Distributing keyword-gen-utd
    452 		 * achieves that, which is why it must be committed
    453 		 * at the same time as keyword-gen.c and ntp_keyword.h.
    454 		 *
    455 		 * Scan the state array iteratively looking for a state
    456 		 * which leads to the current one, collecting matching
    457 		 * characters along the way.  There is only one such
    458 		 * path back to the starting state given the way our
    459 		 * scanner state machine is built and the practice of
    460 		 * using the spelling of the keyword as its T_* token
    461 		 * identifier, which results in never having two
    462 		 * spellings result in the same T_* value.
    463 		 */
    464 			prefix_len = 0;
    465 			this_state = i;
    466 			do {
    467 				for (state = 1; state < sst_highwater; state++)
    468 					if (sst[state].other_next_s == this_state) {
    469 						this_state = state;
    470 						break;
    471 					} else if (sst[state].match_next_s == this_state) {
    472 						this_state = state;
    473 						rprefix[prefix_len] = sst[state].ch;
    474 						prefix_len++;
    475 						break;
    476 					}
    477 			} while (this_state != initial_state);
    478 
    479 			if (prefix_len) {
    480 				/* reverse rprefix into prefix */
    481 				p = prefix + prefix_len;
    482 				r = rprefix;
    483 				while (r < rprefix + prefix_len)
    484 					*--p = *r++;
    485 			}
    486 			prefix[prefix_len] = '\0';
    487 
    488 			snprintf(token_id_comment,
    489 				 sizeof(token_id_comment), "%5d %-17s",
    490 				 i, (initial_state == i)
    491 					? "[initial state]"
    492 					: prefix);
    493 		}
    494 
    495 		printf("  S_ST( '%c',\t%d,    %5u, %5u )%s /* %s */\n",
    496 		       sst[i].ch,
    497 		       sst[i].followedby,
    498 		       sst[i].match_next_s,
    499 		       sst[i].other_next_s,
    500 		       (i + 1 < sst_highwater)
    501 			   ? ","
    502 			   : " ",
    503 		       token_id_comment);
    504 	}
    505 
    506 	printf("};\n\n");
    507 }
    508 
    509 
    510 /* Define a function to create the states of the scanner. This function
    511  * is used by the create_keyword_scanner function below.
    512  *
    513  * This function takes a suffix of a keyword, the token to be returned on
    514  * recognizing the complete keyword, and any pre-existing state that exists
    515  * for some other keyword that has the same prefix as the current one.
    516  */
    517 static u_short
    518 create_scan_states(
    519 	char *	text,
    520 	u_short	token,
    521 	follby	followedby,
    522 	u_short	prev_state
    523 	)
    524 {
    525 	u_short my_state;
    526 	u_short return_state;
    527 	u_short prev_char_s;
    528 	u_short curr_char_s;
    529 
    530 	return_state = prev_state;
    531 	curr_char_s = prev_state;
    532 	prev_char_s = 0;
    533 
    534 	/* Find the correct position to insert the state.
    535 	 * All states should be in alphabetical order
    536 	 */
    537 	while (curr_char_s && (text[0] < sst[curr_char_s].ch)) {
    538 		prev_char_s = curr_char_s;
    539 		curr_char_s = sst[curr_char_s].other_next_s;
    540 	}
    541 
    542 	/*
    543 	 * Check if a previously seen keyword has the same prefix as
    544 	 * the current keyword.  If so, simply use the state for that
    545 	 * keyword as my_state, otherwise, allocate a new state.
    546 	 */
    547 	if (curr_char_s && (text[0] == sst[curr_char_s].ch)) {
    548 		my_state = curr_char_s;
    549 		if ('\0' == text[1]) {
    550 			fprintf(stderr,
    551 				"Duplicate entries for keyword '%s' in"
    552 				" keyword_gen.c ntp_keywords[].\n",
    553 				current_keyword);
    554 			exit(2);
    555 		}
    556 	} else {
    557 		do
    558 			my_state = sst_highwater++;
    559 		while (my_state < COUNTOF(sst)
    560 		       && sst[my_state].finishes_token);
    561 		if (my_state >= COUNTOF(sst)) {
    562 			fprintf(stderr,
    563 				"fatal, keyword scanner state array "
    564 				"sst[%d] is too small, modify\n"
    565 				"keyword-gen.c to increase.\n",
    566 				(int)COUNTOF(sst));
    567 			exit(3);
    568 		}
    569 		/* Store the next character of the keyword */
    570 		sst[my_state].ch = text[0];
    571 		sst[my_state].other_next_s = curr_char_s;
    572 		sst[my_state].followedby = FOLLBY_NON_ACCEPTING;
    573 
    574 		if (prev_char_s)
    575 			sst[prev_char_s].other_next_s = my_state;
    576 		else
    577 			return_state = my_state;
    578 	}
    579 
    580 	/* Check if the next character is '\0'.
    581 	 * If yes, we are done with the recognition and this is an accepting
    582 	 * state.
    583 	 * If not, we need to continue scanning
    584 	 */
    585 	if ('\0' == text[1]) {
    586 		sst[my_state].finishes_token = (u_short)token;
    587 		sst[my_state].followedby = (char)followedby;
    588 
    589 		if (sst[token].finishes_token != (u_short)token) {
    590 			fprintf(stderr,
    591 				"fatal, sst[%d] not reserved for %s.\n",
    592 				token, symbname(token));
    593 			exit(6);
    594 		}
    595 		/* relocate so token id is sst[] index */
    596 		if (my_state != token) {
    597 			sst[token] = sst[my_state];
    598 			ZERO(sst[my_state]);
    599 			do
    600 				sst_highwater--;
    601 			while (sst[sst_highwater].finishes_token);
    602 			my_state = token;
    603 			if (prev_char_s)
    604 				sst[prev_char_s].other_next_s = my_state;
    605 			else
    606 				return_state = my_state;
    607 		}
    608 	} else
    609 		sst[my_state].match_next_s =
    610 		    create_scan_states(
    611 			&text[1],
    612 			token,
    613 			followedby,
    614 			sst[my_state].match_next_s);
    615 
    616 	return return_state;
    617 }
    618 
    619 
    620 /* Define a function that takes a list of (keyword, token) values and
    621  * creates a keywords scanner out of it.
    622  */
    623 
    624 static u_short
    625 create_keyword_scanner(void)
    626 {
    627 	u_short scanner;
    628 	u_short i;
    629 
    630 	sst_highwater = 1;	/* index 0 invalid, unused */
    631 	scanner = 0;
    632 
    633 	for (i = 0; i < COUNTOF(ntp_keywords); i++) {
    634 		current_keyword = ntp_keywords[i].key;
    635 		scanner =
    636 		    create_scan_states(
    637 			ntp_keywords[i].key,
    638 			ntp_keywords[i].token,
    639 			ntp_keywords[i].followedby,
    640 			scanner);
    641 	}
    642 
    643 	return scanner;
    644 }
    645 
    646 
    647 static void
    648 generate_token_text(void)
    649 {
    650 	u_short lowest_id;
    651 	u_short highest_id;
    652 	u_short id_count;
    653 	u_short id;
    654 	u_short i;
    655 
    656 	/* sort ntp_keywords in token ID order */
    657 	qsort(ntp_keywords, COUNTOF(ntp_keywords),
    658 	      sizeof(ntp_keywords[0]), compare_key_tok_id);
    659 
    660 	lowest_id = ntp_keywords[0].token;
    661 	highest_id = ntp_keywords[COUNTOF(ntp_keywords) - 1].token;
    662 	id_count = highest_id - lowest_id + 1;
    663 
    664 	printf("#define LOWEST_KEYWORD_ID %d\n\n", lowest_id);
    665 
    666 	printf("const char * const keyword_text[%d] = {", id_count);
    667 
    668 	id = lowest_id;
    669 	i = 0;
    670 	while (i < COUNTOF(ntp_keywords)) {
    671 		while (id < ntp_keywords[i].token) {
    672 			printf(",\n\t/* %-5d %5d %20s */\tNULL",
    673 			       id - lowest_id, id, symbname(id));
    674 			id++;
    675 		}
    676 		if (i > 0)
    677 			printf(",");
    678 		printf("\n\t/* %-5d %5d %20s */\t\"%s\"",
    679 		       id - lowest_id, id, symbname(id),
    680 		       ntp_keywords[i].key);
    681 		i++;
    682 		id++;
    683 	}
    684 
    685 	printf("\n};\n\n");
    686 }
    687 
    688 
    689 int
    690 compare_key_tok_id(
    691 	const void *a1,
    692 	const void *a2
    693 	)
    694 {
    695 	const struct key_tok *p1 = a1;
    696 	const struct key_tok *p2 = a2;
    697 
    698 	if (p1->token == p2->token)
    699 		return 0;
    700 
    701 	if (p1->token < p2->token)
    702 		return -1;
    703 	else
    704 		return 1;
    705 }
    706 
    707 
    708 int
    709 compare_key_tok_text(
    710 	const void *a1,
    711 	const void *a2
    712 	)
    713 {
    714 	const struct key_tok *p1 = a1;
    715 	const struct key_tok *p2 = a2;
    716 
    717 	return strcmp(p1->key, p2->key);
    718 }
    719 
    720 
    721 /*
    722  * populate_symb() - populate symb[] lookup array with symbolic token
    723  *		     names such that symb[T_Age] == "T_Age", etc.
    724  */
    725 void
    726 populate_symb(
    727 	char *header_file
    728 	)
    729 {
    730 	FILE *	yh;
    731 	char	line[2 * MAX_TOK_LEN];
    732 	char	name[2 * MAX_TOK_LEN];
    733 	int	token;
    734 
    735 	yh = fopen(header_file, "r");
    736 	if (NULL == yh) {
    737 		perror("unable to open yacc/bison header file");
    738 		exit(4);
    739 	}
    740 
    741 	while (NULL != fgets(line, sizeof(line), yh))
    742 		if (2 == sscanf(line, "#define %s %d", name, &token)
    743 		    && 'T' == name[0] && '_' == name[1] && token >= 0
    744 		    && token < COUNTOF(symb)) {
    745 
    746 			symb[token] = estrdup(name);
    747 			if (strlen(name) > MAX_TOK_LEN) {
    748 				fprintf(stderr,
    749 					"MAX_TOK_LEN %d too small for '%s'\n"
    750 					"Edit keyword-gen.c to raise.\n",
    751 					MAX_TOK_LEN, name);
    752 				exit(10);
    753 			}
    754 		}
    755 	fclose(yh);
    756 }
    757 
    758 
    759 const char *
    760 symbname(
    761 	u_short token
    762 	)
    763 {
    764 	char *name;
    765 
    766 	if (token < COUNTOF(symb) && symb[token] != NULL) {
    767 		name = symb[token];
    768 	} else {
    769 		LIB_GETBUF(name);
    770 		snprintf(name, LIB_BUFLENGTH, "%d", token);
    771 	}
    772 
    773 	return name;
    774 }
    775