usr.sbin/makefs/hammer2: Remove redundant hammer2_inode_modify()
[dragonfly.git] / contrib / libedit / src / chared.c
blobff5545bbe16848960f767bd2bc9d92e7b63dc1a4
1 /* $NetBSD: chared.c,v 1.62 2022/02/08 21:13:22 rillig Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
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.
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.
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.62 2022/02/08 21:13:22 rillig Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
45 * chared.c: Character editor utilities
47 #include <ctype.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "el.h"
52 #include "common.h"
53 #include "fcns.h"
55 /* value to leave unused in line buffer */
56 #define EL_LEAVE 2
58 /* cv_undo():
59 * Handle state for the vi undo command
61 libedit_private void
62 cv_undo(EditLine *el)
64 c_undo_t *vu = &el->el_chared.c_undo;
65 c_redo_t *r = &el->el_chared.c_redo;
66 size_t size;
68 /* Save entire line for undo */
69 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
70 vu->len = (ssize_t)size;
71 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
72 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
74 /* save command info for redo */
75 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
76 r->action = el->el_chared.c_vcmd.action;
77 r->pos = r->buf;
78 r->cmd = el->el_state.thiscmd;
79 r->ch = el->el_state.thisch;
82 /* cv_yank():
83 * Save yank/delete data for paste
85 libedit_private void
86 cv_yank(EditLine *el, const wchar_t *ptr, int size)
88 c_kill_t *k = &el->el_chared.c_kill;
90 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
91 k->last = k->buf + size;
95 /* c_insert():
96 * Insert num characters
98 libedit_private void
99 c_insert(EditLine *el, int num)
101 wchar_t *cp;
103 if (el->el_line.lastchar + num >= el->el_line.limit) {
104 if (!ch_enlargebufs(el, (size_t)num))
105 return; /* can't go past end of buffer */
108 if (el->el_line.cursor < el->el_line.lastchar) {
109 /* if I must move chars */
110 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
111 cp[num] = *cp;
113 el->el_line.lastchar += num;
117 /* c_delafter():
118 * Delete num characters after the cursor
120 libedit_private void
121 c_delafter(EditLine *el, int num)
124 if (el->el_line.cursor + num > el->el_line.lastchar)
125 num = (int)(el->el_line.lastchar - el->el_line.cursor);
127 if (el->el_map.current != el->el_map.emacs) {
128 cv_undo(el);
129 cv_yank(el, el->el_line.cursor, num);
132 if (num > 0) {
133 wchar_t *cp;
135 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
136 *cp = cp[num];
138 el->el_line.lastchar -= num;
143 /* c_delafter1():
144 * Delete the character after the cursor, do not yank
146 libedit_private void
147 c_delafter1(EditLine *el)
149 wchar_t *cp;
151 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
152 *cp = cp[1];
154 el->el_line.lastchar--;
158 /* c_delbefore():
159 * Delete num characters before the cursor
161 libedit_private void
162 c_delbefore(EditLine *el, int num)
165 if (el->el_line.cursor - num < el->el_line.buffer)
166 num = (int)(el->el_line.cursor - el->el_line.buffer);
168 if (el->el_map.current != el->el_map.emacs) {
169 cv_undo(el);
170 cv_yank(el, el->el_line.cursor - num, num);
173 if (num > 0) {
174 wchar_t *cp;
176 for (cp = el->el_line.cursor - num;
177 &cp[num] <= el->el_line.lastchar;
178 cp++)
179 *cp = cp[num];
181 el->el_line.lastchar -= num;
186 /* c_delbefore1():
187 * Delete the character before the cursor, do not yank
189 libedit_private void
190 c_delbefore1(EditLine *el)
192 wchar_t *cp;
194 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
195 *cp = cp[1];
197 el->el_line.lastchar--;
201 /* ce__isword():
202 * Return if p is part of a word according to emacs
204 libedit_private int
205 ce__isword(wint_t p)
207 return iswalnum(p) || wcschr(L"*?_-.[]~=", p) != NULL;
211 /* cv__isword():
212 * Return if p is part of a word according to vi
214 libedit_private int
215 cv__isword(wint_t p)
217 if (iswalnum(p) || p == L'_')
218 return 1;
219 if (iswgraph(p))
220 return 2;
221 return 0;
225 /* cv__isWord():
226 * Return if p is part of a big word according to vi
228 libedit_private int
229 cv__isWord(wint_t p)
231 return !iswspace(p);
235 /* c__prev_word():
236 * Find the previous word
238 libedit_private wchar_t *
239 c__prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
241 p--;
243 while (n--) {
244 while ((p >= low) && !(*wtest)(*p))
245 p--;
246 while ((p >= low) && (*wtest)(*p))
247 p--;
250 /* cp now points to one character before the word */
251 p++;
252 if (p < low)
253 p = low;
254 /* cp now points where we want it */
255 return p;
259 /* c__next_word():
260 * Find the next word
262 libedit_private wchar_t *
263 c__next_word(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
265 while (n--) {
266 while ((p < high) && !(*wtest)(*p))
267 p++;
268 while ((p < high) && (*wtest)(*p))
269 p++;
271 if (p > high)
272 p = high;
273 /* p now points where we want it */
274 return p;
277 /* cv_next_word():
278 * Find the next word vi style
280 libedit_private wchar_t *
281 cv_next_word(EditLine *el, wchar_t *p, wchar_t *high, int n,
282 int (*wtest)(wint_t))
284 int test;
286 while (n--) {
287 test = (*wtest)(*p);
288 while ((p < high) && (*wtest)(*p) == test)
289 p++;
291 * vi historically deletes with cw only the word preserving the
292 * trailing whitespace! This is not what 'w' does..
294 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
295 while ((p < high) && iswspace(*p))
296 p++;
299 /* p now points where we want it */
300 if (p > high)
301 return high;
302 else
303 return p;
307 /* cv_prev_word():
308 * Find the previous word vi style
310 libedit_private wchar_t *
311 cv_prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
313 int test;
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--;
323 p++;
325 /* p now points where we want it */
326 if (p < low)
327 return low;
328 else
329 return p;
333 /* cv_delfini():
334 * Finish vi delete action
336 libedit_private void
337 cv_delfini(EditLine *el)
339 int size;
340 int action = el->el_chared.c_vcmd.action;
342 if (action & INSERT)
343 el->el_map.current = el->el_map.key;
345 if (el->el_chared.c_vcmd.pos == 0)
346 /* sanity */
347 return;
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;
367 el->el_chared.c_vcmd.action = NOP;
371 /* cv__endword():
372 * Go to the end of this word according to vi
374 libedit_private wchar_t *
375 cv__endword(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
377 int test;
379 p++;
381 while (n--) {
382 while ((p < high) && iswspace(*p))
383 p++;
385 test = (*wtest)(*p);
386 while ((p < high) && (*wtest)(*p) == test)
387 p++;
389 p--;
390 return p;
393 /* ch_init():
394 * Initialize the character editor
396 libedit_private int
397 ch_init(EditLine *el)
399 el->el_line.buffer = el_calloc(EL_BUFSIZ,
400 sizeof(*el->el_line.buffer));
401 if (el->el_line.buffer == NULL)
402 return -1;
404 el->el_line.cursor = el->el_line.buffer;
405 el->el_line.lastchar = el->el_line.buffer;
406 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
408 el->el_chared.c_undo.buf = el_calloc(EL_BUFSIZ,
409 sizeof(*el->el_chared.c_undo.buf));
410 if (el->el_chared.c_undo.buf == NULL)
411 return -1;
412 el->el_chared.c_undo.len = -1;
413 el->el_chared.c_undo.cursor = 0;
414 el->el_chared.c_redo.buf = el_calloc(EL_BUFSIZ,
415 sizeof(*el->el_chared.c_redo.buf));
416 if (el->el_chared.c_redo.buf == NULL)
417 return -1;
418 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
419 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
420 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
422 el->el_chared.c_vcmd.action = NOP;
423 el->el_chared.c_vcmd.pos = el->el_line.buffer;
425 el->el_chared.c_kill.buf = el_calloc(EL_BUFSIZ,
426 sizeof(*el->el_chared.c_kill.buf));
427 if (el->el_chared.c_kill.buf == NULL)
428 return -1;
429 el->el_chared.c_kill.mark = el->el_line.buffer;
430 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
431 el->el_chared.c_resizefun = NULL;
432 el->el_chared.c_resizearg = NULL;
433 el->el_chared.c_aliasfun = NULL;
434 el->el_chared.c_aliasarg = NULL;
436 el->el_map.current = el->el_map.key;
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;
444 return 0;
447 /* ch_reset():
448 * Reset the character editor
450 libedit_private void
451 ch_reset(EditLine *el)
453 el->el_line.cursor = el->el_line.buffer;
454 el->el_line.lastchar = el->el_line.buffer;
456 el->el_chared.c_undo.len = -1;
457 el->el_chared.c_undo.cursor = 0;
459 el->el_chared.c_vcmd.action = NOP;
460 el->el_chared.c_vcmd.pos = el->el_line.buffer;
462 el->el_chared.c_kill.mark = el->el_line.buffer;
464 el->el_map.current = el->el_map.key;
466 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
467 el->el_state.doingarg = 0;
468 el->el_state.metanext = 0;
469 el->el_state.argument = 1;
470 el->el_state.lastcmd = ED_UNASSIGNED;
472 el->el_history.eventno = 0;
475 /* ch_enlargebufs():
476 * Enlarge line buffer to be able to hold twice as much characters.
477 * Returns 1 if successful, 0 if not.
479 libedit_private int
480 ch_enlargebufs(EditLine *el, size_t addlen)
482 size_t sz, newsz;
483 wchar_t *newbuffer, *oldbuf, *oldkbuf;
485 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
486 newsz = sz * 2;
488 * If newly required length is longer than current buffer, we need
489 * to make the buffer big enough to hold both old and new stuff.
491 if (addlen > sz) {
492 while(newsz - sz < addlen)
493 newsz *= 2;
497 * Reallocate line buffer.
499 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
500 if (!newbuffer)
501 return 0;
503 /* zero the newly added memory, leave old data in */
504 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
506 oldbuf = el->el_line.buffer;
508 el->el_line.buffer = newbuffer;
509 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
510 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
511 /* don't set new size until all buffers are enlarged */
512 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
515 * Reallocate kill buffer.
517 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
518 sizeof(*newbuffer));
519 if (!newbuffer)
520 return 0;
522 /* zero the newly added memory, leave old data in */
523 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
525 oldkbuf = el->el_chared.c_kill.buf;
527 el->el_chared.c_kill.buf = newbuffer;
528 el->el_chared.c_kill.last = newbuffer +
529 (el->el_chared.c_kill.last - oldkbuf);
530 el->el_chared.c_kill.mark = el->el_line.buffer +
531 (el->el_chared.c_kill.mark - oldbuf);
534 * Reallocate undo buffer.
536 newbuffer = el_realloc(el->el_chared.c_undo.buf,
537 newsz * sizeof(*newbuffer));
538 if (!newbuffer)
539 return 0;
541 /* zero the newly added memory, leave old data in */
542 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
543 el->el_chared.c_undo.buf = newbuffer;
545 newbuffer = el_realloc(el->el_chared.c_redo.buf,
546 newsz * sizeof(*newbuffer));
547 if (!newbuffer)
548 return 0;
549 el->el_chared.c_redo.pos = newbuffer +
550 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
551 el->el_chared.c_redo.lim = newbuffer +
552 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
553 el->el_chared.c_redo.buf = newbuffer;
555 if (!hist_enlargebuf(el, sz, newsz))
556 return 0;
558 /* Safe to set enlarged buffer size */
559 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
560 if (el->el_chared.c_resizefun)
561 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
562 return 1;
565 /* ch_end():
566 * Free the data structures used by the editor
568 libedit_private void
569 ch_end(EditLine *el)
571 el_free(el->el_line.buffer);
572 el->el_line.buffer = NULL;
573 el->el_line.limit = NULL;
574 el_free(el->el_chared.c_undo.buf);
575 el->el_chared.c_undo.buf = NULL;
576 el_free(el->el_chared.c_redo.buf);
577 el->el_chared.c_redo.buf = NULL;
578 el->el_chared.c_redo.pos = NULL;
579 el->el_chared.c_redo.lim = NULL;
580 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
581 el_free(el->el_chared.c_kill.buf);
582 el->el_chared.c_kill.buf = NULL;
583 ch_reset(el);
587 /* el_insertstr():
588 * Insert string at cursor
591 el_winsertstr(EditLine *el, const wchar_t *s)
593 size_t len;
595 if (s == NULL || (len = wcslen(s)) == 0)
596 return -1;
597 if (el->el_line.lastchar + len >= el->el_line.limit) {
598 if (!ch_enlargebufs(el, len))
599 return -1;
602 c_insert(el, (int)len);
603 while (*s)
604 *el->el_line.cursor++ = *s++;
605 return 0;
609 /* el_deletestr():
610 * Delete num characters before the cursor
612 void
613 el_deletestr(EditLine *el, int n)
615 if (n <= 0)
616 return;
618 if (el->el_line.cursor < &el->el_line.buffer[n])
619 return;
621 c_delbefore(el, n); /* delete before dot */
622 el->el_line.cursor -= n;
623 if (el->el_line.cursor < el->el_line.buffer)
624 el->el_line.cursor = el->el_line.buffer;
627 /* el_deletestr1():
628 * Delete characters between start and end
631 el_deletestr1(EditLine *el, int start, int end)
633 size_t line_length, len;
634 wchar_t *p1, *p2;
636 if (end <= start)
637 return 0;
639 line_length = (size_t)(el->el_line.lastchar - el->el_line.buffer);
641 if (start >= (int)line_length || end >= (int)line_length)
642 return 0;
644 len = (size_t)(end - start);
645 if (len > line_length - (size_t)end)
646 len = line_length - (size_t)end;
648 p1 = el->el_line.buffer + start;
649 p2 = el->el_line.buffer + end;
650 for (size_t i = 0; i < len; i++) {
651 *p1++ = *p2++;
652 el->el_line.lastchar--;
655 if (el->el_line.cursor < el->el_line.buffer)
656 el->el_line.cursor = el->el_line.buffer;
658 return end - start;
661 /* el_wreplacestr():
662 * Replace the contents of the line with the provided string
665 el_wreplacestr(EditLine *el, const wchar_t *s)
667 size_t len;
668 wchar_t * p;
670 if (s == NULL || (len = wcslen(s)) == 0)
671 return -1;
673 if (el->el_line.buffer + len >= el->el_line.limit) {
674 if (!ch_enlargebufs(el, len))
675 return -1;
678 p = el->el_line.buffer;
679 for (size_t i = 0; i < len; i++)
680 *p++ = *s++;
682 el->el_line.buffer[len] = '\0';
683 el->el_line.lastchar = el->el_line.buffer + len;
684 if (el->el_line.cursor > el->el_line.lastchar)
685 el->el_line.cursor = el->el_line.lastchar;
687 return 0;
690 /* el_cursor():
691 * Move the cursor to the left or the right of the current position
694 el_cursor(EditLine *el, int n)
696 if (n == 0)
697 goto out;
699 el->el_line.cursor += n;
701 if (el->el_line.cursor < el->el_line.buffer)
702 el->el_line.cursor = el->el_line.buffer;
703 if (el->el_line.cursor > el->el_line.lastchar)
704 el->el_line.cursor = el->el_line.lastchar;
705 out:
706 return (int)(el->el_line.cursor - el->el_line.buffer);
709 /* c_gets():
710 * Get a string
712 libedit_private int
713 c_gets(EditLine *el, wchar_t *buf, const wchar_t *prompt)
715 ssize_t len;
716 wchar_t *cp = el->el_line.buffer, ch;
718 if (prompt) {
719 len = (ssize_t)wcslen(prompt);
720 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
721 cp += len;
723 len = 0;
725 for (;;) {
726 el->el_line.cursor = cp;
727 *cp = ' ';
728 el->el_line.lastchar = cp + 1;
729 re_refresh(el);
731 if (el_wgetc(el, &ch) != 1) {
732 ed_end_of_file(el, 0);
733 len = -1;
734 break;
737 switch (ch) {
739 case L'\b': /* Delete and backspace */
740 case 0177:
741 if (len == 0) {
742 len = -1;
743 break;
745 len--;
746 cp--;
747 continue;
749 case 0033: /* ESC */
750 case L'\r': /* Newline */
751 case L'\n':
752 buf[len] = ch;
753 break;
755 default:
756 if (len >= (ssize_t)(EL_BUFSIZ - 16))
757 terminal_beep(el);
758 else {
759 buf[len++] = ch;
760 *cp++ = ch;
762 continue;
764 break;
767 el->el_line.buffer[0] = '\0';
768 el->el_line.lastchar = el->el_line.buffer;
769 el->el_line.cursor = el->el_line.buffer;
770 return (int)len;
774 /* c_hpos():
775 * Return the current horizontal position of the cursor
777 libedit_private int
778 c_hpos(EditLine *el)
780 wchar_t *ptr;
783 * Find how many characters till the beginning of this line.
785 if (el->el_line.cursor == el->el_line.buffer)
786 return 0;
787 else {
788 for (ptr = el->el_line.cursor - 1;
789 ptr >= el->el_line.buffer && *ptr != '\n';
790 ptr--)
791 continue;
792 return (int)(el->el_line.cursor - ptr - 1);
796 libedit_private int
797 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
799 el->el_chared.c_resizefun = f;
800 el->el_chared.c_resizearg = a;
801 return 0;
804 libedit_private int
805 ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
807 el->el_chared.c_aliasfun = f;
808 el->el_chared.c_aliasarg = a;
809 return 0;