diff options
author | hunt <hunt> | 2006-05-30 15:47:58 +0000 |
---|---|---|
committer | hunt <hunt> | 2006-05-30 15:47:58 +0000 |
commit | 3a4677f9c7ca40eafc379e829f0f2a86eae7b661 (patch) | |
tree | e674cc069f78936e4e93017a7844e45e0aed5cd7 | |
parent | 78db65bdd61c837e730d5eecf00e3b4ff8da9d5b (diff) | |
download | systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.tar.gz systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.tar.xz systemtap-steved-3a4677f9c7ca40eafc379e829f0f2a86eae7b661.zip |
2006-05-30 Martin Hunt <hunt@redhat.com>
* string.c (_stp_text_str): New function.
* string.h (_stp_text_str): Declare.
-rw-r--r-- | runtime/ChangeLog | 5 | ||||
-rw-r--r-- | runtime/string.c | 99 | ||||
-rw-r--r-- | runtime/string.h | 2 |
3 files changed, 106 insertions, 0 deletions
diff --git a/runtime/ChangeLog b/runtime/ChangeLog index e81373f8..497ae6d8 100644 --- a/runtime/ChangeLog +++ b/runtime/ChangeLog @@ -1,3 +1,8 @@ +2006-05-30 Martin Hunt <hunt@redhat.com> + + * string.c (_stp_text_str): New function. + * string.h (_stp_text_str): Declare. + 2006-05-25 Martin Hunt <hunt@redhat.com> * vsprintf.c (_stp_vsnprintf): Change %p to work like diff --git a/runtime/string.c b/runtime/string.c index fa8e2dda..c354d86f 100644 --- a/runtime/string.c +++ b/runtime/string.c @@ -212,5 +212,104 @@ char * _stp_string_ptr (String str) } \ }) +/** Return a printable text string. + * + * Takes a string, and any ASCII characters that are not printable are + * replaced by the corresponding escape sequence in the returned + * string. + * + * @param out Output string pointer + * @param in Input string pointer + * @param len Maximum length of string to return not including terminating 0. + * 0 means MAXSTRINGLEN. + * @param quoted Put double quotes around the string. If input string is truncated + * in will have "..." after the second quote. + */ +void _stp_text_str(char *out, char *in, int len, int quoted) +{ + const int length = len; + if (len == 0 || len > STP_STRING_SIZE-1) + len = STP_STRING_SIZE-1; + if (quoted) { + len -= 2; + *out++ = '\"'; + } + + while (*in && len > 0) { + int num = 1; + if (isprint(*in) && isascii(*in)) + *out++ = *in; + else { + switch (*in) { + case '\a': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + num = 2; + break; + default: + if (*in > 077) + num = 4; + else if (*in > 07) + num = 3; + else + num = 2; + break; + } + + if (len < num) + break; + + *out++ = '\\'; + switch (*in) { + case '\a': + *out++ = 'a'; + break; + case '\b': + *out++ = 'b'; + break; + case '\f': + *out++ = 'f'; + break; + case '\n': + *out++ = 'n'; + break; + case '\r': + *out++ = 'r'; + break; + case '\t': + *out++ = 't'; + break; + case '\v': + *out++ = 'v'; + break; + default: /* output octal representation */ + if (*in > 077) + *out++ = to_oct_digit(*in >> 6); + if (*in > 07) + *out++ = to_oct_digit((*in & 070) >> 3); + *out++ = to_oct_digit(*in & 07); + break; + } + } + len -= num; + in++; + } + + if (quoted) { + if (*in) { + out = out - 3 + len; + *out++ = '\"'; + *out++ = '.'; + *out++ = '.'; + *out++ = '.'; + } else + *out++ = '\"'; + } + *out = '\0'; +} /** @} */ #endif /* _STRING_C_ */ diff --git a/runtime/string.h b/runtime/string.h index 3ebf555c..699873af 100644 --- a/runtime/string.h +++ b/runtime/string.h @@ -32,7 +32,9 @@ typedef struct string *String; static struct string __stp_stdout = {.len = 0}; String _stp_stdout = &__stp_stdout; +#define to_oct_digit(c) ((c) + '0') void _stp_vsprintf (String str, const char *fmt, va_list args); void _stp_string_cat_char (String str1, const char c); +void _stp_text_str(char *out, char *in, int len, int quoted); #endif /* _STRING_H_ */ |