[core] consolidate config printing code

funcs use only at startup and only for lighttpd -p
personal/stbuehler/tests-path
Glenn Strauss 2021-05-20 01:18:57 -04:00
parent 942c2f6722
commit fbe55825b4
9 changed files with 173 additions and 212 deletions

View File

@ -555,87 +555,3 @@ array_match_path_or_ext (const array * const a, const buffer * const b)
}
return NULL;
}
#include <stdio.h>
void array_print_indent(int depth) {
int i;
for (i = 0; i < depth; i ++) {
fprintf(stdout, " ");
}
}
uint32_t array_get_max_key_length(const array * const a) {
uint32_t maxlen = 0;
for (uint32_t i = 0; i < a->used; ++i) {
const buffer * const k = &a->data[i]->key;
uint32_t len = buffer_string_length(k);
if (len > maxlen) {
maxlen = len;
}
}
return maxlen;
}
int array_print(const array * const a, int depth) {
uint32_t i;
uint32_t maxlen;
int oneline = 1;
if (a->used > 5) {
oneline = 0;
}
for (i = 0; i < a->used && oneline; i++) {
data_unset *du = a->data[i];
if (!buffer_is_empty(&du->key)) {
oneline = 0;
break;
}
switch (du->type) {
case TYPE_INTEGER:
case TYPE_STRING:
break;
default:
oneline = 0;
break;
}
}
if (oneline) {
fprintf(stdout, "(");
for (i = 0; i < a->used; i++) {
data_unset *du = a->data[i];
if (i != 0) {
fprintf(stdout, ", ");
}
du->fn->print(du, depth + 1);
}
fprintf(stdout, ")");
return 0;
}
maxlen = array_get_max_key_length(a);
fprintf(stdout, "(\n");
for (i = 0; i < a->used; i++) {
data_unset *du = a->data[i];
array_print_indent(depth + 1);
if (!buffer_is_empty(&du->key)) {
int j;
fprintf(stdout, "\"%s\"", du->key.ptr);
for (j = maxlen - buffer_string_length(&du->key); j > 0; j--) {
fprintf(stdout, " ");
}
fprintf(stdout, " => ");
}
du->fn->print(du, depth + 1);
fprintf(stdout, ",\n");
}
array_print_indent(depth);
fprintf(stdout, ")");
return 0;
}

View File

@ -10,7 +10,6 @@ struct data_methods {
struct data_unset *(*copy)(const struct data_unset *src); \
void (*free)(struct data_unset *p); \
int (*insert_dup)(struct data_unset *dst, struct data_unset *src); \
void (*print)(const struct data_unset *p, int depth);
};
typedef enum { TYPE_STRING, TYPE_ARRAY, TYPE_INTEGER, TYPE_CONFIG, TYPE_OTHER } data_type_t;
@ -127,16 +126,6 @@ static inline void array_set_key_value(array * const a, const char * const k, co
__attribute_cold__
void array_replace(array *a, data_unset *entry);
__attribute_cold__
int array_print(const array *a, int depth);
__attribute_cold__
void array_print_indent(int depth);
__attribute_cold__
__attribute_pure__
uint32_t array_get_max_key_length(const array *a);
__attribute_pure__
data_unset * array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen);

View File

@ -1179,9 +1179,177 @@ int config_finalize(server *srv, const buffer *default_server_tag) {
return 1;
}
/* Save some bytes using buffer_append_string() in cold funcs to print config
* (instead of buffer_append_string_len() w/ CONST_STR_LEN() on constant strs)*/
static void config_print_by_type(const data_unset *du, buffer *b, int depth);
static void config_print_indent(buffer *b, int depth) {
depth <<= 2;
memset(buffer_extend(b, depth), ' ', depth);
}
__attribute_pure__
static uint32_t config_print_array_max_klen(const array * const a) {
uint32_t maxlen = 0;
for (uint32_t i = 0; i < a->used; ++i) {
uint32_t len = buffer_string_length(&a->data[i]->key);
if (maxlen < len)
maxlen = len;
}
return maxlen;
}
static void config_print_array(const array * const a, buffer * const b, int depth) {
if (a->used <= 5 && (!a->used || buffer_is_empty(&a->data[0]->key))) {
int oneline = 1;
for (uint32_t i = 0; i < a->used; ++i) {
data_unset *du = a->data[i];
if (du->type != TYPE_STRING && du->type != TYPE_INTEGER) {
oneline = 0;
break;
}
}
if (oneline) {
buffer_append_string(b, "(");
for (uint32_t i = 0; i < a->used; ++i) {
if (i != 0)
buffer_append_string(b, ", ");
config_print_by_type(a->data[i], b, depth + 1);
}
buffer_append_string(b, ")");
return;
}
}
const uint32_t maxlen = config_print_array_max_klen(a);
buffer_append_string(b, "(\n");
for (uint32_t i = 0; i < a->used; ++i) {
config_print_indent(b, depth + 1);
data_unset *du = a->data[i];
if (!buffer_is_empty(&du->key)) {
buffer_append_str3(b, CONST_STR_LEN("\""),
CONST_BUF_LEN(&du->key),
CONST_STR_LEN("\""));
int indent = (int)(maxlen - buffer_string_length(&du->key));
if (indent > 0)
memset(buffer_extend(b, indent), ' ', indent);
buffer_append_string(b, " => ");
}
config_print_by_type(du, b, depth + 1);
buffer_append_string(b, ",\n");
}
config_print_indent(b, depth);
buffer_append_string(b, ")");
}
static void config_print_config(const data_unset *d, buffer * const b, int depth) {
data_config *dc = (data_config *)d;
array *a = (array *)dc->value;
if (0 == dc->context_ndx) {
buffer_append_string(b, "config {\n");
}
else {
if (dc->cond != CONFIG_COND_ELSE) {
buffer_append_string(b, dc->comp_key);
buffer_append_string(b, " ");
}
buffer_append_string(b, "{\n");
config_print_indent(b, depth + 1);
buffer_append_string(b, "# block ");
buffer_append_int(b, dc->context_ndx);
buffer_append_string(b, "\n");
}
++depth;
const uint32_t maxlen = config_print_array_max_klen(a);
for (uint32_t i = 0; i < a->used; ++i) {
config_print_indent(b, depth);
data_unset *du = a->data[i];
buffer_append_string_buffer(b, &du->key);
int indent = (int)(maxlen - buffer_string_length(&du->key));
if (indent > 0)
memset(buffer_extend(b, indent), ' ', indent);
buffer_append_string(b, " = ");
config_print_by_type(du, b, depth);
buffer_append_string(b, "\n");
}
buffer_append_string(b, "\n");
for (uint32_t i = 0; i < dc->children.used; ++i) {
data_config *dcc = dc->children.data[i];
/* only the 1st block of chaining */
if (NULL == dcc->prev) {
buffer_append_string(b, "\n");
config_print_indent(b, depth);
config_print_by_type((data_unset *) dcc, b, depth);
buffer_append_string(b, "\n");
}
}
--depth;
config_print_indent(b, depth);
buffer_append_string(b, "}");
if (0 != dc->context_ndx) {
buffer_append_string(b, " # end of ");
buffer_append_string(b, (dc->cond != CONFIG_COND_ELSE)
? dc->comp_key
: "else");
}
if (dc->next) {
buffer_append_string(b, "\n");
config_print_indent(b, depth);
buffer_append_string(b, "else ");
config_print_by_type((data_unset *)dc->next, b, depth);
}
}
static void config_print_string(const data_unset *du, buffer * const b) {
/* print out the string as is, except prepend '"' with backslash */
const buffer * const vb = &((data_string *)du)->value;
char *dst = buffer_string_prepare_append(b, buffer_string_length(vb)*2);
uint32_t n = 0;
dst[n++] = '"';
if (vb->ptr) {
for (const char *p = vb->ptr; *p; ++p) {
if (*p == '"')
dst[n++] = '\\';
dst[n++] = *p;
}
}
dst[n++] = '"';
buffer_commit(b, n);
}
__attribute_cold__
static void config_print_by_type(const data_unset * const du, buffer * const b, int depth) {
switch (du->type) {
case TYPE_STRING:
config_print_string(du, b);
break;
case TYPE_INTEGER:
buffer_append_int(b, ((data_integer *)du)->value);
break;
case TYPE_ARRAY:
config_print_array(&((data_array *)du)->value, b, depth);
break;
case TYPE_CONFIG:
config_print_config(du, b, depth);
break;
default:
/*if (du->fn->print) du->fn->print(du, b, depth);*/
break;
}
}
void config_print(server *srv) {
buffer_clear(srv->tmp_buf);
data_unset *dc = srv->config_context->data[0];
dc->fn->print(dc, 0);
config_print_by_type(dc, srv->tmp_buf, 0);
}
void config_free(server *srv) {

View File

@ -1,8 +1,8 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <string.h>
#include <stdlib.h>
__attribute_cold__
@ -33,19 +33,11 @@ static int data_array_insert_dup(data_unset *dst, data_unset *src) {
return 0;
}
__attribute_cold__
static void data_array_print(const data_unset *d, int depth) {
data_array *ds = (data_array *)d;
array_print(&ds->value, depth);
}
data_array *data_array_init(void) {
static const struct data_methods fn = {
data_array_copy,
data_array_free,
data_array_insert_dup,
data_array_print,
};
data_array *ds;

View File

@ -4,7 +4,6 @@
#include "configfile.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_PCRE_H
@ -58,81 +57,11 @@ static int data_config_insert_dup(data_unset *dst, data_unset *src) {
return 0;
}
__attribute_cold__
static void data_config_print(const data_unset *d, int depth) {
data_config *ds = (data_config *)d;
array *a = (array *)ds->value;
size_t i;
size_t maxlen;
if (0 == ds->context_ndx) {
fprintf(stdout, "config {\n");
}
else {
if (ds->cond != CONFIG_COND_ELSE) {
fprintf(stdout, "%s {\n", ds->comp_key);
} else {
fprintf(stdout, "{\n");
}
array_print_indent(depth + 1);
fprintf(stdout, "# block %d\n", ds->context_ndx);
}
depth ++;
maxlen = array_get_max_key_length(a);
for (i = 0; i < a->used; i ++) {
data_unset *du = a->data[i];
size_t len = buffer_string_length(&du->key);
size_t j;
array_print_indent(depth);
fprintf(stdout, "%s", du->key.ptr);
for (j = maxlen - len; j > 0; j --) {
fprintf(stdout, " ");
}
fprintf(stdout, " = ");
du->fn->print(du, depth);
fprintf(stdout, "\n");
}
fprintf(stdout, "\n");
for (i = 0; i < ds->children.used; i ++) {
data_config *dc = ds->children.data[i];
/* only the 1st block of chaining */
if (NULL == dc->prev) {
fprintf(stdout, "\n");
array_print_indent(depth);
dc->fn->print((data_unset *) dc, depth);
fprintf(stdout, "\n");
}
}
depth --;
array_print_indent(depth);
fprintf(stdout, "}");
if (0 != ds->context_ndx) {
if (ds->cond != CONFIG_COND_ELSE) {
fprintf(stdout, " # end of %s", ds->comp_key);
} else {
fprintf(stdout, " # end of else");
}
}
if (ds->next) {
fprintf(stdout, "\n");
array_print_indent(depth);
fprintf(stdout, "else ");
ds->next->fn->print((data_unset *)ds->next, depth);
}
}
data_config *data_config_init(void) {
static const struct data_methods fn = {
data_config_copy,
data_config_free,
data_config_insert_dup,
data_config_print,
};
data_config *ds;

View File

@ -1,10 +1,9 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
__attribute_cold__
static data_unset *data_integer_copy(const data_unset *s) {
@ -33,21 +32,11 @@ static int data_integer_insert_dup(data_unset *dst, data_unset *src) {
return 0;
}
__attribute_cold__
static void data_integer_print(const data_unset *d, int depth) {
data_integer *ds = (data_integer *)d;
UNUSED(depth);
fprintf(stdout, "%d", ds->value);
}
data_integer *data_integer_init(void) {
static const struct data_methods fn = {
data_integer_copy,
data_integer_free,
data_integer_insert_dup,
data_integer_print,
};
data_integer *ds;

View File

@ -1,9 +1,8 @@
#include "first.h"
#include "array.h"
#include "buffer.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
__attribute_cold__
@ -41,31 +40,11 @@ static int data_string_insert_dup(data_unset *dst, data_unset *src) {
return 0;
}
__attribute_cold__
static void data_string_print(const data_unset *d, int depth) {
data_string *ds = (data_string *)d;
UNUSED(depth);
/* print out the string as is, except prepend " with backslash */
putc('"', stdout);
const char *p = ds->value.ptr;
if (p) {
for (; *p; ++p) {
if (*p == '"')
putc('\\', stdout);
putc(*p, stdout);
}
}
putc('"', stdout);
}
data_string *data_string_init(void) {
static const struct data_methods fn = {
data_string_copy,
data_string_free,
data_string_insert_dup,
data_string_print,
};
data_string *ds;

View File

@ -265,7 +265,6 @@ static data_auth *data_auth_init(void)
NULL, /* copy must not be called on this data */
data_auth_free,
NULL, /* insert_dup must not be called on this data */
NULL /* print must not be called on this data */
};
data_auth * const dauth = calloc(1, sizeof(*dauth));
force_assert(NULL != dauth);

View File

@ -1147,7 +1147,7 @@ static int server_main_setup (server * const srv, int argc, char **argv) {
if (print_config) {
config_print(srv);
fprintf(stdout, "\n");
puts(srv->tmp_buf->ptr);
}
if (test_config) {