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