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