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(), used by err_line() for more informative messages. */
18 static uint32_t err_line_count = 0;
19 static char * err_line_line = NULL;
20 static char * err_line_intro = NULL;
24 /* Determines the end of the token_from_line() token. */
25 static void set_token_end(char ** start, char ** limit_char);
29 static void set_token_end(char ** start, char ** limit_char)
31 char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
32 * start = (end_quote) ? * start + 1 : *start;
36 * limit_char = end_quote;
39 char * space = strchr(*start, ' ');
40 char * tab = strchr(*start, '\t');
41 space = (!space || (tab && tab < space)) ? tab : space;
46 *limit_char = strchr(*start, '\0');
51 extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
53 char * f_name = "read_new_config_file()";
54 char * prefix = "Failed reading config file: \"";
55 char * affix = "\". ";
56 size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
57 err_line_intro = try_malloc(size, f_name);
58 int test = snprintf(err_line_intro, size, "%s%s%s", prefix, path, affix);
59 exit_trouble(test < 0, f_name, "snprintf()");
60 exit_err(access(path, F_OK), err_line_intro);
61 FILE * file = try_fopen(path, "r", f_name);
62 uint32_t linemax = textfile_width(file);
63 err_line_line = try_malloc(linemax + 1, f_name);
65 err_line(0 == linemax, "File is empty.");
66 char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
68 char * err_val = "No value given.";
69 while (try_fgets(err_line_line, linemax + 1, file, f_name))
72 err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
73 char * line_copy = strdup(err_line_line);
74 token0 = token_from_line(line_copy);
77 err_line(0 == (token1 = token_from_line(NULL)), err_val);
78 token_to_entry(token0, token1);
83 token_to_entry(token0, token1);
84 try_fclose(file, f_name);
91 extern void err_line(uint8_t test, char * msg)
97 char * f_name = "err_line()";
98 char * prefix = " Offending line ";
100 size_t size = strlen(err_line_intro) + strlen(msg) + strlen(prefix)
101 + 10 /* strlen for uint32_t representations */
102 + strlen(affix) + strlen(err_line_line) + 1;
103 char * err = try_malloc(size, f_name);
104 int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
105 err_line_count, affix, err_line_line);
106 exit_trouble(ret < 0, f_name, "snprintf()");
112 extern char * token_from_line(char * line)
114 static char * final_char = NULL;
115 static char * limit_char = NULL;
116 char * start = limit_char + 1;
121 final_char = &(line[strlen(line)]);
122 if ('\n' == *(final_char - 1))
124 *(--final_char) = '\0';
129 for (i = 0; '\0' != start[i]; i++)
131 if (' ' != start[i] && '\t' != start[i])
142 set_token_end(&start, &limit_char);
148 extern void parsetest_int(char * string, char type)
153 err = "Value must be proper representation of unsigned 8 bit integer.";
157 err = "Value must be proper representation of signed 16 bit integer.";
159 err_line(strlen(string) < 1, err);
162 for (i = 0; '\0' != string[i]; i++)
164 char * err_many = "Value of too many characters.";
165 err_line(string[i + 1] && UINT8_MAX == i, err_many);
166 test = ( (0 == i && ('-' == string[i] || '+' == string[i]))
167 || ('0' <= string[i] && string[i] <= '9'));
168 err_line(!test, err);
170 err_line(strlen(string) < 2 && ('-' == string[i] || '+' == string[i]), err);
171 err_line('8'==type && (atoi(string) < 0 || atoi(string) > UINT8_MAX), err);
172 test = 'i'==type && (atoi(string) < INT16_MIN || atoi(string) > INT16_MAX);
178 extern void parsetest_defcontext(uint8_t flags)
180 err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
185 extern void parsetest_singlechar(char * string)
187 err_line(1 != strlen(string), "Value must be single ASCII character.");
192 extern void parsetest_too_many_values()
194 err_line(NULL != token_from_line(NULL), "Too many values.");
199 extern void parse_id_uniq(int test)
201 err_line(0 != test, "Declaration of ID already used.");
206 extern void parse_unknown_arg()
208 err_line(1, "Unknown argument.");
213 extern char * parse_init_entry(uint8_t * flags, size_t size)
215 char * f_name = "parse_init_entry()";
216 *flags = EDIT_STARTED;
217 char * p = try_malloc(size, f_name);
224 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
225 uint8_t * flags, uint8_t set_flag, char type,
228 if (!strcmp(token0, comparand))
230 parsetest_defcontext(*flags);
231 *flags = *flags | set_flag;
234 * (char **) element = strdup(token1);
236 else if ('c' == type)
238 parsetest_singlechar(token1);
239 *element = (token1)[0];
241 else if ('8' == type)
243 parsetest_int(token1, '8');
244 * (uint8_t *) element = atoi(token1);
246 else if ('i' == type)
248 parsetest_int(token1, 'i');
249 * (int16_t *) element = atoi(token1);
258 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag)
260 char * err_fin = "Last definition block not finished yet.";
261 err_line((*flags & ready_flag) ^ ready_flag, err_fin);