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;
58 int next_lazy_cmd = 5;
60 struct citcmd *cmdlist = NULL;
63 /* these variables are local to this module */
64 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
65 int ok_to_interrupt = 0; /* print express msgs asynchronously */
66 time_t AnsiDetect; /* when did we send the detect code? */
67 int enable_color = 0; /* nonzero for ANSI color */
71 * print_express() - print express messages if there are any
73 void print_express(void) {
77 if (express_msgs == 0) return;
81 if (buf[0]!='1') return;
83 if (strlen(rc_exp_cmd) > 0) {
84 outpipe = popen(rc_exp_cmd, "w");
85 if (outpipe != NULL) {
86 while (serv_gets(buf), strcmp(buf,"000")) {
87 fprintf(outpipe, "%s\n", buf);
94 /* fall back to built-in express message display */
100 while (serv_gets(buf), strcmp(buf,"000")) {
108 void set_keepalives(int s)
110 keepalives_enabled = (char)s;
114 * This loop handles the "keepalive" messages sent to the server when idling.
116 void do_keepalive(void) {
118 static time_t idlet = 0;
122 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
125 if (keepalives_enabled != KA_NO) {
127 if (keepalives_enabled == KA_YES) {
131 if (ok_to_interrupt == 1) {
134 printf("%s%c ",room_name,
135 room_prompt(room_flags));
144 int inkey(void) { /* get a character from the keyboard, with */
145 int a; /* the watchdog timer in effect if necessary */
148 time_t start_time, now;
155 /* This loop waits for keyboard input. If the keepalive
156 * timer expires, it sends a keepalive to the server if
157 * necessary and then waits again.
164 tv.tv_sec = S_KEEPALIVE;
168 if (((now-start_time) > SLEEPING)
169 && (SLEEPING != 0) && (getppid()==1)) {
170 printf("Sleeping? Call again.\n");
174 select(1, &rfds, NULL, NULL, &tv);
175 } while (!FD_ISSET(0, &rfds));
180 /* At this point, there's input, so fetch it.
181 * (There's a hole in the bucket...)
188 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
189 && ((a<32)||(a>126))) a=0;
194 void getline(char *string, int lim) /* Gets a line from the terminal */
195 /* Pointer to string buffer */
196 /* Maximum length - if negative, no-show */
201 if (lim<0) { lim=(0-lim); flag=1; }
204 GLA: a=inkey(); a=(a&127);
205 if ((a==8)&&(strlen(string)==0)) goto GLA;
206 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
207 if ((a==8)&&(string[0]!=0)) {
208 string[strlen(string)-1]=0;
209 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
210 if ((a==13)||(a==10)) {
219 if (flag==0) putc(a,stdout);
220 if (flag==1) putc('*',stdout);
226 * strprompt() - prompt for a string, print the existing value and
227 * allow the user to press return to keep it...
229 void strprompt(char *prompt, char *str, int len)
234 printf("%s [", prompt);
242 if (buf[0]!=0) strcpy(str,buf);
246 * intprompt() - like strprompt(), except for an integer
247 * (note that it RETURNS the new value!)
249 int intprompt(char *prompt, int ival, int imin, int imax)
256 strprompt(prompt,buf,15);
258 if (i<imin) printf("*** Must be no less than %d.\n",imin);
259 if (i>imax) printf("*** Must be no more than %d.\n",imax);
260 } while((i<imin)||(i>imax));
265 * newprompt() - prompt for a string with no existing value
266 * (clears out string buffer first)
268 void newprompt(char *prompt, char *str, int len)
278 int lkey(void) { /* returns a lower case value */
281 if (isupper(a)) a=tolower(a);
286 * parse the citadel.rc file
288 void load_command_set(void) {
292 struct citcmd *lastcmd = NULL;
297 /* first, set up some defaults for non-required variables */
299 strcpy(editor_path,"");
301 strcpy(rc_username,"");
302 strcpy(rc_password,"");
305 rc_allow_attachments = 0;
306 strcpy(rc_exp_cmd, "");
307 rc_display_message_numbers = 0;
308 rc_force_mail_prompts = 0;
311 /* now try to open the citadel.rc file */
314 if (getenv("HOME") != NULL) {
315 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
316 ccfile = fopen(buf,"r");
319 ccfile = fopen("/usr/local/lib/citadel.rc","r");
322 sprintf(buf,"%s/citadel.rc",BBSDIR);
323 ccfile = fopen(buf,"r");
326 perror("commands: cannot open citadel.rc");
330 while (fgets(buf, 256, ccfile) != NULL) {
331 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
332 buf[strlen(buf)-1] = 0;
334 if (!struncmp(buf,"editor=",7))
335 strcpy(editor_path,&buf[7]);
337 if (!struncmp(buf,"printcmd=",9))
338 strcpy(printcmd,&buf[9]);
340 if (!struncmp(buf,"expcmd=",7))
341 strcpy(rc_exp_cmd,&buf[7]);
343 if (!struncmp(buf,"local_screen_dimensions=",24))
344 have_xterm = (char)atoi(&buf[24]);
346 if (!struncmp(buf,"use_floors=",11)) {
347 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
348 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
349 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
352 if (!struncmp(buf,"beep=",5)) {
353 rc_exp_beep = atoi(&buf[5]);
356 if (!struncmp(buf,"allow_attachments=", 18)) {
357 rc_allow_attachments = atoi(&buf[18]);
360 if (!struncmp(buf,"display_message_numbers=", 24)) {
361 rc_display_message_numbers = atoi(&buf[24]);
364 if (!struncmp(buf,"force_mail_prompts=", 19)) {
365 rc_force_mail_prompts = atoi(&buf[19]);
368 if (!struncmp(buf,"ansi_color=", 11)) {
369 if (!strncasecmp(&buf[11], "on", 2))
371 if (!strncasecmp(&buf[11], "auto", 4))
372 rc_ansi_color = 2; /* autodetect */
375 if (!struncmp(buf,"username=",9))
376 strcpy(rc_username,&buf[9]);
378 if (!struncmp(buf,"password=",9))
379 strcpy(rc_password,&buf[9]);
381 if (!struncmp(buf,"cmd=",4)) {
384 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
386 cptr->c_cmdnum = atoi (buf);
387 for (d=strlen(buf); d>=0; --d)
388 if (buf[d]==',') b=d;
389 strcpy (buf, &buf[b+1]);
391 cptr->c_axlevel = atoi (buf);
392 for (d=strlen(buf); d>=0; --d)
393 if (buf[d]==',') b=d;
394 strcpy (buf, &buf[b+1]);
396 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
399 buf[strlen(buf)+1] = 0;
400 while (strlen(buf) > 0) {
402 for (d=strlen(buf); d>=0; --d)
403 if (buf[d]==',') b=d;
404 strncpy(cptr->c_keys[a],buf,b);
405 cptr->c_keys[a][b] = 0;
407 strcpy(buf,&buf[b+1]);
414 if (cmdlist == NULL) cmdlist = cptr;
415 else lastcmd->next = cptr;
425 * return the key associated with a command
427 char keycmd(char *cmdstr)
431 for (a=0; a<strlen(cmdstr); ++a)
433 return(tolower(cmdstr[a+1]));
439 * Output the string from a key command without the ampersand
440 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
442 char *cmd_expand(char *strbuf, int mode)
450 for (a=0; a<strlen(exp); ++a) {
451 if (strbuf[a] == '&') {
454 strcpy(&exp[a],&exp[a+1]);
459 strcpy(buf,&exp[a+2]);
467 if (!strncmp(&exp[a],"^r",2)) {
469 strcpy(&exp[a],room_name);
470 strcat(exp,&buf[a+2]);
473 if (!strncmp(&exp[a],"^c",2)) {
475 strcpy(&exp[a+1],&exp[a+2]);
486 * Comparison function to determine if entered commands match a
487 * command loaded from the config file.
489 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
495 if (is_room_aide) cmdax = 1;
496 if (axlevel >=6) cmdax = 2;
498 for (a=0; a<ncomp; ++a) {
499 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
500 || (cptr->c_axlevel > cmdax) )
508 * This function returns 1 if a given command requires a string input
510 int requires_string(struct citcmd *cptr, int ncomp)
515 strcpy(buf,cptr->c_keys[ncomp-1]);
516 for (a=0; a<strlen(buf); ++a) {
517 if (buf[a]==':') return(1);
524 * Input a command at the main prompt.
525 * This function returns an integer command number. If the command prompts
526 * for a string then it is placed in the supplied buffer.
528 int getcmd(char *argbuf)
539 /* if we're running in idiot mode, display a cute little menu */
540 IFNEXPERT formout("mainmenu");
542 print_express(); /* print express messages if there are any */
545 for (a=0; a<5; ++a) cmdbuf[a]=0;
546 /* now the room prompt... */
548 printf("\n%s%c ",room_name,room_prompt(room_flags));
555 /* Handle the backspace key, but only if there's something
556 * to backspace over...
558 if ( (ch == 8) && (cmdpos > 0) ) {
559 back(cmdspaces[cmdpos-1] + 1);
564 /* Spacebar invokes "lazy traversal" commands */
565 if ( (ch == 32) && (cmdpos == 0) ) {
566 this_lazy_cmd = next_lazy_cmd;
567 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
568 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
569 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
570 if (cptr->c_cmdnum == this_lazy_cmd) {
572 if (cptr->c_keys[a][0] != 0)
573 printf("%s ", cmd_expand(
574 cptr->c_keys[a], 0));
576 return(this_lazy_cmd);
580 return(this_lazy_cmd);
583 /* Otherwise, process the command */
584 cmdbuf[cmdpos] = tolower(ch);
586 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
587 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
589 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
590 cmdspaces[cmdpos] = strlen(
591 cmd_expand(cptr->c_keys[cmdpos],0) );
593 if ((cptr->c_keys[cmdpos+1]) != 0)
599 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
600 if (cmdmatch(cmdbuf,cptr,5)) {
601 /* We've found our command. */
602 if (requires_string(cptr,cmdpos)) {
609 /* If this command is one that changes rooms,
610 * then the next lazy-command (space bar)
611 * should be "read new" instead of "goto"
613 if ((cptr->c_cmdnum==5)
614 ||(cptr->c_cmdnum==6)
615 ||(cptr->c_cmdnum==47)
616 ||(cptr->c_cmdnum==52)
617 ||(cptr->c_cmdnum==16)
618 ||(cptr->c_cmdnum==20))
621 return(cptr->c_cmdnum);
627 printf("\rOne of ... \n");
628 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
629 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
630 for (a=0; a<5; ++a) {
631 printf("%s ",cmd_expand(cptr->c_keys[a],1));
637 printf("\n%s%c ",room_name,room_prompt(room_flags));
639 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
640 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
641 for (a=0; a<cmdpos; ++a) {
643 cmd_expand(cptr->c_keys[a],0));
659 * set tty modes. commands are:
661 * 0 - set to bbs mode, intr/quit disabled
662 * 1 - set to bbs mode, intr/quit enabled
663 * 2 - save current settings for later restoral
664 * 3 - restore saved settings
666 #ifdef HAVE_TERMIOS_H
667 void sttybbs(int cmd) /* SysV version of sttybbs() */
670 static struct termios saved_settings;
671 static int last_cmd = 0;
678 if ( (cmd == 0) || (cmd == 1) ) {
680 live.c_iflag=ISTRIP|IXON|IXANY;
681 live.c_oflag=OPOST|ONLCR;
682 live.c_lflag=ISIG|NOFLSH;
684 if (cmd==SB_YES_INTR) {
685 live.c_cc[VINTR]=NEXT_KEY;
686 live.c_cc[VQUIT]=STOP_KEY;
687 signal(SIGINT,*sighandler);
688 signal(SIGQUIT,*sighandler);
691 signal(SIGINT,SIG_IGN);
692 signal(SIGQUIT,SIG_IGN);
693 live.c_cc[VINTR]=(-1);
694 live.c_cc[VQUIT]=(-1);
697 /* do we even need this stuff anymore? */
705 tcsetattr(0,TCSADRAIN,&live);
708 tcgetattr(0,&saved_settings);
711 tcsetattr(0,TCSADRAIN,&saved_settings);
715 void sttybbs(int cmd) /* BSD version of sttybbs() */
718 static struct sgttyb saved_settings;
720 if ( (cmd == 0) || (cmd == 1) ) {
722 live.sg_flags |= CBREAK;
723 live.sg_flags |= CRMOD;
724 live.sg_flags |= NL1;
725 live.sg_flags &= ~ECHO;
726 if (cmd==1) live.sg_flags |= NOFLSH;
730 gtty(0,&saved_settings);
733 stty(0,&saved_settings);
740 * display_help() - help file viewer
742 void display_help(char *name)
749 * fmout() - Citadel text formatter and paginator
751 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
752 /* screen width to use */
753 /* file to read from, or NULL to read from server */
754 /* nonzero if we should use the paginator */
755 /* screen height to use */
756 /* starting value for lines_printed, -1 for global */
757 /* nonzero if we should use hypertext mode */
765 if (starting_lp >= 0) {
766 lines_printed = starting_lp;
768 strcpy(aaa,""); old=255;
770 c=1; /* c is the current pos */
775 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
776 if (sigcaught) goto OOPS;
777 if (fp!=NULL) { /* read from file */
778 if (feof(fp)) eof_flag = 1;
781 buffer[strlen(buffer)+1] = 0;
782 buffer[strlen(buffer)] = a;
785 else { /* read from server */
787 serv_gets(&buffer[d]);
788 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
789 buffer[strlen(buffer)-1]=0;
790 if (!strcmp(&buffer[d],"000")) {
793 while(isspace(buffer[strlen(buffer)-1]))
794 buffer[strlen(buffer)-1] = 0;
802 buffer[strlen(buffer)+1] = 0;
804 strcpy(buffer,&buffer[1]);
808 if (a<=0) goto FMTEND;
810 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
811 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
814 lines_printed = checkpagin(lines_printed,pagin,height);
817 if (a>126) goto FMTA;
820 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
821 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
823 lines_printed = checkpagin(lines_printed,pagin,height);
825 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
828 if ((strlen(aaa)+c)>(width-5)) {
832 lines_printed = checkpagin(lines_printed,pagin,height);
834 printf("%s ",aaa); ++c; c=c+strlen(aaa);
838 if ((a==13)||(a==10)) {
842 lines_printed = checkpagin(lines_printed,pagin,height);
848 /* signal caught; drain the server */
851 } while(strcmp(aaa,"000"));
853 FMTEND: printf("\n");
855 lines_printed = checkpagin(lines_printed,pagin,height);
861 * support ANSI color if defined
863 void color(int colornum)
866 printf("\033[3%dm", colornum);
867 /* printf("\033[1m"); */ /* uncomment for bold colours */
872 void cls(int colornum) {
874 printf("\033[4%dm\033[2J\033[H", colornum);
881 * Detect whether ANSI color is available (answerback)
883 void send_ansi_detect(void) {
884 if (rc_ansi_color == 2) {
891 void look_for_ansi(void) {
898 if (rc_ansi_color == 0) {
901 else if (rc_ansi_color == 1) {
905 /* otherwise, do the auto-detect */
910 if ( (now - AnsiDetect) < 2 ) sleep(1);
918 select(1, &rfds, NULL, NULL, &tv);
919 if (FD_ISSET(0, &rfds)) {
920 abuf[strlen(abuf)+1] = 0;
921 read(0, &abuf[strlen(abuf)], 1);
924 } while (FD_ISSET(0, &rfds));
926 for (a=0; a<strlen(abuf); ++a) {
927 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
928 && (abuf[a+2] == '?') ) {