diff options
Diffstat (limited to 'src/format.c')
-rw-r--r-- | src/format.c | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/src/format.c b/src/format.c index af87329..7870ef2 100644 --- a/src/format.c +++ b/src/format.c @@ -1,4 +1,4 @@ -/* grecs - Gray's Extensible Configuration System +/* argot - Gray's Extensible Configuration System Copyright (C) 2007-2016 Sergey Poznyakoff Grecs is free software; you can redistribute it and/or modify it @@ -17,7 +17,7 @@ #ifdef HAVE_CONFIG_H # include <config.h> #endif -#include <grecs.h> +#include <argot.h> #include <wordsplit.h> #include <stdio.h> #include <stdlib.h> @@ -26,47 +26,47 @@ #include <errno.h> const char * -grecs_data_type_string(enum grecs_data_type type) +argot_data_type_string(enum argot_data_type type) { switch (type) { - case grecs_type_void: + case argot_type_void: return "void"; - case grecs_type_string: + case argot_type_string: return "string"; - case grecs_type_short: - case grecs_type_ushort: - case grecs_type_int: - case grecs_type_uint: - case grecs_type_long: - case grecs_type_ulong: - case grecs_type_size: - /*FIXME case grecs_type_off:*/ + case argot_type_short: + case argot_type_ushort: + case argot_type_int: + case argot_type_uint: + case argot_type_long: + case argot_type_ulong: + case argot_type_size: + /*FIXME case argot_type_off:*/ return "number"; - case grecs_type_time: + case argot_type_time: return "time"; - case grecs_type_bool: + case argot_type_bool: return "boolean"; - case grecs_type_ipv4: + case argot_type_ipv4: return "IPv4"; - case grecs_type_cidr: + case argot_type_cidr: return "CIDR"; - case grecs_type_host: + case argot_type_host: return "hostname"; - case grecs_type_sockaddr: + case argot_type_sockaddr: return "sockaddr"; - case grecs_type_section: + case argot_type_section: return "section"; - case grecs_type_null: + case argot_type_null: return "null"; } return "UNKNOWN?"; @@ -80,7 +80,7 @@ format_level(unsigned level, FILE *stream) } void -grecs_print_docstring(const char *docstring, unsigned level, FILE *stream) +argot_print_docstring(const char *docstring, unsigned level, FILE *stream) { size_t len = strlen(docstring); int width = 78 - level * 2; @@ -124,15 +124,15 @@ grecs_print_docstring(const char *docstring, unsigned level, FILE *stream) } void -grecs_print_simple_statement(struct grecs_keyword *kwp, unsigned level, +argot_print_simple_statement(struct argot_keyword *kwp, unsigned level, FILE *stream) { const char *argstr; - if (kwp->flags & GRECS_INAC) - grecs_print_docstring(N_("Disabled;"), level, stream); + if (kwp->flags & ARGOT_INAC) + argot_print_docstring(N_("Disabled;"), level, stream); if (kwp->docstring) - grecs_print_docstring(kwp->docstring, level, stream); + argot_print_docstring(kwp->docstring, level, stream); format_level(level, stream); if (kwp->argname) @@ -148,22 +148,22 @@ grecs_print_simple_statement(struct grecs_keyword *kwp, unsigned level, fprintf(stream, "%s <%s>;\n", kwp->ident, gettext(argstr)); else { fprintf(stream, "%s <%s: ", kwp->ident, gettext(argstr)); - if (kwp->flags & GRECS_LIST) + if (kwp->flags & ARGOT_LIST) fprintf(stream, "list of %s", - gettext(grecs_data_type_string(kwp->type))); + gettext(argot_data_type_string(kwp->type))); else fprintf(stream, "%s", - gettext(grecs_data_type_string(kwp->type))); + gettext(argot_data_type_string(kwp->type))); fprintf(stream, ">;\n"); } } void -grecs_print_block_statement(struct grecs_keyword *kwp, unsigned level, +argot_print_block_statement(struct argot_keyword *kwp, unsigned level, FILE *stream) { if (kwp->docstring) - grecs_print_docstring(kwp->docstring, level, stream); + argot_print_docstring(kwp->docstring, level, stream); format_level(level, stream); fprintf(stream, "%s", kwp->ident); if (kwp->argname) { @@ -173,13 +173,13 @@ grecs_print_block_statement(struct grecs_keyword *kwp, unsigned level, fprintf(stream, " <%s>", gettext(kwp->argname)); } fprintf(stream, " {\n"); - grecs_print_statement_array(kwp->kwd, 0, level + 1, stream); + argot_print_statement_array(kwp->kwd, 0, level + 1, stream); format_level(level, stream); fprintf(stream, "}\n"); } void -grecs_print_statement_array(struct grecs_keyword *kwp, +argot_print_statement_array(struct argot_keyword *kwp, unsigned n, unsigned level, FILE *stream) @@ -188,47 +188,47 @@ grecs_print_statement_array(struct grecs_keyword *kwp, return; } for (; kwp->ident; kwp++, n++) { - if (kwp->flags & GRECS_HIDDEN) + if (kwp->flags & ARGOT_HIDDEN) continue; if (n) fputc('\n', stream); - if (kwp->type == grecs_type_section) - grecs_print_block_statement(kwp, level, stream); + if (kwp->type == argot_type_section) + argot_print_block_statement(kwp, level, stream); else - grecs_print_simple_statement(kwp, level, stream); + argot_print_simple_statement(kwp, level, stream); } } void -grecs_format_locus(grecs_locus_t *locus, struct grecs_format_closure *clos) +argot_format_locus(argot_locus_t *locus, struct argot_format_closure *clos) { if (locus) { char *str = NULL; size_t size = 0; if (locus->beg.col == 0) - grecs_asprintf(&str, &size, "%s:%u", + argot_asprintf(&str, &size, "%s:%u", locus->beg.file, locus->beg.line); else if (strcmp(locus->beg.file, locus->end.file)) - grecs_asprintf(&str, &size, "%s:%u.%u-%s:%u.%u", + argot_asprintf(&str, &size, "%s:%u.%u-%s:%u.%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.file, locus->end.line, locus->end.col); else if (locus->beg.line != locus->end.line) - grecs_asprintf(&str, &size, "%s:%u.%u-%u.%u", + argot_asprintf(&str, &size, "%s:%u.%u-%u.%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.line, locus->end.col); else if (locus->beg.col != locus->end.col) - grecs_asprintf(&str, &size, "%s:%u.%u-%u", + argot_asprintf(&str, &size, "%s:%u.%u-%u", locus->beg.file, locus->beg.line, locus->beg.col, locus->end.col); else - grecs_asprintf(&str, &size, "%s:%u.%u", + argot_asprintf(&str, &size, "%s:%u.%u", locus->beg.file, locus->beg.line, locus->beg.col); @@ -239,8 +239,8 @@ grecs_format_locus(grecs_locus_t *locus, struct grecs_format_closure *clos) } void -grecs_format_node_path(struct grecs_node *node, int flags, - struct grecs_format_closure *clos) +argot_format_node_path(struct argot_node *node, int flags, + struct argot_format_closure *clos) { char delim[2] = "."; @@ -250,80 +250,80 @@ grecs_format_node_path(struct grecs_node *node, int flags, } if (node->up) - grecs_format_node_path(node->up, flags, clos); - if (node->type == grecs_node_root) + argot_format_node_path(node->up, flags, clos); + if (node->type == argot_node_root) return; - if (flags & _GRECS_NODE_MASK_DELIM) - delim[0] = flags & _GRECS_NODE_MASK_DELIM; + if (flags & _ARGOT_NODE_MASK_DELIM) + delim[0] = flags & _ARGOT_NODE_MASK_DELIM; clos->fmtfun(delim, clos->data); clos->fmtfun(node->ident, clos->data); - if (node->type == grecs_node_block && - !GRECS_VALUE_EMPTY_P(node->v.value)) { + if (node->type == argot_node_block && + !ARGOT_VALUE_EMPTY_P(node->v.value)) { clos->fmtfun("=", clos->data); - grecs_format_value(node->v.value, flags|GRECS_NODE_FLAG_QUOTE, + argot_format_value(node->v.value, flags|ARGOT_NODE_FLAG_QUOTE, clos); } } void -grecs_format_value(struct grecs_value *val, int flags, - struct grecs_format_closure *clos) +argot_format_value(struct argot_value *val, int flags, + struct argot_format_closure *clos) { int i; - struct grecs_list_entry *ep; + struct argot_list_entry *ep; size_t clen; int need_quote; if (!val) return; switch (val->type) { - case GRECS_TYPE_STRING: + case ARGOT_TYPE_STRING: clen = wordsplit_c_quoted_length(val->v.string, - flags & GRECS_NODE_FLAG_QUOTE_HEX, + flags & ARGOT_NODE_FLAG_QUOTE_HEX, &need_quote); - if (flags & GRECS_NODE_FLAG_QUOTE) + if (flags & ARGOT_NODE_FLAG_QUOTE) need_quote = 1; - else if (flags & GRECS_NODE_FLAG_NOQUOTE) + else if (flags & ARGOT_NODE_FLAG_NOQUOTE) need_quote = 0; if (need_quote) { - char *cbuf = grecs_malloc(clen + 1); + char *cbuf = argot_malloc(clen + 1); wordsplit_c_quote_copy(cbuf, val->v.string, - flags & GRECS_NODE_FLAG_QUOTE_HEX); + flags & ARGOT_NODE_FLAG_QUOTE_HEX); cbuf[clen] = 0; clos->fmtfun("\"", clos->data); clos->fmtfun(cbuf, clos->data); clos->fmtfun("\"", clos->data); - grecs_free(cbuf); + argot_free(cbuf); } else clos->fmtfun(val->v.string, clos->data); break; - case GRECS_TYPE_LIST: + case ARGOT_TYPE_LIST: clos->fmtfun("(", clos->data); for (ep = val->v.list->head; ep; ep = ep->next) { - grecs_format_value(ep->data, flags, clos); + argot_format_value(ep->data, flags, clos); if (ep->next) clos->fmtfun(", ", clos->data); } clos->fmtfun(")", clos->data); break; - case GRECS_TYPE_ARRAY: + case ARGOT_TYPE_ARRAY: for (i = 0; i < val->v.arg.c; i++) { if (i) clos->fmtfun(" ", clos->data); - grecs_format_value(val->v.arg.v[i], flags, clos); + argot_format_value(val->v.arg.v[i], flags, clos); } } } int -grecs_format_node(struct grecs_node *node, int flags, - struct grecs_format_closure *clos) +argot_format_node(struct argot_node *node, int flags, + struct argot_format_closure *clos) { const char *delim_str = NULL; - if (!(flags & _GRECS_NODE_MASK_OUTPUT)) { + if (!(flags & _ARGOT_NODE_MASK_OUTPUT)) { errno = EINVAL; return 1; } @@ -334,43 +334,43 @@ grecs_format_node(struct grecs_node *node, int flags, } switch (node->type) { - case grecs_node_root: - case grecs_node_block: - if (flags & GRECS_NODE_FLAG_DESCEND) { + case argot_node_root: + case argot_node_block: + if (flags & ARGOT_NODE_FLAG_DESCEND) { for (node = node->down; node; node = node->next) { - grecs_format_node(node, flags, clos); + argot_format_node(node, flags, clos); if (node->next) clos->fmtfun("\n", clos->data); } break; } - case grecs_node_stmt: - if (flags & GRECS_NODE_FLAG_LOCUS) { - grecs_locus_t *locus; + case argot_node_stmt: + if (flags & ARGOT_NODE_FLAG_LOCUS) { + argot_locus_t *locus; - if (flags & GRECS_NODE_FLAG_PATH) { - if (flags & GRECS_NODE_FLAG_VALUE) + if (flags & ARGOT_NODE_FLAG_PATH) { + if (flags & ARGOT_NODE_FLAG_VALUE) locus = &node->locus; else locus = &node->idloc; - } else if (flags & GRECS_NODE_FLAG_VALUE) + } else if (flags & ARGOT_NODE_FLAG_VALUE) locus = &node->v.value->locus; else locus = &node->locus; - grecs_format_locus(locus, clos); + argot_format_locus(locus, clos); delim_str = ": "; } - if (flags & GRECS_NODE_FLAG_PATH) { + if (flags & ARGOT_NODE_FLAG_PATH) { if (delim_str) clos->fmtfun(delim_str, clos->data); - grecs_format_node_path(node, flags, clos); + argot_format_node_path(node, flags, clos); delim_str = ": "; } - if (flags & GRECS_NODE_FLAG_VALUE) { + if (flags & ARGOT_NODE_FLAG_VALUE) { if (delim_str) clos->fmtfun(delim_str, clos->data); - grecs_format_value(node->v.value, flags, clos); + argot_format_value(node->v.value, flags, clos); } } return 0; @@ -380,17 +380,17 @@ grecs_format_node(struct grecs_node *node, int flags, static int txtacc_fmt(const char *str, void *data) { - struct grecs_txtacc *acc = data; - grecs_txtacc_grow(acc, str, strlen(str)); + struct argot_txtacc *acc = data; + argot_txtacc_grow(acc, str, strlen(str)); return 0; } void -grecs_txtacc_format_value(struct grecs_value *val, int flags, - struct grecs_txtacc *acc) +argot_txtacc_format_value(struct argot_value *val, int flags, + struct argot_txtacc *acc) { - struct grecs_format_closure clos = { txtacc_fmt, acc }; - grecs_format_value(val, flags, &clos); + struct argot_format_closure clos = { txtacc_fmt, acc }; + argot_format_value(val, flags, &clos); } @@ -402,29 +402,29 @@ file_fmt(const char *str, void *data) } void -grecs_print_locus(grecs_locus_t *locus, FILE *fp) +argot_print_locus(argot_locus_t *locus, FILE *fp) { - struct grecs_format_closure clos = { file_fmt, fp }; - grecs_format_locus(locus, &clos); + struct argot_format_closure clos = { file_fmt, fp }; + argot_format_locus(locus, &clos); } void -grecs_print_node_path(struct grecs_node *node, int flag, FILE *fp) +argot_print_node_path(struct argot_node *node, int flag, FILE *fp) { - struct grecs_format_closure clos = { file_fmt, fp }; - grecs_format_node_path(node, flag, &clos); + struct argot_format_closure clos = { file_fmt, fp }; + argot_format_node_path(node, flag, &clos); } void -grecs_print_value(struct grecs_value *val, int flags, FILE *fp) +argot_print_value(struct argot_value *val, int flags, FILE *fp) { - struct grecs_format_closure clos = { file_fmt, fp }; - grecs_format_value(val, flags, &clos); + struct argot_format_closure clos = { file_fmt, fp }; + argot_format_value(val, flags, &clos); } int -grecs_print_node(struct grecs_node *node, int flags, FILE *fp) +argot_print_node(struct argot_node *node, int flags, FILE *fp) { - struct grecs_format_closure clos = { file_fmt, fp }; - return grecs_format_node(node, flags, &clos); + struct argot_format_closure clos = { file_fmt, fp }; + return argot_format_node(node, flags, &clos); } |