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