parser.c 57.1 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2003  Internet Software Consortium.
4 5 6 7 8
 *
 * 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.
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
16 17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id: parser.c,v 1.122 2006/02/19 06:50:48 marka Exp $ */
19 20

/*! \file */
21 22 23 24 25

#include <config.h>

#include <isc/buffer.h>
#include <isc/dir.h>
26
#include <isc/formatcheck.h>
27 28 29 30 31
#include <isc/lex.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/net.h>
#include <isc/netaddr.h>
32
#include <isc/print.h>
33 34
#include <isc/string.h>
#include <isc/sockaddr.h>
Mark Andrews's avatar
Mark Andrews committed
35
#include <isc/netscope.h>
36 37 38 39
#include <isc/util.h>
#include <isc/symtab.h>

#include <isccfg/cfg.h>
40
#include <isccfg/grammar.h>
41
#include <isccfg/log.h>
42

43
/* Shorthand */
44 45
#define CAT CFG_LOGCATEGORY_CONFIG
#define MOD CFG_LOGMODULE_PARSER
46 47 48

#define MAP_SYM 1 	/* Unique type for isc_symtab */

49 50
#define TOKEN_STRING(pctx) (pctx->token.value.as_textregion.base)

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* Check a return value. */
#define CHECK(op) 						\
     	do { result = (op); 					\
		if (result != ISC_R_SUCCESS) goto cleanup; 	\
	} while (0)

/* Clean up a configuration object if non-NULL. */
#define CLEANUP_OBJ(obj) \
	do { if ((obj) != NULL) cfg_obj_destroy(pctx, &(obj)); } while (0)


/*
 * Forward declarations of static functions.
 */

static void
67
free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj);
68

69
static isc_result_t
70
parse_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
71 72 73 74 75

static void
print_list(cfg_printer_t *pctx, cfg_obj_t *obj);

static void
76
free_list(cfg_parser_t *pctx, cfg_obj_t *obj);
77

78
static isc_result_t
79
create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp);
80 81

static isc_result_t
82 83
create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
	      cfg_obj_t **ret);
84 85

static void
86
free_string(cfg_parser_t *pctx, cfg_obj_t *obj);
87

88
static isc_result_t
89
create_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
90 91

static void
92
free_map(cfg_parser_t *pctx, cfg_obj_t *obj);
93 94 95

static isc_result_t
parse_symtab_elt(cfg_parser_t *pctx, const char *name,
96 97
		 cfg_type_t *elttype, isc_symtab_t *symtab,
		 isc_boolean_t callback);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

static void
free_noop(cfg_parser_t *pctx, cfg_obj_t *obj);

static isc_result_t
cfg_getstringtoken(cfg_parser_t *pctx);

static void
parser_complain(cfg_parser_t *pctx, isc_boolean_t is_warning,
		unsigned int flags, const char *format, va_list args);

/*
 * Data representations.  These correspond to members of the
 * "value" union in struct cfg_obj (except "void", which does
 * not need a union member).
 */
114

115
cfg_rep_t cfg_rep_uint32 = { "uint32", free_noop };
116
cfg_rep_t cfg_rep_uint64 = { "uint64", free_noop };
117 118 119 120 121 122 123 124 125 126 127 128 129
cfg_rep_t cfg_rep_string = { "string", free_string };
cfg_rep_t cfg_rep_boolean = { "boolean", free_noop };
cfg_rep_t cfg_rep_map = { "map", free_map };
cfg_rep_t cfg_rep_list = { "list", free_list };
cfg_rep_t cfg_rep_tuple = { "tuple", free_tuple };
cfg_rep_t cfg_rep_sockaddr = { "sockaddr", free_noop };
cfg_rep_t cfg_rep_netprefix = { "netprefix", free_noop };
cfg_rep_t cfg_rep_void = { "void", free_noop };

/*
 * Configuration type definitions.
 */

130
/*%
131 132 133
 * An implicit list.  These are formed by clauses that occur multiple times.
 */
static cfg_type_t cfg_type_implicitlist = {
134
	"implicitlist", NULL, print_list, NULL, &cfg_rep_list, NULL };
135 136 137

/* Functions. */

138 139
void
cfg_print_obj(cfg_printer_t *pctx, cfg_obj_t *obj) {
140 141 142
	obj->type->print(pctx, obj);
}

143 144
void
cfg_print_chars(cfg_printer_t *pctx, const char *text, int len) {
145 146 147 148 149
	pctx->f(pctx->closure, text, len);
}

static void
print_open(cfg_printer_t *pctx) {
150
	cfg_print_chars(pctx, "{\n", 2);
151 152 153 154 155 156 157
	pctx->indent++;
}

static void
print_indent(cfg_printer_t *pctx) {
	int indent = pctx->indent;
	while (indent > 0) {
158
		cfg_print_chars(pctx, "\t", 1);
159 160 161 162 163 164 165 166
		indent--;
	}
}

static void
print_close(cfg_printer_t *pctx) {
	pctx->indent--;
	print_indent(pctx);
167
	cfg_print_chars(pctx, "}", 1);
168 169
}

170 171
isc_result_t
cfg_parse_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
172 173 174 175 176 177 178
	isc_result_t result;
	INSIST(ret != NULL && *ret == NULL);
	result = type->parse(pctx, type, ret);
	if (result != ISC_R_SUCCESS)
		return (result);
	INSIST(*ret != NULL);
	return (ISC_R_SUCCESS);
179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

void
cfg_print(cfg_obj_t *obj,
	  void (*f)(void *closure, const char *text, int textlen),
	  void *closure)
{
	cfg_printer_t pctx;
	pctx.f = f;
	pctx.closure = closure;
	pctx.indent = 0;
	obj->type->print(&pctx, obj);
}

193 194 195

/* Tuples. */
  
196 197
isc_result_t
cfg_create_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
198
	isc_result_t result;
199 200
	const cfg_tuplefielddef_t *fields = type->of;
	const cfg_tuplefielddef_t *f;
201 202 203
	cfg_obj_t *obj = NULL;
	unsigned int nfields = 0;
	int i;
204

205
	for (f = fields; f->name != NULL; f++)
206 207
		nfields++;

208
	CHECK(cfg_create_obj(pctx, type, &obj));
209 210
	obj->value.tuple = isc_mem_get(pctx->mctx,
				       nfields * sizeof(cfg_obj_t *));
211 212 213 214
	if (obj->value.tuple == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
215
	for (f = fields, i = 0; f->name != NULL; f++, i++)
216
		obj->value.tuple[i] = NULL;
217 218 219 220
	*ret = obj;
	return (ISC_R_SUCCESS);

 cleanup:
221 222
	if (obj != NULL)
		isc_mem_put(pctx->mctx, obj, sizeof(*obj));
223 224 225
	return (result);
}

226 227
isc_result_t
cfg_parse_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
228 229
{
	isc_result_t result;
230 231
	const cfg_tuplefielddef_t *fields = type->of;
	const cfg_tuplefielddef_t *f;
232 233
	cfg_obj_t *obj = NULL;
	unsigned int i;
234

235
	CHECK(cfg_create_tuple(pctx, type, &obj));
236
	for (f = fields, i = 0; f->name != NULL; f++, i++)
237
		CHECK(cfg_parse_obj(pctx, f->type, &obj->value.tuple[i]));
238 239 240 241 242 243 244 245 246

	*ret = obj;
	return (ISC_R_SUCCESS);

 cleanup:
	CLEANUP_OBJ(obj);
	return (result);
}

247 248
void
cfg_print_tuple(cfg_printer_t *pctx, cfg_obj_t *obj) {
249
	unsigned int i;
250 251
	const cfg_tuplefielddef_t *fields = obj->type->of;
	const cfg_tuplefielddef_t *f;
252
	isc_boolean_t need_space = ISC_FALSE;
253

254
	for (f = fields, i = 0; f->name != NULL; f++, i++) {
255 256
		cfg_obj_t *fieldobj = obj->value.tuple[i];
		if (need_space)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
			cfg_print_chars(pctx, " ", 1);
		cfg_print_obj(pctx, fieldobj);
		need_space = ISC_TF(fieldobj->type->print != cfg_print_void);
	}
}

void
cfg_doc_tuple(cfg_printer_t *pctx, const cfg_type_t *type) {
	const cfg_tuplefielddef_t *fields = type->of;
	const cfg_tuplefielddef_t *f;
	isc_boolean_t need_space = ISC_FALSE;

	for (f = fields; f->name != NULL; f++) {
		if (need_space)
			cfg_print_chars(pctx, " ", 1);
		cfg_doc_obj(pctx, f->type);
		need_space = ISC_TF(f->type->print != cfg_print_void);
274 275 276 277 278 279
	}
}

static void
free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj) {
	unsigned int i;
280 281
	const cfg_tuplefielddef_t *fields = obj->type->of;
	const cfg_tuplefielddef_t *f;
282 283
	unsigned int nfields = 0;

284 285 286
	if (obj->value.tuple == NULL)
		return;

287
	for (f = fields, i = 0; f->name != NULL; f++, i++) {
288 289 290 291 292 293 294
		CLEANUP_OBJ(obj->value.tuple[i]);
		nfields++;
	}
	isc_mem_put(pctx->mctx, obj->value.tuple,
		    nfields * sizeof(cfg_obj_t *));
}

295 296 297 298 299 300
isc_boolean_t
cfg_obj_istuple(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_tuple));
}

301 302 303
cfg_obj_t *
cfg_tuple_get(cfg_obj_t *tupleobj, const char* name) {
	unsigned int i;
304 305
	const cfg_tuplefielddef_t *fields;
	const cfg_tuplefielddef_t *f;
306 307 308 309 310 311 312 313 314 315 316 317
	
	REQUIRE(tupleobj != NULL && tupleobj->type->rep == &cfg_rep_tuple);

	fields = tupleobj->type->of;
	for (f = fields, i = 0; f->name != NULL; f++, i++) {
		if (strcmp(f->name, name) == 0)
			return (tupleobj->value.tuple[i]);
	}
	INSIST(0);
	return (NULL);
}

318 319
isc_result_t
cfg_parse_special(cfg_parser_t *pctx, int special) {
320 321 322 323 324 325
        isc_result_t result;
	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type == isc_tokentype_special &&
	    pctx->token.value.as_char == special)
		return (ISC_R_SUCCESS);

326
	cfg_parser_error(pctx, CFG_LOG_NEAR, "'%c' expected", special);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	return (ISC_R_UNEXPECTEDTOKEN);
 cleanup:
	return (result);
}

/*
 * Parse a required semicolon.  If it is not there, log
 * an error and increment the error count but continue
 * parsing.  Since the next token is pushed back,
 * care must be taken to make sure it is eventually
 * consumed or an infinite loop may result.
 */
static isc_result_t
parse_semicolon(cfg_parser_t *pctx) {
        isc_result_t result;
	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type == isc_tokentype_special &&
	    pctx->token.value.as_char == ';')
		return (ISC_R_SUCCESS);

347
	cfg_parser_error(pctx, CFG_LOG_BEFORE, "missing ';'");
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	cfg_ungettoken(pctx);
 cleanup:
	return (result);
}

/*
 * Parse EOF, logging and returning an error if not there.
 */
static isc_result_t
parse_eof(cfg_parser_t *pctx) {
        isc_result_t result;
	CHECK(cfg_gettoken(pctx, 0));

	if (pctx->token.type == isc_tokentype_eof)
		return (ISC_R_SUCCESS);

364
	cfg_parser_error(pctx, CFG_LOG_NEAR, "syntax error");
365 366
	return (ISC_R_UNEXPECTEDTOKEN);
 cleanup:
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
367
	return (result);
368 369 370
}

/* A list of files, used internally for pctx->files. */
371

372
static cfg_type_t cfg_type_filelist = {
373
	"filelist", NULL, print_list, NULL, &cfg_rep_list,
374 375 376 377
	&cfg_type_qstring
};

isc_result_t
378
cfg_parser_create(isc_mem_t *mctx, isc_log_t *lctx, cfg_parser_t **ret) {
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	isc_result_t result;
	cfg_parser_t *pctx;
	isc_lexspecials_t specials;

	REQUIRE(mctx != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

	pctx = isc_mem_get(mctx, sizeof(*pctx));
	if (pctx == NULL)
		return (ISC_R_NOMEMORY);

	pctx->mctx = mctx;
	pctx->lctx = lctx;
	pctx->lexer = NULL;
	pctx->seen_eof = ISC_FALSE;
	pctx->ungotten = ISC_FALSE;
	pctx->errors = 0;
Mark Andrews's avatar
Mark Andrews committed
396
	pctx->warnings = 0;
397 398
	pctx->open_files = NULL;
	pctx->closed_files = NULL;
399
	pctx->line = 0;
400 401
	pctx->callback = NULL;
	pctx->callbackarg = NULL;
Mark Andrews's avatar
Mark Andrews committed
402
	pctx->token.type = isc_tokentype_unknown;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

	memset(specials, 0, sizeof(specials));
	specials['{'] = 1;
	specials['}'] = 1;
	specials[';'] = 1;
	specials['/'] = 1;
	specials['"'] = 1;
	specials['!'] = 1;

	CHECK(isc_lex_create(pctx->mctx, 1024, &pctx->lexer));

	isc_lex_setspecials(pctx->lexer, specials);
	isc_lex_setcomments(pctx->lexer, (ISC_LEXCOMMENT_C |
					 ISC_LEXCOMMENT_CPLUSPLUS |
					 ISC_LEXCOMMENT_SHELL));

419 420
	CHECK(cfg_create_list(pctx, &cfg_type_filelist, &pctx->open_files));
	CHECK(cfg_create_list(pctx, &cfg_type_filelist, &pctx->closed_files));
421 422 423 424 425 426 427

	*ret = pctx;
	return (ISC_R_SUCCESS);

 cleanup:
	if (pctx->lexer != NULL)
		isc_lex_destroy(&pctx->lexer);
428 429
	CLEANUP_OBJ(pctx->open_files);
	CLEANUP_OBJ(pctx->closed_files);
430 431 432 433 434
	isc_mem_put(mctx, pctx, sizeof(*pctx));
	return (result);
}

static isc_result_t
435
parser_openfile(cfg_parser_t *pctx, const char *filename) {
436 437 438 439 440 441
	isc_result_t result;
	cfg_listelt_t *elt = NULL;
	cfg_obj_t *stringobj = NULL;

	result = isc_lex_openfile(pctx->lexer, filename);
	if (result != ISC_R_SUCCESS) {
442
		cfg_parser_error(pctx, 0, "open: %s: %s",
443 444 445 446 447 448 449
			     filename, isc_result_totext(result));
		goto cleanup;
	}

	CHECK(create_string(pctx, filename, &cfg_type_qstring, &stringobj));
	CHECK(create_listelt(pctx, &elt));
	elt->obj = stringobj;
450
	ISC_LIST_APPEND(pctx->open_files->value.list, elt, link);
451 452 453 454 455 456 457

	return (ISC_R_SUCCESS);
 cleanup:
	CLEANUP_OBJ(stringobj);
	return (result);
}

458 459 460 461 462 463 464 465 466
void
cfg_parser_setcallback(cfg_parser_t *pctx,
		       cfg_parsecallback_t callback,
		       void *arg)
{
	pctx->callback = callback;
	pctx->callbackarg = arg;
}

467 468 469 470 471
/*
 * Parse a configuration using a pctx where a lexer has already
 * been set up with a source.
 */
static isc_result_t
472
parse2(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
473 474 475
	isc_result_t result;
	cfg_obj_t *obj = NULL;

476
	result = cfg_parse_obj(pctx, type, &obj);
477 478 479 480 481 482 483 484 485 486

	if (pctx->errors != 0) {
		/* Errors have been logged. */
		if (result == ISC_R_SUCCESS)
			result = ISC_R_FAILURE;
		goto cleanup;
	}

	if (result != ISC_R_SUCCESS) {
		/* Parsing failed but no errors have been logged. */
487
		cfg_parser_error(pctx, 0, "parsing failed");
488 489 490 491 492 493 494 495 496 497 498 499 500
		goto cleanup;
	}

	CHECK(parse_eof(pctx));

	*ret = obj;
	return (ISC_R_SUCCESS);

 cleanup:
	CLEANUP_OBJ(obj);
	return (result);
}

501 502
isc_result_t
cfg_parse_file(cfg_parser_t *pctx, const char *filename,
503
	       const cfg_type_t *type, cfg_obj_t **ret)
504 505 506 507 508 509 510 511 512 513 514 515 516 517
{
	isc_result_t result;

	REQUIRE(filename != NULL);

	CHECK(parser_openfile(pctx, filename));
	CHECK(parse2(pctx, type, ret));
 cleanup:
	return (result);
}


isc_result_t
cfg_parse_buffer(cfg_parser_t *pctx, isc_buffer_t *buffer,
518
	const cfg_type_t *type, cfg_obj_t **ret)
519 520 521 522 523 524 525 526 527
{
	isc_result_t result;
	REQUIRE(buffer != NULL);
	CHECK(isc_lex_openbuffer(pctx->lexer, buffer));	
	CHECK(parse2(pctx, type, ret));
 cleanup:
	return (result);
}

528 529 530 531
void
cfg_parser_destroy(cfg_parser_t **pctxp) {
	cfg_parser_t *pctx = *pctxp;
	isc_lex_destroy(&pctx->lexer);
532 533 534 535 536 537 538
	/*
	 * Cleaning up open_files does not
	 * close the files; that was already done
	 * by closing the lexer.
	 */
	CLEANUP_OBJ(pctx->open_files);
	CLEANUP_OBJ(pctx->closed_files);
539 540 541 542 543 544 545
	isc_mem_put(pctx->mctx, pctx, sizeof(*pctx));
	*pctxp = NULL;
}

/*
 * void
 */
546 547
isc_result_t
cfg_parse_void(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
548
	UNUSED(type);
549
	return (cfg_create_obj(pctx, &cfg_type_void, ret));
550 551
}

552 553
void
cfg_print_void(cfg_printer_t *pctx, cfg_obj_t *obj) {
554 555 556
	UNUSED(pctx);
	UNUSED(obj);
}
557

558 559 560 561 562 563
void
cfg_doc_void(cfg_printer_t *pctx, const cfg_type_t *type) {
	UNUSED(pctx);
	UNUSED(type);
}

564 565 566 567 568 569
isc_boolean_t
cfg_obj_isvoid(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_void));
}

570 571 572
cfg_type_t cfg_type_void = {
	"void", cfg_parse_void, cfg_print_void, cfg_doc_void, &cfg_rep_void,
	NULL };
573 574 575 576 577


/*
 * uint32
 */
578 579
isc_result_t
cfg_parse_uint32(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
580 581 582 583
        isc_result_t result;
	cfg_obj_t *obj = NULL;
	UNUSED(type);

584
	CHECK(cfg_gettoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
585
	if (pctx->token.type != isc_tokentype_number) {
586
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected number");
587 588 589
		return (ISC_R_UNEXPECTEDTOKEN);
	}

590
	CHECK(cfg_create_obj(pctx, &cfg_type_uint32, &obj));
591 592 593 594 595 596 597

	obj->value.uint32 = pctx->token.value.as_ulong;
	*ret = obj;
 cleanup:
	return (result);
}

598 599 600
void
cfg_print_cstr(cfg_printer_t *pctx, const char *s) {
	cfg_print_chars(pctx, s, strlen(s));
601 602
}

603 604
void
cfg_print_rawuint(cfg_printer_t *pctx, unsigned int u) {
605
	char buf[32];
606
	snprintf(buf, sizeof(buf), "%u", u);
607
	cfg_print_cstr(pctx, buf);
608 609
}

610 611 612
void
cfg_print_uint32(cfg_printer_t *pctx, cfg_obj_t *obj) {
	cfg_print_rawuint(pctx, obj->value.uint32);
613 614
}

615 616 617 618 619 620
isc_boolean_t
cfg_obj_isuint32(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_uint32));
}

621 622 623 624 625 626
isc_uint32_t
cfg_obj_asuint32(cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_uint32);
	return (obj->value.uint32);
}

627 628 629 630
cfg_type_t cfg_type_uint32 = {
	"integer", cfg_parse_uint32, cfg_print_uint32, cfg_doc_terminal,
	&cfg_rep_uint32, NULL
};
631 632


633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
/*
 * uint64
 */
isc_boolean_t
cfg_obj_isuint64(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_uint64));
}

isc_uint64_t
cfg_obj_asuint64(cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_uint64);
	return (obj->value.uint64);
}

648 649
void
cfg_print_uint64(cfg_printer_t *pctx, cfg_obj_t *obj) {
650
	char buf[32];
651 652
	snprintf(buf, sizeof(buf), "%" ISC_PRINT_QUADFORMAT "u",
		 obj->value.uint64);
653
	cfg_print_cstr(pctx, buf);
654 655
}

656 657 658
cfg_type_t cfg_type_uint64 = {
	"64_bit_integer", NULL, cfg_print_uint64, cfg_doc_terminal,
	&cfg_rep_uint64, NULL
659 660
};

661
/*
662 663
 * qstring (quoted string), ustring (unquoted string), astring
 * (any string)
664 665 666 667
 */

/* Create a string object from a null-terminated C string. */
static isc_result_t
668
create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
669 670 671 672 673 674
	      cfg_obj_t **ret)
{
	isc_result_t result;
	cfg_obj_t *obj = NULL;
	int len;

675
	CHECK(cfg_create_obj(pctx, type, &obj));
676 677 678 679
	len = strlen(contents);
	obj->value.string.length = len;
	obj->value.string.base = isc_mem_get(pctx->mctx, len + 1);
	if (obj->value.string.base == 0) {
680
		isc_mem_put(pctx->mctx, obj, sizeof(*obj));
681 682 683 684 685 686 687 688 689 690
		return (ISC_R_NOMEMORY);
	}
	memcpy(obj->value.string.base, contents, len);
	obj->value.string.base[len] = '\0';

	*ret = obj;
 cleanup:
	return (result);
}

691 692
isc_result_t
cfg_parse_qstring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
693 694 695
        isc_result_t result;
	UNUSED(type);

696
	CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
697
	if (pctx->token.type != isc_tokentype_qstring) {
698
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected quoted string");
699 700 701
		return (ISC_R_UNEXPECTEDTOKEN);
	}
	return (create_string(pctx,
702
			      TOKEN_STRING(pctx),
703 704 705 706 707 708 709
			      &cfg_type_qstring,
			      ret));
 cleanup:
	return (result);
}

static isc_result_t
710
parse_ustring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
711 712 713 714 715
        isc_result_t result;
	UNUSED(type);

	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type != isc_tokentype_string) {
716
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected unquoted string");
717 718 719
		return (ISC_R_UNEXPECTEDTOKEN);
	}
	return (create_string(pctx,
720
			      TOKEN_STRING(pctx),
721 722 723 724 725 726
			      &cfg_type_ustring,
			      ret));
 cleanup:
	return (result);
}

727 728
isc_result_t
cfg_parse_astring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
729 730 731 732 733
        isc_result_t result;
	UNUSED(type);

	CHECK(cfg_getstringtoken(pctx));
	return (create_string(pctx,
734
			      TOKEN_STRING(pctx),
735 736 737 738 739 740
			      &cfg_type_qstring,
			      ret));
 cleanup:
	return (result);
}

741 742
isc_boolean_t
cfg_is_enum(const char *s, const char *const *enums) {
743
	const char * const *p;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
744 745
	for (p = enums; *p != NULL; p++) {
		if (strcasecmp(*p, s) == 0)
746
			return (ISC_TRUE);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
747
	}
748 749 750 751
	return (ISC_FALSE);
}

static isc_result_t
752
check_enum(cfg_parser_t *pctx, cfg_obj_t *obj, const char *const *enums) {
753
	const char *s = obj->value.string.base;
754
	if (cfg_is_enum(s, enums))
755
		return (ISC_R_SUCCESS);
756
	cfg_parser_error(pctx, 0, "'%s' unexpected", s);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
757 758 759
	return (ISC_R_UNEXPECTEDTOKEN);
}

760 761
isc_result_t
cfg_parse_enum(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
762 763 764 765 766 767 768 769 770 771 772
        isc_result_t result;
	cfg_obj_t *obj = NULL;
	CHECK(parse_ustring(pctx, NULL, &obj));
	CHECK(check_enum(pctx, obj, type->of));
	*ret = obj;
	return (ISC_R_SUCCESS);
 cleanup:
	CLEANUP_OBJ(obj);	
	return (result);
}

773 774 775 776 777 778 779 780
void
cfg_doc_enum(cfg_printer_t *pctx, const cfg_type_t *type) {
	const char * const *p;
	cfg_print_chars(pctx, "( ", 2);
	for (p = type->of; *p != NULL; p++) {
		cfg_print_cstr(pctx, *p);
		if (p[1] != NULL)
			cfg_print_chars(pctx, " | ", 3);
781
	}
782
	cfg_print_chars(pctx, " )", 2);
783 784
}

785 786 787
void
cfg_print_ustring(cfg_printer_t *pctx, cfg_obj_t *obj) {
	cfg_print_chars(pctx, obj->value.string.base, obj->value.string.length);
788 789 790 791
}

static void
print_qstring(cfg_printer_t *pctx, cfg_obj_t *obj) {
792 793 794
	cfg_print_chars(pctx, "\"", 1);
	cfg_print_ustring(pctx, obj);
	cfg_print_chars(pctx, "\"", 1);
795 796 797 798
}

static void
free_string(cfg_parser_t *pctx, cfg_obj_t *obj) {
799 800
	isc_mem_put(pctx->mctx, obj->value.string.base,
		    obj->value.string.length + 1);
801 802
}

803 804 805 806 807 808
isc_boolean_t
cfg_obj_isstring(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_string));
}

809
const char *
810 811 812 813 814
cfg_obj_asstring(cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_string);
	return (obj->value.string.base);
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/* Quoted string only */
cfg_type_t cfg_type_qstring = {
	"quoted_string", cfg_parse_qstring, print_qstring, cfg_doc_terminal,
	&cfg_rep_string, NULL
};

/* Unquoted string only */
cfg_type_t cfg_type_ustring = {
	"string", parse_ustring, cfg_print_ustring, cfg_doc_terminal,
	&cfg_rep_string, NULL
};

/* Any string (quoted or unquoted); printed with quotes */
cfg_type_t cfg_type_astring = {
	"string", cfg_parse_astring, print_qstring, cfg_doc_terminal,
	&cfg_rep_string, NULL
};

/*
 * Booleans
 */

837 838 839 840 841 842
isc_boolean_t
cfg_obj_isboolean(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_boolean));
}

Brian Wellington's avatar
Brian Wellington committed
843 844 845 846 847 848
isc_boolean_t
cfg_obj_asboolean(cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_boolean);
	return (obj->value.boolean);
}

849
static isc_result_t
850
parse_boolean(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
851
{
852 853 854 855 856 857 858 859 860 861
        isc_result_t result;
	isc_boolean_t value;
	cfg_obj_t *obj = NULL;
	UNUSED(type);

	result = cfg_gettoken(pctx, 0);
	if (result != ISC_R_SUCCESS)
		return (result);

	if (pctx->token.type != isc_tokentype_string)
862
		goto bad_boolean;
863

864 865 866
	if ((strcasecmp(TOKEN_STRING(pctx), "true") == 0) ||
	    (strcasecmp(TOKEN_STRING(pctx), "yes") == 0) ||
	    (strcmp(TOKEN_STRING(pctx), "1") == 0)) {
867
		value = ISC_TRUE;
868 869 870
	} else if ((strcasecmp(TOKEN_STRING(pctx), "false") == 0) ||
		   (strcasecmp(TOKEN_STRING(pctx), "no") == 0) ||
		   (strcmp(TOKEN_STRING(pctx), "0") == 0)) {
871 872 873 874 875
		value = ISC_FALSE;
	} else {
		goto bad_boolean;
	}

876
	CHECK(cfg_create_obj(pctx, &cfg_type_boolean, &obj));
877 878 879 880 881
	obj->value.boolean = value;
	*ret = obj;
	return (result);

 bad_boolean:
882
	cfg_parser_error(pctx, CFG_LOG_NEAR, "boolean expected");
883 884 885 886 887 888 889 890 891
	return (ISC_R_UNEXPECTEDTOKEN);

 cleanup:
	return (result);
}

static void
print_boolean(cfg_printer_t *pctx, cfg_obj_t *obj) {
	if (obj->value.boolean)
892
		cfg_print_chars(pctx, "yes", 3);
893
	else
894
		cfg_print_chars(pctx, "no", 2);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
895
}
896

897 898 899
cfg_type_t cfg_type_boolean = {
	"boolean", parse_boolean, print_boolean, cfg_doc_terminal,
	&cfg_rep_boolean, NULL
900 901
};

902
/*
903
 * Lists.
904 905
 */

906 907
isc_result_t
cfg_create_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **obj) {
908
	isc_result_t result;
909
	CHECK(cfg_create_obj(pctx, type, obj));
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
	ISC_LIST_INIT((*obj)->value.list);
 cleanup:
	return (result);
}

static isc_result_t
create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp) {
	cfg_listelt_t *elt;
	elt = isc_mem_get(pctx->mctx, sizeof(*elt));
	if (elt == NULL)
		return (ISC_R_NOMEMORY);
	elt->obj = NULL;
	ISC_LINK_INIT(elt, link);
	*eltp = elt;
	return (ISC_R_SUCCESS);
}

927 928 929 930 931 932
static void
free_list_elt(cfg_parser_t *pctx, cfg_listelt_t *elt) {
	cfg_obj_destroy(pctx, &elt->obj);
	isc_mem_put(pctx->mctx, elt, sizeof(*elt));
}

933 934 935 936 937 938 939 940
static void
free_list(cfg_parser_t *pctx, cfg_obj_t *obj) {
	cfg_listelt_t *elt, *next;
	for (elt = ISC_LIST_HEAD(obj->value.list);
	     elt != NULL;
	     elt = next)
	{
		next = ISC_LIST_NEXT(elt, link);
941
		free_list_elt(pctx, elt);
942 943 944
	}
}

945 946 947
isc_result_t
cfg_parse_listelt(cfg_parser_t *pctx, const cfg_type_t *elttype,
		  cfg_listelt_t **ret)
948 949 950 951 952 953 954
{
	isc_result_t result;
	cfg_listelt_t *elt = NULL;
	cfg_obj_t *value = NULL;

	CHECK(create_listelt(pctx, &elt));

955
	result = cfg_parse_obj(pctx, elttype, &value);
956 957 958 959 960 961 962 963 964 965 966 967 968
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	elt->obj = value;

	*ret = elt;
	return (ISC_R_SUCCESS);

 cleanup:
	isc_mem_put(pctx->mctx, elt, sizeof(*elt));
	return (result);
}

969 970 971 972
/*
 * Parse a homogeneous list whose elements are of type 'elttype'
 * and where each element is terminated by a semicolon.
 */
973
static isc_result_t
974
parse_list(cfg_parser_t *pctx, const cfg_type_t *listtype, cfg_obj_t **ret)
975 976
{
	cfg_obj_t *listobj = NULL;
977
	const cfg_type_t *listof = listtype->of;
978
	isc_result_t result;
979
	cfg_listelt_t *elt = NULL;
980

981
	CHECK(cfg_create_list(pctx, listtype, &listobj));
982 983 984 985

	for (;;) {
		CHECK(cfg_peektoken(pctx, 0));
		if (pctx->token.type == isc_tokentype_special &&
986
		    pctx->token.value.as_char == /*{*/ '}')
987
			break;
988
		CHECK(cfg_parse_listelt(pctx, listof, &elt));
989 990
		CHECK(parse_semicolon(pctx));
		ISC_LIST_APPEND(listobj->value.list, elt, link);
991
		elt = NULL;
992 993 994 995 996
	}
	*ret = listobj;
	return (ISC_R_SUCCESS);

 cleanup:
997 998
	if (elt != NULL)
		free_list_elt(pctx, elt);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	CLEANUP_OBJ(listobj);
	return (result);
}

static void
print_list(cfg_printer_t *pctx, cfg_obj_t *obj) {
	cfg_list_t *list = &obj->value.list;
	cfg_listelt_t *elt;

	for (elt = ISC_LIST_HEAD(*list);
	     elt != NULL;
	     elt = ISC_LIST_NEXT(elt, link)) {
		print_indent(pctx);
1012 1013
		cfg_print_obj(pctx, elt->obj);
		cfg_print_chars(pctx, ";\n", 2);
1014 1015 1016
	}
}

1017 1018 1019
isc_result_t
cfg_parse_bracketed_list(cfg_parser_t *pctx, const cfg_type_t *type,
		     cfg_obj_t **ret)
1020 1021
{
	isc_result_t result;
1022
	CHECK(cfg_parse_special(pctx, '{'));
1023
	CHECK(parse_list(pctx, type, ret));
1024
	CHECK(cfg_parse_special(pctx, '}'));
1025 1026 1027 1028
 cleanup:
	return (result);
}

1029 1030
void
cfg_print_bracketed_list(cfg_printer_t *pctx, cfg_obj_t *obj) {
1031 1032 1033 1034 1035
	print_open(pctx);
	print_list(pctx, obj);
	print_close(pctx);
}

1036 1037 1038 1039 1040 1041 1042
void
cfg_doc_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type) {
	cfg_print_chars(pctx, "{ ", 2);
	cfg_doc_obj(pctx, type->of);
	cfg_print_chars(pctx, "; ... }", 7);
}

1043 1044 1045 1046 1047
/*
 * Parse a homogeneous list whose elements are of type 'elttype'
 * and where elements are separated by space.  The list ends
 * before the first semicolon.
 */
1048 1049 1050
isc_result_t
cfg_parse_spacelist(cfg_parser_t *pctx, const cfg_type_t *listtype,
		    cfg_obj_t **ret)
1051 1052
{
	cfg_obj_t *listobj = NULL;
1053
	const cfg_type_t *listof = listtype->of;
1054 1055
	isc_result_t result;

1056
	CHECK(cfg_create_list(pctx, listtype, &listobj));
1057 1058 1059 1060 1061 1062 1063 1064

	for (;;) {
		cfg_listelt_t *elt = NULL;

		CHECK(cfg_peektoken(pctx, 0));
		if (pctx->token.type == isc_tokentype_special &&
		    pctx->token.value.as_char == ';')
			break;
1065
		CHECK(cfg_parse_listelt(pctx, listof, &elt));
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		ISC_LIST_APPEND(listobj->value.list, elt, link);
	}
	*ret = listobj;
	return (ISC_R_SUCCESS);

 cleanup:
	CLEANUP_OBJ(listobj);
	return (result);
}

1076 1077
void
cfg_print_spacelist(cfg_printer_t *pctx, cfg_obj_t *obj) {
1078 1079 1080 1081 1082 1083
	cfg_list_t *list = &obj->value.list;
	cfg_listelt_t *elt;

	for (elt = ISC_LIST_HEAD(*list);
	     elt != NULL;
	     elt = ISC_LIST_NEXT(elt, link)) {
1084
		cfg_print_obj(pctx, elt->obj);
1085
		if (ISC_LIST_NEXT(elt, link) != NULL)
1086
			cfg_print_chars(pctx, " ", 1);
1087 1088 1089
	}
}

1090 1091 1092 1093 1094 1095
isc_boolean_t
cfg_obj_islist(cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_list));
}

1096 1097
cfg_listelt_t *
cfg_list_first(cfg_obj_t *obj) {
1098 1099 1100
	REQUIRE(obj == NULL || obj->type->rep == &cfg_rep_list);
	if (obj == NULL)
		return (NULL);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
	return (ISC_LIST_HEAD(obj->value.list));
}

cfg_listelt_t *
cfg_list_next(cfg_listelt_t *elt) {
	REQUIRE(elt != NULL);
	return (ISC_LIST_NEXT(elt, link));
}

cfg_obj_t *
cfg_listelt_value(cfg_listelt_t *elt) {
	REQUIRE(elt != NULL);
	return (elt->obj);
}

/*
 * Maps.
 */

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
/*
 * Parse a map body.  That's something like
 *
 *   "foo 1; bar { glub; }; zap true; zap false;"
 *
 * i.e., a sequence of option names followed by values and
 * terminated by semicolons.  Used for the top level of
 * the named.conf syntax, as well as for the body of the
 * options, view, zone, and other statements.
 */
1130 1131
isc_result_t
cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
1132
{
1133
	const cfg_clausedef_t * const *clausesets = type->of;
1134
	isc_result_t result;
1135 1136
	const cfg_clausedef_t * const *clauseset;
	const cfg_clausedef_t *clause;
1137
	cfg_obj_t *value = NULL;
1138
	cfg_obj_t *obj = NULL;
1139 1140 1141 1142
	cfg_obj_t *eltobj = NULL;
	cfg_obj_t *includename = NULL;
	isc_symvalue_t symval;
	cfg_list_t *list = NULL;
1143

1144
	CHECK(create_map(pctx, type, &obj));
1145

1146
	obj->value.map.clausesets = clausesets;
1147
</