OSSP CVS Repository

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

ossp-pkg/rc/rc_cliopt.c 1.20 -> 1.21

--- rc_cliopt.c  2003/07/07 13:30:51     1.20
+++ rc_cliopt.c  2003/07/11 14:13:16     1.21
@@ -31,64 +31,29 @@
 #include <string.h>
 
 #include "rc.h"
+#include "rc_cliopt.h"
 #include "rc_const.h"
 #include "rc_config.h"
 #include "popt.h"                              /* OSSP popt options library   */
 
-static char        *m_pszOptuples[RC_NUMOPTS]; /* Option name value tuples    */
-static char        *m_szRcfile  = NULL;        /* rc file name                */
-static char        **m_pszSecs  = NULL;        /* Section names               */
-static popt_context m_Optcon;                  /* Context for parsing options */
-
-static struct popt_option m_pOptable[] = {
-    /* Long options are defined as 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_PAR_NAME, 'a', POPT_ARG_NONE, 0, RC_PAR_VAL, RC_PAR_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"},
-
-    /* Special stuff    */
-    POPT_AUTOHELP
-    {NULL, 0, 0, NULL, 0}
-};
-
 
 /***************************************
 * clioptNew(void)                      *
 * Construct a command line option      *
 ***************************************/
-rc_return_t clioptNew(void)
+rc_cliopt_t *clioptNew(void)
 {
+    rc_cliopt_t *pRetobj = calloc(1, sizeof (rc_cliopt_t));
+    if (pRetobj) {
+        pRetobj->m_pszOptuples = calloc(RC_NUMOPTS, sizeof (char *));
+        if (!pRetobj->m_pszOptuples)
+            RC_THROW(RC_ERR_MEM);
+    }
+    else
+        RC_THROW(RC_ERR_MEM);
+
+    return(pRetobj);
+
 /*    assert(s_pBintab == NULL); */           /* Error if constructed already */
 /*    s_pBintab = malloc(sizeof(*s_pBintab));*/ /* Allocate a cliopt instance   */
 /*    if (!s_pBintab)
@@ -98,17 +63,13 @@
 /*FIXME    optNew((rc_opt_t **)&s_pBintab->pOptlist->pvData);FIXME*/
 /*    s_pBintab->pOptlist->pvData = NULL;
     s_pBintab->pOptlist->pvNext = NULL;*/
-
-    memset(m_pszOptuples, 0L, sizeof(m_pszOptuples));
-
-    return(RC_THROW(RC_OK));
 }
 
 /***************************************
-* clioptPrintusage()                   *
+* clioptPrintusage(rc_cliopt_t *)      *
 * Print usage to command line stderr   *
 ***************************************/
-rc_return_t clioptPrintusage(void)
+rc_return_t clioptPrintusage(rc_cliopt_t *this)
 {
     popt_printusage(m_Optcon, stderr, 0);
 
@@ -116,35 +77,35 @@
 }
 
 /***************************************
-* clioptGetXXX()                       *
-* clioptSetXXX()                       *
+* clioptGetXXX(rc_cliopt_t *)          *
+* clioptSetXXX(rc_cliopt_t *)          *
 * Command line option accessors        *
 ***************************************/
-const char *clioptGetval(rc_opt_t Optname)
+const char *clioptGetval(rc_cliopt_t *this, rc_opt_t Optname)
 {
     if (!(Optname < RC_NUMOPTS))    /* Validate option range    */
         RC_THROW(RC_ERR_USE);
 
-    return((const char *)m_pszOptuples[Optname]);
+    return((const char *)this->m_pszOptuples[Optname]);
 }
 
-const char *clioptGetrcfile(void)
+const char *clioptGetrcfile(rc_cliopt_t *this)
 {
-    if (!m_szRcfile)
+    if (!this->m_szRcfile)
         RC_THROW(RC_ERR_USE);
 
-    return((const char *)m_szRcfile);
+    return((const char *)this->m_szRcfile);
 }
 
-const char **clioptGetsecs(void)
+const char **clioptGetsecs(rc_cliopt_t *this)
 {
-    if (!m_pszSecs)
+    if (!this->m_pszSecs)
         RC_THROW(RC_ERR_USE);
 
-    return((const char **)m_pszSecs);
+    return((const char **)this->m_pszSecs);
 }
 
-rc_return_t clioptSetval(rc_opt_t Optname, const char *kszOptval)
+rc_return_t clioptSetval(rc_cliopt_t *this, rc_opt_t Optname, const char *kszOptval)
 {
     if (!(Optname < RC_NUMOPTS))    /* Validate option range    */
         return(RC_THROW(RC_ERR_USE));
@@ -156,28 +117,28 @@
     return(RC_THROW(RC_OK));
 }
 
-rc_return_t clioptSetrcfile(const char *kszRc)
+rc_return_t clioptSetrcfile(rc_cliopt_t *this, const char *kszRc)
 {
-    if (m_szRcfile)                     /* Warn on overwrites */
+    if (this->m_szRcfile)                     /* Warn on overwrites */
         RC_THROW(RC_WRN_OWR);
     if (kszRc)
-        m_szRcfile = strdup(kszRc);
+        this->m_szRcfile = strdup(kszRc);
     else
         return(RC_THROW(RC_ERR_USE));   /* Incoming string is NULL? */
 
     return(RC_THROW(RC_OK));
 }
 
-rc_return_t clioptSetsecs(const char **pkszSecs)
+rc_return_t clioptSetsecs(rc_cliopt_t *this, const char **pkszSecs)
 {
     ex_t Except;
 
     assert (pkszSecs);          /* IF NULL, its broken */
-    if (m_pszSecs)              /* Warn on overwrites */
+    if (this->m_pszSecs)        /* Warn on overwrites */
         RC_THROW(RC_WRN_OWR);
 
     try {
-        m_pszSecs = vectorCopy(pkszSecs);
+        this->m_pszSecs = vectorCopy(pkszSecs);
     }
     catch(Except)
         rethrow;
@@ -185,91 +146,91 @@
     return(RC_THROW(RC_OK));
 }
 
-/***************************************
-* clioptProcess(int)                   *
-* Switch through available options     *
-* processing the corresponding option  *
-* and update option table accordingly  *
-***************************************/
-rc_return_t clioptProcess(int cliOption, const char *kszArg)
+/****************************************************
+* clioptProcess(rc_cliopt_t *, int, const char *)   *
+* Switch through available options                  *
+* processing the corresponding option               *
+* and update option table accordingly               *
+****************************************************/
+rc_return_t clioptProcess(rc_cliopt_t *this, int cliOption, const char *kszArg)
 {
     switch (cliOption) {
     /* Begin concrete (digital) options */
         case RC_USE_VAL:
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Usage    */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Usage    */
         case RC_DBG_VAL:
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Debug    */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Debug    */
         case RC_VER_VAL:
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Version  */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Version  */
         case RC_EVL_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Eval     */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Eval     */
         case RC_HLP_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Help     */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Help     */
         case RC_INF_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Info     */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Info     */
         case RC_LBL_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Label    */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Label    */
         case RC_PRN_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Print    */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Print    */
         case RC_PAR_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Print    */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Print    */
         case RC_SIL_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Silent   */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Silent   */
         case RC_RAW_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Raw      */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Raw      */
         case RC_VRB_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Verbose  */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Verbose  */
         case RC_EXC_VAL:                                   
-            clioptSetval(cliOption, RC_DEF_ON); break;   /* Exec     */
+            clioptSetval(this, cliOption, RC_DEF_ON); break;   /* Exec     */
 
     /* Begin abstract (nondigital) options */
         case RC_LOC_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Locations   */
+            clioptSetval(this, cliOption, kszArg); break; /* Locations   */
         case RC_CNF_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Conf file   */
+            clioptSetval(this, cliOption, kszArg); break; /* Conf file   */
         case RC_FNC_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Func file   */
+            clioptSetval(this, cliOption, kszArg); break; /* Func file   */
         case RC_QRY_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Format      */
+            clioptSetval(this, cliOption, kszArg); break; /* Format      */
         case RC_TMP_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Temp dir    */
+            clioptSetval(this, cliOption, kszArg); break; /* Temp dir    */
         case RC_OWN_VAL:
-            clioptSetval(cliOption, kszArg); break; /* User name   */
+            clioptSetval(this, cliOption, kszArg); break; /* User name   */
         case RC_GRP_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Group name  */
+            clioptSetval(this, cliOption, kszArg); break; /* Group name  */
         case RC_MSK_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Umask       */
+            clioptSetval(this, cliOption, kszArg); break; /* Umask       */
 
         case RC_ASS_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Assignregex */
+            clioptSetval(this, cliOption, kszArg); break; /* Assignregex */
         case RC_DEF_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Labelregex  */
+            clioptSetval(this, cliOption, kszArg); break; /* Labelregex  */
         case RC_REF_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Refregex    */
+            clioptSetval(this, cliOption, kszArg); break; /* Refregex    */
         case RC_PRM_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Paramregex  */
+            clioptSetval(this, cliOption, kszArg); break; /* Paramregex  */
         case RC_TRM_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Termregex   */
+            clioptSetval(this, cliOption, kszArg); break; /* Termregex   */
 
         case RC_NCF_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Configname  */
+            clioptSetval(this, cliOption, kszArg); break; /* Configname  */
         case RC_CMN_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Commonname  */
+            clioptSetval(this, cliOption, kszArg); break; /* Commonname  */
         case RC_DFL_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Defaultname */
+            clioptSetval(this, cliOption, kszArg); break; /* Defaultname */
         case RC_ERR_VAL:
-            clioptSetval(cliOption, kszArg); break; /* Errorname   */
+            clioptSetval(this, cliOption, kszArg); break; /* Errorname   */
         default : break;
     }
 
     return(RC_THROW(RC_OK));
 }
 
-/***************************************
-* clioptParseopts(int, const char **)  *
-* Parse command line options           *
-***************************************/
-rc_return_t clioptParseopts(int nArgs, const char *szVector[])
+/********************************************************
+* clioptParseopts(rc_cliopt_t *, int, const char **)    *
+* Parse command line options                            *
+********************************************************/
+rc_return_t clioptParseopts(rc_cliopt_t *this, int nArgs, const char *szVector[])
 {
     ex_t Except;
     char cliOpt = 0;    /* For argument parsing */
@@ -286,7 +247,7 @@
     /* Now do options processing */
     while ((cliOpt = popt_getnextopt(m_Optcon)) >= 0) {/* Loop, each time     */
         try {                                          /* eating a new option */
-            clioptProcess(cliOpt, popt_getoptarg(m_Optcon));
+            clioptProcess(this, cliOpt, popt_getoptarg(m_Optcon));
         }
         catch(Except) /* Error condition probably deserves attention */
             rethrow;
@@ -303,17 +264,17 @@
 }
 
 /******************************************
-* clioptParseargs(void)                   *
+* clioptParseargs(rc_cliopt_t *)          *
 * Parse command line rc file and sections *
 ******************************************/
-rc_return_t clioptParseargs(void)
+rc_return_t clioptParseargs(rc_cliopt_t *this)
 {
     ex_t Except;
 
     /* Use popt as a transport to read the user specified rcfile and sections */
     try {
-        clioptSetrcfile(popt_getarg(m_Optcon));
-        clioptSetsecs(popt_getargs(m_Optcon));
+        clioptSetrcfile(this, popt_getarg(m_Optcon));
+        clioptSetsecs(this, popt_getargs(m_Optcon));
     }
     catch(Except)
         rethrow; /* Our generic response */
@@ -322,10 +283,10 @@
 }
 
 /***************************************
-* clioptDelete(void)                   *
+* clioptDelete(rc_cliopt_t *)          *
 * Destruct a command line option       *
 ***************************************/
-rc_return_t clioptDelete(void)
+rc_return_t clioptDelete(rc_cliopt_t *this)
 {
     int i = 0;
 /*    ex_t Except;
@@ -343,24 +304,25 @@
     */
 
     popt_freecontext(m_Optcon);         /* Free the popt context      */
-    for (i = 0; i < RC_NUMOPTS; i++) {  /* Free the tuples themselves */
-        if (m_pszOptuples[i]) {
-            free(m_pszOptuples[i]);
-            m_pszOptuples[i] = NULL;
+    if (m_pszOptuples) {
+        for (i = 0; i < RC_NUMOPTS; i++) {  /* Free the tuples themselves */
+            if (m_pszOptuples[i]) {
+                free(m_pszOptuples[i]);
+                m_pszOptuples[i] = NULL;
+            }
         }
+        free(this->m_pszOptuples);
+        this->m_pszOptuples = NULL;
+    }
+    if (this->m_szRcfile) {         /* Free the rc file arg      */
+        free(this->m_szRcfile);
+        this->m_szRcfile = NULL;
+    }
+    if (this->m_pszSecs) {          /* Free the section name arg */
+        vectorDel(this->m_pszSecs);
+        this->m_pszSecs = NULL;
     }
 
-    if (m_szRcfile)                     /* Free the rc file arg       */
-        free(m_szRcfile);
-
-    if (m_pszSecs)                      /* Free the section name arg  */
-        vectorDel(m_pszSecs);
-
-/* FIXME BEGIN DEBUG */
-for (i = 0; i < RC_NUMOPTS; i++)
-    if (m_pszOptuples[i])
-        TRACE("Problem! Destructor failed!\n");
-/* FIXME END DEBUG */
-
+    free(this);
     return(RC_THROW(RC_OK));
 }

CVSTrac 2.0.1