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