lex.c 18.8 KB
Newer Older
Bob Halley's avatar
add lex    
Bob Halley committed
1
/*
David Lawrence's avatar
David Lawrence committed
2
 * Copyright (C) 1998-2000  Internet Software Consortium.
3
 *
Bob Halley's avatar
add lex    
Bob Halley committed
4
5
6
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
7
 *
8
9
10
11
12
13
14
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Bob Halley's avatar
add lex    
Bob Halley committed
16
17
 */

18
/* $Id: lex.c,v 1.42 2000/11/10 01:41:00 bwelling Exp $ */
David Lawrence's avatar
David Lawrence committed
19

Bob Halley's avatar
add lex    
Bob Halley committed
20
21
22
#include <config.h>

#include <ctype.h>
23
#include <errno.h>
Mark Andrews's avatar
Mark Andrews committed
24
#include <stdlib.h>
Bob Halley's avatar
add lex    
Bob Halley committed
25

26
#include <isc/buffer.h>
27
#include <isc/file.h>
Bob Halley's avatar
add lex    
Bob Halley committed
28
#include <isc/lex.h>
29
#include <isc/mem.h>
30
#include <isc/stdio.h>
31
#include <isc/string.h>
Michael Graff's avatar
Michael Graff committed
32
#include <isc/util.h>
Bob Halley's avatar
add lex    
Bob Halley committed
33
34
35
36
37

typedef struct inputsource {
	isc_result_t			result;
	isc_boolean_t			is_file;
	isc_boolean_t			need_close;
38
	isc_boolean_t			at_eof;
39
	isc_buffer_t *			pushback;
Bob Halley's avatar
add lex    
Bob Halley committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	void *				input;
	char *				name;
	unsigned long			line;
	LINK(struct inputsource)	link;
} inputsource;

#define LEX_MAGIC			0x4C657821U	/* Lex!. */
#define VALID_LEX(l)			((l) != NULL && \
					 (l)->magic == LEX_MAGIC)

struct isc_lex {
	/* Unlocked. */
	unsigned int			magic;
	isc_mem_t *			mctx;
	size_t				max_token;
	char *				data;
	unsigned int			options;
	unsigned int			comments;
	isc_boolean_t			comment_ok;
	isc_boolean_t			last_was_eol;
60
	unsigned int			paren_count;
Bob Halley's avatar
add lex    
Bob Halley committed
61
62
63
64
	isc_lexspecials_t		specials;
	LIST(struct inputsource)	sources;
};

65
66
67
68
69
static inline isc_result_t
grow_data(isc_lex_t *lex, size_t *remainingp, char **currp, char **prevp) {
	char *new;

	new = isc_mem_get(lex->mctx, lex->max_token * 2 + 1);
70
	if (new == NULL)
71
72
73
74
75
76
77
78
79
80
81
82
		return (ISC_R_NOMEMORY);
	memcpy(new, lex->data, lex->max_token + 1);
	*currp = new + (*currp - lex->data);
	if (*prevp != NULL)
		*prevp = new + (*prevp - lex->data);
	isc_mem_put(lex->mctx, lex->data, lex->max_token + 1);
	lex->data = new;
	*remainingp += lex->max_token;
	lex->max_token *= 2;
	return (ISC_R_SUCCESS);
}

Bob Halley's avatar
add lex    
Bob Halley committed
83
84
85
86
87
88
89
isc_result_t
isc_lex_create(isc_mem_t *mctx, size_t max_token, isc_lex_t **lexp) {
	isc_lex_t *lex;

	/*
	 * Create a lexer.
	 */
90

Bob Halley's avatar
add lex    
Bob Halley committed
91
92
93
94
95
96
	REQUIRE(lexp != NULL && *lexp == NULL);
	REQUIRE(max_token > 0);

	lex = isc_mem_get(mctx, sizeof *lex);
	if (lex == NULL)
		return (ISC_R_NOMEMORY);
Mark Andrews's avatar
Mark Andrews committed
97
	lex->data = isc_mem_get(mctx, max_token + 1);
Bob Halley's avatar
add lex    
Bob Halley committed
98
99
100
101
102
103
104
105
106
	if (lex->data == NULL) {
		isc_mem_put(mctx, lex, sizeof *lex);
		return (ISC_R_NOMEMORY);
	}
	lex->mctx = mctx;
	lex->max_token = max_token;
	lex->comments = 0;
	lex->comment_ok = ISC_TRUE;
	lex->last_was_eol = ISC_TRUE;
107
	lex->paren_count = 0;
Bob Halley's avatar
add lex    
Bob Halley committed
108
109
110
	memset(lex->specials, 0, 256);
	INIT_LIST(lex->sources);
	lex->magic = LEX_MAGIC;
111

Bob Halley's avatar
add lex    
Bob Halley committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	*lexp = lex;

	return (ISC_R_SUCCESS);
}

void
isc_lex_destroy(isc_lex_t **lexp) {
	isc_lex_t *lex;

	/*
	 * Destroy the lexer.
	 */

	REQUIRE(lexp != NULL);
	lex = *lexp;
	REQUIRE(VALID_LEX(lex));

	while (!EMPTY(lex->sources))
		isc_lex_close(lex);
	if (lex->data != NULL)
Mark Andrews's avatar
Mark Andrews committed
132
		isc_mem_put(lex->mctx, lex->data, lex->max_token + 1);
Bob Halley's avatar
add lex    
Bob Halley committed
133
	lex->magic = 0;
134
	isc_mem_put(lex->mctx, lex, sizeof *lex);
Bob Halley's avatar
add lex    
Bob Halley committed
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

	*lexp = NULL;
}

unsigned int
isc_lex_getcomments(isc_lex_t *lex) {
	/*
	 * Return the current lexer commenting styles.
	 */

	REQUIRE(VALID_LEX(lex));

	return (lex->comments);
}

void
isc_lex_setcomments(isc_lex_t *lex, unsigned int comments) {
	/*
	 * Set allowed lexer commenting styles.
	 */

	REQUIRE(VALID_LEX(lex));

	lex->comments = comments;
}

void
isc_lex_getspecials(isc_lex_t *lex, isc_lexspecials_t specials) {
	/*
	 * Put the current list of specials into 'specials'.
	 */

	REQUIRE(VALID_LEX(lex));

	memcpy(specials, lex->specials, 256);
}

void
isc_lex_setspecials(isc_lex_t *lex, isc_lexspecials_t specials) {
	/*
	 * The characters in 'specials' are returned as tokens.  Along with
	 * whitespace, they delimit strings and numbers.
	 */

	REQUIRE(VALID_LEX(lex));

	memcpy(lex->specials, specials, 256);
}

static inline isc_result_t
new_source(isc_lex_t *lex, isc_boolean_t is_file, isc_boolean_t need_close,
Bob Halley's avatar
Bob Halley committed
186
	   void *input, const char *name)
Bob Halley's avatar
add lex    
Bob Halley committed
187
188
{
	inputsource *source;
189
	isc_result_t result;
Bob Halley's avatar
add lex    
Bob Halley committed
190
191
192
193
194
195
196

	source = isc_mem_get(lex->mctx, sizeof *source);
	if (source == NULL)
		return (ISC_R_NOMEMORY);
	source->result = ISC_R_SUCCESS;
	source->is_file = is_file;
	source->need_close = need_close;
197
	source->at_eof = ISC_FALSE;
Bob Halley's avatar
add lex    
Bob Halley committed
198
199
	source->input = input;
	source->name = isc_mem_strdup(lex->mctx, name);
200
201
202
203
	if (source->name == NULL) {
		isc_mem_put(lex->mctx, source, sizeof *source);
		return (ISC_R_NOMEMORY);
	}
204
205
206
207
208
209
210
211
	source->pushback = NULL;
	result = isc_buffer_allocate(lex->mctx, &source->pushback,
				     lex->max_token);
	if (result != ISC_R_SUCCESS) {
		isc_mem_free(lex->mctx, source->name);
		isc_mem_put(lex->mctx, source, sizeof *source);
		return (result);
	}
Bob Halley's avatar
add lex    
Bob Halley committed
212
	source->line = 1;
213
	ISC_LIST_PREPENDUNSAFE(lex->sources, source, link);
Bob Halley's avatar
add lex    
Bob Halley committed
214
215
216
217
218

	return (ISC_R_SUCCESS);
}

isc_result_t
Bob Halley's avatar
Bob Halley committed
219
isc_lex_openfile(isc_lex_t *lex, const char *filename) {
220
221
	isc_result_t result;
	FILE *stream = NULL;
Bob Halley's avatar
add lex    
Bob Halley committed
222
223
224
225
226
227
228

	/*
	 * Open 'filename' and make it the current input source for 'lex'.
	 */

	REQUIRE(VALID_LEX(lex));

229
	result = isc_stdio_open(filename, "r", &stream);
230
231
232
	if (result != ISC_R_SUCCESS)
		return (result);

233
#ifdef HAVE_FLOCKFILE
Bob Halley's avatar
add lex    
Bob Halley committed
234
	flockfile(stream);
235
#endif
236

Bob Halley's avatar
add lex    
Bob Halley committed
237
238
239
240
241
242
243
244
245
246
247
248
249
	return (new_source(lex, ISC_TRUE, ISC_TRUE, stream, filename));
}

isc_result_t
isc_lex_openstream(isc_lex_t *lex, FILE *stream) {
	char name[128];

	/*
	 * Make 'stream' the current input source for 'lex'.
	 */

	REQUIRE(VALID_LEX(lex));

250
#ifdef HAVE_FLOCKFILE
Bob Halley's avatar
add lex    
Bob Halley committed
251
	flockfile(stream);
252
#endif
Bob Halley's avatar
add lex    
Bob Halley committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
	/* This is safe. */
	sprintf(name, "stream-%p", stream);

	return (new_source(lex, ISC_TRUE, ISC_FALSE, stream, name));
}

isc_result_t
isc_lex_openbuffer(isc_lex_t *lex, isc_buffer_t *buffer) {
	char name[128];

	/*
	 * Make 'buffer' the current input source for 'lex'.
	 */

	REQUIRE(VALID_LEX(lex));

	/* This is safe. */
	sprintf(name, "buffer-%p", buffer);

	return (new_source(lex, ISC_FALSE, ISC_FALSE, buffer, name));
}

275
isc_result_t
Bob Halley's avatar
add lex    
Bob Halley committed
276
277
278
279
280
281
282
283
isc_lex_close(isc_lex_t *lex) {
	inputsource *source;

	/*
	 * Close the most recently opened object (i.e. file or buffer).
	 */

	REQUIRE(VALID_LEX(lex));
284

Bob Halley's avatar
add lex    
Bob Halley committed
285
	source = HEAD(lex->sources);
286
287
	if (source == NULL)
		return (ISC_R_NOMORE);
288

Bob Halley's avatar
add lex    
Bob Halley committed
289
290
	UNLINK(lex->sources, source, link);
	if (source->is_file) {
291
#ifdef HAVE_FLOCKFILE
Bob Halley's avatar
add lex    
Bob Halley committed
292
		funlockfile((FILE *)(source->input));
293
#endif
Bob Halley's avatar
add lex    
Bob Halley committed
294
295
296
297
		if (source->need_close)
			fclose((FILE *)(source->input));
	}
	isc_mem_free(lex->mctx, source->name);
298
	isc_buffer_free(&source->pushback);
Bob Halley's avatar
add lex    
Bob Halley committed
299
	isc_mem_put(lex->mctx, source, sizeof *source);
300
301

	return (ISC_R_SUCCESS);
Bob Halley's avatar
add lex    
Bob Halley committed
302
303
304
305
306
307
308
309
310
311
312
}

typedef enum {
	lexstate_start,
	lexstate_crlf,
	lexstate_string,
	lexstate_number,
	lexstate_maybecomment,
	lexstate_ccomment,
	lexstate_ccommentend,
	lexstate_eatline,
Bob Halley's avatar
Bob Halley committed
313
	lexstate_qstring
Bob Halley's avatar
add lex    
Bob Halley committed
314
315
} lexstate;

316
317
#define IWSEOL (ISC_LEXOPT_INITIALWS | ISC_LEXOPT_EOL)

318
static void
319
pushback(inputsource *source, int c) {
320
321
	REQUIRE(source->pushback->current > 0);
	source->pushback->current--;
322
323
324
325
	if (c == '\n')
		source->line--;
}

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
static void
unpushback(inputsource *source) {
	isc_buffer_subtract(source->pushback, 1);
}

static isc_result_t
pushandgrow(isc_lex_t *lex, inputsource *source, int c) {
	if (isc_buffer_availablelength(source->pushback) == 0) {
		isc_buffer_t *tbuf = NULL;
		unsigned int oldlen;
		isc_region_t used;
		isc_result_t result;

		oldlen = isc_buffer_length(source->pushback);
		result = isc_buffer_allocate(lex->mctx, &tbuf, oldlen * 2);
		if (result != ISC_R_SUCCESS)
			return (result);
		isc_buffer_usedregion(source->pushback, &used);
		result = isc_buffer_copyregion(tbuf, &used);
		INSIST(result == ISC_R_SUCCESS);
		isc_buffer_free(&source->pushback);
		source->pushback = tbuf;
	}
	isc_buffer_putuint8(source->pushback, (isc_uint8_t)c);
	return (ISC_R_SUCCESS);
}
	
Bob Halley's avatar
add lex    
Bob Halley committed
353
354
355
356
357
358
359
360
361
362
363
364
365
isc_result_t
isc_lex_gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *tokenp) {
	inputsource *source;
	int c;
	isc_boolean_t done = ISC_FALSE;
	isc_boolean_t no_comments = ISC_FALSE;
	isc_boolean_t escaped = ISC_FALSE;
	lexstate state = lexstate_start;
	lexstate saved_state = lexstate_start;
	isc_buffer_t *buffer;
	FILE *stream;
	char *curr, *prev;
	size_t remaining;
366
	unsigned long ulong;
Mark Andrews's avatar
Mark Andrews committed
367
368
	unsigned int saved_options;
	char *e;
369
	isc_result_t result;
Bob Halley's avatar
add lex    
Bob Halley committed
370
371
372
373
374
375
376
377
378

	/*
	 * Get the next token.
	 */

	REQUIRE(VALID_LEX(lex));
	source = HEAD(lex->sources);
	REQUIRE(tokenp != NULL);

379
380
381
382
383
384
385
386
387
388
389
	if (source == NULL) {
		if ((options & ISC_LEXOPT_NOMORE) != 0) {
			tokenp->type = isc_tokentype_nomore;
			return (ISC_R_SUCCESS);
		}
		return (ISC_R_NOMORE);
	}

	if (source->result != ISC_R_SUCCESS)
		return (source->result);

390
391
392
	if (isc_buffer_remaininglength(source->pushback) == 0 &&
	    source->at_eof)
	{
393
394
395
396
397
398
399
400
401
		if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 &&
		    lex->paren_count != 0)
			return (ISC_R_UNBALANCED);
		if ((options & ISC_LEXOPT_EOF) != 0) {
			tokenp->type = isc_tokentype_eof;
			return (ISC_R_SUCCESS);
		}
		return (ISC_R_EOF);
	}
Bob Halley's avatar
add lex    
Bob Halley committed
402

403
404
	isc_buffer_compact(source->pushback);

405
406
407
	saved_options = options;
	if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 && lex->paren_count > 0)
		options &= ~IWSEOL;
408

Bob Halley's avatar
add lex    
Bob Halley committed
409
410
411
412
	curr = lex->data;
	prev = NULL;
	remaining = lex->max_token;
	do {
413
414
415
		if (isc_buffer_remaininglength(source->pushback) == 0) {
			if (source->is_file) {
				stream = source->input;
Bob Halley's avatar
add lex    
Bob Halley committed
416

417
#ifdef HAVE_FLOCKFILE
418
				c = getc_unlocked(stream);
419
#else
420
				c = getc(stream);
421
#endif
422
423
424
425
426
427
				if (c == EOF) {
					if (ferror(stream)) {
						source->result = ISC_R_IOERROR;
						return (source->result);
					}
					source->at_eof = ISC_TRUE;
Bob Halley's avatar
add lex    
Bob Halley committed
428
				}
429
			} else {
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
				buffer = source->input;

				if (buffer->current == buffer->used) {
					c = EOF;
					source->at_eof = ISC_TRUE;
				} else {
					c = *((char *)buffer->base +
					      buffer->current);
					buffer->current++;
				}
			}
			if (c != EOF) {
				source->result = pushandgrow(lex, source, c);
				if (source->result != ISC_R_SUCCESS)
					return (source->result);
445
			}
Bob Halley's avatar
add lex    
Bob Halley committed
446
		}
447
448
		if (!source->at_eof)
			c = isc_buffer_getuint8(source->pushback);
Bob Halley's avatar
add lex    
Bob Halley committed
449

450
		if (c == '\n')
451
			source->line++;
452

Bob Halley's avatar
add lex    
Bob Halley committed
453
		if (lex->comment_ok && !no_comments) {
454
			if (!escaped && c == ';' &&
Bob Halley's avatar
add lex    
Bob Halley committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
			    ((lex->comments & ISC_LEXCOMMENT_DNSMASTERFILE)
			     != 0)) {
				saved_state = state;
				state = lexstate_eatline;
				no_comments = ISC_TRUE;
				continue;
			} else if (c == '/' &&
				   (lex->comments &
				    (ISC_LEXCOMMENT_C|
				     ISC_LEXCOMMENT_CPLUSPLUS)) != 0) {
				saved_state = state;
				state = lexstate_maybecomment;
				no_comments = ISC_TRUE;
				continue;
			} else if (c == '#' &&
				   ((lex->comments & ISC_LEXCOMMENT_SHELL)
				    != 0)) {
				saved_state = state;
				state = lexstate_eatline;
				no_comments = ISC_TRUE;
				continue;
			}
		}

	no_read:
		/* INSIST(c == EOF || (c >= 0 && c <= 255)); */
		switch (state) {
		case lexstate_start:
			if (c == EOF) {
				lex->last_was_eol = ISC_FALSE;
485
486
487
				if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 &&
				    lex->paren_count != 0)
						return (ISC_R_UNBALANCED);
Bob Halley's avatar
add lex    
Bob Halley committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
				if ((options & ISC_LEXOPT_EOF) == 0)
					return (ISC_R_EOF);
				tokenp->type = isc_tokentype_eof;
				done = ISC_TRUE;
			} else if (c == ' ' || c == '\t') {
				if (lex->last_was_eol &&
				    (options & ISC_LEXOPT_INITIALWS)
				    != 0) {
					lex->last_was_eol = ISC_FALSE;
					tokenp->type = isc_tokentype_initialws;
					tokenp->value.as_char = c;
					done = ISC_TRUE;
				}
			} else if (c == '\n') {
				if ((options & ISC_LEXOPT_EOL) != 0) {
					tokenp->type = isc_tokentype_eol;
					done = ISC_TRUE;
				}
				lex->last_was_eol = ISC_TRUE;
			} else if (c == '\r') {
				if ((options & ISC_LEXOPT_EOL) != 0)
					state = lexstate_crlf;
			} else if (c == '"' &&
511
				   (options & ISC_LEXOPT_QSTRING) != 0) {
Bob Halley's avatar
add lex    
Bob Halley committed
512
513
514
515
516
				lex->last_was_eol = ISC_FALSE;
				no_comments = ISC_TRUE;
				state = lexstate_qstring;
			} else if (lex->specials[c]) {
				lex->last_was_eol = ISC_FALSE;
517
518
519
520
521
522
523
524
525
526
527
				if ((c == '(' || c == ')') &&
				    (options & ISC_LEXOPT_DNSMULTILINE) != 0) {
					if (c == '(') {
						if (lex->paren_count == 0)
							options &= ~IWSEOL;
						lex->paren_count++;
					} else {
						if (lex->paren_count == 0)
						    return (ISC_R_UNBALANCED);
						lex->paren_count--;
						if (lex->paren_count == 0)
528
529
							options =
								saved_options;
530
						unpushback(source);
531
532
533
					}
					continue;
				}
Bob Halley's avatar
add lex    
Bob Halley committed
534
535
536
				tokenp->type = isc_tokentype_special;
				tokenp->value.as_char = c;
				done = ISC_TRUE;
537
			} else if (isdigit((unsigned char)c) &&
538
				   (options & ISC_LEXOPT_NUMBER) != 0) {
Bob Halley's avatar
add lex    
Bob Halley committed
539
540
541
542
543
544
545
546
547
548
				lex->last_was_eol = ISC_FALSE;
				state = lexstate_number;
				goto no_read;
			} else {
				lex->last_was_eol = ISC_FALSE;
				state = lexstate_string;
				goto no_read;
			}
			break;
		case lexstate_crlf:
549
550
			if (c != '\n')
				pushback(source, c);
Bob Halley's avatar
add lex    
Bob Halley committed
551
552
553
554
555
			tokenp->type = isc_tokentype_eol;
			done = ISC_TRUE;
			lex->last_was_eol = ISC_TRUE;
			break;
		case lexstate_number:
556
			if (c == EOF || !isdigit((unsigned char)c)) {
Bob Halley's avatar
add lex    
Bob Halley committed
557
558
559
				if (c == ' ' || c == '\t' || c == '\r' ||
				    c == '\n' || c == EOF ||
				    lex->specials[c]) {
560
561
562
563
564
					int base;
					if ((options & ISC_LEXOPT_CNUMBER) != 0)
						base = 0;
					else
						base = 10;
565
					pushback(source, c);
566
					ulong = strtoul(lex->data, &e, base);
567
568
569
570
					if (ulong == ULONG_MAX &&
					    errno == ERANGE) {
						return (ISC_R_RANGE);
					} else if (*e == 0) {
Mark Andrews's avatar
Mark Andrews committed
571
572
573
574
575
576
577
578
579
580
581
582
						tokenp->type =
							isc_tokentype_number;
						tokenp->value.as_ulong =
							ulong;
					} else {
						isc_tokenvalue_t *v;

						tokenp->type =
							isc_tokentype_string;
						v = &(tokenp->value);
						v->as_textregion.base =
							lex->data;
583
						v->as_textregion.length =
Mark Andrews's avatar
Mark Andrews committed
584
585
							lex->max_token -
							remaining;
Mark Andrews's avatar
Mark Andrews committed
586
					}
Bob Halley's avatar
add lex    
Bob Halley committed
587
588
					done = ISC_TRUE;
					continue;
589
590
591
592
593
				} else if (!(options & ISC_LEXOPT_CNUMBER) ||
					   ((c != 'x' && c != 'X') ||
					   (curr != &lex->data[1]) ||
					   (lex->data[0] != '0'))) {
					/* Above test supports hex numbers */
Bob Halley's avatar
add lex    
Bob Halley committed
594
					state = lexstate_string;
595
				}
Bob Halley's avatar
add lex    
Bob Halley committed
596
			}
597
598
599
600
601
602
603
604
605
606
			if (remaining == 0) {
				result = grow_data(lex, &remaining,
						   &curr, &prev);
				if (result != ISC_R_SUCCESS)
					return (result);
			}
			INSIST(remaining > 0);
			*curr++ = c;
			*curr = '\0';
			remaining--;
Bob Halley's avatar
add lex    
Bob Halley committed
607
608
			break;
		case lexstate_string:
609
610
611
			if ((!escaped &&
			     (c == ' ' || c == '\t' || lex->specials[c])) ||
			    c == '\r' || c == '\n' || c == EOF) {
612
				pushback(source, c);
613
614
				if (source->result != ISC_R_SUCCESS)
					return (source->result);
Bob Halley's avatar
add lex    
Bob Halley committed
615
				tokenp->type = isc_tokentype_string;
Bob Halley's avatar
Bob Halley committed
616
				tokenp->value.as_textregion.base = lex->data;
617
				tokenp->value.as_textregion.length =
Bob Halley's avatar
add lex    
Bob Halley committed
618
619
620
621
					lex->max_token - remaining;
				done = ISC_TRUE;
				continue;
			}
622
623
624
			if ((options & ISC_LEXOPT_ESCAPE) != 0)
				escaped = (!escaped && c == '\\') ?
						ISC_TRUE : ISC_FALSE;
625
626
627
628
629
630
631
632
			if (remaining == 0) {
				result = grow_data(lex, &remaining,
						   &curr, &prev);
				if (result != ISC_R_SUCCESS)
					return (result);
			}
			INSIST(remaining > 0);
			*curr++ = c;
633
			*curr = '\0';
634
			remaining--;
Bob Halley's avatar
add lex    
Bob Halley committed
635
636
637
638
639
640
641
642
643
644
645
			break;
		case lexstate_maybecomment:
			if (c == '*' &&
			    (lex->comments & ISC_LEXCOMMENT_C) != 0) {
				state = lexstate_ccomment;
				continue;
			} else if (c == '/' &&
			    (lex->comments & ISC_LEXCOMMENT_CPLUSPLUS) != 0) {
				state = lexstate_eatline;
				continue;
			}
646
			pushback(source, c);
Bob Halley's avatar
add lex    
Bob Halley committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
			c = '/';
			no_comments = ISC_FALSE;
			state = saved_state;
			goto no_read;
		case lexstate_ccomment:
			if (c == EOF)
				return (ISC_R_UNEXPECTEDEND);
			if (c == '*')
				state = lexstate_ccommentend;
			break;
		case lexstate_ccommentend:
			if (c == EOF)
				return (ISC_R_UNEXPECTEDEND);
			if (c == '/') {
				/*
				 * C-style comments become a single space.
				 * We do this to ensure that a comment will
				 * act as a delimiter for strings and
				 * numbers.
				 */
				c = ' ';
				no_comments = ISC_FALSE;
				state = saved_state;
				goto no_read;
			} else
				state = lexstate_ccomment;
			break;
		case lexstate_eatline:
			if (c == EOF)
				return (ISC_R_UNEXPECTEDEND);
			if (c == '\n') {
				no_comments = ISC_FALSE;
				state = saved_state;
				goto no_read;
			}
			break;
		case lexstate_qstring:
			if (c == EOF)
				return (ISC_R_UNEXPECTEDEND);
			if (c == '"') {
				if (escaped) {
					escaped = ISC_FALSE;
					/*
					 * Overwrite the preceding backslash.
					 */
					INSIST(prev != NULL);
					*prev = '"';
				} else {
					tokenp->type = isc_tokentype_qstring;
Bob Halley's avatar
Bob Halley committed
696
					tokenp->value.as_textregion.base =
Bob Halley's avatar
add lex    
Bob Halley committed
697
						lex->data;
698
					tokenp->value.as_textregion.length =
Bob Halley's avatar
add lex    
Bob Halley committed
699
700
701
702
703
704
705
706
707
						lex->max_token - remaining;
					no_comments = ISC_FALSE;
					done = ISC_TRUE;
				}
			} else {
				if (c == '\\' && !escaped)
					escaped = ISC_TRUE;
				else
					escaped = ISC_FALSE;
708
709
710
711
712
713
714
715
716
				if (remaining == 0) {
					result = grow_data(lex, &remaining,
							   &curr, &prev);
					if (result != ISC_R_SUCCESS)
						return (result);
				}
				INSIST(remaining > 0);
				prev = curr;
				*curr++ = c;
717
				*curr = '\0';
718
				remaining--;
Bob Halley's avatar
add lex    
Bob Halley committed
719
720
721
			}
			break;
		default:
722
723
724
			FATAL_ERROR(__FILE__, __LINE__,
				    "Unexpected state %d", state);
			/* Does not return. */
Bob Halley's avatar
add lex    
Bob Halley committed
725
726
727
728
729
730
731
		}

	} while (!done);

	return (ISC_R_SUCCESS);
}

732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
isc_result_t
isc_lex_getmastertoken(isc_lex_t *lex, isc_token_t *token,
		       isc_tokentype_t expect, isc_boolean_t eol)
{
	unsigned int options = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
			       ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
	isc_result_t result;

	if (expect == isc_tokentype_qstring)
		options |= ISC_LEXOPT_QSTRING;
	else if (expect == isc_tokentype_number)
		options |= ISC_LEXOPT_NUMBER;
	result = isc_lex_gettoken(lex, options, token);
	if (result != ISC_R_SUCCESS)
		return (result);

	if (eol && ((token->type == isc_tokentype_eol) ||
		    (token->type == isc_tokentype_eof)))
		return (ISC_R_SUCCESS);
	if (token->type == isc_tokentype_string &&
	    expect == isc_tokentype_qstring)
		return (ISC_R_SUCCESS);
	if (token->type != expect) {
		isc_lex_ungettoken(lex, token);
		if (token->type == isc_tokentype_eol ||
		    token->type == isc_tokentype_eof)
			return (ISC_R_UNEXPECTEDEND);
		return (ISC_R_UNEXPECTEDTOKEN);
	}
	return (ISC_R_SUCCESS);
}

Bob Halley's avatar
add lex    
Bob Halley committed
764
765
766
767
768
769
770
771
772
773
774
void
isc_lex_ungettoken(isc_lex_t *lex, isc_token_t *tokenp) {
	inputsource *source;
	/*
	 * Unget the current token.
	 */

	REQUIRE(VALID_LEX(lex));
	source = HEAD(lex->sources);
	REQUIRE(source != NULL);
	REQUIRE(tokenp != NULL);
775
776
777
	REQUIRE(isc_buffer_consumedlength(source->pushback) != 0);

	UNUSED(tokenp);
778

779
	isc_buffer_first(source->pushback);
Bob Halley's avatar
add lex    
Bob Halley committed
780
}
Mark Andrews's avatar
Mark Andrews committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794

char *
isc_lex_getsourcename(isc_lex_t *lex) {
	inputsource *source;

	REQUIRE(VALID_LEX(lex));
	source = HEAD(lex->sources);

	if (source == NULL)
		return(NULL);

	return (source->name);
}

David Lawrence's avatar
David Lawrence committed
795
unsigned long
Mark Andrews's avatar
Mark Andrews committed
796
797
798
799
800
801
802
803
804
805
806
isc_lex_getsourceline(isc_lex_t *lex) {
	inputsource *source;

	REQUIRE(VALID_LEX(lex));
	source = HEAD(lex->sources);

	if (source == NULL)
		return(0);

	return (source->line);
}
Mark Andrews's avatar
Mark Andrews committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820

isc_boolean_t
isc_lex_isfile(isc_lex_t *lex) {
	inputsource *source;

	REQUIRE(VALID_LEX(lex));

	source = HEAD(lex->sources);

	if (source == NULL)
		return (ISC_FALSE);

	return (source->is_file);
}