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 $ */
35 void action_define(char *defname, int value)
39 if ( (int) strlen( defname ) > MAXLINE / 2 )
41 format_pinpoint_message( _( "name \"%s\" ridiculously long" ),
46 sprintf( buf, "#define %s %d\n", defname, value );
51 void add_action(char *new_text)
53 int len = strlen( new_text );
55 while ( len + action_index >= action_size - 10 /* slop */ )
57 int new_size = action_size * 2;
60 /* Increase just a little, to try to avoid overflow
63 action_size += action_size / 8;
65 action_size = new_size;
68 reallocate_character_array( action_array, action_size );
71 strcpy( &action_array[action_index], new_text );
77 /* allocate_array - allocate memory for an integer array of the given size */
79 void *allocate_array(int size, size_t element_size)
82 size_t num_bytes = element_size * size;
84 mem = flex_alloc( num_bytes );
87 _( "memory allocation failed in allocate_array()" ) );
93 /* all_lower - true if a string is all lower-case */
95 int all_lower(char *str)
99 if ( ! isascii( (Char) *str ) || ! islower( *str ) )
108 /* all_upper - true if a string is all upper-case */
110 int all_upper(char *str)
114 if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
123 /* bubble - bubble sort an integer array in increasing order
127 * void bubble( v, n );
130 * sorts the first n elements of array v and replaces them in
134 * v - the array to be sorted
135 * n - the number of elements of 'v' to be sorted
138 void bubble(int *v, int n)
142 for ( i = n; i > 1; --i )
143 for ( j = 1; j < i; ++j )
144 if ( v[j] > v[j + 1] ) /* compare */
146 k = v[j]; /* exchange */
153 /* check_char - checks a character to make sure it's within the range
154 * we're expecting. If not, generates fatal error message
158 void check_char(int c)
161 lerrsf( _( "bad character '%s' detected in check_char()" ),
162 readable_form( c ) );
166 _( "scanner requires -8 flag to use the character %s" ),
167 readable_form( c ) );
172 /* clower - replace upper-case letter to lower-case */
176 return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
180 /* copy_string - returns a dynamically allocated copy of a string */
182 char *copy_string(const char *str)
190 for ( c1 = str; *c1; ++c1 )
193 size = (c1 - str + 1) * sizeof( char );
194 copy = (char *) flex_alloc( size );
197 flexfatal( _( "dynamic memory failure in copy_string()" ) );
199 for ( c2 = copy; (*c2++ = *str++) != 0; )
206 /* copy_unsigned_string -
207 * returns a dynamically allocated copy of a (potentially) unsigned string
210 Char *copy_unsigned_string(Char *str)
216 for ( c = str; *c; ++c )
219 copy = allocate_Character_array( c - str + 1 );
221 for ( c = copy; (*c++ = *str++) != 0; )
228 /* cshell - shell sort a character array in increasing order
233 * int n, special_case_0;
234 * cshell( v, n, special_case_0 );
237 * Does a shell sort of the first n elements of array v.
238 * If special_case_0 is true, then any element equal to 0
239 * is instead assumed to have infinite weight.
242 * v - array to be sorted
243 * n - number of elements of v to be sorted
246 void cshell(Char *v, int n, int special_case_0)
251 for ( gap = n / 2; gap > 0; gap = gap / 2 )
252 for ( i = gap; i < n; ++i )
253 for ( j = i - gap; j >= 0; j = j - gap )
257 if ( special_case_0 )
262 else if ( v[j] != 0 && v[j] <= v[jg] )
266 else if ( v[j] <= v[jg] )
276 /* dataend - finish up a block of data declarations */
283 /* add terminator for initialization; { for vi */
291 /* dataflush - flush generated data statements */
297 if ( ++dataline >= NUMDATALINES )
299 /* Put out a blank line so that the table is grouped into
300 * large blocks that enable the user to find elements easily.
306 /* Reset the number of characters written on the current line. */
311 /* flexerror - report an error message and terminate */
313 void flexerror(const char *msg)
315 fprintf( stderr, "%s: %s\n", program_name, msg );
320 /* flexfatal - report a fatal error message and terminate */
322 void flexfatal(const char *msg)
324 fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
330 /* htoi - convert a hexadecimal digit string to an integer value */
336 (void) sscanf( (char *) str, "%x", &result );
342 /* lerrif - report an error message formatted with one integer argument */
344 void lerrif(const char *msg, int arg)
346 char errmsg[MAXLINE];
347 (void) sprintf( errmsg, msg, arg );
352 /* lerrsf - report an error message formatted with one string argument */
354 void lerrsf(const char *msg, const char *arg)
356 char errmsg[MAXLINE];
358 (void) sprintf( errmsg, msg, arg );
363 /* line_directive_out - spit out a "#line" statement */
365 void line_directive_out(FILE *output_file, int do_infile)
367 char directive[MAXLINE], filename[MAXLINE];
369 static char line_fmt[] = "#line %d \"%s\"\n";
371 if ( ! gen_line_dirs )
374 if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
375 /* don't know the filename to use, skip */
378 s1 = do_infile ? infilename : outfilename;
380 s3 = &filename[sizeof( filename ) - 2];
382 while ( s2 < s3 && *s1 )
394 sprintf( directive, line_fmt, linenum, filename );
397 if ( output_file == stdout )
398 /* Account for the line directive itself. */
401 sprintf( directive, line_fmt, out_linenum, filename );
404 /* If output_file is nil then we should put the directive in
405 * the accumulated actions.
409 fputs( directive, output_file );
412 add_action( directive );
416 /* mark_defs1 - mark the current position in the action array as
417 * representing where the user's section 1 definitions end
418 * and the prolog begins
420 void mark_defs1(void)
423 action_array[action_index++] = '\0';
424 action_offset = prolog_offset = action_index;
425 action_array[action_index] = '\0';
429 /* mark_prolog - mark the current position in the action array as
430 * representing the end of the action prolog
432 void mark_prolog(void)
434 action_array[action_index++] = '\0';
435 action_offset = action_index;
436 action_array[action_index] = '\0';
440 /* mk2data - generate a data statement for a two-dimensional array
442 * Generates a data statement initializing the current 2-D array to "value".
444 void mk2data(int value)
446 if ( datapos >= NUMDATAITEMS )
461 out_dec( "%5d", value );
465 /* mkdata - generate a data statement
467 * Generates a data statement initializing the current array element to
470 void mkdata(int value)
472 if ( datapos >= NUMDATAITEMS )
486 out_dec( "%5d", value );
490 /* myctoi - return the integer represented by a string of digits */
492 int myctoi(char *array)
496 (void) sscanf( array, "%d", &val );
502 /* myesc - return character corresponding to escape sequence */
504 Char myesc(Char *array)
510 case 'b': return '\b';
511 case 'f': return '\f';
512 case 'n': return '\n';
513 case 'r': return '\r';
514 case 't': return '\t';
515 case 'a': return '\a';
516 case 'v': return '\v';
529 while ( isascii( array[sptr] ) &&
530 isdigit( array[sptr] ) )
531 /* Don't increment inside loop control
532 * because if isdigit() is a macro it might
533 * expand into multiple increments ...
540 esc_char = otoi( array + 1 );
551 while ( isascii( array[sptr] ) &&
552 isxdigit( (char) array[sptr] ) )
553 /* Don't increment inside loop control
554 * because if isdigit() is a macro it might
555 * expand into multiple increments ...
562 esc_char = htoi( array + 2 );
575 /* otoi - convert an octal digit string to an integer value */
581 (void) sscanf( (char *) str, "%o", &result );
586 /* out - various flavors of outputing a (possibly formatted) string for the
587 * generated scanner, keeping track of the line count.
590 void out(const char *str)
592 fputs( str, stdout );
593 out_line_count( str );
596 void out_dec(const char *fmt, int n)
599 out_line_count( fmt );
602 void out_dec2(const char *fmt, int n1, int n2)
604 printf( fmt, n1, n2 );
605 out_line_count( fmt );
608 void out_hex(const char *fmt, unsigned int x)
611 out_line_count( fmt );
614 void out_line_count(const char *str)
618 for ( i = 0; str[i]; ++i )
619 if ( str[i] == '\n' )
623 void out_str(const char *fmt, const char *str)
626 out_line_count( fmt );
627 out_line_count( str );
630 void out_str3(const char *fmt, const char *s1, const char *s2, const char *s3)
632 printf( fmt, s1, s2, s3 );
633 out_line_count( fmt );
634 out_line_count( s1 );
635 out_line_count( s2 );
636 out_line_count( s3 );
639 void out_str_dec(const char *fmt, const char *str, int n)
641 printf( fmt, str, n );
642 out_line_count( fmt );
643 out_line_count( str );
654 void outn(const char *str)
657 out_line_count( str );
662 /* readable_form - return the the human-readable form of a character
664 * The returned string is in static storage.
667 char *readable_form(int c)
669 static char rform[10];
671 if ( (c >= 0 && c < 32) || c >= 127 )
675 case '\b': return "\\b";
676 case '\f': return "\\f";
677 case '\n': return "\\n";
678 case '\r': return "\\r";
679 case '\t': return "\\t";
680 case '\a': return "\\a";
681 case '\v': return "\\v";
684 (void) sprintf( rform, "\\%.3o",
703 /* reallocate_array - increase the size of a dynamic array */
705 void *reallocate_array(void *array, int size, size_t element_size)
708 size_t num_bytes = element_size * size;
710 new_array = flex_realloc( array, num_bytes );
712 flexfatal( _( "attempt to increase array size failed" ) );
718 /* skelout - write out one section of the skeleton file
721 * Copies skelfile or skel array to stdout until a line beginning with
722 * "%%" or EOF is found.
726 char buf_storage[MAXLINE];
727 char *buf = buf_storage;
730 /* Loop pulling lines either from the skelfile, if we're using
731 * one, or from the skel[] array.
734 (fgets( buf, MAXLINE, skelfile ) != NULL) :
735 ((buf = (char *) skel[skel_ind++]) != NULL) )
736 { /* copy from skel array */
745 do_copy = C_plus_plus;
749 do_copy = ! C_plus_plus;
758 _( "bad line in skeleton file" ) );
765 /* Skeleton file reads include final
766 * newline, skel[] array does not.
776 /* transition_struct_out - output a yy_trans_info structure
778 * outputs the yy_trans_info structure with the two elements, element_v and
779 * element_n. Formats the output with spaces and carriage returns.
782 void transition_struct_out(int element_v, int element_n)
784 out_dec2( " {%4d,%4d },", element_v, element_n );
786 datapos += TRANS_STRUCT_PRINT_LENGTH;
788 if ( datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH )
792 if ( ++dataline % 10 == 0 )
800 /* The following is only needed when building flex's parser using certain
801 * broken versions of bison.
803 void *yy_flex_xmalloc(int size)
805 void *result = flex_alloc( (size_t) size );
809 _( "memory allocation failed in yy_flex_xmalloc()" ) );
815 /* zero_out - set a region of memory to 0
817 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
820 void zero_out(char *region_ptr, size_t size_in_bytes )
825 rp_end = region_ptr + size_in_bytes;
827 while ( rp < rp_end )