for paranoid_confirm:quit,die,attack,&c prompting; allows yes, n|no, or q|quit; result is one of 'y' or 'n' or 'q'; ESC yields 'q' */
| 5585 | /* for paranoid_confirm:quit,die,attack,&c prompting; allows yes, n|no, |
| 5586 | or q|quit; result is one of 'y' or 'n' or 'q'; ESC yields 'q' */ |
| 5587 | char |
| 5588 | paranoid_ynq( |
| 5589 | boolean be_paranoid, |
| 5590 | const char *prompt, |
| 5591 | boolean accept_q) |
| 5592 | { |
| 5593 | char c = 'n'; /* default result */ |
| 5594 | |
| 5595 | /* when paranoid, player must respond with "yes" rather than just 'y' |
| 5596 | to give the go-ahead for this query; default is "no" unless the |
| 5597 | ParanoidConfirm flag is set in which case there's no default */ |
| 5598 | if (be_paranoid) { |
| 5599 | char pbuf[BUFSZ], qbuf[QBUFSZ], ans[BUFSZ]; |
| 5600 | const char *promptprefix = "", /* empty for first iteration */ |
| 5601 | *responsetype = ParanoidConfirm ? (accept_q ? "[yes|no|quit]" |
| 5602 | : "[yes|no]") |
| 5603 | /* default of 'n' is shown for |
| 5604 | * the !ParanoidConfirm cases */ |
| 5605 | : (accept_q ? "[yes|n|q] (n)" |
| 5606 | : "[yes|n] (n)"); |
| 5607 | int k, trylimit = 6; /* 1 normal, 5 more with "Yes or No:" prefix */ |
| 5608 | |
| 5609 | copynchars(pbuf, prompt, BUFSZ - 1); |
| 5610 | /* in addition to being paranoid about this particular |
| 5611 | query, we might be even more paranoid about all paranoia |
| 5612 | responses (ie, ParanoidConfirm is set) in which case we |
| 5613 | require "no" to reject in addition to "yes" to confirm |
| 5614 | (except we won't loop if response is ESC; it means no) */ |
| 5615 | do { |
| 5616 | /* make sure we won't overflow a QBUFSZ sized buffer */ |
| 5617 | k = (int) (strlen(promptprefix) + 1 + strlen(responsetype)); |
| 5618 | if ((int) strlen(pbuf) + k > QBUFSZ - 1) { |
| 5619 | /* chop off some at the end */ |
| 5620 | Strcpy(pbuf + (QBUFSZ - 1) - k - 4, "...?"); /* -4: "...?" */ |
| 5621 | } |
| 5622 | |
| 5623 | Snprintf(qbuf, sizeof qbuf, "%s%s %s", promptprefix, pbuf, |
| 5624 | responsetype); |
| 5625 | *ans = '\0'; |
| 5626 | getlin(qbuf, ans); |
| 5627 | (void) mungspaces(ans); |
| 5628 | if (!strcmpi(ans, "yes")) { |
| 5629 | c = 'y'; |
| 5630 | break; |
| 5631 | } |
| 5632 | if (!strcmpi(ans, "quit") || *ans == '\033') { |
| 5633 | c = 'q'; |
| 5634 | break; |
| 5635 | } |
| 5636 | /* we don't bother adding "or \"Quit\"" for the accept_q case */ |
| 5637 | promptprefix = "\"Yes\" or \"No\": "; |
| 5638 | /* for empty input, return value c will already be 'n' */ |
| 5639 | } while (ParanoidConfirm && strcmpi(ans, "no") && --trylimit); |
| 5640 | } else if (accept_q) { |
| 5641 | /* 'y', 'n', or 'q' */ |
| 5642 | c = yn_function(prompt, ynqchars, 'n', FALSE); |
| 5643 | } else { |
| 5644 | /* 'y' or 'n' */ |
no test coverage detected