Added a "Defaults" structure with both globals and locals in it.
[rsync/rsync.git] / loadparm.c
index e638c64..2a9d8b4 100644 (file)
  *
  * To add a parameter:
  *
- * 1) add it to the global or section structure definition
+ * 1) add it to the global_vars or local_vars structure definition
  * 2) add it to the parm_table
  * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
- * 4) If it's a global then initialise it in init_globals. If a local module
- *    (ie. section) parameter then initialise it in the Locals structure
+ * 4) initialise it in the Defaults static stucture
  *
  * Notes:
  *   The configuration file is processed sequentially for speed. For this
@@ -86,7 +85,7 @@ struct parm_struct {
 #endif
 
 /* some helpful bits */
-#define iSECTION(i) ((section*)section_list.items)[i]
+#define iSECTION(i) ((local_vars*)section_list.items)[i]
 #define LP_SNUM_OK(i) ((i) >= 0 && (i) < (int)section_list.count)
 #define SECTION_PTR(s, p) (((char*)(s)) + (ptrdiff_t)(((char*)(p))-(char*)&Locals))
 
@@ -100,9 +99,9 @@ typedef struct {
        char *socket_options;
 
        int rsync_port;
-} global;
+} global_vars;
 
-static global Globals;
+static global_vars Globals;
 
 /*
  * This structure describes a single section.  Their order must match the
@@ -153,18 +152,32 @@ typedef struct {
        BOOL transfer_logging;
        BOOL use_chroot;
        BOOL write_only;
-} section;
+} local_vars;
+
+static local_vars Locals;
 
 typedef struct {
-       global g;
-       section s;
-} global_and_section;
+       global_vars g;
+       local_vars l;
+} all_vars;
 
-/* This is a default section used to prime a sections structure.  In order
+/* This is used to reset all values before a config read.  In order
  * to make these easy to keep sorted in the same way as the variables
  * above, use the variable name in the leading comment, including a
  * trailing ';' (to avoid a sorting problem with trailing digits). */
-static section Locals = {
+static const all_vars Defaults = {
+ /* ==== global_vars ==== */
+ {
+ /* bind_address; */           NULL,
+ /* motd_file; */              NULL,
+ /* pid_file; */               NULL,
+ /* socket_options; */         NULL,
+
+ /* rsync_port; */             0,
+ },
+
+ /* ==== local_vars ==== */
+ {
  /* auth_users; */             NULL,
  /* charset; */                NULL,
  /* comment; */                NULL,
@@ -208,6 +221,7 @@ static section Locals = {
  /* transfer_logging; */       False,
  /* use_chroot; */             True,
  /* write_only; */             False,
+ }
 };
 
 /* local variables */
@@ -339,16 +353,11 @@ static struct parm_struct parm_table[] =
  {NULL,                P_BOOL,   P_NONE,  NULL,                        NULL,0}
 };
 
-/* Initialise the global parameter structure. */
-static void init_globals(void)
+/* Initialise the Default all_vars structure. */
+static void reset_all_vars(void)
 {
-       memset(&Globals, 0, sizeof Globals);
-}
-
-/* Initialise the Locals parameter structure. */
-static void init_locals(void)
-{
-       /* Nothing needed yet... */
+       memcpy(&Globals, &Defaults.g, sizeof Globals);
+       memcpy(&Locals, &Defaults.l, sizeof Locals);
 }
 
 /* In this section all the functions that are used to access the
@@ -440,11 +449,11 @@ static void string_set(char **s, const char *v)
                return;
        }
        if (!(*s = strdup(v)))
-               exit_cleanup(RERR_MALLOC);
+               out_of_memory("string_set");
 }
 
-/* Copy a section structure to another. */
-static void copy_section(section *psectionDest, section *psectionSource)
+/* Copy the local_vars, duplicating any strings in the source. */
+static void copy_section(local_vars *psectionDest, local_vars *psectionSource)
 {
        int i;
 
@@ -483,9 +492,9 @@ static void copy_section(section *psectionDest, section *psectionSource)
 }
 
 /* Initialise a section to the defaults. */
-static void init_section(section *psection)
+static void init_section(local_vars *psection)
 {
-       memset((char *)psection, 0, sizeof (section));
+       memset(psection, 0, sizeof Locals);
        copy_section(psection, &Locals);
 }
 
@@ -518,16 +527,13 @@ static int strwicmp(char *psz1, char *psz2)
 }
 
 /* Find a section by name. Otherwise works like get_section. */
-static int getsectionbyname(char *name, section *psectionDest)
+static int getsectionbyname(char *name)
 {
        int i;
 
        for (i = section_list.count - 1; i >= 0; i--) {
-               if (strwicmp(iSECTION(i).name, name) == 0) {
-                       if (psectionDest != NULL)
-                               copy_section(psectionDest, &iSECTION(i));
+               if (strwicmp(iSECTION(i).name, name) == 0)
                        break;
-               }
        }
 
        return i;
@@ -537,17 +543,17 @@ static int getsectionbyname(char *name, section *psectionDest)
 static int add_a_section(char *name)
 {
        int i;
-       section *s;
+       local_vars *s;
 
        /* it might already exist */
        if (name) {
-               i = getsectionbyname(name, NULL);
+               i = getsectionbyname(name);
                if (i >= 0)
                        return i;
        }
 
        i = section_list.count;
-       s = EXPAND_ITEM_LIST(&section_list, section, 2);
+       s = EXPAND_ITEM_LIST(&section_list, local_vars, 2);
 
        init_section(s);
        if (name)
@@ -683,16 +689,16 @@ static BOOL do_section(char *sectionname)
        if (*sectionname == ']') { /* A special push/pop/reset directive from params.c */
                bInGlobalSection = 1;
                if (strcmp(sectionname+1, "push") == 0) {
-                       global_and_section *gs = EXPAND_ITEM_LIST(&section_stack, global_and_section, 2);
-                       memcpy(&gs->g, &Globals, sizeof Globals);
-                       memcpy(&gs->s, &Locals, sizeof Locals);
+                       all_vars *vp = EXPAND_ITEM_LIST(&section_stack, all_vars, 2);
+                       memcpy(&vp->g, &Globals, sizeof Globals);
+                       memcpy(&vp->l, &Locals, sizeof Locals);
                } else if (strcmp(sectionname+1, "pop") == 0
                 || strcmp(sectionname+1, "reset") == 0) {
-                       global_and_section *gs = ((global_and_section *)section_stack.items) + section_stack.count - 1;
+                       all_vars *vp = ((all_vars*)section_stack.items) + section_stack.count - 1;
                        if (!section_stack.count)
                                return False;
-                       memcpy(&Globals, &gs->g, sizeof Globals);
-                       memcpy(&Locals, &gs->s, sizeof Locals);
+                       memcpy(&Globals, &vp->g, sizeof Globals);
+                       memcpy(&Locals, &vp->l, sizeof Locals);
                        if (sectionname[1] == 'p')
                                section_stack.count--;
                } else
@@ -702,11 +708,10 @@ static BOOL do_section(char *sectionname)
 
        isglobal = strwicmp(sectionname, GLOBAL_NAME) == 0;
 
-       /* if we were in a global section then do the local inits */
+       /* At the end of the global section, add any --dparam items. */
        if (bInGlobalSection && !isglobal) {
                if (!section_list.count)
                        set_dparams(0);
-               init_locals();
        }
 
        /* if we've just struck a global section, note the fact. */
@@ -745,7 +750,7 @@ int lp_load(char *pszFname, int globals_only)
 {
        bInGlobalSection = True;
 
-       init_globals();
+       reset_all_vars();
 
        /* We get sections first, so have to start 'behind' to make up. */
        iSectionIndex = -1;