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