chared.c (238178) | chared.c (276881) |
---|---|
1/* $NetBSD: chared.c,v 1.40 2014/06/18 18:12:28 christos Exp $ */ 2 |
|
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 --- 14 unchanged lines hidden (view full) --- 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. | 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 --- 14 unchanged lines hidden (view full) --- 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. |
31 * 32 * $NetBSD: chared.c,v 1.27 2009/02/15 21:55:23 christos Exp $ | |
33 */ 34 | 33 */ 34 |
35#include "config.h" |
|
35#if !defined(lint) && !defined(SCCSID) | 36#if !defined(lint) && !defined(SCCSID) |
37#if 0 |
|
36static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93"; | 38static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93"; |
39#else 40__RCSID("$NetBSD: chared.c,v 1.40 2014/06/18 18:12:28 christos Exp $"); 41#endif |
|
37#endif /* not lint && not SCCSID */ 38#include <sys/cdefs.h> | 42#endif /* not lint && not SCCSID */ 43#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: head/lib/libedit/chared.c 238178 2012-07-06 19:30:50Z pfg $"); | 44__FBSDID("$FreeBSD: head/lib/libedit/chared.c 276881 2015-01-09 07:40:56Z bapt $"); |
40 41/* 42 * chared.c: Character editor utilities 43 */ | 45 46/* 47 * chared.c: Character editor utilities 48 */ |
44#include "sys.h" 45 | |
46#include <stdlib.h> 47#include "el.h" 48 | 49#include <stdlib.h> 50#include "el.h" 51 |
49private void ch__clearmacro(EditLine *); | 52private 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 size_t size; 63 64 /* Save entire line for undo */ | 53 54/* value to leave unused in line buffer */ 55#define EL_LEAVE 2 56 57/* cv_undo(): 58 * Handle state for the vi undo command 59 */ 60protected void 61cv_undo(EditLine *el) 62{ 63 c_undo_t *vu = &el->el_chared.c_undo; 64 c_redo_t *r = &el->el_chared.c_redo; 65 size_t size; 66 67 /* Save entire line for undo */ |
65 size = el->el_line.lastchar - el->el_line.buffer; 66 vu->len = size; | 68 size = (size_t)(el->el_line.lastchar - el->el_line.buffer); 69 vu->len = (ssize_t)size; |
67 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer); | 70 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer); |
68 memcpy(vu->buf, el->el_line.buffer, size); | 71 (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 */ 81protected void | 72 73 /* save command info for redo */ 74 r->count = el->el_state.doingarg ? el->el_state.argument : 0; 75 r->action = el->el_chared.c_vcmd.action; 76 r->pos = r->buf; 77 r->cmd = el->el_state.thiscmd; 78 r->ch = el->el_state.thisch; 79} 80 81/* cv_yank(): 82 * Save yank/delete data for paste 83 */ 84protected void |
82cv_yank(EditLine *el, const char *ptr, int size) | 85cv_yank(EditLine *el, const Char *ptr, int size) |
83{ 84 c_kill_t *k = &el->el_chared.c_kill; 85 | 86{ 87 c_kill_t *k = &el->el_chared.c_kill; 88 |
86 memcpy(k->buf, ptr, (size_t)size); | 89 (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 */ 94protected void 95c_insert(EditLine *el, int num) 96{ | 90 k->last = k->buf + size; 91} 92 93 94/* c_insert(): 95 * Insert num characters 96 */ 97protected void 98c_insert(EditLine *el, int num) 99{ |
97 char *cp; | 100 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 */ --- 15 unchanged lines hidden (view full) --- 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) { | 101 102 if (el->el_line.lastchar + num >= el->el_line.limit) { 103 if (!ch_enlargebufs(el, (size_t)num)) 104 return; /* can't go past end of buffer */ 105 } 106 107 if (el->el_line.cursor < el->el_line.lastchar) { 108 /* if I must move chars */ --- 15 unchanged lines hidden (view full) --- 124 num = (int)(el->el_line.lastchar - el->el_line.cursor); 125 126 if (el->el_map.current != el->el_map.emacs) { 127 cv_undo(el); 128 cv_yank(el, el->el_line.cursor, num); 129 } 130 131 if (num > 0) { |
129 char *cp; | 132 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{ | 133 134 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) 135 *cp = cp[num]; 136 137 el->el_line.lastchar -= num; 138 } 139} 140 141 142/* c_delafter1(): 143 * Delete the character after the cursor, do not yank 144 */ 145protected void 146c_delafter1(EditLine *el) 147{ |
145 char *cp; | 148 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 --- 8 unchanged lines hidden (view full) --- 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) { | 149 150 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) 151 *cp = cp[1]; 152 153 el->el_line.lastchar--; 154} 155 156 --- 8 unchanged lines hidden (view full) --- 165 num = (int)(el->el_line.cursor - el->el_line.buffer); 166 167 if (el->el_map.current != el->el_map.emacs) { 168 cv_undo(el); 169 cv_yank(el, el->el_line.cursor - num, num); 170 } 171 172 if (num > 0) { |
170 char *cp; | 173 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{ | 174 175 for (cp = el->el_line.cursor - num; 176 cp <= el->el_line.lastchar; 177 cp++) 178 *cp = cp[num]; 179 180 el->el_line.lastchar -= num; 181 } 182} 183 184 185/* c_delbefore1(): 186 * Delete the character before the cursor, do not yank 187 */ 188protected void 189c_delbefore1(EditLine *el) 190{ |
188 char *cp; | 191 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 | 192 193 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++) 194 *cp = cp[1]; 195 196 el->el_line.lastchar--; 197} 198 199 200/* ce__isword(): 201 * Return if p is part of a word according to emacs 202 */ 203protected int |
201ce__isword(int p) | 204ce__isword(Int p) |
202{ | 205{ |
203 return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL); | 206 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 */ 210protected int | 207} 208 209 210/* cv__isword(): 211 * Return if p is part of a word according to vi 212 */ 213protected int |
211cv__isword(int p) | 214cv__isword(Int p) |
212{ | 215{ |
213 if (isalnum(p) || p == '_') | 216 if (Isalnum(p) || p == '_') |
214 return 1; | 217 return 1; |
215 if (isgraph(p)) | 218 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 | 219 return 2; 220 return 0; 221} 222 223 224/* cv__isWord(): 225 * Return if p is part of a big word according to vi 226 */ 227protected int |
225cv__isWord(int p) | 228cv__isWord(Int p) |
226{ | 229{ |
227 return (!isspace(p)); | 230 return !Isspace(p); |
228} 229 230 231/* c__prev_word(): 232 * Find the previous word 233 */ | 231} 232 233 234/* c__prev_word(): 235 * Find the previous word 236 */ |
234protected char * 235c__prev_word(char *p, char *low, int n, int (*wtest)(int)) | 237protected Char * 238c__prev_word(Char *p, Char *low, int n, int (*wtest)(Int)) |
236{ 237 p--; 238 239 while (n--) { | 239{ 240 p--; 241 242 while (n--) { |
240 while ((p >= low) && !(*wtest)((unsigned char) *p)) | 243 while ((p >= low) && !(*wtest)(*p)) |
241 p--; | 244 p--; |
242 while ((p >= low) && (*wtest)((unsigned char) *p)) | 245 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 */ | 246 p--; 247 } 248 249 /* cp now points to one character before the word */ 250 p++; 251 if (p < low) 252 p = low; 253 /* cp now points where we want it */ |
251 return (p); | 254 return p; |
252} 253 254 255/* c__next_word(): 256 * Find the next word 257 */ | 255} 256 257 258/* c__next_word(): 259 * Find the next word 260 */ |
258protected char * 259c__next_word(char *p, char *high, int n, int (*wtest)(int)) | 261protected Char * 262c__next_word(Char *p, Char *high, int n, int (*wtest)(Int)) |
260{ 261 while (n--) { | 263{ 264 while (n--) { |
262 while ((p < high) && !(*wtest)((unsigned char) *p)) | 265 while ((p < high) && !(*wtest)(*p)) |
263 p++; | 266 p++; |
264 while ((p < high) && (*wtest)((unsigned char) *p)) | 267 while ((p < high) && (*wtest)(*p)) |
265 p++; 266 } 267 if (p > high) 268 p = high; 269 /* p now points where we want it */ | 268 p++; 269 } 270 if (p > high) 271 p = high; 272 /* p now points where we want it */ |
270 return (p); | 273 return p; |
271} 272 273/* cv_next_word(): 274 * Find the next word vi style 275 */ | 274} 275 276/* cv_next_word(): 277 * Find the next word vi style 278 */ |
276protected char * 277cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int)) | 279protected Char * 280cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(Int)) |
278{ 279 int test; 280 281 while (n--) { | 281{ 282 int test; 283 284 while (n--) { |
282 test = (*wtest)((unsigned char) *p); 283 while ((p < high) && (*wtest)((unsigned char) *p) == test) | 285 test = (*wtest)(*p); 286 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)) | 287 p++; 288 /* 289 * vi historically deletes with cw only the word preserving the 290 * trailing whitespace! This is not what 'w' does.. 291 */ 292 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT)) |
290 while ((p < high) && isspace((unsigned char) *p)) | 293 while ((p < high) && Isspace(*p)) |
291 p++; 292 } 293 294 /* p now points where we want it */ 295 if (p > high) | 294 p++; 295 } 296 297 /* p now points where we want it */ 298 if (p > high) |
296 return (high); | 299 return high; |
297 else | 300 else |
298 return (p); | 301 return p; |
299} 300 301 302/* cv_prev_word(): 303 * Find the previous word vi style 304 */ | 302} 303 304 305/* cv_prev_word(): 306 * Find the previous word vi style 307 */ |
305protected char * 306cv_prev_word(char *p, char *low, int n, int (*wtest)(int)) | 308protected Char * 309cv_prev_word(Char *p, Char *low, int n, int (*wtest)(Int)) |
307{ 308 int test; 309 310 p--; 311 while (n--) { | 310{ 311 int test; 312 313 p--; 314 while (n--) { |
312 while ((p > low) && isspace((unsigned char) *p)) | 315 while ((p > low) && Isspace(*p)) |
313 p--; | 316 p--; |
314 test = (*wtest)((unsigned char) *p); 315 while ((p >= low) && (*wtest)((unsigned char) *p) == test) | 317 test = (*wtest)(*p); 318 while ((p >= low) && (*wtest)(*p) == test) |
316 p--; 317 } 318 p++; 319 320 /* p now points where we want it */ 321 if (p < low) | 319 p--; 320 } 321 p++; 322 323 /* p now points where we want it */ 324 if (p < low) |
322 return (low); | 325 return low; |
323 else | 326 else |
324 return (p); | 327 return p; |
325} 326 327 | 328} 329 330 |
328#ifdef notdef 329/* c__number(): 330 * Ignore character p points to, return number appearing after that. 331 * A '$' by itself means a big number; "$-" is for negative; '^' means 1. 332 * Return p pointing to last char used. 333 */ 334protected char * 335c__number( 336 char *p, /* character position */ 337 int *num, /* Return value */ 338 int dval) /* dval is the number to subtract from like $-3 */ 339{ 340 int i; 341 int sign = 1; 342 343 if (*++p == '^') { 344 *num = 1; 345 return (p); 346 } 347 if (*p == '$') { 348 if (*++p != '-') { 349 *num = 0x7fffffff; /* Handle $ */ 350 return (--p); 351 } 352 sign = -1; /* Handle $- */ 353 ++p; 354 } 355 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0') 356 continue; 357 *num = (sign < 0 ? dval - i : i); 358 return (--p); 359} 360#endif 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; --- 22 unchanged lines hidden (view full) --- 392 c_delbefore(el, -size); 393 el->el_line.cursor += size; 394 } 395 } 396 el->el_chared.c_vcmd.action = NOP; 397} 398 399 | 331/* cv_delfini(): 332 * Finish vi delete action 333 */ 334protected void 335cv_delfini(EditLine *el) 336{ 337 int size; 338 int action = el->el_chared.c_vcmd.action; --- 22 unchanged lines hidden (view full) --- 361 c_delbefore(el, -size); 362 el->el_line.cursor += size; 363 } 364 } 365 el->el_chared.c_vcmd.action = NOP; 366} 367 368 |
400#ifdef notdef 401/* ce__endword(): 402 * Go to the end of this word according to emacs 403 */ 404protected char * 405ce__endword(char *p, char *high, int n) 406{ 407 p++; 408 409 while (n--) { 410 while ((p < high) && isspace((unsigned char) *p)) 411 p++; 412 while ((p < high) && !isspace((unsigned char) *p)) 413 p++; 414 } 415 416 p--; 417 return (p); 418} 419#endif 420 421 | |
422/* cv__endword(): 423 * Go to the end of this word according to vi 424 */ | 369/* cv__endword(): 370 * Go to the end of this word according to vi 371 */ |
425protected char * 426cv__endword(char *p, char *high, int n, int (*wtest)(int)) | 372protected Char * 373cv__endword(Char *p, Char *high, int n, int (*wtest)(Int)) |
427{ 428 int test; 429 430 p++; 431 432 while (n--) { | 374{ 375 int test; 376 377 p++; 378 379 while (n--) { |
433 while ((p < high) && isspace((unsigned char) *p)) | 380 while ((p < high) && Isspace(*p)) |
434 p++; 435 | 381 p++; 382 |
436 test = (*wtest)((unsigned char) *p); 437 while ((p < high) && (*wtest)((unsigned char) *p) == test) | 383 test = (*wtest)(*p); 384 while ((p < high) && (*wtest)(*p) == test) |
438 p++; 439 } 440 p--; | 385 p++; 386 } 387 p--; |
441 return (p); | 388 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 | 389} 390 391/* ch_init(): 392 * Initialize the character editor 393 */ 394protected int 395ch_init(EditLine *el) 396{ 397 c_macro_t *ma = &el->el_chared.c_macro; 398 |
452 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ); | 399 el->el_line.buffer = el_malloc(EL_BUFSIZ * 400 sizeof(*el->el_line.buffer)); |
453 if (el->el_line.buffer == NULL) | 401 if (el->el_line.buffer == NULL) |
454 return (-1); | 402 return -1; |
455 | 403 |
456 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ); | 404 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ * 405 sizeof(*el->el_line.buffer)); |
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 | 406 el->el_line.cursor = el->el_line.buffer; 407 el->el_line.lastchar = el->el_line.buffer; 408 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE]; 409 |
461 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ); | 410 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ * 411 sizeof(*el->el_chared.c_undo.buf)); |
462 if (el->el_chared.c_undo.buf == NULL) | 412 if (el->el_chared.c_undo.buf == NULL) |
463 return (-1); 464 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ); | 413 return -1; 414 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ * 415 sizeof(*el->el_chared.c_undo.buf)); |
465 el->el_chared.c_undo.len = -1; 466 el->el_chared.c_undo.cursor = 0; | 416 el->el_chared.c_undo.len = -1; 417 el->el_chared.c_undo.cursor = 0; |
467 el->el_chared.c_redo.buf = (char *) el_malloc(EL_BUFSIZ); | 418 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ * 419 sizeof(*el->el_chared.c_redo.buf)); |
468 if (el->el_chared.c_redo.buf == NULL) | 420 if (el->el_chared.c_redo.buf == NULL) |
469 return (-1); | 421 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 | 422 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf; 423 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ; 424 el->el_chared.c_redo.cmd = ED_UNASSIGNED; 425 426 el->el_chared.c_vcmd.action = NOP; 427 el->el_chared.c_vcmd.pos = el->el_line.buffer; 428 |
477 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ); | 429 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ * 430 sizeof(*el->el_chared.c_kill.buf)); |
478 if (el->el_chared.c_kill.buf == NULL) | 431 if (el->el_chared.c_kill.buf == NULL) |
479 return (-1); 480 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ); | 432 return -1; 433 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ * 434 sizeof(*el->el_chared.c_kill.buf)); |
481 el->el_chared.c_kill.mark = el->el_line.buffer; 482 el->el_chared.c_kill.last = el->el_chared.c_kill.buf; | 435 el->el_chared.c_kill.mark = el->el_line.buffer; 436 el->el_chared.c_kill.last = el->el_chared.c_kill.buf; |
437 el->el_chared.c_resizefun = NULL; 438 el->el_chared.c_resizearg = NULL; 439 el->el_chared.c_aliasfun = NULL; 440 el->el_chared.c_aliasarg = NULL; |
|
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; | 441 442 el->el_map.current = el->el_map.key; 443 444 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */ 445 el->el_state.doingarg = 0; 446 el->el_state.metanext = 0; 447 el->el_state.argument = 1; 448 el->el_state.lastcmd = ED_UNASSIGNED; 449 450 ma->level = -1; 451 ma->offset = 0; |
494 ma->macro = (char **) el_malloc(EL_MAXMACRO * sizeof(char *)); | 452 ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro)); |
495 if (ma->macro == NULL) | 453 if (ma->macro == NULL) |
496 return (-1); 497 return (0); | 454 return -1; 455 return 0; |
498} 499 500/* ch_reset(): 501 * Reset the character editor 502 */ 503protected void 504ch_reset(EditLine *el, int mclear) 505{ --- 22 unchanged lines hidden (view full) --- 528 ch__clearmacro(el); 529} 530 531private void 532ch__clearmacro(EditLine *el) 533{ 534 c_macro_t *ma = &el->el_chared.c_macro; 535 while (ma->level >= 0) | 456} 457 458/* ch_reset(): 459 * Reset the character editor 460 */ 461protected void 462ch_reset(EditLine *el, int mclear) 463{ --- 22 unchanged lines hidden (view full) --- 486 ch__clearmacro(el); 487} 488 489private void 490ch__clearmacro(EditLine *el) 491{ 492 c_macro_t *ma = &el->el_chared.c_macro; 493 while (ma->level >= 0) |
536 el_free((ptr_t)ma->macro[ma->level--]); | 494 el_free(ma->macro[ma->level--]); |
537} 538 539/* ch_enlargebufs(): 540 * Enlarge line buffer to be able to hold twice as much characters. 541 * Returns 1 if successful, 0 if not. 542 */ 543protected int 544ch_enlargebufs(EditLine *el, size_t addlen) 545{ 546 size_t sz, newsz; | 495} 496 497/* ch_enlargebufs(): 498 * Enlarge line buffer to be able to hold twice as much characters. 499 * Returns 1 if successful, 0 if not. 500 */ 501protected int 502ch_enlargebufs(EditLine *el, size_t addlen) 503{ 504 size_t sz, newsz; |
547 char *newbuffer, *oldbuf, *oldkbuf; | 505 Char *newbuffer, *oldbuf, *oldkbuf; |
548 | 506 |
549 sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE; | 507 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE); |
550 newsz = sz * 2; 551 /* 552 * If newly required length is longer than current buffer, we need 553 * to make the buffer big enough to hold both old and new stuff. 554 */ 555 if (addlen > sz) { 556 while(newsz - sz < addlen) 557 newsz *= 2; 558 } 559 560 /* 561 * Reallocate line buffer. 562 */ | 508 newsz = sz * 2; 509 /* 510 * If newly required length is longer than current buffer, we need 511 * to make the buffer big enough to hold both old and new stuff. 512 */ 513 if (addlen > sz) { 514 while(newsz - sz < addlen) 515 newsz *= 2; 516 } 517 518 /* 519 * Reallocate line buffer. 520 */ |
563 newbuffer = el_realloc(el->el_line.buffer, newsz); | 521 newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer)); |
564 if (!newbuffer) 565 return 0; 566 567 /* zero the newly added memory, leave old data in */ | 522 if (!newbuffer) 523 return 0; 524 525 /* zero the newly added memory, leave old data in */ |
568 (void) memset(&newbuffer[sz], 0, newsz - sz); | 526 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); |
569 570 oldbuf = el->el_line.buffer; 571 572 el->el_line.buffer = newbuffer; 573 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); 574 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); 575 /* don't set new size until all buffers are enlarged */ 576 el->el_line.limit = &newbuffer[sz - EL_LEAVE]; 577 578 /* 579 * Reallocate kill buffer. 580 */ | 527 528 oldbuf = el->el_line.buffer; 529 530 el->el_line.buffer = newbuffer; 531 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); 532 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); 533 /* don't set new size until all buffers are enlarged */ 534 el->el_line.limit = &newbuffer[sz - EL_LEAVE]; 535 536 /* 537 * Reallocate kill buffer. 538 */ |
581 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz); | 539 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz * 540 sizeof(*newbuffer)); |
582 if (!newbuffer) 583 return 0; 584 585 /* zero the newly added memory, leave old data in */ | 541 if (!newbuffer) 542 return 0; 543 544 /* zero the newly added memory, leave old data in */ |
586 (void) memset(&newbuffer[sz], 0, newsz - sz); | 545 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); |
587 588 oldkbuf = el->el_chared.c_kill.buf; 589 590 el->el_chared.c_kill.buf = newbuffer; 591 el->el_chared.c_kill.last = newbuffer + 592 (el->el_chared.c_kill.last - oldkbuf); 593 el->el_chared.c_kill.mark = el->el_line.buffer + 594 (el->el_chared.c_kill.mark - oldbuf); 595 596 /* 597 * Reallocate undo buffer. 598 */ | 546 547 oldkbuf = el->el_chared.c_kill.buf; 548 549 el->el_chared.c_kill.buf = newbuffer; 550 el->el_chared.c_kill.last = newbuffer + 551 (el->el_chared.c_kill.last - oldkbuf); 552 el->el_chared.c_kill.mark = el->el_line.buffer + 553 (el->el_chared.c_kill.mark - oldbuf); 554 555 /* 556 * Reallocate undo buffer. 557 */ |
599 newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz); | 558 newbuffer = el_realloc(el->el_chared.c_undo.buf, 559 newsz * sizeof(*newbuffer)); |
600 if (!newbuffer) 601 return 0; 602 603 /* zero the newly added memory, leave old data in */ | 560 if (!newbuffer) 561 return 0; 562 563 /* zero the newly added memory, leave old data in */ |
604 (void) memset(&newbuffer[sz], 0, newsz - sz); | 564 (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); |
605 el->el_chared.c_undo.buf = newbuffer; 606 | 565 el->el_chared.c_undo.buf = newbuffer; 566 |
607 newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz); | 567 newbuffer = el_realloc(el->el_chared.c_redo.buf, 568 newsz * sizeof(*newbuffer)); |
608 if (!newbuffer) 609 return 0; 610 el->el_chared.c_redo.pos = newbuffer + 611 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf); 612 el->el_chared.c_redo.lim = newbuffer + 613 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf); 614 el->el_chared.c_redo.buf = newbuffer; 615 616 if (!hist_enlargebuf(el, sz, newsz)) 617 return 0; 618 619 /* Safe to set enlarged buffer size */ 620 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE]; | 569 if (!newbuffer) 570 return 0; 571 el->el_chared.c_redo.pos = newbuffer + 572 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf); 573 el->el_chared.c_redo.lim = newbuffer + 574 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf); 575 el->el_chared.c_redo.buf = newbuffer; 576 577 if (!hist_enlargebuf(el, sz, newsz)) 578 return 0; 579 580 /* Safe to set enlarged buffer size */ 581 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE]; |
582 if (el->el_chared.c_resizefun) 583 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg); |
|
621 return 1; 622} 623 624/* ch_end(): 625 * Free the data structures used by the editor 626 */ 627protected void 628ch_end(EditLine *el) 629{ | 584 return 1; 585} 586 587/* ch_end(): 588 * Free the data structures used by the editor 589 */ 590protected void 591ch_end(EditLine *el) 592{ |
630 el_free((ptr_t) el->el_line.buffer); | 593 el_free(el->el_line.buffer); |
631 el->el_line.buffer = NULL; 632 el->el_line.limit = NULL; | 594 el->el_line.buffer = NULL; 595 el->el_line.limit = NULL; |
633 el_free((ptr_t) el->el_chared.c_undo.buf); | 596 el_free(el->el_chared.c_undo.buf); |
634 el->el_chared.c_undo.buf = NULL; | 597 el->el_chared.c_undo.buf = NULL; |
635 el_free((ptr_t) el->el_chared.c_redo.buf); | 598 el_free(el->el_chared.c_redo.buf); |
636 el->el_chared.c_redo.buf = NULL; 637 el->el_chared.c_redo.pos = NULL; 638 el->el_chared.c_redo.lim = NULL; 639 el->el_chared.c_redo.cmd = ED_UNASSIGNED; | 599 el->el_chared.c_redo.buf = NULL; 600 el->el_chared.c_redo.pos = NULL; 601 el->el_chared.c_redo.lim = NULL; 602 el->el_chared.c_redo.cmd = ED_UNASSIGNED; |
640 el_free((ptr_t) el->el_chared.c_kill.buf); | 603 el_free(el->el_chared.c_kill.buf); |
641 el->el_chared.c_kill.buf = NULL; 642 ch_reset(el, 1); | 604 el->el_chared.c_kill.buf = NULL; 605 ch_reset(el, 1); |
643 el_free((ptr_t) el->el_chared.c_macro.macro); | 606 el_free(el->el_chared.c_macro.macro); |
644 el->el_chared.c_macro.macro = NULL; 645} 646 647 648/* el_insertstr(): 649 * Insert string at cursorI 650 */ 651public int | 607 el->el_chared.c_macro.macro = NULL; 608} 609 610 611/* el_insertstr(): 612 * Insert string at cursorI 613 */ 614public int |
652el_insertstr(EditLine *el, const char *s) | 615FUN(el,insertstr)(EditLine *el, const Char *s) |
653{ 654 size_t len; 655 | 616{ 617 size_t len; 618 |
656 if ((len = strlen(s)) == 0) 657 return (-1); | 619 if (s == NULL || (len = Strlen(s)) == 0) 620 return -1; |
658 if (el->el_line.lastchar + len >= el->el_line.limit) { 659 if (!ch_enlargebufs(el, len)) | 621 if (el->el_line.lastchar + len >= el->el_line.limit) { 622 if (!ch_enlargebufs(el, len)) |
660 return (-1); | 623 return -1; |
661 } 662 663 c_insert(el, (int)len); 664 while (*s) 665 *el->el_line.cursor++ = *s++; | 624 } 625 626 c_insert(el, (int)len); 627 while (*s) 628 *el->el_line.cursor++ = *s++; |
666 return (0); | 629 return 0; |
667} 668 669 670/* el_deletestr(): 671 * Delete num characters before the cursor 672 */ 673public void 674el_deletestr(EditLine *el, int n) --- 5 unchanged lines hidden (view full) --- 680 return; 681 682 c_delbefore(el, n); /* delete before dot */ 683 el->el_line.cursor -= n; 684 if (el->el_line.cursor < el->el_line.buffer) 685 el->el_line.cursor = el->el_line.buffer; 686} 687 | 630} 631 632 633/* el_deletestr(): 634 * Delete num characters before the cursor 635 */ 636public void 637el_deletestr(EditLine *el, int n) --- 5 unchanged lines hidden (view full) --- 643 return; 644 645 c_delbefore(el, n); /* delete before dot */ 646 el->el_line.cursor -= n; 647 if (el->el_line.cursor < el->el_line.buffer) 648 el->el_line.cursor = el->el_line.buffer; 649} 650 |
651/* el_cursor(): 652 * Move the cursor to the left or the right of the current position 653 */ 654public int 655el_cursor(EditLine *el, int n) 656{ 657 if (n == 0) 658 goto out; 659 660 el->el_line.cursor += n; 661 662 if (el->el_line.cursor < el->el_line.buffer) 663 el->el_line.cursor = el->el_line.buffer; 664 if (el->el_line.cursor > el->el_line.lastchar) 665 el->el_line.cursor = el->el_line.lastchar; 666out: 667 return (int)(el->el_line.cursor - el->el_line.buffer); 668} 669 |
|
688/* c_gets(): 689 * Get a string 690 */ 691protected int | 670/* c_gets(): 671 * Get a string 672 */ 673protected int |
692c_gets(EditLine *el, char *buf, const char *prompt) | 674c_gets(EditLine *el, Char *buf, const Char *prompt) |
693{ | 675{ |
694 char ch; | 676 Char ch; |
695 ssize_t len; | 677 ssize_t len; |
696 char *cp = el->el_line.buffer; | 678 Char *cp = el->el_line.buffer; |
697 698 if (prompt) { | 679 680 if (prompt) { |
699 len = strlen(prompt); 700 memcpy(cp, prompt, (size_t)len); | 681 len = (ssize_t)Strlen(prompt); 682 (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp)); |
701 cp += len; 702 } 703 len = 0; 704 705 for (;;) { 706 el->el_line.cursor = cp; 707 *cp = ' '; 708 el->el_line.lastchar = cp + 1; 709 re_refresh(el); 710 | 683 cp += len; 684 } 685 len = 0; 686 687 for (;;) { 688 el->el_line.cursor = cp; 689 *cp = ' '; 690 el->el_line.lastchar = cp + 1; 691 re_refresh(el); 692 |
711 if (el_getc(el, &ch) != 1) { | 693 if (FUN(el,getc)(el, &ch) != 1) { |
712 ed_end_of_file(el, 0); 713 len = -1; 714 break; 715 } 716 717 switch (ch) { 718 | 694 ed_end_of_file(el, 0); 695 len = -1; 696 break; 697 } 698 699 switch (ch) { 700 |
719 case '\010': /* Delete and backspace */ 720 case '\177': | 701 case 0010: /* Delete and backspace */ 702 case 0177: |
721 if (len == 0) { 722 len = -1; 723 break; 724 } 725 cp--; 726 continue; 727 | 703 if (len == 0) { 704 len = -1; 705 break; 706 } 707 cp--; 708 continue; 709 |
728 case '\033': /* ESC */ | 710 case 0033: /* ESC */ |
729 case '\r': /* Newline */ 730 case '\n': 731 buf[len] = ch; 732 break; 733 734 default: | 711 case '\r': /* Newline */ 712 case '\n': 713 buf[len] = ch; 714 break; 715 716 default: |
735 if (len >= EL_BUFSIZ - 16) 736 term_beep(el); | 717 if (len >= (ssize_t)(EL_BUFSIZ - 16)) 718 terminal_beep(el); |
737 else { 738 buf[len++] = ch; 739 *cp++ = ch; 740 } 741 continue; 742 } 743 break; 744 } --- 6 unchanged lines hidden (view full) --- 751 752 753/* c_hpos(): 754 * Return the current horizontal position of the cursor 755 */ 756protected int 757c_hpos(EditLine *el) 758{ | 719 else { 720 buf[len++] = ch; 721 *cp++ = ch; 722 } 723 continue; 724 } 725 break; 726 } --- 6 unchanged lines hidden (view full) --- 733 734 735/* c_hpos(): 736 * Return the current horizontal position of the cursor 737 */ 738protected int 739c_hpos(EditLine *el) 740{ |
759 char *ptr; | 741 Char *ptr; |
760 761 /* 762 * Find how many characters till the beginning of this line. 763 */ 764 if (el->el_line.cursor == el->el_line.buffer) | 742 743 /* 744 * Find how many characters till the beginning of this line. 745 */ 746 if (el->el_line.cursor == el->el_line.buffer) |
765 return (0); | 747 return 0; |
766 else { 767 for (ptr = el->el_line.cursor - 1; 768 ptr >= el->el_line.buffer && *ptr != '\n'; 769 ptr--) 770 continue; 771 return (int)(el->el_line.cursor - ptr - 1); 772 } 773} | 748 else { 749 for (ptr = el->el_line.cursor - 1; 750 ptr >= el->el_line.buffer && *ptr != '\n'; 751 ptr--) 752 continue; 753 return (int)(el->el_line.cursor - ptr - 1); 754 } 755} |
756 757protected int 758ch_resizefun(EditLine *el, el_zfunc_t f, void *a) 759{ 760 el->el_chared.c_resizefun = f; 761 el->el_chared.c_resizearg = a; 762 return 0; 763} 764 765protected int 766ch_aliasfun(EditLine *el, el_afunc_t f, void *a) 767{ 768 el->el_chared.c_aliasfun = f; 769 el->el_chared.c_aliasarg = a; 770 return 0; 771} |
|