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;
56 int next_lazy_cmd = 5;
58 struct citcmd *cmdlist = NULL;
61 /* these variables are local to this module */
62 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
63 int ok_to_interrupt = 0; /* print express msgs asynchronously */
64 time_t AnsiDetect; /* when did we send the detect code? */
65 int enable_color = 0; /* nonzero for ANSI color */
69 * print_express() - print express messages if there are any
71 void print_express(void) {
75 if (express_msgs == 0) return;
79 if (buf[0]!='1') return;
81 if (strlen(rc_exp_cmd) > 0) {
82 outpipe = popen(rc_exp_cmd, "w");
83 if (outpipe != NULL) {
84 while (serv_gets(buf), strcmp(buf,"000")) {
85 fprintf(outpipe, "%s\n", buf);
92 /* fall back to built-in express message display */
98 while (serv_gets(buf), strcmp(buf,"000")) {
106 void set_keepalives(int s)
108 keepalives_enabled = (char)s;
112 * This loop handles the "keepalive" messages sent to the server when idling.
114 void do_keepalive(void) {
116 static time_t idlet = 0;
120 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
123 if (keepalives_enabled != KA_NO) {
125 if (keepalives_enabled == KA_YES) {
129 if (ok_to_interrupt == 1) {
132 printf("%s%c ",room_name,
133 room_prompt(room_flags));
142 int inkey(void) { /* get a character from the keyboard, with */
143 int a; /* the watchdog timer in effect if necessary */
146 time_t start_time, now;
153 /* This loop waits for keyboard input. If the keepalive
154 * timer expires, it sends a keepalive to the server if
155 * necessary and then waits again.
162 tv.tv_sec = S_KEEPALIVE;
166 if (((now-start_time) > SLEEPING)
167 && (SLEEPING != 0) && (getppid()==1)) {
168 printf("Sleeping? Call again.\n");
172 select(1, &rfds, NULL, NULL, &tv);
173 } while (!FD_ISSET(0, &rfds));
178 /* At this point, there's input, so fetch it.
179 * (There's a hole in the bucket...)
186 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
187 && ((a<32)||(a>126))) a=0;
192 void getline(char *string, int lim) /* Gets a line from the terminal */
193 /* Pointer to string buffer */
194 /* Maximum length - if negative, no-show */
199 if (lim<0) { lim=(0-lim); flag=1; }
202 GLA: a=inkey(); a=(a&127);
203 if ((a==8)&&(strlen(string)==0)) goto GLA;
204 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
205 if ((a==8)&&(string[0]!=0)) {
206 string[strlen(string)-1]=0;
207 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
208 if ((a==13)||(a==10)) {
217 if (flag==0) putc(a,stdout);
218 if (flag==1) putc('*',stdout);
224 * strprompt() - prompt for a string, print the existing value and
225 * allow the user to press return to keep it...
227 void strprompt(char *prompt, char *str, int len)
232 printf("%s [", prompt);
240 if (buf[0]!=0) strcpy(str,buf);
244 * intprompt() - like strprompt(), except for an integer
245 * (note that it RETURNS the new value!)
247 int intprompt(char *prompt, int ival, int imin, int imax)
254 strprompt(prompt,buf,15);
256 if (i<imin) printf("*** Must be no less than %d.\n",imin);
257 if (i>imax) printf("*** Must be no more than %d.\n",imax);
258 } while((i<imin)||(i>imax));
263 * newprompt() - prompt for a string with no existing value
264 * (clears out string buffer first)
266 void newprompt(char *prompt, char *str, int len)
276 int lkey(void) { /* returns a lower case value */
279 if (isupper(a)) a=tolower(a);
284 * parse the citadel.rc file
286 void load_command_set(void) {
290 struct citcmd *lastcmd = NULL;
295 /* first, set up some defaults for non-required variables */
297 strcpy(editor_path,"");
299 strcpy(rc_username,"");
300 strcpy(rc_password,"");
303 rc_allow_attachments = 0;
304 strcpy(rc_exp_cmd, "");
305 rc_display_message_numbers = 0;
307 /* now try to open the citadel.rc file */
310 if (getenv("HOME") != NULL) {
311 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
312 ccfile = fopen(buf,"r");
315 ccfile = fopen("/usr/local/lib/citadel.rc","r");
318 sprintf(buf,"%s/citadel.rc",BBSDIR);
319 ccfile = fopen(buf,"r");
322 perror("commands: cannot open citadel.rc");
326 while (fgets(buf, 256, ccfile) != NULL) {
327 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
328 buf[strlen(buf)-1] = 0;
330 if (!struncmp(buf,"editor=",7))
331 strcpy(editor_path,&buf[7]);
333 if (!struncmp(buf,"printcmd=",9))
334 strcpy(printcmd,&buf[9]);
336 if (!struncmp(buf,"expcmd=",7))
337 strcpy(rc_exp_cmd,&buf[7]);
339 if (!struncmp(buf,"local_screen_dimensions=",24))
340 have_xterm = (char)atoi(&buf[24]);
342 if (!struncmp(buf,"use_floors=",11)) {
343 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
344 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
345 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
348 if (!struncmp(buf,"beep=",5)) {
349 rc_exp_beep = atoi(&buf[5]);
352 if (!struncmp(buf,"allow_attachments=", 18)) {
353 rc_allow_attachments = atoi(&buf[18]);
356 if (!struncmp(buf,"display_message_numbers=", 24)) {
357 rc_display_message_numbers = atoi(&buf[24]);
360 if (!struncmp(buf,"username=",9))
361 strcpy(rc_username,&buf[9]);
363 if (!struncmp(buf,"password=",9))
364 strcpy(rc_password,&buf[9]);
366 if (!struncmp(buf,"cmd=",4)) {
369 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
371 cptr->c_cmdnum = atoi (buf);
372 for (d=strlen(buf); d>=0; --d)
373 if (buf[d]==',') b=d;
374 strcpy (buf, &buf[b+1]);
376 cptr->c_axlevel = atoi (buf);
377 for (d=strlen(buf); d>=0; --d)
378 if (buf[d]==',') b=d;
379 strcpy (buf, &buf[b+1]);
381 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
384 buf[strlen(buf)+1] = 0;
385 while (strlen(buf) > 0) {
387 for (d=strlen(buf); d>=0; --d)
388 if (buf[d]==',') b=d;
389 strncpy(cptr->c_keys[a],buf,b);
390 cptr->c_keys[a][b] = 0;
392 strcpy(buf,&buf[b+1]);
399 if (cmdlist == NULL) cmdlist = cptr;
400 else lastcmd->next = cptr;
410 * return the key associated with a command
412 char keycmd(char *cmdstr)
416 for (a=0; a<strlen(cmdstr); ++a)
418 return(tolower(cmdstr[a+1]));
424 * Output the string from a key command without the ampersand
425 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
427 char *cmd_expand(char *strbuf, int mode)
435 for (a=0; a<strlen(exp); ++a) {
436 if (strbuf[a] == '&') {
439 strcpy(&exp[a],&exp[a+1]);
444 strcpy(buf,&exp[a+2]);
452 if (!strncmp(&exp[a],"^r",2)) {
454 strcpy(&exp[a],room_name);
455 strcat(exp,&buf[a+2]);
458 if (!strncmp(&exp[a],"^c",2)) {
460 strcpy(&exp[a+1],&exp[a+2]);
471 * Comparison function to determine if entered commands match a
472 * command loaded from the config file.
474 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
480 if (is_room_aide) cmdax = 1;
481 if (axlevel >=6) cmdax = 2;
483 for (a=0; a<ncomp; ++a) {
484 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
485 || (cptr->c_axlevel > cmdax) )
493 * This function returns 1 if a given command requires a string input
495 int requires_string(struct citcmd *cptr, int ncomp)
500 strcpy(buf,cptr->c_keys[ncomp-1]);
501 for (a=0; a<strlen(buf); ++a) {
502 if (buf[a]==':') return(1);
509 * Input a command at the main prompt.
510 * This function returns an integer command number. If the command prompts
511 * for a string then it is placed in the supplied buffer.
513 int getcmd(char *argbuf)
524 /* if we're running in idiot mode, display a cute little menu */
525 IFNEXPERT formout("mainmenu");
527 print_express(); /* print express messages if there are any */
530 for (a=0; a<5; ++a) cmdbuf[a]=0;
531 /* now the room prompt... */
533 printf("\n%s%c ",room_name,room_prompt(room_flags));
540 /* Handle the backspace key, but only if there's something
541 * to backspace over...
543 if ( (ch == 8) && (cmdpos > 0) ) {
544 back(cmdspaces[cmdpos-1] + 1);
549 /* Spacebar invokes "lazy traversal" commands */
550 if ( (ch == 32) && (cmdpos == 0) ) {
551 this_lazy_cmd = next_lazy_cmd;
552 if (this_lazy_cmd == 13) next_lazy_cmd = 5;
553 if (this_lazy_cmd == 5) next_lazy_cmd = 13;
554 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
555 if (cptr->c_cmdnum == this_lazy_cmd) {
557 if (cptr->c_keys[a][0] != 0)
558 printf("%s ", cmd_expand(
559 cptr->c_keys[a], 0));
561 return(this_lazy_cmd);
565 return(this_lazy_cmd);
568 /* Otherwise, process the command */
569 cmdbuf[cmdpos] = tolower(ch);
571 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
572 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
574 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
575 cmdspaces[cmdpos] = strlen(
576 cmd_expand(cptr->c_keys[cmdpos],0) );
578 if ((cptr->c_keys[cmdpos+1]) != 0)
584 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
585 if (cmdmatch(cmdbuf,cptr,5)) {
586 /* We've found our command. */
587 if (requires_string(cptr,cmdpos)) {
594 /* If this command is one that changes rooms,
595 * then the next lazy-command (space bar)
596 * should be "read new" instead of "goto"
598 if ((cptr->c_cmdnum==5)
599 ||(cptr->c_cmdnum==6)
600 ||(cptr->c_cmdnum==47)
601 ||(cptr->c_cmdnum==52)
602 ||(cptr->c_cmdnum==16)
603 ||(cptr->c_cmdnum==20))
606 return(cptr->c_cmdnum);
612 printf("\rOne of ... \n");
613 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
614 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
615 for (a=0; a<5; ++a) {
616 printf("%s ",cmd_expand(cptr->c_keys[a],1));
622 printf("\n%s%c ",room_name,room_prompt(room_flags));
624 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
625 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
626 for (a=0; a<cmdpos; ++a) {
628 cmd_expand(cptr->c_keys[a],0));
644 * set tty modes. commands are:
646 * 0 - set to bbs mode, intr/quit disabled
647 * 1 - set to bbs mode, intr/quit enabled
648 * 2 - save current settings for later restoral
649 * 3 - restore saved settings
651 #ifdef HAVE_TERMIOS_H
652 void sttybbs(int cmd) /* SysV version of sttybbs() */
655 static struct termios saved_settings;
657 if ( (cmd == 0) || (cmd == 1) ) {
659 live.c_iflag=ISTRIP|IXON|IXANY;
660 live.c_oflag=OPOST|ONLCR;
662 if (cmd==1) live.c_lflag=ISIG|NOFLSH;
664 if (cmd==SB_YES_INTR) {
665 live.c_cc[VINTR]=NEXT_KEY;
666 live.c_cc[VQUIT]=STOP_KEY;
667 signal(SIGINT,*sighandler);
668 signal(SIGQUIT,*sighandler);
671 signal(SIGINT,SIG_IGN);
672 signal(SIGQUIT,SIG_IGN);
673 live.c_cc[VINTR]=(-1);
674 live.c_cc[VQUIT]=(-1);
677 /* do we even need this stuff anymore? */
685 tcsetattr(0,TCSADRAIN,&live);
688 tcgetattr(0,&saved_settings);
691 tcsetattr(0,TCSADRAIN,&saved_settings);
695 void sttybbs(int cmd) /* BSD version of sttybbs() */
698 static struct sgttyb saved_settings;
700 if ( (cmd == 0) || (cmd == 1) ) {
702 live.sg_flags |= CBREAK;
703 live.sg_flags |= CRMOD;
704 live.sg_flags |= NL1;
705 live.sg_flags &= ~ECHO;
706 if (cmd==1) live.sg_flags |= NOFLSH;
710 gtty(0,&saved_settings);
713 stty(0,&saved_settings);
720 * display_help() - help file viewer
722 void display_help(char *name)
729 * fmout() - Citadel text formatter and paginator
731 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
732 /* screen width to use */
733 /* file to read from, or NULL to read from server */
734 /* nonzero if we should use the paginator */
735 /* screen height to use */
736 /* starting value for lines_printed, -1 for global */
737 /* nonzero if we should use hypertext mode */
745 if (starting_lp >= 0) {
746 lines_printed = starting_lp;
748 strcpy(aaa,""); old=255;
750 c=1; /* c is the current pos */
755 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
756 if (sigcaught) goto OOPS;
757 if (fp!=NULL) { /* read from file */
758 if (feof(fp)) eof_flag = 1;
761 buffer[strlen(buffer)+1] = 0;
762 buffer[strlen(buffer)] = a;
765 else { /* read from server */
767 serv_gets(&buffer[d]);
768 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
769 buffer[strlen(buffer)-1]=0;
770 if (!strcmp(&buffer[d],"000")) {
773 while(isspace(buffer[strlen(buffer)-1]))
774 buffer[strlen(buffer)-1] = 0;
782 buffer[strlen(buffer)+1] = 0;
784 strcpy(buffer,&buffer[1]);
788 if (a<=0) goto FMTEND;
790 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
791 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
794 lines_printed = checkpagin(lines_printed,pagin,height);
797 if (a>126) goto FMTA;
800 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
801 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
803 lines_printed = checkpagin(lines_printed,pagin,height);
805 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
808 if ((strlen(aaa)+c)>(width-5)) {
812 lines_printed = checkpagin(lines_printed,pagin,height);
814 printf("%s ",aaa); ++c; c=c+strlen(aaa);
818 if ((a==13)||(a==10)) {
822 lines_printed = checkpagin(lines_printed,pagin,height);
828 /* signal caught; drain the server */
831 } while(strcmp(aaa,"000"));
833 FMTEND: printf("\n");
835 lines_printed = checkpagin(lines_printed,pagin,height);
841 * support ANSI color if defined
843 void color(int colornum)
849 printf("%c[3%dm%c[1m", 27, colornum, 27);
858 printf("%c[2J%c[H", 27, 27);
865 * Detect whether ANSI color is available (answerback)
867 void send_ansi_detect(void) {
875 void look_for_ansi(void) {
886 if ( (now - AnsiDetect) < 2 ) sleep(1);
894 select(1, &rfds, NULL, NULL, &tv);
895 if (FD_ISSET(0, &rfds)) {
896 abuf[strlen(abuf)+1] = 0;
897 read(0, &abuf[strlen(abuf)], 1);
900 } while (FD_ISSET(0, &rfds));
902 for (a=0; a<strlen(abuf); ++a) {
903 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
904 && (abuf[a+2] == '?') ) {