Add two tunables to run netisr and udp_thread without mplock, so experiment
[dfdiff.git] / usr.bin / lex / misc.c
blobb13a198430e3a5d926284978a1ba4c8298b332fc
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 /* $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 $ */
33 #include "flexdef.h"
36 void action_define(char *defname, 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 sprintf( buf, "#define %s %d\n", defname, value );
48 add_action( buf );
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;
60 if ( new_size <= 0 )
61 /* Increase just a little, to try to avoid overflow
62 * on 16-bit machines.
64 action_size += action_size / 8;
65 else
66 action_size = new_size;
68 action_array =
69 reallocate_character_array( action_array, action_size );
72 strcpy( &action_array[action_index], new_text );
74 action_index += len;
78 /* allocate_array - allocate memory for an integer array of the given size */
80 void *allocate_array(int size, size_t element_size)
82 void *mem;
83 size_t num_bytes = element_size * size;
85 mem = flex_alloc( num_bytes );
86 if ( ! mem )
87 flexfatal(
88 _( "memory allocation failed in allocate_array()" ) );
90 return mem;
94 /* all_lower - true if a string is all lower-case */
96 int all_lower(char *str)
98 while ( *str )
100 if ( ! isascii( (Char) *str ) || ! islower( *str ) )
101 return 0;
102 ++str;
105 return 1;
109 /* all_upper - true if a string is all upper-case */
111 int all_upper(char *str)
113 while ( *str )
115 if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
116 return 0;
117 ++str;
120 return 1;
124 /* bubble - bubble sort an integer array in increasing order
126 * synopsis
127 * int v[n], n;
128 * void bubble( v, n );
130 * description
131 * sorts the first n elements of array v and replaces them in
132 * increasing order.
134 * passed
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)
141 int i, j, k;
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 */
148 v[j] = v[j + 1];
149 v[j + 1] = k;
154 /* check_char - checks a character to make sure it's within the range
155 * we're expecting. If not, generates fatal error message
156 * and exits.
159 void check_char(int c)
161 if ( c >= CSIZE )
162 lerrsf( _( "bad character '%s' detected in check_char()" ),
163 readable_form( c ) );
165 if ( c >= csize )
166 lerrsf(
167 _( "scanner requires -8 flag to use the character %s" ),
168 readable_form( c ) );
173 /* clower - replace upper-case letter to lower-case */
175 Char clower(int c)
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)
185 const char *c1;
186 char *c2;
187 char *copy;
188 unsigned int size;
190 /* find length */
191 for ( c1 = str; *c1; ++c1 )
194 size = (c1 - str + 1) * sizeof( char );
195 copy = (char *) flex_alloc( size );
197 if ( copy == NULL )
198 flexfatal( _( "dynamic memory failure in copy_string()" ) );
200 for ( c2 = copy; (*c2++ = *str++) != 0; )
203 return copy;
207 /* copy_unsigned_string -
208 * returns a dynamically allocated copy of a (potentially) unsigned string
211 Char *copy_unsigned_string(Char *str)
213 Char *c;
214 Char *copy;
216 /* find length */
217 for ( c = str; *c; ++c )
220 copy = allocate_Character_array( c - str + 1 );
222 for ( c = copy; (*c++ = *str++) != 0; )
225 return copy;
229 /* cshell - shell sort a character array in increasing order
231 * synopsis
233 * Char v[n];
234 * int n, special_case_0;
235 * cshell( v, n, special_case_0 );
237 * description
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.
242 * passed
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)
249 int gap, i, j, jg;
250 Char k;
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 )
256 jg = j + gap;
258 if ( special_case_0 )
260 if ( v[jg] == 0 )
261 break;
263 else if ( v[j] != 0 && v[j] <= v[jg] )
264 break;
267 else if ( v[j] <= v[jg] )
268 break;
270 k = v[j];
271 v[j] = v[jg];
272 v[jg] = k;
277 /* dataend - finish up a block of data declarations */
279 void dataend(void)
281 if ( datapos > 0 )
282 dataflush();
284 /* add terminator for initialization; { for vi */
285 outn( " } ;\n" );
287 dataline = 0;
288 datapos = 0;
292 /* dataflush - flush generated data statements */
294 void dataflush(void)
296 outc( '\n' );
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.
303 outc( '\n' );
304 dataline = 0;
307 /* Reset the number of characters written on the current line. */
308 datapos = 0;
312 /* flexerror - report an error message and terminate */
314 void flexerror(const char *msg)
316 fprintf( stderr, "%s: %s\n", program_name, msg );
317 flexend( 1 );
321 /* flexfatal - report a fatal error message and terminate */
323 void flexfatal(const char *msg)
325 fprintf( stderr, _( "%s: fatal internal error, %s\n" ),
326 program_name, msg );
327 exit( 1 );
331 /* htoi - convert a hexadecimal digit string to an integer value */
333 int htoi(Char *str)
335 unsigned int result;
337 (void) sscanf( (char *) str, "%x", &result );
339 return 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 );
349 flexerror( errmsg );
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 );
360 flexerror( errmsg );
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];
369 char *s1, *s2, *s3;
370 static char line_fmt[] = "#line %d \"%s\"\n";
372 if ( ! gen_line_dirs )
373 return;
375 if ( (do_infile && ! infilename) || (! do_infile && ! outfilename) )
376 /* don't know the filename to use, skip */
377 return;
379 s1 = do_infile ? infilename : outfilename;
380 s2 = filename;
381 s3 = &filename[sizeof( filename ) - 2];
383 while ( s2 < s3 && *s1 )
385 if ( *s1 == '\\' )
386 /* Escape the '\' */
387 *s2++ = '\\';
389 *s2++ = *s1++;
392 *s2 = '\0';
394 if ( do_infile )
395 sprintf( directive, line_fmt, linenum, filename );
396 else
398 if ( output_file == stdout )
399 /* Account for the line directive itself. */
400 ++out_linenum;
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.
408 if ( output_file )
410 fputs( directive, output_file );
412 else
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)
423 defs1_offset = 0;
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 )
449 outc( ',' );
450 dataflush();
453 if ( datapos == 0 )
454 /* Indent. */
455 out( " " );
457 else
458 outc( ',' );
460 ++datapos;
462 out_dec( "%5d", value );
466 /* mkdata - generate a data statement
468 * Generates a data statement initializing the current array element to
469 * "value".
471 void mkdata(int value)
473 if ( datapos >= NUMDATAITEMS )
475 outc( ',' );
476 dataflush();
479 if ( datapos == 0 )
480 /* Indent. */
481 out( " " );
482 else
483 outc( ',' );
485 ++datapos;
487 out_dec( "%5d", value );
491 /* myctoi - return the integer represented by a string of digits */
493 int myctoi(char *array)
495 int val = 0;
497 (void) sscanf( array, "%d", &val );
499 return val;
503 /* myesc - return character corresponding to escape sequence */
505 Char myesc(Char *array)
507 Char c, esc_char;
509 switch ( array[1] )
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';
517 #if __STDC__
518 case 'a': return '\a';
519 case 'v': return '\v';
520 #else
521 case 'a': return '\007';
522 case 'v': return '\013';
523 #endif
525 case '0':
526 case '1':
527 case '2':
528 case '3':
529 case '4':
530 case '5':
531 case '6':
532 case '7':
533 { /* \<octal> */
534 int sptr = 1;
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 ...
542 ++sptr;
544 c = array[sptr];
545 array[sptr] = '\0';
547 esc_char = otoi( array + 1 );
549 array[sptr] = c;
551 return esc_char;
554 case 'x':
555 { /* \x<hex> */
556 int sptr = 2;
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 ...
564 ++sptr;
566 c = array[sptr];
567 array[sptr] = '\0';
569 esc_char = htoi( array + 2 );
571 array[sptr] = c;
573 return esc_char;
576 default:
577 return array[1];
582 /* otoi - convert an octal digit string to an integer value */
584 int otoi(Char *str)
586 unsigned int result;
588 (void) sscanf( (char *) str, "%o", &result );
589 return 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)
605 printf( fmt, 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)
617 printf( fmt, x );
618 out_line_count( fmt );
621 void out_line_count(const char *str)
623 int i;
625 for ( i = 0; str[i]; ++i )
626 if ( str[i] == '\n' )
627 ++out_linenum;
630 void out_str(const char *fmt, const char *str)
632 printf( fmt, 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 );
653 void outc(int c)
655 putc( c, stdout );
657 if ( c == '\n' )
658 ++out_linenum;
661 void outn(const char *str)
663 puts( str );
664 out_line_count( str );
665 ++out_linenum;
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 )
680 switch ( c )
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";
688 #if __STDC__
689 case '\a': return "\\a";
690 case '\v': return "\\v";
691 #endif
693 default:
694 (void) sprintf( rform, "\\%.3o",
695 (unsigned int) c );
696 return rform;
700 else if ( c == ' ' )
701 return "' '";
703 else
705 rform[0] = c;
706 rform[1] = '\0';
708 return rform;
713 /* reallocate_array - increase the size of a dynamic array */
715 void *reallocate_array(void *array, int size, size_t element_size)
717 void *new_array;
718 size_t num_bytes = element_size * size;
720 new_array = flex_realloc( array, num_bytes );
721 if ( ! new_array )
722 flexfatal( _( "attempt to increase array size failed" ) );
724 return new_array;
728 /* skelout - write out one section of the skeleton file
730 * Description
731 * Copies skelfile or skel array to stdout until a line beginning with
732 * "%%" or EOF is found.
734 void skelout(void)
736 char buf_storage[MAXLINE];
737 char *buf = buf_storage;
738 int do_copy = 1;
740 /* Loop pulling lines either from the skelfile, if we're using
741 * one, or from the skel[] array.
743 while ( skelfile ?
744 (fgets( buf, MAXLINE, skelfile ) != NULL) :
745 ((buf = (char *) skel[skel_ind++]) != 0) )
746 { /* copy from skel array */
747 if ( buf[0] == '%' )
748 { /* control line */
749 switch ( buf[1] )
751 case '%':
752 return;
754 case '+':
755 do_copy = C_plus_plus;
756 break;
758 case '-':
759 do_copy = ! C_plus_plus;
760 break;
762 case '*':
763 do_copy = 1;
764 break;
766 default:
767 flexfatal(
768 _( "bad line in skeleton file" ) );
772 else if ( do_copy )
774 if ( skelfile )
775 /* Skeleton file reads include final
776 * newline, skel[] array does not.
778 out( buf );
779 else
780 outn( buf );
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 )
800 outc( '\n' );
802 if ( ++dataline % 10 == 0 )
803 outc( '\n' );
805 datapos = 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 );
817 if ( ! result )
818 flexfatal(
819 _( "memory allocation failed in yy_flex_xmalloc()" ) );
821 return result;
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 )
832 char *rp, *rp_end;
834 rp = region_ptr;
835 rp_end = region_ptr + size_in_bytes;
837 while ( rp < rp_end )
838 *rp++ = 0;