xref: /netbsd/lib/libedit/chared.c (revision bf9ec67e)
1 /*	$NetBSD: chared.c,v 1.15 2002/03/18 16:00:50 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Christos Zoulas of Cornell University.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include "config.h"
40 #if !defined(lint) && !defined(SCCSID)
41 #if 0
42 static char sccsid[] = "@(#)chared.c	8.1 (Berkeley) 6/4/93";
43 #else
44 __RCSID("$NetBSD: chared.c,v 1.15 2002/03/18 16:00:50 christos Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47 
48 /*
49  * chared.c: Character editor utilities
50  */
51 #include <stdlib.h>
52 #include "el.h"
53 
54 /* value to leave unused in line buffer */
55 #define	EL_LEAVE	2
56 
57 /* cv_undo():
58  *	Handle state for the vi undo command
59  */
60 protected void
61 cv_undo(EditLine *el,int action, size_t size, char *ptr)
62 {
63 	c_undo_t *vu = &el->el_chared.c_undo;
64 	vu->action = action;
65 	vu->ptr    = ptr;
66 	vu->isize  = size;
67 	(void) memcpy(vu->buf, vu->ptr, size);
68 #ifdef DEBUG_UNDO
69 	(void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
70 	       vu->ptr, vu->isize, vu->dsize);
71 #endif
72 }
73 
74 
75 /* c_insert():
76  *	Insert num characters
77  */
78 protected void
79 c_insert(EditLine *el, int num)
80 {
81 	char *cp;
82 
83 	if (el->el_line.lastchar + num >= el->el_line.limit)
84 		return;			/* can't go past end of buffer */
85 
86 	if (el->el_line.cursor < el->el_line.lastchar) {
87 		/* if I must move chars */
88 		for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
89 			cp[num] = *cp;
90 	}
91 	el->el_line.lastchar += num;
92 }
93 
94 
95 /* c_delafter():
96  *	Delete num characters after the cursor
97  */
98 protected void
99 c_delafter(EditLine *el, int num)
100 {
101 
102 	if (el->el_line.cursor + num > el->el_line.lastchar)
103 		num = el->el_line.lastchar - el->el_line.cursor;
104 
105 	if (num > 0) {
106 		char *cp;
107 
108 		if (el->el_map.current != el->el_map.emacs)
109 			cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
110 
111 		for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
112 			*cp = cp[num];
113 
114 		el->el_line.lastchar -= num;
115 	}
116 }
117 
118 
119 /* c_delbefore():
120  *	Delete num characters before the cursor
121  */
122 protected void
123 c_delbefore(EditLine *el, int num)
124 {
125 
126 	if (el->el_line.cursor - num < el->el_line.buffer)
127 		num = el->el_line.cursor - el->el_line.buffer;
128 
129 	if (num > 0) {
130 		char *cp;
131 
132 		if (el->el_map.current != el->el_map.emacs)
133 			cv_undo(el, INSERT, (size_t)num,
134 			    el->el_line.cursor - num);
135 
136 		for (cp = el->el_line.cursor - num;
137 		    cp <= el->el_line.lastchar;
138 		    cp++)
139 			*cp = cp[num];
140 
141 		el->el_line.lastchar -= num;
142 	}
143 }
144 
145 
146 /* ce__isword():
147  *	Return if p is part of a word according to emacs
148  */
149 protected int
150 ce__isword(int p)
151 {
152 	return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
153 }
154 
155 
156 /* cv__isword():
157  *	Return if p is part of a word according to vi
158  */
159 protected int
160 cv__isword(int p)
161 {
162 	return (!isspace(p));
163 }
164 
165 
166 /* c__prev_word():
167  *	Find the previous word
168  */
169 protected char *
170 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
171 {
172 	p--;
173 
174 	while (n--) {
175 		while ((p >= low) && !(*wtest)((unsigned char) *p))
176 			p--;
177 		while ((p >= low) && (*wtest)((unsigned char) *p))
178 			p--;
179 	}
180 
181 	/* cp now points to one character before the word */
182 	p++;
183 	if (p < low)
184 		p = low;
185 	/* cp now points where we want it */
186 	return (p);
187 }
188 
189 
190 /* c__next_word():
191  *	Find the next word
192  */
193 protected char *
194 c__next_word(char *p, char *high, int n, int (*wtest)(int))
195 {
196 	while (n--) {
197 		while ((p < high) && !(*wtest)((unsigned char) *p))
198 			p++;
199 		while ((p < high) && (*wtest)((unsigned char) *p))
200 			p++;
201 	}
202 	if (p > high)
203 		p = high;
204 	/* p now points where we want it */
205 	return (p);
206 }
207 
208 /* cv_next_word():
209  *	Find the next word vi style
210  */
211 protected char *
212 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
213 {
214 	int test;
215 
216 	while (n--) {
217 		test = (*wtest)((unsigned char) *p);
218 		while ((p < high) && (*wtest)((unsigned char) *p) == test)
219 			p++;
220 		/*
221 		 * vi historically deletes with cw only the word preserving the
222 		 * trailing whitespace! This is not what 'w' does..
223 		 */
224 		if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
225 			while ((p < high) && isspace((unsigned char) *p))
226 				p++;
227 	}
228 
229 	/* p now points where we want it */
230 	if (p > high)
231 		return (high);
232 	else
233 		return (p);
234 }
235 
236 
237 /* cv_prev_word():
238  *	Find the previous word vi style
239  */
240 protected char *
241 cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
242 {
243 	int test;
244 
245 	while (n--) {
246 		p--;
247 		/*
248 		 * vi historically deletes with cb only the word preserving the
249 		 * leading whitespace! This is not what 'b' does..
250 		 */
251 		if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
252 			while ((p > low) && isspace((unsigned char) *p))
253 				p--;
254 		test = (*wtest)((unsigned char) *p);
255 		while ((p >= low) && (*wtest)((unsigned char) *p) == test)
256 			p--;
257 		p++;
258 		while (isspace((unsigned char) *p))
259 			p++;
260 	}
261 
262 	/* p now points where we want it */
263 	if (p < low)
264 		return (low);
265 	else
266 		return (p);
267 }
268 
269 
270 #ifdef notdef
271 /* c__number():
272  *	Ignore character p points to, return number appearing after that.
273  * 	A '$' by itself means a big number; "$-" is for negative; '^' means 1.
274  * 	Return p pointing to last char used.
275  */
276 protected char *
277 c__number(
278     char *p,	/* character position */
279     int *num,	/* Return value	*/
280     int dval)	/* dval is the number to subtract from like $-3 */
281 {
282 	int i;
283 	int sign = 1;
284 
285 	if (*++p == '^') {
286 		*num = 1;
287 		return (p);
288 	}
289 	if (*p == '$') {
290 		if (*++p != '-') {
291 			*num = 0x7fffffff;	/* Handle $ */
292 			return (--p);
293 		}
294 		sign = -1;			/* Handle $- */
295 		++p;
296 	}
297 	for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
298 		continue;
299 	*num = (sign < 0 ? dval - i : i);
300 	return (--p);
301 }
302 #endif
303 
304 /* cv_delfini():
305  *	Finish vi delete action
306  */
307 protected void
308 cv_delfini(EditLine *el)
309 {
310 	int size;
311 	int oaction;
312 
313 	if (el->el_chared.c_vcmd.action & INSERT)
314 		el->el_map.current = el->el_map.key;
315 
316 	oaction = el->el_chared.c_vcmd.action;
317 	el->el_chared.c_vcmd.action = NOP;
318 
319 	if (el->el_chared.c_vcmd.pos == 0)
320 		return;
321 
322 
323 	if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
324 		size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
325 		c_delbefore(el, size);
326 		el->el_line.cursor = el->el_chared.c_vcmd.pos;
327 		re_refresh_cursor(el);
328 	} else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
329 		size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
330 		c_delafter(el, size);
331 	} else {
332 		size = 1;
333 		c_delafter(el, size);
334 	}
335 	switch (oaction) {
336 	case DELETE|INSERT:
337 		el->el_chared.c_undo.action = DELETE|INSERT;
338 		break;
339 	case DELETE:
340 		el->el_chared.c_undo.action = INSERT;
341 		break;
342 	case NOP:
343 	case INSERT:
344 	default:
345 		EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction));
346 		break;
347 	}
348 
349 
350 	el->el_chared.c_undo.ptr = el->el_line.cursor;
351 	el->el_chared.c_undo.dsize = size;
352 }
353 
354 
355 #ifdef notdef
356 /* ce__endword():
357  *	Go to the end of this word according to emacs
358  */
359 protected char *
360 ce__endword(char *p, char *high, int n)
361 {
362 	p++;
363 
364 	while (n--) {
365 		while ((p < high) && isspace((unsigned char) *p))
366 			p++;
367 		while ((p < high) && !isspace((unsigned char) *p))
368 			p++;
369 	}
370 
371 	p--;
372 	return (p);
373 }
374 #endif
375 
376 
377 /* cv__endword():
378  *	Go to the end of this word according to vi
379  */
380 protected char *
381 cv__endword(char *p, char *high, int n)
382 {
383 	p++;
384 
385 	while (n--) {
386 		while ((p < high) && isspace((unsigned char) *p))
387 			p++;
388 
389 		if (isalnum((unsigned char) *p))
390 			while ((p < high) && isalnum((unsigned char) *p))
391 				p++;
392 		else
393 			while ((p < high) && !(isspace((unsigned char) *p) ||
394 			    isalnum((unsigned char) *p)))
395 				p++;
396 	}
397 	p--;
398 	return (p);
399 }
400 
401 /* ch_init():
402  *	Initialize the character editor
403  */
404 protected int
405 ch_init(EditLine *el)
406 {
407 	el->el_line.buffer		= (char *) el_malloc(EL_BUFSIZ);
408 	if (el->el_line.buffer == NULL)
409 		return (-1);
410 
411 	(void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
412 	el->el_line.cursor		= el->el_line.buffer;
413 	el->el_line.lastchar		= el->el_line.buffer;
414 	el->el_line.limit		= &el->el_line.buffer[EL_BUFSIZ - 2];
415 
416 	el->el_chared.c_undo.buf	= (char *) el_malloc(EL_BUFSIZ);
417 	if (el->el_chared.c_undo.buf == NULL)
418 		return (-1);
419 	(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
420 	el->el_chared.c_undo.action	= NOP;
421 	el->el_chared.c_undo.isize	= 0;
422 	el->el_chared.c_undo.dsize	= 0;
423 	el->el_chared.c_undo.ptr	= el->el_line.buffer;
424 
425 	el->el_chared.c_vcmd.action	= NOP;
426 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
427 	el->el_chared.c_vcmd.ins	= el->el_line.buffer;
428 
429 	el->el_chared.c_kill.buf	= (char *) el_malloc(EL_BUFSIZ);
430 	if (el->el_chared.c_kill.buf == NULL)
431 		return (-1);
432 	(void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
433 	el->el_chared.c_kill.mark	= el->el_line.buffer;
434 	el->el_chared.c_kill.last	= el->el_chared.c_kill.buf;
435 
436 	el->el_map.current		= el->el_map.key;
437 
438 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
439 	el->el_state.doingarg		= 0;
440 	el->el_state.metanext		= 0;
441 	el->el_state.argument		= 1;
442 	el->el_state.lastcmd		= ED_UNASSIGNED;
443 
444 	el->el_chared.c_macro.nline	= NULL;
445 	el->el_chared.c_macro.level	= -1;
446 	el->el_chared.c_macro.macro	= (char **) el_malloc(EL_MAXMACRO *
447 	    sizeof(char *));
448 	if (el->el_chared.c_macro.macro == NULL)
449 		return (-1);
450 	return (0);
451 }
452 
453 /* ch_reset():
454  *	Reset the character editor
455  */
456 protected void
457 ch_reset(EditLine *el)
458 {
459 	el->el_line.cursor		= el->el_line.buffer;
460 	el->el_line.lastchar		= el->el_line.buffer;
461 
462 	el->el_chared.c_undo.action	= NOP;
463 	el->el_chared.c_undo.isize	= 0;
464 	el->el_chared.c_undo.dsize	= 0;
465 	el->el_chared.c_undo.ptr	= el->el_line.buffer;
466 
467 	el->el_chared.c_vcmd.action	= NOP;
468 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
469 	el->el_chared.c_vcmd.ins	= el->el_line.buffer;
470 
471 	el->el_chared.c_kill.mark	= el->el_line.buffer;
472 
473 	el->el_map.current		= el->el_map.key;
474 
475 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
476 	el->el_state.doingarg		= 0;
477 	el->el_state.metanext		= 0;
478 	el->el_state.argument		= 1;
479 	el->el_state.lastcmd		= ED_UNASSIGNED;
480 
481 	el->el_chared.c_macro.level	= -1;
482 
483 	el->el_history.eventno		= 0;
484 }
485 
486 /* ch_enlargebufs():
487  *	Enlarge line buffer to be able to hold twice as much characters.
488  *	Returns 1 if successful, 0 if not.
489  */
490 protected int
491 ch_enlargebufs(el, addlen)
492 	EditLine *el;
493 	size_t addlen;
494 {
495 	size_t sz, newsz;
496 	char *newbuffer, *oldbuf, *oldkbuf;
497 
498 	sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
499 	newsz = sz * 2;
500 	/*
501 	 * If newly required length is longer than current buffer, we need
502 	 * to make the buffer big enough to hold both old and new stuff.
503 	 */
504 	if (addlen > sz) {
505 		while(newsz - sz < addlen)
506 			newsz *= 2;
507 	}
508 
509 	/*
510 	 * Reallocate line buffer.
511 	 */
512 	newbuffer = el_realloc(el->el_line.buffer, newsz);
513 	if (!newbuffer)
514 		return 0;
515 
516 	/* zero the newly added memory, leave old data in */
517 	(void) memset(&newbuffer[sz], 0, newsz - sz);
518 
519 	oldbuf = el->el_line.buffer;
520 
521 	el->el_line.buffer = newbuffer;
522 	el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
523 	el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
524 	el->el_line.limit  = &newbuffer[newsz - EL_LEAVE];
525 
526 	/*
527 	 * Reallocate kill buffer.
528 	 */
529 	newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
530 	if (!newbuffer)
531 		return 0;
532 
533 	/* zero the newly added memory, leave old data in */
534 	(void) memset(&newbuffer[sz], 0, newsz - sz);
535 
536 	oldkbuf = el->el_chared.c_kill.buf;
537 
538 	el->el_chared.c_kill.buf = newbuffer;
539 	el->el_chared.c_kill.last = newbuffer +
540 					(el->el_chared.c_kill.last - oldkbuf);
541 	el->el_chared.c_kill.mark = el->el_line.buffer +
542 					(el->el_chared.c_kill.mark - oldbuf);
543 
544 	/*
545 	 * Reallocate undo buffer.
546 	 */
547 	newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
548 	if (!newbuffer)
549 		return 0;
550 
551 	/* zero the newly added memory, leave old data in */
552 	(void) memset(&newbuffer[sz], 0, newsz - sz);
553 
554 	el->el_chared.c_undo.ptr = el->el_line.buffer +
555 				    (el->el_chared.c_undo.ptr - oldbuf);
556 	el->el_chared.c_undo.buf = newbuffer;
557 
558 	if (!hist_enlargebuf(el, sz, newsz))
559 		return 0;
560 
561 	return 1;
562 }
563 
564 /* ch_end():
565  *	Free the data structures used by the editor
566  */
567 protected void
568 ch_end(EditLine *el)
569 {
570 	el_free((ptr_t) el->el_line.buffer);
571 	el->el_line.buffer = NULL;
572 	el->el_line.limit = NULL;
573 	el_free((ptr_t) el->el_chared.c_undo.buf);
574 	el->el_chared.c_undo.buf = NULL;
575 	el_free((ptr_t) el->el_chared.c_kill.buf);
576 	el->el_chared.c_kill.buf = NULL;
577 	el_free((ptr_t) el->el_chared.c_macro.macro);
578 	el->el_chared.c_macro.macro = NULL;
579 	ch_reset(el);
580 }
581 
582 
583 /* el_insertstr():
584  *	Insert string at cursorI
585  */
586 public int
587 el_insertstr(EditLine *el, const char *s)
588 {
589 	size_t len;
590 
591 	if ((len = strlen(s)) == 0)
592 		return (-1);
593 	if (el->el_line.lastchar + len >= el->el_line.limit) {
594 		if (!ch_enlargebufs(el, len))
595 			return (-1);
596 	}
597 
598 	c_insert(el, (int)len);
599 	while (*s)
600 		*el->el_line.cursor++ = *s++;
601 	return (0);
602 }
603 
604 
605 /* el_deletestr():
606  *	Delete num characters before the cursor
607  */
608 public void
609 el_deletestr(EditLine *el, int n)
610 {
611 	if (n <= 0)
612 		return;
613 
614 	if (el->el_line.cursor < &el->el_line.buffer[n])
615 		return;
616 
617 	c_delbefore(el, n);		/* delete before dot */
618 	el->el_line.cursor -= n;
619 	if (el->el_line.cursor < el->el_line.buffer)
620 		el->el_line.cursor = el->el_line.buffer;
621 }
622 
623 /* c_gets():
624  *	Get a string
625  */
626 protected int
627 c_gets(EditLine *el, char *buf)
628 {
629 	char ch;
630 	int len = 0;
631 
632 	for (ch = 0; ch == 0;) {
633 		if (el_getc(el, &ch) != 1)
634 			return (ed_end_of_file(el, 0));
635 		switch (ch) {
636 		case 0010:	/* Delete and backspace */
637 		case 0177:
638 			if (len > 1) {
639 				*el->el_line.cursor-- = '\0';
640 				el->el_line.lastchar = el->el_line.cursor;
641 				buf[len--] = '\0';
642 			} else {
643 				el->el_line.buffer[0] = '\0';
644 				el->el_line.lastchar = el->el_line.buffer;
645 				el->el_line.cursor = el->el_line.buffer;
646 				return (CC_REFRESH);
647 			}
648 			re_refresh(el);
649 			ch = 0;
650 			break;
651 
652 		case 0033:	/* ESC */
653 		case '\r':	/* Newline */
654 		case '\n':
655 			break;
656 
657 		default:
658 			if (len >= EL_BUFSIZ)
659 				term_beep(el);
660 			else {
661 				buf[len++] = ch;
662 				*el->el_line.cursor++ = ch;
663 				el->el_line.lastchar = el->el_line.cursor;
664 			}
665 			re_refresh(el);
666 			ch = 0;
667 			break;
668 		}
669 	}
670 	buf[len] = ch;
671 	return (len);
672 }
673 
674 
675 /* c_hpos():
676  *	Return the current horizontal position of the cursor
677  */
678 protected int
679 c_hpos(EditLine *el)
680 {
681 	char *ptr;
682 
683 	/*
684 	 * Find how many characters till the beginning of this line.
685 	 */
686 	if (el->el_line.cursor == el->el_line.buffer)
687 		return (0);
688 	else {
689 		for (ptr = el->el_line.cursor - 1;
690 		     ptr >= el->el_line.buffer && *ptr != '\n';
691 		     ptr--)
692 			continue;
693 		return (el->el_line.cursor - ptr - 1);
694 	}
695 }
696