Skip to content

printf does not implement %Q, and does not properly handle %q #907

@Skyb0rg007

Description

@Skyb0rg007

There are a few incompatibilities between brush's printf and that of bash.
I understand from #854 that some of these issues can only be fixed through upstreaming to uucore, so I only highlight errors here that I believe can be fixed without changes to uucore.

Issue 1: %Q is not implemented

The printf code special-cases on %q: it can also match %Q with no changes (%Q only differs when it comes to handling precision).

Issue 2: %q does not accept (and ignore) specifiers

The implementation should ignore all precision specifiers: instead of checking fmt == "%q", it should check that fmt[0] == '%' && fmt[fmt.len()-1] == 'q' (or something similar).

Issue 3: %q performs the wrong quoting for strings with spaces

The %q specifier should use backslashes to escape characters instead of using single quotes.

Issue 4: %#q is not implemented

The current quoting method (always using single quotes) is the alternate quoting mechanism.
Handling the full parsing of the %q format is likely annoying without upstream support, but just matching %#q is hopefully not too bad.

Transcript

$ brush -c 'printf "%q\n" "foo bar"'
'foo bar'
$ bash -c 'printf "%q\n" "foo bar"'
foo\ bar

$ brush -c 'printf "%2q\n" "foo bar"'
error: printf: printf: printf parsing error: %2q: invalid conversion specification
$ bash -c 'printf "%2q\n" "foo bar"'
foo\ bar

$ brush -c 'printf "%#q\n" "foo bar"'
error: printf: printf: printf parsing error: %#q: invalid conversion specification
$ bash -c 'printf "%#q\n" "foo bar"'
'foo bar'

$ brush -c 'printf "%Q\n" "foo bar"'
error: printf: printf: printf parsing error: %Q: invalid conversion specification
$ bash -c 'printf "%Q\n" "foo bar"'
foo\ bar

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: builtinsIssues or PRs specific to built-in commandsarea: compatRelated to compatibility with standard shells

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions