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>
39 #include "citadel_decls.h"
41 #include "routines2.h"
53 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
58 int rc_allow_attachments;
59 int rc_display_message_numbers;
60 int rc_force_mail_prompts;
64 int next_lazy_cmd = 5;
66 struct citcmd *cmdlist = NULL;
69 /* these variables are local to this module */
70 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
71 int ok_to_interrupt = 0; /* print express msgs asynchronously */
72 time_t AnsiDetect; /* when did we send the detect code? */
73 int enable_color = 0; /* nonzero for ANSI color */
77 * print_express() - print express messages if there are any
79 void print_express(void) {
83 if (express_msgs == 0) return;
87 if (buf[0]!='1') return;
89 if (strlen(rc_exp_cmd) > 0) {
90 outpipe = popen(rc_exp_cmd, "w");
91 if (outpipe != NULL) {
92 while (serv_gets(buf), strcmp(buf,"000")) {
93 fprintf(outpipe, "%s\n", buf);
100 /* fall back to built-in express message display */
106 while (serv_gets(buf), strcmp(buf,"000")) {
114 void set_keepalives(int s)
116 keepalives_enabled = (char)s;
120 * This loop handles the "keepalive" messages sent to the server when idling.
122 void do_keepalive(void) {
124 static time_t idlet = 0;
128 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
131 /* Do a space-backspace to keep socksified telnet sessions open */
135 if (keepalives_enabled != KA_NO) {
137 if (keepalives_enabled == KA_YES) {
141 if (ok_to_interrupt == 1) {
142 printf("\r%64s\r", "");
144 printf("%s%c ",room_name,
145 room_prompt(room_flags));
154 int inkey(void) { /* get a character from the keyboard, with */
155 int a; /* the watchdog timer in effect if necessary */
158 time_t start_time, now;
165 /* This loop waits for keyboard input. If the keepalive
166 * timer expires, it sends a keepalive to the server if
167 * necessary and then waits again.
174 tv.tv_sec = S_KEEPALIVE;
178 if (((now-start_time) > SLEEPING)
179 && (SLEEPING != 0) && (getppid()==1)) {
180 printf("Sleeping? Call again.\n");
184 select(1, &rfds, NULL, NULL, &tv);
185 } while (!FD_ISSET(0, &rfds));
190 /* At this point, there's input, so fetch it.
191 * (There's a hole in the bucket...)
198 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
199 && ((a<32)||(a>126))) a=0;
205 int yesno(void) { /* Returns 1 for yes, 0 for no */
208 a=inkey(); a=tolower(a);
209 if (a=='y') { printf("Yes\n"); return(1); }
210 if (a=='n') { printf("No\n"); return(0); }
214 int yesno_d(int d) /* Returns 1 for yes, 0 for no, arg is default value */
218 a=inkey(); a=tolower(a);
219 if (a==13) a=(d ? 'y' : 'n');
220 if (a=='y') { printf("Yes\n"); return(1); }
221 if (a=='n') { printf("No\n"); return(0); }
228 void getline(char *string, int lim) /* Gets a line from the terminal */
229 /* Pointer to string buffer */
230 /* Maximum length - if negative, no-show */
235 if (lim<0) { lim=(0-lim); flag=1; }
238 GLA: a=inkey(); a=(a&127);
239 if ((a==8)&&(strlen(string)==0)) goto GLA;
240 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
241 if ((a==8)&&(string[0]!=0)) {
242 string[strlen(string)-1]=0;
243 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
244 if ((a==13)||(a==10)) {
253 if (flag==0) putc(a,stdout);
254 if (flag==1) putc('*',stdout);
260 * strprompt() - prompt for a string, print the existing value and
261 * allow the user to press return to keep it...
263 void strprompt(char *prompt, char *str, int len)
268 printf("%s [", prompt);
276 if (buf[0]!=0) strcpy(str,buf);
280 * boolprompt() - prompt for a yes/no, print the existing value and
281 * allow the user to press return to keep it...
283 int boolprompt(char *prompt, int prev_val) {
285 printf("%s [", prompt);
287 printf("%s", (prev_val ? "Yes" : "No"));
291 return(yesno_d(prev_val));
295 * intprompt() - like strprompt(), except for an integer
296 * (note that it RETURNS the new value!)
298 int intprompt(char *prompt, int ival, int imin, int imax)
304 snprintf(buf,sizeof buf,"%d",i);
305 strprompt(prompt,buf,15);
307 if (i<imin) printf("*** Must be no less than %d.\n",imin);
308 if (i>imax) printf("*** Must be no more than %d.\n",imax);
309 } while((i<imin)||(i>imax));
314 * newprompt() - prompt for a string with no existing value
315 * (clears out string buffer first)
317 void newprompt(char *prompt, char *str, int len)
327 int lkey(void) { /* returns a lower case value */
330 if (isupper(a)) a=tolower(a);
335 * parse the citadel.rc file
337 void load_command_set(void) {
341 struct citcmd *lastcmd = NULL;
346 /* first, set up some defaults for non-required variables */
348 strcpy(editor_path,"");
350 strcpy(rc_username,"");
351 strcpy(rc_password,"");
354 rc_allow_attachments = 0;
355 strcpy(rc_exp_cmd, "");
356 rc_display_message_numbers = 0;
357 rc_force_mail_prompts = 0;
360 /* now try to open the citadel.rc file */
363 if (getenv("HOME") != NULL) {
364 snprintf(buf,sizeof buf,"%s/.citadelrc",getenv("HOME"));
365 ccfile = fopen(buf,"r");
368 ccfile = fopen("/usr/local/lib/citadel.rc","r");
371 snprintf(buf,sizeof buf,"%s/citadel.rc",BBSDIR);
372 ccfile = fopen(buf,"r");
375 ccfile = fopen("./citadel.rc","r");
378 perror("commands: cannot open citadel.rc");
382 while (fgets(buf, 256, ccfile) != NULL) {
383 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
384 buf[strlen(buf)-1] = 0;
386 if (!struncmp(buf,"editor=",7))
387 strcpy(editor_path,&buf[7]);
389 if (!struncmp(buf,"printcmd=",9))
390 strcpy(printcmd,&buf[9]);
392 if (!struncmp(buf,"expcmd=",7))
393 strcpy(rc_exp_cmd,&buf[7]);
395 if (!struncmp(buf,"local_screen_dimensions=",24))
396 have_xterm = (char)atoi(&buf[24]);
398 if (!struncmp(buf,"use_floors=",11)) {
399 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
400 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
401 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
404 if (!struncmp(buf,"beep=",5)) {
405 rc_exp_beep = atoi(&buf[5]);
408 if (!struncmp(buf,"allow_attachments=", 18)) {
409 rc_allow_attachments = atoi(&buf[18]);
412 if (!struncmp(buf,"display_message_numbers=", 24)) {
413 rc_display_message_numbers = atoi(&buf[24]);
416 if (!struncmp(buf,"force_mail_prompts=", 19)) {
417 rc_force_mail_prompts = atoi(&buf[19]);
420 if (!struncmp(buf,"ansi_color=", 11)) {
421 if (!strncasecmp(&buf[11], "on", 2))
423 if (!strncasecmp(&buf[11], "auto", 4))
424 rc_ansi_color = 2; /* autodetect */
425 if (!strncasecmp(&buf[11], "user", 4))
426 rc_ansi_color = 3; /* user config */
429 if (!struncmp(buf,"username=",9))
430 strcpy(rc_username,&buf[9]);
432 if (!struncmp(buf,"password=",9))
433 strcpy(rc_password,&buf[9]);
435 if (!struncmp(buf,"cmd=",4)) {
438 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
440 cptr->c_cmdnum = atoi (buf);
441 for (d=strlen(buf); d>=0; --d)
442 if (buf[d]==',') b=d;
443 strcpy (buf, &buf[b+1]);
445 cptr->c_axlevel = atoi (buf);
446 for (d=strlen(buf); d>=0; --d)
447 if (buf[d]==',') b=d;
448 strcpy (buf, &buf[b+1]);
450 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
453 buf[strlen(buf)+1] = 0;
454 while (strlen(buf) > 0) {
456 for (d=strlen(buf); d>=0; --d)
457 if (buf[d]==',') b=d;
458 strncpy(cptr->c_keys[a],buf,b);
459 cptr->c_keys[a][b] = 0;
461 strcpy(buf,&buf[b+1]);
468 if (cmdlist == NULL) cmdlist = cptr;
469 else lastcmd->next = cptr;
479 * return the key associated with a command
481 char keycmd(char *cmdstr)
485 for (a=0; a<strlen(cmdstr); ++a)
487 return(tolower(cmdstr[a+1]));
493 * Output the string from a key command without the ampersand
494 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
496 char *cmd_expand(char *strbuf, int mode)
504 for (a=0; a<strlen(exp); ++a) {
505 if (strbuf[a] == '&') {
508 strcpy(&exp[a],&exp[a+1]);
513 strcpy(buf,&exp[a+2]);
521 if (!strncmp(&exp[a],"^r",2)) {
523 strcpy(&exp[a],room_name);
524 strcat(exp,&buf[a+2]);
527 if (!strncmp(&exp[a],"^c",2)) {
529 strcpy(&exp[a+1],&exp[a+2]);
540 * Comparison function to determine if entered commands match a
541 * command loaded from the config file.
543 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
549 if (is_room_aide) cmdax = 1;
550 if (axlevel >=6) cmdax = 2;
552 for (a=0; a<ncomp; ++a) {
553 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
554 || (cptr->c_axlevel > cmdax) )
562 * This function returns 1 if a given command requires a string input
564 int requires_string(struct citcmd *cptr, int ncomp)
569 strcpy(buf,cptr->c_keys[ncomp-1]);
570 for (a=0; a<strlen(buf); ++a) {
571 if (buf[a]==':') return(1);
578 * Input a command at the main prompt.
579 * This function returns an integer command number. If the command prompts
580 * for a string then it is placed in the supplied buffer.
582 int getcmd(char *argbuf)
593 /* Switch color support on or off if we're in user mode */
594 if (rc_ansi_color == 3) {
595 if (userflags & US_COLOR) enable_color = 1;
596 else enable_color = 0;
599 /* if we're running in idiot mode, display a cute little menu */
600 IFNEXPERT formout("mainmenu");
602 print_express(); /* print express messages if there are any */
605 for (a=0; a<5; ++a) cmdbuf[a]=0;
606 /* now the room prompt... */
608 printf("\n%s%c ",room_name,room_prompt(room_flags));
615 /* Handle the backspace key, but only if there's something
616 * to backspace over...
618 if ( (ch == 8) && (cmdpos > 0) ) {
619 back(cmdspaces[cmdpos-1] + 1);
624 /* Spacebar invokes "lazy traversal" commands */
625 if ( (ch == 32) && (cmdpos == 0) ) {
626 this_lazy_cmd = next_lazy_cmd;
627 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
628 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
629 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
630 if (cptr->c_cmdnum == this_lazy_cmd) {
632 if (cptr->c_keys[a][0] != 0)
633 printf("%s ", cmd_expand(
634 cptr->c_keys[a], 0));
636 return(this_lazy_cmd);
640 return(this_lazy_cmd);
643 /* Otherwise, process the command */
644 cmdbuf[cmdpos] = tolower(ch);
646 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
647 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
649 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
650 cmdspaces[cmdpos] = strlen(
651 cmd_expand(cptr->c_keys[cmdpos],0) );
653 if ((cptr->c_keys[cmdpos+1]) != 0)
659 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
660 if (cmdmatch(cmdbuf,cptr,5)) {
661 /* We've found our command. */
662 if (requires_string(cptr,cmdpos)) {
669 /* If this command is one that changes rooms,
670 * then the next lazy-command (space bar)
671 * should be "read new" instead of "goto"
673 if ((cptr->c_cmdnum==5)
674 ||(cptr->c_cmdnum==6)
675 ||(cptr->c_cmdnum==47)
676 ||(cptr->c_cmdnum==52)
677 ||(cptr->c_cmdnum==16)
678 ||(cptr->c_cmdnum==20))
681 return(cptr->c_cmdnum);
687 printf("\rOne of ... \n");
688 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
689 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
690 for (a=0; a<5; ++a) {
691 printf("%s ",cmd_expand(cptr->c_keys[a],1));
697 printf("\n%s%c ",room_name,room_prompt(room_flags));
699 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
700 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
701 for (a=0; a<cmdpos; ++a) {
703 cmd_expand(cptr->c_keys[a],0));
719 * set tty modes. commands are:
721 * 0 - set to bbs mode, intr/quit disabled
722 * 1 - set to bbs mode, intr/quit enabled
723 * 2 - save current settings for later restoral
724 * 3 - restore saved settings
726 #ifdef HAVE_TERMIOS_H
727 void sttybbs(int cmd) /* SysV version of sttybbs() */
730 static struct termios saved_settings;
731 static int last_cmd = 0;
738 if ( (cmd == 0) || (cmd == 1) ) {
740 live.c_iflag=ISTRIP|IXON|IXANY;
741 live.c_oflag=OPOST|ONLCR;
742 live.c_lflag=ISIG|NOFLSH;
744 if (cmd==SB_YES_INTR) {
745 live.c_cc[VINTR]=NEXT_KEY;
746 live.c_cc[VQUIT]=STOP_KEY;
747 signal(SIGINT,*sighandler);
748 signal(SIGQUIT,*sighandler);
751 signal(SIGINT,SIG_IGN);
752 signal(SIGQUIT,SIG_IGN);
753 live.c_cc[VINTR]=(-1);
754 live.c_cc[VQUIT]=(-1);
757 /* do we even need this stuff anymore? */
765 tcsetattr(0,TCSADRAIN,&live);
768 tcgetattr(0,&saved_settings);
771 tcsetattr(0,TCSADRAIN,&saved_settings);
775 void sttybbs(int cmd) /* BSD version of sttybbs() */
778 static struct sgttyb saved_settings;
780 if ( (cmd == 0) || (cmd == 1) ) {
782 live.sg_flags |= CBREAK;
783 live.sg_flags |= CRMOD;
784 live.sg_flags |= NL1;
785 live.sg_flags &= ~ECHO;
786 if (cmd==1) live.sg_flags |= NOFLSH;
790 gtty(0,&saved_settings);
793 stty(0,&saved_settings);
800 * display_help() - help file viewer
802 void display_help(char *name)
809 * fmout() - Citadel text formatter and paginator
811 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
812 /* screen width to use */
813 /* file to read from, or NULL to read from server */
814 /* nonzero if we should use the paginator */
815 /* screen height to use */
816 /* starting value for lines_printed, -1 for global */
817 /* nonzero if we should use hypertext mode */
825 if (starting_lp >= 0) {
826 lines_printed = starting_lp;
828 strcpy(aaa,""); old=255;
830 c=1; /* c is the current pos */
835 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
836 if (sigcaught) goto OOPS;
837 if (fp!=NULL) { /* read from file */
838 if (feof(fp)) eof_flag = 1;
841 buffer[strlen(buffer)+1] = 0;
842 buffer[strlen(buffer)] = a;
845 else { /* read from server */
847 serv_gets(&buffer[d]);
848 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
849 buffer[strlen(buffer)-1]=0;
850 if (!strcmp(&buffer[d],"000")) {
853 while(isspace(buffer[strlen(buffer)-1]))
854 buffer[strlen(buffer)-1] = 0;
862 buffer[strlen(buffer)+1] = 0;
864 strcpy(buffer,&buffer[1]);
868 if (a<=0) goto FMTEND;
870 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
871 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
874 lines_printed = checkpagin(lines_printed,pagin,height);
877 if (a>126) goto FMTA;
880 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
881 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
883 lines_printed = checkpagin(lines_printed,pagin,height);
885 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
888 if ((strlen(aaa)+c)>(width-5)) {
892 lines_printed = checkpagin(lines_printed,pagin,height);
894 printf("%s ",aaa); ++c; c=c+strlen(aaa);
898 if ((a==13)||(a==10)) {
902 lines_printed = checkpagin(lines_printed,pagin,height);
908 /* signal caught; drain the server */
911 } while(strcmp(aaa,"000"));
913 FMTEND: printf("\n");
915 lines_printed = checkpagin(lines_printed,pagin,height);
921 * support ANSI color if defined
923 void color(int colornum)
926 printf("\033[3%dm", colornum);
931 void cls(int colornum) {
933 printf("\033[4%dm\033[2J\033[H", colornum);
935 printf("\033[1m"); /* bold colors */
936 /* printf("\033[0m"); */ /* non-bold colors */
945 * Detect whether ANSI color is available (answerback)
947 void send_ansi_detect(void) {
948 if (rc_ansi_color == 2) {
955 void look_for_ansi(void) {
962 if (rc_ansi_color == 0) {
965 else if (rc_ansi_color == 1) {
968 else if (rc_ansi_color == 2) {
970 /* otherwise, do the auto-detect */
975 if ( (now - AnsiDetect) < 2 ) sleep(1);
983 select(1, &rfds, NULL, NULL, &tv);
984 if (FD_ISSET(0, &rfds)) {
985 abuf[strlen(abuf)+1] = 0;
986 read(0, &abuf[strlen(abuf)], 1);
989 } while (FD_ISSET(0, &rfds));
991 for (a=0; a<strlen(abuf); ++a) {
992 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
993 && (abuf[a+2] == '?') ) {