diff options
author | Pádraig Brady <P@draigBrady.com> | 2015-11-03 12:56:22 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2015-11-04 23:30:15 +0000 |
commit | 5ef08864113505c6392158c9fac9a6cb1b3ac0e6 (patch) | |
tree | 03989580af0b143935d1319e69f6fd519d880aab /src | |
parent | 697b8ce26549cceffa8745a1dc0f13654173c575 (diff) | |
download | coreutils-5ef08864113505c6392158c9fac9a6cb1b3ac0e6.tar.xz |
printf: support the %q format to quote for shell
* src/printf.c (usage): Mention the new format.
(print_formatted): Handle the quoting by calling
out to the quotearg module with "shell-escape" mode.
* doc/coreutils.texi (printf invocation): Document %q.
* tests/misc/printf-quote.sh: New test.
* tests/local.mk: Reference new test.
* NEWS: Mention the new feature.
Diffstat (limited to 'src')
-rw-r--r-- | src/printf.c | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/printf.c b/src/printf.c index 3e68b995f..a24494631 100644 --- a/src/printf.c +++ b/src/printf.c @@ -41,6 +41,10 @@ %b = print an argument string, interpreting backslash escapes, except that octal escapes are of the form \0 or \0ooo. + %q = print an argument string in a format that can be + reused as shell input. Escaped characters used the proposed + POSIX $'' syntax supported by most shells. + The 'format' argument is re-used as many times as necessary to convert all of the given arguments. @@ -124,7 +128,9 @@ FORMAT controls the output as in C printf. Interpreted sequences are:\n\ %% a single %\n\ %b ARGUMENT as a string with '\\' escapes interpreted,\n\ except that octal escapes are of the form \\0 or \\0NNN\n\ -\n\ + %q ARGUMENT is printed in a format that can be reused as shell input,\n\ + escaping non-printable characters with the proposed POSIX $'' syntax.\ +\n\n\ and all C format specifications ending with one of diouxXfeEgGcs, with\n\ ARGUMENTs converted to proper type first. Variable widths are handled.\n\ "), stdout); @@ -506,6 +512,18 @@ print_formatted (const char *format, int argc, char **argv) break; } + if (*f == 'q') + { + if (argc > 0) + { + fputs (quotearg_style (shell_escape_quoting_style, *argv), + stdout); + ++argv; + --argc; + } + break; + } + memset (ok, 0, sizeof ok); ok['a'] = ok['A'] = ok['c'] = ok['d'] = ok['e'] = ok['E'] = ok['f'] = ok['F'] = ok['g'] = ok['G'] = ok['i'] = ok['o'] = |