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);
65 flexfatal(_("allocation of sko_stack failed"));
68 if(sko_len >= sko_sz){
70 sko_stack = (struct sko_state*)flex_realloc(sko_stack,sizeof(struct sko_state)*sko_sz);
73 /* initialize to zero and push */
74 sko_stack[sko_len].dc = dc;
77 static void sko_peek(bool *dc)
80 flex_die("peek attempt when sko stack is empty");
82 *dc = sko_stack[sko_len-1].dc;
84 static void sko_pop(bool* dc)
89 flex_die("popped too many times in skeleton.");
92 /* Append "#define defname value\n" to the running buffer. */
93 void action_define (defname, value)
100 if ((int) strlen (defname) > MAXLINE / 2) {
101 format_pinpoint_message (_
102 ("name \"%s\" ridiculously long"),
107 snprintf (buf, sizeof(buf), "#define %s %d\n", defname, value);
110 /* track #defines so we can undef them when we're done. */
111 cpy = copy_string (defname);
112 buf_append (&defs_buf, &cpy, 1);
116 /** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer.
117 * @param defname The macro name.
118 * @param value The macro value, can be NULL, which is the same as the empty string.
120 void action_m4_define (const char *defname, const char * value)
124 flexfatal ("DO NOT USE THIS FUNCTION!");
126 if ((int) strlen (defname) > MAXLINE / 2) {
127 format_pinpoint_message (_
128 ("name \"%s\" ridiculously long"),
133 snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:"");
137 /* Append "new_text" to the running buffer. */
138 void add_action (new_text)
139 const char *new_text;
141 int len = strlen (new_text);
143 while (len + action_index >= action_size - 10 /* slop */ ) {
144 int new_size = action_size * 2;
147 /* Increase just a little, to try to avoid overflow
148 * on 16-bit machines.
150 action_size += action_size / 8;
152 action_size = new_size;
155 reallocate_character_array (action_array,
159 strcpy (&action_array[action_index], new_text);
165 /* allocate_array - allocate memory for an integer array of the given size */
167 void *allocate_array (size, element_size)
172 size_t num_bytes = element_size * size;
174 mem = flex_alloc (num_bytes);
177 ("memory allocation failed in allocate_array()"));
183 /* all_lower - true if a string is all lower-case */
189 if (!isascii ((Char) * str) || !islower ((Char) * str))
198 /* all_upper - true if a string is all upper-case */
204 if (!isascii ((Char) * str) || !isupper ((Char) * str))
213 /* intcmp - compares two integers for use by qsort. */
215 int intcmp (const void *a, const void *b)
217 return *(const int *) a - *(const int *) b;
221 /* check_char - checks a character to make sure it's within the range
222 * we're expecting. If not, generates fatal error message
230 lerrsf (_("bad character '%s' detected in check_char()"),
235 ("scanner requires -8 flag to use the character %s"),
241 /* clower - replace upper-case letter to lower-case */
246 return (Char) ((isascii (c) && isupper (c)) ? tolower (c) : c);
250 /* copy_string - returns a dynamically allocated copy of a string */
252 char *copy_string (str)
253 register const char *str;
255 register const char *c1;
261 for (c1 = str; *c1; ++c1) ;
263 size = (c1 - str + 1) * sizeof (char);
265 copy = (char *) flex_alloc (size);
268 flexfatal (_("dynamic memory failure in copy_string()"));
270 for (c2 = copy; (*c2++ = *str++) != 0;) ;
276 /* copy_unsigned_string -
277 * returns a dynamically allocated copy of a (potentially) unsigned string
280 Char *copy_unsigned_string (str)
287 for (c = str; *c; ++c) ;
289 copy = allocate_Character_array (c - str + 1);
291 for (c = copy; (*c++ = *str++) != 0;) ;
297 /* cclcmp - compares two characters for use by qsort with '\0' sorting last. */
299 int cclcmp (const void *a, const void *b)
301 if (!*(const Char *) a)
304 if (!*(const Char *) b)
307 return *(const Char *) a - *(const Char *) b;
311 /* dataend - finish up a block of data declarations */
315 /* short circuit any output */
321 /* add terminator for initialization; { for vi */
329 /* dataflush - flush generated data statements */
333 /* short circuit any output */
339 if (++dataline >= NUMDATALINES) {
340 /* Put out a blank line so that the table is grouped into
341 * large blocks that enable the user to find elements easily.
347 /* Reset the number of characters written on the current line. */
352 /* flexerror - report an error message and terminate */
357 fprintf (stderr, "%s: %s\n", program_name, msg);
362 /* flexfatal - report a fatal error message and terminate */
367 fprintf (stderr, _("%s: fatal internal error, %s\n"),
373 /* htoi - convert a hexadecimal digit string to an integer value */
380 (void) sscanf ((char *) str, "%x", &result);
386 /* lerrif - report an error message formatted with one integer argument */
388 void lerrif (msg, arg)
392 char errmsg[MAXLINE];
394 snprintf (errmsg, sizeof(errmsg), msg, arg);
399 /* lerrsf - report an error message formatted with one string argument */
401 void lerrsf (msg, arg)
402 const char *msg, arg[];
404 char errmsg[MAXLINE];
406 snprintf (errmsg, sizeof(errmsg)-1, msg, arg);
407 errmsg[sizeof(errmsg)-1] = 0; /* ensure NULL termination */
412 /* lerrsf_fatal - as lerrsf, but call flexfatal */
414 void lerrsf_fatal (msg, arg)
415 const char *msg, arg[];
417 char errmsg[MAXLINE];
419 snprintf (errmsg, sizeof(errmsg)-1, msg, arg);
420 errmsg[sizeof(errmsg)-1] = 0; /* ensure NULL termination */
425 /* line_directive_out - spit out a "#line" statement */
427 void line_directive_out (output_file, do_infile)
431 char directive[MAXLINE], filename[MAXLINE];
433 static const char *line_fmt = "#line %d \"%s\"\n";
438 s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME";
440 if (do_infile && !s1)
444 s3 = &filename[sizeof (filename) - 2];
446 while (s2 < s3 && *s1) {
457 snprintf (directive, sizeof(directive), line_fmt, linenum, filename);
459 snprintf (directive, sizeof(directive), line_fmt, 0, filename);
462 /* If output_file is nil then we should put the directive in
463 * the accumulated actions.
466 fputs (directive, output_file);
469 add_action (directive);
473 /* mark_defs1 - mark the current position in the action array as
474 * representing where the user's section 1 definitions end
475 * and the prolog begins
480 action_array[action_index++] = '\0';
481 action_offset = prolog_offset = action_index;
482 action_array[action_index] = '\0';
486 /* mark_prolog - mark the current position in the action array as
487 * representing the end of the action prolog
491 action_array[action_index++] = '\0';
492 action_offset = action_index;
493 action_array[action_index] = '\0';
497 /* mk2data - generate a data statement for a two-dimensional array
499 * Generates a data statement initializing the current 2-D array to "value".
504 /* short circuit any output */
508 if (datapos >= NUMDATAITEMS) {
522 out_dec ("%5d", value);
526 /* mkdata - generate a data statement
528 * Generates a data statement initializing the current array element to
534 /* short circuit any output */
538 if (datapos >= NUMDATAITEMS) {
551 out_dec ("%5d", value);
555 /* myctoi - return the integer represented by a string of digits */
562 (void) sscanf (array, "%d", &val);
568 /* myesc - return character corresponding to escape sequence */
587 #if defined (__STDC__)
610 while (isascii (array[sptr]) &&
611 isdigit (array[sptr]))
612 /* Don't increment inside loop control
613 * because if isdigit() is a macro it might
614 * expand into multiple increments ...
621 esc_char = otoi (array + 1);
632 while (isascii (array[sptr]) &&
633 isxdigit (array[sptr]))
634 /* Don't increment inside loop control
635 * because if isdigit() is a macro it might
636 * expand into multiple increments ...
643 esc_char = htoi (array + 2);
656 /* otoi - convert an octal digit string to an integer value */
663 (void) sscanf ((char *) str, "%o", &result);
668 /* out - various flavors of outputing a (possibly formatted) string for the
669 * generated scanner, keeping track of the line count.
678 void out_dec (fmt, n)
682 fprintf (stdout, fmt, n);
685 void out_dec2 (fmt, n1, n2)
689 fprintf (stdout, fmt, n1, n2);
692 void out_hex (fmt, x)
696 fprintf (stdout, fmt, x);
699 void out_str (fmt, str)
700 const char *fmt, str[];
702 fprintf (stdout,fmt, str);
705 void out_str3 (fmt, s1, s2, s3)
706 const char *fmt, s1[], s2[], s3[];
708 fprintf (stdout,fmt, s1, s2, s3);
711 void out_str_dec (fmt, str, n)
712 const char *fmt, str[];
715 fprintf (stdout,fmt, str, n);
731 /** Print "m4_define( [[def]], [[val]])m4_dnl\n".
732 * @param def The m4 symbol to define.
733 * @param val The definition; may be NULL.
736 void out_m4_define (const char* def, const char* val)
738 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
739 fprintf(stdout, fmt, def, val?val:"");
743 /* readable_form - return the the human-readable form of a character
745 * The returned string is in static storage.
748 char *readable_form (c)
751 static char rform[10];
753 if ((c >= 0 && c < 32) || c >= 127) {
766 #if defined (__STDC__)
774 snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c);
791 /* reallocate_array - increase the size of a dynamic array */
793 void *reallocate_array (array, size, element_size)
798 register void *new_array;
799 size_t num_bytes = element_size * size;
801 new_array = flex_realloc (array, num_bytes);
803 flexfatal (_("attempt to increase array size failed"));
809 /* skelout - write out one section of the skeleton file
812 * Copies skelfile or skel array to stdout until a line beginning with
813 * "%%" or EOF is found.
817 char buf_storage[MAXLINE];
818 char *buf = buf_storage;
821 /* "reset" the state by clearing the buffer and pushing a '1' */
825 sko_push(do_copy=true);
828 /* Loop pulling lines either from the skelfile, if we're using
829 * one, or from the skel[] array.
832 (fgets (buf, MAXLINE, skelfile) != NULL) :
833 ((buf = (char *) skel[skel_ind++]) != 0)) {
838 /* copy from skel array */
839 if (buf[0] == '%') { /* control line */
840 /* print the control line as a comment. */
841 if (ddebug && buf[1] != '#') {
842 if (buf[strlen (buf) - 1] == '\\')
843 out_str ("/* %s */\\\n", buf);
845 out_str ("/* %s */\n", buf);
848 /* We've been accused of using cryptic markers in the skel.
849 * So we'll use emacs-style-hyphenated-commands.
850 * We might consider a hash if this if-else-if-else
851 * chain gets too large.
853 #define cmd_match(s) (strncmp(buf,(s),strlen(s))==0)
856 /* %% is a break point for skelout() */
859 else if (cmd_match (CMD_PUSH)){
862 out_str("/*(state = (%s) */",do_copy?"true":"false");
864 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
866 else if (cmd_match (CMD_POP)){
869 out_str("/*(state = (%s) */",do_copy?"true":"false");
871 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
873 else if (cmd_match (CMD_IF_REENTRANT)){
875 do_copy = reentrant && do_copy;
877 else if (cmd_match (CMD_IF_NOT_REENTRANT)){
879 do_copy = !reentrant && do_copy;
881 else if (cmd_match(CMD_IF_BISON_BRIDGE)){
883 do_copy = bison_bridge_lval && do_copy;
885 else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){
887 do_copy = !bison_bridge_lval && do_copy;
889 else if (cmd_match (CMD_ENDIF)){
892 else if (cmd_match (CMD_IF_TABLES_SER)) {
893 do_copy = do_copy && tablesext;
895 else if (cmd_match (CMD_TABLES_YYDMAP)) {
896 if (tablesext && yydmap_buf.elts)
897 outn ((char *) (yydmap_buf.elts));
899 else if (cmd_match (CMD_DEFINE_YYTABLES)) {
900 out_str("#define YYTABLES_NAME \"%s\"\n",
901 tablesname?tablesname:"yytables");
903 else if (cmd_match (CMD_IF_CPP_ONLY)) {
906 do_copy = C_plus_plus;
908 else if (cmd_match (CMD_IF_C_ONLY)) {
911 do_copy = !C_plus_plus;
913 else if (cmd_match (CMD_IF_C_OR_CPP)) {
914 /* %* for C and C++ */
918 else if (cmd_match (CMD_NOT_FOR_HEADER)) {
919 /* %c begin linkage-only (non-header) code. */
922 else if (cmd_match (CMD_OK_FOR_HEADER)) {
923 /* %e end linkage-only code. */
926 else if (buf[1] == '#') {
927 /* %# a comment in the skel. ignore. */
930 flexfatal (_("bad line in skeleton file"));
940 /* transition_struct_out - output a yy_trans_info structure
942 * outputs the yy_trans_info structure with the two elements, element_v and
943 * element_n. Formats the output with spaces and carriage returns.
946 void transition_struct_out (element_v, element_n)
947 int element_v, element_n;
950 /* short circuit any output */
954 out_dec2 (" {%4d,%4d },", element_v, element_n);
956 datapos += TRANS_STRUCT_PRINT_LENGTH;
958 if (datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH) {
961 if (++dataline % 10 == 0)
969 /* The following is only needed when building flex's parser using certain
970 * broken versions of bison.
972 void *yy_flex_xmalloc (size)
975 void *result = flex_alloc ((size_t) size);
979 ("memory allocation failed in yy_flex_xmalloc()"));
985 /* zero_out - set a region of memory to 0
987 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
990 void zero_out (region_ptr, size_in_bytes)
992 size_t size_in_bytes;
994 register char *rp, *rp_end;
997 rp_end = region_ptr + size_in_bytes;
1003 /* Remove all '\n' and '\r' characters, if any, from the end of str.
1004 * str can be any null-terminated string, or NULL.
1011 if (!str || !*str) /* s is null or empty string */
1014 /* find end of string minus one */
1020 while (p >= str && (*p == '\r' || *p == '\n'))