Skip to content

Commit

Permalink
unify fr_pair_print() and fr_pair_list_print() functions
Browse files Browse the repository at this point in the history
so that they call each other.  i.e. less code.

And clean up various other bits
  • Loading branch information
alandekok committed Aug 7, 2023
1 parent bf93142 commit f1d8eda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/bin/unit_test_attribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ static size_t command_normalise_attribute(command_result_t *result, command_file
* it if so.
*/

slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), &head);
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head);
if (slen <= 0) {
fr_assert(0);
RETURN_OK_WITH_ERROR();
Expand Down Expand Up @@ -1597,7 +1597,7 @@ static size_t command_decode_pair(command_result_t *result, command_file_ctx_t *
* Output may be an error, and we ignore
* it if so.
*/
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), &head);
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head);
if (slen <= 0) {
fr_assert(0);
RETURN_OK_WITH_ERROR();
Expand Down Expand Up @@ -1682,7 +1682,7 @@ static size_t command_decode_proto(command_result_t *result, command_file_ctx_t
/*
* Print the pairs.
*/
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), &head);
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head);
if (slen <= 0) {
fr_assert(0);
RETURN_OK_WITH_ERROR();
Expand Down Expand Up @@ -2023,7 +2023,7 @@ static size_t command_read_file(command_result_t *result, command_file_ctx_t *cc
/*
* Print the pairs.
*/
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), &head);
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head);
if (slen <= 0) {
fr_assert(0);
RETURN_OK_WITH_ERROR();
Expand Down Expand Up @@ -2535,7 +2535,7 @@ static size_t command_pair(command_result_t *result, command_file_ctx_t *cc,

PAIR_LIST_VERIFY(&head);

slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), &head);
slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head);
if (slen <= 0) {
fr_assert(0);
RETURN_OK_WITH_ERROR();
Expand Down
10 changes: 5 additions & 5 deletions src/lib/util/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,20 +769,20 @@ static inline fr_slen_t CC_HINT(nonnull(2,3))
fr_pair_t const *vp, fr_token_t quote)
SBUFF_OUT_TALLOC_FUNC_NO_LEN_DEF(fr_pair_print_value_quoted, vp, quote)

ssize_t fr_pair_print(fr_sbuff_t *out, fr_pair_t const *parent,
ssize_t fr_pair_print(fr_sbuff_t *out, fr_dict_attr_t const *parent,
fr_pair_t const *vp) CC_HINT(nonnull(1,3));

ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_pair_t const *parent,
ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_dict_attr_t const *parent,
fr_pair_t const *vp) CC_HINT(nonnull(1,3));

ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_pair_list_t const *list);
ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_list_t const *list);

static inline fr_slen_t CC_HINT(nonnull(2,4))
fr_pair_aprint(TALLOC_CTX *ctx, char **out, fr_pair_t const *parent, fr_pair_t const *vp)
fr_pair_aprint(TALLOC_CTX *ctx, char **out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
SBUFF_OUT_TALLOC_FUNC_NO_LEN_DEF(fr_pair_print, parent, vp)

static inline fr_slen_t CC_HINT(nonnull(2,4))
fr_pair_aprint_secure(TALLOC_CTX *ctx, char **out, fr_pair_t const *parent, fr_pair_t const *vp)
fr_pair_aprint_secure(TALLOC_CTX *ctx, char **out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
SBUFF_OUT_TALLOC_FUNC_NO_LEN_DEF(fr_pair_print_secure, parent, vp)

void fr_pair_fprint(FILE *, fr_pair_t const *vp) CC_HINT(nonnull);
Expand Down
44 changes: 19 additions & 25 deletions src/lib/util/pair_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
{
fr_sbuff_t our_out;
fr_pair_t *child;
fr_dcursor_t cursor;

PAIR_VERIFY(vp);

Expand All @@ -54,12 +52,9 @@ ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_toke
*/
our_out = FR_SBUFF(out);
FR_SBUFF_IN_CHAR_RETURN(&our_out, '{', ' ');
for (child = fr_pair_dcursor_init(&cursor, &vp->vp_group);
child != NULL;
child = fr_dcursor_next(&cursor)) {
FR_SBUFF_RETURN(fr_pair_print, &our_out, vp, child);
if (fr_dcursor_next_peek(&cursor)) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
}

FR_SBUFF_RETURN(fr_pair_list_print, &our_out, vp->da, &vp->vp_group);

FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');

FR_SBUFF_SET_RETURN(out, &our_out);
Expand Down Expand Up @@ -88,11 +83,10 @@ ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_toke
* - Length of data written to out.
* - value >= outlen on truncation.
*/
ssize_t fr_pair_print(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t const *vp)
ssize_t fr_pair_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
{
char const *token = NULL;
fr_sbuff_t our_out = FR_SBUFF(out);
fr_dict_attr_t const *parent_da = NULL;

PAIR_VERIFY(vp);

Expand All @@ -102,10 +96,13 @@ ssize_t fr_pair_print(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t const
token = "<INVALID-TOKEN>";
}

if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
/*
* Groups are printed from the root.
*/
if (parent && (parent->type == FR_TYPE_GROUP)) parent = NULL;

if (vp->da->flags.is_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent_da, vp->da, false);
FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
Expand Down Expand Up @@ -136,11 +133,10 @@ ssize_t fr_pair_print(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t const
* - Length of data written to out.
* - value >= outlen on truncation.
*/
ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t const *vp)
ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const *vp)
{
char const *token = NULL;
fr_sbuff_t our_out = FR_SBUFF(out);
fr_dict_attr_t const *parent_da = NULL;

PAIR_VERIFY(vp);

Expand All @@ -150,10 +146,13 @@ ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t
token = "<INVALID-TOKEN>";
}

if (parent && (parent->vp_type != FR_TYPE_GROUP)) parent_da = parent->da;
/*
* Groups are printed from the root.
*/
if (parent && (parent->type == FR_TYPE_GROUP)) parent = NULL;

if (vp->da->flags.is_raw) FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "raw.");
FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent_da, vp->da, false);
FR_DICT_ATTR_OID_PRINT_RETURN(&our_out, parent, vp->da, false);
FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
FR_SBUFF_IN_STRCPY_RETURN(&our_out, token);
FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
Expand Down Expand Up @@ -190,7 +189,7 @@ ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t
for (child = fr_pair_dcursor_init(&cursor, &vp->vp_group);
child != NULL;
child = fr_dcursor_next(&cursor)) {
FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp, child);
FR_SBUFF_RETURN(fr_pair_print_secure, &our_out, vp->da, child);
if (fr_dcursor_next_peek(&cursor)) FR_SBUFF_IN_CHAR_RETURN(&our_out, ',', ' ');
}
FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ', '}');
Expand All @@ -202,12 +201,13 @@ ssize_t fr_pair_print_secure(fr_sbuff_t *out, fr_pair_t const *parent, fr_pair_t
/** Print a pair list
*
* @param[in] out Where to write the string.
* @param[in] parent parent da to start from.
* @param[in] list pair list
* @return
* - Length of data written to out.
* - value >= outlen on truncation.
*/
ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_pair_list_t const *list)
ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_list_t const *list)
{
fr_pair_t *vp;
fr_sbuff_t our_out = FR_SBUFF(out);
Expand All @@ -219,13 +219,7 @@ ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_pair_list_t const *list)
}

while (true) {
ssize_t slen;

slen = fr_pair_print(&our_out, NULL, vp);
if (slen <= 0) {
fr_assert(0);
return slen;
}
FR_SBUFF_RETURN(fr_pair_print, &our_out, parent, vp);

vp = fr_pair_list_next(list, vp);
if (!vp) break;
Expand Down

0 comments on commit f1d8eda

Please sign in to comment.