summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin@localhost.localdomain>2008-05-13 18:58:20 -0400
committerNalin Dahyabhai <nalin@localhost.localdomain>2008-05-13 18:58:20 -0400
commitdc78fc1572e25007acbf40a6ec7d0d0b0e3182d0 (patch)
tree8de3380e31db7536839b4e2ba7f9000f23b8a26e /src
parentd01a5adac69f1fde9d64a88938a22147d0cfc25e (diff)
- add a test "echo" function for testing
Diffstat (limited to 'src')
-rw-r--r--src/format.c74
1 files changed, 72 insertions, 2 deletions
diff --git a/src/format.c b/src/format.c
index b6cc37d..74f8d41 100644
--- a/src/format.c
+++ b/src/format.c
@@ -21,6 +21,44 @@ format_free_ndn_list(char **ndn_list)
}
}
+/* Echo the parameter text. */
+static int
+format_echo(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
+ const char *args, char *outbuf, int outbuf_len,
+ char ***visited_ndns)
+{
+ int i;
+ i = strlen(args);
+ if (i <= outbuf_len) {
+ memcpy(outbuf, args, i);
+ }
+ return i;
+}
+
+/* Choose a formatting function by name. */
+static void *
+format_lookup_fn(const char *fnname)
+{
+ unsigned int i;
+ struct {
+ const char *name;
+ int (*fct_ptr)(struct plugin_state *state,
+ Slapi_PBlock *pb, Slapi_Entry *e,
+ const char *args,
+ char *outbuf, int outbuf_len,
+ char ***visited_ndns);
+ } fns[] = {
+ {"echo", format_echo},
+ };
+ for (i = 0; i < sizeof(fns) / sizeof(fns[0]); i++) {
+ if ((fns[i].name != NULL) &&
+ (strcmp(fns[i].name, fnname) == 0)) {
+ return fns[i].fct_ptr;
+ }
+ }
+ return NULL;
+}
+
/* Retrieve a single value for an attribute. If there are no values, or more
* than one, fail. */
static char *
@@ -79,6 +117,9 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
char exp[outbuf_len * 2];
const char *default_value, *alternate_value, *paramstart, *paramend;
size_t spn;
+ int (*formatfn)(struct plugin_state *state,
+ Slapi_PBlock *pb, Slapi_Entry *e, const char *args,
+ char *outbuf, int outbuf_len, char ***visited_ndns);
spd_id = state->plugin_desc->spd_id;
slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
@@ -160,7 +201,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
default:
/* Assume it's a "function" call. Pick out the
* name of the function. */
- paramstart = strchr(fmt + i, '{');
+ paramstart = strchr(fmt + i, '(');
if (paramstart == NULL) {
/* No start? Bad format. */
slapi_log_error(SLAPI_LOG_PLUGIN,
@@ -170,7 +211,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"invocation\n");
return -1;
}
- paramend = strchr(paramstart + 1, '}');
+ paramend = strchr(paramstart + 1, ')');
if (paramend == NULL) {
/* No matching end? Bad format. */
slapi_log_error(SLAPI_LOG_PLUGIN,
@@ -206,12 +247,41 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
memcpy(tmp, paramstart + 1,
paramend - (paramstart + 1));
tmp[paramend - paramstart] = '\0';
+ /* Find the "function". */
+ formatfn = format_lookup_fn(fnname);
+ if (formatfn == NULL) {
+ slapi_log_error(SLAPI_LOG_PLUGIN,
+ spd_id,
+ "expansion "
+ "failed: no function "
+ "named \"%s\" is "
+ "defined\n", fnname);
+ free(fnname);
+ free(tmp);
+ return -1;
+ }
slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
"calling \"%s\"(\"%s\")\n",
fnname, tmp);
+ exp_len = (*formatfn)(state, pb, e, tmp,
+ exp + j, sizeof(exp) - j,
+ visited_ndns);
free(fnname);
free(tmp);
+ if ((exp_len < 0) ||
+ (exp_len + j >= (int) sizeof(exp))) {
+ /* We'd be out of space,
+ * FAIL. */
+ slapi_log_error(SLAPI_LOG_PLUGIN,
+ spd_id,
+ "expansion "
+ "failed: result"
+ " would be too "
+ "big\n");
+ return -1;
+ }
i = (paramend - fmt) + 1;
+ j += exp_len;
continue;
break;
}