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;
197 int yesno(void) { /* Returns 1 for yes, 0 for no */
200 a=inkey(); a=tolower(a);
201 if (a=='y') { printf("Yes\n"); return(1); }
202 if (a=='n') { printf("No\n"); return(0); }
206 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
210 a=inkey(); a=tolower(a);
211 if (a==13) a=(d ? 'y' : 'n');
212 if (a=='y') { printf("Yes\n"); return(1); }
213 if (a=='n') { printf("No\n"); return(0); }
220 void getline(char *string, int lim) /* Gets a line from the terminal */
221 /* Pointer to string buffer */
222 /* Maximum length - if negative, no-show */
227 if (lim<0) { lim=(0-lim); flag=1; }
230 GLA: a=inkey(); a=(a&127);
231 if ((a==8)&&(strlen(string)==0)) goto GLA;
232 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
233 if ((a==8)&&(string[0]!=0)) {
234 string[strlen(string)-1]=0;
235 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
236 if ((a==13)||(a==10)) {
245 if (flag==0) putc(a,stdout);
246 if (flag==1) putc('*',stdout);
252 * strprompt() - prompt for a string, print the existing value and
253 * allow the user to press return to keep it...
255 void strprompt(char *prompt, char *str, int len)
260 printf("%s [", prompt);
268 if (buf[0]!=0) strcpy(str,buf);
272 * boolprompt() - prompt for a yes/no, print the existing value and
273 * allow the user to press return to keep it...
275 int boolprompt(char *prompt, int prev_val) {
277 printf("%s [", prompt);
279 printf("%s", (prev_val ? "Yes" : "No"));
283 return(yesno_d(prev_val));
287 * intprompt() - like strprompt(), except for an integer
288 * (note that it RETURNS the new value!)
290 int intprompt(char *prompt, int ival, int imin, int imax)
296 snprintf(buf,sizeof buf,"%d",i);
297 strprompt(prompt,buf,15);
299 if (i<imin) printf("*** Must be no less than %d.\n",imin);
300 if (i>imax) printf("*** Must be no more than %d.\n",imax);
301 } while((i<imin)||(i>imax));
306 * newprompt() - prompt for a string with no existing value
307 * (clears out string buffer first)
309 void newprompt(char *prompt, char *str, int len)
319 int lkey(void) { /* returns a lower case value */
322 if (isupper(a)) a=tolower(a);
327 * parse the citadel.rc file
329 void load_command_set(void) {
333 struct citcmd *lastcmd = NULL;
338 /* first, set up some defaults for non-required variables */
340 strcpy(editor_path,"");
342 strcpy(rc_username,"");
343 strcpy(rc_password,"");
346 rc_allow_attachments = 0;
347 strcpy(rc_exp_cmd, "");
348 rc_display_message_numbers = 0;
349 rc_force_mail_prompts = 0;
352 /* now try to open the citadel.rc file */
355 if (getenv("HOME") != NULL) {
356 snprintf(buf,sizeof buf,"%s/.citadelrc",getenv("HOME"));
357 ccfile = fopen(buf,"r");
360 ccfile = fopen("/usr/local/lib/citadel.rc","r");
363 snprintf(buf,sizeof buf,"%s/citadel.rc",BBSDIR);
364 ccfile = fopen(buf,"r");
367 perror("commands: cannot open citadel.rc");
371 while (fgets(buf, 256, ccfile) != NULL) {
372 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
373 buf[strlen(buf)-1] = 0;
375 if (!struncmp(buf,"editor=",7))
376 strcpy(editor_path,&buf[7]);
378 if (!struncmp(buf,"printcmd=",9))
379 strcpy(printcmd,&buf[9]);
381 if (!struncmp(buf,"expcmd=",7))
382 strcpy(rc_exp_cmd,&buf[7]);
384 if (!struncmp(buf,"local_screen_dimensions=",24))
385 have_xterm = (char)atoi(&buf[24]);
387 if (!struncmp(buf,"use_floors=",11)) {
388 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
389 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
390 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
393 if (!struncmp(buf,"beep=",5)) {
394 rc_exp_beep = atoi(&buf[5]);
397 if (!struncmp(buf,"allow_attachments=", 18)) {
398 rc_allow_attachments = atoi(&buf[18]);
401 if (!struncmp(buf,"display_message_numbers=", 24)) {
402 rc_display_message_numbers = atoi(&buf[24]);
405 if (!struncmp(buf,"force_mail_prompts=", 19)) {
406 rc_force_mail_prompts = atoi(&buf[19]);
409 if (!struncmp(buf,"ansi_color=", 11)) {
410 if (!strncasecmp(&buf[11], "on", 2))
412 if (!strncasecmp(&buf[11], "auto", 4))
413 rc_ansi_color = 2; /* autodetect */
416 if (!struncmp(buf,"username=",9))
417 strcpy(rc_username,&buf[9]);
419 if (!struncmp(buf,"password=",9))
420 strcpy(rc_password,&buf[9]);
422 if (!struncmp(buf,"cmd=",4)) {
425 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
427 cptr->c_cmdnum = atoi (buf);
428 for (d=strlen(buf); d>=0; --d)
429 if (buf[d]==',') b=d;
430 strcpy (buf, &buf[b+1]);
432 cptr->c_axlevel = atoi (buf);
433 for (d=strlen(buf); d>=0; --d)
434 if (buf[d]==',') b=d;
435 strcpy (buf, &buf[b+1]);
437 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
440 buf[strlen(buf)+1] = 0;
441 while (strlen(buf) > 0) {
443 for (d=strlen(buf); d>=0; --d)
444 if (buf[d]==',') b=d;
445 strncpy(cptr->c_keys[a],buf,b);
446 cptr->c_keys[a][b] = 0;
448 strcpy(buf,&buf[b+1]);
455 if (cmdlist == NULL) cmdlist = cptr;
456 else lastcmd->next = cptr;
466 * return the key associated with a command
468 char keycmd(char *cmdstr)
472 for (a=0; a<strlen(cmdstr); ++a)
474 return(tolower(cmdstr[a+1]));
480 * Output the string from a key command without the ampersand
481 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
483 char *cmd_expand(char *strbuf, int mode)
491 for (a=0; a<strlen(exp); ++a) {
492 if (strbuf[a] == '&') {
495 strcpy(&exp[a],&exp[a+1]);
500 strcpy(buf,&exp[a+2]);
508 if (!strncmp(&exp[a],"^r",2)) {
510 strcpy(&exp[a],room_name);
511 strcat(exp,&buf[a+2]);
514 if (!strncmp(&exp[a],"^c",2)) {
516 strcpy(&exp[a+1],&exp[a+2]);
527 * Comparison function to determine if entered commands match a
528 * command loaded from the config file.
530 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
536 if (is_room_aide) cmdax = 1;
537 if (axlevel >=6) cmdax = 2;
539 for (a=0; a<ncomp; ++a) {
540 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
541 || (cptr->c_axlevel > cmdax) )
549 * This function returns 1 if a given command requires a string input
551 int requires_string(struct citcmd *cptr, int ncomp)
556 strcpy(buf,cptr->c_keys[ncomp-1]);
557 for (a=0; a<strlen(buf); ++a) {
558 if (buf[a]==':') return(1);
565 * Input a command at the main prompt.
566 * This function returns an integer command number. If the command prompts
567 * for a string then it is placed in the supplied buffer.
569 int getcmd(char *argbuf)
580 /* if we're running in idiot mode, display a cute little menu */
581 IFNEXPERT formout("mainmenu");
583 print_express(); /* print express messages if there are any */
586 for (a=0; a<5; ++a) cmdbuf[a]=0;
587 /* now the room prompt... */
589 printf("\n%s%c ",room_name,room_prompt(room_flags));
596 /* Handle the backspace key, but only if there's something
597 * to backspace over...
599 if ( (ch == 8) && (cmdpos > 0) ) {
600 back(cmdspaces[cmdpos-1] + 1);
605 /* Spacebar invokes "lazy traversal" commands */
606 if ( (ch == 32) && (cmdpos == 0) ) {
607 this_lazy_cmd = next_lazy_cmd;
608 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
609 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
610 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
611 if (cptr->c_cmdnum == this_lazy_cmd) {
613 if (cptr->c_keys[a][0] != 0)
614 printf("%s ", cmd_expand(
615 cptr->c_keys[a], 0));
617 return(this_lazy_cmd);
621 return(this_lazy_cmd);
624 /* Otherwise, process the command */
625 cmdbuf[cmdpos] = tolower(ch);
627 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
628 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
630 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
631 cmdspaces[cmdpos] = strlen(
632 cmd_expand(cptr->c_keys[cmdpos],0) );
634 if ((cptr->c_keys[cmdpos+1]) != 0)
640 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
641 if (cmdmatch(cmdbuf,cptr,5)) {
642 /* We've found our command. */
643 if (requires_string(cptr,cmdpos)) {
650 /* If this command is one that changes rooms,
651 * then the next lazy-command (space bar)
652 * should be "read new" instead of "goto"
654 if ((cptr->c_cmdnum==5)
655 ||(cptr->c_cmdnum==6)
656 ||(cptr->c_cmdnum==47)
657 ||(cptr->c_cmdnum==52)
658 ||(cptr->c_cmdnum==16)
659 ||(cptr->c_cmdnum==20))
662 return(cptr->c_cmdnum);
668 printf("\rOne of ... \n");
669 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
670 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
671 for (a=0; a<5; ++a) {
672 printf("%s ",cmd_expand(cptr->c_keys[a],1));
678 printf("\n%s%c ",room_name,room_prompt(room_flags));
680 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
681 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
682 for (a=0; a<cmdpos; ++a) {
684 cmd_expand(cptr->c_keys[a],0));
700 * set tty modes. commands are:
702 * 0 - set to bbs mode, intr/quit disabled
703 * 1 - set to bbs mode, intr/quit enabled
704 * 2 - save current settings for later restoral
705 * 3 - restore saved settings
707 #ifdef HAVE_TERMIOS_H
708 void sttybbs(int cmd) /* SysV version of sttybbs() */
711 static struct termios saved_settings;
712 static int last_cmd = 0;
719 if ( (cmd == 0) || (cmd == 1) ) {
721 live.c_iflag=ISTRIP|IXON|IXANY;
722 live.c_oflag=OPOST|ONLCR;
723 live.c_lflag=ISIG|NOFLSH;
725 if (cmd==SB_YES_INTR) {
726 live.c_cc[VINTR]=NEXT_KEY;
727 live.c_cc[VQUIT]=STOP_KEY;
728 signal(SIGINT,*sighandler);
729 signal(SIGQUIT,*sighandler);
732 signal(SIGINT,SIG_IGN);
733 signal(SIGQUIT,SIG_IGN);
734 live.c_cc[VINTR]=(-1);
735 live.c_cc[VQUIT]=(-1);
738 /* do we even need this stuff anymore? */
746 tcsetattr(0,TCSADRAIN,&live);
749 tcgetattr(0,&saved_settings);
752 tcsetattr(0,TCSADRAIN,&saved_settings);
756 void sttybbs(int cmd) /* BSD version of sttybbs() */
759 static struct sgttyb saved_settings;
761 if ( (cmd == 0) || (cmd == 1) ) {
763 live.sg_flags |= CBREAK;
764 live.sg_flags |= CRMOD;
765 live.sg_flags |= NL1;
766 live.sg_flags &= ~ECHO;
767 if (cmd==1) live.sg_flags |= NOFLSH;
771 gtty(0,&saved_settings);
774 stty(0,&saved_settings);
781 * display_help() - help file viewer
783 void display_help(char *name)
790 * fmout() - Citadel text formatter and paginator
792 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
793 /* screen width to use */
794 /* file to read from, or NULL to read from server */
795 /* nonzero if we should use the paginator */
796 /* screen height to use */
797 /* starting value for lines_printed, -1 for global */
798 /* nonzero if we should use hypertext mode */
806 if (starting_lp >= 0) {
807 lines_printed = starting_lp;
809 strcpy(aaa,""); old=255;
811 c=1; /* c is the current pos */
816 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
817 if (sigcaught) goto OOPS;
818 if (fp!=NULL) { /* read from file */
819 if (feof(fp)) eof_flag = 1;
822 buffer[strlen(buffer)+1] = 0;
823 buffer[strlen(buffer)] = a;
826 else { /* read from server */
828 serv_gets(&buffer[d]);
829 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
830 buffer[strlen(buffer)-1]=0;
831 if (!strcmp(&buffer[d],"000")) {
834 while(isspace(buffer[strlen(buffer)-1]))
835 buffer[strlen(buffer)-1] = 0;
843 buffer[strlen(buffer)+1] = 0;
845 strcpy(buffer,&buffer[1]);
849 if (a<=0) goto FMTEND;
851 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
852 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
855 lines_printed = checkpagin(lines_printed,pagin,height);
858 if (a>126) goto FMTA;
861 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
862 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
864 lines_printed = checkpagin(lines_printed,pagin,height);
866 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
869 if ((strlen(aaa)+c)>(width-5)) {
873 lines_printed = checkpagin(lines_printed,pagin,height);
875 printf("%s ",aaa); ++c; c=c+strlen(aaa);
879 if ((a==13)||(a==10)) {
883 lines_printed = checkpagin(lines_printed,pagin,height);
889 /* signal caught; drain the server */
892 } while(strcmp(aaa,"000"));
894 FMTEND: printf("\n");
896 lines_printed = checkpagin(lines_printed,pagin,height);
902 * support ANSI color if defined
904 void color(int colornum)
907 printf("\033[3%dm", colornum);
908 /* printf("\033[1m"); */ /* uncomment for bold colours */
913 void cls(int colornum) {
915 printf("\033[4%dm\033[2J\033[H", colornum);
922 * Detect whether ANSI color is available (answerback)
924 void send_ansi_detect(void) {
925 if (rc_ansi_color == 2) {
932 void look_for_ansi(void) {
939 if (rc_ansi_color == 0) {
942 else if (rc_ansi_color == 1) {
946 /* otherwise, do the auto-detect */
951 if ( (now - AnsiDetect) < 2 ) sleep(1);
959 select(1, &rfds, NULL, NULL, &tv);
960 if (FD_ISSET(0, &rfds)) {
961 abuf[strlen(abuf)+1] = 0;
962 read(0, &abuf[strlen(abuf)], 1);
965 } while (FD_ISSET(0, &rfds));
967 for (a=0; a<strlen(abuf); ++a) {
968 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
969 && (abuf[a+2] == '?') ) {