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