chared.c revision 1.51 1 /* $NetBSD: chared.c,v 1.51 2016/04/11 00:22:48 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.51 2016/04/11 00:22:48 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
54 private void ch__clearmacro (EditLine *);
55
56 /* value to leave unused in line buffer */
57 #define EL_LEAVE 2
58
59 /* cv_undo():
60 * Handle state for the vi undo command
61 */
62 protected void
63 cv_undo(EditLine *el)
64 {
65 c_undo_t *vu = &el->el_chared.c_undo;
66 c_redo_t *r = &el->el_chared.c_redo;
67 size_t size;
68
69 /* Save entire line for undo */
70 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
71 vu->len = (ssize_t)size;
72 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
73 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
74
75 /* save command info for redo */
76 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
77 r->action = el->el_chared.c_vcmd.action;
78 r->pos = r->buf;
79 r->cmd = el->el_state.thiscmd;
80 r->ch = el->el_state.thisch;
81 }
82
83 /* cv_yank():
84 * Save yank/delete data for paste
85 */
86 protected void
87 cv_yank(EditLine *el, const Char *ptr, int size)
88 {
89 c_kill_t *k = &el->el_chared.c_kill;
90
91 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
92 k->last = k->buf + size;
93 }
94
95
96 /* c_insert():
97 * Insert num characters
98 */
99 protected void
100 c_insert(EditLine *el, int num)
101 {
102 Char *cp;
103
104 if (el->el_line.lastchar + num >= el->el_line.limit) {
105 if (!ch_enlargebufs(el, (size_t)num))
106 return; /* can't go past end of buffer */
107 }
108
109 if (el->el_line.cursor < el->el_line.lastchar) {
110 /* if I must move chars */
111 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
112 cp[num] = *cp;
113 }
114 el->el_line.lastchar += num;
115 }
116
117
118 /* c_delafter():
119 * Delete num characters after the cursor
120 */
121 protected void
122 c_delafter(EditLine *el, int num)
123 {
124
125 if (el->el_line.cursor + num > el->el_line.lastchar)
126 num = (int)(el->el_line.lastchar - el->el_line.cursor);
127
128 if (el->el_map.current != el->el_map.emacs) {
129 cv_undo(el);
130 cv_yank(el, el->el_line.cursor, num);
131 }
132
133 if (num > 0) {
134 Char *cp;
135
136 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
137 *cp = cp[num];
138
139 el->el_line.lastchar -= num;
140 }
141 }
142
143
144 /* c_delafter1():
145 * Delete the character after the cursor, do not yank
146 */
147 protected void
148 c_delafter1(EditLine *el)
149 {
150 Char *cp;
151
152 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
153 *cp = cp[1];
154
155 el->el_line.lastchar--;
156 }
157
158
159 /* c_delbefore():
160 * Delete num characters before the cursor
161 */
162 protected void
163 c_delbefore(EditLine *el, int num)
164 {
165
166 if (el->el_line.cursor - num < el->el_line.buffer)
167 num = (int)(el->el_line.cursor - el->el_line.buffer);
168
169 if (el->el_map.current != el->el_map.emacs) {
170 cv_undo(el);
171 cv_yank(el, el->el_line.cursor - num, num);
172 }
173
174 if (num > 0) {
175 Char *cp;
176
177 for (cp = el->el_line.cursor - num;
178 cp <= el->el_line.lastchar;
179 cp++)
180 *cp = cp[num];
181
182 el->el_line.lastchar -= num;
183 }
184 }
185
186
187 /* c_delbefore1():
188 * Delete the character before the cursor, do not yank
189 */
190 protected void
191 c_delbefore1(EditLine *el)
192 {
193 Char *cp;
194
195 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
196 *cp = cp[1];
197
198 el->el_line.lastchar--;
199 }
200
201
202 /* ce__isword():
203 * Return if p is part of a word according to emacs
204 */
205 protected int
206 ce__isword(wint_t p)
207 {
208 return iswalnum(p) || wcschr(L"*?_-.[]~=", p) != NULL;
209 }
210
211
212 /* cv__isword():
213 * Return if p is part of a word according to vi
214 */
215 protected int
216 cv__isword(wint_t p)
217 {
218 if (iswalnum(p) || p == L'_')
219 return 1;
220 if (iswgraph(p))
221 return 2;
222 return 0;
223 }
224
225
226 /* cv__isWord():
227 * Return if p is part of a big word according to vi
228 */
229 protected int
230 cv__isWord(wint_t p)
231 {
232 return !iswspace(p);
233 }
234
235
236 /* c__prev_word():
237 * Find the previous word
238 */
239 protected Char *
240 c__prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
241 {
242 p--;
243
244 while (n--) {
245 while ((p >= low) && !(*wtest)(*p))
246 p--;
247 while ((p >= low) && (*wtest)(*p))
248 p--;
249 }
250
251 /* cp now points to one character before the word */
252 p++;
253 if (p < low)
254 p = low;
255 /* cp now points where we want it */
256 return p;
257 }
258
259
260 /* c__next_word():
261 * Find the next word
262 */
263 protected Char *
264 c__next_word(Char *p, Char *high, int n, int (*wtest)(wint_t))
265 {
266 while (n--) {
267 while ((p < high) && !(*wtest)(*p))
268 p++;
269 while ((p < high) && (*wtest)(*p))
270 p++;
271 }
272 if (p > high)
273 p = high;
274 /* p now points where we want it */
275 return p;
276 }
277
278 /* cv_next_word():
279 * Find the next word vi style
280 */
281 protected Char *
282 cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(wint_t))
283 {
284 int test;
285
286 while (n--) {
287 test = (*wtest)(*p);
288 while ((p < high) && (*wtest)(*p) == test)
289 p++;
290 /*
291 * vi historically deletes with cw only the word preserving the
292 * trailing whitespace! This is not what 'w' does..
293 */
294 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
295 while ((p < high) && iswspace(*p))
296 p++;
297 }
298
299 /* p now points where we want it */
300 if (p > high)
301 return high;
302 else
303 return p;
304 }
305
306
307 /* cv_prev_word():
308 * Find the previous word vi style
309 */
310 protected Char *
311 cv_prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
312 {
313 int test;
314
315 p--;
316 while (n--) {
317 while ((p > low) && iswspace(*p))
318 p--;
319 test = (*wtest)(*p);
320 while ((p >= low) && (*wtest)(*p) == test)
321 p--;
322 }
323 p++;
324
325 /* p now points where we want it */
326 if (p < low)
327 return low;
328 else
329 return p;
330 }
331
332
333 /* cv_delfini():
334 * Finish vi delete action
335 */
336 protected void
337 cv_delfini(EditLine *el)
338 {
339 int size;
340 int action = el->el_chared.c_vcmd.action;
341
342 if (action & INSERT)
343 el->el_map.current = el->el_map.key;
344
345 if (el->el_chared.c_vcmd.pos == 0)
346 /* sanity */
347 return;
348
349 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
350 if (size == 0)
351 size = 1;
352 el->el_line.cursor = el->el_chared.c_vcmd.pos;
353 if (action & YANK) {
354 if (size > 0)
355 cv_yank(el, el->el_line.cursor, size);
356 else
357 cv_yank(el, el->el_line.cursor + size, -size);
358 } else {
359 if (size > 0) {
360 c_delafter(el, size);
361 re_refresh_cursor(el);
362 } else {
363 c_delbefore(el, -size);
364 el->el_line.cursor += size;
365 }
366 }
367 el->el_chared.c_vcmd.action = NOP;
368 }
369
370
371 /* cv__endword():
372 * Go to the end of this word according to vi
373 */
374 protected Char *
375 cv__endword(Char *p, Char *high, int n, int (*wtest)(wint_t))
376 {
377 int test;
378
379 p++;
380
381 while (n--) {
382 while ((p < high) && iswspace(*p))
383 p++;
384
385 test = (*wtest)(*p);
386 while ((p < high) && (*wtest)(*p) == test)
387 p++;
388 }
389 p--;
390 return p;
391 }
392
393 /* ch_init():
394 * Initialize the character editor
395 */
396 protected int
397 ch_init(EditLine *el)
398 {
399 c_macro_t *ma = &el->el_chared.c_macro;
400
401 el->el_line.buffer = el_malloc(EL_BUFSIZ *
402 sizeof(*el->el_line.buffer));
403 if (el->el_line.buffer == NULL)
404 return -1;
405
406 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
407 sizeof(*el->el_line.buffer));
408 el->el_line.cursor = el->el_line.buffer;
409 el->el_line.lastchar = el->el_line.buffer;
410 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
411
412 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
413 sizeof(*el->el_chared.c_undo.buf));
414 if (el->el_chared.c_undo.buf == NULL)
415 return -1;
416 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
417 sizeof(*el->el_chared.c_undo.buf));
418 el->el_chared.c_undo.len = -1;
419 el->el_chared.c_undo.cursor = 0;
420 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
421 sizeof(*el->el_chared.c_redo.buf));
422 if (el->el_chared.c_redo.buf == NULL)
423 return -1;
424 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
425 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
426 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
427
428 el->el_chared.c_vcmd.action = NOP;
429 el->el_chared.c_vcmd.pos = el->el_line.buffer;
430
431 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
432 sizeof(*el->el_chared.c_kill.buf));
433 if (el->el_chared.c_kill.buf == NULL)
434 return -1;
435 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
436 sizeof(*el->el_chared.c_kill.buf));
437 el->el_chared.c_kill.mark = el->el_line.buffer;
438 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
439 el->el_chared.c_resizefun = NULL;
440 el->el_chared.c_resizearg = NULL;
441 el->el_chared.c_aliasfun = NULL;
442 el->el_chared.c_aliasarg = NULL;
443
444 el->el_map.current = el->el_map.key;
445
446 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
447 el->el_state.doingarg = 0;
448 el->el_state.metanext = 0;
449 el->el_state.argument = 1;
450 el->el_state.lastcmd = ED_UNASSIGNED;
451
452 ma->level = -1;
453 ma->offset = 0;
454 ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
455 if (ma->macro == NULL)
456 return -1;
457 return 0;
458 }
459
460 /* ch_reset():
461 * Reset the character editor
462 */
463 protected void
464 ch_reset(EditLine *el, int mclear)
465 {
466 el->el_line.cursor = el->el_line.buffer;
467 el->el_line.lastchar = el->el_line.buffer;
468
469 el->el_chared.c_undo.len = -1;
470 el->el_chared.c_undo.cursor = 0;
471
472 el->el_chared.c_vcmd.action = NOP;
473 el->el_chared.c_vcmd.pos = el->el_line.buffer;
474
475 el->el_chared.c_kill.mark = el->el_line.buffer;
476
477 el->el_map.current = el->el_map.key;
478
479 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
480 el->el_state.doingarg = 0;
481 el->el_state.metanext = 0;
482 el->el_state.argument = 1;
483 el->el_state.lastcmd = ED_UNASSIGNED;
484
485 el->el_history.eventno = 0;
486
487 if (mclear)
488 ch__clearmacro(el);
489 }
490
491 private void
492 ch__clearmacro(EditLine *el)
493 {
494 c_macro_t *ma = &el->el_chared.c_macro;
495 while (ma->level >= 0)
496 el_free(ma->macro[ma->level--]);
497 }
498
499 /* ch_enlargebufs():
500 * Enlarge line buffer to be able to hold twice as much characters.
501 * Returns 1 if successful, 0 if not.
502 */
503 protected int
504 ch_enlargebufs(EditLine *el, size_t addlen)
505 {
506 size_t sz, newsz;
507 Char *newbuffer, *oldbuf, *oldkbuf;
508
509 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
510 newsz = sz * 2;
511 /*
512 * If newly required length is longer than current buffer, we need
513 * to make the buffer big enough to hold both old and new stuff.
514 */
515 if (addlen > sz) {
516 while(newsz - sz < addlen)
517 newsz *= 2;
518 }
519
520 /*
521 * Reallocate line buffer.
522 */
523 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
524 if (!newbuffer)
525 return 0;
526
527 /* zero the newly added memory, leave old data in */
528 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
529
530 oldbuf = el->el_line.buffer;
531
532 el->el_line.buffer = newbuffer;
533 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
534 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
535 /* don't set new size until all buffers are enlarged */
536 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
537
538 /*
539 * Reallocate kill buffer.
540 */
541 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
542 sizeof(*newbuffer));
543 if (!newbuffer)
544 return 0;
545
546 /* zero the newly added memory, leave old data in */
547 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
548
549 oldkbuf = el->el_chared.c_kill.buf;
550
551 el->el_chared.c_kill.buf = newbuffer;
552 el->el_chared.c_kill.last = newbuffer +
553 (el->el_chared.c_kill.last - oldkbuf);
554 el->el_chared.c_kill.mark = el->el_line.buffer +
555 (el->el_chared.c_kill.mark - oldbuf);
556
557 /*
558 * Reallocate undo buffer.
559 */
560 newbuffer = el_realloc(el->el_chared.c_undo.buf,
561 newsz * sizeof(*newbuffer));
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) * sizeof(*newbuffer));
567 el->el_chared.c_undo.buf = newbuffer;
568
569 newbuffer = el_realloc(el->el_chared.c_redo.buf,
570 newsz * sizeof(*newbuffer));
571 if (!newbuffer)
572 return 0;
573 el->el_chared.c_redo.pos = newbuffer +
574 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
575 el->el_chared.c_redo.lim = newbuffer +
576 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
577 el->el_chared.c_redo.buf = newbuffer;
578
579 if (!hist_enlargebuf(el, sz, newsz))
580 return 0;
581
582 /* Safe to set enlarged buffer size */
583 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
584 if (el->el_chared.c_resizefun)
585 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
586 return 1;
587 }
588
589 /* ch_end():
590 * Free the data structures used by the editor
591 */
592 protected void
593 ch_end(EditLine *el)
594 {
595 el_free(el->el_line.buffer);
596 el->el_line.buffer = NULL;
597 el->el_line.limit = NULL;
598 el_free(el->el_chared.c_undo.buf);
599 el->el_chared.c_undo.buf = NULL;
600 el_free(el->el_chared.c_redo.buf);
601 el->el_chared.c_redo.buf = NULL;
602 el->el_chared.c_redo.pos = NULL;
603 el->el_chared.c_redo.lim = NULL;
604 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
605 el_free(el->el_chared.c_kill.buf);
606 el->el_chared.c_kill.buf = NULL;
607 ch_reset(el, 1);
608 el_free(el->el_chared.c_macro.macro);
609 el->el_chared.c_macro.macro = NULL;
610 }
611
612
613 /* el_insertstr():
614 * Insert string at cursorI
615 */
616 public int
617 el_winsertstr(EditLine *el, const Char *s)
618 {
619 size_t len;
620
621 if (s == NULL || (len = wcslen(s)) == 0)
622 return -1;
623 if (el->el_line.lastchar + len >= el->el_line.limit) {
624 if (!ch_enlargebufs(el, len))
625 return -1;
626 }
627
628 c_insert(el, (int)len);
629 while (*s)
630 *el->el_line.cursor++ = *s++;
631 return 0;
632 }
633
634
635 /* el_deletestr():
636 * Delete num characters before the cursor
637 */
638 public void
639 el_deletestr(EditLine *el, int n)
640 {
641 if (n <= 0)
642 return;
643
644 if (el->el_line.cursor < &el->el_line.buffer[n])
645 return;
646
647 c_delbefore(el, n); /* delete before dot */
648 el->el_line.cursor -= n;
649 if (el->el_line.cursor < el->el_line.buffer)
650 el->el_line.cursor = el->el_line.buffer;
651 }
652
653 /* el_cursor():
654 * Move the cursor to the left or the right of the current position
655 */
656 public int
657 el_cursor(EditLine *el, int n)
658 {
659 if (n == 0)
660 goto out;
661
662 el->el_line.cursor += n;
663
664 if (el->el_line.cursor < el->el_line.buffer)
665 el->el_line.cursor = el->el_line.buffer;
666 if (el->el_line.cursor > el->el_line.lastchar)
667 el->el_line.cursor = el->el_line.lastchar;
668 out:
669 return (int)(el->el_line.cursor - el->el_line.buffer);
670 }
671
672 /* c_gets():
673 * Get a string
674 */
675 protected int
676 c_gets(EditLine *el, Char *buf, const Char *prompt)
677 {
678 wchar_t wch;
679 ssize_t len;
680 Char *cp = el->el_line.buffer, ch;
681
682 if (prompt) {
683 len = (ssize_t)wcslen(prompt);
684 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
685 cp += len;
686 }
687 len = 0;
688
689 for (;;) {
690 el->el_line.cursor = cp;
691 *cp = ' ';
692 el->el_line.lastchar = cp + 1;
693 re_refresh(el);
694
695 if (el_wgetc(el, &wch) != 1) {
696 ed_end_of_file(el, 0);
697 len = -1;
698 break;
699 }
700 ch = (Char)wch;
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 Char *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