11.6Sjoerg/* 	$NetBSD: config_yacc.y,v 1.6 2020/04/22 23:51:34 joerg Exp $	*/
21.1Sxtraeme
31.1Sxtraeme/*-
41.1Sxtraeme * Copyright (c) 2007 Juan Romero Pardines.
51.1Sxtraeme * All rights reserved.
61.1Sxtraeme *
71.1Sxtraeme * Redistribution and use in source and binary forms, with or without
81.1Sxtraeme * modification, are permitted provided that the following conditions
91.1Sxtraeme * are met:
101.1Sxtraeme * 1. Redistributions of source code must retain the above copyright
111.1Sxtraeme *    notice, this list of conditions and the following disclaimer.
121.1Sxtraeme * 2. Redistributions in binary form must reproduce the above copyright
131.1Sxtraeme *    notice, this list of conditions and the following disclaimer in the
141.1Sxtraeme *    documentation and/or other materials provided with the distribution.
151.1Sxtraeme *
161.1Sxtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sxtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sxtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sxtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sxtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.1Sxtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.1Sxtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.1Sxtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.1Sxtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.1Sxtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Sxtraeme */
271.1Sxtraeme
281.1Sxtraeme%{
291.1Sxtraeme
301.1Sxtraeme#include <sys/cdefs.h>
311.1Sxtraeme#ifndef lint
321.6Sjoerg__RCSID("$NetBSD: config_yacc.y,v 1.6 2020/04/22 23:51:34 joerg Exp $");
331.1Sxtraeme#endif /* not lint */
341.1Sxtraeme
351.1Sxtraeme#include <stdio.h>
361.1Sxtraeme#include <string.h>
371.1Sxtraeme#include <stdarg.h>
381.1Sxtraeme#include <err.h>
391.1Sxtraeme#include <errno.h>
401.1Sxtraeme#include <stdbool.h>
411.4Sdrochner#include <stdlib.h>
421.1Sxtraeme
431.1Sxtraeme#include <prop/proplib.h>
441.1Sxtraeme
451.1Sxtraeme#include "envstat.h"
461.1Sxtraeme
471.1Sxtraemeint yylex(void);
481.1Sxtraemeint yyparse(void);
491.5Sjoergint yyerror(const char *, ...) __printflike(1, 2);
501.1Sxtraemevoid yyrestart(FILE *);
511.1Sxtraeme
521.1Sxtraemeint yyline;
531.6Sjoergextern char *yytext;
541.1Sxtraemestatic prop_dictionary_t kdict;
551.1Sxtraeme
561.1Sxtraeme%}
571.1Sxtraeme
581.1Sxtraeme%token EOL EQUAL LBRACE RBRACE
591.1Sxtraeme%token STRING NUMBER SENSOR
601.3Sxtraeme%token SENSOR_PROP DEVICE_PROP
611.3Sxtraeme%token <string>	SENSOR STRING SENSOR_PROP DEVICE_PROP
621.1Sxtraeme%union {
631.1Sxtraeme	char *string;
641.1Sxtraeme}
651.1Sxtraeme
661.1Sxtraeme%%
671.1Sxtraeme
681.2Sxtraememain	:	devices
691.2Sxtraeme     	|			{ exit(EXIT_SUCCESS); }
701.2Sxtraeme     	;
711.2Sxtraeme
721.2Sxtraemedevices	:	device
731.2Sxtraeme	|	devices device
741.1Sxtraeme	;
751.1Sxtraeme
761.3Sxtraemedevice	:	STRING LBRACE props RBRACE
771.1Sxtraeme				{ config_devblock_add($1, kdict); }
781.1Sxtraeme	;
791.1Sxtraeme
801.3Sxtraemeprops	:	sensor
811.3Sxtraeme	|	props sensor
821.3Sxtraeme	|	devprop
831.3Sxtraeme	|	props devprop
841.1Sxtraeme	;
851.1Sxtraeme
861.1Sxtraemesensor	:	SENSOR LBRACE params RBRACE
871.1Sxtraeme				{ config_dict_add_prop("index", $1);
881.3Sxtraeme				  config_dict_mark(); }
891.1Sxtraeme	;
901.1Sxtraeme
911.1Sxtraemeparams	:	prop
921.1Sxtraeme	|	params prop
931.1Sxtraeme	;
941.1Sxtraeme
951.1Sxtraemeprop	:	SENSOR_PROP EQUAL STRING EOL
961.1Sxtraeme     				{ config_dict_add_prop($1, $3); }
971.1Sxtraeme	;
981.1Sxtraeme
991.3Sxtraemedevprop	:	DEVICE_PROP EQUAL STRING EOL
1001.3Sxtraeme				{ config_dict_adddev_prop($1, $3, yyline); }
1011.3Sxtraeme	;
1021.3Sxtraeme
1031.3Sxtraeme
1041.1Sxtraeme%%
1051.1Sxtraeme
1061.1Sxtraemeint
1071.1Sxtraemeyyerror(const char *fmt, ...)
1081.1Sxtraeme{
1091.1Sxtraeme	va_list ap;
1101.1Sxtraeme
1111.1Sxtraeme	va_start(ap, fmt);
1121.1Sxtraeme	fprintf(stderr, "%s: ", getprogname());
1131.1Sxtraeme	vfprintf(stderr, fmt, ap);
1141.1Sxtraeme	fprintf(stderr, " in line %d\n", yyline);
1151.1Sxtraeme	va_end(ap);
1161.1Sxtraeme
1171.1Sxtraeme	exit(EXIT_FAILURE);
1181.1Sxtraeme}
1191.1Sxtraeme
1201.1Sxtraemevoid
1211.1Sxtraemeconfig_parse(FILE *f, prop_dictionary_t d)
1221.1Sxtraeme{
1231.1Sxtraeme	kdict = prop_dictionary_copy(d);
1241.1Sxtraeme	yyrestart(f);
1251.3Sxtraeme	yyline = 1;
1261.1Sxtraeme	yyparse();
1271.1Sxtraeme}
128