1 /* NetHack 3.6 getline.c $NHDT-Date: 1523619111 2018/04/13 11:31:51 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.35 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Michael Allison, 2006. */
4 /* NetHack may be freely redistributed. See license for details. */
7 ** Japanese version Copyright
8 ** For 3.4+, Copyright (c) Kentaro Shirakata, 2002-2015
9 ** JNetHack may be freely redistributed. See license for details.
23 char morc = 0; /* tell the outside world what char you chose */
24 STATIC_VAR boolean suppress_history;
25 STATIC_DCL boolean FDECL(ext_cmd_getlin_hook, (char *));
27 typedef boolean FDECL((*getlin_hook_proc), (char *));
29 STATIC_DCL void FDECL(hooked_tty_getlin,
30 (const char *, char *, getlin_hook_proc));
31 extern int NDECL(extcmd_via_menu); /* cmd.c */
33 extern char erase_char, kill_char; /* from appropriate tty.c file */
36 * Read a line closed with '\n' into the array char bufp[BUFSZ].
37 * (The '\n' is not stored. The string is closed with a '\0'.)
38 * Reading can be interrupted by an escape ('\033') - now the
39 * resulting string is "\033".
42 tty_getlin(query, bufp)
46 suppress_history = FALSE;
47 hooked_tty_getlin(query, bufp, (getlin_hook_proc) 0);
52 hooked_tty_getlin(query, bufp, hook)
54 hooked_tty_getlin(query, bfp, hook)
60 getlin_hook_proc hook;
66 register char *obufp = bufp;
71 struct WinDesc *cw = wins[WIN_MESSAGE];
77 if (ttyDisplay->toplin == 1 && !(cw->flags & WIN_STOP))
79 cw->flags &= ~WIN_STOP;
80 ttyDisplay->toplin = 3; /* special prompt state */
83 /* issue the prompt */
84 custompline(OVERRIDE_MSGTYPE | SUPPRESS_HISTORY, "%s ", query);
86 /* bufp is input/output; treat current contents (presumed to be from
87 previous getlin()) as default input */
91 /* !EDIT_GETLIN: bufp is output only; init it to empty */
96 (void) fflush(stdout);
97 Strcat(strcat(strcpy(toplines, query), " "), obufp);
100 uc = (*((unsigned int *)&c));
103 if (c == '\033' || c == EOF) {
104 if (c == '\033' && obufp[0] != '\0') {
107 tty_clear_nhwindow(WIN_MESSAGE);
108 cw->maxcol = cw->maxrow;
118 if (ttyDisplay->intr) {
122 if (c == '\020') { /* ctrl-P */
123 if (iflags.prevmsg_window != 's') {
124 int sav = ttyDisplay->inread;
126 ttyDisplay->inread = 0;
127 (void) tty_doprev_message();
128 ttyDisplay->inread = sav;
129 tty_clear_nhwindow(WIN_MESSAGE);
130 cw->maxcol = cw->maxrow;
137 (void) tty_doprev_message(); /* need two initially */
138 (void) tty_doprev_message();
142 } else if (doprev && iflags.prevmsg_window == 's') {
143 tty_clear_nhwindow(WIN_MESSAGE);
144 cw->maxcol = cw->maxrow;
151 if (c == erase_char || c == '\b') {
159 #endif /* NEWAUTOCOMP */
162 putsyms("\b \b"); /* putsym converts \b */
163 #else /* NEWAUTOCOMP */
165 for (i = bufp; *i; ++i)
167 for (; i > bufp; --i)
170 #endif /* NEWAUTOCOMP */
176 n = offset_in_kanji(tmp, bufp - tmp);
178 /*
\8cã
\82Å1
\83o
\83C
\83g
\88ø
\82©
\82ê
\82é
\82Ì
\82Å
\82»
\82Ì
\95ª
\82Í
\82±
\82±
\82Å
\82Í
\88ø
\82©
\82È
\82¢ */
179 bufp = bufp - (n - 1);
184 } else if (c == '\n' || c == '\r') {
187 #endif /* not NEWAUTOCOMP */
190 } else if (' ' <= (unsigned char) c && c != '\177'
192 } else if (' ' <= uc && uc < '\377'
194 /* avoid isprint() - some people don't have it
195 ' ' is not always a printing char */
196 && (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)) {
200 #endif /* NEWAUTOCOMP */
209 if (hook && (*hook)(obufp)) {
217 #else /* NEWAUTOCOMP */
218 /* pointer and cursor left where they were */
219 for (i = bufp; *i; ++i)
221 } else if (i > bufp) {
224 /* erase rest of prior guess */
225 for (; i > bufp; --i)
227 for (; s > bufp; --s)
229 #endif /* NEWAUTOCOMP */
231 } else if (c == kill_char || c == '\177') { /* Robert Viduya */
232 /* this test last - @ might be the kill_char */
234 while (bufp != obufp) {
238 #else /* NEWAUTOCOMP */
239 for (; *bufp; ++bufp)
241 for (; bufp != obufp; --bufp)
244 #endif /* NEWAUTOCOMP */
248 ttyDisplay->toplin = 2; /* nonempty, no --More-- required */
249 ttyDisplay->inread--;
250 clear_nhwindow(WIN_MESSAGE); /* clean up after ourselves */
252 Strcpy(bfp, str2ic(tmp));
255 if (suppress_history) {
256 /* prevent next message from pushing current query+answer into
257 tty message history */
261 /* needed because we've bypassed pline() */
262 dumplogmsg(toplines);
269 register const char *s; /* chars allowed besides return */
271 register int c, x = ttyDisplay ? (int) ttyDisplay->dismiss_more : '\n';
275 #ifdef HANGUPHANDLING
276 !program_state.done_hup &&
278 (c = tty_nhgetch()) != EOF) {
279 if (c == '\n' || c == '\r')
285 ttyDisplay->dismiss_more = 1;
289 if ((s && index(s, c)) || c == x || (x == '\n' && c == '\r')) {
299 * Implement extended command completion by using this hook into
300 * tty_getlin. Check the characters already typed, if they uniquely
301 * identify an extended command, expand the string to the whole
304 * Return TRUE if we've extended the string at base. Otherwise return FALSE.
307 * + we don't change the characters that are already in base
308 * + base has enough room to hold our string
311 ext_cmd_getlin_hook(base)
314 int oindex, com_index;
317 for (oindex = 0; extcmdlist[oindex].ef_txt != (char *) 0; oindex++) {
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 */