#include "includes.h"
#include "dlinklist.h"
#include "param/generic.h"
+#include "system/filesys.h"
struct param_section *param_get_section(struct param_context *ctx, const char *name)
{
struct param *p;
for (p = section->parameters; p; p = p->next) {
- if (strcasecmp(p->name, name) == 0)
+ if (strcasecmp_m(p->name, name) == 0)
return p;
}
return p->value;
}
-void 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 *section, const char *param, const char *value)
{
struct param *p = param_get_add(ctx, section, param);
+ if (p == NULL)
+ return -1;
+
p->value = talloc_strdup(p, value);
+
+ return 0;
}
const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
if (p == NULL)
return NULL;
+
+ if (separator == NULL)
+ separator = LIST_SEP;
if (p->list_value == NULL) {
p->list_value = str_list_make(ctx, p->value, separator);
DLIST_ADD(ctx->sections, section);
}
+ /* Make sure this section is on top of the list for param_pfunc */
DLIST_PROMOTE(ctx->sections, section);
return True;
}
-
-
static BOOL param_pfunc (const char *name, const char *value, void *_ctx)
{
struct param_context *ctx = _ctx;
return True;
}
-struct param_context *param_read(TALLOC_CTX *mem_ctx, const char *fn)
+struct param_context *param_init(TALLOC_CTX *mem_ctx)
{
- struct param_context *ctx = talloc_zero(mem_ctx, struct param_context);
+ return talloc_zero(mem_ctx, struct param_context);
+}
+
+int param_read(struct param_context *ctx, const char *fn)
+{
ctx->sections = talloc_zero(ctx, struct param_section);
ctx->sections->name = talloc_strdup(ctx->sections, "global");
if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
- talloc_free(ctx);
- return NULL;
+ return -1;
}
- return ctx;
+ return 0;
}
-int param_write(FILE *file, struct param_context *ctx)
+int param_write(struct param_context *ctx, const char *fn)
{
+ XFILE *file;
struct param_section *section;
- if (file == NULL)
+ if (fn == NULL || ctx == NULL)
return -1;
- if (ctx == NULL)
+ file = x_fopen(fn, O_WRONLY|O_CREAT, 0755);
+
+ if (file == NULL)
return -1;
for (section = ctx->sections; section; section = section->next) {
struct param *param;
- fprintf(file, "[%s]\n", section->name);
+ x_fprintf(file, "[%s]\n", section->name);
for (param = section->parameters; param; param = param->next) {
- fprintf(file, "\t%s = %s\n", param->name, param->value);
+ x_fprintf(file, "\t%s = %s\n", param->name, param->value);
}
- fprintf(file, "\n");
+ x_fprintf(file, "\n");
}
+ x_fclose(file);
+
return 0;
}
--- /dev/null
+/*
+ Unix SMB/CIFS implementation.
+
+ provide hooks into smbd C calls from ejs scripts
+
+ Copyright (C) Jelmer Vernooij 2005
+
+ 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 of the License, 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.
+
+ 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "scripting/ejs/smbcalls.h"
+#include "lib/appweb/ejs/ejs.h"
+#include "param/generic.h"
+#include "dynconfig.h"
+
+/*
+ get parameter
+
+ value = param.get("name");
+ value = param.get("section", "name");
+*/
+static int ejs_param_get(MprVarHandle eid, int argc, char **argv)
+{
+ struct param_context *ctx;
+ const char *ret;
+ if (argc != 1 && argc != 2) {
+ ejsSetErrorMsg(eid, "param.get invalid argument count");
+ return -1;
+ }
+
+ ctx = mprGetThisPtr(eid, "param");
+ mprAssert(ctx);
+
+ if (argc == 2) {
+ ret = param_get_string(ctx, argv[0], argv[1]);
+ } else {
+ ret = param_get_string(ctx, NULL, argv[0]);
+ }
+
+ if (ret) {
+ mpr_Return(eid, mprString(ret));
+ } else {
+ mpr_Return(eid, mprCreateUndefinedVar());
+ }
+ return 0;
+}
+
+/*
+ get list parameter
+
+ ok = param.get_list("name");
+ ok = param.get_list("section", "name");
+*/
+static int ejs_param_get_list(MprVarHandle eid, int argc, char **argv)
+{
+ struct param_context *ctx;
+ const char **ret;
+
+ if (argc != 1 && argc != 2) {
+ ejsSetErrorMsg(eid, "param.get invalid argument count");
+ return -1;
+ }
+
+ ctx = mprGetThisPtr(eid, "param");
+ mprAssert(ctx);
+
+ if (argc == 2) {
+ ret = param_get_string_list(ctx, argv[0], argv[1], NULL);
+ } else {
+ ret = param_get_string_list(ctx, NULL, argv[0], NULL);
+ }
+
+ if (ret) {
+ mpr_Return(eid, mprList("array", ret));
+ } else {
+ mpr_Return(eid, mprCreateUndefinedVar());
+ }
+ return 0;
+}
+
+/*
+ set parameter
+
+ ok = param.set("name", "value");
+ ok = param.set("section", "name", "value");
+*/
+static int ejs_param_set(MprVarHandle eid, int argc, char **argv)
+{
+ struct param_context *ctx;
+ bool ret;
+ if (argc != 2 && argc != 3) {
+ ejsSetErrorMsg(eid, "param.set invalid argument count");
+ return -1;
+ }
+
+ ctx = mprGetThisPtr(eid, "param");
+ mprAssert(ctx);
+
+ if (argc == 3) {
+ ret = param_set_string(ctx, argv[0], argv[1], argv[2]);
+ } else {
+ ret = param_set_string(ctx, NULL, argv[0], argv[2]);
+ }
+
+ mpr_Return(eid, mprCreateBoolVar(ret));
+ return 0;
+}
+
+/*
+ load file
+
+ ok = param.load(file);
+*/
+static int ejs_param_load(MprVarHandle eid, int argc, char **argv)
+{
+ struct param_context *ctx;
+ bool ret;
+
+ if (argc != 1) {
+ ejsSetErrorMsg(eid, "param.load invalid argument count");
+ return -1;
+ }
+
+ ctx = mprGetThisPtr(eid, "param");
+ mprAssert(ctx);
+
+ ret = param_read(ctx, argv[0]);
+
+ mpr_Return(eid, mprCreateBoolVar(ret));
+ return 0;
+}
+
+/*
+ save file
+
+ ok = param.save(file);
+*/
+static int ejs_param_save(MprVarHandle eid, int argc, char **argv)
+{
+ struct param_context *ctx;
+ bool ret;
+
+ if (argc != 1) {
+ ejsSetErrorMsg(eid, "param.save invalid argument count");
+ return -1;
+ }
+
+ ctx = mprGetThisPtr(eid, "param");
+ mprAssert(ctx);
+
+ ret = param_write(ctx, argv[0]);
+
+ mpr_Return(eid, mprCreateBoolVar(ret));
+ return 0;
+}
+
+static void param_add_members(struct MprVar *obj)
+{
+ mprSetStringCFunction(obj, "get", ejs_param_get);
+ mprSetStringCFunction(obj, "get_list", ejs_param_get_list);
+ mprSetStringCFunction(obj, "set", ejs_param_set);
+ mprSetStringCFunction(obj, "load", ejs_param_load);
+ mprSetStringCFunction(obj, "save", ejs_param_save);
+}
+
+/*
+ initialise param ejs subsystem
+*/
+static int ejs_param_init(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+ struct MprVar *obj = mprInitObject(eid, "param", argc, argv);
+
+ mprSetPtrChild(obj, "param", param_init(mprMemCtx()));
+
+ param_add_members(obj);
+
+ return 0;
+}
+
+struct MprVar mprParam(struct param_context *ctx)
+{
+ struct MprVar mpv = mprObject("param");
+ mprSetPtrChild(&mpv, "param", ctx);
+ param_add_members(&mpv);
+ return mpv;
+}
+
+/*
+ setup C functions that be called from ejs
+*/
+void smb_setup_ejs_param(void)
+{
+ ejsDefineCFunction(-1, "param_init", ejs_param_init, NULL, MPR_VAR_SCRIPT_HANDLE);
+}