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