command queue handling in getobj, yn_function

Mostly attempting to clean up potential error handling but I don't
have any error cases to test with.  Doesn't seem to break anything
when there aren't any errors....
This commit is contained in:
PatR
2022-04-19 13:53:40 -07:00
parent 2356690054
commit 5489b3ebd9
2 changed files with 51 additions and 35 deletions

View File

@@ -5464,7 +5464,7 @@ char
yn_function(const char *query, const char *resp, char def) yn_function(const char *query, const char *resp, char def)
{ {
char res = '\033', qbuf[QBUFSZ]; char res = '\033', qbuf[QBUFSZ];
struct _cmd_queue *cmdq = cmdq_pop(); struct _cmd_queue cq, *cmdq;
#ifdef DUMPLOG #ifdef DUMPLOG
unsigned idx = g.saved_pline_index; unsigned idx = g.saved_pline_index;
/* buffer to hold query+space+formatted_single_char_response */ /* buffer to hold query+space+formatted_single_char_response */
@@ -5481,15 +5481,24 @@ yn_function(const char *query, const char *resp, char def)
Strcpy(&qbuf[QBUFSZ - 1 - 3], "..."); Strcpy(&qbuf[QBUFSZ - 1 - 3], "...");
query = qbuf; query = qbuf;
} }
if (cmdq) {
if (cmdq->typ == CMDQ_KEY) if ((cmdq = cmdq_pop()) != 0) {
res = cmdq->key; cq = *cmdq;
free(cmdq);
} else {
cq.typ = CMDQ_USER_INPUT;
cq.key = '\0'; /* lint suppression */
}
if (cq.typ != CMDQ_USER_INPUT) {
if (cq.typ == CMDQ_KEY)
res = cq.key;
else else
cmdq_clear(); /* 'res' is ESC */ cmdq_clear(); /* 'res' is ESC */
} else { } else {
res = (*windowprocs.win_yn_function)(query, resp, def); res = (*windowprocs.win_yn_function)(query, resp, def);
} }
free(cmdq);
#ifdef DUMPLOG #ifdef DUMPLOG
if (idx == g.saved_pline_index) { if (idx == g.saved_pline_index) {
/* when idx is still the same as g.saved_pline_index, the interface /* when idx is still the same as g.saved_pline_index, the interface

View File

@@ -1489,9 +1489,10 @@ any_obj_ok(struct obj *obj)
* case. * case.
*/ */
struct obj * struct obj *
getobj(const char *word, getobj(
int (*obj_ok)(OBJ_P), /* callback */ const char *word, /* usually a direct verb such as "drop" */
unsigned int ctrlflags) int (*obj_ok)(OBJ_P), /* callback to classify an object's suitability */
unsigned int ctrlflags) /* some control to fine-tune the behavior */
{ {
register struct obj *otmp; register struct obj *otmp;
register char ilet = 0; register char ilet = 0;
@@ -1504,41 +1505,47 @@ getobj(const char *word,
allownone = FALSE; allownone = FALSE;
int inaccess = 0; /* counts GETOBJ_EXCLUDE_INACCESS items to decide int inaccess = 0; /* counts GETOBJ_EXCLUDE_INACCESS items to decide
* between "you don't have anything to <foo>" * between "you don't have anything to <foo>"
* versus "you don't have anything _else_ to <foo>" */ * versus "you don't have anything _else_ to <foo>"
* (also used for GETOBJ_EXCLUDE_NONINVENT) */
long cnt; long cnt;
boolean cntgiven = FALSE; boolean cntgiven = FALSE;
boolean msggiven = FALSE; boolean msggiven = FALSE;
boolean oneloop = FALSE; boolean oneloop = FALSE;
Loot *sortedinvent, *srtinv; Loot *sortedinvent, *srtinv;
struct _cmd_queue cq, *cmdq;
struct _cmd_queue *cmdq = cmdq_pop(); if ((cmdq = cmdq_pop()) != 0) {
cq = *cmdq;
free(cmdq);
/* user-input means pick something interactively now, with more
in the command queue for after that; if not user-input, it
has to be a key here */
if (cq.typ != CMDQ_USER_INPUT) {
otmp = 0; /* in case of non-key or lookup failure */
if (cq.typ == CMDQ_KEY) {
int v;
if (cmdq && cmdq->typ != CMDQ_USER_INPUT) { if (cq.key == '-') {
int v; /* check whether the hands/self choice is suitable */
v = (*obj_ok)((struct obj *) 0);
/* it's not a key, abort */ if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY)
if (cmdq->typ != CMDQ_KEY) { otmp = (struct obj *) &cg.zeroobj;
free(cmdq); } else {
return (struct obj *) 0; /* there could be more than one match if key is '#';
} take first one which passes the obj_ok callback */
for (otmp = g.invent; otmp; otmp = otmp->nobj) for (otmp = g.invent; otmp; otmp = otmp->nobj)
if (otmp->invlet == cmdq->key) { if (otmp->invlet == cq.key) {
v = (*obj_ok)(otmp); v = (*obj_ok)(otmp);
if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY) if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY)
break; break;
}
}
} }
if (!otmp) { if (!otmp) /* didn't find what we were looking for, */
v = (*obj_ok)((struct obj *) 0); cmdq_clear(); /* so discard any other queued commands */
if (v == GETOBJ_SUGGEST || v == GETOBJ_DOWNPLAY) return otmp;
otmp = (struct obj *) &cg.zeroobj; /* cast away const */ } /* !CMDQ_USER_INPUT */
} } /* cmdq */
free(cmdq);
if (!otmp)
cmdq_clear();
return otmp;
}
if (cmdq)
free(cmdq);
/* is "hands"/"self" a valid thing to do this action on? */ /* is "hands"/"self" a valid thing to do this action on? */
switch ((*obj_ok)((struct obj *) 0)) { switch ((*obj_ok)((struct obj *) 0)) {