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)
358 snprintf(buf, sizeof buf, "%d", i);
359 strprompt(prompt, buf, 15);
361 for (p=0; p<strlen(buf); ++p) {
362 if (!isdigit(buf[p])) i = imin - 1;
365 printf("*** Must be no less than %d.\n", imin);
367 printf("*** Must be no more than %d.\n", imax);
368 } while ((i < imin) || (i > imax));
373 * newprompt() - prompt for a string with no existing value
374 * (clears out string buffer first)
376 void newprompt(char *prompt, char *str, int len)
378 color(BRIGHT_MAGENTA);
379 printf("%s", prompt);
387 { /* returns a lower case value */
396 * parse the citadel.rc file
398 void load_command_set(void)
403 struct citcmd *lastcmd = NULL;
408 /* first, set up some defaults for non-required variables */
410 strcpy(editor_path, "");
411 strcpy(printcmd, "");
412 strcpy(rc_username, "");
413 strcpy(rc_password, "");
416 rc_allow_attachments = 0;
417 strcpy(rc_exp_cmd, "");
418 rc_display_message_numbers = 0;
419 rc_force_mail_prompts = 0;
421 strcpy(rc_url_cmd, "");
423 /* now try to open the citadel.rc file */
426 if (getenv("HOME") != NULL) {
427 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
428 ccfile = fopen(buf, "r");
430 if (ccfile == NULL) {
431 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
433 if (ccfile == NULL) {
434 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
435 ccfile = fopen(buf, "r");
437 if (ccfile == NULL) {
438 ccfile = fopen("./citadel.rc", "r");
440 if (ccfile == NULL) {
441 perror("commands: cannot open citadel.rc");
444 while (fgets(buf, 256, ccfile) != NULL) {
445 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
446 buf[strlen(buf) - 1] = 0;
448 if (!struncmp(buf, "editor=", 7))
449 strcpy(editor_path, &buf[7]);
451 if (!struncmp(buf, "printcmd=", 9))
452 strcpy(printcmd, &buf[9]);
454 if (!struncmp(buf, "expcmd=", 7))
455 strcpy(rc_exp_cmd, &buf[7]);
457 if (!struncmp(buf, "local_screen_dimensions=", 24))
458 have_xterm = (char) atoi(&buf[24]);
460 if (!struncmp(buf, "use_floors=", 11)) {
461 if (!strucmp(&buf[11], "yes"))
462 rc_floor_mode = RC_YES;
463 if (!strucmp(&buf[11], "no"))
464 rc_floor_mode = RC_NO;
465 if (!strucmp(&buf[11], "default"))
466 rc_floor_mode = RC_DEFAULT;
468 if (!struncmp(buf, "beep=", 5)) {
469 rc_exp_beep = atoi(&buf[5]);
471 if (!struncmp(buf, "allow_attachments=", 18)) {
472 rc_allow_attachments = atoi(&buf[18]);
474 if (!struncmp(buf, "display_message_numbers=", 24)) {
475 rc_display_message_numbers = atoi(&buf[24]);
477 if (!struncmp(buf, "force_mail_prompts=", 19)) {
478 rc_force_mail_prompts = atoi(&buf[19]);
480 if (!struncmp(buf, "ansi_color=", 11)) {
481 if (!struncmp(&buf[11], "on", 2))
483 if (!struncmp(&buf[11], "auto", 4))
484 rc_ansi_color = 2; /* autodetect */
485 if (!struncmp(&buf[11], "user", 4))
486 rc_ansi_color = 3; /* user config */
488 if (!struncmp(buf, "username=", 9))
489 strcpy(rc_username, &buf[9]);
491 if (!struncmp(buf, "password=", 9))
492 strcpy(rc_password, &buf[9]);
494 if (!struncmp(buf, "urlcmd=", 7))
495 strcpy(rc_url_cmd, &buf[7]);
497 if (!struncmp(buf, "cmd=", 4)) {
498 strcpy(buf, &buf[4]);
500 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
502 cptr->c_cmdnum = atoi(buf);
503 for (d = strlen(buf); d >= 0; --d)
506 strcpy(buf, &buf[b + 1]);
508 cptr->c_axlevel = atoi(buf);
509 for (d = strlen(buf); d >= 0; --d)
512 strcpy(buf, &buf[b + 1]);
514 for (a = 0; a < 5; ++a)
515 cptr->c_keys[a][0] = 0;
519 buf[strlen(buf) + 1] = 0;
520 while (strlen(buf) > 0) {
522 for (d = strlen(buf); d >= 0; --d)
525 strncpy(cptr->c_keys[a], buf, b);
526 cptr->c_keys[a][b] = 0;
528 strcpy(buf, &buf[b + 1]);
538 lastcmd->next = cptr;
548 * return the key associated with a command
550 char keycmd(char *cmdstr)
554 for (a = 0; a < strlen(cmdstr); ++a)
555 if (cmdstr[a] == '&')
556 return (tolower(cmdstr[a + 1]));
562 * Output the string from a key command without the ampersand
563 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
565 char *cmd_expand(char *strbuf, int mode)
573 for (a = 0; a < strlen(exp); ++a) {
574 if (strbuf[a] == '&') {
577 strcpy(&exp[a], &exp[a + 1]);
581 strcpy(buf, &exp[a + 2]);
587 if (!strncmp(&exp[a], "^r", 2)) {
589 strcpy(&exp[a], room_name);
590 strcat(exp, &buf[a + 2]);
592 if (!strncmp(&exp[a], "^c", 2)) {
594 strcpy(&exp[a + 1], &exp[a + 2]);
604 * Comparison function to determine if entered commands match a
605 * command loaded from the config file.
607 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
618 for (a = 0; a < ncomp; ++a) {
619 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
620 || (cptr->c_axlevel > cmdax))
628 * This function returns 1 if a given command requires a string input
630 int requires_string(struct citcmd *cptr, int ncomp)
635 strcpy(buf, cptr->c_keys[ncomp - 1]);
636 for (a = 0; a < strlen(buf); ++a) {
645 * Input a command at the main prompt.
646 * This function returns an integer command number. If the command prompts
647 * for a string then it is placed in the supplied buffer.
649 int getcmd(char *argbuf)
660 /* Switch color support on or off if we're in user mode */
661 if (rc_ansi_color == 3) {
662 if (userflags & US_COLOR)
667 /* if we're running in idiot mode, display a cute little menu */
668 IFNEXPERT formout("mainmenu");
670 print_express(); /* print express messages if there are any */
673 for (a = 0; a < 5; ++a)
675 /* now the room prompt... */
678 printf("\n%s", room_name);
680 printf("%c ", room_prompt(room_flags));
687 /* Handle the backspace key, but only if there's something
688 * to backspace over...
690 if ((ch == 8) && (cmdpos > 0)) {
691 back(cmdspaces[cmdpos - 1] + 1);
695 /* Spacebar invokes "lazy traversal" commands */
696 if ((ch == 32) && (cmdpos == 0)) {
697 this_lazy_cmd = next_lazy_cmd;
698 if (this_lazy_cmd == 13)
700 if (this_lazy_cmd == 5)
702 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
703 if (cptr->c_cmdnum == this_lazy_cmd) {
704 for (a = 0; a < 5; ++a)
705 if (cptr->c_keys[a][0] != 0)
706 printf("%s ", cmd_expand(
707 cptr->c_keys[a], 0));
709 return (this_lazy_cmd);
713 return (this_lazy_cmd);
715 /* Otherwise, process the command */
716 cmdbuf[cmdpos] = tolower(ch);
718 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
719 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
721 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
722 cmdspaces[cmdpos] = strlen(
723 cmd_expand(cptr->c_keys[cmdpos], 0));
725 if ((cptr->c_keys[cmdpos + 1]) != 0)
731 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
732 if (cmdmatch(cmdbuf, cptr, 5)) {
733 /* We've found our command. */
734 if (requires_string(cptr, cmdpos)) {
740 /* If this command is one that changes rooms,
741 * then the next lazy-command (space bar)
742 * should be "read new" instead of "goto"
744 if ((cptr->c_cmdnum == 5)
745 || (cptr->c_cmdnum == 6)
746 || (cptr->c_cmdnum == 47)
747 || (cptr->c_cmdnum == 52)
748 || (cptr->c_cmdnum == 16)
749 || (cptr->c_cmdnum == 20))
752 return (cptr->c_cmdnum);
758 printf("\rOne of ... \n");
759 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
760 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
761 for (a = 0; a < 5; ++a) {
762 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
768 printf("\n%s%c ", room_name, room_prompt(room_flags));
770 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
771 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
772 for (a = 0; a < cmdpos; ++a) {
774 cmd_expand(cptr->c_keys[a], 0));
789 * set tty modes. commands are:
791 * 0 - set to bbs mode, intr/quit disabled
792 * 1 - set to bbs mode, intr/quit enabled
793 * 2 - save current settings for later restoral
794 * 3 - restore saved settings
796 #ifdef HAVE_TERMIOS_H
797 void sttybbs(int cmd)
798 { /* SysV version of sttybbs() */
800 static struct termios saved_settings;
801 static int last_cmd = 0;
808 if ((cmd == 0) || (cmd == 1)) {
810 live.c_iflag = ISTRIP | IXON | IXANY;
811 live.c_oflag = OPOST | ONLCR;
812 live.c_lflag = ISIG | NOFLSH;
814 if (cmd == SB_YES_INTR) {
815 live.c_cc[VINTR] = NEXT_KEY;
816 live.c_cc[VQUIT] = STOP_KEY;
817 signal(SIGINT, *sighandler);
818 signal(SIGQUIT, *sighandler);
820 signal(SIGINT, SIG_IGN);
821 signal(SIGQUIT, SIG_IGN);
822 live.c_cc[VINTR] = (-1);
823 live.c_cc[VQUIT] = (-1);
826 /* do we even need this stuff anymore? */
828 live.c_cc[VERASE] = 8;
829 live.c_cc[VKILL] = 24;
831 live.c_cc[VEOL] = 255;
832 live.c_cc[VEOL2] = 0;
833 live.c_cc[VSTART] = 0;
834 tcsetattr(0, TCSADRAIN, &live);
837 tcgetattr(0, &saved_settings);
840 tcsetattr(0, TCSADRAIN, &saved_settings);
844 void sttybbs(int cmd)
845 { /* BSD version of sttybbs() */
847 static struct sgttyb saved_settings;
849 if ((cmd == 0) || (cmd == 1)) {
851 live.sg_flags |= CBREAK;
852 live.sg_flags |= CRMOD;
853 live.sg_flags |= NL1;
854 live.sg_flags &= ~ECHO;
856 live.sg_flags |= NOFLSH;
860 gtty(0, &saved_settings);
863 stty(0, &saved_settings);
870 * display_help() - help file viewer
872 void display_help(char *name)
879 * fmout() - Citadel text formatter and paginator
881 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
882 /* screen width to use */
883 /* file to read from, or NULL to read from server */
884 /* nonzero if we should use the paginator */
885 /* screen height to use */
886 /* starting value for lines_printed, -1 for global */
887 /* nonzero if we should use hypertext mode */
895 num_urls = 0; /* Start with a clean slate of embedded URL's */
897 if (starting_lp >= 0) {
898 lines_printed = starting_lp;
903 c = 1; /* c is the current pos */
908 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
912 if (fp != NULL) { /* read from file */
917 buffer[strlen(buffer) + 1] = 0;
918 buffer[strlen(buffer)] = a;
920 } else { /* read from server */
922 serv_gets(&buffer[d]);
923 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
924 buffer[strlen(buffer) - 1] = 0;
925 if (!strcmp(&buffer[d], "000")) {
928 while (isspace(buffer[strlen(buffer) - 1]))
929 buffer[strlen(buffer) - 1] = 0;
937 if ( (!struncmp(buffer, "http://", 7))
938 || (!struncmp(buffer, "ftp://", 6)) ) {
939 safestrncpy(urls[num_urls], buffer, 255);
940 for (a=0; a<strlen(urls[num_urls]); ++a) {
941 b = urls[num_urls][a];
942 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
943 || (b==13) || (b==9) || (b=='\"') )
944 urls[num_urls][a] = 0;
949 buffer[strlen(buffer) + 1] = 0;
951 strcpy(buffer, &buffer[1]);
958 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
960 if (((old == 13) || (old == 10)) && (isspace(real))) {
963 lines_printed = checkpagin(lines_printed, pagin, height);
970 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
975 lines_printed = checkpagin(lines_printed, pagin, height);
982 if ((strlen(aaa) + c) > (width - 5)) {
986 lines_printed = checkpagin(lines_printed, pagin, height);
994 if ((a == 13) || (a == 10)) {
998 lines_printed = checkpagin(lines_printed, pagin, height);
1004 /* signal caught; drain the server */
1007 } while (strcmp(aaa, "000"));
1009 FMTEND:printf("\n");
1011 lines_printed = checkpagin(lines_printed, pagin, height);
1017 * support ANSI color if defined
1019 void color(int colornum)
1021 static int is_bold = 0;
1022 static int hold_color, current_color;
1024 if (colornum == COLOR_PUSH) {
1025 hold_color = current_color;
1029 if (colornum == COLOR_POP) {
1034 current_color = colornum;
1036 printf("\033[3%dm", (colornum % 8));
1037 if ((colornum >= 8) && (is_bold == 0)) {
1040 } else if ((colornum < 8) && (is_bold == 1)) {
1048 void cls(int colornum)
1051 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1058 * Detect whether ANSI color is available (answerback)
1060 void send_ansi_detect(void)
1062 if (rc_ansi_color == 2) {
1069 void look_for_ansi(void)
1077 if (rc_ansi_color == 0) {
1079 } else if (rc_ansi_color == 1) {
1081 } else if (rc_ansi_color == 2) {
1083 /* otherwise, do the auto-detect */
1088 if ((now - AnsiDetect) < 2)
1097 select(1, &rfds, NULL, NULL, &tv);
1098 if (FD_ISSET(0, &rfds)) {
1099 abuf[strlen(abuf) + 1] = 0;
1100 read(0, &abuf[strlen(abuf)], 1);
1102 } while (FD_ISSET(0, &rfds));
1104 for (a = 0; a < strlen(abuf); ++a) {
1105 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1106 && (abuf[a + 2] == '?')) {
1115 * Display key options (highlight hotkeys inside angle brackets)
1117 void keyopt(char *buf) {
1121 for (i=0; i<strlen(buf); ++i) {
1123 putc(buf[i], stdout);
1124 color(BRIGHT_MAGENTA);
1129 putc(buf[i], stdout);
1138 * Present a key-menu line choice type of thing
1140 char keymenu(char *menuprompt, char *menustring) {
1146 int display_prompt = 1;
1148 choices = num_tokens(menustring, '|');
1150 if (menuprompt != NULL) do_prompt = 1;
1151 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1154 if (display_prompt) {
1156 printf("%s ", menuprompt);
1159 for (i=0; i<choices; ++i) {
1160 extract(buf, menustring, i);
1170 if ( (do_prompt) && (ch=='?') ) {
1171 printf("\rOne of... ");
1173 for (i=0; i<choices; ++i) {
1174 extract(buf, menustring, i);
1183 for (i=0; i<choices; ++i) {
1184 extract(buf, menustring, i);
1185 for (c=1; c<strlen(buf); ++c) {
1186 if ( (ch == tolower(buf[c]))
1188 && (buf[c+1]=='>') ) {
1189 for (a=0; a<strlen(buf); ++a) {
1190 if ( (a!=(c-1)) && (a!=(c+1))) {
1191 putc(buf[a], stdout);