home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / develope / mpwflexp.sit / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  14.6 KB  |  832 lines

  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.     "@(#) $Header: misc.c,v 1.4 90/08/14 11:47:18 hmgr 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. #ifdef macintosh
  394. #include <types.h>
  395. #include <time.h>
  396. #else
  397. #ifndef MS_DOS
  398. #ifndef VMS
  399. #include <sys/types.h>
  400. #else
  401. #include <types.h>
  402. #endif
  403. #endif
  404.  
  405. #ifdef MS_DOS
  406. #include <time.h>
  407. typedef long time_t;
  408. #endif
  409. #endif
  410.  
  411. char *flex_gettime()
  412.  
  413.     {
  414.     time_t t, time();
  415.     char *result, *ctime(), *copy_string();
  416.  
  417.     t = time( (long *) 0 );
  418.  
  419.     result = copy_string( ctime( &t ) );
  420.  
  421.     /* get rid of trailing newline */
  422.     result[24] = '\0';
  423.  
  424.     return ( result );
  425.     }
  426.  
  427.  
  428. /* lerrif - report an error message formatted with one integer argument
  429.  *
  430.  * synopsis
  431.  *    char msg[];
  432.  *    int arg;
  433.  *    lerrif( msg, arg );
  434.  */
  435.  
  436. void lerrif( msg, arg )
  437. char msg[];
  438. int arg;
  439.  
  440.     {
  441.     char errmsg[MAXLINE];
  442.     (void) sprintf( errmsg, msg, arg );
  443.     flexerror( errmsg );
  444.     }
  445.  
  446.  
  447. /* lerrsf - report an error message formatted with one string argument
  448.  *
  449.  * synopsis
  450.  *    char msg[], arg[];
  451.  *    lerrsf( msg, arg );
  452.  */
  453.  
  454. void lerrsf( msg, arg )
  455. char msg[], arg[];
  456.  
  457.     {
  458.     char errmsg[MAXLINE];
  459.  
  460.     (void) sprintf( errmsg, msg, arg );
  461.     flexerror( errmsg );
  462.     }
  463.  
  464.  
  465. /* htoi - convert a hexadecimal digit string to an integer value
  466.  *
  467.  * synopsis:
  468.  *    int val, htoi();
  469.  *    Char str[];
  470.  *    val = htoi( str );
  471.  */
  472.  
  473. int htoi( str )
  474. Char str[];
  475.  
  476.     {
  477.     int result;
  478.  
  479.     (void) sscanf( (char *) str, "%x", &result );
  480.  
  481.     return ( result );
  482.     }
  483.  
  484.  
  485. /* is_hex_digit - returns true if a character is a valid hex digit, false
  486.  *          otherwise
  487.  *
  488.  * synopsis:
  489.  *    int true_or_false, is_hex_digit();
  490.  *    int ch;
  491.  *    val = is_hex_digit( ch );
  492.  */
  493.  
  494. int is_hex_digit( ch )
  495. int ch;
  496.  
  497.     {
  498.     if ( isdigit( ch ) )
  499.     return ( 1 );
  500.  
  501.     switch ( clower( ch ) )
  502.     {
  503.     case 'a':
  504.     case 'b':
  505.     case 'c':
  506.     case 'd':
  507.     case 'e':
  508.     case 'f':
  509.         return ( 1 );
  510.  
  511.     default:
  512.         return ( 0 );
  513.     }
  514.     }
  515.  
  516.  
  517. /* line_directive_out - spit out a "# line" statement */
  518.  
  519. void line_directive_out( output_file_name )
  520. FILE *output_file_name;
  521.  
  522.     {
  523.     if ( infilename && gen_line_dirs )
  524.         fprintf( output_file_name, "# line %d \"%s\"\n", linenum, infilename );
  525.     }
  526.  
  527.  
  528. /* mk2data - generate a data statement for a two-dimensional array
  529.  *
  530.  * synopsis
  531.  *    int value;
  532.  *    mk2data( value );
  533.  *
  534.  *  generates a data statement initializing the current 2-D array to "value"
  535.  */
  536. void mk2data( value )
  537. int value;
  538.  
  539.     {
  540.     if ( datapos >= NUMDATAITEMS )
  541.     {
  542.     putchar( ',' );
  543.     dataflush();
  544.     }
  545.  
  546.     if ( datapos == 0 )
  547.     /* indent */
  548.     fputs( "    ", stdout );
  549.  
  550.     else
  551.     putchar( ',' );
  552.  
  553.     ++datapos;
  554.  
  555.     printf( "%5d", value );
  556.     }
  557.  
  558.  
  559. /* mkdata - generate a data statement
  560.  *
  561.  * synopsis
  562.  *    int value;
  563.  *    mkdata( value );
  564.  *
  565.  *  generates a data statement initializing the current array element to
  566.  *  "value"
  567.  */
  568. void mkdata( value )
  569. int value;
  570.  
  571.     {
  572.     if ( datapos >= NUMDATAITEMS )
  573.     {
  574.     putchar( ',' );
  575.     dataflush();
  576.     }
  577.  
  578.     if ( datapos == 0 )
  579.     /* indent */
  580.     fputs( "    ", stdout );
  581.  
  582.     else
  583.     putchar( ',' );
  584.  
  585.     ++datapos;
  586.  
  587.     printf( "%5d", value );
  588.     }
  589.  
  590.  
  591. /* myctoi - return the integer represented by a string of digits
  592.  *
  593.  * synopsis
  594.  *    Char array[];
  595.  *    int val, myctoi();
  596.  *    val = myctoi( array );
  597.  *
  598.  */
  599.  
  600. int myctoi( array )
  601. Char array[];
  602.  
  603.     {
  604.     int val = 0;
  605.  
  606.     (void) sscanf( (char *) array, "%d", &val );
  607.  
  608.     return ( val );
  609.     }
  610.  
  611.  
  612. /* myesc - return character corresponding to escape sequence
  613.  *
  614.  * synopsis
  615.  *    Char array[], c, myesc();
  616.  *    c = myesc( array );
  617.  *
  618.  */
  619.  
  620. Char myesc( array )
  621. Char array[];
  622.  
  623.     {
  624.     Char c, esc_char;
  625.     register int sptr;
  626.  
  627.     switch ( array[1] )
  628.     {
  629.     case 'a': return ( '\a' );
  630.     case 'b': return ( '\b' );
  631.     case 'f': return ( '\f' );
  632.     case 'n': return ( '\n' );
  633.     case 'r': return ( '\r' );
  634.     case 't': return ( '\t' );
  635.     case 'v': return ( '\v' );
  636.  
  637.     case '0':
  638.     case '1':
  639.     case '2':
  640.     case '3':
  641.     case '4':
  642.     case '5':
  643.     case '6':
  644.     case '7':
  645.     case '8':
  646.     case '9':
  647.         { /* \<octal> */
  648.         sptr = 1;
  649.  
  650.         while ( isascii( array[sptr] ) && isdigit( array[sptr] ) )
  651.         /* don't increment inside loop control because if
  652.          * isdigit() is a macro it might expand into multiple
  653.          * increments ...
  654.          */
  655.         ++sptr;
  656.  
  657.         c = array[sptr];
  658.         array[sptr] = '\0';
  659.  
  660.         esc_char = otoi( array + 1 );
  661.  
  662.         array[sptr] = c;
  663.  
  664.         return ( esc_char );
  665.         }
  666.  
  667.     case 'x':
  668.         { /* \x<hex> */
  669.         int sptr = 2;
  670.  
  671.         while ( isascii( array[sptr] ) && is_hex_digit( array[sptr] ) )
  672.         /* don't increment inside loop control because if
  673.          * isdigit() is a macro it might expand into multiple
  674.          * increments ...
  675.          */
  676.         ++sptr;
  677.  
  678.         c = array[sptr];
  679.         array[sptr] = '\0';
  680.  
  681.         esc_char = htoi( array + 2 );
  682.  
  683.         array[sptr] = c;
  684.  
  685.         return ( esc_char );
  686.         }
  687.  
  688.     default:
  689.         return ( array[1] );
  690.     }
  691.     }
  692.  
  693.  
  694. /* otoi - convert an octal digit string to an integer value
  695.  *
  696.  * synopsis:
  697.  *    int val, otoi();
  698.  *    Char str[];
  699.  *    val = otoi( str );
  700.  */
  701.  
  702. int otoi( str )
  703. Char str[];
  704.  
  705.     {
  706.     int result;
  707.  
  708.     (void) sscanf( (char *) str, "%o", &result );
  709.  
  710.     return ( result );
  711.     }
  712.  
  713.  
  714. /* readable_form - return the the human-readable form of a character
  715.  *
  716.  * synopsis:
  717.  *    int c;
  718.  *    char *readable_form();
  719.  *    <string> = readable_form( c );
  720.  *
  721.  * The returned string is in static storage.
  722.  */
  723.  
  724. char *readable_form( c )
  725. register int c;
  726.  
  727.     {
  728.     static char rform[10];
  729.  
  730.     if ( (c >= 0 && c < 32) || c >= 127 )
  731.     {
  732.     switch ( c )
  733.         {
  734.         case '\n': return ( "\\n" );
  735.         case '\t': return ( "\\t" );
  736.         case '\f': return ( "\\f" );
  737.         case '\r': return ( "\\r" );
  738.         case '\b': return ( "\\b" );
  739.  
  740.         default:
  741.         (void) sprintf( rform, "\\%.3o", c );
  742.         return ( rform );
  743.         }
  744.     }
  745.  
  746.     else if ( c == ' ' )
  747.     return ( "' '" );
  748.  
  749.     else
  750.     {
  751.     rform[0] = c;
  752.     rform[1] = '\0';
  753.  
  754.     return ( rform );
  755.     }
  756.     }
  757.  
  758.  
  759. /* reallocate_array - increase the size of a dynamic array */
  760.  
  761. void *reallocate_array( array, size, element_size )
  762. void *array;
  763. int size, element_size;
  764.  
  765.     {
  766.     register void *new_array;
  767.  
  768.     /* same worry as in allocate_array(): */
  769.     if ( size * element_size <= 0 )
  770.         flexfatal( "attempt to increase array size by less than 1 byte" );
  771.  
  772.     new_array =
  773.     (void *) realloc( (char *)array, (unsigned) (size * element_size ));
  774.  
  775.     if ( new_array == NULL )
  776.     flexfatal( "attempt to increase array size failed" );
  777.  
  778.     return ( new_array );
  779.     }
  780.  
  781.  
  782. /* skelout - write out one section of the skeleton file
  783.  *
  784.  * synopsis
  785.  *    skelout();
  786.  *
  787.  * DESCRIPTION
  788.  *    Copies from skelfile to stdout until a line beginning with "%%" or
  789.  *    EOF is found.
  790.  */
  791. void skelout()
  792.  
  793.     {
  794.     char buf[MAXLINE];
  795.  
  796.     while ( fgets( buf, MAXLINE, skelfile ) != NULL )
  797.     if ( buf[0] == '%' && buf[1] == '%' )
  798.         break;
  799.     else
  800.         fputs( buf, stdout );
  801.     }
  802.  
  803.  
  804. /* transition_struct_out - output a yy_trans_info structure
  805.  *
  806.  * synopsis
  807.  *     int element_v, element_n;
  808.  *     transition_struct_out( element_v, element_n );
  809.  *
  810.  * outputs the yy_trans_info structure with the two elements, element_v and
  811.  * element_n.  Formats the output with spaces and carriage returns.
  812.  */
  813.  
  814. void transition_struct_out( element_v, element_n )
  815. int element_v, element_n;
  816.  
  817.     {
  818.     printf( "%7d, %5d,", element_v, element_n );
  819.  
  820.     datapos += TRANS_STRUCT_PRINT_LENGTH;
  821.  
  822.     if ( datapos >= 75 )
  823.     {
  824.     putchar( '\n' );
  825.  
  826.     if ( ++dataline % 10 == 0 )
  827.         putchar( '\n' );
  828.  
  829.     datapos = 0;
  830.     }
  831.     }
  832.