REORG: cli: move dump_text(), dump_text_line(), and dump_binary() to standard.c

These are general purpose functions, move them away.
This commit is contained in:
Willy Tarreau 2016-11-22 18:00:20 +01:00
parent 0baac8cf1f
commit 97c2ae13bc
3 changed files with 130 additions and 125 deletions

View File

@ -1129,6 +1129,11 @@ static inline unsigned long long rdtsc()
struct list;
int list_append_word(struct list *li, const char *str, char **err);
int dump_text(struct chunk *out, const char *buf, int bsize);
int dump_binary(struct chunk *out, const char *buf, int bsize);
int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
int *line, int ptr);
/* same as realloc() except that ptr is also freed upon failure */
static inline void *my_realloc2(void *ptr, size_t size)
{

125
src/cli.c
View File

@ -409,73 +409,6 @@ int cli_has_level(struct appctx *appctx, int level)
}
/* print a string of text buffer to <out>. The format is :
* Non-printable chars \t, \n, \r and \e are * encoded in C format.
* Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped.
* Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
*/
static int dump_text(struct chunk *out, const char *buf, int bsize)
{
unsigned char c;
int ptr = 0;
while (buf[ptr] && ptr < bsize) {
c = buf[ptr];
if (isprint(c) && isascii(c) && c != '\\' && c != ' ' && c != '=') {
if (out->len > out->size - 1)
break;
out->str[out->len++] = c;
}
else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') {
if (out->len > out->size - 2)
break;
out->str[out->len++] = '\\';
switch (c) {
case ' ': c = ' '; break;
case '\t': c = 't'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\e': c = 'e'; break;
case '\\': c = '\\'; break;
case '=': c = '='; break;
}
out->str[out->len++] = c;
}
else {
if (out->len > out->size - 4)
break;
out->str[out->len++] = '\\';
out->str[out->len++] = 'x';
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
}
ptr++;
}
return ptr;
}
/* print a buffer in hexa.
* Print stopped if <bsize> is reached, or if no more place in the chunk.
*/
static int dump_binary(struct chunk *out, const char *buf, int bsize)
{
unsigned char c;
int ptr = 0;
while (ptr < bsize) {
c = buf[ptr];
if (out->len > out->size - 2)
break;
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
ptr++;
}
return ptr;
}
/* Dump the status of a table to a stream interface's
* read buffer. It returns 0 if the output buffer is full
* and needs to be called again, otherwise non-zero.
@ -1996,64 +1929,6 @@ static int stats_table_request(struct stream_interface *si, int action)
return 1;
}
/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
* <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
* which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
* encoded in C format. Other non-printable chars are encoded "\xHH". Original
* lines are respected within the limit of 70 output chars. Lines that are
* continuation of a previous truncated line begin with "+" instead of " "
* after the offset. The new pointer is returned.
*/
static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
int *line, int ptr)
{
int end;
unsigned char c;
end = out->len + 80;
if (end > out->size)
return ptr;
chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
while (ptr < len && ptr < bsize) {
c = buf[ptr];
if (isprint(c) && isascii(c) && c != '\\') {
if (out->len > end - 2)
break;
out->str[out->len++] = c;
} else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
if (out->len > end - 3)
break;
out->str[out->len++] = '\\';
switch (c) {
case '\t': c = 't'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\e': c = 'e'; break;
case '\\': c = '\\'; break;
}
out->str[out->len++] = c;
} else {
if (out->len > end - 5)
break;
out->str[out->len++] = '\\';
out->str[out->len++] = 'x';
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
}
if (buf[ptr++] == '\n') {
/* we had a line break, let's return now */
out->str[out->len++] = '\n';
*line = ptr;
return ptr;
}
}
/* we have an incomplete line, we return it as-is */
out->str[out->len++] = '\n';
return ptr;
}
/* This function dumps all captured errors onto the stream interface's
* read buffer. It returns 0 if the output buffer is full and it needs
* to be called again, otherwise non-zero.

View File

@ -3640,6 +3640,131 @@ int list_append_word(struct list *li, const char *str, char **err)
return 0;
}
/* print a string of text buffer to <out>. The format is :
* Non-printable chars \t, \n, \r and \e are * encoded in C format.
* Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped.
* Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
*/
int dump_text(struct chunk *out, const char *buf, int bsize)
{
unsigned char c;
int ptr = 0;
while (buf[ptr] && ptr < bsize) {
c = buf[ptr];
if (isprint(c) && isascii(c) && c != '\\' && c != ' ' && c != '=') {
if (out->len > out->size - 1)
break;
out->str[out->len++] = c;
}
else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') {
if (out->len > out->size - 2)
break;
out->str[out->len++] = '\\';
switch (c) {
case ' ': c = ' '; break;
case '\t': c = 't'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\e': c = 'e'; break;
case '\\': c = '\\'; break;
case '=': c = '='; break;
}
out->str[out->len++] = c;
}
else {
if (out->len > out->size - 4)
break;
out->str[out->len++] = '\\';
out->str[out->len++] = 'x';
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
}
ptr++;
}
return ptr;
}
/* print a buffer in hexa.
* Print stopped if <bsize> is reached, or if no more place in the chunk.
*/
int dump_binary(struct chunk *out, const char *buf, int bsize)
{
unsigned char c;
int ptr = 0;
while (ptr < bsize) {
c = buf[ptr];
if (out->len > out->size - 2)
break;
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
ptr++;
}
return ptr;
}
/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
* <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
* which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
* encoded in C format. Other non-printable chars are encoded "\xHH". Original
* lines are respected within the limit of 70 output chars. Lines that are
* continuation of a previous truncated line begin with "+" instead of " "
* after the offset. The new pointer is returned.
*/
int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
int *line, int ptr)
{
int end;
unsigned char c;
end = out->len + 80;
if (end > out->size)
return ptr;
chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
while (ptr < len && ptr < bsize) {
c = buf[ptr];
if (isprint(c) && isascii(c) && c != '\\') {
if (out->len > end - 2)
break;
out->str[out->len++] = c;
} else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
if (out->len > end - 3)
break;
out->str[out->len++] = '\\';
switch (c) {
case '\t': c = 't'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\e': c = 'e'; break;
case '\\': c = '\\'; break;
}
out->str[out->len++] = c;
} else {
if (out->len > end - 5)
break;
out->str[out->len++] = '\\';
out->str[out->len++] = 'x';
out->str[out->len++] = hextab[(c >> 4) & 0xF];
out->str[out->len++] = hextab[c & 0xF];
}
if (buf[ptr++] == '\n') {
/* we had a line break, let's return now */
out->str[out->len++] = '\n';
*line = ptr;
return ptr;
}
}
/* we have an incomplete line, we return it as-is */
out->str[out->len++] = '\n';
return ptr;
}
/*
* Local variables:
* c-indent-level: 8