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