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