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