#define QUOTE_FORCE_HEX 0x10
#define QUOTE_EMIT_COMMENT 0x20
-extern int string_quote(const char *, char *, unsigned int, unsigned int);
+extern int string_quote(const char *, char *, unsigned int, unsigned int,
+ const char *escape_chars);
+extern int print_quoted_string_ex(const char *, unsigned int, unsigned int,
+ const char *escape_chars);
extern int print_quoted_string(const char *, unsigned int, unsigned int);
extern int print_quoted_cstring(const char *, unsigned int);
if (path[0] == '\0') {
outstr[1] = '@';
string_quote(path + 1, outstr + 2,
- path_len - 1, QUOTE_0_TERMINATED);
+ path_len - 1, QUOTE_0_TERMINATED, NULL);
} else {
string_quote(path, outstr + 1,
- path_len, QUOTE_0_TERMINATED);
+ path_len, QUOTE_0_TERMINATED, NULL);
}
path_str = outstr;
} else {
int rc = fsync(fd);
printf("fsync(%ld<", fd);
- print_quoted_string_ex(dir, false);
+ print_quoted_string_ex(dir, false, NULL);
printf("/%s>) = %s\n", checks[i].fdstr, sprintrc(rc));
close(fd);
*/
void
-print_quoted_string_ex(const char *instr, bool quote)
+print_quoted_string_ex(const char *instr, bool quote, const char *escape_chars)
{
- print_quoted_memory_ex(instr, strlen(instr), quote);
+ print_quoted_memory_ex(instr, strlen(instr), quote, escape_chars);
}
void
}
}
+static void
+print_octal(unsigned char c, char next)
+{
+ putchar('\\');
+
+ char c1 = '0' + (c & 0x7);
+ char c2 = '0' + ((c >> 3) & 0x7);
+ char c3 = '0' + (c >> 6);
+
+ if (next >= '0' && next <= '7') {
+ /* Print \octal */
+ putchar(c3);
+ putchar(c2);
+ } else {
+ /* Print \[[o]o]o */
+ if (c3 != '0')
+ putchar(c3);
+ if (c3 != '0' || c2 != '0')
+ putchar(c2);
+ }
+ putchar(c1);
+}
+
void
print_quoted_memory_ex(const void *const instr, const size_t len,
- bool quote)
+ bool quote, const char *escape_chars)
{
const unsigned char *str = (const unsigned char *) instr;
size_t i;
printf("\\v");
break;
default:
- if (c >= ' ' && c <= 0x7e)
+ if (c >= ' ' && c <= 0x7e &&
+ !(escape_chars && strchr(escape_chars, c))) {
putchar(c);
- else {
- putchar('\\');
-
- char c1 = '0' + (c & 0x7);
- char c2 = '0' + ((c >> 3) & 0x7);
- char c3 = '0' + (c >> 6);
-
- if (i < (len - 1) &&
- str[i + 1] >= '0' &&
- str[i + 1] <= '7') {
- /* Print \octal */
- putchar(c3);
- putchar(c2);
- } else {
- /* Print \[[o]o]o */
- if (c3 != '0')
- putchar(c3);
- if (c3 != '0' || c2 != '0')
- putchar(c2);
- }
- putchar(c1);
+ } else {
+ print_octal(c,
+ i < (len - 1) ? str[i + 1] : 0);
}
+
break;
}
}
void
print_quoted_memory(const void *const instr, const size_t len)
{
- print_quoted_memory_ex(instr, len, true);
+ print_quoted_memory_ex(instr, len, true, NULL);
}
void
/* Return inode number of socket descriptor. */
unsigned long inode_of_sockfd(int);
-/* Print string in a quoted form. */
-void print_quoted_string_ex(const char *, bool quote);
+/* Print string in a quoted form with optional escape characters. */
+void print_quoted_string_ex(const char *, bool quote, const char *escape_str);
/* Print string in a quoted form. */
void print_quoted_string(const char *);
*/
void print_quoted_cstring(const char *str, size_t size);
-/* Print memory in a quoted form. */
-void print_quoted_memory_ex(const void *, size_t, bool quote);
+/* Print memory in a quoted form with optional escape characters. */
+void print_quoted_memory_ex(const void *, size_t, bool quote,
+ const char *escape_chars);
/* Print memory in a quoted form. */
void print_quoted_memory(const void *, size_t);
* Quote string `instr' of length `size'
* Write up to (3 + `size' * 4) bytes to `outstr' buffer.
*
+ * `escape_chars' specifies characters (in addition to characters with
+ * codes 0..31, 127..255, single and double quotes) that should be escaped.
+ *
* If QUOTE_0_TERMINATED `style' flag is set,
* treat `instr' as a NUL-terminated string,
* checking up to (`size' + 1) bytes of `instr'.
*/
int
string_quote(const char *instr, char *outstr, const unsigned int size,
- const unsigned int style)
+ const unsigned int style, const char *escape_chars)
{
const unsigned char *ustr = (const unsigned char *) instr;
char *s = outstr;
unsigned int i;
int usehex, c, eol;
+ bool escape;
if (style & QUOTE_0_TERMINATED)
eol = '\0';
*s++ = 'v';
break;
default:
- if (c >= ' ' && c <= 0x7e)
+ escape = (c < ' ') || (c > 0x7e);
+
+ if (!escape && escape_chars)
+ escape = !!strchr(escape_chars, c);
+
+ if (!escape) {
*s++ = c;
- else {
+ } else {
/* Print \octal */
*s++ = '\\';
if (i + 1 < size
* Note that if QUOTE_0_TERMINATED is not set, always returns 1.
*/
int
-print_quoted_string(const char *str, unsigned int size,
- const unsigned int style)
+print_quoted_string_ex(const char *str, unsigned int size,
+ const unsigned int style, const char *escape_chars)
{
char *buf;
char *outstr;
}
}
- rc = string_quote(str, outstr, size, style);
+ rc = string_quote(str, outstr, size, style, escape_chars);
tprints(outstr);
free(buf);
return rc;
}
+inline int
+print_quoted_string(const char *str, unsigned int size,
+ const unsigned int style)
+{
+ return print_quoted_string_ex(str, size, style, NULL);
+}
+
/*
* Quote a NUL-terminated string `str' of length up to `size' - 1
* and print the result.
/* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
* or we were requested to print more than -s NUM chars)...
*/
- ellipsis = string_quote(str, outstr, size, style)
+ ellipsis = string_quote(str, outstr, size, style, NULL)
&& len
&& ((style & QUOTE_0_TERMINATED)
|| len > max_strlen);