1 /* src/client/parse.c */
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
5 #include <stddef.h> /* size_t, NULL */
6 #include <stdio.h> /* FILE, snprintf() */
7 #include <stdint.h> /* uint8_t, uint32_t */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* strdup(), strlen() */
10 #include <unistd.h> /* access(), F_OK */
11 #include "../common/parse_file.h" /* set_err_line_options(), err_line_inc(),
12 * err_line_zero(), token_from_line()
14 #include "../common/readwrite.h" /* try_fopen(),try_fclose(),textfile_width() */
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
20 extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
22 char * prefix = "Failed reading config file: \"";
23 char * affix = "\". ";
24 size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
25 char * errline_intro = try_malloc(size, __func__);
26 int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix);
27 exit_trouble(test < 0, __func__, "snprintf");
28 exit_err(access(path, F_OK), errline_intro);
29 FILE * file = try_fopen(path, "r", __func__);
30 uint32_t linemax = textfile_width(file);
31 char * errline_line = try_malloc(linemax + 1, __func__);
32 set_err_line_options(errline_intro, errline_line, 1);
34 err_line(0 == linemax, "File is empty.");
35 char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
37 char * err_val = "No value given.";
38 while (try_fgets(errline_line, linemax + 1, file, __func__))
41 char * line_copy = strdup(errline_line);
42 token0 = token_from_line(line_copy);
45 err_line(0 == (token1 = token_from_line(NULL)), err_val);
46 token_to_entry(token0, token1);
51 token_to_entry(token0, token1);
52 try_fclose(file, __func__);
59 extern void parsetest_defcontext(uint8_t flags)
61 err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
66 extern void parsetest_too_many_values()
68 err_line(NULL != token_from_line(NULL), "Too many values.");
73 extern void parse_id_uniq(int test)
75 err_line(0 != test, "Declaration of ID already used.");
80 extern void parse_unknown_arg()
82 err_line(1, "Unknown argument.");
87 extern char * parse_init_entry(uint8_t * flags, size_t size)
89 *flags = EDIT_STARTED;
90 char * p = try_malloc(size, __func__);
97 extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
98 uint8_t * flags, uint8_t set_flag, char type,
101 if (parse_val(token0, token1, comparand, type, element))
103 parsetest_defcontext(*flags);
104 *flags = *flags | set_flag;
112 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag)
114 char * err_fin = "Last definition block not finished yet.";
115 err_line((*flags & ready_flag) ^ ready_flag, err_fin);