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 * f_name = "read_new_config_file()";
23 char * prefix = "Failed reading config file: \"";
24 char * affix = "\". ";
25 size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
26 char * errline_intro = try_malloc(size, f_name);
27 int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix);
28 exit_trouble(test < 0, f_name, "snprintf()");
29 exit_err(access(path, F_OK), errline_intro);
30 FILE * file = try_fopen(path, "r", f_name);
31 uint32_t linemax = textfile_width(file);
32 char * errline_line = try_malloc(linemax + 1, f_name);
33 set_err_line_options(errline_intro, errline_line, 1);
35 err_line(0 == linemax, "File is empty.");
36 char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
38 char * err_val = "No value given.";
39 while (try_fgets(errline_line, linemax + 1, file, f_name))
42 char * line_copy = strdup(errline_line);
43 token0 = token_from_line(line_copy);
46 err_line(0 == (token1 = token_from_line(NULL)), err_val);
47 token_to_entry(token0, token1);
52 token_to_entry(token0, token1);
53 try_fclose(file, f_name);
60 extern void parsetest_defcontext(uint8_t flags)
62 err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
67 extern void parsetest_too_many_values()
69 err_line(NULL != token_from_line(NULL), "Too many values.");
74 extern void parse_id_uniq(int test)
76 err_line(0 != test, "Declaration of ID already used.");
81 extern void parse_unknown_arg()
83 err_line(1, "Unknown argument.");
88 extern char * parse_init_entry(uint8_t * flags, size_t size)
90 char * f_name = "parse_init_entry()";
91 *flags = EDIT_STARTED;
92 char * p = try_malloc(size, f_name);
99 extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
100 uint8_t * flags, uint8_t set_flag, char type,
103 if (parse_val(token0, token1, comparand, type, element))
105 parsetest_defcontext(*flags);
106 *flags = *flags | set_flag;
114 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag)
116 char * err_fin = "Last definition block not finished yet.";
117 err_line((*flags & ready_flag) ^ ready_flag, err_fin);