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 */
36 #define CMD_IF_TABLES_SER "%if-tables-serialization"
37 #define CMD_TABLES_YYDMAP "%tables-yydmap"
38 #define CMD_DEFINE_YYTABLES "%define-yytables"
39 #define CMD_IF_CPP_ONLY "%if-c++-only"
40 #define CMD_IF_C_ONLY "%if-c-only"
41 #define CMD_IF_C_OR_CPP "%if-c-or-c++"
42 #define CMD_NOT_FOR_HEADER "%not-for-header"
43 #define CMD_OK_FOR_HEADER "%ok-for-header"
44 #define CMD_PUSH "%push"
45 #define CMD_POP "%pop"
46 #define CMD_IF_REENTRANT "%if-reentrant"
47 #define CMD_IF_NOT_REENTRANT "%if-not-reentrant"
48 #define CMD_IF_BISON_BRIDGE "%if-bison-bridge"
49 #define CMD_IF_NOT_BISON_BRIDGE "%if-not-bison-bridge"
50 #define CMD_ENDIF "%endif"
52 /* we allow the skeleton to push and pop. */
54 bool dc; /**< do_copy */
56 static struct sko_state *sko_stack=0;
57 static int sko_len=0,sko_sz=0;
58 static void sko_push(bool dc)
62 sko_stack = malloc(sizeof(struct sko_state) * (size_t) sko_sz);
64 flexfatal(_("allocation of sko_stack failed"));
67 if(sko_len >= sko_sz){
69 sko_stack = realloc(sko_stack,
70 sizeof(struct sko_state) * (size_t) 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 (const char *defname, int 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 = xstrdup(defname);
110 buf_append (&defs_buf, &cpy, 1);
113 /* Append "new_text" to the running buffer. */
114 void add_action (const char *new_text)
116 int len = (int) strlen (new_text);
118 while (len + action_index >= action_size - 10 /* slop */ ) {
119 int new_size = action_size * 2;
122 /* Increase just a little, to try to avoid overflow
123 * on 16-bit machines.
125 action_size += action_size / 8;
127 action_size = new_size;
130 reallocate_character_array (action_array,
134 strcpy (&action_array[action_index], new_text);
140 /* allocate_array - allocate memory for an integer array of the given size */
142 void *allocate_array (int size, size_t element_size)
145 #if HAVE_REALLOCARRAY
146 /* reallocarray has built-in overflow detection */
147 mem = reallocarray(NULL, (size_t) size, element_size);
149 size_t num_bytes = (size_t) size * element_size;
150 mem = (size && SIZE_MAX / (size_t) size < element_size) ? NULL :
155 ("memory allocation failed in allocate_array()"));
161 /* all_lower - true if a string is all lower-case */
163 int all_lower (char *str)
166 if (!isascii ((unsigned char) * str) || !islower ((unsigned char) * str))
175 /* all_upper - true if a string is all upper-case */
177 int all_upper (char *str)
180 if (!isascii ((unsigned char) * str) || !isupper ((unsigned char) * str))
189 /* intcmp - compares two integers for use by qsort. */
191 int intcmp (const void *a, const void *b)
193 return *(const int *) a - *(const int *) b;
197 /* check_char - checks a character to make sure it's within the range
198 * we're expecting. If not, generates fatal error message
202 void check_char (int c)
205 lerr (_("bad character '%s' detected in check_char()"),
210 ("scanner requires -8 flag to use the character %s"),
216 /* clower - replace upper-case letter to lower-case */
218 unsigned char clower (int c)
220 return (unsigned char) ((isascii (c) && isupper (c)) ? tolower (c) : c);
224 char *xstrdup(const char *s)
228 if ((s2 = strdup(s)) == NULL)
229 flexfatal (_("memory allocation failure in xstrdup()"));
235 /* cclcmp - compares two characters for use by qsort with '\0' sorting last. */
237 int cclcmp (const void *a, const void *b)
239 if (!*(const unsigned char *) a)
242 if (!*(const unsigned char *) b)
245 return *(const unsigned char *) a - *(const unsigned char *) b;
249 /* dataend - finish up a block of data declarations */
253 /* short circuit any output */
259 /* add terminator for initialization; { for vi */
267 /* dataflush - flush generated data statements */
269 void dataflush (void)
271 /* short circuit any output */
277 if (++dataline >= NUMDATALINES) {
278 /* Put out a blank line so that the table is grouped into
279 * large blocks that enable the user to find elements easily.
285 /* Reset the number of characters written on the current line. */
290 /* flexerror - report an error message and terminate */
292 void flexerror (const char *msg)
294 fprintf (stderr, "%s: %s\n", program_name, msg);
299 /* flexfatal - report a fatal error message and terminate */
301 void flexfatal (const char *msg)
303 fprintf (stderr, _("%s: fatal internal error, %s\n"),
309 /* lerr - report an error message */
311 void lerr (const char *msg, ...)
313 char errmsg[MAXLINE];
317 vsnprintf (errmsg, sizeof(errmsg), msg, args);
323 /* lerr_fatal - as lerr, but call flexfatal */
325 void lerr_fatal (const char *msg, ...)
327 char errmsg[MAXLINE];
331 vsnprintf (errmsg, sizeof(errmsg), msg, args);
337 /* line_directive_out - spit out a "#line" statement */
339 void line_directive_out (FILE *output_file, int do_infile)
341 char directive[MAXLINE], filename[MAXLINE];
343 static const char line_fmt[] = "#line %d \"%s\"\n";
348 s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME";
350 if (do_infile && !s1)
354 s3 = &filename[sizeof (filename) - 2];
356 while (s2 < s3 && *s1) {
357 if (*s1 == '\\' || *s1 == '"')
358 /* Escape the '\' or '"' */
367 snprintf (directive, sizeof(directive), line_fmt, linenum, filename);
369 snprintf (directive, sizeof(directive), line_fmt, 0, filename);
372 /* If output_file is nil then we should put the directive in
373 * the accumulated actions.
376 fputs (directive, output_file);
379 add_action (directive);
383 /* mark_defs1 - mark the current position in the action array as
384 * representing where the user's section 1 definitions end
385 * and the prolog begins
387 void mark_defs1 (void)
390 action_array[action_index++] = '\0';
391 action_offset = prolog_offset = action_index;
392 action_array[action_index] = '\0';
396 /* mark_prolog - mark the current position in the action array as
397 * representing the end of the action prolog
399 void mark_prolog (void)
401 action_array[action_index++] = '\0';
402 action_offset = action_index;
403 action_array[action_index] = '\0';
407 /* mk2data - generate a data statement for a two-dimensional array
409 * Generates a data statement initializing the current 2-D array to "value".
411 void mk2data (int value)
413 /* short circuit any output */
417 if (datapos >= NUMDATAITEMS) {
431 out_dec ("%5d", value);
435 /* mkdata - generate a data statement
437 * Generates a data statement initializing the current array element to
440 void mkdata (int value)
442 /* short circuit any output */
446 if (datapos >= NUMDATAITEMS) {
459 out_dec ("%5d", value);
463 /* myctoi - return the integer represented by a string of digits */
465 int myctoi (const char *array)
469 (void) sscanf (array, "%d", &val);
475 /* myesc - return character corresponding to escape sequence */
477 unsigned char myesc (unsigned char array[])
479 unsigned char c, esc_char;
508 array[sptr] >= '0' && array[sptr] <= '7') {
515 esc_char = (unsigned char) strtoul (array + 1, NULL, 8);
526 while (sptr <= 3 && isxdigit (array[sptr])) {
527 /* Don't increment inside loop control
528 * because if isxdigit() is a macro it might
529 * expand into multiple increments ...
537 esc_char = (unsigned char) strtoul (array + 2, NULL, 16);
550 /* out - various flavors of outputing a (possibly formatted) string for the
551 * generated scanner, keeping track of the line count.
554 void out (const char *str)
559 void out_dec (const char *fmt, int n)
561 fprintf (stdout, fmt, n);
564 void out_dec2 (const char *fmt, int n1, int n2)
566 fprintf (stdout, fmt, n1, n2);
569 void out_hex (const char *fmt, unsigned int x)
571 fprintf (stdout, fmt, x);
574 void out_str (const char *fmt, const char str[])
576 fprintf (stdout,fmt, str);
579 void out_str3 (const char *fmt, const char s1[], const char s2[], const char s3[])
581 fprintf (stdout,fmt, s1, s2, s3);
584 void out_str_dec (const char *fmt, const char str[], int n)
586 fprintf (stdout,fmt, str, n);
594 void outn (const char *str)
600 /** Print "m4_define( [[def]], [[val]])m4_dnl\n".
601 * @param def The m4 symbol to define.
602 * @param val The definition; may be NULL.
604 void out_m4_define (const char* def, const char* val)
606 const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
607 fprintf(stdout, fmt, def, val?val:"");
611 /* readable_form - return the the human-readable form of a character
613 * The returned string is in static storage.
616 char *readable_form (int c)
618 static char rform[20];
620 if ((c >= 0 && c < 32) || c >= 127) {
638 snprintf (rform, sizeof(rform), "\\x%.2x", (unsigned int) c);
640 snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c);
657 /* reallocate_array - increase the size of a dynamic array */
659 void *reallocate_array (void *array, int size, size_t element_size)
662 #if HAVE_REALLOCARRAY
663 /* reallocarray has built-in overflow detection */
664 new_array = reallocarray(array, (size_t) size, element_size);
666 size_t num_bytes = (size_t) size * element_size;
667 new_array = (size && SIZE_MAX / (size_t) size < element_size) ? NULL :
668 realloc(array, num_bytes);
671 flexfatal (_("attempt to increase array size failed"));
677 /* skelout - write out one section of the skeleton file
680 * Copies skelfile or skel array to stdout until a line beginning with
681 * "%%" or EOF is found.
685 char buf_storage[MAXLINE];
686 char *buf = buf_storage;
689 /* "reset" the state by clearing the buffer and pushing a '1' */
693 sko_push(do_copy=true);
696 /* Loop pulling lines either from the skelfile, if we're using
697 * one, or from the skel[] array.
700 (fgets (buf, MAXLINE, skelfile) != NULL) :
701 ((buf = (char *) skel[skel_ind++]) != 0)) {
706 /* copy from skel array */
707 if (buf[0] == '%') { /* control line */
708 /* print the control line as a comment. */
709 if (ddebug && buf[1] != '#') {
710 if (buf[strlen (buf) - 1] == '\\')
711 out_str ("/* %s */\\\n", buf);
713 out_str ("/* %s */\n", buf);
716 /* We've been accused of using cryptic markers in the skel.
717 * So we'll use emacs-style-hyphenated-commands.
718 * We might consider a hash if this if-else-if-else
719 * chain gets too large.
721 #define cmd_match(s) (strncmp(buf,(s),strlen(s))==0)
724 /* %% is a break point for skelout() */
727 else if (cmd_match (CMD_PUSH)){
730 out_str("/*(state = (%s) */",do_copy?"true":"false");
732 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
734 else if (cmd_match (CMD_POP)){
737 out_str("/*(state = (%s) */",do_copy?"true":"false");
739 out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
741 else if (cmd_match (CMD_IF_REENTRANT)){
743 do_copy = reentrant && do_copy;
745 else if (cmd_match (CMD_IF_NOT_REENTRANT)){
747 do_copy = !reentrant && do_copy;
749 else if (cmd_match(CMD_IF_BISON_BRIDGE)){
751 do_copy = bison_bridge_lval && do_copy;
753 else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){
755 do_copy = !bison_bridge_lval && do_copy;
757 else if (cmd_match (CMD_ENDIF)){
760 else if (cmd_match (CMD_IF_TABLES_SER)) {
761 do_copy = do_copy && tablesext;
763 else if (cmd_match (CMD_TABLES_YYDMAP)) {
764 if (tablesext && yydmap_buf.elts)
765 outn ((char *) (yydmap_buf.elts));
767 else if (cmd_match (CMD_DEFINE_YYTABLES)) {
768 out_str("#define YYTABLES_NAME \"%s\"\n",
769 tablesname?tablesname:"yytables");
771 else if (cmd_match (CMD_IF_CPP_ONLY)) {
774 do_copy = C_plus_plus;
776 else if (cmd_match (CMD_IF_C_ONLY)) {
779 do_copy = !C_plus_plus;
781 else if (cmd_match (CMD_IF_C_OR_CPP)) {
782 /* %* for C and C++ */
786 else if (cmd_match (CMD_NOT_FOR_HEADER)) {
787 /* %c begin linkage-only (non-header) code. */
790 else if (cmd_match (CMD_OK_FOR_HEADER)) {
791 /* %e end linkage-only code. */
795 flexfatal (_("bad line in skeleton file"));
805 /* transition_struct_out - output a yy_trans_info structure
807 * outputs the yy_trans_info structure with the two elements, element_v and
808 * element_n. Formats the output with spaces and carriage returns.
811 void transition_struct_out (int element_v, int element_n)
814 /* short circuit any output */
818 out_dec2 (" {%4d,%4d },", element_v, element_n);
820 datapos += TRANS_STRUCT_PRINT_LENGTH;
822 if (datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH) {
825 if (++dataline % 10 == 0)
833 /* The following is only needed when building flex's parser using certain
834 * broken versions of bison.
836 * XXX: this is should go soon
838 void *yy_flex_xmalloc (int size)
842 result = malloc((size_t) size);
845 ("memory allocation failed in yy_flex_xmalloc()"));
851 /* Remove all '\n' and '\r' characters, if any, from the end of str.
852 * str can be any null-terminated string, or NULL.
854 char *chomp (char *str)
858 if (!str || !*str) /* s is null or empty string */
861 /* find end of string minus one */
867 while (p >= str && (*p == '\r' || *p == '\n'))