Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / lex / misc.c
blobcea3a6aa3632bccff4c8476fb215d48787fd5eb4
1 /* misc - miscellaneous flex routines */
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Vern Paxson.
9 *
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 /* $NetBSD: misc.c,v 1.12 2003/07/14 11:36:49 itojun Exp $ */
31 #include "flexdef.h"
34 void action_define( defname, value )
35 char *defname;
36 int value;
38 char buf[MAXLINE];
40 if ( (int) strlen( defname ) > MAXLINE / 2 )
42 format_pinpoint_message( _( "name \"%s\" ridiculously long" ),
43 defname );
44 return;
47 snprintf(buf, sizeof(buf), "#define %s %d\n", defname, value);
48 add_action( buf );
52 void add_action( new_text )
53 char *new_text;
55 int len = strlen( new_text );
57 while ( len + action_index >= action_size - 10 /* slop */ )
59 int new_size = action_size * 2;
61 if ( new_size <= 0 )
62 /* Increase just a little, to try to avoid overflow
63 * on 16-bit machines.
65 action_size += action_size / 8;
66 else
67 action_size = new_size;
69 action_array =
70 reallocate_character_array( action_array, action_size );
73 strcpy(&action_array[action_index], new_text);
75 action_index += len;
79 /* allocate_array - allocate memory for an integer array of the given size */
81 void *allocate_array( size, element_size )
82 int size;
83 size_t element_size;
85 register void *mem;
86 size_t num_bytes = element_size * size;
88 mem = flex_alloc( num_bytes );
89 if ( ! mem )
90 flexfatal(
91 _( "memory allocation failed in allocate_array()" ) );
93 return mem;
97 /* all_lower - true if a string is all lower-case */
99 int all_lower( str )
100 register char *str;
102 while ( *str )
104 if ( ! isascii( (Char) *str ) || ! islower( (Char)*str ) )
105 return 0;
106 ++str;
109 return 1;
113 /* all_upper - true if a string is all upper-case */
115 int all_upper( str )
116 register char *str;
118 while ( *str )
120 if ( ! isascii( (Char) *str ) || ! isupper( (Char)*str ) )
121 return 0;
122 ++str;
125 return 1;
129 /* bubble - bubble sort an integer array in increasing order
131 * synopsis
132 * int v[n], n;
133 * void bubble( v, n );
135 * description
136 * sorts the first n elements of array v and replaces them in
137 * increasing order.
139 * passed
140 * v - the array to be sorted
141 * n - the number of elements of 'v' to be sorted
144 void bubble( v, n )
145 int v[], n;
147 register int i, j, k;
149 for ( i = n; i > 1; --i )
150 for ( j = 1; j < i; ++j )
151 if ( v[j] > v[j + 1] ) /* compare */
153 k = v[j]; /* exchange */
154 v[j] = v[j + 1];
155 v[j + 1] = k;
160 /* check_char - checks a character to make sure it's within the range
161 * we're expecting. If not, generates fatal error message
162 * and exits.
165 void check_char( c )
166 int c;
168 if ( c >= CSIZE )
169 lerrsf( _( "bad character '%s' detected in check_char()" ),
170 readable_form( c ) );
172 if ( c >= csize )
173 lerrsf(
174 _( "scanner requires -8 flag to use the character %s" ),
175 readable_form( c ) );
180 /* clower - replace upper-case letter to lower-case */
182 Char clower( c )
183 register int c;
185 return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
189 /* copy_string - returns a dynamically allocated copy of a string */
191 char *copy_string( str )
192 register const char *str;
194 register const char *c1;
195 register char *c2;
196 char *copy;
197 unsigned int size;
199 /* find length */
200 for ( c1 = str; *c1; ++c1 )
203 size = (c1 - str + 1) * sizeof( char );
204 copy = (char *) flex_alloc( size );
206 if ( copy == NULL )
207 flexfatal( _( "dynamic memory failure in copy_string()" ) );
209 for ( c2 = copy; (*c2++ = *str++) != 0; )
212 return copy;
216 /* copy_unsigned_string -
217 * returns a dynamically allocated copy of a (potentially) unsigned string
220 Char *copy_unsigned_string( str )
221 register Char *str;
223 register Char *c;
224 Char *copy;
226 /* find length */
227 for ( c = str; *c; ++c )
230 copy = allocate_Character_array( c - str + 1 );
232 for ( c = copy; (*c++ = *str++) != 0; )
235 return copy;
239 /* cshell - shell sort a character array in increasing order
241 * synopsis
243 * Char v[n];
244 * int n, special_case_0;
245 * cshell( v, n, special_case_0 );
247 * description
248 * Does a shell sort of the first n elements of array v.
249 * If special_case_0 is true, then any element equal to 0
250 * is instead assumed to have infinite weight.
252 * passed
253 * v - array to be sorted
254 * n - number of elements of v to be sorted
257 void cshell( v, n, special_case_0 )
258 Char v[];
259 int n, special_case_0;
261 int gap, i, j, jg;
262 Char k;
264 for ( gap = n / 2; gap > 0; gap = gap / 2 )
265 for ( i = gap; i < n; ++i )
266 for ( j = i - gap; j >= 0; j = j - gap )
268 jg = j + gap;
270 if ( special_case_0 )
272 if ( v[jg] == 0 )
273 break;
275 else if ( v[j] != 0 && v[j] <= v[jg] )
276 break;
279 else if ( v[j] <= v[jg] )
280 break;
282 k = v[j];
283 v[j] = v[jg];
284 v[jg] = k;
289 /* dataend - finish up a block of data declarations */
291 void dataend()
293 if ( datapos > 0 )
294 dataflush();
296 /* add terminator for initialization; { for vi */
297 outn( " } ;\n" );
299 dataline = 0;
300 datapos = 0;
304 /* dataflush - flush generated data statements */
306 void dataflush()
308 outc( '\n' );
310 if ( ++dataline >= NUMDATALINES )
312 /* Put out a blank line so that the table is grouped into
313 * large blocks that enable the user to find elements easily.
315 outc( '\n' );
316 dataline = 0;
319 /* Reset the number of characters written on the current line. */
320 datapos = 0;
324 /* flexerror - report an error message and terminate */
326 void flexerror( msg )
327 const char msg[];
329 fprintf( stderr, "%s: %s\n", program_name, msg );
330 flexend( 1 );
334 /* flexfatal - report a fatal error message and terminate */
336 void flexfatal( msg )
337 const char msg[];
339 fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
340 program_name, msg );
341 exit( 1 );
345 /* htoi - convert a hexadecimal digit string to an integer value */
347 int htoi( str )
348 Char str[];
350 unsigned int result;
352 (void) sscanf( (char *) str, "%x", &result );
354 return result;
358 /* lerrif - report an error message formatted with one integer argument */
360 void lerrif( msg, arg )
361 const char msg[];
362 int arg;
364 char errmsg[MAXLINE];
365 (void) snprintf(errmsg, sizeof(errmsg), msg, arg);
366 flexerror( errmsg );
370 /* lerrsf - report an error message formatted with one string argument */
372 void lerrsf( msg, arg )
373 const char msg[], arg[];
375 char errmsg[MAXLINE];
377 (void) snprintf(errmsg, sizeof(errmsg), msg, arg);
378 flexerror( errmsg );
382 /* line_directive_out - spit out a "#line" statement */
384 void line_directive_out( output_file, do_infile )
385 FILE *output_file;
386 int do_infile;
388 char directive[MAXLINE], filename[MAXLINE];
389 char *s1, *s2, *s3;
390 static const char line_fmt[] = "#line %d \"%s\"\n";
392 if ( ! gen_line_dirs )
393 return;
395 if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
396 /* don't know the filename to use, skip */
397 return;
399 s1 = do_infile ? infilename : outfilename;
400 s2 = filename;
401 s3 = &filename[sizeof( filename ) - 2];
403 while ( s2 < s3 && *s1 )
405 if ( *s1 == '\\' )
406 /* Escape the '\' */
407 *s2++ = '\\';
409 *s2++ = *s1++;
412 *s2 = '\0';
414 if ( do_infile )
415 snprintf(directive, sizeof(directive), line_fmt, linenum,
416 filename);
417 else
419 if ( output_file == stdout )
420 /* Account for the line directive itself. */
421 ++out_linenum;
423 snprintf(directive, sizeof(directive), line_fmt, out_linenum,
424 filename);
427 /* If output_file is nil then we should put the directive in
428 * the accumulated actions.
430 if ( output_file )
432 fputs( directive, output_file );
434 else
435 add_action( directive );
439 /* mark_defs1 - mark the current position in the action array as
440 * representing where the user's section 1 definitions end
441 * and the prolog begins
443 void mark_defs1()
445 defs1_offset = 0;
446 action_array[action_index++] = '\0';
447 action_offset = prolog_offset = action_index;
448 action_array[action_index] = '\0';
452 /* mark_prolog - mark the current position in the action array as
453 * representing the end of the action prolog
455 void mark_prolog()
457 action_array[action_index++] = '\0';
458 action_offset = action_index;
459 action_array[action_index] = '\0';
463 /* mk2data - generate a data statement for a two-dimensional array
465 * Generates a data statement initializing the current 2-D array to "value".
467 void mk2data( value )
468 int value;
470 if ( datapos >= NUMDATAITEMS )
472 outc( ',' );
473 dataflush();
476 if ( datapos == 0 )
477 /* Indent. */
478 out( " " );
480 else
481 outc( ',' );
483 ++datapos;
485 out_dec( "%5d", value );
489 /* mkdata - generate a data statement
491 * Generates a data statement initializing the current array element to
492 * "value".
494 void mkdata( value )
495 int value;
497 if ( datapos >= NUMDATAITEMS )
499 outc( ',' );
500 dataflush();
503 if ( datapos == 0 )
504 /* Indent. */
505 out( " " );
506 else
507 outc( ',' );
509 ++datapos;
511 out_dec( "%5d", value );
515 /* myctoi - return the integer represented by a string of digits */
517 int myctoi( array )
518 char array[];
520 int val = 0;
522 (void) sscanf( array, "%d", &val );
524 return val;
528 /* myesc - return character corresponding to escape sequence */
530 Char myesc( array )
531 Char array[];
533 Char c, esc_char;
535 switch ( array[1] )
537 case 'b': return '\b';
538 case 'f': return '\f';
539 case 'n': return '\n';
540 case 'r': return '\r';
541 case 't': return '\t';
543 #if __STDC__
544 case 'a': return '\a';
545 case 'v': return '\v';
546 #else
547 case 'a': return '\007';
548 case 'v': return '\013';
549 #endif
551 case '0':
552 case '1':
553 case '2':
554 case '3':
555 case '4':
556 case '5':
557 case '6':
558 case '7':
559 { /* \<octal> */
560 int sptr = 1;
562 while ( isascii( array[sptr] ) &&
563 isdigit( array[sptr] ) )
564 /* Don't increment inside loop control
565 * because if isdigit() is a macro it might
566 * expand into multiple increments ...
568 ++sptr;
570 c = array[sptr];
571 array[sptr] = '\0';
573 esc_char = otoi( array + 1 );
575 array[sptr] = c;
577 return esc_char;
580 case 'x':
581 { /* \x<hex> */
582 int sptr = 2;
584 while ( isascii( array[sptr] ) &&
585 isxdigit( (Char) array[sptr] ) )
586 /* Don't increment inside loop control
587 * because if isdigit() is a macro it might
588 * expand into multiple increments ...
590 ++sptr;
592 c = array[sptr];
593 array[sptr] = '\0';
595 esc_char = htoi( array + 2 );
597 array[sptr] = c;
599 return esc_char;
602 default:
603 return array[1];
608 /* otoi - convert an octal digit string to an integer value */
610 int otoi( str )
611 Char str[];
613 unsigned int result;
615 (void) sscanf( (char *) str, "%o", &result );
616 return result;
620 /* out - various flavors of outputing a (possibly formatted) string for the
621 * generated scanner, keeping track of the line count.
624 void out( str )
625 const char str[];
627 fputs( str, stdout );
628 out_line_count( str );
631 void out_dec( fmt, n )
632 const char fmt[];
633 int n;
635 printf( fmt, n );
636 out_line_count( fmt );
639 void out_dec2( fmt, n1, n2 )
640 const char fmt[];
641 int n1, n2;
643 printf( fmt, n1, n2 );
644 out_line_count( fmt );
647 void out_hex( fmt, x )
648 const char fmt[];
649 unsigned int x;
651 printf( fmt, x );
652 out_line_count( fmt );
655 void out_line_count( str )
656 const char str[];
658 register int i;
660 for ( i = 0; str[i]; ++i )
661 if ( str[i] == '\n' )
662 ++out_linenum;
665 void out_str( fmt, str )
666 const char fmt[], str[];
668 printf( fmt, str );
669 out_line_count( fmt );
670 out_line_count( str );
673 void out_str3( fmt, s1, s2, s3 )
674 const char fmt[], s1[], s2[], s3[];
676 printf( fmt, s1, s2, s3 );
677 out_line_count( fmt );
678 out_line_count( s1 );
679 out_line_count( s2 );
680 out_line_count( s3 );
683 void out_str_dec( fmt, str, n )
684 const char fmt[], str[];
685 int n;
687 printf( fmt, str, n );
688 out_line_count( fmt );
689 out_line_count( str );
692 void outc( c )
693 int c;
695 putc( c, stdout );
697 if ( c == '\n' )
698 ++out_linenum;
701 void outn( str )
702 const char str[];
704 puts( str );
705 out_line_count( str );
706 ++out_linenum;
710 /* readable_form - return the human-readable form of a character
712 * The returned string is in static storage.
715 char *readable_form( c )
716 register int c;
718 static char rform[10];
720 if ( (c >= 0 && c < 32) || c >= 127 )
722 switch ( c )
724 case '\b': return "\\b";
725 case '\f': return "\\f";
726 case '\n': return "\\n";
727 case '\r': return "\\r";
728 case '\t': return "\\t";
730 #if __STDC__
731 case '\a': return "\\a";
732 case '\v': return "\\v";
733 #endif
735 default:
736 (void) snprintf(rform, sizeof(rform), "\\%.3o",
737 (unsigned int) c );
738 return rform;
742 else if ( c == ' ' )
743 return "' '";
745 else
747 rform[0] = c;
748 rform[1] = '\0';
750 return rform;
755 /* reallocate_array - increase the size of a dynamic array */
757 void *reallocate_array( array, size, element_size )
758 void *array;
759 int size;
760 size_t element_size;
762 register void *new_array;
763 size_t num_bytes = element_size * size;
765 new_array = flex_realloc( array, num_bytes );
766 if ( ! new_array )
767 flexfatal( _( "attempt to increase array size failed" ) );
769 return new_array;
773 /* skelout - write out one section of the skeleton file
775 * Description
776 * Copies skelfile or skel array to stdout until a line beginning with
777 * "%%" or EOF is found.
779 void skelout()
781 char buf_storage[MAXLINE];
782 char *buf = buf_storage;
783 int do_copy = 1;
785 /* Loop pulling lines either from the skelfile, if we're using
786 * one, or from the skel[] array.
788 while ( skelfile ?
789 (fgets( buf, MAXLINE, skelfile ) != NULL) :
790 ((buf = (char *) skel[skel_ind++]) != 0) )
791 { /* copy from skel array */
792 if ( buf[0] == '%' )
793 { /* control line */
794 switch ( buf[1] )
796 case '%':
797 return;
799 case '+':
800 do_copy = C_plus_plus;
801 break;
803 case '-':
804 do_copy = ! C_plus_plus;
805 break;
807 case '*':
808 do_copy = 1;
809 break;
811 default:
812 flexfatal(
813 _( "bad line in skeleton file" ) );
817 else if ( do_copy )
819 if ( skelfile )
820 /* Skeleton file reads include final
821 * newline, skel[] array does not.
823 out( buf );
824 else
825 outn( buf );
831 /* transition_struct_out - output a yy_trans_info structure
833 * outputs the yy_trans_info structure with the two elements, element_v and
834 * element_n. Formats the output with spaces and carriage returns.
837 void transition_struct_out( element_v, element_n )
838 int element_v, element_n;
840 out_dec2( " {%4d,%4d },", element_v, element_n );
842 datapos += TRANS_STRUCT_PRINT_LENGTH;
844 if ( datapos >= 79 - TRANS_STRUCT_PRINT_LENGTH )
846 outc( '\n' );
848 if ( ++dataline % 10 == 0 )
849 outc( '\n' );
851 datapos = 0;
856 /* The following is only needed when building flex's parser using certain
857 * broken versions of bison.
859 void *yy_flex_xmalloc( size )
860 int size;
862 void *result = flex_alloc( (size_t) size );
864 if ( ! result )
865 flexfatal(
866 _( "memory allocation failed in yy_flex_xmalloc()" ) );
868 return result;
872 /* zero_out - set a region of memory to 0
874 * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
877 void zero_out( region_ptr, size_in_bytes )
878 char *region_ptr;
879 size_t size_in_bytes;
881 register char *rp, *rp_end;
883 rp = region_ptr;
884 rp_end = region_ptr + size_in_bytes;
886 while ( rp < rp_end )
887 *rp++ = 0;