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 */
88 * If an interesting key has been pressed, return its value, otherwise 0
90 char was_a_key_pressed(void) {
100 retval = select(1, &rfds, NULL, NULL, &tv);
102 /* Careful! Disable keepalives during keyboard polling; we're probably
103 * in the middle of a data transfer from the server, in which case
104 * sending a NOOP would throw the client protocol out of sync.
106 if (FD_ISSET(0, &rfds)) {
107 set_keepalives(KA_NO);
108 the_character = inkey();
109 set_keepalives(KA_YES);
114 return(the_character);
122 * Check to see if we need to pause at the end of a screen.
123 * If we do, we have to disable server keepalives during the pause because
124 * we are probably in the middle of a server operation and the NOOP command
125 * would confuse everything.
127 int checkpagin(int lp, int pagin, int height)
131 if (sigcaught) return(lp);
132 thekey = was_a_key_pressed();
133 if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
134 if (sigcaught) return(lp);
136 if (!pagin) return(0);
137 if (lp>=(height-1)) {
138 set_keepalives(KA_NO);
140 set_keepalives(KA_YES);
150 * pprintf() ... paginated version of printf()
152 void pprintf(const char *format, ...) {
154 static char buf[4096]; /* static for performance, change if needed */
157 /* If sigcaught is nonzero, a keypress has interrupted this and we
158 * should just drain output.
160 if (sigcaught) return;
162 /* Otherwise, start spewing... */
163 va_start(arg_ptr, format);
164 vsprintf(buf, format, arg_ptr);
167 for (i=0; i<strlen(buf); ++i) {
168 putc(buf[i], stdout);
171 lines_printed = checkpagin(lines_printed,
172 (userflags & US_PAGINATOR),
181 * print_express() - print express messages if there are any
183 void print_express(void)
193 if (express_msgs == 0)
199 if (strlen(rc_exp_cmd) == 0) {
204 while (express_msgs != 0) {
210 express_msgs = extract_int(&buf[4], 0);
211 timestamp = extract_long(&buf[4], 1);
212 flags = extract_int(&buf[4], 2);
213 extract(sender, &buf[4], 3);
214 extract(node, &buf[4], 4);
216 stamp = localtime(×tamp);
218 if (strlen(rc_exp_cmd) > 0) {
219 outpipe = popen(rc_exp_cmd, "w");
220 if (outpipe != NULL) {
221 /* Header derived from flags */
224 "Please log off now, as requested ");
226 fprintf(outpipe, "Broadcast message ");
228 fprintf(outpipe, "Chat request ");
230 fprintf(outpipe, "Message ");
231 /* Timestamp. Can this be improved? */
232 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
233 fprintf(outpipe, "at 12:%02d%cm",
235 stamp->tm_hour ? 'p' : 'a');
236 else if (stamp->tm_hour > 12) /* pm */
237 fprintf(outpipe, "at %d:%02dpm",
241 fprintf(outpipe, "at %d:%02dam",
242 stamp->tm_hour, stamp->tm_min);
243 fprintf(outpipe, " from %s", sender);
244 if (strncmp(serv_info.serv_nodename, node, 32))
245 fprintf(outpipe, " @%s", node);
246 fprintf(outpipe, ":\n");
247 while (serv_gets(buf), strcmp(buf, "000")) {
248 fprintf(outpipe, "%s\n", buf);
251 if (express_msgs == 0)
256 /* fall back to built-in express message display */
259 /* Header derived from flags */
261 printf("Please log off now, as requested ");
263 printf("Broadcast message ");
265 printf("Chat request ");
269 /* Timestamp. Can this be improved? */
270 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
271 printf("at 12:%02d%cm", stamp->tm_min,
272 stamp->tm_hour ? 'p' : 'a');
273 else if (stamp->tm_hour > 12) /* pm */
274 printf("at %d:%02dpm",
275 stamp->tm_hour - 12, stamp->tm_min);
277 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
280 printf(" from %s", sender);
282 /* Remote node, if any */
283 if (strncmp(serv_info.serv_nodename, node, 32))
284 printf(" @%s", node);
288 while (serv_gets(buf), strcmp(buf, "000")) {
297 void set_keepalives(int s)
299 keepalives_enabled = (char) s;
303 * This loop handles the "keepalive" messages sent to the server when idling.
306 static time_t idlet = 0;
307 static void really_do_keepalive(void) {
311 if (keepalives_enabled == KA_YES) {
316 if (ok_to_interrupt == 1) {
317 printf("\r%64s\r", "");
319 printf("%s%c ", room_name,
320 room_prompt(room_flags));
327 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
328 encapsulated interface; in theory there should be no need to touch these
329 globals outside of the async_ka_* functions. */
331 #ifdef THREADED_CLIENT
332 static pthread_t ka_thr_handle;
333 static int ka_thr_active = 0;
334 static int async_ka_enabled = 0;
336 static void *ka_thread(void *arg)
338 really_do_keepalive();
339 pthread_detach(ka_thr_handle);
344 /* start up a thread to handle a keepalive in the background */
345 static void async_ka_exec(void)
347 if (!ka_thr_active) {
349 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
350 perror("pthread_create");
355 #endif /* THREADED_CLIENT */
357 static void do_keepalive(void)
362 if ((now - idlet) < ((long) S_KEEPALIVE))
365 /* Do a space-backspace to keep telnet sessions from idling out */
369 #ifdef THREADED_CLIENT
370 if (async_ka_enabled)
374 really_do_keepalive();
378 /* Now the actual async-keepalve API that we expose to higher levels:
379 async_ka_start() and async_ka_end(). These do nothing when we don't have
380 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
382 /* wait for a background keepalive to complete. this must be done before
383 attempting any further server requests! */
384 void async_ka_end(void)
386 #ifdef THREADED_CLIENT
388 pthread_join(ka_thr_handle, NULL);
394 /* tell do_keepalive() that keepalives are asynchronous. */
395 void async_ka_start(void)
397 #ifdef THREADED_CLIENT
404 { /* get a character from the keyboard, with */
405 int a; /* the watchdog timer in effect if necessary */
408 time_t start_time, now;
417 /* This loop waits for keyboard input. If the keepalive
418 * timer expires, it sends a keepalive to the server if
419 * necessary and then waits again.
426 tv.tv_sec = S_KEEPALIVE;
430 if (((now - start_time) > SLEEPING)
431 && (SLEEPING != 0) && (getppid() == 1)) {
432 printf("Sleeping? Call again.\n");
435 select(1, &rfds, NULL, NULL, &tv);
436 } while (!FD_ISSET(0, &rfds));
441 /* At this point, there's input, so fetch it.
442 * (There's a hole in the bucket...)
452 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
453 && ((a < 32) || (a > 126)))
461 { /* Returns 1 for yes, 0 for no */
477 /* Returns 1 for yes, 0 for no, arg is default value */
500 /* Gets a line from the terminal */
501 /* string == Pointer to string buffer */
502 /* lim == Maximum length - if negative, no-show */
503 void getline(char *string, int lim)
517 if ((a == 8) && (strlen(string) == 0))
519 if ((a != 13) && (a != 8) && (strlen(string) == lim))
521 if ((a == 8) && (string[0] != 0)) {
522 string[strlen(string) - 1] = 0;
528 if ((a == 13) || (a == 10)) {
548 * strprompt() - prompt for a string, print the existing value and
549 * allow the user to press return to keep it...
551 void strprompt(char *prompt, char *str, int len)
556 printf("%s ", prompt);
559 color(BRIGHT_MAGENTA);
573 * boolprompt() - prompt for a yes/no, print the existing value and
574 * allow the user to press return to keep it...
576 int boolprompt(char *prompt, int prev_val)
581 printf("%s ", prompt);
584 color(BRIGHT_MAGENTA);
585 printf("%s", (prev_val ? "Yes" : "No"));
589 r = (yesno_d(prev_val));
595 * intprompt() - like strprompt(), except for an integer
596 * (note that it RETURNS the new value!)
598 int intprompt(char *prompt, int ival, int imin, int imax)
606 snprintf(buf, sizeof buf, "%d", i);
607 strprompt(prompt, buf, 15);
609 for (p=0; p<strlen(buf); ++p) {
610 if ( (!isdigit(buf[p]))
611 && ( (buf[p]!='-') || (p!=0) ) )
615 printf("*** Must be no less than %d.\n", imin);
617 printf("*** Must be no more than %d.\n", imax);
618 } while ((i < imin) || (i > imax));
623 * newprompt() - prompt for a string with no existing value
624 * (clears out string buffer first)
626 void newprompt(char *prompt, char *str, int len)
628 color(BRIGHT_MAGENTA);
629 printf("%s", prompt);
637 { /* returns a lower case value */
646 * parse the citadel.rc file
648 void load_command_set(void)
653 struct citcmd *lastcmd = NULL;
658 /* first, set up some defaults for non-required variables */
660 strcpy(editor_path, "");
661 strcpy(printcmd, "");
662 strcpy(rc_username, "");
663 strcpy(rc_password, "");
666 rc_allow_attachments = 0;
667 rc_remember_passwords = 0;
668 strcpy(rc_exp_cmd, "");
669 rc_display_message_numbers = 0;
670 rc_force_mail_prompts = 0;
672 strcpy(rc_url_cmd, "");
674 /* now try to open the citadel.rc file */
677 if (getenv("HOME") != NULL) {
678 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
679 ccfile = fopen(buf, "r");
681 if (ccfile == NULL) {
682 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
684 if (ccfile == NULL) {
685 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
686 ccfile = fopen(buf, "r");
688 if (ccfile == NULL) {
689 ccfile = fopen("./citadel.rc", "r");
691 if (ccfile == NULL) {
692 perror("commands: cannot open citadel.rc");
695 while (fgets(buf, 256, ccfile) != NULL) {
696 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
697 buf[strlen(buf) - 1] = 0;
699 if (!strncasecmp(buf, "editor=", 7))
700 strcpy(editor_path, &buf[7]);
702 if (!strncasecmp(buf, "printcmd=", 9))
703 strcpy(printcmd, &buf[9]);
705 if (!strncasecmp(buf, "expcmd=", 7))
706 strcpy(rc_exp_cmd, &buf[7]);
708 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
709 have_xterm = (char) atoi(&buf[24]);
711 if (!strncasecmp(buf, "use_floors=", 11)) {
712 if (!strcasecmp(&buf[11], "yes"))
713 rc_floor_mode = RC_YES;
714 if (!strcasecmp(&buf[11], "no"))
715 rc_floor_mode = RC_NO;
716 if (!strcasecmp(&buf[11], "default"))
717 rc_floor_mode = RC_DEFAULT;
719 if (!strncasecmp(buf, "beep=", 5)) {
720 rc_exp_beep = atoi(&buf[5]);
722 if (!strncasecmp(buf, "allow_attachments=", 18)) {
723 rc_allow_attachments = atoi(&buf[18]);
725 if (!strncasecmp(buf, "remember_passwords=", 19)) {
726 rc_remember_passwords = atoi(&buf[19]);
728 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
729 rc_display_message_numbers = atoi(&buf[24]);
731 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
732 rc_force_mail_prompts = atoi(&buf[19]);
734 if (!strncasecmp(buf, "ansi_color=", 11)) {
735 if (!strncasecmp(&buf[11], "on", 2))
737 if (!strncasecmp(&buf[11], "auto", 4))
738 rc_ansi_color = 2; /* autodetect */
739 if (!strncasecmp(&buf[11], "user", 4))
740 rc_ansi_color = 3; /* user config */
742 if (!strncasecmp(buf, "username=", 9))
743 strcpy(rc_username, &buf[9]);
745 if (!strncasecmp(buf, "password=", 9))
746 strcpy(rc_password, &buf[9]);
748 if (!strncasecmp(buf, "urlcmd=", 7))
749 strcpy(rc_url_cmd, &buf[7]);
751 if (!strncasecmp(buf, "cmd=", 4)) {
752 strcpy(buf, &buf[4]);
754 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
756 cptr->c_cmdnum = atoi(buf);
757 for (d = strlen(buf); d >= 0; --d)
760 strcpy(buf, &buf[b + 1]);
762 cptr->c_axlevel = atoi(buf);
763 for (d = strlen(buf); d >= 0; --d)
766 strcpy(buf, &buf[b + 1]);
768 for (a = 0; a < 5; ++a)
769 cptr->c_keys[a][0] = 0;
773 buf[strlen(buf) + 1] = 0;
774 while (strlen(buf) > 0) {
776 for (d = strlen(buf); d >= 0; --d)
779 strncpy(cptr->c_keys[a], buf, b);
780 cptr->c_keys[a][b] = 0;
782 strcpy(buf, &buf[b + 1]);
792 lastcmd->next = cptr;
802 * return the key associated with a command
804 char keycmd(char *cmdstr)
808 for (a = 0; a < strlen(cmdstr); ++a)
809 if (cmdstr[a] == '&')
810 return (tolower(cmdstr[a + 1]));
816 * Output the string from a key command without the ampersand
817 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
819 char *cmd_expand(char *strbuf, int mode)
827 for (a = 0; a < strlen(exp); ++a) {
828 if (strbuf[a] == '&') {
831 strcpy(&exp[a], &exp[a + 1]);
835 strcpy(buf, &exp[a + 2]);
841 if (!strncmp(&exp[a], "^r", 2)) {
843 strcpy(&exp[a], room_name);
844 strcat(exp, &buf[a + 2]);
846 if (!strncmp(&exp[a], "^c", 2)) {
848 strcpy(&exp[a + 1], &exp[a + 2]);
858 * Comparison function to determine if entered commands match a
859 * command loaded from the config file.
861 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
872 for (a = 0; a < ncomp; ++a) {
873 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
874 || (cptr->c_axlevel > cmdax))
882 * This function returns 1 if a given command requires a string input
884 int requires_string(struct citcmd *cptr, int ncomp)
889 strcpy(buf, cptr->c_keys[ncomp - 1]);
890 for (a = 0; a < strlen(buf); ++a) {
899 * Input a command at the main prompt.
900 * This function returns an integer command number. If the command prompts
901 * for a string then it is placed in the supplied buffer.
903 int getcmd(char *argbuf)
915 * Starting a new command now, so set sigcaught to 0. This variable
916 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
917 * been interrupted by a keypress.
921 /* Switch color support on or off if we're in user mode */
922 if (rc_ansi_color == 3) {
923 if (userflags & US_COLOR)
928 /* if we're running in idiot mode, display a cute little menu */
929 IFNEXPERT formout("mainmenu");
931 print_express(); /* print express messages if there are any */
934 for (a = 0; a < 5; ++a)
936 /* now the room prompt... */
939 printf("\n%s", room_name);
941 printf("%c ", room_prompt(room_flags));
948 /* Handle the backspace key, but only if there's something
949 * to backspace over...
951 if ((ch == 8) && (cmdpos > 0)) {
952 back(cmdspaces[cmdpos - 1] + 1);
956 /* Spacebar invokes "lazy traversal" commands */
957 if ((ch == 32) && (cmdpos == 0)) {
958 this_lazy_cmd = next_lazy_cmd;
959 if (this_lazy_cmd == 13)
961 if (this_lazy_cmd == 5)
963 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
964 if (cptr->c_cmdnum == this_lazy_cmd) {
965 for (a = 0; a < 5; ++a)
966 if (cptr->c_keys[a][0] != 0)
967 printf("%s ", cmd_expand(
968 cptr->c_keys[a], 0));
970 return (this_lazy_cmd);
974 return (this_lazy_cmd);
976 /* Otherwise, process the command */
977 cmdbuf[cmdpos] = tolower(ch);
979 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
980 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
982 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
983 cmdspaces[cmdpos] = strlen(
984 cmd_expand(cptr->c_keys[cmdpos], 0));
986 if ((cptr->c_keys[cmdpos + 1]) != 0)
992 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
993 if (cmdmatch(cmdbuf, cptr, 5)) {
994 /* We've found our command. */
995 if (requires_string(cptr, cmdpos)) {
1001 /* If this command is one that changes rooms,
1002 * then the next lazy-command (space bar)
1003 * should be "read new" instead of "goto"
1005 if ((cptr->c_cmdnum == 5)
1006 || (cptr->c_cmdnum == 6)
1007 || (cptr->c_cmdnum == 47)
1008 || (cptr->c_cmdnum == 52)
1009 || (cptr->c_cmdnum == 16)
1010 || (cptr->c_cmdnum == 20))
1013 return (cptr->c_cmdnum);
1019 printf("\rOne of ... \n");
1020 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1021 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1022 for (a = 0; a < 5; ++a) {
1023 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
1029 printf("\n%s%c ", room_name, room_prompt(room_flags));
1031 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1032 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1033 for (a = 0; a < cmdpos; ++a) {
1035 cmd_expand(cptr->c_keys[a], 0));
1050 * set tty modes. commands are:
1052 * 01- set to bbs mode
1053 * 2 - save current settings for later restoral
1054 * 3 - restore saved settings
1056 #ifdef HAVE_TERMIOS_H
1057 void sttybbs(int cmd)
1058 { /* SysV version of sttybbs() */
1059 struct termios live;
1060 static struct termios saved_settings;
1061 static int last_cmd = 0;
1068 if ((cmd == 0) || (cmd == 1)) {
1069 tcgetattr(0, &live);
1070 live.c_iflag = ISTRIP | IXON | IXANY;
1071 live.c_oflag = OPOST | ONLCR;
1072 live.c_lflag = ISIG | NOFLSH;
1074 live.c_cc[VINTR] = (-1);
1075 live.c_cc[VQUIT] = (-1);
1078 live.c_cc[VMIN] = 0;
1079 live.c_cc[VTIME] = 0;
1082 /* do we even need this stuff anymore? */
1083 /* live.c_line=0; */
1084 live.c_cc[VERASE] = 8;
1085 live.c_cc[VKILL] = 24;
1086 live.c_cc[VEOF] = 1;
1087 live.c_cc[VEOL] = 255;
1088 live.c_cc[VEOL2] = 0;
1089 live.c_cc[VSTART] = 0;
1090 tcsetattr(0, TCSADRAIN, &live);
1093 tcgetattr(0, &saved_settings);
1096 tcsetattr(0, TCSADRAIN, &saved_settings);
1100 void sttybbs(int cmd)
1101 { /* BSD version of sttybbs() */
1103 static struct sgttyb saved_settings;
1105 if ((cmd == 0) || (cmd == 1)) {
1107 live.sg_flags |= CBREAK;
1108 live.sg_flags |= CRMOD;
1109 live.sg_flags |= NL1;
1110 live.sg_flags &= ~ECHO;
1112 live.sg_flags |= NOFLSH;
1116 gtty(0, &saved_settings);
1119 stty(0, &saved_settings);
1126 * display_help() - help file viewer
1128 void display_help(char *name)
1135 * fmout() - Citadel text formatter and paginator
1138 int width, /* screen width to use */
1139 FILE *fp, /* file to read from, or NULL to read from server */
1140 char pagin, /* nonzero if we should use the paginator */
1141 int height, /* screen height to use */
1142 int starting_lp,/* starting value for lines_printed, -1 for global */
1143 char subst) /* nonzero if we should use hypertext mode */
1145 int a, b, c, d, old;
1151 num_urls = 0; /* Start with a clean slate of embedded URL's */
1153 if (starting_lp >= 0) {
1154 lines_printed = starting_lp;
1159 c = 1; /* c is the current pos */
1161 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1162 if (fp != NULL) { /* read from file */
1165 if (eof_flag == 0) {
1167 buffer[strlen(buffer) + 1] = 0;
1168 buffer[strlen(buffer)] = a;
1170 } else { /* read from server */
1172 serv_gets(&buffer[d]);
1173 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1174 buffer[strlen(buffer) - 1] = 0;
1175 if (!strcmp(&buffer[d], "000")) {
1178 while (isspace(buffer[strlen(buffer) - 1]))
1179 buffer[strlen(buffer) - 1] = 0;
1187 if ( (!strncasecmp(buffer, "http://", 7))
1188 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1189 safestrncpy(urls[num_urls], buffer, 255);
1190 for (a=0; a<strlen(urls[num_urls]); ++a) {
1191 b = urls[num_urls][a];
1192 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1193 || (b==13) || (b==9) || (b=='\"') )
1194 urls[num_urls][a] = 0;
1199 buffer[strlen(buffer) + 1] = 0;
1201 strcpy(buffer, &buffer[1]);
1208 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1210 if (((old == 13) || (old == 10)) && (isspace(real))) {
1213 lines_printed = checkpagin(lines_printed, pagin, height);
1220 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1221 printf("\n%s", aaa);
1225 lines_printed = checkpagin(lines_printed, pagin, height);
1232 if ((strlen(aaa) + c) > (width - 5)) {
1236 lines_printed = checkpagin(lines_printed, pagin, height);
1240 c = c + strlen(aaa);
1244 if ((a == 13) || (a == 10)) {
1245 printf("%s\n", aaa);
1248 lines_printed = checkpagin(lines_printed, pagin, height);
1249 if (sigcaught) goto OOPS;
1255 /* keypress caught; drain the server */
1258 } while (strcmp(aaa, "000"));
1260 FMTEND: printf("\n");
1262 lines_printed = checkpagin(lines_printed, pagin, height);
1268 * support ANSI color if defined
1270 void color(int colornum)
1272 static int is_bold = 0;
1273 static int hold_color, current_color;
1275 if (colornum == COLOR_PUSH) {
1276 hold_color = current_color;
1280 if (colornum == COLOR_POP) {
1285 current_color = colornum;
1287 /* When switching to dim white, actually output an 'original
1288 * pair' sequence -- this looks better on black-on-white
1291 if (colornum == DIM_WHITE)
1292 printf("\033[39;49m");
1294 printf("\033[3%d;40m", (colornum & 7));
1296 if ((colornum >= 8) && (is_bold == 0)) {
1299 } else if ((colornum < 8) && (is_bold == 1)) {
1307 void cls(int colornum)
1310 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1317 * Detect whether ANSI color is available (answerback)
1319 void send_ansi_detect(void)
1321 if (rc_ansi_color == 2) {
1328 void look_for_ansi(void)
1336 if (rc_ansi_color == 0) {
1338 } else if (rc_ansi_color == 1) {
1340 } else if (rc_ansi_color == 2) {
1342 /* otherwise, do the auto-detect */
1347 if ((now - AnsiDetect) < 2)
1356 select(1, &rfds, NULL, NULL, &tv);
1357 if (FD_ISSET(0, &rfds)) {
1358 abuf[strlen(abuf) + 1] = 0;
1359 read(0, &abuf[strlen(abuf)], 1);
1361 } while (FD_ISSET(0, &rfds));
1363 for (a = 0; a < strlen(abuf); ++a) {
1364 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1365 && (abuf[a + 2] == '?')) {
1374 * Display key options (highlight hotkeys inside angle brackets)
1376 void keyopt(char *buf) {
1380 for (i=0; i<strlen(buf); ++i) {
1382 putc(buf[i], stdout);
1383 color(BRIGHT_MAGENTA);
1388 putc(buf[i], stdout);
1397 * Present a key-menu line choice type of thing
1399 char keymenu(char *menuprompt, char *menustring) {
1405 int display_prompt = 1;
1407 choices = num_tokens(menustring, '|');
1409 if (menuprompt != NULL) do_prompt = 1;
1410 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1413 if (display_prompt) {
1415 printf("%s ", menuprompt);
1418 for (i=0; i<choices; ++i) {
1419 extract(buf, menustring, i);
1429 if ( (do_prompt) && (ch=='?') ) {
1430 printf("\rOne of... ");
1432 for (i=0; i<choices; ++i) {
1433 extract(buf, menustring, i);
1442 for (i=0; i<choices; ++i) {
1443 extract(buf, menustring, i);
1444 for (c=1; c<strlen(buf); ++c) {
1445 if ( (ch == tolower(buf[c]))
1447 && (buf[c+1]=='>') ) {
1448 for (a=0; a<strlen(buf); ++a) {
1449 if ( (a!=(c-1)) && (a!=(c+1))) {
1450 putc(buf[a], stdout);