1 /* misc - miscellaneous flex routines */
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * The United States Government has rights in this work pursuant
11 * to contract no. DE-AC03-76SF00098 between the United States
12 * Department of Energy and the University of California.
14 * Redistribution and use in source and binary forms are permitted provided
15 * that: (1) source distributions retain this entire copyright notice and
16 * comment, and (2) distributions including binaries display the following
17 * acknowledgement: ``This product includes software developed by the
18 * University of California, Berkeley and its contributors'' in the
19 * documentation or other materials provided with the distribution and in
20 * all advertising materials mentioning features or use of this software.
21 * Neither the name of the University nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 /* $Header: /home/daffy/u0/vern/flex/RCS/misc.c,v 2.47 95/04/28 11:39:39 vern Exp $ */
35 void action_define( defname, value )
41 if ( (int) strlen( defname ) > MAXLINE / 2 )
43 format_pinpoint_message( _( "name \"%s\" ridiculously long" ),
48 sprintf( buf, "#define %s %d\n", defname, value );
53 void add_action( new_text )
56 int len = strlen( new_text );
58 while ( len + action_index >= action_size - 10 /* slop */ )
60 int new_size = action_size * 2;
63 /* Increase just a little, to try to avoid overflow
66 action_size += action_size / 8;
68 action_size = new_size;
71 reallocate_character_array( action_array, action_size );
74 strcpy( &action_array[action_index], new_text );
80 /* allocate_array - allocate memory for an integer array of the given size */
82 void *allocate_array( size, element_size )
87 size_t num_bytes = element_size * size;
89 mem = flex_alloc( num_bytes );
92 _( "memory allocation failed in allocate_array()" ) );
98 /* all_lower - true if a string is all lower-case */
105 if ( ! isascii( (Char) *str ) || ! islower( *str ) )
114 /* all_upper - true if a string is all upper-case */
121 if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
130 /* bubble - bubble sort an integer array in increasing order
134 * void bubble( v, n );
137 * sorts the first n elements of array v and replaces them in
141 * v - the array to be sorted
142 * n - the number of elements of 'v' to be sorted
148 register int i, j, k;
150 for ( i = n; i > 1; --i )
151 for ( j = 1; j < i; ++j )
152 if ( v[j] > v[j + 1] ) /* compare */
154 k = v[j]; /* exchange */
161 /* check_char - checks a character to make sure it's within the range
162 * we're expecting. If not, generates fatal error message
170 lerrsf( _( "bad character '%s' detected in check_char()" ),
171 readable_form( c ) );
175 _( "scanner requires -8 flag to use the character %s" ),
176 readable_form( c ) );
181 /* clower - replace upper-case letter to lower-case */
186 return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
190 /* copy_string - returns a dynamically allocated copy of a string */
192 char *copy_string( str )
193 register const char *str;
195 register const char *c1;
201 for ( c1 = str; *c1; ++c1 )
204 size = (c1 - str + 1) * sizeof( char );
205 copy = (char *) flex_alloc( size );
208 flexfatal( _( "dynamic memory failure in copy_string()" ) );
210 for ( c2 = copy; (*c2++ = *str++) != 0; )
217 /* copy_unsigned_string -
218 * returns a dynamically allocated copy of a (potentially) unsigned string
221 Char *copy_unsigned_string( str )
228 for ( c = str; *c; ++c )
231 copy = allocate_Character_array( c - str + 1 );
233 for ( c = copy; (*c++ = *str++) != 0; )
240 /* cshell - shell sort a character array in increasing order
245 * int n, special_case_0;
246 * cshell( v, n, special_case_0 );
249 * Does a shell sort of the first n elements of array v.
250 * If special_case_0 is true, then any element equal to 0
251 * is instead assumed to have infinite weight.
254 * v - array to be sorted
255 * n - number of elements of v to be sorted
258 void cshell( v, n, special_case_0 )
260 int n, special_case_0;
265 for ( gap = n / 2; gap > 0; gap = gap / 2 )
266 for ( i = gap; i < n; ++i )
267 for ( j = i - gap; j >= 0; j = j - gap )
271 if ( special_case_0 )
276 else if ( v[j] != 0 && v[j] <= v[jg] )
280 else if ( v[j] <= v[jg] )
290 /* dataend - finish up a block of data declarations */
297 /* add terminator for initialization; { for vi */
305 /* dataflush - flush generated data statements */
311 if ( ++dataline >= NUMDATALINES )
313 /* Put out a blank line so that the table is grouped into
314 * large blocks that enable the user to find elements easily.
320 /* Reset the number of characters written on the current line. */
325 /* flexerror - report an error message and terminate */
327 void flexerror( msg )
330 fprintf( stderr, "%s: %s\n", program_name, msg );
335 /* flexfatal - report a fatal error message and terminate */
337 void flexfatal( msg )
340 fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
346 /* htoi - convert a hexadecimal digit string to an integer value */
353 (void) sscanf( (char *) str, "%x", &result );
359 /* lerrif - report an error message formatted with one integer argument */
361 void lerrif( msg, arg )
365 char errmsg[MAXLINE];
366 (void) sprintf( errmsg, msg, arg );
371 /* lerrsf - report an error message formatted with one string argument */
373 void lerrsf( msg, arg )
374 const char msg[], arg[];
376 char errmsg[MAXLINE];
378 (void) sprintf( errmsg, msg, arg );
383 /* line_directive_out - spit out a "#line" statement */
385 void line_directive_out( output_file, do_infile )
389 char directive[MAXLINE], filename[MAXLINE];
391 static char line_fmt[] = "#line %d \"%s\"\n";
393 if ( ! gen_line_dirs )
396 if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
397 /* don't know the filename to use, skip */
400 s1 = do_infile ? infilename : outfilename;
402 s3 = &filename[sizeof( filename ) - 2];
404 while ( s2 < s3 && *s1 )
416 sprintf( directive, line_fmt, linenum, filename );
419 if ( output_file == stdout )
420 /* Account for the line directive itself. */
423 sprintf( directive, line_fmt, out_linenum, filename );
426 /* If output_file is nil then we should put the directive in
427 * the accumulated actions.
431 fputs( directive, output_file );
434 add_action( directive );
438 /* mark_defs1 - mark the current position in the action array as
439 * representing where the user's section 1 definitions end
440 * and the prolog begins
445 action_array[action_index++] = '\0';
446 action_offset = prolog_offset = action_index;
447 action_array[action_index] = '\0';
451 /* mark_prolog - mark the current position in the action array as
452 * representing the end of the action prolog
456 action_array[action_index++] = '\0';
457 action_offset = action_index;
458 action_array[action_index] = '\0';
462 /* mk2data - generate a data statement for a two-dimensional array
464 * Generates a data statement initializing the current 2-D array to "value".
466 void mk2data( value )
469 if ( datapos >= NUMDATAITEMS )
484 out_dec( "%5d", value );
488 /* mkdata - generate a data statement
490 * Generates a data statement initializing the current array element to
496 if ( datapos >= NUMDATAITEMS )
510 out_dec( "%5d", value );
514 /* myctoi - return the integer represented by a string of digits */
521 (void) sscanf( array, "%d", &val );
527 /* myesc - return character corresponding to escape sequence */
536 case 'b': return '\b';
537 case 'f': return '\f';
538 case 'n': return '\n';
539 case 'r': return '\r';
540 case 't': return '\t';
543 case 'a': return '\a';
544 case 'v': return '\v';
546 case 'a': return '\007';
547 case 'v': return '\013';
561 while ( isascii( array[sptr] ) &&
562 isdigit( array[sptr] ) )
563 /* Don't increment inside loop control
564 * because if isdigit() is a macro it might
565 * expand into multiple increments ...
572 esc_char = otoi( array + 1 );
583 while ( isascii( array[sptr] ) &&
584 isxdigit( (char) array[sptr] ) )
585 /* Don't increment inside loop control
586 * because if isdigit() is a macro it might
587 * expand into multiple increments ...
594 esc_char = htoi( array + 2 );
607 /* otoi - convert an octal digit string to an integer value */
614 (void) sscanf( (char *) str, "%o", &result );
619 /* out - various flavors of outputing a (possibly formatted) string for the
620 * generated scanner, keeping track of the line count.
626 fputs( str, stdout );
627 out_line_count( str );
630 void out_dec( fmt, n )
635 out_line_count( fmt );
638 void out_dec2( fmt, n1, n2 )
642 printf( fmt, n1, n2 );
643 out_line_count( fmt );
646 void out_hex( fmt, x )
651 out_line_count( fmt );
654 void out_line_count( str )
659 for ( i = 0; str[i]; ++i )
660 if ( str[i] == '\n' )
664 void out_str( fmt, str )
665 const char fmt[], str[];
668 out_line_count( fmt );
669 out_line_count( str );
672 void out_str3( fmt, s1, s2, s3 )
673 const char fmt[], s1[], s2[], s3[];
675 printf( fmt, s1, s2, s3 );
676 out_line_count( fmt );
677 out_line_count( s1 );
678 out_line_count( s2 );
679 out_line_count( s3 );
682 void out_str_dec( fmt, str, n )
683 const char fmt[], str[];
686 printf( fmt, str, n );
687 out_line_count( fmt );
688 out_line_count( str );
704 out_line_count( str );
709 /* readable_form - return the the human-readable form of a character
711 * The returned string is in static storage.
714 char *readable_form( c )
717 static char rform[10];
719 if ( (c >= 0 && c < 32) || c >= 127 )
723 case '\b': return "\\b";
724 case '\f': return "\\f";
725 case '\n': return "\\n";
726 case '\r': return "\\r";
727 case '\t': return "\\t";
730 case '\a': return "\\a";
731 case '\v': return "\\v";
735 (void) sprintf( rform, "\\%.3o",
754 /* reallocate_array - increase the size of a dynamic array */
756 void *reallocate_array( array, size, element_size )
761 register void *new_array;
762 size_t num_bytes = element_size * size;
764 new_array = flex_realloc( array, num_bytes );
766 flexfatal( _( "attempt to increase array size failed" ) );
772 /* skelout - write out one section of the skeleton file
775 * Copies skelfile or skel array to stdout until a line beginning with
776 * "%%" or EOF is found.
780 char buf_storage[MAXLINE];
781 char *buf = buf_storage;
784 /* Loop pulling lines either from the skelfile, if we're using
785 * one, or from the skel[] array.
788 (fgets( buf, MAXLINE, skelfile ) != NULL) :
789 ((buf = (char *) skel[skel_ind++]) != 0) )
790 { /* copy from skel array */
799 do_copy = C_plus_plus;
803 do_copy = ! C_plus_plus;
812 _( "bad line in skeleton file" ) );
819 /* Skeleton file reads include final
820 * newline, skel[] array does not.
830 /* transition_struct_out - output a yy_trans_info structure
832 * outputs the yy_trans_info structure with the two elements, element_v and
833 * element_n. Formats the output with spaces and carriage returns.
836 void transition_struct_out( element_v, element_n )
837 int element_v, element_n;
839 out_dec2( " {%4d,%4d },", element_v, element_n );
841 datapos += TRANS_STRUCT_PRINT_LENGTH;
843 if ( datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH )
847 if ( ++dataline % 10 == 0 )
855 /* The following is only needed when building flex's parser using certain
856 * broken versions of bison.
858 void *yy_flex_xmalloc( size )
861 void *result = flex_alloc( (size_t) size );
865 _( "memory allocation failed in yy_flex_xmalloc()" ) );
871 /* zero_out - set a region of memory to 0
873 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
876 void zero_out( region_ptr, size_in_bytes )
878 size_t size_in_bytes;
880 register char *rp, *rp_end;
883 rp_end = region_ptr + size_in_bytes;
885 while ( rp < rp_end )