OSSP CVS Repository

ossp - Difference in ossp-pkg/rc/rc_cliopt.c versions 1.1 and 1.2
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/rc/rc_cliopt.c 1.1 -> 1.2

--- rc_cliopt.c  2002/04/09 17:01:28     1.1
+++ rc_cliopt.c  2002/04/11 16:52:45     1.2
@@ -29,11 +29,53 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "rc.h"
-#include "rc_cliopt.h"
-
-static rc_cliopt_t *s_pInst = NULL; /* Singleton cliopt instance */
+#include "rc_const.h"
+#include "rc_config.h"
+#include "popt.h"       /* OSSP popt options library    */
+
+static char *s_szOptuples[RC_NUMOPTS];  /* Option name value tuples */
+static struct popt_option optionsTable[] = {
+    /* Long options with short keys but no arguments */
+    { RC_USE_NAME, '?', POPT_ARG_NONE,   0, RC_USE_VAL, RC_USE_DESC, NULL },
+    { RC_DBG_NAME, 'd', POPT_ARG_NONE,   0, RC_DBG_VAL, RC_DBG_DESC, NULL },
+    { RC_VER_NAME, 'V', POPT_ARG_NONE,   0, RC_VER_VAL, RC_VER_DESC, NULL },
+    { RC_EVL_NAME, 'e', POPT_ARG_NONE,   0, RC_EVL_VAL, RC_EVL_DESC, NULL },
+    { RC_HLP_NAME, 'h', POPT_ARG_NONE,   0, RC_HLP_VAL, RC_HLP_DESC, NULL },
+    { RC_INF_NAME, 'i', POPT_ARG_NONE,   0, RC_INF_VAL, RC_INF_DESC, NULL },
+    { RC_LBL_NAME, 'l', POPT_ARG_NONE,   0, RC_LBL_VAL, RC_LBL_DESC, NULL },
+    { RC_PRN_NAME, 'p', POPT_ARG_NONE,   0, RC_PRN_VAL, RC_PRN_DESC, NULL },
+    { RC_SIL_NAME, 's', POPT_ARG_NONE,   0, RC_SIL_VAL, RC_SIL_DESC, NULL },
+    { RC_RAW_NAME, 'r', POPT_ARG_NONE,   0, RC_RAW_VAL, RC_RAW_DESC, NULL },
+    { RC_VRB_NAME, 'v', POPT_ARG_NONE,   0, RC_VRB_VAL, RC_VRB_DESC, NULL },
+    { RC_EXC_NAME, 'x', POPT_ARG_NONE,   0, RC_EXC_VAL, RC_EXC_DESC, NULL },
+                                          
+    /* Single argument long options with short keys */
+    {RC_LOC_NAME, 'L', POPT_ARG_STRING, 0, RC_LOC_VAL, RC_LOC_DESC, "regx"},
+    {RC_CNF_NAME, 'c', POPT_ARG_STRING, 0, RC_CNF_VAL, RC_CNF_DESC, "path"},
+    {RC_FNC_NAME, 'f', POPT_ARG_STRING, 0, RC_FNC_VAL, RC_FNC_DESC, "path"},
+    {RC_QRY_NAME, 'q', POPT_ARG_STRING, 0, RC_QRY_VAL, RC_QRY_DESC, "varx"},
+    {RC_TMP_NAME, 't', POPT_ARG_STRING, 0, RC_TMP_VAL, RC_TMP_DESC, "path"},
+
+    /* Single argument long options without short keys */
+    { RC_OWN_NAME, 0, POPT_ARG_STRING, 0, RC_OWN_VAL, RC_OWN_DESC, "user" },
+    { RC_GRP_NAME, 0, POPT_ARG_STRING, 0, RC_GRP_VAL, RC_GRP_DESC, "group"},
+    { RC_MSK_NAME, 0, POPT_ARG_INT,    0, RC_MSK_VAL, RC_MSK_DESC, "umask"},
+    { RC_ASS_NAME, 0, POPT_ARG_STRING, 0, RC_ASS_VAL, RC_ASS_DESC, "regx" },
+    { RC_DEF_NAME, 0, POPT_ARG_STRING, 0, RC_DEF_VAL, RC_DEF_DESC, "regx" },
+    { RC_REF_NAME, 0, POPT_ARG_STRING, 0, RC_REF_VAL, RC_REF_DESC, "regx" },
+    { RC_PRM_NAME, 0, POPT_ARG_STRING, 0, RC_PRM_VAL, RC_PRM_DESC, "regx" },
+    { RC_TRM_NAME, 0, POPT_ARG_STRING, 0, RC_TRM_VAL, RC_TRM_DESC, "regx" },
+    { RC_NCF_NAME, 0, POPT_ARG_STRING, 0, RC_NCF_VAL, RC_NCF_DESC, "name" },
+    { RC_CMN_NAME, 0, POPT_ARG_STRING, 0, RC_CMN_VAL, RC_CMN_DESC, "name" },
+    { RC_DFL_NAME, 0, POPT_ARG_STRING, 0, RC_DFL_VAL, RC_DFL_DESC, "name" },
+    { RC_ERR_NAME, 0, POPT_ARG_STRING, 0, RC_ERR_VAL, RC_ERR_DESC, "name" },
+
+    POPT_AUTOHELP
+    { NULL, 0, 0, NULL, 0 }
+};
 
 
 /***************************************
@@ -42,43 +84,239 @@
 ***************************************/
 rc_return_t clioptConstruct(void)
 {
-    assert(s_pInst == NULL);
-    s_pInst = malloc(sizeof(s_pInst));  /* Allocate a cliopt instance   */
-    if (!s_pInst)
+/*    assert(s_pBintab == NULL); */           /* Error if constructed already */
+/*    s_pBintab = malloc(sizeof(*s_pBintab));*/ /* Allocate a cliopt instance   */
+/*    if (!s_pBintab)
         return(RC_THROW(RC_ERR_MEM));
 
+    s_pBintab->pOptlist = malloc(sizeof(*s_pBintab->pOptlist));*/
+/*FIXME    optConstruct((rc_opt_t **)&s_pBintab->pOptlist->pvData);FIXME*/
+/*    s_pBintab->pOptlist->pvData = NULL;
+    s_pBintab->pOptlist->pvNext = NULL;*/
+
+    memset(s_szOptuples, NULL, sizeof(s_szOptuples));
+
+TRACE("cliopt constructed.");
     return(RC_THROW(RC_OK));
 }
 
 /***************************************
-* clioptGetXXXX(void)                  *
-* Command line option accessors        *
+* clioptGetval(rc_opt_t)               *
+* Command line option accessor         *
 ***************************************/
-short clioptGetvers(void)
+const char *clioptGetval(rc_opt_t Optname)
 {
     ex_t Except;
-    short nVer = 0;
 
-    assert(s_pInst);
+    assert(Optname < RC_NUMOPTS);   /* Invalidate if nonexistent option */
     ex_try {
-
+        return((const char *)s_szOptuples[Optname]);
     }
     ex_catch(Except) {
         rethrow;
     }
+    return (NULL);  /* Not reached  */
+}
+
+/***************************************
+* clioptSummarize(void)                *
+* Command line option summary          *
+***************************************/
+const char *clioptSummarize(void)
+{
+    return (NULL);
+}
+
+/***************************************
+* clioptProcess(int)                   *
+* Switch through available options     *
+* processing the corresponding option  *
+* and update option table accordingly  *
+***************************************/
+rc_return_t clioptProcess(int cliOption)
+{
+    switch (cliOption) {
+    case RC_USE_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_DBG_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_VER_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_EVL_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_HLP_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_INF_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_LBL_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_PRN_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_SIL_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_RAW_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_VRB_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_EXC_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_LOC_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_CNF_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_FNC_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_QRY_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_TMP_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_OWN_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_GRP_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_MSK_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_ASS_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_DEF_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_REF_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_PRM_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_TRM_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_NCF_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_CMN_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_DFL_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    case RC_ERR_VAL:
+        s_szOptuples[cliOption] = strdup("1");
+        break;
+    default :
+        break;
+    }
 
-    return(nVer);
+    return(RC_THROW(RC_OK));
 }
 
 /***************************************
+* clioptParse(int, char **)            *
+* Parse command line options           *
+***************************************/
+rc_return_t clioptParse(int argc, char *argv[])
+{
+    ex_t Except;
+    char cliOpt = 0;            /* For argument parsing           */
+    char *szCLIBuf = NULL;
+    popt_context optCon;        /* Context for parsing options    */
+
+    optCon = popt_getcontext(NULL, argc, (const char **)argv, optionsTable, 0);
+    popt_setotheroptionhelp(optCon, "[options] <rcfile> <sections [args]>");
+
+    if (argc < 3) {
+        popt_printusage(optCon, stderr, 0);
+        return(RC_THROW(RC_ERR_USE));
+    }
+
+    /* Now do options processing */
+    while ((cliOpt = popt_getnextopt(optCon)) >= 0)
+    ex_try {
+        clioptProcess(cliOpt);
+    }
+    ex_catch(Except) {
+        if ((rc_return_t)Except.ex_value != RC_ERR_USE)
+            fprintf(stderr, "Nonusage exception: %d\n",\
+                (rc_return_t)Except.ex_value);
+
+        fprintf(stderr, "Class '%s' threw exception %d in %s:%s():%d.\n",\
+                (char *)Except.ex_class, *(int *)Except.ex_value,\
+                Except.ex_file, Except.ex_func, Except.ex_line);
+    }
+
+    szCLIBuf = (char *)popt_getarg(optCon);
+    if ((szCLIBuf == NULL)) {
+        fprintf(stderr, "Please specify at least one rcfile and section.\n");
+        popt_printusage(optCon, stderr, 0);
+        return(RC_THROW(RC_ERR_USE));
+    }
+
+    if (cliOpt < -1) {
+        /* An error occurred during option processing */
+        fprintf(stderr, "%s: %s\n",
+                popt_badoption(optCon, POPT_BADOPTION_NOALIAS),
+                popt_strerror(cliOpt));
+        return(RC_THROW(RC_ERR_INT));
+    }
+
+    fprintf(stderr, "Run these commands: %s\n", szCLIBuf);
+    popt_freecontext(optCon);
+    return(RC_THROW(RC_OK));
+}
+/***************************************
 * clioptDestruct(void)                 *
 * Destruct a command line option       *
 ***************************************/
 rc_return_t clioptDestruct(void)
 {
-    assert(s_pInst);
-    free(s_pInst);      /* Deallocate cliopt and    */
-    s_pInst = NULL;     /* clear its reference      */
+    int i = 0;
+/*    ex_t Except;
+
+    assert(s_pBintab);*/                /* Error if not constructed */
+/*    ex_try {
+        clioptRemall();*/             /* Removes ALL list nodes   */
+/*FIXME        optDestruct((rc_opt_t **)&s_pBintab->pOptlist->pvData);FIXME*/
+/*        free(s_pBintab->pOptlist);*/    /* Deallocate option list   */
+/*        free(s_pBintab);*/              /* Deallocate cliopt and    */
+/*        s_pBintab = NULL;*/             /* clear its reference      */
+/*    }
+    ex_catch(Except) {
+        rethrow;
+    }*/
+
+for (i = 0; i < RC_NUMOPTS; i++)
+    if (s_szOptuples[i])
+        TRACEL(i);
+
+    for (i = 0; i < RC_NUMOPTS; i++)                    /* Free the tuple */
+        s_szOptuples[i] ? free(s_szOptuples[i]) : RC_NOP; /* or do nothing  */
+
+    memset(s_szOptuples, NULL, sizeof(s_szOptuples));     /* Clear tuples   */
+
+for (i = 0; i < RC_NUMOPTS; i++)
+    if (s_szOptuples[i])
+        TRACE("Problem! Destructor failed!");
+TRACE("cliopt destructed.");
 
     return(RC_THROW(RC_OK));
 }

CVSTrac 2.0.1