xref: /openbsd/bin/ksh/edit.c (revision 3d8817e4)
1 /*	$OpenBSD: edit.c,v 1.34 2010/05/20 01:13:07 fgsch Exp $	*/
2 
3 /*
4  * Command line editing - common code
5  *
6  */
7 
8 #include "config.h"
9 #ifdef EDIT
10 
11 #include "sh.h"
12 #include "tty.h"
13 #define EXTERN
14 #include "edit.h"
15 #undef EXTERN
16 #include <sys/ioctl.h>
17 #include <ctype.h>
18 #include <libgen.h>
19 #include <sys/stat.h>
20 
21 
22 static void x_sigwinch(int);
23 static volatile sig_atomic_t got_sigwinch;
24 static void check_sigwinch(void);
25 
26 static int	x_file_glob(int, const char *, int, char ***);
27 static int	x_command_glob(int, const char *, int, char ***);
28 static int	x_locate_word(const char *, int, int, int *, int *);
29 
30 
31 /* Called from main */
32 void
33 x_init(void)
34 {
35 	/* set to -2 to force initial binding */
36 	edchars.erase = edchars.kill = edchars.intr = edchars.quit =
37 	    edchars.eof = -2;
38 	/* default value for deficient systems */
39 	edchars.werase = 027;	/* ^W */
40 
41 	if (setsig(&sigtraps[SIGWINCH], x_sigwinch, SS_RESTORE_ORIG|SS_SHTRAP))
42 		sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
43 	got_sigwinch = 1; /* force initial check */
44 	check_sigwinch();
45 
46 #ifdef EMACS
47 	x_init_emacs();
48 #endif /* EMACS */
49 }
50 
51 /* ARGSUSED */
52 static void
53 x_sigwinch(int sig)
54 {
55 	got_sigwinch = 1;
56 }
57 
58 static void
59 check_sigwinch(void)
60 {
61 	if (got_sigwinch) {
62 		struct winsize ws;
63 
64 		got_sigwinch = 0;
65 		if (procpid == kshpid && ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
66 			struct tbl *vp;
67 
68 			/* Do NOT export COLUMNS/LINES.  Many applications
69 			 * check COLUMNS/LINES before checking ws.ws_col/row,
70 			 * so if the app is started with C/L in the environ
71 			 * and the window is then resized, the app won't
72 			 * see the change cause the environ doesn't change.
73 			 */
74 			if (ws.ws_col) {
75 				x_cols = ws.ws_col < MIN_COLS ? MIN_COLS :
76 				    ws.ws_col;
77 
78 				if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
79 					setint(vp, (long) ws.ws_col);
80 			}
81 			if (ws.ws_row && (vp = typeset("LINES", 0, 0, 0, 0)))
82 				setint(vp, (long) ws.ws_row);
83 		}
84 	}
85 }
86 
87 /*
88  * read an edited command line
89  */
90 int
91 x_read(char *buf, size_t len)
92 {
93 	int	i;
94 
95 	x_mode(true);
96 #ifdef EMACS
97 	if (Flag(FEMACS) || Flag(FGMACS))
98 		i = x_emacs(buf, len);
99 	else
100 #endif
101 #ifdef VI
102 	if (Flag(FVI))
103 		i = x_vi(buf, len);
104 	else
105 #endif
106 		i = -1;		/* internal error */
107 	x_mode(false);
108 	check_sigwinch();
109 	return i;
110 }
111 
112 /* tty I/O */
113 
114 int
115 x_getc(void)
116 {
117 	char c;
118 	int n;
119 
120 	while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
121 		if (trap) {
122 			x_mode(false);
123 			runtraps(0);
124 			x_mode(true);
125 		}
126 	if (n != 1)
127 		return -1;
128 	return (int) (unsigned char) c;
129 }
130 
131 void
132 x_flush(void)
133 {
134 	shf_flush(shl_out);
135 }
136 
137 void
138 x_putc(int c)
139 {
140 	shf_putc(c, shl_out);
141 }
142 
143 void
144 x_puts(const char *s)
145 {
146 	while (*s != 0)
147 		shf_putc(*s++, shl_out);
148 }
149 
150 bool
151 x_mode(bool onoff)
152 {
153 	static bool	x_cur_mode;
154 	bool		prev;
155 
156 	if (x_cur_mode == onoff)
157 		return x_cur_mode;
158 	prev = x_cur_mode;
159 	x_cur_mode = onoff;
160 
161 	if (onoff) {
162 		struct termios	cb;
163 		X_chars		oldchars;
164 
165 		oldchars = edchars;
166 		cb = tty_state;
167 
168 		edchars.erase = cb.c_cc[VERASE];
169 		edchars.kill = cb.c_cc[VKILL];
170 		edchars.intr = cb.c_cc[VINTR];
171 		edchars.quit = cb.c_cc[VQUIT];
172 		edchars.eof = cb.c_cc[VEOF];
173 		edchars.werase = cb.c_cc[VWERASE];
174 		cb.c_iflag &= ~(INLCR|ICRNL);
175 		cb.c_lflag &= ~(ISIG|ICANON|ECHO);
176 		/* osf/1 processes lnext when ~icanon */
177 		cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
178 		/* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */
179 		cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
180 		cb.c_cc[VTIME] = 0;
181 		cb.c_cc[VMIN] = 1;
182 
183 		tcsetattr(tty_fd, TCSADRAIN, &cb);
184 
185 		/* Convert unset values to internal `unset' value */
186 		if (edchars.erase == _POSIX_VDISABLE)
187 			edchars.erase = -1;
188 		if (edchars.kill == _POSIX_VDISABLE)
189 			edchars.kill = -1;
190 		if (edchars.intr == _POSIX_VDISABLE)
191 			edchars.intr = -1;
192 		if (edchars.quit == _POSIX_VDISABLE)
193 			edchars.quit = -1;
194 		if (edchars.eof == _POSIX_VDISABLE)
195 			edchars.eof = -1;
196 		if (edchars.werase == _POSIX_VDISABLE)
197 			edchars.werase = -1;
198 		if (memcmp(&edchars, &oldchars, sizeof(edchars)) != 0) {
199 #ifdef EMACS
200 			x_emacs_keys(&edchars);
201 #endif
202 		}
203 	} else {
204 		tcsetattr(tty_fd, TCSADRAIN, &tty_state);
205 	}
206 
207 	return prev;
208 }
209 
210 void
211 set_editmode(const char *ed)
212 {
213 	static const enum sh_flag edit_flags[] = {
214 #ifdef EMACS
215 		FEMACS, FGMACS,
216 #endif
217 #ifdef VI
218 		FVI,
219 #endif
220 	};
221 	char *rcp;
222 	int i;
223 
224 	if ((rcp = strrchr(ed, '/')))
225 		ed = ++rcp;
226 	for (i = 0; i < NELEM(edit_flags); i++)
227 		if (strstr(ed, options[(int) edit_flags[i]].name)) {
228 			change_flag(edit_flags[i], OF_SPECIAL, 1);
229 			return;
230 		}
231 }
232 
233 /* ------------------------------------------------------------------------- */
234 /*           Misc common code for vi/emacs				     */
235 
236 /* Handle the commenting/uncommenting of a line.
237  * Returns:
238  *	1 if a carriage return is indicated (comment added)
239  *	0 if no return (comment removed)
240  *	-1 if there is an error (not enough room for comment chars)
241  * If successful, *lenp contains the new length.  Note: cursor should be
242  * moved to the start of the line after (un)commenting.
243  */
244 int
245 x_do_comment(char *buf, int bsize, int *lenp)
246 {
247 	int i, j;
248 	int len = *lenp;
249 
250 	if (len == 0)
251 		return 1; /* somewhat arbitrary - it's what at&t ksh does */
252 
253 	/* Already commented? */
254 	if (buf[0] == '#') {
255 		int saw_nl = 0;
256 
257 		for (j = 0, i = 1; i < len; i++) {
258 			if (!saw_nl || buf[i] != '#')
259 				buf[j++] = buf[i];
260 			saw_nl = buf[i] == '\n';
261 		}
262 		*lenp = j;
263 		return 0;
264 	} else {
265 		int n = 1;
266 
267 		/* See if there's room for the #'s - 1 per \n */
268 		for (i = 0; i < len; i++)
269 			if (buf[i] == '\n')
270 				n++;
271 		if (len + n >= bsize)
272 			return -1;
273 		/* Now add them... */
274 		for (i = len, j = len + n; --i >= 0; ) {
275 			if (buf[i] == '\n')
276 				buf[--j] = '#';
277 			buf[--j] = buf[i];
278 		}
279 		buf[0] = '#';
280 		*lenp += n;
281 		return 1;
282 	}
283 }
284 
285 /* ------------------------------------------------------------------------- */
286 /*           Common file/command completion code for vi/emacs	             */
287 
288 
289 static char	*add_glob(const char *str, int slen);
290 static void	glob_table(const char *pat, XPtrV *wp, struct table *tp);
291 static void	glob_path(int flags, const char *pat, XPtrV *wp,
292 				const char *path);
293 
294 void
295 x_print_expansions(int nwords, char *const *words, int is_command)
296 {
297 	int use_copy = 0;
298 	int prefix_len;
299 	XPtrV l;
300 
301 	/* Check if all matches are in the same directory (in this
302 	 * case, we want to omit the directory name)
303 	 */
304 	if (!is_command &&
305 	    (prefix_len = x_longest_prefix(nwords, words)) > 0) {
306 		int i;
307 
308 		/* Special case for 1 match (prefix is whole word) */
309 		if (nwords == 1)
310 			prefix_len = x_basename(words[0], (char *) 0);
311 		/* Any (non-trailing) slashes in non-common word suffixes? */
312 		for (i = 0; i < nwords; i++)
313 			if (x_basename(words[i] + prefix_len, (char *) 0) >
314 			    prefix_len)
315 				break;
316 		/* All in same directory? */
317 		if (i == nwords) {
318 			while (prefix_len > 0 && words[0][prefix_len - 1] != '/')
319 				prefix_len--;
320 			use_copy = 1;
321 			XPinit(l, nwords + 1);
322 			for (i = 0; i < nwords; i++)
323 				XPput(l, words[i] + prefix_len);
324 			XPput(l, (char *) 0);
325 		}
326 	}
327 
328 	/*
329 	 * Enumerate expansions
330 	 */
331 	x_putc('\r');
332 	x_putc('\n');
333 	pr_list(use_copy ? (char **) XPptrv(l) : words);
334 
335 	if (use_copy)
336 		XPfree(l); /* not x_free_words() */
337 }
338 
339 /*
340  *  Do file globbing:
341  *	- appends * to (copy of) str if no globbing chars found
342  *	- does expansion, checks for no match, etc.
343  *	- sets *wordsp to array of matching strings
344  *	- returns number of matching strings
345  */
346 static int
347 x_file_glob(int flags, const char *str, int slen, char ***wordsp)
348 {
349 	char *toglob;
350 	char **words;
351 	int nwords, i, idx, escaping;
352 	XPtrV w;
353 	struct source *s, *sold;
354 
355 	if (slen < 0)
356 		return 0;
357 
358 	toglob = add_glob(str, slen);
359 
360 	/* remove all escaping backward slashes */
361 	escaping = 0;
362 	for (i = 0, idx = 0; toglob[i]; i++) {
363 		if (toglob[i] == '\\' && !escaping) {
364 			escaping = 1;
365 			continue;
366 		}
367 
368 		toglob[idx] = toglob[i];
369 		idx++;
370 		if (escaping) escaping = 0;
371 	}
372 	toglob[idx] = '\0';
373 
374 	/*
375 	 * Convert "foo*" (toglob) to an array of strings (words)
376 	 */
377 	sold = source;
378 	s = pushs(SWSTR, ATEMP);
379 	s->start = s->str = toglob;
380 	source = s;
381 	if (yylex(ONEWORD) != LWORD) {
382 		source = sold;
383 		internal_errorf(0, "fileglob: substitute error");
384 		return 0;
385 	}
386 	source = sold;
387 	XPinit(w, 32);
388 	expand(yylval.cp, &w, DOGLOB|DOTILDE|DOMARKDIRS);
389 	XPput(w, NULL);
390 	words = (char **) XPclose(w);
391 
392 	for (nwords = 0; words[nwords]; nwords++)
393 		;
394 	if (nwords == 1) {
395 		struct stat statb;
396 
397 		/* Check if globbing failed (returned glob pattern),
398 		 * but be careful (E.g. toglob == "ab*" when the file
399 		 * "ab*" exists is not an error).
400 		 * Also, check for empty result - happens if we tried
401 		 * to glob something which evaluated to an empty
402 		 * string (e.g., "$FOO" when there is no FOO, etc).
403 		 */
404 		if ((strcmp(words[0], toglob) == 0 &&
405 		    stat(words[0], &statb) < 0) ||
406 		    words[0][0] == '\0') {
407 			x_free_words(nwords, words);
408 			words = NULL;
409 			nwords = 0;
410 		}
411 	}
412 	afree(toglob, ATEMP);
413 
414 	if (nwords) {
415 		*wordsp = words;
416 	} else if (words) {
417 		x_free_words(nwords, words);
418 		*wordsp = NULL;
419 	}
420 
421 	return nwords;
422 }
423 
424 /* Data structure used in x_command_glob() */
425 struct path_order_info {
426 	char *word;
427 	int base;
428 	int path_order;
429 };
430 
431 static int path_order_cmp(const void *aa, const void *bb);
432 
433 /* Compare routine used in x_command_glob() */
434 static int
435 path_order_cmp(const void *aa, const void *bb)
436 {
437 	const struct path_order_info *a = (const struct path_order_info *) aa;
438 	const struct path_order_info *b = (const struct path_order_info *) bb;
439 	int t;
440 
441 	t = strcmp(a->word + a->base, b->word + b->base);
442 	return t ? t : a->path_order - b->path_order;
443 }
444 
445 static int
446 x_command_glob(int flags, const char *str, int slen, char ***wordsp)
447 {
448 	char *toglob;
449 	char *pat;
450 	char *fpath;
451 	int nwords;
452 	XPtrV w;
453 	struct block *l;
454 
455 	if (slen < 0)
456 		return 0;
457 
458 	toglob = add_glob(str, slen);
459 
460 	/* Convert "foo*" (toglob) to a pattern for future use */
461 	pat = evalstr(toglob, DOPAT|DOTILDE);
462 	afree(toglob, ATEMP);
463 
464 	XPinit(w, 32);
465 
466 	glob_table(pat, &w, &keywords);
467 	glob_table(pat, &w, &aliases);
468 	glob_table(pat, &w, &builtins);
469 	for (l = e->loc; l; l = l->next)
470 		glob_table(pat, &w, &l->funs);
471 
472 	glob_path(flags, pat, &w, path);
473 	if ((fpath = str_val(global("FPATH"))) != null)
474 		glob_path(flags, pat, &w, fpath);
475 
476 	nwords = XPsize(w);
477 
478 	if (!nwords) {
479 		*wordsp = (char **) 0;
480 		XPfree(w);
481 		return 0;
482 	}
483 
484 	/* Sort entries */
485 	if (flags & XCF_FULLPATH) {
486 		/* Sort by basename, then path order */
487 		struct path_order_info *info;
488 		struct path_order_info *last_info = 0;
489 		char **words = (char **) XPptrv(w);
490 		int path_order = 0;
491 		int i;
492 
493 		info = (struct path_order_info *)
494 			alloc(sizeof(struct path_order_info) * nwords, ATEMP);
495 		for (i = 0; i < nwords; i++) {
496 			info[i].word = words[i];
497 			info[i].base = x_basename(words[i], (char *) 0);
498 			if (!last_info || info[i].base != last_info->base ||
499 			    strncmp(words[i], last_info->word, info[i].base) != 0) {
500 				last_info = &info[i];
501 				path_order++;
502 			}
503 			info[i].path_order = path_order;
504 		}
505 		qsort(info, nwords, sizeof(struct path_order_info),
506 			path_order_cmp);
507 		for (i = 0; i < nwords; i++)
508 			words[i] = info[i].word;
509 		afree((void *) info, ATEMP);
510 	} else {
511 		/* Sort and remove duplicate entries */
512 		char **words = (char **) XPptrv(w);
513 		int i, j;
514 
515 		qsortp(XPptrv(w), (size_t) nwords, xstrcmp);
516 
517 		for (i = j = 0; i < nwords - 1; i++) {
518 			if (strcmp(words[i], words[i + 1]))
519 				words[j++] = words[i];
520 			else
521 				afree(words[i], ATEMP);
522 		}
523 		words[j++] = words[i];
524 		nwords = j;
525 		w.cur = (void **) &words[j];
526 	}
527 
528 	XPput(w, NULL);
529 	*wordsp = (char **) XPclose(w);
530 
531 	return nwords;
532 }
533 
534 #define IS_WORDC(c)	!( ctype(c, C_LEX1) || (c) == '\'' || (c) == '"' || \
535 			    (c) == '`' || (c) == '=' || (c) == ':' )
536 
537 static int
538 x_locate_word(const char *buf, int buflen, int pos, int *startp,
539     int *is_commandp)
540 {
541 	int p;
542 	int start, end;
543 
544 	/* Bad call?  Probably should report error */
545 	if (pos < 0 || pos > buflen) {
546 		*startp = pos;
547 		*is_commandp = 0;
548 		return 0;
549 	}
550 	/* The case where pos == buflen happens to take care of itself... */
551 
552 	start = pos;
553 	/* Keep going backwards to start of word (has effect of allowing
554 	 * one blank after the end of a word)
555 	 */
556 	for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
557 	    (start > 1 && buf[start-2] == '\\'); start--)
558 		;
559 	/* Go forwards to end of word */
560 	for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
561 		if (buf[end] == '\\' && (end+1) < buflen)
562 			end++;
563 	}
564 
565 	if (is_commandp) {
566 		int iscmd;
567 
568 		/* Figure out if this is a command */
569 		for (p = start - 1; p >= 0 && isspace(buf[p]); p--)
570 			;
571 		iscmd = p < 0 || strchr(";|&()`", buf[p]);
572 		if (iscmd) {
573 			/* If command has a /, path, etc. is not searched;
574 			 * only current directory is searched, which is just
575 			 * like file globbing.
576 			 */
577 			for (p = start; p < end; p++)
578 				if (buf[p] == '/')
579 					break;
580 			iscmd = p == end;
581 		}
582 		*is_commandp = iscmd;
583 	}
584 
585 	*startp = start;
586 
587 	return end - start;
588 }
589 
590 int
591 x_cf_glob(int flags, const char *buf, int buflen, int pos, int *startp,
592     int *endp, char ***wordsp, int *is_commandp)
593 {
594 	int len;
595 	int nwords;
596 	char **words;
597 	int is_command;
598 
599 	len = x_locate_word(buf, buflen, pos, startp, &is_command);
600 	if (!(flags & XCF_COMMAND))
601 		is_command = 0;
602 	/* Don't do command globing on zero length strings - it takes too
603 	 * long and isn't very useful.  File globs are more likely to be
604 	 * useful, so allow these.
605 	 */
606 	if (len == 0 && is_command)
607 		return 0;
608 
609 	nwords = (is_command ? x_command_glob : x_file_glob)(flags,
610 	    buf + *startp, len, &words);
611 	if (nwords == 0) {
612 		*wordsp = (char **) 0;
613 		return 0;
614 	}
615 
616 	if (is_commandp)
617 		*is_commandp = is_command;
618 	*wordsp = words;
619 	*endp = *startp + len;
620 
621 	return nwords;
622 }
623 
624 /* Given a string, copy it and possibly add a '*' to the end.  The
625  * new string is returned.
626  */
627 static char *
628 add_glob(const char *str, int slen)
629 {
630 	char *toglob;
631 	char *s;
632 	bool saw_slash = false;
633 
634 	if (slen < 0)
635 		return (char *) 0;
636 
637 	toglob = str_nsave(str, slen + 1, ATEMP); /* + 1 for "*" */
638 	toglob[slen] = '\0';
639 
640 	/*
641 	 * If the pathname contains a wildcard (an unquoted '*',
642 	 * '?', or '[') or parameter expansion ('$'), or a ~username
643 	 * with no trailing slash, then it is globbed based on that
644 	 * value (i.e., without the appended '*').
645 	 */
646 	for (s = toglob; *s; s++) {
647 		if (*s == '\\' && s[1])
648 			s++;
649 		else if (*s == '*' || *s == '[' || *s == '?' || *s == '$' ||
650 		    (s[1] == '(' /*)*/ && strchr("+@!", *s)))
651 			break;
652 		else if (*s == '/')
653 			saw_slash = true;
654 	}
655 	if (!*s && (*toglob != '~' || saw_slash)) {
656 		toglob[slen] = '*';
657 		toglob[slen + 1] = '\0';
658 	}
659 
660 	return toglob;
661 }
662 
663 /*
664  * Find longest common prefix
665  */
666 int
667 x_longest_prefix(int nwords, char *const *words)
668 {
669 	int i, j;
670 	int prefix_len;
671 	char *p;
672 
673 	if (nwords <= 0)
674 		return 0;
675 
676 	prefix_len = strlen(words[0]);
677 	for (i = 1; i < nwords; i++)
678 		for (j = 0, p = words[i]; j < prefix_len; j++)
679 			if (p[j] != words[0][j]) {
680 				prefix_len = j;
681 				break;
682 			}
683 	return prefix_len;
684 }
685 
686 void
687 x_free_words(int nwords, char **words)
688 {
689 	int i;
690 
691 	for (i = 0; i < nwords; i++)
692 		if (words[i])
693 			afree(words[i], ATEMP);
694 	afree(words, ATEMP);
695 }
696 
697 /* Return the offset of the basename of string s (which ends at se - need not
698  * be null terminated).  Trailing slashes are ignored.  If s is just a slash,
699  * then the offset is 0 (actually, length - 1).
700  *	s		Return
701  *	/etc		1
702  *	/etc/		1
703  *	/etc//		1
704  *	/etc/fo		5
705  *	foo		0
706  *	///		2
707  *			0
708  */
709 int
710 x_basename(const char *s, const char *se)
711 {
712 	const char *p;
713 
714 	if (se == (char *) 0)
715 		se = s + strlen(s);
716 	if (s == se)
717 		return 0;
718 
719 	/* Skip trailing slashes */
720 	for (p = se - 1; p > s && *p == '/'; p--)
721 		;
722 	for (; p > s && *p != '/'; p--)
723 		;
724 	if (*p == '/' && p + 1 < se)
725 		p++;
726 
727 	return p - s;
728 }
729 
730 /*
731  *  Apply pattern matching to a table: all table entries that match a pattern
732  * are added to wp.
733  */
734 static void
735 glob_table(const char *pat, XPtrV *wp, struct table *tp)
736 {
737 	struct tstate ts;
738 	struct tbl *te;
739 
740 	for (ktwalk(&ts, tp); (te = ktnext(&ts)); ) {
741 		if (gmatch(te->name, pat, false))
742 			XPput(*wp, str_save(te->name, ATEMP));
743 	}
744 }
745 
746 static void
747 glob_path(int flags, const char *pat, XPtrV *wp, const char *path)
748 {
749 	const char *sp, *p;
750 	char *xp;
751 	int staterr;
752 	int pathlen;
753 	int patlen;
754 	int oldsize, newsize, i, j;
755 	char **words;
756 	XString xs;
757 
758 	patlen = strlen(pat) + 1;
759 	sp = path;
760 	Xinit(xs, xp, patlen + 128, ATEMP);
761 	while (sp) {
762 		xp = Xstring(xs, xp);
763 		if (!(p = strchr(sp, ':')))
764 			p = sp + strlen(sp);
765 		pathlen = p - sp;
766 		if (pathlen) {
767 			/* Copy sp into xp, stuffing any MAGIC characters
768 			 * on the way
769 			 */
770 			const char *s = sp;
771 
772 			XcheckN(xs, xp, pathlen * 2);
773 			while (s < p) {
774 				if (ISMAGIC(*s))
775 					*xp++ = MAGIC;
776 				*xp++ = *s++;
777 			}
778 			*xp++ = '/';
779 			pathlen++;
780 		}
781 		sp = p;
782 		XcheckN(xs, xp, patlen);
783 		memcpy(xp, pat, patlen);
784 
785 		oldsize = XPsize(*wp);
786 		glob_str(Xstring(xs, xp), wp, 1); /* mark dirs */
787 		newsize = XPsize(*wp);
788 
789 		/* Check that each match is executable... */
790 		words = (char **) XPptrv(*wp);
791 		for (i = j = oldsize; i < newsize; i++) {
792 			staterr = 0;
793 			if ((search_access(words[i], X_OK, &staterr) >= 0) ||
794 			    (staterr == EISDIR)) {
795 				words[j] = words[i];
796 				if (!(flags & XCF_FULLPATH))
797 					memmove(words[j], words[j] + pathlen,
798 					    strlen(words[j] + pathlen) + 1);
799 				j++;
800 			} else
801 				afree(words[i], ATEMP);
802 		}
803 		wp->cur = (void **) &words[j];
804 
805 		if (!*sp++)
806 			break;
807 	}
808 	Xfree(xs, xp);
809 }
810 
811 /*
812  * if argument string contains any special characters, they will
813  * be escaped and the result will be put into edit buffer by
814  * keybinding-specific function
815  */
816 int
817 x_escape(const char *s, size_t len, int (*putbuf_func) (const char *, size_t))
818 {
819 	size_t add, wlen;
820 	const char *ifs = str_val(local("IFS", 0));
821 	int rval = 0;
822 
823 	for (add = 0, wlen = len; wlen - add > 0; add++) {
824 		if (strchr("\"#$&'()*;<=>?[\\]`{|}", s[add]) ||
825 		    strchr(ifs, s[add])) {
826 			if (putbuf_func(s, add) != 0) {
827 				rval = -1;
828 				break;
829 			}
830 
831 			putbuf_func("\\", 1);
832 			putbuf_func(&s[add], 1);
833 
834 			add++;
835 			wlen -= add;
836 			s += add;
837 			add = -1; /* after the increment it will go to 0 */
838 		}
839 	}
840 	if (wlen > 0 && rval == 0)
841 		rval = putbuf_func(s, wlen);
842 
843 	return (rval);
844 }
845 #endif /* EDIT */
846