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;
62 int rc_remember_passwords;
65 char urls[MAXURLS][256];
69 int next_lazy_cmd = 5;
71 int lines_printed = 0; /* line count for paginator */
72 extern int screenwidth, screenheight;
74 struct citcmd *cmdlist = NULL;
77 /* these variables are local to this module */
78 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
79 int ok_to_interrupt = 0; /* print express msgs asynchronously */
80 time_t AnsiDetect; /* when did we send the detect code? */
81 int enable_color = 0; /* nonzero for ANSI color */
88 * Check to see if we need to pause at the end of a screen.
89 * If we do, we have to disable server keepalives during the pause because
90 * we are probably in the middle of a server operation and the NOOP command
91 * would confuse everything.
93 int checkpagin(int lp, int pagin, int height)
95 if (!pagin) return(0);
97 set_keepalives(KA_NO);
99 set_keepalives(KA_YES);
109 * pprintf() ... paginated version of printf()
111 void pprintf(const char *format, ...) {
113 static char buf[4096]; /* static for performance, change if needed */
116 va_start(arg_ptr, format);
117 vsprintf(buf, format, arg_ptr);
120 for (i=0; i<strlen(buf); ++i) {
121 putc(buf[i], stdout);
124 lines_printed = checkpagin(lines_printed,
125 (userflags & US_PAGINATOR),
134 * print_express() - print express messages if there are any
136 void print_express(void)
141 if (express_msgs == 0)
149 if (strlen(rc_exp_cmd) > 0) {
150 outpipe = popen(rc_exp_cmd, "w");
151 if (outpipe != NULL) {
152 while (serv_gets(buf), strcmp(buf, "000")) {
153 fprintf(outpipe, "%s\n", buf);
159 /* fall back to built-in express message display */
165 while (serv_gets(buf), strcmp(buf, "000")) {
173 void set_keepalives(int s)
175 keepalives_enabled = (char) s;
179 * This loop handles the "keepalive" messages sent to the server when idling.
182 static time_t idlet = 0;
183 static void really_do_keepalive(void) {
187 if (keepalives_enabled != KA_NO) {
189 if (keepalives_enabled == KA_YES) {
193 if (ok_to_interrupt == 1) {
194 printf("\r%64s\r", "");
196 printf("%s%c ", room_name,
197 room_prompt(room_flags));
205 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
206 encapsulated interface; in theory there should be no need to touch these
207 globals outside of the async_ka_* functions. */
209 #ifdef THREADED_CLIENT
210 static pthread_t ka_thr_handle;
211 static int ka_thr_active = 0;
212 static int async_ka_enabled = 0;
214 static void *ka_thread(void *arg)
216 really_do_keepalive();
217 pthread_detach(ka_thr_handle);
222 /* start up a thread to handle a keepalive in the background */
223 static void async_ka_exec(void)
225 if (!ka_thr_active) {
227 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
228 perror("pthread_create");
233 #endif /* THREADED_CLIENT */
235 static void do_keepalive(void)
240 if ((now - idlet) < ((long) S_KEEPALIVE))
243 /* Do a space-backspace to keep telnet sessions from idling out */
247 #ifdef THREADED_CLIENT
248 if (async_ka_enabled)
252 really_do_keepalive();
256 /* Now the actual async-keepalve API that we expose to higher levels:
257 async_ka_start() and async_ka_end(). These do nothing when we don't have
258 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
260 /* wait for a background keepalive to complete. this must be done before
261 attempting any further server requests! */
262 void async_ka_end(void)
264 #ifdef THREADED_CLIENT
266 pthread_join(ka_thr_handle, NULL);
272 /* tell do_keepalive() that keepalives are asynchronous. */
273 void async_ka_start(void)
275 #ifdef THREADED_CLIENT
282 { /* get a character from the keyboard, with */
283 int a; /* the watchdog timer in effect if necessary */
286 time_t start_time, now;
295 /* This loop waits for keyboard input. If the keepalive
296 * timer expires, it sends a keepalive to the server if
297 * necessary and then waits again.
304 tv.tv_sec = S_KEEPALIVE;
308 if (((now - start_time) > SLEEPING)
309 && (SLEEPING != 0) && (getppid() == 1)) {
310 printf("Sleeping? Call again.\n");
313 select(1, &rfds, NULL, NULL, &tv);
314 } while (!FD_ISSET(0, &rfds));
319 /* At this point, there's input, so fetch it.
320 * (There's a hole in the bucket...)
330 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
331 && ((a < 32) || (a > 126)))
339 { /* Returns 1 for yes, 0 for no */
355 /* Returns 1 for yes, 0 for no, arg is default value */
378 /* Gets a line from the terminal */
379 /* string == Pointer to string buffer */
380 /* lim == Maximum length - if negative, no-show */
381 void getline(char *string, int lim)
395 if ((a == 8) && (strlen(string) == 0))
397 if ((a != 13) && (a != 8) && (strlen(string) == lim))
399 if ((a == 8) && (string[0] != 0)) {
400 string[strlen(string) - 1] = 0;
406 if ((a == 13) || (a == 10)) {
426 * strprompt() - prompt for a string, print the existing value and
427 * allow the user to press return to keep it...
429 void strprompt(char *prompt, char *str, int len)
434 printf("%s ", prompt);
437 color(BRIGHT_MAGENTA);
451 * boolprompt() - prompt for a yes/no, print the existing value and
452 * allow the user to press return to keep it...
454 int boolprompt(char *prompt, int prev_val)
459 printf("%s ", prompt);
462 color(BRIGHT_MAGENTA);
463 printf("%s", (prev_val ? "Yes" : "No"));
467 r = (yesno_d(prev_val));
473 * intprompt() - like strprompt(), except for an integer
474 * (note that it RETURNS the new value!)
476 int intprompt(char *prompt, int ival, int imin, int imax)
484 snprintf(buf, sizeof buf, "%d", i);
485 strprompt(prompt, buf, 15);
487 for (p=0; p<strlen(buf); ++p) {
488 if (!isdigit(buf[p])) i = imin - 1;
491 printf("*** Must be no less than %d.\n", imin);
493 printf("*** Must be no more than %d.\n", imax);
494 } while ((i < imin) || (i > imax));
499 * newprompt() - prompt for a string with no existing value
500 * (clears out string buffer first)
502 void newprompt(char *prompt, char *str, int len)
504 color(BRIGHT_MAGENTA);
505 printf("%s", prompt);
513 { /* returns a lower case value */
522 * parse the citadel.rc file
524 void load_command_set(void)
529 struct citcmd *lastcmd = NULL;
534 /* first, set up some defaults for non-required variables */
536 strcpy(editor_path, "");
537 strcpy(printcmd, "");
538 strcpy(rc_username, "");
539 strcpy(rc_password, "");
542 rc_allow_attachments = 0;
543 rc_remember_passwords = 0;
544 strcpy(rc_exp_cmd, "");
545 rc_display_message_numbers = 0;
546 rc_force_mail_prompts = 0;
548 strcpy(rc_url_cmd, "");
550 /* now try to open the citadel.rc file */
553 if (getenv("HOME") != NULL) {
554 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
555 ccfile = fopen(buf, "r");
557 if (ccfile == NULL) {
558 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
560 if (ccfile == NULL) {
561 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
562 ccfile = fopen(buf, "r");
564 if (ccfile == NULL) {
565 ccfile = fopen("./citadel.rc", "r");
567 if (ccfile == NULL) {
568 perror("commands: cannot open citadel.rc");
571 while (fgets(buf, 256, ccfile) != NULL) {
572 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
573 buf[strlen(buf) - 1] = 0;
575 if (!strncasecmp(buf, "editor=", 7))
576 strcpy(editor_path, &buf[7]);
578 if (!strncasecmp(buf, "printcmd=", 9))
579 strcpy(printcmd, &buf[9]);
581 if (!strncasecmp(buf, "expcmd=", 7))
582 strcpy(rc_exp_cmd, &buf[7]);
584 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
585 have_xterm = (char) atoi(&buf[24]);
587 if (!strncasecmp(buf, "use_floors=", 11)) {
588 if (!strcasecmp(&buf[11], "yes"))
589 rc_floor_mode = RC_YES;
590 if (!strcasecmp(&buf[11], "no"))
591 rc_floor_mode = RC_NO;
592 if (!strcasecmp(&buf[11], "default"))
593 rc_floor_mode = RC_DEFAULT;
595 if (!strncasecmp(buf, "beep=", 5)) {
596 rc_exp_beep = atoi(&buf[5]);
598 if (!strncasecmp(buf, "allow_attachments=", 18)) {
599 rc_allow_attachments = atoi(&buf[18]);
601 if (!strncasecmp(buf, "remember_passwords=", 19)) {
602 rc_remember_passwords = atoi(&buf[19]);
604 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
605 rc_display_message_numbers = atoi(&buf[24]);
607 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
608 rc_force_mail_prompts = atoi(&buf[19]);
610 if (!strncasecmp(buf, "ansi_color=", 11)) {
611 if (!strncasecmp(&buf[11], "on", 2))
613 if (!strncasecmp(&buf[11], "auto", 4))
614 rc_ansi_color = 2; /* autodetect */
615 if (!strncasecmp(&buf[11], "user", 4))
616 rc_ansi_color = 3; /* user config */
618 if (!strncasecmp(buf, "username=", 9))
619 strcpy(rc_username, &buf[9]);
621 if (!strncasecmp(buf, "password=", 9))
622 strcpy(rc_password, &buf[9]);
624 if (!strncasecmp(buf, "urlcmd=", 7))
625 strcpy(rc_url_cmd, &buf[7]);
627 if (!strncasecmp(buf, "cmd=", 4)) {
628 strcpy(buf, &buf[4]);
630 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
632 cptr->c_cmdnum = atoi(buf);
633 for (d = strlen(buf); d >= 0; --d)
636 strcpy(buf, &buf[b + 1]);
638 cptr->c_axlevel = atoi(buf);
639 for (d = strlen(buf); d >= 0; --d)
642 strcpy(buf, &buf[b + 1]);
644 for (a = 0; a < 5; ++a)
645 cptr->c_keys[a][0] = 0;
649 buf[strlen(buf) + 1] = 0;
650 while (strlen(buf) > 0) {
652 for (d = strlen(buf); d >= 0; --d)
655 strncpy(cptr->c_keys[a], buf, b);
656 cptr->c_keys[a][b] = 0;
658 strcpy(buf, &buf[b + 1]);
668 lastcmd->next = cptr;
678 * return the key associated with a command
680 char keycmd(char *cmdstr)
684 for (a = 0; a < strlen(cmdstr); ++a)
685 if (cmdstr[a] == '&')
686 return (tolower(cmdstr[a + 1]));
692 * Output the string from a key command without the ampersand
693 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
695 char *cmd_expand(char *strbuf, int mode)
703 for (a = 0; a < strlen(exp); ++a) {
704 if (strbuf[a] == '&') {
707 strcpy(&exp[a], &exp[a + 1]);
711 strcpy(buf, &exp[a + 2]);
717 if (!strncmp(&exp[a], "^r", 2)) {
719 strcpy(&exp[a], room_name);
720 strcat(exp, &buf[a + 2]);
722 if (!strncmp(&exp[a], "^c", 2)) {
724 strcpy(&exp[a + 1], &exp[a + 2]);
734 * Comparison function to determine if entered commands match a
735 * command loaded from the config file.
737 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
748 for (a = 0; a < ncomp; ++a) {
749 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
750 || (cptr->c_axlevel > cmdax))
758 * This function returns 1 if a given command requires a string input
760 int requires_string(struct citcmd *cptr, int ncomp)
765 strcpy(buf, cptr->c_keys[ncomp - 1]);
766 for (a = 0; a < strlen(buf); ++a) {
775 * Input a command at the main prompt.
776 * This function returns an integer command number. If the command prompts
777 * for a string then it is placed in the supplied buffer.
779 int getcmd(char *argbuf)
790 /* Switch color support on or off if we're in user mode */
791 if (rc_ansi_color == 3) {
792 if (userflags & US_COLOR)
797 /* if we're running in idiot mode, display a cute little menu */
798 IFNEXPERT formout("mainmenu");
800 print_express(); /* print express messages if there are any */
803 for (a = 0; a < 5; ++a)
805 /* now the room prompt... */
808 printf("\n%s", room_name);
810 printf("%c ", room_prompt(room_flags));
817 /* Handle the backspace key, but only if there's something
818 * to backspace over...
820 if ((ch == 8) && (cmdpos > 0)) {
821 back(cmdspaces[cmdpos - 1] + 1);
825 /* Spacebar invokes "lazy traversal" commands */
826 if ((ch == 32) && (cmdpos == 0)) {
827 this_lazy_cmd = next_lazy_cmd;
828 if (this_lazy_cmd == 13)
830 if (this_lazy_cmd == 5)
832 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
833 if (cptr->c_cmdnum == this_lazy_cmd) {
834 for (a = 0; a < 5; ++a)
835 if (cptr->c_keys[a][0] != 0)
836 printf("%s ", cmd_expand(
837 cptr->c_keys[a], 0));
839 return (this_lazy_cmd);
843 return (this_lazy_cmd);
845 /* Otherwise, process the command */
846 cmdbuf[cmdpos] = tolower(ch);
848 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
849 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
851 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
852 cmdspaces[cmdpos] = strlen(
853 cmd_expand(cptr->c_keys[cmdpos], 0));
855 if ((cptr->c_keys[cmdpos + 1]) != 0)
861 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
862 if (cmdmatch(cmdbuf, cptr, 5)) {
863 /* We've found our command. */
864 if (requires_string(cptr, cmdpos)) {
870 /* If this command is one that changes rooms,
871 * then the next lazy-command (space bar)
872 * should be "read new" instead of "goto"
874 if ((cptr->c_cmdnum == 5)
875 || (cptr->c_cmdnum == 6)
876 || (cptr->c_cmdnum == 47)
877 || (cptr->c_cmdnum == 52)
878 || (cptr->c_cmdnum == 16)
879 || (cptr->c_cmdnum == 20))
882 return (cptr->c_cmdnum);
888 printf("\rOne of ... \n");
889 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
890 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
891 for (a = 0; a < 5; ++a) {
892 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
898 printf("\n%s%c ", room_name, room_prompt(room_flags));
900 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
901 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
902 for (a = 0; a < cmdpos; ++a) {
904 cmd_expand(cptr->c_keys[a], 0));
919 * set tty modes. commands are:
921 * 0 - set to bbs mode, intr/quit disabled
922 * 1 - set to bbs mode, intr/quit enabled
923 * 2 - save current settings for later restoral
924 * 3 - restore saved settings
926 #ifdef HAVE_TERMIOS_H
927 void sttybbs(int cmd)
928 { /* SysV version of sttybbs() */
930 static struct termios saved_settings;
931 static int last_cmd = 0;
938 if ((cmd == 0) || (cmd == 1)) {
940 live.c_iflag = ISTRIP | IXON | IXANY;
941 live.c_oflag = OPOST | ONLCR;
942 live.c_lflag = ISIG | NOFLSH;
944 if (cmd == SB_YES_INTR) {
945 live.c_cc[VINTR] = NEXT_KEY;
946 live.c_cc[VQUIT] = STOP_KEY;
947 signal(SIGINT, *sighandler);
948 signal(SIGQUIT, *sighandler);
950 signal(SIGINT, SIG_IGN);
951 signal(SIGQUIT, SIG_IGN);
952 live.c_cc[VINTR] = (-1);
953 live.c_cc[VQUIT] = (-1);
956 /* do we even need this stuff anymore? */
958 live.c_cc[VERASE] = 8;
959 live.c_cc[VKILL] = 24;
961 live.c_cc[VEOL] = 255;
962 live.c_cc[VEOL2] = 0;
963 live.c_cc[VSTART] = 0;
964 tcsetattr(0, TCSADRAIN, &live);
967 tcgetattr(0, &saved_settings);
970 tcsetattr(0, TCSADRAIN, &saved_settings);
974 void sttybbs(int cmd)
975 { /* BSD version of sttybbs() */
977 static struct sgttyb saved_settings;
979 if ((cmd == 0) || (cmd == 1)) {
981 live.sg_flags |= CBREAK;
982 live.sg_flags |= CRMOD;
983 live.sg_flags |= NL1;
984 live.sg_flags &= ~ECHO;
986 live.sg_flags |= NOFLSH;
990 gtty(0, &saved_settings);
993 stty(0, &saved_settings);
1000 * display_help() - help file viewer
1002 void display_help(char *name)
1009 * fmout() - Citadel text formatter and paginator
1011 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
1012 /* screen width to use */
1013 /* file to read from, or NULL to read from server */
1014 /* nonzero if we should use the paginator */
1015 /* screen height to use */
1016 /* starting value for lines_printed, -1 for global */
1017 /* nonzero if we should use hypertext mode */
1019 int a, b, c, d, old;
1025 num_urls = 0; /* Start with a clean slate of embedded URL's */
1027 if (starting_lp >= 0) {
1028 lines_printed = starting_lp;
1033 c = 1; /* c is the current pos */
1038 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1042 if (fp != NULL) { /* read from file */
1045 if (eof_flag == 0) {
1047 buffer[strlen(buffer) + 1] = 0;
1048 buffer[strlen(buffer)] = a;
1050 } else { /* read from server */
1052 serv_gets(&buffer[d]);
1053 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1054 buffer[strlen(buffer) - 1] = 0;
1055 if (!strcmp(&buffer[d], "000")) {
1058 while (isspace(buffer[strlen(buffer) - 1]))
1059 buffer[strlen(buffer) - 1] = 0;
1067 if ( (!strncasecmp(buffer, "http://", 7))
1068 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1069 safestrncpy(urls[num_urls], buffer, 255);
1070 for (a=0; a<strlen(urls[num_urls]); ++a) {
1071 b = urls[num_urls][a];
1072 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1073 || (b==13) || (b==9) || (b=='\"') )
1074 urls[num_urls][a] = 0;
1079 buffer[strlen(buffer) + 1] = 0;
1081 strcpy(buffer, &buffer[1]);
1088 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1090 if (((old == 13) || (old == 10)) && (isspace(real))) {
1093 lines_printed = checkpagin(lines_printed, pagin, height);
1100 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1101 printf("\n%s", aaa);
1105 lines_printed = checkpagin(lines_printed, pagin, height);
1112 if ((strlen(aaa) + c) > (width - 5)) {
1116 lines_printed = checkpagin(lines_printed, pagin, height);
1120 c = c + strlen(aaa);
1124 if ((a == 13) || (a == 10)) {
1125 printf("%s\n", aaa);
1128 lines_printed = checkpagin(lines_printed, pagin, height);
1134 /* signal caught; drain the server */
1137 } while (strcmp(aaa, "000"));
1139 FMTEND:printf("\n");
1141 lines_printed = checkpagin(lines_printed, pagin, height);
1147 * support ANSI color if defined
1149 void color(int colornum)
1151 static int is_bold = 0;
1152 static int hold_color, current_color;
1154 if (colornum == COLOR_PUSH) {
1155 hold_color = current_color;
1159 if (colornum == COLOR_POP) {
1164 current_color = colornum;
1166 /* When switching to dim white, actually output an 'original
1167 * pair' sequence -- this looks better on black-on-white
1170 if (colornum == DIM_WHITE)
1171 printf("\033[39;49m");
1173 printf("\033[3%d;40m", (colornum & 7));
1175 if ((colornum >= 8) && (is_bold == 0)) {
1178 } else if ((colornum < 8) && (is_bold == 1)) {
1186 void cls(int colornum)
1189 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1196 * Detect whether ANSI color is available (answerback)
1198 void send_ansi_detect(void)
1200 if (rc_ansi_color == 2) {
1207 void look_for_ansi(void)
1215 if (rc_ansi_color == 0) {
1217 } else if (rc_ansi_color == 1) {
1219 } else if (rc_ansi_color == 2) {
1221 /* otherwise, do the auto-detect */
1226 if ((now - AnsiDetect) < 2)
1235 select(1, &rfds, NULL, NULL, &tv);
1236 if (FD_ISSET(0, &rfds)) {
1237 abuf[strlen(abuf) + 1] = 0;
1238 read(0, &abuf[strlen(abuf)], 1);
1240 } while (FD_ISSET(0, &rfds));
1242 for (a = 0; a < strlen(abuf); ++a) {
1243 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1244 && (abuf[a + 2] == '?')) {
1253 * Display key options (highlight hotkeys inside angle brackets)
1255 void keyopt(char *buf) {
1259 for (i=0; i<strlen(buf); ++i) {
1261 putc(buf[i], stdout);
1262 color(BRIGHT_MAGENTA);
1267 putc(buf[i], stdout);
1276 * Present a key-menu line choice type of thing
1278 char keymenu(char *menuprompt, char *menustring) {
1284 int display_prompt = 1;
1286 choices = num_tokens(menustring, '|');
1288 if (menuprompt != NULL) do_prompt = 1;
1289 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1292 if (display_prompt) {
1294 printf("%s ", menuprompt);
1297 for (i=0; i<choices; ++i) {
1298 extract(buf, menustring, i);
1308 if ( (do_prompt) && (ch=='?') ) {
1309 printf("\rOne of... ");
1311 for (i=0; i<choices; ++i) {
1312 extract(buf, menustring, i);
1321 for (i=0; i<choices; ++i) {
1322 extract(buf, menustring, i);
1323 for (c=1; c<strlen(buf); ++c) {
1324 if ( (ch == tolower(buf[c]))
1326 && (buf[c+1]=='>') ) {
1327 for (a=0; a<strlen(buf); ++a) {
1328 if ( (a!=(c-1)) && (a!=(c+1))) {
1329 putc(buf[a], stdout);