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)
55 struct citcmd *cmdlist = NULL;
58 /* these variables are local to this module */
59 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
60 int ok_to_interrupt = 0; /* print express msgs asynchronously */
61 time_t AnsiDetect; /* when did we send the detect code? */
62 int enable_color = 0; /* nonzero for ANSI color */
66 * print_express() - print express messages if there are any
68 void print_express(void) {
72 if (express_msgs == 0) return;
76 if (buf[0]!='1') return;
78 if (strlen(rc_exp_cmd) > 0) {
79 outpipe = popen(rc_exp_cmd, "w");
80 if (outpipe != NULL) {
81 while (serv_gets(buf), strcmp(buf,"000")) {
82 fprintf(outpipe, "%s\n", buf);
89 /* fall back to built-in express message display */
95 while (serv_gets(buf), strcmp(buf,"000")) {
103 void set_keepalives(int s)
105 keepalives_enabled = (char)s;
109 * This loop handles the "keepalive" messages sent to the server when idling.
111 void do_keepalive(void) {
113 static long idlet = 0L;
117 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
120 if (keepalives_enabled != KA_NO) {
122 if (keepalives_enabled == KA_YES) {
126 if (ok_to_interrupt == 1) {
129 printf("%s%c ",room_name,
130 room_prompt(room_flags));
139 int inkey(void) { /* get a character from the keyboard, with */
140 int a; /* the watchdog timer in effect if necessary */
143 long start_time, now;
150 /* This loop waits for keyboard input. If the keepalive
151 * timer expires, it sends a keepalive to the server if
152 * necessary and then waits again.
159 tv.tv_sec = S_KEEPALIVE;
163 if (((now-start_time) > SLEEPING)
164 && (SLEEPING != 0) && (getppid()==1)) {
165 printf("Sleeping? Call again.\n");
169 select(1, &rfds, NULL, NULL, &tv);
170 } while (!FD_ISSET(0, &rfds));
175 /* At this point, there's input, so fetch it.
176 * (There's a hole in the bucket...)
183 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
184 && ((a<32)||(a>126))) a=0;
189 void getline(char *string, int lim) /* Gets a line from the terminal */
190 /* Pointer to string buffer */
191 /* Maximum length - if negative, no-show */
196 if (lim<0) { lim=(0-lim); flag=1; }
199 GLA: a=inkey(); a=(a&127);
200 if ((a==8)&&(strlen(string)==0)) goto GLA;
201 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
202 if ((a==8)&&(string[0]!=0)) {
203 string[strlen(string)-1]=0;
204 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
205 if ((a==13)||(a==10)) {
214 if (flag==0) putc(a,stdout);
215 if (flag==1) putc('*',stdout);
221 * strprompt() - prompt for a string, print the existing value and
222 * allow the user to press return to keep it...
224 void strprompt(char *prompt, char *str, int len)
229 printf("%s [", prompt);
237 if (buf[0]!=0) strcpy(str,buf);
241 * intprompt() - like strprompt(), except for an integer
242 * (note that it RETURNS the new value!)
244 int intprompt(char *prompt, int ival, int imin, int imax)
251 strprompt(prompt,buf,15);
253 if (i<imin) printf("*** Must be no less than %d.\n",imin);
254 if (i>imax) printf("*** Must be no more than %d.\n",imax);
255 } while((i<imin)||(i>imax));
260 * newprompt() - prompt for a string with no existing value
261 * (clears out string buffer first)
263 void newprompt(char *prompt, char *str, int len)
273 int lkey(void) { /* returns a lower case value */
276 if (isupper(a)) a=tolower(a);
281 * parse the citadel.rc file
283 void load_command_set(void) {
291 /* first, set up some defaults for non-required variables */
293 strcpy(editor_path,"");
295 strcpy(rc_username,"");
296 strcpy(rc_password,"");
299 strcpy(rc_exp_cmd, "");
301 /* now try to open the citadel.rc file */
304 if (getenv("HOME") != NULL) {
305 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
306 ccfile = fopen(buf,"r");
309 ccfile = fopen("/usr/local/lib/citadel.rc","r");
312 sprintf(buf,"%s/citadel.rc",BBSDIR);
313 ccfile = fopen(buf,"r");
316 perror("commands: cannot open citadel.rc");
320 while (fgets(buf, 256, ccfile) != NULL) {
321 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
322 buf[strlen(buf)-1] = 0;
324 if (!struncmp(buf,"editor=",7))
325 strcpy(editor_path,&buf[7]);
327 if (!struncmp(buf,"printcmd=",9))
328 strcpy(printcmd,&buf[9]);
330 if (!struncmp(buf,"expcmd=",7))
331 strcpy(rc_exp_cmd,&buf[7]);
333 if (!struncmp(buf,"local_screen_dimensions=",24))
334 have_xterm = (char)atoi(&buf[24]);
336 if (!struncmp(buf,"use_floors=",11)) {
337 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
338 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
339 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
342 if (!struncmp(buf,"beep=",5)) {
343 rc_exp_beep = atoi(&buf[5]);
346 if (!struncmp(buf,"username=",9))
347 strcpy(rc_username,&buf[9]);
349 if (!struncmp(buf,"password=",9))
350 strcpy(rc_password,&buf[9]);
352 if (!struncmp(buf,"cmd=",4)) {
355 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
357 cptr->c_cmdnum = atoi (buf);
358 for (d=strlen(buf); d>=0; --d)
359 if (buf[d]==',') b=d;
360 strcpy (buf, &buf[b+1]);
362 cptr->c_axlevel = atoi (buf);
363 for (d=strlen(buf); d>=0; --d)
364 if (buf[d]==',') b=d;
365 strcpy (buf, &buf[b+1]);
367 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
370 buf[strlen(buf)+1] = 0;
371 while (strlen(buf) > 0) {
373 for (d=strlen(buf); d>=0; --d)
374 if (buf[d]==',') b=d;
375 strncpy(cptr->c_keys[a],buf,b);
376 cptr->c_keys[a][b] = 0;
378 strcpy(buf,&buf[b+1]);
384 cptr->next = cmdlist;
395 * return the key associated with a command
397 char keycmd(char *cmdstr)
401 for (a=0; a<strlen(cmdstr); ++a)
403 return(tolower(cmdstr[a+1]));
409 * Output the string from a key command without the ampersand
410 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
412 char *cmd_expand(char *strbuf, int mode)
420 for (a=0; a<strlen(exp); ++a) {
421 if (strbuf[a] == '&') {
424 strcpy(&exp[a],&exp[a+1]);
429 strcpy(buf,&exp[a+2]);
437 if (!strncmp(&exp[a],"^r",2)) {
439 strcpy(&exp[a],room_name);
440 strcat(exp,&buf[a+2]);
443 if (!strncmp(&exp[a],"^c",2)) {
445 strcpy(&exp[a+1],&exp[a+2]);
456 * Comparison function to determine if entered commands match a
457 * command loaded from the config file.
459 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
465 if (is_room_aide) cmdax = 1;
466 if (axlevel >=6) cmdax = 2;
468 for (a=0; a<ncomp; ++a) {
469 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
470 || (cptr->c_axlevel > cmdax) )
478 * This function returns 1 if a given command requires a string input
480 int requires_string(struct citcmd *cptr, int ncomp)
485 strcpy(buf,cptr->c_keys[ncomp-1]);
486 for (a=0; a<strlen(buf); ++a) {
487 if (buf[a]==':') return(1);
494 * Input a command at the main prompt.
495 * This function returns an integer command number. If the command prompts
496 * for a string then it is placed in the supplied buffer.
498 int getcmd(char *argbuf)
508 /* if we're running in idiot mode, display a cute little menu */
509 IFNEXPERT formout("mainmenu");
511 print_express(); /* print express messages if there are any */
514 for (a=0; a<5; ++a) cmdbuf[a]=0;
515 /* now the room prompt... */
517 printf("\n%s%c ",room_name,room_prompt(room_flags));
524 if ( (ch == 8) && (cmdpos > 0) ) {
525 back(cmdspaces[cmdpos-1] + 1);
530 cmdbuf[cmdpos] = tolower(ch);
532 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
533 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
535 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
536 cmdspaces[cmdpos] = strlen(
537 cmd_expand(cptr->c_keys[cmdpos],0) );
539 if ((cptr->c_keys[cmdpos+1]) != 0)
545 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
546 if (cmdmatch(cmdbuf,cptr,5)) {
547 if (requires_string(cptr,cmdpos)) {
553 return(cptr->c_cmdnum);
558 printf("\rOne of ... \n");
559 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
560 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
561 for (a=0; a<5; ++a) {
562 printf("%s ",cmd_expand(cptr->c_keys[a],1));
568 printf("\n%s%c ",room_name,room_prompt(room_flags));
570 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
571 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
572 for (a=0; a<cmdpos; ++a) {
574 cmd_expand(cptr->c_keys[a],0));
590 * set tty modes. commands are:
592 * 0 - set to bbs mode, intr/quit disabled
593 * 1 - set to bbs mode, intr/quit enabled
594 * 2 - save current settings for later restoral
595 * 3 - restore saved settings
598 void sttybbs(int cmd) /* SysV version of sttybbs() */
601 static struct termios saved_settings;
603 if ( (cmd == 0) || (cmd == 1) ) {
605 live.c_iflag=ISTRIP|IXON|IXANY;
606 live.c_oflag=OPOST|ONLCR;
608 if (cmd==1) live.c_lflag=ISIG|NOFLSH;
610 if (cmd==SB_YES_INTR) {
611 live.c_cc[VINTR]=NEXT_KEY;
612 live.c_cc[VQUIT]=STOP_KEY;
613 signal(SIGINT,*sighandler);
614 signal(SIGQUIT,*sighandler);
617 signal(SIGINT,SIG_IGN);
618 signal(SIGQUIT,SIG_IGN);
619 live.c_cc[VINTR]=(-1);
620 live.c_cc[VQUIT]=(-1);
623 /* do we even need this stuff anymore? */
631 tcsetattr(0,TCSADRAIN,&live);
634 tcgetattr(0,&saved_settings);
637 tcsetattr(0,TCSADRAIN,&saved_settings);
641 void sttybbs(int cmd) /* BSD version of sttybbs() */
644 static struct sgttyb saved_settings;
646 if ( (cmd == 0) || (cmd == 1) ) {
648 live.sg_flags |= CBREAK;
649 live.sg_flags |= CRMOD;
650 live.sg_flags |= NL1;
651 live.sg_flags &= ~ECHO;
652 if (cmd==1) live.sg_flags |= NOFLSH;
656 gtty(0,&saved_settings);
659 stty(0,&saved_settings);
666 * display_help() - help file viewer
668 void display_help(char *name)
675 * fmout() - Citadel text formatter and paginator
677 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
678 /* screen width to use */
679 /* file to read from, or NULL to read from server */
680 /* nonzero if we should use the paginator */
681 /* screen height to use */
682 /* starting value for lines_printed, -1 for global */
683 /* nonzero if we should use hypertext mode */
691 if (starting_lp >= 0) {
692 lines_printed = starting_lp;
694 strcpy(aaa,""); old=255;
696 c=1; /* c is the current pos */
701 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
702 if (sigcaught) goto OOPS;
703 if (fp!=NULL) { /* read from file */
704 if (feof(fp)) eof_flag = 1;
707 buffer[strlen(buffer)+1] = 0;
708 buffer[strlen(buffer)] = a;
711 else { /* read from server */
713 serv_gets(&buffer[d]);
714 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
715 buffer[strlen(buffer)-1]=0;
716 if (!strcmp(&buffer[d],"000")) {
719 while(isspace(buffer[strlen(buffer)-1]))
720 buffer[strlen(buffer)-1] = 0;
728 buffer[strlen(buffer)+1] = 0;
730 strcpy(buffer,&buffer[1]);
734 if (a<=0) goto FMTEND;
736 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
737 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
740 lines_printed = checkpagin(lines_printed,pagin,height);
743 if (a>126) goto FMTA;
746 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
747 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
749 lines_printed = checkpagin(lines_printed,pagin,height);
751 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
754 if ((strlen(aaa)+c)>(width-5)) {
758 lines_printed = checkpagin(lines_printed,pagin,height);
760 printf("%s ",aaa); ++c; c=c+strlen(aaa);
764 if ((a==13)||(a==10)) {
768 lines_printed = checkpagin(lines_printed,pagin,height);
774 /* signal caught; drain the server */
777 } while(strcmp(aaa,"000"));
779 FMTEND: printf("\n");
781 lines_printed = checkpagin(lines_printed,pagin,height);
787 * support ANSI color if defined
789 void color(int colornum)
795 printf("%c[3%dm%c[1m", 27, colornum, 27);
804 printf("%c[2J%c[H", 27, 27);
811 * Detect whether ANSI color is available (answerback)
813 void send_ansi_detect(void) {
821 void look_for_ansi(void) {
832 if ( (now - AnsiDetect) < 2 ) sleep(1);
840 select(1, &rfds, NULL, NULL, &tv);
841 if (FD_ISSET(0, &rfds)) {
842 abuf[strlen(abuf)+1] = 0;
843 read(0, &abuf[strlen(abuf)], 1);
846 } while (FD_ISSET(0, &rfds));
848 for (a=0; a<strlen(abuf); ++a) {
849 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
850 && (abuf[a+2] == '?') ) {