* Federico G. Schwindt <fg...@lodoss.net> [110315 17:38]: > > I think I'm slightly confused as to you'd like fixed - do you mean that > > one shouldn't need to escape a '[' if it's the first character? (note > > that /bin/[ exists). Else, can you clarify a bit more? Thanks! > > correct, shouldn't need to escape a '[' if it's teh first character. > i do know of [, but command and file completion are two different things. > see > bash too.
I don't want to be defensive, but hey, how is bash relevant? [ is treated as a globbing character, you know. Is it first character or not, ksh doesn't care, it just tries to expand it. Oh well, you can try expanding it and then - if it failed - escape it and try again, like in the patch below. Bear in mind I didn't give it much thought, because I have a feeling it's not a good idea. diff --git a/edit.c b/edit.c index 4cff25b..eee8a5d 100644 --- a/edit.c +++ b/edit.c @@ -18,6 +18,7 @@ #include <libgen.h> #include <sys/stat.h> +#define ESCAPEDCHARS "\"#$&'()*;<=>?[\\`{|}" static void x_sigwinch(int); static volatile sig_atomic_t got_sigwinch; @@ -348,6 +349,7 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp) { char *toglob; char **words; + char *estr; int nwords, i, idx, escaping; XPtrV w; struct source *s, *sold; @@ -416,6 +418,21 @@ x_file_glob(int flags, const char *str, int slen, char ***wordsp) } afree(toglob, ATEMP); + /* Globbing failed, do escaping and try again. */ + if (!nwords && !words) { + estr = alloc(2 * slen + 1, ATEMP); + idx = 0; + for(i = 0; i < slen; i++) { + if (strchr(ESCAPEDCHARS, str[i])) + estr[idx++] = '\\'; + estr[idx++] = str[i]; + } + estr[idx] = '\0'; + nwords = x_file_glob(flags, estr, idx, wordsp); + afree(estr, ATEMP); + return nwords; + } + if (nwords) { *wordsp = words; } else if (words) { @@ -826,7 +843,7 @@ x_escape(const char *s, size_t len, int (*putbuf_func) (const char *, size_t)) int rval = 0; for (add = 0, wlen = len; wlen - add > 0; add++) { - if (strchr("\"#$&'()*;<=>?[\\`{|}", s[add]) || + if (strchr(ESCAPEDCHARS, s[add]) || strchr(ifs, s[add])) { if (putbuf_func(s, add) != 0) { rval = -1; -- Alexander Polakov | plhk.ru