GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ksh/edit.c Lines: 213 355 60.0 %
Date: 2016-12-06 Branches: 121 312 38.8 %

Line Branch Exec Source
1
/*	$OpenBSD: edit.c,v 1.53 2016/03/17 23:33:23 mmcc Exp $	*/
2
3
/*
4
 * Command line editing - common code
5
 *
6
 */
7
8
#include "config.h"
9
#ifdef EDIT
10
11
#include <sys/ioctl.h>
12
#include <sys/stat.h>
13
14
#include <ctype.h>
15
#include <errno.h>
16
#include <libgen.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <unistd.h>
20
21
#include "sh.h"
22
#include "edit.h"
23
#include "tty.h"
24
25
X_chars edchars;
26
27
static void x_sigwinch(int);
28
volatile sig_atomic_t got_sigwinch;
29
static void check_sigwinch(void);
30
31
static int	x_file_glob(int, const char *, int, char ***);
32
static int	x_command_glob(int, const char *, int, char ***);
33
static int	x_locate_word(const char *, int, int, int *, int *);
34
35
36
/* Called from main */
37
void
38
x_init(void)
39
63
{
40
	/* set to -2 to force initial binding */
41
63
	edchars.erase = edchars.kill = edchars.intr = edchars.quit =
42
	    edchars.eof = -2;
43
	/* default value for deficient systems */
44
63
	edchars.werase = 027;	/* ^W */
45
46
63
	if (setsig(&sigtraps[SIGWINCH], x_sigwinch, SS_RESTORE_ORIG|SS_SHTRAP))
47
63
		sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
48
63
	got_sigwinch = 1; /* force initial check */
49
63
	check_sigwinch();
50
51
#ifdef EMACS
52
63
	x_init_emacs();
53
#endif /* EMACS */
54
63
}
55
56
static void
57
x_sigwinch(int sig)
58
{
59
	got_sigwinch = 1;
60
}
61
62
static void
63
check_sigwinch(void)
64
84
{
65
84
	if (got_sigwinch) {
66
		struct winsize ws;
67
68
84
		got_sigwinch = 0;
69

84
		if (procpid == kshpid && ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
70
			struct tbl *vp;
71
72
			/* Do NOT export COLUMNS/LINES.  Many applications
73
			 * check COLUMNS/LINES before checking ws.ws_col/row,
74
			 * so if the app is started with C/L in the environ
75
			 * and the window is then resized, the app won't
76
			 * see the change cause the environ doesn't change.
77
			 */
78
84
			if (ws.ws_col) {
79
84
				x_cols = ws.ws_col < MIN_COLS ? MIN_COLS :
80
				    ws.ws_col;
81
82
84
				if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
83
84
					setint(vp, (long) ws.ws_col);
84
			}
85

84
			if (ws.ws_row && (vp = typeset("LINES", 0, 0, 0, 0)))
86
84
				setint(vp, (long) ws.ws_row);
87
		}
88
	}
89
84
}
90
91
/*
92
 * read an edited command line
93
 */
94
int
95
x_read(char *buf, size_t len)
96
21
{
97
	int	i;
98
99
21
	x_mode(true);
100
#ifdef EMACS
101

42
	if (Flag(FEMACS) || Flag(FGMACS))
102
21
		i = x_emacs(buf, len);
103
	else
104
#endif
105
#ifdef VI
106
	if (Flag(FVI))
107
		i = x_vi(buf, len);
108
	else
109
#endif
110
		i = -1;		/* internal error */
111
21
	x_mode(false);
112
21
	check_sigwinch();
113
21
	return i;
114
}
115
116
/* tty I/O */
117
118
int
119
x_getc(void)
120
73
{
121
	char c;
122
	int n;
123
124

146
	while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
125
		if (trap) {
126
			x_mode(false);
127
			runtraps(0);
128
			x_mode(true);
129
		}
130
73
	if (n != 1)
131
		return -1;
132
73
	return (int) (unsigned char) c;
133
}
134
135
void
136
x_flush(void)
137
112
{
138
112
	shf_flush(shl_out);
139
112
}
140
141
void
142
x_putc(int c)
143
1479
{
144
1479
	shf_putc(c, shl_out);
145
1479
}
146
147
void
148
x_puts(const char *s)
149
{
150
	while (*s != 0)
151
		shf_putc(*s++, shl_out);
152
}
153
154
bool
155
x_mode(bool onoff)
156
42
{
157
	static bool	x_cur_mode;
158
	bool		prev;
159
160
42
	if (x_cur_mode == onoff)
161
		return x_cur_mode;
162
42
	prev = x_cur_mode;
163
42
	x_cur_mode = onoff;
164
165
42
	if (onoff) {
166
		struct termios	cb;
167
		X_chars		oldchars;
168
169
21
		oldchars = edchars;
170
21
		cb = tty_state;
171
172
21
		edchars.erase = cb.c_cc[VERASE];
173
21
		edchars.kill = cb.c_cc[VKILL];
174
21
		edchars.intr = cb.c_cc[VINTR];
175
21
		edchars.quit = cb.c_cc[VQUIT];
176
21
		edchars.eof = cb.c_cc[VEOF];
177
21
		edchars.werase = cb.c_cc[VWERASE];
178
21
		cb.c_iflag &= ~(INLCR|ICRNL);
179
21
		cb.c_lflag &= ~(ISIG|ICANON|ECHO);
180
		/* osf/1 processes lnext when ~icanon */
181
21
		cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
182
		/* sunos 4.1.x & osf/1 processes discard(flush) when ~icanon */
183
21
		cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
184
21
		cb.c_cc[VTIME] = 0;
185
21
		cb.c_cc[VMIN] = 1;
186
187
21
		tcsetattr(tty_fd, TCSADRAIN, &cb);
188
189
		/* Convert unset values to internal `unset' value */
190
21
		if (edchars.erase == _POSIX_VDISABLE)
191
			edchars.erase = -1;
192
21
		if (edchars.kill == _POSIX_VDISABLE)
193
			edchars.kill = -1;
194
21
		if (edchars.intr == _POSIX_VDISABLE)
195
			edchars.intr = -1;
196
21
		if (edchars.quit == _POSIX_VDISABLE)
197
			edchars.quit = -1;
198
21
		if (edchars.eof == _POSIX_VDISABLE)
199
			edchars.eof = -1;
200
21
		if (edchars.werase == _POSIX_VDISABLE)
201
			edchars.werase = -1;
202
21
		if (memcmp(&edchars, &oldchars, sizeof(edchars)) != 0) {
203
#ifdef EMACS
204
7
			x_emacs_keys(&edchars);
205
#endif
206
		}
207
	} else {
208
21
		tcsetattr(tty_fd, TCSADRAIN, &tty_state);
209
	}
210
211
42
	return prev;
212
}
213
214
void
215
set_editmode(const char *ed)
216
{
217
	static const enum sh_flag edit_flags[] = {
218
#ifdef EMACS
219
		FEMACS, FGMACS,
220
#endif
221
#ifdef VI
222
		FVI,
223
#endif
224
	};
225
	char *rcp;
226
	int i;
227
228
	if ((rcp = strrchr(ed, '/')))
229
		ed = ++rcp;
230
	for (i = 0; i < NELEM(edit_flags); i++)
231
		if (strstr(ed, options[(int) edit_flags[i]].name)) {
232
			change_flag(edit_flags[i], OF_SPECIAL, 1);
233
			return;
234
		}
235
}
236
237
/* ------------------------------------------------------------------------- */
238
/*           Misc common code for vi/emacs				     */
239
240
/* Handle the commenting/uncommenting of a line.
241
 * Returns:
242
 *	1 if a carriage return is indicated (comment added)
243
 *	0 if no return (comment removed)
244
 *	-1 if there is an error (not enough room for comment chars)
245
 * If successful, *lenp contains the new length.  Note: cursor should be
246
 * moved to the start of the line after (un)commenting.
247
 */
248
int
249
x_do_comment(char *buf, int bsize, int *lenp)
250
{
251
	int i, j;
252
	int len = *lenp;
253
254
	if (len == 0)
255
		return 1; /* somewhat arbitrary - it's what at&t ksh does */
256
257
	/* Already commented? */
258
	if (buf[0] == '#') {
259
		int saw_nl = 0;
260
261
		for (j = 0, i = 1; i < len; i++) {
262
			if (!saw_nl || buf[i] != '#')
263
				buf[j++] = buf[i];
264
			saw_nl = buf[i] == '\n';
265
		}
266
		*lenp = j;
267
		return 0;
268
	} else {
269
		int n = 1;
270
271
		/* See if there's room for the #'s - 1 per \n */
272
		for (i = 0; i < len; i++)
273
			if (buf[i] == '\n')
274
				n++;
275
		if (len + n >= bsize)
276
			return -1;
277
		/* Now add them... */
278
		for (i = len, j = len + n; --i >= 0; ) {
279
			if (buf[i] == '\n')
280
				buf[--j] = '#';
281
			buf[--j] = buf[i];
282
		}
283
		buf[0] = '#';
284
		*lenp += n;
285
		return 1;
286
	}
287
}
288
289
/* ------------------------------------------------------------------------- */
290
/*           Common file/command completion code for vi/emacs	             */
291
292
293
static char	*add_glob(const char *str, int slen);
294
static void	glob_table(const char *pat, XPtrV *wp, struct table *tp);
295
static void	glob_path(int flags, const char *pat, XPtrV *wp,
296
				const char *path);
297
298
void
299
x_print_expansions(int nwords, char *const *words, int is_command)
300
{
301
	int use_copy = 0;
302
	int prefix_len;
303
	XPtrV l;
304
305
	/* Check if all matches are in the same directory (in this
306
	 * case, we want to omit the directory name)
307
	 */
308
	if (!is_command &&
309
	    (prefix_len = x_longest_prefix(nwords, words)) > 0) {
310
		int i;
311
312
		/* Special case for 1 match (prefix is whole word) */
313
		if (nwords == 1)
314
			prefix_len = x_basename(words[0], NULL);
315
		/* Any (non-trailing) slashes in non-common word suffixes? */
316
		for (i = 0; i < nwords; i++)
317
			if (x_basename(words[i] + prefix_len, NULL) >
318
			    prefix_len)
319
				break;
320
		/* All in same directory? */
321
		if (i == nwords) {
322
			while (prefix_len > 0 && words[0][prefix_len - 1] != '/')
323
				prefix_len--;
324
			use_copy = 1;
325
			XPinit(l, nwords + 1);
326
			for (i = 0; i < nwords; i++)
327
				XPput(l, words[i] + prefix_len);
328
			XPput(l, NULL);
329
		}
330
	}
331
332
	/*
333
	 * Enumerate expansions
334
	 */
335
	x_putc('\r');
336
	x_putc('\n');
337
	pr_list(use_copy ? (char **) XPptrv(l) : words);
338
339
	if (use_copy)
340
		XPfree(l); /* not x_free_words() */
341
}
342
343
/*
344
 *  Do file globbing:
345
 *	- appends * to (copy of) str if no globbing chars found
346
 *	- does expansion, checks for no match, etc.
347
 *	- sets *wordsp to array of matching strings
348
 *	- returns number of matching strings
349
 */
350
static int
351
x_file_glob(int flags, const char *str, int slen, char ***wordsp)
352
2
{
353
	char *toglob;
354
	char **words;
355
	int nwords;
356
	XPtrV w;
357
	struct source *s, *sold;
358
359
2
	if (slen < 0)
360
		return 0;
361
362
2
	toglob = add_glob(str, slen);
363
364
	/*
365
	 * Convert "foo*" (toglob) to an array of strings (words)
366
	 */
367
2
	sold = source;
368
2
	s = pushs(SWSTR, ATEMP);
369
2
	s->start = s->str = toglob;
370
2
	source = s;
371
2
	if (yylex(ONEWORD|UNESCAPE) != LWORD) {
372
		source = sold;
373
		internal_errorf(0, "fileglob: substitute error");
374
		return 0;
375
	}
376
2
	source = sold;
377
2
	XPinit(w, 32);
378
2
	expand(yylval.cp, &w, DOGLOB|DOTILDE|DOMARKDIRS);
379
2
	XPput(w, NULL);
380
2
	words = (char **) XPclose(w);
381
382
2
	for (nwords = 0; words[nwords]; nwords++)
383
		;
384
2
	if (nwords == 1) {
385
		struct stat statb;
386
387
		/* Check if file exists, also, check for empty
388
		 * result - happens if we tried to glob something
389
		 * which evaluated to an empty string (e.g.,
390
		 * "$FOO" when there is no FOO, etc).
391
		 */
392

2
		 if ((lstat(words[0], &statb) < 0) ||
393
		    words[0][0] == '\0') {
394
			x_free_words(nwords, words);
395
			words = NULL;
396
			nwords = 0;
397
		}
398
	}
399
2
	afree(toglob, ATEMP);
400
401
2
	if (nwords) {
402
2
		*wordsp = words;
403
	} else if (words) {
404
		x_free_words(nwords, words);
405
		*wordsp = NULL;
406
	}
407
408
2
	return nwords;
409
}
410
411
/* Data structure used in x_command_glob() */
412
struct path_order_info {
413
	char *word;
414
	int base;
415
	int path_order;
416
};
417
418
static int path_order_cmp(const void *aa, const void *bb);
419
420
/* Compare routine used in x_command_glob() */
421
static int
422
path_order_cmp(const void *aa, const void *bb)
423
{
424
	const struct path_order_info *a = (const struct path_order_info *) aa;
425
	const struct path_order_info *b = (const struct path_order_info *) bb;
426
	int t;
427
428
	t = strcmp(a->word + a->base, b->word + b->base);
429
	return t ? t : a->path_order - b->path_order;
430
}
431
432
static int
433
x_command_glob(int flags, const char *str, int slen, char ***wordsp)
434
4
{
435
	char *toglob;
436
	char *pat;
437
	char *fpath;
438
	int nwords;
439
	XPtrV w;
440
	struct block *l;
441
442
4
	if (slen < 0)
443
		return 0;
444
445
4
	toglob = add_glob(str, slen);
446
447
	/* Convert "foo*" (toglob) to a pattern for future use */
448
4
	pat = evalstr(toglob, DOPAT|DOTILDE);
449
4
	afree(toglob, ATEMP);
450
451
4
	XPinit(w, 32);
452
453
4
	glob_table(pat, &w, &keywords);
454
4
	glob_table(pat, &w, &aliases);
455
4
	glob_table(pat, &w, &builtins);
456
8
	for (l = genv->loc; l; l = l->next)
457
4
		glob_table(pat, &w, &l->funs);
458
459
4
	glob_path(flags, pat, &w, path);
460
4
	if ((fpath = str_val(global("FPATH"))) != null)
461
		glob_path(flags, pat, &w, fpath);
462
463
4
	nwords = XPsize(w);
464
465
4
	if (!nwords) {
466
		*wordsp = NULL;
467
		XPfree(w);
468
		return 0;
469
	}
470
471
	/* Sort entries */
472
4
	if (flags & XCF_FULLPATH) {
473
		/* Sort by basename, then path order */
474
		struct path_order_info *info;
475
		struct path_order_info *last_info = NULL;
476
		char **words = (char **) XPptrv(w);
477
		int path_order = 0;
478
		int i;
479
480
		info = areallocarray(NULL, nwords,
481
		    sizeof(struct path_order_info), ATEMP);
482
483
		for (i = 0; i < nwords; i++) {
484
			info[i].word = words[i];
485
			info[i].base = x_basename(words[i], NULL);
486
			if (!last_info || info[i].base != last_info->base ||
487
			    strncmp(words[i], last_info->word, info[i].base) != 0) {
488
				last_info = &info[i];
489
				path_order++;
490
			}
491
			info[i].path_order = path_order;
492
		}
493
		qsort(info, nwords, sizeof(struct path_order_info),
494
			path_order_cmp);
495
		for (i = 0; i < nwords; i++)
496
			words[i] = info[i].word;
497
		afree(info, ATEMP);
498
	} else {
499
		/* Sort and remove duplicate entries */
500
4
		char **words = (char **) XPptrv(w);
501
		int i, j;
502
503
4
		qsortp(XPptrv(w), (size_t) nwords, xstrcmp);
504
505
4
		for (i = j = 0; i < nwords - 1; i++) {
506
			if (strcmp(words[i], words[i + 1]))
507
				words[j++] = words[i];
508
			else
509
				afree(words[i], ATEMP);
510
		}
511
4
		words[j++] = words[i];
512
4
		nwords = j;
513
4
		w.cur = (void **) &words[j];
514
	}
515
516
4
	XPput(w, NULL);
517
4
	*wordsp = (char **) XPclose(w);
518
519
4
	return nwords;
520
}
521
522
#define IS_WORDC(c)	!( ctype(c, C_LEX1) || (c) == '\'' || (c) == '"' || \
523
			    (c) == '`' || (c) == '=' || (c) == ':' )
524
525
static int
526
x_locate_word(const char *buf, int buflen, int pos, int *startp,
527
    int *is_commandp)
528
6
{
529
	int p;
530
	int start, end;
531
532
	/* Bad call?  Probably should report error */
533
6
	if (pos < 0 || pos > buflen) {
534
		*startp = pos;
535
		*is_commandp = 0;
536
		return 0;
537
	}
538
	/* The case where pos == buflen happens to take care of itself... */
539
540
6
	start = pos;
541
	/* Keep going backwards to start of word (has effect of allowing
542
	 * one blank after the end of a word)
543
	 */
544




37
	for (; (start > 0 && IS_WORDC(buf[start - 1])) ||
545
25
	    (start > 1 && buf[start-2] == '\\'); start--)
546
		;
547
	/* Go forwards to end of word */
548



31
	for (end = start; end < buflen && IS_WORDC(buf[end]); end++) {
549

25
		if (buf[end] == '\\' && (end+1) < buflen)
550
			end++;
551
	}
552
553
6
	if (is_commandp) {
554
		int iscmd;
555
556
		/* Figure out if this is a command */
557

18
		for (p = start - 1; p >= 0 && isspace((unsigned char)buf[p]);
558
2
		    p--)
559
			;
560

6
		iscmd = p < 0 || strchr(";|&()`", buf[p]);
561
6
		if (iscmd) {
562
			/* If command has a /, path, etc. is not searched;
563
			 * only current directory is searched, which is just
564
			 * like file globbing.
565
			 */
566
19
			for (p = start; p < end; p++)
567
15
				if (buf[p] == '/')
568
					break;
569
4
			iscmd = p == end;
570
		}
571
6
		*is_commandp = iscmd;
572
	}
573
574
6
	*startp = start;
575
576
6
	return end - start;
577
}
578
579
int
580
x_cf_glob(int flags, const char *buf, int buflen, int pos, int *startp,
581
    int *endp, char ***wordsp, int *is_commandp)
582
6
{
583
	int len;
584
	int nwords;
585
	char **words;
586
	int is_command;
587
588
6
	len = x_locate_word(buf, buflen, pos, startp, &is_command);
589
6
	if (!(flags & XCF_COMMAND))
590
		is_command = 0;
591
	/* Don't do command globing on zero length strings - it takes too
592
	 * long and isn't very useful.  File globs are more likely to be
593
	 * useful, so allow these.
594
	 */
595

6
	if (len == 0 && is_command)
596
		return 0;
597
598
6
	nwords = (is_command ? x_command_glob : x_file_glob)(flags,
599
	    buf + *startp, len, &words);
600
6
	if (nwords == 0) {
601
		*wordsp = NULL;
602
		return 0;
603
	}
604
605
6
	if (is_commandp)
606
6
		*is_commandp = is_command;
607
6
	*wordsp = words;
608
6
	*endp = *startp + len;
609
610
6
	return nwords;
611
}
612
613
/* Given a string, copy it and possibly add a '*' to the end.  The
614
 * new string is returned.
615
 */
616
static char *
617
add_glob(const char *str, int slen)
618
6
{
619
	char *toglob;
620
	char *s;
621
6
	bool saw_slash = false;
622
623
6
	if (slen < 0)
624
		return NULL;
625
626
6
	toglob = str_nsave(str, slen + 1, ATEMP); /* + 1 for "*" */
627
6
	toglob[slen] = '\0';
628
629
	/*
630
	 * If the pathname contains a wildcard (an unquoted '*',
631
	 * '?', or '[') or parameter expansion ('$'), or a ~username
632
	 * with no trailing slash, then it is globbed based on that
633
	 * value (i.e., without the appended '*').
634
	 */
635
31
	for (s = toglob; *s; s++) {
636

25
		if (*s == '\\' && s[1])
637
			s++;
638


25
		else if (*s == '*' || *s == '[' || *s == '?' || *s == '$' ||
639
		    (s[1] == '(' /*)*/ && strchr("+@!", *s)))
640
			break;
641
25
		else if (*s == '/')
642
3
			saw_slash = true;
643
	}
644

6
	if (!*s && (*toglob != '~' || saw_slash)) {
645
6
		toglob[slen] = '*';
646
6
		toglob[slen + 1] = '\0';
647
	}
648
649
6
	return toglob;
650
}
651
652
/*
653
 * Find longest common prefix
654
 */
655
int
656
x_longest_prefix(int nwords, char *const *words)
657
6
{
658
	int i, j;
659
	int prefix_len;
660
	char *p;
661
662
6
	if (nwords <= 0)
663
		return 0;
664
665
6
	prefix_len = strlen(words[0]);
666
6
	for (i = 1; i < nwords; i++)
667
		for (j = 0, p = words[i]; j < prefix_len; j++)
668
			if (p[j] != words[0][j]) {
669
				prefix_len = j;
670
				break;
671
			}
672
6
	return prefix_len;
673
}
674
675
void
676
x_free_words(int nwords, char **words)
677
6
{
678
	int i;
679
680
12
	for (i = 0; i < nwords; i++)
681
6
		afree(words[i], ATEMP);
682
6
	afree(words, ATEMP);
683
6
}
684
685
/* Return the offset of the basename of string s (which ends at se - need not
686
 * be null terminated).  Trailing slashes are ignored.  If s is just a slash,
687
 * then the offset is 0 (actually, length - 1).
688
 *	s		Return
689
 *	/etc		1
690
 *	/etc/		1
691
 *	/etc//		1
692
 *	/etc/fo		5
693
 *	foo		0
694
 *	///		2
695
 *			0
696
 */
697
int
698
x_basename(const char *s, const char *se)
699
{
700
	const char *p;
701
702
	if (se == NULL)
703
		se = s + strlen(s);
704
	if (s == se)
705
		return 0;
706
707
	/* Skip trailing slashes */
708
	for (p = se - 1; p > s && *p == '/'; p--)
709
		;
710
	for (; p > s && *p != '/'; p--)
711
		;
712
	if (*p == '/' && p + 1 < se)
713
		p++;
714
715
	return p - s;
716
}
717
718
/*
719
 *  Apply pattern matching to a table: all table entries that match a pattern
720
 * are added to wp.
721
 */
722
static void
723
glob_table(const char *pat, XPtrV *wp, struct table *tp)
724
16
{
725
	struct tstate ts;
726
	struct tbl *te;
727
728
324
	for (ktwalk(&ts, tp); (te = ktnext(&ts)); ) {
729
292
		if (gmatch(te->name, pat, false))
730
			XPput(*wp, str_save(te->name, ATEMP));
731
	}
732
16
}
733
734
static void
735
glob_path(int flags, const char *pat, XPtrV *wp, const char *path)
736
4
{
737
	const char *sp, *p;
738
	char *xp;
739
	int staterr;
740
	int pathlen;
741
	int patlen;
742
	int oldsize, newsize, i, j;
743
	char **words;
744
	XString xs;
745
746
4
	patlen = strlen(pat) + 1;
747
4
	sp = path;
748
4
	Xinit(xs, xp, patlen + 128, ATEMP);
749
32
	while (sp) {
750
28
		xp = Xstring(xs, xp);
751
28
		if (!(p = strchr(sp, ':')))
752
4
			p = sp + strlen(sp);
753
28
		pathlen = p - sp;
754
28
		if (pathlen) {
755
			/* Copy sp into xp, stuffing any MAGIC characters
756
			 * on the way
757
			 */
758
28
			const char *s = sp;
759
760
28
			XcheckN(xs, xp, pathlen * 2);
761
304
			while (s < p) {
762
276
				if (ISMAGIC(*s))
763
					*xp++ = MAGIC;
764
276
				*xp++ = *s++;
765
			}
766
28
			*xp++ = '/';
767
28
			pathlen++;
768
		}
769
28
		sp = p;
770
28
		XcheckN(xs, xp, patlen);
771
28
		memcpy(xp, pat, patlen);
772
773
28
		oldsize = XPsize(*wp);
774
28
		glob_str(Xstring(xs, xp), wp, 1); /* mark dirs */
775
28
		newsize = XPsize(*wp);
776
777
		/* Check that each match is executable... */
778
28
		words = (char **) XPptrv(*wp);
779
32
		for (i = j = oldsize; i < newsize; i++) {
780
4
			staterr = 0;
781

8
			if ((search_access(words[i], X_OK, &staterr) >= 0) ||
782
			    (staterr == EISDIR)) {
783
4
				words[j] = words[i];
784
4
				if (!(flags & XCF_FULLPATH))
785
4
					memmove(words[j], words[j] + pathlen,
786
					    strlen(words[j] + pathlen) + 1);
787
4
				j++;
788
			} else
789
				afree(words[i], ATEMP);
790
		}
791
28
		wp->cur = (void **) &words[j];
792
793
28
		if (!*sp++)
794
4
			break;
795
	}
796
4
	Xfree(xs, xp);
797
4
}
798
799
/*
800
 * if argument string contains any special characters, they will
801
 * be escaped and the result will be put into edit buffer by
802
 * keybinding-specific function
803
 */
804
int
805
x_escape(const char *s, size_t len, int (*putbuf_func) (const char *, size_t))
806
6
{
807
	size_t add, wlen;
808
6
	const char *ifs = str_val(local("IFS", 0));
809
6
	int rval = 0;
810
811
41
	for (add = 0, wlen = len; wlen - add > 0; add++) {
812

35
		if (strchr("!\"#$&'()*:;<=>?[\\]`{|}", s[add]) ||
813
		    strchr(ifs, s[add])) {
814
			if (putbuf_func(s, add) != 0) {
815
				rval = -1;
816
				break;
817
			}
818
819
			putbuf_func("\\", 1);
820
			putbuf_func(&s[add], 1);
821
822
			add++;
823
			wlen -= add;
824
			s += add;
825
			add = -1; /* after the increment it will go to 0 */
826
		}
827
	}
828
6
	if (wlen > 0 && rval == 0)
829
6
		rval = putbuf_func(s, wlen);
830
831
6
	return (rval);
832
}
833
#endif /* EDIT */