GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ksh/emacs.c Lines: 389 975 39.9 %
Date: 2016-12-06 Branches: 138 619 22.3 %

Line Branch Exec Source
1
/*	$OpenBSD: emacs.c,v 1.65 2016/01/26 17:39:31 mmcc Exp $	*/
2
3
/*
4
 *  Emacs-like command line editing and history
5
 *
6
 *  created by Ron Natalie at BRL
7
 *  modified by Doug Kingston, Doug Gwyn, and Lou Salkind
8
 *  adapted to PD ksh by Eric Gisin
9
 *
10
 * partial rewrite by Marco Peereboom <marco@openbsd.org>
11
 * under the same license
12
 */
13
14
#include "config.h"
15
#ifdef EMACS
16
17
#include <sys/queue.h>
18
#include <sys/stat.h>
19
20
#include <ctype.h>
21
#include <locale.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "sh.h"
27
#include "edit.h"
28
29
static	Area	aedit;
30
#define	AEDIT	&aedit		/* area for kill ring and macro defns */
31
32
#define	CTRL(x)		((x) == '?' ? 0x7F : (x) & 0x1F)	/* ASCII */
33
#define	UNCTRL(x)	((x) == 0x7F ? '?' : (x) | 0x40)	/* ASCII */
34
35
/* values returned by keyboard functions */
36
#define	KSTD	0
37
#define	KEOL	1		/* ^M, ^J */
38
#define	KINTR	2		/* ^G, ^C */
39
40
struct	x_ftab {
41
	int		(*xf_func)(int c);
42
	const char	*xf_name;
43
	short		xf_flags;
44
};
45
46
#define XF_ARG		1	/* command takes number prefix */
47
#define	XF_NOBIND	2	/* not allowed to bind to function */
48
#define	XF_PREFIX	4	/* function sets prefix */
49
50
/* Separator for completion */
51
#define	is_cfs(c)	(c == ' ' || c == '\t' || c == '"' || c == '\'')
52
53
/* Separator for motion */
54
#define	is_mfs(c)	(!(isalnum((unsigned char)c) || \
55
			c == '_' || c == '$' || c & 0x80))
56
57
/* Arguments for do_complete()
58
 * 0 = enumerate  M-= complete as much as possible and then list
59
 * 1 = complete   M-Esc
60
 * 2 = list       M-?
61
 */
62
typedef enum {
63
	CT_LIST,	/* list the possible completions */
64
	CT_COMPLETE,	/* complete to longest prefix */
65
	CT_COMPLIST	/* complete and then list (if non-exact) */
66
} Comp_type;
67
68
/* keybindings */
69
struct kb_entry {
70
	TAILQ_ENTRY(kb_entry)	entry;
71
	unsigned char		*seq;
72
	int			len;
73
	struct x_ftab		*ftab;
74
	void			*args;
75
};
76
TAILQ_HEAD(kb_list, kb_entry);
77
struct kb_list			kblist = TAILQ_HEAD_INITIALIZER(kblist);
78
79
/* { from 4.9 edit.h */
80
/*
81
 * The following are used for my horizontal scrolling stuff
82
 */
83
static char    *xbuf;		/* beg input buffer */
84
static char    *xend;		/* end input buffer */
85
static char    *xcp;		/* current position */
86
static char    *xep;		/* current end */
87
static char    *xbp;		/* start of visible portion of input buffer */
88
static char    *xlp;		/* last byte visible on screen */
89
static int	x_adj_ok;
90
/*
91
 * we use x_adj_done so that functions can tell
92
 * whether x_adjust() has been called while they are active.
93
 */
94
static int	x_adj_done;
95
96
static int	xx_cols;
97
static int	x_col;
98
static int	x_displen;
99
static int	x_arg;		/* general purpose arg */
100
static int	x_arg_defaulted;/* x_arg not explicitly set; defaulted to 1 */
101
102
static int	xlp_valid;
103
/* end from 4.9 edit.h } */
104
static	int	x_tty;		/* are we on a tty? */
105
static	int	x_bind_quiet;	/* be quiet when binding keys */
106
static int	(*x_last_command)(int);
107
108
static	char   **x_histp;	/* history position */
109
static	int	x_nextcmd;	/* for newline-and-next */
110
static	char	*xmp;		/* mark pointer */
111
#define	KILLSIZE	20
112
static	char	*killstack[KILLSIZE];
113
static	int	killsp, killtp;
114
static	int	x_literal_set;
115
static	int	x_arg_set;
116
static	char	*macro_args;
117
static	int	prompt_skip;
118
static	int	prompt_redraw;
119
120
static int	x_ins(char *);
121
static void	x_delete(int, int);
122
static int	x_bword(void);
123
static int	x_fword(void);
124
static void	x_goto(char *);
125
static void	x_bs(int);
126
static int	x_size_str(char *);
127
static int	x_size(int);
128
static void	x_zots(char *);
129
static void	x_zotc(int);
130
static void	x_load_hist(char **);
131
static int	x_search(char *, int, int);
132
static int	x_match(char *, char *);
133
static void	x_redraw(int);
134
static void	x_push(int);
135
static void	x_adjust(void);
136
static void	x_e_ungetc(int);
137
static int	x_e_getc(void);
138
static void	x_e_putc(int);
139
static void	x_e_puts(const char *);
140
static int	x_comment(int);
141
static int	x_fold_case(int);
142
static char	*x_lastcp(void);
143
static void	do_complete(int, Comp_type);
144
static int	isu8cont(unsigned char);
145
146
/* proto's for keybindings */
147
static int	x_abort(int);
148
static int	x_beg_hist(int);
149
static int	x_comp_comm(int);
150
static int	x_comp_file(int);
151
static int	x_complete(int);
152
static int	x_del_back(int);
153
static int	x_del_bword(int);
154
static int	x_del_char(int);
155
static int	x_del_fword(int);
156
static int	x_del_line(int);
157
static int	x_draw_line(int);
158
static int	x_end_hist(int);
159
static int	x_end_of_text(int);
160
static int	x_enumerate(int);
161
static int	x_eot_del(int);
162
static int	x_error(int);
163
static int	x_goto_hist(int);
164
static int	x_ins_string(int);
165
static int	x_insert(int);
166
static int	x_kill(int);
167
static int	x_kill_region(int);
168
static int	x_list_comm(int);
169
static int	x_list_file(int);
170
static int	x_literal(int);
171
static int	x_meta_yank(int);
172
static int	x_mv_back(int);
173
static int	x_mv_begin(int);
174
static int	x_mv_bword(int);
175
static int	x_mv_end(int);
176
static int	x_mv_forw(int);
177
static int	x_mv_fword(int);
178
static int	x_newline(int);
179
static int	x_next_com(int);
180
static int	x_nl_next_com(int);
181
static int	x_noop(int);
182
static int	x_prev_com(int);
183
static int	x_prev_histword(int);
184
static int	x_search_char_forw(int);
185
static int	x_search_char_back(int);
186
static int	x_search_hist(int);
187
static int	x_set_mark(int);
188
static int	x_stuff(int);
189
static int	x_stuffreset(int);
190
static int	x_transpose(int);
191
static int	x_version(int);
192
static int	x_xchg_point_mark(int);
193
static int	x_yank(int);
194
static int	x_comp_list(int);
195
static int	x_expand(int);
196
static int	x_fold_capitalize(int);
197
static int	x_fold_lower(int);
198
static int	x_fold_upper(int);
199
static int	x_set_arg(int);
200
static int	x_comment(int);
201
#ifdef DEBUG
202
static int	x_debug_info(int);
203
#endif
204
205
static const struct x_ftab x_ftab[] = {
206
	{ x_abort,		"abort",			0 },
207
	{ x_beg_hist,		"beginning-of-history",		0 },
208
	{ x_comp_comm,		"complete-command",		0 },
209
	{ x_comp_file,		"complete-file",		0 },
210
	{ x_complete,		"complete",			0 },
211
	{ x_del_back,		"delete-char-backward",		XF_ARG },
212
	{ x_del_bword,		"delete-word-backward",		XF_ARG },
213
	{ x_del_char,		"delete-char-forward",		XF_ARG },
214
	{ x_del_fword,		"delete-word-forward",		XF_ARG },
215
	{ x_del_line,		"kill-line",			0 },
216
	{ x_draw_line,		"redraw",			0 },
217
	{ x_end_hist,		"end-of-history",		0 },
218
	{ x_end_of_text,	"eot",				0 },
219
	{ x_enumerate,		"list",				0 },
220
	{ x_eot_del,		"eot-or-delete",		XF_ARG },
221
	{ x_error,		"error",			0 },
222
	{ x_goto_hist,		"goto-history",			XF_ARG },
223
	{ x_ins_string,		"macro-string",			XF_NOBIND },
224
	{ x_insert,		"auto-insert",			XF_ARG },
225
	{ x_kill,		"kill-to-eol",			XF_ARG },
226
	{ x_kill_region,	"kill-region",			0 },
227
	{ x_list_comm,		"list-command",			0 },
228
	{ x_list_file,		"list-file",			0 },
229
	{ x_literal,		"quote",			0 },
230
	{ x_meta_yank,		"yank-pop",			0 },
231
	{ x_mv_back,		"backward-char",		XF_ARG },
232
	{ x_mv_begin,		"beginning-of-line",		0 },
233
	{ x_mv_bword,		"backward-word",		XF_ARG },
234
	{ x_mv_end,		"end-of-line",			0 },
235
	{ x_mv_forw,		"forward-char",			XF_ARG },
236
	{ x_mv_fword,		"forward-word",			XF_ARG },
237
	{ x_newline,		"newline",			0 },
238
	{ x_next_com,		"down-history",			XF_ARG },
239
	{ x_nl_next_com,	"newline-and-next",		0 },
240
	{ x_noop,		"no-op",			0 },
241
	{ x_prev_com,		"up-history",			XF_ARG },
242
	{ x_prev_histword,	"prev-hist-word",		XF_ARG },
243
	{ x_search_char_forw,	"search-character-forward",	XF_ARG },
244
	{ x_search_char_back,	"search-character-backward",	XF_ARG },
245
	{ x_search_hist,	"search-history",		0 },
246
	{ x_set_mark,		"set-mark-command",		0 },
247
	{ x_stuff,		"stuff",			0 },
248
	{ x_stuffreset,		"stuff-reset",			0 },
249
	{ x_transpose,		"transpose-chars",		0 },
250
	{ x_version,		"version",			0 },
251
	{ x_xchg_point_mark,	"exchange-point-and-mark",	0 },
252
	{ x_yank,		"yank",				0 },
253
	{ x_comp_list,		"complete-list",		0 },
254
	{ x_expand,		"expand-file",			0 },
255
	{ x_fold_capitalize,	"capitalize-word",		XF_ARG },
256
	{ x_fold_lower,		"downcase-word",		XF_ARG },
257
	{ x_fold_upper,		"upcase-word",			XF_ARG },
258
	{ x_set_arg,		"set-arg",			XF_NOBIND },
259
	{ x_comment,		"comment",			0 },
260
	{ 0, 0, 0 },
261
#ifdef DEBUG
262
	{ x_debug_info,		"debug-info",			0 },
263
#else
264
	{ 0, 0, 0 },
265
#endif
266
	{ 0, 0, 0 },
267
};
268
269
int
270
isu8cont(unsigned char c)
271
493
{
272
493
	return (c & (0x80 | 0x40)) == 0x80;
273
}
274
275
int
276
x_emacs(char *buf, size_t len)
277
21
{
278
21
	struct kb_entry		*k, *kmatch = NULL;
279
	char			line[LINE + 1];
280
21
	int			at = 0, submatch, ret, c;
281
	const char		*p;
282
283
21
	xbp = xbuf = buf; xend = buf + len;
284
21
	xlp = xcp = xep = buf;
285
21
	*xcp = 0;
286
21
	xlp_valid = true;
287
21
	xmp = NULL;
288
21
	x_histp = histptr + 1;
289
290
21
	xx_cols = x_cols;
291
21
	x_col = promptlen(prompt, &p);
292
21
	prompt_skip = p - prompt;
293
21
	x_adj_ok = 1;
294
21
	prompt_redraw = 1;
295
21
	if (x_col > xx_cols)
296
		x_col = x_col - (x_col / xx_cols) * xx_cols;
297
21
	x_displen = xx_cols - 2 - x_col;
298
21
	x_adj_done = 0;
299
300
21
	pprompt(prompt, 0);
301
21
	if (x_displen < 1) {
302
		x_col = 0;
303
		x_displen = xx_cols - 2;
304
		x_e_putc('\n');
305
		prompt_redraw = 0;
306
	}
307
308
21
	if (x_nextcmd >= 0) {
309
		int off = source->line - x_nextcmd;
310
		if (histptr - history >= off)
311
			x_load_hist(histptr - off);
312
		x_nextcmd = -1;
313
	}
314
315
21
	line[0] = '\0';
316
21
	x_literal_set = 0;
317
21
	x_arg = -1;
318
21
	x_last_command = NULL;
319
	while (1) {
320
73
		x_flush();
321
73
		if ((c = x_e_getc()) < 0)
322
			return 0;
323
324
73
		line[at++] = c;
325
73
		line[at] = '\0';
326
327
73
		if (x_arg == -1) {
328
73
			x_arg = 1;
329
73
			x_arg_defaulted = 1;
330
		}
331
332
73
		if (x_literal_set) {
333
			/* literal, so insert it */
334
			x_literal_set = 0;
335
			submatch = 0;
336
		} else {
337
73
			submatch = 0;
338
73
			kmatch = NULL;
339
6643
			TAILQ_FOREACH(k, &kblist, entry) {
340
6570
				if (at > k->len)
341
					continue;
342
343
6570
				if (memcmp(k->seq, line, at) == 0) {
344
					/* sub match */
345
27
					submatch++;
346
27
					if (k->len == at)
347
27
						kmatch = k;
348
				}
349
350
				/* see if we can abort search early */
351
6570
				if (submatch > 1)
352
					break;
353
			}
354
		}
355
356
73
		if (submatch == 1 && kmatch) {
357

27
			if (kmatch->ftab->xf_func == x_ins_string &&
358
			    kmatch->args && !macro_args) {
359
				/* treat macro string as input */
360
				macro_args = kmatch->args;
361
				ret = KSTD;
362
			} else
363
27
				ret = kmatch->ftab->xf_func(c);
364
		} else {
365
46
			if (submatch)
366
				continue;
367
46
			if (at == 1)
368
46
				ret = x_insert(c);
369
			else
370
				ret = x_error(c); /* not matched meta sequence */
371
		}
372
373

73
		switch (ret) {
374
		case KSTD:
375
52
			if (kmatch)
376
6
				x_last_command = kmatch->ftab->xf_func;
377
			else
378
46
				x_last_command = NULL;
379
			break;
380
		case KEOL:
381
21
			ret = xep - xbuf;
382
21
			return (ret);
383
			break;
384
		case KINTR:
385
			trapsig(SIGINT);
386
			x_mode(false);
387
			unwind(LSHELL);
388
			x_arg = -1;
389
			break;
390
		default:
391
			bi_errorf("invalid return code"); /* can't happen */
392
		}
393
394
		/* reset meta sequence */
395
52
		at = 0;
396
52
		line[0] = '\0';
397
52
		if (x_arg_set)
398
			x_arg_set = 0; /* reset args next time around */
399
		else
400
52
			x_arg = -1;
401
	}
402
}
403
404
static int
405
x_insert(int c)
406
46
{
407
	char	str[2];
408
409
	/*
410
	 *  Should allow tab and control chars.
411
	 */
412
46
	if (c == 0) {
413
		x_e_putc(BEL);
414
		return KSTD;
415
	}
416
46
	str[0] = c;
417
46
	str[1] = '\0';
418
138
	while (x_arg--)
419
46
		x_ins(str);
420
46
	return KSTD;
421
}
422
423
static int
424
x_ins_string(int c)
425
{
426
	return x_insert(c);
427
}
428
429
static int
430
x_do_ins(const char *cp, size_t len)
431
56
{
432
56
	if (xep+len >= xend) {
433
		x_e_putc(BEL);
434
		return -1;
435
	}
436
437
56
	memmove(xcp+len, xcp, xep - xcp + 1);
438
56
	memmove(xcp, cp, len);
439
56
	xcp += len;
440
56
	xep += len;
441
56
	return 0;
442
}
443
444
static int
445
x_ins(char *s)
446
50
{
447
50
	char	*cp = xcp;
448
50
	int	adj = x_adj_done;
449
450
50
	if (x_do_ins(s, strlen(s)) < 0)
451
		return -1;
452
	/*
453
	 * x_zots() may result in a call to x_adjust()
454
	 * we want xcp to reflect the new position.
455
	 */
456
50
	xlp_valid = false;
457
50
	x_lastcp();
458
50
	x_adj_ok = (xcp >= xlp);
459
50
	x_zots(cp);
460
50
	if (adj == x_adj_done) {	/* has x_adjust() been called? */
461
		/* no */
462
100
		for (cp = xlp; cp > xcp; )
463
			x_bs(*--cp);
464
	}
465
466
50
	x_adj_ok = 1;
467
50
	return 0;
468
}
469
470
static int
471
x_del_back(int c)
472
{
473
	int col = xcp - xbuf;
474
475
	if (col == 0) {
476
		x_e_putc(BEL);
477
		return KSTD;
478
	}
479
	if (x_arg > col)
480
		x_arg = col;
481
	while (x_arg < col && isu8cont(xcp[-x_arg]))
482
		x_arg++;
483
	x_goto(xcp - x_arg);
484
	x_delete(x_arg, false);
485
	return KSTD;
486
}
487
488
static int
489
x_del_char(int c)
490
{
491
	int nleft = xep - xcp;
492
493
	if (!nleft) {
494
		x_e_putc(BEL);
495
		return KSTD;
496
	}
497
	if (x_arg > nleft)
498
		x_arg = nleft;
499
	while (x_arg < nleft && isu8cont(xcp[x_arg]))
500
		x_arg++;
501
	x_delete(x_arg, false);
502
	return KSTD;
503
}
504
505
/* Delete nc bytes to the right of the cursor (including cursor position) */
506
static void
507
x_delete(int nc, int push)
508
6
{
509
	int	i,j;
510
	char	*cp;
511
512
6
	if (nc == 0)
513
		return;
514

6
	if (xmp != NULL && xmp > xcp) {
515
		if (xcp + nc > xmp)
516
			xmp = xcp;
517
		else
518
			xmp -= nc;
519
	}
520
521
	/*
522
	 * This lets us yank a word we have deleted.
523
	 */
524
6
	if (push)
525
		x_push(nc);
526
527
6
	xep -= nc;
528
6
	cp = xcp;
529
6
	j = 0;
530
6
	i = nc;
531
37
	while (i--) {
532
25
		j += x_size((unsigned char)*cp++);
533
	}
534
6
	memmove(xcp, xcp+nc, xep - xcp + 1);	/* Copies the null */
535
6
	x_adj_ok = 0;			/* don't redraw */
536
6
	x_zots(xcp);
537
	/*
538
	 * if we are already filling the line,
539
	 * there is no need to ' ','\b'.
540
	 * But if we must, make sure we do the minimum.
541
	 */
542
6
	if ((i = xx_cols - 2 - x_col) > 0) {
543
6
		j = (j < i) ? j : i;
544
6
		i = j;
545
37
		while (i--)
546
25
			x_e_putc(' ');
547
6
		i = j;
548
37
		while (i--)
549
25
			x_e_putc('\b');
550
	}
551
	/*x_goto(xcp);*/
552
6
	x_adj_ok = 1;
553
6
	xlp_valid = false;
554
12
	for (cp = x_lastcp(); cp > xcp; )
555
		x_bs(*--cp);
556
557
	return;
558
}
559
560
static int
561
x_del_bword(int c)
562
{
563
	x_delete(x_bword(), true);
564
	return KSTD;
565
}
566
567
static int
568
x_mv_bword(int c)
569
{
570
	(void)x_bword();
571
	return KSTD;
572
}
573
574
static int
575
x_mv_fword(int c)
576
{
577
	x_goto(xcp + x_fword());
578
	return KSTD;
579
}
580
581
static int
582
x_del_fword(int c)
583
{
584
	x_delete(x_fword(), true);
585
	return KSTD;
586
}
587
588
static int
589
x_bword(void)
590
{
591
	int	nc = 0;
592
	char	*cp = xcp;
593
594
	if (cp == xbuf) {
595
		x_e_putc(BEL);
596
		return 0;
597
	}
598
	while (x_arg--) {
599
		while (cp != xbuf && is_mfs(cp[-1])) {
600
			cp--;
601
			nc++;
602
		}
603
		while (cp != xbuf && !is_mfs(cp[-1])) {
604
			cp--;
605
			nc++;
606
		}
607
	}
608
	x_goto(cp);
609
	return nc;
610
}
611
612
static int
613
x_fword(void)
614
{
615
	int	nc = 0;
616
	char	*cp = xcp;
617
618
	if (cp == xep) {
619
		x_e_putc(BEL);
620
		return 0;
621
	}
622
	while (x_arg--) {
623
		while (cp != xep && is_mfs(*cp)) {
624
			cp++;
625
			nc++;
626
		}
627
		while (cp != xep && !is_mfs(*cp)) {
628
			cp++;
629
			nc++;
630
		}
631
	}
632
	return nc;
633
}
634
635
static void
636
x_goto(char *cp)
637
6
{
638

6
	if (cp < xbp || cp >= (xbp + x_displen)) {
639
		/* we are heading off screen */
640
		xcp = cp;
641
		x_adjust();
642
6
	} else if (cp < xcp) {		/* move back */
643
31
		while (cp < xcp)
644
25
			x_bs((unsigned char)*--xcp);
645
	} else if (cp > xcp) {		/* move forward */
646
		while (cp > xcp)
647
			x_zotc((unsigned char)*xcp++);
648
	}
649
6
}
650
651
static void
652
x_bs(int c)
653
25
{
654
	int i;
655
656
25
	i = x_size(c);
657
75
	while (i--)
658
25
		x_e_putc('\b');
659
25
}
660
661
static int
662
x_size_str(char *cp)
663
{
664
	int size = 0;
665
	while (*cp)
666
		size += x_size(*cp++);
667
	return size;
668
}
669
670
static int
671
x_size(int c)
672
449
{
673
449
	if (c=='\t')
674
		return 4;	/* Kludge, tabs are always four spaces. */
675
449
	if (iscntrl(c))		/* control char */
676
		return 2;
677
449
	if (isu8cont(c))
678
		return 0;
679
449
	return 1;
680
}
681
682
static void
683
x_zots(char *str)
684
68
{
685
68
	int	adj = x_adj_done;
686
687

68
	if (str > xbuf && isu8cont(*str)) {
688
		while (str > xbuf && isu8cont(*str))
689
			str--;
690
		x_e_putc('\b');
691
	}
692
68
	x_lastcp();
693

272
	while (*str && str < xlp && adj == x_adj_done)
694
136
		x_zotc(*str++);
695
68
}
696
697
static void
698
x_zotc(int c)
699
142
{
700
142
	if (c == '\t') {
701
		/*  Kludge, tabs are always four spaces.  */
702
		x_e_puts("    ");
703
142
	} else if (iscntrl(c)) {
704
6
		x_e_putc('^');
705
6
		x_e_putc(UNCTRL(c));
706
	} else
707
136
		x_e_putc(c);
708
142
}
709
710
static int
711
x_mv_back(int c)
712
{
713
	int col = xcp - xbuf;
714
715
	if (col == 0) {
716
		x_e_putc(BEL);
717
		return KSTD;
718
	}
719
	if (x_arg > col)
720
		x_arg = col;
721
	while (x_arg < col && isu8cont(xcp[-x_arg]))
722
		x_arg++;
723
	x_goto(xcp - x_arg);
724
	return KSTD;
725
}
726
727
static int
728
x_mv_forw(int c)
729
{
730
	int nleft = xep - xcp;
731
732
	if (!nleft) {
733
		x_e_putc(BEL);
734
		return KSTD;
735
	}
736
	if (x_arg > nleft)
737
		x_arg = nleft;
738
	while (x_arg < nleft && isu8cont(xcp[x_arg]))
739
		x_arg++;
740
	x_goto(xcp + x_arg);
741
	return KSTD;
742
}
743
744
static int
745
x_search_char_forw(int c)
746
{
747
	char *cp = xcp;
748
749
	*xep = '\0';
750
	c = x_e_getc();
751
	while (x_arg--) {
752
		if (c < 0 ||
753
		    ((cp = (cp == xep) ? NULL : strchr(cp + 1, c)) == NULL &&
754
		    (cp = strchr(xbuf, c)) == NULL)) {
755
			x_e_putc(BEL);
756
			return KSTD;
757
		}
758
	}
759
	x_goto(cp);
760
	return KSTD;
761
}
762
763
static int
764
x_search_char_back(int c)
765
{
766
	char *cp = xcp, *p;
767
768
	c = x_e_getc();
769
	for (; x_arg--; cp = p)
770
		for (p = cp; ; ) {
771
			if (p-- == xbuf)
772
				p = xep;
773
			if (c < 0 || p == cp) {
774
				x_e_putc(BEL);
775
				return KSTD;
776
			}
777
			if (*p == c)
778
				break;
779
		}
780
	x_goto(cp);
781
	return KSTD;
782
}
783
784
static int
785
x_newline(int c)
786
15
{
787
15
	x_e_putc('\r');
788
15
	x_e_putc('\n');
789
15
	x_flush();
790
15
	*xep++ = '\n';
791
15
	return KEOL;
792
}
793
794
static int
795
x_end_of_text(int c)
796
6
{
797
6
	x_zotc(edchars.eof);
798
6
	x_putc('\r');
799
6
	x_putc('\n');
800
6
	x_flush();
801
6
	return KEOL;
802
}
803
804
static int x_beg_hist(int c) { x_load_hist(history); return KSTD;}
805
806
static int x_end_hist(int c) { x_load_hist(histptr); return KSTD;}
807
808
static int x_prev_com(int c) { x_load_hist(x_histp - x_arg); return KSTD;}
809
810
static int x_next_com(int c) { x_load_hist(x_histp + x_arg); return KSTD;}
811
812
/* Goto a particular history number obtained from argument.
813
 * If no argument is given history 1 is probably not what you
814
 * want so we'll simply go to the oldest one.
815
 */
816
static int
817
x_goto_hist(int c)
818
{
819
	if (x_arg_defaulted)
820
		x_load_hist(history);
821
	else
822
		x_load_hist(histptr + x_arg - source->line);
823
	return KSTD;
824
}
825
826
static void
827
x_load_hist(char **hp)
828
{
829
	int	oldsize;
830
831
	if (hp < history || hp > histptr) {
832
		x_e_putc(BEL);
833
		return;
834
	}
835
	x_histp = hp;
836
	oldsize = x_size_str(xbuf);
837
	strlcpy(xbuf, *hp, xend - xbuf);
838
	xbp = xbuf;
839
	xep = xcp = xbuf + strlen(xbuf);
840
	xlp_valid = false;
841
	if (xep <= x_lastcp())
842
		x_redraw(oldsize);
843
	x_goto(xep);
844
}
845
846
static int
847
x_nl_next_com(int c)
848
{
849
	x_nextcmd = source->line - (histptr - x_histp) + 1;
850
	return (x_newline(c));
851
}
852
853
static int
854
x_eot_del(int c)
855
6
{
856

6
	if (xep == xbuf && x_arg_defaulted)
857
6
		return (x_end_of_text(c));
858
	else
859
		return (x_del_char(c));
860
}
861
862
static void *
863
kb_find_hist_func(char c)
864
{
865
	struct kb_entry		*k;
866
	char			line[LINE + 1];
867
868
	line[0] = c;
869
	line[1] = '\0';
870
	TAILQ_FOREACH(k, &kblist, entry)
871
		if (!strcmp(k->seq, line))
872
			return (k->ftab->xf_func);
873
874
	return (x_insert);
875
}
876
877
/* reverse incremental history search */
878
static int
879
x_search_hist(int c)
880
{
881
	int offset = -1;	/* offset of match in xbuf, else -1 */
882
	char pat [256+1];	/* pattern buffer */
883
	char *p = pat;
884
	int (*f)(int);
885
886
	*p = '\0';
887
	while (1) {
888
		if (offset < 0) {
889
			x_e_puts("\nI-search: ");
890
			x_e_puts(pat);
891
		}
892
		x_flush();
893
		if ((c = x_e_getc()) < 0)
894
			return KSTD;
895
		f = kb_find_hist_func(c);
896
		if (c == CTRL('['))
897
			break;
898
		else if (f == x_search_hist)
899
			offset = x_search(pat, 0, offset);
900
		else if (f == x_del_back) {
901
			if (p == pat) {
902
				offset = -1;
903
				break;
904
			}
905
			if (p > pat)
906
				*--p = '\0';
907
			if (p == pat)
908
				offset = -1;
909
			else
910
				offset = x_search(pat, 1, offset);
911
			continue;
912
		} else if (f == x_insert) {
913
			/* add char to pattern */
914
			/* overflow check... */
915
			if (p >= &pat[sizeof(pat) - 1]) {
916
				x_e_putc(BEL);
917
				continue;
918
			}
919
			*p++ = c, *p = '\0';
920
			if (offset >= 0) {
921
				/* already have partial match */
922
				offset = x_match(xbuf, pat);
923
				if (offset >= 0) {
924
					x_goto(xbuf + offset + (p - pat) -
925
					    (*pat == '^'));
926
					continue;
927
				}
928
			}
929
			offset = x_search(pat, 0, offset);
930
		} else { /* other command */
931
			x_e_ungetc(c);
932
			break;
933
		}
934
	}
935
	if (offset < 0)
936
		x_redraw(-1);
937
	return KSTD;
938
}
939
940
/* search backward from current line */
941
static int
942
x_search(char *pat, int sameline, int offset)
943
{
944
	char **hp;
945
	int i;
946
947
	for (hp = x_histp - (sameline ? 0 : 1) ; hp >= history; --hp) {
948
		i = x_match(*hp, pat);
949
		if (i >= 0) {
950
			if (offset < 0)
951
				x_e_putc('\n');
952
			x_load_hist(hp);
953
			x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
954
			return i;
955
		}
956
	}
957
	x_e_putc(BEL);
958
	x_histp = histptr;
959
	return -1;
960
}
961
962
/* return position of first match of pattern in string, else -1 */
963
static int
964
x_match(char *str, char *pat)
965
{
966
	if (*pat == '^') {
967
		return (strncmp(str, pat+1, strlen(pat+1)) == 0) ? 0 : -1;
968
	} else {
969
		char *q = strstr(str, pat);
970
		return (q == NULL) ? -1 : q - str;
971
	}
972
}
973
974
static int
975
x_del_line(int c)
976
{
977
	int	i, j;
978
979
	*xep = 0;
980
	i = xep - xbuf;
981
	j = x_size_str(xbuf);
982
	xcp = xbuf;
983
	x_push(i);
984
	xlp = xbp = xep = xbuf;
985
	xlp_valid = true;
986
	*xcp = 0;
987
	xmp = NULL;
988
	x_redraw(j);
989
	return KSTD;
990
}
991
992
static int
993
x_mv_end(int c)
994
{
995
	x_goto(xep);
996
	return KSTD;
997
}
998
999
static int
1000
x_mv_begin(int c)
1001
{
1002
	x_goto(xbuf);
1003
	return KSTD;
1004
}
1005
1006
static int
1007
x_draw_line(int c)
1008
{
1009
	x_redraw(-1);
1010
	return KSTD;
1011
1012
}
1013
1014
/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
1015
 * on a NEW line, otherwise limit is the screen column up to which needs
1016
 * redrawing.
1017
 */
1018
static void
1019
x_redraw(int limit)
1020
12
{
1021
12
	int	i, j, truncate = 0;
1022
	char	*cp;
1023
1024
12
	x_adj_ok = 0;
1025
12
	if (limit == -1)
1026
		x_e_putc('\n');
1027
	else
1028
12
		x_e_putc('\r');
1029
12
	x_flush();
1030
12
	if (xbp == xbuf) {
1031
12
		x_col = promptlen(prompt, NULL);
1032
12
		if (x_col > xx_cols)
1033
			truncate = (x_col / xx_cols) * xx_cols;
1034
12
		if (prompt_redraw)
1035
12
			pprompt(prompt + prompt_skip, truncate);
1036
	}
1037
12
	if (x_col > xx_cols)
1038
		x_col = x_col - (x_col / xx_cols) * xx_cols;
1039
12
	x_displen = xx_cols - 2 - x_col;
1040
12
	if (x_displen < 1) {
1041
		x_col = 0;
1042
		x_displen = xx_cols - 2;
1043
	}
1044
12
	xlp_valid = false;
1045
12
	cp = x_lastcp();
1046
12
	x_zots(xbp);
1047

12
	if (xbp != xbuf || xep > xlp)
1048
		limit = xx_cols;
1049
12
	if (limit >= 0) {
1050
12
		if (xep > xlp)
1051
			i = 0;			/* we fill the line */
1052
		else
1053
12
			i = limit - (xlp - xbp);
1054
1055

601
		for (j = 0; j < i && x_col < (xx_cols - 2); j++)
1056
589
			x_e_putc(' ');
1057
12
		i = ' ';
1058
12
		if (xep > xlp) {		/* more off screen */
1059
			if (xbp > xbuf)
1060
				i = '*';
1061
			else
1062
				i = '>';
1063
12
		} else if (xbp > xbuf)
1064
			i = '<';
1065
12
		x_e_putc(i);
1066
12
		j++;
1067
625
		while (j--)
1068
601
			x_e_putc('\b');
1069
	}
1070
24
	for (cp = xlp; cp > xcp; )
1071
		x_bs(*--cp);
1072
12
	x_adj_ok = 1;
1073
#ifdef DEBUG
1074
	x_flush();
1075
#endif
1076
	return;
1077
}
1078
1079
static int
1080
x_transpose(int c)
1081
{
1082
	char	tmp;
1083
1084
	/* What transpose is meant to do seems to be up for debate. This
1085
	 * is a general summary of the options; the text is abcd with the
1086
	 * upper case character or underscore indicating the cursor position:
1087
	 *     Who			Before	After  Before	After
1088
	 *     at&t ksh in emacs mode:	abCd	abdC   abcd_	(bell)
1089
	 *     at&t ksh in gmacs mode:	abCd	baCd   abcd_	abdc_
1090
	 *     gnu emacs:		abCd	acbD   abcd_	abdc_
1091
	 * Pdksh currently goes with GNU behavior since I believe this is the
1092
	 * most common version of emacs, unless in gmacs mode, in which case
1093
	 * it does the at&t ksh gmacs mode.
1094
	 * This should really be broken up into 3 functions so users can bind
1095
	 * to the one they want.
1096
	 */
1097
	if (xcp == xbuf) {
1098
		x_e_putc(BEL);
1099
		return KSTD;
1100
	} else if (xcp == xep || Flag(FGMACS)) {
1101
		if (xcp - xbuf == 1) {
1102
			x_e_putc(BEL);
1103
			return KSTD;
1104
		}
1105
		/* Gosling/Unipress emacs style: Swap two characters before the
1106
		 * cursor, do not change cursor position
1107
		 */
1108
		x_bs(xcp[-1]);
1109
		x_bs(xcp[-2]);
1110
		x_zotc(xcp[-1]);
1111
		x_zotc(xcp[-2]);
1112
		tmp = xcp[-1];
1113
		xcp[-1] = xcp[-2];
1114
		xcp[-2] = tmp;
1115
	} else {
1116
		/* GNU emacs style: Swap the characters before and under the
1117
		 * cursor, move cursor position along one.
1118
		 */
1119
		x_bs(xcp[-1]);
1120
		x_zotc(xcp[0]);
1121
		x_zotc(xcp[-1]);
1122
		tmp = xcp[-1];
1123
		xcp[-1] = xcp[0];
1124
		xcp[0] = tmp;
1125
		x_bs(xcp[0]);
1126
		x_goto(xcp + 1);
1127
	}
1128
	return KSTD;
1129
}
1130
1131
static int
1132
x_literal(int c)
1133
{
1134
	x_literal_set = 1;
1135
	return KSTD;
1136
}
1137
1138
static int
1139
x_kill(int c)
1140
{
1141
	int col = xcp - xbuf;
1142
	int lastcol = xep - xbuf;
1143
	int ndel;
1144
1145
	if (x_arg_defaulted)
1146
		x_arg = lastcol;
1147
	else if (x_arg > lastcol)
1148
		x_arg = lastcol;
1149
	while (x_arg < lastcol && isu8cont(xbuf[x_arg]))
1150
		x_arg++;
1151
	ndel = x_arg - col;
1152
	if (ndel < 0) {
1153
		x_goto(xbuf + x_arg);
1154
		ndel = -ndel;
1155
	}
1156
	x_delete(ndel, true);
1157
	return KSTD;
1158
}
1159
1160
static void
1161
x_push(int nchars)
1162
{
1163
	char	*cp = str_nsave(xcp, nchars, AEDIT);
1164
	afree(killstack[killsp], AEDIT);
1165
	killstack[killsp] = cp;
1166
	killsp = (killsp + 1) % KILLSIZE;
1167
}
1168
1169
static int
1170
x_yank(int c)
1171
{
1172
	if (killsp == 0)
1173
		killtp = KILLSIZE;
1174
	else
1175
		killtp = killsp;
1176
	killtp --;
1177
	if (killstack[killtp] == 0) {
1178
		x_e_puts("\nnothing to yank");
1179
		x_redraw(-1);
1180
		return KSTD;
1181
	}
1182
	xmp = xcp;
1183
	x_ins(killstack[killtp]);
1184
	return KSTD;
1185
}
1186
1187
static int
1188
x_meta_yank(int c)
1189
{
1190
	int	len;
1191
	if ((x_last_command != x_yank && x_last_command != x_meta_yank) ||
1192
	    killstack[killtp] == 0) {
1193
		killtp = killsp;
1194
		x_e_puts("\nyank something first");
1195
		x_redraw(-1);
1196
		return KSTD;
1197
	}
1198
	len = strlen(killstack[killtp]);
1199
	x_goto(xcp - len);
1200
	x_delete(len, false);
1201
	do {
1202
		if (killtp == 0)
1203
			killtp = KILLSIZE - 1;
1204
		else
1205
			killtp--;
1206
	} while (killstack[killtp] == 0);
1207
	x_ins(killstack[killtp]);
1208
	return KSTD;
1209
}
1210
1211
static int
1212
x_abort(int c)
1213
{
1214
	/* x_zotc(c); */
1215
	xlp = xep = xcp = xbp = xbuf;
1216
	xlp_valid = true;
1217
	*xcp = 0;
1218
	return KINTR;
1219
}
1220
1221
static int
1222
x_error(int c)
1223
{
1224
	x_e_putc(BEL);
1225
	return KSTD;
1226
}
1227
1228
static int
1229
x_stuffreset(int c)
1230
{
1231
#ifdef TIOCSTI
1232
	(void)x_stuff(c);
1233
	return KINTR;
1234
#else
1235
	x_zotc(c);
1236
	xlp = xcp = xep = xbp = xbuf;
1237
	xlp_valid = true;
1238
	*xcp = 0;
1239
	x_redraw(-1);
1240
	return KSTD;
1241
#endif
1242
}
1243
1244
static int
1245
x_stuff(int c)
1246
{
1247
#ifdef TIOCSTI
1248
	char	ch = c;
1249
	bool	savmode = x_mode(false);
1250
1251
	(void)ioctl(TTY, TIOCSTI, &ch);
1252
	(void)x_mode(savmode);
1253
	x_redraw(-1);
1254
#endif
1255
	return KSTD;
1256
}
1257
1258
static char *
1259
kb_encode(const char *s)
1260
{
1261
	static char		l[LINE + 1];
1262
	int			at = 0;
1263
1264
	l[at] = '\0';
1265
	while (*s) {
1266
		if (*s == '^') {
1267
			s++;
1268
			if (*s >= '?')
1269
				l[at++] = CTRL(*s);
1270
			else {
1271
				l[at++] = '^';
1272
				s--;
1273
			}
1274
		} else
1275
			l[at++] = *s;
1276
		l[at] = '\0';
1277
		s++;
1278
	}
1279
	return (l);
1280
}
1281
1282
static char *
1283
kb_decode(const char *s)
1284
{
1285
	static char		l[LINE + 1];
1286
	int			i, at = 0;
1287
1288
	l[0] = '\0';
1289
	for (i = 0; i < strlen(s); i++) {
1290
		if (iscntrl((unsigned char)s[i])) {
1291
			l[at++] = '^';
1292
			l[at++] = UNCTRL(s[i]);
1293
		} else
1294
			l[at++] = s[i];
1295
		l[at] = '\0';
1296
	}
1297
1298
	return (l);
1299
}
1300
1301
static int
1302
kb_match(char *s)
1303
5523
{
1304
5523
	int			len = strlen(s);
1305
	struct kb_entry		*k;
1306
1307
243516
	TAILQ_FOREACH(k, &kblist, entry) {
1308
238014
		if (len > k->len)
1309
110355
			continue;
1310
1311
127659
		if (memcmp(k->seq, s, len) == 0)
1312
21
			return (1);
1313
	}
1314
1315
5502
	return (0);
1316
}
1317
1318
static void
1319
kb_del(struct kb_entry *k)
1320
{
1321
	TAILQ_REMOVE(&kblist, k, entry);
1322
	free(k->args);
1323
	afree(k, AEDIT);
1324
}
1325
1326
static struct kb_entry *
1327
kb_add_string(void *func, void *args, char *str)
1328
5523
{
1329
	int			i, count;
1330
	struct kb_entry		*k;
1331
5523
	struct x_ftab		*xf = NULL;
1332
1333
170058
	for (i = 0; i < NELEM(x_ftab); i++)
1334
170058
		if (x_ftab[i].xf_func == func) {
1335
5523
			xf = (struct x_ftab *)&x_ftab[i];
1336
5523
			break;
1337
		}
1338
5523
	if (xf == NULL)
1339
		return (NULL);
1340
1341
5523
	if (kb_match(str)) {
1342
21
		if (x_bind_quiet == 0)
1343
			bi_errorf("duplicate binding for %s", kb_decode(str));
1344
21
		return (NULL);
1345
	}
1346
5502
	count = strlen(str);
1347
1348
5502
	k = alloc(sizeof *k + count + 1, AEDIT);
1349
5502
	k->seq = (unsigned char *)(k + 1);
1350
5502
	k->len = count;
1351
5502
	k->ftab = xf;
1352
5502
	k->args = args ? strdup(args) : NULL;
1353
1354
5502
	strlcpy(k->seq, str, count + 1);
1355
1356
5502
	TAILQ_INSERT_TAIL(&kblist, k, entry);
1357
1358
5502
	return (k);
1359
}
1360
1361
static struct kb_entry *
1362
kb_add(void *func, void *args, ...)
1363
5523
{
1364
	va_list			ap;
1365
	int			i, count;
1366
	char			l[LINE + 1];
1367
1368
5523
	va_start(ap, args);
1369
5523
	count = 0;
1370

22687
	while (va_arg(ap, unsigned int) != 0)
1371
11641
		count++;
1372
5523
	va_end(ap);
1373
1374
5523
	va_start(ap, args);
1375
22687
	for (i = 0; i <= count /* <= is correct */; i++)
1376
17164
		l[i] = (unsigned char)va_arg(ap, unsigned int);
1377
5523
	va_end(ap);
1378
1379
5523
	return (kb_add_string(func, args, l));
1380
}
1381
1382
static void
1383
kb_print(struct kb_entry *k)
1384
{
1385
	if (!(k->ftab->xf_flags & XF_NOBIND))
1386
		shprintf("%s = %s\n",
1387
		    kb_decode(k->seq), k->ftab->xf_name);
1388
	else if (k->args) {
1389
		shprintf("%s = ", kb_decode(k->seq));
1390
		shprintf("'%s'\n", kb_decode(k->args));
1391
	}
1392
}
1393
1394
int
1395
x_bind(const char *a1, const char *a2,
1396
	int macro,		/* bind -m */
1397
	int list)		/* bind -l */
1398
{
1399
	int			i;
1400
	struct kb_entry		*k, *kb;
1401
	char			in[LINE + 1];
1402
1403
	if (x_tty == 0) {
1404
		bi_errorf("cannot bind, not a tty");
1405
		return (1);
1406
	}
1407
1408
	if (list) {
1409
		/* show all function names */
1410
		for (i = 0; i < NELEM(x_ftab); i++) {
1411
			if (x_ftab[i].xf_name == NULL)
1412
				continue;
1413
			if (x_ftab[i].xf_name &&
1414
			    !(x_ftab[i].xf_flags & XF_NOBIND))
1415
				shprintf("%s\n", x_ftab[i].xf_name);
1416
		}
1417
		return (0);
1418
	}
1419
1420
	if (a1 == NULL) {
1421
		/* show all bindings */
1422
		TAILQ_FOREACH(k, &kblist, entry)
1423
			kb_print(k);
1424
		return (0);
1425
	}
1426
1427
	snprintf(in, sizeof in, "%s", kb_encode(a1));
1428
	if (a2 == NULL) {
1429
		/* print binding */
1430
		TAILQ_FOREACH(k, &kblist, entry)
1431
			if (!strcmp(k->seq, in)) {
1432
				kb_print(k);
1433
				return (0);
1434
			}
1435
		shprintf("%s = %s\n", kb_decode(a1), "auto-insert");
1436
		return (0);
1437
	}
1438
1439
	if (strlen(a2) == 0) {
1440
		/* clear binding */
1441
		TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
1442
			if (!strcmp(k->seq, in)) {
1443
				kb_del(k);
1444
				break;
1445
			}
1446
		return (0);
1447
	}
1448
1449
	/* set binding */
1450
	if (macro) {
1451
		/* delete old mapping */
1452
		TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
1453
			if (!strcmp(k->seq, in)) {
1454
				kb_del(k);
1455
				break;
1456
			}
1457
		kb_add_string(x_ins_string, kb_encode(a2), in);
1458
		return (0);
1459
	}
1460
1461
	/* set non macro binding */
1462
	for (i = 0; i < NELEM(x_ftab); i++) {
1463
		if (x_ftab[i].xf_name == NULL)
1464
			continue;
1465
		if (!strcmp(x_ftab[i].xf_name, a2)) {
1466
			/* delete old mapping */
1467
			TAILQ_FOREACH_SAFE(k, &kblist, entry, kb)
1468
				if (!strcmp(k->seq, in)) {
1469
					kb_del(k);
1470
					break;
1471
				}
1472
			kb_add_string(x_ftab[i].xf_func, NULL, in);
1473
			return (0);
1474
		}
1475
	}
1476
	bi_errorf("%s: no such function", a2);
1477
	return (1);
1478
}
1479
1480
void
1481
x_init_emacs(void)
1482
63
{
1483
	char *locale;
1484
1485
63
	x_tty = 1;
1486
63
	ainit(AEDIT);
1487
63
	x_nextcmd = -1;
1488
1489
	/* Determine if we can translate meta key or use 8-bit AscII
1490
	 * XXX - It would be nice if there was a locale attribute to
1491
	 * determine if the locale is 7-bit or not.
1492
	 */
1493
63
	locale = setlocale(LC_CTYPE, NULL);
1494

63
	if (locale == NULL || !strcmp(locale, "C") || !strcmp(locale, "POSIX"))
1495
63
		Flag(FEMACSUSEMETA) = 1;
1496
1497
	/* new keybinding stuff */
1498
63
	TAILQ_INIT(&kblist);
1499
1500
	/* man page order */
1501
63
	kb_add(x_abort,			NULL, CTRL('G'), 0);
1502
63
	kb_add(x_mv_back,		NULL, CTRL('B'), 0);
1503
63
	kb_add(x_mv_back,		NULL, CTRL('X'), CTRL('D'), 0);
1504
63
	kb_add(x_mv_bword,		NULL, CTRL('['), 'b', 0);
1505
63
	kb_add(x_beg_hist,		NULL, CTRL('['), '<', 0);
1506
63
	kb_add(x_mv_begin,		NULL, CTRL('A'), 0);
1507
63
	kb_add(x_fold_capitalize,	NULL, CTRL('['), 'C', 0);
1508
63
	kb_add(x_fold_capitalize,	NULL, CTRL('['), 'c', 0);
1509
63
	kb_add(x_comment,		NULL, CTRL('['), '#', 0);
1510
63
	kb_add(x_complete,		NULL, CTRL('['), CTRL('['), 0);
1511
63
	kb_add(x_comp_comm,		NULL, CTRL('X'), CTRL('['), 0);
1512
63
	kb_add(x_comp_file,		NULL, CTRL('['), CTRL('X'), 0);
1513
63
	kb_add(x_comp_list,		NULL, CTRL('I'), 0);
1514
63
	kb_add(x_comp_list,		NULL, CTRL('['), '=', 0);
1515
63
	kb_add(x_del_back,		NULL, CTRL('?'), 0);
1516
63
	kb_add(x_del_back,		NULL, CTRL('H'), 0);
1517
63
	kb_add(x_del_char,		NULL, CTRL('['), '[', '3', '~', 0); /* delete */
1518
63
	kb_add(x_del_bword,		NULL, CTRL('['), CTRL('?'), 0);
1519
63
	kb_add(x_del_bword,		NULL, CTRL('['), CTRL('H'), 0);
1520
63
	kb_add(x_del_bword,		NULL, CTRL('['), 'h', 0);
1521
63
	kb_add(x_del_fword,		NULL, CTRL('['), 'd', 0);
1522
63
	kb_add(x_next_com,		NULL, CTRL('N'), 0);
1523
63
	kb_add(x_next_com,		NULL, CTRL('X'), 'B', 0);
1524
63
	kb_add(x_fold_lower,		NULL, CTRL('['), 'L', 0);
1525
63
	kb_add(x_fold_lower,		NULL, CTRL('['), 'l', 0);
1526
63
	kb_add(x_end_hist,		NULL, CTRL('['), '>', 0);
1527
63
	kb_add(x_mv_end,		NULL, CTRL('E'), 0);
1528
	/* how to handle: eot: ^_, underneath copied from original keybindings */
1529
63
	kb_add(x_end_of_text,		NULL, CTRL('_'), 0);
1530
63
	kb_add(x_eot_del,		NULL, CTRL('D'), 0);
1531
	/* error */
1532
63
	kb_add(x_xchg_point_mark,	NULL, CTRL('X'), CTRL('X'), 0);
1533
63
	kb_add(x_expand,		NULL, CTRL('['), '*', 0);
1534
63
	kb_add(x_mv_forw,		NULL, CTRL('F'), 0);
1535
63
	kb_add(x_mv_forw,		NULL, CTRL('X'), 'C', 0);
1536
63
	kb_add(x_mv_fword,		NULL, CTRL('['), 'f', 0);
1537
63
	kb_add(x_goto_hist,		NULL, CTRL('['), 'g', 0);
1538
	/* kill-line */
1539
63
	kb_add(x_del_bword,		NULL, CTRL('W'), 0); /* not what man says */
1540
63
	kb_add(x_kill,			NULL, CTRL('K'), 0);
1541
63
	kb_add(x_enumerate,		NULL, CTRL('['), '?', 0);
1542
63
	kb_add(x_list_comm,		NULL, CTRL('X'), '?', 0);
1543
63
	kb_add(x_list_file,		NULL, CTRL('X'), CTRL('Y'), 0);
1544
63
	kb_add(x_newline,		NULL, CTRL('J'), 0);
1545
63
	kb_add(x_newline,		NULL, CTRL('M'), 0);
1546
63
	kb_add(x_nl_next_com,		NULL, CTRL('O'), 0);
1547
	/* no-op */
1548
63
	kb_add(x_prev_histword,		NULL, CTRL('['), '.', 0);
1549
63
	kb_add(x_prev_histword,		NULL, CTRL('['), '_', 0);
1550
	/* how to handle: quote: ^^ */
1551
63
	kb_add(x_draw_line,		NULL, CTRL('L'), 0);
1552
63
	kb_add(x_search_char_back,	NULL, CTRL('['), CTRL(']'), 0);
1553
63
	kb_add(x_search_char_forw,	NULL, CTRL(']'), 0);
1554
63
	kb_add(x_search_hist,		NULL, CTRL('R'), 0);
1555
63
	kb_add(x_set_mark,		NULL, CTRL('['), ' ', 0);
1556
#if defined(TIOCSTI)
1557
	kb_add(x_stuff,			NULL, CTRL('T'), 0);
1558
	/* stuff-reset */
1559
#else
1560
63
	kb_add(x_transpose,		NULL, CTRL('T'), 0);
1561
#endif
1562
63
	kb_add(x_prev_com,		NULL, CTRL('P'), 0);
1563
63
	kb_add(x_prev_com,		NULL, CTRL('X'), 'A', 0);
1564
63
	kb_add(x_fold_upper,		NULL, CTRL('['), 'U', 0);
1565
63
	kb_add(x_fold_upper,		NULL, CTRL('['), 'u', 0);
1566
63
	kb_add(x_literal,		NULL, CTRL('V'), 0);
1567
63
	kb_add(x_literal,		NULL, CTRL('^'), 0);
1568
63
	kb_add(x_yank,			NULL, CTRL('Y'), 0);
1569
63
	kb_add(x_meta_yank,		NULL, CTRL('['), 'y', 0);
1570
	/* man page ends here */
1571
1572
	/* arrow keys */
1573
63
	kb_add(x_prev_com,		NULL, CTRL('['), '[', 'A', 0); /* up */
1574
63
	kb_add(x_next_com,		NULL, CTRL('['), '[', 'B', 0); /* down */
1575
63
	kb_add(x_mv_forw,		NULL, CTRL('['), '[', 'C', 0); /* right */
1576
63
	kb_add(x_mv_back,		NULL, CTRL('['), '[', 'D', 0); /* left */
1577
63
	kb_add(x_prev_com,		NULL, CTRL('['), 'O', 'A', 0); /* up */
1578
63
	kb_add(x_next_com,		NULL, CTRL('['), 'O', 'B', 0); /* down */
1579
63
	kb_add(x_mv_forw,		NULL, CTRL('['), 'O', 'C', 0); /* right */
1580
63
	kb_add(x_mv_back,		NULL, CTRL('['), 'O', 'D', 0); /* left */
1581
1582
	/* more navigation keys */
1583
63
	kb_add(x_mv_begin,		NULL, CTRL('['), '[', 'H', 0); /* home */
1584
63
	kb_add(x_mv_end,		NULL, CTRL('['), '[', 'F', 0); /* end */
1585
63
	kb_add(x_mv_begin,		NULL, CTRL('['), 'O', 'H', 0); /* home */
1586
63
	kb_add(x_mv_end,		NULL, CTRL('['), 'O', 'F', 0); /* end */
1587
63
	kb_add(x_mv_begin,		NULL, CTRL('['), '[', '1', '~', 0); /* home */
1588
63
	kb_add(x_mv_end,		NULL, CTRL('['), '[', '4', '~', 0); /* end */
1589
1590
	/* can't be bound */
1591
63
	kb_add(x_set_arg,		NULL, CTRL('['), '0', 0);
1592
63
	kb_add(x_set_arg,		NULL, CTRL('['), '1', 0);
1593
63
	kb_add(x_set_arg,		NULL, CTRL('['), '2', 0);
1594
63
	kb_add(x_set_arg,		NULL, CTRL('['), '3', 0);
1595
63
	kb_add(x_set_arg,		NULL, CTRL('['), '4', 0);
1596
63
	kb_add(x_set_arg,		NULL, CTRL('['), '5', 0);
1597
63
	kb_add(x_set_arg,		NULL, CTRL('['), '6', 0);
1598
63
	kb_add(x_set_arg,		NULL, CTRL('['), '7', 0);
1599
63
	kb_add(x_set_arg,		NULL, CTRL('['), '8', 0);
1600
63
	kb_add(x_set_arg,		NULL, CTRL('['), '9', 0);
1601
1602
	/* ctrl arrow keys */
1603
63
	kb_add(x_mv_end,		NULL, CTRL('['), '[', '1', ';', '5', 'A', 0); /* ctrl up */
1604
63
	kb_add(x_mv_begin,		NULL, CTRL('['), '[', '1', ';', '5', 'B', 0); /* ctrl down */
1605
63
	kb_add(x_mv_fword,		NULL, CTRL('['), '[', '1', ';', '5', 'C', 0); /* ctrl right */
1606
63
	kb_add(x_mv_bword,		NULL, CTRL('['), '[', '1', ';', '5', 'D', 0); /* ctrl left */
1607
63
}
1608
1609
void
1610
x_emacs_keys(X_chars *ec)
1611
7
{
1612
7
	x_bind_quiet = 1;
1613
7
	if (ec->erase >= 0) {
1614
7
		kb_add(x_del_back, NULL, ec->erase, 0);
1615
7
		kb_add(x_del_bword, NULL, CTRL('['), ec->erase, 0);
1616
	}
1617
7
	if (ec->kill >= 0)
1618
7
		kb_add(x_del_line, NULL, ec->kill, 0);
1619
7
	if (ec->werase >= 0)
1620
7
		kb_add(x_del_bword, NULL, ec->werase, 0);
1621
7
	if (ec->intr >= 0)
1622
7
		kb_add(x_abort, NULL, ec->intr, 0);
1623
7
	if (ec->quit >= 0)
1624
7
		kb_add(x_noop, NULL, ec->quit, 0);
1625
7
	x_bind_quiet = 0;
1626
7
}
1627
1628
static int
1629
x_set_mark(int c)
1630
{
1631
	xmp = xcp;
1632
	return KSTD;
1633
}
1634
1635
static int
1636
x_kill_region(int c)
1637
{
1638
	int	rsize;
1639
	char	*xr;
1640
1641
	if (xmp == NULL) {
1642
		x_e_putc(BEL);
1643
		return KSTD;
1644
	}
1645
	if (xmp > xcp) {
1646
		rsize = xmp - xcp;
1647
		xr = xcp;
1648
	} else {
1649
		rsize = xcp - xmp;
1650
		xr = xmp;
1651
	}
1652
	x_goto(xr);
1653
	x_delete(rsize, true);
1654
	xmp = xr;
1655
	return KSTD;
1656
}
1657
1658
static int
1659
x_xchg_point_mark(int c)
1660
{
1661
	char	*tmp;
1662
1663
	if (xmp == NULL) {
1664
		x_e_putc(BEL);
1665
		return KSTD;
1666
	}
1667
	tmp = xmp;
1668
	xmp = xcp;
1669
	x_goto( tmp );
1670
	return KSTD;
1671
}
1672
1673
static int
1674
x_version(int c)
1675
{
1676
	char *o_xbuf = xbuf, *o_xend = xend;
1677
	char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
1678
	int lim = x_lastcp() - xbp;
1679
1680
	xbuf = xbp = xcp = (char *) ksh_version + 4;
1681
	xend = xep = (char *) ksh_version + 4 + strlen(ksh_version + 4);
1682
	x_redraw(lim);
1683
	x_flush();
1684
1685
	c = x_e_getc();
1686
	xbuf = o_xbuf;
1687
	xend = o_xend;
1688
	xbp = o_xbp;
1689
	xep = o_xep;
1690
	xcp = o_xcp;
1691
	x_redraw(strlen(ksh_version));
1692
1693
	if (c < 0)
1694
		return KSTD;
1695
	/* This is what at&t ksh seems to do...  Very bizarre */
1696
	if (c != ' ')
1697
		x_e_ungetc(c);
1698
1699
	return KSTD;
1700
}
1701
1702
static int
1703
x_noop(int c)
1704
{
1705
	return KSTD;
1706
}
1707
1708
/*
1709
 *	File/command name completion routines
1710
 */
1711
1712
static int
1713
x_comp_comm(int c)
1714
{
1715
	do_complete(XCF_COMMAND, CT_COMPLETE);
1716
	return KSTD;
1717
}
1718
static int
1719
x_list_comm(int c)
1720
{
1721
	do_complete(XCF_COMMAND, CT_LIST);
1722
	return KSTD;
1723
}
1724
static int
1725
x_complete(int c)
1726
{
1727
	do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
1728
	return KSTD;
1729
}
1730
static int
1731
x_enumerate(int c)
1732
{
1733
	do_complete(XCF_COMMAND_FILE, CT_LIST);
1734
	return KSTD;
1735
}
1736
static int
1737
x_comp_file(int c)
1738
{
1739
	do_complete(XCF_FILE, CT_COMPLETE);
1740
	return KSTD;
1741
}
1742
static int
1743
x_list_file(int c)
1744
{
1745
	do_complete(XCF_FILE, CT_LIST);
1746
	return KSTD;
1747
}
1748
static int
1749
x_comp_list(int c)
1750
6
{
1751
6
	do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
1752
6
	return KSTD;
1753
}
1754
static int
1755
x_expand(int c)
1756
{
1757
	char **words;
1758
	int nwords = 0;
1759
	int start, end;
1760
	int is_command;
1761
	int i;
1762
1763
	nwords = x_cf_glob(XCF_FILE, xbuf, xep - xbuf, xcp - xbuf,
1764
	    &start, &end, &words, &is_command);
1765
1766
	if (nwords == 0) {
1767
		x_e_putc(BEL);
1768
		return KSTD;
1769
	}
1770
1771
	x_goto(xbuf + start);
1772
	x_delete(end - start, false);
1773
	for (i = 0; i < nwords;) {
1774
		if (x_escape(words[i], strlen(words[i]), x_do_ins) < 0 ||
1775
		    (++i < nwords && x_ins(" ") < 0)) {
1776
			x_e_putc(BEL);
1777
			return KSTD;
1778
		}
1779
	}
1780
	x_adjust();
1781
1782
	return KSTD;
1783
}
1784
1785
/* type == 0 for list, 1 for complete and 2 for complete-list */
1786
static void
1787
do_complete(int flags,	/* XCF_{COMMAND,FILE,COMMAND_FILE} */
1788
    Comp_type type)
1789
6
{
1790
	char **words;
1791
	int nwords;
1792
	int start, end, nlen, olen;
1793
	int is_command;
1794
6
	int completed = 0;
1795
1796
6
	nwords = x_cf_glob(flags, xbuf, xep - xbuf, xcp - xbuf,
1797
	    &start, &end, &words, &is_command);
1798
	/* no match */
1799
6
	if (nwords == 0) {
1800
		x_e_putc(BEL);
1801
		return;
1802
	}
1803
1804
6
	if (type == CT_LIST) {
1805
		x_print_expansions(nwords, words, is_command);
1806
		x_redraw(0);
1807
		x_free_words(nwords, words);
1808
		return;
1809
	}
1810
1811
6
	olen = end - start;
1812
6
	nlen = x_longest_prefix(nwords, words);
1813
	/* complete */
1814
6
	if (nwords == 1 || nlen > olen) {
1815
6
		x_goto(xbuf + start);
1816
6
		x_delete(olen, false);
1817
6
		x_escape(words[0], nlen, x_do_ins);
1818
6
		x_adjust();
1819
6
		completed = 1;
1820
	}
1821
	/* add space if single non-dir match */
1822

6
	if (nwords == 1 && words[0][nlen - 1] != '/') {
1823
4
		x_ins(" ");
1824
4
		completed = 1;
1825
	}
1826
1827
6
	if (type == CT_COMPLIST && !completed) {
1828
		x_print_expansions(nwords, words, is_command);
1829
		completed = 1;
1830
	}
1831
1832
6
	if (completed)
1833
6
		x_redraw(0);
1834
1835
6
	x_free_words(nwords, words);
1836
}
1837
1838
/* NAME:
1839
 *      x_adjust - redraw the line adjusting starting point etc.
1840
 *
1841
 * DESCRIPTION:
1842
 *      This function is called when we have exceeded the bounds
1843
 *      of the edit window.  It increments x_adj_done so that
1844
 *      functions like x_ins and x_delete know that we have been
1845
 *      called and can skip the x_bs() stuff which has already
1846
 *      been done by x_redraw.
1847
 *
1848
 * RETURN VALUE:
1849
 *      None
1850
 */
1851
1852
static void
1853
x_adjust(void)
1854
6
{
1855
6
	x_adj_done++;			/* flag the fact that we were called. */
1856
	/*
1857
	 * we had a problem if the prompt length > xx_cols / 2
1858
	 */
1859
6
	if ((xbp = xcp - (x_displen / 2)) < xbuf)
1860
6
		xbp = xbuf;
1861
6
	xlp_valid = false;
1862
6
	x_redraw(xx_cols);
1863
6
	x_flush();
1864
6
}
1865
1866
static int unget_char = -1;
1867
1868
static void
1869
x_e_ungetc(int c)
1870
{
1871
	unget_char = c;
1872
}
1873
1874
static int
1875
x_e_getc(void)
1876
73
{
1877
	int c;
1878
1879
73
	if (unget_char >= 0) {
1880
		c = unget_char;
1881
		unget_char = -1;
1882
73
	} else if (macro_args) {
1883
		c = *macro_args++;
1884
		if (!c) {
1885
			macro_args = NULL;
1886
			c = x_getc();
1887
		}
1888
	} else
1889
73
		c = x_getc();
1890
1891
73
	return c;
1892
}
1893
1894
static void
1895
x_e_putc(int c)
1896
1467
{
1897
1467
	if (c == '\r' || c == '\n')
1898
42
		x_col = 0;
1899
1467
	if (x_col < xx_cols) {
1900
1467
		x_putc(c);
1901
1467
		switch (c) {
1902
		case BEL:
1903
			break;
1904
		case '\r':
1905
		case '\n':
1906
			break;
1907
		case '\b':
1908
651
			x_col--;
1909
651
			break;
1910
		default:
1911
774
			x_col++;
1912
			break;
1913
		}
1914
	}
1915

1467
	if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
1916
		x_adjust();
1917
1467
}
1918
1919
#ifdef DEBUG
1920
static int
1921
x_debug_info(int c)
1922
{
1923
	x_flush();
1924
	shellf("\nksh debug:\n");
1925
	shellf("\tx_col == %d,\t\tx_cols == %d,\tx_displen == %d\n",
1926
	    x_col, xx_cols, x_displen);
1927
	shellf("\txcp == 0x%lx,\txep == 0x%lx\n", (long) xcp, (long) xep);
1928
	shellf("\txbp == 0x%lx,\txbuf == 0x%lx\n", (long) xbp, (long) xbuf);
1929
	shellf("\txlp == 0x%lx\n", (long) xlp);
1930
	shellf("\txlp == 0x%lx\n", (long) x_lastcp());
1931
	shellf("\n");
1932
	x_redraw(-1);
1933
	return 0;
1934
}
1935
#endif
1936
1937
static void
1938
x_e_puts(const char *s)
1939
{
1940
	int	adj = x_adj_done;
1941
1942
	while (*s && adj == x_adj_done)
1943
		x_e_putc(*s++);
1944
}
1945
1946
/* NAME:
1947
 *      x_set_arg - set an arg value for next function
1948
 *
1949
 * DESCRIPTION:
1950
 *      This is a simple implementation of M-[0-9].
1951
 *
1952
 * RETURN VALUE:
1953
 *      KSTD
1954
 */
1955
1956
static int
1957
x_set_arg(int c)
1958
{
1959
	int n = 0;
1960
	int first = 1;
1961
1962
	for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0)
1963
		n = n * 10 + (c - '0');
1964
	if (c < 0 || first) {
1965
		x_e_putc(BEL);
1966
		x_arg = 1;
1967
		x_arg_defaulted = 1;
1968
	} else {
1969
		x_e_ungetc(c);
1970
		x_arg = n;
1971
		x_arg_defaulted = 0;
1972
		x_arg_set = 1;
1973
	}
1974
	return KSTD;
1975
}
1976
1977
1978
/* Comment or uncomment the current line. */
1979
static int
1980
x_comment(int c)
1981
{
1982
	int oldsize = x_size_str(xbuf);
1983
	int len = xep - xbuf;
1984
	int ret = x_do_comment(xbuf, xend - xbuf, &len);
1985
1986
	if (ret < 0)
1987
		x_e_putc(BEL);
1988
	else {
1989
		xep = xbuf + len;
1990
		*xep = '\0';
1991
		xcp = xbp = xbuf;
1992
		x_redraw(oldsize);
1993
		if (ret > 0)
1994
			return x_newline('\n');
1995
	}
1996
	return KSTD;
1997
}
1998
1999
2000
/* NAME:
2001
 *      x_prev_histword - recover word from prev command
2002
 *
2003
 * DESCRIPTION:
2004
 *      This function recovers the last word from the previous
2005
 *      command and inserts it into the current edit line.  If a
2006
 *      numeric arg is supplied then the n'th word from the
2007
 *      start of the previous command is used.
2008
 *
2009
 *      Bound to M-.
2010
 *
2011
 * RETURN VALUE:
2012
 *      KSTD
2013
 */
2014
2015
static int
2016
x_prev_histword(int c)
2017
{
2018
	char *rcp;
2019
	char *cp;
2020
2021
	cp = *histptr;
2022
	if (!cp)
2023
		x_e_putc(BEL);
2024
	else if (x_arg_defaulted) {
2025
		rcp = &cp[strlen(cp) - 1];
2026
		/*
2027
		 * ignore white-space after the last word
2028
		 */
2029
		while (rcp > cp && is_cfs(*rcp))
2030
			rcp--;
2031
		while (rcp > cp && !is_cfs(*rcp))
2032
			rcp--;
2033
		if (is_cfs(*rcp))
2034
			rcp++;
2035
		x_ins(rcp);
2036
	} else {
2037
		int c;
2038
2039
		rcp = cp;
2040
		/*
2041
		 * ignore white-space at start of line
2042
		 */
2043
		while (*rcp && is_cfs(*rcp))
2044
			rcp++;
2045
		while (x_arg-- > 1) {
2046
			while (*rcp && !is_cfs(*rcp))
2047
				rcp++;
2048
			while (*rcp && is_cfs(*rcp))
2049
				rcp++;
2050
		}
2051
		cp = rcp;
2052
		while (*rcp && !is_cfs(*rcp))
2053
			rcp++;
2054
		c = *rcp;
2055
		*rcp = '\0';
2056
		x_ins(cp);
2057
		*rcp = c;
2058
	}
2059
	return KSTD;
2060
}
2061
2062
/* Uppercase N(1) words */
2063
static int
2064
x_fold_upper(int c)
2065
{
2066
	return x_fold_case('U');
2067
}
2068
2069
/* Lowercase N(1) words */
2070
static int
2071
x_fold_lower(int c)
2072
{
2073
	return x_fold_case('L');
2074
}
2075
2076
/* Lowercase N(1) words */
2077
static int
2078
x_fold_capitalize(int c)
2079
{
2080
	return x_fold_case('C');
2081
}
2082
2083
/* NAME:
2084
 *      x_fold_case - convert word to UPPER/lower/Capital case
2085
 *
2086
 * DESCRIPTION:
2087
 *      This function is used to implement M-U,M-u,M-L,M-l,M-C and M-c
2088
 *      to UPPER case, lower case or Capitalize words.
2089
 *
2090
 * RETURN VALUE:
2091
 *      None
2092
 */
2093
2094
static int
2095
x_fold_case(int c)
2096
{
2097
	char *cp = xcp;
2098
2099
	if (cp == xep) {
2100
		x_e_putc(BEL);
2101
		return KSTD;
2102
	}
2103
	while (x_arg--) {
2104
		/*
2105
		 * first skip over any white-space
2106
		 */
2107
		while (cp != xep && is_mfs(*cp))
2108
			cp++;
2109
		/*
2110
		 * do the first char on its own since it may be
2111
		 * a different action than for the rest.
2112
		 */
2113
		if (cp != xep) {
2114
			if (c == 'L') {		/* lowercase */
2115
				if (isupper((unsigned char)*cp))
2116
					*cp = tolower((unsigned char)*cp);
2117
			} else {		/* uppercase, capitalize */
2118
				if (islower((unsigned char)*cp))
2119
					*cp = toupper((unsigned char)*cp);
2120
			}
2121
			cp++;
2122
		}
2123
		/*
2124
		 * now for the rest of the word
2125
		 */
2126
		while (cp != xep && !is_mfs(*cp)) {
2127
			if (c == 'U') {		/* uppercase */
2128
				if (islower((unsigned char)*cp))
2129
					*cp = toupper((unsigned char)*cp);
2130
			} else {		/* lowercase, capitalize */
2131
				if (isupper((unsigned char)*cp))
2132
					*cp = tolower((unsigned char)*cp);
2133
			}
2134
			cp++;
2135
		}
2136
	}
2137
	x_goto(cp);
2138
	return KSTD;
2139
}
2140
2141
/* NAME:
2142
 *      x_lastcp - last visible byte
2143
 *
2144
 * SYNOPSIS:
2145
 *      x_lastcp()
2146
 *
2147
 * DESCRIPTION:
2148
 *      This function returns a pointer to that byte in the
2149
 *      edit buffer that will be the last displayed on the
2150
 *      screen.  The sequence:
2151
 *
2152
 *      for (cp = x_lastcp(); cp > xcp; cp)
2153
 *        x_bs(*--cp);
2154
 *
2155
 *      Will position the cursor correctly on the screen.
2156
 *
2157
 * RETURN VALUE:
2158
 *      cp or NULL
2159
 */
2160
2161
static char *
2162
x_lastcp(void)
2163
136
{
2164
	char *rcp;
2165
	int i;
2166
2167
136
	if (!xlp_valid) {
2168

467
		for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++)
2169
399
			i += x_size((unsigned char)*rcp);
2170
68
		xlp = rcp;
2171
	}
2172
136
	xlp_valid = true;
2173
136
	return (xlp);
2174
}
2175
2176
#endif /* EDIT */