--- lmtp2nntp_config.c 2002/01/30 14:22:23 1.17
+++ lmtp2nntp_config.c 2002/01/30 16:41:02 1.18
@@ -28,6 +28,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
+#include <errno.h>
/* third party (included) */
#include "lmtp2nntp_argz.h"
@@ -36,6 +37,7 @@
#include "str.h"
#include "val.h"
#include "popt.h"
+#include "l2.h"
/* library version check (compile-time) */
#define STR_VERSION_HEX_REQ 0x009206
@@ -75,20 +77,200 @@
#define NUL '\0'
#endif
+static l2_result_t
+formatter_prefix(l2_context_t *_ctx, const char id, const char *param,
+ char *bufptr, size_t bufsize, size_t *buflen, va_list *ap)
+{
+ lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx->vp;
+
+ if ((ctx->msg != NULL) && (ctx->msg->cpFid != NULL)) {
+ sprintf(bufptr, "%s: ", ctx->msg->cpFid);
+ *buflen = strlen(bufptr);
+ }
+ else
+ *buflen = 0;
+ return L2_OK;
+}
+
+static l2_result_t
+formatter_errno(l2_context_t *_ctx, const char id, const char *param,
+ char *bufptr, size_t bufsize, size_t *buflen, va_list *ap)
+{
+ sprintf(bufptr, "(%d) %s", errno, strerror(errno));
+ *buflen = strlen(bufptr);
+ return L2_OK;
+}
+
void config_context(lmtp2nntp_t *ctx)
{
+ ex_t ex;
optionval_t *ov;
- char *cp;
+ //char *cp;
+ int rc;
+
+ /* create L2 environment */
+ if (l2_env_create(&ctx->l2_env) != L2_OK) {
+ fprintf(stderr, "%s:Error: failed to create L2 environment\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_env_formatter(ctx->l2_env, 'P', formatter_prefix, &ctx->ctx) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to register prefix formatter\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_env_formatter(ctx->l2_env, 'D', l2_util_fmt_dump, NULL) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to register dump formatter\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_env_formatter(ctx->l2_env, 'S', l2_util_fmt_string, NULL) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to register string formatter\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_env_formatter(ctx->l2_env, 'm', formatter_errno, NULL) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to register errno formatter\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (val_get(ctx->val, "option.l2spec", &ov) != VAL_OK) {
+ fprintf(stderr, "%s:Error: (internal) config did not register 'l2spec' option\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (ov->data.s != NULL) {
+ if ((rc = l2_spec(&ctx->l2, ctx->l2_env, ov->data.s)) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to create stream\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_channel_levels(ctx->l2, L2_LEVEL_ALL, L2_LEVEL_NONE) != L2_OK) { /* FIXME should this globalmask and flushmask be user-configurable? */
+ fprintf(stderr, "%s:Error: logging failed to set global logging level\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ if (l2_channel_open(ctx->l2) != L2_OK) {
+ fprintf(stderr, "%s:Error: logging failed to open channel stream\n", ctx->progname);
+ CU(ERR_EXECUTION);
+ }
+ }
+ /* from this point on logging is up and running and fprintf(stderr, ...)
+ * should not be used in the remainder of the program flow.
+ */
+ log1(ctx, NOTICE, "startup, version %s", lmtp2nntp_version.v_gnu);
+
+ /* --childsmax SINGLE */
+ try {
+ if ( (val_get(ctx->val, "option.childsmax", &ov) != VAL_OK)
+ || (ov->ndata != 1)
+ || (ov->data.s == NULL)
+ ) throw(0,0,0);
+ log1(ctx, TRACE, "--childsmax = \"%s\"", ov->data.s);
+ if ((ctx->option_childsmax = atoi(ov->data.s)) <= 0) {
+ log1(ctx, ERROR, "number (%d) out of range for option --childsmax\n", ctx->option_childsmax);
+ throw(0,0,0);
+ }
+ }
+ catch (ex) {
+ log1(ctx, ERROR, "caught class %s\n", ex.ex_class == NULL ? "N/A" : (char *)ex.ex_class );
+ log1(ctx, ERROR, "caught object %s\n", ex.ex_object == NULL ? "N/A" : (char *)ex.ex_object);
+ log1(ctx, ERROR, "caught value %s\n", ex.ex_value == NULL ? "N/A" : (char *)ex.ex_value );
+ rethrow;
+ }
+
+ /* --daemonize FLAG */
+ try {
+ if ( (val_get(ctx->val, "option.daemonize", &ov) != VAL_OK)
+ || (ov->ndata != 1)
+ || (ov->data.f != 1)
+ ) throw(0,0,0);
+ log1(ctx, TRACE, "--daemonize = %d", ov->data.f);
+ ctx->option_daemon = TRUE;
+ }
+ catch (ex)
+ rethrow;
- printf("DEBUG: Hello, World!\n");
- //case 'C': /*POD [B<-C> I<childsmax>] */
- if (val_get(ctx->val, "option.childsmax", &ov) != VAL_OK)
- fprintf(stderr, "%s:Error: (internal) config did not register 'childsmax' option\n", ctx->progname);
- printf("DEBUG: option_childsmax %d = \"%s\"\n", ov->ndata, ov->data.s);
- ctx->option_childsmax = atoi(ov->data.s);
- if (ctx->option_childsmax <= 0) {
- fprintf(stderr, "%s:Error: Invalid number (%d) to option -C\n", ctx->progname, ctx->option_childsmax);
- return; //FIXME CU(ERR_EXECUTION);
+ /* --kill FLAG */
+ try {
+ if ( (val_get(ctx->val, "option.kill", &ov) != VAL_OK)
+ || (ov->ndata != 1)
+ || (ov->data.f != 1)
+ ) throw(0,0,0);
+ log1(ctx, TRACE, "--kill = %d", ov->data.f);
+ ctx->option_killflag = TRUE;
}
+ catch (ex)
+ rethrow;
+
+ /* --pidfile SINGLE */
+ try {
+ if ( (val_get(ctx->val, "option.pidfile", &ov) != VAL_OK)
+ || (ov->ndata != 1)
+ || (ov->data.s == NULL)
+ ) throw(0,0,0);
+ log1(ctx, TRACE, "--pidfile = \"%s\"", ov->data.s);
+ ctx->option_pidfile = ov->data.s;
+ }
+ catch (ex)
+ rethrow;
+
+ /* --acl MULTI */
+ try {
+ int i;
+ char *cp;
+ struct acl *acl;
+
+ if ( (val_get(ctx->val, "option.acl", &ov) != VAL_OK)
+ || ((ov->ndata >= 1) && (ov->data.m == NULL))
+ ) throw(0,0,0);
+ for (i = 0; i < ov->ndata; i++)
+ log2(ctx, TRACE, "--acl[%d] = \"%s\"", i, (ov->data.m)[i]);
+ log1(ctx, DEBUG, "ov->ndata = %d", ov->ndata);
+ if ((acl = (struct acl *)malloc(ov->ndata * sizeof(struct acl))) == NULL) throw(0,0,0);
+ for (i = 0; i < ov->ndata; i++) {
+ cp = (ov->data.m)[i];
+ log2(ctx, DEBUG, "cp = (data.m)[%d] = \"%s\"", i, cp);
+ //ctx->option_acl[ctx->option_aclc].acl = strdup(cp);
+ }
+
+ }
+ catch (ex)
+ rethrow;
+#if 0
+
+ if (argz_create_sep(optarg, ',', &azACL, &asACL) != 0)
+ CU(ERR_EXECUTION);
+ cp = NULL;
+ while ((cp = argz_next(azACL, asACL, cp)) != NULL) {
+ if (ctx->option_aclc >= MAXACLS) {
+ fprintf(stderr, "%s:Error: Too many ACL (%d) using option -a\n", ctx->progname, ctx->option_aclc);
+ CU(ERR_EXECUTION);
+ }
+
+
+
+ ctx->option_acl[ctx->option_aclc].acl = strdup(cp);
+ if (cp[0] == '!') {
+ ctx->option_acl[ctx->option_aclc].not = TRUE;
+ cpAddr = strdup(cp+1);
+ }
+ else {
+ cpAddr = strdup(cp);
+ }
+ if ((cpPrefixLen = strrchr(cpAddr, '/')) != NULL)
+ *cpPrefixLen++ = NUL;
+ else
+ cpPrefixLen = "-1";
+ ctx->option_acl[ctx->option_aclc].prefixlen = atoi(cpPrefixLen);
+ if ((rc = sa_addr_create(&ctx->option_acl[ctx->option_aclc].saa)) != SA_OK) {
+ fprintf(stderr, "%s:Error: Creating address failed for -a option (%d)\n",
+ ctx->progname, rc);
+ }
+ if ((rc = sa_addr_u2a(ctx->option_acl[ctx->option_aclc].saa, "inet://%s:0", cpAddr)) != SA_OK) {
+ fprintf(stderr, "%s:Error: Parsing host address failed for \"%s:0\" (%d)\n",
+ ctx->progname, cpAddr, rc);
+ CU(ERR_EXECUTION);
+ }
+ ctx->option_aclc++;
+ free(cpAddr);
+ }
+ free(azACL);
+#endif
+
+
+CUS:
return;
}
|