print_timespec.c \
print_timeval.c \
print_timex.c \
+ print_utils.h \
printmode.c \
printrusage.c \
printsiginfo.c \
--- /dev/null
+#ifndef STRACE_PRINT_UTILS_H
+# define STRACE_PRINT_UTILS_H
+
+# include <inttypes.h>
+
+/* Hexadecimal output utils */
+
+static const char hex_chars[16] = "0123456789abcdef";
+
+/**
+ * Character array representing hexadecimal encoding of a character value.
+ *
+ * @param b_ Byte to provide representation for.
+ */
+# define BYTE_HEX_CHARS(b_) \
+ hex_chars[((uint8_t) (b_)) >> 4], hex_chars[((uint8_t) (b_)) & 0xf]
+# define BYTE_HEX_CHARS_PRINTF(b_) \
+ '\\', 'x', BYTE_HEX_CHARS(b_)
+# define BYTE_HEX_CHARS_PRINTF_QUOTED(b_) \
+ '\'', BYTE_HEX_CHARS_PRINTF(b_), '\''
+
+static inline char *
+sprint_byte_hex(char *buf, uint8_t val)
+{
+ *buf++ = hex_chars[val >> 4];
+ *buf++ = hex_chars[val & 0xf];
+
+ return buf;
+}
+
+/* Character classification utils */
+
+static inline bool
+is_print(uint8_t c)
+{
+ return (c >= ' ') && (c < 0x7f);
+}
+
+#endif /* STRACE_PRINT_UTILS_H */
#include <sys/uio.h>
#include "largefile_wrappers.h"
+#include "print_utils.h"
#include "xlat.h"
#include "xstring.h"
char *s = outstr;
unsigned int i;
int usehex, c, eol;
- bool escape;
+ bool printable;
if (style & QUOTE_0_TERMINATED)
eol = '\0';
goto asciz_ended;
*s++ = '\\';
*s++ = 'x';
- *s++ = "0123456789abcdef"[c >> 4];
- *s++ = "0123456789abcdef"[c & 0xf];
+ s = sprint_byte_hex(s, c);
}
goto string_ended;
*s++ = 'v';
break;
default:
- escape = (c < ' ') || (c > 0x7e);
+ printable = is_print(c);
- if (!escape && escape_chars)
- escape = !!strchr(escape_chars, c);
+ if (printable && escape_chars)
+ printable = !strchr(escape_chars, c);
- if (!escape) {
+ if (printable) {
*s++ = c;
} else {
/* Print \octal */
/* Hex dump */
do {
if (i < len) {
- *dst++ = "0123456789abcdef"[*src >> 4];
- *dst++ = "0123456789abcdef"[*src & 0xf];
+ dst = sprint_byte_hex(dst, *src);
} else {
*dst++ = ' ';
*dst++ = ' ';
i -= 16;
src -= 16;
do {
- if (*src >= ' ' && *src < 0x7f)
+ if (is_print(*src))
*dst++ = *src;
else
*dst++ = '.';
#include MPERS_DEFS
#include "print_fields.h"
+#include "print_utils.h"
#include "xstring.h"
/* v4l2_fourcc_be was added by Linux commit v3.18-rc1~101^2^2~127 */
unsigned int i;
tprints("v4l2_fourcc(");
+ /* Generic char array printing routine. */
for (i = 0; i < ARRAY_SIZE(a); ++i) {
unsigned char c = a[i];
'\0'
};
tprints(sym);
- } else if (c >= ' ' && c <= 0x7e) {
+ } else if (is_print(c)) {
char sym[] = {
'\'',
c,
tprints(sym);
} else {
char hex[] = {
- '\'',
- '\\',
- 'x',
- "0123456789abcdef"[c >> 4],
- "0123456789abcdef"[c & 0xf],
- '\'',
+ BYTE_HEX_CHARS_PRINTF_QUOTED(c),
'\0'
};
tprints(hex);