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 $ */
30 /* $FreeBSD: src/usr.bin/lex/misc.c,v 1.5 1999/10/27 07:56:45 obrien Exp $ */
31 /* $DragonFly: src/usr.bin/lex/misc.c,v 1.4 2005/08/04 17:31:22 drhodus Exp $ */
36 void action_define(char *defname, int value)
40 if ( (int) strlen( defname ) > MAXLINE / 2 )
42 format_pinpoint_message( _( "name \"%s\" ridiculously long" ),
47 sprintf( buf, "#define %s %d\n", defname, value );
52 void add_action(char *new_text)
54 int len = strlen( new_text );
56 while ( len + action_index >= action_size - 10 /* slop */ )
58 int new_size = action_size * 2;
61 /* Increase just a little, to try to avoid overflow
64 action_size += action_size / 8;
66 action_size = new_size;
69 reallocate_character_array( action_array, action_size );
72 strcpy( &action_array[action_index], new_text );
78 /* allocate_array - allocate memory for an integer array of the given size */
80 void *allocate_array(int size, size_t element_size)
83 size_t num_bytes = element_size * size;
85 mem = flex_alloc( num_bytes );
88 _( "memory allocation failed in allocate_array()" ) );
94 /* all_lower - true if a string is all lower-case */
96 int all_lower(char *str)
100 if ( ! isascii( (Char) *str ) || ! islower( *str ) )
109 /* all_upper - true if a string is all upper-case */
111 int all_upper(char *str)
115 if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
124 /* bubble - bubble sort an integer array in increasing order
128 * void bubble( v, n );
131 * sorts the first n elements of array v and replaces them in
135 * v - the array to be sorted
136 * n - the number of elements of 'v' to be sorted
139 void bubble(int *v, int n)
143 for ( i = n; i > 1; --i )
144 for ( j = 1; j < i; ++j )
145 if ( v[j] > v[j + 1] ) /* compare */
147 k = v[j]; /* exchange */
154 /* check_char - checks a character to make sure it's within the range
155 * we're expecting. If not, generates fatal error message
159 void check_char(int c)
162 lerrsf( _( "bad character '%s' detected in check_char()" ),
163 readable_form( c ) );
167 _( "scanner requires -8 flag to use the character %s" ),
168 readable_form( c ) );
173 /* clower - replace upper-case letter to lower-case */
177 return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
181 /* copy_string - returns a dynamically allocated copy of a string */
183 char *copy_string(const char *str)
191 for ( c1 = str; *c1; ++c1 )
194 size = (c1 - str + 1) * sizeof( char );
195 copy = (char *) flex_alloc( size );
198 flexfatal( _( "dynamic memory failure in copy_string()" ) );
200 for ( c2 = copy; (*c2++ = *str++) != 0; )
207 /* copy_unsigned_string -
208 * returns a dynamically allocated copy of a (potentially) unsigned string
211 Char *copy_unsigned_string(Char *str)
217 for ( c = str; *c; ++c )
220 copy = allocate_Character_array( c - str + 1 );
222 for ( c = copy; (*c++ = *str++) != 0; )
229 /* cshell - shell sort a character array in increasing order
234 * int n, special_case_0;
235 * cshell( v, n, special_case_0 );
238 * Does a shell sort of the first n elements of array v.
239 * If special_case_0 is true, then any element equal to 0
240 * is instead assumed to have infinite weight.
243 * v - array to be sorted
244 * n - number of elements of v to be sorted
247 void cshell(Char *v, int n, int special_case_0)
252 for ( gap = n / 2; gap > 0; gap = gap / 2 )
253 for ( i = gap; i < n; ++i )
254 for ( j = i - gap; j >= 0; j = j - gap )
258 if ( special_case_0 )
263 else if ( v[j] != 0 && v[j] <= v[jg] )
267 else if ( v[j] <= v[jg] )
277 /* dataend - finish up a block of data declarations */
284 /* add terminator for initialization; { for vi */
292 /* dataflush - flush generated data statements */
298 if ( ++dataline >= NUMDATALINES )
300 /* Put out a blank line so that the table is grouped into
301 * large blocks that enable the user to find elements easily.
307 /* Reset the number of characters written on the current line. */
312 /* flexerror - report an error message and terminate */
314 void flexerror(const char *msg)
316 fprintf( stderr, "%s: %s\n", program_name, msg );
321 /* flexfatal - report a fatal error message and terminate */
323 void flexfatal(const char *msg)
325 fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
331 /* htoi - convert a hexadecimal digit string to an integer value */
337 (void) sscanf( (char *) str, "%x", &result );
343 /* lerrif - report an error message formatted with one integer argument */
345 void lerrif(const char *msg, int arg)
347 char errmsg[MAXLINE];
348 (void) sprintf( errmsg, msg, arg );
353 /* lerrsf - report an error message formatted with one string argument */
355 void lerrsf(const char *msg, const char *arg)
357 char errmsg[MAXLINE];
359 (void) sprintf( errmsg, msg, arg );
364 /* line_directive_out - spit out a "#line" statement */
366 void line_directive_out(FILE *output_file, int do_infile)
368 char directive[MAXLINE], filename[MAXLINE];
370 static char line_fmt[] = "#line %d \"%s\"\n";
372 if ( ! gen_line_dirs )
375 if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
376 /* don't know the filename to use, skip */
379 s1 = do_infile ? infilename : outfilename;
381 s3 = &filename[sizeof( filename ) - 2];
383 while ( s2 < s3 && *s1 )
395 sprintf( directive, line_fmt, linenum, filename );
398 if ( output_file == stdout )
399 /* Account for the line directive itself. */
402 sprintf( directive, line_fmt, out_linenum, filename );
405 /* If output_file is nil then we should put the directive in
406 * the accumulated actions.
410 fputs( directive, output_file );
413 add_action( directive );
417 /* mark_defs1 - mark the current position in the action array as
418 * representing where the user's section 1 definitions end
419 * and the prolog begins
421 void mark_defs1(void)
424 action_array[action_index++] = '\0';
425 action_offset = prolog_offset = action_index;
426 action_array[action_index] = '\0';
430 /* mark_prolog - mark the current position in the action array as
431 * representing the end of the action prolog
433 void mark_prolog(void)
435 action_array[action_index++] = '\0';
436 action_offset = action_index;
437 action_array[action_index] = '\0';
441 /* mk2data - generate a data statement for a two-dimensional array
443 * Generates a data statement initializing the current 2-D array to "value".
445 void mk2data(int value)
447 if ( datapos >= NUMDATAITEMS )
462 out_dec( "%5d", value );
466 /* mkdata - generate a data statement
468 * Generates a data statement initializing the current array element to
471 void mkdata(int value)
473 if ( datapos >= NUMDATAITEMS )
487 out_dec( "%5d", value );
491 /* myctoi - return the integer represented by a string of digits */
493 int myctoi(char *array)
497 (void) sscanf( array, "%d", &val );
503 /* myesc - return character corresponding to escape sequence */
505 Char myesc(Char *array)
511 case 'b': return '\b';
512 case 'f': return '\f';
513 case 'n': return '\n';
514 case 'r': return '\r';
515 case 't': return '\t';
518 case 'a': return '\a';
519 case 'v': return '\v';
521 case 'a': return '\007';
522 case 'v': return '\013';
536 while ( isascii( array[sptr] ) &&
537 isdigit( array[sptr] ) )
538 /* Don't increment inside loop control
539 * because if isdigit() is a macro it might
540 * expand into multiple increments ...
547 esc_char = otoi( array + 1 );
558 while ( isascii( array[sptr] ) &&
559 isxdigit( (char) array[sptr] ) )
560 /* Don't increment inside loop control
561 * because if isdigit() is a macro it might
562 * expand into multiple increments ...
569 esc_char = htoi( array + 2 );
582 /* otoi - convert an octal digit string to an integer value */
588 (void) sscanf( (char *) str, "%o", &result );
593 /* out - various flavors of outputing a (possibly formatted) string for the
594 * generated scanner, keeping track of the line count.
597 void out(const char *str)
599 fputs( str, stdout );
600 out_line_count( str );
603 void out_dec(const char *fmt, int n)
606 out_line_count( fmt );
609 void out_dec2(const char *fmt, int n1, int n2)
611 printf( fmt, n1, n2 );
612 out_line_count( fmt );
615 void out_hex(const char *fmt, unsigned int x)
618 out_line_count( fmt );
621 void out_line_count(const char *str)
625 for ( i = 0; str[i]; ++i )
626 if ( str[i] == '\n' )
630 void out_str(const char *fmt, const char *str)
633 out_line_count( fmt );
634 out_line_count( str );
637 void out_str3(const char *fmt, const char *s1, const char *s2, const char *s3)
639 printf( fmt, s1, s2, s3 );
640 out_line_count( fmt );
641 out_line_count( s1 );
642 out_line_count( s2 );
643 out_line_count( s3 );
646 void out_str_dec(const char *fmt, const char *str, int n)
648 printf( fmt, str, n );
649 out_line_count( fmt );
650 out_line_count( str );
661 void outn(const char *str)
664 out_line_count( str );
669 /* readable_form - return the the human-readable form of a character
671 * The returned string is in static storage.
674 char *readable_form(int c)
676 static char rform[10];
678 if ( (c >= 0 && c < 32) || c >= 127 )
682 case '\b': return "\\b";
683 case '\f': return "\\f";
684 case '\n': return "\\n";
685 case '\r': return "\\r";
686 case '\t': return "\\t";
689 case '\a': return "\\a";
690 case '\v': return "\\v";
694 (void) sprintf( rform, "\\%.3o",
713 /* reallocate_array - increase the size of a dynamic array */
715 void *reallocate_array(void *array, int size, size_t element_size)
718 size_t num_bytes = element_size * size;
720 new_array = flex_realloc( array, num_bytes );
722 flexfatal( _( "attempt to increase array size failed" ) );
728 /* skelout - write out one section of the skeleton file
731 * Copies skelfile or skel array to stdout until a line beginning with
732 * "%%" or EOF is found.
736 char buf_storage[MAXLINE];
737 char *buf = buf_storage;
740 /* Loop pulling lines either from the skelfile, if we're using
741 * one, or from the skel[] array.
744 (fgets( buf, MAXLINE, skelfile ) != NULL) :
745 ((buf = (char *) skel[skel_ind++]) != 0) )
746 { /* copy from skel array */
755 do_copy = C_plus_plus;
759 do_copy = ! C_plus_plus;
768 _( "bad line in skeleton file" ) );
775 /* Skeleton file reads include final
776 * newline, skel[] array does not.
786 /* transition_struct_out - output a yy_trans_info structure
788 * outputs the yy_trans_info structure with the two elements, element_v and
789 * element_n. Formats the output with spaces and carriage returns.
792 void transition_struct_out(int element_v, int element_n)
794 out_dec2( " {%4d,%4d },", element_v, element_n );
796 datapos += TRANS_STRUCT_PRINT_LENGTH;
798 if ( datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH )
802 if ( ++dataline % 10 == 0 )
810 /* The following is only needed when building flex's parser using certain
811 * broken versions of bison.
813 void *yy_flex_xmalloc(int size)
815 void *result = flex_alloc( (size_t) size );
819 _( "memory allocation failed in yy_flex_xmalloc()" ) );
825 /* zero_out - set a region of memory to 0
827 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
830 void zero_out(char *region_ptr, size_t size_in_bytes )
835 rp_end = region_ptr + size_in_bytes;
837 while ( rp < rp_end )