1 /* NetHack 3.6 getline.c $NHDT-Date: 1543830347 2018/12/03 09:45:47 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.37 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Michael Allison, 2006. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* JNetHack Copyright */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2023 */
8 /* JNetHack may be freely redistributed. See license for details. */
21 char morc = 0; /* tell the outside world what char you chose */
22 STATIC_VAR boolean suppress_history;
23 STATIC_DCL boolean FDECL(ext_cmd_getlin_hook, (char *));
25 typedef boolean FDECL((*getlin_hook_proc), (char *));
27 STATIC_DCL void FDECL(hooked_tty_getlin,
28 (const char *, char *, getlin_hook_proc));
29 extern int NDECL(extcmd_via_menu); /* cmd.c */
31 extern char erase_char, kill_char; /* from appropriate tty.c file */
34 * Read a line closed with '\n' into the array char bufp[BUFSZ].
35 * (The '\n' is not stored. The string is closed with a '\0'.)
36 * Reading can be interrupted by an escape ('\033') - now the
37 * resulting string is "\033".
40 tty_getlin(query, bufp)
44 suppress_history = FALSE;
45 hooked_tty_getlin(query, bufp, (getlin_hook_proc) 0);
50 hooked_tty_getlin(query, bufp, hook)
52 hooked_tty_getlin(query, bfp, hook)
58 getlin_hook_proc hook;
64 register char *obufp = bufp;
69 struct WinDesc *cw = wins[WIN_MESSAGE];
75 if (ttyDisplay->toplin == 1 && !(cw->flags & WIN_STOP))
77 cw->flags &= ~WIN_STOP;
78 ttyDisplay->toplin = 3; /* special prompt state */
81 /* issue the prompt */
82 custompline(OVERRIDE_MSGTYPE | SUPPRESS_HISTORY, "%s ", query);
84 /* bufp is input/output; treat current contents (presumed to be from
85 previous getlin()) as default input */
89 /* !EDIT_GETLIN: bufp is output only; init it to empty */
94 (void) fflush(stdout);
95 Strcat(strcat(strcpy(toplines, query), " "), obufp);
98 uc = (*((unsigned int *)&c));
101 if (c == '\033' || c == EOF) {
102 if (c == '\033' && obufp[0] != '\0') {
105 tty_clear_nhwindow(WIN_MESSAGE);
106 cw->maxcol = cw->maxrow;
116 if (ttyDisplay->intr) {
120 if (c == '\020') { /* ctrl-P */
121 if (iflags.prevmsg_window != 's') {
122 int sav = ttyDisplay->inread;
124 ttyDisplay->inread = 0;
125 (void) tty_doprev_message();
126 ttyDisplay->inread = sav;
127 tty_clear_nhwindow(WIN_MESSAGE);
128 cw->maxcol = cw->maxrow;
135 (void) tty_doprev_message(); /* need two initially */
136 (void) tty_doprev_message();
140 } else if (doprev && iflags.prevmsg_window == 's') {
141 tty_clear_nhwindow(WIN_MESSAGE);
142 cw->maxcol = cw->maxrow;
149 if (c == erase_char || c == '\b') {
157 #endif /* NEWAUTOCOMP */
160 putsyms("\b \b"); /* putsym converts \b */
161 #else /* NEWAUTOCOMP */
163 for (i = bufp; *i; ++i)
165 for (; i > bufp; --i)
168 #endif /* NEWAUTOCOMP */
174 n = offset_in_kanji((unsigned char *)tmp, bufp - tmp);
176 /*
\8cã
\82Å1
\83o
\83C
\83g
\88ø
\82©
\82ê
\82é
\82Ì
\82Å
\82»
\82Ì
\95ª
\82Í
\82±
\82±
\82Å
\82Í
\88ø
\82©
\82È
\82¢ */
177 bufp = bufp - (n - 1);
182 } else if (c == '\n' || c == '\r') {
185 #endif /* not NEWAUTOCOMP */
188 } else if (' ' <= (unsigned char) c && c != '\177'
190 } else if (' ' <= uc && uc < 255
192 /* avoid isprint() - some people don't have it
193 ' ' is not always a printing char */
194 && (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)) {
198 #endif /* NEWAUTOCOMP */
207 if (hook && (*hook)(obufp)) {
215 #else /* NEWAUTOCOMP */
216 /* pointer and cursor left where they were */
217 for (i = bufp; *i; ++i)
219 } else if (i > bufp) {
222 /* erase rest of prior guess */
223 for (; i > bufp; --i)
225 for (; s > bufp; --s)
227 #endif /* NEWAUTOCOMP */
229 } else if (c == kill_char || c == '\177') { /* Robert Viduya */
230 /* this test last - @ might be the kill_char */
232 while (bufp != obufp) {
236 #else /* NEWAUTOCOMP */
237 for (; *bufp; ++bufp)
239 for (; bufp != obufp; --bufp)
242 #endif /* NEWAUTOCOMP */
246 ttyDisplay->toplin = 2; /* nonempty, no --More-- required */
247 ttyDisplay->inread--;
248 clear_nhwindow(WIN_MESSAGE); /* clean up after ourselves */
250 Strcpy(bfp, str2ic(tmp));
253 if (suppress_history) {
254 /* prevent next message from pushing current query+answer into
255 tty message history */
259 /* needed because we've bypassed pline() */
260 dumplogmsg(toplines);
267 register const char *s; /* chars allowed besides return */
269 register int c, x = ttyDisplay ? (int) ttyDisplay->dismiss_more : '\n';
273 #ifdef HANGUPHANDLING
274 !program_state.done_hup &&
276 (c = tty_nhgetch()) != EOF) {
277 if (c == '\n' || c == '\r')
283 ttyDisplay->dismiss_more = 1;
287 if ((s && index(s, c)) || c == x || (x == '\n' && c == '\r')) {
297 * Implement extended command completion by using this hook into
298 * tty_getlin. Check the characters already typed, if they uniquely
299 * identify an extended command, expand the string to the whole
302 * Return TRUE if we've extended the string at base. Otherwise return FALSE.
305 * + we don't change the characters that are already in base
306 * + base has enough room to hold our string
309 ext_cmd_getlin_hook(base)
312 int oindex, com_index;
315 for (oindex = 0; extcmdlist[oindex].ef_txt != (char *) 0; oindex++) {
316 if (extcmdlist[oindex].flags & CMD_NOT_AVAILABLE)
318 if ((extcmdlist[oindex].flags & AUTOCOMPLETE)
319 && !(!wizard && (extcmdlist[oindex].flags & WIZMODECMD))
320 && !strncmpi(base, extcmdlist[oindex].ef_txt, strlen(base))) {
321 if (com_index == -1) /* no matches yet */
323 else /* more than 1 match */
327 if (com_index >= 0) {
328 Strcpy(base, extcmdlist[com_index].ef_txt);
332 return FALSE; /* didn't match anything */
336 * Read in an extended command, doing command line completion. We
337 * stop when we have found enough characters to make a unique command.
346 return extcmd_via_menu();
348 suppress_history = TRUE;
349 /* maybe a runtime option?
350 * hooked_tty_getlin("#", buf,
351 * (flags.cmd_comp && !in_doagain)
352 * ? ext_cmd_getlin_hook
353 * : (getlin_hook_proc) 0);
356 hooked_tty_getlin("#", buf, in_doagain ? (getlin_hook_proc) 0
357 : ext_cmd_getlin_hook);
358 (void) mungspaces(buf);
359 if (buf[0] == 0 || buf[0] == '\033')
362 for (i = 0; extcmdlist[i].ef_txt != (char *) 0; i++)
363 if (!strcmpi(buf, extcmdlist[i].ef_txt))
368 for (j = 0; buf[j]; j++)
373 if (extcmdlist[i].ef_txt == (char *) 0) {
375 pline("%s: unknown extended command.", buf);
377 pline("%s:
\8ag
\92£
\83R
\83}
\83\93\83h
\83G
\83\89\81[", buf);
384 #endif /* TTY_GRAPHICS */