]> code.citadel.org Git - citadel.git/blob - citadel/commands.c
color 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(BRIGHT_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         printf(": ");
316         color(BRIGHT_CYAN);
317         getline(buf, len);
318         if (buf[0] != 0)
319                 strcpy(str, buf);
320         color(DIM_WHITE);
321 }
322
323 /*
324  * boolprompt()  -  prompt for a yes/no, print the existing value and
325  *                  allow the user to press return to keep it...
326  */
327 int boolprompt(char *prompt, int prev_val)
328 {
329         int r;
330
331         color(DIM_WHITE);
332         printf("%s ", prompt);
333         color(DIM_MAGENTA);
334         printf(" [");
335         color(BRIGHT_MAGENTA);
336         printf("%s", (prev_val ? "Yes" : "No"));
337         color(DIM_MAGENTA);
338         printf("]: ");
339         color(BRIGHT_CYAN);
340         r = (yesno_d(prev_val));
341         color(DIM_WHITE);
342         return r;
343 }
344
345 /* 
346  * intprompt()  -  like strprompt(), except for an integer
347  *                 (note that it RETURNS the new value!)
348  */
349 int intprompt(char *prompt, int ival, int imin, int imax)
350 {
351         char buf[16];
352         int i;
353         i = ival;
354         do {
355                 snprintf(buf, sizeof buf, "%d", i);
356                 strprompt(prompt, buf, 15);
357                 i = atoi(buf);
358                 if (i < imin)
359                         printf("*** Must be no less than %d.\n", imin);
360                 if (i > imax)
361                         printf("*** Must be no more than %d.\n", imax);
362         } while ((i < imin) || (i > imax));
363         return (i);
364 }
365
366 /* 
367  * newprompt()  -  prompt for a string with no existing value
368  *                 (clears out string buffer first)
369  */
370 void newprompt(char *prompt, char *str, int len)
371 {
372         color(BRIGHT_MAGENTA);
373         printf("%s", prompt);
374         color(DIM_MAGENTA);
375         getline(str, len);
376         color(DIM_WHITE);
377 }
378
379
380 int lkey(void)
381 {                               /* returns a lower case value */
382         int a;
383         a = inkey();
384         if (isupper(a))
385                 a = tolower(a);
386         return (a);
387 }
388
389 /*
390  * parse the citadel.rc file
391  */
392 void load_command_set(void)
393 {
394         FILE *ccfile;
395         char buf[256];
396         struct citcmd *cptr;
397         struct citcmd *lastcmd = NULL;
398         int a, d;
399         int b = 0;
400
401
402         /* first, set up some defaults for non-required variables */
403
404         strcpy(editor_path, "");
405         strcpy(printcmd, "");
406         strcpy(rc_username, "");
407         strcpy(rc_password, "");
408         rc_floor_mode = 0;
409         rc_exp_beep = 1;
410         rc_allow_attachments = 0;
411         strcpy(rc_exp_cmd, "");
412         rc_display_message_numbers = 0;
413         rc_force_mail_prompts = 0;
414         rc_ansi_color = 0;
415
416         /* now try to open the citadel.rc file */
417
418         ccfile = NULL;
419         if (getenv("HOME") != NULL) {
420                 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
421                 ccfile = fopen(buf, "r");
422         }
423         if (ccfile == NULL) {
424                 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
425         }
426         if (ccfile == NULL) {
427                 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
428                 ccfile = fopen(buf, "r");
429         }
430         if (ccfile == NULL) {
431                 ccfile = fopen("./citadel.rc", "r");
432         }
433         if (ccfile == NULL) {
434                 perror("commands: cannot open citadel.rc");
435                 logoff(errno);
436         }
437         while (fgets(buf, 256, ccfile) != NULL) {
438                 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
439                         buf[strlen(buf) - 1] = 0;
440
441                 if (!struncmp(buf, "editor=", 7))
442                         strcpy(editor_path, &buf[7]);
443
444                 if (!struncmp(buf, "printcmd=", 9))
445                         strcpy(printcmd, &buf[9]);
446
447                 if (!struncmp(buf, "expcmd=", 7))
448                         strcpy(rc_exp_cmd, &buf[7]);
449
450                 if (!struncmp(buf, "local_screen_dimensions=", 24))
451                         have_xterm = (char) atoi(&buf[24]);
452
453                 if (!struncmp(buf, "use_floors=", 11)) {
454                         if (!strucmp(&buf[11], "yes"))
455                                 rc_floor_mode = RC_YES;
456                         if (!strucmp(&buf[11], "no"))
457                                 rc_floor_mode = RC_NO;
458                         if (!strucmp(&buf[11], "default"))
459                                 rc_floor_mode = RC_DEFAULT;
460                 }
461                 if (!struncmp(buf, "beep=", 5)) {
462                         rc_exp_beep = atoi(&buf[5]);
463                 }
464                 if (!struncmp(buf, "allow_attachments=", 18)) {
465                         rc_allow_attachments = atoi(&buf[18]);
466                 }
467                 if (!struncmp(buf, "display_message_numbers=", 24)) {
468                         rc_display_message_numbers = atoi(&buf[24]);
469                 }
470                 if (!struncmp(buf, "force_mail_prompts=", 19)) {
471                         rc_force_mail_prompts = atoi(&buf[19]);
472                 }
473                 if (!struncmp(buf, "ansi_color=", 11)) {
474                         if (!strncasecmp(&buf[11], "on", 2))
475                                 rc_ansi_color = 1;
476                         if (!strncasecmp(&buf[11], "auto", 4))
477                                 rc_ansi_color = 2;      /* autodetect */
478                         if (!strncasecmp(&buf[11], "user", 4))
479                                 rc_ansi_color = 3;      /* user config */
480                 }
481                 if (!struncmp(buf, "username=", 9))
482                         strcpy(rc_username, &buf[9]);
483
484                 if (!struncmp(buf, "password=", 9))
485                         strcpy(rc_password, &buf[9]);
486
487                 if (!struncmp(buf, "cmd=", 4)) {
488                         strcpy(buf, &buf[4]);
489
490                         cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
491
492                         cptr->c_cmdnum = atoi(buf);
493                         for (d = strlen(buf); d >= 0; --d)
494                                 if (buf[d] == ',')
495                                         b = d;
496                         strcpy(buf, &buf[b + 1]);
497
498                         cptr->c_axlevel = atoi(buf);
499                         for (d = strlen(buf); d >= 0; --d)
500                                 if (buf[d] == ',')
501                                         b = d;
502                         strcpy(buf, &buf[b + 1]);
503
504                         for (a = 0; a < 5; ++a)
505                                 cptr->c_keys[a][0] = 0;
506
507                         a = 0;
508                         b = 0;
509                         buf[strlen(buf) + 1] = 0;
510                         while (strlen(buf) > 0) {
511                                 b = strlen(buf);
512                                 for (d = strlen(buf); d >= 0; --d)
513                                         if (buf[d] == ',')
514                                                 b = d;
515                                 strncpy(cptr->c_keys[a], buf, b);
516                                 cptr->c_keys[a][b] = 0;
517                                 if (buf[b] == ',')
518                                         strcpy(buf, &buf[b + 1]);
519                                 else
520                                         strcpy(buf, "");
521                                 ++a;
522                         }
523
524                         cptr->next = NULL;
525                         if (cmdlist == NULL)
526                                 cmdlist = cptr;
527                         else
528                                 lastcmd->next = cptr;
529                         lastcmd = cptr;
530                 }
531         }
532         fclose(ccfile);
533 }
534
535
536
537 /*
538  * return the key associated with a command
539  */
540 char keycmd(char *cmdstr)
541 {
542         int a;
543
544         for (a = 0; a < strlen(cmdstr); ++a)
545                 if (cmdstr[a] == '&')
546                         return (tolower(cmdstr[a + 1]));
547         return (0);
548 }
549
550
551 /*
552  * Output the string from a key command without the ampersand
553  * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
554  */
555 char *cmd_expand(char *strbuf, int mode)
556 {
557         int a;
558         static char exp[64];
559         char buf[256];
560
561         strcpy(exp, strbuf);
562
563         for (a = 0; a < strlen(exp); ++a) {
564                 if (strbuf[a] == '&') {
565
566                         if (mode == 0) {
567                                 strcpy(&exp[a], &exp[a + 1]);
568                         }
569                         if (mode == 1) {
570                                 exp[a] = '<';
571                                 strcpy(buf, &exp[a + 2]);
572                                 exp[a + 2] = '>';
573                                 exp[a + 3] = 0;
574                                 strcat(exp, buf);
575                         }
576                 }
577                 if (!strncmp(&exp[a], "^r", 2)) {
578                         strcpy(buf, exp);
579                         strcpy(&exp[a], room_name);
580                         strcat(exp, &buf[a + 2]);
581                 }
582                 if (!strncmp(&exp[a], "^c", 2)) {
583                         exp[a] = ',';
584                         strcpy(&exp[a + 1], &exp[a + 2]);
585                 }
586         }
587
588         return (exp);
589 }
590
591
592
593 /*
594  * Comparison function to determine if entered commands match a
595  * command loaded from the config file.
596  */
597 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
598 {
599         int a;
600         int cmdax;
601
602         cmdax = 0;
603         if (is_room_aide)
604                 cmdax = 1;
605         if (axlevel >= 6)
606                 cmdax = 2;
607
608         for (a = 0; a < ncomp; ++a) {
609                 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
610                     || (cptr->c_axlevel > cmdax))
611                         return (0);
612         }
613         return (1);
614 }
615
616
617 /*
618  * This function returns 1 if a given command requires a string input
619  */
620 int requires_string(struct citcmd *cptr, int ncomp)
621 {
622         int a;
623         char buf[64];
624
625         strcpy(buf, cptr->c_keys[ncomp - 1]);
626         for (a = 0; a < strlen(buf); ++a) {
627                 if (buf[a] == ':')
628                         return (1);
629         }
630         return (0);
631 }
632
633
634 /*
635  * Input a command at the main prompt.
636  * This function returns an integer command number.  If the command prompts
637  * for a string then it is placed in the supplied buffer.
638  */
639 int getcmd(char *argbuf)
640 {
641         char cmdbuf[5];
642         int cmdspaces[5];
643         int cmdpos;
644         int ch;
645         int a;
646         int got;
647         int this_lazy_cmd;
648         struct citcmd *cptr;
649
650         /* Switch color support on or off if we're in user mode */
651         if (rc_ansi_color == 3) {
652                 if (userflags & US_COLOR)
653                         enable_color = 1;
654                 else
655                         enable_color = 0;
656         }
657         /* if we're running in idiot mode, display a cute little menu */
658         IFNEXPERT formout("mainmenu");
659
660         print_express();        /* print express messages if there are any */
661         strcpy(argbuf, "");
662         cmdpos = 0;
663         for (a = 0; a < 5; ++a)
664                 cmdbuf[a] = 0;
665         /* now the room prompt... */
666         ok_to_interrupt = 1;
667         color(BRIGHT_WHITE);
668         printf("\n%s", room_name);
669         color(DIM_WHITE);
670         printf("%c ", room_prompt(room_flags));
671         fflush(stdout);
672
673         while (1) {
674                 ch = inkey();
675                 ok_to_interrupt = 0;
676
677                 /* Handle the backspace key, but only if there's something
678                  * to backspace over...
679                  */
680                 if ((ch == 8) && (cmdpos > 0)) {
681                         back(cmdspaces[cmdpos - 1] + 1);
682                         cmdbuf[cmdpos] = 0;
683                         --cmdpos;
684                 }
685                 /* Spacebar invokes "lazy traversal" commands */
686                 if ((ch == 32) && (cmdpos == 0)) {
687                         this_lazy_cmd = next_lazy_cmd;
688                         if (this_lazy_cmd == 13)
689                                 next_lazy_cmd = 5;
690                         if (this_lazy_cmd == 5)
691                                 next_lazy_cmd = 13;
692                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
693                                 if (cptr->c_cmdnum == this_lazy_cmd) {
694                                         for (a = 0; a < 5; ++a)
695                                                 if (cptr->c_keys[a][0] != 0)
696                                                         printf("%s ", cmd_expand(
697                                                                                         cptr->c_keys[a], 0));
698                                         printf("\n");
699                                         return (this_lazy_cmd);
700                                 }
701                         }
702                         printf("\n");
703                         return (this_lazy_cmd);
704                 }
705                 /* Otherwise, process the command */
706                 cmdbuf[cmdpos] = tolower(ch);
707
708                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
709                         if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
710
711                                 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
712                                 cmdspaces[cmdpos] = strlen(
713                                     cmd_expand(cptr->c_keys[cmdpos], 0));
714                                 if (cmdpos < 4)
715                                         if ((cptr->c_keys[cmdpos + 1]) != 0)
716                                                 putc(' ', stdout);
717                                 ++cmdpos;
718                         }
719                 }
720
721                 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
722                         if (cmdmatch(cmdbuf, cptr, 5)) {
723                                 /* We've found our command. */
724                                 if (requires_string(cptr, cmdpos)) {
725                                         getline(argbuf, 32);
726                                 } else {
727                                         printf("\n");
728                                 }
729
730                                 /* If this command is one that changes rooms,
731                                  * then the next lazy-command (space bar)
732                                  * should be "read new" instead of "goto"
733                                  */
734                                 if ((cptr->c_cmdnum == 5)
735                                     || (cptr->c_cmdnum == 6)
736                                     || (cptr->c_cmdnum == 47)
737                                     || (cptr->c_cmdnum == 52)
738                                     || (cptr->c_cmdnum == 16)
739                                     || (cptr->c_cmdnum == 20))
740                                         next_lazy_cmd = 13;
741
742                                 return (cptr->c_cmdnum);
743
744                         }
745                 }
746
747                 if (ch == '?') {
748                         printf("\rOne of ...                         \n");
749                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
750                                 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
751                                         for (a = 0; a < 5; ++a) {
752                                                 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
753                                         }
754                                         printf("\n");
755                                 }
756                         }
757
758                         printf("\n%s%c ", room_name, room_prompt(room_flags));
759                         got = 0;
760                         for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
761                                 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
762                                         for (a = 0; a < cmdpos; ++a) {
763                                                 printf("%s ",
764                                                        cmd_expand(cptr->c_keys[a], 0));
765                                         }
766                                         got = 1;
767                                 }
768                         }
769                 }
770         }
771
772 }
773
774
775
776
777
778 /*
779  * set tty modes.  commands are:
780  * 
781  * 0 - set to bbs mode, intr/quit disabled
782  * 1 - set to bbs mode, intr/quit enabled
783  * 2 - save current settings for later restoral
784  * 3 - restore saved settings
785  */
786 #ifdef HAVE_TERMIOS_H
787 void sttybbs(int cmd)
788 {                               /* SysV version of sttybbs() */
789         struct termios live;
790         static struct termios saved_settings;
791         static int last_cmd = 0;
792
793         if (cmd == SB_LAST)
794                 cmd = last_cmd;
795         else
796                 last_cmd = cmd;
797
798         if ((cmd == 0) || (cmd == 1)) {
799                 tcgetattr(0, &live);
800                 live.c_iflag = ISTRIP | IXON | IXANY;
801                 live.c_oflag = OPOST | ONLCR;
802                 live.c_lflag = ISIG | NOFLSH;
803
804                 if (cmd == SB_YES_INTR) {
805                         live.c_cc[VINTR] = NEXT_KEY;
806                         live.c_cc[VQUIT] = STOP_KEY;
807                         signal(SIGINT, *sighandler);
808                         signal(SIGQUIT, *sighandler);
809                 } else {
810                         signal(SIGINT, SIG_IGN);
811                         signal(SIGQUIT, SIG_IGN);
812                         live.c_cc[VINTR] = (-1);
813                         live.c_cc[VQUIT] = (-1);
814                 }
815
816                 /* do we even need this stuff anymore? */
817                 /* live.c_line=0; */
818                 live.c_cc[VERASE] = 8;
819                 live.c_cc[VKILL] = 24;
820                 live.c_cc[VEOF] = 1;
821                 live.c_cc[VEOL] = 255;
822                 live.c_cc[VEOL2] = 0;
823                 live.c_cc[VSTART] = 0;
824                 tcsetattr(0, TCSADRAIN, &live);
825         }
826         if (cmd == 2) {
827                 tcgetattr(0, &saved_settings);
828         }
829         if (cmd == 3) {
830                 tcsetattr(0, TCSADRAIN, &saved_settings);
831         }
832 }
833 #else
834 void sttybbs(int cmd)
835 {                               /* BSD version of sttybbs() */
836         struct sgttyb live;
837         static struct sgttyb saved_settings;
838
839         if ((cmd == 0) || (cmd == 1)) {
840                 gtty(0, &live);
841                 live.sg_flags |= CBREAK;
842                 live.sg_flags |= CRMOD;
843                 live.sg_flags |= NL1;
844                 live.sg_flags &= ~ECHO;
845                 if (cmd == 1)
846                         live.sg_flags |= NOFLSH;
847                 stty(0, &live);
848         }
849         if (cmd == 2) {
850                 gtty(0, &saved_settings);
851         }
852         if (cmd == 3) {
853                 stty(0, &saved_settings);
854         }
855 }
856 #endif
857
858
859 /*
860  * display_help()  -  help file viewer
861  */
862 void display_help(char *name)
863 {
864         formout(name);
865 }
866
867
868 /*
869  * fmout()  -  Citadel text formatter and paginator
870  */
871 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
872                         /* screen width to use */
873                         /* file to read from, or NULL to read from server */
874                         /* nonzero if we should use the paginator */
875                         /* screen height to use */
876                         /* starting value for lines_printed, -1 for global */
877                         /* nonzero if we should use hypertext mode */
878 {
879         int a, b, c, d, old;
880         int real = (-1);
881         char aaa[140];
882         char buffer[512];
883         int eof_flag = 0;
884
885         if (starting_lp >= 0) {
886                 lines_printed = starting_lp;
887         }
888         strcpy(aaa, "");
889         old = 255;
890         strcpy(buffer, "");
891         c = 1;                  /* c is the current pos */
892
893         sigcaught = 0;
894         sttybbs(1);
895
896       FMTA:while ((eof_flag == 0) && (strlen(buffer) < 126)) {
897                 if (sigcaught)
898                         goto OOPS;
899                 if (fp != NULL) {       /* read from file */
900                         if (feof(fp))
901                                 eof_flag = 1;
902                         if (eof_flag == 0) {
903                                 a = getc(fp);
904                                 buffer[strlen(buffer) + 1] = 0;
905                                 buffer[strlen(buffer)] = a;
906                         }
907                 } else {        /* read from server */
908                         d = strlen(buffer);
909                         serv_gets(&buffer[d]);
910                         while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
911                                 buffer[strlen(buffer) - 1] = 0;
912                         if (!strcmp(&buffer[d], "000")) {
913                                 buffer[d] = 0;
914                                 eof_flag = 1;
915                                 while (isspace(buffer[strlen(buffer) - 1]))
916                                         buffer[strlen(buffer) - 1] = 0;
917                         }
918                         d = strlen(buffer);
919                         buffer[d] = 10;
920                         buffer[d + 1] = 0;
921                 }
922         }
923
924         buffer[strlen(buffer) + 1] = 0;
925         a = buffer[0];
926         strcpy(buffer, &buffer[1]);
927
928         old = real;
929         real = a;
930         if (a <= 0)
931                 goto FMTEND;
932
933         if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
934                 a = 32;
935         if (((old == 13) || (old == 10)) && (isspace(real))) {
936                 printf("\n");
937                 ++lines_printed;
938                 lines_printed = checkpagin(lines_printed, pagin, height);
939                 c = 1;
940         }
941         if (a > 126)
942                 goto FMTA;
943
944         if (a > 32) {
945                 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
946                         printf("\n%s", aaa);
947                         c = strlen(aaa);
948                         aaa[0] = 0;
949                         ++lines_printed;
950                         lines_printed = checkpagin(lines_printed, pagin, height);
951                 }
952                 b = strlen(aaa);
953                 aaa[b] = a;
954                 aaa[b + 1] = 0;
955         }
956         if (a == 32) {
957                 if ((strlen(aaa) + c) > (width - 5)) {
958                         c = 1;
959                         printf("\n");
960                         ++lines_printed;
961                         lines_printed = checkpagin(lines_printed, pagin, height);
962                 }
963                 printf("%s ", aaa);
964                 ++c;
965                 c = c + strlen(aaa);
966                 strcpy(aaa, "");
967                 goto FMTA;
968         }
969         if ((a == 13) || (a == 10)) {
970                 printf("%s\n", aaa);
971                 c = 1;
972                 ++lines_printed;
973                 lines_printed = checkpagin(lines_printed, pagin, height);
974                 strcpy(aaa, "");
975                 goto FMTA;
976         }
977         goto FMTA;
978
979         /* signal caught; drain the server */
980       OOPS:do {
981                 serv_gets(aaa);
982         } while (strcmp(aaa, "000"));
983
984       FMTEND:printf("\n");
985         ++lines_printed;
986         lines_printed = checkpagin(lines_printed, pagin, height);
987         return (sigcaught);
988 }
989
990
991 /*
992  * support ANSI color if defined
993  */
994 void color(int colornum)
995 {
996         static int is_bold = 0;
997         static int hold_color, current_color;
998
999         if (colornum == COLOR_PUSH) {
1000                 hold_color = current_color;
1001                 return;
1002         }
1003
1004         if (colornum == COLOR_POP) {
1005                 color(hold_color);
1006                 return;
1007         }
1008
1009         current_color = colornum;
1010         if (enable_color) {
1011                 printf("\033[3%dm", (colornum % 8));
1012                 if ((colornum >= 8) && (is_bold == 0)) {
1013                         printf("\033[1m");
1014                         is_bold = 1;
1015                 } else if ((colornum < 8) && (is_bold == 1)) {
1016                         printf("\033[0m");
1017                         is_bold = 0;
1018                 }
1019                 fflush(stdout);
1020         }
1021 }
1022
1023 void cls(int colornum)
1024 {
1025         if (enable_color) {
1026                 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1027                 fflush(stdout);
1028         }
1029 }
1030
1031
1032 /*
1033  * Detect whether ANSI color is available (answerback)
1034  */
1035 void send_ansi_detect(void)
1036 {
1037         if (rc_ansi_color == 2) {
1038                 printf("\033[c");
1039                 fflush(stdout);
1040                 time(&AnsiDetect);
1041         }
1042 }
1043
1044 void look_for_ansi(void)
1045 {
1046         fd_set rfds;
1047         struct timeval tv;
1048         char abuf[512];
1049         time_t now;
1050         int a;
1051
1052         if (rc_ansi_color == 0) {
1053                 enable_color = 0;
1054         } else if (rc_ansi_color == 1) {
1055                 enable_color = 1;
1056         } else if (rc_ansi_color == 2) {
1057
1058                 /* otherwise, do the auto-detect */
1059
1060                 strcpy(abuf, "");
1061
1062                 time(&now);
1063                 if ((now - AnsiDetect) < 2)
1064                         sleep(1);
1065
1066                 do {
1067                         FD_ZERO(&rfds);
1068                         FD_SET(0, &rfds);
1069                         tv.tv_sec = 0;
1070                         tv.tv_usec = 1;
1071
1072                         select(1, &rfds, NULL, NULL, &tv);
1073                         if (FD_ISSET(0, &rfds)) {
1074                                 abuf[strlen(abuf) + 1] = 0;
1075                                 read(0, &abuf[strlen(abuf)], 1);
1076                         }
1077                 } while (FD_ISSET(0, &rfds));
1078
1079                 for (a = 0; a < strlen(abuf); ++a) {
1080                         if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1081                             && (abuf[a + 2] == '?')) {
1082                                 enable_color = 1;
1083                         }
1084                 }
1085         }
1086 }
1087
1088
1089 /*
1090  * Display key options (highlight hotkeys inside angle brackets)
1091  */
1092 void keyopt(char *buf) {
1093         int i;
1094
1095         color(DIM_WHITE);
1096         for (i=0; i<strlen(buf); ++i) {
1097                 if (buf[i]=='<') {
1098                         putc(buf[i], stdout);
1099                         color(BRIGHT_MAGENTA);
1100                 } else {
1101                         if (buf[i]=='>') {
1102                                 color(DIM_WHITE);
1103                         }
1104                         putc(buf[i], stdout);
1105                 }
1106         }
1107         color(DIM_WHITE);
1108 }