OSSP CVS Repository

ossp - Difference in ossp-pkg/lmtp2nntp/lmtp2nntp_main.c versions 1.41 and 1.42
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/lmtp2nntp/lmtp2nntp_main.c 1.41 -> 1.42

--- lmtp2nntp_main.c     2002/02/14 11:03:52     1.41
+++ lmtp2nntp_main.c     2002/02/28 12:57:55     1.42
@@ -115,17 +115,18 @@
 static void resetsession(struct session *session);
 int groupmatch(char *, size_t, char *);
 
-static var_config_t ctx_lookup_cfg = {
+static var_syntax_t syntax_ctx = {
+    '\\',         /* escape        */ 
     '$',          /* varinit       */ 
     '{',          /* startdelim    */ 
     '}',          /* enddelim      */ 
     '[',          /* startindex    */ 
     ']',          /* endindex      */ 
     '#',          /* current_index */ 
-    '\\',         /* escape        */ 
-    "a-zA-Z0-9_.-" /* namechars     */ 
+    "a-zA-Z0-9.-" /* namechars     */ 
 };
 
+#if 0
 static var_rc_t ctx_lookup(
     void *_ctx, 
     const char *var_ptr, size_t var_len, int var_idx,
@@ -181,6 +182,59 @@
              var_ptr /* FIXME: NUL-termination? */, var_len, var_strerror(rc));
     return rc;
 }
+#endif
+
+static var_rc_t ctx_lookup(
+    var_t *var,
+    void *_ctx, 
+    const char *var_ptr, size_t var_len, int var_idx,
+    const char **val_ptr, size_t *val_len, size_t *val_size)
+{
+    lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
+    var_rc_t rc;
+    char *cp;
+    optionval_t *ov;
+
+    log2(ctx, DEBUG, "ctx_lookup variable \"%s\" (%d)", var_ptr, var_len);
+    rc = VAR_ERR_UNDEFINED_VARIABLE;
+    if (strncasecmp(var_ptr, "option.", strlen("option.")) == 0) {
+        cp = str_dupex(var_ptr, var_len);
+        if (val_get(ctx->val, cp, &ov) == VAL_OK) {
+            //log3(ctx, DEBUG, "FIXME var_idx=%d, ov->type=%d, ov->ndata=%d", var_idx, ov->type, ov->ndata);
+            if ((var_idx == 0) && (ov->ndata == 1) && (ov->type == OPT_SINGLE)) { /* request first/only single value */
+                *val_ptr = ov->data.s;
+                *val_len = strlen(ov->data.s);
+                *val_size = 0;
+                rc = VAR_OK;
+            }
+            else if ((var_idx == 0) && (ov->ndata == 1) && (ov->type == OPT_FLAG)) { /* request first/only single value */
+                *val_ptr = ov->data.f == TRUE ? "yes" : "no";
+                *val_len = strlen(ov->data.s);
+                *val_size = 0;
+                rc = VAR_OK;
+            }
+            else if ((var_idx < ov->ndata) && (ov->type == OPT_MULTI)) { /* request second+ from multi value */
+                *val_ptr = ov->data.m[var_idx];
+                *val_len = strlen(ov->data.m[var_idx]);
+                *val_size = 0;
+                rc = VAR_OK;
+            }
+        }
+        free(cp);
+    }
+    else if (strncasecmp(var_ptr, "xyz", var_len) == 0) {
+        *val_ptr = "Hello, World!";
+        *val_len = strlen("Hello, World!");
+        *val_size = 0;
+        rc = VAR_OK;
+    }
+    if (rc == VAR_OK)
+        log4(ctx, DEBUG, "ctx_lookup variable \"%s\" (%d) ok: result is \"%s\" (%d)", var_ptr, var_len, *val_ptr, *val_len);
+    else
+        log4(ctx, DEBUG, "ctx_lookup variable \"%s\" (%d) failed: %s (%d)", var_ptr, var_len, var_strerror(var, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
+    return rc;
+}
+
 
 
 static void lmtp_gfs_ns(struct ns *);
@@ -385,6 +439,8 @@
     initsession(&ctx->session);
     ctx->msg = NULL;
     memset(&ctx->uname, 0, sizeof(struct utsname));
+    ctx->config_varregex = NULL;
+    ctx->config_varctx = NULL;
     
     /* fill in application context */
     if (val_create(&ctx->val) != VAL_OK)
@@ -395,6 +451,24 @@
     }
     ctx->progname = strdup(argv[0]);
 
+    /* establish variable expansion context */
+    {
+        var_rc_t rc;
+        char *cp;
+
+        if ((rc = var_create(&ctx->config_varctx)) != VAR_OK) {
+            fprintf(stderr, "%s:Error: create varctx context failed with %s (%d)", ctx->progname, var_strerror(ctx->config_varctx, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
+            CU(ERR_EXECUTION);
+        }
+        if ((rc = var_config(ctx->config_varctx, VAR_CONFIG_SYNTAX, &syntax_ctx)) != VAR_OK) {
+            fprintf(stderr, "%s:Error: config varctx context failed with %s (%d)", ctx->progname, var_strerror(ctx->config_varctx, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
+            CU(ERR_EXECUTION);
+        }
+        if ((rc = var_config(ctx->config_varctx, VAR_CONFIG_CB_VALUE, ctx_lookup, ctx)) != VAR_OK) {
+            fprintf(stderr, "%s:Error: config varctx callback failed with %s (%d)", ctx->progname, var_strerror(ctx->config_varctx, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
+            CU(ERR_EXECUTION);
+        }
+    }
     /* read in the arguments */
     (void)option_create(&o, ctx->val); /* FIXME */
     (void)option_parse(o, argc, argv);
@@ -658,6 +732,10 @@
         free(ctx->progname);
     if (ctx->azGroupargs != NULL)
         free(ctx->azGroupargs);
+    if (ctx->config_varregex != NULL)
+        var_destroy(ctx->config_varregex);
+    if (ctx->config_varctx != NULL)
+        var_destroy(ctx->config_varctx);
     if (ctx != NULL)
         free(ctx);
     str_parse(NULL, NULL);
@@ -1454,9 +1532,8 @@
     while ((cp = argz_next(ctx->azHeaderValuePairs, ctx->asHeaderValuePairs, cp)) != NULL) {
         var_rc_t var_rc;
         char *res_ptr;
-        if ((var_rc = var_expand(cp, strlen(cp), &res_ptr, NULL,
-                                 ctx_lookup, ctx, &ctx_lookup_cfg, TRUE)) != VAR_OK) {
-            log2(ctx, ERROR, "expansion of '%s' failed: %s", cp, var_strerror(var_rc));
+        if ((var_rc = var_expand(ctx->config_varctx, cp, strlen(cp), &res_ptr, NULL, TRUE)) != VAR_OK) {
+            log3(ctx, ERROR, "expansion of '%s' failed: %s (%d)", cp, var_strerror(ctx->config_varctx, rc, &cp) == VAR_OK ? cp : "Unknown Error", rc);
             continue;
         }
         log1(ctx, DEBUG, "adding expanded header: \"%s\"", res_ptr);

CVSTrac 2.0.1