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