4 * This file contains functions which implement parts of the
5 * text-mode user interface.
16 #include <sys/types.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
36 #include "citadel_decls.h"
38 #include "routines2.h"
51 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
56 int rc_allow_attachments;
57 int rc_display_message_numbers;
58 int rc_force_mail_prompts;
61 char urls[MAXURLS][256];
65 int next_lazy_cmd = 5;
67 struct citcmd *cmdlist = NULL;
70 /* these variables are local to this module */
71 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
72 int ok_to_interrupt = 0; /* print express msgs asynchronously */
73 time_t AnsiDetect; /* when did we send the detect code? */
74 int enable_color = 0; /* nonzero for ANSI color */
78 * print_express() - print express messages if there are any
80 void print_express(void)
85 if (express_msgs == 0)
93 if (strlen(rc_exp_cmd) > 0) {
94 outpipe = popen(rc_exp_cmd, "w");
95 if (outpipe != NULL) {
96 while (serv_gets(buf), strcmp(buf, "000")) {
97 fprintf(outpipe, "%s\n", buf);
103 /* fall back to built-in express message display */
109 while (serv_gets(buf), strcmp(buf, "000")) {
117 void set_keepalives(int s)
119 keepalives_enabled = (char) s;
123 * This loop handles the "keepalive" messages sent to the server when idling.
125 void do_keepalive(void)
128 static time_t idlet = 0;
132 if ((now - idlet) < ((long) S_KEEPALIVE))
136 /* Do a space-backspace to keep telnet sessions from idling out */
140 if (keepalives_enabled != KA_NO) {
142 if (keepalives_enabled == KA_YES) {
146 if (ok_to_interrupt == 1) {
147 printf("\r%64s\r", "");
149 printf("%s%c ", room_name,
150 room_prompt(room_flags));
160 { /* get a character from the keyboard, with */
161 int a; /* the watchdog timer in effect if necessary */
164 time_t start_time, now;
172 /* This loop waits for keyboard input. If the keepalive
173 * timer expires, it sends a keepalive to the server if
174 * necessary and then waits again.
180 tv.tv_sec = S_KEEPALIVE;
184 if (((now - start_time) > SLEEPING)
185 && (SLEEPING != 0) && (getppid() == 1)) {
186 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...)
206 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
207 && ((a < 32) || (a > 126)))
215 { /* Returns 1 for yes, 0 for no */
231 /* Returns 1 for yes, 0 for no, arg is default value */
254 /* Gets a line from the terminal */
255 /* string == Pointer to string buffer */
256 /* lim == Maximum length - if negative, no-show */
257 void getline(char *string, int lim)
270 if ((a == 8) && (strlen(string) == 0))
272 if ((a != 13) && (a != 8) && (strlen(string) == lim))
274 if ((a == 8) && (string[0] != 0)) {
275 string[strlen(string) - 1] = 0;
281 if ((a == 13) || (a == 10)) {
300 * strprompt() - prompt for a string, print the existing value and
301 * allow the user to press return to keep it...
303 void strprompt(char *prompt, char *str, int len)
308 printf("%s ", prompt);
311 color(BRIGHT_MAGENTA);
325 * boolprompt() - prompt for a yes/no, print the existing value and
326 * allow the user to press return to keep it...
328 int boolprompt(char *prompt, int prev_val)
333 printf("%s ", prompt);
336 color(BRIGHT_MAGENTA);
337 printf("%s", (prev_val ? "Yes" : "No"));
341 r = (yesno_d(prev_val));
347 * intprompt() - like strprompt(), except for an integer
348 * (note that it RETURNS the new value!)
350 int intprompt(char *prompt, int ival, int imin, int imax)
356 snprintf(buf, sizeof buf, "%d", i);
357 strprompt(prompt, buf, 15);
360 printf("*** Must be no less than %d.\n", imin);
362 printf("*** Must be no more than %d.\n", imax);
363 } while ((i < imin) || (i > imax));
368 * newprompt() - prompt for a string with no existing value
369 * (clears out string buffer first)
371 void newprompt(char *prompt, char *str, int len)
373 color(BRIGHT_MAGENTA);
374 printf("%s", prompt);
382 { /* returns a lower case value */
391 * parse the citadel.rc file
393 void load_command_set(void)
398 struct citcmd *lastcmd = NULL;
403 /* first, set up some defaults for non-required variables */
405 strcpy(editor_path, "");
406 strcpy(printcmd, "");
407 strcpy(rc_username, "");
408 strcpy(rc_password, "");
411 rc_allow_attachments = 0;
412 strcpy(rc_exp_cmd, "");
413 rc_display_message_numbers = 0;
414 rc_force_mail_prompts = 0;
416 strcpy(rc_url_cmd, "");
418 /* now try to open the citadel.rc file */
421 if (getenv("HOME") != NULL) {
422 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
423 ccfile = fopen(buf, "r");
425 if (ccfile == NULL) {
426 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
428 if (ccfile == NULL) {
429 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
430 ccfile = fopen(buf, "r");
432 if (ccfile == NULL) {
433 ccfile = fopen("./citadel.rc", "r");
435 if (ccfile == NULL) {
436 perror("commands: cannot open citadel.rc");
439 while (fgets(buf, 256, ccfile) != NULL) {
440 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
441 buf[strlen(buf) - 1] = 0;
443 if (!struncmp(buf, "editor=", 7))
444 strcpy(editor_path, &buf[7]);
446 if (!struncmp(buf, "printcmd=", 9))
447 strcpy(printcmd, &buf[9]);
449 if (!struncmp(buf, "expcmd=", 7))
450 strcpy(rc_exp_cmd, &buf[7]);
452 if (!struncmp(buf, "local_screen_dimensions=", 24))
453 have_xterm = (char) atoi(&buf[24]);
455 if (!struncmp(buf, "use_floors=", 11)) {
456 if (!strucmp(&buf[11], "yes"))
457 rc_floor_mode = RC_YES;
458 if (!strucmp(&buf[11], "no"))
459 rc_floor_mode = RC_NO;
460 if (!strucmp(&buf[11], "default"))
461 rc_floor_mode = RC_DEFAULT;
463 if (!struncmp(buf, "beep=", 5)) {
464 rc_exp_beep = atoi(&buf[5]);
466 if (!struncmp(buf, "allow_attachments=", 18)) {
467 rc_allow_attachments = atoi(&buf[18]);
469 if (!struncmp(buf, "display_message_numbers=", 24)) {
470 rc_display_message_numbers = atoi(&buf[24]);
472 if (!struncmp(buf, "force_mail_prompts=", 19)) {
473 rc_force_mail_prompts = atoi(&buf[19]);
475 if (!struncmp(buf, "ansi_color=", 11)) {
476 if (!struncmp(&buf[11], "on", 2))
478 if (!struncmp(&buf[11], "auto", 4))
479 rc_ansi_color = 2; /* autodetect */
480 if (!struncmp(&buf[11], "user", 4))
481 rc_ansi_color = 3; /* user config */
483 if (!struncmp(buf, "username=", 9))
484 strcpy(rc_username, &buf[9]);
486 if (!struncmp(buf, "password=", 9))
487 strcpy(rc_password, &buf[9]);
489 if (!struncmp(buf, "urlcmd=", 7))
490 strcpy(rc_url_cmd, &buf[7]);
492 if (!struncmp(buf, "cmd=", 4)) {
493 strcpy(buf, &buf[4]);
495 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
497 cptr->c_cmdnum = atoi(buf);
498 for (d = strlen(buf); d >= 0; --d)
501 strcpy(buf, &buf[b + 1]);
503 cptr->c_axlevel = atoi(buf);
504 for (d = strlen(buf); d >= 0; --d)
507 strcpy(buf, &buf[b + 1]);
509 for (a = 0; a < 5; ++a)
510 cptr->c_keys[a][0] = 0;
514 buf[strlen(buf) + 1] = 0;
515 while (strlen(buf) > 0) {
517 for (d = strlen(buf); d >= 0; --d)
520 strncpy(cptr->c_keys[a], buf, b);
521 cptr->c_keys[a][b] = 0;
523 strcpy(buf, &buf[b + 1]);
533 lastcmd->next = cptr;
543 * return the key associated with a command
545 char keycmd(char *cmdstr)
549 for (a = 0; a < strlen(cmdstr); ++a)
550 if (cmdstr[a] == '&')
551 return (tolower(cmdstr[a + 1]));
557 * Output the string from a key command without the ampersand
558 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
560 char *cmd_expand(char *strbuf, int mode)
568 for (a = 0; a < strlen(exp); ++a) {
569 if (strbuf[a] == '&') {
572 strcpy(&exp[a], &exp[a + 1]);
576 strcpy(buf, &exp[a + 2]);
582 if (!strncmp(&exp[a], "^r", 2)) {
584 strcpy(&exp[a], room_name);
585 strcat(exp, &buf[a + 2]);
587 if (!strncmp(&exp[a], "^c", 2)) {
589 strcpy(&exp[a + 1], &exp[a + 2]);
599 * Comparison function to determine if entered commands match a
600 * command loaded from the config file.
602 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
613 for (a = 0; a < ncomp; ++a) {
614 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
615 || (cptr->c_axlevel > cmdax))
623 * This function returns 1 if a given command requires a string input
625 int requires_string(struct citcmd *cptr, int ncomp)
630 strcpy(buf, cptr->c_keys[ncomp - 1]);
631 for (a = 0; a < strlen(buf); ++a) {
640 * Input a command at the main prompt.
641 * This function returns an integer command number. If the command prompts
642 * for a string then it is placed in the supplied buffer.
644 int getcmd(char *argbuf)
655 /* Switch color support on or off if we're in user mode */
656 if (rc_ansi_color == 3) {
657 if (userflags & US_COLOR)
662 /* if we're running in idiot mode, display a cute little menu */
663 IFNEXPERT formout("mainmenu");
665 print_express(); /* print express messages if there are any */
668 for (a = 0; a < 5; ++a)
670 /* now the room prompt... */
673 printf("\n%s", room_name);
675 printf("%c ", room_prompt(room_flags));
682 /* Handle the backspace key, but only if there's something
683 * to backspace over...
685 if ((ch == 8) && (cmdpos > 0)) {
686 back(cmdspaces[cmdpos - 1] + 1);
690 /* Spacebar invokes "lazy traversal" commands */
691 if ((ch == 32) && (cmdpos == 0)) {
692 this_lazy_cmd = next_lazy_cmd;
693 if (this_lazy_cmd == 13)
695 if (this_lazy_cmd == 5)
697 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
698 if (cptr->c_cmdnum == this_lazy_cmd) {
699 for (a = 0; a < 5; ++a)
700 if (cptr->c_keys[a][0] != 0)
701 printf("%s ", cmd_expand(
702 cptr->c_keys[a], 0));
704 return (this_lazy_cmd);
708 return (this_lazy_cmd);
710 /* Otherwise, process the command */
711 cmdbuf[cmdpos] = tolower(ch);
713 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
714 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
716 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
717 cmdspaces[cmdpos] = strlen(
718 cmd_expand(cptr->c_keys[cmdpos], 0));
720 if ((cptr->c_keys[cmdpos + 1]) != 0)
726 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
727 if (cmdmatch(cmdbuf, cptr, 5)) {
728 /* We've found our command. */
729 if (requires_string(cptr, cmdpos)) {
735 /* If this command is one that changes rooms,
736 * then the next lazy-command (space bar)
737 * should be "read new" instead of "goto"
739 if ((cptr->c_cmdnum == 5)
740 || (cptr->c_cmdnum == 6)
741 || (cptr->c_cmdnum == 47)
742 || (cptr->c_cmdnum == 52)
743 || (cptr->c_cmdnum == 16)
744 || (cptr->c_cmdnum == 20))
747 return (cptr->c_cmdnum);
753 printf("\rOne of ... \n");
754 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
755 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
756 for (a = 0; a < 5; ++a) {
757 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
763 printf("\n%s%c ", room_name, room_prompt(room_flags));
765 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
766 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
767 for (a = 0; a < cmdpos; ++a) {
769 cmd_expand(cptr->c_keys[a], 0));
784 * set tty modes. commands are:
786 * 0 - set to bbs mode, intr/quit disabled
787 * 1 - set to bbs mode, intr/quit enabled
788 * 2 - save current settings for later restoral
789 * 3 - restore saved settings
791 #ifdef HAVE_TERMIOS_H
792 void sttybbs(int cmd)
793 { /* SysV version of sttybbs() */
795 static struct termios saved_settings;
796 static int last_cmd = 0;
803 if ((cmd == 0) || (cmd == 1)) {
805 live.c_iflag = ISTRIP | IXON | IXANY;
806 live.c_oflag = OPOST | ONLCR;
807 live.c_lflag = ISIG | NOFLSH;
809 if (cmd == SB_YES_INTR) {
810 live.c_cc[VINTR] = NEXT_KEY;
811 live.c_cc[VQUIT] = STOP_KEY;
812 signal(SIGINT, *sighandler);
813 signal(SIGQUIT, *sighandler);
815 signal(SIGINT, SIG_IGN);
816 signal(SIGQUIT, SIG_IGN);
817 live.c_cc[VINTR] = (-1);
818 live.c_cc[VQUIT] = (-1);
821 /* do we even need this stuff anymore? */
823 live.c_cc[VERASE] = 8;
824 live.c_cc[VKILL] = 24;
826 live.c_cc[VEOL] = 255;
827 live.c_cc[VEOL2] = 0;
828 live.c_cc[VSTART] = 0;
829 tcsetattr(0, TCSADRAIN, &live);
832 tcgetattr(0, &saved_settings);
835 tcsetattr(0, TCSADRAIN, &saved_settings);
839 void sttybbs(int cmd)
840 { /* BSD version of sttybbs() */
842 static struct sgttyb saved_settings;
844 if ((cmd == 0) || (cmd == 1)) {
846 live.sg_flags |= CBREAK;
847 live.sg_flags |= CRMOD;
848 live.sg_flags |= NL1;
849 live.sg_flags &= ~ECHO;
851 live.sg_flags |= NOFLSH;
855 gtty(0, &saved_settings);
858 stty(0, &saved_settings);
865 * display_help() - help file viewer
867 void display_help(char *name)
874 * fmout() - Citadel text formatter and paginator
876 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
877 /* screen width to use */
878 /* file to read from, or NULL to read from server */
879 /* nonzero if we should use the paginator */
880 /* screen height to use */
881 /* starting value for lines_printed, -1 for global */
882 /* nonzero if we should use hypertext mode */
890 num_urls = 0; /* Start with a clean slate of embedded URL's */
892 if (starting_lp >= 0) {
893 lines_printed = starting_lp;
898 c = 1; /* c is the current pos */
903 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
907 if (fp != NULL) { /* read from file */
912 buffer[strlen(buffer) + 1] = 0;
913 buffer[strlen(buffer)] = a;
915 } else { /* read from server */
917 serv_gets(&buffer[d]);
918 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
919 buffer[strlen(buffer) - 1] = 0;
920 if (!strcmp(&buffer[d], "000")) {
923 while (isspace(buffer[strlen(buffer) - 1]))
924 buffer[strlen(buffer) - 1] = 0;
932 if ( (!struncmp(buffer, "http://", 7))
933 || (!struncmp(buffer, "ftp://", 6)) ) {
934 safestrncpy(urls[num_urls], buffer, 255);
935 for (a=0; a<strlen(urls[num_urls]); ++a) {
936 b = urls[num_urls][a];
937 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
938 || (b==13) || (b==9) || (b=='\"') )
939 urls[num_urls][a] = 0;
944 buffer[strlen(buffer) + 1] = 0;
946 strcpy(buffer, &buffer[1]);
953 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
955 if (((old == 13) || (old == 10)) && (isspace(real))) {
958 lines_printed = checkpagin(lines_printed, pagin, height);
965 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
970 lines_printed = checkpagin(lines_printed, pagin, height);
977 if ((strlen(aaa) + c) > (width - 5)) {
981 lines_printed = checkpagin(lines_printed, pagin, height);
989 if ((a == 13) || (a == 10)) {
993 lines_printed = checkpagin(lines_printed, pagin, height);
999 /* signal caught; drain the server */
1002 } while (strcmp(aaa, "000"));
1004 FMTEND:printf("\n");
1006 lines_printed = checkpagin(lines_printed, pagin, height);
1012 * support ANSI color if defined
1014 void color(int colornum)
1016 static int is_bold = 0;
1017 static int hold_color, current_color;
1019 if (colornum == COLOR_PUSH) {
1020 hold_color = current_color;
1024 if (colornum == COLOR_POP) {
1029 current_color = colornum;
1031 printf("\033[3%dm", (colornum % 8));
1032 if ((colornum >= 8) && (is_bold == 0)) {
1035 } else if ((colornum < 8) && (is_bold == 1)) {
1043 void cls(int colornum)
1046 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1053 * Detect whether ANSI color is available (answerback)
1055 void send_ansi_detect(void)
1057 if (rc_ansi_color == 2) {
1064 void look_for_ansi(void)
1072 if (rc_ansi_color == 0) {
1074 } else if (rc_ansi_color == 1) {
1076 } else if (rc_ansi_color == 2) {
1078 /* otherwise, do the auto-detect */
1083 if ((now - AnsiDetect) < 2)
1092 select(1, &rfds, NULL, NULL, &tv);
1093 if (FD_ISSET(0, &rfds)) {
1094 abuf[strlen(abuf) + 1] = 0;
1095 read(0, &abuf[strlen(abuf)], 1);
1097 } while (FD_ISSET(0, &rfds));
1099 for (a = 0; a < strlen(abuf); ++a) {
1100 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1101 && (abuf[a + 2] == '?')) {
1110 * Display key options (highlight hotkeys inside angle brackets)
1112 void keyopt(char *buf) {
1116 for (i=0; i<strlen(buf); ++i) {
1118 putc(buf[i], stdout);
1119 color(BRIGHT_MAGENTA);
1124 putc(buf[i], stdout);
1133 * Present a key-menu line choice type of thing
1135 char keymenu(char *menuprompt, char *menustring) {
1141 int display_prompt = 1;
1143 choices = num_tokens(menustring, '|');
1145 if (menuprompt != NULL) do_prompt = 1;
1146 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1149 if (display_prompt) {
1151 printf("%s ", menuprompt);
1154 for (i=0; i<choices; ++i) {
1155 extract(buf, menustring, i);
1165 if ( (do_prompt) && (ch=='?') ) {
1166 printf("\rOne of... ");
1168 for (i=0; i<choices; ++i) {
1169 extract(buf, menustring, i);
1178 for (i=0; i<choices; ++i) {
1179 extract(buf, menustring, i);
1180 for (c=1; c<strlen(buf); ++c) {
1181 if ( (ch == tolower(buf[c]))
1183 && (buf[c+1]=='>') ) {
1184 for (a=0; a<strlen(buf); ++a) {
1185 if ( (a!=(c-1)) && (a!=(c+1))) {
1186 putc(buf[a], stdout);