Index: ossp-pkg/cfg/Makefile RCS File: /v/ossp/cvs/ossp-pkg/cfg/Attic/Makefile,v rcsdiff -q -kk '-r1.3' '-r1.4' -u '/v/ossp/cvs/ossp-pkg/cfg/Attic/Makefile,v' 2>/dev/null --- Makefile 2002/07/04 12:30:46 1.3 +++ Makefile 2002/07/04 14:51:21 1.4 @@ -1,6 +1,6 @@ CC = /usr/opkg/bin/gcc -CFLAGS = -pipe -O2 -g -ggdb3 \ +CFLAGS = -pipe -O0 -g -ggdb3 \ -ansi -pedantic -Wall \ -Wmultichar -Wno-system-headers -Wtraditional \ -Wshadow -Wpointer-arith \ Index: ossp-pkg/cfg/cfg_buf.c RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_buf.c,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_buf.c,v' 2>/dev/null --- cfg_buf.c 2002/07/04 12:18:47 1.1 +++ cfg_buf.c 2002/07/04 14:51:21 1.2 @@ -86,11 +86,20 @@ cfg_rc_t cfg_buf_format(cfg_buf_t *buf, const char *fmt, ...) { - cfg_rc_t rc; va_list ap; - int n; + cfg_rc_t rc; va_start(ap, fmt); + rc = cfg_buf_vformat(buf, fmt, ap); + va_end(ap); + return rc; +} + +cfg_rc_t cfg_buf_vformat(cfg_buf_t *buf, const char *fmt, va_list ap) +{ + cfg_rc_t rc; + int n; + if (buf == NULL || fmt == NULL) return CFG_ERR_ARG; if ((n = cfg_fmt_vsprintf(NULL, -1, fmt, ap)) == -1) @@ -102,7 +111,6 @@ fmt, ap)) == -1) return CFG_ERR_FMT; buf->buf_len += n; - va_end(ap); return CFG_OK; } Index: ossp-pkg/cfg/cfg_buf.h RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_buf.h,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_buf.h,v' 2>/dev/null --- cfg_buf.h 2002/07/04 12:18:47 1.1 +++ cfg_buf.h 2002/07/04 14:51:21 1.2 @@ -45,6 +45,7 @@ extern cfg_rc_t cfg_buf_create (cfg_buf_t **buf); extern cfg_rc_t cfg_buf_resize (cfg_buf_t *buf, int n); extern cfg_rc_t cfg_buf_format (cfg_buf_t *buf, const char *fmt, ...); +extern cfg_rc_t cfg_buf_vformat (cfg_buf_t *buf, const char *fmt, va_list ap); extern cfg_rc_t cfg_buf_content (cfg_buf_t *buf, char **ptr, size_t *len, size_t *size); extern cfg_rc_t cfg_buf_destroy (cfg_buf_t *buf); Index: ossp-pkg/cfg/cfg_syn.c RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_syn.c,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_syn.c,v' 2>/dev/null --- cfg_syn.c 2002/07/03 13:25:34 1.1 +++ cfg_syn.c 2002/07/04 14:51:21 1.2 @@ -28,11 +28,14 @@ ** cfg_syn.c: syntax parsing */ +#include + #include "cfg.h" #include "cfg_grid.h" #include "cfg_node.h" #include "cfg_fmt.h" #include "cfg_syn.h" +#include "cfg_buf.h" /* prototypes for Flex-generated scanner */ extern int cfg_syn_lex_init(void *); @@ -43,10 +46,18 @@ extern int cfg_syn_parse(void *); /* build a node tree according to a textual specification */ -cfg_rc_t cfg_syn(cfg_t *cfg, cfg_node_t **node, char *err_buf, size_t err_len, const char *input) +cfg_rc_t cfg_syn_import( + cfg_t *cfg, + cfg_node_t **node, + const char *input, + char *err_buf, size_t err_len) { cfg_syn_ctx_t ctx; void *yyscan; + + /* argument sanity checking */ + if (node == NULL || input == NULL) + return CFG_ERR_ARG; /* initialize scanner */ cfg_syn_lex_init(&yyscan); @@ -147,3 +158,118 @@ return; } +typedef struct { + cfg_buf_t *buf; + int indent; +} export_t; + +static void export_format(export_t *ctx, char *fmt, ...) +{ + int i; + char *cp; + va_list ap; + char *str; + + va_start(ap, fmt); + if ((str = cfg_fmt_vasprintf(fmt, ap)) == NULL) + return; + while ((cp = strchr(str, '\n')) != NULL) { + cfg_buf_format(ctx->buf, "%.*s", cp-str+1, str); + for (i = 0; i < ctx->indent; i++) + cfg_buf_format(ctx->buf, " "); + str = cp+1; + } + if (str[0] != '\0') + cfg_buf_format(ctx->buf, "%s", str); + free(str); + va_end(ap); + return; +} + +static void export_indent(export_t *ctx, int n) +{ + if (n > 0) { + while (n > 0) { + n--; + ctx->indent++; + cfg_buf_format(ctx->buf, " "); + } + } + else { + while (n < 0 && ctx->indent > 0) { + n++; + ctx->indent--; + cfg_buf_resize(ctx->buf, -4); + } + } +} + +static void export_token(export_t *ctx, const char *token) +{ + if (strcspn(token, " {};\\") != strlen(token)) + export_format(ctx, "\"%s\"", token); + else + export_format(ctx, "%s", token); + return; +} + +static void export_node(export_t *ctx, cfg_node_t *node) +{ + cfg_node_type_t type; + cfg_node_t *node2; + cfg_rc_t rc; + char *token; + + rc = cfg_node_get(node, CFG_NODE_ATTR_TYPE, &type); + if (type == CFG_NODE_TYPE_SEQ) { + export_format(ctx, "{\n"); + export_indent(ctx, 1); + cfg_node_goto(node, CFG_NODE_GOTO_CHILD1, &node2); + while (node2 != NULL) { + export_node(ctx, node2); + cfg_node_goto(node2, CFG_NODE_GOTO_RBROTH, &node2); + } + export_indent(ctx, -1); + export_format(ctx, "}"); + } + else if (type == CFG_NODE_TYPE_DIR) { + cfg_node_goto(node, CFG_NODE_GOTO_CHILD1, &node2); + while (node2 != NULL) { + export_node(ctx, node2); + cfg_node_goto(node2, CFG_NODE_GOTO_RBROTH, &node2); + if (node2 != NULL) + export_format(ctx, " "); + } + export_format(ctx, ";\n"); + } + else if (type == CFG_NODE_TYPE_TOK) { + cfg_node_get(node, CFG_NODE_ATTR_TOKEN, &token); + if (token != NULL) + export_token(ctx, token); + else + export_format(ctx, ""); + } + return; +} + +cfg_rc_t cfg_syn_export( + cfg_t *cfg, + cfg_node_t *node, + char **output) +{ + cfg_buf_t *buf; + cfg_rc_t rc; + export_t ctx; + + if (node == NULL || output == NULL) + return CFG_ERR_ARG; + if ((rc = cfg_buf_create(&buf)) != CFG_OK) + return rc; + ctx.buf = buf; + ctx.indent = 0; + export_node(&ctx, node); + cfg_buf_content(buf, output, NULL, NULL); + cfg_buf_destroy(buf); + return CFG_OK; +} + Index: ossp-pkg/cfg/cfg_syn.h RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_syn.h,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_syn.h,v' 2>/dev/null --- cfg_syn.h 2002/07/03 13:25:34 1.1 +++ cfg_syn.h 2002/07/04 14:51:21 1.2 @@ -65,7 +65,8 @@ #define last_column last /* internal API */ -extern cfg_rc_t cfg_syn(cfg_t *cfg, cfg_node_t **node, char *err_buf, size_t err_len, const char *input); +extern cfg_rc_t cfg_syn_import (cfg_t *cfg, cfg_node_t **node, const char *input, char *err_buf, size_t err_len); +extern cfg_rc_t cfg_syn_export (cfg_t *cfg, cfg_node_t *node, char **output); /* error reporting function */ extern void cfg_syn_error(cfg_syn_ctx_t *ctx, cfg_rc_t rv, YYLTYPE *loc, const char *fmt, ...); Index: ossp-pkg/cfg/cfg_syn_parse.y RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_syn_parse.y,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_syn_parse.y,v' 2>/dev/null --- cfg_syn_parse.y 2002/07/03 13:25:34 1.1 +++ cfg_syn_parse.y 2002/07/04 14:51:21 1.2 @@ -108,6 +108,7 @@ cfg_node_create(&n); cfg_node_set(n, CFG_NODE_ATTR_TYPE, CFG_NODE_TYPE_SEQ); cfg_node_set(n, CFG_NODE_ATTR_CHILD1, $1); + $$ = n; } ; @@ -130,6 +131,7 @@ cfg_node_create(&n); cfg_node_set(n, CFG_NODE_ATTR_TYPE, CFG_NODE_TYPE_DIR); cfg_node_set(n, CFG_NODE_ATTR_CHILD1, $1); + $$ = n; } ; Index: ossp-pkg/cfg/cfg_test.c RCS File: /v/ossp/cvs/ossp-pkg/cfg/cfg_test.c,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/cfg_test.c,v' 2>/dev/null --- cfg_test.c 2002/07/03 13:25:34 1.1 +++ cfg_test.c 2002/07/04 14:51:21 1.2 @@ -7,61 +7,7 @@ #include "cfg_util.h" #include "cfg_node.h" #include "cfg_syn.h" - -typedef struct { - int line; - int col; - FILE *fp; -} cb_ctx_t; - -static void cb_out(cb_ctx_t *ctx, char *str) -{ - int i; - - fprintf(stderr, "cb_out <%s>\n", str); - fprintf(ctx->fp, "%s", str); - for (i = 0; str[i] != '\0'; i++) { - if (str[i] == '\n') { - ctx->line++; - ctx->col = 0; - } - else { - ctx->col++; - } - } -} - -static void cb_fct(void *_ctx, cfg_node_t *node) -{ - cb_ctx_t *ctx = (cb_ctx_t *)_ctx; - cfg_node_type_t type; - cfg_node_t *node2; - cfg_rc_t rc; - char *token; - - fprintf(stderr, "cb_fct enter\n"); - rc = cfg_node_get(node, CFG_NODE_ATTR_TYPE, &type); - fprintf(stderr, "rc=%d type=%d\n", rc, type); - if (type == CFG_NODE_TYPE_SEQ) { - cb_out(ctx, " {\n"); - } - else if (type == CFG_NODE_TYPE_DIR) { - cb_out(ctx, ";\n"); - cfg_node_get(node, CFG_NODE_ATTR_RBROTH, &node2); - if (node2 == NULL) - cb_out(ctx, "};\n"); - } - else if (type == CFG_NODE_TYPE_TOK) { - cfg_node_get(node, CFG_NODE_ATTR_TOKEN, &token); - if (token != NULL) { - cb_out(ctx, token); - cfg_node_get(node, CFG_NODE_ATTR_RBROTH, &node2); - if (node2 != NULL) - cb_out(ctx, " "); - } - } - fprintf(stderr, "cb_fct leave\n"); -} +#include "cfg_buf.h" int main(int argc, char *argv[]) { @@ -79,18 +25,15 @@ fprintf(stderr, "ERROR: reading file \"%s\"\n", argv[1]); exit(1); } - if ((rc = cfg_syn(NULL, &node, err_buf, sizeof(err_buf), buf_ptr)) != CFG_OK) { + rc = cfg_syn_import(NULL, &node, buf_ptr, err_buf, sizeof(err_buf)); + free(buf_ptr); + + if (rc != CFG_OK) fprintf(stderr, "ERROR: %s\n", err_buf); - } else { - cb_ctx_t ctx; - - ctx.line = 0; - ctx.col = 0; - ctx.fp = stdout; - cfg_node_apply(node, cb_fct, &ctx); + cfg_syn_export(NULL, node, &buf_ptr); + fprintf(stdout, "%s\n", buf_ptr); } - free(buf_ptr); return 0; } Index: ossp-pkg/cfg/sample.cfg RCS File: /v/ossp/cvs/ossp-pkg/cfg/Attic/sample.cfg,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/cfg/Attic/sample.cfg,v' 2>/dev/null --- sample.cfg 2002/07/03 13:25:34 1.1 +++ sample.cfg 2002/07/04 14:51:21 1.2 @@ -5,7 +5,7 @@ /* now comes a C/C++ style block comment ** quux */ -foo -b --bar -qX --quux=X baz +foo bar -b --bar -qX --quux=X baz; rootdir /; // C++ style EOL comment tmpdir /tmp; Index: ossp-pkg/cfg/sample2.cfg RCS File: /v/ossp/cvs/ossp-pkg/cfg/Attic/sample2.cfg,v co -q -kk -p'1.1' '/v/ossp/cvs/ossp-pkg/cfg/Attic/sample2.cfg,v' | diff -u /dev/null - -L'ossp-pkg/cfg/sample2.cfg' 2>/dev/null --- ossp-pkg/cfg/sample2.cfg +++ - 2024-05-17 01:48:40.789900318 +0200 @@ -0,0 +1,11 @@ + +foo; +bar1 "bar2 bar2b"; +quux1 { + quux1a; + quux1b; +} quux2 { + quux2a; + quux2b; +}; +