4 * commands.c - front end for Citadel
6 * This version is the traditional command parser for room prompts.
17 #include <sys/types.h>
26 #ifdef HAVE_SYS_SELECT_H
27 #include <sys/select.h>
36 #include "citadel_decls.h"
38 #include "routines2.h"
47 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
52 int rc_allow_attachments;
53 int rc_display_message_numbers;
54 int rc_force_mail_prompts;
57 int next_lazy_cmd = 5;
59 struct citcmd *cmdlist = NULL;
62 /* these variables are local to this module */
63 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
64 int ok_to_interrupt = 0; /* print express msgs asynchronously */
65 time_t AnsiDetect; /* when did we send the detect code? */
66 int enable_color = 0; /* nonzero for ANSI color */
70 * print_express() - print express messages if there are any
72 void print_express(void) {
76 if (express_msgs == 0) return;
80 if (buf[0]!='1') return;
82 if (strlen(rc_exp_cmd) > 0) {
83 outpipe = popen(rc_exp_cmd, "w");
84 if (outpipe != NULL) {
85 while (serv_gets(buf), strcmp(buf,"000")) {
86 fprintf(outpipe, "%s\n", buf);
93 /* fall back to built-in express message display */
99 while (serv_gets(buf), strcmp(buf,"000")) {
107 void set_keepalives(int s)
109 keepalives_enabled = (char)s;
113 * This loop handles the "keepalive" messages sent to the server when idling.
115 void do_keepalive(void) {
117 static time_t idlet = 0;
121 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
124 if (keepalives_enabled != KA_NO) {
126 if (keepalives_enabled == KA_YES) {
130 if (ok_to_interrupt == 1) {
133 printf("%s%c ",room_name,
134 room_prompt(room_flags));
143 int inkey(void) { /* get a character from the keyboard, with */
144 int a; /* the watchdog timer in effect if necessary */
147 time_t start_time, now;
154 /* This loop waits for keyboard input. If the keepalive
155 * timer expires, it sends a keepalive to the server if
156 * necessary and then waits again.
163 tv.tv_sec = S_KEEPALIVE;
167 if (((now-start_time) > SLEEPING)
168 && (SLEEPING != 0) && (getppid()==1)) {
169 printf("Sleeping? Call again.\n");
173 select(1, &rfds, NULL, NULL, &tv);
174 } while (!FD_ISSET(0, &rfds));
179 /* At this point, there's input, so fetch it.
180 * (There's a hole in the bucket...)
187 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
188 && ((a<32)||(a>126))) a=0;
193 void getline(char *string, int lim) /* Gets a line from the terminal */
194 /* Pointer to string buffer */
195 /* Maximum length - if negative, no-show */
200 if (lim<0) { lim=(0-lim); flag=1; }
203 GLA: a=inkey(); a=(a&127);
204 if ((a==8)&&(strlen(string)==0)) goto GLA;
205 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
206 if ((a==8)&&(string[0]!=0)) {
207 string[strlen(string)-1]=0;
208 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
209 if ((a==13)||(a==10)) {
218 if (flag==0) putc(a,stdout);
219 if (flag==1) putc('*',stdout);
225 * strprompt() - prompt for a string, print the existing value and
226 * allow the user to press return to keep it...
228 void strprompt(char *prompt, char *str, int len)
233 printf("%s [", prompt);
241 if (buf[0]!=0) strcpy(str,buf);
245 * intprompt() - like strprompt(), except for an integer
246 * (note that it RETURNS the new value!)
248 int intprompt(char *prompt, int ival, int imin, int imax)
255 strprompt(prompt,buf,15);
257 if (i<imin) printf("*** Must be no less than %d.\n",imin);
258 if (i>imax) printf("*** Must be no more than %d.\n",imax);
259 } while((i<imin)||(i>imax));
264 * newprompt() - prompt for a string with no existing value
265 * (clears out string buffer first)
267 void newprompt(char *prompt, char *str, int len)
277 int lkey(void) { /* returns a lower case value */
280 if (isupper(a)) a=tolower(a);
285 * parse the citadel.rc file
287 void load_command_set(void) {
291 struct citcmd *lastcmd = NULL;
296 /* first, set up some defaults for non-required variables */
298 strcpy(editor_path,"");
300 strcpy(rc_username,"");
301 strcpy(rc_password,"");
304 rc_allow_attachments = 0;
305 strcpy(rc_exp_cmd, "");
306 rc_display_message_numbers = 0;
307 rc_force_mail_prompts = 0;
309 /* now try to open the citadel.rc file */
312 if (getenv("HOME") != NULL) {
313 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
314 ccfile = fopen(buf,"r");
317 ccfile = fopen("/usr/local/lib/citadel.rc","r");
320 sprintf(buf,"%s/citadel.rc",BBSDIR);
321 ccfile = fopen(buf,"r");
324 perror("commands: cannot open citadel.rc");
328 while (fgets(buf, 256, ccfile) != NULL) {
329 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
330 buf[strlen(buf)-1] = 0;
332 if (!struncmp(buf,"editor=",7))
333 strcpy(editor_path,&buf[7]);
335 if (!struncmp(buf,"printcmd=",9))
336 strcpy(printcmd,&buf[9]);
338 if (!struncmp(buf,"expcmd=",7))
339 strcpy(rc_exp_cmd,&buf[7]);
341 if (!struncmp(buf,"local_screen_dimensions=",24))
342 have_xterm = (char)atoi(&buf[24]);
344 if (!struncmp(buf,"use_floors=",11)) {
345 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
346 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
347 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
350 if (!struncmp(buf,"beep=",5)) {
351 rc_exp_beep = atoi(&buf[5]);
354 if (!struncmp(buf,"allow_attachments=", 18)) {
355 rc_allow_attachments = atoi(&buf[18]);
358 if (!struncmp(buf,"display_message_numbers=", 24)) {
359 rc_display_message_numbers = atoi(&buf[24]);
362 if (!struncmp(buf,"force_mail_prompts=", 19)) {
363 rc_force_mail_prompts = atoi(&buf[19]);
366 if (!struncmp(buf,"username=",9))
367 strcpy(rc_username,&buf[9]);
369 if (!struncmp(buf,"password=",9))
370 strcpy(rc_password,&buf[9]);
372 if (!struncmp(buf,"cmd=",4)) {
375 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
377 cptr->c_cmdnum = atoi (buf);
378 for (d=strlen(buf); d>=0; --d)
379 if (buf[d]==',') b=d;
380 strcpy (buf, &buf[b+1]);
382 cptr->c_axlevel = atoi (buf);
383 for (d=strlen(buf); d>=0; --d)
384 if (buf[d]==',') b=d;
385 strcpy (buf, &buf[b+1]);
387 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
390 buf[strlen(buf)+1] = 0;
391 while (strlen(buf) > 0) {
393 for (d=strlen(buf); d>=0; --d)
394 if (buf[d]==',') b=d;
395 strncpy(cptr->c_keys[a],buf,b);
396 cptr->c_keys[a][b] = 0;
398 strcpy(buf,&buf[b+1]);
405 if (cmdlist == NULL) cmdlist = cptr;
406 else lastcmd->next = cptr;
416 * return the key associated with a command
418 char keycmd(char *cmdstr)
422 for (a=0; a<strlen(cmdstr); ++a)
424 return(tolower(cmdstr[a+1]));
430 * Output the string from a key command without the ampersand
431 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
433 char *cmd_expand(char *strbuf, int mode)
441 for (a=0; a<strlen(exp); ++a) {
442 if (strbuf[a] == '&') {
445 strcpy(&exp[a],&exp[a+1]);
450 strcpy(buf,&exp[a+2]);
458 if (!strncmp(&exp[a],"^r",2)) {
460 strcpy(&exp[a],room_name);
461 strcat(exp,&buf[a+2]);
464 if (!strncmp(&exp[a],"^c",2)) {
466 strcpy(&exp[a+1],&exp[a+2]);
477 * Comparison function to determine if entered commands match a
478 * command loaded from the config file.
480 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
486 if (is_room_aide) cmdax = 1;
487 if (axlevel >=6) cmdax = 2;
489 for (a=0; a<ncomp; ++a) {
490 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
491 || (cptr->c_axlevel > cmdax) )
499 * This function returns 1 if a given command requires a string input
501 int requires_string(struct citcmd *cptr, int ncomp)
506 strcpy(buf,cptr->c_keys[ncomp-1]);
507 for (a=0; a<strlen(buf); ++a) {
508 if (buf[a]==':') return(1);
515 * Input a command at the main prompt.
516 * This function returns an integer command number. If the command prompts
517 * for a string then it is placed in the supplied buffer.
519 int getcmd(char *argbuf)
530 /* if we're running in idiot mode, display a cute little menu */
531 IFNEXPERT formout("mainmenu");
533 print_express(); /* print express messages if there are any */
536 for (a=0; a<5; ++a) cmdbuf[a]=0;
537 /* now the room prompt... */
539 printf("\n%s%c ",room_name,room_prompt(room_flags));
546 /* Handle the backspace key, but only if there's something
547 * to backspace over...
549 if ( (ch == 8) && (cmdpos > 0) ) {
550 back(cmdspaces[cmdpos-1] + 1);
555 /* Spacebar invokes "lazy traversal" commands */
556 if ( (ch == 32) && (cmdpos == 0) ) {
557 this_lazy_cmd = next_lazy_cmd;
558 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
559 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
560 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
561 if (cptr->c_cmdnum == this_lazy_cmd) {
563 if (cptr->c_keys[a][0] != 0)
564 printf("%s ", cmd_expand(
565 cptr->c_keys[a], 0));
567 return(this_lazy_cmd);
571 return(this_lazy_cmd);
574 /* Otherwise, process the command */
575 cmdbuf[cmdpos] = tolower(ch);
577 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
578 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
580 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
581 cmdspaces[cmdpos] = strlen(
582 cmd_expand(cptr->c_keys[cmdpos],0) );
584 if ((cptr->c_keys[cmdpos+1]) != 0)
590 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
591 if (cmdmatch(cmdbuf,cptr,5)) {
592 /* We've found our command. */
593 if (requires_string(cptr,cmdpos)) {
600 /* If this command is one that changes rooms,
601 * then the next lazy-command (space bar)
602 * should be "read new" instead of "goto"
604 if ((cptr->c_cmdnum==5)
605 ||(cptr->c_cmdnum==6)
606 ||(cptr->c_cmdnum==47)
607 ||(cptr->c_cmdnum==52)
608 ||(cptr->c_cmdnum==16)
609 ||(cptr->c_cmdnum==20))
612 return(cptr->c_cmdnum);
618 printf("\rOne of ... \n");
619 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
620 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
621 for (a=0; a<5; ++a) {
622 printf("%s ",cmd_expand(cptr->c_keys[a],1));
628 printf("\n%s%c ",room_name,room_prompt(room_flags));
630 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
631 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
632 for (a=0; a<cmdpos; ++a) {
634 cmd_expand(cptr->c_keys[a],0));
650 * set tty modes. commands are:
652 * 0 - set to bbs mode, intr/quit disabled
653 * 1 - set to bbs mode, intr/quit enabled
654 * 2 - save current settings for later restoral
655 * 3 - restore saved settings
657 #ifdef HAVE_TERMIOS_H
658 void sttybbs(int cmd) /* SysV version of sttybbs() */
661 static struct termios saved_settings;
662 static int last_cmd = 0;
669 if ( (cmd == 0) || (cmd == 1) ) {
671 live.c_iflag=ISTRIP|IXON|IXANY;
672 live.c_oflag=OPOST|ONLCR;
673 live.c_lflag=ISIG|NOFLSH;
675 if (cmd==SB_YES_INTR) {
676 live.c_cc[VINTR]=NEXT_KEY;
677 live.c_cc[VQUIT]=STOP_KEY;
678 signal(SIGINT,*sighandler);
679 signal(SIGQUIT,*sighandler);
682 signal(SIGINT,SIG_IGN);
683 signal(SIGQUIT,SIG_IGN);
684 live.c_cc[VINTR]=(-1);
685 live.c_cc[VQUIT]=(-1);
688 /* do we even need this stuff anymore? */
696 tcsetattr(0,TCSADRAIN,&live);
699 tcgetattr(0,&saved_settings);
702 tcsetattr(0,TCSADRAIN,&saved_settings);
706 void sttybbs(int cmd) /* BSD version of sttybbs() */
709 static struct sgttyb saved_settings;
711 if ( (cmd == 0) || (cmd == 1) ) {
713 live.sg_flags |= CBREAK;
714 live.sg_flags |= CRMOD;
715 live.sg_flags |= NL1;
716 live.sg_flags &= ~ECHO;
717 if (cmd==1) live.sg_flags |= NOFLSH;
721 gtty(0,&saved_settings);
724 stty(0,&saved_settings);
731 * display_help() - help file viewer
733 void display_help(char *name)
740 * fmout() - Citadel text formatter and paginator
742 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
743 /* screen width to use */
744 /* file to read from, or NULL to read from server */
745 /* nonzero if we should use the paginator */
746 /* screen height to use */
747 /* starting value for lines_printed, -1 for global */
748 /* nonzero if we should use hypertext mode */
756 if (starting_lp >= 0) {
757 lines_printed = starting_lp;
759 strcpy(aaa,""); old=255;
761 c=1; /* c is the current pos */
766 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
767 if (sigcaught) goto OOPS;
768 if (fp!=NULL) { /* read from file */
769 if (feof(fp)) eof_flag = 1;
772 buffer[strlen(buffer)+1] = 0;
773 buffer[strlen(buffer)] = a;
776 else { /* read from server */
778 serv_gets(&buffer[d]);
779 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
780 buffer[strlen(buffer)-1]=0;
781 if (!strcmp(&buffer[d],"000")) {
784 while(isspace(buffer[strlen(buffer)-1]))
785 buffer[strlen(buffer)-1] = 0;
793 buffer[strlen(buffer)+1] = 0;
795 strcpy(buffer,&buffer[1]);
799 if (a<=0) goto FMTEND;
801 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
802 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
805 lines_printed = checkpagin(lines_printed,pagin,height);
808 if (a>126) goto FMTA;
811 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
812 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
814 lines_printed = checkpagin(lines_printed,pagin,height);
816 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
819 if ((strlen(aaa)+c)>(width-5)) {
823 lines_printed = checkpagin(lines_printed,pagin,height);
825 printf("%s ",aaa); ++c; c=c+strlen(aaa);
829 if ((a==13)||(a==10)) {
833 lines_printed = checkpagin(lines_printed,pagin,height);
839 /* signal caught; drain the server */
842 } while(strcmp(aaa,"000"));
844 FMTEND: printf("\n");
846 lines_printed = checkpagin(lines_printed,pagin,height);
852 * support ANSI color if defined
854 void color(int colornum)
858 printf("\033[3%dm", colornum);
859 /* printf("\033[1m"); */ /* uncomment for bold colours */
865 void cls(int colornum) {
868 printf("\033[4%dm\033[2J\033[H", colornum);
876 * Detect whether ANSI color is available (answerback)
878 void send_ansi_detect(void) {
886 void look_for_ansi(void) {
897 if ( (now - AnsiDetect) < 2 ) sleep(1);
905 select(1, &rfds, NULL, NULL, &tv);
906 if (FD_ISSET(0, &rfds)) {
907 abuf[strlen(abuf)+1] = 0;
908 read(0, &abuf[strlen(abuf)], 1);
911 } while (FD_ISSET(0, &rfds));
913 for (a=0; a<strlen(abuf); ++a) {
914 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
915 && (abuf[a+2] == '?') ) {