OSSP CVS Repository

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

ossp-pkg/rc/rc_config.c 1.3 -> 1.4

--- rc_config.c  2002/02/28 15:30:04     1.3
+++ rc_config.c  2002/02/28 18:24:04     1.4
@@ -32,48 +32,76 @@
 
 #include "rc.h"
 #include "rc_private.h"
-#include "rc_const.h"   /* String constants               */
-#include "rc_option.h"  /* Option operations rely on popt */
-#include "val.h"        /* OSSP val library holds config  */
+#include "rc_const.h"               /* String constants                 */
+#include "rc_option.h"              /* Option operations rely on popt   */
+#include "val.h"                    /* OSSP val library holds config    */
+
+static rc_config_t *s_pInst = NULL; /* Singleton configuration instance */
 
 
 /***************************************
-* configConstruct(rc_config_t)         *
+* configConstruct(void)                *
 * Construct a configuration            *
 ***************************************/
-rc_return_t configConstruct(rc_config_t **ppConfig)
+rc_return_t configConstruct(void)
 {
     ex_t Except;
 
-    *ppConfig = malloc(sizeof(rc_config_t));    /* Allocate the    */
-    if (!*ppConfig)                             /* configuration's */
-        return(RC_THROW(RC_ERR_MEM));           /* identity        */
-
-    ex_try {                                    /* Make a val instance  */
-        val_create(&(*ppConfig)->pVal);         /* to hold individual   */
-    }                                           /* configuration values */
-    ex_catch(Except) {
-        rethrow;
+    if (s_pInst == NULL) {                  /* If we don't have one */
+        s_pInst = malloc(sizeof(s_pInst));  /* yet, then allocate   */
+        if (!s_pInst)                       /* a configuration      */
+            return(RC_THROW(RC_ERR_MEM));   /* instance             */
+        s_pInst->nLocks = 0;
+
+        ex_try {                            /* Make a val instance  */
+            val_create(&s_pInst->pVal);     /* to hold individual   */
+        }                                   /* configuration values */
+        ex_catch(Except) {
+            rethrow;
+        }
     }
+    s_pInst->nLocks++;                      /* FIXME not threadsafe */
 
     return(RC_THROW(RC_OK));
 }
 
+/***************************************
+* configGetXXXX(void)                  *
+* Configuration accessors              *
+***************************************/
+short configGetvers(void)
+{
+    ex_t Except;
+    short nVer = 0;
+
+    if (s_pInst != NULL) {
+        ex_try {
+            val_get(s_pInst->pVal, RC_VER_NAME, &nVer);
+        }
+        ex_catch(Except) {
+            rethrow;
+        }
+    }
+    else {
+        RC_THROW(RC_ERR_USE);
+        return(NULL);
+    }
+
+    return(nVer);
+}
+
 /************************************************
-* configLoad(rc_config_t, int, char **)         *
+* configLoad(int, char **)                      *
 * Load a configuration                          *
 ************************************************/
-rc_return_t configLoad(rc_config_t *pConfig, int argc, char *argv[])
+rc_return_t configLoad(int argc, char *argv[])
 {
     ex_t Except;
-    char *pTestopt = NULL;
-    const char *pFuncpath = "/sfw/etc/rc.func";
 
     ex_try {    /* Register and set configuration values */
-        val_reg(pConfig->pVal, RC_FNC_NAME, VAL_TYPE_PTR, RC_FNC_DESC, NULL);
-        val_set(pConfig->pVal, RC_FNC_NAME, pFuncpath);
-        val_get(pConfig->pVal, RC_FNC_NAME, &pTestopt);
-        fprintf(stderr, "Hier!%s!\n", pTestopt);
+        /* FIXME This is real bad, replace with our own  */
+        val_reg(s_pInst->pVal, RC_VER_NAME, VAL_TYPE_SHORT, RC_VER_DESC, NULL);
+        val_set(s_pInst->pVal, RC_VER_NAME, 1);
     }
     ex_catch(Except) {
         rethrow;
@@ -83,23 +111,28 @@
 }
 
 /***************************************
-* configDestruct(rc_config_t)          *
+* configDestruct(void)                 *
 * Destruct a configuration             *
 ***************************************/
-rc_return_t configDestruct(rc_config_t *pConfig)
+rc_return_t configDestruct(void)
 {
     ex_t Except;
 
-    assert(pConfig);
-    free(pConfig);      /* Deallocate configuration */
-    pConfig = NULL;     /* and clear its reference  */
-
-    ex_try {
-        val_destroy(pConfig->pVal); /* Destroy the val instance and assume */
-    }                               /* that its reference is cleared also  */
-    ex_catch(Except) {
-        rethrow;
+    if (s_pInst) {
+        if (!(--s_pInst->nLocks)) {         /* If nLocks is 0, dealloc  */
+            ex_try {                        /* FIXME, not thread-safe   */
+                val_destroy(s_pInst->pVal); /* Destroy val instance and */
+            }                               /* Assume that pVal is NULL */
+            ex_catch(Except) {
+                rethrow;
+            }
+
+            free(s_pInst);                  /* Deallocate configuration */
+            s_pInst = NULL;                 /* and clear its reference  */
+        }
     }
+    else
+        return(RC_THROW(RC_ERR_USE));
 
     return(RC_THROW(RC_OK));
 }

CVSTrac 2.0.1