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