Blame src/misc.c

Packit f00812
/* misc - miscellaneous flex routines */
Packit f00812
Packit f00812
/*  Copyright (c) 1990 The Regents of the University of California. */
Packit f00812
/*  All rights reserved. */
Packit f00812
Packit f00812
/*  This code is derived from software contributed to Berkeley by */
Packit f00812
/*  Vern Paxson. */
Packit f00812
Packit f00812
/*  The United States Government has rights in this work pursuant */
Packit f00812
/*  to contract no. DE-AC03-76SF00098 between the United States */
Packit f00812
/*  Department of Energy and the University of California. */
Packit f00812
Packit f00812
/*  This file is part of flex. */
Packit f00812
Packit f00812
/*  Redistribution and use in source and binary forms, with or without */
Packit f00812
/*  modification, are permitted provided that the following conditions */
Packit f00812
/*  are met: */
Packit f00812
Packit f00812
/*  1. Redistributions of source code must retain the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer. */
Packit f00812
/*  2. Redistributions in binary form must reproduce the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer in the */
Packit f00812
/*     documentation and/or other materials provided with the distribution. */
Packit f00812
Packit f00812
/*  Neither the name of the University nor the names of its contributors */
Packit f00812
/*  may be used to endorse or promote products derived from this software */
Packit f00812
/*  without specific prior written permission. */
Packit f00812
Packit f00812
/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
Packit f00812
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
Packit f00812
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
Packit f00812
/*  PURPOSE. */
Packit f00812
Packit f00812
#include "flexdef.h"
Packit f00812
#include "tables.h"
Packit f00812
Packit f00812
#define CMD_IF_TABLES_SER    "%if-tables-serialization"
Packit f00812
#define CMD_TABLES_YYDMAP    "%tables-yydmap"
Packit f00812
#define CMD_DEFINE_YYTABLES  "%define-yytables"
Packit f00812
#define CMD_IF_CPP_ONLY      "%if-c++-only"
Packit f00812
#define CMD_IF_C_ONLY        "%if-c-only"
Packit f00812
#define CMD_IF_C_OR_CPP      "%if-c-or-c++"
Packit f00812
#define CMD_NOT_FOR_HEADER   "%not-for-header"
Packit f00812
#define CMD_OK_FOR_HEADER    "%ok-for-header"
Packit f00812
#define CMD_PUSH             "%push"
Packit f00812
#define CMD_POP              "%pop"
Packit f00812
#define CMD_IF_REENTRANT     "%if-reentrant"
Packit f00812
#define CMD_IF_NOT_REENTRANT "%if-not-reentrant"
Packit f00812
#define CMD_IF_BISON_BRIDGE  "%if-bison-bridge"
Packit f00812
#define CMD_IF_NOT_BISON_BRIDGE  "%if-not-bison-bridge"
Packit f00812
#define CMD_ENDIF            "%endif"
Packit f00812
Packit f00812
/* we allow the skeleton to push and pop. */
Packit f00812
struct sko_state {
Packit f00812
    bool dc; /**< do_copy */
Packit f00812
};
Packit f00812
static struct sko_state *sko_stack=0;
Packit f00812
static int sko_len=0,sko_sz=0;
Packit f00812
static void sko_push(bool dc)
Packit f00812
{
Packit f00812
    if(!sko_stack){
Packit f00812
        sko_sz = 1;
Packit f00812
        sko_stack = malloc(sizeof(struct sko_state) * (size_t) sko_sz);
Packit f00812
        if (!sko_stack)
Packit f00812
            flexfatal(_("allocation of sko_stack failed"));
Packit f00812
        sko_len = 0;
Packit f00812
    }
Packit f00812
    if(sko_len >= sko_sz){
Packit f00812
        sko_sz *= 2;
Packit f00812
        sko_stack = realloc(sko_stack,
Packit f00812
			sizeof(struct sko_state) * (size_t) sko_sz);
Packit f00812
    }
Packit f00812
    
Packit f00812
    /* initialize to zero and push */
Packit f00812
    sko_stack[sko_len].dc = dc;
Packit f00812
    sko_len++;
Packit f00812
}
Packit f00812
static void sko_peek(bool *dc)
Packit f00812
{
Packit f00812
    if(sko_len <= 0)
Packit f00812
        flex_die("peek attempt when sko stack is empty");
Packit f00812
    if(dc)
Packit f00812
        *dc = sko_stack[sko_len-1].dc;
Packit f00812
}
Packit f00812
static void sko_pop(bool* dc)
Packit f00812
{
Packit f00812
    sko_peek(dc);
Packit f00812
    sko_len--;
Packit f00812
    if(sko_len < 0)
Packit f00812
        flex_die("popped too many times in skeleton.");
Packit f00812
}
Packit f00812
Packit f00812
/* Append "#define defname value\n" to the running buffer. */
Packit f00812
void action_define (const char *defname, int value)
Packit f00812
{
Packit f00812
	char    buf[MAXLINE];
Packit f00812
	char   *cpy;
Packit f00812
Packit f00812
	if ((int) strlen (defname) > MAXLINE / 2) {
Packit f00812
		format_pinpoint_message (_
Packit f00812
					 ("name \"%s\" ridiculously long"),
Packit f00812
					 defname);
Packit f00812
		return;
Packit f00812
	}
Packit f00812
Packit f00812
	snprintf (buf, sizeof(buf), "#define %s %d\n", defname, value);
Packit f00812
	add_action (buf);
Packit f00812
Packit f00812
	/* track #defines so we can undef them when we're done. */
Packit f00812
	cpy = xstrdup(defname);
Packit f00812
	buf_append (&defs_buf, &cpy, 1);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
#ifdef notdef
Packit f00812
/** Append "m4_define([[defname]],[[value]])m4_dnl\n" to the running buffer.
Packit f00812
 *  @param defname The macro name.
Packit f00812
 *  @param value The macro value, can be NULL, which is the same as the empty string.
Packit f00812
 */
Packit f00812
static void action_m4_define (const char *defname, const char * value)
Packit f00812
{
Packit f00812
	char    buf[MAXLINE];
Packit f00812
Packit f00812
    flexfatal ("DO NOT USE THIS FUNCTION!");
Packit f00812
Packit f00812
	if ((int) strlen (defname) > MAXLINE / 2) {
Packit f00812
		format_pinpoint_message (_
Packit f00812
					 ("name \"%s\" ridiculously long"),
Packit f00812
					 defname);
Packit f00812
		return;
Packit f00812
	}
Packit f00812
Packit f00812
	snprintf (buf, sizeof(buf), "m4_define([[%s]],[[%s]])m4_dnl\n", defname, value?value:"");
Packit f00812
	add_action (buf);
Packit f00812
}
Packit f00812
#endif
Packit f00812
Packit f00812
/* Append "new_text" to the running buffer. */
Packit f00812
void add_action (const char *new_text)
Packit f00812
{
Packit f00812
	int     len = (int) strlen (new_text);
Packit f00812
Packit f00812
	while (len + action_index >= action_size - 10 /* slop */ ) {
Packit f00812
		int     new_size = action_size * 2;
Packit f00812
Packit f00812
		if (new_size <= 0)
Packit f00812
			/* Increase just a little, to try to avoid overflow
Packit f00812
			 * on 16-bit machines.
Packit f00812
			 */
Packit f00812
			action_size += action_size / 8;
Packit f00812
		else
Packit f00812
			action_size = new_size;
Packit f00812
Packit f00812
		action_array =
Packit f00812
			reallocate_character_array (action_array,
Packit f00812
						    action_size);
Packit f00812
	}
Packit f00812
Packit f00812
	strcpy (&action_array[action_index], new_text);
Packit f00812
Packit f00812
	action_index += len;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* allocate_array - allocate memory for an integer array of the given size */
Packit f00812
Packit f00812
void   *allocate_array (int size, size_t element_size)
Packit f00812
{
Packit f00812
	void *mem;
Packit f00812
	size_t  num_bytes = element_size * size;
Packit f00812
Packit f00812
	mem = malloc(num_bytes);
Packit f00812
	if (!mem)
Packit f00812
		flexfatal (_
Packit f00812
			   ("memory allocation failed in allocate_array()"));
Packit f00812
Packit f00812
	return mem;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* all_lower - true if a string is all lower-case */
Packit f00812
Packit f00812
int all_lower (char *str)
Packit f00812
{
Packit f00812
	while (*str) {
Packit f00812
		if (!isascii ((unsigned char) * str) || !islower ((unsigned char) * str))
Packit f00812
			return 0;
Packit f00812
		++str;
Packit f00812
	}
Packit f00812
Packit f00812
	return 1;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* all_upper - true if a string is all upper-case */
Packit f00812
Packit f00812
int all_upper (char *str)
Packit f00812
{
Packit f00812
	while (*str) {
Packit f00812
		if (!isascii ((unsigned char) * str) || !isupper ((unsigned char) * str))
Packit f00812
			return 0;
Packit f00812
		++str;
Packit f00812
	}
Packit f00812
Packit f00812
	return 1;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* intcmp - compares two integers for use by qsort. */
Packit f00812
Packit f00812
int intcmp (const void *a, const void *b)
Packit f00812
{
Packit f00812
  return *(const int *) a - *(const int *) b;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* check_char - checks a character to make sure it's within the range
Packit f00812
 *		we're expecting.  If not, generates fatal error message
Packit f00812
 *		and exits.
Packit f00812
 */
Packit f00812
Packit f00812
void check_char (int c)
Packit f00812
{
Packit f00812
	if (c >= CSIZE)
Packit f00812
		lerr (_("bad character '%s' detected in check_char()"),
Packit f00812
			readable_form (c));
Packit f00812
Packit f00812
	if (c >= csize)
Packit f00812
		lerr (_
Packit f00812
			("scanner requires -8 flag to use the character %s"),
Packit f00812
			readable_form (c));
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
Packit f00812
/* clower - replace upper-case letter to lower-case */
Packit f00812
Packit f00812
unsigned char clower (int c)
Packit f00812
{
Packit f00812
	return (unsigned char) ((isascii (c) && isupper (c)) ? tolower (c) : c);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
char *xstrdup(const char *s)
Packit f00812
{
Packit f00812
	char *s2;
Packit f00812
Packit f00812
	if ((s2 = strdup(s)) == NULL)
Packit f00812
		flexfatal (_("memory allocation failure in xstrdup()"));
Packit f00812
Packit f00812
	return s2;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* cclcmp - compares two characters for use by qsort with '\0' sorting last. */
Packit f00812
Packit f00812
int cclcmp (const void *a, const void *b)
Packit f00812
{
Packit f00812
  if (!*(const unsigned char *) a)
Packit f00812
	return 1;
Packit f00812
  else
Packit f00812
	if (!*(const unsigned char *) b)
Packit f00812
	  return - 1;
Packit f00812
	else
Packit f00812
	  return *(const unsigned char *) a - *(const unsigned char *) b;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* dataend - finish up a block of data declarations */
Packit f00812
Packit f00812
void dataend (void)
Packit f00812
{
Packit f00812
	/* short circuit any output */
Packit f00812
	if (gentables) {
Packit f00812
Packit f00812
		if (datapos > 0)
Packit f00812
			dataflush ();
Packit f00812
Packit f00812
		/* add terminator for initialization; { for vi */
Packit f00812
		outn ("    } ;\n");
Packit f00812
	}
Packit f00812
	dataline = 0;
Packit f00812
	datapos = 0;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* dataflush - flush generated data statements */
Packit f00812
Packit f00812
void dataflush (void)
Packit f00812
{
Packit f00812
	/* short circuit any output */
Packit f00812
	if (!gentables)
Packit f00812
		return;
Packit f00812
Packit f00812
	outc ('\n');
Packit f00812
Packit f00812
	if (++dataline >= NUMDATALINES) {
Packit f00812
		/* Put out a blank line so that the table is grouped into
Packit f00812
		 * large blocks that enable the user to find elements easily.
Packit f00812
		 */
Packit f00812
		outc ('\n');
Packit f00812
		dataline = 0;
Packit f00812
	}
Packit f00812
Packit f00812
	/* Reset the number of characters written on the current line. */
Packit f00812
	datapos = 0;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* flexerror - report an error message and terminate */
Packit f00812
Packit f00812
void flexerror (const char *msg)
Packit f00812
{
Packit f00812
	fprintf (stderr, "%s: %s\n", program_name, msg);
Packit f00812
	flexend (1);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* flexfatal - report a fatal error message and terminate */
Packit f00812
Packit f00812
void flexfatal (const char *msg)
Packit f00812
{
Packit f00812
	fprintf (stderr, _("%s: fatal internal error, %s\n"),
Packit f00812
		 program_name, msg);
Packit f00812
	FLEX_EXIT (1);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* htoi - convert a hexadecimal digit string to an integer value */
Packit f00812
Packit f00812
int htoi (unsigned char str[])
Packit f00812
{
Packit f00812
	unsigned int result;
Packit f00812
Packit f00812
	(void) sscanf ((char *) str, "%x", &result);
Packit f00812
Packit f00812
	return result;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* lerr - report an error message */
Packit f00812
Packit f00812
void lerr (const char *msg, ...)
Packit f00812
{
Packit f00812
	char    errmsg[MAXLINE];
Packit f00812
	va_list args;
Packit f00812
Packit f00812
	va_start(args, msg);
Packit f00812
	vsnprintf (errmsg, sizeof(errmsg), msg, args);
Packit f00812
	va_end(args);
Packit f00812
	flexerror (errmsg);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* lerr_fatal - as lerr, but call flexfatal */
Packit f00812
Packit f00812
void lerr_fatal (const char *msg, ...)
Packit f00812
{
Packit f00812
	char    errmsg[MAXLINE];
Packit f00812
	va_list args;
Packit f00812
	va_start(args, msg);
Packit f00812
Packit f00812
	vsnprintf (errmsg, sizeof(errmsg), msg, args);
Packit f00812
	va_end(args);
Packit f00812
	flexfatal (errmsg);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* line_directive_out - spit out a "#line" statement */
Packit f00812
Packit f00812
void line_directive_out (FILE *output_file, int do_infile)
Packit f00812
{
Packit f00812
	char    directive[MAXLINE], filename[MAXLINE];
Packit f00812
	char   *s1, *s2, *s3;
Packit f00812
	static const char *line_fmt = "#line %d \"%s\"\n";
Packit f00812
Packit f00812
	if (!gen_line_dirs)
Packit f00812
		return;
Packit f00812
Packit f00812
	s1 = do_infile ? infilename : "M4_YY_OUTFILE_NAME";
Packit f00812
Packit f00812
	if (do_infile && !s1)
Packit f00812
        s1 = "<stdin>";
Packit f00812
    
Packit f00812
	s2 = filename;
Packit f00812
	s3 = &filename[sizeof (filename) - 2];
Packit f00812
Packit f00812
	while (s2 < s3 && *s1) {
Packit f00812
		if (*s1 == '\\')
Packit f00812
			/* Escape the '\' */
Packit f00812
			*s2++ = '\\';
Packit f00812
Packit f00812
		*s2++ = *s1++;
Packit f00812
	}
Packit f00812
Packit f00812
	*s2 = '\0';
Packit f00812
Packit f00812
	if (do_infile)
Packit f00812
		snprintf (directive, sizeof(directive), line_fmt, linenum, filename);
Packit f00812
	else {
Packit f00812
		snprintf (directive, sizeof(directive), line_fmt, 0, filename);
Packit f00812
	}
Packit f00812
Packit f00812
	/* If output_file is nil then we should put the directive in
Packit f00812
	 * the accumulated actions.
Packit f00812
	 */
Packit f00812
	if (output_file) {
Packit f00812
		fputs (directive, output_file);
Packit f00812
	}
Packit f00812
	else
Packit f00812
		add_action (directive);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mark_defs1 - mark the current position in the action array as
Packit f00812
 *               representing where the user's section 1 definitions end
Packit f00812
 *		 and the prolog begins
Packit f00812
 */
Packit f00812
void mark_defs1 (void)
Packit f00812
{
Packit f00812
	defs1_offset = 0;
Packit f00812
	action_array[action_index++] = '\0';
Packit f00812
	action_offset = prolog_offset = action_index;
Packit f00812
	action_array[action_index] = '\0';
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mark_prolog - mark the current position in the action array as
Packit f00812
 *               representing the end of the action prolog
Packit f00812
 */
Packit f00812
void mark_prolog (void)
Packit f00812
{
Packit f00812
	action_array[action_index++] = '\0';
Packit f00812
	action_offset = action_index;
Packit f00812
	action_array[action_index] = '\0';
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mk2data - generate a data statement for a two-dimensional array
Packit f00812
 *
Packit f00812
 * Generates a data statement initializing the current 2-D array to "value".
Packit f00812
 */
Packit f00812
void mk2data (int value)
Packit f00812
{
Packit f00812
	/* short circuit any output */
Packit f00812
	if (!gentables)
Packit f00812
		return;
Packit f00812
Packit f00812
	if (datapos >= NUMDATAITEMS) {
Packit f00812
		outc (',');
Packit f00812
		dataflush ();
Packit f00812
	}
Packit f00812
Packit f00812
	if (datapos == 0)
Packit f00812
		/* Indent. */
Packit f00812
		out ("    ");
Packit f00812
Packit f00812
	else
Packit f00812
		outc (',');
Packit f00812
Packit f00812
	++datapos;
Packit f00812
Packit f00812
	out_dec ("%5d", value);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkdata - generate a data statement
Packit f00812
 *
Packit f00812
 * Generates a data statement initializing the current array element to
Packit f00812
 * "value".
Packit f00812
 */
Packit f00812
void mkdata (int value)
Packit f00812
{
Packit f00812
	/* short circuit any output */
Packit f00812
	if (!gentables)
Packit f00812
		return;
Packit f00812
Packit f00812
	if (datapos >= NUMDATAITEMS) {
Packit f00812
		outc (',');
Packit f00812
		dataflush ();
Packit f00812
	}
Packit f00812
Packit f00812
	if (datapos == 0)
Packit f00812
		/* Indent. */
Packit f00812
		out ("    ");
Packit f00812
	else
Packit f00812
		outc (',');
Packit f00812
Packit f00812
	++datapos;
Packit f00812
Packit f00812
	out_dec ("%5d", value);
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* myctoi - return the integer represented by a string of digits */
Packit f00812
Packit f00812
int myctoi (const char *array)
Packit f00812
{
Packit f00812
	int     val = 0;
Packit f00812
Packit f00812
	(void) sscanf (array, "%d", &val;;
Packit f00812
Packit f00812
	return val;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* myesc - return character corresponding to escape sequence */
Packit f00812
Packit f00812
unsigned char myesc (unsigned char array[])
Packit f00812
{
Packit f00812
	unsigned char    c, esc_char;
Packit f00812
Packit f00812
	switch (array[1]) {
Packit f00812
	case 'b':
Packit f00812
		return '\b';
Packit f00812
	case 'f':
Packit f00812
		return '\f';
Packit f00812
	case 'n':
Packit f00812
		return '\n';
Packit f00812
	case 'r':
Packit f00812
		return '\r';
Packit f00812
	case 't':
Packit f00812
		return '\t';
Packit f00812
	case 'a':
Packit f00812
		return '\a';
Packit f00812
	case 'v':
Packit f00812
		return '\v';
Packit f00812
	case '0':
Packit f00812
	case '1':
Packit f00812
	case '2':
Packit f00812
	case '3':
Packit f00812
	case '4':
Packit f00812
	case '5':
Packit f00812
	case '6':
Packit f00812
	case '7':
Packit f00812
		{		/* \<octal> */
Packit f00812
			int     sptr = 1;
Packit f00812
Packit f00812
			while (isascii (array[sptr]) &&
Packit f00812
			       isdigit (array[sptr]))
Packit f00812
				/* Don't increment inside loop control
Packit f00812
				 * because if isdigit() is a macro it might
Packit f00812
				 * expand into multiple increments ...
Packit f00812
				 */
Packit f00812
				++sptr;
Packit f00812
Packit f00812
			c = array[sptr];
Packit f00812
			array[sptr] = '\0';
Packit f00812
Packit f00812
			esc_char = otoi (array + 1);
Packit f00812
Packit f00812
			array[sptr] = c;
Packit f00812
Packit f00812
			return esc_char;
Packit f00812
		}
Packit f00812
Packit f00812
	case 'x':
Packit f00812
		{		/* \x<hex> */
Packit f00812
			int     sptr = 2;
Packit f00812
Packit f00812
			while (isascii (array[sptr]) &&
Packit f00812
			       isxdigit (array[sptr]))
Packit f00812
				/* Don't increment inside loop control
Packit f00812
				 * because if isdigit() is a macro it might
Packit f00812
				 * expand into multiple increments ...
Packit f00812
				 */
Packit f00812
				++sptr;
Packit f00812
Packit f00812
			c = array[sptr];
Packit f00812
			array[sptr] = '\0';
Packit f00812
Packit f00812
			esc_char = htoi (array + 2);
Packit f00812
Packit f00812
			array[sptr] = c;
Packit f00812
Packit f00812
			return esc_char;
Packit f00812
		}
Packit f00812
Packit f00812
	default:
Packit f00812
		return array[1];
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* otoi - convert an octal digit string to an integer value */
Packit f00812
Packit f00812
int otoi (unsigned char str[])
Packit f00812
{
Packit f00812
	unsigned int result;
Packit f00812
Packit f00812
	(void) sscanf ((char *) str, "%o", &result);
Packit f00812
	return result;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* out - various flavors of outputing a (possibly formatted) string for the
Packit f00812
 *	 generated scanner, keeping track of the line count.
Packit f00812
 */
Packit f00812
Packit f00812
void out (const char *str)
Packit f00812
{
Packit f00812
	fputs (str, stdout);
Packit f00812
}
Packit f00812
Packit f00812
void out_dec (const char *fmt, int n)
Packit f00812
{
Packit f00812
	fprintf (stdout, fmt, n);
Packit f00812
}
Packit f00812
Packit f00812
void out_dec2 (const char *fmt, int n1, int n2)
Packit f00812
{
Packit f00812
	fprintf (stdout, fmt, n1, n2);
Packit f00812
}
Packit f00812
Packit f00812
void out_hex (const char *fmt, unsigned int x)
Packit f00812
{
Packit f00812
	fprintf (stdout, fmt, x);
Packit f00812
}
Packit f00812
Packit f00812
void out_str (const char *fmt, const char str[])
Packit f00812
{
Packit f00812
	fprintf (stdout,fmt, str);
Packit f00812
}
Packit f00812
Packit f00812
void out_str3 (const char *fmt, const char s1[], const char s2[], const char s3[])
Packit f00812
{
Packit f00812
	fprintf (stdout,fmt, s1, s2, s3);
Packit f00812
}
Packit f00812
Packit f00812
void out_str_dec (const char *fmt, const char str[], int n)
Packit f00812
{
Packit f00812
	fprintf (stdout,fmt, str, n);
Packit f00812
}
Packit f00812
Packit f00812
void outc (int c)
Packit f00812
{
Packit f00812
	fputc (c, stdout);
Packit f00812
}
Packit f00812
Packit f00812
void outn (const char *str)
Packit f00812
{
Packit f00812
	fputs (str,stdout);
Packit f00812
    fputc('\n',stdout);
Packit f00812
}
Packit f00812
Packit f00812
/** Print "m4_define( [[def]], [[val]])m4_dnl\n".
Packit f00812
 * @param def The m4 symbol to define.
Packit f00812
 * @param val The definition; may be NULL.
Packit f00812
 */
Packit f00812
void out_m4_define (const char* def, const char* val)
Packit f00812
{
Packit f00812
    const char * fmt = "m4_define( [[%s]], [[%s]])m4_dnl\n";
Packit f00812
    fprintf(stdout, fmt, def, val?val:"");
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* readable_form - return the the human-readable form of a character
Packit f00812
 *
Packit f00812
 * The returned string is in static storage.
Packit f00812
 */
Packit f00812
Packit f00812
char   *readable_form (int c)
Packit f00812
{
Packit f00812
	static char rform[20];
Packit f00812
Packit f00812
	if ((c >= 0 && c < 32) || c >= 127) {
Packit f00812
		switch (c) {
Packit f00812
		case '\b':
Packit f00812
			return "\\b";
Packit f00812
		case '\f':
Packit f00812
			return "\\f";
Packit f00812
		case '\n':
Packit f00812
			return "\\n";
Packit f00812
		case '\r':
Packit f00812
			return "\\r";
Packit f00812
		case '\t':
Packit f00812
			return "\\t";
Packit f00812
		case '\a':
Packit f00812
			return "\\a";
Packit f00812
		case '\v':
Packit f00812
			return "\\v";
Packit f00812
		default:
Packit f00812
			if(trace_hex)
Packit f00812
				snprintf (rform, sizeof(rform), "\\x%.2x", (unsigned int) c);
Packit f00812
			else
Packit f00812
				snprintf (rform, sizeof(rform), "\\%.3o", (unsigned int) c);
Packit f00812
			return rform;
Packit f00812
		}
Packit f00812
	}
Packit f00812
Packit f00812
	else if (c == ' ')
Packit f00812
		return "' '";
Packit f00812
Packit f00812
	else {
Packit f00812
		rform[0] = c;
Packit f00812
		rform[1] = '\0';
Packit f00812
Packit f00812
		return rform;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* reallocate_array - increase the size of a dynamic array */
Packit f00812
Packit f00812
void   *reallocate_array (void *array, int size, size_t element_size)
Packit f00812
{
Packit f00812
	void *new_array;
Packit f00812
	size_t  num_bytes = element_size * size;
Packit f00812
Packit f00812
	new_array = realloc(array, num_bytes);
Packit f00812
	if (!new_array)
Packit f00812
		flexfatal (_("attempt to increase array size failed"));
Packit f00812
Packit f00812
	return new_array;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* skelout - write out one section of the skeleton file
Packit f00812
 *
Packit f00812
 * Description
Packit f00812
 *    Copies skelfile or skel array to stdout until a line beginning with
Packit f00812
 *    "%%" or EOF is found.
Packit f00812
 */
Packit f00812
void skelout (void)
Packit f00812
{
Packit f00812
	char    buf_storage[MAXLINE];
Packit f00812
	char   *buf = buf_storage;
Packit f00812
	bool   do_copy = true;
Packit f00812
Packit f00812
    /* "reset" the state by clearing the buffer and pushing a '1' */
Packit f00812
    if(sko_len > 0)
Packit f00812
        sko_peek(&do_copy);
Packit f00812
    sko_len = 0;
Packit f00812
    sko_push(do_copy=true);
Packit f00812
Packit f00812
Packit f00812
	/* Loop pulling lines either from the skelfile, if we're using
Packit f00812
	 * one, or from the skel[] array.
Packit f00812
	 */
Packit f00812
	while (skelfile ?
Packit f00812
	       (fgets (buf, MAXLINE, skelfile) != NULL) :
Packit f00812
	       ((buf = (char *) skel[skel_ind++]) != 0)) {
Packit f00812
Packit f00812
		if (skelfile)
Packit f00812
			chomp (buf);
Packit f00812
Packit f00812
		/* copy from skel array */
Packit f00812
		if (buf[0] == '%') {	/* control line */
Packit f00812
			/* print the control line as a comment. */
Packit f00812
			if (ddebug && buf[1] != '#') {
Packit f00812
				if (buf[strlen (buf) - 1] == '\\')
Packit f00812
					out_str ("/* %s */\\\n", buf);
Packit f00812
				else
Packit f00812
					out_str ("/* %s */\n", buf);
Packit f00812
			}
Packit f00812
Packit f00812
			/* We've been accused of using cryptic markers in the skel.
Packit f00812
			 * So we'll use emacs-style-hyphenated-commands.
Packit f00812
             * We might consider a hash if this if-else-if-else
Packit f00812
             * chain gets too large.
Packit f00812
			 */
Packit f00812
#define cmd_match(s) (strncmp(buf,(s),strlen(s))==0)
Packit f00812
Packit f00812
			if (buf[1] == '%') {
Packit f00812
				/* %% is a break point for skelout() */
Packit f00812
				return;
Packit f00812
			}
Packit f00812
            else if (cmd_match (CMD_PUSH)){
Packit f00812
                sko_push(do_copy);
Packit f00812
                if(ddebug){
Packit f00812
                    out_str("/*(state = (%s) */",do_copy?"true":"false");
Packit f00812
                }
Packit f00812
                out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
Packit f00812
            }
Packit f00812
            else if (cmd_match (CMD_POP)){
Packit f00812
                sko_pop(&do_copy);
Packit f00812
                if(ddebug){
Packit f00812
                    out_str("/*(state = (%s) */",do_copy?"true":"false");
Packit f00812
                }
Packit f00812
                out_str("%s\n", buf[strlen (buf) - 1] =='\\' ? "\\" : "");
Packit f00812
            }
Packit f00812
            else if (cmd_match (CMD_IF_REENTRANT)){
Packit f00812
                sko_push(do_copy);
Packit f00812
                do_copy = reentrant && do_copy;
Packit f00812
            }
Packit f00812
            else if (cmd_match (CMD_IF_NOT_REENTRANT)){
Packit f00812
                sko_push(do_copy);
Packit f00812
                do_copy = !reentrant && do_copy;
Packit f00812
            }
Packit f00812
            else if (cmd_match(CMD_IF_BISON_BRIDGE)){
Packit f00812
                sko_push(do_copy);
Packit f00812
                do_copy = bison_bridge_lval && do_copy;
Packit f00812
            }
Packit f00812
            else if (cmd_match(CMD_IF_NOT_BISON_BRIDGE)){
Packit f00812
                sko_push(do_copy);
Packit f00812
                do_copy = !bison_bridge_lval && do_copy;
Packit f00812
            }
Packit f00812
            else if (cmd_match (CMD_ENDIF)){
Packit f00812
                sko_pop(&do_copy);
Packit f00812
            }
Packit f00812
			else if (cmd_match (CMD_IF_TABLES_SER)) {
Packit f00812
                do_copy = do_copy && tablesext;
Packit f00812
			}
Packit f00812
			else if (cmd_match (CMD_TABLES_YYDMAP)) {
Packit f00812
				if (tablesext && yydmap_buf.elts)
Packit f00812
					outn ((char *) (yydmap_buf.elts));
Packit f00812
			}
Packit f00812
            else if (cmd_match (CMD_DEFINE_YYTABLES)) {
Packit f00812
                out_str("#define YYTABLES_NAME \"%s\"\n",
Packit f00812
                        tablesname?tablesname:"yytables");
Packit f00812
            }
Packit f00812
			else if (cmd_match (CMD_IF_CPP_ONLY)) {
Packit f00812
				/* only for C++ */
Packit f00812
                sko_push(do_copy);
Packit f00812
				do_copy = C_plus_plus;
Packit f00812
			}
Packit f00812
			else if (cmd_match (CMD_IF_C_ONLY)) {
Packit f00812
				/* %- only for C */
Packit f00812
                sko_push(do_copy);
Packit f00812
				do_copy = !C_plus_plus;
Packit f00812
			}
Packit f00812
			else if (cmd_match (CMD_IF_C_OR_CPP)) {
Packit f00812
				/* %* for C and C++ */
Packit f00812
                sko_push(do_copy);
Packit f00812
				do_copy = true;
Packit f00812
			}
Packit f00812
			else if (cmd_match (CMD_NOT_FOR_HEADER)) {
Packit f00812
				/* %c begin linkage-only (non-header) code. */
Packit f00812
				OUT_BEGIN_CODE ();
Packit f00812
			}
Packit f00812
			else if (cmd_match (CMD_OK_FOR_HEADER)) {
Packit f00812
				/* %e end linkage-only code. */
Packit f00812
				OUT_END_CODE ();
Packit f00812
			}
Packit f00812
			else if (buf[1] == '#') {
Packit f00812
				/* %# a comment in the skel. ignore. */
Packit f00812
			}
Packit f00812
			else {
Packit f00812
				flexfatal (_("bad line in skeleton file"));
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		else if (do_copy) 
Packit f00812
            outn (buf);
Packit f00812
	}			/* end while */
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* transition_struct_out - output a yy_trans_info structure
Packit f00812
 *
Packit f00812
 * outputs the yy_trans_info structure with the two elements, element_v and
Packit f00812
 * element_n.  Formats the output with spaces and carriage returns.
Packit f00812
 */
Packit f00812
Packit f00812
void transition_struct_out (int element_v, int element_n)
Packit f00812
{
Packit f00812
Packit f00812
	/* short circuit any output */
Packit f00812
	if (!gentables)
Packit f00812
		return;
Packit f00812
Packit f00812
	out_dec2 (" {%4d,%4d },", element_v, element_n);
Packit f00812
Packit f00812
	datapos += TRANS_STRUCT_PRINT_LENGTH;
Packit f00812
Packit f00812
	if (datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH) {
Packit f00812
		outc ('\n');
Packit f00812
Packit f00812
		if (++dataline % 10 == 0)
Packit f00812
			outc ('\n');
Packit f00812
Packit f00812
		datapos = 0;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* The following is only needed when building flex's parser using certain
Packit f00812
 * broken versions of bison.
Packit f00812
 *
Packit f00812
 * XXX: this is should go soon
Packit f00812
 */
Packit f00812
void   *yy_flex_xmalloc (int size)
Packit f00812
{
Packit f00812
	void   *result;
Packit f00812
Packit f00812
	result = malloc((size_t) size);
Packit f00812
	if (!result)
Packit f00812
		flexfatal (_
Packit f00812
			   ("memory allocation failed in yy_flex_xmalloc()"));
Packit f00812
Packit f00812
	return result;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* Remove all '\n' and '\r' characters, if any, from the end of str.
Packit f00812
 * str can be any null-terminated string, or NULL.
Packit f00812
 * returns str. */
Packit f00812
char   *chomp (char *str)
Packit f00812
{
Packit f00812
	char   *p = str;
Packit f00812
Packit f00812
	if (!str || !*str)	/* s is null or empty string */
Packit f00812
		return str;
Packit f00812
Packit f00812
	/* find end of string minus one */
Packit f00812
	while (*p)
Packit f00812
		++p;
Packit f00812
	--p;
Packit f00812
Packit f00812
	/* eat newlines */
Packit f00812
	while (p >= str && (*p == '\r' || *p == '\n'))
Packit f00812
		*p-- = 0;
Packit f00812
	return str;
Packit f00812
}