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