NetBSD/usr.bin/lex/misc.c

887 lines
16 KiB
C
Raw Normal View History

1993-05-04 11:44:39 +04:00
/* misc - miscellaneous flex routines */
1993-03-21 12:45:37 +03:00
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
1993-05-04 11:44:39 +04:00
* Vern Paxson.
1993-03-21 12:45:37 +03:00
*
* The United States Government has rights in this work pursuant
* to contract no. DE-AC03-76SF00098 between the United States
* Department of Energy and the University of California.
*
1993-05-04 11:44:39 +04:00
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1993-03-21 12:45:37 +03:00
*/
1998-01-05 08:15:43 +03:00
/* $NetBSD: misc.c,v 1.9 1998/01/05 05:15:55 perry Exp $ */
1993-03-21 12:45:37 +03:00
#include "flexdef.h"
1993-05-04 11:44:39 +04:00
1995-05-05 09:35:08 +04:00
void action_define( defname, value )
char *defname;
int value;
{
char buf[MAXLINE];
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
if ( (int) strlen( defname ) > MAXLINE / 2 )
{
format_pinpoint_message( _( "name \"%s\" ridiculously long" ),
defname );
return;
}
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
sprintf( buf, "#define %s %d\n", defname, value );
add_action( buf );
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
void add_action( new_text )
char *new_text;
{
1994-01-07 02:58:18 +03:00
int len = strlen( new_text );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
while ( len + action_index >= action_size - 10 /* slop */ )
{
1995-05-05 09:35:08 +04:00
int new_size = action_size * 2;
if ( new_size <= 0 )
/* Increase just a little, to try to avoid overflow
* on 16-bit machines.
*/
action_size += action_size / 8;
else
action_size = new_size;
1993-12-02 22:17:24 +03:00
action_array =
reallocate_character_array( action_array, action_size );
}
1993-03-21 12:45:37 +03:00
1994-01-07 02:58:18 +03:00
strcpy( &action_array[action_index], new_text );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
action_index += len;
}
1993-03-21 12:45:37 +03:00
/* allocate_array - allocate memory for an integer array of the given size */
void *allocate_array( size, element_size )
1995-05-05 09:35:08 +04:00
int size;
size_t element_size;
1993-12-02 22:17:24 +03:00
{
register void *mem;
1995-05-05 09:35:08 +04:00
size_t num_bytes = element_size * size;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
mem = flex_alloc( num_bytes );
if ( ! mem )
flexfatal(
_( "memory allocation failed in allocate_array()" ) );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return mem;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* all_lower - true if a string is all lower-case */
1993-03-21 12:45:37 +03:00
int all_lower( str )
1993-12-02 22:17:24 +03:00
register char *str;
1993-03-21 12:45:37 +03:00
{
1993-12-02 22:17:24 +03:00
while ( *str )
{
if ( ! isascii( (Char) *str ) || ! islower( *str ) )
return 0;
++str;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return 1;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* all_upper - true if a string is all upper-case */
1993-03-21 12:45:37 +03:00
int all_upper( str )
1993-12-02 22:17:24 +03:00
register char *str;
1993-03-21 12:45:37 +03:00
{
1993-12-02 22:17:24 +03:00
while ( *str )
{
if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
return 0;
++str;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return 1;
}
1993-03-21 12:45:37 +03:00
/* bubble - bubble sort an integer array in increasing order
*
* synopsis
* int v[n], n;
1993-12-02 22:17:24 +03:00
* void bubble( v, n );
1993-03-21 12:45:37 +03:00
*
* description
* sorts the first n elements of array v and replaces them in
* increasing order.
*
* passed
* v - the array to be sorted
1993-12-02 22:17:24 +03:00
* n - the number of elements of 'v' to be sorted
*/
1993-03-21 12:45:37 +03:00
void bubble( v, n )
int v[], n;
1993-12-02 22:17:24 +03:00
{
register int i, j, k;
for ( i = n; i > 1; --i )
for ( j = 1; j < i; ++j )
if ( v[j] > v[j + 1] ) /* compare */
{
k = v[j]; /* exchange */
v[j] = v[j + 1];
v[j + 1] = k;
}
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* check_char - checks a character to make sure it's within the range
* we're expecting. If not, generates fatal error message
* and exits.
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
void check_char( c )
int c;
{
if ( c >= CSIZE )
1995-05-05 09:35:08 +04:00
lerrsf( _( "bad character '%s' detected in check_char()" ),
1993-12-02 22:17:24 +03:00
readable_form( c ) );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( c >= csize )
1995-05-05 09:35:08 +04:00
lerrsf(
_( "scanner requires -8 flag to use the character %s" ),
1993-12-02 22:17:24 +03:00
readable_form( c ) );
}
/* clower - replace upper-case letter to lower-case */
1993-03-21 12:45:37 +03:00
Char clower( c )
register int c;
1993-12-02 22:17:24 +03:00
{
return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* copy_string - returns a dynamically allocated copy of a string */
1993-03-21 12:45:37 +03:00
char *copy_string( str )
1995-05-05 09:35:08 +04:00
register const char *str;
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
register const char *c1;
register char *c2;
1993-12-02 22:17:24 +03:00
char *copy;
1995-05-05 09:35:08 +04:00
unsigned int size;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* find length */
1995-05-05 09:35:08 +04:00
for ( c1 = str; *c1; ++c1 )
1993-12-02 22:17:24 +03:00
;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
size = (c1 - str + 1) * sizeof( char );
copy = (char *) flex_alloc( size );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( copy == NULL )
1995-05-05 09:35:08 +04:00
flexfatal( _( "dynamic memory failure in copy_string()" ) );
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
for ( c2 = copy; (*c2++ = *str++) != 0; )
1993-12-02 22:17:24 +03:00
;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return copy;
}
1993-03-21 12:45:37 +03:00
/* copy_unsigned_string -
* returns a dynamically allocated copy of a (potentially) unsigned string
*/
Char *copy_unsigned_string( str )
register Char *str;
1993-12-02 22:17:24 +03:00
{
register Char *c;
Char *copy;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* find length */
for ( c = str; *c; ++c )
;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
copy = allocate_Character_array( c - str + 1 );
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
for ( c = copy; (*c++ = *str++) != 0; )
1993-12-02 22:17:24 +03:00
;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return copy;
}
1993-03-21 12:45:37 +03:00
/* cshell - shell sort a character array in increasing order
*
* synopsis
*
* Char v[n];
* int n, special_case_0;
* cshell( v, n, special_case_0 );
*
* description
1993-12-02 22:17:24 +03:00
* Does a shell sort of the first n elements of array v.
1993-03-21 12:45:37 +03:00
* If special_case_0 is true, then any element equal to 0
* is instead assumed to have infinite weight.
*
* passed
* v - array to be sorted
* n - number of elements of v to be sorted
*/
void cshell( v, n, special_case_0 )
Char v[];
int n, special_case_0;
1993-12-02 22:17:24 +03:00
{
int gap, i, j, jg;
Char k;
for ( gap = n / 2; gap > 0; gap = gap / 2 )
for ( i = gap; i < n; ++i )
for ( j = i - gap; j >= 0; j = j - gap )
{
jg = j + gap;
if ( special_case_0 )
{
if ( v[jg] == 0 )
break;
else if ( v[j] != 0 && v[j] <= v[jg] )
break;
}
else if ( v[j] <= v[jg] )
break;
k = v[j];
v[j] = v[jg];
v[jg] = k;
}
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* dataend - finish up a block of data declarations */
1993-03-21 12:45:37 +03:00
void dataend()
1993-12-02 22:17:24 +03:00
{
if ( datapos > 0 )
dataflush();
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* add terminator for initialization; { for vi */
1995-05-05 09:35:08 +04:00
outn( " } ;\n" );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
dataline = 0;
datapos = 0;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* dataflush - flush generated data statements */
1993-03-21 12:45:37 +03:00
void dataflush()
{
1995-05-05 09:35:08 +04:00
outc( '\n' );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( ++dataline >= NUMDATALINES )
{
/* Put out a blank line so that the table is grouped into
* large blocks that enable the user to find elements easily.
*/
1995-05-05 09:35:08 +04:00
outc( '\n' );
1993-12-02 22:17:24 +03:00
dataline = 0;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* Reset the number of characters written on the current line. */
datapos = 0;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* flexerror - report an error message and terminate */
1993-03-21 12:45:37 +03:00
void flexerror( msg )
1995-05-05 09:35:08 +04:00
const char msg[];
1993-12-02 22:17:24 +03:00
{
fprintf( stderr, "%s: %s\n", program_name, msg );
flexend( 1 );
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* flexfatal - report a fatal error message and terminate */
1993-03-21 12:45:37 +03:00
void flexfatal( msg )
1995-05-05 09:35:08 +04:00
const char msg[];
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
program_name, msg );
1993-12-02 22:17:24 +03:00
exit( 1 );
}
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
/* htoi - convert a hexadecimal digit string to an integer value */
int htoi( str )
Char str[];
{
unsigned int result;
(void) sscanf( (char *) str, "%x", &result );
return result;
}
1993-12-02 22:17:24 +03:00
/* lerrif - report an error message formatted with one integer argument */
1993-03-21 12:45:37 +03:00
void lerrif( msg, arg )
1995-05-05 09:35:08 +04:00
const char msg[];
1993-03-21 12:45:37 +03:00
int arg;
1993-12-02 22:17:24 +03:00
{
char errmsg[MAXLINE];
(void) sprintf( errmsg, msg, arg );
flexerror( errmsg );
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* lerrsf - report an error message formatted with one string argument */
1993-03-21 12:45:37 +03:00
void lerrsf( msg, arg )
1995-05-05 09:35:08 +04:00
const char msg[], arg[];
1993-12-02 22:17:24 +03:00
{
char errmsg[MAXLINE];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
(void) sprintf( errmsg, msg, arg );
flexerror( errmsg );
}
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
/* line_directive_out - spit out a "#line" statement */
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
void line_directive_out( output_file, do_infile )
FILE *output_file;
int do_infile;
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
char directive[MAXLINE], filename[MAXLINE];
char *s1, *s2, *s3;
static char line_fmt[] = "#line %d \"%s\"\n";
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
if ( ! gen_line_dirs )
return;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
/* don't know the filename to use, skip */
return;
1993-05-04 11:44:39 +04:00
1995-05-05 09:35:08 +04:00
s1 = do_infile ? infilename : outfilename;
s2 = filename;
s3 = &filename[sizeof( filename ) - 2];
1993-12-02 22:17:24 +03:00
1995-05-05 09:35:08 +04:00
while ( s2 < s3 && *s1 )
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
if ( *s1 == '\\' )
/* Escape the '\' */
*s2++ = '\\';
1993-12-02 22:17:24 +03:00
1995-05-05 09:35:08 +04:00
*s2++ = *s1++;
1993-12-02 22:17:24 +03:00
}
1993-05-04 11:44:39 +04:00
1995-05-05 09:35:08 +04:00
*s2 = '\0';
1993-12-02 22:17:24 +03:00
1995-05-05 09:35:08 +04:00
if ( do_infile )
sprintf( directive, line_fmt, linenum, filename );
else
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
if ( output_file == stdout )
/* Account for the line directive itself. */
++out_linenum;
1993-12-02 22:17:24 +03:00
1995-05-05 09:35:08 +04:00
sprintf( directive, line_fmt, out_linenum, filename );
}
/* If output_file is nil then we should put the directive in
* the accumulated actions.
*/
if ( output_file )
{
fputs( directive, output_file );
1993-12-02 22:17:24 +03:00
}
1995-05-05 09:35:08 +04:00
else
add_action( directive );
1993-05-04 11:44:39 +04:00
}
1993-12-02 22:17:24 +03:00
/* mark_defs1 - mark the current position in the action array as
* representing where the user's section 1 definitions end
* and the prolog begins
*/
void mark_defs1()
{
defs1_offset = 0;
action_array[action_index++] = '\0';
action_offset = prolog_offset = action_index;
action_array[action_index] = '\0';
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* mark_prolog - mark the current position in the action array as
* representing the end of the action prolog
*/
void mark_prolog()
{
action_array[action_index++] = '\0';
action_offset = action_index;
action_array[action_index] = '\0';
}
1993-03-21 12:45:37 +03:00
/* mk2data - generate a data statement for a two-dimensional array
*
1993-12-02 22:17:24 +03:00
* Generates a data statement initializing the current 2-D array to "value".
1993-03-21 12:45:37 +03:00
*/
void mk2data( value )
int value;
{
1993-12-02 22:17:24 +03:00
if ( datapos >= NUMDATAITEMS )
{
1995-05-05 09:35:08 +04:00
outc( ',' );
1993-12-02 22:17:24 +03:00
dataflush();
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( datapos == 0 )
/* Indent. */
1995-05-05 09:35:08 +04:00
out( " " );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
else
1995-05-05 09:35:08 +04:00
outc( ',' );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
++datapos;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
out_dec( "%5d", value );
1993-12-02 22:17:24 +03:00
}
1993-03-21 12:45:37 +03:00
/* mkdata - generate a data statement
*
1993-12-02 22:17:24 +03:00
* Generates a data statement initializing the current array element to
* "value".
1993-03-21 12:45:37 +03:00
*/
void mkdata( value )
int value;
{
1993-12-02 22:17:24 +03:00
if ( datapos >= NUMDATAITEMS )
{
1995-05-05 09:35:08 +04:00
outc( ',' );
1993-12-02 22:17:24 +03:00
dataflush();
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( datapos == 0 )
/* Indent. */
1995-05-05 09:35:08 +04:00
out( " " );
1993-12-02 22:17:24 +03:00
else
1995-05-05 09:35:08 +04:00
outc( ',' );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
++datapos;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
out_dec( "%5d", value );
1993-12-02 22:17:24 +03:00
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* myctoi - return the integer represented by a string of digits */
1993-03-21 12:45:37 +03:00
int myctoi( array )
1993-12-02 22:17:24 +03:00
char array[];
{
int val = 0;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
(void) sscanf( array, "%d", &val );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return val;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* myesc - return character corresponding to escape sequence */
1993-03-21 12:45:37 +03:00
Char myesc( array )
Char array[];
1993-12-02 22:17:24 +03:00
{
Char c, esc_char;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
switch ( array[1] )
{
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
1993-05-04 11:44:39 +04:00
1995-05-05 09:35:08 +04:00
#if __STDC__
1993-12-02 22:17:24 +03:00
case 'a': return '\a';
case 'v': return '\v';
#else
case 'a': return '\007';
case 'v': return '\013';
1993-05-04 11:44:39 +04:00
#endif
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{ /* \<octal> */
1993-12-06 22:29:01 +03:00
int sptr = 1;
1993-12-02 22:17:24 +03:00
while ( isascii( array[sptr] ) &&
isdigit( array[sptr] ) )
/* Don't increment inside loop control
* because if isdigit() is a macro it might
* expand into multiple increments ...
*/
++sptr;
c = array[sptr];
array[sptr] = '\0';
esc_char = otoi( array + 1 );
array[sptr] = c;
return esc_char;
}
case 'x':
{ /* \x<hex> */
int sptr = 2;
while ( isascii( array[sptr] ) &&
1995-05-05 09:35:08 +04:00
isxdigit( (char) array[sptr] ) )
1993-12-02 22:17:24 +03:00
/* Don't increment inside loop control
* because if isdigit() is a macro it might
* expand into multiple increments ...
*/
++sptr;
c = array[sptr];
array[sptr] = '\0';
esc_char = htoi( array + 2 );
array[sptr] = c;
return esc_char;
}
default:
return array[1];
}
1993-03-21 12:45:37 +03:00
}
1993-12-02 22:17:24 +03:00
/* otoi - convert an octal digit string to an integer value */
1993-03-21 12:45:37 +03:00
int otoi( str )
Char str[];
1993-12-02 22:17:24 +03:00
{
unsigned int result;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
(void) sscanf( (char *) str, "%o", &result );
return result;
}
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
/* out - various flavors of outputing a (possibly formatted) string for the
* generated scanner, keeping track of the line count.
*/
void out( str )
const char str[];
{
fputs( str, stdout );
out_line_count( str );
}
void out_dec( fmt, n )
const char fmt[];
int n;
{
printf( fmt, n );
out_line_count( fmt );
}
void out_dec2( fmt, n1, n2 )
const char fmt[];
int n1, n2;
{
printf( fmt, n1, n2 );
out_line_count( fmt );
}
void out_hex( fmt, x )
const char fmt[];
unsigned int x;
{
printf( fmt, x );
out_line_count( fmt );
}
void out_line_count( str )
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[];
{
printf( fmt, str );
out_line_count( fmt );
out_line_count( str );
}
void out_str3( fmt, s1, s2, s3 )
const char fmt[], s1[], s2[], s3[];
{
printf( fmt, s1, s2, s3 );
out_line_count( fmt );
out_line_count( s1 );
out_line_count( s2 );
out_line_count( s3 );
}
void out_str_dec( fmt, str, n )
const char fmt[], str[];
int n;
{
printf( fmt, str, n );
out_line_count( fmt );
out_line_count( str );
}
void outc( c )
int c;
{
putc( c, stdout );
if ( c == '\n' )
++out_linenum;
}
void outn( str )
const char str[];
{
puts( str );
out_line_count( str );
++out_linenum;
}
1993-03-21 12:45:37 +03:00
/* readable_form - return the the human-readable form of a character
*
* The returned string is in static storage.
*/
char *readable_form( c )
register int c;
1993-12-02 22:17:24 +03:00
{
static char rform[10];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( (c >= 0 && c < 32) || c >= 127 )
{
switch ( c )
{
case '\b': return "\\b";
case '\f': return "\\f";
case '\n': return "\\n";
case '\r': return "\\r";
case '\t': return "\\t";
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
#if __STDC__
1993-12-02 22:17:24 +03:00
case '\a': return "\\a";
case '\v': return "\\v";
#endif
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
default:
(void) sprintf( rform, "\\%.3o",
(unsigned int) c );
return rform;
}
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
else if ( c == ' ' )
return "' '";
else
{
rform[0] = c;
rform[1] = '\0';
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return rform;
}
1993-03-21 12:45:37 +03:00
}
/* reallocate_array - increase the size of a dynamic array */
void *reallocate_array( array, size, element_size )
void *array;
1995-05-05 09:35:08 +04:00
int size;
size_t element_size;
1993-12-02 22:17:24 +03:00
{
register void *new_array;
1995-05-05 09:35:08 +04:00
size_t num_bytes = element_size * size;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
new_array = flex_realloc( array, num_bytes );
if ( ! new_array )
flexfatal( _( "attempt to increase array size failed" ) );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
return new_array;
}
1993-03-21 12:45:37 +03:00
/* skelout - write out one section of the skeleton file
*
1993-12-02 22:17:24 +03:00
* Description
* Copies skelfile or skel array to stdout until a line beginning with
* "%%" or EOF is found.
1993-03-21 12:45:37 +03:00
*/
void skelout()
1993-12-02 22:17:24 +03:00
{
char buf_storage[MAXLINE];
char *buf = buf_storage;
int do_copy = 1;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* Loop pulling lines either from the skelfile, if we're using
* one, or from the skel[] array.
*/
while ( skelfile ?
(fgets( buf, MAXLINE, skelfile ) != NULL) :
1995-05-05 09:35:08 +04:00
((buf = (char *) skel[skel_ind++]) != 0) )
1993-12-02 22:17:24 +03:00
{ /* copy from skel array */
if ( buf[0] == '%' )
{ /* control line */
switch ( buf[1] )
{
case '%':
return;
case '+':
do_copy = C_plus_plus;
break;
case '-':
do_copy = ! C_plus_plus;
break;
case '*':
do_copy = 1;
break;
default:
flexfatal(
1995-05-05 09:35:08 +04:00
_( "bad line in skeleton file" ) );
1993-12-02 22:17:24 +03:00
}
}
else if ( do_copy )
{
if ( skelfile )
/* Skeleton file reads include final
* newline, skel[] array does not.
*/
1995-05-05 09:35:08 +04:00
out( buf );
1993-12-02 22:17:24 +03:00
else
1995-05-05 09:35:08 +04:00
outn( buf );
1993-12-02 22:17:24 +03:00
}
}
}
1993-03-21 12:45:37 +03:00
/* transition_struct_out - output a yy_trans_info structure
*
* outputs the yy_trans_info structure with the two elements, element_v and
* element_n. Formats the output with spaces and carriage returns.
*/
void transition_struct_out( element_v, element_n )
int element_v, element_n;
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
out_dec2( " {%4d,%4d },", element_v, element_n );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
datapos += TRANS_STRUCT_PRINT_LENGTH;
1993-03-21 12:45:37 +03:00
1995-05-05 09:35:08 +04:00
if ( datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH )
1993-12-02 22:17:24 +03:00
{
1995-05-05 09:35:08 +04:00
outc( '\n' );
1993-12-02 22:17:24 +03:00
if ( ++dataline % 10 == 0 )
1995-05-05 09:35:08 +04:00
outc( '\n' );
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
datapos = 0;
}
}
1993-12-06 22:29:01 +03:00
/* The following is only needed when building flex's parser using certain
* broken versions of bison.
*/
void *yy_flex_xmalloc( size )
int size;
{
1995-05-05 09:35:08 +04:00
void *result = flex_alloc( (size_t) size );
1993-12-06 22:29:01 +03:00
if ( ! result )
1995-05-05 09:35:08 +04:00
flexfatal(
_( "memory allocation failed in yy_flex_xmalloc()" ) );
1993-12-06 22:29:01 +03:00
return result;
}
1993-12-02 22:17:24 +03:00
/* zero_out - set a region of memory to 0
*
* Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
*/
void zero_out( region_ptr, size_in_bytes )
char *region_ptr;
1995-05-05 09:35:08 +04:00
size_t size_in_bytes;
1993-03-21 12:45:37 +03:00
{
1993-12-02 22:17:24 +03:00
register char *rp, *rp_end;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
rp = region_ptr;
rp_end = region_ptr + size_in_bytes;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
while ( rp < rp_end )
*rp++ = 0;
1993-03-21 12:45:37 +03:00
}