[security] encode quoting chars in HTML and XML
[lighttpd.git] / src / data_config.c
blobae7aa1dccc4ab244c6c24247a5cb1940017a6b6b
1 #include "first.h"
3 #include "array.h"
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 static data_unset *data_config_copy(const data_unset *s) {
10 data_config *src = (data_config *)s;
11 data_config *ds = data_config_init();
13 buffer_copy_buffer(ds->key, src->key);
14 buffer_copy_buffer(ds->comp_key, src->comp_key);
15 array_free(ds->value);
16 ds->value = array_init_array(src->value);
17 return (data_unset *)ds;
20 static void data_config_free(data_unset *d) {
21 data_config *ds = (data_config *)d;
23 buffer_free(ds->key);
24 buffer_free(ds->op);
25 buffer_free(ds->comp_key);
27 array_free(ds->value);
28 vector_config_weak_clear(&ds->children);
30 if (ds->string) buffer_free(ds->string);
31 #ifdef HAVE_PCRE_H
32 if (ds->regex) pcre_free(ds->regex);
33 if (ds->regex_study) pcre_free(ds->regex_study);
34 #endif
36 free(d);
39 static void data_config_reset(data_unset *d) {
40 data_config *ds = (data_config *)d;
42 /* reused array elements */
43 buffer_reset(ds->key);
44 buffer_reset(ds->comp_key);
45 array_reset(ds->value);
48 static int data_config_insert_dup(data_unset *dst, data_unset *src) {
49 UNUSED(dst);
51 src->free(src);
53 return 0;
56 static void data_config_print(const data_unset *d, int depth) {
57 data_config *ds = (data_config *)d;
58 array *a = (array *)ds->value;
59 size_t i;
60 size_t maxlen;
62 if (0 == ds->context_ndx) {
63 fprintf(stdout, "config {\n");
65 else {
66 fprintf(stdout, "$%s %s \"%s\" {\n",
67 ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
68 array_print_indent(depth + 1);
69 fprintf(stdout, "# block %d\n", ds->context_ndx);
71 depth ++;
73 maxlen = array_get_max_key_length(a);
74 for (i = 0; i < a->used; i ++) {
75 data_unset *du = a->data[i];
76 size_t len = strlen(du->key->ptr);
77 size_t j;
79 array_print_indent(depth);
80 fprintf(stdout, "%s", du->key->ptr);
81 for (j = maxlen - len; j > 0; j --) {
82 fprintf(stdout, " ");
84 fprintf(stdout, " = ");
85 du->print(du, depth);
86 fprintf(stdout, "\n");
89 fprintf(stdout, "\n");
90 for (i = 0; i < ds->children.used; i ++) {
91 data_config *dc = ds->children.data[i];
93 /* only the 1st block of chaining */
94 if (NULL == dc->prev) {
95 fprintf(stdout, "\n");
96 array_print_indent(depth);
97 dc->print((data_unset *) dc, depth);
98 fprintf(stdout, "\n");
102 depth --;
103 array_print_indent(depth);
104 fprintf(stdout, "}");
105 if (0 != ds->context_ndx) {
106 fprintf(stdout, " # end of $%s %s \"%s\"",
107 ds->comp_key->ptr, ds->op->ptr, ds->string->ptr);
110 if (ds->next) {
111 fprintf(stdout, "\n");
112 array_print_indent(depth);
113 fprintf(stdout, "else ");
114 ds->next->print((data_unset *)ds->next, depth);
118 data_config *data_config_init(void) {
119 data_config *ds;
121 ds = calloc(1, sizeof(*ds));
123 ds->key = buffer_init();
124 ds->op = buffer_init();
125 ds->comp_key = buffer_init();
126 ds->value = array_init();
127 vector_config_weak_init(&ds->children);
129 ds->copy = data_config_copy;
130 ds->free = data_config_free;
131 ds->reset = data_config_reset;
132 ds->insert_dup = data_config_insert_dup;
133 ds->print = data_config_print;
134 ds->type = TYPE_CONFIG;
136 return ds;