4 * commands.c - front end for Citadel
6 * This version is the traditional command parser for room prompts.
17 #include <sys/types.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;
56 struct citcmd *cmdlist = NULL;
59 /* these variables are local to this module */
60 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
61 int ok_to_interrupt = 0; /* print express msgs asynchronously */
62 time_t AnsiDetect; /* when did we send the detect code? */
63 int enable_color = 0; /* nonzero for ANSI color */
67 * print_express() - print express messages if there are any
69 void print_express(void) {
73 if (express_msgs == 0) return;
77 if (buf[0]!='1') return;
79 if (strlen(rc_exp_cmd) > 0) {
80 outpipe = popen(rc_exp_cmd, "w");
81 if (outpipe != NULL) {
82 while (serv_gets(buf), strcmp(buf,"000")) {
83 fprintf(outpipe, "%s\n", buf);
90 /* fall back to built-in express message display */
96 while (serv_gets(buf), strcmp(buf,"000")) {
104 void set_keepalives(int s)
106 keepalives_enabled = (char)s;
110 * This loop handles the "keepalive" messages sent to the server when idling.
112 void do_keepalive(void) {
114 static long idlet = 0L;
118 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
121 if (keepalives_enabled != KA_NO) {
123 if (keepalives_enabled == KA_YES) {
127 if (ok_to_interrupt == 1) {
130 printf("%s%c ",room_name,
131 room_prompt(room_flags));
140 int inkey(void) { /* get a character from the keyboard, with */
141 int a; /* the watchdog timer in effect if necessary */
144 long start_time, now;
151 /* This loop waits for keyboard input. If the keepalive
152 * timer expires, it sends a keepalive to the server if
153 * necessary and then waits again.
160 tv.tv_sec = S_KEEPALIVE;
164 if (((now-start_time) > SLEEPING)
165 && (SLEEPING != 0) && (getppid()==1)) {
166 printf("Sleeping? Call again.\n");
170 select(1, &rfds, NULL, NULL, &tv);
171 } while (!FD_ISSET(0, &rfds));
176 /* At this point, there's input, so fetch it.
177 * (There's a hole in the bucket...)
184 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
185 && ((a<32)||(a>126))) a=0;
190 void getline(char *string, int lim) /* Gets a line from the terminal */
191 /* Pointer to string buffer */
192 /* Maximum length - if negative, no-show */
197 if (lim<0) { lim=(0-lim); flag=1; }
200 GLA: a=inkey(); a=(a&127);
201 if ((a==8)&&(strlen(string)==0)) goto GLA;
202 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
203 if ((a==8)&&(string[0]!=0)) {
204 string[strlen(string)-1]=0;
205 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
206 if ((a==13)||(a==10)) {
215 if (flag==0) putc(a,stdout);
216 if (flag==1) putc('*',stdout);
222 * strprompt() - prompt for a string, print the existing value and
223 * allow the user to press return to keep it...
225 void strprompt(char *prompt, char *str, int len)
230 printf("%s [", prompt);
238 if (buf[0]!=0) strcpy(str,buf);
242 * intprompt() - like strprompt(), except for an integer
243 * (note that it RETURNS the new value!)
245 int intprompt(char *prompt, int ival, int imin, int imax)
252 strprompt(prompt,buf,15);
254 if (i<imin) printf("*** Must be no less than %d.\n",imin);
255 if (i>imax) printf("*** Must be no more than %d.\n",imax);
256 } while((i<imin)||(i>imax));
261 * newprompt() - prompt for a string with no existing value
262 * (clears out string buffer first)
264 void newprompt(char *prompt, char *str, int len)
274 int lkey(void) { /* returns a lower case value */
277 if (isupper(a)) a=tolower(a);
282 * parse the citadel.rc file
284 void load_command_set(void) {
292 /* first, set up some defaults for non-required variables */
294 strcpy(editor_path,"");
296 strcpy(rc_username,"");
297 strcpy(rc_password,"");
300 rc_allow_attachments = 0;
301 strcpy(rc_exp_cmd, "");
303 /* now try to open the citadel.rc file */
306 if (getenv("HOME") != NULL) {
307 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
308 ccfile = fopen(buf,"r");
311 ccfile = fopen("/usr/local/lib/citadel.rc","r");
314 sprintf(buf,"%s/citadel.rc",BBSDIR);
315 ccfile = fopen(buf,"r");
318 perror("commands: cannot open citadel.rc");
322 while (fgets(buf, 256, ccfile) != NULL) {
323 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
324 buf[strlen(buf)-1] = 0;
326 if (!struncmp(buf,"editor=",7))
327 strcpy(editor_path,&buf[7]);
329 if (!struncmp(buf,"printcmd=",9))
330 strcpy(printcmd,&buf[9]);
332 if (!struncmp(buf,"expcmd=",7))
333 strcpy(rc_exp_cmd,&buf[7]);
335 if (!struncmp(buf,"local_screen_dimensions=",24))
336 have_xterm = (char)atoi(&buf[24]);
338 if (!struncmp(buf,"use_floors=",11)) {
339 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
340 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
341 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
344 if (!struncmp(buf,"beep=",5)) {
345 rc_exp_beep = atoi(&buf[5]);
348 if (!struncmp(buf,"allow_attachments=", 18)) {
349 rc_allow_attachments = atoi(&buf[18]);
352 if (!struncmp(buf,"username=",9))
353 strcpy(rc_username,&buf[9]);
355 if (!struncmp(buf,"password=",9))
356 strcpy(rc_password,&buf[9]);
358 if (!struncmp(buf,"cmd=",4)) {
361 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
363 cptr->c_cmdnum = atoi (buf);
364 for (d=strlen(buf); d>=0; --d)
365 if (buf[d]==',') b=d;
366 strcpy (buf, &buf[b+1]);
368 cptr->c_axlevel = atoi (buf);
369 for (d=strlen(buf); d>=0; --d)
370 if (buf[d]==',') b=d;
371 strcpy (buf, &buf[b+1]);
373 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
376 buf[strlen(buf)+1] = 0;
377 while (strlen(buf) > 0) {
379 for (d=strlen(buf); d>=0; --d)
380 if (buf[d]==',') b=d;
381 strncpy(cptr->c_keys[a],buf,b);
382 cptr->c_keys[a][b] = 0;
384 strcpy(buf,&buf[b+1]);
390 cptr->next = cmdlist;
401 * return the key associated with a command
403 char keycmd(char *cmdstr)
407 for (a=0; a<strlen(cmdstr); ++a)
409 return(tolower(cmdstr[a+1]));
415 * Output the string from a key command without the ampersand
416 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
418 char *cmd_expand(char *strbuf, int mode)
426 for (a=0; a<strlen(exp); ++a) {
427 if (strbuf[a] == '&') {
430 strcpy(&exp[a],&exp[a+1]);
435 strcpy(buf,&exp[a+2]);
443 if (!strncmp(&exp[a],"^r",2)) {
445 strcpy(&exp[a],room_name);
446 strcat(exp,&buf[a+2]);
449 if (!strncmp(&exp[a],"^c",2)) {
451 strcpy(&exp[a+1],&exp[a+2]);
462 * Comparison function to determine if entered commands match a
463 * command loaded from the config file.
465 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
471 if (is_room_aide) cmdax = 1;
472 if (axlevel >=6) cmdax = 2;
474 for (a=0; a<ncomp; ++a) {
475 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
476 || (cptr->c_axlevel > cmdax) )
484 * This function returns 1 if a given command requires a string input
486 int requires_string(struct citcmd *cptr, int ncomp)
491 strcpy(buf,cptr->c_keys[ncomp-1]);
492 for (a=0; a<strlen(buf); ++a) {
493 if (buf[a]==':') return(1);
500 * Input a command at the main prompt.
501 * This function returns an integer command number. If the command prompts
502 * for a string then it is placed in the supplied buffer.
504 int getcmd(char *argbuf)
514 /* if we're running in idiot mode, display a cute little menu */
515 IFNEXPERT formout("mainmenu");
517 print_express(); /* print express messages if there are any */
520 for (a=0; a<5; ++a) cmdbuf[a]=0;
521 /* now the room prompt... */
523 printf("\n%s%c ",room_name,room_prompt(room_flags));
530 if ( (ch == 8) && (cmdpos > 0) ) {
531 back(cmdspaces[cmdpos-1] + 1);
536 cmdbuf[cmdpos] = tolower(ch);
538 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
539 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
541 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
542 cmdspaces[cmdpos] = strlen(
543 cmd_expand(cptr->c_keys[cmdpos],0) );
545 if ((cptr->c_keys[cmdpos+1]) != 0)
551 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
552 if (cmdmatch(cmdbuf,cptr,5)) {
553 if (requires_string(cptr,cmdpos)) {
559 return(cptr->c_cmdnum);
564 printf("\rOne of ... \n");
565 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
566 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
567 for (a=0; a<5; ++a) {
568 printf("%s ",cmd_expand(cptr->c_keys[a],1));
574 printf("\n%s%c ",room_name,room_prompt(room_flags));
576 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
577 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
578 for (a=0; a<cmdpos; ++a) {
580 cmd_expand(cptr->c_keys[a],0));
596 * set tty modes. commands are:
598 * 0 - set to bbs mode, intr/quit disabled
599 * 1 - set to bbs mode, intr/quit enabled
600 * 2 - save current settings for later restoral
601 * 3 - restore saved settings
604 void sttybbs(int cmd) /* SysV version of sttybbs() */
607 static struct termios saved_settings;
609 if ( (cmd == 0) || (cmd == 1) ) {
611 live.c_iflag=ISTRIP|IXON|IXANY;
612 live.c_oflag=OPOST|ONLCR;
614 if (cmd==1) live.c_lflag=ISIG|NOFLSH;
616 if (cmd==SB_YES_INTR) {
617 live.c_cc[VINTR]=NEXT_KEY;
618 live.c_cc[VQUIT]=STOP_KEY;
619 signal(SIGINT,*sighandler);
620 signal(SIGQUIT,*sighandler);
623 signal(SIGINT,SIG_IGN);
624 signal(SIGQUIT,SIG_IGN);
625 live.c_cc[VINTR]=(-1);
626 live.c_cc[VQUIT]=(-1);
629 /* do we even need this stuff anymore? */
637 tcsetattr(0,TCSADRAIN,&live);
640 tcgetattr(0,&saved_settings);
643 tcsetattr(0,TCSADRAIN,&saved_settings);
647 void sttybbs(int cmd) /* BSD version of sttybbs() */
650 static struct sgttyb saved_settings;
652 if ( (cmd == 0) || (cmd == 1) ) {
654 live.sg_flags |= CBREAK;
655 live.sg_flags |= CRMOD;
656 live.sg_flags |= NL1;
657 live.sg_flags &= ~ECHO;
658 if (cmd==1) live.sg_flags |= NOFLSH;
662 gtty(0,&saved_settings);
665 stty(0,&saved_settings);
672 * display_help() - help file viewer
674 void display_help(char *name)
681 * fmout() - Citadel text formatter and paginator
683 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
684 /* screen width to use */
685 /* file to read from, or NULL to read from server */
686 /* nonzero if we should use the paginator */
687 /* screen height to use */
688 /* starting value for lines_printed, -1 for global */
689 /* nonzero if we should use hypertext mode */
697 if (starting_lp >= 0) {
698 lines_printed = starting_lp;
700 strcpy(aaa,""); old=255;
702 c=1; /* c is the current pos */
707 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
708 if (sigcaught) goto OOPS;
709 if (fp!=NULL) { /* read from file */
710 if (feof(fp)) eof_flag = 1;
713 buffer[strlen(buffer)+1] = 0;
714 buffer[strlen(buffer)] = a;
717 else { /* read from server */
719 serv_gets(&buffer[d]);
720 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
721 buffer[strlen(buffer)-1]=0;
722 if (!strcmp(&buffer[d],"000")) {
725 while(isspace(buffer[strlen(buffer)-1]))
726 buffer[strlen(buffer)-1] = 0;
734 buffer[strlen(buffer)+1] = 0;
736 strcpy(buffer,&buffer[1]);
740 if (a<=0) goto FMTEND;
742 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
743 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
746 lines_printed = checkpagin(lines_printed,pagin,height);
749 if (a>126) goto FMTA;
752 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
753 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
755 lines_printed = checkpagin(lines_printed,pagin,height);
757 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
760 if ((strlen(aaa)+c)>(width-5)) {
764 lines_printed = checkpagin(lines_printed,pagin,height);
766 printf("%s ",aaa); ++c; c=c+strlen(aaa);
770 if ((a==13)||(a==10)) {
774 lines_printed = checkpagin(lines_printed,pagin,height);
780 /* signal caught; drain the server */
783 } while(strcmp(aaa,"000"));
785 FMTEND: printf("\n");
787 lines_printed = checkpagin(lines_printed,pagin,height);
793 * support ANSI color if defined
795 void color(int colornum)
801 printf("%c[3%dm%c[1m", 27, colornum, 27);
810 printf("%c[2J%c[H", 27, 27);
817 * Detect whether ANSI color is available (answerback)
819 void send_ansi_detect(void) {
827 void look_for_ansi(void) {
838 if ( (now - AnsiDetect) < 2 ) sleep(1);
846 select(1, &rfds, NULL, NULL, &tv);
847 if (FD_ISSET(0, &rfds)) {
848 abuf[strlen(abuf)+1] = 0;
849 read(0, &abuf[strlen(abuf)], 1);
852 } while (FD_ISSET(0, &rfds));
854 for (a=0; a<strlen(abuf); ++a) {
855 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
856 && (abuf[a+2] == '?') ) {