Navigation Menu

Skip to content

Commit

Permalink
Add fr_canonicalize_error()
Browse files Browse the repository at this point in the history
So the error messages are all of the same format, and the
code isn't duplicated all over the place
  • Loading branch information
alandekok committed Sep 23, 2014
1 parent 664e97d commit 8952508
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/include/parser.h
Expand Up @@ -103,6 +103,8 @@ size_t fr_cond_sprint(char *buffer, size_t bufsize, fr_cond_t const *c);

bool fr_condition_walk(fr_cond_t *head, bool (*callback)(void *, fr_cond_t *), void *ctx);

void fr_canonicalize_error(TALLOC_CTX *ctx, ssize_t slen, char **spaces, char **text, char const *msg);

/*
* In xlat.c for now
*/
Expand Down
45 changes: 45 additions & 0 deletions src/main/parser.c
Expand Up @@ -1504,3 +1504,48 @@ bool fr_condition_walk(fr_cond_t *c, bool (*callback)(void *, fr_cond_t *), void

return true;
}

void fr_canonicalize_error(TALLOC_CTX *ctx, ssize_t slen, char **spaces, char **text, char const *msg)
{
size_t offset, skip = 0;
char *spbuf, *p;
char *value;

offset = -slen;

/*
* Ensure that the error isn't indented
* too far.
*/
if (offset > 45) {
skip = offset - 40;
offset -= skip;
value = talloc_strdup(ctx, msg + skip);
memcpy(value, "...", 3);

} else {
value = talloc_strdup(ctx, msg);
}

spbuf = talloc_array(ctx, char, offset + 1);
memset(spbuf, ' ', offset);
spbuf[offset] = '\0';

/*
* Smash tabs to spaces for the input string.
*/
for (p = value; *p != '\0'; p++) {
if (*p == '\t') *p = ' ';
}


/*
* Ensure that there isn't too much text after the error.
*/
if (strlen(value) > 100) {
memcpy(value + 95, "... ", 5);
}

*spaces = spbuf;
*text = value;
}

0 comments on commit 8952508

Please sign in to comment.