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);
323 * boolprompt() - prompt for a yes/no, print the existing value and
324 * allow the user to press return to keep it...
326 int boolprompt(char *prompt, int prev_val)
331 printf("%s ", prompt);
334 color(BRIGHT_MAGENTA);
335 printf("%s", (prev_val ? "Yes" : "No"));
339 r = (yesno_d(prev_val));
345 * intprompt() - like strprompt(), except for an integer
346 * (note that it RETURNS the new value!)
348 int intprompt(char *prompt, int ival, int imin, int imax)
354 snprintf(buf, sizeof buf, "%d", i);
355 strprompt(prompt, buf, 15);
358 printf("*** Must be no less than %d.\n", imin);
360 printf("*** Must be no more than %d.\n", imax);
361 } while ((i < imin) || (i > imax));
366 * newprompt() - prompt for a string with no existing value
367 * (clears out string buffer first)
369 void newprompt(char *prompt, char *str, int len)
371 color(BRIGHT_MAGENTA);
372 printf("%s", prompt);
380 { /* returns a lower case value */
389 * parse the citadel.rc file
391 void load_command_set(void)
396 struct citcmd *lastcmd = NULL;
401 /* first, set up some defaults for non-required variables */
403 strcpy(editor_path, "");
404 strcpy(printcmd, "");
405 strcpy(rc_username, "");
406 strcpy(rc_password, "");
409 rc_allow_attachments = 0;
410 strcpy(rc_exp_cmd, "");
411 rc_display_message_numbers = 0;
412 rc_force_mail_prompts = 0;
415 /* now try to open the citadel.rc file */
418 if (getenv("HOME") != NULL) {
419 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
420 ccfile = fopen(buf, "r");
422 if (ccfile == NULL) {
423 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
425 if (ccfile == NULL) {
426 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
427 ccfile = fopen(buf, "r");
429 if (ccfile == NULL) {
430 ccfile = fopen("./citadel.rc", "r");
432 if (ccfile == NULL) {
433 perror("commands: cannot open citadel.rc");
436 while (fgets(buf, 256, ccfile) != NULL) {
437 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
438 buf[strlen(buf) - 1] = 0;
440 if (!struncmp(buf, "editor=", 7))
441 strcpy(editor_path, &buf[7]);
443 if (!struncmp(buf, "printcmd=", 9))
444 strcpy(printcmd, &buf[9]);
446 if (!struncmp(buf, "expcmd=", 7))
447 strcpy(rc_exp_cmd, &buf[7]);
449 if (!struncmp(buf, "local_screen_dimensions=", 24))
450 have_xterm = (char) atoi(&buf[24]);
452 if (!struncmp(buf, "use_floors=", 11)) {
453 if (!strucmp(&buf[11], "yes"))
454 rc_floor_mode = RC_YES;
455 if (!strucmp(&buf[11], "no"))
456 rc_floor_mode = RC_NO;
457 if (!strucmp(&buf[11], "default"))
458 rc_floor_mode = RC_DEFAULT;
460 if (!struncmp(buf, "beep=", 5)) {
461 rc_exp_beep = atoi(&buf[5]);
463 if (!struncmp(buf, "allow_attachments=", 18)) {
464 rc_allow_attachments = atoi(&buf[18]);
466 if (!struncmp(buf, "display_message_numbers=", 24)) {
467 rc_display_message_numbers = atoi(&buf[24]);
469 if (!struncmp(buf, "force_mail_prompts=", 19)) {
470 rc_force_mail_prompts = atoi(&buf[19]);
472 if (!struncmp(buf, "ansi_color=", 11)) {
473 if (!strncasecmp(&buf[11], "on", 2))
475 if (!strncasecmp(&buf[11], "auto", 4))
476 rc_ansi_color = 2; /* autodetect */
477 if (!strncasecmp(&buf[11], "user", 4))
478 rc_ansi_color = 3; /* user config */
480 if (!struncmp(buf, "username=", 9))
481 strcpy(rc_username, &buf[9]);
483 if (!struncmp(buf, "password=", 9))
484 strcpy(rc_password, &buf[9]);
486 if (!struncmp(buf, "cmd=", 4)) {
487 strcpy(buf, &buf[4]);
489 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
491 cptr->c_cmdnum = atoi(buf);
492 for (d = strlen(buf); d >= 0; --d)
495 strcpy(buf, &buf[b + 1]);
497 cptr->c_axlevel = atoi(buf);
498 for (d = strlen(buf); d >= 0; --d)
501 strcpy(buf, &buf[b + 1]);
503 for (a = 0; a < 5; ++a)
504 cptr->c_keys[a][0] = 0;
508 buf[strlen(buf) + 1] = 0;
509 while (strlen(buf) > 0) {
511 for (d = strlen(buf); d >= 0; --d)
514 strncpy(cptr->c_keys[a], buf, b);
515 cptr->c_keys[a][b] = 0;
517 strcpy(buf, &buf[b + 1]);
527 lastcmd->next = cptr;
537 * return the key associated with a command
539 char keycmd(char *cmdstr)
543 for (a = 0; a < strlen(cmdstr); ++a)
544 if (cmdstr[a] == '&')
545 return (tolower(cmdstr[a + 1]));
551 * Output the string from a key command without the ampersand
552 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
554 char *cmd_expand(char *strbuf, int mode)
562 for (a = 0; a < strlen(exp); ++a) {
563 if (strbuf[a] == '&') {
566 strcpy(&exp[a], &exp[a + 1]);
570 strcpy(buf, &exp[a + 2]);
576 if (!strncmp(&exp[a], "^r", 2)) {
578 strcpy(&exp[a], room_name);
579 strcat(exp, &buf[a + 2]);
581 if (!strncmp(&exp[a], "^c", 2)) {
583 strcpy(&exp[a + 1], &exp[a + 2]);
593 * Comparison function to determine if entered commands match a
594 * command loaded from the config file.
596 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
607 for (a = 0; a < ncomp; ++a) {
608 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
609 || (cptr->c_axlevel > cmdax))
617 * This function returns 1 if a given command requires a string input
619 int requires_string(struct citcmd *cptr, int ncomp)
624 strcpy(buf, cptr->c_keys[ncomp - 1]);
625 for (a = 0; a < strlen(buf); ++a) {
634 * Input a command at the main prompt.
635 * This function returns an integer command number. If the command prompts
636 * for a string then it is placed in the supplied buffer.
638 int getcmd(char *argbuf)
649 /* Switch color support on or off if we're in user mode */
650 if (rc_ansi_color == 3) {
651 if (userflags & US_COLOR)
656 /* if we're running in idiot mode, display a cute little menu */
657 IFNEXPERT formout("mainmenu");
659 print_express(); /* print express messages if there are any */
662 for (a = 0; a < 5; ++a)
664 /* now the room prompt... */
666 printf("\n%s%c ", room_name, room_prompt(room_flags));
673 /* Handle the backspace key, but only if there's something
674 * to backspace over...
676 if ((ch == 8) && (cmdpos > 0)) {
677 back(cmdspaces[cmdpos - 1] + 1);
681 /* Spacebar invokes "lazy traversal" commands */
682 if ((ch == 32) && (cmdpos == 0)) {
683 this_lazy_cmd = next_lazy_cmd;
684 if (this_lazy_cmd == 13)
686 if (this_lazy_cmd == 5)
688 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
689 if (cptr->c_cmdnum == this_lazy_cmd) {
690 for (a = 0; a < 5; ++a)
691 if (cptr->c_keys[a][0] != 0)
692 printf("%s ", cmd_expand(
693 cptr->c_keys[a], 0));
695 return (this_lazy_cmd);
699 return (this_lazy_cmd);
701 /* Otherwise, process the command */
702 cmdbuf[cmdpos] = tolower(ch);
704 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
705 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
707 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
708 cmdspaces[cmdpos] = strlen(
709 cmd_expand(cptr->c_keys[cmdpos], 0));
711 if ((cptr->c_keys[cmdpos + 1]) != 0)
717 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
718 if (cmdmatch(cmdbuf, cptr, 5)) {
719 /* We've found our command. */
720 if (requires_string(cptr, cmdpos)) {
726 /* If this command is one that changes rooms,
727 * then the next lazy-command (space bar)
728 * should be "read new" instead of "goto"
730 if ((cptr->c_cmdnum == 5)
731 || (cptr->c_cmdnum == 6)
732 || (cptr->c_cmdnum == 47)
733 || (cptr->c_cmdnum == 52)
734 || (cptr->c_cmdnum == 16)
735 || (cptr->c_cmdnum == 20))
738 return (cptr->c_cmdnum);
744 printf("\rOne of ... \n");
745 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
746 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
747 for (a = 0; a < 5; ++a) {
748 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
754 printf("\n%s%c ", room_name, room_prompt(room_flags));
756 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
757 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
758 for (a = 0; a < cmdpos; ++a) {
760 cmd_expand(cptr->c_keys[a], 0));
775 * set tty modes. commands are:
777 * 0 - set to bbs mode, intr/quit disabled
778 * 1 - set to bbs mode, intr/quit enabled
779 * 2 - save current settings for later restoral
780 * 3 - restore saved settings
782 #ifdef HAVE_TERMIOS_H
783 void sttybbs(int cmd)
784 { /* SysV version of sttybbs() */
786 static struct termios saved_settings;
787 static int last_cmd = 0;
794 if ((cmd == 0) || (cmd == 1)) {
796 live.c_iflag = ISTRIP | IXON | IXANY;
797 live.c_oflag = OPOST | ONLCR;
798 live.c_lflag = ISIG | NOFLSH;
800 if (cmd == SB_YES_INTR) {
801 live.c_cc[VINTR] = NEXT_KEY;
802 live.c_cc[VQUIT] = STOP_KEY;
803 signal(SIGINT, *sighandler);
804 signal(SIGQUIT, *sighandler);
806 signal(SIGINT, SIG_IGN);
807 signal(SIGQUIT, SIG_IGN);
808 live.c_cc[VINTR] = (-1);
809 live.c_cc[VQUIT] = (-1);
812 /* do we even need this stuff anymore? */
814 live.c_cc[VERASE] = 8;
815 live.c_cc[VKILL] = 24;
817 live.c_cc[VEOL] = 255;
818 live.c_cc[VEOL2] = 0;
819 live.c_cc[VSTART] = 0;
820 tcsetattr(0, TCSADRAIN, &live);
823 tcgetattr(0, &saved_settings);
826 tcsetattr(0, TCSADRAIN, &saved_settings);
830 void sttybbs(int cmd)
831 { /* BSD version of sttybbs() */
833 static struct sgttyb saved_settings;
835 if ((cmd == 0) || (cmd == 1)) {
837 live.sg_flags |= CBREAK;
838 live.sg_flags |= CRMOD;
839 live.sg_flags |= NL1;
840 live.sg_flags &= ~ECHO;
842 live.sg_flags |= NOFLSH;
846 gtty(0, &saved_settings);
849 stty(0, &saved_settings);
856 * display_help() - help file viewer
858 void display_help(char *name)
865 * fmout() - Citadel text formatter and paginator
867 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
868 /* screen width to use */
869 /* file to read from, or NULL to read from server */
870 /* nonzero if we should use the paginator */
871 /* screen height to use */
872 /* starting value for lines_printed, -1 for global */
873 /* nonzero if we should use hypertext mode */
881 if (starting_lp >= 0) {
882 lines_printed = starting_lp;
887 c = 1; /* c is the current pos */
892 FMTA:while ((eof_flag == 0) && (strlen(buffer) < 126)) {
895 if (fp != NULL) { /* read from file */
900 buffer[strlen(buffer) + 1] = 0;
901 buffer[strlen(buffer)] = a;
903 } else { /* read from server */
905 serv_gets(&buffer[d]);
906 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
907 buffer[strlen(buffer) - 1] = 0;
908 if (!strcmp(&buffer[d], "000")) {
911 while (isspace(buffer[strlen(buffer) - 1]))
912 buffer[strlen(buffer) - 1] = 0;
920 buffer[strlen(buffer) + 1] = 0;
922 strcpy(buffer, &buffer[1]);
929 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
931 if (((old == 13) || (old == 10)) && (isspace(real))) {
934 lines_printed = checkpagin(lines_printed, pagin, height);
941 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
946 lines_printed = checkpagin(lines_printed, pagin, height);
953 if ((strlen(aaa) + c) > (width - 5)) {
957 lines_printed = checkpagin(lines_printed, pagin, height);
965 if ((a == 13) || (a == 10)) {
969 lines_printed = checkpagin(lines_printed, pagin, height);
975 /* signal caught; drain the server */
978 } while (strcmp(aaa, "000"));
982 lines_printed = checkpagin(lines_printed, pagin, height);
988 * support ANSI color if defined
990 void color(int colornum)
992 static int is_bold = 0;
995 printf("\033[3%dm", (colornum % 8));
996 if ((colornum >= 8) && (is_bold == 0)) {
999 } else if ((colornum < 8) && (is_bold == 1)) {
1007 void cls(int colornum)
1010 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1017 * Detect whether ANSI color is available (answerback)
1019 void send_ansi_detect(void)
1021 if (rc_ansi_color == 2) {
1028 void look_for_ansi(void)
1036 if (rc_ansi_color == 0) {
1038 } else if (rc_ansi_color == 1) {
1040 } else if (rc_ansi_color == 2) {
1042 /* otherwise, do the auto-detect */
1047 if ((now - AnsiDetect) < 2)
1056 select(1, &rfds, NULL, NULL, &tv);
1057 if (FD_ISSET(0, &rfds)) {
1058 abuf[strlen(abuf) + 1] = 0;
1059 read(0, &abuf[strlen(abuf)], 1);
1061 } while (FD_ISSET(0, &rfds));
1063 for (a = 0; a < strlen(abuf); ++a) {
1064 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1065 && (abuf[a + 2] == '?')) {