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 * pprintf() ... paginated version of printf()
89 void pprintf(const char *format, ...) {
91 static char buf[4096]; /* static for performance, change if needed */
94 va_start(arg_ptr, format);
95 vsprintf(buf, format, arg_ptr);
98 for (i=0; i<strlen(buf); ++i) {
102 lines_printed = checkpagin(lines_printed,
103 (userflags & US_PAGINATOR),
112 * print_express() - print express messages if there are any
114 void print_express(void)
119 if (express_msgs == 0)
127 if (strlen(rc_exp_cmd) > 0) {
128 outpipe = popen(rc_exp_cmd, "w");
129 if (outpipe != NULL) {
130 while (serv_gets(buf), strcmp(buf, "000")) {
131 fprintf(outpipe, "%s\n", buf);
137 /* fall back to built-in express message display */
143 while (serv_gets(buf), strcmp(buf, "000")) {
151 void set_keepalives(int s)
153 keepalives_enabled = (char) s;
157 * This loop handles the "keepalive" messages sent to the server when idling.
160 static time_t idlet = 0;
161 static void really_do_keepalive(void) {
165 if (keepalives_enabled != KA_NO) {
167 if (keepalives_enabled == KA_YES) {
171 if (ok_to_interrupt == 1) {
172 printf("\r%64s\r", "");
174 printf("%s%c ", room_name,
175 room_prompt(room_flags));
183 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
184 encapsulated interface; in theory there should be no need to touch these
185 globals outside of the async_ka_* functions. */
187 #ifdef THREADED_CLIENT
188 static pthread_t ka_thr_handle;
189 static int ka_thr_active = 0;
190 static int async_ka_enabled = 0;
192 static void *ka_thread(void *arg)
194 really_do_keepalive();
195 pthread_detach(ka_thr_handle);
200 /* start up a thread to handle a keepalive in the background */
201 static void async_ka_exec(void)
203 if (!ka_thr_active) {
205 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
206 perror("pthread_create");
211 #endif /* THREADED_CLIENT */
213 static void do_keepalive(void)
218 if ((now - idlet) < ((long) S_KEEPALIVE))
221 /* Do a space-backspace to keep telnet sessions from idling out */
225 #ifdef THREADED_CLIENT
226 if (async_ka_enabled)
230 really_do_keepalive();
234 /* Now the actual async-keepalve API that we expose to higher levels:
235 async_ka_start() and async_ka_end(). These do nothing when we don't have
236 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
238 /* wait for a background keepalive to complete. this must be done before
239 attempting any further server requests! */
240 void async_ka_end(void)
242 #ifdef THREADED_CLIENT
244 pthread_join(ka_thr_handle, NULL);
250 /* tell do_keepalive() that keepalives are asynchronous. */
251 void async_ka_start(void)
253 #ifdef THREADED_CLIENT
260 { /* get a character from the keyboard, with */
261 int a; /* the watchdog timer in effect if necessary */
264 time_t start_time, now;
273 /* This loop waits for keyboard input. If the keepalive
274 * timer expires, it sends a keepalive to the server if
275 * necessary and then waits again.
282 tv.tv_sec = S_KEEPALIVE;
286 if (((now - start_time) > SLEEPING)
287 && (SLEEPING != 0) && (getppid() == 1)) {
288 printf("Sleeping? Call again.\n");
291 select(1, &rfds, NULL, NULL, &tv);
292 } while (!FD_ISSET(0, &rfds));
297 /* At this point, there's input, so fetch it.
298 * (There's a hole in the bucket...)
308 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
309 && ((a < 32) || (a > 126)))
317 { /* Returns 1 for yes, 0 for no */
333 /* Returns 1 for yes, 0 for no, arg is default value */
356 /* Gets a line from the terminal */
357 /* string == Pointer to string buffer */
358 /* lim == Maximum length - if negative, no-show */
359 void getline(char *string, int lim)
373 if ((a == 8) && (strlen(string) == 0))
375 if ((a != 13) && (a != 8) && (strlen(string) == lim))
377 if ((a == 8) && (string[0] != 0)) {
378 string[strlen(string) - 1] = 0;
384 if ((a == 13) || (a == 10)) {
404 * strprompt() - prompt for a string, print the existing value and
405 * allow the user to press return to keep it...
407 void strprompt(char *prompt, char *str, int len)
412 printf("%s ", prompt);
415 color(BRIGHT_MAGENTA);
429 * boolprompt() - prompt for a yes/no, print the existing value and
430 * allow the user to press return to keep it...
432 int boolprompt(char *prompt, int prev_val)
437 printf("%s ", prompt);
440 color(BRIGHT_MAGENTA);
441 printf("%s", (prev_val ? "Yes" : "No"));
445 r = (yesno_d(prev_val));
451 * intprompt() - like strprompt(), except for an integer
452 * (note that it RETURNS the new value!)
454 int intprompt(char *prompt, int ival, int imin, int imax)
462 snprintf(buf, sizeof buf, "%d", i);
463 strprompt(prompt, buf, 15);
465 for (p=0; p<strlen(buf); ++p) {
466 if (!isdigit(buf[p])) i = imin - 1;
469 printf("*** Must be no less than %d.\n", imin);
471 printf("*** Must be no more than %d.\n", imax);
472 } while ((i < imin) || (i > imax));
477 * newprompt() - prompt for a string with no existing value
478 * (clears out string buffer first)
480 void newprompt(char *prompt, char *str, int len)
482 color(BRIGHT_MAGENTA);
483 printf("%s", prompt);
491 { /* returns a lower case value */
500 * parse the citadel.rc file
502 void load_command_set(void)
507 struct citcmd *lastcmd = NULL;
512 /* first, set up some defaults for non-required variables */
514 strcpy(editor_path, "");
515 strcpy(printcmd, "");
516 strcpy(rc_username, "");
517 strcpy(rc_password, "");
520 rc_allow_attachments = 0;
521 strcpy(rc_exp_cmd, "");
522 rc_display_message_numbers = 0;
523 rc_force_mail_prompts = 0;
525 strcpy(rc_url_cmd, "");
527 /* now try to open the citadel.rc file */
530 if (getenv("HOME") != NULL) {
531 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
532 ccfile = fopen(buf, "r");
534 if (ccfile == NULL) {
535 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
537 if (ccfile == NULL) {
538 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
539 ccfile = fopen(buf, "r");
541 if (ccfile == NULL) {
542 ccfile = fopen("./citadel.rc", "r");
544 if (ccfile == NULL) {
545 perror("commands: cannot open citadel.rc");
548 while (fgets(buf, 256, ccfile) != NULL) {
549 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
550 buf[strlen(buf) - 1] = 0;
552 if (!struncmp(buf, "editor=", 7))
553 strcpy(editor_path, &buf[7]);
555 if (!struncmp(buf, "printcmd=", 9))
556 strcpy(printcmd, &buf[9]);
558 if (!struncmp(buf, "expcmd=", 7))
559 strcpy(rc_exp_cmd, &buf[7]);
561 if (!struncmp(buf, "local_screen_dimensions=", 24))
562 have_xterm = (char) atoi(&buf[24]);
564 if (!struncmp(buf, "use_floors=", 11)) {
565 if (!strucmp(&buf[11], "yes"))
566 rc_floor_mode = RC_YES;
567 if (!strucmp(&buf[11], "no"))
568 rc_floor_mode = RC_NO;
569 if (!strucmp(&buf[11], "default"))
570 rc_floor_mode = RC_DEFAULT;
572 if (!struncmp(buf, "beep=", 5)) {
573 rc_exp_beep = atoi(&buf[5]);
575 if (!struncmp(buf, "allow_attachments=", 18)) {
576 rc_allow_attachments = atoi(&buf[18]);
578 if (!struncmp(buf, "display_message_numbers=", 24)) {
579 rc_display_message_numbers = atoi(&buf[24]);
581 if (!struncmp(buf, "force_mail_prompts=", 19)) {
582 rc_force_mail_prompts = atoi(&buf[19]);
584 if (!struncmp(buf, "ansi_color=", 11)) {
585 if (!struncmp(&buf[11], "on", 2))
587 if (!struncmp(&buf[11], "auto", 4))
588 rc_ansi_color = 2; /* autodetect */
589 if (!struncmp(&buf[11], "user", 4))
590 rc_ansi_color = 3; /* user config */
592 if (!struncmp(buf, "username=", 9))
593 strcpy(rc_username, &buf[9]);
595 if (!struncmp(buf, "password=", 9))
596 strcpy(rc_password, &buf[9]);
598 if (!struncmp(buf, "urlcmd=", 7))
599 strcpy(rc_url_cmd, &buf[7]);
601 if (!struncmp(buf, "cmd=", 4)) {
602 strcpy(buf, &buf[4]);
604 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
606 cptr->c_cmdnum = atoi(buf);
607 for (d = strlen(buf); d >= 0; --d)
610 strcpy(buf, &buf[b + 1]);
612 cptr->c_axlevel = atoi(buf);
613 for (d = strlen(buf); d >= 0; --d)
616 strcpy(buf, &buf[b + 1]);
618 for (a = 0; a < 5; ++a)
619 cptr->c_keys[a][0] = 0;
623 buf[strlen(buf) + 1] = 0;
624 while (strlen(buf) > 0) {
626 for (d = strlen(buf); d >= 0; --d)
629 strncpy(cptr->c_keys[a], buf, b);
630 cptr->c_keys[a][b] = 0;
632 strcpy(buf, &buf[b + 1]);
642 lastcmd->next = cptr;
652 * return the key associated with a command
654 char keycmd(char *cmdstr)
658 for (a = 0; a < strlen(cmdstr); ++a)
659 if (cmdstr[a] == '&')
660 return (tolower(cmdstr[a + 1]));
666 * Output the string from a key command without the ampersand
667 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
669 char *cmd_expand(char *strbuf, int mode)
677 for (a = 0; a < strlen(exp); ++a) {
678 if (strbuf[a] == '&') {
681 strcpy(&exp[a], &exp[a + 1]);
685 strcpy(buf, &exp[a + 2]);
691 if (!strncmp(&exp[a], "^r", 2)) {
693 strcpy(&exp[a], room_name);
694 strcat(exp, &buf[a + 2]);
696 if (!strncmp(&exp[a], "^c", 2)) {
698 strcpy(&exp[a + 1], &exp[a + 2]);
708 * Comparison function to determine if entered commands match a
709 * command loaded from the config file.
711 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
722 for (a = 0; a < ncomp; ++a) {
723 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
724 || (cptr->c_axlevel > cmdax))
732 * This function returns 1 if a given command requires a string input
734 int requires_string(struct citcmd *cptr, int ncomp)
739 strcpy(buf, cptr->c_keys[ncomp - 1]);
740 for (a = 0; a < strlen(buf); ++a) {
749 * Input a command at the main prompt.
750 * This function returns an integer command number. If the command prompts
751 * for a string then it is placed in the supplied buffer.
753 int getcmd(char *argbuf)
764 /* Switch color support on or off if we're in user mode */
765 if (rc_ansi_color == 3) {
766 if (userflags & US_COLOR)
771 /* if we're running in idiot mode, display a cute little menu */
772 IFNEXPERT formout("mainmenu");
774 print_express(); /* print express messages if there are any */
777 for (a = 0; a < 5; ++a)
779 /* now the room prompt... */
782 printf("\n%s", room_name);
784 printf("%c ", room_prompt(room_flags));
791 /* Handle the backspace key, but only if there's something
792 * to backspace over...
794 if ((ch == 8) && (cmdpos > 0)) {
795 back(cmdspaces[cmdpos - 1] + 1);
799 /* Spacebar invokes "lazy traversal" commands */
800 if ((ch == 32) && (cmdpos == 0)) {
801 this_lazy_cmd = next_lazy_cmd;
802 if (this_lazy_cmd == 13)
804 if (this_lazy_cmd == 5)
806 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
807 if (cptr->c_cmdnum == this_lazy_cmd) {
808 for (a = 0; a < 5; ++a)
809 if (cptr->c_keys[a][0] != 0)
810 printf("%s ", cmd_expand(
811 cptr->c_keys[a], 0));
813 return (this_lazy_cmd);
817 return (this_lazy_cmd);
819 /* Otherwise, process the command */
820 cmdbuf[cmdpos] = tolower(ch);
822 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
823 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
825 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
826 cmdspaces[cmdpos] = strlen(
827 cmd_expand(cptr->c_keys[cmdpos], 0));
829 if ((cptr->c_keys[cmdpos + 1]) != 0)
835 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
836 if (cmdmatch(cmdbuf, cptr, 5)) {
837 /* We've found our command. */
838 if (requires_string(cptr, cmdpos)) {
844 /* If this command is one that changes rooms,
845 * then the next lazy-command (space bar)
846 * should be "read new" instead of "goto"
848 if ((cptr->c_cmdnum == 5)
849 || (cptr->c_cmdnum == 6)
850 || (cptr->c_cmdnum == 47)
851 || (cptr->c_cmdnum == 52)
852 || (cptr->c_cmdnum == 16)
853 || (cptr->c_cmdnum == 20))
856 return (cptr->c_cmdnum);
862 printf("\rOne of ... \n");
863 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
864 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
865 for (a = 0; a < 5; ++a) {
866 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
872 printf("\n%s%c ", room_name, room_prompt(room_flags));
874 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
875 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
876 for (a = 0; a < cmdpos; ++a) {
878 cmd_expand(cptr->c_keys[a], 0));
893 * set tty modes. commands are:
895 * 0 - set to bbs mode, intr/quit disabled
896 * 1 - set to bbs mode, intr/quit enabled
897 * 2 - save current settings for later restoral
898 * 3 - restore saved settings
900 #ifdef HAVE_TERMIOS_H
901 void sttybbs(int cmd)
902 { /* SysV version of sttybbs() */
904 static struct termios saved_settings;
905 static int last_cmd = 0;
912 if ((cmd == 0) || (cmd == 1)) {
914 live.c_iflag = ISTRIP | IXON | IXANY;
915 live.c_oflag = OPOST | ONLCR;
916 live.c_lflag = ISIG | NOFLSH;
918 if (cmd == SB_YES_INTR) {
919 live.c_cc[VINTR] = NEXT_KEY;
920 live.c_cc[VQUIT] = STOP_KEY;
921 signal(SIGINT, *sighandler);
922 signal(SIGQUIT, *sighandler);
924 signal(SIGINT, SIG_IGN);
925 signal(SIGQUIT, SIG_IGN);
926 live.c_cc[VINTR] = (-1);
927 live.c_cc[VQUIT] = (-1);
930 /* do we even need this stuff anymore? */
932 live.c_cc[VERASE] = 8;
933 live.c_cc[VKILL] = 24;
935 live.c_cc[VEOL] = 255;
936 live.c_cc[VEOL2] = 0;
937 live.c_cc[VSTART] = 0;
938 tcsetattr(0, TCSADRAIN, &live);
941 tcgetattr(0, &saved_settings);
944 tcsetattr(0, TCSADRAIN, &saved_settings);
948 void sttybbs(int cmd)
949 { /* BSD version of sttybbs() */
951 static struct sgttyb saved_settings;
953 if ((cmd == 0) || (cmd == 1)) {
955 live.sg_flags |= CBREAK;
956 live.sg_flags |= CRMOD;
957 live.sg_flags |= NL1;
958 live.sg_flags &= ~ECHO;
960 live.sg_flags |= NOFLSH;
964 gtty(0, &saved_settings);
967 stty(0, &saved_settings);
974 * display_help() - help file viewer
976 void display_help(char *name)
983 * fmout() - Citadel text formatter and paginator
985 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp, char subst)
986 /* screen width to use */
987 /* file to read from, or NULL to read from server */
988 /* nonzero if we should use the paginator */
989 /* screen height to use */
990 /* starting value for lines_printed, -1 for global */
991 /* nonzero if we should use hypertext mode */
999 num_urls = 0; /* Start with a clean slate of embedded URL's */
1001 if (starting_lp >= 0) {
1002 lines_printed = starting_lp;
1007 c = 1; /* c is the current pos */
1012 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1016 if (fp != NULL) { /* read from file */
1019 if (eof_flag == 0) {
1021 buffer[strlen(buffer) + 1] = 0;
1022 buffer[strlen(buffer)] = a;
1024 } else { /* read from server */
1026 serv_gets(&buffer[d]);
1027 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1028 buffer[strlen(buffer) - 1] = 0;
1029 if (!strcmp(&buffer[d], "000")) {
1032 while (isspace(buffer[strlen(buffer) - 1]))
1033 buffer[strlen(buffer) - 1] = 0;
1041 if ( (!struncmp(buffer, "http://", 7))
1042 || (!struncmp(buffer, "ftp://", 6)) ) {
1043 safestrncpy(urls[num_urls], buffer, 255);
1044 for (a=0; a<strlen(urls[num_urls]); ++a) {
1045 b = urls[num_urls][a];
1046 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1047 || (b==13) || (b==9) || (b=='\"') )
1048 urls[num_urls][a] = 0;
1053 buffer[strlen(buffer) + 1] = 0;
1055 strcpy(buffer, &buffer[1]);
1062 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1064 if (((old == 13) || (old == 10)) && (isspace(real))) {
1067 lines_printed = checkpagin(lines_printed, pagin, height);
1074 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1075 printf("\n%s", aaa);
1079 lines_printed = checkpagin(lines_printed, pagin, height);
1086 if ((strlen(aaa) + c) > (width - 5)) {
1090 lines_printed = checkpagin(lines_printed, pagin, height);
1094 c = c + strlen(aaa);
1098 if ((a == 13) || (a == 10)) {
1099 printf("%s\n", aaa);
1102 lines_printed = checkpagin(lines_printed, pagin, height);
1108 /* signal caught; drain the server */
1111 } while (strcmp(aaa, "000"));
1113 FMTEND:printf("\n");
1115 lines_printed = checkpagin(lines_printed, pagin, height);
1121 * support ANSI color if defined
1123 void color(int colornum)
1125 static int is_bold = 0;
1126 static int hold_color, current_color;
1128 if (colornum == COLOR_PUSH) {
1129 hold_color = current_color;
1133 if (colornum == COLOR_POP) {
1138 current_color = colornum;
1140 /* When switching to dim white, actually output an 'original
1141 * pair' sequence -- this looks better on black-on-white
1144 if (colornum == DIM_WHITE)
1145 printf("\033[39;49m");
1147 printf("\033[3%d;40m", (colornum & 7));
1149 if ((colornum >= 8) && (is_bold == 0)) {
1152 } else if ((colornum < 8) && (is_bold == 1)) {
1160 void cls(int colornum)
1163 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1170 * Detect whether ANSI color is available (answerback)
1172 void send_ansi_detect(void)
1174 if (rc_ansi_color == 2) {
1181 void look_for_ansi(void)
1189 if (rc_ansi_color == 0) {
1191 } else if (rc_ansi_color == 1) {
1193 } else if (rc_ansi_color == 2) {
1195 /* otherwise, do the auto-detect */
1200 if ((now - AnsiDetect) < 2)
1209 select(1, &rfds, NULL, NULL, &tv);
1210 if (FD_ISSET(0, &rfds)) {
1211 abuf[strlen(abuf) + 1] = 0;
1212 read(0, &abuf[strlen(abuf)], 1);
1214 } while (FD_ISSET(0, &rfds));
1216 for (a = 0; a < strlen(abuf); ++a) {
1217 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1218 && (abuf[a + 2] == '?')) {
1227 * Display key options (highlight hotkeys inside angle brackets)
1229 void keyopt(char *buf) {
1233 for (i=0; i<strlen(buf); ++i) {
1235 putc(buf[i], stdout);
1236 color(BRIGHT_MAGENTA);
1241 putc(buf[i], stdout);
1250 * Present a key-menu line choice type of thing
1252 char keymenu(char *menuprompt, char *menustring) {
1258 int display_prompt = 1;
1260 choices = num_tokens(menustring, '|');
1262 if (menuprompt != NULL) do_prompt = 1;
1263 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1266 if (display_prompt) {
1268 printf("%s ", menuprompt);
1271 for (i=0; i<choices; ++i) {
1272 extract(buf, menustring, i);
1282 if ( (do_prompt) && (ch=='?') ) {
1283 printf("\rOne of... ");
1285 for (i=0; i<choices; ++i) {
1286 extract(buf, menustring, i);
1295 for (i=0; i<choices; ++i) {
1296 extract(buf, menustring, i);
1297 for (c=1; c<strlen(buf); ++c) {
1298 if ( (ch == tolower(buf[c]))
1300 && (buf[c+1]=='>') ) {
1301 for (a=0; a<strlen(buf); ++a) {
1302 if ( (a!=(c-1)) && (a!=(c+1))) {
1303 putc(buf[a], stdout);