commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue/geas/src/config configuration.c configurat...


From: Reinhard Mueller
Subject: gnue/geas/src/config configuration.c configurat...
Date: Tue, 29 May 2001 15:28:46 -0700

CVSROOT:        /cvs
Module name:    gnue
Changes by:     Reinhard Mueller <address@hidden>       01/05/29 15:28:46

Modified files:
        geas/src/config: configuration.c configuration.h 

Log message:
        reformatted

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/geas/src/config/configuration.c.diff?cvsroot=OldCVS&tr1=1.9&tr2=1.10&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/geas/src/config/configuration.h.diff?cvsroot=OldCVS&tr1=1.4&tr2=1.5&r1=text&r2=text

Patches:
Index: gnue/geas/src/config/configuration.c
diff -u gnue/geas/src/config/configuration.c:1.9 
gnue/geas/src/config/configuration.c:1.10
--- gnue/geas/src/config/configuration.c:1.9    Fri May 25 13:24:07 2001
+++ gnue/geas/src/config/configuration.c        Tue May 29 15:28:46 2001
@@ -1,23 +1,25 @@
-
 /*
-   geas - GNU Enterprise Application Server
- 
+   GEAS configuration management library
+
    Copyright (C) 2001 Free Software Foundation
- 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
- 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
- 
+
+   This file is part of the GNU Enterprise Application Server (GEAS)
+
+   GEAS is free software; you can redistribute it and/or modify it under
+   the terms of the GNU General Public License as published by the Free
+   Software Foundation; either version 2, or (at your option) any later
+   version.
+
+   GEAS is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+   for more details.
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
- 
+   along with GEAS; if not, write to the Free Software Foundation, Inc.,
+   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   $Id: configuration.c,v 1.10 2001/05/29 22:28:46 reinhard Exp $
 */
 
 /*
@@ -43,738 +45,823 @@
  */
 
 #include "config.h"
-
+#include "configuration.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include "configuration.h"
 #include "geas-server.h"
+
+/* ========================================================================= *\
+ * Private structures
+\* ========================================================================= */
+
+/* ------------------------------------------------------------------------- *\
+ * A line in the configuration file
+\* ------------------------------------------------------------------------- */
+struct _config_item
+{
+  char *key;
+  char *value;
+};
+
+typedef struct _config_item config_item;
 
-/* internal structures */
-typedef struct config_item
-   {
-   char *key;
-   char *value;
-   }
-config_item;
-
-typedef struct configuration_data
-   {
-   char *name;
-   GList *database;
-   GList *globals;
-   }
-configuration_data;
+/* ------------------------------------------------------------------------- *\
+ * The complete configuration
+\* ------------------------------------------------------------------------- */
 
+struct _configuration_data
+{
+  char *name;
+  GList *database;
+  GList *globals;
+};
+
+typedef struct _configuration_data configuration_data;
+
+/* ========================================================================= *\
+ * Global variables
+\* ========================================================================= */
+
 configuration configdata = NO_CONFIGURATION;
 
 static int config_failed = 0;
 
-/* private functions */
-static void read_shadow_password(configuration_data * c,
+/* ========================================================================= *\
+ * Private functions
+\* ========================================================================= */
 
-                                 const char *shadowpwfile);
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static configuration_data *
+alloc_configuration_data (const char *name)
+{
+  configuration_data *c =
+    (configuration_data *) g_malloc0 (sizeof (configuration_data));
+  if (c)
+    {
+      c->name = g_strdup (name);
+      c->globals = NULL;
+      c->database = NULL;
+    }
+  return (c);
+}
 
-static void enter_default_properties(configuration_data * c);
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static configuration_data *
+find_database (configuration_data *c, const char *name)
+{
+  configuration_data *i = NULL;
+  GList *l = c->database;
 
-static config_item *alloc_config_item(const char *key, const char *value);
-static void free_config_item(config_item * item);
+  while (l)
+    {
+      i = (configuration_data *) l->data;
+      if (g_strcasecmp (i->name, name) == 0)
+       return (i);
+      l = g_list_next (l);
+    }
+  return (NULL);
+}
 
-static void add_section_option(configuration_data * c, const char *key,
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static void
+free_config_item (config_item * item)
+{
+  if (item)
+    {
+      if (item->key)
+       g_free (item->key);
+      if (item->value)
+       g_free (item->value);
+      g_free (item);
+    }
+}
 
-                               const char *value);
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static config_item *
+alloc_config_item (const char *key, const char *value)
+{
+  config_item *i = (config_item *) g_malloc0 (sizeof (config_item));
+  char *p, tmp;
 
-static configuration_data *alloc_configuration_data(const char *name);
+  if (i)
+    {
+      i->key = g_strdup (key);
+      p = i->key;
+      while (p != NULL && *p != '\0')
+       {
+         *p = tolower (*p);
+         p++;
+       }
 
-static configuration_data *find_database(configuration_data * c,
+      if (value)
+       {
+         /* if there's an extra space, truncate line */
+         p = strchr (value, ' ');
+         if (p && isspace (*p))
+           {
+             fprintf (stderr,
+                      "warning: option '%s' has been truncated from '%s' ",
+                      key, value);
+             tmp = *p;
+             *p = '\0';
+             fprintf (stderr, "to '%s'\n", value);
+           }
+         i->value = g_strdup (value);
+         if (p)
+           *p = tmp;
+       }
+      else
+       i->value = g_strdup (value);
+      if (!i->key || !i->value)
+       {
+         free_config_item (i);
+         return (NULL);
+       }
+    }
+  return (i);
+}
 
-      const char *name);
-static void add_database(configuration_data * c, const char *name);
-static void add_database_option(configuration_data * c, const char *name,
-                                const char *key, const char *value);
+/* ------------------------------------------------------------------------- *\
+ * use this to add a key/value pai to a section
+\* ------------------------------------------------------------------------- */
 static void
+add_section_option (configuration_data * c, const char *key,
+                   const char *value)
+{
+  config_item *i;
 
-read_main_configuration(configuration_data * c, const char *filename);
+  /* ignore duplicates */
+  if (get_global_option ((configuration) c, key) != NULL)
+    return;
+  /* allocate and store new data */
+  i = alloc_config_item (key, value);
+  if (i)
 
+    c->globals = g_list_append (c->globals, i);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
 static void
+add_database (configuration_data * c, const char *name)
+{
+  configuration_data *i;
 
-real_show_configuration(configuration config, int indent,
-                        gboolean show_passwords);
+  /* ignore duplicates */
+  if (find_database ((configuration) c, name) != NULL)
+    return;
+  /* allocate and store new data */
+  i = alloc_configuration_data (name);
+  if (i)
+    c->database = g_list_append (c->database, i);
+}
 
-/* load/release configuration files */
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static void
+add_database_option (configuration_data * c,
+                    const char *name, const char *key, const char *value)
+{
+  configuration_data *i;
+
+  /* ignore duplicates */
+  i = find_database ((configuration) c, key);
+  if (i == NULL)
+    {
+      add_database (c, name);
+      i = find_database ((configuration) c, name);
+      if (!i)
+       {
+         printf ("error: failed to add option '%s' in database '%s'\n",
+                 key, name);
+         config_failed = 1;
+         return;
+       }
+    }
+
+  /* allocate and store new data */
+  add_section_option (i, key, value);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static void
+read_shadow_password (configuration_data * c, const char *shadowpwfile)
+{
+  const char *adminuser = get_global_option (c, "adminuser");
+  int errdone = 0;
+  char *p, *q, *r;
+
+  char buf[256];
+  FILE *fp = fopen (shadowpwfile, "r");
+
+  if (!fp)
+    return;
+  buf[255] = '\0';
+  while (!feof (fp))
+    {
+      if (fgets (buf, 255, fp) != NULL)
+       {
+         /* strip comments */
+         p = strchr (buf, '#');
+         if (p)
+           *p = '\0';
+         for (p = buf; *p != '\0'; p++)
+           if (!isspace (*p))
+             break;
+         if (*p == '\0')
+           continue;           /* blank line or comment only line
+                                */
+         q = strchr (p, ':');
+         if (!q)
+           {
+             config_failed = 1;
+             if (!errdone)
+               fprintf (stderr, "Shadow password file error.");
+             errdone = 1;
+             continue;
+           }
+         *q = '\0';
+
+         q++;
+         /* trim whitespace from left side */
+         while (isspace (*p))
+           p++;
+         while (isspace (p[strlen (p) - 1]))
+           p[strlen (p) - 1] = '\0';
+         /* trim whitespace from right side */
+         while (isspace (*q))
+           q++;
+         while (isspace (q[strlen (q) - 1]))
+           q[strlen (q) - 1] = '\0';
+         if (adminuser && strcmp (p, "adminuser") == 0)
+           add_section_option (c, "adminpassword", q);
+         else
+           add_database_option (c, p, "password", q);
+       }
+    }
+  fclose (fp);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static void
+real_show_configuration (configuration config,
+                        int indent, gboolean show_passwords)
+{
+  configuration_data *c = (configuration_data *) config;
+  GList *l;
+
+  int loop;
+  config_item *i;
+
+  if (c)
+    {
+      for (loop = 0; loop < indent; loop++)
+       printf (" ");
+      if (c->name)
+       {
+         printf ("Section: %s\n", c->name);
+         for (loop = 0; loop < indent; loop++)
+           printf (" ");
+         printf ("--------\n");
+       }
+      else
+       {
+         printf ("Section: <unknown>\n", c->name);
+         for (loop = 0; loop < indent; loop++)
+           printf (" ");
+         printf ("--------\n");
+       }
+      l = c->globals;
+      while (l)
+       {
+         for (loop = 0; loop < indent; loop++)
+           printf (" ");
+         i = (config_item *) l->data;
+         if (show_passwords == FALSE && strstr (i->key, "password") != NULL)
+           printf ("%s \t: <hidden for security>\n", i->key);
+         else
+           printf ("%s \t: %s\n", i->key, i->value);
+         l = g_list_next (l);
+       }
+
+      l = c->database;
+      while (l)
+       {
+         printf ("\n");
+         real_show_configuration ((configuration) l->data, indent + 4,
+                                  show_passwords);
+         l = g_list_next (l);
+       }
+      printf ("\n");
+    }
+}
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static char *
+read_line (FILE * fp)
+{
+  char buf[1024];
+  char *p, *q, *retval;
+
+  int onespace = 1;
+
+  buf[1023] = '\0';
+  while (1)
+    {
+      if (fgets (buf, 1022, fp) != NULL)
+       {
+         while (buf[strlen (buf) - 1] == '\r'
+                || buf[strlen (buf) - 1] ==
+                '\n') buf[strlen (buf) - 1] = '\0';
+         /* strip comments */
+         p = strchr (buf, '#');
+         if (p)
+           *p = '\0';
+         /* strip whitespace from ends */
+         p = buf;
+         while (isspace (*p))
+           p++;
+         if (*p == '\0')
+           continue;
+         while (isspace (p[strlen (p) - 1]))
+           p[strlen (p) - 1] = '\0';
+         /* skip empty lines/only comment lines */
+         if (*p == '\0')
+           continue;
+         /* strip double spaces */
+         retval = p;
+         q = p;
+         while (*p != '\0')
+           {
+             p = strchr (p, ' ');
+             if (!p)
+               break;
+             p++;
+             q = p;
+             while (isspace (*q))
+               q++;
+             memmove (p, q, strlen (q) + 1);
+             p++;
+           }
+
+         /* got a valid line */
+         return (retval);
+       }
+
+      /* EOF return NULL */
+      if (feof (fp))
+       return (NULL);
+    }
+}
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+#define MODE_UNKNOWN    0
+#define MODE_DATABASE 1
+#define MODE_GLOBAL     2
+
+/* ------------------------------------------------------------------------- *\
+ * 
+\* ------------------------------------------------------------------------- */
+static void
+read_main_configuration (configuration_data * c, const char *filename)
+{
+  int mode = MODE_GLOBAL;
+  const char *adminuser = get_global_option (c, "adminuser");
+  int errdone = 0;
+  char *p, *q, *tmp;
+  FILE *fp = fopen (filename, "r");
+  int done = 0;
+  char *dbname = NULL;
+
+  if (!fp)
+    {
+      add_section_option (c, "configfile", "unknown");
+      return;
+    }
+  add_section_option (c, "configfile", filename);
+
+  while (!done)
+    {
+      p = read_line (fp);
+      if (!p)
+       break;
+      if (p[0] == '[' && p[strlen (p) - 1] == ']')
+       {
+         mode = MODE_UNKNOWN;
+         /* strip [ ] braces */
+         if (p[1] == ' ')
+           p += 2;
+         else
+           p += 1;
+         p[strlen (p) - 1] = '\0';
+         if (isspace (p[strlen (p) - 1]))
+           p[strlen (p) - 1] = '\0';
+         q = strchr (p, ' ');
+         if (q)
+           *q++ = '\0';
+
+         if (g_strcasecmp (p, "database") == 0)
+           {
+             if (q && *q != '\0')
+               {
+                 mode = MODE_DATABASE;
+                 if (dbname)
+                   g_free (dbname);
+                 dbname = g_strdup (q);
+                 add_database (c, q);
+               }
+             else
+               {
+                 mode = MODE_UNKNOWN;
+                 fprintf (stderr, "error: database name not given.\n");
+                 config_failed = 1;
+               }
+           }
+         else if (g_strcasecmp (p, "global") == 0)
+           {
+             mode = MODE_GLOBAL;
+           }
+         else
+           {
+             config_failed = 1;
+             fprintf (stderr, "error: unknown section '%s'\n", p);
+           }
+       }
+      else
+       {
+         q = strchr (p, ' ');
+         if (q)
+           *q++ = '\0';
+         if (!q)
+           {
+             config_failed = 1;
+             fprintf (stderr,
+                      "error: option %s does not have a value.\n", p);
+             continue;
+           }
+         /* key to lower case */
+         tmp = p;
+         while (p != NULL && *p != '\0')
+           {
+             *p = tolower (*p);
+             p++;
+           }
+         p = tmp;
+
+         switch (mode)
+           {
+           case MODE_UNKNOWN:
+             break;
+           case MODE_DATABASE:
+             /* printf( "database %s : %s = %s\n" , dbname , p , q
+                ); */
+             add_database_option (c, dbname, p, q);
+             break;
+           case MODE_GLOBAL:
+             /* printf( "global: %s = %s\n" , p , q ); */
+             if (strstr (p, "password") != NULL)
+               {
+                 fprintf (stderr,
+                          "error: passwords must not be included in the main 
configuration file.\n");
+                 fprintf (stderr,
+                          "       the shadow password file should be used, 
instead.\n");
+                 config_failed = 1;
+               }
+             add_section_option (c, p, q);
+             break;
+           }
+       }
+    }
+  fclose (fp);
+  if (dbname)
+    g_free (dbname);
+}
+
+/* ------------------------------------------------------------------------- *\
+ * to add default options, copy the line below, replacing key and value with
+ * appropriate strings:
+   add_section_option (c, "key", "value"); 
+ * eg:
+ * to set a default debug level of 5, use:
+   add_section_option (c, "debuglevel", "5");
+ * note that command line options override the configuration file settings.
+\* ------------------------------------------------------------------------- */
+static void
+enter_default_properties (configuration_data * c)
+{
+  /* none defined yet */
+}
+
+/* ------------------------------------------------------------------------- *\
+ * load configuration file
+\* ------------------------------------------------------------------------- */
 configuration
-load_configuration_file(const char *filename, const char *shadowpwfile)
-   {
-   configuration_data *c = alloc_configuration_data("global options");
+load_configuration_file (const char *filename, const char *shadowpwfile)
+{
+  configuration_data *c = alloc_configuration_data ("global options");
 
-   /* Instructions for altering this module:
+  /* Instructions for altering this module:
 
-      to add a global option: call add_section_option(c,key,value)
+     to add a global option: call add_section_option(c,key,value)
 
-      to add a database specific option: call
-      add_database_option(c,databasename,key,value)
+     to add a database specific option: call
+     add_database_option(c,databasename,key,value)
 
-    */
+   */
 
-   if (c)
-      {
+  if (c)
+    {
       /* read main configuration file */
       if (filename != NULL)
-         read_main_configuration(c, filename);
+       read_main_configuration (c, filename);
 
       /* set defaults: function ignores if already set */
-      enter_default_properties(c);
+      enter_default_properties (c);
 
       /* read shadow password file */
       if (shadowpwfile != NULL)
-         read_shadow_password(c, shadowpwfile);
-      }
+       read_shadow_password (c, shadowpwfile);
+    }
 
-   /* if anything failed, return nothing */
-   if (config_failed != 0)
-      {
-      free_configuration(c);
+  /* if anything failed, return nothing */
+  if (config_failed != 0)
+    {
+      free_configuration (c);
       return (NULL);
-      }
+    }
 
-   return ((configuration) c);
-   }
+  return ((configuration) c);
+}
 
+/* ------------------------------------------------------------------------- *\
+ * free configuration data in memory
+\* ------------------------------------------------------------------------- */
 void
-free_configuration(configuration config)
-   {
-   configuration_data *c = (configuration_data *) config;
-   GList *l;
+free_configuration (configuration config)
+{
+  configuration_data *c = (configuration_data *) config;
+  GList *l;
 
-   if (c)
-      {
+  if (c)
+    {
       /* free name */
       if (c->name)
-         g_free(c->name);
+       g_free (c->name);
       /* free options in this section */
       l = c->globals;
       while (l)
-         {
-         config_item *i = (config_item *) l->data;
+       {
+         config_item *i = (config_item *) l->data;
 
-         free_config_item(i);
-         l = g_list_next(l);
-         }
-      g_list_free(c->globals);
+         free_config_item (i);
+         l = g_list_next (l);
+       }
+      g_list_free (c->globals);
 
       /* free contained sections */
       l = c->database;
       while (l)
-         {
-         free_configuration((configuration) l->data);
-         l = g_list_next(l);
-         }
-      g_list_free(c->database);
+       {
+         free_configuration ((configuration) l->data);
+         l = g_list_next (l);
+       }
+      g_list_free (c->database);
       /* free data */
-      g_free(c);
-      }
-   }
+      g_free (c);
+    }
+}
 
+/* ------------------------------------------------------------------------- *\
+ * Print out configuration for debugging purposes
+\* ------------------------------------------------------------------------- */
 void
-show_configuration(configuration config, gboolean show_passwords)
-   {
-   printf("\nConfiguration:\n\n");
-   real_show_configuration(config, 0, show_passwords);
-   }
+show_configuration (configuration config, gboolean show_passwords)
+{
+  printf ("\nConfiguration:\n\n");
+  real_show_configuration (config, 0, show_passwords);
+}
 
-/* track databases in the file */
+/* ---------------------------------------------------------------------- *\
+ * count databases in the file
+\* ------------------------------------------------------------------------- */
 int
-count_configured_databases(configuration config)
-   {
-   int l;
-   if( config == NO_CONFIGURATION ) return( 0 );
-   l = g_list_length(((configuration_data *) config)->database);
+count_configured_databases (configuration config)
+{
+  int l;
+  if (config == NO_CONFIGURATION)
+    return (0);
+  l = g_list_length (((configuration_data *) config)->database);
 
-   return (l);
-   }
+  return (l);
+}
 
+/* ------------------------------------------------------------------------- *\
+ * get the name of a specific database
+\* ------------------------------------------------------------------------- */
 const char *
-get_database_name(configuration config, int index)
-   {
-      configuration_data *c = (configuration_data *) config;
+get_database_name (configuration config, int index)
+{
+  configuration_data *c = (configuration_data *) config;
 
-      /* validate index */
-      if (index < 0 || index >= count_configured_databases(config))
-       return (NULL);
+  /* validate index */
+  if (index < 0 || index >= count_configured_databases (config))
+    return (NULL);
+
+  /* find entry */
+  c = (configuration_data *) g_list_nth_data (c->database, index);
+  if (!c)
+    return (NULL);
 
-      /* find entry */
-      c = (configuration_data *) g_list_nth_data(c->database, index);
-      if (!c) return (NULL);
-
-      /* return its name */
-      return (c->name);
-   }
+  /* return its name */
+  return (c->name);
+}
 
-/* global configuration */
+/* ------------------------------------------------------------------------- *\
+ * Global option - general
+\* ------------------------------------------------------------------------- */
 const char *
-get_global_option(configuration config, const char *key)
-   {
-   config_item *i = NULL;
-   GList *l = ((configuration_data *) config)->globals;
+get_global_option (configuration config, const char *key)
+{
+  config_item *i = NULL;
+  GList *l = ((configuration_data *) config)->globals;
 
-   while (l)
-      {
+  while (l)
+    {
       i = (config_item *) l->data;
-      if (g_strcasecmp(i->key, key) == 0) {
-         return (i->value);
-      }
-      l = g_list_next(l);
-      }
-   return (NULL);
-   }
+      if (g_strcasecmp (i->key, key) == 0)
+       {
+         return (i->value);
+       }
+      l = g_list_next (l);
+    }
+  return (NULL);
+}
 
+/* ------------------------------------------------------------------------- *\
+ * Global option - Integer
+\* ------------------------------------------------------------------------- */
 int
-get_global_option_int(configuration config, const char *key,int defaultval)
+get_global_option_int (configuration config, const char *key, int defaultval)
 {
-   const char *o = get_global_option( config , key );
-   if( !o ) return(defaultval);
-   return( atoi(o) );
+  const char *o = get_global_option (config, key);
+  if (!o)
+    return (defaultval);
+  return (atoi (o));
 }
 
+/* ------------------------------------------------------------------------- *\
+ * Global option - Boolean
+\* ------------------------------------------------------------------------- */
 gboolean
-get_global_option_bool(configuration config, const char *key,gboolean 
defaultval)
+get_global_option_bool (configuration config, const char *key,
+                       gboolean defaultval)
 {
-   const char *o = get_global_option( config , key );
-   if( !o ) return(defaultval);
-
-   if( g_strcasecmp(o,"1")     == 0 ) return( TRUE );
-   if( g_strcasecmp(o,"yes")   == 0 ) return( TRUE );
-   if( g_strcasecmp(o,"true")  == 0 ) return( TRUE );
+  const char *o = get_global_option (config, key);
+  if (!o)
+    return (defaultval);
+
+  if (g_strcasecmp (o, "1") == 0)
+    return (TRUE);
+  if (g_strcasecmp (o, "yes") == 0)
+    return (TRUE);
+  if (g_strcasecmp (o, "true") == 0)
+    return (TRUE);
+
+  if (g_strcasecmp (o, "1") == 0)
+    return (FALSE);
+  if (g_strcasecmp (o, "no") == 0)
+    return (FALSE);
+  if (g_strcasecmp (o, "false") == 0)
+    return (FALSE);
 
-   if( g_strcasecmp(o,"1")     == 0 ) return( FALSE );
-   if( g_strcasecmp(o,"no")    == 0 ) return( FALSE );
-   if( g_strcasecmp(o,"false") == 0 ) return( FALSE );
-
-   return( defaultval );
+  return (defaultval);
 }
 
-const char *get_global_option_str (configuration config, const char *key , 
const char *defaultval )
+/* ------------------------------------------------------------------------- *\
+ * Global option - String
+\* ------------------------------------------------------------------------- */
+const char *
+get_global_option_str (configuration config, const char *key,
+                      const char *defaultval)
 {
-   const char *o = get_global_option(config,key);
-   if( o ) return(o);
-   else return(defaultval);
+  const char *o = get_global_option (config, key);
+  if (o)
+    return (o);
+  else
+    return (defaultval);
 }
 
-/* find configuration item for a particular database */
+/* ------------------------------------------------------------------------- *\
+ * Database option - general
+\* ------------------------------------------------------------------------- */
 const char *
-get_database_option(configuration tree, const char *database, const char *key)
-   {
-   configuration_data *c = find_database((configuration_data *) tree,
-                                         database);
-   if (c)
-      {
+get_database_option (configuration tree, const char *database,
+                    const char *key)
+{
+  configuration_data *c = find_database ((configuration_data *) tree,
+                                        database);
+  if (c)
+    {
       // self_test_message( "database option: db = %s, opt = %s, val = %s" , 
database , key , get_global_option(c,key) );
-      return (get_global_option(c, key));
-      }
-      // self_test_message( "unknown database: %s" , database );
-      return (NULL);
-   }
+      return (get_global_option (c, key));
+    }
+  // self_test_message( "unknown database: %s" , database );
+  return (NULL);
+}
 
-const char *get_database_option_str (configuration tree,const char 
*databasename, const char *key,
-                                                                         const 
char *defaultvalue)
+/* ------------------------------------------------------------------------- *\
+ * Database option - String
+\* ------------------------------------------------------------------------- */
+const char *
+get_database_option_str (configuration tree, const char *databasename,
+                        const char *key, const char *defaultvalue)
 {
-   const char *o = get_database_option(tree,databasename,key);
-   if( o ) return(o);
-   else return(defaultvalue);
+  const char *o = get_database_option (tree, databasename, key);
+  if (o)
+    return (o);
+  else
+    return (defaultvalue);
 }
 
-
-gboolean    get_database_option_bool(configuration tree,const char 
*databasename, const char *key,
-                                                                         
gboolean defaultval)
+/* ------------------------------------------------------------------------- *\
+ * Database option - Boolean
+\* ------------------------------------------------------------------------- */
+gboolean
+get_database_option_bool (configuration tree, const char *databasename,
+                         const char *key, gboolean defaultval)
 {
-   const char *o = get_database_option( tree , databasename , key );
-   if( !o ) return(defaultval);
-
-   if( g_strcasecmp(o,"1")     == 0 ) return( TRUE );
-   if( g_strcasecmp(o,"yes")   == 0 ) return( TRUE );
-   if( g_strcasecmp(o,"true")  == 0 ) return( TRUE );
+  const char *o = get_database_option (tree, databasename, key);
+  if (!o)
+    return (defaultval);
+
+  if (g_strcasecmp (o, "1") == 0)
+    return (TRUE);
+  if (g_strcasecmp (o, "yes") == 0)
+    return (TRUE);
+  if (g_strcasecmp (o, "true") == 0)
+    return (TRUE);
+
+  if (g_strcasecmp (o, "1") == 0)
+    return (FALSE);
+  if (g_strcasecmp (o, "no") == 0)
+    return (FALSE);
+  if (g_strcasecmp (o, "false") == 0)
+    return (FALSE);
 
-   if( g_strcasecmp(o,"1")     == 0 ) return( FALSE );
-   if( g_strcasecmp(o,"no")    == 0 ) return( FALSE );
-   if( g_strcasecmp(o,"false") == 0 ) return( FALSE );
-
-   return( defaultval );
+  return (defaultval);
 }
 
-int         get_database_option_int (configuration tree,const char 
*databasename, const char *key,
-                                                                         int 
defaultval)
+/* ------------------------------------------------------------------------- *\
+ * Database option - Integer
+\* ------------------------------------------------------------------------- */
+int
+get_database_option_int (configuration tree, const char *databasename,
+                        const char *key, int defaultval)
 {
-   const char *o = get_database_option( tree , databasename , key );
-   if( o ) return( atoi(o) );
-   return( defaultval );
+  const char *o = get_database_option (tree, databasename, key);
+  if (o)
+    return (atoi (o));
+  return (defaultval);
 }
-
-/* private functions */
-
-static void
-read_shadow_password(configuration_data * c, const char *shadowpwfile)
-   {
-   const char *adminuser = get_global_option(c, "adminuser");
-   int errdone = 0;
-   char *p, *q, *r;
-
-   char buf[256];
-   FILE *fp = fopen(shadowpwfile, "r");
-
-   if (!fp)
-      return ;
-   buf[255] = '\0';
-   while (!feof(fp))
-      {
-      if (fgets(buf, 255, fp) != NULL)
-         {
-         /* strip comments */
-         p = strchr(buf, '#');
-         if (p)
-            *p = '\0';
-         for (p = buf; *p != '\0'; p++)
-            if (!isspace(*p))
-               break;
-         if (*p == '\0')
-            continue;                                   /* blank line or 
comment only line
-                                                                               
                                          */
-         q = strchr(p, ':');
-         if (!q)
-            {
-            config_failed = 1;
-            if (!errdone)
-               fprintf(stderr, "Shadow password file error.");
-            errdone = 1;
-            continue;
-            }
-         *q = '\0';
-
-         q++;
-         /* trim whitespace from left side */
-         while (isspace(*p))
-            p++;
-         while (isspace(p[strlen(p) - 1]))
-            p[strlen(p) - 1] = '\0';
-         /* trim whitespace from right side */
-         while (isspace(*q))
-            q++;
-         while (isspace(q[strlen(q) - 1]))
-            q[strlen(q) - 1] = '\0';
-           if (adminuser && strcmp(p, "adminuser") == 0)
-            add_section_option(c, "adminpassword", q);
-         else
-            add_database_option(c, p, "password", q);
-         }
-      }
-   fclose(fp);
-   }
-
-static config_item *
-alloc_config_item(const char *key, const char *value)
-   {
-   config_item *i = (config_item *) g_malloc0(sizeof(config_item));
-   char *p, tmp;
-
-   if (i)
-      {
-      i->key = g_strdup(key);
-      p = i->key;
-      while (p != NULL && *p != '\0')
-         {
-         *p = tolower(*p);
-         p++;
-         }
-
-      if (value)
-         {
-         /* if there's an extra space, truncate line */
-         p = strchr(value, ' ');
-         if (p && isspace(*p))
-            {
-            fprintf(stderr,
-                    "warning: option '%s' has been truncated from '%s' ",
-                    key, value);
-            tmp = *p;
-            *p = '\0';
-            fprintf(stderr, "to '%s'\n", value);
-            }
-         i->value = g_strdup(value);
-         if (p)
-            *p = tmp;
-         }
-      else
-         i->value = g_strdup(value);
-      if (!i->key || !i->value)
-         {
-         free_config_item(i);
-         return (NULL);
-         }
-      }
-   return (i);
-   }
 
-static void
-free_config_item(config_item * item)
-   {
-   if (item)
-      {
-      if (item->key)
-         g_free(item->key);
-      if (item->value)
-         g_free(item->value);
-      g_free(item);
-      }
-   }
-
-static configuration_data *
-alloc_configuration_data(const char *name)
-   {
-   configuration_data *c =
-
-      (configuration_data *) g_malloc0(sizeof(configuration_data));
-   if (c)
-      {
-      c->name = g_strdup(name);
-      c->globals = NULL;
-      c->database = NULL;
-      }
-   return (c);
-   }
-
-/* use this to add a key/value pai to a section */
-static void
-add_section_option(configuration_data * c, const char *key, const char *value)
-   {
-   config_item *i;
-
-   /* ignore duplicates */
-   if (get_global_option((configuration) c, key) != NULL)
-      return ;
-   /* allocate and store new data */
-   i = alloc_config_item(key, value);
-   if (i)
-
-      c->globals = g_list_append(c->globals, i);
-   }
-
-static void
-add_database(configuration_data * c, const char *name)
-   {
-   configuration_data *i;
-
-   /* ignore duplicates */
-   if (find_database((configuration) c, name) != NULL)
-      return ;
-   /* allocate and store new data */
-   i = alloc_configuration_data(name);
-   if (i)
-      c->database = g_list_append(c->database, i);
-   }
-
-static void
-add_database_option(configuration_data * c,
-                    const char *name, const char *key, const char *value)
-   {
-   configuration_data *i;
-
-   /* ignore duplicates */
-   i = find_database((configuration) c, key);
-   if (i == NULL)
-      {
-      add_database(c, name);
-      i = find_database((configuration) c, name);
-      if (!i)
-         {
-         printf("error: failed to add option '%s' in database '%s'\n",
-                key, name);
-         config_failed = 1;
-         return ;
-         }
-      }
-
-   /* allocate and store new data */
-   add_section_option(i, key, value);
-   }
-
-static configuration_data *
-find_database(configuration_data * c, const char *name)
-   {
-   configuration_data *i = NULL;
-   GList *l = c->database;
-
-   while (l)
-      {
-      i = (configuration_data *) l->data;
-      if (g_strcasecmp(i->name, name) == 0)
-         return (i);
-      l = g_list_next(l);
-      }
-   return (NULL);
-   }
-
-void
-real_show_configuration(configuration config,
-                        int indent, gboolean show_passwords)
-   {
-   configuration_data *c = (configuration_data *) config;
-   GList *l;
-
-   int loop;
-   config_item *i;
-
-   if (c)
-      {
-      for (loop = 0; loop < indent; loop++)
-         printf(" ");
-      if (c->name)
-         {
-         printf("Section: %s\n", c->name);
-         for (loop = 0; loop < indent; loop++)
-            printf(" ");
-         printf("--------\n");
-         }
-      else
-         {
-         printf("Section: <unknown>\n", c->name);
-         for (loop = 0; loop < indent; loop++)
-            printf(" ");
-         printf("--------\n");
-         }
-      l = c->globals;
-      while (l)
-         {
-         for (loop = 0; loop < indent; loop++)
-            printf(" ");
-         i = (config_item *) l->data;
-         if (show_passwords == FALSE
-               && strstr(i->key, "password") != NULL)
-            printf("%s \t: <hidden for security>\n", i->key);
-         else
-            printf("%s \t: %s\n", i->key, i->value);
-         l = g_list_next(l);
-         }
-
-      l = c->database;
-      while (l)
-         {
-         printf("\n");
-         real_show_configuration((configuration) l->data, indent + 4,
-                                 show_passwords);
-         l = g_list_next(l);
-         }
-      printf("\n");
-      }
-   }
-
-static char *
-read_line(FILE * fp)
-   {
-   char buf[1024];
-   char *p, *q, *retval;
-
-   int onespace = 1;
-
-   buf[1023] = '\0';
-   while (1)
-      {
-      if (fgets(buf, 1022, fp) != NULL)
-         {
-         while (buf[strlen(buf) - 1] == '\r'
-                || buf[strlen(buf) - 1] ==
-                '\n') buf[strlen(buf) - 1] = '\0';
-         /* strip comments */
-         p = strchr(buf, '#');
-         if (p)
-            *p = '\0';
-         /* strip whitespace from ends */
-         p = buf;
-         while (isspace(*p))
-            p++;
-         if (*p == '\0')
-            continue;
-         while (isspace(p[strlen(p) - 1]))
-            p[strlen(p) - 1] = '\0';
-         /* skip empty lines/only comment lines */
-         if (*p == '\0')
-            continue;
-         /* strip double spaces */
-         retval = p;
-         q = p;
-         while (*p != '\0')
-            {
-            p = strchr(p, ' ');
-            if (!p)
-               break;
-            p++;
-            q = p;
-            while (isspace(*q))
-               q++;
-            memmove(p, q, strlen(q) + 1);
-            p++;
-            }
-
-         /* got a valid line */
-         return (retval);
-         }
-
-      /* EOF return NULL */
-      if (feof(fp))
-         return (NULL);
-      }
-   }
-
-#define MODE_UNKNOWN    0
-#define MODE_DATABASE 1
-#define MODE_GLOBAL     2
-
-static void
-read_main_configuration(configuration_data * c, const char *filename)
-   {
-   int mode = MODE_GLOBAL;
-   const char *adminuser = get_global_option(c, "adminuser");
-   int errdone = 0;
-   char *p, *q, *tmp;
-   FILE *fp = fopen(filename, "r");
-   int done = 0;
-   char *dbname = NULL;
-
-   if (!fp) {
-      add_section_option(c, "configfile", "unknown");
-      return ;
-   }
-   add_section_option(c, "configfile", filename);
-
-   while (!done)
-      {
-      p = read_line(fp);
-      if (!p)
-         break;
-      if (p[0] == '[' && p[strlen(p) - 1] == ']')
-         {
-         mode = MODE_UNKNOWN;
-         /* strip [ ] braces */
-         if (p[1] == ' ')
-            p += 2;
-         else
-            p += 1;
-         p[strlen(p) - 1] = '\0';
-         if (isspace(p[strlen(p) - 1]))
-            p[strlen(p) - 1] = '\0';
-         q = strchr(p, ' ');
-         if (q)
-            *q++ = '\0';
-
-         if (g_strcasecmp(p, "database") == 0)
-            {
-            if (q && *q != '\0')
-               {
-               mode = MODE_DATABASE;
-               if (dbname)
-                  g_free(dbname);
-               dbname = g_strdup(q);
-               add_database(c, q);
-               }
-            else
-               {
-               mode = MODE_UNKNOWN;
-               fprintf(stderr, "error: database name not given.\n");
-               config_failed = 1;
-               }
-            }
-         else if (g_strcasecmp(p, "global") == 0)
-            {
-            mode = MODE_GLOBAL;
-            }
-         else
-            {
-            config_failed = 1;
-            fprintf(stderr, "error: unknown section '%s'\n", p);
-            }
-         }
-      else
-         {
-         q = strchr(p, ' ');
-         if (q)
-            *q++ = '\0';
-         if (!q)
-            {
-            config_failed = 1;
-            fprintf(stderr,
-                    "error: option %s does not have a value.\n", p);
-            continue;
-            }
-         /* key to lower case */
-         tmp = p;
-         while (p != NULL && *p != '\0')
-            {
-            *p = tolower(*p);
-            p++;
-            }
-         p = tmp;
-
-         switch (mode)
-            {
-         case MODE_UNKNOWN:
-            break;
-         case MODE_DATABASE:
-            /* printf( "database %s : %s = %s\n" , dbname , p , q
-               ); */
-            add_database_option(c, dbname, p, q);
-            break;
-         case MODE_GLOBAL:
-            /* printf( "global: %s = %s\n" , p , q ); */
-            if (strstr(p, "password") != NULL)
-               {
-               fprintf(stderr,
-                       "error: passwords must not be included in the main 
configuration file.\n");
-               fprintf(stderr,
-                       "       the shadow password file should be used, 
instead.\n");
-               config_failed = 1;
-               }
-            add_section_option(c, p, q);
-            break;
-            }
-         }
-      }
-   fclose(fp);
-   if (dbname)
-      g_free(dbname);
-   }
-
-#if 0
-(note: #if 0 used to prevent compiling of this section, and avoids problems 
with
-   nested comments)
-
-to add default options, copy the line below, replacing key and value with
-   appropriate strings add_section_option(c, "key", "value");
-
-eg:
-to set a default debug level of 5, use:
-add_section_option(c, "debuglevel", "5");
-
-note that command line options override the configuration file settings.
-#endif
-static void enter_default_properties(configuration_data * c)
-   {
-      /* none defined yet */
-   }
-
-/* HACK - temporary function */
-const char *get_first_active_database( configuration tree )
-{
-   configuration_data *c = (configuration_data *)tree;
-   configuration_data *i = NULL;
-   GList *l = c->database;
-   char *dbname;
+/* ------------------------------------------------------------------------- *\
+ * HACK - temporary function
+\* ------------------------------------------------------------------------- */
+const char *
+get_first_active_database (configuration tree)
+{
+  configuration_data *c = (configuration_data *) tree;
+  configuration_data *i = NULL;
+  GList *l = c->database;
+  char *dbname;
 
-   while (l)
-      {
+  while (l)
+    {
       i = (configuration_data *) l->data;
       dbname = i->name;
-      if( get_database_option_bool(tree,dbname,"active",FALSE) )
-         return ( dbname );
-      l = g_list_next(l);
-      }
-   return (NULL);
+      if (get_database_option_bool (tree, dbname, "active", FALSE))
+       return (dbname);
+      l = g_list_next (l);
+    }
+  return (NULL);
 }
Index: gnue/geas/src/config/configuration.h
diff -u gnue/geas/src/config/configuration.h:1.4 
gnue/geas/src/config/configuration.h:1.5
--- gnue/geas/src/config/configuration.h:1.4    Fri May 25 13:24:08 2001
+++ gnue/geas/src/config/configuration.h        Tue May 29 15:28:46 2001
@@ -1,23 +1,25 @@
-
 /*
-   geas - GNU Enterprise Application Server
- 
+   GEAS configuration management library
+
    Copyright (C) 2001 Free Software Foundation
- 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
- 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
- 
+
+   This file is part of the GNU Enterprise Application Server (GEAS)
+
+   GEAS is free software; you can redistribute it and/or modify it under
+   the terms of the GNU General Public License as published by the Free
+   Software Foundation; either version 2, or (at your option) any later
+   version.
+
+   GEAS is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+   for more details.
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
- 
+   along with GEAS; if not, write to the Free Software Foundation, Inc.,
+   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   $Id: configuration.h,v 1.5 2001/05/29 22:28:46 reinhard Exp $
 */
 
 /*
@@ -27,8 +29,8 @@
  * 
  */
 
-#ifndef LIBCONFIG_H
-#define LIBCONFIG_H
+#ifndef CONFIGURATION_H
+#define CONFIGURATION_H
 
 #include <glib.h>
 
@@ -40,33 +42,42 @@
 #define NO_CONFIGURATION NULL
 
 /* load/release configuration files */
-configuration
-load_configuration_file(const char *filename, const char *shadowpwfile);
-void free_configuration(configuration config);
+configuration load_configuration_file (const char *filename,
+                                       const char *shadowpwfile);
+void          free_configuration (configuration config);
 
 /* display current config */
-void show_configuration(configuration config , gboolean show_passwords );
+void          show_configuration (configuration config,
+                                  gboolean show_passwords);
 
 /* track databases in the file */
-int count_configured_databases(configuration config);
-const char *get_database_name(configuration config, int index);
+int           count_configured_databases (configuration config);
+const char   *get_database_name (configuration config, int index);
 
 /* global configuration */
-const char *get_global_option     (configuration config, const char *key);
-const char *get_global_option_str (configuration config, const char *key , 
const char *defaultval );
-int         get_global_option_int (configuration config, const char *key , int 
defaultval);
-gboolean    get_global_option_bool(configuration config, const char *key , 
gboolean defaultval);
+const char   *get_global_option (configuration config, const char *key);
+const char   *get_global_option_str (configuration config, const char *key,
+                                     const char *defaultval);
+int           get_global_option_int (configuration config, const char *key,
+                                     int defaultval);
+gboolean      get_global_option_bool (configuration config, const char *key,
+                                      gboolean defaultval);
 
 /* find configuration item for a particular database */
-const char *get_database_option     (configuration tree,const char 
*databasename, const char *key);
-gboolean    get_database_option_bool(configuration tree,const char 
*databasename, const char *key,
-                                    gboolean defaultval);
-int         get_database_option_int (configuration tree,const char 
*databasename, const char *key,
-                                    int defaultval);
-const char *get_database_option_str (configuration tree,const char 
*databasename, const char *key,
-                                    const char *defaultvalue);
+const char   *get_database_option (configuration tree,
+                                   const char *databasename, const char *key);
+gboolean      get_database_option_bool (configuration tree,
+                                        const char *databasename,
+                                        const char *key, gboolean defaultval);
+int           get_database_option_int (configuration tree,
+                                       const char *databasename,
+                                       const char *key, int defaultval);
+const char   *get_database_option_str (configuration tree,
+                                       const char *databasename,
+                                       const char *key,
+                                       const char *defaultvalue);
 
-/* HACK - temporary function, until m,ultiple databases are supported */
-const char *get_first_active_database( configuration tree );
+/* HACK - temporary function, until multiple databases are supported */
+const char   *get_first_active_database (configuration tree);
 
-#endif
+#endif /* CONFIGURATION_H */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]