mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / cmd-line-utils / libedit / chared.c
blob647a5afe439ef2d94a2c5b6f58346ce0c3e15799
1 /* $NetBSD: chared.c,v 1.36 2011/10/23 17:37:55 christos 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 #endif
41 #endif /* not lint && not SCCSID */
44 * chared.c: Character editor utilities
46 #include <stdlib.h>
47 #include "el.h"
49 private void ch__clearmacro (EditLine *);
51 /* value to leave unused in line buffer */
52 #define EL_LEAVE 2
54 /* cv_undo():
55 * Handle state for the vi undo command
57 protected void
58 cv_undo(EditLine *el)
60 c_undo_t *vu = &el->el_chared.c_undo;
61 c_redo_t *r = &el->el_chared.c_redo;
62 size_t size;
64 /* Save entire line for undo */
65 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
66 vu->len = (ssize_t)size;
67 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
68 (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
70 /* save command info for redo */
71 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
72 r->action = el->el_chared.c_vcmd.action;
73 r->pos = r->buf;
74 r->cmd = el->el_state.thiscmd;
75 r->ch = el->el_state.thisch;
78 /* cv_yank():
79 * Save yank/delete data for paste
81 protected void
82 cv_yank(EditLine *el, const Char *ptr, int size)
84 c_kill_t *k = &el->el_chared.c_kill;
86 (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
87 k->last = k->buf + size;
91 /* c_insert():
92 * Insert num characters
94 protected void
95 c_insert(EditLine *el, int num)
97 Char *cp;
99 if (el->el_line.lastchar + num >= el->el_line.limit) {
100 if (!ch_enlargebufs(el, (size_t)num))
101 return; /* can't go past end of buffer */
104 if (el->el_line.cursor < el->el_line.lastchar) {
105 /* if I must move chars */
106 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
107 cp[num] = *cp;
109 el->el_line.lastchar += num;
113 /* c_delafter():
114 * Delete num characters after the cursor
116 protected void
117 c_delafter(EditLine *el, int num)
120 if (el->el_line.cursor + num > el->el_line.lastchar)
121 num = (int)(el->el_line.lastchar - el->el_line.cursor);
123 if (el->el_map.current != el->el_map.emacs) {
124 cv_undo(el);
125 cv_yank(el, el->el_line.cursor, num);
128 if (num > 0) {
129 Char *cp;
131 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
132 *cp = cp[num];
134 el->el_line.lastchar -= num;
139 /* c_delafter1():
140 * Delete the character after the cursor, do not yank
142 protected void
143 c_delafter1(EditLine *el)
145 Char *cp;
147 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
148 *cp = cp[1];
150 el->el_line.lastchar--;
154 /* c_delbefore():
155 * Delete num characters before the cursor
157 protected void
158 c_delbefore(EditLine *el, int num)
161 if (el->el_line.cursor - num < el->el_line.buffer)
162 num = (int)(el->el_line.cursor - el->el_line.buffer);
164 if (el->el_map.current != el->el_map.emacs) {
165 cv_undo(el);
166 cv_yank(el, el->el_line.cursor - num, num);
169 if (num > 0) {
170 Char *cp;
172 for (cp = el->el_line.cursor - num;
173 cp <= el->el_line.lastchar;
174 cp++)
175 *cp = cp[num];
177 el->el_line.lastchar -= num;
182 /* c_delbefore1():
183 * Delete the character before the cursor, do not yank
185 protected void
186 c_delbefore1(EditLine *el)
188 Char *cp;
190 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
191 *cp = cp[1];
193 el->el_line.lastchar--;
197 /* ce__isword():
198 * Return if p is part of a word according to emacs
200 protected int
201 ce__isword(Int p)
203 return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
207 /* cv__isword():
208 * Return if p is part of a word according to vi
210 protected int
211 cv__isword(Int p)
213 if (Isalnum(p) || p == '_')
214 return 1;
215 if (Isgraph(p))
216 return 2;
217 return 0;
221 /* cv__isWord():
222 * Return if p is part of a big word according to vi
224 protected int
225 cv__isWord(Int p)
227 return !Isspace(p);
231 /* c__prev_word():
232 * Find the previous word
234 protected Char *
235 c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
237 p--;
239 while (n--) {
240 while ((p >= low) && !(*wtest)(*p))
241 p--;
242 while ((p >= low) && (*wtest)(*p))
243 p--;
246 /* cp now points to one character before the word */
247 p++;
248 if (p < low)
249 p = low;
250 /* cp now points where we want it */
251 return p;
255 /* c__next_word():
256 * Find the next word
258 protected Char *
259 c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
261 while (n--) {
262 while ((p < high) && !(*wtest)(*p))
263 p++;
264 while ((p < high) && (*wtest)(*p))
265 p++;
267 if (p > high)
268 p = high;
269 /* p now points where we want it */
270 return p;
273 /* cv_next_word():
274 * Find the next word vi style
276 protected Char *
277 cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
279 int test;
281 while (n--) {
282 test = (*wtest)(*p);
283 while ((p < high) && (*wtest)(*p) == test)
284 p++;
286 * vi historically deletes with cw only the word preserving the
287 * trailing whitespace! This is not what 'w' does..
289 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
290 while ((p < high) && Isspace(*p))
291 p++;
294 /* p now points where we want it */
295 if (p > high)
296 return high;
297 else
298 return p;
302 /* cv_prev_word():
303 * Find the previous word vi style
305 protected Char *
306 cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
308 int test;
310 p--;
311 while (n--) {
312 while ((p > low) && Isspace(*p))
313 p--;
314 test = (*wtest)(*p);
315 while ((p >= low) && (*wtest)(*p) == test)
316 p--;
318 p++;
320 /* p now points where we want it */
321 if (p < low)
322 return low;
323 else
324 return p;
328 /* cv_delfini():
329 * Finish vi delete action
331 protected void
332 cv_delfini(EditLine *el)
334 int size;
335 int action = el->el_chared.c_vcmd.action;
337 if (action & INSERT)
338 el->el_map.current = el->el_map.key;
340 if (el->el_chared.c_vcmd.pos == 0)
341 /* sanity */
342 return;
344 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
345 if (size == 0)
346 size = 1;
347 el->el_line.cursor = el->el_chared.c_vcmd.pos;
348 if (action & YANK) {
349 if (size > 0)
350 cv_yank(el, el->el_line.cursor, size);
351 else
352 cv_yank(el, el->el_line.cursor + size, -size);
353 } else {
354 if (size > 0) {
355 c_delafter(el, size);
356 re_refresh_cursor(el);
357 } else {
358 c_delbefore(el, -size);
359 el->el_line.cursor += size;
362 el->el_chared.c_vcmd.action = NOP;
366 /* cv__endword():
367 * Go to the end of this word according to vi
369 protected Char *
370 cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
372 int test;
374 p++;
376 while (n--) {
377 while ((p < high) && Isspace(*p))
378 p++;
380 test = (*wtest)(*p);
381 while ((p < high) && (*wtest)(*p) == test)
382 p++;
384 p--;
385 return p;
388 /* ch_init():
389 * Initialize the character editor
391 protected int
392 ch_init(EditLine *el)
394 c_macro_t *ma = &el->el_chared.c_macro;
396 el->el_line.buffer = el_malloc(EL_BUFSIZ *
397 sizeof(*el->el_line.buffer));
398 if (el->el_line.buffer == NULL)
399 return -1;
401 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
402 sizeof(*el->el_line.buffer));
403 el->el_line.cursor = el->el_line.buffer;
404 el->el_line.lastchar = el->el_line.buffer;
405 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
407 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
408 sizeof(*el->el_chared.c_undo.buf));
409 if (el->el_chared.c_undo.buf == NULL)
410 return -1;
411 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
412 sizeof(*el->el_chared.c_undo.buf));
413 el->el_chared.c_undo.len = -1;
414 el->el_chared.c_undo.cursor = 0;
415 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
416 sizeof(*el->el_chared.c_redo.buf));
417 if (el->el_chared.c_redo.buf == NULL)
418 return -1;
419 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
420 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
421 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
423 el->el_chared.c_vcmd.action = NOP;
424 el->el_chared.c_vcmd.pos = el->el_line.buffer;
426 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
427 sizeof(*el->el_chared.c_kill.buf));
428 if (el->el_chared.c_kill.buf == NULL)
429 return -1;
430 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
431 sizeof(*el->el_chared.c_kill.buf));
432 el->el_chared.c_kill.mark = el->el_line.buffer;
433 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
434 el->el_chared.c_resizefun = NULL;
435 el->el_chared.c_resizearg = NULL;
437 el->el_map.current = el->el_map.key;
439 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
440 el->el_state.doingarg = 0;
441 el->el_state.metanext = 0;
442 el->el_state.argument = 1;
443 el->el_state.lastcmd = ED_UNASSIGNED;
445 ma->level = -1;
446 ma->offset = 0;
447 ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
448 if (ma->macro == NULL)
449 return -1;
450 return 0;
453 /* ch_reset():
454 * Reset the character editor
456 protected void
457 ch_reset(EditLine *el, int mclear)
459 el->el_line.cursor = el->el_line.buffer;
460 el->el_line.lastchar = el->el_line.buffer;
462 el->el_chared.c_undo.len = -1;
463 el->el_chared.c_undo.cursor = 0;
465 el->el_chared.c_vcmd.action = NOP;
466 el->el_chared.c_vcmd.pos = el->el_line.buffer;
468 el->el_chared.c_kill.mark = el->el_line.buffer;
470 el->el_map.current = el->el_map.key;
472 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
473 el->el_state.doingarg = 0;
474 el->el_state.metanext = 0;
475 el->el_state.argument = 1;
476 el->el_state.lastcmd = ED_UNASSIGNED;
478 el->el_history.eventno = 0;
480 if (mclear)
481 ch__clearmacro(el);
484 private void
485 ch__clearmacro(EditLine *el)
487 c_macro_t *ma = &el->el_chared.c_macro;
488 while (ma->level >= 0)
489 el_free(ma->macro[ma->level--]);
492 /* ch_enlargebufs():
493 * Enlarge line buffer to be able to hold twice as much characters.
494 * Returns 1 if successful, 0 if not.
496 protected int
497 ch_enlargebufs(EditLine *el, size_t addlen)
499 size_t sz, newsz;
500 Char *newbuffer, *oldbuf, *oldkbuf;
502 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
503 newsz = sz * 2;
505 * If newly required length is longer than current buffer, we need
506 * to make the buffer big enough to hold both old and new stuff.
508 if (addlen > sz) {
509 while(newsz - sz < addlen)
510 newsz *= 2;
514 * Reallocate line buffer.
516 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
517 if (!newbuffer)
518 return 0;
520 /* zero the newly added memory, leave old data in */
521 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
523 oldbuf = el->el_line.buffer;
525 el->el_line.buffer = newbuffer;
526 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
527 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
528 /* don't set new size until all buffers are enlarged */
529 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
532 * Reallocate kill buffer.
534 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
535 sizeof(*newbuffer));
536 if (!newbuffer)
537 return 0;
539 /* zero the newly added memory, leave old data in */
540 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
542 oldkbuf = el->el_chared.c_kill.buf;
544 el->el_chared.c_kill.buf = newbuffer;
545 el->el_chared.c_kill.last = newbuffer +
546 (el->el_chared.c_kill.last - oldkbuf);
547 el->el_chared.c_kill.mark = el->el_line.buffer +
548 (el->el_chared.c_kill.mark - oldbuf);
551 * Reallocate undo buffer.
553 newbuffer = el_realloc(el->el_chared.c_undo.buf,
554 newsz * sizeof(*newbuffer));
555 if (!newbuffer)
556 return 0;
558 /* zero the newly added memory, leave old data in */
559 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
560 el->el_chared.c_undo.buf = newbuffer;
562 newbuffer = el_realloc(el->el_chared.c_redo.buf,
563 newsz * sizeof(*newbuffer));
564 if (!newbuffer)
565 return 0;
566 el->el_chared.c_redo.pos = newbuffer +
567 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
568 el->el_chared.c_redo.lim = newbuffer +
569 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
570 el->el_chared.c_redo.buf = newbuffer;
572 if (!hist_enlargebuf(el, sz, newsz))
573 return 0;
575 /* Safe to set enlarged buffer size */
576 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
577 if (el->el_chared.c_resizefun)
578 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
579 return 1;
582 /* ch_end():
583 * Free the data structures used by the editor
585 protected void
586 ch_end(EditLine *el)
588 el_free(el->el_line.buffer);
589 el->el_line.buffer = NULL;
590 el->el_line.limit = NULL;
591 el_free(el->el_chared.c_undo.buf);
592 el->el_chared.c_undo.buf = NULL;
593 el_free(el->el_chared.c_redo.buf);
594 el->el_chared.c_redo.buf = NULL;
595 el->el_chared.c_redo.pos = NULL;
596 el->el_chared.c_redo.lim = NULL;
597 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
598 el_free(el->el_chared.c_kill.buf);
599 el->el_chared.c_kill.buf = NULL;
600 ch_reset(el, 1);
601 el_free(el->el_chared.c_macro.macro);
602 el->el_chared.c_macro.macro = NULL;
606 /* el_insertstr():
607 * Insert string at cursorI
609 public int
610 FUN(el,insertstr)(EditLine *el, const Char *s)
612 size_t len;
614 if ((len = Strlen(s)) == 0)
615 return -1;
616 if (el->el_line.lastchar + len >= el->el_line.limit) {
617 if (!ch_enlargebufs(el, len))
618 return -1;
621 c_insert(el, (int)len);
622 while (*s)
623 *el->el_line.cursor++ = *s++;
624 return 0;
628 /* el_deletestr():
629 * Delete num characters before the cursor
631 public void
632 el_deletestr(EditLine *el, int n)
634 if (n <= 0)
635 return;
637 if (el->el_line.cursor < &el->el_line.buffer[n])
638 return;
640 c_delbefore(el, n); /* delete before dot */
641 el->el_line.cursor -= n;
642 if (el->el_line.cursor < el->el_line.buffer)
643 el->el_line.cursor = el->el_line.buffer;
646 /* c_gets():
647 * Get a string
649 protected int
650 c_gets(EditLine *el, Char *buf, const Char *prompt)
652 Char ch;
653 ssize_t len;
654 Char *cp = el->el_line.buffer;
656 if (prompt) {
657 len = (ssize_t)Strlen(prompt);
658 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
659 cp += len;
661 len = 0;
663 for (;;) {
664 el->el_line.cursor = cp;
665 *cp = ' ';
666 el->el_line.lastchar = cp + 1;
667 re_refresh(el);
669 if (FUN(el,getc)(el, &ch) != 1) {
670 ed_end_of_file(el, 0);
671 len = -1;
672 break;
675 switch (ch) {
677 case 0010: /* Delete and backspace */
678 case 0177:
679 if (len == 0) {
680 len = -1;
681 break;
683 cp--;
684 continue;
686 case 0033: /* ESC */
687 case '\r': /* Newline */
688 case '\n':
689 buf[len] = ch;
690 break;
692 default:
693 if (len >= (ssize_t)(EL_BUFSIZ - 16))
694 terminal_beep(el);
695 else {
696 buf[len++] = ch;
697 *cp++ = ch;
699 continue;
701 break;
704 el->el_line.buffer[0] = '\0';
705 el->el_line.lastchar = el->el_line.buffer;
706 el->el_line.cursor = el->el_line.buffer;
707 return (int)len;
711 /* c_hpos():
712 * Return the current horizontal position of the cursor
714 protected int
715 c_hpos(EditLine *el)
717 Char *ptr;
720 * Find how many characters till the beginning of this line.
722 if (el->el_line.cursor == el->el_line.buffer)
723 return 0;
724 else {
725 for (ptr = el->el_line.cursor - 1;
726 ptr >= el->el_line.buffer && *ptr != '\n';
727 ptr--)
728 continue;
729 return (int)(el->el_line.cursor - ptr - 1);
733 protected int
734 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
736 el->el_chared.c_resizefun = f;
737 el->el_chared.c_resizearg = a;
738 return 0;