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