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"
44 #include "client_chat.h"
56 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
60 char rc_exp_cmd[1024];
61 int rc_allow_attachments;
62 int rc_display_message_numbers;
63 int rc_force_mail_prompts;
64 int rc_remember_passwords;
67 char urls[MAXURLS][SIZ];
71 int next_lazy_cmd = 5;
73 int lines_printed = 0; /* line count for paginator */
74 extern int screenwidth, screenheight;
77 struct citcmd *cmdlist = NULL;
80 /* these variables are local to this module */
81 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
82 int ok_to_interrupt = 0; /* print express msgs asynchronously */
83 time_t AnsiDetect; /* when did we send the detect code? */
84 int enable_color = 0; /* nonzero for ANSI color */
90 * If an interesting key has been pressed, return its value, otherwise 0
92 char was_a_key_pressed(void) {
102 retval = select(1, &rfds, NULL, NULL, &tv);
104 /* Careful! Disable keepalives during keyboard polling; we're probably
105 * in the middle of a data transfer from the server, in which case
106 * sending a NOOP would throw the client protocol out of sync.
108 if (FD_ISSET(0, &rfds)) {
109 set_keepalives(KA_NO);
110 the_character = inkey();
111 set_keepalives(KA_YES);
116 return(the_character);
124 * Check to see if we need to pause at the end of a screen.
125 * If we do, we have to disable server keepalives during the pause because
126 * we are probably in the middle of a server operation and the NOOP command
127 * would confuse everything.
129 int checkpagin(int lp, int pagin, int height)
133 if (sigcaught) return(lp);
134 thekey = was_a_key_pressed();
135 if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
136 if (sigcaught) return(lp);
138 if (!pagin) return(0);
139 if (lp>=(height-1)) {
140 set_keepalives(KA_NO);
142 set_keepalives(KA_YES);
152 * pprintf() ... paginated version of printf()
154 void pprintf(const char *format, ...) {
156 static char buf[4096]; /* static for performance, change if needed */
159 /* If sigcaught is nonzero, a keypress has interrupted this and we
160 * should just drain output.
162 if (sigcaught) return;
164 /* Otherwise, start spewing... */
165 va_start(arg_ptr, format);
166 vsprintf(buf, format, arg_ptr);
169 for (i=0; i<strlen(buf); ++i) {
170 putc(buf[i], stdout);
173 lines_printed = checkpagin(lines_printed,
174 (userflags & US_PAGINATOR),
183 * print_express() - print express messages if there are any
185 void print_express(void)
195 if (express_msgs == 0)
201 if (strlen(rc_exp_cmd) == 0) {
206 while (express_msgs != 0) {
212 express_msgs = extract_int(&buf[4], 0);
213 timestamp = extract_long(&buf[4], 1);
214 flags = extract_int(&buf[4], 2);
215 extract(sender, &buf[4], 3);
216 extract(node, &buf[4], 4);
217 strcpy(last_paged, sender);
219 stamp = localtime(×tamp);
221 /* If the page is a Logoff Request, honor it. */
227 if (strlen(rc_exp_cmd) > 0) {
228 outpipe = popen(rc_exp_cmd, "w");
229 if (outpipe != NULL) {
230 /* Header derived from flags */
233 "Please log off now, as requested ");
235 fprintf(outpipe, "Broadcast message ");
237 fprintf(outpipe, "Chat request ");
239 fprintf(outpipe, "Message ");
240 /* Timestamp. Can this be improved? */
241 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
242 fprintf(outpipe, "at 12:%02d%cm",
244 stamp->tm_hour ? 'p' : 'a');
245 else if (stamp->tm_hour > 12) /* pm */
246 fprintf(outpipe, "at %d:%02dpm",
250 fprintf(outpipe, "at %d:%02dam",
251 stamp->tm_hour, stamp->tm_min);
252 fprintf(outpipe, " from %s", sender);
253 if (strncmp(serv_info.serv_nodename, node, 32))
254 fprintf(outpipe, " @%s", node);
255 fprintf(outpipe, ":\n");
256 while (serv_gets(buf), strcmp(buf, "000")) {
257 fprintf(outpipe, "%s\n", buf);
260 if (express_msgs == 0)
265 /* fall back to built-in express message display */
268 /* Header derived from flags */
270 printf("Please log off now, as requested ");
272 printf("Broadcast message ");
274 printf("Chat request ");
278 /* Timestamp. Can this be improved? */
279 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
280 printf("at 12:%02d%cm", stamp->tm_min,
281 stamp->tm_hour ? 'p' : 'a');
282 else if (stamp->tm_hour > 12) /* pm */
283 printf("at %d:%02dpm",
284 stamp->tm_hour - 12, stamp->tm_min);
286 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
289 printf(" from %s", sender);
291 /* Remote node, if any */
292 if (strncmp(serv_info.serv_nodename, node, 32))
293 printf(" @%s", node);
297 while (serv_gets(buf), strcmp(buf, "000")) {
308 void set_keepalives(int s)
310 keepalives_enabled = (char) s;
314 * This loop handles the "keepalive" messages sent to the server when idling.
317 static time_t idlet = 0;
318 static void really_do_keepalive(void) {
322 if (keepalives_enabled == KA_YES) {
327 if (ok_to_interrupt == 1) {
328 printf("\r%64s\r", "");
330 printf("%s%c ", room_name,
331 room_prompt(room_flags));
338 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
339 encapsulated interface; in theory there should be no need to touch these
340 globals outside of the async_ka_* functions. */
342 #ifdef THREADED_CLIENT
343 static pthread_t ka_thr_handle;
344 static int ka_thr_active = 0;
345 static int async_ka_enabled = 0;
347 static void *ka_thread(void *arg)
349 really_do_keepalive();
350 pthread_detach(ka_thr_handle);
355 /* start up a thread to handle a keepalive in the background */
356 static void async_ka_exec(void)
358 if (!ka_thr_active) {
360 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
361 perror("pthread_create");
366 #endif /* THREADED_CLIENT */
368 static void do_keepalive(void)
373 if ((now - idlet) < ((long) S_KEEPALIVE))
376 /* Do a space-backspace to keep telnet sessions from idling out */
380 #ifdef THREADED_CLIENT
381 if (async_ka_enabled)
385 really_do_keepalive();
389 /* Now the actual async-keepalve API that we expose to higher levels:
390 async_ka_start() and async_ka_end(). These do nothing when we don't have
391 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
393 /* wait for a background keepalive to complete. this must be done before
394 attempting any further server requests! */
395 void async_ka_end(void)
397 #ifdef THREADED_CLIENT
399 pthread_join(ka_thr_handle, NULL);
405 /* tell do_keepalive() that keepalives are asynchronous. */
406 void async_ka_start(void)
408 #ifdef THREADED_CLIENT
415 { /* get a character from the keyboard, with */
416 int a; /* the watchdog timer in effect if necessary */
419 time_t start_time, now;
428 /* This loop waits for keyboard input. If the keepalive
429 * timer expires, it sends a keepalive to the server if
430 * necessary and then waits again.
437 tv.tv_sec = S_KEEPALIVE;
441 if (((now - start_time) > SLEEPING)
442 && (SLEEPING != 0) && (getppid() == 1)) {
443 printf("Sleeping? Call again.\n");
446 select(1, &rfds, NULL, NULL, &tv);
447 } while (!FD_ISSET(0, &rfds));
452 /* At this point, there's input, so fetch it.
453 * (There's a hole in the bucket...)
463 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
464 && ((a < 32) || (a > 126)))
472 { /* Returns 1 for yes, 0 for no */
488 /* Returns 1 for yes, 0 for no, arg is default value */
511 /* Gets a line from the terminal */
512 /* string == Pointer to string buffer */
513 /* lim == Maximum length - if negative, no-show */
514 void getline(char *string, int lim)
528 if ((a == 8) && (strlen(string) == 0))
530 if ((a != 13) && (a != 8) && (strlen(string) == lim))
532 if ((a == 8) && (string[0] != 0)) {
533 string[strlen(string) - 1] = 0;
539 if ((a == 13) || (a == 10)) {
559 * strprompt() - prompt for a string, print the existing value and
560 * allow the user to press return to keep it...
562 void strprompt(char *prompt, char *str, int len)
567 printf("%s ", prompt);
570 color(BRIGHT_MAGENTA);
584 * boolprompt() - prompt for a yes/no, print the existing value and
585 * allow the user to press return to keep it...
587 int boolprompt(char *prompt, int prev_val)
592 printf("%s ", prompt);
595 color(BRIGHT_MAGENTA);
596 printf("%s", (prev_val ? "Yes" : "No"));
600 r = (yesno_d(prev_val));
606 * intprompt() - like strprompt(), except for an integer
607 * (note that it RETURNS the new value!)
609 int intprompt(char *prompt, int ival, int imin, int imax)
617 snprintf(buf, sizeof buf, "%d", i);
618 strprompt(prompt, buf, 15);
620 for (p=0; p<strlen(buf); ++p) {
621 if ( (!isdigit(buf[p]))
622 && ( (buf[p]!='-') || (p!=0) ) )
626 printf("*** Must be no less than %d.\n", imin);
628 printf("*** Must be no more than %d.\n", imax);
629 } while ((i < imin) || (i > imax));
634 * newprompt() - prompt for a string with no existing value
635 * (clears out string buffer first)
637 void newprompt(char *prompt, char *str, int len)
639 color(BRIGHT_MAGENTA);
640 printf("%s", prompt);
648 { /* returns a lower case value */
657 * parse the citadel.rc file
659 void load_command_set(void)
664 struct citcmd *lastcmd = NULL;
669 /* first, set up some defaults for non-required variables */
671 strcpy(editor_path, "");
672 strcpy(printcmd, "");
673 strcpy(rc_username, "");
674 strcpy(rc_password, "");
677 rc_allow_attachments = 0;
678 rc_remember_passwords = 0;
679 strcpy(rc_exp_cmd, "");
680 rc_display_message_numbers = 0;
681 rc_force_mail_prompts = 0;
683 strcpy(rc_url_cmd, "");
685 /* now try to open the citadel.rc file */
688 if (getenv("HOME") != NULL) {
689 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
690 ccfile = fopen(buf, "r");
692 if (ccfile == NULL) {
693 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
695 if (ccfile == NULL) {
696 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
697 ccfile = fopen(buf, "r");
699 if (ccfile == NULL) {
700 ccfile = fopen("./citadel.rc", "r");
702 if (ccfile == NULL) {
703 perror("commands: cannot open citadel.rc");
706 while (fgets(buf, sizeof buf, ccfile) != NULL) {
707 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
708 buf[strlen(buf) - 1] = 0;
710 if (!strncasecmp(buf, "editor=", 7))
711 strcpy(editor_path, &buf[7]);
713 if (!strncasecmp(buf, "printcmd=", 9))
714 strcpy(printcmd, &buf[9]);
716 if (!strncasecmp(buf, "expcmd=", 7))
717 strcpy(rc_exp_cmd, &buf[7]);
719 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
720 have_xterm = (char) atoi(&buf[24]);
722 if (!strncasecmp(buf, "use_floors=", 11)) {
723 if (!strcasecmp(&buf[11], "yes"))
724 rc_floor_mode = RC_YES;
725 if (!strcasecmp(&buf[11], "no"))
726 rc_floor_mode = RC_NO;
727 if (!strcasecmp(&buf[11], "default"))
728 rc_floor_mode = RC_DEFAULT;
730 if (!strncasecmp(buf, "beep=", 5)) {
731 rc_exp_beep = atoi(&buf[5]);
733 if (!strncasecmp(buf, "allow_attachments=", 18)) {
734 rc_allow_attachments = atoi(&buf[18]);
736 if (!strncasecmp(buf, "remember_passwords=", 19)) {
737 rc_remember_passwords = atoi(&buf[19]);
739 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
740 rc_display_message_numbers = atoi(&buf[24]);
742 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
743 rc_force_mail_prompts = atoi(&buf[19]);
745 if (!strncasecmp(buf, "ansi_color=", 11)) {
746 if (!strncasecmp(&buf[11], "on", 2))
748 if (!strncasecmp(&buf[11], "auto", 4))
749 rc_ansi_color = 2; /* autodetect */
750 if (!strncasecmp(&buf[11], "user", 4))
751 rc_ansi_color = 3; /* user config */
753 if (!strncasecmp(buf, "username=", 9))
754 strcpy(rc_username, &buf[9]);
756 if (!strncasecmp(buf, "password=", 9))
757 strcpy(rc_password, &buf[9]);
759 if (!strncasecmp(buf, "urlcmd=", 7))
760 strcpy(rc_url_cmd, &buf[7]);
762 if (!strncasecmp(buf, "cmd=", 4)) {
763 strcpy(buf, &buf[4]);
765 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
767 cptr->c_cmdnum = atoi(buf);
768 for (d = strlen(buf); d >= 0; --d)
771 strcpy(buf, &buf[b + 1]);
773 cptr->c_axlevel = atoi(buf);
774 for (d = strlen(buf); d >= 0; --d)
777 strcpy(buf, &buf[b + 1]);
779 for (a = 0; a < 5; ++a)
780 cptr->c_keys[a][0] = 0;
784 buf[strlen(buf) + 1] = 0;
785 while (strlen(buf) > 0) {
787 for (d = strlen(buf); d >= 0; --d)
790 strncpy(cptr->c_keys[a], buf, b);
791 cptr->c_keys[a][b] = 0;
793 strcpy(buf, &buf[b + 1]);
803 lastcmd->next = cptr;
813 * return the key associated with a command
815 char keycmd(char *cmdstr)
819 for (a = 0; a < strlen(cmdstr); ++a)
820 if (cmdstr[a] == '&')
821 return (tolower(cmdstr[a + 1]));
827 * Output the string from a key command without the ampersand
828 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
830 char *cmd_expand(char *strbuf, int mode)
838 for (a = 0; a < strlen(exp); ++a) {
839 if (strbuf[a] == '&') {
842 strcpy(&exp[a], &exp[a + 1]);
846 strcpy(buf, &exp[a + 2]);
852 if (!strncmp(&exp[a], "^r", 2)) {
854 strcpy(&exp[a], room_name);
855 strcat(exp, &buf[a + 2]);
857 if (!strncmp(&exp[a], "^c", 2)) {
859 strcpy(&exp[a + 1], &exp[a + 2]);
869 * Comparison function to determine if entered commands match a
870 * command loaded from the config file.
872 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
883 for (a = 0; a < ncomp; ++a) {
884 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
885 || (cptr->c_axlevel > cmdax))
893 * This function returns 1 if a given command requires a string input
895 int requires_string(struct citcmd *cptr, int ncomp)
900 strcpy(buf, cptr->c_keys[ncomp - 1]);
901 for (a = 0; a < strlen(buf); ++a) {
910 * Input a command at the main prompt.
911 * This function returns an integer command number. If the command prompts
912 * for a string then it is placed in the supplied buffer.
914 int getcmd(char *argbuf)
926 * Starting a new command now, so set sigcaught to 0. This variable
927 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
928 * been interrupted by a keypress.
932 /* Switch color support on or off if we're in user mode */
933 if (rc_ansi_color == 3) {
934 if (userflags & US_COLOR)
939 /* if we're running in idiot mode, display a cute little menu */
940 IFNEXPERT formout("mainmenu");
942 print_express(); /* print express messages if there are any */
945 for (a = 0; a < 5; ++a)
947 /* now the room prompt... */
950 printf("\n%s", room_name);
952 printf("%c ", room_prompt(room_flags));
959 /* Handle the backspace key, but only if there's something
960 * to backspace over...
962 if ((ch == 8) && (cmdpos > 0)) {
963 back(cmdspaces[cmdpos - 1] + 1);
967 /* Spacebar invokes "lazy traversal" commands */
968 if ((ch == 32) && (cmdpos == 0)) {
969 this_lazy_cmd = next_lazy_cmd;
970 if (this_lazy_cmd == 13)
972 if (this_lazy_cmd == 5)
974 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
975 if (cptr->c_cmdnum == this_lazy_cmd) {
976 for (a = 0; a < 5; ++a)
977 if (cptr->c_keys[a][0] != 0)
978 printf("%s ", cmd_expand(
979 cptr->c_keys[a], 0));
981 return (this_lazy_cmd);
985 return (this_lazy_cmd);
987 /* Otherwise, process the command */
988 cmdbuf[cmdpos] = tolower(ch);
990 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
991 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
993 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
994 cmdspaces[cmdpos] = strlen(
995 cmd_expand(cptr->c_keys[cmdpos], 0));
997 if ((cptr->c_keys[cmdpos + 1]) != 0)
1003 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1004 if (cmdmatch(cmdbuf, cptr, 5)) {
1005 /* We've found our command. */
1006 if (requires_string(cptr, cmdpos)) {
1007 getline(argbuf, 32);
1012 /* If this command is one that changes rooms,
1013 * then the next lazy-command (space bar)
1014 * should be "read new" instead of "goto"
1016 if ((cptr->c_cmdnum == 5)
1017 || (cptr->c_cmdnum == 6)
1018 || (cptr->c_cmdnum == 47)
1019 || (cptr->c_cmdnum == 52)
1020 || (cptr->c_cmdnum == 16)
1021 || (cptr->c_cmdnum == 20))
1024 return (cptr->c_cmdnum);
1030 printf("\rOne of ... \n");
1031 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1032 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1033 for (a = 0; a < 5; ++a) {
1034 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
1040 printf("\n%s%c ", room_name, room_prompt(room_flags));
1042 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1043 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1044 for (a = 0; a < cmdpos; ++a) {
1046 cmd_expand(cptr->c_keys[a], 0));
1061 * set tty modes. commands are:
1063 * 01- set to bbs mode
1064 * 2 - save current settings for later restoral
1065 * 3 - restore saved settings
1067 #ifdef HAVE_TERMIOS_H
1068 void sttybbs(int cmd)
1069 { /* SysV version of sttybbs() */
1070 struct termios live;
1071 static struct termios saved_settings;
1072 static int last_cmd = 0;
1079 if ((cmd == 0) || (cmd == 1)) {
1080 tcgetattr(0, &live);
1081 live.c_iflag = ISTRIP | IXON | IXANY;
1082 live.c_oflag = OPOST | ONLCR;
1083 live.c_lflag = ISIG | NOFLSH;
1085 live.c_cc[VINTR] = (-1);
1086 live.c_cc[VQUIT] = (-1);
1089 live.c_cc[VMIN] = 0;
1090 live.c_cc[VTIME] = 0;
1093 /* do we even need this stuff anymore? */
1094 /* live.c_line=0; */
1095 live.c_cc[VERASE] = 8;
1096 live.c_cc[VKILL] = 24;
1097 live.c_cc[VEOF] = 1;
1098 live.c_cc[VEOL] = 255;
1099 live.c_cc[VEOL2] = 0;
1100 live.c_cc[VSTART] = 0;
1101 tcsetattr(0, TCSADRAIN, &live);
1104 tcgetattr(0, &saved_settings);
1107 tcsetattr(0, TCSADRAIN, &saved_settings);
1111 void sttybbs(int cmd)
1112 { /* BSD version of sttybbs() */
1114 static struct sgttyb saved_settings;
1116 if ((cmd == 0) || (cmd == 1)) {
1118 live.sg_flags |= CBREAK;
1119 live.sg_flags |= CRMOD;
1120 live.sg_flags |= NL1;
1121 live.sg_flags &= ~ECHO;
1123 live.sg_flags |= NOFLSH;
1127 gtty(0, &saved_settings);
1130 stty(0, &saved_settings);
1137 * display_help() - help file viewer
1139 void display_help(char *name)
1146 * fmout() - Citadel text formatter and paginator
1149 int width, /* screen width to use */
1150 FILE *fp, /* file to read from, or NULL to read from server */
1151 char pagin, /* nonzero if we should use the paginator */
1152 int height, /* screen height to use */
1153 int starting_lp,/* starting value for lines_printed, -1 for global */
1154 char subst) /* nonzero if we should use hypertext mode */
1156 int a, b, c, d, old;
1162 num_urls = 0; /* Start with a clean slate of embedded URL's */
1164 if (starting_lp >= 0) {
1165 lines_printed = starting_lp;
1170 c = 1; /* c is the current pos */
1172 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1173 if (fp != NULL) { /* read from file */
1176 if (eof_flag == 0) {
1178 buffer[strlen(buffer) + 1] = 0;
1179 buffer[strlen(buffer)] = a;
1181 } else { /* read from server */
1183 serv_gets(&buffer[d]);
1184 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1185 buffer[strlen(buffer) - 1] = 0;
1186 if (!strcmp(&buffer[d], "000")) {
1189 while (isspace(buffer[strlen(buffer) - 1]))
1190 buffer[strlen(buffer) - 1] = 0;
1198 if ( (!strncasecmp(buffer, "http://", 7))
1199 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1200 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1201 for (a=0; a<strlen(urls[num_urls]); ++a) {
1202 b = urls[num_urls][a];
1203 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1204 || (b==13) || (b==9) || (b=='\"') )
1205 urls[num_urls][a] = 0;
1210 buffer[strlen(buffer) + 1] = 0;
1212 strcpy(buffer, &buffer[1]);
1219 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1221 if (((old == 13) || (old == 10)) && (isspace(real))) {
1224 lines_printed = checkpagin(lines_printed, pagin, height);
1231 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1232 printf("\n%s", aaa);
1236 lines_printed = checkpagin(lines_printed, pagin, height);
1243 if ((strlen(aaa) + c) > (width - 5)) {
1247 lines_printed = checkpagin(lines_printed, pagin, height);
1251 c = c + strlen(aaa);
1255 if ((a == 13) || (a == 10)) {
1256 printf("%s\n", aaa);
1259 lines_printed = checkpagin(lines_printed, pagin, height);
1260 if (sigcaught) goto OOPS;
1266 /* keypress caught; drain the server */
1269 } while (strcmp(aaa, "000"));
1271 FMTEND: printf("\n");
1273 lines_printed = checkpagin(lines_printed, pagin, height);
1279 * support ANSI color if defined
1281 void color(int colornum)
1283 static int is_bold = 0;
1284 static int hold_color, current_color;
1286 if (colornum == COLOR_PUSH) {
1287 hold_color = current_color;
1291 if (colornum == COLOR_POP) {
1296 current_color = colornum;
1298 /* When switching to dim white, actually output an 'original
1299 * pair' sequence -- this looks better on black-on-white
1302 if (colornum == DIM_WHITE)
1303 printf("\033[39;49m");
1305 printf("\033[3%d;40m", (colornum & 7));
1307 if ((colornum >= 8) && (is_bold == 0)) {
1310 } else if ((colornum < 8) && (is_bold == 1)) {
1318 void cls(int colornum)
1321 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1328 * Detect whether ANSI color is available (answerback)
1330 void send_ansi_detect(void)
1332 if (rc_ansi_color == 2) {
1339 void look_for_ansi(void)
1347 if (rc_ansi_color == 0) {
1349 } else if (rc_ansi_color == 1) {
1351 } else if (rc_ansi_color == 2) {
1353 /* otherwise, do the auto-detect */
1358 if ((now - AnsiDetect) < 2)
1367 select(1, &rfds, NULL, NULL, &tv);
1368 if (FD_ISSET(0, &rfds)) {
1369 abuf[strlen(abuf) + 1] = 0;
1370 read(0, &abuf[strlen(abuf)], 1);
1372 } while (FD_ISSET(0, &rfds));
1374 for (a = 0; a < strlen(abuf); ++a) {
1375 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1376 && (abuf[a + 2] == '?')) {
1385 * Display key options (highlight hotkeys inside angle brackets)
1387 void keyopt(char *buf) {
1391 for (i=0; i<strlen(buf); ++i) {
1393 putc(buf[i], stdout);
1394 color(BRIGHT_MAGENTA);
1399 putc(buf[i], stdout);
1408 * Present a key-menu line choice type of thing
1410 char keymenu(char *menuprompt, char *menustring) {
1416 int display_prompt = 1;
1418 choices = num_tokens(menustring, '|');
1420 if (menuprompt != NULL) do_prompt = 1;
1421 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1424 if (display_prompt) {
1426 printf("%s ", menuprompt);
1429 for (i=0; i<choices; ++i) {
1430 extract(buf, menustring, i);
1440 if ( (do_prompt) && (ch=='?') ) {
1441 printf("\rOne of... ");
1443 for (i=0; i<choices; ++i) {
1444 extract(buf, menustring, i);
1453 for (i=0; i<choices; ++i) {
1454 extract(buf, menustring, i);
1455 for (c=1; c<strlen(buf); ++c) {
1456 if ( (ch == tolower(buf[c]))
1458 && (buf[c+1]=='>') ) {
1459 for (a=0; a<strlen(buf); ++a) {
1460 if ( (a!=(c-1)) && (a!=(c+1))) {
1461 putc(buf[a], stdout);