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 test_for_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 uint8_t set_val(char * token0, char * token1, char * comparand,
179 uint8_t * flags, uint8_t set_flag, char type,
182 if (!strcmp(token0, comparand))
184 char * err_out = "Outside appropriate definition's context.";
185 char * err_singlechar = "Value must be single ASCII character.";
186 err_line(!(*flags & EDIT_STARTED), err_out);
187 *flags = *flags | set_flag;
190 * (char **) element = strdup(token1);
192 else if ('c' == type)
194 err_line(1 != strlen(token1), err_singlechar);
195 *element = (token1)[0];
197 else if ('8' == type)
199 test_for_int(token1, '8');
200 * (uint8_t *) element = atoi(token1);
202 else if ('i' == type)
204 test_for_int(token1, 'i');
205 * (int16_t *) element = atoi(token1);