4 * commands.c - front end for Citadel
6 * This version is the traditional command parser for room prompts.
19 #include <sys/types.h>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
39 #include "citadel_decls.h"
41 #include "routines2.h"
53 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
58 int rc_allow_attachments;
59 int rc_display_message_numbers;
60 int rc_force_mail_prompts;
64 int next_lazy_cmd = 5;
66 struct citcmd *cmdlist = NULL;
69 /* these variables are local to this module */
70 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
71 int ok_to_interrupt = 0; /* print express msgs asynchronously */
72 time_t AnsiDetect; /* when did we send the detect code? */
73 int enable_color = 0; /* nonzero for ANSI color */
77 * print_express() - print express messages if there are any
79 void print_express(void)
84 if (express_msgs == 0)
92 if (strlen(rc_exp_cmd) > 0) {
93 outpipe = popen(rc_exp_cmd, "w");
94 if (outpipe != NULL) {
95 while (serv_gets(buf), strcmp(buf, "000")) {
96 fprintf(outpipe, "%s\n", buf);
102 /* fall back to built-in express message display */
108 while (serv_gets(buf), strcmp(buf, "000")) {
116 void set_keepalives(int s)
118 keepalives_enabled = (char) s;
122 * This loop handles the "keepalive" messages sent to the server when idling.
124 void do_keepalive(void)
127 static time_t idlet = 0;
131 if ((now - idlet) < ((long) S_KEEPALIVE))
135 /* Do a space-backspace to keep socksified telnet sessions open */
139 if (keepalives_enabled != KA_NO) {
141 if (keepalives_enabled == KA_YES) {
145 if (ok_to_interrupt == 1) {
146 printf("\r%64s\r", "");
148 printf("%s%c ", room_name,
149 room_prompt(room_flags));
159 { /* get a character from the keyboard, with */
160 int a; /* the watchdog timer in effect if necessary */
163 time_t start_time, now;
171 /* This loop waits for keyboard input. If the keepalive
172 * timer expires, it sends a keepalive to the server if
173 * 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");
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...)
205 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
206 && ((a < 32) || (a > 126)))
214 { /* Returns 1 for yes, 0 for no */
230 /* Returns 1 for yes, 0 for no, arg is default value */
253 /* Gets a line from the terminal */
254 /* string == Pointer to string buffer */
255 /* lim == Maximum length - if negative, no-show */
256 void getline(char *string, int lim)
269 if ((a == 8) && (strlen(string) == 0))
271 if ((a != 13) && (a != 8) && (strlen(string) == lim))
273 if ((a == 8) && (string[0] != 0)) {
274 string[strlen(string) - 1] = 0;
280 if ((a == 13) || (a == 10)) {
299 * strprompt() - prompt for a string, print the existing value and
300 * allow the user to press return to keep it...
302 void strprompt(char *prompt, char *str, int len)
307 printf("%s ", prompt);
310 color(BRIGHT_MAGENTA);
324 * boolprompt() - prompt for a yes/no, print the existing value and
325 * allow the user to press return to keep it...
327 int boolprompt(char *prompt, int prev_val)
332 printf("%s ", prompt);
335 color(BRIGHT_MAGENTA);
336 printf("%s", (prev_val ? "Yes" : "No"));
340 r = (yesno_d(prev_val));
346 * intprompt() - like strprompt(), except for an integer
347 * (note that it RETURNS the new value!)
349 int intprompt(char *prompt, int ival, int imin, int imax)
355 snprintf(buf, sizeof buf, "%d", i);
356 strprompt(prompt, buf, 15);
359 printf("*** Must be no less than %d.\n", imin);
361 printf("*** Must be no more than %d.\n", imax);
362 } while ((i < imin) || (i > imax));
367 * newprompt() - prompt for a string with no existing value
368 * (clears out string buffer first)
370 void newprompt(char *prompt, char *str, int len)
372 color(BRIGHT_MAGENTA);
373 printf("%s", prompt);
381 { /* returns a lower case value */
390 * parse the citadel.rc file
392 void load_command_set(void)
397 struct citcmd *lastcmd = NULL;
402 /* first, set up some defaults for non-required variables */
404 strcpy(editor_path, "");
405 strcpy(printcmd, "");
406 strcpy(rc_username, "");
407 strcpy(rc_password, "");
410 rc_allow_attachments = 0;
411 strcpy(rc_exp_cmd, "");
412 rc_display_message_numbers = 0;
413 rc_force_mail_prompts = 0;
416 /* now try to open the citadel.rc file */
419 if (getenv("HOME") != NULL) {
420 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
421 ccfile = fopen(buf, "r");
423 if (ccfile == NULL) {
424 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
426 if (ccfile == NULL) {
427 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
428 ccfile = fopen(buf, "r");
430 if (ccfile == NULL) {
431 ccfile = fopen("./citadel.rc", "r");
433 if (ccfile == NULL) {
434 perror("commands: cannot open citadel.rc");
437 while (fgets(buf, 256, ccfile) != NULL) {
438 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
439 buf[strlen(buf) - 1] = 0;
441 if (!struncmp(buf, "editor=", 7))
442 strcpy(editor_path, &buf[7]);
444 if (!struncmp(buf, "printcmd=", 9))
445 strcpy(printcmd, &buf[9]);
447 if (!struncmp(buf, "expcmd=", 7))
448 strcpy(rc_exp_cmd, &buf[7]);
450 if (!struncmp(buf, "local_screen_dimensions=", 24))
451 have_xterm = (char) atoi(&buf[24]);
453 if (!struncmp(buf, "use_floors=", 11)) {
454 if (!strucmp(&buf[11], "yes"))
455 rc_floor_mode = RC_YES;
456 if (!strucmp(&buf[11], "no"))
457 rc_floor_mode = RC_NO;
458 if (!strucmp(&buf[11], "default"))
459 rc_floor_mode = RC_DEFAULT;
461 if (!struncmp(buf, "beep=", 5)) {
462 rc_exp_beep = atoi(&buf[5]);
464 if (!struncmp(buf, "allow_attachments=", 18)) {
465 rc_allow_attachments = atoi(&buf[18]);
467 if (!struncmp(buf, "display_message_numbers=", 24)) {
468 rc_display_message_numbers = atoi(&buf[24]);
470 if (!struncmp(buf, "force_mail_prompts=", 19)) {
471 rc_force_mail_prompts = atoi(&buf[19]);
473 if (!struncmp(buf, "ansi_color=", 11)) {
474 if (!strncasecmp(&buf[11], "on", 2))
476 if (!strncasecmp(&buf[11], "auto", 4))
477 rc_ansi_color = 2; /* autodetect */
478 if (!strncasecmp(&buf[11], "user", 4))
479 rc_ansi_color = 3; /* user config */
481 if (!struncmp(buf, "username=", 9))
482 strcpy(rc_username, &buf[9]);
484 if (!struncmp(buf, "password=", 9))
485 strcpy(rc_password, &buf[9]);
487 if (!struncmp(buf, "cmd=", 4)) {
488 strcpy(buf, &buf[4]);
490 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
492 cptr->c_cmdnum = atoi(buf);
493 for (d = strlen(buf); d >= 0; --d)
496 strcpy(buf, &buf[b + 1]);
498 cptr->c_axlevel = atoi(buf);
499 for (d = strlen(buf); d >= 0; --d)
502 strcpy(buf, &buf[b + 1]);
504 for (a = 0; a < 5; ++a)
505 cptr->c_keys[a][0] = 0;
509 buf[strlen(buf) + 1] = 0;
510 while (strlen(buf) > 0) {
512 for (d = strlen(buf); d >= 0; --d)
515 strncpy(cptr->c_keys[a], buf, b);
516 cptr->c_keys[a][b] = 0;
518 strcpy(buf, &buf[b + 1]);
528 lastcmd->next = cptr;
538 * return the key associated with a command
540 char keycmd(char *cmdstr)
544 for (a = 0; a < strlen(cmdstr); ++a)
545 if (cmdstr[a] == '&')
546 return (tolower(cmdstr[a + 1]));
552 * Output the string from a key command without the ampersand
553 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
555 char *cmd_expand(char *strbuf, int mode)
563 for (a = 0; a < strlen(exp); ++a) {
564 if (strbuf[a] == '&') {
567 strcpy(&exp[a], &exp[a + 1]);
571 strcpy(buf, &exp[a + 2]);
577 if (!strncmp(&exp[a], "^r", 2)) {
579 strcpy(&exp[a], room_name);
580 strcat(exp, &buf[a + 2]);
582 if (!strncmp(&exp[a], "^c", 2)) {
584 strcpy(&exp[a + 1], &exp[a + 2]);
594 * Comparison function to determine if entered commands match a
595 * command loaded from the config file.
597 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
608 for (a = 0; a < ncomp; ++a) {
609 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
610 || (cptr->c_axlevel > cmdax))
618 * This function returns 1 if a given command requires a string input
620 int requires_string(struct citcmd *cptr, int ncomp)
625 strcpy(buf, cptr->c_keys[ncomp - 1]);
626 for (a = 0; a < strlen(buf); ++a) {
635 * Input a command at the main prompt.
636 * This function returns an integer command number. If the command prompts
637 * for a string then it is placed in the supplied buffer.
639 int getcmd(char *argbuf)
650 /* Switch color support on or off if we're in user mode */
651 if (rc_ansi_color == 3) {
652 if (userflags & US_COLOR)
657 /* if we're running in idiot mode, display a cute little menu */
658 IFNEXPERT formout("mainmenu");
660 print_express(); /* print express messages if there are any */
663 for (a = 0; a < 5; ++a)
665 /* now the room prompt... */
668 printf("\n%s", room_name);
670 printf("%c ", room_prompt(room_flags));
677 /* Handle the backspace key, but only if there's something
678 * to backspace over...
680 if ((ch == 8) && (cmdpos > 0)) {
681 back(cmdspaces[cmdpos - 1] + 1);
685 /* Spacebar invokes "lazy traversal" commands */
686 if ((ch == 32) && (cmdpos == 0)) {
687 this_lazy_cmd = next_lazy_cmd;
688 if (this_lazy_cmd == 13)
690 if (this_lazy_cmd == 5)
692 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
693 if (cptr->c_cmdnum == this_lazy_cmd) {
694 for (a = 0; a < 5; ++a)
695 if (cptr->c_keys[a][0] != 0)
696 printf("%s ", cmd_expand(
697 cptr->c_keys[a], 0));
699 return (this_lazy_cmd);
703 return (this_lazy_cmd);
705 /* Otherwise, process the command */
706 cmdbuf[cmdpos] = tolower(ch);
708 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
709 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
711 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
712 cmdspaces[cmdpos] = strlen(
713 cmd_expand(cptr->c_keys[cmdpos], 0));
715 if ((cptr->c_keys[cmdpos + 1]) != 0)
721 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
722 if (cmdmatch(cmdbuf, cptr, 5)) {
723 /* We've found our command. */
724 if (requires_string(cptr, cmdpos)) {
730 /* If this command is one that changes rooms,
731 * then the next lazy-command (space bar)
732 * should be "read new" instead of "goto"
734 if ((cptr->c_cmdnum == 5)
735 || (cptr->c_cmdnum == 6)
736 || (cptr->c_cmdnum == 47)
737 || (cptr->c_cmdnum == 52)
738 || (cptr->c_cmdnum == 16)
739 || (cptr->c_cmdnum == 20))
742 return (cptr->c_cmdnum);
748 printf("\rOne of ... \n");
749 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
750 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
751 for (a = 0; a < 5; ++a) {
752 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
758 printf("\n%s%c ", room_name, room_prompt(room_flags));
760 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
761 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
762 for (a = 0; a < cmdpos; ++a) {
764 cmd_expand(cptr->c_keys[a], 0));
779 * set tty modes. commands are:
781 * 0 - set to bbs mode, intr/quit disabled
782 * 1 - set to bbs mode, intr/quit enabled
783 * 2 - save current settings for later restoral
784 * 3 - restore saved settings
786 #ifdef HAVE_TERMIOS_H
787 void sttybbs(int cmd)
788 { /* SysV version of sttybbs() */
790 static struct termios saved_settings;
791 static int last_cmd = 0;
798 if ((cmd == 0) || (cmd == 1)) {
800 live.c_iflag = ISTRIP | IXON | IXANY;
801 live.c_oflag = OPOST | ONLCR;
802 live.c_lflag = ISIG | NOFLSH;
804 if (cmd == SB_YES_INTR) {
805 live.c_cc[VINTR] = NEXT_KEY;
806 live.c_cc[VQUIT] = STOP_KEY;
807 signal(SIGINT, *sighandler);
808 signal(SIGQUIT, *sighandler);
810 signal(SIGINT, SIG_IGN);
811 signal(SIGQUIT, SIG_IGN);
812 live.c_cc[VINTR] = (-1);
813 live.c_cc[VQUIT] = (-1);
816 /* do we even need this stuff anymore? */
818 live.c_cc[VERASE] = 8;
819 live.c_cc[VKILL] = 24;
821 live.c_cc[VEOL] = 255;
822 live.c_cc[VEOL2] = 0;
823 live.c_cc[VSTART] = 0;
824 tcsetattr(0, TCSADRAIN, &live);
827 tcgetattr(0, &saved_settings);
830 tcsetattr(0, TCSADRAIN, &saved_settings);
834 void sttybbs(int cmd)
835 { /* BSD version of sttybbs() */
837 static struct sgttyb saved_settings;
839 if ((cmd == 0) || (cmd == 1)) {
841 live.sg_flags |= CBREAK;
842 live.sg_flags |= CRMOD;
843 live.sg_flags |= NL1;
844 live.sg_flags &= ~ECHO;
846 live.sg_flags |= NOFLSH;
850 gtty(0, &saved_settings);
853 stty(0, &saved_settings);
860 * display_help() - help file viewer
862 void display_help(char *name)
869 * fmout() - Citadel text formatter and paginator
871 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
872 /* screen width to use */
873 /* file to read from, or NULL to read from server */
874 /* nonzero if we should use the paginator */
875 /* screen height to use */
876 /* starting value for lines_printed, -1 for global */
877 /* nonzero if we should use hypertext mode */
885 if (starting_lp >= 0) {
886 lines_printed = starting_lp;
891 c = 1; /* c is the current pos */
896 FMTA:while ((eof_flag == 0) && (strlen(buffer) < 126)) {
899 if (fp != NULL) { /* read from file */
904 buffer[strlen(buffer) + 1] = 0;
905 buffer[strlen(buffer)] = a;
907 } else { /* read from server */
909 serv_gets(&buffer[d]);
910 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
911 buffer[strlen(buffer) - 1] = 0;
912 if (!strcmp(&buffer[d], "000")) {
915 while (isspace(buffer[strlen(buffer) - 1]))
916 buffer[strlen(buffer) - 1] = 0;
924 buffer[strlen(buffer) + 1] = 0;
926 strcpy(buffer, &buffer[1]);
933 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
935 if (((old == 13) || (old == 10)) && (isspace(real))) {
938 lines_printed = checkpagin(lines_printed, pagin, height);
945 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
950 lines_printed = checkpagin(lines_printed, pagin, height);
957 if ((strlen(aaa) + c) > (width - 5)) {
961 lines_printed = checkpagin(lines_printed, pagin, height);
969 if ((a == 13) || (a == 10)) {
973 lines_printed = checkpagin(lines_printed, pagin, height);
979 /* signal caught; drain the server */
982 } while (strcmp(aaa, "000"));
986 lines_printed = checkpagin(lines_printed, pagin, height);
992 * support ANSI color if defined
994 void color(int colornum)
996 static int is_bold = 0;
997 static int hold_color, current_color;
999 if (colornum == COLOR_PUSH) {
1000 hold_color = current_color;
1004 if (colornum == COLOR_POP) {
1009 current_color = colornum;
1011 printf("\033[3%dm", (colornum % 8));
1012 if ((colornum >= 8) && (is_bold == 0)) {
1015 } else if ((colornum < 8) && (is_bold == 1)) {
1023 void cls(int colornum)
1026 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1033 * Detect whether ANSI color is available (answerback)
1035 void send_ansi_detect(void)
1037 if (rc_ansi_color == 2) {
1044 void look_for_ansi(void)
1052 if (rc_ansi_color == 0) {
1054 } else if (rc_ansi_color == 1) {
1056 } else if (rc_ansi_color == 2) {
1058 /* otherwise, do the auto-detect */
1063 if ((now - AnsiDetect) < 2)
1072 select(1, &rfds, NULL, NULL, &tv);
1073 if (FD_ISSET(0, &rfds)) {
1074 abuf[strlen(abuf) + 1] = 0;
1075 read(0, &abuf[strlen(abuf)], 1);
1077 } while (FD_ISSET(0, &rfds));
1079 for (a = 0; a < strlen(abuf); ++a) {
1080 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1081 && (abuf[a + 2] == '?')) {
1090 * Display key options (highlight hotkeys inside angle brackets)
1092 void keyopt(char *buf) {
1096 for (i=0; i<strlen(buf); ++i) {
1098 putc(buf[i], stdout);
1099 color(BRIGHT_MAGENTA);
1104 putc(buf[i], stdout);