2013-08-26
An example GNU Readline quoting function
Because I would have killed for an actual example when I was doing this,
here is the quoting function I'm currently using (as kind of promised
in my entry on how to do this in general). It does
rc-style filename quoting (put a '
at the front and end and escape
every '
with a second '
):
char *my_rl_quote(char *text, int m_t, char *qp) { char *r, *p, *tp; /* The worst case is that every character of text needs to be escaped; at that point we need 2x its space plus the ' at the start and end and a NULL byte. */ p = r = malloc(strlen(text)*2 + 3); if (r == NULL) return NULL; *p++ = '\''; for (tp = text; *tp; tp++) { if (*tp == '\'') *p++ = '\''; *p++ = *tp; } if (m_t == SINGLE_MATCH) *p++ = '\''; *p++ = 0; return r; }
(I suppose you could bum the for
loop a bit more by making the
increment operation be '*p++ = *tp++
' and taking out the last line of
the loop body. I feel that would make it a little bit too clever.)
You should set up things for Readline as follows:
rl_filename_quoting_function = my_rl_quote; rl_attempted_completion_function = my_rl_yesquote; rl_completer_quote_characters = "'"; /* for rc, probably incomplete: */ rl_filename_quote_characters = " `'=[]{}()<>|&\\\t";
my_rl_yesquote
is from my entry on how to do this in general.