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