From 82b4a9da03cb7b932629a91268945d130ee9fbf3 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Fri, 30 Mar 2012 16:51:36 -0400 Subject: - treat the padding values used by %link() into expressions instead of as literals --- src/format.c | 86 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 34 deletions(-) (limited to 'src/format.c') diff --git a/src/format.c b/src/format.c index aed71f1..ee85b6f 100644 --- a/src/format.c +++ b/src/format.c @@ -2588,15 +2588,15 @@ format_regsubi(struct plugin_state *state, } static int format_mregsubi(struct plugin_state *state, - Slapi_PBlock *pb, Slapi_Entry *e, - const char *group, const char *set, - const char *args, const char *disallowed, - char *outbuf, int outbuf_len, - struct format_choice **outbuf_choices, - char ***rel_attrs, char ***ref_attrs, - struct format_inref_attr ***inref_attrs, - struct format_ref_attr_list ***ref_attr_list, - struct format_ref_attr_list ***inref_attr_list) + Slapi_PBlock *pb, Slapi_Entry *e, + const char *group, const char *set, + const char *args, const char *disallowed, + char *outbuf, int outbuf_len, + struct format_choice **outbuf_choices, + char ***rel_attrs, char ***ref_attrs, + struct format_inref_attr ***inref_attrs, + struct format_ref_attr_list ***ref_attr_list, + struct format_ref_attr_list ***inref_attr_list) { return format_match_generic(state, pb, e, group, set, args, 3, -1, disallowed, @@ -2898,9 +2898,9 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, } /* Allocate space to store the information. */ - values = malloc(sizeof(char **) * ((argc + 1) / 3)); - lengths = malloc(sizeof(int *) * ((argc + 1) / 3)); - n_items = malloc(sizeof(int) * ((argc + 1) / 3)); + values = malloc(sizeof(char **) * (((argc + 1) / 3) * 2)); + lengths = malloc(sizeof(int *) * (((argc + 1) / 3) * 2)); + n_items = malloc(sizeof(int) * (((argc + 1) / 3) * 2)); if ((values == NULL) || (lengths == NULL) || (n_items == NULL)) { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, "link: out of memory\n"); @@ -2917,14 +2917,28 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, for (i = 0; i < argc; i += 3) { slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, "link: evaluating \"%s\"\n", argv[i]); - values[i / 3] = format_get_data_set(state, pb, e, group, set, - argv[i], disallowed, - rel_attrs, - ref_attrs, inref_attrs, - ref_attr_list, - inref_attr_list, - &lengths[i / 3]); - if (values[i / 3] != NULL) { + j = (i / 3) * 2; + values[j] = format_get_data_set(state, pb, e, group, set, + argv[i], disallowed, + rel_attrs, + ref_attrs, inref_attrs, + ref_attr_list, + inref_attr_list, + &lengths[j]); + if (values[j] != NULL) { + n_lists++; + } + j++; + slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id, + "link: evaluating \"%s\"\n", argv[i + 1]); + values[j] = format_get_data_set(state, pb, e, group, set, + argv[i + 1], disallowed, + rel_attrs, + ref_attrs, inref_attrs, + ref_attr_list, + inref_attr_list, + &lengths[j]); + if (values[j] != NULL) { n_lists++; } } @@ -2939,7 +2953,7 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, } /* Walk the lists, building the output data items. */ - n_lists = (argc + 1) / 3; + n_lists = ((argc + 1) / 3) * 2; /* Count the number of items in each list. */ for (i = 0; i < n_lists; i++) { for (j = 0; @@ -2953,28 +2967,30 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, buffer = NULL; length = 0; n_done = 0; - for (result_n = 0; n_done < n_lists; result_n++) { + for (result_n = 0; n_done < (n_lists / 2); result_n++) { /* Calculate how much space we need for this result. */ length = 0; n_done = 0; - for (i = 0; i < n_lists; i++) { + for (i = 0; i < n_lists; i += 2) { if (result_n < n_items[i]) { /* This list has an item for this result. */ length += lengths[i][result_n]; } else { - /* This list ran out of items -- use padding. */ - length += strlen(argv[i * 3 + 1]); + /* This list ran out of items -- use a value + * from the pad result list. */ + length += lengths[i + 1][result_n % + n_items[i + 1]]; /* Note that this list has run out. */ n_done++; } - if (i < (n_lists - 1)) { + if (i < (n_lists - 2)) { /* And the separator. */ - length += strlen(argv[i * 3 + 2]); + length += strlen(argv[(i / 2) * 3 + 2]); } } /* If we're out of data, we should stop before adding a result * to the list. */ - if (n_done == n_lists) { + if (n_done == n_lists / 2) { break; } /* Make sure the buffer is large enough. */ @@ -2997,7 +3013,7 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, } /* Build the output value. */ p = buffer; - for (i = 0; i < n_lists; i++) { + for (i = 0; i < n_lists; i += 2) { if (result_n < n_items[i]) { /* This list has an item for this result. */ l = lengths[i][result_n]; @@ -3005,14 +3021,16 @@ format_link(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, p += l; } else { /* This list ran out of items -- use padding. */ - l = strlen(argv[i * 3 + 1]); - memcpy(p, argv[i * 3 + 1], l); + l = lengths[i + 1][result_n % + n_items[i + 1]]; + memcpy(p, values[i + 1][result_n % + n_items[i + 1]], l); p += l; } - if (i < (n_lists - 1)) { + if (i < (n_lists - 2)) { /* Separator. */ - l = strlen(argv[i * 3 + 2]); - memcpy(p, argv[i * 3 + 2], l); + l = strlen(argv[(i / 2) * 3 + 2]); + memcpy(p, argv[(i / 2) * 3 + 2], l); p += l; } } -- cgit