4 * commands.c - front end for Citadel
6 * This version is the traditional command parser for room prompts.
19 #include <sys/types.h>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
38 #include "citadel_decls.h"
40 #include "routines2.h"
49 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
54 int rc_allow_attachments;
55 int rc_display_message_numbers;
56 int rc_force_mail_prompts;
60 int next_lazy_cmd = 5;
62 struct citcmd *cmdlist = NULL;
65 /* these variables are local to this module */
66 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
67 int ok_to_interrupt = 0; /* print express msgs asynchronously */
68 time_t AnsiDetect; /* when did we send the detect code? */
69 int enable_color = 0; /* nonzero for ANSI color */
73 * print_express() - print express messages if there are any
75 void print_express(void) {
79 if (express_msgs == 0) return;
83 if (buf[0]!='1') return;
85 if (strlen(rc_exp_cmd) > 0) {
86 outpipe = popen(rc_exp_cmd, "w");
87 if (outpipe != NULL) {
88 while (serv_gets(buf), strcmp(buf,"000")) {
89 fprintf(outpipe, "%s\n", buf);
96 /* fall back to built-in express message display */
102 while (serv_gets(buf), strcmp(buf,"000")) {
110 void set_keepalives(int s)
112 keepalives_enabled = (char)s;
116 * This loop handles the "keepalive" messages sent to the server when idling.
118 void do_keepalive(void) {
120 static time_t idlet = 0;
124 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
127 if (keepalives_enabled != KA_NO) {
129 if (keepalives_enabled == KA_YES) {
133 if (ok_to_interrupt == 1) {
136 printf("%s%c ",room_name,
137 room_prompt(room_flags));
146 int inkey(void) { /* get a character from the keyboard, with */
147 int a; /* the watchdog timer in effect if necessary */
150 time_t start_time, now;
157 /* This loop waits for keyboard input. If the keepalive
158 * timer expires, it sends a keepalive to the server if
159 * necessary and then waits again.
166 tv.tv_sec = S_KEEPALIVE;
170 if (((now-start_time) > SLEEPING)
171 && (SLEEPING != 0) && (getppid()==1)) {
172 printf("Sleeping? Call again.\n");
176 select(1, &rfds, NULL, NULL, &tv);
177 } while (!FD_ISSET(0, &rfds));
182 /* At this point, there's input, so fetch it.
183 * (There's a hole in the bucket...)
190 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
191 && ((a<32)||(a>126))) a=0;
196 void getline(char *string, int lim) /* Gets a line from the terminal */
197 /* Pointer to string buffer */
198 /* Maximum length - if negative, no-show */
203 if (lim<0) { lim=(0-lim); flag=1; }
206 GLA: a=inkey(); a=(a&127);
207 if ((a==8)&&(strlen(string)==0)) goto GLA;
208 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
209 if ((a==8)&&(string[0]!=0)) {
210 string[strlen(string)-1]=0;
211 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
212 if ((a==13)||(a==10)) {
221 if (flag==0) putc(a,stdout);
222 if (flag==1) putc('*',stdout);
228 * strprompt() - prompt for a string, print the existing value and
229 * allow the user to press return to keep it...
231 void strprompt(char *prompt, char *str, int len)
236 printf("%s [", prompt);
244 if (buf[0]!=0) strcpy(str,buf);
248 * intprompt() - like strprompt(), except for an integer
249 * (note that it RETURNS the new value!)
251 int intprompt(char *prompt, int ival, int imin, int imax)
258 strprompt(prompt,buf,15);
260 if (i<imin) printf("*** Must be no less than %d.\n",imin);
261 if (i>imax) printf("*** Must be no more than %d.\n",imax);
262 } while((i<imin)||(i>imax));
267 * newprompt() - prompt for a string with no existing value
268 * (clears out string buffer first)
270 void newprompt(char *prompt, char *str, int len)
280 int lkey(void) { /* returns a lower case value */
283 if (isupper(a)) a=tolower(a);
288 * parse the citadel.rc file
290 void load_command_set(void) {
294 struct citcmd *lastcmd = NULL;
299 /* first, set up some defaults for non-required variables */
301 strcpy(editor_path,"");
303 strcpy(rc_username,"");
304 strcpy(rc_password,"");
307 rc_allow_attachments = 0;
308 strcpy(rc_exp_cmd, "");
309 rc_display_message_numbers = 0;
310 rc_force_mail_prompts = 0;
313 /* now try to open the citadel.rc file */
316 if (getenv("HOME") != NULL) {
317 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
318 ccfile = fopen(buf,"r");
321 ccfile = fopen("/usr/local/lib/citadel.rc","r");
324 sprintf(buf,"%s/citadel.rc",BBSDIR);
325 ccfile = fopen(buf,"r");
328 perror("commands: cannot open citadel.rc");
332 while (fgets(buf, 256, ccfile) != NULL) {
333 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
334 buf[strlen(buf)-1] = 0;
336 if (!struncmp(buf,"editor=",7))
337 strcpy(editor_path,&buf[7]);
339 if (!struncmp(buf,"printcmd=",9))
340 strcpy(printcmd,&buf[9]);
342 if (!struncmp(buf,"expcmd=",7))
343 strcpy(rc_exp_cmd,&buf[7]);
345 if (!struncmp(buf,"local_screen_dimensions=",24))
346 have_xterm = (char)atoi(&buf[24]);
348 if (!struncmp(buf,"use_floors=",11)) {
349 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
350 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
351 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
354 if (!struncmp(buf,"beep=",5)) {
355 rc_exp_beep = atoi(&buf[5]);
358 if (!struncmp(buf,"allow_attachments=", 18)) {
359 rc_allow_attachments = atoi(&buf[18]);
362 if (!struncmp(buf,"display_message_numbers=", 24)) {
363 rc_display_message_numbers = atoi(&buf[24]);
366 if (!struncmp(buf,"force_mail_prompts=", 19)) {
367 rc_force_mail_prompts = atoi(&buf[19]);
370 if (!struncmp(buf,"ansi_color=", 11)) {
371 if (!strncasecmp(&buf[11], "on", 2))
373 if (!strncasecmp(&buf[11], "auto", 4))
374 rc_ansi_color = 2; /* autodetect */
377 if (!struncmp(buf,"username=",9))
378 strcpy(rc_username,&buf[9]);
380 if (!struncmp(buf,"password=",9))
381 strcpy(rc_password,&buf[9]);
383 if (!struncmp(buf,"cmd=",4)) {
386 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
388 cptr->c_cmdnum = atoi (buf);
389 for (d=strlen(buf); d>=0; --d)
390 if (buf[d]==',') b=d;
391 strcpy (buf, &buf[b+1]);
393 cptr->c_axlevel = atoi (buf);
394 for (d=strlen(buf); d>=0; --d)
395 if (buf[d]==',') b=d;
396 strcpy (buf, &buf[b+1]);
398 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
401 buf[strlen(buf)+1] = 0;
402 while (strlen(buf) > 0) {
404 for (d=strlen(buf); d>=0; --d)
405 if (buf[d]==',') b=d;
406 strncpy(cptr->c_keys[a],buf,b);
407 cptr->c_keys[a][b] = 0;
409 strcpy(buf,&buf[b+1]);
416 if (cmdlist == NULL) cmdlist = cptr;
417 else lastcmd->next = cptr;
427 * return the key associated with a command
429 char keycmd(char *cmdstr)
433 for (a=0; a<strlen(cmdstr); ++a)
435 return(tolower(cmdstr[a+1]));
441 * Output the string from a key command without the ampersand
442 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
444 char *cmd_expand(char *strbuf, int mode)
452 for (a=0; a<strlen(exp); ++a) {
453 if (strbuf[a] == '&') {
456 strcpy(&exp[a],&exp[a+1]);
461 strcpy(buf,&exp[a+2]);
469 if (!strncmp(&exp[a],"^r",2)) {
471 strcpy(&exp[a],room_name);
472 strcat(exp,&buf[a+2]);
475 if (!strncmp(&exp[a],"^c",2)) {
477 strcpy(&exp[a+1],&exp[a+2]);
488 * Comparison function to determine if entered commands match a
489 * command loaded from the config file.
491 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
497 if (is_room_aide) cmdax = 1;
498 if (axlevel >=6) cmdax = 2;
500 for (a=0; a<ncomp; ++a) {
501 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
502 || (cptr->c_axlevel > cmdax) )
510 * This function returns 1 if a given command requires a string input
512 int requires_string(struct citcmd *cptr, int ncomp)
517 strcpy(buf,cptr->c_keys[ncomp-1]);
518 for (a=0; a<strlen(buf); ++a) {
519 if (buf[a]==':') return(1);
526 * Input a command at the main prompt.
527 * This function returns an integer command number. If the command prompts
528 * for a string then it is placed in the supplied buffer.
530 int getcmd(char *argbuf)
541 /* if we're running in idiot mode, display a cute little menu */
542 IFNEXPERT formout("mainmenu");
544 print_express(); /* print express messages if there are any */
547 for (a=0; a<5; ++a) cmdbuf[a]=0;
548 /* now the room prompt... */
550 printf("\n%s%c ",room_name,room_prompt(room_flags));
557 /* Handle the backspace key, but only if there's something
558 * to backspace over...
560 if ( (ch == 8) && (cmdpos > 0) ) {
561 back(cmdspaces[cmdpos-1] + 1);
566 /* Spacebar invokes "lazy traversal" commands */
567 if ( (ch == 32) && (cmdpos == 0) ) {
568 this_lazy_cmd = next_lazy_cmd;
569 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
570 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
571 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
572 if (cptr->c_cmdnum == this_lazy_cmd) {
574 if (cptr->c_keys[a][0] != 0)
575 printf("%s ", cmd_expand(
576 cptr->c_keys[a], 0));
578 return(this_lazy_cmd);
582 return(this_lazy_cmd);
585 /* Otherwise, process the command */
586 cmdbuf[cmdpos] = tolower(ch);
588 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
589 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
591 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
592 cmdspaces[cmdpos] = strlen(
593 cmd_expand(cptr->c_keys[cmdpos],0) );
595 if ((cptr->c_keys[cmdpos+1]) != 0)
601 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
602 if (cmdmatch(cmdbuf,cptr,5)) {
603 /* We've found our command. */
604 if (requires_string(cptr,cmdpos)) {
611 /* If this command is one that changes rooms,
612 * then the next lazy-command (space bar)
613 * should be "read new" instead of "goto"
615 if ((cptr->c_cmdnum==5)
616 ||(cptr->c_cmdnum==6)
617 ||(cptr->c_cmdnum==47)
618 ||(cptr->c_cmdnum==52)
619 ||(cptr->c_cmdnum==16)
620 ||(cptr->c_cmdnum==20))
623 return(cptr->c_cmdnum);
629 printf("\rOne of ... \n");
630 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
631 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
632 for (a=0; a<5; ++a) {
633 printf("%s ",cmd_expand(cptr->c_keys[a],1));
639 printf("\n%s%c ",room_name,room_prompt(room_flags));
641 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
642 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
643 for (a=0; a<cmdpos; ++a) {
645 cmd_expand(cptr->c_keys[a],0));
661 * set tty modes. commands are:
663 * 0 - set to bbs mode, intr/quit disabled
664 * 1 - set to bbs mode, intr/quit enabled
665 * 2 - save current settings for later restoral
666 * 3 - restore saved settings
668 #ifdef HAVE_TERMIOS_H
669 void sttybbs(int cmd) /* SysV version of sttybbs() */
672 static struct termios saved_settings;
673 static int last_cmd = 0;
680 if ( (cmd == 0) || (cmd == 1) ) {
682 live.c_iflag=ISTRIP|IXON|IXANY;
683 live.c_oflag=OPOST|ONLCR;
684 live.c_lflag=ISIG|NOFLSH;
686 if (cmd==SB_YES_INTR) {
687 live.c_cc[VINTR]=NEXT_KEY;
688 live.c_cc[VQUIT]=STOP_KEY;
689 signal(SIGINT,*sighandler);
690 signal(SIGQUIT,*sighandler);
693 signal(SIGINT,SIG_IGN);
694 signal(SIGQUIT,SIG_IGN);
695 live.c_cc[VINTR]=(-1);
696 live.c_cc[VQUIT]=(-1);
699 /* do we even need this stuff anymore? */
707 tcsetattr(0,TCSADRAIN,&live);
710 tcgetattr(0,&saved_settings);
713 tcsetattr(0,TCSADRAIN,&saved_settings);
717 void sttybbs(int cmd) /* BSD version of sttybbs() */
720 static struct sgttyb saved_settings;
722 if ( (cmd == 0) || (cmd == 1) ) {
724 live.sg_flags |= CBREAK;
725 live.sg_flags |= CRMOD;
726 live.sg_flags |= NL1;
727 live.sg_flags &= ~ECHO;
728 if (cmd==1) live.sg_flags |= NOFLSH;
732 gtty(0,&saved_settings);
735 stty(0,&saved_settings);
742 * display_help() - help file viewer
744 void display_help(char *name)
751 * fmout() - Citadel text formatter and paginator
753 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
754 /* screen width to use */
755 /* file to read from, or NULL to read from server */
756 /* nonzero if we should use the paginator */
757 /* screen height to use */
758 /* starting value for lines_printed, -1 for global */
759 /* nonzero if we should use hypertext mode */
767 if (starting_lp >= 0) {
768 lines_printed = starting_lp;
770 strcpy(aaa,""); old=255;
772 c=1; /* c is the current pos */
777 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
778 if (sigcaught) goto OOPS;
779 if (fp!=NULL) { /* read from file */
780 if (feof(fp)) eof_flag = 1;
783 buffer[strlen(buffer)+1] = 0;
784 buffer[strlen(buffer)] = a;
787 else { /* read from server */
789 serv_gets(&buffer[d]);
790 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
791 buffer[strlen(buffer)-1]=0;
792 if (!strcmp(&buffer[d],"000")) {
795 while(isspace(buffer[strlen(buffer)-1]))
796 buffer[strlen(buffer)-1] = 0;
804 buffer[strlen(buffer)+1] = 0;
806 strcpy(buffer,&buffer[1]);
810 if (a<=0) goto FMTEND;
812 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
813 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
816 lines_printed = checkpagin(lines_printed,pagin,height);
819 if (a>126) goto FMTA;
822 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
823 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
825 lines_printed = checkpagin(lines_printed,pagin,height);
827 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
830 if ((strlen(aaa)+c)>(width-5)) {
834 lines_printed = checkpagin(lines_printed,pagin,height);
836 printf("%s ",aaa); ++c; c=c+strlen(aaa);
840 if ((a==13)||(a==10)) {
844 lines_printed = checkpagin(lines_printed,pagin,height);
850 /* signal caught; drain the server */
853 } while(strcmp(aaa,"000"));
855 FMTEND: printf("\n");
857 lines_printed = checkpagin(lines_printed,pagin,height);
863 * support ANSI color if defined
865 void color(int colornum)
868 printf("\033[3%dm", colornum);
869 /* printf("\033[1m"); */ /* uncomment for bold colours */
874 void cls(int colornum) {
876 printf("\033[4%dm\033[2J\033[H", colornum);
883 * Detect whether ANSI color is available (answerback)
885 void send_ansi_detect(void) {
886 if (rc_ansi_color == 2) {
893 void look_for_ansi(void) {
900 if (rc_ansi_color == 0) {
903 else if (rc_ansi_color == 1) {
907 /* otherwise, do the auto-detect */
912 if ( (now - AnsiDetect) < 2 ) sleep(1);
920 select(1, &rfds, NULL, NULL, &tv);
921 if (FD_ISSET(0, &rfds)) {
922 abuf[strlen(abuf)+1] = 0;
923 read(0, &abuf[strlen(abuf)], 1);
926 } while (FD_ISSET(0, &rfds));
928 for (a=0; a<strlen(abuf); ++a) {
929 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
930 && (abuf[a+2] == '?') ) {