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