1 /* src/common/parse_file.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "parse_file.h"
5 #include <stddef.h> /* size_t, NULL */
6 #include <stdio.h> /* FILE, snprintf() */
7 #include <stdint.h> /* int16_t,uint8_t,uint32_t, INT16_MIN, UINT{8,16,32}_MAX */
8 #include <stdlib.h> /* atoi(), free() */
9 #include <string.h> /* strchr, strcmp(), strdup(), strlen() */
10 #include <unistd.h> /* access(), F_OK */
11 #include "readwrite.h" /* try_fopen(), try_fclose(), textfile_width() */
12 #include "rexit.h" /* exit_err(), exit_trouble() */
13 #include "try_malloc.h" /* try_malloc() */
17 /* Set by parse_file(), helps err_line() deciding what to do/output on error. */
18 static uint32_t err_line_count = 0;
19 static char * err_line_line = NULL;
20 static char * err_line_intro = NULL;
21 static uint8_t err_line_exit = 1;
25 /* Determines the end of the token_from_line() token. */
26 static void set_token_end(char ** start, char ** limit_char);
30 static void set_token_end(char ** start, char ** limit_char)
32 char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
33 * start = (end_quote) ? * start + 1 : *start;
37 * limit_char = end_quote;
40 char * space = strchr(*start, ' ');
41 char * tab = strchr(*start, '\t');
42 space = (!space || (tab && tab < space)) ? tab : space;
47 *limit_char = strchr(*start, '\0');
52 extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
54 char * f_name = "read_new_config_file()";
55 char * prefix = "Failed reading config file: \"";
56 char * affix = "\". ";
57 size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
58 char * errline_intro = try_malloc(size, f_name);
59 int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix);
60 exit_trouble(test < 0, f_name, "snprintf()");
61 exit_err(access(path, F_OK), errline_intro);
62 FILE * file = try_fopen(path, "r", f_name);
63 uint32_t linemax = textfile_width(file);
64 char * errline_line = try_malloc(linemax + 1, f_name);
65 set_err_line_options(errline_intro, errline_line, 0, 1);
66 err_line(0 == linemax, "File is empty.");
67 char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
69 char * err_val = "No value given.";
70 while (try_fgets(errline_line, linemax + 1, file, f_name))
73 err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
74 char * line_copy = strdup(errline_line);
75 token0 = token_from_line(line_copy);
78 err_line(0 == (token1 = token_from_line(NULL)), err_val);
79 token_to_entry(token0, token1);
84 token_to_entry(token0, token1);
85 try_fclose(file, f_name);
92 extern void set_err_line_options(char * intro, char * line, uint32_t count,
95 err_line_count = count;
97 err_line_intro = intro;
103 extern uint8_t err_line(uint8_t test, char * msg)
109 char * f_name = "err_line()";
110 char * prefix = " Offending line ";
111 char * affix = ":\n";
112 size_t size = strlen(err_line_intro) + strlen(msg) + strlen(prefix)
113 + 10 /* strlen for uint32_t representations */
114 + strlen(affix) + strlen(err_line_line) + 1;
115 char * err = try_malloc(size, f_name);
116 int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
117 err_line_count, affix, err_line_line);
118 exit_trouble(ret < 0, f_name, "snprintf()");
123 exit_trouble(0 > printf("%s\n", err), f_name, "printf()");
124 exit_trouble(EOF == fflush(stdout), f_name, "fflush()");
131 extern char * token_from_line(char * line)
133 static char * final_char = NULL;
134 static char * limit_char = NULL;
135 char * start = limit_char + 1;
140 final_char = &(line[strlen(line)]);
141 if ('\n' == *(final_char - 1))
143 *(--final_char) = '\0';
146 if (final_char < start)
152 for (i = 0; '\0' != start[i]; i++)
154 if (' ' != start[i] && '\t' != start[i])
165 set_token_end(&start, &limit_char);
171 extern uint8_t parsetest_int(char * string, char type)
173 char * err_8 = "Value must represent proper unsigned 8 bit integer.";
174 char * err_i = "Value must represent proper signed 16 bit integer.";
175 char * err_u = "Value must represent proper unsigned 16 bit integer.";
176 char * err_U = "Value must represent proper unsigned 32 bit integer.";
177 char * err = ('8' == type) ? err_8 : err_U;
178 err = ('i' == type) ? err_i : err;
179 err = ('u' == type) ? err_u : err;
180 uint8_t ret = err_line(strlen(string) < 1, err);
183 for (i = 0; '\0' != string[i]; i++)
185 char * err_many = "Value of too many characters.";
186 ret = ret + err_line(string[i + 1] && UINT8_MAX == i, err_many);
187 test = ( (0 == i && ('-' == string[i] || '+' == string[i]))
188 || ('0' <= string[i] && string[i] <= '9'));
189 ret = ret + err_line(!test, err);
191 ret = ret + err_line( strlen(string) < 2
192 && ('-' == string[i] || '+' == string[i]), err);
194 && ( strlen(string) > 4
195 || atoi(string) < 0 || atoi(string) > UINT8_MAX))
197 && ( strlen(string) > 6
198 || atol(string) < INT16_MIN || atol(string) > INT16_MAX))
200 && ( strlen(string) > 6
201 || atoll(string) < 0 || atol(string) > UINT16_MAX))
203 && ( strlen(string) > 11
204 || atoll(string) < 0 || atoll(string) > UINT32_MAX));
205 ret = ret + err_line(test, err);
211 extern void parsetest_defcontext(uint8_t flags)
213 err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
218 extern uint8_t parsetest_singlechar(char * string)
220 return err_line(1 !=strlen(string),"Value must be single ASCII character.");
225 extern void parsetest_too_many_values()
227 err_line(NULL != token_from_line(NULL), "Too many values.");
232 extern void parse_id_uniq(int test)
234 err_line(0 != test, "Declaration of ID already used.");
239 extern void parse_unknown_arg()
241 err_line(1, "Unknown argument.");
246 extern char * parse_init_entry(uint8_t * flags, size_t size)
248 char * f_name = "parse_init_entry()";
249 *flags = EDIT_STARTED;
250 char * p = try_malloc(size, f_name);
257 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
258 char type, char * element)
260 if (!strcmp(token0, comparand))
264 * (char **) element = strdup(token1);
266 else if ('c' == type && !parsetest_singlechar(token1))
268 *element = (token1)[0];
270 else if (!parsetest_int(token1, type))
274 * (uint8_t *) element = atoi(token1);
276 else if ('i' == type)
278 * (int16_t *) element = atoi(token1);
280 else if ('u' == type)
282 * (uint16_t *) element = atol(token1);
284 else if ('U' == type)
286 * (uint32_t *) element = atoll(token1);
296 extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
297 uint8_t * flags, uint8_t set_flag, char type,
300 if (parse_val(token0, token1, comparand, type, element))
302 parsetest_defcontext(*flags);
303 *flags = *flags | set_flag;
311 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag)
313 char * err_fin = "Last definition block not finished yet.";
314 err_line((*flags & ready_flag) ^ ready_flag, err_fin);