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>
29 #ifdef THREADED_CLIENT
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 int lines_printed = 0; /* line count for paginator */
71 extern int screenwidth, screenheight;
73 struct citcmd *cmdlist = NULL;
76 /* these variables are local to this module */
77 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
78 int ok_to_interrupt = 0; /* print express msgs asynchronously */
79 time_t AnsiDetect; /* when did we send the detect code? */
80 int enable_color = 0; /* nonzero for ANSI color */
87 * Check to see if we need to pause at the end of a screen.
88 * If we do, we have to disable server keepalives during the pause because
89 * we are probably in the middle of a server operation and the NOOP command
90 * would confuse everything.
92 int checkpagin(int lp, int pagin, int height)
94 if (!pagin) return(0);
96 set_keepalives(KA_NO);
98 set_keepalives(KA_YES);
108 * pprintf() ... paginated version of printf()
110 void pprintf(const char *format, ...) {
112 static char buf[4096]; /* static for performance, change if needed */
115 va_start(arg_ptr, format);
116 vsprintf(buf, format, arg_ptr);
119 for (i=0; i<strlen(buf); ++i) {
120 putc(buf[i], stdout);
123 lines_printed = checkpagin(lines_printed,
124 (userflags & US_PAGINATOR),
133 * print_express() - print express messages if there are any
135 void print_express(void)
140 if (express_msgs == 0)
148 if (strlen(rc_exp_cmd) > 0) {
149 outpipe = popen(rc_exp_cmd, "w");
150 if (outpipe != NULL) {
151 while (serv_gets(buf), strcmp(buf, "000")) {
152 fprintf(outpipe, "%s\n", buf);
158 /* fall back to built-in express message display */
164 while (serv_gets(buf), strcmp(buf, "000")) {
172 void set_keepalives(int s)
174 keepalives_enabled = (char) s;
178 * This loop handles the "keepalive" messages sent to the server when idling.
181 static time_t idlet = 0;
182 static void really_do_keepalive(void) {
186 if (keepalives_enabled != KA_NO) {
188 if (keepalives_enabled == KA_YES) {
192 if (ok_to_interrupt == 1) {
193 printf("\r%64s\r", "");
195 printf("%s%c ", room_name,
196 room_prompt(room_flags));
204 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
205 encapsulated interface; in theory there should be no need to touch these
206 globals outside of the async_ka_* functions. */
208 #ifdef THREADED_CLIENT
209 static pthread_t ka_thr_handle;
210 static int ka_thr_active = 0;
211 static int async_ka_enabled = 0;
213 static void *ka_thread(void *arg)
215 really_do_keepalive();
216 pthread_detach(ka_thr_handle);
221 /* start up a thread to handle a keepalive in the background */
222 static void async_ka_exec(void)
224 if (!ka_thr_active) {
226 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
227 perror("pthread_create");
232 #endif /* THREADED_CLIENT */
234 static void do_keepalive(void)
239 if ((now - idlet) < ((long) S_KEEPALIVE))
242 /* Do a space-backspace to keep telnet sessions from idling out */
246 #ifdef THREADED_CLIENT
247 if (async_ka_enabled)
251 really_do_keepalive();
255 /* Now the actual async-keepalve API that we expose to higher levels:
256 async_ka_start() and async_ka_end(). These do nothing when we don't have
257 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
259 /* wait for a background keepalive to complete. this must be done before
260 attempting any further server requests! */
261 void async_ka_end(void)
263 #ifdef THREADED_CLIENT
265 pthread_join(ka_thr_handle, NULL);
271 /* tell do_keepalive() that keepalives are asynchronous. */
272 void async_ka_start(void)
274 #ifdef THREADED_CLIENT
281 { /* get a character from the keyboard, with */
282 int a; /* the watchdog timer in effect if necessary */
285 time_t start_time, now;
294 /* This loop waits for keyboard input. If the keepalive
295 * timer expires, it sends a keepalive to the server if
296 * necessary and then waits again.
303 tv.tv_sec = S_KEEPALIVE;
307 if (((now - start_time) > SLEEPING)
308 && (SLEEPING != 0) && (getppid() == 1)) {
309 printf("Sleeping? Call again.\n");
312 select(1, &rfds, NULL, NULL, &tv);
313 } while (!FD_ISSET(0, &rfds));
318 /* At this point, there's input, so fetch it.
319 * (There's a hole in the bucket...)
329 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
330 && ((a < 32) || (a > 126)))
338 { /* Returns 1 for yes, 0 for no */
354 /* Returns 1 for yes, 0 for no, arg is default value */
377 /* Gets a line from the terminal */
378 /* string == Pointer to string buffer */
379 /* lim == Maximum length - if negative, no-show */
380 void getline(char *string, int lim)
394 if ((a == 8) && (strlen(string) == 0))
396 if ((a != 13) && (a != 8) && (strlen(string) == lim))
398 if ((a == 8) && (string[0] != 0)) {
399 string[strlen(string) - 1] = 0;
405 if ((a == 13) || (a == 10)) {
425 * strprompt() - prompt for a string, print the existing value and
426 * allow the user to press return to keep it...
428 void strprompt(char *prompt, char *str, int len)
433 printf("%s ", prompt);
436 color(BRIGHT_MAGENTA);
450 * boolprompt() - prompt for a yes/no, print the existing value and
451 * allow the user to press return to keep it...
453 int boolprompt(char *prompt, int prev_val)
458 printf("%s ", prompt);
461 color(BRIGHT_MAGENTA);
462 printf("%s", (prev_val ? "Yes" : "No"));
466 r = (yesno_d(prev_val));
472 * intprompt() - like strprompt(), except for an integer
473 * (note that it RETURNS the new value!)
475 int intprompt(char *prompt, int ival, int imin, int imax)
483 snprintf(buf, sizeof buf, "%d", i);
484 strprompt(prompt, buf, 15);
486 for (p=0; p<strlen(buf); ++p) {
487 if (!isdigit(buf[p])) i = imin - 1;
490 printf("*** Must be no less than %d.\n", imin);
492 printf("*** Must be no more than %d.\n", imax);
493 } while ((i < imin) || (i > imax));
498 * newprompt() - prompt for a string with no existing value
499 * (clears out string buffer first)
501 void newprompt(char *prompt, char *str, int len)
503 color(BRIGHT_MAGENTA);
504 printf("%s", prompt);
512 { /* returns a lower case value */
521 * parse the citadel.rc file
523 void load_command_set(void)
528 struct citcmd *lastcmd = NULL;
533 /* first, set up some defaults for non-required variables */
535 strcpy(editor_path, "");
536 strcpy(printcmd, "");
537 strcpy(rc_username, "");
538 strcpy(rc_password, "");
541 rc_allow_attachments = 0;
542 strcpy(rc_exp_cmd, "");
543 rc_display_message_numbers = 0;
544 rc_force_mail_prompts = 0;
546 strcpy(rc_url_cmd, "");
548 /* now try to open the citadel.rc file */
551 if (getenv("HOME") != NULL) {
552 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
553 ccfile = fopen(buf, "r");
555 if (ccfile == NULL) {
556 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
558 if (ccfile == NULL) {
559 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
560 ccfile = fopen(buf, "r");
562 if (ccfile == NULL) {
563 ccfile = fopen("./citadel.rc", "r");
565 if (ccfile == NULL) {
566 perror("commands: cannot open citadel.rc");
569 while (fgets(buf, 256, ccfile) != NULL) {
570 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
571 buf[strlen(buf) - 1] = 0;
573 if (!struncmp(buf, "editor=", 7))
574 strcpy(editor_path, &buf[7]);
576 if (!struncmp(buf, "printcmd=", 9))
577 strcpy(printcmd, &buf[9]);
579 if (!struncmp(buf, "expcmd=", 7))
580 strcpy(rc_exp_cmd, &buf[7]);
582 if (!struncmp(buf, "local_screen_dimensions=", 24))
583 have_xterm = (char) atoi(&buf[24]);
585 if (!struncmp(buf, "use_floors=", 11)) {
586 if (!strucmp(&buf[11], "yes"))
587 rc_floor_mode = RC_YES;
588 if (!strucmp(&buf[11], "no"))
589 rc_floor_mode = RC_NO;
590 if (!strucmp(&buf[11], "default"))
591 rc_floor_mode = RC_DEFAULT;
593 if (!struncmp(buf, "beep=", 5)) {
594 rc_exp_beep = atoi(&buf[5]);
596 if (!struncmp(buf, "allow_attachments=", 18)) {
597 rc_allow_attachments = atoi(&buf[18]);
599 if (!struncmp(buf, "display_message_numbers=", 24)) {
600 rc_display_message_numbers = atoi(&buf[24]);
602 if (!struncmp(buf, "force_mail_prompts=", 19)) {
603 rc_force_mail_prompts = atoi(&buf[19]);
605 if (!struncmp(buf, "ansi_color=", 11)) {
606 if (!struncmp(&buf[11], "on", 2))
608 if (!struncmp(&buf[11], "auto", 4))
609 rc_ansi_color = 2; /* autodetect */
610 if (!struncmp(&buf[11], "user", 4))
611 rc_ansi_color = 3; /* user config */
613 if (!struncmp(buf, "username=", 9))
614 strcpy(rc_username, &buf[9]);
616 if (!struncmp(buf, "password=", 9))
617 strcpy(rc_password, &buf[9]);
619 if (!struncmp(buf, "urlcmd=", 7))
620 strcpy(rc_url_cmd, &buf[7]);
622 if (!struncmp(buf, "cmd=", 4)) {
623 strcpy(buf, &buf[4]);
625 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
627 cptr->c_cmdnum = atoi(buf);
628 for (d = strlen(buf); d >= 0; --d)
631 strcpy(buf, &buf[b + 1]);
633 cptr->c_axlevel = atoi(buf);
634 for (d = strlen(buf); d >= 0; --d)
637 strcpy(buf, &buf[b + 1]);
639 for (a = 0; a < 5; ++a)
640 cptr->c_keys[a][0] = 0;
644 buf[strlen(buf) + 1] = 0;
645 while (strlen(buf) > 0) {
647 for (d = strlen(buf); d >= 0; --d)
650 strncpy(cptr->c_keys[a], buf, b);
651 cptr->c_keys[a][b] = 0;
653 strcpy(buf, &buf[b + 1]);
663 lastcmd->next = cptr;
673 * return the key associated with a command
675 char keycmd(char *cmdstr)
679 for (a = 0; a < strlen(cmdstr); ++a)
680 if (cmdstr[a] == '&')
681 return (tolower(cmdstr[a + 1]));
687 * Output the string from a key command without the ampersand
688 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
690 char *cmd_expand(char *strbuf, int mode)
698 for (a = 0; a < strlen(exp); ++a) {
699 if (strbuf[a] == '&') {
702 strcpy(&exp[a], &exp[a + 1]);
706 strcpy(buf, &exp[a + 2]);
712 if (!strncmp(&exp[a], "^r", 2)) {
714 strcpy(&exp[a], room_name);
715 strcat(exp, &buf[a + 2]);
717 if (!strncmp(&exp[a], "^c", 2)) {
719 strcpy(&exp[a + 1], &exp[a + 2]);
729 * Comparison function to determine if entered commands match a
730 * command loaded from the config file.
732 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
743 for (a = 0; a < ncomp; ++a) {
744 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
745 || (cptr->c_axlevel > cmdax))
753 * This function returns 1 if a given command requires a string input
755 int requires_string(struct citcmd *cptr, int ncomp)
760 strcpy(buf, cptr->c_keys[ncomp - 1]);
761 for (a = 0; a < strlen(buf); ++a) {
770 * Input a command at the main prompt.
771 * This function returns an integer command number. If the command prompts
772 * for a string then it is placed in the supplied buffer.
774 int getcmd(char *argbuf)
785 /* Switch color support on or off if we're in user mode */
786 if (rc_ansi_color == 3) {
787 if (userflags & US_COLOR)
792 /* if we're running in idiot mode, display a cute little menu */
793 IFNEXPERT formout("mainmenu");
795 print_express(); /* print express messages if there are any */
798 for (a = 0; a < 5; ++a)
800 /* now the room prompt... */
803 printf("\n%s", room_name);
805 printf("%c ", room_prompt(room_flags));
812 /* Handle the backspace key, but only if there's something
813 * to backspace over...
815 if ((ch == 8) && (cmdpos > 0)) {
816 back(cmdspaces[cmdpos - 1] + 1);
820 /* Spacebar invokes "lazy traversal" commands */
821 if ((ch == 32) && (cmdpos == 0)) {
822 this_lazy_cmd = next_lazy_cmd;
823 if (this_lazy_cmd == 13)
825 if (this_lazy_cmd == 5)
827 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
828 if (cptr->c_cmdnum == this_lazy_cmd) {
829 for (a = 0; a < 5; ++a)
830 if (cptr->c_keys[a][0] != 0)
831 printf("%s ", cmd_expand(
832 cptr->c_keys[a], 0));
834 return (this_lazy_cmd);
838 return (this_lazy_cmd);
840 /* Otherwise, process the command */
841 cmdbuf[cmdpos] = tolower(ch);
843 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
844 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
846 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
847 cmdspaces[cmdpos] = strlen(
848 cmd_expand(cptr->c_keys[cmdpos], 0));
850 if ((cptr->c_keys[cmdpos + 1]) != 0)
856 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
857 if (cmdmatch(cmdbuf, cptr, 5)) {
858 /* We've found our command. */
859 if (requires_string(cptr, cmdpos)) {
865 /* If this command is one that changes rooms,
866 * then the next lazy-command (space bar)
867 * should be "read new" instead of "goto"
869 if ((cptr->c_cmdnum == 5)
870 || (cptr->c_cmdnum == 6)
871 || (cptr->c_cmdnum == 47)
872 || (cptr->c_cmdnum == 52)
873 || (cptr->c_cmdnum == 16)
874 || (cptr->c_cmdnum == 20))
877 return (cptr->c_cmdnum);
883 printf("\rOne of ... \n");
884 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
885 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
886 for (a = 0; a < 5; ++a) {
887 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
893 printf("\n%s%c ", room_name, room_prompt(room_flags));
895 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
896 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
897 for (a = 0; a < cmdpos; ++a) {
899 cmd_expand(cptr->c_keys[a], 0));
914 * set tty modes. commands are:
916 * 0 - set to bbs mode, intr/quit disabled
917 * 1 - set to bbs mode, intr/quit enabled
918 * 2 - save current settings for later restoral
919 * 3 - restore saved settings
921 #ifdef HAVE_TERMIOS_H
922 void sttybbs(int cmd)
923 { /* SysV version of sttybbs() */
925 static struct termios saved_settings;
926 static int last_cmd = 0;
933 if ((cmd == 0) || (cmd == 1)) {
935 live.c_iflag = ISTRIP | IXON | IXANY;
936 live.c_oflag = OPOST | ONLCR;
937 live.c_lflag = ISIG | NOFLSH;
939 if (cmd == SB_YES_INTR) {
940 live.c_cc[VINTR] = NEXT_KEY;
941 live.c_cc[VQUIT] = STOP_KEY;
942 signal(SIGINT, *sighandler);
943 signal(SIGQUIT, *sighandler);
945 signal(SIGINT, SIG_IGN);
946 signal(SIGQUIT, SIG_IGN);
947 live.c_cc[VINTR] = (-1);
948 live.c_cc[VQUIT] = (-1);
951 /* do we even need this stuff anymore? */
953 live.c_cc[VERASE] = 8;
954 live.c_cc[VKILL] = 24;
956 live.c_cc[VEOL] = 255;
957 live.c_cc[VEOL2] = 0;
958 live.c_cc[VSTART] = 0;
959 tcsetattr(0, TCSADRAIN, &live);
962 tcgetattr(0, &saved_settings);
965 tcsetattr(0, TCSADRAIN, &saved_settings);
969 void sttybbs(int cmd)
970 { /* BSD version of sttybbs() */
972 static struct sgttyb saved_settings;
974 if ((cmd == 0) || (cmd == 1)) {
976 live.sg_flags |= CBREAK;
977 live.sg_flags |= CRMOD;
978 live.sg_flags |= NL1;
979 live.sg_flags &= ~ECHO;
981 live.sg_flags |= NOFLSH;
985 gtty(0, &saved_settings);
988 stty(0, &saved_settings);
995 * display_help() - help file viewer
997 void display_help(char *name)
1004 * fmout() - Citadel text formatter and paginator
1006 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
1007 /* screen width to use */
1008 /* file to read from, or NULL to read from server */
1009 /* nonzero if we should use the paginator */
1010 /* screen height to use */
1011 /* starting value for lines_printed, -1 for global */
1012 /* nonzero if we should use hypertext mode */
1014 int a, b, c, d, old;
1020 num_urls = 0; /* Start with a clean slate of embedded URL's */
1022 if (starting_lp >= 0) {
1023 lines_printed = starting_lp;
1028 c = 1; /* c is the current pos */
1033 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1037 if (fp != NULL) { /* read from file */
1040 if (eof_flag == 0) {
1042 buffer[strlen(buffer) + 1] = 0;
1043 buffer[strlen(buffer)] = a;
1045 } else { /* read from server */
1047 serv_gets(&buffer[d]);
1048 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1049 buffer[strlen(buffer) - 1] = 0;
1050 if (!strcmp(&buffer[d], "000")) {
1053 while (isspace(buffer[strlen(buffer) - 1]))
1054 buffer[strlen(buffer) - 1] = 0;
1062 if ( (!struncmp(buffer, "http://", 7))
1063 || (!struncmp(buffer, "ftp://", 6)) ) {
1064 safestrncpy(urls[num_urls], buffer, 255);
1065 for (a=0; a<strlen(urls[num_urls]); ++a) {
1066 b = urls[num_urls][a];
1067 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1068 || (b==13) || (b==9) || (b=='\"') )
1069 urls[num_urls][a] = 0;
1074 buffer[strlen(buffer) + 1] = 0;
1076 strcpy(buffer, &buffer[1]);
1083 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1085 if (((old == 13) || (old == 10)) && (isspace(real))) {
1088 lines_printed = checkpagin(lines_printed, pagin, height);
1095 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1096 printf("\n%s", aaa);
1100 lines_printed = checkpagin(lines_printed, pagin, height);
1107 if ((strlen(aaa) + c) > (width - 5)) {
1111 lines_printed = checkpagin(lines_printed, pagin, height);
1115 c = c + strlen(aaa);
1119 if ((a == 13) || (a == 10)) {
1120 printf("%s\n", aaa);
1123 lines_printed = checkpagin(lines_printed, pagin, height);
1129 /* signal caught; drain the server */
1132 } while (strcmp(aaa, "000"));
1134 FMTEND:printf("\n");
1136 lines_printed = checkpagin(lines_printed, pagin, height);
1142 * support ANSI color if defined
1144 void color(int colornum)
1146 static int is_bold = 0;
1147 static int hold_color, current_color;
1149 if (colornum == COLOR_PUSH) {
1150 hold_color = current_color;
1154 if (colornum == COLOR_POP) {
1159 current_color = colornum;
1161 /* When switching to dim white, actually output an 'original
1162 * pair' sequence -- this looks better on black-on-white
1165 if (colornum == DIM_WHITE)
1166 printf("\033[39;49m");
1168 printf("\033[3%d;40m", (colornum & 7));
1170 if ((colornum >= 8) && (is_bold == 0)) {
1173 } else if ((colornum < 8) && (is_bold == 1)) {
1181 void cls(int colornum)
1184 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1191 * Detect whether ANSI color is available (answerback)
1193 void send_ansi_detect(void)
1195 if (rc_ansi_color == 2) {
1202 void look_for_ansi(void)
1210 if (rc_ansi_color == 0) {
1212 } else if (rc_ansi_color == 1) {
1214 } else if (rc_ansi_color == 2) {
1216 /* otherwise, do the auto-detect */
1221 if ((now - AnsiDetect) < 2)
1230 select(1, &rfds, NULL, NULL, &tv);
1231 if (FD_ISSET(0, &rfds)) {
1232 abuf[strlen(abuf) + 1] = 0;
1233 read(0, &abuf[strlen(abuf)], 1);
1235 } while (FD_ISSET(0, &rfds));
1237 for (a = 0; a < strlen(abuf); ++a) {
1238 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1239 && (abuf[a + 2] == '?')) {
1248 * Display key options (highlight hotkeys inside angle brackets)
1250 void keyopt(char *buf) {
1254 for (i=0; i<strlen(buf); ++i) {
1256 putc(buf[i], stdout);
1257 color(BRIGHT_MAGENTA);
1262 putc(buf[i], stdout);
1271 * Present a key-menu line choice type of thing
1273 char keymenu(char *menuprompt, char *menustring) {
1279 int display_prompt = 1;
1281 choices = num_tokens(menustring, '|');
1283 if (menuprompt != NULL) do_prompt = 1;
1284 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1287 if (display_prompt) {
1289 printf("%s ", menuprompt);
1292 for (i=0; i<choices; ++i) {
1293 extract(buf, menustring, i);
1303 if ( (do_prompt) && (ch=='?') ) {
1304 printf("\rOne of... ");
1306 for (i=0; i<choices; ++i) {
1307 extract(buf, menustring, i);
1316 for (i=0; i<choices; ++i) {
1317 extract(buf, menustring, i);
1318 for (c=1; c<strlen(buf); ++c) {
1319 if ( (ch == tolower(buf[c]))
1321 && (buf[c+1]=='>') ) {
1322 for (a=0; a<strlen(buf); ++a) {
1323 if ( (a!=(c-1)) && (a!=(c+1))) {
1324 putc(buf[a], stdout);