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