summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-06-02 16:17:41 -0400
committerNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-06-02 16:17:41 -0400
commit33385e9b68d34fc5d7c97ad7d61d05c245b32209 (patch)
tree2092a92041c630adc6c1879f68e7449454a686fb /src
parent51fc6739cc4fa5f058c3062f6e0f4509b7b228a8 (diff)
downloadslapi-nis-33385e9b68d34fc5d7c97ad7d61d05c245b32209.tar.gz
slapi-nis-33385e9b68d34fc5d7c97ad7d61d05c245b32209.tar.xz
slapi-nis-33385e9b68d34fc5d7c97ad7d61d05c245b32209.zip
- use error codes to report better
- fixups for dynamic formatting buffer
Diffstat (limited to 'src')
-rw-r--r--src/format.c149
1 files changed, 82 insertions, 67 deletions
diff --git a/src/format.c b/src/format.c
index db71709..dc6feeb 100644
--- a/src/format.c
+++ b/src/format.c
@@ -24,6 +24,7 @@
#endif
#include <sys/types.h>
+#include <errno.h>
#include <fnmatch.h>
#include <regex.h>
#include <stdlib.h>
@@ -139,13 +140,13 @@ format_echo(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"echo: error parsing arguments\n");
- return -1;
+ return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"echo: requires at least one argument\n");
format_free_parsed_args(argv);
- return -1;
+ return -EINVAL;
}
for (i = 0, ret = 0; i < argc; i++) {
/* Get the length of this argument. */
@@ -155,7 +156,7 @@ format_echo(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
"echo: out of space\n");
- return -1;
+ return -ENOBUFS;
}
/* Append the text of the argument. */
memcpy(outbuf + ret, argv[i], len);
@@ -182,7 +183,7 @@ format_list(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"list: error parsing arguments\n");
- return -1;
+ return -EINVAL;
}
for (i = 1, ret = 0, count = 0; i < argc; i++) {
/* Get the list of values for this attribute. */
@@ -205,7 +206,7 @@ format_list(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
slapi_vattr_values_free(&values, &actual_attr,
buffer_flags);
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
/* If the value is empty, skip it. */
if (len == 0) {
@@ -224,7 +225,7 @@ format_list(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
&actual_attr,
buffer_flags);
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
/* Copy in the separator. */
memcpy(outbuf + ret, argv[0], slen);
@@ -262,13 +263,13 @@ format_deref(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref: error parsing arguments\n");
- return -1;
+ return -EINVAL;
}
if (argc < 3) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"deref: requires 3 arguments\n");
format_free_parsed_args(argv);
- return -1;
+ return -EINVAL;
}
/* Get the names of the reference attribute. */
if (slapi_vattr_values_get(e, argv[1], &ref_values,
@@ -338,7 +339,7 @@ format_deref(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
&actual_ref_attr,
ref_buffer_flags);
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
/* If the value is empty, skip it. */
if (len == 0) {
@@ -362,7 +363,7 @@ format_deref(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
&actual_ref_attr,
ref_buffer_flags);
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
/* Append the separator. */
memcpy(outbuf + ret, sep, slen);
@@ -397,13 +398,13 @@ format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"merge: error parsing arguments\n");
- return -1;
+ return -EINVAL;
}
if (argc < 1) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"merge: requires at least one argument\n");
format_free_parsed_args(argv);
- return -1;
+ return -EINVAL;
}
for (i = 1, ret = 0, count = 0; i < argc; i++) {
/* Expand this argument. */
@@ -418,8 +419,8 @@ format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
if (len < 0) {
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
- "merge: failed to expand ->%s<-\n",
- argv[i]);
+ "merge: failed to expand ->%s<-: %s\n",
+ argv[i], strerror(-len));
continue;
}
if (ret + len > outbuf_len) {
@@ -427,7 +428,7 @@ format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
state->plugin_desc->spd_id,
"merge: out of space\n");
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
slapi_log_error(SLAPI_LOG_PLUGIN,
state->plugin_desc->spd_id,
@@ -447,7 +448,7 @@ format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
state->plugin_desc->spd_id,
"merge: out of space\n");
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
/* Append the separator. */
memcpy(outbuf + ret, argv[0], slen);
@@ -485,13 +486,13 @@ format_match_generic(struct plugin_state *state,
if (ret != 0) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"%s: error parsing arguments\n", fnname);
- return -1;
+ return -EINVAL;
}
if (argc < 2) {
slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
"%s: requires at least 2 arguments\n", fnname);
format_free_parsed_args(argv);
- return -1;
+ return -EINVAL;
}
/* Walk the list of values for the attribute. */
matched = NULL;
@@ -552,7 +553,7 @@ format_match_generic(struct plugin_state *state,
buffer_flags);
}
format_free_parsed_args(argv);
- return -1;
+ return -ENOENT;
}
break;
case 1:
@@ -578,7 +579,7 @@ format_match_generic(struct plugin_state *state,
buffer_flags);
}
format_free_parsed_args(argv);
- return -1;
+ return -ENOENT;
}
break;
}
@@ -591,7 +592,7 @@ format_match_generic(struct plugin_state *state,
buffer_flags);
}
format_free_parsed_args(argv);
- return -1;
+ return -ENOBUFS;
}
memcpy(outbuf, matched, len);
if (values != NULL) {
@@ -744,7 +745,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
bool_t literal)
{
int i, j;
- int exp_len, level;
+ int exp_used, exp_size, level;
const char *fmtstart, *fmtend, *match, *attribute;
char *tmp, *fnname, *spd_id;
char *expr;
@@ -754,11 +755,12 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
Slapi_PBlock *pb, Slapi_Entry *e, const char *args,
char *outbuf, int outbuf_len, char ***visited_ndns);
- expr = malloc(outbuf_len * 2);
+ exp_size = outbuf_len * 2;
+ expr = malloc(exp_size);
if (expr == NULL) {
slapi_log_error(SLAPI_LOG_PLUGIN, spd_id,
"expansion failed: out of memory\n");
- return -1;
+ return -ENOMEM;
}
/* First, expand any subexpressions and call any "functions". */
@@ -789,7 +791,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"expansion failed: "
"no closing brace\n");
free(expr);
- return -1;
+ return -EINVAL;
} else {
/* Mark the first character after the
* subexpression. */
@@ -803,34 +805,35 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"failed: out "
"of memory\n");
free(expr);
- return -1;
+ return -ENOMEM;
}
memcpy(tmp, fmtstart + 2,
fmtend - fmtstart - 3);
tmp[fmtend - fmtstart - 3] = '\0';
/* Recursively expand the
* subexpression. */
- exp_len = format_expand(state, pb, e,
- tmp,
- expr + j,
- sizeof(expr) - j,
- visited_ndns,
- FALSE);
- if (exp_len < 0) {
+ exp_used = format_expand(state, pb, e,
+ tmp,
+ expr + j,
+ exp_size - j,
+ visited_ndns,
+ FALSE);
+ if (exp_used < 0) {
/* Miscellaneous failure, FAIL.
*/
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"error "
"expanding "
- "->%s<-\n",
- tmp);
+ "->%s<-: %s\n",
+ tmp,
+ strerror(-exp_used));
free(tmp);
free(expr);
- return -1;
+ return exp_used;
}
free(tmp);
- if (exp_len + j >= (int) sizeof(expr)) {
+ if (exp_used + j >= exp_size) {
/* Out of space, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
@@ -839,11 +842,11 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
" would be too "
"big\n");
free(expr);
- return -1;
+ return -ENOBUFS;
} else {
/* It fit, so keep going. */
i = (match + 1) - fmt;
- j += exp_len;
+ j += exp_used;
}
}
continue;
@@ -860,7 +863,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"bad function "
"invocation\n");
free(expr);
- return -1;
+ return -EINVAL;
}
if (*paramstart == '{') {
paramend = format_find_closer("{}",
@@ -877,7 +880,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"bad function "
"invocation\n");
free(expr);
- return -1;
+ return -EINVAL;
}
fnname = malloc(paramstart - (fmt + i));
if (fnname == NULL) {
@@ -887,7 +890,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"expansion failed: "
"out of memory\n");
free(expr);
- return -1;
+ return -ENOMEM;
}
memcpy(fnname, fmt + i + 1,
paramstart - (fmt + i + 1));
@@ -902,7 +905,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
"out of memory\n");
free(fnname);
free(expr);
- return -1;
+ return -ENOMEM;
}
memcpy(tmp, paramstart + 1,
paramend - (paramstart + 1));
@@ -919,28 +922,29 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
free(fnname);
free(tmp);
free(expr);
- return -1;
+ return -ENOSYS;
}
/* Call the "function". */
- exp_len = (*formatfn)(state, pb, e, tmp,
- expr + j,
- sizeof(expr) - j,
- visited_ndns);
+ exp_used = (*formatfn)(state, pb, e, tmp,
+ expr + j,
+ exp_size - j,
+ visited_ndns);
free(tmp);
- if (exp_len < 0) {
+ if (exp_used < 0) {
/* Error in function, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
"expansion "
"failed: function "
- "'%s' failed\n",
- fnname);
+ "'%s' failed: %s\n",
+ fnname,
+ strerror(-exp_used));
free(fnname);
free(expr);
- return -1;
+ return exp_used;
}
free(fnname);
- if (exp_len + j >= (int) sizeof(expr)) {
+ if (exp_used + j >= exp_size) {
/* We'd be out of space, FAIL. */
slapi_log_error(SLAPI_LOG_PLUGIN,
spd_id,
@@ -949,10 +953,10 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
" would be too "
"big\n");
free(expr);
- return -1;
+ return -ENOBUFS;
}
i = (paramend - fmt) + 1;
- j += exp_len;
+ j += exp_used;
continue;
break;
}
@@ -970,9 +974,12 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
i = strlen(expr);
if (i <= outbuf_len) {
memcpy(outbuf, expr, i);
+ free(expr);
+ return i;
+ } else {
+ free(expr);
+ return -ENOBUFS;
}
- free(expr);
- return i;
} else {
/* It's an expression, so evaluate it. Check if it uses a
* default/alternate value. */
@@ -987,16 +994,22 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
i = strlen(tmp);
if (i <= outbuf_len) {
memcpy(outbuf, tmp, i);
+ free(tmp);
+ /* Return the length of the expanded
+ * expression. */
+ free(expr);
+ return i;
+ } else {
+ free(tmp);
+ /* Return the length of the expanded
+ * expression. */
+ free(expr);
+ return -ENOBUFS;
}
- free(tmp);
- /* Return the length of the expanded
- * expression. */
- free(expr);
- return i;
} else {
/* No value found? FAIL. */
free(expr);
- return -1;
+ return -ENOENT;
}
} else {
/* Make a copy of the attribute name. */
@@ -1034,7 +1047,7 @@ format_expand(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
} else {
/* No value, and no default: FAIL. */
free(expr);
- return -1;
+ return -ENOENT;
}
} else {
/* There's no value defined (or it's
@@ -1070,8 +1083,7 @@ format_format(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
{
char *buf, *tmp, *ret;
const char *match, *fmtstart, *fmtend;
- unsigned int i, j, buflen;
- int exp_len;
+ int i, buflen, exp_len;
buflen = state->max_value_size;
buf = malloc(buflen);
@@ -1080,10 +1092,13 @@ format_format(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e,
}
i = format_expand(state, pb, e, fmt, buf, buflen, visited_ndns, TRUE);
- if (i < buflen) {
+ if ((i >= 0) && (i < buflen)) {
buf[i] = '\0';
ret = strdup(buf);
} else {
+ slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
+ "expansion of \"%s\" failed: %s\n",
+ fmt, strerror(-i));
ret = NULL;
}