parser.c 76.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2000-2018  Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8
 */

9
/*! \file */
10 11 12

#include <config.h>

Evan Hunt's avatar
Evan Hunt committed
13 14
#include <stdlib.h>

15 16
#include <isc/buffer.h>
#include <isc/dir.h>
17
#include <isc/formatcheck.h>
18 19 20 21 22
#include <isc/lex.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/net.h>
#include <isc/netaddr.h>
23
#include <isc/netscope.h>
24
#include <isc/print.h>
25 26 27
#include <isc/string.h>
#include <isc/sockaddr.h>
#include <isc/symtab.h>
28
#include <isc/util.h>
29 30

#include <isccfg/cfg.h>
31
#include <isccfg/grammar.h>
32
#include <isccfg/log.h>
33

34
/* Shorthand */
35 36
#define CAT CFG_LOGCATEGORY_CONFIG
#define MOD CFG_LOGMODULE_PARSER
37 38 39

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

40 41
#define TOKEN_STRING(pctx) (pctx->token.value.as_textregion.base)

42 43
/* Check a return value. */
#define CHECK(op) 						\
Automatic Updater's avatar
Automatic Updater committed
44
	do { result = (op); 					\
45 46 47 48 49 50 51 52 53 54 55 56 57
		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
58
free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj);
59

60
static isc_result_t
61
parse_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
62 63

static void
64
print_list(cfg_printer_t *pctx, const cfg_obj_t *obj);
65 66

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

69
static isc_result_t
70
create_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp);
71 72

static isc_result_t
73 74
create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
	      cfg_obj_t **ret);
75 76

static void
77
free_string(cfg_parser_t *pctx, cfg_obj_t *obj);
78

79
static isc_result_t
80
create_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
81 82

static void
83
free_map(cfg_parser_t *pctx, cfg_obj_t *obj);
84 85 86

static isc_result_t
parse_symtab_elt(cfg_parser_t *pctx, const char *name,
87 88
		 cfg_type_t *elttype, isc_symtab_t *symtab,
		 isc_boolean_t callback);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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).
 */
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_uint32 = { "uint32", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_uint64 = { "uint64", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_string = { "string", free_string };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_boolean = { "boolean", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_map = { "map", free_map };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_list = { "list", free_list };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_tuple = { "tuple", free_tuple };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_sockaddr = { "sockaddr", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_netprefix =
	{ "netprefix", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_void = { "void", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_fixedpoint =
	{ "fixedpoint", free_noop };
LIBISCCFG_EXTERNAL_DATA cfg_rep_t cfg_rep_percentage =
	{ "percentage", free_noop };
121 122 123 124 125

/*
 * Configuration type definitions.
 */

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

/* Functions. */

134
void
135
cfg_print_obj(cfg_printer_t *pctx, const cfg_obj_t *obj) {
136 137 138
	REQUIRE(pctx != NULL);
	REQUIRE(obj != NULL);

139 140 141
	obj->type->print(pctx, obj);
}

142 143
void
cfg_print_chars(cfg_printer_t *pctx, const char *text, int len) {
144 145 146
	REQUIRE(pctx != NULL);
	REQUIRE(text != NULL);

147 148 149 150 151
	pctx->f(pctx->closure, text, len);
}

static void
print_open(cfg_printer_t *pctx) {
152 153 154 155 156 157
	if ((pctx->flags & CFG_PRINTER_ONELINE) != 0)
		cfg_print_cstr(pctx, "{ ");
	else {
		cfg_print_cstr(pctx, "{\n");
		pctx->indent++;
	}
158 159
}

160 161
void
cfg_print_indent(cfg_printer_t *pctx) {
162
	int indent = pctx->indent;
163 164 165 166
	if ((pctx->flags & CFG_PRINTER_ONELINE) != 0) {
		cfg_print_cstr(pctx, " ");
		return;
	}
167
	while (indent > 0) {
168
		cfg_print_cstr(pctx, "\t");
169 170 171 172 173 174
		indent--;
	}
}

static void
print_close(cfg_printer_t *pctx) {
175 176
	if ((pctx->flags & CFG_PRINTER_ONELINE) == 0) {
		pctx->indent--;
177
		cfg_print_indent(pctx);
178 179
	}
	cfg_print_cstr(pctx, "}");
180 181
}

182 183
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
184
	isc_result_t result;
185 186 187 188 189

	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
190 191 192
	result = type->parse(pctx, type, ret);
	if (result != ISC_R_SUCCESS)
		return (result);
193
	ENSURE(*ret != NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
194
	return (ISC_R_SUCCESS);
195 196 197
}

void
198
cfg_print(const cfg_obj_t *obj,
199 200
	  void (*f)(void *closure, const char *text, int textlen),
	  void *closure)
201
{
202 203 204
	REQUIRE(obj != NULL);
	REQUIRE(f != NULL);

205 206 207 208 209 210 211
	cfg_printx(obj, 0, f, closure);
}

void
cfg_printx(const cfg_obj_t *obj, unsigned int flags,
	     void (*f)(void *closure, const char *text, int textlen),
	     void *closure)
212 213
{
	cfg_printer_t pctx;
214 215 216 217

	REQUIRE(obj != NULL);
	REQUIRE(f != NULL);

218 219 220
	pctx.f = f;
	pctx.closure = closure;
	pctx.indent = 0;
221
	pctx.flags = flags;
222 223 224
	obj->type->print(&pctx, obj);
}

225
/* Tuples. */
Automatic Updater's avatar
Automatic Updater committed
226

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

236 237 238 239
	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

240
	for (f = fields; f->name != NULL; f++)
241 242
		nfields++;

243
	CHECK(cfg_create_obj(pctx, type, &obj));
244 245
	obj->value.tuple = isc_mem_get(pctx->mctx,
				       nfields * sizeof(cfg_obj_t *));
246 247 248 249
	if (obj->value.tuple == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}
250
	for (f = fields, i = 0; f->name != NULL; f++, i++)
251
		obj->value.tuple[i] = NULL;
252 253 254 255
	*ret = obj;
	return (ISC_R_SUCCESS);

 cleanup:
256 257
	if (obj != NULL)
		isc_mem_put(pctx->mctx, obj, sizeof(*obj));
258 259 260
	return (result);
}

261 262
isc_result_t
cfg_parse_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret)
263 264
{
	isc_result_t result;
265 266
	const cfg_tuplefielddef_t *fields = type->of;
	const cfg_tuplefielddef_t *f;
267 268
	cfg_obj_t *obj = NULL;
	unsigned int i;
269

270 271 272 273
	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

274
	CHECK(cfg_create_tuple(pctx, type, &obj));
275
	for (f = fields, i = 0; f->name != NULL; f++, i++)
276
		CHECK(cfg_parse_obj(pctx, f->type, &obj->value.tuple[i]));
277 278 279 280 281 282 283 284 285

	*ret = obj;
	return (ISC_R_SUCCESS);

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

286
void
287
cfg_print_tuple(cfg_printer_t *pctx, const cfg_obj_t *obj) {
288
	unsigned int i;
289
	const cfg_tuplefielddef_t *fields;
290
	const cfg_tuplefielddef_t *f;
291
	isc_boolean_t need_space = ISC_FALSE;
292

293 294 295 296 297
	REQUIRE(pctx != NULL);
	REQUIRE(obj != NULL);

	fields = obj->type->of;

298
	for (f = fields, i = 0; f->name != NULL; f++, i++) {
299
		const cfg_obj_t *fieldobj = obj->value.tuple[i];
300
		if (need_space && fieldobj->type->rep != &cfg_rep_void)
301
			cfg_print_cstr(pctx, " ");
302
		cfg_print_obj(pctx, fieldobj);
303 304
		need_space = ISC_TF(need_space ||
				    fieldobj->type->print != cfg_print_void);
305 306 307 308 309
	}
}

void
cfg_doc_tuple(cfg_printer_t *pctx, const cfg_type_t *type) {
310
	const cfg_tuplefielddef_t *fields;
311 312 313
	const cfg_tuplefielddef_t *f;
	isc_boolean_t need_space = ISC_FALSE;

314 315 316 317 318
	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);

	fields = type->of;

319 320
	for (f = fields; f->name != NULL; f++) {
		if (need_space)
321
			cfg_print_cstr(pctx, " ");
322 323
		cfg_doc_obj(pctx, f->type);
		need_space = ISC_TF(f->type->print != cfg_print_void);
324 325 326 327 328 329
	}
}

static void
free_tuple(cfg_parser_t *pctx, cfg_obj_t *obj) {
	unsigned int i;
330 331
	const cfg_tuplefielddef_t *fields = obj->type->of;
	const cfg_tuplefielddef_t *f;
332 333
	unsigned int nfields = 0;

334 335 336
	if (obj->value.tuple == NULL)
		return;

337
	for (f = fields, i = 0; f->name != NULL; f++, i++) {
338 339 340 341 342 343 344
		CLEANUP_OBJ(obj->value.tuple[i]);
		nfields++;
	}
	isc_mem_put(pctx->mctx, obj->value.tuple,
		    nfields * sizeof(cfg_obj_t *));
}

345
isc_boolean_t
346
cfg_obj_istuple(const cfg_obj_t *obj) {
347 348 349 350
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_tuple));
}

351 352
const cfg_obj_t *
cfg_tuple_get(const cfg_obj_t *tupleobj, const char* name) {
353
	unsigned int i;
354 355
	const cfg_tuplefielddef_t *fields;
	const cfg_tuplefielddef_t *f;
Automatic Updater's avatar
Automatic Updater committed
356

357
	REQUIRE(tupleobj != NULL && tupleobj->type->rep == &cfg_rep_tuple);
358
	REQUIRE(name != NULL);
359 360 361 362 363 364 365 366 367 368

	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);
}

369 370
isc_result_t
cfg_parse_special(cfg_parser_t *pctx, int special) {
Automatic Updater's avatar
Automatic Updater committed
371
	isc_result_t result;
372 373 374

	REQUIRE(pctx != NULL);

375 376 377 378 379
	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type == isc_tokentype_special &&
	    pctx->token.value.as_char == special)
		return (ISC_R_SUCCESS);

380
	cfg_parser_error(pctx, CFG_LOG_NEAR, "'%c' expected", special);
381 382 383 384 385 386 387 388 389 390 391 392 393 394
	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) {
Automatic Updater's avatar
Automatic Updater committed
395
	isc_result_t result;
396

397 398 399 400 401
	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type == isc_tokentype_special &&
	    pctx->token.value.as_char == ';')
		return (ISC_R_SUCCESS);

402
	cfg_parser_error(pctx, CFG_LOG_BEFORE, "missing ';'");
403 404 405 406 407 408 409 410 411 412
	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) {
Automatic Updater's avatar
Automatic Updater committed
413
	isc_result_t result;
414

415 416 417 418 419
	CHECK(cfg_gettoken(pctx, 0));

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

420
	cfg_parser_error(pctx, CFG_LOG_NEAR, "syntax error");
421 422
	return (ISC_R_UNEXPECTEDTOKEN);
 cleanup:
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
423
	return (result);
424 425 426
}

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

428
static cfg_type_t cfg_type_filelist = {
429
	"filelist", NULL, print_list, NULL, &cfg_rep_list,
430 431 432 433
	&cfg_type_qstring
};

isc_result_t
434
cfg_parser_create(isc_mem_t *mctx, isc_log_t *lctx, cfg_parser_t **ret) {
435 436 437 438 439 440 441 442 443 444 445
	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);

446 447 448
	pctx->mctx = NULL;
	isc_mem_attach(mctx, &pctx->mctx);

449 450
	result = isc_refcount_init(&pctx->references, 1);
	if (result != ISC_R_SUCCESS) {
451
		isc_mem_putanddetach(&pctx->mctx, pctx, sizeof(*pctx));
452 453 454
		return (result);
	}

455 456 457 458 459
	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
460
	pctx->warnings = 0;
461 462
	pctx->open_files = NULL;
	pctx->closed_files = NULL;
463
	pctx->line = 0;
464 465
	pctx->callback = NULL;
	pctx->callbackarg = NULL;
Mark Andrews's avatar
Mark Andrews committed
466
	pctx->token.type = isc_tokentype_unknown;
Automatic Updater's avatar
Automatic Updater committed
467
	pctx->flags = 0;
468
	pctx->buf_name = NULL;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

	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));

485 486
	CHECK(cfg_create_list(pctx, &cfg_type_filelist, &pctx->open_files));
	CHECK(cfg_create_list(pctx, &cfg_type_filelist, &pctx->closed_files));
487 488 489 490 491 492 493

	*ret = pctx;
	return (ISC_R_SUCCESS);

 cleanup:
	if (pctx->lexer != NULL)
		isc_lex_destroy(&pctx->lexer);
494 495
	CLEANUP_OBJ(pctx->open_files);
	CLEANUP_OBJ(pctx->closed_files);
496
	isc_mem_putanddetach(&pctx->mctx, pctx, sizeof(*pctx));
497 498 499 500
	return (result);
}

static isc_result_t
501
parser_openfile(cfg_parser_t *pctx, const char *filename) {
502 503 504 505 506 507
	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) {
508
		cfg_parser_error(pctx, 0, "open: %s: %s",
509 510 511 512 513 514 515
			     filename, isc_result_totext(result));
		goto cleanup;
	}

	CHECK(create_string(pctx, filename, &cfg_type_qstring, &stringobj));
	CHECK(create_listelt(pctx, &elt));
	elt->obj = stringobj;
516
	ISC_LIST_APPEND(pctx->open_files->value.list, elt, link);
517 518 519 520 521 522 523

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

524 525 526 527 528
void
cfg_parser_setcallback(cfg_parser_t *pctx,
		       cfg_parsecallback_t callback,
		       void *arg)
{
529 530
	REQUIRE(pctx != NULL);

531 532 533 534
	pctx->callback = callback;
	pctx->callbackarg = arg;
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548
void
cfg_parser_reset(cfg_parser_t *pctx) {
	REQUIRE(pctx != NULL);

	if (pctx->lexer != NULL)
		isc_lex_close(pctx->lexer);

	pctx->seen_eof = ISC_FALSE;
	pctx->ungotten = ISC_FALSE;
	pctx->errors = 0;
	pctx->warnings = 0;
	pctx->line = 0;
}

549 550 551 552 553
/*
 * Parse a configuration using a pctx where a lexer has already
 * been set up with a source.
 */
static isc_result_t
554
parse2(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
555 556 557
	isc_result_t result;
	cfg_obj_t *obj = NULL;

558
	result = cfg_parse_obj(pctx, type, &obj);
559 560 561 562 563 564 565 566 567 568

	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. */
569 570
		cfg_parser_error(pctx, 0, "parsing failed: %s",
				 isc_result_totext(result));
571 572 573 574 575 576 577 578 579 580 581 582 583
		goto cleanup;
	}

	CHECK(parse_eof(pctx));

	*ret = obj;
	return (ISC_R_SUCCESS);

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

584 585
isc_result_t
cfg_parse_file(cfg_parser_t *pctx, const char *filename,
586
	       const cfg_type_t *type, cfg_obj_t **ret)
587 588
{
	isc_result_t result;
589
	cfg_listelt_t *elt;
590

591
	REQUIRE(pctx != NULL);
592
	REQUIRE(filename != NULL);
593 594
	REQUIRE(type != NULL);
	REQUIRE(ret != NULL && *ret == NULL);
595 596

	CHECK(parser_openfile(pctx, filename));
597 598 599 600 601 602 603 604 605

	result = parse2(pctx, type, ret);

	/* Clean up the opened file */
	elt = ISC_LIST_TAIL(pctx->open_files->value.list);
	INSIST(elt != NULL);
	ISC_LIST_UNLINK(pctx->open_files->value.list, elt, link);
	ISC_LIST_APPEND(pctx->closed_files->value.list, elt, link);

606 607 608 609 610 611 612
 cleanup:
	return (result);
}


isc_result_t
cfg_parse_buffer(cfg_parser_t *pctx, isc_buffer_t *buffer,
613
	const cfg_type_t *type, cfg_obj_t **ret)
614
{
615
	return (cfg_parse_buffer3(pctx, buffer, NULL, 0, type, ret));
616 617 618 619
}

isc_result_t
cfg_parse_buffer2(cfg_parser_t *pctx, isc_buffer_t *buffer,
620
		  const char *file, const cfg_type_t *type,
621
		  cfg_obj_t **ret)
622 623 624 625 626 627 628 629
{
	return (cfg_parse_buffer3(pctx, buffer, file, 0, type, ret));
}

isc_result_t
cfg_parse_buffer3(cfg_parser_t *pctx, isc_buffer_t *buffer,
		  const char *file, unsigned int line,
		  const cfg_type_t *type, cfg_obj_t **ret)
630 631
{
	isc_result_t result;
632

633 634
	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);
635
	REQUIRE(buffer != NULL);
636
	REQUIRE(ret != NULL && *ret == NULL);
637

Automatic Updater's avatar
Automatic Updater committed
638
	CHECK(isc_lex_openbuffer(pctx->lexer, buffer));
639 640 641 642 643 644

	pctx->buf_name = file;

	if (line != 0U)
		CHECK(isc_lex_setsourceline(pctx->lexer, line));

645
	CHECK(parse2(pctx, type, ret));
646
	pctx->buf_name = NULL;
647

648 649 650 651
 cleanup:
	return (result);
}

652 653 654 655
void
cfg_parser_attach(cfg_parser_t *src, cfg_parser_t **dest) {
	REQUIRE(src != NULL);
	REQUIRE(dest != NULL && *dest == NULL);
656

657 658 659 660
	isc_refcount_increment(&src->references, NULL);
	*dest = src;
}

661 662
void
cfg_parser_destroy(cfg_parser_t **pctxp) {
663
	cfg_parser_t *pctx;
664 665
	unsigned int refs;

666 667 668 669 670
	REQUIRE(pctxp != NULL && *pctxp != NULL);

	pctx = *pctxp;
	*pctxp = NULL;

671 672 673 674 675 676 677 678 679 680
	isc_refcount_decrement(&pctx->references, &refs);
	if (refs == 0) {
		isc_lex_destroy(&pctx->lexer);
		/*
		 * 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);
681
		isc_mem_putanddetach(&pctx->mctx, pctx, sizeof(*pctx));
682
	}
683 684 685 686 687
}

/*
 * void
 */
688 689
isc_result_t
cfg_parse_void(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
690 691 692
	REQUIRE(pctx != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

693
	UNUSED(type);
694

695
	return (cfg_create_obj(pctx, &cfg_type_void, ret));
696 697
}

698
void
699
cfg_print_void(cfg_printer_t *pctx, const cfg_obj_t *obj) {
700 701 702 703

	REQUIRE(pctx != NULL);
	REQUIRE(obj != NULL);

704 705 706
	UNUSED(pctx);
	UNUSED(obj);
}
707

708 709
void
cfg_doc_void(cfg_printer_t *pctx, const cfg_type_t *type) {
710 711 712 713

	REQUIRE(pctx != NULL);
	REQUIRE(type != NULL);

714 715 716 717
	UNUSED(pctx);
	UNUSED(type);
}

718
isc_boolean_t
719
cfg_obj_isvoid(const cfg_obj_t *obj) {
720 721 722 723
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_void));
}

724
LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_void = {
725 726
	"void", cfg_parse_void, cfg_print_void, cfg_doc_void, &cfg_rep_void,
	NULL };
727

728 729 730 731 732 733 734 735 736 737
/*
 * percentage
 */
isc_result_t
cfg_parse_percentage(cfg_parser_t *pctx, const cfg_type_t *type,
		     cfg_obj_t **ret)
{
	char *endp;
	isc_result_t result;
	cfg_obj_t *obj = NULL;
738
	isc_uint64_t percent;
739

740 741 742
	REQUIRE(pctx != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
	UNUSED(type);

	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type != isc_tokentype_string) {
		cfg_parser_error(pctx, CFG_LOG_NEAR,
				 "expected percentage");
		return (ISC_R_UNEXPECTEDTOKEN);
	}

	percent = isc_string_touint64(TOKEN_STRING(pctx), &endp, 10);
	if (*endp != '%' || *(endp+1) != 0) {
		cfg_parser_error(pctx, CFG_LOG_NEAR,
				 "expected percentage");
		return (ISC_R_UNEXPECTEDTOKEN);
	}

	CHECK(cfg_create_obj(pctx, &cfg_type_percentage, &obj));
760
	obj->value.uint32 = (isc_uint32_t)percent;
761 762 763 764 765 766 767 768 769 770 771
	*ret = obj;

 cleanup:
	return (result);
}

void
cfg_print_percentage(cfg_printer_t *pctx, const cfg_obj_t *obj) {
	char buf[64];
	int n;

772 773 774
	REQUIRE(pctx != NULL);
	REQUIRE(obj != NULL);

775 776 777 778 779 780 781 782 783 784 785
	n = snprintf(buf, sizeof(buf), "%u%%", obj->value.uint32);
	INSIST(n > 0 && (size_t)n < sizeof(buf));
	cfg_print_chars(pctx, buf, strlen(buf));
}

isc_uint32_t
cfg_obj_aspercentage(const cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_percentage);
	return (obj->value.uint32);
}

786
LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_percentage = {
787 788 789 790 791 792 793 794 795 796
	"percentage", cfg_parse_percentage, cfg_print_percentage,
	cfg_doc_terminal, &cfg_rep_percentage, NULL
};

isc_boolean_t
cfg_obj_ispercentage(const cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_percentage));
}

Evan Hunt's avatar
Evan Hunt committed
797 798 799 800 801 802 803 804 805 806 807 808
/*
 * Fixed point
 */
isc_result_t
cfg_parse_fixedpoint(cfg_parser_t *pctx, const cfg_type_t *type,
		     cfg_obj_t **ret)
{
	isc_result_t result;
	cfg_obj_t *obj = NULL;
	size_t n1, n2, n3, l;
	const char *p;

809 810 811
	REQUIRE(pctx != NULL);
	REQUIRE(ret != NULL && *ret == NULL);

Evan Hunt's avatar
Evan Hunt committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	UNUSED(type);

	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type != isc_tokentype_string) {
		cfg_parser_error(pctx, CFG_LOG_NEAR,
				 "expected fixed point number");
		return (ISC_R_UNEXPECTEDTOKEN);
	}


	p = TOKEN_STRING(pctx);
	l = strlen(p);
	n1 = strspn(p, "0123456789");
	n2 = strspn(p + n1, ".");
	n3 = strspn(p + n1 + n2, "0123456789");

	if ((n1 + n2 + n3 != l) || (n1 + n3 == 0) ||
	    n1 > 5 || n2 > 1 || n3 > 2) {
		cfg_parser_error(pctx, CFG_LOG_NEAR,
				 "expected fixed point number");
		return (ISC_R_UNEXPECTEDTOKEN);
	}

	CHECK(cfg_create_obj(pctx, &cfg_type_fixedpoint, &obj));

	obj->value.uint32 = strtoul(p, NULL, 10) * 100;
	switch (n3) {
	case 2:
		obj->value.uint32 += strtoul(p + n1 + n2, NULL, 10);
		break;
	case 1:
		obj->value.uint32 += strtoul(p + n1 + n2, NULL, 10) * 10;
		break;
	}
	*ret = obj;

 cleanup:
	return (result);
}

void
cfg_print_fixedpoint(cfg_printer_t *pctx, const cfg_obj_t *obj) {
	char buf[64];
	int n;

857 858 859
	REQUIRE(pctx != NULL);
	REQUIRE(obj != NULL);

Evan Hunt's avatar
Evan Hunt committed
860 861 862 863 864 865 866 867 868 869 870 871
	n = snprintf(buf, sizeof(buf), "%u.%02u",
		     obj->value.uint32/100, obj->value.uint32%100);
	INSIST(n > 0 && (size_t)n < sizeof(buf));
	cfg_print_chars(pctx, buf, strlen(buf));
}

isc_uint32_t
cfg_obj_asfixedpoint(const cfg_obj_t *obj) {
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_fixedpoint);
	return (obj->value.uint32);
}

872
LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_fixedpoint = {
Evan Hunt's avatar
Evan Hunt committed
873 874 875
	"fixedpoint", cfg_parse_fixedpoint, cfg_print_fixedpoint,
	cfg_doc_terminal, &cfg_rep_fixedpoint, NULL
};
876

877 878 879 880 881 882
isc_boolean_t
cfg_obj_isfixedpoint(const cfg_obj_t *obj) {
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_fixedpoint));
}

883 884 885
/*
 * uint32
 */
886 887
isc_result_t
cfg_parse_uint32(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
Automatic Updater's avatar
Automatic Updater committed
888
	isc_result_t result;
889
	cfg_obj_t *obj = NULL;
890 891 892 893

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

894 895
	UNUSED(type);

896
	CHECK(cfg_gettoken(pctx, ISC_LEXOPT_NUMBER | ISC_LEXOPT_CNUMBER));
897
	if (pctx->token.type != isc_tokentype_number) {
898
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected number");
899 900 901
		return (ISC_R_UNEXPECTEDTOKEN);
	}

902
	CHECK(cfg_create_obj(pctx, &cfg_type_uint32, &obj));
903 904 905 906 907 908 909

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

910 911 912
void
cfg_print_cstr(cfg_printer_t *pctx, const char *s) {
	cfg_print_chars(pctx, s, strlen(s));
913 914
}

915 916
void
cfg_print_rawuint(cfg_printer_t *pctx, unsigned int u) {
917
	char buf[32];
918

919
	snprintf(buf, sizeof(buf), "%u", u);
920
	cfg_print_cstr(pctx, buf);
921 922
}

923
void
924
cfg_print_uint32(cfg_printer_t *pctx, const cfg_obj_t *obj) {
925
	cfg_print_rawuint(pctx, obj->value.uint32);
926 927
}

928
isc_boolean_t
929
cfg_obj_isuint32(const cfg_obj_t *obj) {
930 931 932 933
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_uint32));
}

934
isc_uint32_t
935
cfg_obj_asuint32(const cfg_obj_t *obj) {
936 937 938 939
	REQUIRE(obj != NULL && obj->type->rep == &cfg_rep_uint32);
	return (obj->value.uint32);
}

940
LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_uint32 = {
941 942 943
	"integer", cfg_parse_uint32, cfg_print_uint32, cfg_doc_terminal,
	&cfg_rep_uint32, NULL
};
944 945


946 947 948 949
/*
 * uint64
 */
isc_boolean_t
950
cfg_obj_isuint64(const cfg_obj_t *obj) {
951 952 953 954 955
	REQUIRE(obj != NULL);
	return (ISC_TF(obj->type->rep == &cfg_rep_uint64));
}

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

961
void
962
cfg_print_uint64(cfg_printer_t *pctx, const cfg_obj_t *obj) {
963
	char buf[32];
964

965 966
	snprintf(buf, sizeof(buf), "%" ISC_PRINT_QUADFORMAT "u",
		 obj->value.uint64);
967
	cfg_print_cstr(pctx, buf);
968 969
}

970
LIBISCCFG_EXTERNAL_DATA cfg_type_t cfg_type_uint64 = {
971 972
	"64_bit_integer", NULL, cfg_print_uint64, cfg_doc_terminal,
	&cfg_rep_uint64, NULL
973 974
};

975
/*
976 977
 * qstring (quoted string), ustring (unquoted string), astring
 * (any string)
978 979 980 981
 */

/* Create a string object from a null-terminated C string. */
static isc_result_t
982
create_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type,
983 984 985 986 987 988
	      cfg_obj_t **ret)
{
	isc_result_t result;
	cfg_obj_t *obj = NULL;
	int len;

989
	CHECK(cfg_create_obj(pctx, type, &obj));
990 991 992 993
	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) {
994
		isc_mem_put(pctx->mctx, obj, sizeof(*obj));
995 996
		return (ISC_R_NOMEMORY);
	}
997
	memmove(obj->value.string.base, contents, len);
998 999 1000 1001 1002 1003 1004
	obj->value.string.base[len] = '\0';

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

1005 1006
isc_result_t
cfg_parse_qstring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
Automatic Updater's avatar
Automatic Updater committed
1007
	isc_result_t result;
1008 1009 1010 1011

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

1012 1013
	UNUSED(type);

1014
	CHECK(cfg_gettoken(pctx, CFG_LEXOPT_QSTRING));
1015
	if (pctx->token.type != isc_tokentype_qstring) {
1016
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected quoted string");
1017 1018
		return (ISC_R_UNEXPECTEDTOKEN);
	}
1019 1020
	return (create_string(pctx, TOKEN_STRING(pctx),
			      &cfg_type_qstring, ret));
1021 1022 1023 1024 1025
 cleanup:
	return (result);
}

static isc_result_t
1026
parse_ustring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret) {
Automatic Updater's avatar
Automatic Updater committed
1027
	isc_result_t result;
1028

1029 1030 1031 1032
	UNUSED(type);

	CHECK(cfg_gettoken(pctx, 0));
	if (pctx->token.type != isc_tokentype_string) {
1033
		cfg_parser_error(pctx, CFG_LOG_NEAR, "expected unquoted string");
1034 1035 1036
		return (ISC_R_UNEXPECTEDTOKEN);
	}
	return (create_string(pctx,
1037
			      TOKEN_STRING(pctx),
1038 1039 1040 1041 1042 1043
			      &cfg_type_ustring,
			      ret));
 cleanup:
	return (result);
}

1044
isc_result_t
1045 1046 1047
cfg_parse_astring(cfg_parser_t *pctx, const cfg_type_t *type,
		  cfg_obj_t **ret)
{
Automatic Updater's avatar
Automatic Updater committed
1048
	isc_result_t result;
1049 1050 1051 1052

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

1053 1054 1055 1056
	UNUSED(type);

	CHECK(cfg_getstringtoken(pctx));
	return (create_string(pctx,