summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c367
1 files changed, 73 insertions, 294 deletions
diff --git a/misc.c b/misc.c
index e3fdd50..343dd55 100644
--- a/misc.c
+++ b/misc.c
@@ -34,65 +34,10 @@
#include "flexdef.h"
#include "tables.h"
-#define CMD_IF_TABLES_SER "%if-tables-serialization"
-#define CMD_TABLES_YYDMAP "%tables-yydmap"
-#define CMD_DEFINE_YYTABLES "%define-yytables"
-#define CMD_IF_CPP_ONLY "%if-c++-only"
-#define CMD_IF_C_ONLY "%if-c-only"
-#define CMD_IF_C_OR_CPP "%if-c-or-c++"
-#define CMD_NOT_FOR_HEADER "%not-for-header"
-#define CMD_OK_FOR_HEADER "%ok-for-header"
-#define CMD_PUSH "%push"
-#define CMD_POP "%pop"
-#define CMD_IF_REENTRANT "%if-reentrant"
-#define CMD_IF_NOT_REENTRANT "%if-not-reentrant"
-#define CMD_IF_BISON_BRIDGE "%if-bison-bridge"
-#define CMD_IF_NOT_BISON_BRIDGE "%if-not-bison-bridge"
-#define CMD_ENDIF "%endif"
-
-/* we allow the skeleton to push and pop. */
-struct sko_state {
- bool dc; /**< do_copy */
-};
-static struct sko_state *sko_stack=0;
-static int sko_len=0,sko_sz=0;
-static void sko_push(bool dc)
-{
- if(!sko_stack){
- sko_sz = 1;
- sko_stack = (struct sko_state*)flex_alloc(sizeof(struct sko_state)*sko_sz);
- if (!sko_stack)
- flexfatal(_("allocation of sko_stack failed"));
- sko_len = 0;
- }
- if(sko_len >= sko_sz){
- sko_sz *= 2;
- sko_stack = (struct sko_state*)flex_realloc(sko_stack,sizeof(struct sko_state)*sko_sz);
- }
-
- /* initialize to zero and push */
- sko_stack[sko_len].dc = dc;
- sko_len++;
-}
-static void sko_peek(bool *dc)
-{
- if(sko_len <= 0)
- flex_die("peek attempt when sko stack is empty");
- if(dc)
- *dc = sko_stack[sko_len-1].dc;
-}
-static void sko_pop(bool* dc)
-{
- sko_peek(dc);
- sko_len--;
- if(sko_len < 0)
- flex_die("popped too many times in skeleton.");
-}
-
/* Append "#define defname value\n" to the running buffer. */
-void action_define (defname, value)
- const char *defname;
- int value;
+void action_define (
+ const char *defname,
+ int value)
{
char buf[MAXLINE];
char *cpy;
@@ -113,30 +58,8 @@ void action_define (defname, value)
}
-/** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer.
- * @param defname The macro name.
- * @param value The macro value, can be NULL, which is the same as the empty string.
- */
-void action_m4_define (const char *defname, const char * value)
-{
- char buf[MAXLINE];
-
- flexfatal ("DO NOT USE THIS FUNCTION!");
-
- if ((int) strlen (defname) > MAXLINE / 2) {
- format_pinpoint_message (_
- ("name \"%s\" ridiculously long"),
- defname);
- return;
- }
-
- snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:"");
- add_action (buf);
-}
-
/* Append "new_text" to the running buffer. */
-void add_action (new_text)
- const char *new_text;
+void add_action (const char *new_text)
{
int len = strlen (new_text);
@@ -164,9 +87,9 @@ void add_action (new_text)
/* allocate_array - allocate memory for an integer array of the given size */
-void *allocate_array (size, element_size)
- int size;
- size_t element_size;
+void *allocate_array (
+ int size,
+ size_t element_size)
{
register void *mem;
size_t num_bytes = element_size * size;
@@ -182,8 +105,7 @@ void *allocate_array (size, element_size)
/* all_lower - true if a string is all lower-case */
-int all_lower (str)
- register char *str;
+int all_lower (register char *str)
{
while (*str) {
if (!isascii ((Char) * str) || !islower ((Char) * str))
@@ -197,8 +119,7 @@ int all_lower (str)
/* all_upper - true if a string is all upper-case */
-int all_upper (str)
- register char *str;
+int all_upper (register char *str)
{
while (*str) {
if (!isascii ((Char) * str) || !isupper ((Char) * str))
@@ -223,8 +144,7 @@ int intcmp (const void *a, const void *b)
* and exits.
*/
-void check_char (c)
- int c;
+void check_char (int c)
{
if (c >= CSIZE)
lerrsf (_("bad character '%s' detected in check_char()"),
@@ -237,20 +157,9 @@ void check_char (c)
}
-
-/* clower - replace upper-case letter to lower-case */
-
-Char clower (c)
- register int c;
-{
- return (Char) ((isascii (c) && isupper (c)) ? tolower (c) : c);
-}
-
-
/* copy_string - returns a dynamically allocated copy of a string */
-char *copy_string (str)
- register const char *str;
+char *copy_string (register const char *str)
{
register const char *c1;
register char *c2;
@@ -277,8 +186,7 @@ char *copy_string (str)
* returns a dynamically allocated copy of a (potentially) unsigned string
*/
-Char *copy_unsigned_string (str)
- register Char *str;
+Char *copy_unsigned_string (register Char *str)
{
register Char *c;
Char *copy;
@@ -310,7 +218,7 @@ int cclcmp (const void *a, const void *b)
/* dataend - finish up a block of data declarations */
-void dataend ()
+void dataend (void)
{
/* short circuit any output */
if (gentables) {
@@ -318,8 +226,8 @@ void dataend ()
if (datapos > 0)
dataflush ();
- /* add terminator for initialization; { for vi */
- outn (" } ;\n");
+ /* add terminator for m4_define */
+ outn ("]])\n");
}
dataline = 0;
datapos = 0;
@@ -328,7 +236,7 @@ void dataend ()
/* dataflush - flush generated data statements */
-void dataflush ()
+void dataflush (void)
{
/* short circuit any output */
if (!gentables)
@@ -351,8 +259,7 @@ void dataflush ()
/* flexerror - report an error message and terminate */
-void flexerror (msg)
- const char *msg;
+void flexerror (const char *msg)
{
fprintf (stderr, "%s: %s\n", program_name, msg);
flexend (1);
@@ -361,8 +268,7 @@ void flexerror (msg)
/* flexfatal - report a fatal error message and terminate */
-void flexfatal (msg)
- const char *msg;
+void flexfatal (const char *msg)
{
fprintf (stderr, _("%s: fatal internal error, %s\n"),
program_name, msg);
@@ -372,8 +278,7 @@ void flexfatal (msg)
/* htoi - convert a hexadecimal digit string to an integer value */
-int htoi (str)
- Char str[];
+int htoi (Char str[])
{
unsigned int result;
@@ -385,9 +290,7 @@ int htoi (str)
/* lerrif - report an error message formatted with one integer argument */
-void lerrif (msg, arg)
- const char *msg;
- int arg;
+void lerrif (const char *msg, int arg)
{
char errmsg[MAXLINE];
@@ -398,8 +301,7 @@ void lerrif (msg, arg)
/* lerrsf - report an error message formatted with one string argument */
-void lerrsf (msg, arg)
- const char *msg, arg[];
+void lerrsf (const char *msg, const char arg[])
{
char errmsg[MAXLINE];
@@ -424,18 +326,19 @@ void lerrsf_fatal (msg, arg)
/* line_directive_out - spit out a "#line" statement */
-void line_directive_out (output_file, do_infile)
- FILE *output_file;
- int do_infile;
+void line_directive_out (
+ FILE *output_file,
+ int do_infile)
{
char directive[MAXLINE], filename[MAXLINE];
- char *s1, *s2, *s3;
+ const char *s1;
+ char *s2, *s3;
static const char *line_fmt = "#line %d \"%s\"\n";
if (!gen_line_dirs)
return;
- s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME";
+ s1 = do_infile ? infilename : outfilename;
if (do_infile && !s1)
s1 = "<stdin>";
@@ -474,7 +377,7 @@ void line_directive_out (output_file, do_infile)
* representing where the user's section 1 definitions end
* and the prolog begins
*/
-void mark_defs1 ()
+void mark_defs1 (void)
{
defs1_offset = 0;
action_array[action_index++] = '\0';
@@ -486,7 +389,7 @@ void mark_defs1 ()
/* mark_prolog - mark the current position in the action array as
* representing the end of the action prolog
*/
-void mark_prolog ()
+void mark_prolog (void)
{
action_array[action_index++] = '\0';
action_offset = action_index;
@@ -498,8 +401,7 @@ void mark_prolog ()
*
* Generates a data statement initializing the current 2-D array to "value".
*/
-void mk2data (value)
- int value;
+void mk2data (int value)
{
/* short circuit any output */
if (!gentables)
@@ -528,8 +430,7 @@ void mk2data (value)
* Generates a data statement initializing the current array element to
* "value".
*/
-void mkdata (value)
- int value;
+void mkdata (int value)
{
/* short circuit any output */
if (!gentables)
@@ -554,8 +455,7 @@ void mkdata (value)
/* myctoi - return the integer represented by a string of digits */
-int myctoi (array)
- const char *array;
+int myctoi (const char *array)
{
int val = 0;
@@ -567,8 +467,7 @@ int myctoi (array)
/* myesc - return character corresponding to escape sequence */
-Char myesc (array)
- Char array[];
+Char myesc (Char array[])
{
Char c, esc_char;
@@ -655,8 +554,7 @@ Char myesc (array)
/* otoi - convert an octal digit string to an integer value */
-int otoi (str)
- Char str[];
+int otoi (Char str[])
{
unsigned int result;
@@ -669,60 +567,58 @@ int otoi (str)
* generated scanner, keeping track of the line count.
*/
-void out (str)
- const char *str;
+void out (const char *str)
{
fputs (str, stdout);
}
-void out_dec (fmt, n)
- const char *fmt;
- int n;
+void out_dec (const char *fmt, int n)
{
fprintf (stdout, fmt, n);
}
-void out_dec2 (fmt, n1, n2)
- const char *fmt;
- int n1, n2;
+void out_dec2 (const char *fmt, int n1, int n2)
{
fprintf (stdout, fmt, n1, n2);
}
-void out_hex (fmt, x)
- const char *fmt;
- unsigned int x;
+void out_hex (const char *fmt, unsigned int x)
{
fprintf (stdout, fmt, x);
+ out_line_count (fmt);
+}
+
+void out_line_count (const char *str)
+{
+ register int i;
+
+ for (i = 0; str[i]; ++i)
+ if (str[i] == '\n')
+ ++out_linenum;
}
-void out_str (fmt, str)
- const char *fmt, str[];
+void out_str (const char *fmt, const char str[])
{
fprintf (stdout,fmt, str);
}
-void out_str3 (fmt, s1, s2, s3)
- const char *fmt, s1[], s2[], s3[];
+void out_str3 (const char *fmt, const char s1[],
+ const char s2[], const char s3[])
{
fprintf (stdout,fmt, s1, s2, s3);
}
-void out_str_dec (fmt, str, n)
- const char *fmt, str[];
- int n;
+void out_str_dec (const char *fmt, const char str[], int n)
{
fprintf (stdout,fmt, str, n);
}
-void outc (c)
- int c;
+void outc (int c)
{
fputc (c, stdout);
}
-void outn (str)
- const char *str;
+void outn (const char *str)
{
fputs (str,stdout);
fputc('\n',stdout);
@@ -730,13 +626,13 @@ void outn (str)
/** Print "m4_define( [[def]], [[val]])m4_dnl\n".
* @param def The m4 symbol to define.
- * @param val The definition; may be NULL.
+ * @param val The definition; may be NULL. If so, val="1".
* @return buf
*/
void out_m4_define (const char* def, const char* val)
{
const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
- fprintf(stdout, fmt, def, val?val:"");
+ fprintf(stdout, fmt, def, val?val:"1");
}
@@ -745,8 +641,7 @@ void out_m4_define (const char* def, const char* val)
* The returned string is in static storage.
*/
-char *readable_form (c)
- register int c;
+char *readable_form (register int c)
{
static char rform[10];
@@ -790,10 +685,10 @@ char *readable_form (c)
/* reallocate_array - increase the size of a dynamic array */
-void *reallocate_array (array, size, element_size)
- void *array;
- int size;
- size_t element_size;
+void *reallocate_array (
+ void *array,
+ int size,
+ size_t element_size)
{
register void *new_array;
size_t num_bytes = element_size * size;
@@ -809,131 +704,20 @@ void *reallocate_array (array, size, element_size)
/* skelout - write out one section of the skeleton file
*
* Description
- * Copies skelfile or skel array to stdout until a line beginning with
+ * Copies skelfile to stdout until a line beginning with
* "%%" or EOF is found.
*/
-void skelout ()
+void skelout (void)
{
char buf_storage[MAXLINE];
char *buf = buf_storage;
- bool do_copy = true;
-
- /* "reset" the state by clearing the buffer and pushing a '1' */
- if(sko_len > 0)
- sko_peek(&do_copy);
- sko_len = 0;
- sko_push(do_copy=true);
-
- /* Loop pulling lines either from the skelfile, if we're using
- * one, or from the skel[] array.
- */
- while (skelfile ?
- (fgets (buf, MAXLINE, skelfile) != NULL) :
- ((buf = (char *) skel[skel_ind++]) != 0)) {
-
- if (skelfile)
- chomp (buf);
-
- /* copy from skel array */
- if (buf[0] == '%') { /* control line */
- /* print the control line as a comment. */
- if (ddebug && buf[1] != '#') {
- if (buf[strlen (buf) - 1] == '\\')
- out_str ("/* %s */\\\n", buf);
- else
- out_str ("/* %s */\n", buf);
- }
-
- /* We've been accused of using cryptic markers in the skel.
- * So we'll use emacs-style-hyphenated-commands.
- * We might consider a hash if this if-else-if-else
- * chain gets too large.
- */
-#define cmd_match(s) (strncmp(buf,(s),strlen(s))==0)
-
- if (buf[1] == '%') {
- /* %% is a break point for skelout() */
- return;
- }
- else if (cmd_match (CMD_PUSH)){
- sko_push(do_copy);
- if(ddebug){
- out_str("/*(state = (%s) */",do_copy?"true":"false");
- }
- out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
- }
- else if (cmd_match (CMD_POP)){
- sko_pop(&do_copy);
- if(ddebug){
- out_str("/*(state = (%s) */",do_copy?"true":"false");
- }
- out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
- }
- else if (cmd_match (CMD_IF_REENTRANT)){
- sko_push(do_copy);
- do_copy = reentrant && do_copy;
- }
- else if (cmd_match (CMD_IF_NOT_REENTRANT)){
- sko_push(do_copy);
- do_copy = !reentrant && do_copy;
- }
- else if (cmd_match(CMD_IF_BISON_BRIDGE)){
- sko_push(do_copy);
- do_copy = bison_bridge_lval && do_copy;
- }
- else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){
- sko_push(do_copy);
- do_copy = !bison_bridge_lval && do_copy;
- }
- else if (cmd_match (CMD_ENDIF)){
- sko_pop(&do_copy);
- }
- else if (cmd_match (CMD_IF_TABLES_SER)) {
- do_copy = do_copy && tablesext;
- }
- else if (cmd_match (CMD_TABLES_YYDMAP)) {
- if (tablesext && yydmap_buf.elts)
- outn ((char *) (yydmap_buf.elts));
- }
- else if (cmd_match (CMD_DEFINE_YYTABLES)) {
- out_str("#define YYTABLES_NAME \"%s\"\n",
- tablesname?tablesname:"yytables");
- }
- else if (cmd_match (CMD_IF_CPP_ONLY)) {
- /* only for C++ */
- sko_push(do_copy);
- do_copy = C_plus_plus;
- }
- else if (cmd_match (CMD_IF_C_ONLY)) {
- /* %- only for C */
- sko_push(do_copy);
- do_copy = !C_plus_plus;
- }
- else if (cmd_match (CMD_IF_C_OR_CPP)) {
- /* %* for C and C++ */
- sko_push(do_copy);
- do_copy = true;
- }
- else if (cmd_match (CMD_NOT_FOR_HEADER)) {
- /* %c begin linkage-only (non-header) code. */
- OUT_BEGIN_CODE ();
- }
- else if (cmd_match (CMD_OK_FOR_HEADER)) {
- /* %e end linkage-only code. */
- OUT_END_CODE ();
- }
- else if (buf[1] == '#') {
- /* %# a comment in the skel. ignore. */
- }
- else {
- flexfatal (_("bad line in skeleton file"));
- }
- }
-
- else if (do_copy)
- outn (buf);
- } /* end while */
+ /* Loop pulling lines from the skelfile */
+ while ((fgets (buf, MAXLINE, skelfile) != NULL)) {
+ chomp (buf);
+ if (buf[0] == '%' && buf[1] == '%') break;
+ outn (buf);
+ }
}
@@ -943,8 +727,7 @@ void skelout ()
* element_n. Formats the output with spaces and carriage returns.
*/
-void transition_struct_out (element_v, element_n)
- int element_v, element_n;
+void transition_struct_out (int element_v, int element_n)
{
/* short circuit any output */
@@ -969,8 +752,7 @@ void transition_struct_out (element_v, element_n)
/* The following is only needed when building flex's parser using certain
* broken versions of bison.
*/
-void *yy_flex_xmalloc (size)
- int size;
+void *yy_flex_xmalloc (int size)
{
void *result = flex_alloc ((size_t) size);
@@ -987,9 +769,7 @@ void *yy_flex_xmalloc (size)
* Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
*/
-void zero_out (region_ptr, size_in_bytes)
- char *region_ptr;
- size_t size_in_bytes;
+void zero_out (char *region_ptr, size_t size_in_bytes)
{
register char *rp, *rp_end;
@@ -1003,8 +783,7 @@ void zero_out (region_ptr, size_in_bytes)
/* Remove all '\n' and '\r' characters, if any, from the end of str.
* str can be any null-terminated string, or NULL.
* returns str. */
-char *chomp (str)
- char *str;
+char *chomp (char *str)
{
char *p = str;