4 * This file contains functions which implement parts of the
5 * text-mode user interface.
16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
39 #ifdef THREADED_CLIENT
49 #include "citadel_decls.h"
51 #include "routines2.h"
54 #include "client_chat.h"
66 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
70 char rc_exp_cmd[1024];
71 int rc_allow_attachments;
72 int rc_display_message_numbers;
73 int rc_force_mail_prompts;
74 int rc_remember_passwords;
77 char urls[MAXURLS][SIZ];
81 int next_lazy_cmd = 5;
83 int lines_printed = 0; /* line count for paginator */
84 extern int screenwidth, screenheight;
87 struct citcmd *cmdlist = NULL;
90 /* these variables are local to this module */
91 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
92 int ok_to_interrupt = 0; /* print express msgs asynchronously */
93 time_t AnsiDetect; /* when did we send the detect code? */
94 int enable_color = 0; /* nonzero for ANSI color */
100 * If an interesting key has been pressed, return its value, otherwise 0
102 char was_a_key_pressed(void) {
112 retval = select(1, &rfds, NULL, NULL, &tv);
114 /* Careful! Disable keepalives during keyboard polling; we're probably
115 * in the middle of a data transfer from the server, in which case
116 * sending a NOOP would throw the client protocol out of sync.
118 if (FD_ISSET(0, &rfds)) {
119 set_keepalives(KA_NO);
120 the_character = inkey();
121 set_keepalives(KA_YES);
126 return(the_character);
134 * Check to see if we need to pause at the end of a screen.
135 * If we do, we have to disable server keepalives during the pause because
136 * we are probably in the middle of a server operation and the NOOP command
137 * would confuse everything.
139 int checkpagin(int lp, int pagin, int height)
143 if (sigcaught) return(lp);
144 thekey = was_a_key_pressed();
145 if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
146 if (sigcaught) return(lp);
148 if (!pagin) return(0);
149 if (lp>=(height-1)) {
150 set_keepalives(KA_NO);
152 set_keepalives(KA_YES);
162 * pprintf() ... paginated version of printf()
164 void pprintf(const char *format, ...) {
166 static char buf[4096]; /* static for performance, change if needed */
169 /* If sigcaught is nonzero, a keypress has interrupted this and we
170 * should just drain output.
172 if (sigcaught) return;
174 /* Otherwise, start spewing... */
175 va_start(arg_ptr, format);
176 vsprintf(buf, format, arg_ptr);
179 for (i=0; i<strlen(buf); ++i) {
180 putc(buf[i], stdout);
183 lines_printed = checkpagin(lines_printed,
184 (userflags & US_PAGINATOR),
193 * print_express() - print express messages if there are any
195 void print_express(void)
205 if (express_msgs == 0)
211 if (strlen(rc_exp_cmd) == 0) {
216 while (express_msgs != 0) {
222 express_msgs = extract_int(&buf[4], 0);
223 timestamp = extract_long(&buf[4], 1);
224 flags = extract_int(&buf[4], 2);
225 extract(sender, &buf[4], 3);
226 extract(node, &buf[4], 4);
227 strcpy(last_paged, sender);
229 stamp = localtime(×tamp);
231 /* If the page is a Logoff Request, honor it. */
237 if (strlen(rc_exp_cmd) > 0) {
238 outpipe = popen(rc_exp_cmd, "w");
239 if (outpipe != NULL) {
240 /* Header derived from flags */
243 "Please log off now, as requested ");
245 fprintf(outpipe, "Broadcast message ");
247 fprintf(outpipe, "Chat request ");
249 fprintf(outpipe, "Message ");
250 /* Timestamp. Can this be improved? */
251 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
252 fprintf(outpipe, "at 12:%02d%cm",
254 stamp->tm_hour ? 'p' : 'a');
255 else if (stamp->tm_hour > 12) /* pm */
256 fprintf(outpipe, "at %d:%02dpm",
260 fprintf(outpipe, "at %d:%02dam",
261 stamp->tm_hour, stamp->tm_min);
262 fprintf(outpipe, " from %s", sender);
263 if (strncmp(serv_info.serv_nodename, node, 32))
264 fprintf(outpipe, " @%s", node);
265 fprintf(outpipe, ":\n");
266 while (serv_gets(buf), strcmp(buf, "000")) {
267 fprintf(outpipe, "%s\n", buf);
270 if (express_msgs == 0)
275 /* fall back to built-in express message display */
278 /* Header derived from flags */
280 printf("Please log off now, as requested ");
282 printf("Broadcast message ");
284 printf("Chat request ");
288 /* Timestamp. Can this be improved? */
289 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
290 printf("at 12:%02d%cm", stamp->tm_min,
291 stamp->tm_hour ? 'p' : 'a');
292 else if (stamp->tm_hour > 12) /* pm */
293 printf("at %d:%02dpm",
294 stamp->tm_hour - 12, stamp->tm_min);
296 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
299 printf(" from %s", sender);
301 /* Remote node, if any */
302 if (strncmp(serv_info.serv_nodename, node, 32))
303 printf(" @%s", node);
307 while (serv_gets(buf), strcmp(buf, "000")) {
318 void set_keepalives(int s)
320 keepalives_enabled = (char) s;
324 * This loop handles the "keepalive" messages sent to the server when idling.
327 static time_t idlet = 0;
328 static void really_do_keepalive(void) {
332 if (keepalives_enabled == KA_YES) {
337 if (ok_to_interrupt == 1) {
338 printf("\r%64s\r", "");
340 printf("%s%c ", room_name,
341 room_prompt(room_flags));
348 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
349 encapsulated interface; in theory there should be no need to touch these
350 globals outside of the async_ka_* functions. */
352 #ifdef THREADED_CLIENT
353 static pthread_t ka_thr_handle;
354 static int ka_thr_active = 0;
355 static int async_ka_enabled = 0;
357 static void *ka_thread(void *arg)
359 really_do_keepalive();
360 pthread_detach(ka_thr_handle);
365 /* start up a thread to handle a keepalive in the background */
366 static void async_ka_exec(void)
368 if (!ka_thr_active) {
370 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
371 perror("pthread_create");
376 #endif /* THREADED_CLIENT */
378 static void do_keepalive(void)
383 if ((now - idlet) < ((long) S_KEEPALIVE))
386 /* Do a space-backspace to keep telnet sessions from idling out */
390 #ifdef THREADED_CLIENT
391 if (async_ka_enabled)
395 really_do_keepalive();
399 /* Now the actual async-keepalve API that we expose to higher levels:
400 async_ka_start() and async_ka_end(). These do nothing when we don't have
401 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
403 /* wait for a background keepalive to complete. this must be done before
404 attempting any further server requests! */
405 void async_ka_end(void)
407 #ifdef THREADED_CLIENT
409 pthread_join(ka_thr_handle, NULL);
415 /* tell do_keepalive() that keepalives are asynchronous. */
416 void async_ka_start(void)
418 #ifdef THREADED_CLIENT
425 { /* get a character from the keyboard, with */
426 int a; /* the watchdog timer in effect if necessary */
429 time_t start_time, now;
438 /* This loop waits for keyboard input. If the keepalive
439 * timer expires, it sends a keepalive to the server if
440 * necessary and then waits again.
447 tv.tv_sec = S_KEEPALIVE;
451 select(1, &rfds, NULL, NULL, &tv);
452 } while (!FD_ISSET(0, &rfds));
457 /* At this point, there's input, so fetch it.
458 * (There's a hole in the bucket...)
468 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
469 && ((a < 32) || (a > 126)))
477 { /* Returns 1 for yes, 0 for no */
493 /* Returns 1 for yes, 0 for no, arg is default value */
516 /* Gets a line from the terminal */
517 /* string == Pointer to string buffer */
518 /* lim == Maximum length - if negative, no-show */
519 void getline(char *string, int lim)
533 if ((a == 8) && (strlen(string) == 0))
535 if ((a != 13) && (a != 8) && (strlen(string) == lim))
537 if ((a == 8) && (string[0] != 0)) {
538 string[strlen(string) - 1] = 0;
544 if ((a == 13) || (a == 10)) {
564 * strprompt() - prompt for a string, print the existing value and
565 * allow the user to press return to keep it...
567 void strprompt(char *prompt, char *str, int len)
572 printf("%s ", prompt);
575 color(BRIGHT_MAGENTA);
589 * boolprompt() - prompt for a yes/no, print the existing value and
590 * allow the user to press return to keep it...
592 int boolprompt(char *prompt, int prev_val)
597 printf("%s ", prompt);
600 color(BRIGHT_MAGENTA);
601 printf("%s", (prev_val ? "Yes" : "No"));
605 r = (yesno_d(prev_val));
611 * intprompt() - like strprompt(), except for an integer
612 * (note that it RETURNS the new value!)
614 int intprompt(char *prompt, int ival, int imin, int imax)
622 snprintf(buf, sizeof buf, "%d", i);
623 strprompt(prompt, buf, 15);
625 for (p=0; p<strlen(buf); ++p) {
626 if ( (!isdigit(buf[p]))
627 && ( (buf[p]!='-') || (p!=0) ) )
631 printf("*** Must be no less than %d.\n", imin);
633 printf("*** Must be no more than %d.\n", imax);
634 } while ((i < imin) || (i > imax));
639 * newprompt() - prompt for a string with no existing value
640 * (clears out string buffer first)
642 void newprompt(char *prompt, char *str, int len)
644 color(BRIGHT_MAGENTA);
645 printf("%s", prompt);
653 { /* returns a lower case value */
662 * parse the citadel.rc file
664 void load_command_set(void)
669 struct citcmd *lastcmd = NULL;
674 /* first, set up some defaults for non-required variables */
676 strcpy(editor_path, "");
677 strcpy(printcmd, "");
678 strcpy(rc_username, "");
679 strcpy(rc_password, "");
682 rc_allow_attachments = 0;
683 rc_remember_passwords = 0;
684 strcpy(rc_exp_cmd, "");
685 rc_display_message_numbers = 0;
686 rc_force_mail_prompts = 0;
688 strcpy(rc_url_cmd, "");
690 /* now try to open the citadel.rc file */
693 if (getenv("HOME") != NULL) {
694 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
695 ccfile = fopen(buf, "r");
697 if (ccfile == NULL) {
698 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
700 if (ccfile == NULL) {
701 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
702 ccfile = fopen(buf, "r");
704 if (ccfile == NULL) {
705 ccfile = fopen("./citadel.rc", "r");
707 if (ccfile == NULL) {
708 perror("commands: cannot open citadel.rc");
711 while (fgets(buf, sizeof buf, ccfile) != NULL) {
712 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
713 buf[strlen(buf) - 1] = 0;
715 if (!strncasecmp(buf, "editor=", 7))
716 strcpy(editor_path, &buf[7]);
718 if (!strncasecmp(buf, "printcmd=", 9))
719 strcpy(printcmd, &buf[9]);
721 if (!strncasecmp(buf, "expcmd=", 7))
722 strcpy(rc_exp_cmd, &buf[7]);
724 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
725 have_xterm = (char) atoi(&buf[24]);
727 if (!strncasecmp(buf, "use_floors=", 11)) {
728 if (!strcasecmp(&buf[11], "yes"))
729 rc_floor_mode = RC_YES;
730 if (!strcasecmp(&buf[11], "no"))
731 rc_floor_mode = RC_NO;
732 if (!strcasecmp(&buf[11], "default"))
733 rc_floor_mode = RC_DEFAULT;
735 if (!strncasecmp(buf, "beep=", 5)) {
736 rc_exp_beep = atoi(&buf[5]);
738 if (!strncasecmp(buf, "allow_attachments=", 18)) {
739 rc_allow_attachments = atoi(&buf[18]);
741 if (!strncasecmp(buf, "remember_passwords=", 19)) {
742 rc_remember_passwords = atoi(&buf[19]);
744 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
745 rc_display_message_numbers = atoi(&buf[24]);
747 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
748 rc_force_mail_prompts = atoi(&buf[19]);
750 if (!strncasecmp(buf, "ansi_color=", 11)) {
751 if (!strncasecmp(&buf[11], "on", 2))
753 if (!strncasecmp(&buf[11], "auto", 4))
754 rc_ansi_color = 2; /* autodetect */
755 if (!strncasecmp(&buf[11], "user", 4))
756 rc_ansi_color = 3; /* user config */
758 if (!strncasecmp(buf, "username=", 9))
759 strcpy(rc_username, &buf[9]);
761 if (!strncasecmp(buf, "password=", 9))
762 strcpy(rc_password, &buf[9]);
764 if (!strncasecmp(buf, "urlcmd=", 7))
765 strcpy(rc_url_cmd, &buf[7]);
767 if (!strncasecmp(buf, "cmd=", 4)) {
768 strcpy(buf, &buf[4]);
770 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
772 cptr->c_cmdnum = atoi(buf);
773 for (d = strlen(buf); d >= 0; --d)
776 strcpy(buf, &buf[b + 1]);
778 cptr->c_axlevel = atoi(buf);
779 for (d = strlen(buf); d >= 0; --d)
782 strcpy(buf, &buf[b + 1]);
784 for (a = 0; a < 5; ++a)
785 cptr->c_keys[a][0] = 0;
789 buf[strlen(buf) + 1] = 0;
790 while (strlen(buf) > 0) {
792 for (d = strlen(buf); d >= 0; --d)
795 strncpy(cptr->c_keys[a], buf, b);
796 cptr->c_keys[a][b] = 0;
798 strcpy(buf, &buf[b + 1]);
808 lastcmd->next = cptr;
818 * return the key associated with a command
820 char keycmd(char *cmdstr)
824 for (a = 0; a < strlen(cmdstr); ++a)
825 if (cmdstr[a] == '&')
826 return (tolower(cmdstr[a + 1]));
832 * Output the string from a key command without the ampersand
833 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
835 char *cmd_expand(char *strbuf, int mode)
843 for (a = 0; a < strlen(exp); ++a) {
844 if (strbuf[a] == '&') {
847 strcpy(&exp[a], &exp[a + 1]);
851 strcpy(buf, &exp[a + 2]);
857 if (!strncmp(&exp[a], "^r", 2)) {
859 strcpy(&exp[a], room_name);
860 strcat(exp, &buf[a + 2]);
862 if (!strncmp(&exp[a], "^c", 2)) {
864 strcpy(&exp[a + 1], &exp[a + 2]);
874 * Comparison function to determine if entered commands match a
875 * command loaded from the config file.
877 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
888 for (a = 0; a < ncomp; ++a) {
889 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
890 || (cptr->c_axlevel > cmdax))
898 * This function returns 1 if a given command requires a string input
900 int requires_string(struct citcmd *cptr, int ncomp)
905 strcpy(buf, cptr->c_keys[ncomp - 1]);
906 for (a = 0; a < strlen(buf); ++a) {
915 * Input a command at the main prompt.
916 * This function returns an integer command number. If the command prompts
917 * for a string then it is placed in the supplied buffer.
919 int getcmd(char *argbuf)
931 * Starting a new command now, so set sigcaught to 0. This variable
932 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
933 * been interrupted by a keypress.
937 /* Switch color support on or off if we're in user mode */
938 if (rc_ansi_color == 3) {
939 if (userflags & US_COLOR)
944 /* if we're running in idiot mode, display a cute little menu */
945 IFNEXPERT formout("mainmenu");
947 print_express(); /* print express messages if there are any */
950 for (a = 0; a < 5; ++a)
952 /* now the room prompt... */
955 printf("\n%s", room_name);
957 printf("%c ", room_prompt(room_flags));
964 /* Handle the backspace key, but only if there's something
965 * to backspace over...
967 if ((ch == 8) && (cmdpos > 0)) {
968 back(cmdspaces[cmdpos - 1] + 1);
972 /* Spacebar invokes "lazy traversal" commands */
973 if ((ch == 32) && (cmdpos == 0)) {
974 this_lazy_cmd = next_lazy_cmd;
975 if (this_lazy_cmd == 13)
977 if (this_lazy_cmd == 5)
979 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
980 if (cptr->c_cmdnum == this_lazy_cmd) {
981 for (a = 0; a < 5; ++a)
982 if (cptr->c_keys[a][0] != 0)
983 printf("%s ", cmd_expand(
984 cptr->c_keys[a], 0));
986 return (this_lazy_cmd);
990 return (this_lazy_cmd);
992 /* Otherwise, process the command */
993 cmdbuf[cmdpos] = tolower(ch);
995 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
996 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
998 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
999 cmdspaces[cmdpos] = strlen(
1000 cmd_expand(cptr->c_keys[cmdpos], 0));
1002 if ((cptr->c_keys[cmdpos + 1]) != 0)
1008 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1009 if (cmdmatch(cmdbuf, cptr, 5)) {
1010 /* We've found our command. */
1011 if (requires_string(cptr, cmdpos)) {
1012 getline(argbuf, 32);
1017 /* If this command is one that changes rooms,
1018 * then the next lazy-command (space bar)
1019 * should be "read new" instead of "goto"
1021 if ((cptr->c_cmdnum == 5)
1022 || (cptr->c_cmdnum == 6)
1023 || (cptr->c_cmdnum == 47)
1024 || (cptr->c_cmdnum == 52)
1025 || (cptr->c_cmdnum == 16)
1026 || (cptr->c_cmdnum == 20))
1029 return (cptr->c_cmdnum);
1035 pprintf("\rOne of ... \n");
1036 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1037 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1038 for (a = 0; a < 5; ++a) {
1039 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1045 pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1047 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1048 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1049 for (a = 0; a < cmdpos; ++a) {
1051 cmd_expand(cptr->c_keys[a], 0));
1066 * set tty modes. commands are:
1068 * 01- set to bbs mode
1069 * 2 - save current settings for later restoral
1070 * 3 - restore saved settings
1072 #ifdef HAVE_TERMIOS_H
1073 void sttybbs(int cmd)
1074 { /* SysV version of sttybbs() */
1075 struct termios live;
1076 static struct termios saved_settings;
1077 static int last_cmd = 0;
1084 if ((cmd == 0) || (cmd == 1)) {
1085 tcgetattr(0, &live);
1086 live.c_iflag = ISTRIP | IXON | IXANY;
1087 live.c_oflag = OPOST | ONLCR;
1088 live.c_lflag = ISIG | NOFLSH;
1090 live.c_cc[VINTR] = (-1);
1091 live.c_cc[VQUIT] = (-1);
1094 live.c_cc[VMIN] = 0;
1095 live.c_cc[VTIME] = 0;
1098 /* do we even need this stuff anymore? */
1099 /* live.c_line=0; */
1100 live.c_cc[VERASE] = 8;
1101 live.c_cc[VKILL] = 24;
1102 live.c_cc[VEOF] = 1;
1103 live.c_cc[VEOL] = 255;
1104 live.c_cc[VEOL2] = 0;
1105 live.c_cc[VSTART] = 0;
1106 tcsetattr(0, TCSADRAIN, &live);
1109 tcgetattr(0, &saved_settings);
1112 tcsetattr(0, TCSADRAIN, &saved_settings);
1116 void sttybbs(int cmd)
1117 { /* BSD version of sttybbs() */
1119 static struct sgttyb saved_settings;
1121 if ((cmd == 0) || (cmd == 1)) {
1123 live.sg_flags |= CBREAK;
1124 live.sg_flags |= CRMOD;
1125 live.sg_flags |= NL1;
1126 live.sg_flags &= ~ECHO;
1128 live.sg_flags |= NOFLSH;
1132 gtty(0, &saved_settings);
1135 stty(0, &saved_settings);
1142 * display_help() - help file viewer
1144 void display_help(char *name)
1151 * fmout() - Citadel text formatter and paginator
1154 int width, /* screen width to use */
1155 FILE *fp, /* file to read from, or NULL to read from server */
1156 char pagin, /* nonzero if we should use the paginator */
1157 int height, /* screen height to use */
1158 int starting_lp,/* starting value for lines_printed, -1 for global */
1159 char subst) /* nonzero if we should use hypertext mode */
1161 int a, b, c, d, old;
1167 num_urls = 0; /* Start with a clean slate of embedded URL's */
1169 if (starting_lp >= 0) {
1170 lines_printed = starting_lp;
1175 c = 1; /* c is the current pos */
1177 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1178 if (fp != NULL) { /* read from file */
1181 if (eof_flag == 0) {
1183 buffer[strlen(buffer) + 1] = 0;
1184 buffer[strlen(buffer)] = a;
1186 } else { /* read from server */
1188 serv_gets(&buffer[d]);
1189 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1190 buffer[strlen(buffer) - 1] = 0;
1191 if (!strcmp(&buffer[d], "000")) {
1194 while (isspace(buffer[strlen(buffer) - 1]))
1195 buffer[strlen(buffer) - 1] = 0;
1203 if ( (!strncasecmp(buffer, "http://", 7))
1204 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1205 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1206 for (a=0; a<strlen(urls[num_urls]); ++a) {
1207 b = urls[num_urls][a];
1208 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1209 || (b==13) || (b==9) || (b=='\"') )
1210 urls[num_urls][a] = 0;
1215 buffer[strlen(buffer) + 1] = 0;
1217 strcpy(buffer, &buffer[1]);
1224 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1226 if (((old == 13) || (old == 10)) && (isspace(real))) {
1229 lines_printed = checkpagin(lines_printed, pagin, height);
1236 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1237 printf("\n%s", aaa);
1241 lines_printed = checkpagin(lines_printed, pagin, height);
1248 if ((strlen(aaa) + c) > (width - 5)) {
1252 lines_printed = checkpagin(lines_printed, pagin, height);
1256 c = c + strlen(aaa);
1260 if ((a == 13) || (a == 10)) {
1261 printf("%s\n", aaa);
1264 lines_printed = checkpagin(lines_printed, pagin, height);
1265 if (sigcaught) goto OOPS;
1271 /* keypress caught; drain the server */
1274 } while (strcmp(aaa, "000"));
1276 FMTEND: printf("\n");
1278 lines_printed = checkpagin(lines_printed, pagin, height);
1284 * support ANSI color if defined
1286 void color(int colornum)
1288 static int is_bold = 0;
1289 static int hold_color, current_color;
1291 if (colornum == COLOR_PUSH) {
1292 hold_color = current_color;
1296 if (colornum == COLOR_POP) {
1301 current_color = colornum;
1303 /* When switching to dim white, actually output an 'original
1304 * pair' sequence -- this looks better on black-on-white
1307 if (colornum == DIM_WHITE)
1308 printf("\033[39;49m");
1310 printf("\033[3%d;40m", (colornum & 7));
1312 if ((colornum >= 8) && (is_bold == 0)) {
1315 } else if ((colornum < 8) && (is_bold == 1)) {
1323 void cls(int colornum)
1326 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1333 * Detect whether ANSI color is available (answerback)
1335 void send_ansi_detect(void)
1337 if (rc_ansi_color == 2) {
1344 void look_for_ansi(void)
1352 if (rc_ansi_color == 0) {
1354 } else if (rc_ansi_color == 1) {
1356 } else if (rc_ansi_color == 2) {
1358 /* otherwise, do the auto-detect */
1363 if ((now - AnsiDetect) < 2)
1372 select(1, &rfds, NULL, NULL, &tv);
1373 if (FD_ISSET(0, &rfds)) {
1374 abuf[strlen(abuf) + 1] = 0;
1375 read(0, &abuf[strlen(abuf)], 1);
1377 } while (FD_ISSET(0, &rfds));
1379 for (a = 0; a < strlen(abuf); ++a) {
1380 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1381 && (abuf[a + 2] == '?')) {
1390 * Display key options (highlight hotkeys inside angle brackets)
1392 void keyopt(char *buf) {
1396 for (i=0; i<strlen(buf); ++i) {
1398 putc(buf[i], stdout);
1399 color(BRIGHT_MAGENTA);
1404 putc(buf[i], stdout);
1413 * Present a key-menu line choice type of thing
1415 char keymenu(char *menuprompt, char *menustring) {
1421 int display_prompt = 1;
1423 choices = num_tokens(menustring, '|');
1425 if (menuprompt != NULL) do_prompt = 1;
1426 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1429 if (display_prompt) {
1431 printf("%s ", menuprompt);
1434 for (i=0; i<choices; ++i) {
1435 extract(buf, menustring, i);
1445 if ( (do_prompt) && (ch=='?') ) {
1446 printf("\rOne of... ");
1448 for (i=0; i<choices; ++i) {
1449 extract(buf, menustring, i);
1458 for (i=0; i<choices; ++i) {
1459 extract(buf, menustring, i);
1460 for (c=1; c<strlen(buf); ++c) {
1461 if ( (ch == tolower(buf[c]))
1463 && (buf[c+1]=='>') ) {
1464 for (a=0; a<strlen(buf); ++a) {
1465 if ( (a!=(c-1)) && (a!=(c+1))) {
1466 putc(buf[a], stdout);