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 * $NetBSD: chared.c,v 1.24 2005/08/01 23:00:15 christos Exp $ |
33 */ 34 35#if !defined(lint) && !defined(SCCSID) 36static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93"; 37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: head/lib/libedit/chared.c 148834 2005-08-07 20:55:59Z stefanf $"); |
40 41/* 42 * chared.c: Character editor utilities 43 */ 44#include "sys.h" 45 46#include <stdlib.h> 47#include "el.h" 48 |
49private 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 */ 57protected void |
58cv_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 unsigned int size; 63 64 /* Save entire line for undo */ 65 size = el->el_line.lastchar - el->el_line.buffer; 66 vu->len = size; 67 vu->cursor = el->el_line.cursor - el->el_line.buffer; 68 memcpy(vu->buf, el->el_line.buffer, size); 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 */ 81protected void 82cv_yank(EditLine *el, const char *ptr, int size) 83{ 84 c_kill_t *k = &el->el_chared.c_kill; |
85 |
86 memcpy(k->buf, ptr, size +0u); 87 k->last = k->buf + size; 88} 89 90 |
91/* c_insert(): 92 * Insert num characters 93 */ 94protected void 95c_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, num +0u)) 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} --- 4 unchanged lines hidden (view full) --- 115 */ 116protected void 117c_delafter(EditLine *el, int num) 118{ 119 120 if (el->el_line.cursor + num > el->el_line.lastchar) 121 num = 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 */ 142protected void 143c_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 */ 157protected void 158c_delbefore(EditLine *el, int num) 159{ 160 161 if (el->el_line.cursor - num < el->el_line.buffer) 162 num = 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 */ 185protected void 186c_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 */ 200protected int 201ce__isword(int p) 202{ |
203 return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL); |
204} 205 206 207/* cv__isword(): |
208 * Return if p is part of a word according to vi |
209 */ 210protected int 211cv__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 */ 224protected int |
225cv__isWord(int p) |
226{ |
227 return (!isspace(p)); |
228} 229 230 231/* c__prev_word(): 232 * Find the previous word 233 */ 234protected char * 235c__prev_word(char *p, char *low, int n, int (*wtest)(int)) --- 45 unchanged lines hidden (view full) --- 281 while (n--) { 282 test = (*wtest)((unsigned char) *p); 283 while ((p < high) && (*wtest)((unsigned char) *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((unsigned char) *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 */ 305protected char * |
306cv_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((unsigned char) *p)) 313 p--; |
314 test = (*wtest)((unsigned char) *p); 315 while ((p >= low) && (*wtest)((unsigned char) *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 --- 34 unchanged lines hidden (view full) --- 361 362/* cv_delfini(): 363 * Finish vi delete action 364 */ 365protected void 366cv_delfini(EditLine *el) 367{ 368 int size; |
369 int action = el->el_chared.c_vcmd.action; |
370 |
371 if (action & INSERT) |
372 el->el_map.current = el->el_map.key; 373 |
374 if (el->el_chared.c_vcmd.pos == 0) |
375 /* sanity */ |
376 return; 377 |
378 size = el->el_line.cursor - el->el_chared.c_vcmd.pos; 379 if (size == 0) |
380 size = 1; |
381 el->el_line.cursor = el->el_chared.c_vcmd.pos; 382 if (action & YANK) { 383 if (size > 0) 384 cv_yank(el, el->el_line.cursor, size); 385 else 386 cv_yank(el, el->el_line.cursor + size, -size); 387 } else { 388 if (size > 0) { 389 c_delafter(el, size); 390 re_refresh_cursor(el); 391 } else { 392 c_delbefore(el, -size); 393 el->el_line.cursor += size; 394 } |
395 } |
396 el->el_chared.c_vcmd.action = NOP; |
397} 398 399 400#ifdef notdef 401/* ce__endword(): 402 * Go to the end of this word according to emacs 403 */ 404protected char * --- 13 unchanged lines hidden (view full) --- 418} 419#endif 420 421 422/* cv__endword(): 423 * Go to the end of this word according to vi 424 */ 425protected char * |
426cv__endword(char *p, char *high, int n, int (*wtest)(int)) |
427{ |
428 int test; 429 |
430 p++; 431 432 while (n--) { 433 while ((p < high) && isspace((unsigned char) *p)) 434 p++; 435 |
436 test = (*wtest)((unsigned char) *p); 437 while ((p < high) && (*wtest)((unsigned char) *p) == test) 438 p++; |
439 } 440 p--; 441 return (p); 442} 443 444/* ch_init(): 445 * Initialize the character editor 446 */ 447protected int 448ch_init(EditLine *el) 449{ |
450 c_macro_t *ma = &el->el_chared.c_macro; 451 |
452 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ); 453 if (el->el_line.buffer == NULL) 454 return (-1); 455 456 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ); 457 el->el_line.cursor = el->el_line.buffer; 458 el->el_line.lastchar = el->el_line.buffer; |
459 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE]; |
460 461 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ); 462 if (el->el_chared.c_undo.buf == NULL) 463 return (-1); 464 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ); |
465 el->el_chared.c_undo.len = -1; 466 el->el_chared.c_undo.cursor = 0; 467 el->el_chared.c_redo.buf = (char *) el_malloc(EL_BUFSIZ); 468 if (el->el_chared.c_redo.buf == NULL) 469 return (-1); 470 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf; 471 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ; 472 el->el_chared.c_redo.cmd = ED_UNASSIGNED; |
473 474 el->el_chared.c_vcmd.action = NOP; 475 el->el_chared.c_vcmd.pos = el->el_line.buffer; |
476 477 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ); 478 if (el->el_chared.c_kill.buf == NULL) 479 return (-1); 480 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ); 481 el->el_chared.c_kill.mark = el->el_line.buffer; 482 el->el_chared.c_kill.last = el->el_chared.c_kill.buf; 483 484 el->el_map.current = el->el_map.key; 485 486 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */ 487 el->el_state.doingarg = 0; 488 el->el_state.metanext = 0; 489 el->el_state.argument = 1; 490 el->el_state.lastcmd = ED_UNASSIGNED; 491 |
492 ma->level = -1; 493 ma->offset = 0; 494 ma->macro = (char **) el_malloc(EL_MAXMACRO * sizeof(char *)); 495 if (ma->macro == NULL) |
496 return (-1); 497 return (0); 498} 499 500/* ch_reset(): 501 * Reset the character editor 502 */ 503protected void |
504ch_reset(EditLine *el, int mclear) |
505{ 506 el->el_line.cursor = el->el_line.buffer; 507 el->el_line.lastchar = el->el_line.buffer; 508 |
509 el->el_chared.c_undo.len = -1; 510 el->el_chared.c_undo.cursor = 0; |
511 512 el->el_chared.c_vcmd.action = NOP; 513 el->el_chared.c_vcmd.pos = el->el_line.buffer; |
514 515 el->el_chared.c_kill.mark = el->el_line.buffer; 516 517 el->el_map.current = el->el_map.key; 518 519 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */ 520 el->el_state.doingarg = 0; 521 el->el_state.metanext = 0; 522 el->el_state.argument = 1; 523 el->el_state.lastcmd = ED_UNASSIGNED; 524 |
525 if (mclear) 526 ch__clearmacro(el); 527} |
528 |
529private void 530ch__clearmacro(el) 531 EditLine *el; 532{ 533 c_macro_t *ma = &el->el_chared.c_macro; 534 while (ma->level >= 0) 535 el_free((ptr_t)ma->macro[ma->level--]); |
536} 537 538/* ch_enlargebufs(): 539 * Enlarge line buffer to be able to hold twice as much characters. 540 * Returns 1 if successful, 0 if not. 541 */ 542protected int 543ch_enlargebufs(el, addlen) --- 24 unchanged lines hidden (view full) --- 568 /* zero the newly added memory, leave old data in */ 569 (void) memset(&newbuffer[sz], 0, newsz - sz); 570 571 oldbuf = el->el_line.buffer; 572 573 el->el_line.buffer = newbuffer; 574 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); 575 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); |
576 /* don't set new size until all buffers are enlarged */ 577 el->el_line.limit = &newbuffer[sz - EL_LEAVE]; |
578 579 /* 580 * Reallocate kill buffer. 581 */ 582 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz); 583 if (!newbuffer) 584 return 0; 585 --- 12 unchanged lines hidden (view full) --- 598 * Reallocate undo buffer. 599 */ 600 newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz); 601 if (!newbuffer) 602 return 0; 603 604 /* zero the newly added memory, leave old data in */ 605 (void) memset(&newbuffer[sz], 0, newsz - sz); |
606 el->el_chared.c_undo.buf = newbuffer; |
607 608 newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz); 609 if (!newbuffer) 610 return 0; 611 el->el_chared.c_redo.pos = newbuffer + 612 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf); 613 el->el_chared.c_redo.lim = newbuffer + 614 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf); 615 el->el_chared.c_redo.buf = newbuffer; |
616 617 if (!hist_enlargebuf(el, sz, newsz)) 618 return 0; 619 |
620 /* Safe to set enlarged buffer size */ 621 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE]; |
622 return 1; 623} 624 625/* ch_end(): 626 * Free the data structures used by the editor 627 */ 628protected void 629ch_end(EditLine *el) 630{ 631 el_free((ptr_t) el->el_line.buffer); 632 el->el_line.buffer = NULL; 633 el->el_line.limit = NULL; 634 el_free((ptr_t) el->el_chared.c_undo.buf); 635 el->el_chared.c_undo.buf = NULL; |
636 el_free((ptr_t) el->el_chared.c_redo.buf); 637 el->el_chared.c_redo.buf = NULL; 638 el->el_chared.c_redo.pos = NULL; 639 el->el_chared.c_redo.lim = NULL; 640 el->el_chared.c_redo.cmd = ED_UNASSIGNED; |
641 el_free((ptr_t) el->el_chared.c_kill.buf); 642 el->el_chared.c_kill.buf = NULL; |
643 ch_reset(el, 1); |
644 el_free((ptr_t) el->el_chared.c_macro.macro); 645 el->el_chared.c_macro.macro = NULL; |
646} 647 648 649/* el_insertstr(): 650 * Insert string at cursorI 651 */ 652public int 653el_insertstr(EditLine *el, const char *s) --- 31 unchanged lines hidden (view full) --- 685 if (el->el_line.cursor < el->el_line.buffer) 686 el->el_line.cursor = el->el_line.buffer; 687} 688 689/* c_gets(): 690 * Get a string 691 */ 692protected int |
693c_gets(EditLine *el, char *buf, const char *prompt) |
694{ 695 char ch; |
696 int len; 697 char *cp = el->el_line.buffer; |
698 |
699 if (prompt) { 700 len = strlen(prompt); 701 memcpy(cp, prompt, len + 0u); 702 cp += len; 703 } 704 len = 0; 705 706 for (;;) { 707 el->el_line.cursor = cp; 708 *cp = ' '; 709 el->el_line.lastchar = cp + 1; 710 re_refresh(el); 711 712 if (el_getc(el, &ch) != 1) { 713 ed_end_of_file(el, 0); 714 len = -1; 715 break; 716 } 717 |
718 switch (ch) { |
719 |
720 case '\010': /* Delete and backspace */ 721 case '\177': |
722 if (len <= 0) { 723 len = -1; 724 break; |
725 } |
726 cp--; 727 continue; |
728 729 case '\033': /* ESC */ 730 case '\r': /* Newline */ 731 case '\n': |
732 buf[len] = ch; |
733 break; 734 735 default: |
736 if (len >= EL_BUFSIZ - 16) |
737 term_beep(el); 738 else { 739 buf[len++] = ch; |
740 *cp++ = ch; |
741 } |
742 continue; |
743 } |
744 break; |
745 } |
746 747 el->el_line.buffer[0] = '\0'; 748 el->el_line.lastchar = el->el_line.buffer; 749 el->el_line.cursor = el->el_line.buffer; 750 return len; |
751} 752 753 754/* c_hpos(): 755 * Return the current horizontal position of the cursor 756 */ 757protected int 758c_hpos(EditLine *el) --- 16 unchanged lines hidden --- |