summaryrefslogtreecommitdiff
path: root/utils/Spiff/comment.c
blob: f9a38275895da8b69090a6231f46aa63e99a70f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*                        Copyright (c) 1988 Bellcore
**                            All Rights Reserved
**       Permission is granted to copy or use this program, EXCEPT that it
**       may not be sold for profit, the copyright notice must be reproduced
**       on copies, and credit should be given to Bellcore where it is due.
**       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
*/


#ifndef lint
static char rcsid[]= "$Header$";
#endif


#include "misc.h"
#include "comment.h"
#include "strings.h"

/*
**	storage for the comment specifiers that can appear
**		anywhere on a line
*/
static int _W_nextcom = 0;
_W_comstruct _W_coms[_W_COMMAX];

/*
**	storage for comment specifiers that are examined only at the
**		beginning of each line
*/
static int _W_nextbol = 0;
_W_bolstruct _W_bols[_W_BOLMAX];

/*
**	storage for delimiters of literal strings
*/
static int _W_nextlit = 0;
_W_litstruct _W_lits[_W_LITMAX];

/*
**	storage for characters to specify beginning and end of line
**	in the comment and literal commands
*/
char _W_bolchar = '^';
char _W_eolchar = '$';


/*
**	build up a list of comment delimiters
*/
void
W_addcom(str,nestflag)
char *str;
int nestflag;
{
	/*
	**	check for comments that begin at the beginning of line
	*/
	if (*str ==  _W_bolchar)
	{
		if (_W_nextbol >= _W_BOLMAX)
			Z_fatal("too many beginning of line comment delimiter sets");

		str++;	/*skip the bol char */
		S_wordcpy(_W_bols[_W_nextbol].begin,str);

		S_nextword(&str);

		if (*str == _W_eolchar)
		{
			(void) strcpy(_W_bols[_W_nextbol].end,"\n");
		}
		else
		{
			S_wordcpy(_W_bols[_W_nextbol].end,str);
		}

		S_nextword(&str);
		S_wordcpy(_W_bols[_W_nextbol].escape,str);

		/*
		**
		*/
		if (nestflag)
			Z_complain("begining of line comment won't nest");

		_W_nextbol++;
	}
	else
	{
		if (_W_nextcom >= _W_COMMAX)
			Z_fatal("too many comment delimiter sets");

		S_wordcpy(_W_coms[_W_nextcom].begin,str);

		S_nextword(&str);

		if (*str == _W_eolchar)
		{
			(void) strcpy(_W_coms[_W_nextbol].end,"\n");
		}
		else
		{
			S_wordcpy(_W_coms[_W_nextbol].end,str);
		}

		S_nextword(&str);
		S_wordcpy(_W_coms[_W_nextcom].escape,str);

		_W_coms[_W_nextcom].nestbit = nestflag;

		_W_nextcom++;
	}
	return;
}


/*
**	clear the comment delimiter storage
*/
void
W_clearcoms()
{
	_W_nextcom = 0;
	_W_nextbol = 0;
	return;
}

/*
**	build up the list of literal delimiters
*/
void
W_addlit(str)
char *str;
{
	if (_W_nextlit >= _W_LITMAX)
		Z_fatal("too many literal delimiter sets");

	S_wordcpy(_W_lits[_W_nextlit].begin,str);

	S_nextword(&str);
	S_wordcpy(_W_lits[_W_nextlit].end,str);

	S_nextword(&str);
	S_wordcpy(_W_lits[_W_nextlit].escape,str);

	_W_nextlit++;
	return;
}

/*
**	clear the literal delimiter storage
*/
void
W_clearlits()
{
	_W_nextlit = 0;
	return;
}



static _W_bolstruct bol_scratch;

static void
_W_copybol(to,from)
W_bol to,from;
{
	(void) strcpy(to->begin,from->begin);
	(void) strcpy(to->end,from->end);
	(void) strcpy(to->escape,from->escape);
}

W_bol
W_isbol(str)
char *str;
{
	int i;

	for(i=0;i<_W_nextbol;i++)
	{
		if(!S_wordcmp(str,_W_bols[i].begin))
		{
			_W_copybol(&bol_scratch,&_W_bols[i]);
			return(&bol_scratch);
		}
	}
	return(W_BOLNULL);
}

W_is_bol(ptr)
W_bol ptr;
{
	int i;

	for(i=0;i<_W_nextbol;i++)
	{
		if(!S_wordcmp(ptr->begin,_W_bols[i].begin) &&
			!S_wordcmp(ptr->end,_W_bols[i].end) &&
			!S_wordcmp(ptr->escape,_W_bols[i].escape))
		{
			return(1);
		}

	}
	return(0);
}


static _W_litstruct lit_scratch;

static void
_W_copylit(to,from)
W_lit to,from;
{
	(void) strcpy(to->begin,from->begin);
	(void) strcpy(to->end,from->end);
	(void) strcpy(to->escape,from->escape);
}

W_lit
W_islit(str)
char *str;
{
	int i;

	for(i=0;i<_W_nextlit;i++)
	{
		if(!S_wordcmp(str,_W_lits[i].begin))
		{
			_W_copylit(&lit_scratch,&_W_lits[i]);
			return(&lit_scratch);
		}
	}
	return(W_LITNULL);
}

W_is_lit(ptr)
W_lit ptr;
{
	int i;

	for(i=0;i<_W_nextlit;i++)
	{
		if(!S_wordcmp(ptr->begin,_W_lits[i].begin) &&
			!S_wordcmp(ptr->end,_W_lits[i].end) &&
			!S_wordcmp(ptr->escape,_W_lits[i].escape))
		{
			return(1);
		}

	}
	return(0);
}

static _W_comstruct com_scratch;

static void
_W_copycom(to,from)
W_com to,from;
{
	(void) strcpy(to->begin,from->begin);
	(void) strcpy(to->end,from->end);
	(void) strcpy(to->escape,from->escape);
	to->nestbit = from->nestbit;
}

W_com
W_iscom(str)
char *str;
{
	int i;

	for(i=0;i<_W_nextcom;i++)
	{
		if(!S_wordcmp(str,_W_coms[i].begin))
		{
			_W_copycom(&com_scratch,&_W_coms[i]);
			return(&com_scratch);
		}
	}
	return(W_COMNULL);
}

W_is_com(ptr)
W_com ptr;
{
	int i;

	for(i=0;i<_W_nextcom;i++)
	{
		if(!S_wordcmp(ptr->begin,_W_coms[i].begin) &&
			!S_wordcmp(ptr->end,_W_coms[i].end) &&
			!S_wordcmp(ptr->escape,_W_coms[i].escape) &&
			ptr->nestbit == _W_coms[i].nestbit)
		{
			return(1);
		}

	}
	return(0);
}

W_is_nesting(ptr)
W_com ptr;
{
	return(ptr->nestbit);
}