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