Fix includes.
[kai/samba.git] / source4 / param / generic.c
index d65502c02d22321d5ded892e604f683232c19b80..ba5464a0f0aabf8166887073e17366630ec64126 100644 (file)
@@ -17,8 +17,9 @@
  */
 
 #include "includes.h"
-#include "lib/util/dlinklist.h"
+#include "../lib/util/dlinklist.h"
 #include "param/param.h"
+#include "param/loadparm.h"
 #include "system/filesys.h"
 
 struct param_section *param_get_section(struct param_context *ctx, const char *name)
@@ -49,7 +50,7 @@ struct param_opt *param_section_get(struct param_section *section,
        return NULL;
 }
 
-struct param_opt *param_get (struct param_context *ctx, const char *section_name, const char *name)
+struct param_opt *param_get (struct param_context *ctx, const char *name, const char *section_name)
 {
        struct param_section *section = param_get_section(ctx, section_name);
        if (section == NULL)
@@ -58,21 +59,31 @@ struct param_opt *param_get (struct param_context *ctx, const char *section_name
        return param_section_get(section, name);
 }
 
+struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
+{
+       struct param_section *section;
+       section = talloc_zero(ctx, struct param_section);
+       if (section == NULL)
+               return NULL;
+
+       section->name = talloc_strdup(section, section_name);
+       DLIST_ADD_END(ctx->sections, section, struct param_section *);
+       return section;
+}
+
 /* Look up parameter. If it is not found, add it */
-static struct param_opt *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
+struct param_opt *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
 {
        struct param_section *section;
        struct param_opt *p;
 
+       SMB_ASSERT(section_name != NULL);
+       SMB_ASSERT(name != NULL);
+
        section = param_get_section(ctx, section_name);
 
        if (section == NULL) {
-               section = talloc_zero(ctx, struct param_section);
-               if (section == NULL)
-                       return NULL;
-
-               section->name = talloc_strdup(section, section_name);
-               DLIST_ADD(ctx->sections, section);
+               section = param_add_section(ctx, section_name);
        }
 
        p = param_section_get(section, name);
@@ -82,15 +93,15 @@ static struct param_opt *param_get_add(struct param_context *ctx, const char *se
                        return NULL;
 
                p->key = talloc_strdup(p, name);
-               DLIST_ADD(section->parameters, p);
+               DLIST_ADD_END(section->parameters, p, struct param_opt *);
        }
        
        return p;
 }
 
-const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
+const char *param_get_string(struct param_context *ctx, const char *param, const char *section)
 {
-       struct param_opt *p = param_get(ctx, section, param);
+       struct param_opt *p = param_get(ctx, param, section);
 
        if (p == NULL)
                return NULL;
@@ -98,9 +109,9 @@ const char *param_get_string(struct param_context *ctx, const char *section, con
        return p->value;
 }
 
-int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
+int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section)
 {
-       struct param_opt *p = param_get_add(ctx, section, param);
+       struct param_opt *p = param_get_add(ctx, param, section);
 
        if (p == NULL)
                return -1;
@@ -110,32 +121,28 @@ int param_set_string(struct param_context *ctx, const char *section, const char
        return 0;
 }
 
-const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
-                                const char *separator)
+const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section)
 {
-       struct param_opt *p = param_get(ctx, section, param);
+       struct param_opt *p = param_get(ctx, param, section);
        
        if (p == NULL)
                return NULL;
 
-       if (separator == NULL)
-               separator = LIST_SEP;
-       
-       return str_list_make(ctx, p->value, separator);
+       return (const char **)str_list_make(ctx, p->value, separator);
 }
 
-int param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
+int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
 {
-       struct param_opt *p = param_get_add(ctx, section, param);       
+       struct param_opt *p = param_get_add(ctx, param, section);       
 
        p->value = str_list_join(p, list, ' ');
 
        return 0;
 }
 
-int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
+int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section)
 {
-       const char *value = param_get_string(ctx, section, param);
+       const char *value = param_get_string(ctx, param, section);
        
        if (value)
                return strtol(value, NULL, 0); 
@@ -143,7 +150,7 @@ int param_get_int(struct param_context *ctx, const char *section, const char *pa
        return default_v;
 }
 
-void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
+void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
 {
        struct param_opt *p = param_get_add(ctx, section, param);
 
@@ -153,9 +160,9 @@ void param_set_int(struct param_context *ctx, const char *section, const char *p
        p->value = talloc_asprintf(p, "%d", value);
 }
 
-unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
+unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
 {
-       const char *value = param_get_string(ctx, section, param);
+       const char *value = param_get_string(ctx, param, section);
        
        if (value)
                return strtoul(value, NULL, 0);
@@ -163,9 +170,9 @@ unsigned long param_get_ulong(struct param_context *ctx, const char *section, co
        return default_v;
 }
 
-void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
+void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
 {
-       struct param_opt *p = param_get_add(ctx, section, name);
+       struct param_opt *p = param_get_add(ctx, name, section);
 
        if (!p)
                return;
@@ -185,7 +192,7 @@ static bool param_sfunc (const char *name, void *_ctx)
 
                section->name = talloc_strdup(section, name);
 
-               DLIST_ADD(ctx->sections, section);
+               DLIST_ADD_END(ctx->sections, section, struct param_section *);
        }
 
        /* Make sure this section is on top of the list for param_pfunc */
@@ -235,6 +242,29 @@ int param_read(struct param_context *ctx, const char *fn)
        return 0;
 }
 
+int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
+{
+       struct param_section *section;
+
+       for (section = ctx->sections; section; section = section->next) {
+               struct param_opt *param;
+               bool isglobal = strcmp(section->name, "global") == 0;
+               for (param = section->parameters; param; param = param->next) {
+                       if (isglobal)
+                               lp_do_global_parameter(lp_ctx, param->key,
+                                                      param->value);
+                       else {
+                               struct loadparm_service *service = 
+                                                       lp_service(lp_ctx, section->name);
+                               if (service == NULL)
+                                       service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
+                               lp_do_service_parameter(lp_ctx, service, param->key, param->value);
+                       }
+               }
+       }
+       return 0;
+}
+
 int param_write(struct param_context *ctx, const char *fn)
 {
        int file;