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>
42 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
45 extern unsigned room_flags;
46 extern char room_name[];
47 extern struct CtdlServInfo serv_info;
49 extern char is_room_aide;
50 extern unsigned userflags;
51 extern char sigcaught;
52 extern char editor_path[];
53 extern char printcmd[];
54 extern char have_xterm;
55 extern char rc_username[];
56 extern char rc_password[];
57 extern char rc_floor_mode;
60 extern int lines_printed;
61 extern char express_msgs;
65 struct citcmd *cmdlist = NULL;
77 /* these variables are local to this module */
78 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
79 int ok_to_interrupt = 0; /* print express msgs asynchronously */
80 time_t AnsiDetect; /* when did we send the detect code? */
81 int enable_color = 0; /* nonzero for ANSI color */
85 * print_express() - print express messages if there are any
87 void print_express() {
91 if (express_msgs == 0) return;
95 if (buf[0]!='1') return;
97 if (strlen(rc_exp_cmd) > 0) {
98 outpipe = popen(rc_exp_cmd, "w");
99 if (outpipe != NULL) {
100 while (serv_gets(buf), strcmp(buf,"000")) {
101 fprintf(outpipe, "%s\n", buf);
108 /* fall back to built-in express message display */
114 while (serv_gets(buf), strcmp(buf,"000")) {
122 void set_keepalives(s)
124 keepalives_enabled = (char)s;
128 * This loop handles the "keepalive" messages sent to the server when idling.
130 void do_keepalive() {
132 static long idlet = 0L;
136 if ((now - idlet) < ((long)S_KEEPALIVE)) return;
139 if (keepalives_enabled != KA_NO) {
141 if (keepalives_enabled == KA_YES) {
145 if (ok_to_interrupt == 1) {
148 printf("%s%c ",room_name,
149 room_prompt(room_flags));
158 int inkey() { /* get a character from the keyboard, with */
159 int a; /* the watchdog timer in effect if necessary */
162 long start_time, now;
169 /* This loop waits for keyboard input. If the keepalive
170 * timer expires, it sends a keepalive to the server if
171 * necessary and then waits again.
178 tv.tv_sec = S_KEEPALIVE;
182 if (((now-start_time) > SLEEPING)
183 && (SLEEPING != 0) && (getppid()==1)) {
184 printf("Sleeping? Call again.\n");
188 select(1, &rfds, NULL, NULL, &tv);
189 } while (!FD_ISSET(0, &rfds));
194 /* At this point, there's input, so fetch it.
195 * (There's a hole in the bucket...)
202 if (((a!=4)&&(a!=13)&&(a!=8)&&(a!=NEXT_KEY)&&(a!=STOP_KEY))
203 && ((a<32)||(a>126))) a=0;
208 void getline(string,lim) /* Gets a line from the terminal */
209 char string[]; /* Pointer to string buffer */
210 int lim; /* Maximum length - if negative, no-show */
215 if (lim<0) { lim=(0-lim); flag=1; }
218 GLA: a=inkey(); a=(a&127);
219 if ((a==8)&&(strlen(string)==0)) goto GLA;
220 if ((a!=13)&&(a!=8)&&(strlen(string)==lim)) goto GLA;
221 if ((a==8)&&(string[0]!=0)) {
222 string[strlen(string)-1]=0;
223 putc(8,stdout); putc(32,stdout); putc(8,stdout); goto GLA; }
224 if ((a==13)||(a==10)) {
233 if (flag==0) putc(a,stdout);
234 if (flag==1) putc('*',stdout);
240 * strprompt() - prompt for a string, print the existing value and
241 * allow the user to press return to keep it...
243 void strprompt(prompt,str,len)
250 printf("%s [", prompt);
258 if (buf[0]!=0) strcpy(str,buf);
262 * intprompt() - like strprompt(), except for an integer
263 * (note that it RETURNS the new value!)
265 int intprompt(prompt,ival,imin,imax)
275 strprompt(prompt,buf,15);
277 if (i<imin) printf("*** Must be no less than %d.\n",imin);
278 if (i>imax) printf("*** Must be no more than %d.\n",imax);
279 } while((i<imin)||(i>imax));
284 * newprompt() - prompt for a string with no existing value
285 * (clears out string buffer first)
287 void newprompt(prompt,str,len)
299 int lkey() { /* returns a lower case value */
302 if (isupper(a)) a=tolower(a);
307 * parse the citadel.rc file
309 void load_command_set() {
317 /* first, set up some defaults for non-required variables */
319 strcpy(editor_path,"");
321 strcpy(rc_username,"");
322 strcpy(rc_password,"");
325 strcpy(rc_exp_cmd, "");
327 /* now try to open the citadel.rc file */
330 if (getenv("HOME") != NULL) {
331 sprintf(buf,"%s/.citadelrc",getenv("HOME"));
332 ccfile = fopen(buf,"r");
335 ccfile = fopen("/usr/local/lib/citadel.rc","r");
338 sprintf(buf,"%s/citadel.rc",BBSDIR);
339 ccfile = fopen(buf,"r");
342 perror("commands: cannot open citadel.rc");
346 while (fgets(buf, 256, ccfile) != NULL) {
347 while ( (strlen(buf)>0) ? (isspace(buf[strlen(buf)-1])) : 0 )
348 buf[strlen(buf)-1] = 0;
350 if (!struncmp(buf,"editor=",7))
351 strcpy(editor_path,&buf[7]);
353 if (!struncmp(buf,"printcmd=",9))
354 strcpy(printcmd,&buf[9]);
356 if (!struncmp(buf,"expcmd=",7))
357 strcpy(rc_exp_cmd,&buf[7]);
359 if (!struncmp(buf,"local_screen_dimensions=",24))
360 have_xterm = (char)atoi(&buf[24]);
362 if (!struncmp(buf,"use_floors=",11)) {
363 if (!strucmp(&buf[11],"yes")) rc_floor_mode = RC_YES;
364 if (!strucmp(&buf[11],"no")) rc_floor_mode = RC_NO;
365 if (!strucmp(&buf[11],"default")) rc_floor_mode = RC_DEFAULT;
368 if (!struncmp(buf,"beep=",5)) {
369 rc_exp_beep = atoi(&buf[5]);
372 if (!struncmp(buf,"username=",9))
373 strcpy(rc_username,&buf[9]);
375 if (!struncmp(buf,"password=",9))
376 strcpy(rc_password,&buf[9]);
378 if (!struncmp(buf,"cmd=",4)) {
381 cptr = (struct citcmd *) malloc (sizeof(struct citcmd));
383 cptr->c_cmdnum = atoi (buf);
384 for (d=strlen(buf); d>=0; --d)
385 if (buf[d]==',') b=d;
386 strcpy (buf, &buf[b+1]);
388 cptr->c_axlevel = atoi (buf);
389 for (d=strlen(buf); d>=0; --d)
390 if (buf[d]==',') b=d;
391 strcpy (buf, &buf[b+1]);
393 for (a=0; a<5; ++a) cptr->c_keys[a][0] = 0;
396 buf[strlen(buf)+1] = 0;
397 while (strlen(buf) > 0) {
399 for (d=strlen(buf); d>=0; --d)
400 if (buf[d]==',') b=d;
401 strncpy(cptr->c_keys[a],buf,b);
402 cptr->c_keys[a][b] = 0;
404 strcpy(buf,&buf[b+1]);
410 cptr->next = cmdlist;
421 * return the key associated with a command
427 for (a=0; a<strlen(cmdstr); ++a)
429 return(tolower(cmdstr[a+1]));
435 * Output the string from a key command without the ampersand
436 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
438 char *cmd_expand(strbuf,mode)
447 for (a=0; a<strlen(exp); ++a) {
448 if (strbuf[a] == '&') {
451 strcpy(&exp[a],&exp[a+1]);
456 strcpy(buf,&exp[a+2]);
464 if (!strncmp(&exp[a],"^r",2)) {
466 strcpy(&exp[a],room_name);
467 strcat(exp,&buf[a+2]);
470 if (!strncmp(&exp[a],"^c",2)) {
472 strcpy(&exp[a+1],&exp[a+2]);
483 * Comparison function to determine if entered commands match a
484 * command loaded from the config file.
486 int cmdmatch(cmdbuf,cptr,ncomp)
494 if (is_room_aide) cmdax = 1;
495 if (axlevel >=6) cmdax = 2;
497 for (a=0; a<ncomp; ++a) {
498 if ( (tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
499 || (cptr->c_axlevel > cmdax) )
507 * This function returns 1 if a given command requires a string input
509 int requires_string(cptr,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.
538 /* if we're running in idiot mode, display a cute little menu */
539 IFNEXPERT formout("mainmenu");
541 print_express(); /* print express messages if there are any */
544 for (a=0; a<5; ++a) cmdbuf[a]=0;
545 /* now the room prompt... */
547 printf("\n%s%c ",room_name,room_prompt(room_flags));
554 if ( (ch == 8) && (cmdpos > 0) ) {
555 back(cmdspaces[cmdpos-1] + 1);
560 cmdbuf[cmdpos] = tolower(ch);
562 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
563 if (cmdmatch(cmdbuf,cptr,cmdpos+1)) {
565 printf("%s",cmd_expand(cptr->c_keys[cmdpos],0));
566 cmdspaces[cmdpos] = strlen(
567 cmd_expand(cptr->c_keys[cmdpos],0) );
569 if ((cptr->c_keys[cmdpos+1]) != 0)
575 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
576 if (cmdmatch(cmdbuf,cptr,5)) {
577 if (requires_string(cptr,cmdpos)) {
583 return(cptr->c_cmdnum);
588 printf("\rOne of ... \n");
589 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
590 if (cmdmatch(cmdbuf,cptr,cmdpos)) {
591 for (a=0; a<5; ++a) {
592 printf("%s ",cmd_expand(cptr->c_keys[a],1));
598 printf("\n%s%c ",room_name,room_prompt(room_flags));
600 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
601 if ((got==0)&&(cmdmatch(cmdbuf,cptr,cmdpos))) {
602 for (a=0; a<cmdpos; ++a) {
604 cmd_expand(cptr->c_keys[a],0));
620 * set tty modes. commands are:
622 * 0 - set to bbs mode, intr/quit disabled
623 * 1 - set to bbs mode, intr/quit enabled
624 * 2 - save current settings for later restoral
625 * 3 - restore saved settings
628 void sttybbs(cmd) /* SysV version of sttybbs() */
631 static struct termios saved_settings;
633 if ( (cmd == 0) || (cmd == 1) ) {
635 live.c_iflag=ISTRIP|IXON|IXANY;
636 live.c_oflag=OPOST|ONLCR;
638 if (cmd==1) live.c_lflag=ISIG|NOFLSH;
640 if (cmd==SB_YES_INTR) {
641 live.c_cc[VINTR]=NEXT_KEY;
642 live.c_cc[VQUIT]=STOP_KEY;
643 signal(SIGINT,*sighandler);
644 signal(SIGQUIT,*sighandler);
647 signal(SIGINT,SIG_IGN);
648 signal(SIGQUIT,SIG_IGN);
649 live.c_cc[VINTR]=(-1);
650 live.c_cc[VQUIT]=(-1);
653 /* do we even need this stuff anymore? */
661 tcsetattr(0,TCSADRAIN,&live);
664 tcgetattr(0,&saved_settings);
667 tcsetattr(0,TCSADRAIN,&saved_settings);
671 void sttybbs(cmd) /* BSD version of sttybbs() */
674 static struct sgttyb saved_settings;
676 if ( (cmd == 0) || (cmd == 1) ) {
678 live.sg_flags |= CBREAK;
679 live.sg_flags |= CRMOD;
680 live.sg_flags |= NL1;
681 live.sg_flags &= ~ECHO;
682 if (cmd==1) live.sg_flags |= NOFLSH;
686 gtty(0,&saved_settings);
689 stty(0,&saved_settings);
696 * display_help() - help file viewer
698 void display_help(name)
705 * fmout() - Citadel text formatter and paginator
707 int fmout(width,fp,pagin,height,starting_lp,subst)
708 int width; /* screen width to use */
709 FILE *fp; /* file to read from, or NULL to read from server */
710 char pagin; /* nonzero if we should use the paginator */
711 int height; /* screen height to use */
712 int starting_lp; /* starting value for lines_printed, -1 for global */
713 char subst; /* nonzero if we should use hypertext mode */
721 if (starting_lp >= 0) {
722 lines_printed = starting_lp;
724 strcpy(aaa,""); old=255;
726 c=1; /* c is the current pos */
731 FMTA: while ( (eof_flag==0) && (strlen(buffer)<126) ) {
732 if (sigcaught) goto OOPS;
733 if (fp!=NULL) { /* read from file */
734 if (feof(fp)) eof_flag = 1;
737 buffer[strlen(buffer)+1] = 0;
738 buffer[strlen(buffer)] = a;
741 else { /* read from server */
743 serv_gets(&buffer[d]);
744 while ( (!isspace(buffer[d])) && (isspace(buffer[strlen(buffer)-1])) )
745 buffer[strlen(buffer)-1]=0;
746 if (!strcmp(&buffer[d],"000")) {
749 while(isspace(buffer[strlen(buffer)-1]))
750 buffer[strlen(buffer)-1] = 0;
758 buffer[strlen(buffer)+1] = 0;
760 strcpy(buffer,&buffer[1]);
764 if (a<=0) goto FMTEND;
766 if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32;
767 if ( ((old==13)||(old==10)) && (isspace(real)) ) {
770 lines_printed = checkpagin(lines_printed,pagin,height);
773 if (a>126) goto FMTA;
776 if ( ((strlen(aaa)+c)>(width-5)) && (strlen(aaa)>(width-5)) ) {
777 printf("\n%s",aaa); c=strlen(aaa); aaa[0]=0;
779 lines_printed = checkpagin(lines_printed,pagin,height);
781 b=strlen(aaa); aaa[b]=a; aaa[b+1]=0;
784 if ((strlen(aaa)+c)>(width-5)) {
788 lines_printed = checkpagin(lines_printed,pagin,height);
790 printf("%s ",aaa); ++c; c=c+strlen(aaa);
794 if ((a==13)||(a==10)) {
798 lines_printed = checkpagin(lines_printed,pagin,height);
804 /* signal caught; drain the server */
807 } while(strcmp(aaa,"000"));
809 FMTEND: printf("\n");
811 lines_printed = checkpagin(lines_printed,pagin,height);
817 * support ANSI color if defined
825 printf("%c[3%dm%c[1m", 27, colornum, 27);
834 printf("%c[2J%c[H", 27, 27);
841 * Detect whether ANSI color is available (answerback)
843 void send_ansi_detect() {
851 void look_for_ansi() {
862 if ( (now - AnsiDetect) < 2 ) sleep(1);
870 select(1, &rfds, NULL, NULL, &tv);
871 if (FD_ISSET(0, &rfds)) {
872 abuf[strlen(abuf)+1] = 0;
873 read(0, &abuf[strlen(abuf)], 1);
876 } while (FD_ISSET(0, &rfds));
878 for (a=0; a<strlen(abuf); ++a) {
879 if ( (abuf[a] == 27) && (abuf[a+1] == '[')
880 && (abuf[a+2] == '?') ) {