Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / lex / ccl.c
1 /* ccl - routines for character classes */
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: /home/daffy/u0/vern/flex/RCS/ccl.c,v 2.9 93/09/16 20:32:14 vern Exp $ */
30 /* $FreeBSD: src/usr.bin/lex/ccl.c,v 1.5 1999/10/27 07:56:43 obrien Exp $ */
31
32 #include "flexdef.h"
33
34 /* ccladd - add a single character to a ccl */
35
36 void ccladd( cclp, ch )
37 int cclp;
38 int ch;
39         {
40         int ind, len, newpos, i;
41
42         check_char( ch );
43
44         len = ccllen[cclp];
45         ind = cclmap[cclp];
46
47         /* check to see if the character is already in the ccl */
48
49         for ( i = 0; i < len; ++i )
50                 if ( ccltbl[ind + i] == ch )
51                         return;
52
53         newpos = ind + len;
54
55         if ( newpos >= current_max_ccl_tbl_size )
56                 {
57                 current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
58
59                 ++num_reallocs;
60
61                 ccltbl = reallocate_Character_array( ccltbl,
62                                                 current_max_ccl_tbl_size );
63                 }
64
65         ccllen[cclp] = len + 1;
66         ccltbl[newpos] = ch;
67         }
68
69
70 /* cclinit - return an empty ccl */
71
72 int cclinit()
73         {
74         if ( ++lastccl >= current_maxccls )
75                 {
76                 current_maxccls += MAX_CCLS_INCREMENT;
77
78                 ++num_reallocs;
79
80                 cclmap = reallocate_integer_array( cclmap, current_maxccls );
81                 ccllen = reallocate_integer_array( ccllen, current_maxccls );
82                 cclng = reallocate_integer_array( cclng, current_maxccls );
83                 }
84
85         if ( lastccl == 1 )
86                 /* we're making the first ccl */
87                 cclmap[lastccl] = 0;
88
89         else
90                 /* The new pointer is just past the end of the last ccl.
91                  * Since the cclmap points to the \first/ character of a
92                  * ccl, adding the length of the ccl to the cclmap pointer
93                  * will produce a cursor to the first free space.
94                  */
95                 cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
96
97         ccllen[lastccl] = 0;
98         cclng[lastccl] = 0;     /* ccl's start out life un-negated */
99
100         return lastccl;
101         }
102
103
104 /* cclnegate - negate the given ccl */
105
106 void cclnegate( cclp )
107 int cclp;
108         {
109         cclng[cclp] = 1;
110         }
111
112
113 /* list_character_set - list the members of a set of characters in CCL form
114  *
115  * Writes to the given file a character-class representation of those
116  * characters present in the given CCL.  A character is present if it
117  * has a non-zero value in the cset array.
118  */
119
120 void list_character_set( file, cset )
121 FILE *file;
122 int cset[];
123         {
124         register int i;
125
126         putc( '[', file );
127
128         for ( i = 0; i < csize; ++i )
129                 {
130                 if ( cset[i] )
131                         {
132                         register int start_char = i;
133
134                         putc( ' ', file );
135
136                         fputs( readable_form( i ), file );
137
138                         while ( ++i < csize && cset[i] )
139                                 ;
140
141                         if ( i - 1 > start_char )
142                                 /* this was a run */
143                                 fprintf( file, "-%s", readable_form( i - 1 ) );
144
145                         putc( ' ', file );
146                         }
147                 }
148
149         putc( ']', file );
150         }