diff options
author | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2008-07-14 17:52:34 -0400 |
---|---|---|
committer | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2008-07-14 17:52:34 -0400 |
commit | e46779faa14c4e0ea0fcf8f7da0638d3ee877a39 (patch) | |
tree | 49949b3eca0b198592f84ce4da3de24033392866 /src/format.c | |
parent | efa8e2f43b4c99848d962675b2e3a5b60f613e92 (diff) | |
download | slapi-nis-e46779faa14c4e0ea0fcf8f7da0638d3ee877a39.tar.gz slapi-nis-e46779faa14c4e0ea0fcf8f7da0638d3ee877a39.tar.xz slapi-nis-e46779faa14c4e0ea0fcf8f7da0638d3ee877a39.zip |
- make %first() take a pattern and not a literal
Diffstat (limited to 'src/format.c')
-rw-r--r-- | src/format.c | 72 |
1 files changed, 42 insertions, 30 deletions
diff --git a/src/format.c b/src/format.c index 3df4ab9..5cd8fcb 100644 --- a/src/format.c +++ b/src/format.c @@ -473,7 +473,8 @@ format_parse_args(struct plugin_state *state, const char *args, return 0; } -/* Choose the first value of the attribute in the entry. */ +/* Choose the first value of the set of results for the first argument, and if + * we get no results, return the second argument. */ static int format_first(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, const char *group, const char *set, @@ -483,43 +484,54 @@ format_first(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, char ***ref_attrs, struct format_inref_attr ***inref_attrs) { int ret, i, argc; - Slapi_ValueSet *values; - Slapi_Value *value; - const struct berval *val; - char **argv, *actual_attr; - int disposition, buffer_flags; + char **argv, *value, **values; + const char *value_format, *default_value; + unsigned int length, *lengths; ret = format_parse_args(state, args, &argc, &argv); - if (argc != 1) { + if (argc < 1) { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, "first: error parsing arguments\n"); return -EINVAL; } + if (argc < 2) { + value_format = argv[0]; + default_value = NULL; + } else { + value_format = argv[0]; + default_value = argv[1]; + } ret = -ENOENT; - /* Get the list of values for this attribute. */ - if (slapi_vattr_values_get(e, argv[0], &values, &disposition, - &actual_attr, 0, - &buffer_flags) == 0) { - i = slapi_valueset_first_value(values, &value); - if (i != -1) { - /* Get the length of the value. */ - val = slapi_value_get_berval(value); - /* Check if we have space for the value. */ - if (ret + val->bv_len > (unsigned int) outbuf_len) { - slapi_log_error(SLAPI_LOG_PLUGIN, - state->plugin_desc->spd_id, - "first: out of space\n"); - ret = -ENOBUFS; - } else { - /* Copy in the value. */ - memcpy(outbuf + ret, val->bv_val, val->bv_len); - ret += val->bv_len; - } - } else { - /* No such attribute. */ + values = format_get_data_set(state, e, group, set, + value_format, disallowed, + ref_attrs, inref_attrs, + &lengths); + if (values == NULL) { + if (default_value == NULL) { + slapi_log_error(SLAPI_LOG_PLUGIN, + state->plugin_desc->spd_id, + "first: no values for ->%s<-, " + "and no default value provided\n", + value_format); + format_free_parsed_args(argv); ret = -ENOENT; + } else { + i = format_expand(state, pb, e, + group, set, + default_value, NULL, + outbuf, outbuf_len, + outbuf_choices, + ref_attrs, inref_attrs); + format_free_parsed_args(argv); + ret = i; } - /* Free the value set for this attribute. */ - slapi_vattr_values_free(&values, &actual_attr, buffer_flags); + } else { + if ((int) lengths[0] > outbuf_len) { + ret = -ENOSPC; + } else { + memcpy(outbuf, values[0], lengths[0]); + ret = lengths[0]; + } + format_free_data_set(values, lengths); } format_free_parsed_args(argv); return ret; |