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"
55 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
60 int rc_allow_attachments;
61 int rc_display_message_numbers;
62 int rc_force_mail_prompts;
63 int rc_remember_passwords;
66 char urls[MAXURLS][256];
70 int next_lazy_cmd = 5;
72 int lines_printed = 0; /* line count for paginator */
73 extern int screenwidth, screenheight;
75 struct citcmd *cmdlist = NULL;
78 /* these variables are local to this module */
79 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
80 int ok_to_interrupt = 0; /* print express msgs asynchronously */
81 time_t AnsiDetect; /* when did we send the detect code? */
82 int enable_color = 0; /* nonzero for ANSI color */
89 * Check to see if we need to pause at the end of a screen.
90 * If we do, we have to disable server keepalives during the pause because
91 * we are probably in the middle of a server operation and the NOOP command
92 * would confuse everything.
94 int checkpagin(int lp, int pagin, int height)
96 if (!pagin) return(0);
98 set_keepalives(KA_NO);
100 set_keepalives(KA_YES);
110 * pprintf() ... paginated version of printf()
112 void pprintf(const char *format, ...) {
114 static char buf[4096]; /* static for performance, change if needed */
117 va_start(arg_ptr, format);
118 vsprintf(buf, format, arg_ptr);
121 for (i=0; i<strlen(buf); ++i) {
122 putc(buf[i], stdout);
125 lines_printed = checkpagin(lines_printed,
126 (userflags & US_PAGINATOR),
135 * print_express() - print express messages if there are any
137 void print_express(void)
142 if (express_msgs == 0)
150 if (strlen(rc_exp_cmd) > 0) {
151 outpipe = popen(rc_exp_cmd, "w");
152 if (outpipe != NULL) {
153 while (serv_gets(buf), strcmp(buf, "000")) {
154 fprintf(outpipe, "%s\n", buf);
160 /* fall back to built-in express message display */
166 while (serv_gets(buf), strcmp(buf, "000")) {
174 void set_keepalives(int s)
176 keepalives_enabled = (char) s;
180 * This loop handles the "keepalive" messages sent to the server when idling.
183 static time_t idlet = 0;
184 static void really_do_keepalive(void) {
188 if (keepalives_enabled != KA_NO) {
190 if (keepalives_enabled == KA_YES) {
194 if (ok_to_interrupt == 1) {
195 printf("\r%64s\r", "");
197 printf("%s%c ", room_name,
198 room_prompt(room_flags));
206 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
207 encapsulated interface; in theory there should be no need to touch these
208 globals outside of the async_ka_* functions. */
210 #ifdef THREADED_CLIENT
211 static pthread_t ka_thr_handle;
212 static int ka_thr_active = 0;
213 static int async_ka_enabled = 0;
215 static void *ka_thread(void *arg)
217 really_do_keepalive();
218 pthread_detach(ka_thr_handle);
223 /* start up a thread to handle a keepalive in the background */
224 static void async_ka_exec(void)
226 if (!ka_thr_active) {
228 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
229 perror("pthread_create");
234 #endif /* THREADED_CLIENT */
236 static void do_keepalive(void)
241 if ((now - idlet) < ((long) S_KEEPALIVE))
244 /* Do a space-backspace to keep telnet sessions from idling out */
248 #ifdef THREADED_CLIENT
249 if (async_ka_enabled)
253 really_do_keepalive();
257 /* Now the actual async-keepalve API that we expose to higher levels:
258 async_ka_start() and async_ka_end(). These do nothing when we don't have
259 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
261 /* wait for a background keepalive to complete. this must be done before
262 attempting any further server requests! */
263 void async_ka_end(void)
265 #ifdef THREADED_CLIENT
267 pthread_join(ka_thr_handle, NULL);
273 /* tell do_keepalive() that keepalives are asynchronous. */
274 void async_ka_start(void)
276 #ifdef THREADED_CLIENT
283 { /* get a character from the keyboard, with */
284 int a; /* the watchdog timer in effect if necessary */
287 time_t start_time, now;
296 /* This loop waits for keyboard input. If the keepalive
297 * timer expires, it sends a keepalive to the server if
298 * necessary and then waits again.
305 tv.tv_sec = S_KEEPALIVE;
309 if (((now - start_time) > SLEEPING)
310 && (SLEEPING != 0) && (getppid() == 1)) {
311 printf("Sleeping? Call again.\n");
314 select(1, &rfds, NULL, NULL, &tv);
315 } while (!FD_ISSET(0, &rfds));
320 /* At this point, there's input, so fetch it.
321 * (There's a hole in the bucket...)
331 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
332 && ((a < 32) || (a > 126)))
340 { /* Returns 1 for yes, 0 for no */
356 /* Returns 1 for yes, 0 for no, arg is default value */
379 /* Gets a line from the terminal */
380 /* string == Pointer to string buffer */
381 /* lim == Maximum length - if negative, no-show */
382 void getline(char *string, int lim)
396 if ((a == 8) && (strlen(string) == 0))
398 if ((a != 13) && (a != 8) && (strlen(string) == lim))
400 if ((a == 8) && (string[0] != 0)) {
401 string[strlen(string) - 1] = 0;
407 if ((a == 13) || (a == 10)) {
427 * strprompt() - prompt for a string, print the existing value and
428 * allow the user to press return to keep it...
430 void strprompt(char *prompt, char *str, int len)
435 printf("%s ", prompt);
438 color(BRIGHT_MAGENTA);
452 * boolprompt() - prompt for a yes/no, print the existing value and
453 * allow the user to press return to keep it...
455 int boolprompt(char *prompt, int prev_val)
460 printf("%s ", prompt);
463 color(BRIGHT_MAGENTA);
464 printf("%s", (prev_val ? "Yes" : "No"));
468 r = (yesno_d(prev_val));
474 * intprompt() - like strprompt(), except for an integer
475 * (note that it RETURNS the new value!)
477 int intprompt(char *prompt, int ival, int imin, int imax)
485 snprintf(buf, sizeof buf, "%d", i);
486 strprompt(prompt, buf, 15);
488 for (p=0; p<strlen(buf); ++p) {
489 if ( (!isdigit(buf[p]))
490 && ( (buf[p]!='-') || (p!=0) ) )
494 printf("*** Must be no less than %d.\n", imin);
496 printf("*** Must be no more than %d.\n", imax);
497 } while ((i < imin) || (i > imax));
502 * newprompt() - prompt for a string with no existing value
503 * (clears out string buffer first)
505 void newprompt(char *prompt, char *str, int len)
507 color(BRIGHT_MAGENTA);
508 printf("%s", prompt);
516 { /* returns a lower case value */
525 * parse the citadel.rc file
527 void load_command_set(void)
532 struct citcmd *lastcmd = NULL;
537 /* first, set up some defaults for non-required variables */
539 strcpy(editor_path, "");
540 strcpy(printcmd, "");
541 strcpy(rc_username, "");
542 strcpy(rc_password, "");
545 rc_allow_attachments = 0;
546 rc_remember_passwords = 0;
547 strcpy(rc_exp_cmd, "");
548 rc_display_message_numbers = 0;
549 rc_force_mail_prompts = 0;
551 strcpy(rc_url_cmd, "");
553 /* now try to open the citadel.rc file */
556 if (getenv("HOME") != NULL) {
557 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
558 ccfile = fopen(buf, "r");
560 if (ccfile == NULL) {
561 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
563 if (ccfile == NULL) {
564 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
565 ccfile = fopen(buf, "r");
567 if (ccfile == NULL) {
568 ccfile = fopen("./citadel.rc", "r");
570 if (ccfile == NULL) {
571 perror("commands: cannot open citadel.rc");
574 while (fgets(buf, 256, ccfile) != NULL) {
575 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
576 buf[strlen(buf) - 1] = 0;
578 if (!strncasecmp(buf, "editor=", 7))
579 strcpy(editor_path, &buf[7]);
581 if (!strncasecmp(buf, "printcmd=", 9))
582 strcpy(printcmd, &buf[9]);
584 if (!strncasecmp(buf, "expcmd=", 7))
585 strcpy(rc_exp_cmd, &buf[7]);
587 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
588 have_xterm = (char) atoi(&buf[24]);
590 if (!strncasecmp(buf, "use_floors=", 11)) {
591 if (!strcasecmp(&buf[11], "yes"))
592 rc_floor_mode = RC_YES;
593 if (!strcasecmp(&buf[11], "no"))
594 rc_floor_mode = RC_NO;
595 if (!strcasecmp(&buf[11], "default"))
596 rc_floor_mode = RC_DEFAULT;
598 if (!strncasecmp(buf, "beep=", 5)) {
599 rc_exp_beep = atoi(&buf[5]);
601 if (!strncasecmp(buf, "allow_attachments=", 18)) {
602 rc_allow_attachments = atoi(&buf[18]);
604 if (!strncasecmp(buf, "remember_passwords=", 19)) {
605 rc_remember_passwords = atoi(&buf[19]);
607 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
608 rc_display_message_numbers = atoi(&buf[24]);
610 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
611 rc_force_mail_prompts = atoi(&buf[19]);
613 if (!strncasecmp(buf, "ansi_color=", 11)) {
614 if (!strncasecmp(&buf[11], "on", 2))
616 if (!strncasecmp(&buf[11], "auto", 4))
617 rc_ansi_color = 2; /* autodetect */
618 if (!strncasecmp(&buf[11], "user", 4))
619 rc_ansi_color = 3; /* user config */
621 if (!strncasecmp(buf, "username=", 9))
622 strcpy(rc_username, &buf[9]);
624 if (!strncasecmp(buf, "password=", 9))
625 strcpy(rc_password, &buf[9]);
627 if (!strncasecmp(buf, "urlcmd=", 7))
628 strcpy(rc_url_cmd, &buf[7]);
630 if (!strncasecmp(buf, "cmd=", 4)) {
631 strcpy(buf, &buf[4]);
633 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
635 cptr->c_cmdnum = atoi(buf);
636 for (d = strlen(buf); d >= 0; --d)
639 strcpy(buf, &buf[b + 1]);
641 cptr->c_axlevel = atoi(buf);
642 for (d = strlen(buf); d >= 0; --d)
645 strcpy(buf, &buf[b + 1]);
647 for (a = 0; a < 5; ++a)
648 cptr->c_keys[a][0] = 0;
652 buf[strlen(buf) + 1] = 0;
653 while (strlen(buf) > 0) {
655 for (d = strlen(buf); d >= 0; --d)
658 strncpy(cptr->c_keys[a], buf, b);
659 cptr->c_keys[a][b] = 0;
661 strcpy(buf, &buf[b + 1]);
671 lastcmd->next = cptr;
681 * return the key associated with a command
683 char keycmd(char *cmdstr)
687 for (a = 0; a < strlen(cmdstr); ++a)
688 if (cmdstr[a] == '&')
689 return (tolower(cmdstr[a + 1]));
695 * Output the string from a key command without the ampersand
696 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
698 char *cmd_expand(char *strbuf, int mode)
706 for (a = 0; a < strlen(exp); ++a) {
707 if (strbuf[a] == '&') {
710 strcpy(&exp[a], &exp[a + 1]);
714 strcpy(buf, &exp[a + 2]);
720 if (!strncmp(&exp[a], "^r", 2)) {
722 strcpy(&exp[a], room_name);
723 strcat(exp, &buf[a + 2]);
725 if (!strncmp(&exp[a], "^c", 2)) {
727 strcpy(&exp[a + 1], &exp[a + 2]);
737 * Comparison function to determine if entered commands match a
738 * command loaded from the config file.
740 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
751 for (a = 0; a < ncomp; ++a) {
752 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
753 || (cptr->c_axlevel > cmdax))
761 * This function returns 1 if a given command requires a string input
763 int requires_string(struct citcmd *cptr, int ncomp)
768 strcpy(buf, cptr->c_keys[ncomp - 1]);
769 for (a = 0; a < strlen(buf); ++a) {
778 * Input a command at the main prompt.
779 * This function returns an integer command number. If the command prompts
780 * for a string then it is placed in the supplied buffer.
782 int getcmd(char *argbuf)
793 /* Switch color support on or off if we're in user mode */
794 if (rc_ansi_color == 3) {
795 if (userflags & US_COLOR)
800 /* if we're running in idiot mode, display a cute little menu */
801 IFNEXPERT formout("mainmenu");
803 print_express(); /* print express messages if there are any */
806 for (a = 0; a < 5; ++a)
808 /* now the room prompt... */
811 printf("\n%s", room_name);
813 printf("%c ", room_prompt(room_flags));
820 /* Handle the backspace key, but only if there's something
821 * to backspace over...
823 if ((ch == 8) && (cmdpos > 0)) {
824 back(cmdspaces[cmdpos - 1] + 1);
828 /* Spacebar invokes "lazy traversal" commands */
829 if ((ch == 32) && (cmdpos == 0)) {
830 this_lazy_cmd = next_lazy_cmd;
831 if (this_lazy_cmd == 13)
833 if (this_lazy_cmd == 5)
835 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
836 if (cptr->c_cmdnum == this_lazy_cmd) {
837 for (a = 0; a < 5; ++a)
838 if (cptr->c_keys[a][0] != 0)
839 printf("%s ", cmd_expand(
840 cptr->c_keys[a], 0));
842 return (this_lazy_cmd);
846 return (this_lazy_cmd);
848 /* Otherwise, process the command */
849 cmdbuf[cmdpos] = tolower(ch);
851 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
852 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
854 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
855 cmdspaces[cmdpos] = strlen(
856 cmd_expand(cptr->c_keys[cmdpos], 0));
858 if ((cptr->c_keys[cmdpos + 1]) != 0)
864 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
865 if (cmdmatch(cmdbuf, cptr, 5)) {
866 /* We've found our command. */
867 if (requires_string(cptr, cmdpos)) {
873 /* If this command is one that changes rooms,
874 * then the next lazy-command (space bar)
875 * should be "read new" instead of "goto"
877 if ((cptr->c_cmdnum == 5)
878 || (cptr->c_cmdnum == 6)
879 || (cptr->c_cmdnum == 47)
880 || (cptr->c_cmdnum == 52)
881 || (cptr->c_cmdnum == 16)
882 || (cptr->c_cmdnum == 20))
885 return (cptr->c_cmdnum);
891 printf("\rOne of ... \n");
892 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
893 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
894 for (a = 0; a < 5; ++a) {
895 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
901 printf("\n%s%c ", room_name, room_prompt(room_flags));
903 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
904 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
905 for (a = 0; a < cmdpos; ++a) {
907 cmd_expand(cptr->c_keys[a], 0));
922 * set tty modes. commands are:
924 * 0 - set to bbs mode, intr/quit disabled
925 * 1 - set to bbs mode, intr/quit enabled
926 * 2 - save current settings for later restoral
927 * 3 - restore saved settings
929 #ifdef HAVE_TERMIOS_H
930 void sttybbs(int cmd)
931 { /* SysV version of sttybbs() */
933 static struct termios saved_settings;
934 static int last_cmd = 0;
941 if ((cmd == 0) || (cmd == 1)) {
943 live.c_iflag = ISTRIP | IXON | IXANY;
944 live.c_oflag = OPOST | ONLCR;
945 live.c_lflag = ISIG | NOFLSH;
947 if (cmd == SB_YES_INTR) {
948 live.c_cc[VINTR] = NEXT_KEY;
949 live.c_cc[VQUIT] = STOP_KEY;
950 signal(SIGINT, *sighandler);
951 signal(SIGQUIT, *sighandler);
953 signal(SIGINT, SIG_IGN);
954 signal(SIGQUIT, SIG_IGN);
955 live.c_cc[VINTR] = (-1);
956 live.c_cc[VQUIT] = (-1);
959 /* do we even need this stuff anymore? */
961 live.c_cc[VERASE] = 8;
962 live.c_cc[VKILL] = 24;
964 live.c_cc[VEOL] = 255;
965 live.c_cc[VEOL2] = 0;
966 live.c_cc[VSTART] = 0;
967 tcsetattr(0, TCSADRAIN, &live);
970 tcgetattr(0, &saved_settings);
973 tcsetattr(0, TCSADRAIN, &saved_settings);
977 void sttybbs(int cmd)
978 { /* BSD version of sttybbs() */
980 static struct sgttyb saved_settings;
982 if ((cmd == 0) || (cmd == 1)) {
984 live.sg_flags |= CBREAK;
985 live.sg_flags |= CRMOD;
986 live.sg_flags |= NL1;
987 live.sg_flags &= ~ECHO;
989 live.sg_flags |= NOFLSH;
993 gtty(0, &saved_settings);
996 stty(0, &saved_settings);
1003 * display_help() - help file viewer
1005 void display_help(char *name)
1012 * fmout() - Citadel text formatter and paginator
1014 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
1015 /* screen width to use */
1016 /* file to read from, or NULL to read from server */
1017 /* nonzero if we should use the paginator */
1018 /* screen height to use */
1019 /* starting value for lines_printed, -1 for global */
1020 /* nonzero if we should use hypertext mode */
1022 int a, b, c, d, old;
1028 num_urls = 0; /* Start with a clean slate of embedded URL's */
1030 if (starting_lp >= 0) {
1031 lines_printed = starting_lp;
1036 c = 1; /* c is the current pos */
1041 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1045 if (fp != NULL) { /* read from file */
1048 if (eof_flag == 0) {
1050 buffer[strlen(buffer) + 1] = 0;
1051 buffer[strlen(buffer)] = a;
1053 } else { /* read from server */
1055 serv_gets(&buffer[d]);
1056 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1057 buffer[strlen(buffer) - 1] = 0;
1058 if (!strcmp(&buffer[d], "000")) {
1061 while (isspace(buffer[strlen(buffer) - 1]))
1062 buffer[strlen(buffer) - 1] = 0;
1070 if ( (!strncasecmp(buffer, "http://", 7))
1071 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1072 safestrncpy(urls[num_urls], buffer, 255);
1073 for (a=0; a<strlen(urls[num_urls]); ++a) {
1074 b = urls[num_urls][a];
1075 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1076 || (b==13) || (b==9) || (b=='\"') )
1077 urls[num_urls][a] = 0;
1082 buffer[strlen(buffer) + 1] = 0;
1084 strcpy(buffer, &buffer[1]);
1091 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1093 if (((old == 13) || (old == 10)) && (isspace(real))) {
1096 lines_printed = checkpagin(lines_printed, pagin, height);
1103 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1104 printf("\n%s", aaa);
1108 lines_printed = checkpagin(lines_printed, pagin, height);
1115 if ((strlen(aaa) + c) > (width - 5)) {
1119 lines_printed = checkpagin(lines_printed, pagin, height);
1123 c = c + strlen(aaa);
1127 if ((a == 13) || (a == 10)) {
1128 printf("%s\n", aaa);
1131 lines_printed = checkpagin(lines_printed, pagin, height);
1137 /* signal caught; drain the server */
1140 } while (strcmp(aaa, "000"));
1142 FMTEND:printf("\n");
1144 lines_printed = checkpagin(lines_printed, pagin, height);
1150 * support ANSI color if defined
1152 void color(int colornum)
1154 static int is_bold = 0;
1155 static int hold_color, current_color;
1157 if (colornum == COLOR_PUSH) {
1158 hold_color = current_color;
1162 if (colornum == COLOR_POP) {
1167 current_color = colornum;
1169 /* When switching to dim white, actually output an 'original
1170 * pair' sequence -- this looks better on black-on-white
1173 if (colornum == DIM_WHITE)
1174 printf("\033[39;49m");
1176 printf("\033[3%d;40m", (colornum & 7));
1178 if ((colornum >= 8) && (is_bold == 0)) {
1181 } else if ((colornum < 8) && (is_bold == 1)) {
1189 void cls(int colornum)
1192 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1199 * Detect whether ANSI color is available (answerback)
1201 void send_ansi_detect(void)
1203 if (rc_ansi_color == 2) {
1210 void look_for_ansi(void)
1218 if (rc_ansi_color == 0) {
1220 } else if (rc_ansi_color == 1) {
1222 } else if (rc_ansi_color == 2) {
1224 /* otherwise, do the auto-detect */
1229 if ((now - AnsiDetect) < 2)
1238 select(1, &rfds, NULL, NULL, &tv);
1239 if (FD_ISSET(0, &rfds)) {
1240 abuf[strlen(abuf) + 1] = 0;
1241 read(0, &abuf[strlen(abuf)], 1);
1243 } while (FD_ISSET(0, &rfds));
1245 for (a = 0; a < strlen(abuf); ++a) {
1246 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1247 && (abuf[a + 2] == '?')) {
1256 * Display key options (highlight hotkeys inside angle brackets)
1258 void keyopt(char *buf) {
1262 for (i=0; i<strlen(buf); ++i) {
1264 putc(buf[i], stdout);
1265 color(BRIGHT_MAGENTA);
1270 putc(buf[i], stdout);
1279 * Present a key-menu line choice type of thing
1281 char keymenu(char *menuprompt, char *menustring) {
1287 int display_prompt = 1;
1289 choices = num_tokens(menustring, '|');
1291 if (menuprompt != NULL) do_prompt = 1;
1292 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1295 if (display_prompt) {
1297 printf("%s ", menuprompt);
1300 for (i=0; i<choices; ++i) {
1301 extract(buf, menustring, i);
1311 if ( (do_prompt) && (ch=='?') ) {
1312 printf("\rOne of... ");
1314 for (i=0; i<choices; ++i) {
1315 extract(buf, menustring, i);
1324 for (i=0; i<choices; ++i) {
1325 extract(buf, menustring, i);
1326 for (c=1; c<strlen(buf); ++c) {
1327 if ( (ch == tolower(buf[c]))
1329 && (buf[c+1]=='>') ) {
1330 for (a=0; a<strlen(buf); ++a) {
1331 if ( (a!=(c-1)) && (a!=(c+1))) {
1332 putc(buf[a], stdout);