OSSP CVS Repository

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

Check-in Number: 1955
Date: 2002-Mar-06 15:25:39 (local)
2002-Mar-06 14:25:39 (UTC)
User:thl
Branch:
Comment: Syntax and argument checking does no longer ignore errors silently. It properly prints error messages for unknown options or invalid arguments. The message printed includes what it got and what it expected. Processing is continued to check for more errors but finally it aborts before the main program starts.
Tickets:
Inspections:
Files:
ossp-pkg/lmtp2nntp/lmtp2nntp_main.c      1.47 -> 1.48     11 inserted, 3 deleted
ossp-pkg/lmtp2nntp/lmtp2nntp_option.c      1.15 -> 1.16     57 inserted, 118 deleted
ossp-pkg/lmtp2nntp/lmtp2nntp_option.h      1.5 -> 1.6     4 inserted, 29 deleted

ossp-pkg/lmtp2nntp/lmtp2nntp_main.c 1.47 -> 1.48

--- lmtp2nntp_main.c     2002/03/05 14:17:39     1.47
+++ lmtp2nntp_main.c     2002/03/06 14:25:39     1.48
@@ -431,10 +431,18 @@
             CU(ERR_EXECUTION);
         }
     }
+
     /* read in the arguments */
-    (void)option_create(&o, ctx->val); /* FIXME */
-    (void)option_parse(o, argc, argv);
-    (void)config_context(ctx);
+    {
+        lmtp2nntp_option_rc_t rv;
+
+        if (option_create(&o, ctx->val) != OPTION_OK)
+                CU(ERR_EXECUTION);
+        rv = option_parse(o, argc, argv);
+        config_context(ctx); //FIXME rc
+        if (rv != OPTION_OK)
+                CU(ERR_EXECUTION);
+    }
 
     if (getuid() != ctx->option_uid) {
         if (setuid(ctx->option_uid) == -1) {


ossp-pkg/lmtp2nntp/lmtp2nntp_option.c 1.15 -> 1.16

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


ossp-pkg/lmtp2nntp/lmtp2nntp_option.h 1.5 -> 1.6

--- lmtp2nntp_option.h   2002/02/14 10:47:20     1.5
+++ lmtp2nntp_option.h   2002/03/06 14:25:39     1.6
@@ -34,34 +34,8 @@
 typedef struct optionval_s optionval_t;
 
 typedef struct {
-    int                 childsmax;
-    int                 daemonize;
-    int                 kill;
-    char               *pidfile;
-    argz_t              acl;
-    char               *bind;
-    char               *client;
-    argz_t              destination;
-    char               *groupmode;
-    argz_t              headervalue;
-    argz_t              include;
-    char               *size;
-    int                *timeoutlmtpaccept;
-    int                *timeoutlmtpread;
-    int                *timeoutlmtpwrite;
-    int                *timeoutnntpconnect;
-    int                *timeoutnntpread;
-    int                *timeoutnntpwrite;
-    char               *mailfrom;
-    char               *nodename;
-    char               *operationmode;
-    char               *l2spec;
-    char               *uid;
-    argz_t              restrictheader;
-    argz_t              newsgroup;
-                /*FIXME above*/
-    optionval_t     *first;
-    optionval_t     *last;
+    optionval_t        *first;
+    optionval_t        *last;
     val_t              *vo;     /* val_t for all options */
     int                 pi;     /* popt index to next record */
     int                 pn;     /* popt number of available records */
@@ -80,7 +54,8 @@
     OPTION_ERR_ARG, /* invalid args passed into function */
     OPTION_ERR_USE, /* invalid usage, bad data passed into function */
     OPTION_ERR_MEM, /* out of memory */
-    OPTION_ERR_VAL  /* libval failed */
+    OPTION_ERR_VAL, /* libval failed */
+    OPTION_ERR_NUM  /* option_find not successful */
 } lmtp2nntp_option_rc_t;
 
 typedef lmtp2nntp_option_rc_t (optionloop_cb_t)(optionval_t *oc, char *arg, char *cbctx);

CVSTrac 2.0.1