1 /*-
2  * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include <ctype.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <string.h>
39 
40 #include "compat_stdbool.h"
41 
42 #include "logmsg.h"
43 #include "token.h"
44 
45 size_t lineno = 1;
46 
47 const struct token_keyword *
token_get_keyword(FILE * fp,const struct token_keyword * keys)48 token_get_keyword(FILE *fp, const struct token_keyword *keys)
49 {
50 	const struct token_keyword *key;
51 	struct token tk;
52 	int c;
53 
54 	if (!token_skip_whitespace(fp)) {
55 		logmsg_err("premature EOF");
56 		return (false);
57 	}
58 
59 	tk.length = 0;
60 
61 	if ((c = fgetc(fp)) == EOF) {
62 		logmsg_err("premature EOF");
63 		return (NULL);
64 	}
65 
66 	while (isalnum(c) || (c == '{') || (c == '}') || (c == '-')) {
67 		tk.token[tk.length++] = c;
68 		if (tk.length == sizeof(tk.token)) {
69 			logmsg_err("too long token");
70 			return (NULL);
71 		}
72 		if ((c = fgetc(fp)) == EOF) {
73 			logmsg_err("premature EOF");
74 			return (NULL);
75 		}
76 	}
77 	if (tk.length == 0) {
78 		logmsg_err("line %u: no keywords", lineno);
79 		return (NULL);
80 	}
81 	tk.token[tk.length] = '\0';
82 
83 	if (ungetc(c, fp) == EOF)
84 		return (false);
85 
86 	for (key = keys ; key->name != NULL ; key++) {
87 		if ((tk.length == key->namelen) &&
88 		    (memcmp(tk.token, key->name, tk.length) == 0)) {
89 			return (key);
90 		}
91 	}
92 
93 	logmsg_err("%s: invalid keyword", tk.token);
94 
95 	return (NULL);
96 }
97 
98 bool
token_get_number(FILE * fp,unsigned long * num)99 token_get_number(FILE *fp, unsigned long *num)
100 {
101 	struct token tk;
102 	char *ep;
103 	unsigned long v;
104 
105 	if (!token_get_string(fp, &tk))
106 		return (false);
107 
108 	errno = 0;
109 	v = strtoul(tk.token, &ep, 0);
110 	if ((ep == NULL) || (*ep != '\0') || ((v == 0) && (errno == EINVAL)) ||
111 	    ((v == ULONG_MAX) && (errno == ERANGE))) {
112 		logmsg_err("line %u: %s: %s", lineno, tk.token,
113 			   strerror(EINVAL));
114 		return (false);
115 	}
116 
117 	*num = v;
118 
119 	return (true);
120 }
121 
122 bool
token_get_string(FILE * fp,struct token * tk)123 token_get_string(FILE *fp, struct token *tk)
124 {
125 	char *quot;
126 	int c;
127 
128 	tk->length = 0;
129 
130 	if (!token_skip_whitespace(fp)) {
131 		logmsg_err("premature EOF");
132 		return (false);
133 	}
134 
135 	if ((c = fgetc(fp)) == EOF) {
136 		logmsg_err("premature EOF");
137 		return (false);
138 	}
139 
140 	if ((quot = strchr("\"'", c)) == NULL) {
141 		do {
142 			tk->token[tk->length++] = c;
143 			if (tk->length == sizeof(tk->token)) {
144 				logmsg_err("too long token");
145 				return (false);
146 			}
147 			if ((c = fgetc(fp)) == EOF) {
148 				logmsg_err("premature EOF");
149 				return (false);
150 			}
151 		} while (!isspace(c));
152 		if (c == '\n')
153 			lineno++;
154 	} else {
155 		if ((c = fgetc(fp)) == EOF) {
156 			logmsg_err("premature EOF");
157 			return (false);
158 		}
159 		while (c != *quot) {
160 			tk->token[tk->length++] = c;
161 			if (tk->length == sizeof(tk->token)) {
162 				logmsg_err("too long token");
163 				return (false);
164 			}
165 			if ((c = fgetc(fp)) == EOF) {
166 				logmsg_err("premature EOF");
167 				return (false);
168 			}
169 			if (c == '\n')  {
170 				logmsg_err("missing close quotation %c",
171 					   *quot);
172 				return (false);
173 			}
174 		}
175 		if (tk->length == 0) {
176 			logmsg_err("empty string");
177 			return (false);
178 		}
179 	}
180 
181 	tk->token[tk->length] = '\0';
182 
183 	return (true);
184 }
185 
186 bool
token_skip_whitespace(FILE * fp)187 token_skip_whitespace(FILE *fp)
188 {
189 	int c;
190 
191 	for (;;) {
192 		do {
193 			if ((c = fgetc(fp)) == EOF)
194 				return (false);
195 			if (c == '\n')
196 				lineno++;
197 		} while (isspace(c));
198 
199 		if (c != '#')
200 			break;
201 
202 		do {
203 			if ((c = fgetc(fp)) == EOF)
204 				return (false);
205 		} while (c != '\n');
206 		lineno++;
207 	}
208 
209 	if (ungetc(c, fp) == EOF)
210 		return (false);
211 
212 	return (true);
213 }
214