Prepare to support polling(4) on multiple cpus:
[dragonfly/port-amd64.git] / lib / libedit / chared.c
blobb99e9ed162799b2e4dc2ac037054fc8473294630
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)chared.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: chared.c,v 1.25 2005/08/08 01:41:30 christos Exp $
34 * $DragonFly: src/lib/libedit/chared.c,v 1.8 2007/05/05 00:27:39 pavalos Exp $
37 #include "config.h"
40 * chared.c: Character editor utilities
42 #include <stdlib.h>
43 #include "el.h"
45 private void ch__clearmacro(EditLine *);
47 /* value to leave unused in line buffer */
48 #define EL_LEAVE 2
50 /* cv_undo():
51 * Handle state for the vi undo command
53 protected void
54 cv_undo(EditLine *el)
56 c_undo_t *vu = &el->el_chared.c_undo;
57 c_redo_t *r = &el->el_chared.c_redo;
58 unsigned int size;
60 /* Save entire line for undo */
61 size = el->el_line.lastchar - el->el_line.buffer;
62 vu->len = size;
63 vu->cursor = el->el_line.cursor - el->el_line.buffer;
64 memcpy(vu->buf, el->el_line.buffer, size);
66 /* save command info for redo */
67 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
68 r->action = el->el_chared.c_vcmd.action;
69 r->pos = r->buf;
70 r->cmd = el->el_state.thiscmd;
71 r->ch = el->el_state.thisch;
74 /* cv_yank():
75 * Save yank/delete data for paste
77 protected void
78 cv_yank(EditLine *el, const char *ptr, int size)
80 c_kill_t *k = &el->el_chared.c_kill;
82 memcpy(k->buf, ptr, size +0u);
83 k->last = k->buf + size;
87 /* c_insert():
88 * Insert num characters
90 protected void
91 c_insert(EditLine *el, int num)
93 char *cp;
95 if (el->el_line.lastchar + num >= el->el_line.limit) {
96 if (!ch_enlargebufs(el, num +0u))
97 return; /* can't go past end of buffer */
100 if (el->el_line.cursor < el->el_line.lastchar) {
101 /* if I must move chars */
102 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
103 cp[num] = *cp;
105 el->el_line.lastchar += num;
109 /* c_delafter():
110 * Delete num characters after the cursor
112 protected void
113 c_delafter(EditLine *el, int num)
116 if (el->el_line.cursor + num > el->el_line.lastchar)
117 num = el->el_line.lastchar - el->el_line.cursor;
119 if (el->el_map.current != el->el_map.emacs) {
120 cv_undo(el);
121 cv_yank(el, el->el_line.cursor, num);
124 if (num > 0) {
125 char *cp;
127 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
128 *cp = cp[num];
130 el->el_line.lastchar -= num;
135 /* c_delafter1():
136 * Delete the character after the cursor, do not yank
138 protected void
139 c_delafter1(EditLine *el)
141 char *cp;
143 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
144 *cp = cp[1];
146 el->el_line.lastchar--;
150 /* c_delbefore():
151 * Delete num characters before the cursor
153 protected void
154 c_delbefore(EditLine *el, int num)
157 if (el->el_line.cursor - num < el->el_line.buffer)
158 num = el->el_line.cursor - el->el_line.buffer;
160 if (el->el_map.current != el->el_map.emacs) {
161 cv_undo(el);
162 cv_yank(el, el->el_line.cursor - num, num);
165 if (num > 0) {
166 char *cp;
168 for (cp = el->el_line.cursor - num;
169 cp <= el->el_line.lastchar;
170 cp++)
171 *cp = cp[num];
173 el->el_line.lastchar -= num;
178 /* c_delbefore1():
179 * Delete the character before the cursor, do not yank
181 protected void
182 c_delbefore1(EditLine *el)
184 char *cp;
186 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
187 *cp = cp[1];
189 el->el_line.lastchar--;
193 /* ce__isword():
194 * Return if p is part of a word according to emacs
196 protected int
197 ce__isword(int p)
199 return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
203 /* cv__isword():
204 * Return if p is part of a word according to vi
206 protected int
207 cv__isword(int p)
209 if (isalnum(p) || p == '_')
210 return 1;
211 if (isgraph(p))
212 return 2;
213 return 0;
217 /* cv__isWord():
218 * Return if p is part of a big word according to vi
220 protected int
221 cv__isWord(int p)
223 return (!isspace(p));
227 /* c__prev_word():
228 * Find the previous word
230 protected char *
231 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
233 p--;
235 while (n--) {
236 while ((p >= low) && !(*wtest)((unsigned char) *p))
237 p--;
238 while ((p >= low) && (*wtest)((unsigned char) *p))
239 p--;
242 /* cp now points to one character before the word */
243 p++;
244 if (p < low)
245 p = low;
246 /* cp now points where we want it */
247 return (p);
251 /* c__next_word():
252 * Find the next word
254 protected char *
255 c__next_word(char *p, char *high, int n, int (*wtest)(int))
257 while (n--) {
258 while ((p < high) && !(*wtest)((unsigned char) *p))
259 p++;
260 while ((p < high) && (*wtest)((unsigned char) *p))
261 p++;
263 if (p > high)
264 p = high;
265 /* p now points where we want it */
266 return (p);
269 /* cv_next_word():
270 * Find the next word vi style
272 protected char *
273 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
275 int test;
277 while (n--) {
278 test = (*wtest)((unsigned char) *p);
279 while ((p < high) && (*wtest)((unsigned char) *p) == test)
280 p++;
282 * vi historically deletes with cw only the word preserving the
283 * trailing whitespace! This is not what 'w' does..
285 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
286 while ((p < high) && isspace((unsigned char) *p))
287 p++;
290 /* p now points where we want it */
291 if (p > high)
292 return (high);
293 else
294 return (p);
298 /* cv_prev_word():
299 * Find the previous word vi style
301 protected char *
302 cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
304 int test;
306 p--;
307 while (n--) {
308 while ((p > low) && isspace((unsigned char) *p))
309 p--;
310 test = (*wtest)((unsigned char) *p);
311 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
312 p--;
314 p++;
316 /* p now points where we want it */
317 if (p < low)
318 return (low);
319 else
320 return (p);
324 #ifdef notdef
325 /* c__number():
326 * Ignore character p points to, return number appearing after that.
327 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
328 * Return p pointing to last char used.
330 protected char *
331 c__number(
332 char *p, /* character position */
333 int *num, /* Return value */
334 int dval) /* dval is the number to subtract from like $-3 */
336 int i;
337 int sign = 1;
339 if (*++p == '^') {
340 *num = 1;
341 return (p);
343 if (*p == '$') {
344 if (*++p != '-') {
345 *num = 0x7fffffff; /* Handle $ */
346 return (--p);
348 sign = -1; /* Handle $- */
349 ++p;
351 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
352 continue;
353 *num = (sign < 0 ? dval - i : i);
354 return (--p);
356 #endif
358 /* cv_delfini():
359 * Finish vi delete action
361 protected void
362 cv_delfini(EditLine *el)
364 int size;
365 int action = el->el_chared.c_vcmd.action;
367 if (action & INSERT)
368 el->el_map.current = el->el_map.key;
370 if (el->el_chared.c_vcmd.pos == 0)
371 /* sanity */
372 return;
374 size = el->el_line.cursor - el->el_chared.c_vcmd.pos;
375 if (size == 0)
376 size = 1;
377 el->el_line.cursor = el->el_chared.c_vcmd.pos;
378 if (action & YANK) {
379 if (size > 0)
380 cv_yank(el, el->el_line.cursor, size);
381 else
382 cv_yank(el, el->el_line.cursor + size, -size);
383 } else {
384 if (size > 0) {
385 c_delafter(el, size);
386 re_refresh_cursor(el);
387 } else {
388 c_delbefore(el, -size);
389 el->el_line.cursor += size;
392 el->el_chared.c_vcmd.action = NOP;
396 #ifdef notdef
397 /* ce__endword():
398 * Go to the end of this word according to emacs
400 protected char *
401 ce__endword(char *p, char *high, int n)
403 p++;
405 while (n--) {
406 while ((p < high) && isspace((unsigned char) *p))
407 p++;
408 while ((p < high) && !isspace((unsigned char) *p))
409 p++;
412 p--;
413 return (p);
415 #endif
418 /* cv__endword():
419 * Go to the end of this word according to vi
421 protected char *
422 cv__endword(char *p, char *high, int n, int (*wtest)(int))
424 int test;
426 p++;
428 while (n--) {
429 while ((p < high) && isspace((unsigned char) *p))
430 p++;
432 test = (*wtest)((unsigned char) *p);
433 while ((p < high) && (*wtest)((unsigned char) *p) == test)
434 p++;
436 p--;
437 return (p);
440 /* ch_init():
441 * Initialize the character editor
443 protected int
444 ch_init(EditLine *el)
446 c_macro_t *ma = &el->el_chared.c_macro;
448 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
449 if (el->el_line.buffer == NULL)
450 return (-1);
452 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
453 el->el_line.cursor = el->el_line.buffer;
454 el->el_line.lastchar = el->el_line.buffer;
455 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
457 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
458 if (el->el_chared.c_undo.buf == NULL)
459 return (-1);
460 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
461 el->el_chared.c_undo.len = -1;
462 el->el_chared.c_undo.cursor = 0;
463 el->el_chared.c_redo.buf = (char *) el_malloc(EL_BUFSIZ);
464 if (el->el_chared.c_redo.buf == NULL)
465 return (-1);
466 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
467 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
468 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
470 el->el_chared.c_vcmd.action = NOP;
471 el->el_chared.c_vcmd.pos = el->el_line.buffer;
473 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
474 if (el->el_chared.c_kill.buf == NULL)
475 return (-1);
476 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
477 el->el_chared.c_kill.mark = el->el_line.buffer;
478 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
480 el->el_map.current = el->el_map.key;
482 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
483 el->el_state.doingarg = 0;
484 el->el_state.metanext = 0;
485 el->el_state.argument = 1;
486 el->el_state.lastcmd = ED_UNASSIGNED;
488 ma->level = -1;
489 ma->offset = 0;
490 ma->macro = (char **) el_malloc(EL_MAXMACRO * sizeof(char *));
491 if (ma->macro == NULL)
492 return (-1);
493 return (0);
496 /* ch_reset():
497 * Reset the character editor
499 protected void
500 ch_reset(EditLine *el, int mclear)
502 el->el_line.cursor = el->el_line.buffer;
503 el->el_line.lastchar = el->el_line.buffer;
505 el->el_chared.c_undo.len = -1;
506 el->el_chared.c_undo.cursor = 0;
508 el->el_chared.c_vcmd.action = NOP;
509 el->el_chared.c_vcmd.pos = el->el_line.buffer;
511 el->el_chared.c_kill.mark = el->el_line.buffer;
513 el->el_map.current = el->el_map.key;
515 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
516 el->el_state.doingarg = 0;
517 el->el_state.metanext = 0;
518 el->el_state.argument = 1;
519 el->el_state.lastcmd = ED_UNASSIGNED;
521 el->el_history.eventno = 0;
523 if (mclear)
524 ch__clearmacro(el);
527 private void
528 ch__clearmacro(el)
529 EditLine *el;
531 c_macro_t *ma = &el->el_chared.c_macro;
532 while (ma->level >= 0)
533 el_free((ptr_t)ma->macro[ma->level--]);
536 /* ch_enlargebufs():
537 * Enlarge line buffer to be able to hold twice as much characters.
538 * Returns 1 if successful, 0 if not.
540 protected int
541 ch_enlargebufs(el, addlen)
542 EditLine *el;
543 size_t addlen;
545 size_t sz, newsz;
546 char *newbuffer, *oldbuf, *oldkbuf;
548 sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
549 newsz = sz * 2;
551 * If newly required length is longer than current buffer, we need
552 * to make the buffer big enough to hold both old and new stuff.
554 if (addlen > sz) {
555 while(newsz - sz < addlen)
556 newsz *= 2;
560 * Reallocate line buffer.
562 newbuffer = el_realloc(el->el_line.buffer, newsz);
563 if (!newbuffer)
564 return 0;
566 /* zero the newly added memory, leave old data in */
567 (void) memset(&newbuffer[sz], 0, newsz - sz);
569 oldbuf = el->el_line.buffer;
571 el->el_line.buffer = newbuffer;
572 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
573 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
574 /* don't set new size until all buffers are enlarged */
575 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
578 * Reallocate kill buffer.
580 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
581 if (!newbuffer)
582 return 0;
584 /* zero the newly added memory, leave old data in */
585 (void) memset(&newbuffer[sz], 0, newsz - sz);
587 oldkbuf = el->el_chared.c_kill.buf;
589 el->el_chared.c_kill.buf = newbuffer;
590 el->el_chared.c_kill.last = newbuffer +
591 (el->el_chared.c_kill.last - oldkbuf);
592 el->el_chared.c_kill.mark = el->el_line.buffer +
593 (el->el_chared.c_kill.mark - oldbuf);
596 * Reallocate undo buffer.
598 newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
599 if (!newbuffer)
600 return 0;
602 /* zero the newly added memory, leave old data in */
603 (void) memset(&newbuffer[sz], 0, newsz - sz);
604 el->el_chared.c_undo.buf = newbuffer;
606 newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz);
607 if (!newbuffer)
608 return 0;
609 el->el_chared.c_redo.pos = newbuffer +
610 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
611 el->el_chared.c_redo.lim = newbuffer +
612 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
613 el->el_chared.c_redo.buf = newbuffer;
615 if (!hist_enlargebuf(el, sz, newsz))
616 return 0;
618 /* Safe to set enlarged buffer size */
619 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
620 return 1;
623 /* ch_end():
624 * Free the data structures used by the editor
626 protected void
627 ch_end(EditLine *el)
629 el_free((ptr_t) el->el_line.buffer);
630 el->el_line.buffer = NULL;
631 el->el_line.limit = NULL;
632 el_free((ptr_t) el->el_chared.c_undo.buf);
633 el->el_chared.c_undo.buf = NULL;
634 el_free((ptr_t) el->el_chared.c_redo.buf);
635 el->el_chared.c_redo.buf = NULL;
636 el->el_chared.c_redo.pos = NULL;
637 el->el_chared.c_redo.lim = NULL;
638 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
639 el_free((ptr_t) el->el_chared.c_kill.buf);
640 el->el_chared.c_kill.buf = NULL;
641 ch_reset(el, 1);
642 el_free((ptr_t) el->el_chared.c_macro.macro);
643 el->el_chared.c_macro.macro = NULL;
647 /* el_insertstr():
648 * Insert string at cursorI
650 public int
651 el_insertstr(EditLine *el, const char *s)
653 size_t len;
655 if ((len = strlen(s)) == 0)
656 return (-1);
657 if (el->el_line.lastchar + len >= el->el_line.limit) {
658 if (!ch_enlargebufs(el, len))
659 return (-1);
662 c_insert(el, (int)len);
663 while (*s)
664 *el->el_line.cursor++ = *s++;
665 return (0);
669 /* el_deletestr():
670 * Delete num characters before the cursor
672 public void
673 el_deletestr(EditLine *el, int n)
675 if (n <= 0)
676 return;
678 if (el->el_line.cursor < &el->el_line.buffer[n])
679 return;
681 c_delbefore(el, n); /* delete before dot */
682 el->el_line.cursor -= n;
683 if (el->el_line.cursor < el->el_line.buffer)
684 el->el_line.cursor = el->el_line.buffer;
687 /* c_gets():
688 * Get a string
690 protected int
691 c_gets(EditLine *el, char *buf, const char *prompt)
693 char ch;
694 int len;
695 char *cp = el->el_line.buffer;
697 if (prompt) {
698 len = strlen(prompt);
699 memcpy(cp, prompt, len + 0u);
700 cp += len;
702 len = 0;
704 for (;;) {
705 el->el_line.cursor = cp;
706 *cp = ' ';
707 el->el_line.lastchar = cp + 1;
708 re_refresh(el);
710 if (el_getc(el, &ch) != 1) {
711 ed_end_of_file(el, 0);
712 len = -1;
713 break;
716 switch (ch) {
718 case 0010: /* Delete and backspace */
719 case 0177:
720 if (len <= 0) {
721 len = -1;
722 break;
724 cp--;
725 continue;
727 case 0033: /* ESC */
728 case '\r': /* Newline */
729 case '\n':
730 buf[len] = ch;
731 break;
733 default:
734 if (len >= EL_BUFSIZ - 16)
735 term_beep(el);
736 else {
737 buf[len++] = ch;
738 *cp++ = ch;
740 continue;
742 break;
745 el->el_line.buffer[0] = '\0';
746 el->el_line.lastchar = el->el_line.buffer;
747 el->el_line.cursor = el->el_line.buffer;
748 return len;
752 /* c_hpos():
753 * Return the current horizontal position of the cursor
755 protected int
756 c_hpos(EditLine *el)
758 char *ptr;
761 * Find how many characters till the beginning of this line.
763 if (el->el_line.cursor == el->el_line.buffer)
764 return (0);
765 else {
766 for (ptr = el->el_line.cursor - 1;
767 ptr >= el->el_line.buffer && *ptr != '\n';
768 ptr--)
769 continue;
770 return (el->el_line.cursor - ptr - 1);