ftp: NetBSD uses __dead, and we use __dead2.
[dragonfly.git] / lib / libedit / chared.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Christos Zoulas of Cornell University.
7  *
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.
19  *
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.
31  *
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 $
35  */
36
37 #include "config.h"
38
39 /*
40  * chared.c: Character editor utilities
41  */
42 #include <stdlib.h>
43 #include "el.h"
44
45 private void ch__clearmacro(EditLine *);
46
47 /* value to leave unused in line buffer */
48 #define EL_LEAVE        2
49
50 /* cv_undo():
51  *      Handle state for the vi undo command
52  */
53 protected void
54 cv_undo(EditLine *el)
55 {
56         c_undo_t *vu = &el->el_chared.c_undo;
57         c_redo_t *r = &el->el_chared.c_redo;
58         unsigned int size;
59
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);
65
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;
72 }
73
74 /* cv_yank():
75  *      Save yank/delete data for paste
76  */
77 protected void
78 cv_yank(EditLine *el, const char *ptr, int size)
79 {
80         c_kill_t *k = &el->el_chared.c_kill;
81
82         memcpy(k->buf, ptr, size +0u);
83         k->last = k->buf + size;
84 }
85
86
87 /* c_insert():
88  *      Insert num characters
89  */
90 protected void
91 c_insert(EditLine *el, int num)
92 {
93         char *cp;
94
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 */
98         }
99
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;
104         }
105         el->el_line.lastchar += num;
106 }
107
108
109 /* c_delafter():
110  *      Delete num characters after the cursor
111  */
112 protected void
113 c_delafter(EditLine *el, int num)
114 {
115
116         if (el->el_line.cursor + num > el->el_line.lastchar)
117                 num = el->el_line.lastchar - el->el_line.cursor;
118
119         if (el->el_map.current != el->el_map.emacs) {
120                 cv_undo(el);
121                 cv_yank(el, el->el_line.cursor, num);
122         }
123
124         if (num > 0) {
125                 char *cp;
126
127                 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
128                         *cp = cp[num];
129
130                 el->el_line.lastchar -= num;
131         }
132 }
133
134
135 /* c_delafter1():
136  *      Delete the character after the cursor, do not yank
137  */
138 protected void
139 c_delafter1(EditLine *el)
140 {
141         char *cp;
142
143         for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
144                 *cp = cp[1];
145
146         el->el_line.lastchar--;
147 }
148
149
150 /* c_delbefore():
151  *      Delete num characters before the cursor
152  */
153 protected void
154 c_delbefore(EditLine *el, int num)
155 {
156
157         if (el->el_line.cursor - num < el->el_line.buffer)
158                 num = el->el_line.cursor - el->el_line.buffer;
159
160         if (el->el_map.current != el->el_map.emacs) {
161                 cv_undo(el);
162                 cv_yank(el, el->el_line.cursor - num, num);
163         }
164
165         if (num > 0) {
166                 char *cp;
167
168                 for (cp = el->el_line.cursor - num;
169                     cp <= el->el_line.lastchar;
170                     cp++)
171                         *cp = cp[num];
172
173                 el->el_line.lastchar -= num;
174         }
175 }
176
177
178 /* c_delbefore1():
179  *      Delete the character before the cursor, do not yank
180  */
181 protected void
182 c_delbefore1(EditLine *el)
183 {
184         char *cp;
185
186         for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
187                 *cp = cp[1];
188
189         el->el_line.lastchar--;
190 }
191
192
193 /* ce__isword():
194  *      Return if p is part of a word according to emacs
195  */
196 protected int
197 ce__isword(int p)
198 {
199         return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
200 }
201
202
203 /* cv__isword():
204  *      Return if p is part of a word according to vi
205  */
206 protected int
207 cv__isword(int p)
208 {
209         if (isalnum(p) || p == '_')
210                 return 1;
211         if (isgraph(p))
212                 return 2;
213         return 0;
214 }
215
216
217 /* cv__isWord():
218  *      Return if p is part of a big word according to vi
219  */
220 protected int
221 cv__isWord(int p)
222 {
223         return (!isspace(p));
224 }
225
226
227 /* c__prev_word():
228  *      Find the previous word
229  */
230 protected char *
231 c__prev_word(char *p, char *low, int n, int (*wtest)(int))
232 {
233         p--;
234
235         while (n--) {
236                 while ((p >= low) && !(*wtest)((unsigned char) *p))
237                         p--;
238                 while ((p >= low) && (*wtest)((unsigned char) *p))
239                         p--;
240         }
241
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);
248 }
249
250
251 /* c__next_word():
252  *      Find the next word
253  */
254 protected char *
255 c__next_word(char *p, char *high, int n, int (*wtest)(int))
256 {
257         while (n--) {
258                 while ((p < high) && !(*wtest)((unsigned char) *p))
259                         p++;
260                 while ((p < high) && (*wtest)((unsigned char) *p))
261                         p++;
262         }
263         if (p > high)
264                 p = high;
265         /* p now points where we want it */
266         return (p);
267 }
268
269 /* cv_next_word():
270  *      Find the next word vi style
271  */
272 protected char *
273 cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
274 {
275         int test;
276
277         while (n--) {
278                 test = (*wtest)((unsigned char) *p);
279                 while ((p < high) && (*wtest)((unsigned char) *p) == test)
280                         p++;
281                 /*
282                  * vi historically deletes with cw only the word preserving the
283                  * trailing whitespace! This is not what 'w' does..
284                  */
285                 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
286                         while ((p < high) && isspace((unsigned char) *p))
287                                 p++;
288         }
289
290         /* p now points where we want it */
291         if (p > high)
292                 return (high);
293         else
294                 return (p);
295 }
296
297
298 /* cv_prev_word():
299  *      Find the previous word vi style
300  */
301 protected char *
302 cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
303 {
304         int test;
305
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--;
313         }
314         p++;
315
316         /* p now points where we want it */
317         if (p < low)
318                 return (low);
319         else
320                 return (p);
321 }
322
323
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.
329  */
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 */
335 {
336         int i;
337         int sign = 1;
338
339         if (*++p == '^') {
340                 *num = 1;
341                 return (p);
342         }
343         if (*p == '$') {
344                 if (*++p != '-') {
345                         *num = 0x7fffffff;      /* Handle $ */
346                         return (--p);
347                 }
348                 sign = -1;                      /* Handle $- */
349                 ++p;
350         }
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);
355 }
356 #endif
357
358 /* cv_delfini():
359  *      Finish vi delete action
360  */
361 protected void
362 cv_delfini(EditLine *el)
363 {
364         int size;
365         int action = el->el_chared.c_vcmd.action;
366
367         if (action & INSERT)
368                 el->el_map.current = el->el_map.key;
369
370         if (el->el_chared.c_vcmd.pos == 0)
371                 /* sanity */
372                 return;
373
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;
390                 }
391         }
392         el->el_chared.c_vcmd.action = NOP;
393 }
394
395
396 #ifdef notdef
397 /* ce__endword():
398  *      Go to the end of this word according to emacs
399  */
400 protected char *
401 ce__endword(char *p, char *high, int n)
402 {
403         p++;
404
405         while (n--) {
406                 while ((p < high) && isspace((unsigned char) *p))
407                         p++;
408                 while ((p < high) && !isspace((unsigned char) *p))
409                         p++;
410         }
411
412         p--;
413         return (p);
414 }
415 #endif
416
417
418 /* cv__endword():
419  *      Go to the end of this word according to vi
420  */
421 protected char *
422 cv__endword(char *p, char *high, int n, int (*wtest)(int))
423 {
424         int test;
425
426         p++;
427
428         while (n--) {
429                 while ((p < high) && isspace((unsigned char) *p))
430                         p++;
431
432                 test = (*wtest)((unsigned char) *p);
433                 while ((p < high) && (*wtest)((unsigned char) *p) == test)
434                         p++;
435         }
436         p--;
437         return (p);
438 }
439
440 /* ch_init():
441  *      Initialize the character editor
442  */
443 protected int
444 ch_init(EditLine *el)
445 {
446         c_macro_t *ma = &el->el_chared.c_macro;
447
448         el->el_line.buffer              = (char *) el_malloc(EL_BUFSIZ);
449         if (el->el_line.buffer == NULL)
450                 return (-1);
451
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];
456
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;
469
470         el->el_chared.c_vcmd.action     = NOP;
471         el->el_chared.c_vcmd.pos        = el->el_line.buffer;
472
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;
479
480         el->el_map.current              = el->el_map.key;
481
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;
487
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);
494 }
495
496 /* ch_reset():
497  *      Reset the character editor
498  */
499 protected void
500 ch_reset(EditLine *el, int mclear)
501 {
502         el->el_line.cursor              = el->el_line.buffer;
503         el->el_line.lastchar            = el->el_line.buffer;
504
505         el->el_chared.c_undo.len        = -1;
506         el->el_chared.c_undo.cursor     = 0;
507
508         el->el_chared.c_vcmd.action     = NOP;
509         el->el_chared.c_vcmd.pos        = el->el_line.buffer;
510
511         el->el_chared.c_kill.mark       = el->el_line.buffer;
512
513         el->el_map.current              = el->el_map.key;
514
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;
520
521         el->el_history.eventno          = 0;
522
523         if (mclear)
524                 ch__clearmacro(el);
525 }
526
527 private void
528 ch__clearmacro(EditLine *el)
529 {
530         c_macro_t *ma = &el->el_chared.c_macro;
531         while (ma->level >= 0)
532                 el_free((ptr_t)ma->macro[ma->level--]);
533 }
534
535 /* ch_enlargebufs():
536  *      Enlarge line buffer to be able to hold twice as much characters.
537  *      Returns 1 if successful, 0 if not.
538  */
539 protected int
540 ch_enlargebufs(EditLine *el, size_t addlen)
541 {
542         size_t sz, newsz;
543         char *newbuffer, *oldbuf, *oldkbuf;
544
545         sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
546         newsz = sz * 2;
547         /*
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.
550          */
551         if (addlen > sz) {
552                 while(newsz - sz < addlen)
553                         newsz *= 2;
554         }
555
556         /*
557          * Reallocate line buffer.
558          */
559         newbuffer = el_realloc(el->el_line.buffer, newsz);
560         if (!newbuffer)
561                 return 0;
562
563         /* zero the newly added memory, leave old data in */
564         (void) memset(&newbuffer[sz], 0, newsz - sz);
565             
566         oldbuf = el->el_line.buffer;
567
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];
573
574         /*
575          * Reallocate kill buffer.
576          */
577         newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
578         if (!newbuffer)
579                 return 0;
580
581         /* zero the newly added memory, leave old data in */
582         (void) memset(&newbuffer[sz], 0, newsz - sz);
583
584         oldkbuf = el->el_chared.c_kill.buf;
585
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);
591
592         /*
593          * Reallocate undo buffer.
594          */
595         newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
596         if (!newbuffer)
597                 return 0;
598
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;
602
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;
611         
612         if (!hist_enlargebuf(el, sz, newsz))
613                 return 0;
614
615         /* Safe to set enlarged buffer size */
616         el->el_line.limit  = &el->el_line.buffer[newsz - EL_LEAVE];
617         return 1;
618 }
619
620 /* ch_end():
621  *      Free the data structures used by the editor
622  */
623 protected void
624 ch_end(EditLine *el)
625 {
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;
641 }
642
643
644 /* el_insertstr():
645  *      Insert string at cursorI
646  */
647 public int
648 el_insertstr(EditLine *el, const char *s)
649 {
650         size_t len;
651
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);
657         }
658
659         c_insert(el, (int)len);
660         while (*s)
661                 *el->el_line.cursor++ = *s++;
662         return (0);
663 }
664
665
666 /* el_deletestr():
667  *      Delete num characters before the cursor
668  */
669 public void
670 el_deletestr(EditLine *el, int n)
671 {
672         if (n <= 0)
673                 return;
674
675         if (el->el_line.cursor < &el->el_line.buffer[n])
676                 return;
677
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;
682 }
683
684 /* c_gets():
685  *      Get a string
686  */
687 protected int
688 c_gets(EditLine *el, char *buf, const char *prompt)
689 {
690         char ch;
691         int len;
692         char *cp = el->el_line.buffer;
693
694         if (prompt) {
695                 len = strlen(prompt);
696                 memcpy(cp, prompt, len + 0u);
697                 cp += len;
698         }
699         len = 0;
700
701         for (;;) {
702                 el->el_line.cursor = cp;
703                 *cp = ' ';
704                 el->el_line.lastchar = cp + 1;
705                 re_refresh(el);
706
707                 if (el_getc(el, &ch) != 1) {
708                         ed_end_of_file(el, 0);
709                         len = -1;
710                         break;
711                 }
712
713                 switch (ch) {
714
715                 case 0010:      /* Delete and backspace */
716                 case 0177:
717                         if (len <= 0) {
718                                 len = -1;
719                                 break;
720                         }
721                         cp--;
722                         continue;
723
724                 case 0033:      /* ESC */
725                 case '\r':      /* Newline */
726                 case '\n':
727                         buf[len] = ch;
728                         break;
729
730                 default:
731                         if (len >= EL_BUFSIZ - 16)
732                                 term_beep(el);
733                         else {
734                                 buf[len++] = ch;
735                                 *cp++ = ch;
736                         }
737                         continue;
738                 }
739                 break;
740         }
741
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;
746 }
747
748
749 /* c_hpos():
750  *      Return the current horizontal position of the cursor
751  */
752 protected int
753 c_hpos(EditLine *el)
754 {
755         char *ptr;
756
757         /*
758          * Find how many characters till the beginning of this line.
759          */
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);
768         }
769 }