Rework readmore(), replace goto with a switch

This commit is contained in:
lostd 2015-01-28 12:30:59 +02:00
parent ab0c9e5921
commit 3c45733f47

27
noice.c
View file

@ -367,26 +367,26 @@ readln(void)
int int
readmore(char **str) readmore(char **str)
{ {
int c; int c, ret = 0;
int i; size_t i;
char *ln = *str; char *ln = *str;
int ret = 0;
if (ln != NULL) if (ln != NULL)
i = strlen(ln); i = strlen(ln);
else else
i = 0; i = 0;
DPRINTF_D(i); DPRINTF_D(i);
curs_set(TRUE); curs_set(TRUE);
c = getch(); c = getch();
if (c == KEY_ENTER || c == '\r') { switch (c) {
case KEY_ENTER:
case '\r':
ret = 1; ret = 1;
goto out; break;
} case KEY_BACKSPACE:
if (c == KEY_BACKSPACE || c == CONTROL('H')) { case CONTROL('H'):
i--; i--;
if (i > 0) { if (i > 0) {
ln = xrealloc(ln, (i + 1) * sizeof(*ln)); ln = xrealloc(ln, (i + 1) * sizeof(*ln));
@ -395,13 +395,14 @@ readmore(char **str)
free(ln); free(ln);
ln = NULL; ln = NULL;
} }
goto out; break;
} default:
ln = xrealloc(ln, (i + 2) * sizeof(*ln));
ln[i] = c;
i++; i++;
ln = xrealloc(ln, (i + 1) * sizeof(*ln));
ln[i - 1] = c;
ln[i] = '\0'; ln[i] = '\0';
out: }
curs_set(FALSE); curs_set(FALSE);
*str = ln; *str = ln;