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