Match acpi_cpu_add_child()'s prototype with that of the corresponding
[dragonfly.git] / lib / libedit / chared.c
blob90eacf6c485964fe2559c29411ef213e28977eec
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.9 2008/09/30 16:57:05 swildner 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(EditLine *el)
530 c_macro_t *ma = &el->el_chared.c_macro;
531 while (ma->level >= 0)
532 el_free((ptr_t)ma->macro[ma->level--]);
535 /* ch_enlargebufs():
536 * Enlarge line buffer to be able to hold twice as much characters.
537 * Returns 1 if successful, 0 if not.
539 protected int
540 ch_enlargebufs(EditLine *el, size_t addlen)
542 size_t sz, newsz;
543 char *newbuffer, *oldbuf, *oldkbuf;
545 sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
546 newsz = sz * 2;
548 * If newly required length is longer than current buffer, we need
549 * to make the buffer big enough to hold both old and new stuff.
551 if (addlen > sz) {
552 while(newsz - sz < addlen)
553 newsz *= 2;
557 * Reallocate line buffer.
559 newbuffer = el_realloc(el->el_line.buffer, newsz);
560 if (!newbuffer)
561 return 0;
563 /* zero the newly added memory, leave old data in */
564 (void) memset(&newbuffer[sz], 0, newsz - sz);
566 oldbuf = el->el_line.buffer;
568 el->el_line.buffer = newbuffer;
569 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
570 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
571 /* don't set new size until all buffers are enlarged */
572 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
575 * Reallocate kill buffer.
577 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
578 if (!newbuffer)
579 return 0;
581 /* zero the newly added memory, leave old data in */
582 (void) memset(&newbuffer[sz], 0, newsz - sz);
584 oldkbuf = el->el_chared.c_kill.buf;
586 el->el_chared.c_kill.buf = newbuffer;
587 el->el_chared.c_kill.last = newbuffer +
588 (el->el_chared.c_kill.last - oldkbuf);
589 el->el_chared.c_kill.mark = el->el_line.buffer +
590 (el->el_chared.c_kill.mark - oldbuf);
593 * Reallocate undo buffer.
595 newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
596 if (!newbuffer)
597 return 0;
599 /* zero the newly added memory, leave old data in */
600 (void) memset(&newbuffer[sz], 0, newsz - sz);
601 el->el_chared.c_undo.buf = newbuffer;
603 newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz);
604 if (!newbuffer)
605 return 0;
606 el->el_chared.c_redo.pos = newbuffer +
607 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
608 el->el_chared.c_redo.lim = newbuffer +
609 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
610 el->el_chared.c_redo.buf = newbuffer;
612 if (!hist_enlargebuf(el, sz, newsz))
613 return 0;
615 /* Safe to set enlarged buffer size */
616 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
617 return 1;
620 /* ch_end():
621 * Free the data structures used by the editor
623 protected void
624 ch_end(EditLine *el)
626 el_free((ptr_t) el->el_line.buffer);
627 el->el_line.buffer = NULL;
628 el->el_line.limit = NULL;
629 el_free((ptr_t) el->el_chared.c_undo.buf);
630 el->el_chared.c_undo.buf = NULL;
631 el_free((ptr_t) el->el_chared.c_redo.buf);
632 el->el_chared.c_redo.buf = NULL;
633 el->el_chared.c_redo.pos = NULL;
634 el->el_chared.c_redo.lim = NULL;
635 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
636 el_free((ptr_t) el->el_chared.c_kill.buf);
637 el->el_chared.c_kill.buf = NULL;
638 ch_reset(el, 1);
639 el_free((ptr_t) el->el_chared.c_macro.macro);
640 el->el_chared.c_macro.macro = NULL;
644 /* el_insertstr():
645 * Insert string at cursorI
647 public int
648 el_insertstr(EditLine *el, const char *s)
650 size_t len;
652 if ((len = strlen(s)) == 0)
653 return (-1);
654 if (el->el_line.lastchar + len >= el->el_line.limit) {
655 if (!ch_enlargebufs(el, len))
656 return (-1);
659 c_insert(el, (int)len);
660 while (*s)
661 *el->el_line.cursor++ = *s++;
662 return (0);
666 /* el_deletestr():
667 * Delete num characters before the cursor
669 public void
670 el_deletestr(EditLine *el, int n)
672 if (n <= 0)
673 return;
675 if (el->el_line.cursor < &el->el_line.buffer[n])
676 return;
678 c_delbefore(el, n); /* delete before dot */
679 el->el_line.cursor -= n;
680 if (el->el_line.cursor < el->el_line.buffer)
681 el->el_line.cursor = el->el_line.buffer;
684 /* c_gets():
685 * Get a string
687 protected int
688 c_gets(EditLine *el, char *buf, const char *prompt)
690 char ch;
691 int len;
692 char *cp = el->el_line.buffer;
694 if (prompt) {
695 len = strlen(prompt);
696 memcpy(cp, prompt, len + 0u);
697 cp += len;
699 len = 0;
701 for (;;) {
702 el->el_line.cursor = cp;
703 *cp = ' ';
704 el->el_line.lastchar = cp + 1;
705 re_refresh(el);
707 if (el_getc(el, &ch) != 1) {
708 ed_end_of_file(el, 0);
709 len = -1;
710 break;
713 switch (ch) {
715 case 0010: /* Delete and backspace */
716 case 0177:
717 if (len <= 0) {
718 len = -1;
719 break;
721 cp--;
722 continue;
724 case 0033: /* ESC */
725 case '\r': /* Newline */
726 case '\n':
727 buf[len] = ch;
728 break;
730 default:
731 if (len >= EL_BUFSIZ - 16)
732 term_beep(el);
733 else {
734 buf[len++] = ch;
735 *cp++ = ch;
737 continue;
739 break;
742 el->el_line.buffer[0] = '\0';
743 el->el_line.lastchar = el->el_line.buffer;
744 el->el_line.cursor = el->el_line.buffer;
745 return len;
749 /* c_hpos():
750 * Return the current horizontal position of the cursor
752 protected int
753 c_hpos(EditLine *el)
755 char *ptr;
758 * Find how many characters till the beginning of this line.
760 if (el->el_line.cursor == el->el_line.buffer)
761 return (0);
762 else {
763 for (ptr = el->el_line.cursor - 1;
764 ptr >= el->el_line.buffer && *ptr != '\n';
765 ptr--)
766 continue;
767 return (el->el_line.cursor - ptr - 1);