summaryrefslogtreecommitdiff
path: root/src/sp_config_utils.c
diff options
context:
space:
mode:
authorBen Fuhrmannek2021-08-16 15:47:01 +0200
committerBen Fuhrmannek2021-08-16 15:47:01 +0200
commit5148ded7268b569fd5e720f90b44645c83ac3e9e (patch)
tree9d5c3035a7a85ffc27de7c32b441994a21a6347a /src/sp_config_utils.c
parent9dc6b23a2219e809e665bac7d82567533751d39d (diff)
fincy new scanner/parser for config rules + fixed a few bugs along the way + fixed related unittests
Diffstat (limited to 'src/sp_config_utils.c')
-rw-r--r--src/sp_config_utils.c103
1 files changed, 0 insertions, 103 deletions
diff --git a/src/sp_config_utils.c b/src/sp_config_utils.c
index bc7b405..e93ef31 100644
--- a/src/sp_config_utils.c
+++ b/src/sp_config_utils.c
@@ -1,108 +1,5 @@
1#include "php_snuffleupagus.h" 1#include "php_snuffleupagus.h"
2 2
3int parse_keywords(sp_config_functions *funcs, char *line) {
4 int value_len = 0;
5 const char *original_line = line;
6 for (size_t i = 0; funcs[i].func; i++) {
7 if (!strncmp(funcs[i].token, line, strlen(funcs[i].token))) {
8 line += strlen(funcs[i].token);
9 value_len = funcs[i].func(line, funcs[i].token, funcs[i].retval) + 1;
10 if (value_len == 0) { // bad parameter
11 return -1;
12 }
13 line += value_len;
14 i = -1; // we start the loop again
15 }
16 }
17 while (*line == ';' || *line == '\t' || *line == ' ') {
18 line++;
19 }
20
21 if (*line == '#') {
22 return 0;
23 }
24
25 if (*line) {
26 sp_log_err("config", "Trailing chars '%s' at the end of '%s' on line %zu",
27 line, original_line, sp_line_no);
28 return -1;
29 }
30 return 0;
31}
32
33zend_string *get_param(size_t *consumed, char *restrict line, sp_type type,
34 const char *restrict keyword) {
35 enum { IN_ESCAPE, NONE } state = NONE;
36 char *original_line = line;
37 size_t j = 0;
38
39 zend_string *ret = NULL;
40 if (NULL == line || '\0' == *line) {
41 goto err;
42 }
43
44 ret = zend_string_alloc(strlen(line) + 1, 1);
45
46 /* The first char of a string is always '"', since they MUST be quoted. */
47 if ('"' == *line) {
48 line++;
49 } else {
50 goto err;
51 }
52
53 for (size_t i = 0; line[i] && j < strlen(original_line) - 2; i++) {
54 switch (line[i]) {
55 case '"':
56 /* A double quote at this point is either:
57 - at the very end of the string.
58 - escaped
59 */
60 if ((state == NONE) && (line[i + 1] == SP_TOKEN_END_PARAM)) {
61 /* The `+2` if for
62 1. the terminal double-quote
63 2. the SP_TOKEN_END_PARAM
64 */
65 *consumed = i + 2;
66 // Make sure that the string we return is the right size,
67 // as it can be smaller than strlen(line)
68 ret = zend_string_truncate(ret, j, 1);
69 // truncate does not add a \0
70 ZSTR_VAL(ret)[ZSTR_LEN(ret)] = 0;
71 return ret;
72 } else if (state == IN_ESCAPE) {
73 break; // we're on an escped double quote
74 } else {
75 goto err;
76 }
77 case '\\':
78 if (state == NONE) {
79 state = IN_ESCAPE;
80 continue;
81 }
82 default:
83 break;
84 }
85 if (state == IN_ESCAPE) {
86 state = NONE;
87 }
88 ZSTR_VAL(ret)[j++] = line[i];
89 }
90err:
91 if (0 == j) {
92 sp_log_err("error", "A valid string as parameter is expected on line %zu",
93 sp_line_no);
94 } else {
95 sp_log_err("error",
96 "There is an issue with the parsing of '%s': it doesn't look "
97 "like a valid string on line %zu",
98 original_line ? original_line : "NULL", sp_line_no);
99 }
100 line = NULL;
101 if (ret) {
102 zend_string_release(ret);
103 }
104 return NULL;
105}
106 3
107sp_list_node *parse_functions_list(char *value) { 4sp_list_node *parse_functions_list(char *value) {
108 static const char *sep = ">"; 5 static const char *sep = ">";