Skip to main content

quote_tokenized_output

Function quote_tokenized_output 

Source
pub fn quote_tokenized_output(str_in: &str, file: &mut impl Write) -> Result<()>
Expand description

Port of void quote_tokenized_output(char *str, FILE *file) from Src/exec.c:2114.

C body (abridged):

for (; *s; s++) {
    switch (*s) {
        case Meta: putc(*++s ^ 32, file); continue;
        case Nularg: continue;
        case '\\' '<' '>' '(' '|' ')' '^' '#' '~' '[' ']' '*' '?' '$' ' ':
            putc('\\', file); break;
        case '\t': fputs("$'\\t'", file); continue;
        case '\n': fputs("$'\\n'", file); continue;
        case '\r': fputs("$'\\r'", file); continue;
        case '=': if (s == str) putc('\\', file); break;
        default:
            if (itok(*s)) { putc(ztokens[*s - Pound], file); continue; }
    }
    putc(*s, file);
}

Used by xtrace (set -x printer) and whence -c to display a tokenized argv in a form where lexer tokens (Star, Inpar, …) surface as unescaped chars (*, () while literal special chars get backslash-escaped — round-tripping through the shell.