keyword-gen.c revision 1.1.1.8 1 /* $NetBSD: keyword-gen.c,v 1.1.1.8 2016/01/08 21:21:26 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 { "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 { "beacon", T_Beacon, FOLLBY_TOKEN },
154 { "orphan", T_Orphan, FOLLBY_TOKEN },
155 { "orphanwait", T_Orphanwait, FOLLBY_TOKEN },
156 { "nonvolatile", T_Nonvolatile, FOLLBY_TOKEN },
157 /* access_control_flag */
158 { "default", T_Default, FOLLBY_TOKEN },
159 { "source", T_Source, FOLLBY_TOKEN },
160 { "flake", T_Flake, FOLLBY_TOKEN },
161 { "ignore", T_Ignore, FOLLBY_TOKEN },
162 { "limited", T_Limited, FOLLBY_TOKEN },
163 { "mssntp", T_Mssntp, FOLLBY_TOKEN },
164 { "kod", T_Kod, FOLLBY_TOKEN },
165 { "lowpriotrap", T_Lowpriotrap, FOLLBY_TOKEN },
166 { "mask", T_Mask, FOLLBY_TOKEN },
167 { "nomodify", T_Nomodify, FOLLBY_TOKEN },
168 { "nomrulist", T_Nomrulist, FOLLBY_TOKEN },
169 { "nopeer", T_Nopeer, FOLLBY_TOKEN },
170 { "noquery", T_Noquery, FOLLBY_TOKEN },
171 { "noserve", T_Noserve, FOLLBY_TOKEN },
172 { "notrap", T_Notrap, FOLLBY_TOKEN },
173 { "notrust", T_Notrust, FOLLBY_TOKEN },
174 { "ntpport", T_Ntpport, FOLLBY_TOKEN },
175 /* discard_option */
176 { "average", T_Average, FOLLBY_TOKEN },
177 { "minimum", T_Minimum, FOLLBY_TOKEN },
178 { "monitor", T_Monitor, FOLLBY_TOKEN },
179 /* mru_option */
180 { "incalloc", T_Incalloc, FOLLBY_TOKEN },
181 { "incmem", T_Incmem, FOLLBY_TOKEN },
182 { "initalloc", T_Initalloc, FOLLBY_TOKEN },
183 { "initmem", T_Initmem, FOLLBY_TOKEN },
184 { "mindepth", T_Mindepth, FOLLBY_TOKEN },
185 { "maxage", T_Maxage, FOLLBY_TOKEN },
186 { "maxdepth", T_Maxdepth, FOLLBY_TOKEN },
187 { "maxmem", T_Maxmem, FOLLBY_TOKEN },
188 { "mru", T_Mru, FOLLBY_TOKEN },
189 /* fudge_factor */
190 { "abbrev", T_Abbrev, FOLLBY_STRING },
191 { "flag1", T_Flag1, FOLLBY_TOKEN },
192 { "flag2", T_Flag2, FOLLBY_TOKEN },
193 { "flag3", T_Flag3, FOLLBY_TOKEN },
194 { "flag4", T_Flag4, FOLLBY_TOKEN },
195 { "refid", T_Refid, FOLLBY_STRING },
196 { "stratum", T_Stratum, FOLLBY_TOKEN },
197 { "time1", T_Time1, FOLLBY_TOKEN },
198 { "time2", T_Time2, FOLLBY_TOKEN },
199 /* system_option */
200 { "auth", T_Auth, FOLLBY_TOKEN },
201 { "bclient", T_Bclient, FOLLBY_TOKEN },
202 { "calibrate", T_Calibrate, FOLLBY_TOKEN },
203 { "kernel", T_Kernel, FOLLBY_TOKEN },
204 { "ntp", T_Ntp, FOLLBY_TOKEN },
205 { "mode7", T_Mode7, FOLLBY_TOKEN },
206 { "stats", T_Stats, FOLLBY_TOKEN },
207 /* rlimit_option */
208 { "memlock", T_Memlock, FOLLBY_TOKEN },
209 { "stacksize", T_Stacksize, FOLLBY_TOKEN },
210 { "filenum", T_Filenum, FOLLBY_TOKEN },
211 /* tinker_option */
212 { "step", T_Step, FOLLBY_TOKEN },
213 { "stepback", T_Stepback, FOLLBY_TOKEN },
214 { "stepfwd", T_Stepfwd, FOLLBY_TOKEN },
215 { "panic", T_Panic, FOLLBY_TOKEN },
216 { "dispersion", T_Dispersion, FOLLBY_TOKEN },
217 { "stepout", T_Stepout, FOLLBY_TOKEN },
218 { "allan", T_Allan, FOLLBY_TOKEN },
219 { "huffpuff", T_Huffpuff, FOLLBY_TOKEN },
220 { "freq", T_Freq, FOLLBY_TOKEN },
221 /* miscellaneous_command */
222 { "port", T_Port, FOLLBY_TOKEN },
223 { "interface", T_Interface, FOLLBY_TOKEN },
224 { "saveconfigdir", T_Saveconfigdir, FOLLBY_STRING },
225 /* interface_command (ignore and interface already defined) */
226 { "nic", T_Nic, FOLLBY_TOKEN },
227 { "all", T_All, FOLLBY_TOKEN },
228 { "ipv4", T_Ipv4, FOLLBY_TOKEN },
229 { "ipv6", T_Ipv6, FOLLBY_TOKEN },
230 { "wildcard", T_Wildcard, FOLLBY_TOKEN },
231 { "listen", T_Listen, FOLLBY_TOKEN },
232 { "drop", T_Drop, FOLLBY_TOKEN },
233 /* simulator commands */
234 { "simulate", T_Simulate, FOLLBY_TOKEN },
235 { "simulation_duration",T_Sim_Duration, FOLLBY_TOKEN },
236 { "beep_delay", T_Beep_Delay, FOLLBY_TOKEN },
237 { "duration", T_Duration, FOLLBY_TOKEN },
238 { "server_offset", T_Server_Offset, FOLLBY_TOKEN },
239 { "freq_offset", T_Freq_Offset, FOLLBY_TOKEN },
240 { "wander", T_Wander, FOLLBY_TOKEN },
241 { "jitter", T_Jitter, FOLLBY_TOKEN },
242 { "prop_delay", T_Prop_Delay, FOLLBY_TOKEN },
243 { "proc_delay", T_Proc_Delay, FOLLBY_TOKEN },
244 };
245
246 typedef struct big_scan_state_tag {
247 char ch; /* Character this state matches on */
248 char followedby; /* Forces next token(s) to T_String */
249 u_short finishes_token; /* nonzero ID if last keyword char */
250 u_short match_next_s; /* next state to check matching ch */
251 u_short other_next_s; /* next state to check if not ch */
252 } big_scan_state;
253
254 /*
255 * Note: to increase MAXSTATES beyond 2048, be aware it is currently
256 * crammed into 11 bits in scan_state form. Raising to 4096 would be
257 * relatively easy by storing the followedby value in a separate
258 * array with one entry per token, and shrinking the char value to
259 * 7 bits to free a bit for accepting/non-accepting. More than 4096
260 * states will require expanding scan_state beyond 32 bits each.
261 */
262 #define MAXSTATES 2048
263 #define MAX_TOK_LEN 63
264
265 const char * current_keyword;/* for error reporting */
266 big_scan_state sst[MAXSTATES]; /* scanner FSM state entries */
267 u_short sst_highwater; /* next entry index to consider */
268 char * symb[1024]; /* map token ID to symbolic name */
269
270 /* for libntp */
271 const char * progname = "keyword-gen";
272
273 int main (int, char **);
274 static void generate_preamble (void);
275 static void generate_fsm (void);
276 static void generate_token_text (void);
277 static u_short create_keyword_scanner (void);
278 static u_short create_scan_states (char *, u_short, follby, u_short);
279 int compare_key_tok_id (const void *, const void *);
280 int compare_key_tok_text (const void *, const void *);
281 void populate_symb (char *);
282 const char * symbname (u_short);
283
284
285 int main(int argc, char **argv)
286 {
287 if (argc < 2) {
288 fprintf(stderr, "Usage:\n%s t_header.h\n", argv[0]);
289 exit(1);
290 }
291 debug = 1;
292
293 populate_symb(argv[1]);
294
295 generate_preamble();
296 generate_token_text();
297 generate_fsm();
298
299 return 0;
300 }
301
302
303 static void
304 generate_preamble(void)
305 {
306 time_t now;
307 char timestamp[128];
308 char preamble[] =
309 "/*\n"
310 " * ntp_keyword.h\n"
311 " * \n"
312 " * NOTE: edit this file with caution, it is generated by keyword-gen.c\n"
313 " *\t Generated %s UTC diff_ignore_line\n"
314 " *\n"
315 " */\n"
316 "#include \"ntp_scanner.h\"\n"
317 "#include \"ntp_parser.h\"\n"
318 "\n";
319
320 time(&now);
321 if (!strftime(timestamp, sizeof(timestamp),
322 "%Y-%m-%d %H:%M:%S", gmtime(&now)))
323 timestamp[0] = '\0';
324
325 printf(preamble, timestamp);
326 }
327
328
329 static void
330 generate_fsm(void)
331 {
332 char rprefix[MAX_TOK_LEN + 1];
333 char prefix[MAX_TOK_LEN + 1];
334 char token_id_comment[16 + MAX_TOK_LEN + 1];
335 size_t prefix_len;
336 char *p;
337 char *r;
338 u_short initial_state;
339 u_short this_state;
340 u_short state;
341 u_short i;
342 u_short token;
343
344 /*
345 * Sort ntp_keywords in alphabetical keyword order. This is
346 * not necessary, but minimizes nonfunctional changes in the
347 * generated finite state machine when keywords are modified.
348 */
349 qsort(ntp_keywords, COUNTOF(ntp_keywords),
350 sizeof(ntp_keywords[0]), compare_key_tok_text);
351
352 /*
353 * To save space, reserve the state array entry matching each
354 * token number for its terminal state, so the token identifier
355 * does not need to be stored in each state, but can be
356 * recovered trivially. To mark the entry reserved,
357 * finishes_token is nonzero.
358 */
359
360 for (i = 0; i < COUNTOF(ntp_keywords); i++) {
361 token = ntp_keywords[i].token;
362 if (1 > token || token >= COUNTOF(sst)) {
363 fprintf(stderr,
364 "keyword-gen sst[%u] too small "
365 "for keyword '%s' id %d\n",
366 (int)COUNTOF(sst),
367 ntp_keywords[i].key,
368 token);
369 exit(4);
370 }
371 sst[token].finishes_token = token;
372 }
373
374 initial_state = create_keyword_scanner();
375
376 fprintf(stderr,
377 "%d keywords consumed %d states of %d max.\n",
378 (int)COUNTOF(ntp_keywords),
379 sst_highwater - 1,
380 (int)COUNTOF(sst) - 1);
381
382 printf("#define SCANNER_INIT_S %d\n\n", initial_state);
383
384 printf("const scan_state sst[%d] = {\n"
385 "/*SS_T( ch,\tf-by, match, other ),\t\t\t\t */\n"
386 " 0,\t\t\t\t /* %5d %-17s */\n",
387 sst_highwater,
388 0, "");
389
390 for (i = 1; i < sst_highwater; i++) {
391
392 /* verify fields will fit */
393 if (sst[i].followedby & ~0x3) {
394 fprintf(stderr,
395 "keyword-gen internal error "
396 "sst[%d].followedby %d too big\n",
397 i, sst[i].followedby);
398 exit(7);
399 }
400
401 if (sst_highwater <= sst[i].match_next_s
402 || sst[i].match_next_s & ~0x7ff) {
403 fprintf(stderr,
404 "keyword-gen internal error "
405 "sst[%d].match_next_s %d too big\n",
406 i, sst[i].match_next_s);
407 exit(8);
408 }
409
410 if (sst_highwater <= sst[i].other_next_s
411 || sst[i].other_next_s & ~0x7ff) {
412 fprintf(stderr,
413 "keyword-gen internal error "
414 "sst[%d].other_next_s %d too big\n",
415 i, sst[i].other_next_s);
416 exit(9);
417 }
418
419 if (sst[i].finishes_token) {
420 snprintf(token_id_comment,
421 sizeof(token_id_comment), "%5d %-17s",
422 i, symbname(sst[i].finishes_token));
423 if (i != sst[i].finishes_token) {
424 fprintf(stderr,
425 "keyword-gen internal error "
426 "entry %d finishes token %d\n",
427 i, sst[i].finishes_token);
428 exit(5);
429 }
430 } else {
431 /*
432 * Determine the keyword prefix that leads to this
433 * state. This is expensive but keyword-gen is run
434 * only when it changes. Distributing keyword-gen-utd
435 * achieves that, which is why it must be committed
436 * at the same time as keyword-gen.c and ntp_keyword.h.
437 *
438 * Scan the state array iteratively looking for a state
439 * which leads to the current one, collecting matching
440 * characters along the way. There is only one such
441 * path back to the starting state given the way our
442 * scanner state machine is built and the practice of
443 * using the spelling of the keyword as its T_* token
444 * identifier, which results in never having two
445 * spellings result in the same T_* value.
446 */
447 prefix_len = 0;
448 this_state = i;
449 do {
450 for (state = 1; state < sst_highwater; state++)
451 if (sst[state].other_next_s == this_state) {
452 this_state = state;
453 break;
454 } else if (sst[state].match_next_s == this_state) {
455 this_state = state;
456 rprefix[prefix_len] = sst[state].ch;
457 prefix_len++;
458 break;
459 }
460 } while (this_state != initial_state);
461
462 if (prefix_len) {
463 /* reverse rprefix into prefix */
464 p = prefix + prefix_len;
465 r = rprefix;
466 while (r < rprefix + prefix_len)
467 *--p = *r++;
468 }
469 prefix[prefix_len] = '\0';
470
471 snprintf(token_id_comment,
472 sizeof(token_id_comment), "%5d %-17s",
473 i, (initial_state == i)
474 ? "[initial state]"
475 : prefix);
476 }
477
478 printf(" S_ST( '%c',\t%d, %5u, %5u )%s /* %s */\n",
479 sst[i].ch,
480 sst[i].followedby,
481 sst[i].match_next_s,
482 sst[i].other_next_s,
483 (i + 1 < sst_highwater)
484 ? ","
485 : " ",
486 token_id_comment);
487 }
488
489 printf("};\n\n");
490 }
491
492
493 /* Define a function to create the states of the scanner. This function
494 * is used by the create_keyword_scanner function below.
495 *
496 * This function takes a suffix of a keyword, the token to be returned on
497 * recognizing the complete keyword, and any pre-existing state that exists
498 * for some other keyword that has the same prefix as the current one.
499 */
500 static u_short
501 create_scan_states(
502 char * text,
503 u_short token,
504 follby followedby,
505 u_short prev_state
506 )
507 {
508 u_short my_state;
509 u_short return_state;
510 u_short prev_char_s;
511 u_short curr_char_s;
512
513 return_state = prev_state;
514 curr_char_s = prev_state;
515 prev_char_s = 0;
516
517 /* Find the correct position to insert the state.
518 * All states should be in alphabetical order
519 */
520 while (curr_char_s && (text[0] < sst[curr_char_s].ch)) {
521 prev_char_s = curr_char_s;
522 curr_char_s = sst[curr_char_s].other_next_s;
523 }
524
525 /*
526 * Check if a previously seen keyword has the same prefix as
527 * the current keyword. If so, simply use the state for that
528 * keyword as my_state, otherwise, allocate a new state.
529 */
530 if (curr_char_s && (text[0] == sst[curr_char_s].ch)) {
531 my_state = curr_char_s;
532 if ('\0' == text[1]) {
533 fprintf(stderr,
534 "Duplicate entries for keyword '%s' in"
535 " keyword_gen.c ntp_keywords[].\n",
536 current_keyword);
537 exit(2);
538 }
539 } else {
540 do
541 my_state = sst_highwater++;
542 while (my_state < COUNTOF(sst)
543 && sst[my_state].finishes_token);
544 if (my_state >= COUNTOF(sst)) {
545 fprintf(stderr,
546 "fatal, keyword scanner state array "
547 "sst[%d] is too small, modify\n"
548 "keyword-gen.c to increase.\n",
549 (int)COUNTOF(sst));
550 exit(3);
551 }
552 /* Store the next character of the keyword */
553 sst[my_state].ch = text[0];
554 sst[my_state].other_next_s = curr_char_s;
555 sst[my_state].followedby = FOLLBY_NON_ACCEPTING;
556
557 if (prev_char_s)
558 sst[prev_char_s].other_next_s = my_state;
559 else
560 return_state = my_state;
561 }
562
563 /* Check if the next character is '\0'.
564 * If yes, we are done with the recognition and this is an accepting
565 * state.
566 * If not, we need to continue scanning
567 */
568 if ('\0' == text[1]) {
569 sst[my_state].finishes_token = (u_short)token;
570 sst[my_state].followedby = (char)followedby;
571
572 if (sst[token].finishes_token != (u_short)token) {
573 fprintf(stderr,
574 "fatal, sst[%d] not reserved for %s.\n",
575 token, symbname(token));
576 exit(6);
577 }
578 /* relocate so token id is sst[] index */
579 if (my_state != token) {
580 sst[token] = sst[my_state];
581 ZERO(sst[my_state]);
582 do
583 sst_highwater--;
584 while (sst[sst_highwater].finishes_token);
585 my_state = token;
586 if (prev_char_s)
587 sst[prev_char_s].other_next_s = my_state;
588 else
589 return_state = my_state;
590 }
591 } else
592 sst[my_state].match_next_s =
593 create_scan_states(
594 &text[1],
595 token,
596 followedby,
597 sst[my_state].match_next_s);
598
599 return return_state;
600 }
601
602
603 /* Define a function that takes a list of (keyword, token) values and
604 * creates a keywords scanner out of it.
605 */
606
607 static u_short
608 create_keyword_scanner(void)
609 {
610 u_short scanner;
611 u_short i;
612
613 sst_highwater = 1; /* index 0 invalid, unused */
614 scanner = 0;
615
616 for (i = 0; i < COUNTOF(ntp_keywords); i++) {
617 current_keyword = ntp_keywords[i].key;
618 scanner =
619 create_scan_states(
620 ntp_keywords[i].key,
621 ntp_keywords[i].token,
622 ntp_keywords[i].followedby,
623 scanner);
624 }
625
626 return scanner;
627 }
628
629
630 static void
631 generate_token_text(void)
632 {
633 u_short lowest_id;
634 u_short highest_id;
635 u_short id_count;
636 u_short id;
637 u_short i;
638
639 /* sort ntp_keywords in token ID order */
640 qsort(ntp_keywords, COUNTOF(ntp_keywords),
641 sizeof(ntp_keywords[0]), compare_key_tok_id);
642
643 lowest_id = ntp_keywords[0].token;
644 highest_id = ntp_keywords[COUNTOF(ntp_keywords) - 1].token;
645 id_count = highest_id - lowest_id + 1;
646
647 printf("#define LOWEST_KEYWORD_ID %d\n\n", lowest_id);
648
649 printf("const char * const keyword_text[%d] = {", id_count);
650
651 id = lowest_id;
652 i = 0;
653 while (i < COUNTOF(ntp_keywords)) {
654 while (id < ntp_keywords[i].token) {
655 printf(",\n\t/* %-5d %5d %20s */\tNULL",
656 id - lowest_id, id, symbname(id));
657 id++;
658 }
659 if (i > 0)
660 printf(",");
661 printf("\n\t/* %-5d %5d %20s */\t\"%s\"",
662 id - lowest_id, id, symbname(id),
663 ntp_keywords[i].key);
664 i++;
665 id++;
666 }
667
668 printf("\n};\n\n");
669 }
670
671
672 int
673 compare_key_tok_id(
674 const void *a1,
675 const void *a2
676 )
677 {
678 const struct key_tok *p1 = a1;
679 const struct key_tok *p2 = a2;
680
681 if (p1->token == p2->token)
682 return 0;
683
684 if (p1->token < p2->token)
685 return -1;
686 else
687 return 1;
688 }
689
690
691 int
692 compare_key_tok_text(
693 const void *a1,
694 const void *a2
695 )
696 {
697 const struct key_tok *p1 = a1;
698 const struct key_tok *p2 = a2;
699
700 return strcmp(p1->key, p2->key);
701 }
702
703
704 /*
705 * populate_symb() - populate symb[] lookup array with symbolic token
706 * names such that symb[T_Age] == "T_Age", etc.
707 */
708 void
709 populate_symb(
710 char *header_file
711 )
712 {
713 FILE * yh;
714 char line[2 * MAX_TOK_LEN];
715 char name[2 * MAX_TOK_LEN];
716 int token;
717
718 yh = fopen(header_file, "r");
719 if (NULL == yh) {
720 perror("unable to open yacc/bison header file");
721 exit(4);
722 }
723
724 while (NULL != fgets(line, sizeof(line), yh))
725 if (2 == sscanf(line, "#define %s %d", name, &token)
726 && 'T' == name[0] && '_' == name[1] && token >= 0
727 && token < COUNTOF(symb)) {
728
729 symb[token] = estrdup(name);
730 if (strlen(name) > MAX_TOK_LEN) {
731 fprintf(stderr,
732 "MAX_TOK_LEN %d too small for '%s'\n"
733 "Edit keyword-gen.c to raise.\n",
734 MAX_TOK_LEN, name);
735 exit(10);
736 }
737 }
738 fclose(yh);
739 }
740
741
742 const char *
743 symbname(
744 u_short token
745 )
746 {
747 char *name;
748
749 if (token < COUNTOF(symb) && symb[token] != NULL) {
750 name = symb[token];
751 } else {
752 LIB_GETBUF(name);
753 snprintf(name, LIB_BUFLENGTH, "%d", token);
754 }
755
756 return name;
757 }
758