1 /* src/common/parse_file.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "parse_file.h"
5 #include <stddef.h> /* NULL */ // size_t
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdint.h> /* uint8_t, uint32_t */
8 #include <stdlib.h> /* free() */ // atoi()
9 #include <string.h> /* strchr, strdup(), strlen() */ // strcmp()
10 #include <unistd.h> /* access(), F_OK */
11 #include "err_try_fgets.h" /* err_line(), err_try_fgets(),
12 * reset_err_try_fgets_counter()
14 #include "readwrite.h" /* try_fopen(),try_fclose(), textfile_width() */
15 #include "rexit.h" /* exit_err() */
16 #include "try_malloc.h" /* try_malloc() */
20 /* Return next token from "line" or NULL if none is found. Tokens either a)
21 * start at the first non-whitespace character and end before the next
22 * whitespace character after that; or b) if the first non-whitespace character
23 * is a single quote followed by at least one other single quote some time later
24 * on the line, the token starts after that first single quote and ends before
25 * the second, with the next token_from_line() call starting its token search
26 * after that second quote. The only way to return an empty string (instead of
27 * NULL) as a token is to delimit the token by two succeeding single quotes.
29 static char * token_from_line(char * line);
31 /* Determines the end of the token_from_line() token. */
32 static void set_token_end(char ** start, char ** limit_char);
36 static char * token_from_line(char * line)
38 static char * final_char = NULL;
39 static char * limit_char = NULL;
40 char * start = limit_char + 1;
45 final_char = &(line[strlen(line)]);
46 if ('\n' == *(final_char - 1))
48 *(--final_char) = '\0';
53 for (i = 0; '\0' != start[i]; i++)
55 if (' ' != start[i] && '\t' != start[i])
66 set_token_end(&start, &limit_char);
72 static void set_token_end(char ** start, char ** limit_char)
74 char * end_quote = ('\'' == (*start)[0]) ? strchr(*start + 1, '\'') : NULL;
75 *start = (end_quote) ? *start + 1 : *start;
79 *limit_char = end_quote;
82 char * space = strchr(*start, ' ');
83 char * tab = strchr(*start, '\t');
84 space = (!space || (tab && tab < space)) ? tab : space;
89 *limit_char = strchr(*start, '\0');
94 extern void set_uint8(struct Context * context, uint8_t * target)
96 char * err_uint8 = "Value not unsigned decimal number between 0 and 255.";
99 for (i = 0; '\0' != context->token1[i]; i++)
101 if (i > 2 || '0' > context->token1[i] || '9' < context->token1[i])
106 if (is_uint8 && atoi(context->token1) > UINT8_MAX)
110 err_line(!(is_uint8), context->line, context->err_pre, err_uint8);
111 *target = atoi(context->token1);
116 extern uint8_t set_val(struct Context * context, char * comparand,
117 uint8_t * flags, uint8_t set_flag, char type,
120 char * err_out = "Outside appropriate definition's context.";
121 char * err_singlechar = "Value must be single ASCII character.";
122 if (!strcmp(context->token0, comparand))
124 err_line(!(* flags & EDIT_STARTED),
125 context->line, context->err_pre, err_out);
126 * flags = * flags | set_flag;
129 * (char **) element = strdup(context->token1);
131 else if ('c' == type)
133 err_line(1 != strlen(context->token1),
134 context->line, context->err_pre, err_singlechar);
135 * element = (context->token1)[0];
137 else if ('8' == type)
139 set_uint8(context, (uint8_t *) element);
148 extern void parse_file(char * path, void (* token_to_entry) (struct Context *))
150 char * f_name = "read_new_config_file()";
151 struct Context context;
152 char * err_pre_prefix = "Failed reading config file: \"";
153 char * err_pre_affix = "\". ";
154 context.err_pre = try_malloc(strlen(err_pre_prefix) + strlen(path)
155 + strlen(err_pre_affix) + 1, f_name);
156 sprintf(context.err_pre, "%s%s%s", err_pre_prefix, path, err_pre_affix);
157 exit_err(access(path, F_OK), context.err_pre);
158 FILE * file = try_fopen(path, "r", f_name);
159 uint32_t linemax = textfile_width(file);
160 context.line = try_malloc(linemax + 1, f_name);
161 reset_err_try_fgets_counter();
162 err_line(0 == linemax, context.line, context.err_pre, "File is empty.");
163 context.token0 = NULL; /* For token_to_entry() if while() stagnates. */
164 char * err_val = "No value given.";
165 char * err_many = "Too many values.";
166 while (err_try_fgets(context.line, linemax + 1, file, context.err_pre, ""))
168 char * line_copy = strdup(context.line);
169 context.token0 = token_from_line(line_copy);
172 err_line(0 == (context.token1 = token_from_line(NULL)),
173 context.line, context.err_pre, err_val);
174 err_line(NULL != token_from_line(NULL),
175 context.line, context.err_pre, err_many);
176 token_to_entry(&context);
177 context.token0 = NULL;
181 token_to_entry(&context);
182 try_fclose(file, f_name);
184 free(context.err_pre);