MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
chared.c
1 /* $NetBSD: chared.c,v 1.36 2011/10/23 17:37:55 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  * The Regents of the University of California. All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Christos Zoulas of Cornell University.
9  *
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.
21  *
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.
33  */
34 
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 */
42 
43 /*
44  * chared.c: Character editor utilities
45  */
46 #include <stdlib.h>
47 #include "el.h"
48 
49 private void ch__clearmacro (EditLine *);
50 
51 /* value to leave unused in line buffer */
52 #define EL_LEAVE 2
53 
54 /* cv_undo():
55  * Handle state for the vi undo command
56  */
57 protected void
58 cv_undo(EditLine *el)
59 {
60  c_undo_t *vu = &el->el_chared.c_undo;
61  c_redo_t *r = &el->el_chared.c_redo;
62  size_t size;
63 
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));
69 
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;
76 }
77 
78 /* cv_yank():
79  * Save yank/delete data for paste
80  */
81 protected void
82 cv_yank(EditLine *el, const Char *ptr, int size)
83 {
84  c_kill_t *k = &el->el_chared.c_kill;
85 
86  (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
87  k->last = k->buf + size;
88 }
89 
90 
91 /* c_insert():
92  * Insert num characters
93  */
94 protected void
95 c_insert(EditLine *el, int num)
96 {
97  Char *cp;
98 
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 */
102  }
103 
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;
108  }
109  el->el_line.lastchar += num;
110 }
111 
112 
113 /* c_delafter():
114  * Delete num characters after the cursor
115  */
116 protected void
117 c_delafter(EditLine *el, int num)
118 {
119 
120  if (el->el_line.cursor + num > el->el_line.lastchar)
121  num = (int)(el->el_line.lastchar - el->el_line.cursor);
122 
123  if (el->el_map.current != el->el_map.emacs) {
124  cv_undo(el);
125  cv_yank(el, el->el_line.cursor, num);
126  }
127 
128  if (num > 0) {
129  Char *cp;
130 
131  for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
132  *cp = cp[num];
133 
134  el->el_line.lastchar -= num;
135  }
136 }
137 
138 
139 /* c_delafter1():
140  * Delete the character after the cursor, do not yank
141  */
142 protected void
143 c_delafter1(EditLine *el)
144 {
145  Char *cp;
146 
147  for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
148  *cp = cp[1];
149 
150  el->el_line.lastchar--;
151 }
152 
153 
154 /* c_delbefore():
155  * Delete num characters before the cursor
156  */
157 protected void
158 c_delbefore(EditLine *el, int num)
159 {
160 
161  if (el->el_line.cursor - num < el->el_line.buffer)
162  num = (int)(el->el_line.cursor - el->el_line.buffer);
163 
164  if (el->el_map.current != el->el_map.emacs) {
165  cv_undo(el);
166  cv_yank(el, el->el_line.cursor - num, num);
167  }
168 
169  if (num > 0) {
170  Char *cp;
171 
172  for (cp = el->el_line.cursor - num;
173  cp <= el->el_line.lastchar;
174  cp++)
175  *cp = cp[num];
176 
177  el->el_line.lastchar -= num;
178  }
179 }
180 
181 
182 /* c_delbefore1():
183  * Delete the character before the cursor, do not yank
184  */
185 protected void
186 c_delbefore1(EditLine *el)
187 {
188  Char *cp;
189 
190  for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
191  *cp = cp[1];
192 
193  el->el_line.lastchar--;
194 }
195 
196 
197 /* ce__isword():
198  * Return if p is part of a word according to emacs
199  */
200 protected int
201 ce__isword(Int p)
202 {
203  return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
204 }
205 
206 
207 /* cv__isword():
208  * Return if p is part of a word according to vi
209  */
210 protected int
211 cv__isword(Int p)
212 {
213  if (Isalnum(p) || p == '_')
214  return 1;
215  if (Isgraph(p))
216  return 2;
217  return 0;
218 }
219 
220 
221 /* cv__isWord():
222  * Return if p is part of a big word according to vi
223  */
224 protected int
225 cv__isWord(Int p)
226 {
227  return !Isspace(p);
228 }
229 
230 
231 /* c__prev_word():
232  * Find the previous word
233  */
234 protected Char *
235 c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
236 {
237  p--;
238 
239  while (n--) {
240  while ((p >= low) && !(*wtest)(*p))
241  p--;
242  while ((p >= low) && (*wtest)(*p))
243  p--;
244  }
245 
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;
252 }
253 
254 
255 /* c__next_word():
256  * Find the next word
257  */
258 protected Char *
259 c__next_word(Char *p, Char *high, int n, int (*wtest)(Int))
260 {
261  while (n--) {
262  while ((p < high) && !(*wtest)(*p))
263  p++;
264  while ((p < high) && (*wtest)(*p))
265  p++;
266  }
267  if (p > high)
268  p = high;
269  /* p now points where we want it */
270  return p;
271 }
272 
273 /* cv_next_word():
274  * Find the next word vi style
275  */
276 protected Char *
277 cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int))
278 {
279  int test;
280 
281  while (n--) {
282  test = (*wtest)(*p);
283  while ((p < high) && (*wtest)(*p) == test)
284  p++;
285  /*
286  * vi historically deletes with cw only the word preserving the
287  * trailing whitespace! This is not what 'w' does..
288  */
289  if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
290  while ((p < high) && Isspace(*p))
291  p++;
292  }
293 
294  /* p now points where we want it */
295  if (p > high)
296  return high;
297  else
298  return p;
299 }
300 
301 
302 /* cv_prev_word():
303  * Find the previous word vi style
304  */
305 protected Char *
306 cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int))
307 {
308  int test;
309 
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--;
317  }
318  p++;
319 
320  /* p now points where we want it */
321  if (p < low)
322  return low;
323  else
324  return p;
325 }
326 
327 
328 /* cv_delfini():
329  * Finish vi delete action
330  */
331 protected void
332 cv_delfini(EditLine *el)
333 {
334  int size;
335  int action = el->el_chared.c_vcmd.action;
336 
337  if (action & INSERT)
338  el->el_map.current = el->el_map.key;
339 
340  if (el->el_chared.c_vcmd.pos == 0)
341  /* sanity */
342  return;
343 
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;
360  }
361  }
362  el->el_chared.c_vcmd.action = NOP;
363 }
364 
365 
366 /* cv__endword():
367  * Go to the end of this word according to vi
368  */
369 protected Char *
370 cv__endword(Char *p, Char *high, int n, int (*wtest)(Int))
371 {
372  int test;
373 
374  p++;
375 
376  while (n--) {
377  while ((p < high) && Isspace(*p))
378  p++;
379 
380  test = (*wtest)(*p);
381  while ((p < high) && (*wtest)(*p) == test)
382  p++;
383  }
384  p--;
385  return p;
386 }
387 
388 /* ch_init():
389  * Initialize the character editor
390  */
391 protected int
392 ch_init(EditLine *el)
393 {
394  c_macro_t *ma = &el->el_chared.c_macro;
395 
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;
400 
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];
406 
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;
422 
423  el->el_chared.c_vcmd.action = NOP;
424  el->el_chared.c_vcmd.pos = el->el_line.buffer;
425 
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;
436 
437  el->el_map.current = el->el_map.key;
438 
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;
444 
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;
451 }
452 
453 /* ch_reset():
454  * Reset the character editor
455  */
456 protected void
457 ch_reset(EditLine *el, int mclear)
458 {
459  el->el_line.cursor = el->el_line.buffer;
460  el->el_line.lastchar = el->el_line.buffer;
461 
462  el->el_chared.c_undo.len = -1;
463  el->el_chared.c_undo.cursor = 0;
464 
465  el->el_chared.c_vcmd.action = NOP;
466  el->el_chared.c_vcmd.pos = el->el_line.buffer;
467 
468  el->el_chared.c_kill.mark = el->el_line.buffer;
469 
470  el->el_map.current = el->el_map.key;
471 
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;
477 
478  el->el_history.eventno = 0;
479 
480  if (mclear)
481  ch__clearmacro(el);
482 }
483 
484 private void
485 ch__clearmacro(EditLine *el)
486 {
487  c_macro_t *ma = &el->el_chared.c_macro;
488  while (ma->level >= 0)
489  el_free(ma->macro[ma->level--]);
490 }
491 
492 /* ch_enlargebufs():
493  * Enlarge line buffer to be able to hold twice as much characters.
494  * Returns 1 if successful, 0 if not.
495  */
496 protected int
497 ch_enlargebufs(EditLine *el, size_t addlen)
498 {
499  size_t sz, newsz;
500  Char *newbuffer, *oldbuf, *oldkbuf;
501 
502  sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
503  newsz = sz * 2;
504  /*
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.
507  */
508  if (addlen > sz) {
509  while(newsz - sz < addlen)
510  newsz *= 2;
511  }
512 
513  /*
514  * Reallocate line buffer.
515  */
516  newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
517  if (!newbuffer)
518  return 0;
519 
520  /* zero the newly added memory, leave old data in */
521  (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
522 
523  oldbuf = el->el_line.buffer;
524 
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];
530 
531  /*
532  * Reallocate kill buffer.
533  */
534  newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
535  sizeof(*newbuffer));
536  if (!newbuffer)
537  return 0;
538 
539  /* zero the newly added memory, leave old data in */
540  (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
541 
542  oldkbuf = el->el_chared.c_kill.buf;
543 
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);
549 
550  /*
551  * Reallocate undo buffer.
552  */
553  newbuffer = el_realloc(el->el_chared.c_undo.buf,
554  newsz * sizeof(*newbuffer));
555  if (!newbuffer)
556  return 0;
557 
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;
561 
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;
571 
572  if (!hist_enlargebuf(el, sz, newsz))
573  return 0;
574 
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;
580 }
581 
582 /* ch_end():
583  * Free the data structures used by the editor
584  */
585 protected void
586 ch_end(EditLine *el)
587 {
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;
603 }
604 
605 
606 /* el_insertstr():
607  * Insert string at cursorI
608  */
609 public int
610 FUN(el,insertstr)(EditLine *el, const Char *s)
611 {
612  size_t len;
613 
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;
619  }
620 
621  c_insert(el, (int)len);
622  while (*s)
623  *el->el_line.cursor++ = *s++;
624  return 0;
625 }
626 
627 
628 /* el_deletestr():
629  * Delete num characters before the cursor
630  */
631 public void
632 el_deletestr(EditLine *el, int n)
633 {
634  if (n <= 0)
635  return;
636 
637  if (el->el_line.cursor < &el->el_line.buffer[n])
638  return;
639 
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;
644 }
645 
646 /* c_gets():
647  * Get a string
648  */
649 protected int
650 c_gets(EditLine *el, Char *buf, const Char *prompt)
651 {
652  Char ch;
653  ssize_t len;
654  Char *cp = el->el_line.buffer;
655 
656  if (prompt) {
657  len = (ssize_t)Strlen(prompt);
658  (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
659  cp += len;
660  }
661  len = 0;
662 
663  for (;;) {
664  el->el_line.cursor = cp;
665  *cp = ' ';
666  el->el_line.lastchar = cp + 1;
667  re_refresh(el);
668 
669  if (FUN(el,getc)(el, &ch) != 1) {
670  ed_end_of_file(el, 0);
671  len = -1;
672  break;
673  }
674 
675  switch (ch) {
676 
677  case 0010: /* Delete and backspace */
678  case 0177:
679  if (len == 0) {
680  len = -1;
681  break;
682  }
683  cp--;
684  continue;
685 
686  case 0033: /* ESC */
687  case '\r': /* Newline */
688  case '\n':
689  buf[len] = ch;
690  break;
691 
692  default:
693  if (len >= (ssize_t)(EL_BUFSIZ - 16))
694  terminal_beep(el);
695  else {
696  buf[len++] = ch;
697  *cp++ = ch;
698  }
699  continue;
700  }
701  break;
702  }
703 
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;
708 }
709 
710 
711 /* c_hpos():
712  * Return the current horizontal position of the cursor
713  */
714 protected int
715 c_hpos(EditLine *el)
716 {
717  Char *ptr;
718 
719  /*
720  * Find how many characters till the beginning of this line.
721  */
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);
730  }
731 }
732 
733 protected int
734 ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
735 {
736  el->el_chared.c_resizefun = f;
737  el->el_chared.c_resizearg = a;
738  return 0;
739 }