| 1 | /* |
| 2 | * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | getstr - get a string from the player |
| 10 | Function |
| 11 | Reads a string from the player, doing all the necessary formatting |
| 12 | and logging. |
| 13 | Notes |
| 14 | This routine flushes output before getting the string. The caller |
| 15 | should display any desired prompt prior to calling getstring(). Never |
| 16 | call os_gets() directly; use getstring() instead, since it logs the |
| 17 | string to the log file if one is open. |
| 18 | Modified |
| 19 | 09/05/92 MJRoberts - add buf length parameter to getstring |
| 20 | 04/07/91 JEras - log '>' on prompt; disable moremode before prompt |
| 21 | 03/10/91 MJRoberts - created (separated from vocab.c) |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "os.h" |
| 27 | #include "tio.h" |
| 28 | #include "cmap.h" |
| 29 | #include "run.h" |
| 30 | |
| 31 | /* |
| 32 | * Global variable with the current command logging file. If this is |
| 33 | * not null, we'll log each command that we read to this file. |
| 34 | */ |
| 35 | osfildef *cmdfile; |
| 36 | |
| 37 | /* |
| 38 | * External global with the current script input file. If this is |
| 39 | * non-null, we'll read commands from this file rather than from the |
| 40 | * keyboard. |
| 41 | */ |
| 42 | extern osfildef *scrfp; |
| 43 | |
| 44 | /* |
| 45 | * External global indicating script echo status. If we're reading from |
| 46 | * a script input file (i.e., scrfp is non-null), and this variable is |
| 47 | * true, it indicates that we're in "quiet" mode reading the script, so |
| 48 | * we will not echo commands that we read from the script file to the |
| 49 | * display. |
| 50 | */ |
| 51 | extern int scrquiet; |
| 52 | |
| 53 | /* |
| 54 | * getstring reads a string from the keyboard, doing all necessary |
| 55 | * output flushing. Prompting is to be done by the caller. This |
| 56 | * routine should be called instead of os_gets. |
| 57 | */ |
| 58 | int getstring(char *prompt, char *buf, int bufl) |
| 59 | { |
| 60 | char *result; |
| 61 | int savemoremode; |
| 62 | int retval; |
| 63 | |
| 64 | /* show prompt if one was given and flush output */ |
| 65 | savemoremode = setmore(0); |
| 66 | if (prompt != 0) |
| 67 | { |
| 68 | /* display the prompt text */ |
| 69 | outformat(prompt); |
| 70 | |
| 71 | /* make sure it shows up in the log file as well */ |
| 72 | out_logfile_print(prompt, FALSE); |
| 73 | } |
| 74 | outflushn(0); |
| 75 | outreset(); |
| 76 | |
| 77 | /* read from the command input file if we have one */ |
| 78 | if (scrfp != 0) |
| 79 | { |
| 80 | int quiet = scrquiet; |
| 81 | |
| 82 | /* try reading from command input file */ |
| 83 | if ((result = qasgets(buf, bufl)) == 0) |
| 84 | { |
| 85 | /* |
| 86 | * End of command input file; return to reading the |
| 87 | * keyboard. If we didn't already show the prompt, show it |
| 88 | * now. |
| 89 | * |
| 90 | * Note that qasgets() will have closed the script file |
| 91 | * before returning eof, so we won't directly read the |
| 92 | * command here but instead handle it later when we check to |
| 93 | * see if we need to read from the keyboard. |
| 94 | */ |
| 95 | if (quiet && prompt != 0) |
| 96 | outformat(prompt); |
| 97 | outflushn(0); |
| 98 | outreset(); |
| 99 | |
| 100 | /* |
| 101 | * Guarantee that moremode is turned back on. (moremode can |
| 102 | * be turned off for one of two reasons: we're printing the |
| 103 | * prompt, or we're reading from a script with no pauses. |
| 104 | * In either case, moremode should be turned back on at this |
| 105 | * point. -CDN) |
| 106 | */ |
| 107 | savemoremode = 1; |
| 108 | |
| 109 | /* turn off NONSTOP mode now that we're done with the script */ |
| 110 | os_nonstop_mode(FALSE); |
| 111 | } |
| 112 | |
| 113 | /* success */ |
| 114 | retval = 0; |
| 115 | } |
| 116 | |
| 117 | /* if we don't have a script file, read from the keyboard */ |
| 118 | if (scrfp == 0) |
| 119 | { |
| 120 | /* update the status line */ |
| 121 | runstat(); |
| 122 | |
| 123 | /* read a line from the keyboard */ |
| 124 | result = (char *)os_gets((uchar *)buf, bufl); |
| 125 | |
| 126 | /* |
| 127 | * if the result is null, we're at eof, so return a non-zero |
| 128 | * value; otherwise, we successfully read a command, so return |
| 129 | * zero |
| 130 | */ |
| 131 | retval = (result == 0); |
| 132 | } |
| 133 | |
| 134 | /* restore the original "more" mode */ |
| 135 | setmore(savemoremode); |
| 136 | |
| 137 | /* check the result */ |
| 138 | if (retval != 0) |
| 139 | { |
| 140 | /* we got an error reading the command - return the error */ |
| 141 | return retval; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | char *p; |
| 146 | |
| 147 | /* |
| 148 | * we got a command, or at least a partial command (if we timed |
| 149 | * out, we may still have a partial line in the buffer) - write |
| 150 | * the input line to the log and/or command files, as |
| 151 | * appropriate |
| 152 | */ |
| 153 | out_logfile_print(buf, TRUE); |
| 154 | if (cmdfile != 0) |
| 155 | { |
| 156 | os_fprintz(cmdfile, ">"); |
| 157 | os_fprintz(cmdfile, buf); |
| 158 | os_fprintz(cmdfile, "\n"); |
| 159 | } |
| 160 | |
| 161 | /* translate the input to the internal character set */ |
| 162 | for (p = buf ; *p != '\0' ; ++p) |
| 163 | *p = cmap_n2i(*p); |
| 164 | |
| 165 | /* success */ |
| 166 | return retval; |
| 167 | } |
| 168 | } |
| 169 |