ch_enlargebufs(): * Enlarge line buffer to be able to hold twice as much characters. * Returns 1 if successful, 0 if not. */
| 497 | * Returns 1 if successful, 0 if not. |
| 498 | */ |
| 499 | protected int |
| 500 | ch_enlargebufs(EditLine *el, size_t addlen) |
| 501 | { |
| 502 | size_t sz, newsz; |
| 503 | Char *newbuffer, *oldbuf, *oldkbuf; |
| 504 | |
| 505 | sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE); |
| 506 | newsz = sz * 2; |
| 507 | /* |
| 508 | * If newly required length is longer than current buffer, we need |
| 509 | * to make the buffer big enough to hold both old and new stuff. |
| 510 | */ |
| 511 | if (addlen > sz) { |
| 512 | while(newsz - sz < addlen) |
| 513 | newsz *= 2; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Reallocate line buffer. |
| 518 | */ |
| 519 | newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer)); |
| 520 | if (!newbuffer) |
| 521 | return 0; |
| 522 | |
| 523 | /* zero the newly added memory, leave old data in */ |
| 524 | (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); |
| 525 | |
| 526 | oldbuf = el->el_line.buffer; |
| 527 | |
| 528 | el->el_line.buffer = newbuffer; |
| 529 | el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); |
| 530 | el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); |
| 531 | /* don't set new size until all buffers are enlarged */ |
| 532 | el->el_line.limit = &newbuffer[sz - EL_LEAVE]; |
| 533 | |
| 534 | /* |
| 535 | * Reallocate kill buffer. |
| 536 | */ |
| 537 | newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz * |
| 538 | sizeof(*newbuffer)); |
| 539 | if (!newbuffer) |
| 540 | return 0; |
| 541 | |
| 542 | /* zero the newly added memory, leave old data in */ |
| 543 | (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); |
| 544 | |
| 545 | oldkbuf = el->el_chared.c_kill.buf; |
| 546 | |
| 547 | el->el_chared.c_kill.buf = newbuffer; |
| 548 | el->el_chared.c_kill.last = newbuffer + |
| 549 | (el->el_chared.c_kill.last - oldkbuf); |
| 550 | el->el_chared.c_kill.mark = el->el_line.buffer + |
| 551 | (el->el_chared.c_kill.mark - oldbuf); |
| 552 | |
| 553 | /* |
| 554 | * Reallocate undo buffer. |
| 555 | */ |
| 556 | newbuffer = el_realloc(el->el_chared.c_undo.buf, |
no test coverage detected