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