1: #include "first.h"
2:
3: #include "array.h"
4:
5: #include <string.h>
6: #include <stdio.h>
7: #include <stdlib.h>
8:
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();
12:
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;
18: }
19:
20: static void data_config_free(data_unset *d) {
21: data_config *ds = (data_config *)d;
22:
23: buffer_free(ds->key);
24: buffer_free(ds->op);
25: buffer_free(ds->comp_key);
26:
27: array_free(ds->value);
28: vector_config_weak_clear(&ds->children);
29:
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
35:
36: free(d);
37: }
38:
39: static void data_config_reset(data_unset *d) {
40: data_config *ds = (data_config *)d;
41:
42: /* reused array elements */
43: buffer_reset(ds->key);
44: buffer_reset(ds->comp_key);
45: array_reset(ds->value);
46: }
47:
48: static int data_config_insert_dup(data_unset *dst, data_unset *src) {
49: UNUSED(dst);
50:
51: src->free(src);
52:
53: return 0;
54: }
55:
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;
61:
62: if (0 == ds->context_ndx) {
63: fprintf(stdout, "config {\n");
64: }
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);
70: }
71: depth ++;
72:
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;
78:
79: array_print_indent(depth);
80: fprintf(stdout, "%s", du->key->ptr);
81: for (j = maxlen - len; j > 0; j --) {
82: fprintf(stdout, " ");
83: }
84: fprintf(stdout, " = ");
85: du->print(du, depth);
86: fprintf(stdout, "\n");
87: }
88:
89: fprintf(stdout, "\n");
90: for (i = 0; i < ds->children.used; i ++) {
91: data_config *dc = ds->children.data[i];
92:
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");
99: }
100: }
101:
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);
108: }
109:
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);
115: }
116: }
117:
118: data_config *data_config_init(void) {
119: data_config *ds;
120:
121: ds = calloc(1, sizeof(*ds));
122:
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);
128:
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;
135:
136: return ds;
137: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>