Home | History | Annotate | Line # | Download | only in ksh
edit.c revision 1.12
      1  1.12       wiz /*	$NetBSD: edit.c,v 1.12 2003/08/26 07:28:39 wiz Exp $	*/
      2   1.2       tls 
      3   1.1       jtc /*
      4   1.1       jtc  * Command line editing - common code
      5   1.1       jtc  *
      6   1.1       jtc  */
      7  1.11       agc #include <sys/cdefs.h>
      8  1.11       agc 
      9  1.11       agc #ifndef lint
     10  1.12       wiz __RCSID("$NetBSD: edit.c,v 1.12 2003/08/26 07:28:39 wiz Exp $");
     11  1.11       agc #endif
     12  1.11       agc 
     13   1.1       jtc 
     14   1.1       jtc #include "config.h"
     15   1.1       jtc #ifdef EDIT
     16   1.1       jtc 
     17   1.1       jtc #include "sh.h"
     18   1.1       jtc #include "tty.h"
     19   1.1       jtc #define EXTERN
     20   1.1       jtc #include "edit.h"
     21   1.1       jtc #undef EXTERN
     22   1.1       jtc #ifdef OS_SCO	/* SCO Unix 3.2v4.1 */
     23   1.1       jtc # include <sys/stream.h>	/* needed for <sys/ptem.h> */
     24   1.1       jtc # include <sys/ptem.h>		/* needed for struct winsize */
     25   1.1       jtc #endif /* OS_SCO */
     26   1.1       jtc #include <ctype.h>
     27   1.3  christos #include <sys/ioctl.h>
     28   1.1       jtc #include "ksh_stat.h"
     29   1.1       jtc 
     30   1.1       jtc 
     31   1.1       jtc #if defined(TIOCGWINSZ)
     32   1.1       jtc static RETSIGTYPE x_sigwinch ARGS((int sig));
     33   1.1       jtc static int got_sigwinch;
     34   1.1       jtc static void check_sigwinch ARGS((void));
     35   1.1       jtc #endif /* TIOCGWINSZ */
     36   1.1       jtc 
     37   1.1       jtc static int	x_file_glob ARGS((int flags, const char *str, int slen,
     38   1.1       jtc 				  char ***wordsp));
     39   1.1       jtc static int	x_command_glob ARGS((int flags, const char *str, int slen,
     40   1.1       jtc 				     char ***wordsp));
     41   1.1       jtc static int	x_locate_word ARGS((const char *buf, int buflen, int pos,
     42   1.1       jtc 				    int *startp, int *is_command));
     43   1.3  christos static int 	path_order_cmp ARGS((const void *, const void *));
     44   1.1       jtc 
     45   1.1       jtc static char vdisable_c;
     46   1.1       jtc 
     47   1.1       jtc 
     48   1.1       jtc /* Called from main */
     49   1.1       jtc void
     50   1.1       jtc x_init()
     51   1.1       jtc {
     52   1.5   hubertf 	/* set to -2 to force initial binding */
     53   1.1       jtc 	edchars.erase = edchars.kill = edchars.intr = edchars.quit
     54   1.5   hubertf 		= edchars.eof = -2;
     55   1.1       jtc 	/* default value for deficient systems */
     56   1.1       jtc 	edchars.werase = 027;	/* ^W */
     57   1.1       jtc 
     58   1.1       jtc #ifdef TIOCGWINSZ
     59   1.1       jtc # ifdef SIGWINCH
     60   1.1       jtc 	if (setsig(&sigtraps[SIGWINCH], x_sigwinch, SS_RESTORE_ORIG|SS_SHTRAP))
     61   1.1       jtc 		sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
     62   1.1       jtc # endif /* SIGWINCH */
     63   1.2       tls 	got_sigwinch = 1; /* force initial check */
     64   1.1       jtc 	check_sigwinch();
     65   1.1       jtc #endif /* TIOCGWINSZ */
     66   1.1       jtc 
     67   1.1       jtc #ifdef EMACS
     68   1.1       jtc 	x_init_emacs();
     69   1.1       jtc #endif /* EMACS */
     70   1.1       jtc 
     71   1.1       jtc 	/* Bizarreness to figure out how to disable
     72   1.1       jtc 	 * a struct termios.c_cc[] char
     73   1.1       jtc 	 */
     74   1.1       jtc #ifdef _POSIX_VDISABLE
     75   1.1       jtc 	if (_POSIX_VDISABLE >= 0)
     76   1.1       jtc 		vdisable_c = (char) _POSIX_VDISABLE;
     77   1.1       jtc 	else
     78   1.1       jtc 		/* `feature not available' */
     79   1.1       jtc 		vdisable_c = (char) 0377;
     80   1.1       jtc #else
     81   1.1       jtc # if defined(HAVE_PATHCONF) && defined(_PC_VDISABLE)
     82   1.1       jtc 	vdisable_c = fpathconf(tty_fd, _PC_VDISABLE);
     83   1.1       jtc # else
     84   1.1       jtc 	vdisable_c = (char) 0377;	/* default to old BSD value */
     85   1.1       jtc # endif
     86   1.1       jtc #endif /* _POSIX_VDISABLE */
     87   1.1       jtc }
     88   1.1       jtc 
     89   1.1       jtc #if defined(TIOCGWINSZ)
     90   1.1       jtc static RETSIGTYPE
     91   1.1       jtc x_sigwinch(sig)
     92   1.1       jtc     	int sig;
     93   1.1       jtc {
     94   1.1       jtc 	got_sigwinch = 1;
     95   1.1       jtc 	return RETSIGVAL;
     96   1.1       jtc }
     97   1.1       jtc 
     98   1.1       jtc static void
     99   1.1       jtc check_sigwinch ARGS((void))
    100   1.1       jtc {
    101   1.1       jtc 	if (got_sigwinch) {
    102   1.1       jtc 		struct winsize ws;
    103   1.1       jtc 
    104   1.1       jtc 		got_sigwinch = 0;
    105   1.1       jtc 		if (procpid == kshpid && ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
    106   1.1       jtc 			struct tbl *vp;
    107   1.1       jtc 
    108   1.1       jtc 			/* Do NOT export COLUMNS/LINES.  Many applications
    109   1.1       jtc 			 * check COLUMNS/LINES before checking ws.ws_col/row,
    110   1.1       jtc 			 * so if the app is started with C/L in the environ
    111   1.1       jtc 			 * and the window is then resized, the app won't
    112   1.1       jtc 			 * see the change cause the environ doesn't change.
    113   1.1       jtc 			 */
    114   1.1       jtc 			if (ws.ws_col) {
    115   1.1       jtc 				x_cols = ws.ws_col < MIN_COLS ? MIN_COLS
    116   1.1       jtc 						: ws.ws_col;
    117   1.1       jtc 
    118   1.1       jtc 				if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
    119   1.1       jtc 					setint(vp, (long) ws.ws_col);
    120   1.1       jtc 			}
    121   1.1       jtc 			if (ws.ws_row
    122   1.1       jtc 			    && (vp = typeset("LINES", 0, 0, 0, 0)))
    123   1.1       jtc 				setint(vp, (long) ws.ws_row);
    124   1.1       jtc 		}
    125   1.1       jtc 	}
    126   1.1       jtc }
    127   1.1       jtc #endif /* TIOCGWINSZ */
    128   1.1       jtc 
    129   1.1       jtc /*
    130   1.1       jtc  * read an edited command line
    131   1.1       jtc  */
    132   1.1       jtc int
    133   1.1       jtc x_read(buf, len)
    134   1.1       jtc 	char *buf;
    135   1.1       jtc 	size_t len;
    136   1.1       jtc {
    137   1.1       jtc 	int	i;
    138   1.1       jtc 
    139   1.1       jtc #if defined(TIOCGWINSZ)
    140   1.1       jtc 	if (got_sigwinch)
    141   1.1       jtc 		check_sigwinch();
    142   1.1       jtc #endif /* TIOCGWINSZ */
    143   1.1       jtc 
    144   1.1       jtc 	x_mode(TRUE);
    145   1.1       jtc #ifdef EMACS
    146   1.1       jtc 	if (Flag(FEMACS) || Flag(FGMACS))
    147   1.1       jtc 		i = x_emacs(buf, len);
    148   1.1       jtc 	else
    149   1.1       jtc #endif
    150   1.1       jtc #ifdef VI
    151   1.1       jtc 	if (Flag(FVI))
    152   1.1       jtc 		i = x_vi(buf, len);
    153   1.1       jtc 	else
    154   1.1       jtc #endif
    155   1.1       jtc 		i = -1;		/* internal error */
    156   1.1       jtc 	x_mode(FALSE);
    157   1.1       jtc 	return i;
    158   1.1       jtc }
    159   1.1       jtc 
    160   1.1       jtc /* tty I/O */
    161   1.1       jtc 
    162   1.1       jtc int
    163   1.1       jtc x_getc()
    164   1.1       jtc {
    165   1.1       jtc #ifdef OS2
    166   1.1       jtc 	unsigned char c = _read_kbd(0, 1, 0);
    167   1.1       jtc 	return c == 0 ? 0xE0 : c;
    168   1.1       jtc #else /* OS2 */
    169   1.1       jtc 	char c;
    170   1.1       jtc 	int n;
    171   1.1       jtc 
    172   1.1       jtc 	while ((n = blocking_read(0, &c, 1)) < 0 && errno == EINTR)
    173   1.1       jtc 		if (trap) {
    174   1.1       jtc 			x_mode(FALSE);
    175   1.1       jtc 			runtraps(0);
    176   1.1       jtc 			x_mode(TRUE);
    177   1.1       jtc 		}
    178   1.1       jtc 	if (n != 1)
    179   1.1       jtc 		return -1;
    180   1.1       jtc 	return (int) (unsigned char) c;
    181   1.1       jtc #endif /* OS2 */
    182   1.1       jtc }
    183   1.1       jtc 
    184   1.1       jtc void
    185   1.1       jtc x_flush()
    186   1.1       jtc {
    187   1.1       jtc 	shf_flush(shl_out);
    188   1.1       jtc }
    189   1.1       jtc 
    190   1.1       jtc void
    191   1.1       jtc x_putc(c)
    192   1.1       jtc 	int c;
    193   1.1       jtc {
    194   1.1       jtc 	shf_putc(c, shl_out);
    195   1.1       jtc }
    196   1.1       jtc 
    197   1.1       jtc void
    198   1.1       jtc x_puts(s)
    199   1.1       jtc 	const char *s;
    200   1.1       jtc {
    201   1.1       jtc 	while (*s != 0)
    202   1.1       jtc 		shf_putc(*s++, shl_out);
    203   1.1       jtc }
    204   1.1       jtc 
    205   1.1       jtc bool_t
    206   1.1       jtc x_mode(onoff)
    207   1.1       jtc 	bool_t	onoff;
    208   1.1       jtc {
    209   1.1       jtc 	static bool_t	x_cur_mode;
    210   1.1       jtc 	bool_t		prev;
    211   1.1       jtc 
    212   1.1       jtc 	if (x_cur_mode == onoff)
    213   1.1       jtc 		return x_cur_mode;
    214   1.1       jtc 	prev = x_cur_mode;
    215   1.1       jtc 	x_cur_mode = onoff;
    216   1.1       jtc 
    217   1.1       jtc 	if (onoff) {
    218   1.1       jtc 		TTY_state	cb;
    219   1.1       jtc 		X_chars		oldchars;
    220   1.1       jtc 
    221   1.1       jtc 		oldchars = edchars;
    222   1.1       jtc 		cb = tty_state;
    223   1.1       jtc 
    224   1.1       jtc #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
    225   1.1       jtc 		edchars.erase = cb.c_cc[VERASE];
    226   1.1       jtc 		edchars.kill = cb.c_cc[VKILL];
    227   1.1       jtc 		edchars.intr = cb.c_cc[VINTR];
    228   1.1       jtc 		edchars.quit = cb.c_cc[VQUIT];
    229   1.1       jtc 		edchars.eof = cb.c_cc[VEOF];
    230   1.1       jtc # ifdef VWERASE
    231   1.1       jtc 		edchars.werase = cb.c_cc[VWERASE];
    232   1.1       jtc # endif
    233   1.1       jtc # ifdef _CRAY2		/* brain-damaged terminal handler */
    234   1.1       jtc 		cb.c_lflag &= ~(ICANON|ECHO);
    235   1.1       jtc 		/* rely on print routine to map '\n' to CR,LF */
    236   1.1       jtc # else
    237   1.1       jtc 		cb.c_iflag &= ~(INLCR|ICRNL);
    238   1.1       jtc #  ifdef _BSD_SYSV	/* need to force CBREAK instead of RAW (need CRMOD on output) */
    239   1.1       jtc 		cb.c_lflag &= ~(ICANON|ECHO);
    240   1.1       jtc #  else
    241   1.1       jtc #   ifdef SWTCH	/* need CBREAK to handle swtch char */
    242   1.1       jtc 		cb.c_lflag &= ~(ICANON|ECHO);
    243   1.1       jtc 		cb.c_lflag |= ISIG;
    244   1.1       jtc 		cb.c_cc[VINTR] = vdisable_c;
    245   1.1       jtc 		cb.c_cc[VQUIT] = vdisable_c;
    246   1.1       jtc #   else
    247   1.1       jtc 		cb.c_lflag &= ~(ISIG|ICANON|ECHO);
    248   1.1       jtc #   endif
    249   1.1       jtc #  endif
    250   1.1       jtc #  ifdef VLNEXT
    251   1.1       jtc 		/* osf/1 processes lnext when ~icanon */
    252   1.1       jtc 		cb.c_cc[VLNEXT] = vdisable_c;
    253   1.1       jtc #  endif /* VLNEXT */
    254   1.1       jtc #  ifdef VDISCARD
    255   1.1       jtc 		/* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */
    256   1.1       jtc 		cb.c_cc[VDISCARD] = vdisable_c;
    257   1.1       jtc #  endif /* VDISCARD */
    258   1.1       jtc 		cb.c_cc[VTIME] = 0;
    259   1.1       jtc 		cb.c_cc[VMIN] = 1;
    260   1.1       jtc # endif	/* _CRAY2 */
    261   1.1       jtc #else
    262   1.1       jtc 	/* Assume BSD tty stuff. */
    263   1.1       jtc 		edchars.erase = cb.sgttyb.sg_erase;
    264   1.1       jtc 		edchars.kill = cb.sgttyb.sg_kill;
    265   1.1       jtc 		cb.sgttyb.sg_flags &= ~ECHO;
    266   1.1       jtc 		cb.sgttyb.sg_flags |= CBREAK;
    267   1.1       jtc #  ifdef TIOCGATC
    268   1.1       jtc 		edchars.intr = cb.lchars.tc_intrc;
    269   1.1       jtc 		edchars.quit = cb.lchars.tc_quitc;
    270   1.1       jtc 		edchars.eof = cb.lchars.tc_eofc;
    271   1.1       jtc 		edchars.werase = cb.lchars.tc_werasc;
    272   1.1       jtc 		cb.lchars.tc_suspc = -1;
    273   1.1       jtc 		cb.lchars.tc_dsuspc = -1;
    274   1.1       jtc 		cb.lchars.tc_lnextc = -1;
    275   1.1       jtc 		cb.lchars.tc_statc = -1;
    276   1.1       jtc 		cb.lchars.tc_intrc = -1;
    277   1.1       jtc 		cb.lchars.tc_quitc = -1;
    278   1.1       jtc 		cb.lchars.tc_rprntc = -1;
    279   1.1       jtc #  else
    280   1.1       jtc 		edchars.intr = cb.tchars.t_intrc;
    281   1.1       jtc 		edchars.quit = cb.tchars.t_quitc;
    282   1.1       jtc 		edchars.eof = cb.tchars.t_eofc;
    283   1.1       jtc 		cb.tchars.t_intrc = -1;
    284   1.1       jtc 		cb.tchars.t_quitc = -1;
    285   1.1       jtc #   ifdef TIOCGLTC
    286   1.1       jtc 		edchars.werase = cb.ltchars.t_werasc;
    287   1.1       jtc 		cb.ltchars.t_suspc = -1;
    288   1.1       jtc 		cb.ltchars.t_dsuspc = -1;
    289   1.1       jtc 		cb.ltchars.t_lnextc = -1;
    290   1.1       jtc 		cb.ltchars.t_rprntc = -1;
    291   1.1       jtc #   endif
    292   1.1       jtc #  endif /* TIOCGATC */
    293   1.1       jtc #endif /* HAVE_TERMIOS_H || HAVE_TERMIO_H */
    294   1.1       jtc 
    295   1.1       jtc 		set_tty(tty_fd, &cb, TF_WAIT);
    296   1.1       jtc 
    297   1.5   hubertf #ifdef __CYGWIN__
    298   1.5   hubertf 		if (edchars.eof == '\0')
    299   1.5   hubertf 			edchars.eof = '\4';
    300   1.5   hubertf #endif /* __CYGWIN__ */
    301   1.5   hubertf 
    302   1.5   hubertf 		/* Convert unset values to internal `unset' value */
    303   1.5   hubertf 		if (edchars.erase == vdisable_c)
    304   1.5   hubertf 			edchars.erase = -1;
    305   1.5   hubertf 		if (edchars.kill == vdisable_c)
    306   1.5   hubertf 			edchars.kill = -1;
    307   1.5   hubertf 		if (edchars.intr == vdisable_c)
    308   1.5   hubertf 			edchars.intr = -1;
    309   1.5   hubertf 		if (edchars.quit == vdisable_c)
    310   1.5   hubertf 			edchars.quit = -1;
    311   1.5   hubertf 		if (edchars.eof == vdisable_c)
    312   1.5   hubertf 			edchars.eof = -1;
    313   1.5   hubertf 		if (edchars.werase == vdisable_c)
    314   1.5   hubertf 			edchars.werase = -1;
    315   1.1       jtc 		if (memcmp(&edchars, &oldchars, sizeof(edchars)) != 0) {
    316   1.1       jtc #ifdef EMACS
    317   1.1       jtc 			x_emacs_keys(&edchars);
    318   1.1       jtc #endif
    319   1.1       jtc 		}
    320   1.5   hubertf 	} else {
    321   1.1       jtc 		/* TF_WAIT doesn't seem to be necessary when leaving xmode */
    322   1.1       jtc 		set_tty(tty_fd, &tty_state, TF_NONE);
    323   1.5   hubertf 	}
    324   1.1       jtc 
    325   1.1       jtc 	return prev;
    326   1.1       jtc }
    327   1.1       jtc 
    328   1.1       jtc /* NAME:
    329   1.1       jtc  *      promptlen - calculate the length of PS1 etc.
    330   1.1       jtc  *
    331   1.1       jtc  * DESCRIPTION:
    332   1.1       jtc  *      This function is based on a fix from guy (at) demon.co.uk
    333   1.1       jtc  *      It fixes a bug in that if PS1 contains '!', the length
    334   1.1       jtc  *      given by strlen() is probably wrong.
    335   1.1       jtc  *
    336   1.1       jtc  * RETURN VALUE:
    337   1.1       jtc  *      length
    338   1.1       jtc  */
    339   1.1       jtc int
    340   1.1       jtc promptlen(cp, spp)
    341   1.1       jtc     const char  *cp;
    342   1.1       jtc     const char **spp;
    343   1.1       jtc {
    344   1.1       jtc     int count = 0;
    345   1.1       jtc     const char *sp = cp;
    346   1.2       tls     char delimiter = 0;
    347   1.2       tls     int indelimit = 0;
    348   1.2       tls 
    349   1.2       tls     /* Undocumented AT&T ksh feature:
    350   1.2       tls      * If the second char in the prompt string is \r then the first char
    351   1.2       tls      * is taken to be a non-printing delimiter and any chars between two
    352   1.2       tls      * instances of the delimiter are not considered to be part of the
    353   1.2       tls      * prompt length
    354   1.2       tls      */
    355   1.2       tls     if (*cp && cp[1] == '\r') {
    356   1.2       tls 	delimiter = *cp;
    357   1.2       tls 	cp += 2;
    358   1.2       tls     }
    359   1.1       jtc 
    360   1.2       tls     for (; *cp; cp++) {
    361   1.2       tls 	if (indelimit && *cp != delimiter)
    362   1.2       tls 	    ;
    363   1.2       tls 	else if (*cp == '\n' || *cp == '\r') {
    364   1.1       jtc 	    count = 0;
    365   1.2       tls 	    sp = cp + 1;
    366   1.1       jtc 	} else if (*cp == '\t') {
    367   1.1       jtc 	    count = (count | 7) + 1;
    368   1.1       jtc 	} else if (*cp == '\b') {
    369   1.1       jtc 	    if (count > 0)
    370   1.1       jtc 		count--;
    371   1.2       tls 	} else if (*cp == delimiter)
    372   1.2       tls 	    indelimit = !indelimit;
    373   1.1       jtc 	else
    374   1.1       jtc 	    count++;
    375   1.1       jtc     }
    376   1.1       jtc     if (spp)
    377   1.1       jtc 	*spp = sp;
    378   1.1       jtc     return count;
    379   1.1       jtc }
    380   1.1       jtc 
    381   1.1       jtc void
    382   1.1       jtc set_editmode(ed)
    383   1.1       jtc 	const char *ed;
    384   1.1       jtc {
    385   1.1       jtc 	static const enum sh_flag edit_flags[] = {
    386   1.1       jtc #ifdef EMACS
    387   1.1       jtc 			FEMACS, FGMACS,
    388   1.1       jtc #endif
    389   1.1       jtc #ifdef VI
    390   1.1       jtc 			FVI,
    391   1.1       jtc #endif
    392   1.1       jtc 		    };
    393   1.1       jtc 	char *rcp;
    394   1.1       jtc 	int i;
    395   1.1       jtc 
    396   1.1       jtc 	if ((rcp = ksh_strrchr_dirsep(ed)))
    397   1.1       jtc 		ed = ++rcp;
    398   1.1       jtc 	for (i = 0; i < NELEM(edit_flags); i++)
    399   1.1       jtc 		if (strstr(ed, options[(int) edit_flags[i]].name)) {
    400   1.1       jtc 			change_flag(edit_flags[i], OF_SPECIAL, 1);
    401   1.1       jtc 			return;
    402   1.1       jtc 		}
    403   1.1       jtc }
    404   1.1       jtc 
    405   1.1       jtc /* ------------------------------------------------------------------------- */
    406   1.1       jtc /*           Misc common code for vi/emacs				     */
    407   1.1       jtc 
    408   1.1       jtc /* Handle the commenting/uncommenting of a line.
    409   1.1       jtc  * Returns:
    410   1.1       jtc  *	1 if a carriage return is indicated (comment added)
    411   1.1       jtc  *	0 if no return (comment removed)
    412   1.1       jtc  *	-1 if there is an error (not enough room for comment chars)
    413   1.1       jtc  * If successful, *lenp contains the new length.  Note: cursor should be
    414   1.1       jtc  * moved to the start of the line after (un)commenting.
    415   1.1       jtc  */
    416   1.1       jtc int
    417   1.1       jtc x_do_comment(buf, bsize, lenp)
    418   1.1       jtc 	char *buf;
    419   1.1       jtc 	int bsize;
    420   1.1       jtc 	int *lenp;
    421   1.1       jtc {
    422   1.1       jtc 	int i, j;
    423   1.1       jtc 	int len = *lenp;
    424   1.1       jtc 
    425   1.1       jtc 	if (len == 0)
    426   1.1       jtc 		return 1; /* somewhat arbitrary - it's what at&t ksh does */
    427   1.1       jtc 
    428   1.1       jtc 	/* Already commented? */
    429   1.1       jtc 	if (buf[0] == '#') {
    430   1.1       jtc 		int saw_nl = 0;
    431   1.1       jtc 
    432   1.1       jtc 		for (j = 0, i = 1; i < len; i++) {
    433   1.1       jtc 			if (!saw_nl || buf[i] != '#')
    434   1.1       jtc 				buf[j++] = buf[i];
    435   1.1       jtc 			saw_nl = buf[i] == '\n';
    436   1.1       jtc 		}
    437   1.1       jtc 		*lenp = j;
    438   1.1       jtc 		return 0;
    439   1.1       jtc 	} else {
    440   1.1       jtc 		int n = 1;
    441   1.1       jtc 
    442   1.1       jtc 		/* See if there's room for the #'s - 1 per \n */
    443   1.1       jtc 		for (i = 0; i < len; i++)
    444   1.1       jtc 			if (buf[i] == '\n')
    445   1.1       jtc 				n++;
    446   1.1       jtc 		if (len + n >= bsize)
    447   1.1       jtc 			return -1;
    448   1.1       jtc 		/* Now add them... */
    449   1.1       jtc 		for (i = len, j = len + n; --i >= 0; ) {
    450   1.1       jtc 			if (buf[i] == '\n')
    451   1.1       jtc 				buf[--j] = '#';
    452   1.1       jtc 			buf[--j] = buf[i];
    453   1.1       jtc 		}
    454   1.1       jtc 		buf[0] = '#';
    455   1.1       jtc 		*lenp += n;
    456   1.1       jtc 		return 1;
    457   1.1       jtc 	}
    458   1.1       jtc }
    459   1.1       jtc 
    460   1.1       jtc /* ------------------------------------------------------------------------- */
    461   1.1       jtc /*           Common file/command completion code for vi/emacs	             */
    462   1.1       jtc 
    463   1.1       jtc 
    464   1.1       jtc static char	*add_glob ARGS((const char *str, int slen));
    465   1.1       jtc static void	glob_table ARGS((const char *pat, XPtrV *wp, struct table *tp));
    466   1.1       jtc static void	glob_path ARGS((int flags, const char *pat, XPtrV *wp,
    467   1.1       jtc 				const char *path));
    468   1.1       jtc 
    469   1.1       jtc #if 0 /* not used... */
    470   1.1       jtc int	x_complete_word ARGS((const char *str, int slen, int is_command,
    471   1.1       jtc 			      int *multiple, char **ret));
    472   1.1       jtc int
    473   1.1       jtc x_complete_word(str, slen, is_command, nwordsp, ret)
    474   1.1       jtc 	const char *str;
    475   1.1       jtc 	int slen;
    476   1.1       jtc 	int is_command;
    477   1.1       jtc 	int *nwordsp;
    478   1.1       jtc 	char **ret;
    479   1.1       jtc {
    480   1.1       jtc 	int nwords;
    481   1.1       jtc 	int prefix_len;
    482   1.1       jtc 	char **words;
    483   1.1       jtc 
    484   1.1       jtc 	nwords = (is_command ? x_command_glob : x_file_glob)(XCF_FULLPATH,
    485   1.1       jtc 				str, slen, &words);
    486   1.1       jtc 	*nwordsp = nwords;
    487   1.1       jtc 	if (nwords == 0) {
    488   1.1       jtc 		*ret = (char *) 0;
    489   1.1       jtc 		return -1;
    490   1.1       jtc 	}
    491   1.1       jtc 
    492   1.1       jtc 	prefix_len = x_longest_prefix(nwords, words);
    493   1.1       jtc 	*ret = str_nsave(words[0], prefix_len, ATEMP);
    494   1.1       jtc 	x_free_words(nwords, words);
    495   1.1       jtc 	return prefix_len;
    496   1.1       jtc }
    497   1.1       jtc #endif /* 0 */
    498   1.1       jtc 
    499   1.1       jtc void
    500   1.1       jtc x_print_expansions(nwords, words, is_command)
    501   1.1       jtc 	int nwords;
    502   1.1       jtc 	char *const *words;
    503   1.1       jtc 	int is_command;
    504   1.1       jtc {
    505   1.1       jtc 	int use_copy = 0;
    506   1.1       jtc 	int prefix_len;
    507   1.1       jtc 	XPtrV l;
    508   1.1       jtc 
    509   1.1       jtc 	/* Check if all matches are in the same directory (in this
    510   1.8    provos 	 * case, we want to omit the directory name)
    511   1.1       jtc 	 */
    512   1.1       jtc 	if (!is_command
    513   1.1       jtc 	    && (prefix_len = x_longest_prefix(nwords, words)) > 0)
    514   1.1       jtc 	{
    515   1.1       jtc 		int i;
    516   1.1       jtc 
    517   1.1       jtc 		/* Special case for 1 match (prefix is whole word) */
    518   1.1       jtc 		if (nwords == 1)
    519   1.1       jtc 			prefix_len = x_basename(words[0], (char *) 0);
    520   1.1       jtc 		/* Any (non-trailing) slashes in non-common word suffixes? */
    521   1.1       jtc 		for (i = 0; i < nwords; i++)
    522   1.1       jtc 			if (x_basename(words[i] + prefix_len, (char *) 0)
    523   1.1       jtc 							> prefix_len)
    524   1.1       jtc 				break;
    525   1.1       jtc 		/* All in same directory? */
    526   1.1       jtc 		if (i == nwords) {
    527   1.1       jtc 			while (prefix_len > 0
    528   1.1       jtc 			       && !ISDIRSEP(words[0][prefix_len - 1]))
    529   1.1       jtc 				prefix_len--;
    530   1.1       jtc 			use_copy = 1;
    531   1.1       jtc 			XPinit(l, nwords + 1);
    532   1.1       jtc 			for (i = 0; i < nwords; i++)
    533   1.1       jtc 				XPput(l, words[i] + prefix_len);
    534   1.1       jtc 			XPput(l, (char *) 0);
    535   1.1       jtc 		}
    536   1.1       jtc 	}
    537   1.1       jtc 
    538   1.1       jtc 	/*
    539   1.1       jtc 	 * Enumerate expansions
    540   1.1       jtc 	 */
    541   1.1       jtc 	x_putc('\r');
    542   1.1       jtc 	x_putc('\n');
    543   1.8    provos 	pr_list(use_copy ? (char **) XPptrv(l) : words);
    544   1.1       jtc 
    545   1.1       jtc 	if (use_copy)
    546   1.1       jtc 		XPfree(l); /* not x_free_words() */
    547   1.1       jtc }
    548   1.1       jtc 
    549   1.1       jtc /*
    550   1.1       jtc  *  Do file globbing:
    551   1.1       jtc  *	- appends * to (copy of) str if no globbing chars found
    552   1.1       jtc  *	- does expansion, checks for no match, etc.
    553   1.1       jtc  *	- sets *wordsp to array of matching strings
    554   1.1       jtc  *	- returns number of matching strings
    555   1.1       jtc  */
    556   1.1       jtc static int
    557   1.1       jtc x_file_glob(flags, str, slen, wordsp)
    558   1.1       jtc 	int flags;
    559   1.1       jtc 	const char *str;
    560   1.1       jtc 	int slen;
    561   1.1       jtc 	char ***wordsp;
    562   1.1       jtc {
    563   1.1       jtc 	char *toglob;
    564   1.1       jtc 	char **words;
    565   1.6  jdolecek 	int nwords, i, idx, escaping;
    566   1.1       jtc 	XPtrV w;
    567   1.1       jtc 	struct source *s, *sold;
    568   1.1       jtc 
    569   1.1       jtc 	if (slen < 0)
    570   1.1       jtc 		return 0;
    571   1.1       jtc 
    572   1.1       jtc 	toglob = add_glob(str, slen);
    573   1.1       jtc 
    574   1.6  jdolecek 	/* remove all escaping backward slashes */
    575   1.6  jdolecek 	escaping = 0;
    576   1.6  jdolecek 	for(i = 0, idx = 0; toglob[i]; i++) {
    577   1.6  jdolecek 		if (toglob[i] == '\\' && !escaping) {
    578   1.6  jdolecek 			escaping = 1;
    579   1.6  jdolecek 			continue;
    580   1.6  jdolecek 		}
    581   1.6  jdolecek 
    582   1.6  jdolecek 		toglob[idx] = toglob[i];
    583   1.6  jdolecek 		idx++;
    584   1.6  jdolecek 		if (escaping) escaping = 0;
    585   1.6  jdolecek 	}
    586   1.6  jdolecek 	toglob[idx] = '\0';
    587   1.6  jdolecek 
    588   1.1       jtc 	/*
    589   1.1       jtc 	 * Convert "foo*" (toglob) to an array of strings (words)
    590   1.1       jtc 	 */
    591   1.1       jtc 	sold = source;
    592   1.1       jtc 	s = pushs(SWSTR, ATEMP);
    593   1.1       jtc 	s->start = s->str = toglob;
    594   1.1       jtc 	source = s;
    595   1.1       jtc 	if (yylex(ONEWORD) != LWORD) {
    596   1.1       jtc 		source = sold;
    597   1.1       jtc 		internal_errorf(0, "fileglob: substitute error");
    598   1.1       jtc 		return 0;
    599   1.1       jtc 	}
    600   1.1       jtc 	source = sold;
    601   1.1       jtc 	XPinit(w, 32);
    602   1.1       jtc 	expand(yylval.cp, &w, DOGLOB|DOTILDE|DOMARKDIRS);
    603   1.1       jtc 	XPput(w, NULL);
    604   1.1       jtc 	words = (char **) XPclose(w);
    605   1.1       jtc 
    606   1.1       jtc 	for (nwords = 0; words[nwords]; nwords++)
    607   1.1       jtc 		;
    608   1.1       jtc 	if (nwords == 1) {
    609   1.1       jtc 		struct stat statb;
    610   1.1       jtc 
    611   1.1       jtc 		/* Check if globbing failed (returned glob pattern),
    612   1.1       jtc 		 * but be careful (E.g. toglob == "ab*" when the file
    613   1.1       jtc 		 * "ab*" exists is not an error).
    614   1.1       jtc 		 * Also, check for empty result - happens if we tried
    615   1.1       jtc 		 * to glob something which evaluated to an empty
    616   1.1       jtc 		 * string (e.g., "$FOO" when there is no FOO, etc).
    617   1.1       jtc 		 */
    618   1.1       jtc 		if ((strcmp(words[0], toglob) == 0
    619   1.1       jtc 		     && stat(words[0], &statb) < 0)
    620   1.1       jtc 		    || words[0][0] == '\0')
    621   1.1       jtc 		{
    622   1.1       jtc 			x_free_words(nwords, words);
    623   1.1       jtc 			nwords = 0;
    624   1.1       jtc 		}
    625   1.1       jtc 	}
    626   1.1       jtc 	afree(toglob, ATEMP);
    627   1.1       jtc 
    628   1.1       jtc 	*wordsp = nwords ? words : (char **) 0;
    629   1.1       jtc 
    630   1.1       jtc 	return nwords;
    631   1.1       jtc }
    632   1.1       jtc 
    633   1.1       jtc /* Data structure used in x_command_glob() */
    634   1.1       jtc struct path_order_info {
    635   1.1       jtc 	char *word;
    636   1.1       jtc 	int base;
    637   1.1       jtc 	int path_order;
    638   1.1       jtc };
    639   1.1       jtc 
    640   1.1       jtc /* Compare routine used in x_command_glob() */
    641   1.1       jtc static int
    642   1.1       jtc path_order_cmp(aa, bb)
    643   1.1       jtc 	const void *aa;
    644   1.1       jtc 	const void *bb;
    645   1.1       jtc {
    646   1.1       jtc 	const struct path_order_info *a = (const struct path_order_info *) aa;
    647   1.1       jtc 	const struct path_order_info *b = (const struct path_order_info *) bb;
    648   1.1       jtc 	int t;
    649   1.1       jtc 
    650   1.1       jtc 	t = FILECMP(a->word + a->base, b->word + b->base);
    651   1.1       jtc 	return t ? t : a->path_order - b->path_order;
    652   1.1       jtc }
    653   1.1       jtc 
    654   1.1       jtc static int
    655   1.1       jtc x_command_glob(flags, str, slen, wordsp)
    656   1.1       jtc 	int flags;
    657   1.1       jtc 	const char *str;
    658   1.1       jtc 	int slen;
    659   1.1       jtc 	char ***wordsp;
    660   1.1       jtc {
    661   1.1       jtc 	char *toglob;
    662   1.1       jtc 	char *pat;
    663   1.1       jtc 	char *fpath;
    664   1.1       jtc 	int nwords;
    665   1.1       jtc 	XPtrV w;
    666   1.1       jtc 	struct block *l;
    667   1.1       jtc 
    668   1.1       jtc 	if (slen < 0)
    669   1.1       jtc 		return 0;
    670   1.1       jtc 
    671   1.1       jtc 	toglob = add_glob(str, slen);
    672   1.1       jtc 
    673   1.1       jtc 	/* Convert "foo*" (toglob) to a pattern for future use */
    674   1.1       jtc 	pat = evalstr(toglob, DOPAT|DOTILDE);
    675   1.1       jtc 	afree(toglob, ATEMP);
    676   1.1       jtc 
    677   1.1       jtc 	XPinit(w, 32);
    678   1.1       jtc 
    679   1.1       jtc 	glob_table(pat, &w, &keywords);
    680   1.1       jtc 	glob_table(pat, &w, &aliases);
    681   1.1       jtc 	glob_table(pat, &w, &builtins);
    682   1.1       jtc 	for (l = e->loc; l; l = l->next)
    683   1.1       jtc 		glob_table(pat, &w, &l->funs);
    684   1.1       jtc 
    685   1.1       jtc 	glob_path(flags, pat, &w, path);
    686   1.1       jtc 	if ((fpath = str_val(global("FPATH"))) != null)
    687   1.1       jtc 		glob_path(flags, pat, &w, fpath);
    688   1.1       jtc 
    689   1.1       jtc 	nwords = XPsize(w);
    690   1.1       jtc 
    691   1.1       jtc 	if (!nwords) {
    692   1.1       jtc 		*wordsp = (char **) 0;
    693   1.1       jtc 		XPfree(w);
    694   1.1       jtc 		return 0;
    695   1.1       jtc 	}
    696   1.1       jtc 
    697   1.1       jtc 	/* Sort entries */
    698   1.1       jtc 	if (flags & XCF_FULLPATH) {
    699   1.1       jtc 		/* Sort by basename, then path order */
    700   1.1       jtc 		struct path_order_info *info;
    701   1.1       jtc 		struct path_order_info *last_info = 0;
    702   1.1       jtc 		char **words = (char **) XPptrv(w);
    703   1.1       jtc 		int path_order = 0;
    704   1.1       jtc 		int i;
    705   1.1       jtc 
    706   1.1       jtc 		info = (struct path_order_info *)
    707   1.1       jtc 			alloc(sizeof(struct path_order_info) * nwords, ATEMP);
    708   1.1       jtc 		for (i = 0; i < nwords; i++) {
    709   1.1       jtc 			info[i].word = words[i];
    710   1.1       jtc 			info[i].base = x_basename(words[i], (char *) 0);
    711   1.1       jtc 			if (!last_info || info[i].base != last_info->base
    712   1.1       jtc 			    || FILENCMP(words[i],
    713   1.1       jtc 					last_info->word, info[i].base) != 0)
    714   1.1       jtc 			{
    715   1.1       jtc 				last_info = &info[i];
    716   1.1       jtc 				path_order++;
    717   1.1       jtc 			}
    718   1.1       jtc 			info[i].path_order = path_order;
    719   1.1       jtc 		}
    720   1.1       jtc 		qsort(info, nwords, sizeof(struct path_order_info),
    721   1.1       jtc 			path_order_cmp);
    722   1.1       jtc 		for (i = 0; i < nwords; i++)
    723   1.1       jtc 			words[i] = info[i].word;
    724   1.1       jtc 		afree((void *) info, ATEMP);
    725   1.1       jtc 	} else {
    726   1.1       jtc 		/* Sort and remove duplicate entries */
    727   1.1       jtc 		char **words = (char **) XPptrv(w);
    728   1.1       jtc 		int i, j;
    729   1.1       jtc 
    730   1.1       jtc 		qsortp(XPptrv(w), (size_t) nwords, xstrcmp);
    731   1.1       jtc 
    732   1.1       jtc 		for (i = j = 0; i < nwords - 1; i++) {
    733   1.1       jtc 			if (strcmp(words[i], words[i + 1]))
    734   1.1       jtc 				words[j++] = words[i];
    735   1.1       jtc 			else
    736   1.1       jtc 				afree(words[i], ATEMP);
    737   1.1       jtc 		}
    738   1.1       jtc 		words[j++] = words[i];
    739   1.1       jtc 		nwords = j;
    740   1.1       jtc 		w.cur = (void **) &words[j];
    741   1.1       jtc 	}
    742   1.1       jtc 
    743   1.1       jtc 	XPput(w, NULL);
    744   1.1       jtc 	*wordsp = (char **) XPclose(w);
    745   1.1       jtc 
    746   1.1       jtc 	return nwords;
    747   1.1       jtc }
    748   1.1       jtc 
    749   1.8    provos #define IS_WORDC(c)	!( ctype(c, C_LEX1) || (c) == '\'' || (c) == '"'  \
    750   1.8    provos 			    || (c) == '`' || (c) == '=' || (c) == ':' )
    751   1.1       jtc 
    752   1.1       jtc static int
    753   1.1       jtc x_locate_word(buf, buflen, pos, startp, is_commandp)
    754   1.1       jtc 	const char *buf;
    755   1.1       jtc 	int buflen;
    756   1.1       jtc 	int pos;
    757   1.1       jtc 	int *startp;
    758   1.1       jtc 	int *is_commandp;
    759   1.1       jtc {
    760   1.1       jtc 	int p;
    761   1.1       jtc 	int start, end;
    762   1.1       jtc 
    763   1.1       jtc 	/* Bad call?  Probably should report error */
    764   1.1       jtc 	if (pos < 0 || pos > buflen) {
    765   1.1       jtc 		*startp = pos;
    766   1.1       jtc 		*is_commandp = 0;
    767   1.1       jtc 		return 0;
    768   1.1       jtc 	}
    769   1.5   hubertf 	/* The case where pos == buflen happens to take care of itself... */
    770   1.1       jtc 
    771   1.1       jtc 	start = pos;
    772   1.1       jtc 	/* Keep going backwards to start of word (has effect of allowing
    773   1.1       jtc 	 * one blank after the end of a word)
    774   1.1       jtc 	 */
    775   1.6  jdolecek 	for (; (start > 0 && IS_WORDC(buf[start - 1]))
    776   1.6  jdolecek 		|| (start > 1 && buf[start-2] == '\\'); start--)
    777   1.1       jtc 		;
    778   1.1       jtc 	/* Go forwards to end of word */
    779   1.6  jdolecek 	for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
    780  1.12       wiz 		if (buf[end] == '\\' && (end+1) < buflen)
    781   1.6  jdolecek 			end++;
    782   1.6  jdolecek 	}
    783   1.1       jtc 
    784   1.1       jtc 	if (is_commandp) {
    785   1.1       jtc 		int iscmd;
    786   1.1       jtc 
    787   1.1       jtc 		/* Figure out if this is a command */
    788   1.4  christos 		for (p = start - 1; p >= 0 && isspace((unsigned char)buf[p]); p--)
    789   1.1       jtc 			;
    790   1.8    provos 		iscmd = p < 0 || strchr(";|&()`", buf[p]);
    791   1.1       jtc 		if (iscmd) {
    792   1.1       jtc 			/* If command has a /, path, etc. is not searched;
    793   1.1       jtc 			 * only current directory is searched, which is just
    794   1.1       jtc 			 * like file globbing.
    795   1.1       jtc 			 */
    796   1.1       jtc 			for (p = start; p < end; p++)
    797   1.1       jtc 				if (ISDIRSEP(buf[p]))
    798   1.1       jtc 					break;
    799   1.1       jtc 			iscmd = p == end;
    800   1.1       jtc 		}
    801   1.1       jtc 		*is_commandp = iscmd;
    802   1.1       jtc 	}
    803   1.1       jtc 
    804   1.1       jtc 	*startp = start;
    805   1.1       jtc 
    806   1.1       jtc 	return end - start;
    807   1.1       jtc }
    808   1.1       jtc 
    809   1.1       jtc int
    810   1.1       jtc x_cf_glob(flags, buf, buflen, pos, startp, endp, wordsp, is_commandp)
    811   1.1       jtc 	int flags;
    812   1.1       jtc 	const char *buf;
    813   1.1       jtc 	int buflen;
    814   1.1       jtc 	int pos;
    815   1.1       jtc 	int *startp;
    816   1.1       jtc 	int *endp;
    817   1.1       jtc 	char ***wordsp;
    818   1.1       jtc 	int *is_commandp;
    819   1.1       jtc {
    820   1.1       jtc 	int len;
    821   1.1       jtc 	int nwords;
    822   1.1       jtc 	char **words;
    823   1.1       jtc 	int is_command;
    824   1.1       jtc 
    825   1.1       jtc 	len = x_locate_word(buf, buflen, pos, startp, &is_command);
    826   1.1       jtc 	if (!(flags & XCF_COMMAND))
    827   1.1       jtc 		is_command = 0;
    828   1.1       jtc 	/* Don't do command globing on zero length strings - it takes too
    829   1.1       jtc 	 * long and isn't very useful.  File globs are more likely to be
    830   1.1       jtc 	 * useful, so allow these.
    831   1.1       jtc 	 */
    832   1.1       jtc 	if (len == 0 && is_command)
    833   1.1       jtc 		return 0;
    834   1.1       jtc 
    835   1.1       jtc 	nwords = (is_command ? x_command_glob : x_file_glob)(flags,
    836   1.1       jtc 				    buf + *startp, len, &words);
    837   1.1       jtc 	if (nwords == 0) {
    838   1.1       jtc 		*wordsp = (char **) 0;
    839   1.1       jtc 		return 0;
    840   1.1       jtc 	}
    841   1.1       jtc 
    842   1.1       jtc 	if (is_commandp)
    843   1.1       jtc 		*is_commandp = is_command;
    844   1.1       jtc 	*wordsp = words;
    845   1.1       jtc 	*endp = *startp + len;
    846   1.1       jtc 
    847   1.1       jtc 	return nwords;
    848   1.1       jtc }
    849   1.1       jtc 
    850   1.7       sjg 
    851   1.7       sjg static char *
    852   1.7       sjg find_match(const char *s, int have)
    853   1.7       sjg {
    854   1.7       sjg 	int     want = 0;
    855   1.7       sjg 	int     nest = 1;
    856   1.7       sjg 	int     c;
    857   1.7       sjg 
    858   1.7       sjg 	switch (have) {
    859   1.7       sjg 	case '(':	want = ')'; break;
    860   1.7       sjg 	case '{':	want = '}'; break;
    861   1.7       sjg 	case '[':	want = ']'; break;
    862   1.7       sjg 	case '\'':
    863   1.7       sjg 	case '"':	want = have; break;
    864   1.7       sjg 	}
    865   1.7       sjg 	if (want == 0 || s == NULL)
    866   1.7       sjg 		return NULL;
    867   1.7       sjg 
    868   1.7       sjg 	while (nest > 0 && (c = *s)) {
    869   1.7       sjg 		if (c == '\\') {
    870   1.7       sjg 			s++;
    871   1.7       sjg 			s++;
    872   1.7       sjg 			continue;
    873   1.7       sjg 		}
    874   1.7       sjg 		if (c == want)
    875   1.7       sjg 			nest--;
    876   1.7       sjg 		else if (c == have)
    877   1.7       sjg 			nest++;
    878   1.7       sjg 		if (nest > 0)
    879   1.7       sjg 			s++;
    880   1.7       sjg 	}
    881   1.7       sjg 	return (nest == 0) ? (char *) s : NULL;
    882   1.7       sjg }
    883   1.7       sjg 
    884   1.1       jtc /* Given a string, copy it and possibly add a '*' to the end.  The
    885   1.1       jtc  * new string is returned.
    886   1.1       jtc  */
    887   1.1       jtc static char *
    888   1.1       jtc add_glob(str, slen)
    889   1.1       jtc 	const char *str;
    890   1.1       jtc 	int slen;
    891   1.1       jtc {
    892   1.1       jtc 	char *toglob;
    893   1.1       jtc 	char *s;
    894   1.2       tls 	bool_t saw_slash = FALSE;
    895   1.1       jtc 
    896   1.1       jtc 	if (slen < 0)
    897   1.1       jtc 		return (char *) 0;
    898   1.1       jtc 
    899   1.1       jtc 	toglob = str_nsave(str, slen + 1, ATEMP); /* + 1 for "*" */
    900   1.1       jtc 	toglob[slen] = '\0';
    901   1.1       jtc 
    902   1.1       jtc 	/*
    903   1.7       sjg 	 * If the pathname contains a wildcard (an unquoted '*', '?',
    904   1.7       sjg 	 * or '[') or parameter expansion ('$') with nothing following
    905   1.7       sjg 	 * it, or a ~username with no trailing slash, then it is
    906   1.7       sjg 	 * globbed based on that value (i.e., without the appended
    907   1.7       sjg 	 * '*').
    908   1.1       jtc 	 */
    909   1.1       jtc 	for (s = toglob; *s; s++) {
    910   1.1       jtc 		if (*s == '\\' && s[1])
    911   1.1       jtc 			s++;
    912   1.7       sjg 		else if (*s == '*' || *s == '[' || *s == '?'
    913   1.1       jtc 			 || (s[1] == '(' /*)*/ && strchr("*+?@!", *s)))
    914   1.1       jtc 			break;
    915   1.2       tls 		else if (ISDIRSEP(*s))
    916   1.2       tls 			saw_slash = TRUE;
    917   1.7       sjg 		else if (*s == '$') {
    918   1.7       sjg 			if (*++s == '{') {
    919   1.7       sjg 				char *cp;
    920   1.7       sjg 
    921   1.7       sjg 				if ((cp = find_match(&s[1], '{')))
    922   1.7       sjg 					s = ++cp;
    923   1.7       sjg 			}
    924   1.7       sjg 			if (*s)
    925   1.7       sjg 				s += strcspn(s,
    926   1.7       sjg 					".,/?-<>[]{}()'\";:\\|=+*&^%$#@!`~");
    927   1.7       sjg 			if (!*s)
    928   1.7       sjg 				return toglob;
    929   1.7       sjg 		}
    930   1.1       jtc 	}
    931   1.2       tls 	if (!*s && (*toglob != '~' || saw_slash)) {
    932   1.1       jtc 		toglob[slen] = '*';
    933   1.1       jtc 		toglob[slen + 1] = '\0';
    934   1.1       jtc 	}
    935   1.1       jtc 
    936   1.1       jtc 	return toglob;
    937   1.1       jtc }
    938   1.1       jtc 
    939   1.1       jtc /*
    940   1.1       jtc  * Find longest common prefix
    941   1.1       jtc  */
    942   1.1       jtc int
    943   1.1       jtc x_longest_prefix(nwords, words)
    944   1.1       jtc 	int nwords;
    945   1.1       jtc 	char *const *words;
    946   1.1       jtc {
    947   1.1       jtc 	int i, j;
    948   1.1       jtc 	int prefix_len;
    949   1.1       jtc 	char *p;
    950   1.1       jtc 
    951   1.1       jtc 	if (nwords <= 0)
    952   1.1       jtc 		return 0;
    953   1.1       jtc 
    954   1.1       jtc 	prefix_len = strlen(words[0]);
    955   1.1       jtc 	for (i = 1; i < nwords; i++)
    956   1.1       jtc 		for (j = 0, p = words[i]; j < prefix_len; j++)
    957   1.1       jtc 			if (FILECHCONV(p[j]) != FILECHCONV(words[0][j])) {
    958   1.1       jtc 				prefix_len = j;
    959   1.1       jtc 				break;
    960   1.1       jtc 			}
    961   1.1       jtc 	return prefix_len;
    962   1.1       jtc }
    963   1.1       jtc 
    964   1.1       jtc void
    965   1.1       jtc x_free_words(nwords, words)
    966   1.1       jtc 	int nwords;
    967   1.1       jtc 	char **words;
    968   1.1       jtc {
    969   1.1       jtc 	int i;
    970   1.1       jtc 
    971   1.1       jtc 	for (i = 0; i < nwords; i++)
    972   1.1       jtc 		if (words[i])
    973   1.1       jtc 			afree(words[i], ATEMP);
    974   1.1       jtc 	afree(words, ATEMP);
    975   1.1       jtc }
    976   1.1       jtc 
    977   1.1       jtc /* Return the offset of the basename of string s (which ends at se - need not
    978   1.1       jtc  * be null terminated).  Trailing slashes are ignored.  If s is just a slash,
    979   1.1       jtc  * then the offset is 0 (actually, length - 1).
    980   1.1       jtc  *	s		Return
    981   1.1       jtc  *	/etc		1
    982   1.1       jtc  *	/etc/		1
    983   1.1       jtc  *	/etc//		1
    984   1.1       jtc  *	/etc/fo		5
    985   1.1       jtc  *	foo		0
    986   1.1       jtc  *	///		2
    987   1.1       jtc  *			0
    988   1.1       jtc  */
    989   1.1       jtc int
    990   1.1       jtc x_basename(s, se)
    991   1.1       jtc 	const char *s;
    992   1.1       jtc 	const char *se;
    993   1.1       jtc {
    994   1.1       jtc 	const char *p;
    995   1.1       jtc 
    996   1.1       jtc 	if (se == (char *) 0)
    997   1.1       jtc 		se = s + strlen(s);
    998   1.1       jtc 	if (s == se)
    999   1.1       jtc 		return 0;
   1000   1.1       jtc 
   1001   1.1       jtc 	/* Skip trailing slashes */
   1002   1.1       jtc 	for (p = se - 1; p > s && ISDIRSEP(*p); p--)
   1003   1.1       jtc 		;
   1004   1.1       jtc 	for (; p > s && !ISDIRSEP(*p); p--)
   1005   1.1       jtc 		;
   1006   1.1       jtc 	if (ISDIRSEP(*p) && p + 1 < se)
   1007   1.1       jtc 		p++;
   1008   1.1       jtc 
   1009   1.1       jtc 	return p - s;
   1010   1.1       jtc }
   1011   1.1       jtc 
   1012   1.1       jtc /*
   1013   1.1       jtc  *  Apply pattern matching to a table: all table entries that match a pattern
   1014   1.1       jtc  * are added to wp.
   1015   1.1       jtc  */
   1016   1.1       jtc static void
   1017   1.1       jtc glob_table(pat, wp, tp)
   1018   1.1       jtc 	const char *pat;
   1019   1.1       jtc 	XPtrV *wp;
   1020   1.1       jtc 	struct table *tp;
   1021   1.1       jtc {
   1022   1.1       jtc 	struct tstate ts;
   1023   1.1       jtc 	struct tbl *te;
   1024   1.1       jtc 
   1025   1.1       jtc 	for (twalk(&ts, tp); (te = tnext(&ts)); ) {
   1026   1.1       jtc 		if (gmatch(te->name, pat, FALSE))
   1027   1.1       jtc 			XPput(*wp, str_save(te->name, ATEMP));
   1028   1.1       jtc 	}
   1029   1.1       jtc }
   1030   1.1       jtc 
   1031   1.1       jtc static void
   1032   1.1       jtc glob_path(flags, pat, wp, path)
   1033   1.1       jtc 	int flags;
   1034   1.1       jtc 	const char *pat;
   1035   1.1       jtc 	XPtrV *wp;
   1036   1.1       jtc 	const char *path;
   1037   1.1       jtc {
   1038   1.1       jtc 	const char *sp, *p;
   1039   1.1       jtc 	char *xp;
   1040   1.8    provos 	int staterr;
   1041   1.1       jtc 	int pathlen;
   1042   1.1       jtc 	int patlen;
   1043   1.1       jtc 	int oldsize, newsize, i, j;
   1044   1.1       jtc 	char **words;
   1045   1.1       jtc 	XString xs;
   1046   1.1       jtc 
   1047   1.1       jtc 	patlen = strlen(pat) + 1;
   1048   1.1       jtc 	sp = path;
   1049   1.1       jtc 	Xinit(xs, xp, patlen + 128, ATEMP);
   1050   1.1       jtc 	while (sp) {
   1051   1.1       jtc 		xp = Xstring(xs, xp);
   1052   1.1       jtc 		if (!(p = strchr(sp, PATHSEP)))
   1053   1.1       jtc 			p = sp + strlen(sp);
   1054   1.1       jtc 		pathlen = p - sp;
   1055   1.1       jtc 		if (pathlen) {
   1056   1.1       jtc 			/* Copy sp into xp, stuffing any MAGIC characters
   1057   1.1       jtc 			 * on the way
   1058   1.1       jtc 			 */
   1059   1.1       jtc 			const char *s = sp;
   1060   1.1       jtc 
   1061   1.1       jtc 			XcheckN(xs, xp, pathlen * 2);
   1062   1.1       jtc 			while (s < p) {
   1063   1.1       jtc 				if (ISMAGIC(*s))
   1064   1.1       jtc 					*xp++ = MAGIC;
   1065   1.1       jtc 				*xp++ = *s++;
   1066   1.1       jtc 			}
   1067   1.1       jtc 			*xp++ = DIRSEP;
   1068   1.1       jtc 			pathlen++;
   1069   1.1       jtc 		}
   1070   1.1       jtc 		sp = p;
   1071   1.1       jtc 		XcheckN(xs, xp, patlen);
   1072   1.1       jtc 		memcpy(xp, pat, patlen);
   1073   1.1       jtc 
   1074   1.1       jtc 		oldsize = XPsize(*wp);
   1075   1.8    provos 		glob_str(Xstring(xs, xp), wp, 1); /* mark dirs */
   1076   1.1       jtc 		newsize = XPsize(*wp);
   1077   1.1       jtc 
   1078   1.1       jtc 		/* Check that each match is executable... */
   1079   1.1       jtc 		words = (char **) XPptrv(*wp);
   1080   1.1       jtc 		for (i = j = oldsize; i < newsize; i++) {
   1081   1.8    provos 			staterr = 0;
   1082   1.8    provos 			if ((search_access(words[i], X_OK, &staterr) >= 0)
   1083   1.8    provos 			    || (staterr == EISDIR)) {
   1084   1.1       jtc 				words[j] = words[i];
   1085   1.1       jtc 				if (!(flags & XCF_FULLPATH))
   1086   1.1       jtc 					memmove(words[j], words[j] + pathlen,
   1087   1.1       jtc 						strlen(words[j] + pathlen) + 1);
   1088   1.1       jtc 				j++;
   1089   1.1       jtc 			} else
   1090   1.1       jtc 				afree(words[i], ATEMP);
   1091   1.1       jtc 		}
   1092   1.1       jtc 		wp->cur = (void **) &words[j];
   1093   1.1       jtc 
   1094   1.1       jtc 		if (!*sp++)
   1095   1.1       jtc 			break;
   1096   1.1       jtc 	}
   1097   1.1       jtc 	Xfree(xs, xp);
   1098   1.1       jtc }
   1099   1.1       jtc 
   1100   1.6  jdolecek /*
   1101   1.6  jdolecek  * if argument string contains any special characters, they will
   1102   1.6  jdolecek  * be escaped and the result will be put into edit buffer by
   1103   1.6  jdolecek  * keybinding-specific function
   1104   1.6  jdolecek  */
   1105   1.6  jdolecek int
   1106   1.6  jdolecek x_escape(s, len, putbuf_func)
   1107   1.6  jdolecek 	const char *s;
   1108   1.6  jdolecek 	size_t len;
   1109   1.6  jdolecek 	int putbuf_func ARGS((const char *s, size_t len));
   1110   1.6  jdolecek {
   1111   1.6  jdolecek 	size_t add, wlen;
   1112   1.6  jdolecek 	const char *ifs = str_val(local("IFS", 0));
   1113   1.6  jdolecek 	int rval=0;
   1114   1.6  jdolecek 
   1115   1.6  jdolecek 	for (add = 0, wlen = len; wlen - add > 0; add++) {
   1116  1.10       wiz 		if (strchr("\\$(){}*&;|<>\"'`#", s[add]) || strchr(ifs, s[add])) {
   1117   1.6  jdolecek 			if (putbuf_func(s, add) != 0) {
   1118   1.6  jdolecek 				rval = -1;
   1119   1.6  jdolecek 				break;
   1120   1.6  jdolecek 			}
   1121   1.6  jdolecek 
   1122   1.6  jdolecek 			putbuf_func("\\", 1);
   1123   1.6  jdolecek 			putbuf_func(&s[add], 1);
   1124   1.6  jdolecek 
   1125   1.6  jdolecek 			add++;
   1126   1.6  jdolecek 			wlen -= add;
   1127   1.6  jdolecek 			s += add;
   1128   1.6  jdolecek 			add = -1; /* after the increment it will go to 0 */
   1129   1.6  jdolecek 		}
   1130   1.6  jdolecek 	}
   1131   1.6  jdolecek 	if (wlen > 0 && rval == 0)
   1132   1.6  jdolecek 		rval = putbuf_func(s, wlen);
   1133   1.6  jdolecek 
   1134   1.6  jdolecek 	return (rval);
   1135   1.6  jdolecek }
   1136   1.1       jtc #endif /* EDIT */
   1137