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 */
414 if (!strncasecmp(&buf[11], "user", 4))
415 rc_ansi_color = 3; /* user config */
418 if (!struncmp(buf,"username=",9))
419 strcpy(rc_username,&buf[9]);
421 if (!struncmp(buf,"password=",9))
422 strcpy(rc_password,&buf[9]);
424 if (!struncmp(buf,"cmd=",4)) {
427 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
429 cptr->c_cmdnum = atoi (buf);
430 for (d=strlen(buf); d>=0; --d)
431 if (buf[d]==',') b=d;
432 strcpy (buf, &buf[b+1]);
434 cptr->c_axlevel = atoi (buf);
435 for (d=strlen(buf); d>=0; --d)
436 if (buf[d]==',') b=d;
437 strcpy (buf, &buf[b+1]);
439 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
442 buf[strlen(buf)+1] = 0;
443 while (strlen(buf) > 0) {
445 for (d=strlen(buf); d>=0; --d)
446 if (buf[d]==',') b=d;
447 strncpy(cptr->c_keys[a],buf,b);
448 cptr->c_keys[a][b] = 0;
450 strcpy(buf,&buf[b+1]);
457 if (cmdlist == NULL) cmdlist = cptr;
458 else lastcmd->next = cptr;
468 * return the key associated with a command
470 char keycmd(char *cmdstr)
474 for (a=0; a<strlen(cmdstr); ++a)
476 return(tolower(cmdstr[a+1]));
482 * Output the string from a key command without the ampersand
483 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
485 char *cmd_expand(char *strbuf, int mode)
493 for (a=0; a<strlen(exp); ++a) {
494 if (strbuf[a] == '&') {
497 strcpy(&exp[a],&exp[a+1]);
502 strcpy(buf,&exp[a+2]);
510 if (!strncmp(&exp[a],"^r",2)) {
512 strcpy(&exp[a],room_name);
513 strcat(exp,&buf[a+2]);
516 if (!strncmp(&exp[a],"^c",2)) {
518 strcpy(&exp[a+1],&exp[a+2]);
529 * Comparison function to determine if entered commands match a
530 * command loaded from the config file.
532 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
538 if (is_room_aide) cmdax = 1;
539 if (axlevel >=6) cmdax = 2;
541 for (a=0; a<ncomp; ++a) {
542 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
543 || (cptr->c_axlevel > cmdax) )
551 * This function returns 1 if a given command requires a string input
553 int requires_string(struct citcmd *cptr, int ncomp)
558 strcpy(buf,cptr->c_keys[ncomp-1]);
559 for (a=0; a<strlen(buf); ++a) {
560 if (buf[a]==':') return(1);
567 * Input a command at the main prompt.
568 * This function returns an integer command number. If the command prompts
569 * for a string then it is placed in the supplied buffer.
571 int getcmd(char *argbuf)
582 /* Switch color support on or off if we're in user mode */
583 if (rc_ansi_color == 3) {
584 if (userflags & US_COLOR) enable_color = 1;
585 else enable_color = 0;
588 /* if we're running in idiot mode, display a cute little menu */
589 IFNEXPERT formout("mainmenu");
591 print_express(); /* print express messages if there are any */
594 for (a=0; a<5; ++a) cmdbuf[a]=0;
595 /* now the room prompt... */
597 printf("\n%s%c ",room_name,room_prompt(room_flags));
604 /* Handle the backspace key, but only if there's something
605 * to backspace over...
607 if ( (ch == 8) && (cmdpos > 0) ) {
608 back(cmdspaces[cmdpos-1] + 1);
613 /* Spacebar invokes "lazy traversal" commands */
614 if ( (ch == 32) && (cmdpos == 0) ) {
615 this_lazy_cmd = next_lazy_cmd;
616 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
617 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
618 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
619 if (cptr->c_cmdnum == this_lazy_cmd) {
621 if (cptr->c_keys[a][0] != 0)
622 printf("%s ", cmd_expand(
623 cptr->c_keys[a], 0));
625 return(this_lazy_cmd);
629 return(this_lazy_cmd);
632 /* Otherwise, process the command */
633 cmdbuf[cmdpos] = tolower(ch);
635 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
636 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
638 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
639 cmdspaces[cmdpos] = strlen(
640 cmd_expand(cptr->c_keys[cmdpos],0) );
642 if ((cptr->c_keys[cmdpos+1]) != 0)
648 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
649 if (cmdmatch(cmdbuf,cptr,5)) {
650 /* We've found our command. */
651 if (requires_string(cptr,cmdpos)) {
658 /* If this command is one that changes rooms,
659 * then the next lazy-command (space bar)
660 * should be "read new" instead of "goto"
662 if ((cptr->c_cmdnum==5)
663 ||(cptr->c_cmdnum==6)
664 ||(cptr->c_cmdnum==47)
665 ||(cptr->c_cmdnum==52)
666 ||(cptr->c_cmdnum==16)
667 ||(cptr->c_cmdnum==20))
670 return(cptr->c_cmdnum);
676 printf("\rOne of ... \n");
677 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
678 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
679 for (a=0; a<5; ++a) {
680 printf("%s ",cmd_expand(cptr->c_keys[a],1));
686 printf("\n%s%c ",room_name,room_prompt(room_flags));
688 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
689 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
690 for (a=0; a<cmdpos; ++a) {
692 cmd_expand(cptr->c_keys[a],0));
708 * set tty modes. commands are:
710 * 0 - set to bbs mode, intr/quit disabled
711 * 1 - set to bbs mode, intr/quit enabled
712 * 2 - save current settings for later restoral
713 * 3 - restore saved settings
715 #ifdef HAVE_TERMIOS_H
716 void sttybbs(int cmd) /* SysV version of sttybbs() */
719 static struct termios saved_settings;
720 static int last_cmd = 0;
727 if ( (cmd == 0) || (cmd == 1) ) {
729 live.c_iflag=ISTRIP|IXON|IXANY;
730 live.c_oflag=OPOST|ONLCR;
731 live.c_lflag=ISIG|NOFLSH;
733 if (cmd==SB_YES_INTR) {
734 live.c_cc[VINTR]=NEXT_KEY;
735 live.c_cc[VQUIT]=STOP_KEY;
736 signal(SIGINT,*sighandler);
737 signal(SIGQUIT,*sighandler);
740 signal(SIGINT,SIG_IGN);
741 signal(SIGQUIT,SIG_IGN);
742 live.c_cc[VINTR]=(-1);
743 live.c_cc[VQUIT]=(-1);
746 /* do we even need this stuff anymore? */
754 tcsetattr(0,TCSADRAIN,&live);
757 tcgetattr(0,&saved_settings);
760 tcsetattr(0,TCSADRAIN,&saved_settings);
764 void sttybbs(int cmd) /* BSD version of sttybbs() */
767 static struct sgttyb saved_settings;
769 if ( (cmd == 0) || (cmd == 1) ) {
771 live.sg_flags |= CBREAK;
772 live.sg_flags |= CRMOD;
773 live.sg_flags |= NL1;
774 live.sg_flags &= ~ECHO;
775 if (cmd==1) live.sg_flags |= NOFLSH;
779 gtty(0,&saved_settings);
782 stty(0,&saved_settings);
789 * display_help() - help file viewer
791 void display_help(char *name)
798 * fmout() - Citadel text formatter and paginator
800 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
801 /* screen width to use */
802 /* file to read from, or NULL to read from server */
803 /* nonzero if we should use the paginator */
804 /* screen height to use */
805 /* starting value for lines_printed, -1 for global */
806 /* nonzero if we should use hypertext mode */
814 if (starting_lp >= 0) {
815 lines_printed = starting_lp;
817 strcpy(aaa,""); old=255;
819 c=1; /* c is the current pos */
824 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
825 if (sigcaught) goto OOPS;
826 if (fp!=NULL) { /* read from file */
827 if (feof(fp)) eof_flag = 1;
830 buffer[strlen(buffer)+1] = 0;
831 buffer[strlen(buffer)] = a;
834 else { /* read from server */
836 serv_gets(&buffer[d]);
837 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
838 buffer[strlen(buffer)-1]=0;
839 if (!strcmp(&buffer[d],"000")) {
842 while(isspace(buffer[strlen(buffer)-1]))
843 buffer[strlen(buffer)-1] = 0;
851 buffer[strlen(buffer)+1] = 0;
853 strcpy(buffer,&buffer[1]);
857 if (a<=0) goto FMTEND;
859 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
860 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
863 lines_printed = checkpagin(lines_printed,pagin,height);
866 if (a>126) goto FMTA;
869 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
870 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
872 lines_printed = checkpagin(lines_printed,pagin,height);
874 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
877 if ((strlen(aaa)+c)>(width-5)) {
881 lines_printed = checkpagin(lines_printed,pagin,height);
883 printf("%s ",aaa); ++c; c=c+strlen(aaa);
887 if ((a==13)||(a==10)) {
891 lines_printed = checkpagin(lines_printed,pagin,height);
897 /* signal caught; drain the server */
900 } while(strcmp(aaa,"000"));
902 FMTEND: printf("\n");
904 lines_printed = checkpagin(lines_printed,pagin,height);
910 * support ANSI color if defined
912 void color(int colornum)
915 printf("\033[3%dm", colornum);
916 /* printf("\033[1m"); */ /* uncomment for bold colours */
921 void cls(int colornum) {
923 printf("\033[4%dm\033[2J\033[H", colornum);
930 * Detect whether ANSI color is available (answerback)
932 void send_ansi_detect(void) {
933 if (rc_ansi_color == 2) {
940 void look_for_ansi(void) {
947 if (rc_ansi_color == 0) {
950 else if (rc_ansi_color == 1) {
953 else if (rc_ansi_color == 2) {
955 /* otherwise, do the auto-detect */
960 if ( (now - AnsiDetect) < 2 ) sleep(1);
968 select(1, &rfds, NULL, NULL, &tv);
969 if (FD_ISSET(0, &rfds)) {
970 abuf[strlen(abuf)+1] = 0;
971 read(0, &abuf[strlen(abuf)], 1);
974 } while (FD_ISSET(0, &rfds));
976 for (a=0; a<strlen(abuf); ++a) {
977 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
978 && (abuf[a+2] == '?') ) {