3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup(), snprintf() */
10 #include <stddef.h> /* size_t, NULL */
11 #include <stdio.h> /* FILE, snprintf() */
12 #include <stdint.h> /* uint8_t, uint32_t */
13 #include <stdlib.h> /* free() */
14 #include <string.h> /* strdup(), strlen() */
15 #include <unistd.h> /* access(), F_OK */
16 #include "../common/parse_file.h" /* set_err_line_options(), err_line_inc(),
17 * err_line_zero(), token_from_line()
19 #include "../common/readwrite.h" /* try_fopen(),try_fclose(),textfile_width() */
20 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
21 #include "../common/try_malloc.h" /* try_malloc() */
25 extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
27 char * prefix = "Failed reading config file: \"";
28 char * affix = "\". ";
29 size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
30 char * errline_intro = try_malloc(size, __func__);
31 int test = snprintf(errline_intro, size, "%s%s%s", prefix, path, affix);
32 exit_trouble(test < 0, __func__, "snprintf");
33 exit_err(access(path, F_OK), errline_intro);
34 FILE * file = try_fopen(path, "r", __func__);
35 uint32_t linemax = textfile_width(file);
36 char * errline_line = try_malloc(linemax + 1, __func__);
37 set_err_line_options(errline_intro, errline_line, 1);
39 err_line(!linemax, "File is empty.");
40 char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
42 char * err_val = "No value given.";
43 while (try_fgets(errline_line, linemax + 1, file, __func__))
46 char * line_copy = strdup(errline_line);
47 token0 = token_from_line(line_copy);
50 err_line(0 == (token1 = token_from_line(NULL)), err_val);
51 token_to_entry(token0, token1);
56 token_to_entry(token0, token1);
57 try_fclose(file, __func__);
64 extern void parsetest_defcontext(uint8_t flags)
66 err_line(!(flags & EDIT_STARTED),"Outside appropriate definition context.");
71 extern void parsetest_too_many_values()
73 err_line(!(!token_from_line(NULL)), "Too many values.");
78 extern void parse_id_uniq(int test)
80 err_line(0 != test, "Declaration of ID already used.");
85 extern void parse_unknown_arg()
87 err_line(1, "Unknown argument.");
92 extern char * parse_init_entry(uint8_t * flags, size_t size)
94 *flags = EDIT_STARTED;
95 char * p = try_malloc(size, __func__);
102 extern uint8_t parse_flagval(char * token0, char * token1, char * comparand,
103 uint8_t * flags, uint8_t set_flag, char type,
106 if (parse_val(token0, token1, comparand, type, element))
108 parsetest_defcontext(*flags);
109 *flags = *flags | set_flag;
117 extern void parse_and_reduce_to_readyflag(uint8_t * flags, uint8_t ready_flag)
119 char * err_fin = "Last definition block not finished yet.";
120 err_line((*flags & ready_flag) ^ ready_flag, err_fin);