1 /* $NetBSD: misc.c,v 1.2 2009/10/26 04:27:15 christos Exp $ */ 2 3 /* misc - miscellaneous flex routines */ 4 5 /* Copyright (c) 1990 The Regents of the University of California. */ 6 /* All rights reserved. */ 7 8 /* This code is derived from software contributed to Berkeley by */ 9 /* Vern Paxson. */ 10 11 /* The United States Government has rights in this work pursuant */ 12 /* to contract no. DE-AC03-76SF00098 between the United States */ 13 /* Department of Energy and the University of California. */ 14 15 /* This file is part of flex. */ 16 17 /* Redistribution and use in source and binary forms, with or without */ 18 /* modification, are permitted provided that the following conditions */ 19 /* are met: */ 20 21 /* 1. Redistributions of source code must retain the above copyright */ 22 /* notice, this list of conditions and the following disclaimer. */ 23 /* 2. Redistributions in binary form must reproduce the above copyright */ 24 /* notice, this list of conditions and the following disclaimer in the */ 25 /* documentation and/or other materials provided with the distribution. */ 26 27 /* Neither the name of the University nor the names of its contributors */ 28 /* may be used to endorse or promote products derived from this software */ 29 /* without specific prior written permission. */ 30 31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ 32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ 34 /* PURPOSE. */ 35 36 #include "flexdef.h" 37 #include "tables.h" 38 39 #define CMD_IF_TABLES_SER "%if-tables-serialization" 40 #define CMD_TABLES_YYDMAP "%tables-yydmap" 41 #define CMD_DEFINE_YYTABLES "%define-yytables" 42 #define CMD_IF_CPP_ONLY "%if-c++-only" 43 #define CMD_IF_C_ONLY "%if-c-only" 44 #define CMD_IF_C_OR_CPP "%if-c-or-c++" 45 #define CMD_NOT_FOR_HEADER "%not-for-header" 46 #define CMD_OK_FOR_HEADER "%ok-for-header" 47 #define CMD_PUSH "%push" 48 #define CMD_POP "%pop" 49 #define CMD_IF_REENTRANT "%if-reentrant" 50 #define CMD_IF_NOT_REENTRANT "%if-not-reentrant" 51 #define CMD_IF_BISON_BRIDGE "%if-bison-bridge" 52 #define CMD_IF_NOT_BISON_BRIDGE "%if-not-bison-bridge" 53 #define CMD_ENDIF "%endif" 54 55 /* we allow the skeleton to push and pop. */ 56 struct sko_state { 57 bool dc; /**< do_copy */ 58 }; 59 static struct sko_state *sko_stack=0; 60 static int sko_len=0,sko_sz=0; 61 static void sko_push(bool dc) 62 { 63 if(!sko_stack){ 64 sko_sz = 1; 65 sko_stack = (struct sko_state*)flex_alloc(sizeof(struct sko_state)*sko_sz); 66 sko_len = 0; 67 } 68 if(sko_len >= sko_sz){ 69 sko_sz *= 2; 70 sko_stack = (struct sko_state*)flex_realloc(sko_stack,sizeof(struct sko_state)*sko_sz); 71 } 72 73 /* initialize to zero and push */ 74 sko_stack[sko_len].dc = dc; 75 sko_len++; 76 } 77 static void sko_peek(bool *dc) 78 { 79 if(sko_len <= 0) 80 flex_die("peek attempt when sko stack is empty"); 81 if(dc) 82 *dc = sko_stack[sko_len-1].dc; 83 } 84 static void sko_pop(bool* dc) 85 { 86 sko_peek(dc); 87 sko_len--; 88 if(sko_len < 0) 89 flex_die("popped too many times in skeleton."); 90 } 91 92 /* Append "#define defname value\n" to the running buffer. */ 93 void action_define (defname, value) 94 const char *defname; 95 int value; 96 { 97 char buf[MAXLINE]; 98 char *cpy; 99 100 if ((int) strlen (defname) > MAXLINE / 2) { 101 format_pinpoint_message (_ 102 ("name \"%s\" ridiculously long"), 103 defname); 104 return; 105 } 106 107 snprintf (buf, sizeof(buf), "#define %s %d\n", defname, value); 108 add_action (buf); 109 110 /* track #defines so we can undef them when we're done. */ 111 cpy = copy_string (defname); 112 buf_append (&defs_buf, &cpy, 1); 113 } 114 115 116 #ifdef notdef 117 /** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer. 118 * @param defname The macro name. 119 * @param value The macro value, can be NULL, which is the same as the empty string. 120 */ 121 static void action_m4_define (const char *defname, const char * value) 122 { 123 char buf[MAXLINE]; 124 125 flexfatal ("DO NOT USE THIS FUNCTION!"); 126 127 if ((int) strlen (defname) > MAXLINE / 2) { 128 format_pinpoint_message (_ 129 ("name \"%s\" ridiculously long"), 130 defname); 131 return; 132 } 133 134 snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:""); 135 add_action (buf); 136 } 137 #endif 138 139 /* Append "new_text" to the running buffer. */ 140 void add_action (new_text) 141 const char *new_text; 142 { 143 int len = strlen (new_text); 144 145 while (len + action_index >= action_size - 10 /* slop */ ) { 146 int new_size = action_size * 2; 147 148 if (new_size <= 0) 149 /* Increase just a little, to try to avoid overflow 150 * on 16-bit machines. 151 */ 152 action_size += action_size / 8; 153 else 154 action_size = new_size; 155 156 action_array = 157 reallocate_character_array (action_array, 158 action_size); 159 } 160 161 strcpy (&action_array[action_index], new_text); 162 163 action_index += len; 164 } 165 166 167 /* allocate_array - allocate memory for an integer array of the given size */ 168 169 void *allocate_array (size, element_size) 170 int size; 171 size_t element_size; 172 { 173 register void *mem; 174 size_t num_bytes = element_size * size; 175 176 mem = flex_alloc (num_bytes); 177 if (!mem) 178 flexfatal (_ 179 ("memory allocation failed in allocate_array()")); 180 181 return mem; 182 } 183 184 185 /* all_lower - true if a string is all lower-case */ 186 187 int all_lower (str) 188 register char *str; 189 { 190 while (*str) { 191 if (!isascii ((Char)*str) || !islower ((Char)*str)) 192 return 0; 193 ++str; 194 } 195 196 return 1; 197 } 198 199 200 /* all_upper - true if a string is all upper-case */ 201 202 int all_upper (str) 203 register char *str; 204 { 205 while (*str) { 206 if (!isascii ((Char)*str) || !isupper ((Char)*str)) 207 return 0; 208 ++str; 209 } 210 211 return 1; 212 } 213 214 215 /* bubble - bubble sort an integer array in increasing order 216 * 217 * synopsis 218 * int v[n], n; 219 * void bubble( v, n ); 220 * 221 * description 222 * sorts the first n elements of array v and replaces them in 223 * increasing order. 224 * 225 * passed 226 * v - the array to be sorted 227 * n - the number of elements of 'v' to be sorted 228 */ 229 230 void bubble (v, n) 231 int v[], n; 232 { 233 register int i, j, k; 234 235 for (i = n; i > 1; --i) 236 for (j = 1; j < i; ++j) 237 if (v[j] > v[j + 1]) { /* compare */ 238 k = v[j]; /* exchange */ 239 v[j] = v[j + 1]; 240 v[j + 1] = k; 241 } 242 } 243 244 245 /* check_char - checks a character to make sure it's within the range 246 * we're expecting. If not, generates fatal error message 247 * and exits. 248 */ 249 250 void check_char (c) 251 int c; 252 { 253 if (c >= CSIZE) 254 lerrsf (_("bad character '%s' detected in check_char()"), 255 readable_form (c)); 256 257 if (c >= csize) 258 lerrsf (_ 259 ("scanner requires -8 flag to use the character %s"), 260 readable_form (c)); 261 } 262 263 264 265 /* clower - replace upper-case letter to lower-case */ 266 267 Char clower (c) 268 register int c; 269 { 270 return (Char) ((isascii (c) && isupper (c)) ? tolower (c) : c); 271 } 272 273 274 /* copy_string - returns a dynamically allocated copy of a string */ 275 276 char *copy_string (str) 277 register const char *str; 278 { 279 register const char *c1; 280 register char *c2; 281 char *copy; 282 unsigned int size; 283 284 /* find length */ 285 for (c1 = str; *c1; ++c1) ; 286 287 size = (c1 - str + 1) * sizeof (char); 288 289 copy = (char *) flex_alloc (size); 290 291 if (copy == NULL) 292 flexfatal (_("dynamic memory failure in copy_string()")); 293 294 for (c2 = copy; (*c2++ = *str++) != 0;) ; 295 296 return copy; 297 } 298 299 300 /* copy_unsigned_string - 301 * returns a dynamically allocated copy of a (potentially) unsigned string 302 */ 303 304 Char *copy_unsigned_string (str) 305 register Char *str; 306 { 307 register Char *c; 308 Char *copy; 309 310 /* find length */ 311 for (c = str; *c; ++c) ; 312 313 copy = allocate_Character_array (c - str + 1); 314 315 for (c = copy; (*c++ = *str++) != 0;) ; 316 317 return copy; 318 } 319 320 321 /* cshell - shell sort a character array in increasing order 322 * 323 * synopsis 324 * 325 * Char v[n]; 326 * int n, special_case_0; 327 * cshell( v, n, special_case_0 ); 328 * 329 * description 330 * Does a shell sort of the first n elements of array v. 331 * If special_case_0 is true, then any element equal to 0 332 * is instead assumed to have infinite weight. 333 * 334 * passed 335 * v - array to be sorted 336 * n - number of elements of v to be sorted 337 */ 338 339 void cshell (v, n, special_case_0) 340 Char v[]; 341 int n, special_case_0; 342 { 343 int gap, i, j, jg; 344 Char k; 345 346 for (gap = n / 2; gap > 0; gap = gap / 2) 347 for (i = gap; i < n; ++i) 348 for (j = i - gap; j >= 0; j = j - gap) { 349 jg = j + gap; 350 351 if (special_case_0) { 352 if (v[jg] == 0) 353 break; 354 355 else if (v[j] != 0 356 && v[j] <= v[jg]) 357 break; 358 } 359 360 else if (v[j] <= v[jg]) 361 break; 362 363 k = v[j]; 364 v[j] = v[jg]; 365 v[jg] = k; 366 } 367 } 368 369 370 /* dataend - finish up a block of data declarations */ 371 372 void dataend () 373 { 374 /* short circuit any output */ 375 if (gentables) { 376 377 if (datapos > 0) 378 dataflush (); 379 380 /* add terminator for initialization; { for vi */ 381 outn (" } ;\n"); 382 } 383 dataline = 0; 384 datapos = 0; 385 } 386 387 388 /* dataflush - flush generated data statements */ 389 390 void dataflush () 391 { 392 /* short circuit any output */ 393 if (!gentables) 394 return; 395 396 outc ('\n'); 397 398 if (++dataline >= NUMDATALINES) { 399 /* Put out a blank line so that the table is grouped into 400 * large blocks that enable the user to find elements easily. 401 */ 402 outc ('\n'); 403 dataline = 0; 404 } 405 406 /* Reset the number of characters written on the current line. */ 407 datapos = 0; 408 } 409 410 411 /* flexerror - report an error message and terminate */ 412 413 void flexerror (msg) 414 const char *msg; 415 { 416 fprintf (stderr, "%s: %s\n", program_name, msg); 417 flexend (1); 418 } 419 420 421 /* flexfatal - report a fatal error message and terminate */ 422 423 void flexfatal (msg) 424 const char *msg; 425 { 426 fprintf (stderr, _("%s: fatal internal error, %s\n"), 427 program_name, msg); 428 FLEX_EXIT (1); 429 } 430 431 432 /* htoi - convert a hexadecimal digit string to an integer value */ 433 434 int htoi (str) 435 Char str[]; 436 { 437 unsigned int result; 438 439 (void) sscanf ((char *) str, "%x", &result); 440 441 return result; 442 } 443 444 445 /* lerrif - report an error message formatted with one integer argument */ 446 447 void lerrif (msg, arg) 448 const char *msg; 449 int arg; 450 { 451 char errmsg[MAXLINE]; 452 453 snprintf (errmsg, sizeof(errmsg), msg, arg); 454 flexerror (errmsg); 455 } 456 457 458 /* lerrsf - report an error message formatted with one string argument */ 459 460 void lerrsf (msg, arg) 461 const char *msg, arg[]; 462 { 463 char errmsg[MAXLINE]; 464 465 snprintf (errmsg, sizeof(errmsg), msg, arg); 466 flexerror (errmsg); 467 } 468 469 470 /* line_directive_out - spit out a "#line" statement */ 471 472 void line_directive_out (output_file, do_infile) 473 FILE *output_file; 474 int do_infile; 475 { 476 char directive[MAXLINE], filename[MAXLINE]; 477 char *s1, *s2, *s3; 478 static const char line_fmt[] = "#line %d \"%s\"\n"; 479 480 if (!gen_line_dirs) 481 return; 482 483 s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME"; 484 485 if (do_infile && !s1) 486 s1 = "<stdin>"; 487 488 s2 = filename; 489 s3 = &filename[sizeof (filename) - 2]; 490 491 while (s2 < s3 && *s1) { 492 if (*s1 == '\\') 493 /* Escape the '\' */ 494 *s2++ = '\\'; 495 496 *s2++ = *s1++; 497 } 498 499 *s2 = '\0'; 500 501 if (do_infile) 502 snprintf (directive, sizeof(directive), line_fmt, linenum, filename); 503 else { 504 if (output_file == stdout) 505 /* Account for the line directive itself. */ 506 ++out_linenum; 507 508 snprintf (directive, sizeof(directive), line_fmt, out_linenum, filename); 509 } 510 511 /* If output_file is nil then we should put the directive in 512 * the accumulated actions. 513 */ 514 if (output_file) { 515 fputs (directive, output_file); 516 } 517 else 518 add_action (directive); 519 } 520 521 522 /* mark_defs1 - mark the current position in the action array as 523 * representing where the user's section 1 definitions end 524 * and the prolog begins 525 */ 526 void mark_defs1 () 527 { 528 defs1_offset = 0; 529 action_array[action_index++] = '\0'; 530 action_offset = prolog_offset = action_index; 531 action_array[action_index] = '\0'; 532 } 533 534 535 /* mark_prolog - mark the current position in the action array as 536 * representing the end of the action prolog 537 */ 538 void mark_prolog () 539 { 540 action_array[action_index++] = '\0'; 541 action_offset = action_index; 542 action_array[action_index] = '\0'; 543 } 544 545 546 /* mk2data - generate a data statement for a two-dimensional array 547 * 548 * Generates a data statement initializing the current 2-D array to "value". 549 */ 550 void mk2data (value) 551 int value; 552 { 553 /* short circuit any output */ 554 if (!gentables) 555 return; 556 557 if (datapos >= NUMDATAITEMS) { 558 outc (','); 559 dataflush (); 560 } 561 562 if (datapos == 0) 563 /* Indent. */ 564 out (" "); 565 566 else 567 outc (','); 568 569 ++datapos; 570 571 out_dec ("%5d", value); 572 } 573 574 575 /* mkdata - generate a data statement 576 * 577 * Generates a data statement initializing the current array element to 578 * "value". 579 */ 580 void mkdata (value) 581 int value; 582 { 583 /* short circuit any output */ 584 if (!gentables) 585 return; 586 587 if (datapos >= NUMDATAITEMS) { 588 outc (','); 589 dataflush (); 590 } 591 592 if (datapos == 0) 593 /* Indent. */ 594 out (" "); 595 else 596 outc (','); 597 598 ++datapos; 599 600 out_dec ("%5d", value); 601 } 602 603 604 /* myctoi - return the integer represented by a string of digits */ 605 606 int myctoi (array) 607 const char *array; 608 { 609 int val = 0; 610 611 (void) sscanf (array, "%d", &val); 612 613 return val; 614 } 615 616 617 /* myesc - return character corresponding to escape sequence */ 618 619 Char myesc (array) 620 Char array[]; 621 { 622 Char c, esc_char; 623 624 switch (array[1]) { 625 case 'b': 626 return '\b'; 627 case 'f': 628 return '\f'; 629 case 'n': 630 return '\n'; 631 case 'r': 632 return '\r'; 633 case 't': 634 return '\t'; 635 636 #if defined (__STDC__) 637 case 'a': 638 return '\a'; 639 case 'v': 640 return '\v'; 641 #else 642 case 'a': 643 return '\007'; 644 case 'v': 645 return '\013'; 646 #endif 647 648 case '0': 649 case '1': 650 case '2': 651 case '3': 652 case '4': 653 case '5': 654 case '6': 655 case '7': 656 { /* \<octal> */ 657 int sptr = 1; 658 659 while (isascii (array[sptr]) && 660 isdigit (array[sptr])) 661 /* Don't increment inside loop control 662 * because if isdigit() is a macro it might 663 * expand into multiple increments ... 664 */ 665 ++sptr; 666 667 c = array[sptr]; 668 array[sptr] = '\0'; 669 670 esc_char = otoi (array + 1); 671 672 array[sptr] = c; 673 674 return esc_char; 675 } 676 677 case 'x': 678 { /* \x<hex> */ 679 int sptr = 2; 680 681 while (isascii (array[sptr]) && 682 isxdigit ((Char)array[sptr])) 683 /* Don't increment inside loop control 684 * because if isdigit() is a macro it might 685 * expand into multiple increments ... 686 */ 687 ++sptr; 688 689 c = array[sptr]; 690 array[sptr] = '\0'; 691 692 esc_char = htoi (array + 2); 693 694 array[sptr] = c; 695 696 return esc_char; 697 } 698 699 default: 700 return array[1]; 701 } 702 } 703 704 705 /* otoi - convert an octal digit string to an integer value */ 706 707 int otoi (str) 708 Char str[]; 709 { 710 unsigned int result; 711 712 (void) sscanf ((char *) str, "%o", &result); 713 return result; 714 } 715 716 717 /* out - various flavors of outputing a (possibly formatted) string for the 718 * generated scanner, keeping track of the line count. 719 */ 720 721 void out (str) 722 const char *str; 723 { 724 fputs (str, stdout); 725 out_line_count (str); 726 } 727 728 void out_dec (fmt, n) 729 const char *fmt; 730 int n; 731 { 732 fprintf (stdout, fmt, n); 733 out_line_count (fmt); 734 } 735 736 void out_dec2 (fmt, n1, n2) 737 const char *fmt; 738 int n1, n2; 739 { 740 fprintf (stdout, fmt, n1, n2); 741 out_line_count (fmt); 742 } 743 744 void out_hex (fmt, x) 745 const char *fmt; 746 unsigned int x; 747 { 748 fprintf (stdout, fmt, x); 749 out_line_count (fmt); 750 } 751 752 void out_line_count (str) 753 const char *str; 754 { 755 register int i; 756 757 for (i = 0; str[i]; ++i) 758 if (str[i] == '\n') 759 ++out_linenum; 760 } 761 762 void out_str (fmt, str) 763 const char *fmt, str[]; 764 { 765 fprintf (stdout,fmt, str); 766 out_line_count (fmt); 767 out_line_count (str); 768 } 769 770 void out_str3 (fmt, s1, s2, s3) 771 const char *fmt, s1[], s2[], s3[]; 772 { 773 fprintf (stdout,fmt, s1, s2, s3); 774 out_line_count (fmt); 775 out_line_count (s1); 776 out_line_count (s2); 777 out_line_count (s3); 778 } 779 780 void out_str_dec (fmt, str, n) 781 const char *fmt, str[]; 782 int n; 783 { 784 fprintf (stdout,fmt, str, n); 785 out_line_count (fmt); 786 out_line_count (str); 787 } 788 789 void outc (c) 790 int c; 791 { 792 fputc (c, stdout); 793 794 if (c == '\n') 795 ++out_linenum; 796 } 797 798 void outn (str) 799 const char *str; 800 { 801 fputs (str,stdout); 802 fputc('\n',stdout); 803 out_line_count (str); 804 ++out_linenum; 805 } 806 807 /** Print "m4_define( [[def]], [[val]])m4_dnl\n". 808 * @param def The m4 symbol to define. 809 * @param val The definition; may be NULL. 810 * @return buf 811 */ 812 void out_m4_define (const char* def, const char* val) 813 { 814 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n"; 815 fprintf(stdout, fmt, def, val?val:""); 816 } 817 818 819 /* readable_form - return the the human-readable form of a character 820 * 821 * The returned string is in static storage. 822 */ 823 824 char *readable_form (c) 825 register int c; 826 { 827 static char rform[10]; 828 829 if ((c >= 0 && c < 32) || c >= 127) { 830 switch (c) { 831 case '\b': 832 return "\\b"; 833 case '\f': 834 return "\\f"; 835 case '\n': 836 return "\\n"; 837 case '\r': 838 return "\\r"; 839 case '\t': 840 return "\\t"; 841 842 #if defined (__STDC__) 843 case '\a': 844 return "\\a"; 845 case '\v': 846 return "\\v"; 847 #endif 848 849 default: 850 snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c); 851 return rform; 852 } 853 } 854 855 else if (c == ' ') 856 return "' '"; 857 858 else { 859 rform[0] = c; 860 rform[1] = '\0'; 861 862 return rform; 863 } 864 } 865 866 867 /* reallocate_array - increase the size of a dynamic array */ 868 869 void *reallocate_array (array, size, element_size) 870 void *array; 871 int size; 872 size_t element_size; 873 { 874 register void *new_array; 875 size_t num_bytes = element_size * size; 876 877 new_array = flex_realloc (array, num_bytes); 878 if (!new_array) 879 flexfatal (_("attempt to increase array size failed")); 880 881 return new_array; 882 } 883 884 885 /* skelout - write out one section of the skeleton file 886 * 887 * Description 888 * Copies skelfile or skel array to stdout until a line beginning with 889 * "%%" or EOF is found. 890 */ 891 void skelout () 892 { 893 char buf_storage[MAXLINE]; 894 char *buf = buf_storage; 895 bool do_copy = true; 896 897 /* "reset" the state by clearing the buffer and pushing a '1' */ 898 if(sko_len > 0) 899 sko_peek(&do_copy); 900 sko_len = 0; 901 sko_push(do_copy=true); 902 903 904 /* Loop pulling lines either from the skelfile, if we're using 905 * one, or from the skel[] array. 906 */ 907 while (skelfile ? 908 (fgets (buf, MAXLINE, skelfile) != NULL) : 909 ((buf = (char *) skel[skel_ind++]) != 0)) { 910 911 if (skelfile) 912 chomp (buf); 913 914 /* copy from skel array */ 915 if (buf[0] == '%') { /* control line */ 916 /* print the control line as a comment. */ 917 if (ddebug && buf[1] != '#') { 918 if (buf[strlen (buf) - 1] == '\\') 919 out_str ("/* %s */\\\n", buf); 920 else 921 out_str ("/* %s */\n", buf); 922 } 923 924 /* We've been accused of using cryptic markers in the skel. 925 * So we'll use emacs-style-hyphenated-commands. 926 * We might consider a hash if this if-else-if-else 927 * chain gets too large. 928 */ 929 #define cmd_match(s) (strncmp(buf,(s),strlen(s))==0) 930 931 if (buf[1] == '%') { 932 /* %% is a break point for skelout() */ 933 return; 934 } 935 else if (cmd_match (CMD_PUSH)){ 936 sko_push(do_copy); 937 if(ddebug){ 938 out_str("/*(state = (%s) */",do_copy?"true":"false"); 939 } 940 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : ""); 941 } 942 else if (cmd_match (CMD_POP)){ 943 sko_pop(&do_copy); 944 if(ddebug){ 945 out_str("/*(state = (%s) */",do_copy?"true":"false"); 946 } 947 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : ""); 948 } 949 else if (cmd_match (CMD_IF_REENTRANT)){ 950 sko_push(do_copy); 951 do_copy = reentrant && do_copy; 952 } 953 else if (cmd_match (CMD_IF_NOT_REENTRANT)){ 954 sko_push(do_copy); 955 do_copy = !reentrant && do_copy; 956 } 957 else if (cmd_match(CMD_IF_BISON_BRIDGE)){ 958 sko_push(do_copy); 959 do_copy = bison_bridge_lval && do_copy; 960 } 961 else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){ 962 sko_push(do_copy); 963 do_copy = !bison_bridge_lval && do_copy; 964 } 965 else if (cmd_match (CMD_ENDIF)){ 966 sko_pop(&do_copy); 967 } 968 else if (cmd_match (CMD_IF_TABLES_SER)) { 969 do_copy = do_copy && tablesext; 970 } 971 else if (cmd_match (CMD_TABLES_YYDMAP)) { 972 if (tablesext && yydmap_buf.elts) 973 outn ((char *) (yydmap_buf.elts)); 974 } 975 else if (cmd_match (CMD_DEFINE_YYTABLES)) { 976 out_str("#define YYTABLES_NAME \"%s\"\n", 977 tablesname?tablesname:"yytables"); 978 } 979 else if (cmd_match (CMD_IF_CPP_ONLY)) { 980 /* only for C++ */ 981 sko_push(do_copy); 982 do_copy = C_plus_plus; 983 } 984 else if (cmd_match (CMD_IF_C_ONLY)) { 985 /* %- only for C */ 986 sko_push(do_copy); 987 do_copy = !C_plus_plus; 988 } 989 else if (cmd_match (CMD_IF_C_OR_CPP)) { 990 /* %* for C and C++ */ 991 sko_push(do_copy); 992 do_copy = true; 993 } 994 else if (cmd_match (CMD_NOT_FOR_HEADER)) { 995 /* %c begin linkage-only (non-header) code. */ 996 OUT_BEGIN_CODE (); 997 } 998 else if (cmd_match (CMD_OK_FOR_HEADER)) { 999 /* %e end linkage-only code. */ 1000 OUT_END_CODE (); 1001 } 1002 else if (buf[1] == '#') { 1003 /* %# a comment in the skel. ignore. */ 1004 } 1005 else { 1006 flexfatal (_("bad line in skeleton file")); 1007 } 1008 } 1009 1010 else if (do_copy) 1011 outn (buf); 1012 } /* end while */ 1013 } 1014 1015 1016 /* transition_struct_out - output a yy_trans_info structure 1017 * 1018 * outputs the yy_trans_info structure with the two elements, element_v and 1019 * element_n. Formats the output with spaces and carriage returns. 1020 */ 1021 1022 void transition_struct_out (element_v, element_n) 1023 int element_v, element_n; 1024 { 1025 1026 /* short circuit any output */ 1027 if (!gentables) 1028 return; 1029 1030 out_dec2 (" {%4d,%4d },", element_v, element_n); 1031 1032 datapos += TRANS_STRUCT_PRINT_LENGTH; 1033 1034 if (datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH) { 1035 outc ('\n'); 1036 1037 if (++dataline % 10 == 0) 1038 outc ('\n'); 1039 1040 datapos = 0; 1041 } 1042 } 1043 1044 1045 /* The following is only needed when building flex's parser using certain 1046 * broken versions of bison. 1047 */ 1048 void *yy_flex_xmalloc (size) 1049 int size; 1050 { 1051 void *result = flex_alloc ((size_t) size); 1052 1053 if (!result) 1054 flexfatal (_ 1055 ("memory allocation failed in yy_flex_xmalloc()")); 1056 1057 return result; 1058 } 1059 1060 1061 /* zero_out - set a region of memory to 0 1062 * 1063 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero. 1064 */ 1065 1066 void zero_out (region_ptr, size_in_bytes) 1067 char *region_ptr; 1068 size_t size_in_bytes; 1069 { 1070 register char *rp, *rp_end; 1071 1072 rp = region_ptr; 1073 rp_end = region_ptr + size_in_bytes; 1074 1075 while (rp < rp_end) 1076 *rp++ = 0; 1077 } 1078 1079 /* Remove all '\n' and '\r' characters, if any, from the end of str. 1080 * str can be any null-terminated string, or NULL. 1081 * returns str. */ 1082 char *chomp (str) 1083 char *str; 1084 { 1085 char *p = str; 1086 1087 if (!str || !*str) /* s is null or empty string */ 1088 return str; 1089 1090 /* find end of string minus one */ 1091 while (*p) 1092 ++p; 1093 --p; 1094 1095 /* eat newlines */ 1096 while (p >= str && (*p == '\r' || *p == '\n')) 1097 *p-- = 0; 1098 return str; 1099 } 1100