home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / flex-2.4.6-src.lha / src / amiga / flex-2.4.6 / misc.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  14KB  |  774 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. /* $Header: misc.c,v 1.2 94/01/04 14:33:10 vern Exp $ */
  30.  
  31. #include "flexdef.h"
  32.  
  33.  
  34.  
  35. /* declare functions that have forward references */
  36.  
  37. void dataflush PROTO((void));
  38. int otoi PROTO((Char []));
  39.  
  40.  
  41. void add_action( new_text )
  42. char *new_text;
  43.     {
  44.     int len = strlen( new_text );
  45.  
  46.     while ( len + action_index >= action_size - 10 /* slop */ )
  47.         {
  48.         action_size *= 2;
  49.         action_array =
  50.             reallocate_character_array( action_array, action_size );
  51.         }
  52.  
  53.     strcpy( &action_array[action_index], new_text );
  54.  
  55.     action_index += len;
  56.     }
  57.  
  58.  
  59. /* allocate_array - allocate memory for an integer array of the given size */
  60.  
  61. void *allocate_array( size, element_size )
  62. int size, element_size;
  63.     {
  64.     register void *mem;
  65.  
  66.     /* On 16-bit int machines (e.g., 80286) we might be trying to
  67.      * allocate more than a signed int can hold, and that won't
  68.      * work.  Cheap test:
  69.      */
  70.     if ( element_size * size <= 0 )
  71.         flexfatal( "request for < 1 byte in allocate_array()" );
  72.  
  73.     mem = flex_alloc( element_size * size );
  74.  
  75.     if ( mem == NULL )
  76.         flexfatal( "memory allocation failed in allocate_array()" );
  77.  
  78.     return mem;
  79.     }
  80.  
  81.  
  82. /* all_lower - true if a string is all lower-case */
  83.  
  84. int all_lower( str )
  85. register char *str;
  86.     {
  87.     while ( *str )
  88.         {
  89.         if ( ! isascii( (Char) *str ) || ! islower( *str ) )
  90.             return 0;
  91.         ++str;
  92.         }
  93.  
  94.     return 1;
  95.     }
  96.  
  97.  
  98. /* all_upper - true if a string is all upper-case */
  99.  
  100. int all_upper( str )
  101. register char *str;
  102.     {
  103.     while ( *str )
  104.         {
  105.         if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
  106.             return 0;
  107.         ++str;
  108.         }
  109.  
  110.     return 1;
  111.     }
  112.  
  113.  
  114. /* bubble - bubble sort an integer array in increasing order
  115.  *
  116.  * synopsis
  117.  *   int v[n], n;
  118.  *   void bubble( v, n );
  119.  *
  120.  * description
  121.  *   sorts the first n elements of array v and replaces them in
  122.  *   increasing order.
  123.  *
  124.  * passed
  125.  *   v - the array to be sorted
  126.  *   n - the number of elements of 'v' to be sorted
  127.  */
  128.  
  129. void bubble( v, n )
  130. int v[], n;
  131.     {
  132.     register int i, j, k;
  133.  
  134.     for ( i = n; i > 1; --i )
  135.         for ( j = 1; j < i; ++j )
  136.             if ( v[j] > v[j + 1] )    /* compare */
  137.                 {
  138.                 k = v[j];    /* exchange */
  139.                 v[j] = v[j + 1];
  140.                 v[j + 1] = k;
  141.                 }
  142.     }
  143.  
  144.  
  145. /* check_char - checks a character to make sure it's within the range
  146.  *        we're expecting.  If not, generates fatal error message
  147.  *        and exits.
  148.  */
  149.  
  150. void check_char( c )
  151. int c;
  152.     {
  153.     if ( c >= CSIZE )
  154.         lerrsf( "bad character '%s' detected in check_char()",
  155.             readable_form( c ) );
  156.  
  157.     if ( c >= csize )
  158.         lerrsf( "scanner requires -8 flag to use the character '%s'",
  159.             readable_form( c ) );
  160.     }
  161.  
  162.  
  163.  
  164. /* clower - replace upper-case letter to lower-case */
  165.  
  166. Char clower( c )
  167. register int c;
  168.     {
  169.     return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
  170.     }
  171.  
  172.  
  173. /* copy_string - returns a dynamically allocated copy of a string */
  174.  
  175. char *copy_string( str )
  176. register char *str;
  177.     {
  178.     register char *c;
  179.     char *copy;
  180.  
  181.     /* find length */
  182.     for ( c = str; *c; ++c )
  183.         ;
  184.  
  185.     copy = (char *) flex_alloc( (c - str + 1) * sizeof( char ) );
  186.  
  187.     if ( copy == NULL )
  188.         flexfatal( "dynamic memory failure in copy_string()" );
  189.  
  190.     for ( c = copy; (*c++ = *str++); )
  191.         ;
  192.  
  193.     return copy;
  194.     }
  195.  
  196.  
  197. /* copy_unsigned_string -
  198.  *    returns a dynamically allocated copy of a (potentially) unsigned string
  199.  */
  200.  
  201. Char *copy_unsigned_string( str )
  202. register Char *str;
  203.     {
  204.     register Char *c;
  205.     Char *copy;
  206.  
  207.     /* find length */
  208.     for ( c = str; *c; ++c )
  209.         ;
  210.  
  211.     copy = allocate_Character_array( c - str + 1 );
  212.  
  213.     for ( c = copy; (*c++ = *str++); )
  214.         ;
  215.  
  216.     return copy;
  217.     }
  218.  
  219.  
  220. /* cshell - shell sort a character array in increasing order
  221.  *
  222.  * synopsis
  223.  *
  224.  *   Char v[n];
  225.  *   int n, special_case_0;
  226.  *   cshell( v, n, special_case_0 );
  227.  *
  228.  * description
  229.  *   Does a shell sort of the first n elements of array v.
  230.  *   If special_case_0 is true, then any element equal to 0
  231.  *   is instead assumed to have infinite weight.
  232.  *
  233.  * passed
  234.  *   v - array to be sorted
  235.  *   n - number of elements of v to be sorted
  236.  */
  237.  
  238. void cshell( v, n, special_case_0 )
  239. Char v[];
  240. int n, special_case_0;
  241.     {
  242.     int gap, i, j, jg;
  243.     Char k;
  244.  
  245.     for ( gap = n / 2; gap > 0; gap = gap / 2 )
  246.         for ( i = gap; i < n; ++i )
  247.             for ( j = i - gap; j >= 0; j = j - gap )
  248.                 {
  249.                 jg = j + gap;
  250.  
  251.                 if ( special_case_0 )
  252.                     {
  253.                     if ( v[jg] == 0 )
  254.                         break;
  255.  
  256.                     else if ( v[j] != 0 && v[j] <= v[jg] )
  257.                         break;
  258.                     }
  259.  
  260.                 else if ( v[j] <= v[jg] )
  261.                     break;
  262.  
  263.                 k = v[j];
  264.                 v[j] = v[jg];
  265.                 v[jg] = k;
  266.                 }
  267.     }
  268.  
  269.  
  270. /* dataend - finish up a block of data declarations */
  271.  
  272. void dataend()
  273.     {
  274.     if ( datapos > 0 )
  275.         dataflush();
  276.  
  277.     /* add terminator for initialization; { for vi */
  278.     puts( "    } ;\n" );
  279.  
  280.     dataline = 0;
  281.     datapos = 0;
  282.     }
  283.  
  284.  
  285. /* dataflush - flush generated data statements */
  286.  
  287. void dataflush()
  288.     {
  289.     putchar( '\n' );
  290.  
  291.     if ( ++dataline >= NUMDATALINES )
  292.         {
  293.         /* Put out a blank line so that the table is grouped into
  294.          * large blocks that enable the user to find elements easily.
  295.          */
  296.         putchar( '\n' );
  297.         dataline = 0;
  298.         }
  299.  
  300.     /* Reset the number of characters written on the current line. */
  301.     datapos = 0;
  302.     }
  303.  
  304.  
  305. /* flexerror - report an error message and terminate */
  306.  
  307. void flexerror( msg )
  308. char msg[];
  309.     {
  310.     fprintf( stderr, "%s: %s\n", program_name, msg );
  311.     flexend( 1 );
  312.     }
  313.  
  314.  
  315. /* flexfatal - report a fatal error message and terminate */
  316.  
  317. void flexfatal( msg )
  318. char msg[];
  319.     {
  320.     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
  321.     exit( 1 );
  322.     }
  323.  
  324.  
  325. /* lerrif - report an error message formatted with one integer argument */
  326.  
  327. void lerrif( msg, arg )
  328. char msg[];
  329. int arg;
  330.     {
  331.     char errmsg[MAXLINE];
  332.     (void) sprintf( errmsg, msg, arg );
  333.     flexerror( errmsg );
  334.     }
  335.  
  336.  
  337. /* lerrsf - report an error message formatted with one string argument */
  338.  
  339. void lerrsf( msg, arg )
  340. char msg[], arg[];
  341.     {
  342.     char errmsg[MAXLINE];
  343.  
  344.     (void) sprintf( errmsg, msg, arg );
  345.     flexerror( errmsg );
  346.     }
  347.  
  348.  
  349. /* htoi - convert a hexadecimal digit string to an integer value */
  350.  
  351. int htoi( str )
  352. Char str[];
  353.     {
  354.     unsigned int result;
  355.  
  356.     (void) sscanf( (char *) str, "%x", &result );
  357.  
  358.     return result;
  359.     }
  360.  
  361.  
  362. /* is_hex_digit - returns true if a character is a valid hex digit, false
  363.  *          otherwise
  364.  */
  365.  
  366. int is_hex_digit( ch )
  367. int ch;
  368.     {
  369.     if ( isdigit( ch ) )
  370.         return 1;
  371.  
  372.     switch ( clower( ch ) )
  373.         {
  374.         case 'a':
  375.         case 'b':
  376.         case 'c':
  377.         case 'd':
  378.         case 'e':
  379.         case 'f':
  380.             return 1;
  381.  
  382.         default:
  383.             return 0;
  384.         }
  385.     }
  386.  
  387.  
  388. /* line_directive_out - spit out a "# line" statement */
  389.  
  390. void line_directive_out( output_file )
  391. FILE *output_file;
  392.     {
  393.     if ( infilename && gen_line_dirs )
  394.         {
  395.         char directive[MAXLINE];
  396.         sprintf( directive, "# line %d \"%s\"\n", linenum, infilename );
  397.  
  398.         /* If output_file is nil then we should put the directive in
  399.          * the accumulated actions.
  400.          */
  401.         if ( output_file )
  402.             fputs( directive, output_file );
  403.         else
  404.             add_action( directive );
  405.         }
  406.     }
  407.  
  408.  
  409. /* mark_defs1 - mark the current position in the action array as
  410.  *               representing where the user's section 1 definitions end
  411.  *         and the prolog begins
  412.  */
  413. void mark_defs1()
  414.     {
  415.     defs1_offset = 0;