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