summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-09-20 21:36:48 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-09-20 21:36:48 -0700
commit7c3fd4898b04c0e7be5e67f781dd123bc1f61237 (patch)
tree4491fe30aadb3d65f5ebc98877ebba9dc2a67624
parent0eed0b1cbcf301c594020fb4bbe9a39c3f94a8a6 (diff)
downloadtxr-7c3fd4898b04c0e7be5e67f781dd123bc1f61237.tar.gz
txr-7c3fd4898b04c0e7be5e67f781dd123bc1f61237.tar.bz2
txr-7c3fd4898b04c0e7be5e67f781dd123bc1f61237.zip
linenoise: single return out of edit.
* linenoise/linenoise.c (edit): Changing most returns to goto out. We have common clean-up to do, namely removing the last history item which represents the current line. This fixes a bug: not removing the history line in the Ctrl-C case.
-rw-r--r--linenoise/linenoise.c54
1 files changed, 27 insertions, 27 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index f62a97d8..330f1cad 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -1359,6 +1359,7 @@ static void edit_in_editor(lino_t *l) {
static int edit(lino_t *l, const char *prompt)
{
int verbatim = 0, extended = 0, paste = 0, extend_num = -1;
+ int ret = -1;
/* Populate the linenoise state that we pass to functions implementing
* specific editing functionalities. */
@@ -1380,7 +1381,7 @@ static int edit(lino_t *l, const char *prompt)
if (write(l->ofd,prompt,l->plen) == -1) {
l->error = lino_ioerr;
- return -1;
+ return ret;
}
while(1) {
unsigned char byte;
@@ -1402,8 +1403,10 @@ static int edit(lino_t *l, const char *prompt)
continue;
}
- if (nread <= 0)
- return l->len ? (int) l->len : -1;
+ if (nread <= 0) {
+ ret = l->len ? (int) l->len : -1;
+ goto out;
+ }
c = byte;
@@ -1417,7 +1420,7 @@ static int edit(lino_t *l, const char *prompt)
{
if (edit_insert(l,c)) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
verbatim = 0;
continue;
@@ -1458,7 +1461,7 @@ static int edit(lino_t *l, const char *prompt)
if (edit_insert_str(l, word_start, word_end - word_start)) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
}
break;
@@ -1480,7 +1483,7 @@ static int edit(lino_t *l, const char *prompt)
if (res) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
}
break;
@@ -1533,7 +1536,7 @@ static int edit(lino_t *l, const char *prompt)
}
if (c < 0)
- return l->len;
+ goto out;
if (c == 0)
continue;
@@ -1542,29 +1545,26 @@ static int edit(lino_t *l, const char *prompt)
if (paste) {
if (edit_insert(l,c)) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
break;
}
- if (l->history_len > 0) {
- l->history_len--;
- free(l->history[l->history_len]);
- l->history[l->history_len] = 0;
- }
- if (l->mlmode) edit_move_end(l);
+ if (l->mlmode)
+ edit_move_end(l);
if (l->need_refresh)
refresh_line(l);
record_undo(l);
- return (int)l->len;
+ ret = l->len;
+ goto out;
case CTL('C'):
if (l->mlmode) {
edit_move_end(l);
if (l->need_refresh)
refresh_line(l);
}
- record_undo(l);
l->error = lino_intr;
- return -1;
+ record_undo(l);
+ goto out;
case BACKSPACE: /* backspace */
case CTL('H'):
edit_backspace(l);
@@ -1575,13 +1575,8 @@ static int edit(lino_t *l, const char *prompt)
yank_sel(l);
edit_delete(l);
} else {
- if (l->history_len > 0) {
- l->history_len--;
- free(l->history[l->history_len]);
- l->history[l->history_len] = 0;
- }
l->error = lino_eof;
- return -1;
+ goto out;
}
break;
case CTL('T'): /* swaps current character with previous. */
@@ -1665,7 +1660,7 @@ static int edit(lino_t *l, const char *prompt)
case ')': case ']': case '}':
if (edit_insert(l,c)) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
paren_jump(l);
break;
@@ -1674,7 +1669,7 @@ static int edit(lino_t *l, const char *prompt)
break;
if (edit_insert(l,c)) {
l->error = lino_ioerr;
- return -1;
+ goto out;
}
break;
case CTL('U'):
@@ -1750,8 +1745,13 @@ static int edit(lino_t *l, const char *prompt)
break;
}
}
- record_undo(l);
- return l->len;
+out:
+ if (l->history_len > 0) {
+ l->history_len--;
+ free(l->history[l->history_len]);
+ l->history[l->history_len] = 0;
+ }
+ return ret;
}
#ifdef SIGWINCH