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 /* Do a space-backspace to keep socksified telnet sessions open */
131 if (keepalives_enabled != KA_NO) {
133 if (keepalives_enabled == KA_YES) {
137 if (ok_to_interrupt == 1) {
138 printf("\r%64s\r", "");
140 printf("%s%c ",room_name,
141 room_prompt(room_flags));
150 int inkey(void) { /* get a character from the keyboard, with */
151 int a; /* the watchdog timer in effect if necessary */
154 time_t start_time, now;
161 /* This loop waits for keyboard input. If the keepalive
162 * timer expires, it sends a keepalive to the server if
163 * necessary and then waits again.
170 tv.tv_sec = S_KEEPALIVE;
174 if (((now-start_time) > SLEEPING)
175 && (SLEEPING != 0) && (getppid()==1)) {
176 printf("Sleeping? Call again.\n");
180 select(1, &rfds, NULL, NULL, &tv);
181 } while (!FD_ISSET(0, &rfds));
186 /* At this point, there's input, so fetch it.
187 * (There's a hole in the bucket...)
194 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
195 && ((a<32)||(a>126))) a=0;
201 int yesno(void) { /* Returns 1 for yes, 0 for no */
204 a=inkey(); a=tolower(a);
205 if (a=='y') { printf("Yes\n"); return(1); }
206 if (a=='n') { printf("No\n"); return(0); }
210 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
214 a=inkey(); a=tolower(a);
215 if (a==13) a=(d ? 'y' : 'n');
216 if (a=='y') { printf("Yes\n"); return(1); }
217 if (a=='n') { printf("No\n"); return(0); }
224 void getline(char *string, int lim) /* Gets a line from the terminal */
225 /* Pointer to string buffer */
226 /* Maximum length - if negative, no-show */
231 if (lim<0) { lim=(0-lim); flag=1; }
234 GLA: a=inkey(); a=(a&127);
235 if ((a==8)&&(strlen(string)==0)) goto GLA;
236 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
237 if ((a==8)&&(string[0]!=0)) {
238 string[strlen(string)-1]=0;
239 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
240 if ((a==13)||(a==10)) {
249 if (flag==0) putc(a,stdout);
250 if (flag==1) putc('*',stdout);
256 * strprompt() - prompt for a string, print the existing value and
257 * allow the user to press return to keep it...
259 void strprompt(char *prompt, char *str, int len)
264 printf("%s [", prompt);
272 if (buf[0]!=0) strcpy(str,buf);
276 * boolprompt() - prompt for a yes/no, print the existing value and
277 * allow the user to press return to keep it...
279 int boolprompt(char *prompt, int prev_val) {
281 printf("%s [", prompt);
283 printf("%s", (prev_val ? "Yes" : "No"));
287 return(yesno_d(prev_val));
291 * intprompt() - like strprompt(), except for an integer
292 * (note that it RETURNS the new value!)
294 int intprompt(char *prompt, int ival, int imin, int imax)
300 snprintf(buf,sizeof buf,"%d",i);
301 strprompt(prompt,buf,15);
303 if (i<imin) printf("*** Must be no less than %d.\n",imin);
304 if (i>imax) printf("*** Must be no more than %d.\n",imax);
305 } while((i<imin)||(i>imax));
310 * newprompt() - prompt for a string with no existing value
311 * (clears out string buffer first)
313 void newprompt(char *prompt, char *str, int len)
323 int lkey(void) { /* returns a lower case value */
326 if (isupper(a)) a=tolower(a);
331 * parse the citadel.rc file
333 void load_command_set(void) {
337 struct citcmd *lastcmd = NULL;
342 /* first, set up some defaults for non-required variables */
344 strcpy(editor_path,"");
346 strcpy(rc_username,"");
347 strcpy(rc_password,"");
350 rc_allow_attachments = 0;
351 strcpy(rc_exp_cmd, "");
352 rc_display_message_numbers = 0;
353 rc_force_mail_prompts = 0;
356 /* now try to open the citadel.rc file */
359 if (getenv("HOME") != NULL) {
360 snprintf(buf,sizeof buf,"%s/.citadelrc",getenv("HOME"));
361 ccfile = fopen(buf,"r");
364 ccfile = fopen("/usr/local/lib/citadel.rc","r");
367 snprintf(buf,sizeof buf,"%s/citadel.rc",BBSDIR);
368 ccfile = fopen(buf,"r");
371 perror("commands: cannot open citadel.rc");
375 while (fgets(buf, 256, ccfile) != NULL) {
376 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
377 buf[strlen(buf)-1] = 0;
379 if (!struncmp(buf,"editor=",7))
380 strcpy(editor_path,&buf[7]);
382 if (!struncmp(buf,"printcmd=",9))
383 strcpy(printcmd,&buf[9]);
385 if (!struncmp(buf,"expcmd=",7))
386 strcpy(rc_exp_cmd,&buf[7]);
388 if (!struncmp(buf,"local_screen_dimensions=",24))
389 have_xterm = (char)atoi(&buf[24]);
391 if (!struncmp(buf,"use_floors=",11)) {
392 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
393 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
394 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
397 if (!struncmp(buf,"beep=",5)) {
398 rc_exp_beep = atoi(&buf[5]);
401 if (!struncmp(buf,"allow_attachments=", 18)) {
402 rc_allow_attachments = atoi(&buf[18]);
405 if (!struncmp(buf,"display_message_numbers=", 24)) {
406 rc_display_message_numbers = atoi(&buf[24]);
409 if (!struncmp(buf,"force_mail_prompts=", 19)) {
410 rc_force_mail_prompts = atoi(&buf[19]);
413 if (!struncmp(buf,"ansi_color=", 11)) {
414 if (!strncasecmp(&buf[11], "on", 2))
416 if (!strncasecmp(&buf[11], "auto", 4))
417 rc_ansi_color = 2; /* autodetect */
418 if (!strncasecmp(&buf[11], "user", 4))
419 rc_ansi_color = 3; /* user config */
422 if (!struncmp(buf,"username=",9))
423 strcpy(rc_username,&buf[9]);
425 if (!struncmp(buf,"password=",9))
426 strcpy(rc_password,&buf[9]);
428 if (!struncmp(buf,"cmd=",4)) {
431 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
433 cptr->c_cmdnum = atoi (buf);
434 for (d=strlen(buf); d>=0; --d)
435 if (buf[d]==',') b=d;
436 strcpy (buf, &buf[b+1]);
438 cptr->c_axlevel = atoi (buf);
439 for (d=strlen(buf); d>=0; --d)
440 if (buf[d]==',') b=d;
441 strcpy (buf, &buf[b+1]);
443 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
446 buf[strlen(buf)+1] = 0;
447 while (strlen(buf) > 0) {
449 for (d=strlen(buf); d>=0; --d)
450 if (buf[d]==',') b=d;
451 strncpy(cptr->c_keys[a],buf,b);
452 cptr->c_keys[a][b] = 0;
454 strcpy(buf,&buf[b+1]);
461 if (cmdlist == NULL) cmdlist = cptr;
462 else lastcmd->next = cptr;
472 * return the key associated with a command
474 char keycmd(char *cmdstr)
478 for (a=0; a<strlen(cmdstr); ++a)
480 return(tolower(cmdstr[a+1]));
486 * Output the string from a key command without the ampersand
487 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
489 char *cmd_expand(char *strbuf, int mode)
497 for (a=0; a<strlen(exp); ++a) {
498 if (strbuf[a] == '&') {
501 strcpy(&exp[a],&exp[a+1]);
506 strcpy(buf,&exp[a+2]);
514 if (!strncmp(&exp[a],"^r",2)) {
516 strcpy(&exp[a],room_name);
517 strcat(exp,&buf[a+2]);
520 if (!strncmp(&exp[a],"^c",2)) {
522 strcpy(&exp[a+1],&exp[a+2]);
533 * Comparison function to determine if entered commands match a
534 * command loaded from the config file.
536 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
542 if (is_room_aide) cmdax = 1;
543 if (axlevel >=6) cmdax = 2;
545 for (a=0; a<ncomp; ++a) {
546 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
547 || (cptr->c_axlevel > cmdax) )
555 * This function returns 1 if a given command requires a string input
557 int requires_string(struct citcmd *cptr, int ncomp)
562 strcpy(buf,cptr->c_keys[ncomp-1]);
563 for (a=0; a<strlen(buf); ++a) {
564 if (buf[a]==':') return(1);
571 * Input a command at the main prompt.
572 * This function returns an integer command number. If the command prompts
573 * for a string then it is placed in the supplied buffer.
575 int getcmd(char *argbuf)
586 /* Switch color support on or off if we're in user mode */
587 if (rc_ansi_color == 3) {
588 if (userflags & US_COLOR) enable_color = 1;
589 else enable_color = 0;
592 /* if we're running in idiot mode, display a cute little menu */
593 IFNEXPERT formout("mainmenu");
595 print_express(); /* print express messages if there are any */
598 for (a=0; a<5; ++a) cmdbuf[a]=0;
599 /* now the room prompt... */
601 printf("\n%s%c ",room_name,room_prompt(room_flags));
608 /* Handle the backspace key, but only if there's something
609 * to backspace over...
611 if ( (ch == 8) && (cmdpos > 0) ) {
612 back(cmdspaces[cmdpos-1] + 1);
617 /* Spacebar invokes "lazy traversal" commands */
618 if ( (ch == 32) && (cmdpos == 0) ) {
619 this_lazy_cmd = next_lazy_cmd;
620 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
621 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
622 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
623 if (cptr->c_cmdnum == this_lazy_cmd) {
625 if (cptr->c_keys[a][0] != 0)
626 printf("%s ", cmd_expand(
627 cptr->c_keys[a], 0));
629 return(this_lazy_cmd);
633 return(this_lazy_cmd);
636 /* Otherwise, process the command */
637 cmdbuf[cmdpos] = tolower(ch);
639 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
640 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
642 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
643 cmdspaces[cmdpos] = strlen(
644 cmd_expand(cptr->c_keys[cmdpos],0) );
646 if ((cptr->c_keys[cmdpos+1]) != 0)
652 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
653 if (cmdmatch(cmdbuf,cptr,5)) {
654 /* We've found our command. */
655 if (requires_string(cptr,cmdpos)) {
662 /* If this command is one that changes rooms,
663 * then the next lazy-command (space bar)
664 * should be "read new" instead of "goto"
666 if ((cptr->c_cmdnum==5)
667 ||(cptr->c_cmdnum==6)
668 ||(cptr->c_cmdnum==47)
669 ||(cptr->c_cmdnum==52)
670 ||(cptr->c_cmdnum==16)
671 ||(cptr->c_cmdnum==20))
674 return(cptr->c_cmdnum);
680 printf("\rOne of ... \n");
681 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
682 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
683 for (a=0; a<5; ++a) {
684 printf("%s ",cmd_expand(cptr->c_keys[a],1));
690 printf("\n%s%c ",room_name,room_prompt(room_flags));
692 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
693 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
694 for (a=0; a<cmdpos; ++a) {
696 cmd_expand(cptr->c_keys[a],0));
712 * set tty modes. commands are:
714 * 0 - set to bbs mode, intr/quit disabled
715 * 1 - set to bbs mode, intr/quit enabled
716 * 2 - save current settings for later restoral
717 * 3 - restore saved settings
719 #ifdef HAVE_TERMIOS_H
720 void sttybbs(int cmd) /* SysV version of sttybbs() */
723 static struct termios saved_settings;
724 static int last_cmd = 0;
731 if ( (cmd == 0) || (cmd == 1) ) {
733 live.c_iflag=ISTRIP|IXON|IXANY;
734 live.c_oflag=OPOST|ONLCR;
735 live.c_lflag=ISIG|NOFLSH;
737 if (cmd==SB_YES_INTR) {
738 live.c_cc[VINTR]=NEXT_KEY;
739 live.c_cc[VQUIT]=STOP_KEY;
740 signal(SIGINT,*sighandler);
741 signal(SIGQUIT,*sighandler);
744 signal(SIGINT,SIG_IGN);
745 signal(SIGQUIT,SIG_IGN);
746 live.c_cc[VINTR]=(-1);
747 live.c_cc[VQUIT]=(-1);
750 /* do we even need this stuff anymore? */
758 tcsetattr(0,TCSADRAIN,&live);
761 tcgetattr(0,&saved_settings);
764 tcsetattr(0,TCSADRAIN,&saved_settings);
768 void sttybbs(int cmd) /* BSD version of sttybbs() */
771 static struct sgttyb saved_settings;
773 if ( (cmd == 0) || (cmd == 1) ) {
775 live.sg_flags |= CBREAK;
776 live.sg_flags |= CRMOD;
777 live.sg_flags |= NL1;
778 live.sg_flags &= ~ECHO;
779 if (cmd==1) live.sg_flags |= NOFLSH;
783 gtty(0,&saved_settings);
786 stty(0,&saved_settings);
793 * display_help() - help file viewer
795 void display_help(char *name)
802 * fmout() - Citadel text formatter and paginator
804 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
805 /* screen width to use */
806 /* file to read from, or NULL to read from server */
807 /* nonzero if we should use the paginator */
808 /* screen height to use */
809 /* starting value for lines_printed, -1 for global */
810 /* nonzero if we should use hypertext mode */
818 if (starting_lp >= 0) {
819 lines_printed = starting_lp;
821 strcpy(aaa,""); old=255;
823 c=1; /* c is the current pos */
828 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
829 if (sigcaught) goto OOPS;
830 if (fp!=NULL) { /* read from file */
831 if (feof(fp)) eof_flag = 1;
834 buffer[strlen(buffer)+1] = 0;
835 buffer[strlen(buffer)] = a;
838 else { /* read from server */
840 serv_gets(&buffer[d]);
841 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
842 buffer[strlen(buffer)-1]=0;
843 if (!strcmp(&buffer[d],"000")) {
846 while(isspace(buffer[strlen(buffer)-1]))
847 buffer[strlen(buffer)-1] = 0;
855 buffer[strlen(buffer)+1] = 0;
857 strcpy(buffer,&buffer[1]);
861 if (a<=0) goto FMTEND;
863 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
864 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
867 lines_printed = checkpagin(lines_printed,pagin,height);
870 if (a>126) goto FMTA;
873 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
874 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
876 lines_printed = checkpagin(lines_printed,pagin,height);
878 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
881 if ((strlen(aaa)+c)>(width-5)) {
885 lines_printed = checkpagin(lines_printed,pagin,height);
887 printf("%s ",aaa); ++c; c=c+strlen(aaa);
891 if ((a==13)||(a==10)) {
895 lines_printed = checkpagin(lines_printed,pagin,height);
901 /* signal caught; drain the server */
904 } while(strcmp(aaa,"000"));
906 FMTEND: printf("\n");
908 lines_printed = checkpagin(lines_printed,pagin,height);
914 * support ANSI color if defined
916 void color(int colornum)
919 printf("\033[3%dm", colornum);
924 void cls(int colornum) {
926 printf("\033[4%dm\033[2J\033[H", colornum);
927 printf("\033[0m"); /* change to 1 for bold colours */
934 * Detect whether ANSI color is available (answerback)
936 void send_ansi_detect(void) {
937 if (rc_ansi_color == 2) {
944 void look_for_ansi(void) {
951 if (rc_ansi_color == 0) {
954 else if (rc_ansi_color == 1) {
957 else if (rc_ansi_color == 2) {
959 /* otherwise, do the auto-detect */
964 if ( (now - AnsiDetect) < 2 ) sleep(1);
972 select(1, &rfds, NULL, NULL, &tv);
973 if (FD_ISSET(0, &rfds)) {
974 abuf[strlen(abuf)+1] = 0;
975 read(0, &abuf[strlen(abuf)], 1);
978 } while (FD_ISSET(0, &rfds));
980 for (a=0; a<strlen(abuf); ++a) {
981 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
982 && (abuf[a+2] == '?') ) {