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>
43 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
46 extern unsigned room_flags;
47 extern char room_name[];
48 extern struct CtdlServInfo serv_info;
50 extern char is_room_aide;
51 extern unsigned userflags;
52 extern char sigcaught;
53 extern char editor_path[];
54 extern char printcmd[];
55 extern char have_xterm;
56 extern char rc_username[];
57 extern char rc_password[];
58 extern char rc_floor_mode;
61 extern int lines_printed;
62 extern char express_msgs;
66 struct citcmd *cmdlist = NULL;
68 void sighandler(int which_sig);
69 void logoff(int code);
70 int struncmp(char *lstr, char *rstr, int len);
71 void formout(char *name);
72 int room_prompt(int qrflags);
73 void back(int spaces);
74 int checkpagin(int lp, int pagin, int height);
75 void color(int colornum);
78 /* these variables are local to this module */
79 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
80 int ok_to_interrupt = 0; /* print express msgs asynchronously */
81 time_t AnsiDetect; /* when did we send the detect code? */
82 int enable_color = 0; /* nonzero for ANSI color */
86 * print_express() - print express messages if there are any
88 void print_express(void) {
92 if (express_msgs == 0) return;
96 if (buf[0]!='1') return;
98 if (strlen(rc_exp_cmd) > 0) {
99 outpipe = popen(rc_exp_cmd, "w");
100 if (outpipe != NULL) {
101 while (serv_gets(buf), strcmp(buf,"000")) {
102 fprintf(outpipe, "%s\n", buf);
109 /* fall back to built-in express message display */
115 while (serv_gets(buf), strcmp(buf,"000")) {
123 void set_keepalives(int s)
125 keepalives_enabled = (char)s;
129 * This loop handles the "keepalive" messages sent to the server when idling.
131 void do_keepalive(void) {
133 static long idlet = 0L;
137 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
140 if (keepalives_enabled != KA_NO) {
142 if (keepalives_enabled == KA_YES) {
146 if (ok_to_interrupt == 1) {
149 printf("%s%c ",room_name,
150 room_prompt(room_flags));
159 int inkey(void) { /* get a character from the keyboard, with */
160 int a; /* the watchdog timer in effect if necessary */
163 long start_time, now;
170 /* This loop waits for keyboard input. If the keepalive
171 * timer expires, it sends a keepalive to the server if
172 * necessary and then waits again.
179 tv.tv_sec = S_KEEPALIVE;
183 if (((now-start_time) > SLEEPING)
184 && (SLEEPING != 0) && (getppid()==1)) {
185 printf("Sleeping? Call again.\n");
189 select(1, &rfds, NULL, NULL, &tv);
190 } while (!FD_ISSET(0, &rfds));
195 /* At this point, there's input, so fetch it.
196 * (There's a hole in the bucket...)
203 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
204 && ((a<32)||(a>126))) a=0;
209 void getline(char *string, int lim) /* Gets a line from the terminal */
210 /* Pointer to string buffer */
211 /* Maximum length - if negative, no-show */
216 if (lim<0) { lim=(0-lim); flag=1; }
219 GLA: a=inkey(); a=(a&127);
220 if ((a==8)&&(strlen(string)==0)) goto GLA;
221 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
222 if ((a==8)&&(string[0]!=0)) {
223 string[strlen(string)-1]=0;
224 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
225 if ((a==13)||(a==10)) {
234 if (flag==0) putc(a,stdout);
235 if (flag==1) putc('*',stdout);
241 * strprompt() - prompt for a string, print the existing value and
242 * allow the user to press return to keep it...
244 void strprompt(char *prompt, char *str, int len)
249 printf("%s [", prompt);
257 if (buf[0]!=0) strcpy(str,buf);
261 * intprompt() - like strprompt(), except for an integer
262 * (note that it RETURNS the new value!)
264 int intprompt(char *prompt, int ival, int imin, int imax)
271 strprompt(prompt,buf,15);
273 if (i<imin) printf("*** Must be no less than %d.\n",imin);
274 if (i>imax) printf("*** Must be no more than %d.\n",imax);
275 } while((i<imin)||(i>imax));
280 * newprompt() - prompt for a string with no existing value
281 * (clears out string buffer first)
283 void newprompt(char *prompt, char *str, int len)
293 int lkey(void) { /* returns a lower case value */
296 if (isupper(a)) a=tolower(a);
301 * parse the citadel.rc file
303 void load_command_set(void) {
311 /* first, set up some defaults for non-required variables */
313 strcpy(editor_path,"");
315 strcpy(rc_username,"");
316 strcpy(rc_password,"");
319 strcpy(rc_exp_cmd, "");
321 /* now try to open the citadel.rc file */
324 if (getenv("HOME") != NULL) {
325 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
326 ccfile = fopen(buf,"r");
329 ccfile = fopen("/usr/local/lib/citadel.rc","r");
332 sprintf(buf,"%s/citadel.rc",BBSDIR);
333 ccfile = fopen(buf,"r");
336 perror("commands: cannot open citadel.rc");
340 while (fgets(buf, 256, ccfile) != NULL) {
341 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
342 buf[strlen(buf)-1] = 0;
344 if (!struncmp(buf,"editor=",7))
345 strcpy(editor_path,&buf[7]);
347 if (!struncmp(buf,"printcmd=",9))
348 strcpy(printcmd,&buf[9]);
350 if (!struncmp(buf,"expcmd=",7))
351 strcpy(rc_exp_cmd,&buf[7]);
353 if (!struncmp(buf,"local_screen_dimensions=",24))
354 have_xterm = (char)atoi(&buf[24]);
356 if (!struncmp(buf,"use_floors=",11)) {
357 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
358 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
359 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
362 if (!struncmp(buf,"beep=",5)) {
363 rc_exp_beep = atoi(&buf[5]);
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]);
404 cptr->next = cmdlist;
415 * return the key associated with a command
417 char keycmd(char *cmdstr)
421 for (a=0; a<strlen(cmdstr); ++a)
423 return(tolower(cmdstr[a+1]));
429 * Output the string from a key command without the ampersand
430 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
432 char *cmd_expand(char *strbuf, int mode)
440 for (a=0; a<strlen(exp); ++a) {
441 if (strbuf[a] == '&') {
444 strcpy(&exp[a],&exp[a+1]);
449 strcpy(buf,&exp[a+2]);
457 if (!strncmp(&exp[a],"^r",2)) {
459 strcpy(&exp[a],room_name);
460 strcat(exp,&buf[a+2]);
463 if (!strncmp(&exp[a],"^c",2)) {
465 strcpy(&exp[a+1],&exp[a+2]);
476 * Comparison function to determine if entered commands match a
477 * command loaded from the config file.
479 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
485 if (is_room_aide) cmdax = 1;
486 if (axlevel >=6) cmdax = 2;
488 for (a=0; a<ncomp; ++a) {
489 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
490 || (cptr->c_axlevel > cmdax) )
498 * This function returns 1 if a given command requires a string input
500 int requires_string(struct citcmd *cptr, int ncomp)
505 strcpy(buf,cptr->c_keys[ncomp-1]);
506 for (a=0; a<strlen(buf); ++a) {
507 if (buf[a]==':') return(1);
514 * Input a command at the main prompt.
515 * This function returns an integer command number. If the command prompts
516 * for a string then it is placed in the supplied buffer.
518 int getcmd(char *argbuf)
528 /* if we're running in idiot mode, display a cute little menu */
529 IFNEXPERT formout("mainmenu");
531 print_express(); /* print express messages if there are any */
534 for (a=0; a<5; ++a) cmdbuf[a]=0;
535 /* now the room prompt... */
537 printf("\n%s%c ",room_name,room_prompt(room_flags));
544 if ( (ch == 8) && (cmdpos > 0) ) {
545 back(cmdspaces[cmdpos-1] + 1);
550 cmdbuf[cmdpos] = tolower(ch);
552 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
553 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
555 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
556 cmdspaces[cmdpos] = strlen(
557 cmd_expand(cptr->c_keys[cmdpos],0) );
559 if ((cptr->c_keys[cmdpos+1]) != 0)
565 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
566 if (cmdmatch(cmdbuf,cptr,5)) {
567 if (requires_string(cptr,cmdpos)) {
573 return(cptr->c_cmdnum);
578 printf("\rOne of ... \n");
579 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
580 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
581 for (a=0; a<5; ++a) {
582 printf("%s ",cmd_expand(cptr->c_keys[a],1));
588 printf("\n%s%c ",room_name,room_prompt(room_flags));
590 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
591 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
592 for (a=0; a<cmdpos; ++a) {
594 cmd_expand(cptr->c_keys[a],0));
610 * set tty modes. commands are:
612 * 0 - set to bbs mode, intr/quit disabled
613 * 1 - set to bbs mode, intr/quit enabled
614 * 2 - save current settings for later restoral
615 * 3 - restore saved settings
618 void sttybbs(int cmd) /* SysV version of sttybbs() */
621 static struct termios saved_settings;
623 if ( (cmd == 0) || (cmd == 1) ) {
625 live.c_iflag=ISTRIP|IXON|IXANY;
626 live.c_oflag=OPOST|ONLCR;
628 if (cmd==1) live.c_lflag=ISIG|NOFLSH;
630 if (cmd==SB_YES_INTR) {
631 live.c_cc[VINTR]=NEXT_KEY;
632 live.c_cc[VQUIT]=STOP_KEY;
633 signal(SIGINT,*sighandler);
634 signal(SIGQUIT,*sighandler);
637 signal(SIGINT,SIG_IGN);
638 signal(SIGQUIT,SIG_IGN);
639 live.c_cc[VINTR]=(-1);
640 live.c_cc[VQUIT]=(-1);
643 /* do we even need this stuff anymore? */
651 tcsetattr(0,TCSADRAIN,&live);
654 tcgetattr(0,&saved_settings);
657 tcsetattr(0,TCSADRAIN,&saved_settings);
661 void sttybbs(int cmd) /* BSD version of sttybbs() */
664 static struct sgttyb saved_settings;
666 if ( (cmd == 0) || (cmd == 1) ) {
668 live.sg_flags |= CBREAK;
669 live.sg_flags |= CRMOD;
670 live.sg_flags |= NL1;
671 live.sg_flags &= ~ECHO;
672 if (cmd==1) live.sg_flags |= NOFLSH;
676 gtty(0,&saved_settings);
679 stty(0,&saved_settings);
686 * display_help() - help file viewer
688 void display_help(char *name)
695 * fmout() - Citadel text formatter and paginator
697 int fmout(int width, FILE *fp, char pagin, int height, int starting_lp, char subst)
698 /* screen width to use */
699 /* file to read from, or NULL to read from server */
700 /* nonzero if we should use the paginator */
701 /* screen height to use */
702 /* starting value for lines_printed, -1 for global */
703 /* nonzero if we should use hypertext mode */
711 if (starting_lp >= 0) {
712 lines_printed = starting_lp;
714 strcpy(aaa,""); old=255;
716 c=1; /* c is the current pos */
721 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
722 if (sigcaught) goto OOPS;
723 if (fp!=NULL) { /* read from file */
724 if (feof(fp)) eof_flag = 1;
727 buffer[strlen(buffer)+1] = 0;
728 buffer[strlen(buffer)] = a;
731 else { /* read from server */
733 serv_gets(&buffer[d]);
734 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
735 buffer[strlen(buffer)-1]=0;
736 if (!strcmp(&buffer[d],"000")) {
739 while(isspace(buffer[strlen(buffer)-1]))
740 buffer[strlen(buffer)-1] = 0;
748 buffer[strlen(buffer)+1] = 0;
750 strcpy(buffer,&buffer[1]);
754 if (a<=0) goto FMTEND;
756 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
757 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
760 lines_printed = checkpagin(lines_printed,pagin,height);
763 if (a>126) goto FMTA;
766 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
767 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
769 lines_printed = checkpagin(lines_printed,pagin,height);
771 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
774 if ((strlen(aaa)+c)>(width-5)) {
778 lines_printed = checkpagin(lines_printed,pagin,height);
780 printf("%s ",aaa); ++c; c=c+strlen(aaa);
784 if ((a==13)||(a==10)) {
788 lines_printed = checkpagin(lines_printed,pagin,height);
794 /* signal caught; drain the server */
797 } while(strcmp(aaa,"000"));
799 FMTEND: printf("\n");
801 lines_printed = checkpagin(lines_printed,pagin,height);
807 * support ANSI color if defined
809 void color(int colornum)
815 printf("%c[3%dm%c[1m", 27, colornum, 27);
824 printf("%c[2J%c[H", 27, 27);
831 * Detect whether ANSI color is available (answerback)
833 void send_ansi_detect(void) {
841 void look_for_ansi(void) {
852 if ( (now - AnsiDetect) < 2 ) sleep(1);
860 select(1, &rfds, NULL, NULL, &tv);
861 if (FD_ISSET(0, &rfds)) {
862 abuf[strlen(abuf)+1] = 0;
863 read(0, &abuf[strlen(abuf)], 1);
866 } while (FD_ISSET(0, &rfds));
868 for (a=0; a<strlen(abuf); ++a) {
869 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
870 && (abuf[a+2] == '?') ) {