I made some changes that I hope will help the lagging input problem
[citadel.git] / citadel / commands.c
1 /*
2  * $Id$
3  *
4  * This file contains functions which implement parts of the
5  * text-mode user interface.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #ifdef HAVE_TERMIOS_H
30 #include <termios.h>
31 #else
32 #include <sgtty.h>
33 #endif
34
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif
38
39 #ifdef THREADED_CLIENT
40 #include <pthread.h>
41 #endif
42
43 #include <signal.h>
44 #include <errno.h>
45 #include <stdarg.h>
46 #include "citadel.h"
47 #include "commands.h"
48 #include "messages.h"
49 #include "citadel_decls.h"
50 #include "routines.h"
51 #include "routines2.h"
52 #include "tools.h"
53 #include "rooms.h"
54 #include "client_chat.h"
55 #ifndef HAVE_SNPRINTF
56 #include "snprintf.h"
57 #endif
58 #include "screen.h"
59
60 struct citcmd {
61         struct citcmd *next;
62         int c_cmdnum;
63         int c_axlevel;
64         char c_keys[5][64];
65 };
66
67 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
68
69
70 int rc_exp_beep;
71 char rc_exp_cmd[1024];
72 int rc_allow_attachments;
73 int rc_display_message_numbers;
74 int rc_force_mail_prompts;
75 int rc_remember_passwords;
76 int rc_ansi_color;
77 int num_urls = 0;
78 int rc_prompt_control = 0;
79 char urls[MAXURLS][SIZ];
80 char rc_url_cmd[SIZ];
81
82 char *gl_string;
83 int next_lazy_cmd = 5;
84
85 int lines_printed = 0;          /* line count for paginator */
86 extern int screenwidth, screenheight;
87 extern int termn8;
88
89 struct citcmd *cmdlist = NULL;
90
91
92 /* these variables are local to this module */
93 char keepalives_enabled = KA_YES;       /* send NOOPs to server when idle */
94 int ok_to_interrupt = 0;                /* print express msgs asynchronously */
95 time_t AnsiDetect;                      /* when did we send the detect code? */
96 int enable_color = 0;                   /* nonzero for ANSI color */
97
98
99
100
101 /*
102  * If an interesting key has been pressed, return its value, otherwise 0
103  */
104 char was_a_key_pressed(void) {
105         fd_set rfds;
106         struct timeval tv;
107         int the_character;
108         int retval;
109
110         FD_ZERO(&rfds);
111         FD_SET(0, &rfds);
112         tv.tv_sec = 0;
113         tv.tv_usec = 0;
114         retval = select(1, &rfds, NULL, NULL, &tv); 
115
116         /* Careful!  Disable keepalives during keyboard polling; we're probably
117          * in the middle of a data transfer from the server, in which case
118          * sending a NOOP would throw the client protocol out of sync.
119          */
120         if (FD_ISSET(0, &rfds)) {
121                 set_keepalives(KA_NO);
122                 the_character = inkey();
123                 set_keepalives(KA_YES);
124         }
125         else {
126                 the_character = 0;
127         }
128         return(the_character);
129 }
130
131
132
133
134
135 /*
136  * Check to see if we need to pause at the end of a screen.
137  * If we do, we have to disable server keepalives during the pause because
138  * we are probably in the middle of a server operation and the NOOP command
139  * would confuse everything.
140  */
141 int checkpagin(int lp, int pagin, int height)
142 {
143         int thekey;
144
145         if (sigcaught) return(lp);
146         thekey = was_a_key_pressed();
147         if (thekey == 'q' || thekey == 'Q' || thekey == 's' || thekey == 'S')
148                 thekey = STOP_KEY;
149         if (thekey == 'n' || thekey == 'N')
150                 thekey = NEXT_KEY;
151         if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
152         if (sigcaught) return(lp);
153
154         if (!pagin) return(0);
155         if (lp>=(height-1)) {
156                 set_keepalives(KA_NO);
157                 hit_any_key();
158                 set_keepalives(KA_YES);
159                 return(0);
160         }
161         return(lp);
162 }
163
164
165
166
167 /*
168  * pprintf()  ...   paginated version of printf()
169  */
170 void pprintf(const char *format, ...) {   
171         va_list arg_ptr;
172         static char buf[4096];  /* static for performance, change if needed */
173         int i;
174
175         /* If sigcaught is nonzero, a keypress has interrupted this and we
176          * should just drain output.
177          */
178         if (sigcaught) return;
179  
180         /* Otherwise, start spewing... */ 
181         va_start(arg_ptr, format);   
182         vsprintf(buf, format, arg_ptr);   
183         va_end(arg_ptr);   
184
185         for (i=0; i<strlen(buf); ++i) {
186                 scr_putc(buf[i]);
187                 if (buf[i]==10) {
188                         ++lines_printed;
189                         lines_printed = checkpagin(lines_printed,
190                                 (userflags & US_PAGINATOR),
191                                 screenheight);
192                 }
193         }
194 }   
195
196
197
198 /*
199  * print_express()  -  print express messages if there are any
200  */
201 void print_express(void)
202 {
203         char buf[1024];
204         FILE *outpipe;
205         time_t timestamp;
206         struct tm *stamp;
207         int flags = 0;
208         char sender[64];
209         char node[64];
210
211         if (express_msgs == 0)
212                 return;
213
214         if (rc_exp_beep) {
215                 scr_putc(7);
216         }
217         if (strlen(rc_exp_cmd) == 0) {
218                 color(BRIGHT_RED);
219                 scr_printf("\r---");
220         }
221         
222         while (express_msgs != 0) {
223                 serv_puts("GEXP");
224                 serv_gets(buf);
225                 if (buf[0] != '1')
226                         return;
227         
228                 express_msgs = extract_int(&buf[4], 0);
229                 timestamp = extract_long(&buf[4], 1);
230                 flags = extract_int(&buf[4], 2);
231                 extract(sender, &buf[4], 3);
232                 extract(node, &buf[4], 4);
233                 strcpy(last_paged, sender);
234         
235                 stamp = localtime(&timestamp);
236
237                 /* If the page is a Logoff Request, honor it. */
238                 if (flags & 2) {
239                         termn8 = 1;
240                         return;
241                 }
242         
243                 if (strlen(rc_exp_cmd) > 0) {
244                         outpipe = popen(rc_exp_cmd, "w");
245                         if (outpipe != NULL) {
246                                 /* Header derived from flags */
247                                 if (flags & 2)
248                                         fprintf(outpipe,
249                                                "Please log off now, as requested ");
250                                 else if (flags & 1)
251                                         fprintf(outpipe, "Broadcast message ");
252                                 else if (flags & 4)
253                                         fprintf(outpipe, "Chat request ");
254                                 else
255                                         fprintf(outpipe, "Message ");
256                                 /* Timestamp.  Can this be improved? */
257                                 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
258                                         fprintf(outpipe, "at 12:%02d%cm",
259                                                 stamp->tm_min, 
260                                                 stamp->tm_hour ? 'p' : 'a');
261                                 else if (stamp->tm_hour > 12)           /* pm */
262                                         fprintf(outpipe, "at %d:%02dpm",
263                                                 stamp->tm_hour - 12,
264                                                 stamp->tm_min);
265                                 else                                    /* am */
266                                         fprintf(outpipe, "at %d:%02dam",
267                                                 stamp->tm_hour, stamp->tm_min);
268                                 fprintf(outpipe, " from %s", sender);
269                                 if (strncmp(serv_info.serv_nodename, node, 32))
270                                         fprintf(outpipe, " @%s", node);
271                                 fprintf(outpipe, ":\n");
272                                 while (serv_gets(buf), strcmp(buf, "000")) {
273                                         fprintf(outpipe, "%s\n", buf);
274                                 }
275                                 pclose(outpipe);
276                                 if (express_msgs == 0)
277                                         return;
278                                 continue;
279                         }
280                 }
281                 /* fall back to built-in express message display */
282                 scr_printf("\n");
283                 lines_printed++;
284
285                 /* Header derived from flags */
286                 if (flags & 2)
287                         scr_printf("Please log off now, as requested ");
288                 else if (flags & 1)
289                         scr_printf("Broadcast message ");
290                 else if (flags & 4)
291                         scr_printf("Chat request ");
292                 else
293                         scr_printf("Message ");
294         
295                 /* Timestamp.  Can this be improved? */
296                 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
297                         scr_printf("at 12:%02d%cm", stamp->tm_min, 
298                                 stamp->tm_hour ? 'p' : 'a');
299                 else if (stamp->tm_hour > 12)                   /* pm */
300                         scr_printf("at %d:%02dpm",
301                                 stamp->tm_hour - 12, stamp->tm_min);
302                 else                                            /* am */
303                         scr_printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
304                 
305                 /* Sender */
306                 scr_printf(" from %s", sender);
307         
308                 /* Remote node, if any */
309                 if (strncmp(serv_info.serv_nodename, node, 32))
310                         scr_printf(" @%s", node);
311         
312                 scr_printf(":\n");
313                 lines_printed++;
314                 fmout(screenwidth, NULL, NULL, 1, screenheight, -1, 0);
315         }
316         scr_printf("\n---\n");
317         color(BRIGHT_WHITE);
318
319
320 }
321
322
323 void set_keepalives(int s)
324 {
325         keepalives_enabled = (char) s;
326 }
327
328 /* 
329  * This loop handles the "keepalive" messages sent to the server when idling.
330  */
331
332 static time_t idlet = 0;
333 static void really_do_keepalive(void) {
334         char buf[1024];
335
336         time(&idlet);
337         if (keepalives_enabled == KA_YES) {
338                 serv_puts("NOOP");
339                 serv_gets(buf);
340                 if (buf[3] == '*') {
341                         express_msgs = 1;
342                         if (ok_to_interrupt == 1) {
343                                 scr_printf("\r%64s\r", "");
344                                 print_express();
345                                 scr_printf("%s%c ", room_name,
346                                        room_prompt(room_flags));
347                                 scr_flush();
348                         }
349                 }
350         }
351 }
352
353 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
354    encapsulated interface; in theory there should be no need to touch these
355    globals outside of the async_ka_* functions. */
356
357 #ifdef THREADED_CLIENT
358 static pthread_t ka_thr_handle;
359 static int ka_thr_active = 0;
360 static int async_ka_enabled = 0;
361
362 static void *ka_thread(void *arg)
363 {
364         really_do_keepalive();
365         pthread_detach(ka_thr_handle);
366         ka_thr_active = 0;
367         return NULL;
368 }
369
370 /* start up a thread to handle a keepalive in the background */
371 static void async_ka_exec(void)
372 {
373         if (!ka_thr_active) {
374                 ka_thr_active = 1;
375                 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
376                         perror("pthread_create");
377                         exit(1);
378                 }
379         }
380 }
381 #endif /* THREADED_CLIENT */
382
383 /* I changed this from static to not because I need to call it from
384    screen.c, either that or make something in screen.c not static.
385    Fix it how you like. Why all the staticness? stu */
386    
387 void do_keepalive(void)
388 {
389         time_t now;
390
391         time(&now);
392         if ((now - idlet) < ((long) S_KEEPALIVE))
393                 return;
394
395         /* Do a space-backspace to keep telnet sessions from idling out */
396         scr_printf(" %c", 8);
397         scr_flush();
398
399 #ifdef THREADED_CLIENT
400         if (async_ka_enabled)
401                 async_ka_exec();
402         else
403 #endif
404                 really_do_keepalive();
405 }
406
407
408 /* Now the actual async-keepalve API that we expose to higher levels:
409    async_ka_start() and async_ka_end(). These do nothing when we don't have
410    threading enabled, so we avoid sprinkling ifdef's throughout the code. */
411
412 /* wait for a background keepalive to complete. this must be done before
413    attempting any further server requests! */
414 void async_ka_end(void)
415 {
416 #ifdef THREADED_CLIENT
417         if (ka_thr_active)
418                 pthread_join(ka_thr_handle, NULL);
419
420         async_ka_enabled--;
421 #endif
422 }
423
424 /* tell do_keepalive() that keepalives are asynchronous. */
425 void async_ka_start(void)
426 {
427 #ifdef THREADED_CLIENT
428         async_ka_enabled++;
429 #endif
430 }
431
432
433 int inkey(void)
434 {                               /* get a character from the keyboard, with   */
435         int a;                  /* the watchdog timer in effect if necessary */
436 #ifndef HAVE_CURSES_H /* avoid compiler warning */
437         fd_set rfds;
438         struct timeval tv;
439 #endif
440         time_t start_time;
441
442         scr_flush();
443         lines_printed = 0;
444         time(&start_time);
445
446         do {
447 #ifdef HAVE_CURSES_H /* IO, maybe you wanna move this to screen.c */
448         a = scr_blockread();
449 #else
450                 /* This loop waits for keyboard input.  If the keepalive
451                  * timer expires, it sends a keepalive to the server if
452                  * necessary and then waits again.
453                  */
454                 do {
455                         do_keepalive();
456
457                         FD_ZERO(&rfds);
458                         FD_SET(0, &rfds);
459                         tv.tv_sec = S_KEEPALIVE;
460                         tv.tv_usec = 0;
461
462                         select(1, &rfds, NULL, NULL, &tv);
463                 } while (!FD_ISSET(0, &rfds));
464
465                 /* At this point, there's input, so fetch it.
466                  * (There's a hole in the bucket...)
467                  */
468                 a = scr_getc();
469 #endif
470
471
472                 if (a == 127)
473                         a = 8;
474                 if (a > 126)
475                         a = 0;
476                 if (a == 13)
477                         a = 10;
478                 if (((a != 4) && (a != 10) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
479                     && ((a < 32) || (a > 126)))
480                         a = 0;
481         } while (a == 0);
482         return (a);
483 }
484
485
486 int yesno(void)
487 {                               /* Returns 1 for yes, 0 for no */
488         int a;
489         while (1) {
490                 a = inkey();
491                 a = tolower(a);
492                 if (a == 'y') {
493                         scr_printf("Yes\n");
494                         return (1);
495                 }
496                 if (a == 'n') {
497                         scr_printf("No\n");
498                         return (0);
499                 }
500         }
501 }
502
503 /* Returns 1 for yes, 0 for no, arg is default value */
504 int yesno_d(int d)
505 {
506         int a;
507         while (1) {
508                 a = inkey();
509                 a = tolower(a);
510                 if (a == 10)
511                         a = (d ? 'y' : 'n');
512                 if (a == 'y') {
513                         scr_printf("Yes\n");
514                         return (1);
515                 }
516                 if (a == 'n') {
517                         scr_printf("No\n");
518                         return (0);
519                 }
520         }
521 }
522
523
524
525
526 /* Gets a line from the terminal */
527 /* string == Pointer to string buffer */
528 /* lim == Maximum length - if negative, no-show */
529 void getline(char *string, int lim) 
530 {
531         int a, b;
532         char flag = 0;
533
534         if (lim < 0) {
535                 lim = (0 - lim);
536                 flag = 1;
537         }
538         strcpy(string, "");
539         gl_string = string;
540         async_ka_start();
541       GLA:a = inkey();
542         a = (a & 127);
543         if ((a == 8) && (strlen(string) == 0))
544                 goto GLA;
545         if ((a != 10) && (a != 8) && (strlen(string) == lim))
546                 goto GLA;
547         if ((a == 8) && (string[0] != 0)) {
548                 string[strlen(string) - 1] = 0;
549                 scr_putc(8);
550                 scr_putc(32);
551                 scr_putc(8);
552                 goto GLA;
553         }
554         if ((a == 10)) {
555                 scr_putc(10);
556                 async_ka_end();
557                 return;
558         }
559         if (a < 32)
560                 a = '.';
561         b = strlen(string);
562         string[b] = a;
563         string[b + 1] = 0;
564         if (flag == 0)
565                 scr_putc(a);
566         if (flag == 1)
567                 scr_putc('*');
568         goto GLA;
569 }
570
571
572 /*
573  * strprompt()  -  prompt for a string, print the existing value and
574  *                 allow the user to press return to keep it...
575  */
576 void strprompt(char *prompt, char *str, int len)
577 {
578         char buf[128];
579         print_express();
580         color(DIM_WHITE);
581         scr_printf("%s ", prompt);
582         color(DIM_MAGENTA);
583         scr_printf("[");
584         color(BRIGHT_MAGENTA);
585         scr_printf("%s", str);
586         color(DIM_MAGENTA);
587         scr_printf("]");
588         color(DIM_WHITE);
589         scr_printf(": ");
590         color(BRIGHT_CYAN);
591         getline(buf, len);
592         if (buf[0] != 0)
593                 strcpy(str, buf);
594         color(DIM_WHITE);
595 }
596
597 /*
598  * boolprompt()  -  prompt for a yes/no, print the existing value and
599  *                  allow the user to press return to keep it...
600  */
601 int boolprompt(char *prompt, int prev_val)
602 {
603         int r;
604
605         color(DIM_WHITE);
606         scr_printf("%s ", prompt);
607         color(DIM_MAGENTA);
608         scr_printf(" [");
609         color(BRIGHT_MAGENTA);
610         scr_printf("%s", (prev_val ? "Yes" : "No"));
611         color(DIM_MAGENTA);
612         scr_printf("]: ");
613         color(BRIGHT_CYAN);
614         r = (yesno_d(prev_val));
615         color(DIM_WHITE);
616         return r;
617 }
618
619 /* 
620  * intprompt()  -  like strprompt(), except for an integer
621  *                 (note that it RETURNS the new value!)
622  */
623 int intprompt(char *prompt, int ival, int imin, int imax)
624 {
625         char buf[16];
626         int i;
627         int p;
628
629         do {
630                 i = ival;
631                 snprintf(buf, sizeof buf, "%d", i);
632                 strprompt(prompt, buf, 15);
633                 i = atoi(buf);
634                 for (p=0; p<strlen(buf); ++p) {
635                         if ( (!isdigit(buf[p]))
636                            && ( (buf[p]!='-') || (p!=0) )  )
637                                 i = imin - 1;
638                 }
639                 if (i < imin)
640                         scr_printf("*** Must be no less than %d.\n", imin);
641                 if (i > imax)
642                         scr_printf("*** Must be no more than %d.\n", imax);
643         } while ((i < imin) || (i > imax));
644         return (i);
645 }
646
647 /* 
648  * newprompt()  -  prompt for a string with no existing value
649  *                 (clears out string buffer first)
650  */
651 void newprompt(char *prompt, char *str, int len)
652 {
653         color(BRIGHT_MAGENTA);
654         scr_printf("%s", prompt);
655         color(DIM_MAGENTA);
656         getline(str, len);
657         color(DIM_WHITE);
658 }
659
660
661 int lkey(void)
662 {                               /* returns a lower case value */
663         int a;
664         a = inkey();
665         if (isupper(a))
666                 a = tolower(a);
667         return (a);
668 }
669
670 /*
671  * parse the citadel.rc file
672  */
673 void load_command_set(void)
674 {
675         FILE *ccfile;
676         char buf[1024];
677         struct citcmd *cptr;
678         struct citcmd *lastcmd = NULL;
679         int a, d;
680         int b = 0;
681
682
683         /* first, set up some defaults for non-required variables */
684
685         strcpy(editor_path, "");
686         strcpy(printcmd, "");
687         strcpy(rc_username, "");
688         strcpy(rc_password, "");
689         rc_floor_mode = 0;
690         rc_exp_beep = 1;
691         rc_allow_attachments = 0;
692         rc_remember_passwords = 0;
693         strcpy(rc_exp_cmd, "");
694         rc_display_message_numbers = 0;
695         rc_force_mail_prompts = 0;
696         rc_ansi_color = 0;
697         strcpy(rc_url_cmd, "");
698         rc_encrypt = RC_DEFAULT;
699 #ifdef HAVE_CURSES_H
700         rc_screen = RC_DEFAULT;
701 #endif
702         rc_alt_semantics = 0;
703
704         /* now try to open the citadel.rc file */
705
706         ccfile = NULL;
707         if (getenv("HOME") != NULL) {
708                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
709                 ccfile = fopen(buf, "r");
710         }
711         if (ccfile == NULL) {
712                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
713                 ccfile = fopen(buf, "r");
714         }
715         if (ccfile == NULL) {
716                 ccfile = fopen("/etc/citadel.rc", "r");
717         }
718         if (ccfile == NULL) {
719                 ccfile = fopen("./citadel.rc", "r");
720         }
721         if (ccfile == NULL) {
722                 perror("commands: cannot open citadel.rc");
723                 logoff(errno);
724         }
725         while (fgets(buf, sizeof buf, ccfile) != NULL) {
726                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
727                         buf[strlen(buf) - 1] = 0;
728
729                 if (!strncasecmp(buf, "encrypt=", 8)) {
730                         if (!strcasecmp(&buf[8], "yes"))
731                                 rc_encrypt = RC_YES;
732                         else if (!strcasecmp(&buf[8], "no"))
733                                 rc_encrypt = RC_NO;
734                         else if (!strcasecmp(&buf[8], "default"))
735                                 rc_encrypt = RC_DEFAULT;
736                 }
737
738 #ifdef HAVE_CURSES_H
739                 if (!strncasecmp(buf, "fullscreen=", 11)) {
740                         if (!strcasecmp(&buf[11], "yes"))
741                                 rc_screen = RC_YES;
742                         else if (!strcasecmp(&buf[11], "no"))
743                                 rc_screen = RC_NO;
744                 }
745 #endif
746
747                 if (!strncasecmp(buf, "editor=", 7))
748                         strcpy(editor_path, &buf[7]);
749
750                 if (!strncasecmp(buf, "printcmd=", 9))
751                         strcpy(printcmd, &buf[9]);
752
753                 if (!strncasecmp(buf, "expcmd=", 7))
754                         strcpy(rc_exp_cmd, &buf[7]);
755
756                 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
757                         have_xterm = (char) atoi(&buf[24]);
758
759                 if (!strncasecmp(buf, "use_floors=", 11)) {
760                         if (!strcasecmp(&buf[11], "yes"))
761                                 rc_floor_mode = RC_YES;
762                         if (!strcasecmp(&buf[11], "no"))
763                                 rc_floor_mode = RC_NO;
764                         if (!strcasecmp(&buf[11], "default"))
765                                 rc_floor_mode = RC_DEFAULT;
766                 }
767                 if (!strncasecmp(buf, "beep=", 5)) {
768                         rc_exp_beep = atoi(&buf[5]);
769                 }
770                 if (!strncasecmp(buf, "allow_attachments=", 18)) {
771                         rc_allow_attachments = atoi(&buf[18]);
772                 }
773                 if (!strncasecmp(buf, "remember_passwords=", 19)) {
774                         rc_remember_passwords = atoi(&buf[19]);
775                 }
776                 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
777                         rc_display_message_numbers = atoi(&buf[24]);
778                 }
779                 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
780                         rc_force_mail_prompts = atoi(&buf[19]);
781                 }
782                 if (!strncasecmp(buf, "ansi_color=", 11)) {
783                         if (!strncasecmp(&buf[11], "on", 2))
784                                 rc_ansi_color = 1;
785                         if (!strncasecmp(&buf[11], "auto", 4))
786                                 rc_ansi_color = 2;      /* autodetect */
787                         if (!strncasecmp(&buf[11], "user", 4))
788                                 rc_ansi_color = 3;      /* user config */
789                 }
790                 if (!strncasecmp(buf, "prompt_control=", 15)) {
791                         if (!strncasecmp(&buf[15], "on", 2))
792                                 rc_prompt_control = 1;
793                         if (!strncasecmp(&buf[15], "user", 4))
794                                 rc_prompt_control = 3;  /* user config */
795                 }
796                 if (!strncasecmp(buf, "username=", 9))
797                         strcpy(rc_username, &buf[9]);
798
799                 if (!strncasecmp(buf, "password=", 9))
800                         strcpy(rc_password, &buf[9]);
801
802                 if (!strncasecmp(buf, "urlcmd=", 7))
803                         strcpy(rc_url_cmd, &buf[7]);
804
805                 if (!strncasecmp(buf, "alternate_semantics=", 20)) {
806                         if (!strncasecmp(&buf[11], "yes", 3))
807                                 rc_alt_semantics = 1;
808                         if (!strncasecmp(&buf[11], "no", 2))
809                                 rc_alt_semantics = 0;
810                 }
811                 if (!strncasecmp(buf, "cmd=", 4)) {
812                         strcpy(buf, &buf[4]);
813
814                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
815
816                         cptr->c_cmdnum = atoi(buf);
817                         for (d = strlen(buf); d >= 0; --d)
818                                 if (buf[d] == ',')
819                                         b = d;
820                         strcpy(buf, &buf[b + 1]);
821
822                         cptr->c_axlevel = atoi(buf);
823                         for (d = strlen(buf); d >= 0; --d)
824                                 if (buf[d] == ',')
825                                         b = d;
826                         strcpy(buf, &buf[b + 1]);
827
828                         for (a = 0; a < 5; ++a)
829                                 cptr->c_keys[a][0] = 0;
830
831                         a = 0;
832                         b = 0;
833                         buf[strlen(buf) + 1] = 0;
834                         while (strlen(buf) > 0) {
835                                 b = strlen(buf);
836                                 for (d = strlen(buf); d >= 0; --d)
837                                         if (buf[d] == ',')
838                                                 b = d;
839                                 strncpy(cptr->c_keys[a], buf, b);
840                                 cptr->c_keys[a][b] = 0;
841                                 if (buf[b] == ',')
842                                         strcpy(buf, &buf[b + 1]);
843                                 else
844                                         strcpy(buf, "");
845                                 ++a;
846                         }
847
848                         cptr->next = NULL;
849                         if (cmdlist == NULL)
850                                 cmdlist = cptr;
851                         else
852                                 lastcmd->next = cptr;
853                         lastcmd = cptr;
854                 }
855         }
856         fclose(ccfile);
857 }
858
859
860
861 /*
862  * return the key associated with a command
863  */
864 char keycmd(char *cmdstr)
865 {
866         int a;
867
868         for (a = 0; a < strlen(cmdstr); ++a)
869                 if (cmdstr[a] == '&')
870                         return (tolower(cmdstr[a + 1]));
871         return (0);
872 }
873
874
875 /*
876  * Output the string from a key command without the ampersand
877  * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
878  */
879 char *cmd_expand(char *strbuf, int mode)
880 {
881         int a;
882         static char exp[64];
883         char buf[1024];
884
885         strcpy(exp, strbuf);
886
887         for (a = 0; a < strlen(exp); ++a) {
888                 if (strbuf[a] == '&') {
889
890                         if (mode == 0) {
891                                 strcpy(&exp[a], &exp[a + 1]);
892                         }
893                         if (mode == 1) {
894                                 exp[a] = '<';
895                                 strcpy(buf, &exp[a + 2]);
896                                 exp[a + 2] = '>';
897                                 exp[a + 3] = 0;
898                                 strcat(exp, buf);
899                         }
900                 }
901                 if (!strncmp(&exp[a], "^r", 2)) {
902                         strcpy(buf, exp);
903                         strcpy(&exp[a], room_name);
904                         strcat(exp, &buf[a + 2]);
905                 }
906                 if (!strncmp(&exp[a], "^c", 2)) {
907                         exp[a] = ',';
908                         strcpy(&exp[a + 1], &exp[a + 2]);
909                 }
910         }
911
912         return (exp);
913 }
914
915
916
917 /*
918  * Comparison function to determine if entered commands match a
919  * command loaded from the config file.
920  */
921 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
922 {
923         int a;
924         int cmdax;
925
926         cmdax = 0;
927         if (is_room_aide)
928                 cmdax = 1;
929         if (axlevel >= 6)
930                 cmdax = 2;
931
932         for (a = 0; a < ncomp; ++a) {
933                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
934                     || (cptr->c_axlevel > cmdax))
935                         return (0);
936         }
937         return (1);
938 }
939
940
941 /*
942  * This function returns 1 if a given command requires a string input
943  */
944 int requires_string(struct citcmd *cptr, int ncomp)
945 {
946         int a;
947         char buf[64];
948
949         strcpy(buf, cptr->c_keys[ncomp - 1]);
950         for (a = 0; a < strlen(buf); ++a) {
951                 if (buf[a] == ':')
952                         return (1);
953         }
954         return (0);
955 }
956
957
958 /*
959  * Input a command at the main prompt.
960  * This function returns an integer command number.  If the command prompts
961  * for a string then it is placed in the supplied buffer.
962  */
963 int getcmd(char *argbuf)
964 {
965         char cmdbuf[5];
966         int cmdspaces[5];
967         int cmdpos;
968         int ch;
969         int a;
970         int got;
971         int this_lazy_cmd;
972         struct citcmd *cptr;
973
974         /*
975          * Starting a new command now, so set sigcaught to 0.  This variable
976          * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
977          * been interrupted by a keypress.
978          */
979         sigcaught = 0;
980
981         /* Switch color support on or off if we're in user mode */
982         if (rc_ansi_color == 3) {
983                 if (userflags & US_COLOR)
984                         enable_color = 1;
985                 else
986                         enable_color = 0;
987         }
988         /* if we're running in idiot mode, display a cute little menu */
989         IFNEXPERT formout("mainmenu");
990
991         print_express();        /* print express messages if there are any */
992         strcpy(argbuf, "");
993         cmdpos = 0;
994         for (a = 0; a < 5; ++a)
995                 cmdbuf[a] = 0;
996         /* now the room prompt... */
997         ok_to_interrupt = 1;
998         color(BRIGHT_WHITE);
999         scr_printf("\n%s", room_name);
1000         color(DIM_WHITE);
1001         scr_printf("%c ", room_prompt(room_flags));
1002         scr_flush();
1003
1004         while (1) {
1005                 ch = inkey();
1006                 ok_to_interrupt = 0;
1007
1008                 /* Handle the backspace key, but only if there's something
1009                  * to backspace over...
1010                  */
1011                 if ((ch == 8) && (cmdpos > 0)) {
1012                         back(cmdspaces[cmdpos - 1] + 1);
1013                         cmdbuf[cmdpos] = 0;
1014                         --cmdpos;
1015                 }
1016                 /* Spacebar invokes "lazy traversal" commands */
1017                 if ((ch == 32) && (cmdpos == 0)) {
1018                         this_lazy_cmd = next_lazy_cmd;
1019                         if (this_lazy_cmd == 13)
1020                                 next_lazy_cmd = 5;
1021                         if (this_lazy_cmd == 5)
1022                                 next_lazy_cmd = 13;
1023                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1024                                 if (cptr->c_cmdnum == this_lazy_cmd) {
1025                                         for (a = 0; a < 5; ++a)
1026                                                 if (cptr->c_keys[a][0] != 0)
1027                                                         scr_printf("%s ", cmd_expand(
1028                                                                                         cptr->c_keys[a], 0));
1029                                         scr_printf("\n");
1030                                         return (this_lazy_cmd);
1031                                 }
1032                         }
1033                         scr_printf("\n");
1034                         return (this_lazy_cmd);
1035                 }
1036                 /* Otherwise, process the command */
1037                 cmdbuf[cmdpos] = tolower(ch);
1038
1039                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1040                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1041
1042                                 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1043                                 cmdspaces[cmdpos] = strlen(
1044                                     cmd_expand(cptr->c_keys[cmdpos], 0));
1045                                 if (cmdpos < 4)
1046                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
1047                                                 scr_putc(' ');
1048                                 ++cmdpos;
1049                         }
1050                 }
1051
1052                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1053                         if (cmdmatch(cmdbuf, cptr, 5)) {
1054                                 /* We've found our command. */
1055                                 if (requires_string(cptr, cmdpos)) {
1056                                         getline(argbuf, 32);
1057                                 } else {
1058                                         scr_printf("\n");
1059                                 }
1060
1061                                 /* If this command is one that changes rooms,
1062                                  * then the next lazy-command (space bar)
1063                                  * should be "read new" instead of "goto"
1064                                  */
1065                                 if ((cptr->c_cmdnum == 5)
1066                                     || (cptr->c_cmdnum == 6)
1067                                     || (cptr->c_cmdnum == 47)
1068                                     || (cptr->c_cmdnum == 52)
1069                                     || (cptr->c_cmdnum == 16)
1070                                     || (cptr->c_cmdnum == 20))
1071                                         next_lazy_cmd = 13;
1072
1073                                 return (cptr->c_cmdnum);
1074
1075                         }
1076                 }
1077
1078                 if (ch == '?') {
1079                         pprintf("\rOne of ...                         \n");
1080                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1081                                 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1082                                         for (a = 0; a < 5; ++a) {
1083                                                 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1084                                         }
1085                                         pprintf("\n");
1086                                 }
1087                         }
1088
1089                         pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1090                         got = 0;
1091                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1092                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1093                                         for (a = 0; a < cmdpos; ++a) {
1094                                                 pprintf("%s ",
1095                                                        cmd_expand(cptr->c_keys[a], 0));
1096                                         }
1097                                         got = 1;
1098                                 }
1099                         }
1100                 }
1101         }
1102
1103 }
1104
1105
1106
1107
1108
1109 /*
1110  * set tty modes.  commands are:
1111  * 
1112  * 01- set to bbs mode
1113  * 2 - save current settings for later restoral
1114  * 3 - restore saved settings
1115  */
1116 #ifdef HAVE_TERMIOS_H
1117 void sttybbs(int cmd)
1118 {                               /* SysV version of sttybbs() */
1119         struct termios live;
1120         static struct termios saved_settings;
1121         static int last_cmd = 0;
1122
1123         if (cmd == SB_LAST)
1124                 cmd = last_cmd;
1125         else
1126                 last_cmd = cmd;
1127
1128         if ((cmd == 0) || (cmd == 1)) {
1129                 tcgetattr(0, &live);
1130                 live.c_iflag = ISTRIP | IXON | IXANY;
1131                 live.c_oflag = OPOST | ONLCR;
1132                 live.c_lflag = ISIG | NOFLSH;
1133
1134                 live.c_cc[VINTR] = (-1);
1135                 live.c_cc[VQUIT] = (-1);
1136
1137 #ifdef hpux
1138                 live.c_cc[VMIN] = 0;
1139                 live.c_cc[VTIME] = 0;
1140 #endif
1141
1142                 /* do we even need this stuff anymore? */
1143                 /* live.c_line=0; */
1144                 live.c_cc[VERASE] = 8;
1145                 live.c_cc[VKILL] = 24;
1146                 live.c_cc[VEOF] = 1;
1147                 live.c_cc[VEOL] = 255;
1148                 live.c_cc[VEOL2] = 0;
1149                 live.c_cc[VSTART] = 0;
1150                 tcsetattr(0, TCSADRAIN, &live);
1151         }
1152         if (cmd == 2) {
1153                 tcgetattr(0, &saved_settings);
1154         }
1155         if (cmd == 3) {
1156                 tcsetattr(0, TCSADRAIN, &saved_settings);
1157         }
1158 }
1159 #else
1160 void sttybbs(int cmd)
1161 {                               /* BSD version of sttybbs() */
1162         struct sgttyb live;
1163         static struct sgttyb saved_settings;
1164         static int last_cmd = 0;
1165
1166         if (cmd == SB_LAST)
1167                 cmd = last_cmd;
1168         else
1169                 last_cmd = cmd;
1170
1171         if ((cmd == 0) || (cmd == 1)) {
1172                 gtty(0, &live);
1173                 live.sg_flags |= CBREAK;
1174                 live.sg_flags |= CRMOD;
1175                 live.sg_flags |= NL1;
1176                 live.sg_flags &= ~ECHO;
1177                 if (cmd == 1)
1178                         live.sg_flags |= NOFLSH;
1179                 stty(0, &live);
1180         }
1181         if (cmd == 2) {
1182                 gtty(0, &saved_settings);
1183         }
1184         if (cmd == 3) {
1185                 stty(0, &saved_settings);
1186         }
1187 }
1188 #endif
1189
1190
1191 /*
1192  * display_help()  -  help file viewer
1193  */
1194 void display_help(char *name)
1195 {
1196         formout(name);
1197 }
1198
1199
1200 /*
1201  * fmout()  -  Citadel text formatter and paginator
1202  */
1203 int fmout(
1204         int width,      /* screen width to use */
1205         FILE *fpin,     /* file to read from, or NULL to read from server */
1206         FILE *fpout,    /* File to write to, or NULL to write to screen */
1207         char pagin,     /* nonzero if we should use the paginator */
1208         int height,     /* screen height to use */
1209         int starting_lp,/* starting value for lines_printed, -1 for global */
1210         char subst)     /* nonzero if we should use hypertext mode */
1211 {
1212         int a, b, c, d, old;
1213         int real = (-1);
1214         char aaa[140];
1215         char buffer[512];
1216         int eof_flag = 0;
1217
1218         num_urls = 0;   /* Start with a clean slate of embedded URL's */
1219
1220         if (starting_lp >= 0) {
1221                 lines_printed = starting_lp;
1222         }
1223         strcpy(aaa, "");
1224         old = 255;
1225         strcpy(buffer, "");
1226         c = 1;                  /* c is the current pos */
1227
1228 FMTA:   while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1229                 if (fpin != NULL) {     /* read from file */
1230                         if (feof(fpin))
1231                                 eof_flag = 1;
1232                         if (eof_flag == 0) {
1233                                 a = getc(fpin);
1234                                 buffer[strlen(buffer) + 1] = 0;
1235                                 buffer[strlen(buffer)] = a;
1236                         }
1237                 } else {        /* read from server */
1238                         d = strlen(buffer);
1239                         serv_gets(&buffer[d]);
1240                         while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1241                                 buffer[strlen(buffer) - 1] = 0;
1242                         if (!strcmp(&buffer[d], "000")) {
1243                                 buffer[d] = 0;
1244                                 eof_flag = 1;
1245                                 while (isspace(buffer[strlen(buffer) - 1]))
1246                                         buffer[strlen(buffer) - 1] = 0;
1247                         }
1248                         d = strlen(buffer);
1249                         buffer[d] = 10;
1250                         buffer[d + 1] = 0;
1251                 }
1252         }
1253
1254         if ( (!strncasecmp(buffer, "http://", 7))
1255            || (!strncasecmp(buffer, "ftp://", 6)) ) {
1256                 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1257                 for (a=0; a<strlen(urls[num_urls]); ++a) {
1258                         b = urls[num_urls][a];
1259                         if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1260                            || (b==13) || (b==9) || (b=='\"') )
1261                                 urls[num_urls][a] = 0;
1262                 }
1263                 ++num_urls;
1264         }
1265
1266         buffer[strlen(buffer) + 1] = 0;
1267         a = buffer[0];
1268         strcpy(buffer, &buffer[1]);
1269
1270         old = real;
1271         real = a;
1272         if (a <= 0)
1273                 goto FMTEND;
1274
1275         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1276                 a = 32;
1277         if (((old == 13) || (old == 10)) && (isspace(real))) {
1278                 if (fpout) {
1279                         fprintf(fpout, "\n");
1280                 } else {
1281                         scr_printf("\n");
1282                         ++lines_printed;
1283                         lines_printed = checkpagin(lines_printed, pagin, height);
1284                 }
1285                 c = 1;
1286         }
1287         if (a > 126)
1288                 goto FMTA;
1289
1290         if (a > 32) {
1291                 if (((strlen(aaa) + c) > (width - 1)) && (strlen(aaa) > (width - 1))) {
1292                         if (fpout) {
1293                                 fprintf(fpout, "\n%s", aaa);
1294                         } else {
1295                                 scr_printf("\n%s", aaa);
1296                                 ++lines_printed;
1297                                 lines_printed = checkpagin(lines_printed, pagin, height);
1298                         }
1299                         c = strlen(aaa);
1300                         aaa[0] = 0;
1301                 }
1302                 b = strlen(aaa);
1303                 aaa[b] = a;
1304                 aaa[b + 1] = 0;
1305         }
1306         if (a == 32) {
1307                 if ((strlen(aaa) + c) > (width - 1)) {
1308                         c = 1;
1309                         if (fpout) {
1310                                 fprintf(fpout, "\n");
1311                         } else {
1312                                 scr_printf("\n");
1313                                 ++lines_printed;
1314                                 lines_printed = checkpagin(lines_printed, pagin, height);
1315                         }
1316                 }
1317                 if (fpout) {
1318                         fprintf(fpout, "%s ", aaa);
1319                 } else {
1320                         scr_printf("%s ", aaa);
1321                 }
1322                 ++c;
1323                 c = c + strlen(aaa);
1324                 strcpy(aaa, "");
1325                 goto FMTA;
1326         }
1327         if ((a == 13) || (a == 10)) {
1328                 if (fpout) {
1329                         fprintf(fpout, "%s\n", aaa);
1330                 } else {
1331                         scr_printf("%s\n", aaa);
1332                         ++lines_printed;
1333                         lines_printed = checkpagin(lines_printed, pagin, height);
1334                 }
1335                 c = 1;
1336                 if (sigcaught) goto OOPS;
1337                 strcpy(aaa, "");
1338                 goto FMTA;
1339         }
1340         goto FMTA;
1341
1342         /* keypress caught; drain the server */
1343 OOPS:   do {
1344                 serv_gets(aaa);
1345         } while (strcmp(aaa, "000"));
1346
1347 FMTEND:
1348         if (fpout) {
1349                 fprintf(fpout, "\n");
1350         } else {
1351                 scr_printf("\n");
1352                 ++lines_printed;
1353                 lines_printed = checkpagin(lines_printed, pagin, height);
1354         }
1355         return (sigcaught);
1356 }
1357
1358
1359 /*
1360  * support ANSI color if defined
1361  */
1362 void color(int colornum)
1363 {
1364         static int is_bold = 0;
1365         static int hold_color, current_color;
1366
1367         if (colornum == COLOR_PUSH) {
1368                 hold_color = current_color;
1369                 return;
1370         }
1371
1372         if (colornum == COLOR_POP) {
1373                 color(hold_color);
1374                 return;
1375         }
1376
1377         current_color = colornum;
1378         if (enable_color) {
1379 #ifdef HAVE_CURSES_H
1380                 if (scr_color(colornum))
1381                         return;
1382 #endif
1383                 /* When switching to dim white, actually output an 'original
1384                  * pair' sequence -- this looks better on black-on-white
1385                  * terminals.
1386                  */
1387                 if (colornum == DIM_WHITE)
1388                         printf("\033[39;49m");
1389                 else
1390                         printf("\033[3%d;40m", (colornum & 7));
1391
1392                 if ((colornum >= 8) && (is_bold == 0)) {
1393                         printf("\033[1m");
1394                         is_bold = 1;
1395                 } else if ((colornum < 8) && (is_bold == 1)) {
1396                         printf("\033[0m");
1397                         is_bold = 0;
1398                 }
1399                 scr_flush();
1400         }
1401 }
1402
1403 void cls(int colornum)
1404 {
1405         if (enable_color) {
1406                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1407                 scr_flush();
1408         }
1409 }
1410
1411
1412 /*
1413  * Detect whether ANSI color is available (answerback)
1414  */
1415 void send_ansi_detect(void)
1416 {
1417         if (rc_ansi_color == 2) {
1418                 printf("\033[c");
1419                 scr_flush();
1420                 time(&AnsiDetect);
1421         }
1422 }
1423
1424 void look_for_ansi(void)
1425 {
1426         fd_set rfds;
1427         struct timeval tv;
1428         char abuf[512];
1429         time_t now;
1430         int a;
1431
1432         if (rc_ansi_color == 0) {
1433                 enable_color = 0;
1434         } else if (rc_ansi_color == 1) {
1435                 enable_color = 1;
1436         } else if (rc_ansi_color == 2) {
1437
1438                 /* otherwise, do the auto-detect */
1439
1440                 strcpy(abuf, "");
1441
1442                 time(&now);
1443                 if ((now - AnsiDetect) < 2)
1444                         sleep(1);
1445
1446                 do {
1447                         FD_ZERO(&rfds);
1448                         FD_SET(0, &rfds);
1449                         tv.tv_sec = 0;
1450                         tv.tv_usec = 1;
1451
1452                         select(1, &rfds, NULL, NULL, &tv);
1453                         if (FD_ISSET(0, &rfds)) {
1454                                 abuf[strlen(abuf) + 1] = 0;
1455                                 read(0, &abuf[strlen(abuf)], 1);
1456                         }
1457                 } while (FD_ISSET(0, &rfds));
1458
1459                 for (a = 0; a < strlen(abuf); ++a) {
1460                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1461                             && (abuf[a + 2] == '?')) {
1462                                 enable_color = 1;
1463                         }
1464                 }
1465         }
1466 }
1467
1468
1469 /*
1470  * Display key options (highlight hotkeys inside angle brackets)
1471  */
1472 void keyopt(char *buf) {
1473         int i;
1474
1475         color(DIM_WHITE);
1476         for (i=0; i<strlen(buf); ++i) {
1477                 if (buf[i]=='<') {
1478                         scr_putc(buf[i]);
1479                         color(BRIGHT_MAGENTA);
1480                 } else {
1481                         if (buf[i]=='>') {
1482                                 color(DIM_WHITE);
1483                         }
1484                         scr_putc(buf[i]);
1485                 }
1486         }
1487         color(DIM_WHITE);
1488 }
1489
1490
1491
1492 /*
1493  * Present a key-menu line choice type of thing
1494  */
1495 char keymenu(char *menuprompt, char *menustring) {
1496         int i, c, a;
1497         int choices;
1498         int do_prompt = 0;
1499         char buf[1024];
1500         int ch;
1501         int display_prompt = 1;
1502
1503         choices = num_tokens(menustring, '|');
1504
1505         if (menuprompt != NULL) do_prompt = 1;
1506         if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1507
1508         while (1) {
1509                 if (display_prompt) {
1510                         if (do_prompt) {
1511                                 scr_printf("%s ", menuprompt);
1512                         } 
1513                         else {
1514                                 for (i=0; i<choices; ++i) {
1515                                         extract(buf, menustring, i);
1516                                         keyopt(buf);
1517                                         scr_printf(" ");
1518                                 }
1519                         }
1520                         scr_printf(" -> ");
1521                         display_prompt = 0;
1522                 }
1523                 ch = lkey();
1524         
1525                 if ( (do_prompt) && (ch=='?') ) {
1526                         scr_printf("\rOne of...                               ");
1527                         scr_printf("                                      \n");
1528                         for (i=0; i<choices; ++i) {
1529                                 extract(buf, menustring, i);
1530                                 scr_printf("   ");
1531                                 keyopt(buf);
1532                                 scr_printf("\n");
1533                         }
1534                         scr_printf("\n");
1535                         display_prompt = 1;
1536                 }
1537
1538                 for (i=0; i<choices; ++i) {
1539                         extract(buf, menustring, i);
1540                         for (c=1; c<strlen(buf); ++c) {
1541                                 if ( (ch == tolower(buf[c]))
1542                                    && (buf[c-1]=='<')
1543                                    && (buf[c+1]=='>') ) {
1544                                         for (a=0; a<strlen(buf); ++a) {
1545                                                 if ( (a!=(c-1)) && (a!=(c+1))) {
1546                                                         scr_putc(buf[a]);
1547                                                 }
1548                                         }
1549                                         scr_printf("\n\n");
1550                                         return ch;
1551                                 }
1552                         }
1553                 }
1554         }
1555 }