--- lmtp2nntp_option.c 2002/02/26 15:56:08 1.15
+++ lmtp2nntp_option.c 2002/03/06 14:25:39 1.16
@@ -113,8 +113,6 @@
static lmtp2nntp_option_rc_t option_find(lmtp2nntp_option_t *o, int number, optionval_t **ocp)
{
- lmtp2nntp_option_rc_t rc = VAL_OK;
-
if (o == NULL || ocp == NULL)
return OPTION_ERR_ARG;
@@ -122,7 +120,10 @@
while (*ocp != NULL && (*ocp)->number != number)
*ocp = (*ocp)->next;
- return rc;
+ if (*ocp == NULL)
+ return OPTION_ERR_NUM;
+
+ return OPTION_OK;
}
@@ -257,130 +258,90 @@
lmtp2nntp_option_rc_t option_create(lmtp2nntp_option_t **op, val_t *parent)
{
- //printf("DEBUG: enter option_create(%.8lx)\n", (long)op);
-
- if (op == NULL)
- return OPTION_ERR_ARG;
+ ex_t ex;
- if ((*op = (lmtp2nntp_option_t *)malloc(sizeof(lmtp2nntp_option_t))) == NULL)
- return OPTION_ERR_MEM;
- (*op)->childsmax = 0;
- (*op)->daemonize = 0;
- (*op)->kill = 0;
- (*op)->pidfile = 0;
- (*op)->acl.as = 0;
- (*op)->acl.az = NULL;
- (*op)->bind = NULL;
- (*op)->client = NULL;
- (*op)->destination.as = 0;
- (*op)->destination.az = NULL;
- (*op)->groupmode = NULL;
- (*op)->headervalue.as = 0;
- (*op)->headervalue.az = NULL;
- (*op)->include.as = 0;
- (*op)->include.az = NULL;
- (*op)->size = 0;
- (*op)->timeoutlmtpaccept = 0;
- (*op)->timeoutlmtpread = 0;
- (*op)->timeoutlmtpwrite = 0;
- (*op)->timeoutnntpconnect = 0;
- (*op)->timeoutnntpread = 0;
- (*op)->timeoutnntpwrite = 0;
- (*op)->mailfrom = NULL;
- (*op)->nodename = NULL;
- (*op)->operationmode = NULL;
- (*op)->l2spec = NULL;
- (*op)->uid = NULL;
- (*op)->restrictheader.as = 0;
- (*op)->restrictheader.az = NULL;
- (*op)->newsgroup.as = 0;
- (*op)->newsgroup.az = NULL;
- /**/
- (*op)->first = NULL;
- (*op)->last = NULL;
- (*op)->vo = NULL;
- (*op)->pn = 0;
- (*op)->pi = 0;
- (*op)->pt = NULL;
-
- if (val_create(&((*op)->vo)) != VAL_OK) {
- free(*op);
- return OPTION_ERR_VAL;
- }
- if (val_reg(parent, "option", VAL_TYPE_VAL, "option", (void *)&((*op)->vo)) != VAL_OK) {
- free(*op);
- return OPTION_ERR_VAL;
+ try {
+ if (op == NULL)
+ throw(option_create, NULL, OPTION_ERR_ARG);
+
+ (*op = (lmtp2nntp_option_t *)mallocex(sizeof(lmtp2nntp_option_t)));
+ (*op)->first = NULL;
+ (*op)->last = NULL;
+ (*op)->vo = NULL;
+ (*op)->pi = 0;
+ (*op)->pn = 0;
+ (*op)->pt = NULL;
+
+ if (val_create(&((*op)->vo)) != VAL_OK)
+ throw(option_create, NULL, OPTION_ERR_VAL);
+
+ if (val_reg(parent, "option", VAL_TYPE_VAL, "option", (void *)&((*op)->vo)) != VAL_OK)
+ throw(option_create, NULL, OPTION_ERR_VAL);
+ }
+ catch(ex) {
+ if (*op != NULL) {
+ if ((*op)->vo != NULL)
+ val_unreg(parent, "option");
+ free(*op);
+ }
+ rethrow;
}
return OPTION_OK;
}
static lmtp2nntp_option_rc_t option_parse_internal(lmtp2nntp_option_t *o, int argc, char **argv)
{
+ lmtp2nntp_option_rc_t rc = OPTION_OK;
+ lmtp2nntp_option_rc_t rv;
int i;
char *cp;
optionval_t *ocp;
popt_context poptCon; /* context for parsing command-line options */
-#if 0
- {
- int i;
-
- for (i=0; i<argc; i++)
- printf("DEBUG: argv[%3d] = ***%s***\n", i, argv[i]);
- }
-#endif
-
poptCon = popt_getcontext(NULL, argc, (const char **)argv, o->pt, 0);
popt_setotheroptionhelp(poptCon, "[OPTIONS]* [newsgroup ...]");
- //printf("DEBUG: argc=%d\n", argc);
if (argc < 2) {
popt_printusage(poptCon, stderr, 0);
- exit(1);
+ exit(1); //FIXME
}
- while ((i = popt_getnextopt(poptCon)) >= 0) {
- (void)option_find(o, i, &ocp);
- //printf("DEBUG: ocp->type=%d\n", ocp->type);
- if (ocp->cb != NULL)
- ocp->cb(ocp, cp = (ocp->type == OPT_FLAG ? NULL : (char *)popt_getoptarg(poptCon)), ocp->cbctx);
- //printf("DEBUG: popt_getnextopt returned %d \"%s\", \"%s\"\n", i-1, o->pt[i-1].longName, ocp->longname);
- //printf("DEBUG: popt_getoptarg returned \"%s\"\n", cp);
+ while ((i = popt_getnextopt(poptCon)) >= 0 || i == POPT_ERROR_NOARG || i == POPT_ERROR_BADOPT) {
+ if (i == POPT_ERROR_NOARG || i == POPT_ERROR_BADOPT) {
+ fprintf(stderr, "ERROR: %s (%d) \"%s\"\n", popt_strerror(i), i, popt_badoption(poptCon, POPT_BADOPTION_NOALIAS));
+ rc = OPTION_ERR_USE;
+ continue;
+ }
+ if ((option_find(o, i, &ocp) == OPTION_OK) && (ocp->cb != NULL)) {
+ rv = ocp->cb(ocp, cp = (ocp->type == OPT_FLAG ? NULL : (char *)popt_getoptarg(poptCon)), ocp->cbctx);
+ if (rc == OPTION_OK)
+ rc = rv;
+ }
}
- //printf("DEBUG: current popt error is \"%s\"(%d)\n", popt_strerror(i), i);
- //printf("DEBUG: ----\n");
{
int largc;
char **largv;
char *cpNew;
- if ((largv = (char **)malloc((1 + 1) * sizeof(char **))) == NULL)
- return OPTION_ERR_MEM;
+ largv = (char **)mallocex((1 + 1) * sizeof(char **));
largc = 0;
largv[largc++] = "leftover";
largv[largc] = NULL;
while ((cp = (char *)popt_getarg(poptCon)) != NULL) {
- //printf("DEBUG: popt_getarg returned \"%s\"\n", cp);
- if ((largv = (char **)realloc(largv, (largc + 2) * sizeof(char **))) == NULL)
- return OPTION_ERR_MEM;
+ largv = (char **)reallocex(largv, (largc + 2) * sizeof(char **));
largv[largc++] = "--newsgroup";
largv[largc] = NULL;
- if ((cpNew = strdup(cp)) == NULL)
- return OPTION_ERR_MEM;
- //printf("DEBUG: cpNew = \"%s\"\n", cpNew);
+ cpNew = strdupex(cp);
largv[largc++] = cpNew;
largv[largc] = NULL;
- //printf("DEBUG: largc = \"%d\"\n", largc);
-#if 0
- for (i=0; i<largc; i++)
- printf("DEBUG: largv[%d] = \"%s\"\n", i, largv[i]);
-#endif
}
- if (largc > 1)
- option_parse_internal(o, largc, largv);
+ if (largc > 1) {
+ rv = option_parse_internal(o, largc, largv);
+ if (rc == OPTION_OK)
+ rc = rv;
+ }
}
- //printf("DEBUG: current popt error is \"%s\"(%d)\n", popt_strerror(i), i);
popt_freecontext(poptCon);
- return OPTION_OK;
+ return rc;
}
static lmtp2nntp_option_rc_t stdsyntax(optionval_t *oc, char *arg, char *cbctx)
@@ -408,7 +369,7 @@
*/
if (cbctx != NULL)
if (str_parse(arg, cbctx) <= 0) {
- //printf("DEBUG: \"%s\" does NOT match \"%s\"\n", arg, cbctx);
+ fprintf(stderr, "ERROR: argument \"%s\" does NOT match syntax \"%s\"\n", arg, cbctx);
return OPTION_ERR_USE;
}
//printf("DEBUG: \"%s\" does match \"%s\"\n", arg, cbctx);
@@ -424,7 +385,7 @@
return OPTION_ERR_USE;
if (cbctx != NULL)
if (str_parse(arg, cbctx) <= 0) {
- //printf("DEBUG: \"%s\" does NOT match \"%s\"\n", arg, cbctx);
+ fprintf(stderr, "ERROR: argument \"%s\" does NOT match syntax \"%s\"\n", arg, cbctx);
return OPTION_ERR_USE;
}
//printf("DEBUG: \"%s\" does match \"%s\"\n", arg, cbctx);
@@ -601,13 +562,10 @@
lmtp2nntp_option_rc_t option_parse(lmtp2nntp_option_t *o, int argc, char **argv)
{
- lmtp2nntp_option_rc_t rc;
- //printf("DEBUG: enter option_parse(%.8lx, %d, %.8lx)\n", (long)o, argc, (long)argv);
-
if (o == NULL)
return OPTION_ERR_ARG;
- (void)option_register(o, "childsmax", 'C', OPT_SINGLE, "10", "childs to spawn at max", "childsmax", &stdsyntax, "m/.*/" ); //"m/[0-9]+/" );
+ (void)option_register(o, "childsmax", 'C', OPT_SINGLE, "10", "childs to spawn at max", "childsmax", &stdsyntax, "m/\\d+/" ); //"m/[0-9]+/" );
(void)option_register(o, "daemonize", 'D', OPT_FLAG, NULL, "detach from terminal", NULL, &stdsyntax, NULL );
(void)option_register(o, "kill", 'K', OPT_FLAG, NULL, "kill a previously run daemon", NULL, &stdsyntax, NULL );
(void)option_register(o, "pidfile", 'P', OPT_SINGLE, NULL, "file containing pid", "pidfile", &stdsyntax, "m/.*/" );
@@ -637,26 +595,7 @@
(void)option_register(o, "version", 'v', OPT_FLAG, NULL, "print version", NULL, &stdsyntax, NULL );
(void)option_register(o, "newsgroup", NUL, OPT_MULTI, NULL, "article destination", "newsgroup", &stdsyntax, "m/.*/" );
-#if 0
- {
- int i;
-
- for (i=0; i<29; i++) {
- printf("DEBUG: o->pt[%3d].longName = %s \n", i, o->pt[i].longName );
- printf("DEBUG: o->pt[%3d].shortName = %c \n", i, o->pt[i].shortName );
- printf("DEBUG: o->pt[%3d].argInfo = %d \n", i, o->pt[i].argInfo );
- printf("DEBUG: o->pt[%3d].arg = %.8lx\n", i, (long)o->pt[i].arg );
- printf("DEBUG: o->pt[%3d].val = %d \n", i, o->pt[i].val );
- printf("DEBUG: o->pt[%3d].descrip = %s \n", i, o->pt[i].descrip );
- printf("DEBUG: o->pt[%3d].argDescrip = %s \n", i, o->pt[i].argDescrip);
- }
- }
-#endif
-
- //val_apply(o->vo, "", 9, dumper, "vorher" );
- rc = option_parse_internal(o, argc, argv); //FIXME should fail for syntax errors and unknown options
- //val_apply(o->vo, "", 9, dumper, "nachher" );
- return rc;
+ return option_parse_internal(o, argc, argv);
}
lmtp2nntp_option_rc_t option_destroy(lmtp2nntp_option_t *o)
|