1 /* NetHack 3.6 getline.c $NHDT-Date: 1432512813 2015/05/25 00:13:33 $ $NHDT-Branch: master $:$NHDT-Revision: 1.28 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
16 char morc = 0; /* tell the outside world what char you chose */
17 STATIC_DCL boolean FDECL(ext_cmd_getlin_hook, (char *));
19 typedef boolean FDECL((*getlin_hook_proc), (char *));
21 STATIC_DCL void FDECL(hooked_tty_getlin,
22 (const char *, char *, getlin_hook_proc));
23 extern int NDECL(extcmd_via_menu); /* cmd.c */
25 extern char erase_char, kill_char; /* from appropriate tty.c file */
28 * Read a line closed with '\n' into the array char bufp[BUFSZ].
29 * (The '\n' is not stored. The string is closed with a '\0'.)
30 * Reading can be interrupted by an escape ('\033') - now the
31 * resulting string is "\033".
34 tty_getlin(query, bufp)
38 hooked_tty_getlin(query, bufp, (getlin_hook_proc) 0);
42 hooked_tty_getlin(query, bufp, hook)
45 getlin_hook_proc hook;
47 register char *obufp = bufp;
49 struct WinDesc *cw = wins[WIN_MESSAGE];
52 if (ttyDisplay->toplin == 1 && !(cw->flags & WIN_STOP))
54 cw->flags &= ~WIN_STOP;
55 ttyDisplay->toplin = 3; /* special prompt state */
60 (void) fflush(stdout);
61 Strcat(strcat(strcpy(toplines, query), " "), obufp);
63 if (c == '\033' || c == EOF) {
64 if (c == '\033' && obufp[0] != '\0') {
67 tty_clear_nhwindow(WIN_MESSAGE);
68 cw->maxcol = cw->maxrow;
78 if (ttyDisplay->intr) {
82 if (c == '\020') { /* ctrl-P */
83 if (iflags.prevmsg_window != 's') {
84 int sav = ttyDisplay->inread;
85 ttyDisplay->inread = 0;
86 (void) tty_doprev_message();
87 ttyDisplay->inread = sav;
88 tty_clear_nhwindow(WIN_MESSAGE);
89 cw->maxcol = cw->maxrow;
96 (void) tty_doprev_message(); /* need two initially */
97 (void) tty_doprev_message();
101 } else if (doprev && iflags.prevmsg_window == 's') {
102 tty_clear_nhwindow(WIN_MESSAGE);
103 cw->maxcol = cw->maxrow;
110 if (c == erase_char || c == '\b') {
115 #endif /* NEWAUTOCOMP */
118 putsyms("\b \b"); /* putsym converts \b */
119 #else /* NEWAUTOCOMP */
121 for (i = bufp; *i; ++i)
123 for (; i > bufp; --i)
126 #endif /* NEWAUTOCOMP */
130 } else if (c == '\n' || c == '\r') {
132 } else if (c == '\n') {
136 #endif /* not NEWAUTOCOMP */
138 } else if (' ' <= (unsigned char) c && c != '\177'
139 && (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)) {
140 /* avoid isprint() - some people don't have it
141 ' ' is not always a printing char */
145 #endif /* NEWAUTOCOMP */
150 if (hook && (*hook)(obufp)) {
154 #else /* NEWAUTOCOMP */
155 /* pointer and cursor left where they were */
156 for (i = bufp; *i; ++i)
158 } else if (i > bufp) {
161 /* erase rest of prior guess */
162 for (; i > bufp; --i)
164 for (; s > bufp; --s)
166 #endif /* NEWAUTOCOMP */
168 } else if (c == kill_char || c == '\177') { /* Robert Viduya */
169 /* this test last - @ might be the kill_char */
171 while (bufp != obufp) {
175 #else /* NEWAUTOCOMP */
176 for (; *bufp; ++bufp)
178 for (; bufp != obufp; --bufp)
181 #endif /* NEWAUTOCOMP */
185 ttyDisplay->toplin = 2; /* nonempty, no --More-- required */
186 ttyDisplay->inread--;
187 clear_nhwindow(WIN_MESSAGE); /* clean up after ourselves */
192 register const char *s; /* chars allowed besides return */
194 register int c, x = ttyDisplay ? (int) ttyDisplay->dismiss_more : '\n';
198 #ifdef HANGUPHANDLING
199 !program_state.done_hup &&
201 (c = tty_nhgetch()) != EOF) {
208 ttyDisplay->dismiss_more = 1;
212 if ((s && index(s, c)) || c == x) {
222 * Implement extended command completion by using this hook into
223 * tty_getlin. Check the characters already typed, if they uniquely
224 * identify an extended command, expand the string to the whole
227 * Return TRUE if we've extended the string at base. Otherwise return FALSE.
230 * + we don't change the characters that are already in base
231 * + base has enough room to hold our string
234 ext_cmd_getlin_hook(base)
237 int oindex, com_index;
240 for (oindex = 0; extcmdlist[oindex].ef_txt != (char *) 0; oindex++) {
241 if (!strncmpi(base, extcmdlist[oindex].ef_txt, strlen(base))) {
242 if (com_index == -1) /* no matches yet */
244 else /* more than 1 match */
248 if (com_index >= 0) {
249 Strcpy(base, extcmdlist[com_index].ef_txt);
253 return FALSE; /* didn't match anything */
257 * Read in an extended command, doing command line completion. We
258 * stop when we have found enough characters to make a unique command.
267 return extcmd_via_menu();
268 /* maybe a runtime option? */
269 /* hooked_tty_getlin("#", buf, flags.cmd_comp ? ext_cmd_getlin_hook :
270 * (getlin_hook_proc) 0); */
271 hooked_tty_getlin("#", buf, in_doagain ? (getlin_hook_proc) 0
272 : ext_cmd_getlin_hook);
273 (void) mungspaces(buf);
274 if (buf[0] == 0 || buf[0] == '\033')
277 for (i = 0; extcmdlist[i].ef_txt != (char *) 0; i++)
278 if (!strcmpi(buf, extcmdlist[i].ef_txt))
283 for (j = 0; buf[j]; j++)
288 if (extcmdlist[i].ef_txt == (char *) 0) {
289 pline("%s: unknown extended command.", buf);
296 #endif /* TTY_GRAPHICS */