Pristine Ack-5.5
[Ack-5.5.git] / util / flex / misc.c
1 /* misc - miscellaneous flex routines */
2
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
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.
13  *
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.
27  */
28
29 #ifndef lint
30 static char rcsid[] =
31     "@(#) $Id: misc.c,v 1.4 1994/06/24 10:57:12 ceriel Exp $ (LBL)";
32 #endif
33
34 #include <ctype.h>
35 #include "flexdef.h"
36
37
38 /* ANSI C does not guarantee that isascii() is defined */
39 #ifndef isascii
40 #define isascii(c) ((c) <= 0177)
41 #endif
42
43
44
45 /* declare functions that have forward references */
46
47 void dataflush PROTO(());
48 int otoi PROTO((Char []));
49
50
51 /* action_out - write the actions from the temporary file to lex.yy.c
52  *
53  * synopsis
54  *     action_out();
55  *
56  *     Copies the action file up to %% (or end-of-file) to lex.yy.c
57  */
58
59 void action_out()
60
61     {
62     char buf[MAXLINE];
63
64     while ( fgets( buf, MAXLINE, temp_action_file ) != NULL )
65         if ( buf[0] == '%' && buf[1] == '%' )
66             break;
67         else
68             fputs( buf, stdout );
69     }
70
71
72 /* allocate_array - allocate memory for an integer array of the given size */
73
74 void *allocate_array( size, element_size )
75 int size, element_size;
76
77     {
78     register void *mem;
79
80     /* on 16-bit int machines (e.g., 80286) we might be trying to
81      * allocate more than a signed int can hold, and that won't
82      * work.  Cheap test:
83      */
84     if ( element_size * size <= 0 )
85         flexfatal( "request for < 1 byte in allocate_array()" );
86
87     mem = (void *) malloc( (unsigned) (element_size * size) );
88
89     if ( mem == NULL )
90         flexfatal( "memory allocation failed in allocate_array()" );
91
92     return ( mem );
93     }
94
95
96 /* all_lower - true if a string is all lower-case
97  *
98  * synopsis:
99  *    Char *str;
100  *    int all_lower();
101  *    true/false = all_lower( str );
102  */
103
104 int all_lower( str )
105 register Char *str;
106
107     {
108     while ( *str )
109         {
110         if ( ! isascii( *str ) || ! islower( *str ) )
111             return ( 0 );
112         ++str;
113         }
114
115     return ( 1 );
116     }
117
118
119 /* all_upper - true if a string is all upper-case
120  *
121  * synopsis:
122  *    Char *str;
123  *    int all_upper();
124  *    true/false = all_upper( str );
125  */
126
127 int all_upper( str )
128 register Char *str;
129
130     {
131     while ( *str )
132         {
133         if ( ! isascii( *str ) || ! isupper( (char) *str ) )
134             return ( 0 );
135         ++str;
136         }
137
138     return ( 1 );
139     }
140
141
142 /* bubble - bubble sort an integer array in increasing order
143  *
144  * synopsis
145  *   int v[n], n;
146  *   bubble( v, n );
147  *
148  * description
149  *   sorts the first n elements of array v and replaces them in
150  *   increasing order.
151  *
152  * passed
153  *   v - the array to be sorted
154  *   n - the number of elements of 'v' to be sorted */
155
156 void bubble( v, n )
157 int v[], n;
158
159     {
160     register int i, j, k;
161
162     for ( i = n; i > 1; --i )
163         for ( j = 1; j < i; ++j )
164             if ( v[j] > v[j + 1] )      /* compare */
165                 {
166                 k = v[j];       /* exchange */
167                 v[j] = v[j + 1];
168                 v[j + 1] = k;
169                 }
170     }
171
172
173 /* clower - replace upper-case letter to lower-case
174  *
175  * synopsis:
176  *    Char clower();
177  *    int c;
178  *    c = clower( c );
179  */
180
181 Char clower( c )
182 register int c;
183
184     {
185     return ( (isascii( c ) && isupper( c )) ? tolower( c ) : c );
186     }
187
188
189 /* copy_string - returns a dynamically allocated copy of a string
190  *
191  * synopsis
192  *    char *str, *copy, *copy_string();
193  *    copy = copy_string( str );
194  */
195
196 char *copy_string( str )
197 register char *str;
198
199     {
200     register char *c;
201     char *copy;
202
203     /* find length */
204     for ( c = str; *c; ++c )
205         ;
206
207     copy = malloc( (unsigned) ((c - str + 1) * sizeof( char )) );
208
209     if ( copy == NULL )
210         flexfatal( "dynamic memory failure in copy_string()" );
211
212     for ( c = copy; (*c++ = *str++); )
213         ;
214
215     return ( copy );
216     }
217
218
219 /* copy_unsigned_string -
220  *    returns a dynamically allocated copy of a (potentially) unsigned string
221  *
222  * synopsis
223  *    Char *str, *copy, *copy_unsigned_string();
224  *    copy = copy_unsigned_string( str );
225  */
226
227 Char *copy_unsigned_string( str )
228 register Char *str;
229
230     {
231     register Char *c;
232     Char *copy;
233
234     /* find length */
235     for ( c = str; *c; ++c )
236         ;
237
238     copy = (Char *) malloc( (unsigned) ((c - str + 1) * sizeof( Char )) );
239
240     if ( copy == NULL )
241         flexfatal( "dynamic memory failure in copy_unsigned_string()" );
242
243     for ( c = copy; (*c++ = *str++); )
244         ;
245
246     return ( copy );
247     }
248
249
250 /* cshell - shell sort a character array in increasing order
251  *
252  * synopsis
253  *
254  *   Char v[n];
255  *   int n, special_case_0;
256  *   cshell( v, n, special_case_0 );
257  *
258  * description
259  *   does a shell sort of the first n elements of array v.
260  *   If special_case_0 is true, then any element equal to 0
261  *   is instead assumed to have infinite weight.
262  *
263  * passed
264  *   v - array to be sorted
265  *   n - number of elements of v to be sorted
266  */
267
268 void cshell( v, n, special_case_0 )
269 Char v[];
270 int n, special_case_0;
271
272     {
273     int gap, i, j, jg;
274     Char k;
275
276     for ( gap = n / 2; gap > 0; gap = gap / 2 )
277         for ( i = gap; i < n; ++i )
278             for ( j = i - gap; j >= 0; j = j - gap )
279                 {
280                 jg = j + gap;
281
282                 if ( special_case_0 )
283                     {
284                     if ( v[jg] == 0 )
285                         break;
286
287                     else if ( v[j] != 0 && v[j] <= v[jg] )
288                         break;
289                     }
290
291                 else if ( v[j] <= v[jg] )
292                     break;
293
294                 k = v[j];
295                 v[j] = v[jg];
296                 v[jg] = k;
297                 }
298     }
299
300
301 /* dataend - finish up a block of data declarations
302  *
303  * synopsis
304  *    dataend();
305  */
306
307 void dataend()
308
309     {
310     if ( datapos > 0 )
311         dataflush();
312
313     /* add terminator for initialization */
314     puts( "    } ;\n" );
315
316     dataline = 0;
317     datapos = 0;
318     }
319
320
321
322 /* dataflush - flush generated data statements
323  *
324  * synopsis
325  *    dataflush();
326  */
327
328 void dataflush()
329
330     {
331     putchar( '\n' );
332
333     if ( ++dataline >= NUMDATALINES )
334         {
335         /* put out a blank line so that the table is grouped into
336          * large blocks that enable the user to find elements easily
337          */
338         putchar( '\n' );
339         dataline = 0;
340         }
341
342     /* reset the number of characters written on the current line */
343     datapos = 0;
344     }
345
346
347 /* flexerror - report an error message and terminate
348  *
349  * synopsis
350  *    char msg[];
351  *    flexerror( msg );
352  */
353
354 void flexerror( msg )
355 char msg[];
356
357     {
358     fprintf( stderr, "%s: %s\n", program_name, msg );
359
360     flexend( 1 );
361     }
362
363
364 /* flexfatal - report a fatal error message and terminate
365  *
366  * synopsis
367  *    char msg[];
368  *    flexfatal( msg );
369  */
370
371 void flexfatal( msg )
372 char msg[];
373
374     {
375     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
376     flexend( 1 );
377     }
378
379
380 /* flex_gettime - return current time
381  *
382  * synopsis
383  *    char *flex_gettime(), *time_str;
384  *    time_str = flex_gettime();
385  *
386  * note
387  *    the routine name has the "flex_" prefix because of name clashes
388  *    with Turbo-C
389  */
390
391 /* include sys/types.h to use time_t and make lint happy */
392
393 #ifndef MS_DOS
394 #ifndef VMS
395 #include <sys/types.h>
396 #else
397 #include <types.h>
398 #endif
399 #endif
400
401 #ifdef MS_DOS
402 #include <time.h>
403 typedef long time_t;
404 #endif
405
406 char *flex_gettime()
407
408     {
409     time_t t, time();
410     char *result, *ctime(), *copy_string();
411
412     t = time( (long *) 0 );
413
414     result = copy_string( ctime( &t ) );
415
416     /* get rid of trailing newline */
417     result[24] = '\0';
418
419     return ( result );
420     }
421
422
423 /* lerrif - report an error message formatted with one integer argument
424  *
425  * synopsis
426  *    char msg[];
427  *    int arg;
428  *    lerrif( msg, arg );
429  */
430
431 void lerrif( msg, arg )
432 char msg[];
433 int arg;
434
435     {
436     char errmsg[MAXLINE];
437     (void) sprintf( errmsg, msg, arg );
438     flexerror( errmsg );
439     }
440
441
442 /* lerrsf - report an error message formatted with one string argument
443  *
444  * synopsis
445  *    char msg[], arg[];
446  *    lerrsf( msg, arg );
447  */
448
449 void lerrsf( msg, arg )
450 char msg[], arg[];
451
452     {
453     char errmsg[MAXLINE];
454
455     (void) sprintf( errmsg, msg, arg );
456     flexerror( errmsg );
457     }
458
459
460 /* htoi - convert a hexadecimal digit string to an integer value
461  *
462  * synopsis:
463  *    int val, htoi();
464  *    Char str[];
465  *    val = htoi( str );
466  */
467
468 int htoi( str )
469 Char str[];
470
471     {
472     int result;
473
474     (void) sscanf( (char *) str, "%x", &result );
475
476     return ( result );
477     }
478
479
480 /* is_hex_digit - returns true if a character is a valid hex digit, false
481  *                otherwise
482  *
483  * synopsis:
484  *    int true_or_false, is_hex_digit();
485  *    int ch;
486  *    val = is_hex_digit( ch );
487  */
488
489 int is_hex_digit( ch )
490 int ch;
491
492     {
493     if ( isdigit( ch ) )
494         return ( 1 );
495
496     switch ( clower( ch ) )
497         {
498         case 'a':
499         case 'b':
500         case 'c':
501         case 'd':
502         case 'e':
503         case 'f':
504             return ( 1 );
505
506         default:
507             return ( 0 );
508         }
509     }
510
511
512 /* line_directive_out - spit out a "# line" statement */
513
514 void line_directive_out( output_file_name )
515 FILE *output_file_name;
516
517     {
518     if ( infilename && gen_line_dirs )
519         fprintf( output_file_name, "# line %d \"%s\"\n", linenum, infilename );
520     }
521
522
523 /* mk2data - generate a data statement for a two-dimensional array
524  *
525  * synopsis
526  *    int value;
527  *    mk2data( value );
528  *
529  *  generates a data statement initializing the current 2-D array to "value"
530  */
531 void mk2data( value )
532 int value;
533
534     {
535     if ( datapos >= NUMDATAITEMS )
536         {
537         putchar( ',' );
538         dataflush();
539         }
540
541     if ( datapos == 0 )
542         /* indent */
543         fputs( "    ", stdout );
544
545     else
546         putchar( ',' );
547
548     ++datapos;
549
550     printf( "%5d", value );
551     }
552
553
554 /* mkdata - generate a data statement
555  *
556  * synopsis
557  *    int value;
558  *    mkdata( value );
559  *
560  *  generates a data statement initializing the current array element to
561  *  "value"
562  */
563 void mkdata( value )
564 int value;
565
566     {
567     if ( datapos >= NUMDATAITEMS )
568         {
569         putchar( ',' );
570         dataflush();
571         }
572
573     if ( datapos == 0 )
574         /* indent */
575         fputs( "    ", stdout );
576
577     else
578         putchar( ',' );
579
580     ++datapos;
581
582     printf( "%5d", value );
583     }
584
585
586 /* myctoi - return the integer represented by a string of digits
587  *
588  * synopsis
589  *    Char array[];
590  *    int val, myctoi();
591  *    val = myctoi( array );
592  *
593  */
594
595 int myctoi( array )
596 Char array[];
597
598     {
599     int val = 0;
600
601     (void) sscanf( (char *) array, "%d", &val );
602
603     return ( val );
604     }
605
606
607 /* myesc - return character corresponding to escape sequence
608  *
609  * synopsis
610  *    Char array[], c, myesc();
611  *    c = myesc( array );
612  *
613  */
614
615 Char myesc( array )
616 Char array[];
617
618     {
619     Char c, esc_char;
620     register int sptr;
621
622     switch ( array[1] )
623         {
624 #ifdef ACK_MOD
625 #if __STDC__
626         case 'a': return ( '\a' );
627         case 'v': return ( '\v' );
628 #else
629         case 'a': return ( '\007' );
630         case 'v': return ( '\013' );
631 #endif
632 #else
633         case 'a': return ( '\a' );
634         case 'v': return ( '\v' );
635 #endif
636         case 'b': return ( '\b' );
637         case 'f': return ( '\f' );
638         case 'n': return ( '\n' );
639         case 'r': return ( '\r' );
640         case 't': return ( '\t' );
641
642         case '0':
643         case '1':
644         case '2':
645         case '3':
646         case '4':
647         case '5':
648         case '6':
649         case '7':
650         case '8':
651         case '9':
652             { /* \<octal> */
653             sptr = 1;
654
655             while ( isascii( array[sptr] ) && isdigit( array[sptr] ) )
656                 /* don't increment inside loop control because if
657                  * isdigit() is a macro it might expand into multiple
658                  * increments ...
659                  */
660                 ++sptr;
661
662             c = array[sptr];
663             array[sptr] = '\0';
664
665             esc_char = otoi( array + 1 );
666
667             array[sptr] = c;
668
669             return ( esc_char );
670             }
671
672         case 'x':
673             { /* \x<hex> */
674             int sptr = 2;
675
676             while ( isascii( array[sptr] ) && is_hex_digit( array[sptr] ) )
677                 /* don't increment inside loop control because if
678                  * isdigit() is a macro it might expand into multiple
679                  * increments ...
680                  */
681                 ++sptr;
682
683             c = array[sptr];
684             array[sptr] = '\0';
685
686             esc_char = htoi( array + 2 );
687
688             array[sptr] = c;
689
690             return ( esc_char );
691             }
692
693         default:
694             return ( array[1] );
695         }
696     }
697
698
699 /* otoi - convert an octal digit string to an integer value
700  *
701  * synopsis:
702  *    int val, otoi();
703  *    Char str[];
704  *    val = otoi( str );
705  */
706
707 int otoi( str )
708 Char str[];
709
710     {
711     int result;
712
713     (void) sscanf( (char *) str, "%o", &result );
714
715     return ( result );
716     }
717
718
719 /* readable_form - return the the human-readable form of a character
720  *
721  * synopsis:
722  *    int c;
723  *    char *readable_form();
724  *    <string> = readable_form( c );
725  *
726  * The returned string is in static storage.
727  */
728
729 char *readable_form( c )
730 register int c;
731
732     {
733     static char rform[10];
734
735     if ( (c >= 0 && c < 32) || c >= 127 )
736         {
737         switch ( c )
738             {
739             case '\n': return ( "\\n" );
740             case '\t': return ( "\\t" );
741             case '\f': return ( "\\f" );
742             case '\r': return ( "\\r" );
743             case '\b': return ( "\\b" );
744
745             default:
746                 (void) sprintf( rform, "\\%.3o", c );
747                 return ( rform );
748             }
749         }
750
751     else if ( c == ' ' )
752         return ( "' '" );
753
754     else
755         {
756         rform[0] = c;
757         rform[1] = '\0';
758
759         return ( rform );
760         }
761     }
762
763
764 /* reallocate_array - increase the size of a dynamic array */
765
766 void *reallocate_array( array, size, element_size )
767 void *array;
768 int size, element_size;
769
770     {
771     register void *new_array;
772
773     /* same worry as in allocate_array(): */
774     if ( size * element_size <= 0 )
775         flexfatal( "attempt to increase array size by less than 1 byte" );
776
777     new_array =
778         (void *) realloc( (char *)array, (unsigned) (size * element_size ));
779
780     if ( new_array == NULL )
781         flexfatal( "attempt to increase array size failed" );
782
783     return ( new_array );
784     }
785
786
787 /* skelout - write out one section of the skeleton file
788  *
789  * synopsis
790  *    skelout();
791  *
792  * DESCRIPTION
793  *    Copies from skelfile to stdout until a line beginning with "%%" or
794  *    EOF is found.
795  */
796 void skelout()
797
798     {
799     char buf[MAXLINE];
800
801     while ( fgets( buf, MAXLINE, skelfile ) != NULL )
802         if ( buf[0] == '%' && buf[1] == '%' )
803             break;
804         else
805             fputs( buf, stdout );
806     }
807
808
809 /* transition_struct_out - output a yy_trans_info structure
810  *
811  * synopsis
812  *     int element_v, element_n;
813  *     transition_struct_out( element_v, element_n );
814  *
815  * outputs the yy_trans_info structure with the two elements, element_v and
816  * element_n.  Formats the output with spaces and carriage returns.
817  */
818
819 void transition_struct_out( element_v, element_n )
820 int element_v, element_n;
821
822     {
823     printf( "%7d, %5d,", element_v, element_n );
824
825     datapos += TRANS_STRUCT_PRINT_LENGTH;
826
827     if ( datapos >= 75 )
828         {
829         putchar( '\n' );
830
831         if ( ++dataline % 10 == 0 )
832             putchar( '\n' );
833
834         datapos = 0;
835         }
836     }