xref: /openbsd/usr.bin/lex/ccl.c (revision 78b63d65)
1 /*	$OpenBSD: ccl.c,v 1.5 2001/11/19 19:02:14 mpech Exp $	*/
2 
3 /* ccl - routines for character classes */
4 
5 /*-
6  * Copyright (c) 1990 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Vern Paxson.
11  *
12  * The United States Government has rights in this work pursuant
13  * to contract no. DE-AC03-76SF00098 between the United States
14  * Department of Energy and the University of California.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that: (1) source distributions
18  * retain this entire copyright notice and comment, and (2) distributions
19  * including binaries display the following acknowledgement:  ``This product
20  * includes software developed by the University of California, Berkeley
21  * and its contributors'' in the documentation or other materials provided
22  * with the distribution and in all advertising materials mentioning
23  * features or use of this software. Neither the name of the University nor
24  * the names of its contributors may be used to endorse or promote products
25  * derived from this software without specific prior written permission.
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30 
31 /* $Header: /home/cvs/src/usr.bin/lex/ccl.c,v 1.5 2001/11/19 19:02:14 mpech Exp $ */
32 
33 #include "flexdef.h"
34 
35 /* ccladd - add a single character to a ccl */
36 
37 void ccladd( cclp, ch )
38 int cclp;
39 int ch;
40 	{
41 	int ind, len, newpos, i;
42 
43 	check_char( ch );
44 
45 	len = ccllen[cclp];
46 	ind = cclmap[cclp];
47 
48 	/* check to see if the character is already in the ccl */
49 
50 	for ( i = 0; i < len; ++i )
51 		if ( ccltbl[ind + i] == ch )
52 			return;
53 
54 	newpos = ind + len;
55 
56 	if ( newpos >= current_max_ccl_tbl_size )
57 		{
58 		current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
59 
60 		++num_reallocs;
61 
62 		ccltbl = reallocate_Character_array( ccltbl,
63 						current_max_ccl_tbl_size );
64 		}
65 
66 	ccllen[cclp] = len + 1;
67 	ccltbl[newpos] = ch;
68 	}
69 
70 
71 /* cclinit - return an empty ccl */
72 
73 int cclinit()
74 	{
75 	if ( ++lastccl >= current_maxccls )
76 		{
77 		current_maxccls += MAX_CCLS_INCREMENT;
78 
79 		++num_reallocs;
80 
81 		cclmap = reallocate_integer_array( cclmap, current_maxccls );
82 		ccllen = reallocate_integer_array( ccllen, current_maxccls );
83 		cclng = reallocate_integer_array( cclng, current_maxccls );
84 		}
85 
86 	if ( lastccl == 1 )
87 		/* we're making the first ccl */
88 		cclmap[lastccl] = 0;
89 
90 	else
91 		/* The new pointer is just past the end of the last ccl.
92 		 * Since the cclmap points to the \first/ character of a
93 		 * ccl, adding the length of the ccl to the cclmap pointer
94 		 * will produce a cursor to the first free space.
95 		 */
96 		cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
97 
98 	ccllen[lastccl] = 0;
99 	cclng[lastccl] = 0;	/* ccl's start out life un-negated */
100 
101 	return lastccl;
102 	}
103 
104 
105 /* cclnegate - negate the given ccl */
106 
107 void cclnegate( cclp )
108 int cclp;
109 	{
110 	cclng[cclp] = 1;
111 	}
112 
113 
114 /* list_character_set - list the members of a set of characters in CCL form
115  *
116  * Writes to the given file a character-class representation of those
117  * characters present in the given CCL.  A character is present if it
118  * has a non-zero value in the cset array.
119  */
120 
121 void list_character_set( file, cset )
122 FILE *file;
123 int cset[];
124 	{
125 	int i;
126 
127 	putc( '[', file );
128 
129 	for ( i = 0; i < csize; ++i )
130 		{
131 		if ( cset[i] )
132 			{
133 			int start_char = i;
134 
135 			putc( ' ', file );
136 
137 			fputs( readable_form( i ), file );
138 
139 			while ( ++i < csize && cset[i] )
140 				;
141 
142 			if ( i - 1 > start_char )
143 				/* this was a run */
144 				fprintf( file, "-%s", readable_form( i - 1 ) );
145 
146 			putc( ' ', file );
147 			}
148 		}
149 
150 	putc( ']', file );
151 	}
152