OSSP CVS Repository

ossp - Check-in [2225]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2225
Date: 2002-Jul-04 16:51:21 (local)
2002-Jul-04 14:51:21 (UTC)
User:rse
Branch:
Comment: Flush today's work: the library is now able to parse a .cfg file into an internal node tree and export this again in textual .cfg file format (including indented blocks).
Tickets:
Inspections:
Files:
ossp-pkg/cfg/Makefile      1.3 -> 1.4     1 inserted, 1 deleted
ossp-pkg/cfg/cfg_buf.c      1.1 -> 1.2     11 inserted, 3 deleted
ossp-pkg/cfg/cfg_buf.h      1.1 -> 1.2     1 inserted, 0 deleted
ossp-pkg/cfg/cfg_syn.c      1.1 -> 1.2     127 inserted, 1 deleted
ossp-pkg/cfg/cfg_syn.h      1.1 -> 1.2     2 inserted, 1 deleted
ossp-pkg/cfg/cfg_syn_parse.y      1.1 -> 1.2     2 inserted, 0 deleted
ossp-pkg/cfg/cfg_test.c      1.1 -> 1.2     7 inserted, 64 deleted
ossp-pkg/cfg/sample.cfg      1.1 -> 1.2     1 inserted, 1 deleted
ossp-pkg/cfg/sample2.cfg      added-> 1.1

ossp-pkg/cfg/Makefile 1.3 -> 1.4

--- 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 \


ossp-pkg/cfg/cfg_buf.c 1.1 -> 1.2

--- 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;
 }
 


ossp-pkg/cfg/cfg_buf.h 1.1 -> 1.2

--- 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);
 


ossp-pkg/cfg/cfg_syn.c 1.1 -> 1.2

--- 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 <stdio.h>
+
 #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;
+}
+


ossp-pkg/cfg/cfg_syn.h 1.1 -> 1.2

--- 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, ...);


ossp-pkg/cfg/cfg_syn_parse.y 1.1 -> 1.2

--- 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;
       }
     ;
 


ossp-pkg/cfg/cfg_test.c 1.1 -> 1.2

--- 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;
 }


ossp-pkg/cfg/sample.cfg 1.1 -> 1.2

--- 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;
 


ossp-pkg/cfg/sample2.cfg -> 1.1

*** /dev/null    Mon Apr 29 17:33:00 2024
--- -    Mon Apr 29 17:36:27 2024
***************
*** 0 ****
--- 1,11 ----
+ 
+ foo;
+ bar1 "bar2 bar2b";
+ quux1 { 
+     quux1a; 
+     quux1b;
+ } quux2 {
+     quux2a;
+     quux2b;
+ };
+ 

CVSTrac 2.0.1