Aaand bingo.
Code: Select all
char *lua_input_box(cptr title, int max)
{
static char buf[80];
int wid, hgt;
strcpy(buf, "");
Term_get_size(&wid, &hgt);
if (!input_box(title, hgt / 2, wid / 2, buf, (max > 79) ? 79 : max))
return "";
return buf;
}
Code: Select all
/*
* Creates an input box
*/
bool_ input_box(cptr text, int y, int x, char *buf, int max)
{
int smax = strlen(text);
if (max > smax) smax = max;
smax++;
draw_box(y - 1, x - (smax / 2), 3, smax);
c_put_str(TERM_WHITE, text, y, x - (strlen(text) / 2));
Term_gotoxy(x - (smax / 2) + 1, y + 1);
return askfor_aux(buf, max);
}
Code: Select all
/*
* Get some input at the cursor location.
* Assume the buffer is initialized to a default string.
* Note that this string is often "empty" (see below).
* The default buffer is displayed in yellow until cleared.
* Pressing RETURN right away accepts the default entry.
* Normal chars clear the default and append the char.
* Backspace clears the default or deletes the final char.
* ESCAPE clears the buffer and the window and returns FALSE.
* RETURN accepts the current buffer contents and returns TRUE.
*/
bool_ askfor_aux_complete = FALSE;
bool_ askfor_aux(char *buf, int len)
{
int y, x;
int i = 0;
int k = 0;
int wid, hgt;
bool_ done = FALSE;
/* Locate the cursor */
Term_locate(&x, &y);
/* Get terminal size */
Term_get_size(&wid, &hgt);
/* Paranoia -- check column */
if ((x < 0) || (x >= wid)) x = 0;
/* Restrict the length */
if (x + len > wid) len = wid - x;
/* Paranoia -- Clip the default entry */
buf[len] = '\0';
/* Display the default answer */
Term_erase(x, y, len);
Term_putstr(x, y, -1, TERM_YELLOW, buf);
if (askfor_aux_complete)
{
screen_save();
complete_where = 0;
strncpy(complete_buf, buf, 100);
}
/* Process input */
while (!done)
{
/* Place cursor */
Term_gotoxy(x + k, y);
/* Get a key */
i = inkey();
/* Analyze the key */
switch (i)
{
case ESCAPE:
k = 0;
done = TRUE;
break;
case '\n':
case '\r':
k = strlen(buf);
done = TRUE;
break;
case '\t':
if (askfor_aux_complete && k)
{
screen_load();
screen_save();
k = complete_command(buf, k, len);
}
else
{
bell();
}
case 0x7F:
case '\010':
if (k > 0) k--;
strncpy(complete_buf, buf, k);
break;
default:
if ((k < len) && (isprint(i)))
{
buf[k++] = i;
strncpy(complete_buf, buf, k);
}
else
{
bell();
}
break;
}
/* Terminate */
buf[k] = '\0';
/* Update the entry */
Term_erase(x, y, len);
Term_putstr(x, y, -1, TERM_WHITE, buf);
}
if (askfor_aux_complete)
{
screen_load();
}
/* Aborted */
if (i == ESCAPE) return (FALSE);
/* Success */
return (TRUE);
}
Esc makes askfor_aux() return FALSE.
That makes input_box() fail.
And that makes lua_input_box() return an empty string, which causes the chain of events leading to the crash.
Hurray.