* Eliminated the whole SIGINT/SIGQUIT based handling of Ctrl-O and Ctrl-C
[citadel.git] / citadel / commands.c
1 /*
2  * $Id$
3  *
4  * This file contains functions which implement parts of the
5  * text-mode user interface.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18
19 #ifdef HAVE_TERMIOS_H
20 #include <termios.h>
21 #else
22 #include <sgtty.h>
23 #endif
24
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28
29 #ifdef THREADED_CLIENT
30 #include <pthread.h>
31 #endif
32
33 #include <signal.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include "citadel.h"
37 #include "commands.h"
38 #include "messages.h"
39 #include "citadel_decls.h"
40 #include "routines.h"
41 #include "routines2.h"
42 #include "tools.h"
43 #include "rooms.h"
44 #ifndef HAVE_SNPRINTF
45 #include "snprintf.h"
46 #endif
47
48 struct citcmd {
49         struct citcmd *next;
50         int c_cmdnum;
51         int c_axlevel;
52         char c_keys[5][64];
53 };
54
55 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
56
57
58 int rc_exp_beep;
59 char rc_exp_cmd[256];
60 int rc_allow_attachments;
61 int rc_display_message_numbers;
62 int rc_force_mail_prompts;
63 int rc_remember_passwords;
64 int rc_ansi_color;
65 int num_urls = 0;
66 char urls[MAXURLS][256];
67 char rc_url_cmd[256];
68
69 char *gl_string;
70 int next_lazy_cmd = 5;
71
72 int lines_printed = 0;          /* line count for paginator */
73 extern int screenwidth, screenheight;
74
75 struct citcmd *cmdlist = NULL;
76
77
78 /* these variables are local to this module */
79 char keepalives_enabled = KA_YES;       /* send NOOPs to server when idle */
80 int ok_to_interrupt = 0;                /* print express msgs asynchronously */
81 time_t AnsiDetect;                      /* when did we send the detect code? */
82 int enable_color = 0;                   /* nonzero for ANSI color */
83
84
85
86
87 /*
88  * If an interesting key has been pressed, return its value, otherwise 0
89  */
90 char was_a_key_pressed(void) {
91         fd_set rfds;
92         struct timeval tv;
93         int the_character;
94         int retval;
95
96         FD_ZERO(&rfds);
97         FD_SET(0, &rfds);
98         tv.tv_sec = 0;
99         tv.tv_usec = 0;
100         retval = select(1, &rfds, NULL, NULL, &tv); 
101
102         if (FD_ISSET(0, &rfds)) {
103                 the_character = inkey();
104         }
105         else {
106                 the_character = 0;
107         }
108         return(the_character);
109 }
110
111
112
113
114
115 /*
116  * Check to see if we need to pause at the end of a screen.
117  * If we do, we have to disable server keepalives during the pause because
118  * we are probably in the middle of a server operation and the NOOP command
119  * would confuse everything.
120  */
121 int checkpagin(int lp, int pagin, int height)
122 {
123         int thekey;
124
125         if (sigcaught) return(lp);
126         thekey = was_a_key_pressed();
127         if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
128         if (sigcaught) return(lp);
129
130         if (!pagin) return(0);
131         if (lp>=(height-1)) {
132                 set_keepalives(KA_NO);
133                 hit_any_key();
134                 set_keepalives(KA_YES);
135                 return(0);
136                 }
137         return(lp);
138         }
139
140
141
142
143 /*
144  * pprintf()  ...   paginated version of printf()
145  */
146 void pprintf(const char *format, ...) {   
147         va_list arg_ptr;
148         static char buf[4096];  /* static for performance, change if needed */
149         int i;
150
151         /* If sigcaught is nonzero, a keypress has interrupted this and we
152          * should just drain output.
153          */
154         if (sigcaught) return;
155  
156         /* Otherwise, start spewing... */ 
157         va_start(arg_ptr, format);   
158         vsprintf(buf, format, arg_ptr);   
159         va_end(arg_ptr);   
160
161         for (i=0; i<strlen(buf); ++i) {
162                 putc(buf[i], stdout);
163                 if (buf[i]==10) {
164                         ++lines_printed;
165                         lines_printed = checkpagin(lines_printed,
166                                 (userflags & US_PAGINATOR),
167                                 screenheight);
168                 }
169         }
170 }   
171
172
173
174 /*
175  * print_express()  -  print express messages if there are any
176  */
177 void print_express(void)
178 {
179         char buf[256];
180         FILE *outpipe;
181
182         if (express_msgs == 0)
183                 return;
184         express_msgs = 0;
185         serv_puts("PEXP");
186         serv_gets(buf);
187         if (buf[0] != '1')
188                 return;
189
190         if (strlen(rc_exp_cmd) > 0) {
191                 outpipe = popen(rc_exp_cmd, "w");
192                 if (outpipe != NULL) {
193                         while (serv_gets(buf), strcmp(buf, "000")) {
194                                 fprintf(outpipe, "%s\n", buf);
195                         }
196                         pclose(outpipe);
197                         return;
198                 }
199         }
200         /* fall back to built-in express message display */
201         if (rc_exp_beep) {
202                 putc(7, stdout);
203         }
204         color(BRIGHT_RED);
205         printf("\r---\n");
206         while (serv_gets(buf), strcmp(buf, "000")) {
207                 printf("%s\n", buf);
208         }
209         printf("---\n");
210         color(BRIGHT_WHITE);
211 }
212
213
214 void set_keepalives(int s)
215 {
216         keepalives_enabled = (char) s;
217 }
218
219 /* 
220  * This loop handles the "keepalive" messages sent to the server when idling.
221  */
222
223 static time_t idlet = 0;
224 static void really_do_keepalive(void) {
225         char buf[256];
226
227         time(&idlet);
228         if (keepalives_enabled != KA_NO) {
229                 serv_puts("NOOP");
230                 if (keepalives_enabled == KA_YES) {
231                         serv_gets(buf);
232                         if (buf[3] == '*') {
233                                 express_msgs = 1;
234                                 if (ok_to_interrupt == 1) {
235                                         printf("\r%64s\r", "");
236                                         print_express();
237                                         printf("%s%c ", room_name,
238                                                room_prompt(room_flags));
239                                         fflush(stdout);
240                                 }
241                         }
242                 }
243         }
244 }
245
246 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
247    encapsulated interface; in theory there should be no need to touch these
248    globals outside of the async_ka_* functions. */
249
250 #ifdef THREADED_CLIENT
251 static pthread_t ka_thr_handle;
252 static int ka_thr_active = 0;
253 static int async_ka_enabled = 0;
254
255 static void *ka_thread(void *arg)
256 {
257         really_do_keepalive();
258         pthread_detach(ka_thr_handle);
259         ka_thr_active = 0;
260         return NULL;
261 }
262
263 /* start up a thread to handle a keepalive in the background */
264 static void async_ka_exec(void)
265 {
266         if (!ka_thr_active) {
267                 ka_thr_active = 1;
268                 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
269                         perror("pthread_create");
270                         exit(1);
271                 }
272         }
273 }
274 #endif /* THREADED_CLIENT */
275
276 static void do_keepalive(void)
277 {
278         time_t now;
279
280         time(&now);
281         if ((now - idlet) < ((long) S_KEEPALIVE))
282                 return;
283
284         /* Do a space-backspace to keep telnet sessions from idling out */
285         printf(" %c", 8);
286         fflush(stdout);
287
288 #ifdef THREADED_CLIENT
289         if (async_ka_enabled)
290                 async_ka_exec();
291         else
292 #endif
293                 really_do_keepalive();
294 }
295
296
297 /* Now the actual async-keepalve API that we expose to higher levels:
298    async_ka_start() and async_ka_end(). These do nothing when we don't have
299    threading enabled, so we avoid sprinkling ifdef's throughout the code. */
300
301 /* wait for a background keepalive to complete. this must be done before
302    attempting any further server requests! */
303 void async_ka_end(void)
304 {
305 #ifdef THREADED_CLIENT
306         if (ka_thr_active)
307                 pthread_join(ka_thr_handle, NULL);
308
309         async_ka_enabled--;
310 #endif
311 }
312
313 /* tell do_keepalive() that keepalives are asynchronous. */
314 void async_ka_start(void)
315 {
316 #ifdef THREADED_CLIENT
317         async_ka_enabled++;
318 #endif
319 }
320
321
322 int inkey(void)
323 {                               /* get a character from the keyboard, with   */
324         int a;                  /* the watchdog timer in effect if necessary */
325         fd_set rfds;
326         struct timeval tv;
327         time_t start_time, now;
328         char inbuf[2];
329
330         fflush(stdout);
331         lines_printed = 0;
332         time(&start_time);
333
334         do {
335
336                 /* This loop waits for keyboard input.  If the keepalive
337                  * timer expires, it sends a keepalive to the server if
338                  * necessary and then waits again.
339                  */
340                 do {
341                         do_keepalive();
342
343                         FD_ZERO(&rfds);
344                         FD_SET(0, &rfds);
345                         tv.tv_sec = S_KEEPALIVE;
346                         tv.tv_usec = 0;
347
348                         time(&now);
349                         if (((now - start_time) > SLEEPING)
350                             && (SLEEPING != 0) && (getppid() == 1)) {
351                                 printf("Sleeping? Call again.\n");
352                                 logoff(SIGALRM);
353                         }
354                         select(1, &rfds, NULL, NULL, &tv);
355                 } while (!FD_ISSET(0, &rfds));
356
357
358
359
360                 /* At this point, there's input, so fetch it.
361                  * (There's a hole in the bucket...)
362                  */
363                 read(0, inbuf, 1);
364                 a = inbuf[0];
365                 if (a == 127)
366                         a = 8;
367                 if (a > 126)
368                         a = 0;
369                 if (a == 10)
370                         a = 13;
371                 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
372                     && ((a < 32) || (a > 126)))
373                         a = 0;
374         } while (a == 0);
375         return (a);
376 }
377
378
379 int yesno(void)
380 {                               /* Returns 1 for yes, 0 for no */
381         int a;
382         while (1) {
383                 a = inkey();
384                 a = tolower(a);
385                 if (a == 'y') {
386                         printf("Yes\n");
387                         return (1);
388                 }
389                 if (a == 'n') {
390                         printf("No\n");
391                         return (0);
392                 }
393         }
394 }
395
396 /* Returns 1 for yes, 0 for no, arg is default value */
397 int yesno_d(int d)
398 {
399         int a;
400         while (1) {
401                 a = inkey();
402                 a = tolower(a);
403                 if (a == 13)
404                         a = (d ? 'y' : 'n');
405                 if (a == 'y') {
406                         printf("Yes\n");
407                         return (1);
408                 }
409                 if (a == 'n') {
410                         printf("No\n");
411                         return (0);
412                 }
413         }
414 }
415
416
417
418
419 /* Gets a line from the terminal */
420 /* string == Pointer to string buffer */
421 /* lim == Maximum length - if negative, no-show */
422 void getline(char *string, int lim) 
423 {
424         int a, b;
425         char flag = 0;
426
427         if (lim < 0) {
428                 lim = (0 - lim);
429                 flag = 1;
430         }
431         strcpy(string, "");
432         gl_string = string;
433         async_ka_start();
434       GLA:a = inkey();
435         a = (a & 127);
436         if ((a == 8) && (strlen(string) == 0))
437                 goto GLA;
438         if ((a != 13) && (a != 8) && (strlen(string) == lim))
439                 goto GLA;
440         if ((a == 8) && (string[0] != 0)) {
441                 string[strlen(string) - 1] = 0;
442                 putc(8, stdout);
443                 putc(32, stdout);
444                 putc(8, stdout);
445                 goto GLA;
446         }
447         if ((a == 13) || (a == 10)) {
448                 putc(13, stdout);
449                 putc(10, stdout);
450                 async_ka_end();
451                 return;
452         }
453         if (a < 32)
454                 a = '.';
455         b = strlen(string);
456         string[b] = a;
457         string[b + 1] = 0;
458         if (flag == 0)
459                 putc(a, stdout);
460         if (flag == 1)
461                 putc('*', stdout);
462         goto GLA;
463 }
464
465
466 /*
467  * strprompt()  -  prompt for a string, print the existing value and
468  *                 allow the user to press return to keep it...
469  */
470 void strprompt(char *prompt, char *str, int len)
471 {
472         char buf[128];
473         print_express();
474         color(DIM_WHITE);
475         printf("%s ", prompt);
476         color(DIM_MAGENTA);
477         printf("[");
478         color(BRIGHT_MAGENTA);
479         printf("%s", str);
480         color(DIM_MAGENTA);
481         printf("]");
482         color(DIM_WHITE);
483         printf(": ");
484         color(BRIGHT_CYAN);
485         getline(buf, len);
486         if (buf[0] != 0)
487                 strcpy(str, buf);
488         color(DIM_WHITE);
489 }
490
491 /*
492  * boolprompt()  -  prompt for a yes/no, print the existing value and
493  *                  allow the user to press return to keep it...
494  */
495 int boolprompt(char *prompt, int prev_val)
496 {
497         int r;
498
499         color(DIM_WHITE);
500         printf("%s ", prompt);
501         color(DIM_MAGENTA);
502         printf(" [");
503         color(BRIGHT_MAGENTA);
504         printf("%s", (prev_val ? "Yes" : "No"));
505         color(DIM_MAGENTA);
506         printf("]: ");
507         color(BRIGHT_CYAN);
508         r = (yesno_d(prev_val));
509         color(DIM_WHITE);
510         return r;
511 }
512
513 /* 
514  * intprompt()  -  like strprompt(), except for an integer
515  *                 (note that it RETURNS the new value!)
516  */
517 int intprompt(char *prompt, int ival, int imin, int imax)
518 {
519         char buf[16];
520         int i;
521         int p;
522
523         do {
524                 i = ival;
525                 snprintf(buf, sizeof buf, "%d", i);
526                 strprompt(prompt, buf, 15);
527                 i = atoi(buf);
528                 for (p=0; p<strlen(buf); ++p) {
529                         if ( (!isdigit(buf[p]))
530                            && ( (buf[p]!='-') || (p!=0) )  )
531                                 i = imin - 1;
532                 }
533                 if (i < imin)
534                         printf("*** Must be no less than %d.\n", imin);
535                 if (i > imax)
536                         printf("*** Must be no more than %d.\n", imax);
537         } while ((i < imin) || (i > imax));
538         return (i);
539 }
540
541 /* 
542  * newprompt()  -  prompt for a string with no existing value
543  *                 (clears out string buffer first)
544  */
545 void newprompt(char *prompt, char *str, int len)
546 {
547         color(BRIGHT_MAGENTA);
548         printf("%s", prompt);
549         color(DIM_MAGENTA);
550         getline(str, len);
551         color(DIM_WHITE);
552 }
553
554
555 int lkey(void)
556 {                               /* returns a lower case value */
557         int a;
558         a = inkey();
559         if (isupper(a))
560                 a = tolower(a);
561         return (a);
562 }
563
564 /*
565  * parse the citadel.rc file
566  */
567 void load_command_set(void)
568 {
569         FILE *ccfile;
570         char buf[256];
571         struct citcmd *cptr;
572         struct citcmd *lastcmd = NULL;
573         int a, d;
574         int b = 0;
575
576
577         /* first, set up some defaults for non-required variables */
578
579         strcpy(editor_path, "");
580         strcpy(printcmd, "");
581         strcpy(rc_username, "");
582         strcpy(rc_password, "");
583         rc_floor_mode = 0;
584         rc_exp_beep = 1;
585         rc_allow_attachments = 0;
586         rc_remember_passwords = 0;
587         strcpy(rc_exp_cmd, "");
588         rc_display_message_numbers = 0;
589         rc_force_mail_prompts = 0;
590         rc_ansi_color = 0;
591         strcpy(rc_url_cmd, "");
592
593         /* now try to open the citadel.rc file */
594
595         ccfile = NULL;
596         if (getenv("HOME") != NULL) {
597                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
598                 ccfile = fopen(buf, "r");
599         }
600         if (ccfile == NULL) {
601                 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
602         }
603         if (ccfile == NULL) {
604                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
605                 ccfile = fopen(buf, "r");
606         }
607         if (ccfile == NULL) {
608                 ccfile = fopen("./citadel.rc", "r");
609         }
610         if (ccfile == NULL) {
611                 perror("commands: cannot open citadel.rc");
612                 logoff(errno);
613         }
614         while (fgets(buf, 256, ccfile) != NULL) {
615                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
616                         buf[strlen(buf) - 1] = 0;
617
618                 if (!strncasecmp(buf, "editor=", 7))
619                         strcpy(editor_path, &buf[7]);
620
621                 if (!strncasecmp(buf, "printcmd=", 9))
622                         strcpy(printcmd, &buf[9]);
623
624                 if (!strncasecmp(buf, "expcmd=", 7))
625                         strcpy(rc_exp_cmd, &buf[7]);
626
627                 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
628                         have_xterm = (char) atoi(&buf[24]);
629
630                 if (!strncasecmp(buf, "use_floors=", 11)) {
631                         if (!strcasecmp(&buf[11], "yes"))
632                                 rc_floor_mode = RC_YES;
633                         if (!strcasecmp(&buf[11], "no"))
634                                 rc_floor_mode = RC_NO;
635                         if (!strcasecmp(&buf[11], "default"))
636                                 rc_floor_mode = RC_DEFAULT;
637                 }
638                 if (!strncasecmp(buf, "beep=", 5)) {
639                         rc_exp_beep = atoi(&buf[5]);
640                 }
641                 if (!strncasecmp(buf, "allow_attachments=", 18)) {
642                         rc_allow_attachments = atoi(&buf[18]);
643                 }
644                 if (!strncasecmp(buf, "remember_passwords=", 19)) {
645                         rc_remember_passwords = atoi(&buf[19]);
646                 }
647                 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
648                         rc_display_message_numbers = atoi(&buf[24]);
649                 }
650                 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
651                         rc_force_mail_prompts = atoi(&buf[19]);
652                 }
653                 if (!strncasecmp(buf, "ansi_color=", 11)) {
654                         if (!strncasecmp(&buf[11], "on", 2))
655                                 rc_ansi_color = 1;
656                         if (!strncasecmp(&buf[11], "auto", 4))
657                                 rc_ansi_color = 2;      /* autodetect */
658                         if (!strncasecmp(&buf[11], "user", 4))
659                                 rc_ansi_color = 3;      /* user config */
660                 }
661                 if (!strncasecmp(buf, "username=", 9))
662                         strcpy(rc_username, &buf[9]);
663
664                 if (!strncasecmp(buf, "password=", 9))
665                         strcpy(rc_password, &buf[9]);
666
667                 if (!strncasecmp(buf, "urlcmd=", 7))
668                         strcpy(rc_url_cmd, &buf[7]);
669
670                 if (!strncasecmp(buf, "cmd=", 4)) {
671                         strcpy(buf, &buf[4]);
672
673                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
674
675                         cptr->c_cmdnum = atoi(buf);
676                         for (d = strlen(buf); d >= 0; --d)
677                                 if (buf[d] == ',')
678                                         b = d;
679                         strcpy(buf, &buf[b + 1]);
680
681                         cptr->c_axlevel = atoi(buf);
682                         for (d = strlen(buf); d >= 0; --d)
683                                 if (buf[d] == ',')
684                                         b = d;
685                         strcpy(buf, &buf[b + 1]);
686
687                         for (a = 0; a < 5; ++a)
688                                 cptr->c_keys[a][0] = 0;
689
690                         a = 0;
691                         b = 0;
692                         buf[strlen(buf) + 1] = 0;
693                         while (strlen(buf) > 0) {
694                                 b = strlen(buf);
695                                 for (d = strlen(buf); d >= 0; --d)
696                                         if (buf[d] == ',')
697                                                 b = d;
698                                 strncpy(cptr->c_keys[a], buf, b);
699                                 cptr->c_keys[a][b] = 0;
700                                 if (buf[b] == ',')
701                                         strcpy(buf, &buf[b + 1]);
702                                 else
703                                         strcpy(buf, "");
704                                 ++a;
705                         }
706
707                         cptr->next = NULL;
708                         if (cmdlist == NULL)
709                                 cmdlist = cptr;
710                         else
711                                 lastcmd->next = cptr;
712                         lastcmd = cptr;
713                 }
714         }
715         fclose(ccfile);
716 }
717
718
719
720 /*
721  * return the key associated with a command
722  */
723 char keycmd(char *cmdstr)
724 {
725         int a;
726
727         for (a = 0; a < strlen(cmdstr); ++a)
728                 if (cmdstr[a] == '&')
729                         return (tolower(cmdstr[a + 1]));
730         return (0);
731 }
732
733
734 /*
735  * Output the string from a key command without the ampersand
736  * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
737  */
738 char *cmd_expand(char *strbuf, int mode)
739 {
740         int a;
741         static char exp[64];
742         char buf[256];
743
744         strcpy(exp, strbuf);
745
746         for (a = 0; a < strlen(exp); ++a) {
747                 if (strbuf[a] == '&') {
748
749                         if (mode == 0) {
750                                 strcpy(&exp[a], &exp[a + 1]);
751                         }
752                         if (mode == 1) {
753                                 exp[a] = '<';
754                                 strcpy(buf, &exp[a + 2]);
755                                 exp[a + 2] = '>';
756                                 exp[a + 3] = 0;
757                                 strcat(exp, buf);
758                         }
759                 }
760                 if (!strncmp(&exp[a], "^r", 2)) {
761                         strcpy(buf, exp);
762                         strcpy(&exp[a], room_name);
763                         strcat(exp, &buf[a + 2]);
764                 }
765                 if (!strncmp(&exp[a], "^c", 2)) {
766                         exp[a] = ',';
767                         strcpy(&exp[a + 1], &exp[a + 2]);
768                 }
769         }
770
771         return (exp);
772 }
773
774
775
776 /*
777  * Comparison function to determine if entered commands match a
778  * command loaded from the config file.
779  */
780 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
781 {
782         int a;
783         int cmdax;
784
785         cmdax = 0;
786         if (is_room_aide)
787                 cmdax = 1;
788         if (axlevel >= 6)
789                 cmdax = 2;
790
791         for (a = 0; a < ncomp; ++a) {
792                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
793                     || (cptr->c_axlevel > cmdax))
794                         return (0);
795         }
796         return (1);
797 }
798
799
800 /*
801  * This function returns 1 if a given command requires a string input
802  */
803 int requires_string(struct citcmd *cptr, int ncomp)
804 {
805         int a;
806         char buf[64];
807
808         strcpy(buf, cptr->c_keys[ncomp - 1]);
809         for (a = 0; a < strlen(buf); ++a) {
810                 if (buf[a] == ':')
811                         return (1);
812         }
813         return (0);
814 }
815
816
817 /*
818  * Input a command at the main prompt.
819  * This function returns an integer command number.  If the command prompts
820  * for a string then it is placed in the supplied buffer.
821  */
822 int getcmd(char *argbuf)
823 {
824         char cmdbuf[5];
825         int cmdspaces[5];
826         int cmdpos;
827         int ch;
828         int a;
829         int got;
830         int this_lazy_cmd;
831         struct citcmd *cptr;
832
833         /*
834          * Starting a new command now, so set sigcaught to 0.  This variable
835          * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
836          * been interrupted by a keypress.
837          */
838         sigcaught = 0;
839
840         /* Switch color support on or off if we're in user mode */
841         if (rc_ansi_color == 3) {
842                 if (userflags & US_COLOR)
843                         enable_color = 1;
844                 else
845                         enable_color = 0;
846         }
847         /* if we're running in idiot mode, display a cute little menu */
848         IFNEXPERT formout("mainmenu");
849
850         print_express();        /* print express messages if there are any */
851         strcpy(argbuf, "");
852         cmdpos = 0;
853         for (a = 0; a < 5; ++a)
854                 cmdbuf[a] = 0;
855         /* now the room prompt... */
856         ok_to_interrupt = 1;
857         color(BRIGHT_WHITE);
858         printf("\n%s", room_name);
859         color(DIM_WHITE);
860         printf("%c ", room_prompt(room_flags));
861         fflush(stdout);
862
863         while (1) {
864                 ch = inkey();
865                 ok_to_interrupt = 0;
866
867                 /* Handle the backspace key, but only if there's something
868                  * to backspace over...
869                  */
870                 if ((ch == 8) && (cmdpos > 0)) {
871                         back(cmdspaces[cmdpos - 1] + 1);
872                         cmdbuf[cmdpos] = 0;
873                         --cmdpos;
874                 }
875                 /* Spacebar invokes "lazy traversal" commands */
876                 if ((ch == 32) && (cmdpos == 0)) {
877                         this_lazy_cmd = next_lazy_cmd;
878                         if (this_lazy_cmd == 13)
879                                 next_lazy_cmd = 5;
880                         if (this_lazy_cmd == 5)
881                                 next_lazy_cmd = 13;
882                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
883                                 if (cptr->c_cmdnum == this_lazy_cmd) {
884                                         for (a = 0; a < 5; ++a)
885                                                 if (cptr->c_keys[a][0] != 0)
886                                                         printf("%s ", cmd_expand(
887                                                                                         cptr->c_keys[a], 0));
888                                         printf("\n");
889                                         return (this_lazy_cmd);
890                                 }
891                         }
892                         printf("\n");
893                         return (this_lazy_cmd);
894                 }
895                 /* Otherwise, process the command */
896                 cmdbuf[cmdpos] = tolower(ch);
897
898                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
899                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
900
901                                 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
902                                 cmdspaces[cmdpos] = strlen(
903                                     cmd_expand(cptr->c_keys[cmdpos], 0));
904                                 if (cmdpos < 4)
905                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
906                                                 putc(' ', stdout);
907                                 ++cmdpos;
908                         }
909                 }
910
911                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
912                         if (cmdmatch(cmdbuf, cptr, 5)) {
913                                 /* We've found our command. */
914                                 if (requires_string(cptr, cmdpos)) {
915                                         getline(argbuf, 32);
916                                 } else {
917                                         printf("\n");
918                                 }
919
920                                 /* If this command is one that changes rooms,
921                                  * then the next lazy-command (space bar)
922                                  * should be "read new" instead of "goto"
923                                  */
924                                 if ((cptr->c_cmdnum == 5)
925                                     || (cptr->c_cmdnum == 6)
926                                     || (cptr->c_cmdnum == 47)
927                                     || (cptr->c_cmdnum == 52)
928                                     || (cptr->c_cmdnum == 16)
929                                     || (cptr->c_cmdnum == 20))
930                                         next_lazy_cmd = 13;
931
932                                 return (cptr->c_cmdnum);
933
934                         }
935                 }
936
937                 if (ch == '?') {
938                         printf("\rOne of ...                         \n");
939                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
940                                 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
941                                         for (a = 0; a < 5; ++a) {
942                                                 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
943                                         }
944                                         printf("\n");
945                                 }
946                         }
947
948                         printf("\n%s%c ", room_name, room_prompt(room_flags));
949                         got = 0;
950                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
951                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
952                                         for (a = 0; a < cmdpos; ++a) {
953                                                 printf("%s ",
954                                                        cmd_expand(cptr->c_keys[a], 0));
955                                         }
956                                         got = 1;
957                                 }
958                         }
959                 }
960         }
961
962 }
963
964
965
966
967
968 /*
969  * set tty modes.  commands are:
970  * 
971  * 01- set to bbs mode
972  * 2 - save current settings for later restoral
973  * 3 - restore saved settings
974  */
975 #ifdef HAVE_TERMIOS_H
976 void sttybbs(int cmd)
977 {                               /* SysV version of sttybbs() */
978         struct termios live;
979         static struct termios saved_settings;
980         static int last_cmd = 0;
981
982         if (cmd == SB_LAST)
983                 cmd = last_cmd;
984         else
985                 last_cmd = cmd;
986
987         if ((cmd == 0) || (cmd == 1)) {
988                 tcgetattr(0, &live);
989                 live.c_iflag = ISTRIP | IXON | IXANY;
990                 live.c_oflag = OPOST | ONLCR;
991                 live.c_lflag = ISIG | NOFLSH;
992
993                 live.c_cc[VINTR] = (-1);
994                 live.c_cc[VQUIT] = (-1);
995
996                 /* do we even need this stuff anymore? */
997                 /* live.c_line=0; */
998                 live.c_cc[VERASE] = 8;
999                 live.c_cc[VKILL] = 24;
1000                 live.c_cc[VEOF] = 1;
1001                 live.c_cc[VEOL] = 255;
1002                 live.c_cc[VEOL2] = 0;
1003                 live.c_cc[VSTART] = 0;
1004                 tcsetattr(0, TCSADRAIN, &live);
1005         }
1006         if (cmd == 2) {
1007                 tcgetattr(0, &saved_settings);
1008         }
1009         if (cmd == 3) {
1010                 tcsetattr(0, TCSADRAIN, &saved_settings);
1011         }
1012 }
1013 #else
1014 void sttybbs(int cmd)
1015 {                               /* BSD version of sttybbs() */
1016         struct sgttyb live;
1017         static struct sgttyb saved_settings;
1018
1019         if ((cmd == 0) || (cmd == 1)) {
1020                 gtty(0, &live);
1021                 live.sg_flags |= CBREAK;
1022                 live.sg_flags |= CRMOD;
1023                 live.sg_flags |= NL1;
1024                 live.sg_flags &= ~ECHO;
1025                 if (cmd == 1)
1026                         live.sg_flags |= NOFLSH;
1027                 stty(0, &live);
1028         }
1029         if (cmd == 2) {
1030                 gtty(0, &saved_settings);
1031         }
1032         if (cmd == 3) {
1033                 stty(0, &saved_settings);
1034         }
1035 }
1036 #endif
1037
1038
1039 /*
1040  * display_help()  -  help file viewer
1041  */
1042 void display_help(char *name)
1043 {
1044         formout(name);
1045 }
1046
1047
1048 /*
1049  * fmout()  -  Citadel text formatter and paginator
1050  */
1051 int fmout(
1052         int width,      /* screen width to use */
1053         FILE *fp,       /* file to read from, or NULL to read from server */
1054         char pagin,     /* nonzero if we should use the paginator */
1055         int height,     /* screen height to use */
1056         int starting_lp,/* starting value for lines_printed, -1 for global */
1057         char subst)     /* nonzero if we should use hypertext mode */
1058 {
1059         int a, b, c, d, old;
1060         int real = (-1);
1061         char aaa[140];
1062         char buffer[512];
1063         int eof_flag = 0;
1064
1065         num_urls = 0;   /* Start with a clean slate of embedded URL's */
1066
1067         if (starting_lp >= 0) {
1068                 lines_printed = starting_lp;
1069         }
1070         strcpy(aaa, "");
1071         old = 255;
1072         strcpy(buffer, "");
1073         c = 1;                  /* c is the current pos */
1074
1075 FMTA:   while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1076                 if (fp != NULL) {       /* read from file */
1077                         if (feof(fp))
1078                                 eof_flag = 1;
1079                         if (eof_flag == 0) {
1080                                 a = getc(fp);
1081                                 buffer[strlen(buffer) + 1] = 0;
1082                                 buffer[strlen(buffer)] = a;
1083                         }
1084                 } else {        /* read from server */
1085                         d = strlen(buffer);
1086                         serv_gets(&buffer[d]);
1087                         while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1088                                 buffer[strlen(buffer) - 1] = 0;
1089                         if (!strcmp(&buffer[d], "000")) {
1090                                 buffer[d] = 0;
1091                                 eof_flag = 1;
1092                                 while (isspace(buffer[strlen(buffer) - 1]))
1093                                         buffer[strlen(buffer) - 1] = 0;
1094                         }
1095                         d = strlen(buffer);
1096                         buffer[d] = 10;
1097                         buffer[d + 1] = 0;
1098                 }
1099         }
1100
1101         if ( (!strncasecmp(buffer, "http://", 7))
1102            || (!strncasecmp(buffer, "ftp://", 6)) ) {
1103                 safestrncpy(urls[num_urls], buffer, 255);
1104                 for (a=0; a<strlen(urls[num_urls]); ++a) {
1105                         b = urls[num_urls][a];
1106                         if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1107                            || (b==13) || (b==9) || (b=='\"') )
1108                                 urls[num_urls][a] = 0;
1109                 }
1110                 ++num_urls;
1111         }
1112
1113         buffer[strlen(buffer) + 1] = 0;
1114         a = buffer[0];
1115         strcpy(buffer, &buffer[1]);
1116
1117         old = real;
1118         real = a;
1119         if (a <= 0)
1120                 goto FMTEND;
1121
1122         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1123                 a = 32;
1124         if (((old == 13) || (old == 10)) && (isspace(real))) {
1125                 printf("\n");
1126                 ++lines_printed;
1127                 lines_printed = checkpagin(lines_printed, pagin, height);
1128                 c = 1;
1129         }
1130         if (a > 126)
1131                 goto FMTA;
1132
1133         if (a > 32) {
1134                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1135                         printf("\n%s", aaa);
1136                         c = strlen(aaa);
1137                         aaa[0] = 0;
1138                         ++lines_printed;
1139                         lines_printed = checkpagin(lines_printed, pagin, height);
1140                 }
1141                 b = strlen(aaa);
1142                 aaa[b] = a;
1143                 aaa[b + 1] = 0;
1144         }
1145         if (a == 32) {
1146                 if ((strlen(aaa) + c) > (width - 5)) {
1147                         c = 1;
1148                         printf("\n");
1149                         ++lines_printed;
1150                         lines_printed = checkpagin(lines_printed, pagin, height);
1151                 }
1152                 printf("%s ", aaa);
1153                 ++c;
1154                 c = c + strlen(aaa);
1155                 strcpy(aaa, "");
1156                 goto FMTA;
1157         }
1158         if ((a == 13) || (a == 10)) {
1159                 printf("%s\n", aaa);
1160                 c = 1;
1161                 ++lines_printed;
1162                 lines_printed = checkpagin(lines_printed, pagin, height);
1163                 if (sigcaught) goto OOPS;
1164                 strcpy(aaa, "");
1165                 goto FMTA;
1166         }
1167         goto FMTA;
1168
1169         /* keypress caught; drain the server */
1170 OOPS:   do {
1171                 serv_gets(aaa);
1172         } while (strcmp(aaa, "000"));
1173
1174 FMTEND: printf("\n");
1175         ++lines_printed;
1176         lines_printed = checkpagin(lines_printed, pagin, height);
1177         return (sigcaught);
1178 }
1179
1180
1181 /*
1182  * support ANSI color if defined
1183  */
1184 void color(int colornum)
1185 {
1186         static int is_bold = 0;
1187         static int hold_color, current_color;
1188
1189         if (colornum == COLOR_PUSH) {
1190                 hold_color = current_color;
1191                 return;
1192         }
1193
1194         if (colornum == COLOR_POP) {
1195                 color(hold_color);
1196                 return;
1197         }
1198
1199         current_color = colornum;
1200         if (enable_color) {
1201                 /* When switching to dim white, actually output an 'original
1202                  * pair' sequence -- this looks better on black-on-white
1203                  * terminals.
1204                  */
1205                 if (colornum == DIM_WHITE)
1206                         printf("\033[39;49m");
1207                 else
1208                         printf("\033[3%d;40m", (colornum & 7));
1209
1210                 if ((colornum >= 8) && (is_bold == 0)) {
1211                         printf("\033[1m");
1212                         is_bold = 1;
1213                 } else if ((colornum < 8) && (is_bold == 1)) {
1214                         printf("\033[0m");
1215                         is_bold = 0;
1216                 }
1217                 fflush(stdout);
1218         }
1219 }
1220
1221 void cls(int colornum)
1222 {
1223         if (enable_color) {
1224                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1225                 fflush(stdout);
1226         }
1227 }
1228
1229
1230 /*
1231  * Detect whether ANSI color is available (answerback)
1232  */
1233 void send_ansi_detect(void)
1234 {
1235         if (rc_ansi_color == 2) {
1236                 printf("\033[c");
1237                 fflush(stdout);
1238                 time(&AnsiDetect);
1239         }
1240 }
1241
1242 void look_for_ansi(void)
1243 {
1244         fd_set rfds;
1245         struct timeval tv;
1246         char abuf[512];
1247         time_t now;
1248         int a;
1249
1250         if (rc_ansi_color == 0) {
1251                 enable_color = 0;
1252         } else if (rc_ansi_color == 1) {
1253                 enable_color = 1;
1254         } else if (rc_ansi_color == 2) {
1255
1256                 /* otherwise, do the auto-detect */
1257
1258                 strcpy(abuf, "");
1259
1260                 time(&now);
1261                 if ((now - AnsiDetect) < 2)
1262                         sleep(1);
1263
1264                 do {
1265                         FD_ZERO(&rfds);
1266                         FD_SET(0, &rfds);
1267                         tv.tv_sec = 0;
1268                         tv.tv_usec = 1;
1269
1270                         select(1, &rfds, NULL, NULL, &tv);
1271                         if (FD_ISSET(0, &rfds)) {
1272                                 abuf[strlen(abuf) + 1] = 0;
1273                                 read(0, &abuf[strlen(abuf)], 1);
1274                         }
1275                 } while (FD_ISSET(0, &rfds));
1276
1277                 for (a = 0; a < strlen(abuf); ++a) {
1278                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1279                             && (abuf[a + 2] == '?')) {
1280                                 enable_color = 1;
1281                         }
1282                 }
1283         }
1284 }
1285
1286
1287 /*
1288  * Display key options (highlight hotkeys inside angle brackets)
1289  */
1290 void keyopt(char *buf) {
1291         int i;
1292
1293         color(DIM_WHITE);
1294         for (i=0; i<strlen(buf); ++i) {
1295                 if (buf[i]=='<') {
1296                         putc(buf[i], stdout);
1297                         color(BRIGHT_MAGENTA);
1298                 } else {
1299                         if (buf[i]=='>') {
1300                                 color(DIM_WHITE);
1301                         }
1302                         putc(buf[i], stdout);
1303                 }
1304         }
1305         color(DIM_WHITE);
1306 }
1307
1308
1309
1310 /*
1311  * Present a key-menu line choice type of thing
1312  */
1313 char keymenu(char *menuprompt, char *menustring) {
1314         int i, c, a;
1315         int choices;
1316         int do_prompt = 0;
1317         char buf[256];
1318         int ch;
1319         int display_prompt = 1;
1320
1321         choices = num_tokens(menustring, '|');
1322
1323         if (menuprompt != NULL) do_prompt = 1;
1324         if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1325
1326         while (1) {
1327                 if (display_prompt) {
1328                         if (do_prompt) {
1329                                 printf("%s ", menuprompt);
1330                         } 
1331                         else {
1332                                 for (i=0; i<choices; ++i) {
1333                                         extract(buf, menustring, i);
1334                                         keyopt(buf);
1335                                         printf(" ");
1336                                 }
1337                         }
1338                         printf(" -> ");
1339                         display_prompt = 0;
1340                 }
1341                 ch = lkey();
1342         
1343                 if ( (do_prompt) && (ch=='?') ) {
1344                         printf("\rOne of...                               ");
1345                         printf("                                      \n");
1346                         for (i=0; i<choices; ++i) {
1347                                 extract(buf, menustring, i);
1348                                 printf("   ");
1349                                 keyopt(buf);
1350                                 printf("\n");
1351                         }
1352                         printf("\n");
1353                         display_prompt = 1;
1354                 }
1355
1356                 for (i=0; i<choices; ++i) {
1357                         extract(buf, menustring, i);
1358                         for (c=1; c<strlen(buf); ++c) {
1359                                 if ( (ch == tolower(buf[c]))
1360                                    && (buf[c-1]=='<')
1361                                    && (buf[c+1]=='>') ) {
1362                                         for (a=0; a<strlen(buf); ++a) {
1363                                                 if ( (a!=(c-1)) && (a!=(c+1))) {
1364                                                         putc(buf[a], stdout);
1365                                                 }
1366                                         }
1367                                         printf("\n\n");
1368                                         return ch;
1369                                 }
1370                         }
1371                 }
1372         }
1373 }