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