get_count() cleanup

Fix several warnings.  Accept ASCII RUBOUT (aka DELETE) in addition
to backspace.  [Should use erase_char (and add support for kill_char)
but that means pushing get_count() into the interface code.]  Guard
against user causing the count to wrap if someone ever adds a call to
get_count() which doesn't specifying a maximum value.
This commit is contained in:
PatR
2016-01-06 15:37:46 -08:00
parent 5d1281c9ac
commit c8cd550a5a
2 changed files with 29 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
/* NetHack 3.6 extern.h $NHDT-Date: 1451955077 2016/01/05 00:51:17 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.530 $ */
/* NetHack 3.6 extern.h $NHDT-Date: 1452123455 2016/01/06 23:37:35 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.535 $ */
/* Copyright (c) Steve Creps, 1988. */
/* NetHack may be freely redistributed. See license for details. */
@@ -204,7 +204,7 @@ E int FDECL(isok, (int, int));
E int FDECL(get_adjacent_loc,
(const char *, const char *, XCHAR_P, XCHAR_P, coord *));
E const char *FDECL(click_to_cmd, (int, int, int));
E char FDECL(get_count, (char *, char, long, long *));
E char FDECL(get_count, (char *, CHAR_P, long, long *));
#ifdef HANGUPHANDLING
E void FDECL(hangup, (int));
E void NDECL(end_of_input);
@@ -1501,9 +1501,9 @@ E boolean FDECL(lined_up, (struct monst *));
E struct obj *FDECL(m_carrying, (struct monst *, int));
E void FDECL(m_useupall, (struct monst *, struct obj *));
E void FDECL(m_useup, (struct monst *, struct obj *));
E void FDECL(m_throw,
(struct monst *, int, int, int, int, int, struct obj *));
E void FDECL(hit_bars, (struct obj **, int, int, int, int, boolean, boolean));
E void FDECL(m_throw, (struct monst *, int, int, int, int, int, struct obj *));
E void FDECL(hit_bars, (struct obj **, int, int, int, int,
BOOLEAN_P, BOOLEAN_P));
E boolean FDECL(hits_bars, (struct obj **, int, int, int, int, int, int));
/* ### muse.c ### */

View File

@@ -1,4 +1,4 @@
/* NetHack 3.6 cmd.c $NHDT-Date: 1451082253 2015/12/25 22:24:13 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.212 $ */
/* NetHack 3.6 cmd.c $NHDT-Date: 1452123457 2016/01/06 23:37:37 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.216 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
@@ -3865,53 +3865,56 @@ int x, y, mod;
}
char
get_count(allowchars, inkey, max, count)
get_count(allowchars, inkey, maxcount, count)
char *allowchars;
char inkey;
long max;
long maxcount;
long *count;
{
char qbuf[QBUFSZ];
int key;
long cnt = 0;
long cnt = 0L;
boolean backspaced = FALSE;
char *ret;
/* this should be done in port code so that we have erase_char
and kill_char available; we can at least fake erase_char */
#define STANDBY_erase_char '\177'
for (;;) {
if (inkey) {
key = inkey;
inkey = '\0';
} else
key = readchar();
if (digit(key)) {
cnt = 10L * cnt + (long)(key - '0');
} else if (key == '\b') {
cnt = 10L * cnt + (long) (key - '0');
if (cnt < 0)
cnt = 0;
else if (maxcount > 0 && cnt > maxcount)
cnt = maxcount;
} else if (key == '\b' || key == STANDBY_erase_char) {
cnt = cnt / 10;
backspaced = TRUE;
} else if (key == '\033') {
return '\033';
} else if (allowchars) {
if (ret = index(allowchars, key)) {
*count = cnt;
return *ret;
}
} else {
break;
} else if (!allowchars || index(allowchars, key)) {
*count = cnt;
return key;
break;
}
if (max && (cnt > max))
cnt = max;
if (cnt > 9 || backspaced) {
clear_nhwindow(WIN_MESSAGE);
if (backspaced && !cnt)
if (backspaced && !cnt) {
Sprintf(qbuf, "Count: ");
else {
Sprintf(qbuf, "Count: %d", cnt);
} else {
Sprintf(qbuf, "Count: %ld", cnt);
backspaced = FALSE;
}
pline1(qbuf);
mark_synch();
}
}
return key;
}
@@ -3925,7 +3928,6 @@ parse()
#endif
register int foo;
boolean prezero = FALSE;
boolean backspaced = FALSE;
multi = 0;
context.move = 1;
@@ -3936,6 +3938,7 @@ parse()
#endif
if (!Cmd.num_pad || (foo = readchar()) == 'n') {
long tmpmulti = multi;
foo = get_count(NULL, '\0', LARGEST_INT, &tmpmulti);
last_multi = multi = tmpmulti;
}