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"
54 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
59 int rc_allow_attachments;
60 int rc_display_message_numbers;
61 int rc_force_mail_prompts;
64 char urls[MAXURLS][256];
68 int next_lazy_cmd = 5;
70 struct citcmd *cmdlist = NULL;
73 /* these variables are local to this module */
74 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
75 int ok_to_interrupt = 0; /* print express msgs asynchronously */
76 time_t AnsiDetect; /* when did we send the detect code? */
77 int enable_color = 0; /* nonzero for ANSI color */
81 * print_express() - print express messages if there are any
83 void print_express(void)
88 if (express_msgs == 0)
96 if (strlen(rc_exp_cmd) > 0) {
97 outpipe = popen(rc_exp_cmd, "w");
98 if (outpipe != NULL) {
99 while (serv_gets(buf), strcmp(buf, "000")) {
100 fprintf(outpipe, "%s\n", buf);
106 /* fall back to built-in express message display */
112 while (serv_gets(buf), strcmp(buf, "000")) {
120 void set_keepalives(int s)
122 keepalives_enabled = (char) s;
126 * This loop handles the "keepalive" messages sent to the server when idling.
128 void do_keepalive(void)
131 static time_t idlet = 0;
135 if ((now - idlet) < ((long) S_KEEPALIVE))
139 /* Do a space-backspace to keep socksified telnet sessions open */
143 if (keepalives_enabled != KA_NO) {
145 if (keepalives_enabled == KA_YES) {
149 if (ok_to_interrupt == 1) {
150 printf("\r%64s\r", "");
152 printf("%s%c ", room_name,
153 room_prompt(room_flags));
163 { /* get a character from the keyboard, with */
164 int a; /* the watchdog timer in effect if necessary */
167 time_t start_time, now;
175 /* This loop waits for keyboard input. If the keepalive
176 * timer expires, it sends a keepalive to the server if
177 * necessary and then waits again.
183 tv.tv_sec = S_KEEPALIVE;
187 if (((now - start_time) > SLEEPING)
188 && (SLEEPING != 0) && (getppid() == 1)) {
189 printf("Sleeping? Call again.\n");
192 select(1, &rfds, NULL, NULL, &tv);
193 } while (!FD_ISSET(0, &rfds));
198 /* At this point, there's input, so fetch it.
199 * (There's a hole in the bucket...)
209 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
210 && ((a < 32) || (a > 126)))
218 { /* Returns 1 for yes, 0 for no */
234 /* Returns 1 for yes, 0 for no, arg is default value */
257 /* Gets a line from the terminal */
258 /* string == Pointer to string buffer */
259 /* lim == Maximum length - if negative, no-show */
260 void getline(char *string, int lim)
273 if ((a == 8) && (strlen(string) == 0))
275 if ((a != 13) && (a != 8) && (strlen(string) == lim))
277 if ((a == 8) && (string[0] != 0)) {
278 string[strlen(string) - 1] = 0;
284 if ((a == 13) || (a == 10)) {
303 * strprompt() - prompt for a string, print the existing value and
304 * allow the user to press return to keep it...
306 void strprompt(char *prompt, char *str, int len)
311 printf("%s ", prompt);
314 color(BRIGHT_MAGENTA);
328 * boolprompt() - prompt for a yes/no, print the existing value and
329 * allow the user to press return to keep it...
331 int boolprompt(char *prompt, int prev_val)
336 printf("%s ", prompt);
339 color(BRIGHT_MAGENTA);
340 printf("%s", (prev_val ? "Yes" : "No"));
344 r = (yesno_d(prev_val));
350 * intprompt() - like strprompt(), except for an integer
351 * (note that it RETURNS the new value!)
353 int intprompt(char *prompt, int ival, int imin, int imax)
359 snprintf(buf, sizeof buf, "%d", i);
360 strprompt(prompt, buf, 15);
363 printf("*** Must be no less than %d.\n", imin);
365 printf("*** Must be no more than %d.\n", imax);
366 } while ((i < imin) || (i > imax));
371 * newprompt() - prompt for a string with no existing value
372 * (clears out string buffer first)
374 void newprompt(char *prompt, char *str, int len)
376 color(BRIGHT_MAGENTA);
377 printf("%s", prompt);
385 { /* returns a lower case value */
394 * parse the citadel.rc file
396 void load_command_set(void)
401 struct citcmd *lastcmd = NULL;
406 /* first, set up some defaults for non-required variables */
408 strcpy(editor_path, "");
409 strcpy(printcmd, "");
410 strcpy(rc_username, "");
411 strcpy(rc_password, "");
414 rc_allow_attachments = 0;
415 strcpy(rc_exp_cmd, "");
416 rc_display_message_numbers = 0;
417 rc_force_mail_prompts = 0;
419 strcpy(rc_url_cmd, "");
421 /* now try to open the citadel.rc file */
424 if (getenv("HOME") != NULL) {
425 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
426 ccfile = fopen(buf, "r");
428 if (ccfile == NULL) {
429 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
431 if (ccfile == NULL) {
432 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
433 ccfile = fopen(buf, "r");
435 if (ccfile == NULL) {
436 ccfile = fopen("./citadel.rc", "r");
438 if (ccfile == NULL) {
439 perror("commands: cannot open citadel.rc");
442 while (fgets(buf, 256, ccfile) != NULL) {
443 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
444 buf[strlen(buf) - 1] = 0;
446 if (!struncmp(buf, "editor=", 7))
447 strcpy(editor_path, &buf[7]);
449 if (!struncmp(buf, "printcmd=", 9))
450 strcpy(printcmd, &buf[9]);
452 if (!struncmp(buf, "expcmd=", 7))
453 strcpy(rc_exp_cmd, &buf[7]);
455 if (!struncmp(buf, "local_screen_dimensions=", 24))
456 have_xterm = (char) atoi(&buf[24]);
458 if (!struncmp(buf, "use_floors=", 11)) {
459 if (!strucmp(&buf[11], "yes"))
460 rc_floor_mode = RC_YES;
461 if (!strucmp(&buf[11], "no"))
462 rc_floor_mode = RC_NO;
463 if (!strucmp(&buf[11], "default"))
464 rc_floor_mode = RC_DEFAULT;
466 if (!struncmp(buf, "beep=", 5)) {
467 rc_exp_beep = atoi(&buf[5]);
469 if (!struncmp(buf, "allow_attachments=", 18)) {
470 rc_allow_attachments = atoi(&buf[18]);
472 if (!struncmp(buf, "display_message_numbers=", 24)) {
473 rc_display_message_numbers = atoi(&buf[24]);
475 if (!struncmp(buf, "force_mail_prompts=", 19)) {
476 rc_force_mail_prompts = atoi(&buf[19]);
478 if (!struncmp(buf, "ansi_color=", 11)) {
479 if (!struncmp(&buf[11], "on", 2))
481 if (!struncmp(&buf[11], "auto", 4))
482 rc_ansi_color = 2; /* autodetect */
483 if (!struncmp(&buf[11], "user", 4))
484 rc_ansi_color = 3; /* user config */
486 if (!struncmp(buf, "username=", 9))
487 strcpy(rc_username, &buf[9]);
489 if (!struncmp(buf, "password=", 9))
490 strcpy(rc_password, &buf[9]);
492 if (!struncmp(buf, "urlcmd=", 7))
493 strcpy(rc_url_cmd, &buf[7]);
495 if (!struncmp(buf, "cmd=", 4)) {
496 strcpy(buf, &buf[4]);
498 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
500 cptr->c_cmdnum = atoi(buf);
501 for (d = strlen(buf); d >= 0; --d)
504 strcpy(buf, &buf[b + 1]);
506 cptr->c_axlevel = atoi(buf);
507 for (d = strlen(buf); d >= 0; --d)
510 strcpy(buf, &buf[b + 1]);
512 for (a = 0; a < 5; ++a)
513 cptr->c_keys[a][0] = 0;
517 buf[strlen(buf) + 1] = 0;
518 while (strlen(buf) > 0) {
520 for (d = strlen(buf); d >= 0; --d)
523 strncpy(cptr->c_keys[a], buf, b);
524 cptr->c_keys[a][b] = 0;
526 strcpy(buf, &buf[b + 1]);
536 lastcmd->next = cptr;
546 * return the key associated with a command
548 char keycmd(char *cmdstr)
552 for (a = 0; a < strlen(cmdstr); ++a)
553 if (cmdstr[a] == '&')
554 return (tolower(cmdstr[a + 1]));
560 * Output the string from a key command without the ampersand
561 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
563 char *cmd_expand(char *strbuf, int mode)
571 for (a = 0; a < strlen(exp); ++a) {
572 if (strbuf[a] == '&') {
575 strcpy(&exp[a], &exp[a + 1]);
579 strcpy(buf, &exp[a + 2]);
585 if (!strncmp(&exp[a], "^r", 2)) {
587 strcpy(&exp[a], room_name);
588 strcat(exp, &buf[a + 2]);
590 if (!strncmp(&exp[a], "^c", 2)) {
592 strcpy(&exp[a + 1], &exp[a + 2]);
602 * Comparison function to determine if entered commands match a
603 * command loaded from the config file.
605 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
616 for (a = 0; a < ncomp; ++a) {
617 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
618 || (cptr->c_axlevel > cmdax))
626 * This function returns 1 if a given command requires a string input
628 int requires_string(struct citcmd *cptr, int ncomp)
633 strcpy(buf, cptr->c_keys[ncomp - 1]);
634 for (a = 0; a < strlen(buf); ++a) {
643 * Input a command at the main prompt.
644 * This function returns an integer command number. If the command prompts
645 * for a string then it is placed in the supplied buffer.
647 int getcmd(char *argbuf)
658 /* Switch color support on or off if we're in user mode */
659 if (rc_ansi_color == 3) {
660 if (userflags & US_COLOR)
665 /* if we're running in idiot mode, display a cute little menu */
666 IFNEXPERT formout("mainmenu");
668 print_express(); /* print express messages if there are any */
671 for (a = 0; a < 5; ++a)
673 /* now the room prompt... */
676 printf("\n%s", room_name);
678 printf("%c ", room_prompt(room_flags));
685 /* Handle the backspace key, but only if there's something
686 * to backspace over...
688 if ((ch == 8) && (cmdpos > 0)) {
689 back(cmdspaces[cmdpos - 1] + 1);
693 /* Spacebar invokes "lazy traversal" commands */
694 if ((ch == 32) && (cmdpos == 0)) {
695 this_lazy_cmd = next_lazy_cmd;
696 if (this_lazy_cmd == 13)
698 if (this_lazy_cmd == 5)
700 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
701 if (cptr->c_cmdnum == this_lazy_cmd) {
702 for (a = 0; a < 5; ++a)
703 if (cptr->c_keys[a][0] != 0)
704 printf("%s ", cmd_expand(
705 cptr->c_keys[a], 0));
707 return (this_lazy_cmd);
711 return (this_lazy_cmd);
713 /* Otherwise, process the command */
714 cmdbuf[cmdpos] = tolower(ch);
716 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
717 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
719 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
720 cmdspaces[cmdpos] = strlen(
721 cmd_expand(cptr->c_keys[cmdpos], 0));
723 if ((cptr->c_keys[cmdpos + 1]) != 0)
729 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
730 if (cmdmatch(cmdbuf, cptr, 5)) {
731 /* We've found our command. */
732 if (requires_string(cptr, cmdpos)) {
738 /* If this command is one that changes rooms,
739 * then the next lazy-command (space bar)
740 * should be "read new" instead of "goto"
742 if ((cptr->c_cmdnum == 5)
743 || (cptr->c_cmdnum == 6)
744 || (cptr->c_cmdnum == 47)
745 || (cptr->c_cmdnum == 52)
746 || (cptr->c_cmdnum == 16)
747 || (cptr->c_cmdnum == 20))
750 return (cptr->c_cmdnum);
756 printf("\rOne of ... \n");
757 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
758 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
759 for (a = 0; a < 5; ++a) {
760 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
766 printf("\n%s%c ", room_name, room_prompt(room_flags));
768 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
769 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
770 for (a = 0; a < cmdpos; ++a) {
772 cmd_expand(cptr->c_keys[a], 0));
787 * set tty modes. commands are:
789 * 0 - set to bbs mode, intr/quit disabled
790 * 1 - set to bbs mode, intr/quit enabled
791 * 2 - save current settings for later restoral
792 * 3 - restore saved settings
794 #ifdef HAVE_TERMIOS_H
795 void sttybbs(int cmd)
796 { /* SysV version of sttybbs() */
798 static struct termios saved_settings;
799 static int last_cmd = 0;
806 if ((cmd == 0) || (cmd == 1)) {
808 live.c_iflag = ISTRIP | IXON | IXANY;
809 live.c_oflag = OPOST | ONLCR;
810 live.c_lflag = ISIG | NOFLSH;
812 if (cmd == SB_YES_INTR) {
813 live.c_cc[VINTR] = NEXT_KEY;
814 live.c_cc[VQUIT] = STOP_KEY;
815 signal(SIGINT, *sighandler);
816 signal(SIGQUIT, *sighandler);
818 signal(SIGINT, SIG_IGN);
819 signal(SIGQUIT, SIG_IGN);
820 live.c_cc[VINTR] = (-1);
821 live.c_cc[VQUIT] = (-1);
824 /* do we even need this stuff anymore? */
826 live.c_cc[VERASE] = 8;
827 live.c_cc[VKILL] = 24;
829 live.c_cc[VEOL] = 255;
830 live.c_cc[VEOL2] = 0;
831 live.c_cc[VSTART] = 0;
832 tcsetattr(0, TCSADRAIN, &live);
835 tcgetattr(0, &saved_settings);
838 tcsetattr(0, TCSADRAIN, &saved_settings);
842 void sttybbs(int cmd)
843 { /* BSD version of sttybbs() */
845 static struct sgttyb saved_settings;
847 if ((cmd == 0) || (cmd == 1)) {
849 live.sg_flags |= CBREAK;
850 live.sg_flags |= CRMOD;
851 live.sg_flags |= NL1;
852 live.sg_flags &= ~ECHO;
854 live.sg_flags |= NOFLSH;
858 gtty(0, &saved_settings);
861 stty(0, &saved_settings);
868 * display_help() - help file viewer
870 void display_help(char *name)
877 * fmout() - Citadel text formatter and paginator
879 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
880 /* screen width to use */
881 /* file to read from, or NULL to read from server */
882 /* nonzero if we should use the paginator */
883 /* screen height to use */
884 /* starting value for lines_printed, -1 for global */
885 /* nonzero if we should use hypertext mode */
893 num_urls = 0; /* Start with a clean slate of embedded URL's */
895 if (starting_lp >= 0) {
896 lines_printed = starting_lp;
901 c = 1; /* c is the current pos */
906 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
910 if (fp != NULL) { /* read from file */
915 buffer[strlen(buffer) + 1] = 0;
916 buffer[strlen(buffer)] = a;
918 } else { /* read from server */
920 serv_gets(&buffer[d]);
921 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
922 buffer[strlen(buffer) - 1] = 0;
923 if (!strcmp(&buffer[d], "000")) {
926 while (isspace(buffer[strlen(buffer) - 1]))
927 buffer[strlen(buffer) - 1] = 0;
935 if ( (!struncmp(buffer, "http://", 7))
936 || (!struncmp(buffer, "ftp://", 6)) ) {
937 safestrncpy(urls[num_urls], buffer, 255);
938 for (a=0; a<strlen(urls[num_urls]); ++a) {
939 b = urls[num_urls][a];
940 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
941 || (b==13) || (b==9) )
942 urls[num_urls][a] = 0;
947 buffer[strlen(buffer) + 1] = 0;
949 strcpy(buffer, &buffer[1]);
956 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
958 if (((old == 13) || (old == 10)) && (isspace(real))) {
961 lines_printed = checkpagin(lines_printed, pagin, height);
968 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
973 lines_printed = checkpagin(lines_printed, pagin, height);
980 if ((strlen(aaa) + c) > (width - 5)) {
984 lines_printed = checkpagin(lines_printed, pagin, height);
992 if ((a == 13) || (a == 10)) {
996 lines_printed = checkpagin(lines_printed, pagin, height);
1002 /* signal caught; drain the server */
1005 } while (strcmp(aaa, "000"));
1007 FMTEND:printf("\n");
1009 lines_printed = checkpagin(lines_printed, pagin, height);
1015 * support ANSI color if defined
1017 void color(int colornum)
1019 static int is_bold = 0;
1020 static int hold_color, current_color;
1022 if (colornum == COLOR_PUSH) {
1023 hold_color = current_color;
1027 if (colornum == COLOR_POP) {
1032 current_color = colornum;
1034 printf("\033[3%dm", (colornum % 8));
1035 if ((colornum >= 8) && (is_bold == 0)) {
1038 } else if ((colornum < 8) && (is_bold == 1)) {
1046 void cls(int colornum)
1049 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1056 * Detect whether ANSI color is available (answerback)
1058 void send_ansi_detect(void)
1060 if (rc_ansi_color == 2) {
1067 void look_for_ansi(void)
1075 if (rc_ansi_color == 0) {
1077 } else if (rc_ansi_color == 1) {
1079 } else if (rc_ansi_color == 2) {
1081 /* otherwise, do the auto-detect */
1086 if ((now - AnsiDetect) < 2)
1095 select(1, &rfds, NULL, NULL, &tv);
1096 if (FD_ISSET(0, &rfds)) {
1097 abuf[strlen(abuf) + 1] = 0;
1098 read(0, &abuf[strlen(abuf)], 1);
1100 } while (FD_ISSET(0, &rfds));
1102 for (a = 0; a < strlen(abuf); ++a) {
1103 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1104 && (abuf[a + 2] == '?')) {
1113 * Display key options (highlight hotkeys inside angle brackets)
1115 void keyopt(char *buf) {
1119 for (i=0; i<strlen(buf); ++i) {
1121 putc(buf[i], stdout);
1122 color(BRIGHT_MAGENTA);
1127 putc(buf[i], stdout);