]> code.citadel.org Git - citadel.git/blob - citadel/commands.c
- fixes for building without OpenSSL
[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         vsnprintf(buf, sizeof(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         fd_set rfds;
437         struct timeval tv;
438         time_t start_time;
439
440         scr_flush();
441         lines_printed = 0;
442         time(&start_time);
443
444         do {
445                 /* This loop waits for keyboard input.  If the keepalive
446                  * timer expires, it sends a keepalive to the server if
447                  * necessary and then waits again.
448                  */
449                 do {
450                         do_keepalive();
451
452                         FD_ZERO(&rfds);
453                         FD_SET(0, &rfds);
454                         tv.tv_sec = S_KEEPALIVE;
455                         tv.tv_usec = 0;
456
457                         select(1, &rfds, NULL, NULL, &tv);
458                 } while (!FD_ISSET(0, &rfds));
459
460                 /* At this point, there's input, so fetch it.
461                  * (There's a hole in the bucket...)
462                  */
463                 a = scr_getc();
464                 if (a == 127)
465                         a = 8;
466                 if (a > 126)
467                         a = 0;
468                 if (a == 13)
469                         a = 10;
470                 if (((a != 4) && (a != 10) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
471                     && ((a < 32) || (a > 126)))
472                         a = 0;
473         } while (a == 0);
474         return (a);
475 }
476
477
478 int yesno(void)
479 {                               /* Returns 1 for yes, 0 for no */
480         int a;
481         while (1) {
482                 a = inkey();
483                 a = tolower(a);
484                 if (a == 'y') {
485                         scr_printf("Yes\n");
486                         return (1);
487                 }
488                 if (a == 'n') {
489                         scr_printf("No\n");
490                         return (0);
491                 }
492         }
493 }
494
495 /* Returns 1 for yes, 0 for no, arg is default value */
496 int yesno_d(int d)
497 {
498         int a;
499         while (1) {
500                 a = inkey();
501                 a = tolower(a);
502                 if (a == 10)
503                         a = (d ? 'y' : 'n');
504                 if (a == 'y') {
505                         scr_printf("Yes\n");
506                         return (1);
507                 }
508                 if (a == 'n') {
509                         scr_printf("No\n");
510                         return (0);
511                 }
512         }
513 }
514
515
516
517
518 /* Gets a line from the terminal */
519 /* string == Pointer to string buffer */
520 /* lim == Maximum length - if negative, no-show */
521 void getline(char *string, int lim) 
522 {
523         int a, b;
524         char flag = 0;
525
526         if (lim < 0) {
527                 lim = (0 - lim);
528                 flag = 1;
529         }
530         strcpy(string, "");
531         gl_string = string;
532         async_ka_start();
533       GLA:a = inkey();
534         a = (a & 127);
535         if ((a == 8) && (strlen(string) == 0))
536                 goto GLA;
537         if ((a != 10) && (a != 8) && (strlen(string) == lim))
538                 goto GLA;
539         if ((a == 8) && (string[0] != 0)) {
540                 string[strlen(string) - 1] = 0;
541                 scr_putc(8);
542                 scr_putc(32);
543                 scr_putc(8);
544                 goto GLA;
545         }
546         if ((a == 10)) {
547                 scr_putc(10);
548                 async_ka_end();
549                 return;
550         }
551         if (a < 32)
552                 a = '.';
553         b = strlen(string);
554         string[b] = a;
555         string[b + 1] = 0;
556         if (flag == 0)
557                 scr_putc(a);
558         if (flag == 1)
559                 scr_putc('*');
560         goto GLA;
561 }
562
563
564 /*
565  * strprompt()  -  prompt for a string, print the existing value and
566  *                 allow the user to press return to keep it...
567  */
568 void strprompt(char *prompt, char *str, int len)
569 {
570         int i;
571         char buf[128];
572
573         print_express();
574         color(DIM_WHITE);
575         scr_printf("%s ", prompt);
576         color(DIM_MAGENTA);
577         scr_printf("[");
578         color(BRIGHT_MAGENTA);
579
580         if (len >= 0) {
581                 scr_printf("%s", str);
582         }
583         else {
584                 for (i=0; i<strlen(str); ++i) {
585                         scr_printf("*");
586                 }
587         }
588
589         color(DIM_MAGENTA);
590         scr_printf("]");
591         color(DIM_WHITE);
592         scr_printf(": ");
593         color(BRIGHT_CYAN);
594         getline(buf, len);
595         if (buf[0] != 0)
596                 strcpy(str, buf);
597         color(DIM_WHITE);
598 }
599
600 /*
601  * boolprompt()  -  prompt for a yes/no, print the existing value and
602  *                  allow the user to press return to keep it...
603  */
604 int boolprompt(char *prompt, int prev_val)
605 {
606         int r;
607
608         color(DIM_WHITE);
609         scr_printf("%s ", prompt);
610         color(DIM_MAGENTA);
611         scr_printf(" [");
612         color(BRIGHT_MAGENTA);
613         scr_printf("%s", (prev_val ? "Yes" : "No"));
614         color(DIM_MAGENTA);
615         scr_printf("]: ");
616         color(BRIGHT_CYAN);
617         r = (yesno_d(prev_val));
618         color(DIM_WHITE);
619         return r;
620 }
621
622 /* 
623  * intprompt()  -  like strprompt(), except for an integer
624  *                 (note that it RETURNS the new value!)
625  */
626 int intprompt(char *prompt, int ival, int imin, int imax)
627 {
628         char buf[16];
629         int i;
630         int p;
631
632         do {
633                 i = ival;
634                 snprintf(buf, sizeof buf, "%d", i);
635                 strprompt(prompt, buf, 15);
636                 i = atoi(buf);
637                 for (p=0; p<strlen(buf); ++p) {
638                         if ( (!isdigit(buf[p]))
639                            && ( (buf[p]!='-') || (p!=0) )  )
640                                 i = imin - 1;
641                 }
642                 if (i < imin)
643                         scr_printf("*** Must be no less than %d.\n", imin);
644                 if (i > imax)
645                         scr_printf("*** Must be no more than %d.\n", imax);
646         } while ((i < imin) || (i > imax));
647         return (i);
648 }
649
650 /* 
651  * newprompt()  -  prompt for a string with no existing value
652  *                 (clears out string buffer first)
653  */
654 void newprompt(char *prompt, char *str, int len)
655 {
656         color(BRIGHT_MAGENTA);
657         scr_printf("%s", prompt);
658         color(DIM_MAGENTA);
659         getline(str, len);
660         color(DIM_WHITE);
661 }
662
663
664 int lkey(void)
665 {                               /* returns a lower case value */
666         int a;
667         a = inkey();
668         if (isupper(a))
669                 a = tolower(a);
670         return (a);
671 }
672
673 /*
674  * parse the citadel.rc file
675  */
676 void load_command_set(void)
677 {
678         FILE *ccfile;
679         char buf[1024];
680         struct citcmd *cptr;
681         struct citcmd *lastcmd = NULL;
682         int a, d;
683         int b = 0;
684
685
686         /* first, set up some defaults for non-required variables */
687
688         strcpy(editor_path, "");
689         strcpy(printcmd, "");
690         strcpy(rc_username, "");
691         strcpy(rc_password, "");
692         rc_floor_mode = 0;
693         rc_exp_beep = 1;
694         rc_allow_attachments = 0;
695         rc_remember_passwords = 0;
696         strcpy(rc_exp_cmd, "");
697         rc_display_message_numbers = 0;
698         rc_force_mail_prompts = 0;
699         rc_ansi_color = 0;
700         strcpy(rc_url_cmd, "");
701 #ifdef HAVE_OPENSSL
702         rc_encrypt = RC_DEFAULT;
703 #endif
704 #ifdef HAVE_CURSES_H
705         rc_screen = RC_DEFAULT;
706 #endif
707         rc_alt_semantics = 0;
708
709         /* now try to open the citadel.rc file */
710
711         ccfile = NULL;
712         if (getenv("HOME") != NULL) {
713                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
714                 ccfile = fopen(buf, "r");
715         }
716         if (ccfile == NULL) {
717                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
718                 ccfile = fopen(buf, "r");
719         }
720         if (ccfile == NULL) {
721                 ccfile = fopen("/etc/citadel.rc", "r");
722         }
723         if (ccfile == NULL) {
724                 ccfile = fopen("./citadel.rc", "r");
725         }
726         if (ccfile == NULL) {
727                 perror("commands: cannot open citadel.rc");
728                 logoff(errno);
729         }
730         while (fgets(buf, sizeof buf, ccfile) != NULL) {
731                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
732                         buf[strlen(buf) - 1] = 0;
733
734                 if (!strncasecmp(buf, "encrypt=", 8)) {
735                         if (!strcasecmp(&buf[8], "yes")) {
736 #ifdef HAVE_OPENSSL
737                                 rc_encrypt = RC_YES;
738 #else
739                                 fprintf(stderr, "citadel.rc requires encryption support but citadel is not compiled with OpenSSL");
740                                 logoff(1);
741 #endif
742                         }
743 #ifdef HAVE_OPENSSL
744                         else if (!strcasecmp(&buf[8], "no")) {
745                                 rc_encrypt = RC_NO;
746                         }
747                         else if (!strcasecmp(&buf[8], "default")) {
748                                 rc_encrypt = RC_DEFAULT;
749                         }
750 #endif
751                 }
752
753 #ifdef HAVE_CURSES_H
754                 if (!strncasecmp(buf, "fullscreen=", 11)) {
755                         if (!strcasecmp(&buf[11], "yes"))
756                                 rc_screen = RC_YES;
757                         else if (!strcasecmp(&buf[11], "no"))
758                                 rc_screen = RC_NO;
759                 }
760 #endif
761
762                 if (!strncasecmp(buf, "editor=", 7))
763                         strcpy(editor_path, &buf[7]);
764
765                 if (!strncasecmp(buf, "printcmd=", 9))
766                         strcpy(printcmd, &buf[9]);
767
768                 if (!strncasecmp(buf, "expcmd=", 7))
769                         strcpy(rc_exp_cmd, &buf[7]);
770
771                 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
772                         have_xterm = (char) atoi(&buf[24]);
773
774                 if (!strncasecmp(buf, "use_floors=", 11)) {
775                         if (!strcasecmp(&buf[11], "yes"))
776                                 rc_floor_mode = RC_YES;
777                         if (!strcasecmp(&buf[11], "no"))
778                                 rc_floor_mode = RC_NO;
779                         if (!strcasecmp(&buf[11], "default"))
780                                 rc_floor_mode = RC_DEFAULT;
781                 }
782                 if (!strncasecmp(buf, "beep=", 5)) {
783                         rc_exp_beep = atoi(&buf[5]);
784                 }
785                 if (!strncasecmp(buf, "allow_attachments=", 18)) {
786                         rc_allow_attachments = atoi(&buf[18]);
787                 }
788                 if (!strncasecmp(buf, "remember_passwords=", 19)) {
789                         rc_remember_passwords = atoi(&buf[19]);
790                 }
791                 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
792                         rc_display_message_numbers = atoi(&buf[24]);
793                 }
794                 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
795                         rc_force_mail_prompts = atoi(&buf[19]);
796                 }
797                 if (!strncasecmp(buf, "ansi_color=", 11)) {
798                         if (!strncasecmp(&buf[11], "on", 2))
799                                 rc_ansi_color = 1;
800                         if (!strncasecmp(&buf[11], "auto", 4))
801                                 rc_ansi_color = 2;      /* autodetect */
802                         if (!strncasecmp(&buf[11], "user", 4))
803                                 rc_ansi_color = 3;      /* user config */
804                 }
805                 if (!strncasecmp(buf, "prompt_control=", 15)) {
806                         if (!strncasecmp(&buf[15], "on", 2))
807                                 rc_prompt_control = 1;
808                         if (!strncasecmp(&buf[15], "user", 4))
809                                 rc_prompt_control = 3;  /* user config */
810                 }
811                 if (!strncasecmp(buf, "username=", 9))
812                         strcpy(rc_username, &buf[9]);
813
814                 if (!strncasecmp(buf, "password=", 9))
815                         strcpy(rc_password, &buf[9]);
816
817                 if (!strncasecmp(buf, "urlcmd=", 7))
818                         strcpy(rc_url_cmd, &buf[7]);
819
820                 if (!strncasecmp(buf, "alternate_semantics=", 20)) {
821                         if (!strncasecmp(&buf[11], "yes", 3))
822                                 rc_alt_semantics = 1;
823                         if (!strncasecmp(&buf[11], "no", 2))
824                                 rc_alt_semantics = 0;
825                 }
826                 if (!strncasecmp(buf, "cmd=", 4)) {
827                         strcpy(buf, &buf[4]);
828
829                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
830
831                         cptr->c_cmdnum = atoi(buf);
832                         for (d = strlen(buf); d >= 0; --d)
833                                 if (buf[d] == ',')
834                                         b = d;
835                         strcpy(buf, &buf[b + 1]);
836
837                         cptr->c_axlevel = atoi(buf);
838                         for (d = strlen(buf); d >= 0; --d)
839                                 if (buf[d] == ',')
840                                         b = d;
841                         strcpy(buf, &buf[b + 1]);
842
843                         for (a = 0; a < 5; ++a)
844                                 cptr->c_keys[a][0] = 0;
845
846                         a = 0;
847                         b = 0;
848                         buf[strlen(buf) + 1] = 0;
849                         while (strlen(buf) > 0) {
850                                 b = strlen(buf);
851                                 for (d = strlen(buf); d >= 0; --d)
852                                         if (buf[d] == ',')
853                                                 b = d;
854                                 strncpy(cptr->c_keys[a], buf, b);
855                                 cptr->c_keys[a][b] = 0;
856                                 if (buf[b] == ',')
857                                         strcpy(buf, &buf[b + 1]);
858                                 else
859                                         strcpy(buf, "");
860                                 ++a;
861                         }
862
863                         cptr->next = NULL;
864                         if (cmdlist == NULL)
865                                 cmdlist = cptr;
866                         else
867                                 lastcmd->next = cptr;
868                         lastcmd = cptr;
869                 }
870         }
871         fclose(ccfile);
872 }
873
874
875
876 /*
877  * return the key associated with a command
878  */
879 char keycmd(char *cmdstr)
880 {
881         int a;
882
883         for (a = 0; a < strlen(cmdstr); ++a)
884                 if (cmdstr[a] == '&')
885                         return (tolower(cmdstr[a + 1]));
886         return (0);
887 }
888
889
890 /*
891  * Output the string from a key command without the ampersand
892  * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
893  */
894 char *cmd_expand(char *strbuf, int mode)
895 {
896         int a;
897         static char exp[64];
898         char buf[1024];
899
900         strcpy(exp, strbuf);
901
902         for (a = 0; a < strlen(exp); ++a) {
903                 if (strbuf[a] == '&') {
904
905                         if (mode == 0) {
906                                 strcpy(&exp[a], &exp[a + 1]);
907                         }
908                         if (mode == 1) {
909                                 exp[a] = '<';
910                                 strcpy(buf, &exp[a + 2]);
911                                 exp[a + 2] = '>';
912                                 exp[a + 3] = 0;
913                                 strcat(exp, buf);
914                         }
915                 }
916                 if (!strncmp(&exp[a], "^r", 2)) {
917                         strcpy(buf, exp);
918                         strcpy(&exp[a], room_name);
919                         strcat(exp, &buf[a + 2]);
920                 }
921                 if (!strncmp(&exp[a], "^c", 2)) {
922                         exp[a] = ',';
923                         strcpy(&exp[a + 1], &exp[a + 2]);
924                 }
925         }
926
927         return (exp);
928 }
929
930
931
932 /*
933  * Comparison function to determine if entered commands match a
934  * command loaded from the config file.
935  */
936 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
937 {
938         int a;
939         int cmdax;
940
941         cmdax = 0;
942         if (is_room_aide)
943                 cmdax = 1;
944         if (axlevel >= 6)
945                 cmdax = 2;
946
947         for (a = 0; a < ncomp; ++a) {
948                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
949                     || (cptr->c_axlevel > cmdax))
950                         return (0);
951         }
952         return (1);
953 }
954
955
956 /*
957  * This function returns 1 if a given command requires a string input
958  */
959 int requires_string(struct citcmd *cptr, int ncomp)
960 {
961         int a;
962         char buf[64];
963
964         strcpy(buf, cptr->c_keys[ncomp - 1]);
965         for (a = 0; a < strlen(buf); ++a) {
966                 if (buf[a] == ':')
967                         return (1);
968         }
969         return (0);
970 }
971
972
973 /*
974  * Input a command at the main prompt.
975  * This function returns an integer command number.  If the command prompts
976  * for a string then it is placed in the supplied buffer.
977  */
978 int getcmd(char *argbuf)
979 {
980         char cmdbuf[5];
981         int cmdspaces[5];
982         int cmdpos;
983         int ch;
984         int a;
985         int got;
986         int this_lazy_cmd;
987         struct citcmd *cptr;
988
989         /*
990          * Starting a new command now, so set sigcaught to 0.  This variable
991          * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
992          * been interrupted by a keypress.
993          */
994         sigcaught = 0;
995
996         /* Switch color support on or off if we're in user mode */
997         if (rc_ansi_color == 3) {
998                 if (userflags & US_COLOR)
999                         enable_color = 1;
1000                 else
1001                         enable_color = 0;
1002         }
1003         /* if we're running in idiot mode, display a cute little menu */
1004         IFNEXPERT formout("mainmenu");
1005
1006         print_express();        /* print express messages if there are any */
1007         strcpy(argbuf, "");
1008         cmdpos = 0;
1009         for (a = 0; a < 5; ++a)
1010                 cmdbuf[a] = 0;
1011         /* now the room prompt... */
1012         ok_to_interrupt = 1;
1013         color(BRIGHT_WHITE);
1014         scr_printf("\n%s", room_name);
1015         color(DIM_WHITE);
1016         scr_printf("%c ", room_prompt(room_flags));
1017         scr_flush();
1018
1019         while (1) {
1020                 ch = inkey();
1021                 ok_to_interrupt = 0;
1022
1023                 /* Handle the backspace key, but only if there's something
1024                  * to backspace over...
1025                  */
1026                 if ((ch == 8) && (cmdpos > 0)) {
1027                         back(cmdspaces[cmdpos - 1] + 1);
1028                         cmdbuf[cmdpos] = 0;
1029                         --cmdpos;
1030                 }
1031                 /* Spacebar invokes "lazy traversal" commands */
1032                 if ((ch == 32) && (cmdpos == 0)) {
1033                         this_lazy_cmd = next_lazy_cmd;
1034                         if (this_lazy_cmd == 13)
1035                                 next_lazy_cmd = 5;
1036                         if (this_lazy_cmd == 5)
1037                                 next_lazy_cmd = 13;
1038                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1039                                 if (cptr->c_cmdnum == this_lazy_cmd) {
1040                                         for (a = 0; a < 5; ++a)
1041                                                 if (cptr->c_keys[a][0] != 0)
1042                                                         scr_printf("%s ", cmd_expand(
1043                                                                                         cptr->c_keys[a], 0));
1044                                         scr_printf("\n");
1045                                         return (this_lazy_cmd);
1046                                 }
1047                         }
1048                         scr_printf("\n");
1049                         return (this_lazy_cmd);
1050                 }
1051                 /* Otherwise, process the command */
1052                 cmdbuf[cmdpos] = tolower(ch);
1053
1054                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1055                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1056
1057                                 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1058                                 cmdspaces[cmdpos] = strlen(
1059                                     cmd_expand(cptr->c_keys[cmdpos], 0));
1060                                 if (cmdpos < 4)
1061                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
1062                                                 scr_putc(' ');
1063                                 ++cmdpos;
1064                         }
1065                 }
1066
1067                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1068                         if (cmdmatch(cmdbuf, cptr, 5)) {
1069                                 /* We've found our command. */
1070                                 if (requires_string(cptr, cmdpos)) {
1071                                         getline(argbuf, 32);
1072                                 } else {
1073                                         scr_printf("\n");
1074                                 }
1075
1076                                 /* If this command is one that changes rooms,
1077                                  * then the next lazy-command (space bar)
1078                                  * should be "read new" instead of "goto"
1079                                  */
1080                                 if ((cptr->c_cmdnum == 5)
1081                                     || (cptr->c_cmdnum == 6)
1082                                     || (cptr->c_cmdnum == 47)
1083                                     || (cptr->c_cmdnum == 52)
1084                                     || (cptr->c_cmdnum == 16)
1085                                     || (cptr->c_cmdnum == 20))
1086                                         next_lazy_cmd = 13;
1087
1088                                 return (cptr->c_cmdnum);
1089
1090                         }
1091                 }
1092
1093                 if (ch == '?') {
1094                         pprintf("\rOne of ...                         \n");
1095                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1096                                 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1097                                         for (a = 0; a < 5; ++a) {
1098                                                 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1099                                         }
1100                                         pprintf("\n");
1101                                 }
1102                         }
1103
1104                         pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1105                         got = 0;
1106                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1107                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1108                                         for (a = 0; a < cmdpos; ++a) {
1109                                                 pprintf("%s ",
1110                                                        cmd_expand(cptr->c_keys[a], 0));
1111                                         }
1112                                         got = 1;
1113                                 }
1114                         }
1115                 }
1116         }
1117
1118 }
1119
1120
1121
1122
1123
1124 /*
1125  * set tty modes.  commands are:
1126  * 
1127  * 01- set to bbs mode
1128  * 2 - save current settings for later restoral
1129  * 3 - restore saved settings
1130  */
1131 #ifdef HAVE_TERMIOS_H
1132 void sttybbs(int cmd)
1133 {                               /* SysV version of sttybbs() */
1134         struct termios live;
1135         static struct termios saved_settings;
1136         static int last_cmd = 0;
1137
1138         if (cmd == SB_LAST)
1139                 cmd = last_cmd;
1140         else
1141                 last_cmd = cmd;
1142
1143         if ((cmd == 0) || (cmd == 1)) {
1144                 tcgetattr(0, &live);
1145                 live.c_iflag = ISTRIP | IXON | IXANY;
1146                 live.c_oflag = OPOST | ONLCR;
1147                 live.c_lflag = ISIG | NOFLSH;
1148
1149                 live.c_cc[VINTR] = (-1);
1150                 live.c_cc[VQUIT] = (-1);
1151
1152 #ifdef hpux
1153                 live.c_cc[VMIN] = 0;
1154                 live.c_cc[VTIME] = 0;
1155 #endif
1156
1157                 /* do we even need this stuff anymore? */
1158                 /* live.c_line=0; */
1159                 live.c_cc[VERASE] = 8;
1160                 live.c_cc[VKILL] = 24;
1161                 live.c_cc[VEOF] = 1;
1162                 live.c_cc[VEOL] = 255;
1163                 live.c_cc[VEOL2] = 0;
1164                 live.c_cc[VSTART] = 0;
1165                 tcsetattr(0, TCSADRAIN, &live);
1166         }
1167         if (cmd == 2) {
1168                 tcgetattr(0, &saved_settings);
1169         }
1170         if (cmd == 3) {
1171                 tcsetattr(0, TCSADRAIN, &saved_settings);
1172         }
1173 }
1174 #else
1175 void sttybbs(int cmd)
1176 {                               /* BSD version of sttybbs() */
1177         struct sgttyb live;
1178         static struct sgttyb saved_settings;
1179         static int last_cmd = 0;
1180
1181         if (cmd == SB_LAST)
1182                 cmd = last_cmd;
1183         else
1184                 last_cmd = cmd;
1185
1186         if ((cmd == 0) || (cmd == 1)) {
1187                 gtty(0, &live);
1188                 live.sg_flags |= CBREAK;
1189                 live.sg_flags |= CRMOD;
1190                 live.sg_flags |= NL1;
1191                 live.sg_flags &= ~ECHO;
1192                 if (cmd == 1)
1193                         live.sg_flags |= NOFLSH;
1194                 stty(0, &live);
1195         }
1196         if (cmd == 2) {
1197                 gtty(0, &saved_settings);
1198         }
1199         if (cmd == 3) {
1200                 stty(0, &saved_settings);
1201         }
1202 }
1203 #endif
1204
1205
1206 /*
1207  * display_help()  -  help file viewer
1208  */
1209 void display_help(char *name)
1210 {
1211         formout(name);
1212 }
1213
1214
1215 /*
1216  * fmout()  -  Citadel text formatter and paginator
1217  */
1218 int fmout(
1219         int width,      /* screen width to use */
1220         FILE *fpin,     /* file to read from, or NULL to read from server */
1221         FILE *fpout,    /* File to write to, or NULL to write to screen */
1222         char pagin,     /* nonzero if we should use the paginator */
1223         int height,     /* screen height to use */
1224         int starting_lp,/* starting value for lines_printed, -1 for global */
1225         char subst)     /* nonzero if we should use hypertext mode */
1226 {
1227         int a, b, c, d, old;
1228         int real = (-1);
1229         char aaa[140];
1230         char buffer[512];
1231         int eof_flag = 0;
1232
1233         num_urls = 0;   /* Start with a clean slate of embedded URL's */
1234
1235         if (starting_lp >= 0) {
1236                 lines_printed = starting_lp;
1237         }
1238         strcpy(aaa, "");
1239         old = 255;
1240         strcpy(buffer, "");
1241         c = 1;                  /* c is the current pos */
1242
1243 FMTA:   while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1244                 if (fpin != NULL) {     /* read from file */
1245                         if (feof(fpin))
1246                                 eof_flag = 1;
1247                         if (eof_flag == 0) {
1248                                 a = getc(fpin);
1249                                 buffer[strlen(buffer) + 1] = 0;
1250                                 buffer[strlen(buffer)] = a;
1251                         }
1252                 } else {        /* read from server */
1253                         d = strlen(buffer);
1254                         serv_gets(&buffer[d]);
1255                         while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1256                                 buffer[strlen(buffer) - 1] = 0;
1257                         if (!strcmp(&buffer[d], "000")) {
1258                                 buffer[d] = 0;
1259                                 eof_flag = 1;
1260                                 while (isspace(buffer[strlen(buffer) - 1]))
1261                                         buffer[strlen(buffer) - 1] = 0;
1262                         }
1263                         d = strlen(buffer);
1264                         buffer[d] = 10;
1265                         buffer[d + 1] = 0;
1266                 }
1267         }
1268
1269         if ( (!strncasecmp(buffer, "http://", 7))
1270            || (!strncasecmp(buffer, "ftp://", 6)) ) {
1271                 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1272                 for (a=0; a<strlen(urls[num_urls]); ++a) {
1273                         b = urls[num_urls][a];
1274                         if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1275                            || (b==13) || (b==9) || (b=='\"') )
1276                                 urls[num_urls][a] = 0;
1277                 }
1278                 ++num_urls;
1279         }
1280
1281         buffer[strlen(buffer) + 1] = 0;
1282         a = buffer[0];
1283         strcpy(buffer, &buffer[1]);
1284
1285         old = real;
1286         real = a;
1287         if (a <= 0)
1288                 goto FMTEND;
1289
1290         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1291                 a = 32;
1292         if (((old == 13) || (old == 10)) && (isspace(real))) {
1293                 if (fpout) {
1294                         fprintf(fpout, "\n");
1295                 } else {
1296                         scr_printf("\n");
1297                         ++lines_printed;
1298                         lines_printed = checkpagin(lines_printed, pagin, height);
1299                 }
1300                 c = 1;
1301         }
1302         if (a > 126)
1303                 goto FMTA;
1304
1305         if (a > 32) {
1306                 if (((strlen(aaa) + c) > (width - 1)) && (strlen(aaa) > (width - 1))) {
1307                         if (fpout) {
1308                                 fprintf(fpout, "\n%s", aaa);
1309                         } else {
1310                                 scr_printf("\n%s", aaa);
1311                                 ++lines_printed;
1312                                 lines_printed = checkpagin(lines_printed, pagin, height);
1313                         }
1314                         c = strlen(aaa);
1315                         aaa[0] = 0;
1316                 }
1317                 b = strlen(aaa);
1318                 aaa[b] = a;
1319                 aaa[b + 1] = 0;
1320         }
1321         if (a == 32) {
1322                 if ((strlen(aaa) + c) > (width - 1)) {
1323                         c = 1;
1324                         if (fpout) {
1325                                 fprintf(fpout, "\n");
1326                         } else {
1327                                 scr_printf("\n");
1328                                 ++lines_printed;
1329                                 lines_printed = checkpagin(lines_printed, pagin, height);
1330                         }
1331                 }
1332                 if (fpout) {
1333                         fprintf(fpout, "%s ", aaa);
1334                 } else {
1335                         scr_printf("%s ", aaa);
1336                 }
1337                 ++c;
1338                 c = c + strlen(aaa);
1339                 strcpy(aaa, "");
1340                 goto FMTA;
1341         }
1342         if ((a == 13) || (a == 10)) {
1343                 if (fpout) {
1344                         fprintf(fpout, "%s\n", aaa);
1345                 } else {
1346                         scr_printf("%s\n", aaa);
1347                         ++lines_printed;
1348                         lines_printed = checkpagin(lines_printed, pagin, height);
1349                 }
1350                 c = 1;
1351                 if (sigcaught) goto OOPS;
1352                 strcpy(aaa, "");
1353                 goto FMTA;
1354         }
1355         goto FMTA;
1356
1357         /* keypress caught; drain the server */
1358 OOPS:   do {
1359                 serv_gets(aaa);
1360         } while (strcmp(aaa, "000"));
1361
1362 FMTEND:
1363         if (fpout) {
1364                 fprintf(fpout, "\n");
1365         } else {
1366                 scr_printf("\n");
1367                 ++lines_printed;
1368                 lines_printed = checkpagin(lines_printed, pagin, height);
1369         }
1370         return (sigcaught);
1371 }
1372
1373
1374 /*
1375  * support ANSI color if defined
1376  */
1377 void color(int colornum)
1378 {
1379         static int is_bold = 0;
1380         static int hold_color, current_color;
1381
1382         if (colornum == COLOR_PUSH) {
1383                 hold_color = current_color;
1384                 return;
1385         }
1386
1387         if (colornum == COLOR_POP) {
1388                 color(hold_color);
1389                 return;
1390         }
1391
1392         current_color = colornum;
1393         if (enable_color) {
1394 #ifdef HAVE_CURSES_H
1395                 if (scr_color(colornum))
1396                         return;
1397 #endif
1398                 /* When switching to dim white, actually output an 'original
1399                  * pair' sequence -- this looks better on black-on-white
1400                  * terminals.
1401                  */
1402                 if (colornum == DIM_WHITE)
1403                         printf("\033[39;49m");
1404                 else
1405                         printf("\033[3%d;40m", (colornum & 7));
1406
1407                 if ((colornum >= 8) && (is_bold == 0)) {
1408                         printf("\033[1m");
1409                         is_bold = 1;
1410                 } else if ((colornum < 8) && (is_bold == 1)) {
1411                         printf("\033[0m");
1412                         is_bold = 0;
1413                 }
1414                 scr_flush();
1415         }
1416 }
1417
1418 void cls(int colornum)
1419 {
1420         if (enable_color) {
1421                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1422                 scr_flush();
1423         }
1424 }
1425
1426
1427 /*
1428  * Detect whether ANSI color is available (answerback)
1429  */
1430 void send_ansi_detect(void)
1431 {
1432         if (rc_ansi_color == 2) {
1433                 printf("\033[c");
1434                 scr_flush();
1435                 time(&AnsiDetect);
1436         }
1437 }
1438
1439 void look_for_ansi(void)
1440 {
1441         fd_set rfds;
1442         struct timeval tv;
1443         char abuf[512];
1444         time_t now;
1445         int a;
1446
1447         if (rc_ansi_color == 0) {
1448                 enable_color = 0;
1449         } else if (rc_ansi_color == 1) {
1450                 enable_color = 1;
1451         } else if (rc_ansi_color == 2) {
1452
1453                 /* otherwise, do the auto-detect */
1454
1455                 strcpy(abuf, "");
1456
1457                 time(&now);
1458                 if ((now - AnsiDetect) < 2)
1459                         sleep(1);
1460
1461                 do {
1462                         FD_ZERO(&rfds);
1463                         FD_SET(0, &rfds);
1464                         tv.tv_sec = 0;
1465                         tv.tv_usec = 1;
1466
1467                         select(1, &rfds, NULL, NULL, &tv);
1468                         if (FD_ISSET(0, &rfds)) {
1469                                 abuf[strlen(abuf) + 1] = 0;
1470                                 read(0, &abuf[strlen(abuf)], 1);
1471                         }
1472                 } while (FD_ISSET(0, &rfds));
1473
1474                 for (a = 0; a < strlen(abuf); ++a) {
1475                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1476                             && (abuf[a + 2] == '?')) {
1477                                 enable_color = 1;
1478                         }
1479                 }
1480         }
1481 }
1482
1483
1484 /*
1485  * Display key options (highlight hotkeys inside angle brackets)
1486  */
1487 void keyopt(char *buf) {
1488         int i;
1489
1490         color(DIM_WHITE);
1491         for (i=0; i<strlen(buf); ++i) {
1492                 if (buf[i]=='<') {
1493                         scr_putc(buf[i]);
1494                         color(BRIGHT_MAGENTA);
1495                 } else {
1496                         if (buf[i]=='>') {
1497                                 color(DIM_WHITE);
1498                         }
1499                         scr_putc(buf[i]);
1500                 }
1501         }
1502         color(DIM_WHITE);
1503 }
1504
1505
1506
1507 /*
1508  * Present a key-menu line choice type of thing
1509  */
1510 char keymenu(char *menuprompt, char *menustring) {
1511         int i, c, a;
1512         int choices;
1513         int do_prompt = 0;
1514         char buf[1024];
1515         int ch;
1516         int display_prompt = 1;
1517
1518         choices = num_tokens(menustring, '|');
1519
1520         if (menuprompt != NULL) do_prompt = 1;
1521         if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1522
1523         while (1) {
1524                 if (display_prompt) {
1525                         if (do_prompt) {
1526                                 scr_printf("%s ", menuprompt);
1527                         } 
1528                         else {
1529                                 for (i=0; i<choices; ++i) {
1530                                         extract(buf, menustring, i);
1531                                         keyopt(buf);
1532                                         scr_printf(" ");
1533                                 }
1534                         }
1535                         scr_printf(" -> ");
1536                         display_prompt = 0;
1537                 }
1538                 ch = lkey();
1539         
1540                 if ( (do_prompt) && (ch=='?') ) {
1541                         scr_printf("\rOne of...                               ");
1542                         scr_printf("                                      \n");
1543                         for (i=0; i<choices; ++i) {
1544                                 extract(buf, menustring, i);
1545                                 scr_printf("   ");
1546                                 keyopt(buf);
1547                                 scr_printf("\n");
1548                         }
1549                         scr_printf("\n");
1550                         display_prompt = 1;
1551                 }
1552
1553                 for (i=0; i<choices; ++i) {
1554                         extract(buf, menustring, i);
1555                         for (c=1; c<strlen(buf); ++c) {
1556                                 if ( (ch == tolower(buf[c]))
1557                                    && (buf[c-1]=='<')
1558                                    && (buf[c+1]=='>') ) {
1559                                         for (a=0; a<strlen(buf); ++a) {
1560                                                 if ( (a!=(c-1)) && (a!=(c+1))) {
1561                                                         scr_putc(buf[a]);
1562                                                 }
1563                                         }
1564                                         scr_printf("\n\n");
1565                                         return ch;
1566                                 }
1567                         }
1568                 }
1569         }
1570 }