home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lex / ccl.c next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  4.8 KB  |  189 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Vern Paxson of Lawrence Berkeley Laboratory.
  7.  * 
  8.  * The United States Government has rights in this work pursuant
  9.  * to contract no. DE-AC03-76SF00098 between the United States
  10.  * Department of Energy and the University of California.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *    This product includes software developed by the University of
  23.  *    California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  */
  40.  
  41. #ifndef lint
  42. static char sccsid[] = "@(#)ccl.c    5.2 (Berkeley) 6/18/90";
  43. #endif /* not lint */
  44.  
  45. /* ccl - routines for character classes */
  46.  
  47. #include "flexdef.h"
  48.  
  49. /* ccladd - add a single character to a ccl
  50.  *
  51.  * synopsis
  52.  *    int cclp;
  53.  *    int ch;
  54.  *    ccladd( cclp, ch );
  55.  */
  56.  
  57. void ccladd( cclp, ch )
  58. int cclp;
  59. int ch;
  60.  
  61.     {
  62.     int ind, len, newpos, i;
  63.  
  64.     len = ccllen[cclp];
  65.     ind = cclmap[cclp];
  66.  
  67.     /* check to see if the character is already in the ccl */
  68.  
  69.     for ( i = 0; i < len; ++i )
  70.     if ( ccltbl[ind + i] == ch )
  71.         return;
  72.  
  73.     newpos = ind + len;
  74.  
  75.     if ( newpos >= current_max_ccl_tbl_size )
  76.     {
  77.     current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
  78.  
  79.     ++num_reallocs;
  80.  
  81.     ccltbl = reallocate_character_array( ccltbl, current_max_ccl_tbl_size );
  82.     }
  83.  
  84.     ccllen[cclp] = len + 1;
  85.     ccltbl[newpos] = ch;
  86.     }
  87.  
  88.  
  89. /* cclinit - make an empty ccl
  90.  *
  91.  * synopsis
  92.  *    int cclinit();
  93.  *    new_ccl = cclinit();
  94.  */
  95.  
  96. int cclinit()
  97.  
  98.     {
  99.     if ( ++lastccl >= current_maxccls )
  100.     {
  101.     current_maxccls += MAX_CCLS_INCREMENT;
  102.  
  103.     ++num_reallocs;
  104.  
  105.     cclmap = reallocate_integer_array( cclmap, current_maxccls );
  106.     ccllen = reallocate_integer_array( ccllen, current_maxccls );
  107.     cclng = reallocate_integer_array( cclng, current_maxccls );
  108.     }
  109.  
  110.     if ( lastccl == 1 )
  111.     /* we're making the first ccl */
  112.     cclmap[lastccl] = 0;
  113.  
  114.     else
  115.     /* the new pointer is just past the end of the last ccl.  Since
  116.      * the cclmap points to the \first/ character of a ccl, adding the
  117.      * length of the ccl to the cclmap pointer will produce a cursor
  118.      * to the first free space
  119.      */
  120.     cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
  121.  
  122.     ccllen[lastccl] = 0;
  123.     cclng[lastccl] = 0;    /* ccl's start out life un-negated */
  124.  
  125.     return ( lastccl );
  126.     }
  127.  
  128.  
  129. /* cclnegate - negate a ccl
  130.  *
  131.  * synopsis
  132.  *    int cclp;
  133.  *    cclnegate( ccl );
  134.  */
  135.  
  136. void cclnegate( cclp )
  137. int cclp;
  138.  
  139.     {
  140.     cclng[cclp] = 1;
  141.     }
  142.  
  143.  
  144. /* list_character_set - list the members of a set of characters in CCL form
  145.  *
  146.  * synopsis
  147.  *     int cset[CSIZE];
  148.  *     FILE *file;
  149.  *     list_character_set( cset );
  150.  *
  151.  * writes to the given file a character-class representation of those
  152.  * characters present in the given set.  A character is present if it
  153.  * has a non-zero value in the set array.
  154.  */
  155.  
  156. void list_character_set( file, cset )
  157. FILE *file;
  158. int cset[];
  159.  
  160.     {
  161.     register int i;
  162.     char *readable_form();
  163.  
  164.     putc( '[', file );
  165.  
  166.     for ( i = 0; i < csize; ++i )
  167.     {
  168.     if ( cset[i] )
  169.         {
  170.         register int start_char = i;
  171.  
  172.         putc( ' ', file );
  173.  
  174.         fputs( readable_form( i ), file );
  175.  
  176.         while ( ++i < csize && cset[i] )
  177.         ;
  178.  
  179.         if ( i - 1 > start_char )
  180.         /* this was a run */
  181.         fprintf( file, "-%s", readable_form( i - 1 ) );
  182.  
  183.         putc( ' ', file );
  184.         }
  185.     }
  186.  
  187.     putc( ']', file );
  188.     }
  189.