1 /* SCCS Id: @(#)getline.c 3.4 2002/10/06 */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
17 char morc = 0; /* tell the outside world what char you chose */
19 STATIC_DCL boolean FDECL(ext_cmd_getlin_hook, (char *));
21 typedef boolean FDECL((*getlin_hook_proc), (char *));
23 STATIC_DCL void FDECL(hooked_tty_getlin, (const char*,char*,getlin_hook_proc));
24 extern int NDECL(extcmd_via_menu); /* cmd.c */
26 extern char erase_char, kill_char; /* from appropriate tty.c file */
31 * Read a line closed with '\n' into the array char bufp[BUFSZ].
32 * (The '\n' is not stored. The string is closed with a '\0'.)
33 * Reading can be interrupted by an escape ('\033') - now the
34 * resulting string is "\033".
37 tty_getlin(query, bufp)
41 hooked_tty_getlin(query, bufp, (getlin_hook_proc) 0);
45 hooked_tty_getlin(query, bufp, hook)
48 getlin_hook_proc hook;
50 register char *obufp = bufp;
52 struct WinDesc *cw = wins[WIN_MESSAGE];
55 if(ttyDisplay->toplin == 1 && !(cw->flags & WIN_STOP)) more();
56 cw->flags &= ~WIN_STOP;
57 ttyDisplay->toplin = 3; /* special prompt state */
62 (void) fflush(stdout);
63 Sprintf(toplines, "%s ", query);
64 Strcat(toplines, obufp);
65 if((c = Getchar()) == EOF) {
68 #endif /* not NEWAUTOCOMP */
76 if (ttyDisplay->intr) {
80 if(c == '\020') { /* ctrl-P */
81 if (iflags.prevmsg_window != 's') {
82 int sav = ttyDisplay->inread;
83 ttyDisplay->inread = 0;
84 (void) tty_doprev_message();
85 ttyDisplay->inread = sav;
86 tty_clear_nhwindow(WIN_MESSAGE);
87 cw->maxcol = cw->maxrow;
94 (void) tty_doprev_message();/* need two initially */
95 (void) tty_doprev_message();
99 } else if (doprev && iflags.prevmsg_window == 's') {
100 tty_clear_nhwindow(WIN_MESSAGE);
101 cw->maxcol = cw->maxrow;
108 if(c == erase_char || c == '\b') {
113 #endif /* NEWAUTOCOMP */
116 putsyms("\b \b");/* putsym converts \b */
117 #else /* NEWAUTOCOMP */
119 for (i = bufp; *i; ++i) putsyms(" ");
120 for (; i > bufp; --i) putsyms("\b");
122 #endif /* NEWAUTOCOMP */
125 } else if(c == '\n' || c == '\r') {
127 } else if(c == '\n') {
131 #endif /* not NEWAUTOCOMP */
133 } else if(' ' <= (unsigned char) c && c != '\177' &&
134 (bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)) {
135 /* avoid isprint() - some people don't have it
136 ' ' is not always a printing char */
140 #endif /* NEWAUTOCOMP */
145 if (hook && (*hook)(obufp)) {
149 #else /* NEWAUTOCOMP */
150 /* pointer and cursor left where they were */
151 for (i = bufp; *i; ++i) putsyms("\b");
152 } else if (i > bufp) {
155 /* erase rest of prior guess */
156 for (; i > bufp; --i) putsyms(" ");
157 for (; s > bufp; --s) putsyms("\b");
158 #endif /* NEWAUTOCOMP */
160 } else if(c == kill_char || c == '\177') { /* Robert Viduya */
161 /* this test last - @ might be the kill_char */
163 while(bufp != obufp) {
167 #else /* NEWAUTOCOMP */
168 for (; *bufp; ++bufp) putsyms(" ");
169 for (; bufp != obufp; --bufp) putsyms("\b \b");
171 #endif /* NEWAUTOCOMP */
175 ttyDisplay->toplin = 2; /* nonempty, no --More-- required */
176 ttyDisplay->inread--;
177 clear_nhwindow(WIN_MESSAGE); /* clean up after ourselves */
182 register const char *s; /* chars allowed besides return */
184 register int c, x = ttyDisplay ? (int) ttyDisplay->dismiss_more : '\n';
188 while((c = tty_nhgetch()) != '\n') {
190 if ((s && index(s,c)) || c == x) {
204 * Implement extended command completion by using this hook into
205 * tty_getlin. Check the characters already typed, if they uniquely
206 * identify an extended command, expand the string to the whole
209 * Return TRUE if we've extended the string at base. Otherwise return FALSE.
212 * + we don't change the characters that are already in base
213 * + base has enough room to hold our string
216 ext_cmd_getlin_hook(base)
219 int oindex, com_index;
222 for (oindex = 0; extcmdlist[oindex].ef_txt != (char *)0; oindex++) {
223 if (!strncmpi(base, extcmdlist[oindex].ef_txt, strlen(base))) {
224 if (com_index == -1) /* no matches yet */
226 else /* more than 1 match */
230 if (com_index >= 0) {
231 Strcpy(base, extcmdlist[com_index].ef_txt);
235 return FALSE; /* didn't match anything */
239 * Read in an extended command, doing command line completion. We
240 * stop when we have found enough characters to make a unique command.
248 if (iflags.extmenu) return extcmd_via_menu();
249 /* maybe a runtime option? */
250 /* hooked_tty_getlin("#", buf, flags.cmd_comp ? ext_cmd_getlin_hook : (getlin_hook_proc) 0); */
252 hooked_tty_getlin("#", buf, in_doagain ? (getlin_hook_proc)0
253 : ext_cmd_getlin_hook);
255 hooked_tty_getlin("#", buf, ext_cmd_getlin_hook);
257 (void) mungspaces(buf);
258 if (buf[0] == 0 || buf[0] == '\033') return -1;
260 for (i = 0; extcmdlist[i].ef_txt != (char *)0; i++)
261 if (!strcmpi(buf, extcmdlist[i].ef_txt)) break;
266 for (j = 0; buf[j]; j++)
272 if (extcmdlist[i].ef_txt == (char *)0) {
273 pline("%s: unknown extended command.", buf);
282 #endif /* TTY_GRAPHICS */