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;
80 printf("printing express messages\n");
84 if (buf[0]!='1') return;
86 if (strlen(rc_exp_cmd) > 0) {
87 outpipe = popen(rc_exp_cmd, "w");
88 if (outpipe != NULL) {
89 while (serv_gets(buf), strcmp(buf,"000")) {
90 fprintf(outpipe, "%s\n", buf);
97 /* fall back to built-in express message display */
103 while (serv_gets(buf), strcmp(buf,"000")) {
111 void set_keepalives(int s)
113 keepalives_enabled = (char)s;
117 * This loop handles the "keepalive" messages sent to the server when idling.
119 void do_keepalive(void) {
121 static time_t idlet = 0;
125 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
128 /* Do a space-backspace to keep socksified telnet sessions open */
132 if (keepalives_enabled != KA_NO) {
134 if (keepalives_enabled == KA_YES) {
138 if (ok_to_interrupt == 1) {
139 printf("\r%64s\r", "");
141 printf("%s%c ",room_name,
142 room_prompt(room_flags));
151 int inkey(void) { /* get a character from the keyboard, with */
152 int a; /* the watchdog timer in effect if necessary */
155 time_t start_time, now;
162 /* This loop waits for keyboard input. If the keepalive
163 * timer expires, it sends a keepalive to the server if
164 * necessary and then waits again.
171 tv.tv_sec = S_KEEPALIVE;
175 if (((now-start_time) > SLEEPING)
176 && (SLEEPING != 0) && (getppid()==1)) {
177 printf("Sleeping? Call again.\n");
181 select(1, &rfds, NULL, NULL, &tv);
182 } while (!FD_ISSET(0, &rfds));
187 /* At this point, there's input, so fetch it.
188 * (There's a hole in the bucket...)
195 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
196 && ((a<32)||(a>126))) a=0;
202 int yesno(void) { /* Returns 1 for yes, 0 for no */
205 a=inkey(); a=tolower(a);
206 if (a=='y') { printf("Yes\n"); return(1); }
207 if (a=='n') { printf("No\n"); return(0); }
211 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
215 a=inkey(); a=tolower(a);
216 if (a==13) a=(d ? 'y' : 'n');
217 if (a=='y') { printf("Yes\n"); return(1); }
218 if (a=='n') { printf("No\n"); return(0); }
225 void getline(char *string, int lim) /* Gets a line from the terminal */
226 /* Pointer to string buffer */
227 /* Maximum length - if negative, no-show */
232 if (lim<0) { lim=(0-lim); flag=1; }
235 GLA: a=inkey(); a=(a&127);
236 if ((a==8)&&(strlen(string)==0)) goto GLA;
237 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
238 if ((a==8)&&(string[0]!=0)) {
239 string[strlen(string)-1]=0;
240 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
241 if ((a==13)||(a==10)) {
250 if (flag==0) putc(a,stdout);
251 if (flag==1) putc('*',stdout);
257 * strprompt() - prompt for a string, print the existing value and
258 * allow the user to press return to keep it...
260 void strprompt(char *prompt, char *str, int len)
265 printf("%s [", prompt);
273 if (buf[0]!=0) strcpy(str,buf);
277 * boolprompt() - prompt for a yes/no, print the existing value and
278 * allow the user to press return to keep it...
280 int boolprompt(char *prompt, int prev_val) {
282 printf("%s [", prompt);
284 printf("%s", (prev_val ? "Yes" : "No"));
288 return(yesno_d(prev_val));
292 * intprompt() - like strprompt(), except for an integer
293 * (note that it RETURNS the new value!)
295 int intprompt(char *prompt, int ival, int imin, int imax)
301 snprintf(buf,sizeof buf,"%d",i);
302 strprompt(prompt,buf,15);
304 if (i<imin) printf("*** Must be no less than %d.\n",imin);
305 if (i>imax) printf("*** Must be no more than %d.\n",imax);
306 } while((i<imin)||(i>imax));
311 * newprompt() - prompt for a string with no existing value
312 * (clears out string buffer first)
314 void newprompt(char *prompt, char *str, int len)
324 int lkey(void) { /* returns a lower case value */
327 if (isupper(a)) a=tolower(a);
332 * parse the citadel.rc file
334 void load_command_set(void) {
338 struct citcmd *lastcmd = NULL;
343 /* first, set up some defaults for non-required variables */
345 strcpy(editor_path,"");
347 strcpy(rc_username,"");
348 strcpy(rc_password,"");
351 rc_allow_attachments = 0;
352 strcpy(rc_exp_cmd, "");
353 rc_display_message_numbers = 0;
354 rc_force_mail_prompts = 0;
357 /* now try to open the citadel.rc file */
360 if (getenv("HOME") != NULL) {
361 snprintf(buf,sizeof buf,"%s/.citadelrc",getenv("HOME"));
362 ccfile = fopen(buf,"r");
365 ccfile = fopen("/usr/local/lib/citadel.rc","r");
368 snprintf(buf,sizeof buf,"%s/citadel.rc",BBSDIR);
369 ccfile = fopen(buf,"r");
372 perror("commands: cannot open citadel.rc");
376 while (fgets(buf, 256, ccfile) != NULL) {
377 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
378 buf[strlen(buf)-1] = 0;
380 if (!struncmp(buf,"editor=",7))
381 strcpy(editor_path,&buf[7]);
383 if (!struncmp(buf,"printcmd=",9))
384 strcpy(printcmd,&buf[9]);
386 if (!struncmp(buf,"expcmd=",7))
387 strcpy(rc_exp_cmd,&buf[7]);
389 if (!struncmp(buf,"local_screen_dimensions=",24))
390 have_xterm = (char)atoi(&buf[24]);
392 if (!struncmp(buf,"use_floors=",11)) {
393 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
394 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
395 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
398 if (!struncmp(buf,"beep=",5)) {
399 rc_exp_beep = atoi(&buf[5]);
402 if (!struncmp(buf,"allow_attachments=", 18)) {
403 rc_allow_attachments = atoi(&buf[18]);
406 if (!struncmp(buf,"display_message_numbers=", 24)) {
407 rc_display_message_numbers = atoi(&buf[24]);
410 if (!struncmp(buf,"force_mail_prompts=", 19)) {
411 rc_force_mail_prompts = atoi(&buf[19]);
414 if (!struncmp(buf,"ansi_color=", 11)) {
415 if (!strncasecmp(&buf[11], "on", 2))
417 if (!strncasecmp(&buf[11], "auto", 4))
418 rc_ansi_color = 2; /* autodetect */
419 if (!strncasecmp(&buf[11], "user", 4))
420 rc_ansi_color = 3; /* user config */
423 if (!struncmp(buf,"username=",9))
424 strcpy(rc_username,&buf[9]);
426 if (!struncmp(buf,"password=",9))
427 strcpy(rc_password,&buf[9]);
429 if (!struncmp(buf,"cmd=",4)) {
432 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
434 cptr->c_cmdnum = atoi (buf);
435 for (d=strlen(buf); d>=0; --d)
436 if (buf[d]==',') b=d;
437 strcpy (buf, &buf[b+1]);
439 cptr->c_axlevel = atoi (buf);
440 for (d=strlen(buf); d>=0; --d)
441 if (buf[d]==',') b=d;
442 strcpy (buf, &buf[b+1]);
444 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
447 buf[strlen(buf)+1] = 0;
448 while (strlen(buf) > 0) {
450 for (d=strlen(buf); d>=0; --d)
451 if (buf[d]==',') b=d;
452 strncpy(cptr->c_keys[a],buf,b);
453 cptr->c_keys[a][b] = 0;
455 strcpy(buf,&buf[b+1]);
462 if (cmdlist == NULL) cmdlist = cptr;
463 else lastcmd->next = cptr;
473 * return the key associated with a command
475 char keycmd(char *cmdstr)
479 for (a=0; a<strlen(cmdstr); ++a)
481 return(tolower(cmdstr[a+1]));
487 * Output the string from a key command without the ampersand
488 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
490 char *cmd_expand(char *strbuf, int mode)
498 for (a=0; a<strlen(exp); ++a) {
499 if (strbuf[a] == '&') {
502 strcpy(&exp[a],&exp[a+1]);
507 strcpy(buf,&exp[a+2]);
515 if (!strncmp(&exp[a],"^r",2)) {
517 strcpy(&exp[a],room_name);
518 strcat(exp,&buf[a+2]);
521 if (!strncmp(&exp[a],"^c",2)) {
523 strcpy(&exp[a+1],&exp[a+2]);
534 * Comparison function to determine if entered commands match a
535 * command loaded from the config file.
537 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
543 if (is_room_aide) cmdax = 1;
544 if (axlevel >=6) cmdax = 2;
546 for (a=0; a<ncomp; ++a) {
547 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
548 || (cptr->c_axlevel > cmdax) )
556 * This function returns 1 if a given command requires a string input
558 int requires_string(struct citcmd *cptr, int ncomp)
563 strcpy(buf,cptr->c_keys[ncomp-1]);
564 for (a=0; a<strlen(buf); ++a) {
565 if (buf[a]==':') return(1);
572 * Input a command at the main prompt.
573 * This function returns an integer command number. If the command prompts
574 * for a string then it is placed in the supplied buffer.
576 int getcmd(char *argbuf)
587 /* Switch color support on or off if we're in user mode */
588 if (rc_ansi_color == 3) {
589 if (userflags & US_COLOR) enable_color = 1;
590 else enable_color = 0;
593 /* if we're running in idiot mode, display a cute little menu */
594 IFNEXPERT formout("mainmenu");
596 print_express(); /* print express messages if there are any */
599 for (a=0; a<5; ++a) cmdbuf[a]=0;
600 /* now the room prompt... */
602 printf("\n%s%c ",room_name,room_prompt(room_flags));
609 /* Handle the backspace key, but only if there's something
610 * to backspace over...
612 if ( (ch == 8) && (cmdpos > 0) ) {
613 back(cmdspaces[cmdpos-1] + 1);
618 /* Spacebar invokes "lazy traversal" commands */
619 if ( (ch == 32) && (cmdpos == 0) ) {
620 this_lazy_cmd = next_lazy_cmd;
621 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
622 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
623 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
624 if (cptr->c_cmdnum == this_lazy_cmd) {
626 if (cptr->c_keys[a][0] != 0)
627 printf("%s ", cmd_expand(
628 cptr->c_keys[a], 0));
630 return(this_lazy_cmd);
634 return(this_lazy_cmd);
637 /* Otherwise, process the command */
638 cmdbuf[cmdpos] = tolower(ch);
640 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
641 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
643 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
644 cmdspaces[cmdpos] = strlen(
645 cmd_expand(cptr->c_keys[cmdpos],0) );
647 if ((cptr->c_keys[cmdpos+1]) != 0)
653 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
654 if (cmdmatch(cmdbuf,cptr,5)) {
655 /* We've found our command. */
656 if (requires_string(cptr,cmdpos)) {
663 /* If this command is one that changes rooms,
664 * then the next lazy-command (space bar)
665 * should be "read new" instead of "goto"
667 if ((cptr->c_cmdnum==5)
668 ||(cptr->c_cmdnum==6)
669 ||(cptr->c_cmdnum==47)
670 ||(cptr->c_cmdnum==52)
671 ||(cptr->c_cmdnum==16)
672 ||(cptr->c_cmdnum==20))
675 return(cptr->c_cmdnum);
681 printf("\rOne of ... \n");
682 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
683 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
684 for (a=0; a<5; ++a) {
685 printf("%s ",cmd_expand(cptr->c_keys[a],1));
691 printf("\n%s%c ",room_name,room_prompt(room_flags));
693 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
694 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
695 for (a=0; a<cmdpos; ++a) {
697 cmd_expand(cptr->c_keys[a],0));
713 * set tty modes. commands are:
715 * 0 - set to bbs mode, intr/quit disabled
716 * 1 - set to bbs mode, intr/quit enabled
717 * 2 - save current settings for later restoral
718 * 3 - restore saved settings
720 #ifdef HAVE_TERMIOS_H
721 void sttybbs(int cmd) /* SysV version of sttybbs() */
724 static struct termios saved_settings;
725 static int last_cmd = 0;
732 if ( (cmd == 0) || (cmd == 1) ) {
734 live.c_iflag=ISTRIP|IXON|IXANY;
735 live.c_oflag=OPOST|ONLCR;
736 live.c_lflag=ISIG|NOFLSH;
738 if (cmd==SB_YES_INTR) {
739 live.c_cc[VINTR]=NEXT_KEY;
740 live.c_cc[VQUIT]=STOP_KEY;
741 signal(SIGINT,*sighandler);
742 signal(SIGQUIT,*sighandler);
745 signal(SIGINT,SIG_IGN);
746 signal(SIGQUIT,SIG_IGN);
747 live.c_cc[VINTR]=(-1);
748 live.c_cc[VQUIT]=(-1);
751 /* do we even need this stuff anymore? */
759 tcsetattr(0,TCSADRAIN,&live);
762 tcgetattr(0,&saved_settings);
765 tcsetattr(0,TCSADRAIN,&saved_settings);
769 void sttybbs(int cmd) /* BSD version of sttybbs() */
772 static struct sgttyb saved_settings;
774 if ( (cmd == 0) || (cmd == 1) ) {
776 live.sg_flags |= CBREAK;
777 live.sg_flags |= CRMOD;
778 live.sg_flags |= NL1;
779 live.sg_flags &= ~ECHO;
780 if (cmd==1) live.sg_flags |= NOFLSH;
784 gtty(0,&saved_settings);
787 stty(0,&saved_settings);
794 * display_help() - help file viewer
796 void display_help(char *name)
803 * fmout() - Citadel text formatter and paginator
805 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
806 /* screen width to use */
807 /* file to read from, or NULL to read from server */
808 /* nonzero if we should use the paginator */
809 /* screen height to use */
810 /* starting value for lines_printed, -1 for global */
811 /* nonzero if we should use hypertext mode */
819 if (starting_lp >= 0) {
820 lines_printed = starting_lp;
822 strcpy(aaa,""); old=255;
824 c=1; /* c is the current pos */
829 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
830 if (sigcaught) goto OOPS;
831 if (fp!=NULL) { /* read from file */
832 if (feof(fp)) eof_flag = 1;
835 buffer[strlen(buffer)+1] = 0;
836 buffer[strlen(buffer)] = a;
839 else { /* read from server */
841 serv_gets(&buffer[d]);
842 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
843 buffer[strlen(buffer)-1]=0;
844 if (!strcmp(&buffer[d],"000")) {
847 while(isspace(buffer[strlen(buffer)-1]))
848 buffer[strlen(buffer)-1] = 0;
856 buffer[strlen(buffer)+1] = 0;
858 strcpy(buffer,&buffer[1]);
862 if (a<=0) goto FMTEND;
864 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
865 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
868 lines_printed = checkpagin(lines_printed,pagin,height);
871 if (a>126) goto FMTA;
874 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
875 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
877 lines_printed = checkpagin(lines_printed,pagin,height);
879 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
882 if ((strlen(aaa)+c)>(width-5)) {
886 lines_printed = checkpagin(lines_printed,pagin,height);
888 printf("%s ",aaa); ++c; c=c+strlen(aaa);
892 if ((a==13)||(a==10)) {
896 lines_printed = checkpagin(lines_printed,pagin,height);
902 /* signal caught; drain the server */
905 } while(strcmp(aaa,"000"));
907 FMTEND: printf("\n");
909 lines_printed = checkpagin(lines_printed,pagin,height);
915 * support ANSI color if defined
917 void color(int colornum)
920 printf("\033[3%dm", colornum);
925 void cls(int colornum) {
927 printf("\033[4%dm\033[2J\033[H", colornum);
928 printf("\033[0m"); /* change to 1 for bold colours */
935 * Detect whether ANSI color is available (answerback)
937 void send_ansi_detect(void) {
938 if (rc_ansi_color == 2) {
945 void look_for_ansi(void) {
952 if (rc_ansi_color == 0) {
955 else if (rc_ansi_color == 1) {
958 else if (rc_ansi_color == 2) {
960 /* otherwise, do the auto-detect */
965 if ( (now - AnsiDetect) < 2 ) sleep(1);
973 select(1, &rfds, NULL, NULL, &tv);
974 if (FD_ISSET(0, &rfds)) {
975 abuf[strlen(abuf)+1] = 0;
976 read(0, &abuf[strlen(abuf)], 1);
979 } while (FD_ISSET(0, &rfds));
981 for (a=0; a<strlen(abuf); ++a) {
982 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
983 && (abuf[a+2] == '?') ) {