1 /* misc - miscellaneous flex routines */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
37 #define CMD_IF_TABLES_SER "%if-tables-serialization"
38 #define CMD_TABLES_YYDMAP "%tables-yydmap"
39 #define CMD_DEFINE_YYTABLES "%define-yytables"
40 #define CMD_IF_CPP_ONLY "%if-c++-only"
41 #define CMD_IF_C_ONLY "%if-c-only"
42 #define CMD_IF_C_OR_CPP "%if-c-or-c++"
43 #define CMD_NOT_FOR_HEADER "%not-for-header"
44 #define CMD_OK_FOR_HEADER "%ok-for-header"
45 #define CMD_PUSH "%push"
46 #define CMD_POP "%pop"
47 #define CMD_IF_REENTRANT "%if-reentrant"
48 #define CMD_IF_NOT_REENTRANT "%if-not-reentrant"
49 #define CMD_IF_BISON_BRIDGE "%if-bison-bridge"
50 #define CMD_IF_NOT_BISON_BRIDGE "%if-not-bison-bridge"
51 #define CMD_ENDIF "%endif"
53 /* we allow the skeleton to push and pop. */
55 bool dc; /**< do_copy */
57 static struct sko_state *sko_stack=0;
58 static int sko_len=0,sko_sz=0;
59 static void sko_push(bool dc)
63 sko_stack = (struct sko_state*)flex_alloc(sizeof(struct sko_state)*sko_sz);
66 if(sko_len >= sko_sz){
68 sko_stack = (struct sko_state*)flex_realloc(sko_stack,sizeof(struct sko_state)*sko_sz);
71 /* initialize to zero and push */
72 sko_stack[sko_len].dc = dc;
75 static void sko_peek(bool *dc)
78 flex_die("peek attempt when sko stack is empty");
80 *dc = sko_stack[sko_len-1].dc;
82 static void sko_pop(bool* dc)
87 flex_die("popped too many times in skeleton.");
90 /* Append "#define defname value\n" to the running buffer. */
91 void action_define (defname, value)
98 if ((int) strlen (defname) > MAXLINE / 2) {
99 format_pinpoint_message (_
100 ("name \"%s\" ridiculously long"),
105 snprintf (buf, sizeof(buf), "#define %s %d\n", defname, value);
108 /* track #defines so we can undef them when we're done. */
109 cpy = copy_string (defname);
110 buf_append (&defs_buf, &cpy, 1);
114 /** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer.
115 * @param defname The macro name.
116 * @param value The macro value, can be NULL, which is the same as the empty string.
118 void action_m4_define (const char *defname, const char * value)
122 flexfatal ("DO NOT USE THIS FUNCTION!");
124 if ((int) strlen (defname) > MAXLINE / 2) {
125 format_pinpoint_message (_
126 ("name \"%s\" ridiculously long"),
131 snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:"");
135 /* Append "new_text" to the running buffer. */
136 void add_action (new_text)
137 const char *new_text;
139 int len = strlen (new_text);
141 while (len + action_index >= action_size - 10 /* slop */ ) {
142 int new_size = action_size * 2;
145 /* Increase just a little, to try to avoid overflow
146 * on 16-bit machines.
148 action_size += action_size / 8;
150 action_size = new_size;
153 reallocate_character_array (action_array,
157 strcpy (&action_array[action_index], new_text);
163 /* allocate_array - allocate memory for an integer array of the given size */
165 void *allocate_array (size, element_size)
170 size_t num_bytes = element_size * size;
172 mem = flex_alloc (num_bytes);
175 ("memory allocation failed in allocate_array()"));
181 /* all_lower - true if a string is all lower-case */
187 if (!isascii ((Char) * str) || !islower (*str))
196 /* all_upper - true if a string is all upper-case */
202 if (!isascii ((Char) * str) || !isupper (*str))
211 /* bubble - bubble sort an integer array in increasing order
215 * void bubble( v, n );
218 * sorts the first n elements of array v and replaces them in
222 * v - the array to be sorted
223 * n - the number of elements of 'v' to be sorted
229 register int i, j, k;
231 for (i = n; i > 1; --i)
232 for (j = 1; j < i; ++j)
233 if (v[j] > v[j + 1]) { /* compare */
234 k = v[j]; /* exchange */
241 /* check_char - checks a character to make sure it's within the range
242 * we're expecting. If not, generates fatal error message
250 lerrsf (_("bad character '%s' detected in check_char()"),
255 ("scanner requires -8 flag to use the character %s"),
261 /* clower - replace upper-case letter to lower-case */
266 return (Char) ((isascii (c) && isupper (c)) ? tolower (c) : c);
270 /* copy_string - returns a dynamically allocated copy of a string */
272 char *copy_string (str)
273 register const char *str;
275 register const char *c1;
281 for (c1 = str; *c1; ++c1) ;
283 size = (c1 - str + 1) * sizeof (char);
285 copy = (char *) flex_alloc (size);
288 flexfatal (_("dynamic memory failure in copy_string()"));
290 for (c2 = copy; (*c2++ = *str++) != 0;) ;
296 /* copy_unsigned_string -
297 * returns a dynamically allocated copy of a (potentially) unsigned string
300 Char *copy_unsigned_string (str)
307 for (c = str; *c; ++c) ;
309 copy = allocate_Character_array (c - str + 1);
311 for (c = copy; (*c++ = *str++) != 0;) ;
317 /* cshell - shell sort a character array in increasing order
322 * int n, special_case_0;
323 * cshell( v, n, special_case_0 );
326 * Does a shell sort of the first n elements of array v.
327 * If special_case_0 is true, then any element equal to 0
328 * is instead assumed to have infinite weight.
331 * v - array to be sorted
332 * n - number of elements of v to be sorted
335 void cshell (v, n, special_case_0)
337 int n, special_case_0;
342 for (gap = n / 2; gap > 0; gap = gap / 2)
343 for (i = gap; i < n; ++i)
344 for (j = i - gap; j >= 0; j = j - gap) {
347 if (special_case_0) {
356 else if (v[j] <= v[jg])
366 /* dataend - finish up a block of data declarations */
370 /* short circuit any output */
376 /* add terminator for initialization; { for vi */
384 /* dataflush - flush generated data statements */
388 /* short circuit any output */
394 if (++dataline >= NUMDATALINES) {
395 /* Put out a blank line so that the table is grouped into
396 * large blocks that enable the user to find elements easily.
402 /* Reset the number of characters written on the current line. */
407 /* flexerror - report an error message and terminate */
412 fprintf (stderr, "%s: %s\n", program_name, msg);
417 /* flexfatal - report a fatal error message and terminate */
422 fprintf (stderr, _("%s: fatal internal error, %s\n"),
428 /* htoi - convert a hexadecimal digit string to an integer value */
435 (void) sscanf ((char *) str, "%x", &result);
441 /* lerrif - report an error message formatted with one integer argument */
443 void lerrif (msg, arg)
447 char errmsg[MAXLINE];
449 snprintf (errmsg, sizeof(errmsg), msg, arg);
454 /* lerrsf - report an error message formatted with one string argument */
456 void lerrsf (msg, arg)
457 const char *msg, arg[];
459 char errmsg[MAXLINE];
461 snprintf (errmsg, sizeof(errmsg), msg, arg);
466 /* line_directive_out - spit out a "#line" statement */
468 void line_directive_out (output_file, do_infile)
472 char directive[MAXLINE], filename[MAXLINE];
474 static const char *line_fmt = "#line %d \"%s\"\n";
479 s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME";
481 if (do_infile && !s1)
485 s3 = &filename[sizeof (filename) - 2];
487 while (s2 < s3 && *s1) {
498 snprintf (directive, sizeof(directive), line_fmt, linenum, filename);
500 if (output_file == stdout)
501 /* Account for the line directive itself. */
504 snprintf (directive, sizeof(directive), line_fmt, out_linenum, filename);
507 /* If output_file is nil then we should put the directive in
508 * the accumulated actions.
511 fputs (directive, output_file);
514 add_action (directive);
518 /* mark_defs1 - mark the current position in the action array as
519 * representing where the user's section 1 definitions end
520 * and the prolog begins
525 action_array[action_index++] = '\0';
526 action_offset = prolog_offset = action_index;
527 action_array[action_index] = '\0';
531 /* mark_prolog - mark the current position in the action array as
532 * representing the end of the action prolog
536 action_array[action_index++] = '\0';
537 action_offset = action_index;
538 action_array[action_index] = '\0';
542 /* mk2data - generate a data statement for a two-dimensional array
544 * Generates a data statement initializing the current 2-D array to "value".
549 /* short circuit any output */
553 if (datapos >= NUMDATAITEMS) {
567 out_dec ("%5d", value);
571 /* mkdata - generate a data statement
573 * Generates a data statement initializing the current array element to
579 /* short circuit any output */
583 if (datapos >= NUMDATAITEMS) {
596 out_dec ("%5d", value);
600 /* myctoi - return the integer represented by a string of digits */
607 (void) sscanf (array, "%d", &val);
613 /* myesc - return character corresponding to escape sequence */
632 #if defined (__STDC__)
655 while (isascii (array[sptr]) &&
656 isdigit (array[sptr]))
657 /* Don't increment inside loop control
658 * because if isdigit() is a macro it might
659 * expand into multiple increments ...
666 esc_char = otoi (array + 1);
677 while (isascii (array[sptr]) &&
678 isxdigit ((char) array[sptr]))
679 /* Don't increment inside loop control
680 * because if isdigit() is a macro it might
681 * expand into multiple increments ...
688 esc_char = htoi (array + 2);
701 /* otoi - convert an octal digit string to an integer value */
708 (void) sscanf ((char *) str, "%o", &result);
713 /* out - various flavors of outputing a (possibly formatted) string for the
714 * generated scanner, keeping track of the line count.
721 out_line_count (str);
724 void out_dec (fmt, n)
728 fprintf (stdout, fmt, n);
729 out_line_count (fmt);
732 void out_dec2 (fmt, n1, n2)
736 fprintf (stdout, fmt, n1, n2);
737 out_line_count (fmt);
740 void out_hex (fmt, x)
744 fprintf (stdout, fmt, x);
745 out_line_count (fmt);
748 void out_line_count (str)
753 for (i = 0; str[i]; ++i)
758 void out_str (fmt, str)
759 const char *fmt, str[];
761 fprintf (stdout,fmt, str);
762 out_line_count (fmt);
763 out_line_count (str);
766 void out_str3 (fmt, s1, s2, s3)
767 const char *fmt, s1[], s2[], s3[];
769 fprintf (stdout,fmt, s1, s2, s3);
770 out_line_count (fmt);
776 void out_str_dec (fmt, str, n)
777 const char *fmt, str[];
780 fprintf (stdout,fmt, str, n);
781 out_line_count (fmt);
782 out_line_count (str);
799 out_line_count (str);
803 /** Print "m4_define( [[def]], [[val]])m4_dnl\n".
804 * @param def The m4 symbol to define.
805 * @param val The definition; may be NULL.
808 void out_m4_define (const char* def, const char* val)
810 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
811 fprintf(stdout, fmt, def, val?val:"");
815 /* readable_form - return the the human-readable form of a character
817 * The returned string is in static storage.
820 char *readable_form (c)
823 static char rform[10];
825 if ((c >= 0 && c < 32) || c >= 127) {
838 #if defined (__STDC__)
846 snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c);
863 /* reallocate_array - increase the size of a dynamic array */
865 void *reallocate_array (array, size, element_size)
870 register void *new_array;
871 size_t num_bytes = element_size * size;
873 new_array = flex_realloc (array, num_bytes);
875 flexfatal (_("attempt to increase array size failed"));
881 /* skelout - write out one section of the skeleton file
884 * Copies skelfile or skel array to stdout until a line beginning with
885 * "%%" or EOF is found.
889 char buf_storage[MAXLINE];
890 char *buf = buf_storage;
893 /* "reset" the state by clearing the buffer and pushing a '1' */
897 sko_push(do_copy=true);
900 /* Loop pulling lines either from the skelfile, if we're using
901 * one, or from the skel[] array.
904 (fgets (buf, MAXLINE, skelfile) != NULL) :
905 ((buf = (char *) skel[skel_ind++]) != 0)) {
910 /* copy from skel array */
911 if (buf[0] == '%') { /* control line */
912 /* print the control line as a comment. */
913 if (ddebug && buf[1] != '#') {
914 if (buf[strlen (buf) - 1] == '\\')
915 out_str ("/* %s */\\\n", buf);
917 out_str ("/* %s */\n", buf);
920 /* We've been accused of using cryptic markers in the skel.
921 * So we'll use emacs-style-hyphenated-commands.
922 * We might consider a hash if this if-else-if-else
923 * chain gets too large.
925 #define cmd_match(s) (strncmp(buf,(s),strlen(s))==0)
928 /* %% is a break point for skelout() */
931 else if (cmd_match (CMD_PUSH)){
934 out_str("/*(state = (%s) */",do_copy?"true":"false");
936 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
938 else if (cmd_match (CMD_POP)){
941 out_str("/*(state = (%s) */",do_copy?"true":"false");
943 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
945 else if (cmd_match (CMD_IF_REENTRANT)){
947 do_copy = reentrant && do_copy;
949 else if (cmd_match (CMD_IF_NOT_REENTRANT)){
951 do_copy = !reentrant && do_copy;
953 else if (cmd_match(CMD_IF_BISON_BRIDGE)){
955 do_copy = bison_bridge_lval && do_copy;
957 else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){
959 do_copy = !bison_bridge_lval && do_copy;
961 else if (cmd_match (CMD_ENDIF)){
964 else if (cmd_match (CMD_IF_TABLES_SER)) {
965 do_copy = do_copy && tablesext;
967 else if (cmd_match (CMD_TABLES_YYDMAP)) {
968 if (tablesext && yydmap_buf.elts)
969 outn ((char *) (yydmap_buf.elts));
971 else if (cmd_match (CMD_DEFINE_YYTABLES)) {
972 out_str("#define YYTABLES_NAME \"%s\"\n",
973 tablesname?tablesname:"yytables");
975 else if (cmd_match (CMD_IF_CPP_ONLY)) {
978 do_copy = C_plus_plus;
980 else if (cmd_match (CMD_IF_C_ONLY)) {
983 do_copy = !C_plus_plus;
985 else if (cmd_match (CMD_IF_C_OR_CPP)) {
986 /* %* for C and C++ */
990 else if (cmd_match (CMD_NOT_FOR_HEADER)) {
991 /* %c begin linkage-only (non-header) code. */
994 else if (cmd_match (CMD_OK_FOR_HEADER)) {
995 /* %e end linkage-only code. */
998 else if (buf[1] == '#') {
999 /* %# a comment in the skel. ignore. */
1002 flexfatal (_("bad line in skeleton file"));
1012 /* transition_struct_out - output a yy_trans_info structure
1014 * outputs the yy_trans_info structure with the two elements, element_v and
1015 * element_n. Formats the output with spaces and carriage returns.
1018 void transition_struct_out (element_v, element_n)
1019 int element_v, element_n;
1022 /* short circuit any output */
1026 out_dec2 (" {%4d,%4d },", element_v, element_n);
1028 datapos += TRANS_STRUCT_PRINT_LENGTH;
1030 if (datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH) {
1033 if (++dataline % 10 == 0)
1041 /* The following is only needed when building flex's parser using certain
1042 * broken versions of bison.
1044 void *yy_flex_xmalloc (size)
1047 void *result = flex_alloc ((size_t) size);
1051 ("memory allocation failed in yy_flex_xmalloc()"));
1057 /* zero_out - set a region of memory to 0
1059 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
1062 void zero_out (region_ptr, size_in_bytes)
1064 size_t size_in_bytes;
1066 register char *rp, *rp_end;
1069 rp_end = region_ptr + size_in_bytes;
1075 /* Remove all '\n' and '\r' characters, if any, from the end of str.
1076 * str can be any null-terminated string, or NULL.
1083 if (!str || !*str) /* s is null or empty string */
1086 /* find end of string minus one */
1092 while (p >= str && (*p == '\r' || *p == '\n'))