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 == 'q' || thekey == 'Q' || thekey == 's' || thekey == 'S')
147 if (thekey == 'n' || thekey == 'N')
149 if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
150 if (sigcaught) return(lp);
152 if (!pagin) return(0);
153 if (lp>=(height-1)) {
154 set_keepalives(KA_NO);
156 set_keepalives(KA_YES);
166 * pprintf() ... paginated version of printf()
168 void pprintf(const char *format, ...) {
170 static char buf[4096]; /* static for performance, change if needed */
173 /* If sigcaught is nonzero, a keypress has interrupted this and we
174 * should just drain output.
176 if (sigcaught) return;
178 /* Otherwise, start spewing... */
179 va_start(arg_ptr, format);
180 vsprintf(buf, format, arg_ptr);
183 for (i=0; i<strlen(buf); ++i) {
184 putc(buf[i], stdout);
187 lines_printed = checkpagin(lines_printed,
188 (userflags & US_PAGINATOR),
197 * print_express() - print express messages if there are any
199 void print_express(void)
209 if (express_msgs == 0)
215 if (strlen(rc_exp_cmd) == 0) {
220 while (express_msgs != 0) {
226 express_msgs = extract_int(&buf[4], 0);
227 timestamp = extract_long(&buf[4], 1);
228 flags = extract_int(&buf[4], 2);
229 extract(sender, &buf[4], 3);
230 extract(node, &buf[4], 4);
231 strcpy(last_paged, sender);
233 stamp = localtime(×tamp);
235 /* If the page is a Logoff Request, honor it. */
241 if (strlen(rc_exp_cmd) > 0) {
242 outpipe = popen(rc_exp_cmd, "w");
243 if (outpipe != NULL) {
244 /* Header derived from flags */
247 "Please log off now, as requested ");
249 fprintf(outpipe, "Broadcast message ");
251 fprintf(outpipe, "Chat request ");
253 fprintf(outpipe, "Message ");
254 /* Timestamp. Can this be improved? */
255 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
256 fprintf(outpipe, "at 12:%02d%cm",
258 stamp->tm_hour ? 'p' : 'a');
259 else if (stamp->tm_hour > 12) /* pm */
260 fprintf(outpipe, "at %d:%02dpm",
264 fprintf(outpipe, "at %d:%02dam",
265 stamp->tm_hour, stamp->tm_min);
266 fprintf(outpipe, " from %s", sender);
267 if (strncmp(serv_info.serv_nodename, node, 32))
268 fprintf(outpipe, " @%s", node);
269 fprintf(outpipe, ":\n");
270 while (serv_gets(buf), strcmp(buf, "000")) {
271 fprintf(outpipe, "%s\n", buf);
274 if (express_msgs == 0)
279 /* fall back to built-in express message display */
283 /* Header derived from flags */
285 printf("Please log off now, as requested ");
287 printf("Broadcast message ");
289 printf("Chat request ");
293 /* Timestamp. Can this be improved? */
294 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
295 printf("at 12:%02d%cm", stamp->tm_min,
296 stamp->tm_hour ? 'p' : 'a');
297 else if (stamp->tm_hour > 12) /* pm */
298 printf("at %d:%02dpm",
299 stamp->tm_hour - 12, stamp->tm_min);
301 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
304 printf(" from %s", sender);
306 /* Remote node, if any */
307 if (strncmp(serv_info.serv_nodename, node, 32))
308 printf(" @%s", node);
312 fmout(screenwidth, NULL, 1, screenheight, -1, 0);
321 void set_keepalives(int s)
323 keepalives_enabled = (char) s;
327 * This loop handles the "keepalive" messages sent to the server when idling.
330 static time_t idlet = 0;
331 static void really_do_keepalive(void) {
335 if (keepalives_enabled == KA_YES) {
340 if (ok_to_interrupt == 1) {
341 printf("\r%64s\r", "");
343 printf("%s%c ", room_name,
344 room_prompt(room_flags));
351 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
352 encapsulated interface; in theory there should be no need to touch these
353 globals outside of the async_ka_* functions. */
355 #ifdef THREADED_CLIENT
356 static pthread_t ka_thr_handle;
357 static int ka_thr_active = 0;
358 static int async_ka_enabled = 0;
360 static void *ka_thread(void *arg)
362 really_do_keepalive();
363 pthread_detach(ka_thr_handle);
368 /* start up a thread to handle a keepalive in the background */
369 static void async_ka_exec(void)
371 if (!ka_thr_active) {
373 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
374 perror("pthread_create");
379 #endif /* THREADED_CLIENT */
381 static void do_keepalive(void)
386 if ((now - idlet) < ((long) S_KEEPALIVE))
389 /* Do a space-backspace to keep telnet sessions from idling out */
393 #ifdef THREADED_CLIENT
394 if (async_ka_enabled)
398 really_do_keepalive();
402 /* Now the actual async-keepalve API that we expose to higher levels:
403 async_ka_start() and async_ka_end(). These do nothing when we don't have
404 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
406 /* wait for a background keepalive to complete. this must be done before
407 attempting any further server requests! */
408 void async_ka_end(void)
410 #ifdef THREADED_CLIENT
412 pthread_join(ka_thr_handle, NULL);
418 /* tell do_keepalive() that keepalives are asynchronous. */
419 void async_ka_start(void)
421 #ifdef THREADED_CLIENT
428 { /* get a character from the keyboard, with */
429 int a; /* the watchdog timer in effect if necessary */
432 time_t start_time, now;
441 /* This loop waits for keyboard input. If the keepalive
442 * timer expires, it sends a keepalive to the server if
443 * necessary and then waits again.
450 tv.tv_sec = S_KEEPALIVE;
454 select(1, &rfds, NULL, NULL, &tv);
455 } while (!FD_ISSET(0, &rfds));
460 /* At this point, there's input, so fetch it.
461 * (There's a hole in the bucket...)
471 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
472 && ((a < 32) || (a > 126)))
480 { /* Returns 1 for yes, 0 for no */
496 /* Returns 1 for yes, 0 for no, arg is default value */
519 /* Gets a line from the terminal */
520 /* string == Pointer to string buffer */
521 /* lim == Maximum length - if negative, no-show */
522 void getline(char *string, int lim)
536 if ((a == 8) && (strlen(string) == 0))
538 if ((a != 13) && (a != 8) && (strlen(string) == lim))
540 if ((a == 8) && (string[0] != 0)) {
541 string[strlen(string) - 1] = 0;
547 if ((a == 13) || (a == 10)) {
567 * strprompt() - prompt for a string, print the existing value and
568 * allow the user to press return to keep it...
570 void strprompt(char *prompt, char *str, int len)
575 printf("%s ", prompt);
578 color(BRIGHT_MAGENTA);
592 * boolprompt() - prompt for a yes/no, print the existing value and
593 * allow the user to press return to keep it...
595 int boolprompt(char *prompt, int prev_val)
600 printf("%s ", prompt);
603 color(BRIGHT_MAGENTA);
604 printf("%s", (prev_val ? "Yes" : "No"));
608 r = (yesno_d(prev_val));
614 * intprompt() - like strprompt(), except for an integer
615 * (note that it RETURNS the new value!)
617 int intprompt(char *prompt, int ival, int imin, int imax)
625 snprintf(buf, sizeof buf, "%d", i);
626 strprompt(prompt, buf, 15);
628 for (p=0; p<strlen(buf); ++p) {
629 if ( (!isdigit(buf[p]))
630 && ( (buf[p]!='-') || (p!=0) ) )
634 printf("*** Must be no less than %d.\n", imin);
636 printf("*** Must be no more than %d.\n", imax);
637 } while ((i < imin) || (i > imax));
642 * newprompt() - prompt for a string with no existing value
643 * (clears out string buffer first)
645 void newprompt(char *prompt, char *str, int len)
647 color(BRIGHT_MAGENTA);
648 printf("%s", prompt);
656 { /* returns a lower case value */
665 * parse the citadel.rc file
667 void load_command_set(void)
672 struct citcmd *lastcmd = NULL;
677 /* first, set up some defaults for non-required variables */
679 strcpy(editor_path, "");
680 strcpy(printcmd, "");
681 strcpy(rc_username, "");
682 strcpy(rc_password, "");
685 rc_allow_attachments = 0;
686 rc_remember_passwords = 0;
687 strcpy(rc_exp_cmd, "");
688 rc_display_message_numbers = 0;
689 rc_force_mail_prompts = 0;
691 strcpy(rc_url_cmd, "");
693 /* now try to open the citadel.rc file */
696 if (getenv("HOME") != NULL) {
697 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
698 ccfile = fopen(buf, "r");
700 if (ccfile == NULL) {
701 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
703 if (ccfile == NULL) {
704 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
705 ccfile = fopen(buf, "r");
707 if (ccfile == NULL) {
708 ccfile = fopen("./citadel.rc", "r");
710 if (ccfile == NULL) {
711 perror("commands: cannot open citadel.rc");
714 while (fgets(buf, sizeof buf, ccfile) != NULL) {
715 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
716 buf[strlen(buf) - 1] = 0;
718 if (!strncasecmp(buf, "editor=", 7))
719 strcpy(editor_path, &buf[7]);
721 if (!strncasecmp(buf, "printcmd=", 9))
722 strcpy(printcmd, &buf[9]);
724 if (!strncasecmp(buf, "expcmd=", 7))
725 strcpy(rc_exp_cmd, &buf[7]);
727 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
728 have_xterm = (char) atoi(&buf[24]);
730 if (!strncasecmp(buf, "use_floors=", 11)) {
731 if (!strcasecmp(&buf[11], "yes"))
732 rc_floor_mode = RC_YES;
733 if (!strcasecmp(&buf[11], "no"))
734 rc_floor_mode = RC_NO;
735 if (!strcasecmp(&buf[11], "default"))
736 rc_floor_mode = RC_DEFAULT;
738 if (!strncasecmp(buf, "beep=", 5)) {
739 rc_exp_beep = atoi(&buf[5]);
741 if (!strncasecmp(buf, "allow_attachments=", 18)) {
742 rc_allow_attachments = atoi(&buf[18]);
744 if (!strncasecmp(buf, "remember_passwords=", 19)) {
745 rc_remember_passwords = atoi(&buf[19]);
747 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
748 rc_display_message_numbers = atoi(&buf[24]);
750 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
751 rc_force_mail_prompts = atoi(&buf[19]);
753 if (!strncasecmp(buf, "ansi_color=", 11)) {
754 if (!strncasecmp(&buf[11], "on", 2))
756 if (!strncasecmp(&buf[11], "auto", 4))
757 rc_ansi_color = 2; /* autodetect */
758 if (!strncasecmp(&buf[11], "user", 4))
759 rc_ansi_color = 3; /* user config */
761 if (!strncasecmp(buf, "username=", 9))
762 strcpy(rc_username, &buf[9]);
764 if (!strncasecmp(buf, "password=", 9))
765 strcpy(rc_password, &buf[9]);
767 if (!strncasecmp(buf, "urlcmd=", 7))
768 strcpy(rc_url_cmd, &buf[7]);
770 if (!strncasecmp(buf, "cmd=", 4)) {
771 strcpy(buf, &buf[4]);
773 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
775 cptr->c_cmdnum = atoi(buf);
776 for (d = strlen(buf); d >= 0; --d)
779 strcpy(buf, &buf[b + 1]);
781 cptr->c_axlevel = atoi(buf);
782 for (d = strlen(buf); d >= 0; --d)
785 strcpy(buf, &buf[b + 1]);
787 for (a = 0; a < 5; ++a)
788 cptr->c_keys[a][0] = 0;
792 buf[strlen(buf) + 1] = 0;
793 while (strlen(buf) > 0) {
795 for (d = strlen(buf); d >= 0; --d)
798 strncpy(cptr->c_keys[a], buf, b);
799 cptr->c_keys[a][b] = 0;
801 strcpy(buf, &buf[b + 1]);
811 lastcmd->next = cptr;
821 * return the key associated with a command
823 char keycmd(char *cmdstr)
827 for (a = 0; a < strlen(cmdstr); ++a)
828 if (cmdstr[a] == '&')
829 return (tolower(cmdstr[a + 1]));
835 * Output the string from a key command without the ampersand
836 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
838 char *cmd_expand(char *strbuf, int mode)
846 for (a = 0; a < strlen(exp); ++a) {
847 if (strbuf[a] == '&') {
850 strcpy(&exp[a], &exp[a + 1]);
854 strcpy(buf, &exp[a + 2]);
860 if (!strncmp(&exp[a], "^r", 2)) {
862 strcpy(&exp[a], room_name);
863 strcat(exp, &buf[a + 2]);
865 if (!strncmp(&exp[a], "^c", 2)) {
867 strcpy(&exp[a + 1], &exp[a + 2]);
877 * Comparison function to determine if entered commands match a
878 * command loaded from the config file.
880 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
891 for (a = 0; a < ncomp; ++a) {
892 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
893 || (cptr->c_axlevel > cmdax))
901 * This function returns 1 if a given command requires a string input
903 int requires_string(struct citcmd *cptr, int ncomp)
908 strcpy(buf, cptr->c_keys[ncomp - 1]);
909 for (a = 0; a < strlen(buf); ++a) {
918 * Input a command at the main prompt.
919 * This function returns an integer command number. If the command prompts
920 * for a string then it is placed in the supplied buffer.
922 int getcmd(char *argbuf)
934 * Starting a new command now, so set sigcaught to 0. This variable
935 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
936 * been interrupted by a keypress.
940 /* Switch color support on or off if we're in user mode */
941 if (rc_ansi_color == 3) {
942 if (userflags & US_COLOR)
947 /* if we're running in idiot mode, display a cute little menu */
948 IFNEXPERT formout("mainmenu");
950 print_express(); /* print express messages if there are any */
953 for (a = 0; a < 5; ++a)
955 /* now the room prompt... */
958 printf("\n%s", room_name);
960 printf("%c ", room_prompt(room_flags));
967 /* Handle the backspace key, but only if there's something
968 * to backspace over...
970 if ((ch == 8) && (cmdpos > 0)) {
971 back(cmdspaces[cmdpos - 1] + 1);
975 /* Spacebar invokes "lazy traversal" commands */
976 if ((ch == 32) && (cmdpos == 0)) {
977 this_lazy_cmd = next_lazy_cmd;
978 if (this_lazy_cmd == 13)
980 if (this_lazy_cmd == 5)
982 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
983 if (cptr->c_cmdnum == this_lazy_cmd) {
984 for (a = 0; a < 5; ++a)
985 if (cptr->c_keys[a][0] != 0)
986 printf("%s ", cmd_expand(
987 cptr->c_keys[a], 0));
989 return (this_lazy_cmd);
993 return (this_lazy_cmd);
995 /* Otherwise, process the command */
996 cmdbuf[cmdpos] = tolower(ch);
998 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
999 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1001 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1002 cmdspaces[cmdpos] = strlen(
1003 cmd_expand(cptr->c_keys[cmdpos], 0));
1005 if ((cptr->c_keys[cmdpos + 1]) != 0)
1011 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1012 if (cmdmatch(cmdbuf, cptr, 5)) {
1013 /* We've found our command. */
1014 if (requires_string(cptr, cmdpos)) {
1015 getline(argbuf, 32);
1020 /* If this command is one that changes rooms,
1021 * then the next lazy-command (space bar)
1022 * should be "read new" instead of "goto"
1024 if ((cptr->c_cmdnum == 5)
1025 || (cptr->c_cmdnum == 6)
1026 || (cptr->c_cmdnum == 47)
1027 || (cptr->c_cmdnum == 52)
1028 || (cptr->c_cmdnum == 16)
1029 || (cptr->c_cmdnum == 20))
1032 return (cptr->c_cmdnum);
1038 pprintf("\rOne of ... \n");
1039 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1040 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1041 for (a = 0; a < 5; ++a) {
1042 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1048 pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1050 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1051 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1052 for (a = 0; a < cmdpos; ++a) {
1054 cmd_expand(cptr->c_keys[a], 0));
1069 * set tty modes. commands are:
1071 * 01- set to bbs mode
1072 * 2 - save current settings for later restoral
1073 * 3 - restore saved settings
1075 #ifdef HAVE_TERMIOS_H
1076 void sttybbs(int cmd)
1077 { /* SysV version of sttybbs() */
1078 struct termios live;
1079 static struct termios saved_settings;
1080 static int last_cmd = 0;
1087 if ((cmd == 0) || (cmd == 1)) {
1088 tcgetattr(0, &live);
1089 live.c_iflag = ISTRIP | IXON | IXANY;
1090 live.c_oflag = OPOST | ONLCR;
1091 live.c_lflag = ISIG | NOFLSH;
1093 live.c_cc[VINTR] = (-1);
1094 live.c_cc[VQUIT] = (-1);
1097 live.c_cc[VMIN] = 0;
1098 live.c_cc[VTIME] = 0;
1101 /* do we even need this stuff anymore? */
1102 /* live.c_line=0; */
1103 live.c_cc[VERASE] = 8;
1104 live.c_cc[VKILL] = 24;
1105 live.c_cc[VEOF] = 1;
1106 live.c_cc[VEOL] = 255;
1107 live.c_cc[VEOL2] = 0;
1108 live.c_cc[VSTART] = 0;
1109 tcsetattr(0, TCSADRAIN, &live);
1112 tcgetattr(0, &saved_settings);
1115 tcsetattr(0, TCSADRAIN, &saved_settings);
1119 void sttybbs(int cmd)
1120 { /* BSD version of sttybbs() */
1122 static struct sgttyb saved_settings;
1124 if ((cmd == 0) || (cmd == 1)) {
1126 live.sg_flags |= CBREAK;
1127 live.sg_flags |= CRMOD;
1128 live.sg_flags |= NL1;
1129 live.sg_flags &= ~ECHO;
1131 live.sg_flags |= NOFLSH;
1135 gtty(0, &saved_settings);
1138 stty(0, &saved_settings);
1145 * display_help() - help file viewer
1147 void display_help(char *name)
1154 * fmout() - Citadel text formatter and paginator
1157 int width, /* screen width to use */
1158 FILE *fp, /* file to read from, or NULL to read from server */
1159 char pagin, /* nonzero if we should use the paginator */
1160 int height, /* screen height to use */
1161 int starting_lp,/* starting value for lines_printed, -1 for global */
1162 char subst) /* nonzero if we should use hypertext mode */
1164 int a, b, c, d, old;
1170 num_urls = 0; /* Start with a clean slate of embedded URL's */
1172 if (starting_lp >= 0) {
1173 lines_printed = starting_lp;
1178 c = 1; /* c is the current pos */
1180 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1181 if (fp != NULL) { /* read from file */
1184 if (eof_flag == 0) {
1186 buffer[strlen(buffer) + 1] = 0;
1187 buffer[strlen(buffer)] = a;
1189 } else { /* read from server */
1191 serv_gets(&buffer[d]);
1192 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1193 buffer[strlen(buffer) - 1] = 0;
1194 if (!strcmp(&buffer[d], "000")) {
1197 while (isspace(buffer[strlen(buffer) - 1]))
1198 buffer[strlen(buffer) - 1] = 0;
1206 if ( (!strncasecmp(buffer, "http://", 7))
1207 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1208 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1209 for (a=0; a<strlen(urls[num_urls]); ++a) {
1210 b = urls[num_urls][a];
1211 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1212 || (b==13) || (b==9) || (b=='\"') )
1213 urls[num_urls][a] = 0;
1218 buffer[strlen(buffer) + 1] = 0;
1220 strcpy(buffer, &buffer[1]);
1227 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1229 if (((old == 13) || (old == 10)) && (isspace(real))) {
1232 lines_printed = checkpagin(lines_printed, pagin, height);
1239 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1240 printf("\n%s", aaa);
1244 lines_printed = checkpagin(lines_printed, pagin, height);
1251 if ((strlen(aaa) + c) > (width - 5)) {
1255 lines_printed = checkpagin(lines_printed, pagin, height);
1259 c = c + strlen(aaa);
1263 if ((a == 13) || (a == 10)) {
1264 printf("%s\n", aaa);
1267 lines_printed = checkpagin(lines_printed, pagin, height);
1268 if (sigcaught) goto OOPS;
1274 /* keypress caught; drain the server */
1277 } while (strcmp(aaa, "000"));
1279 FMTEND: printf("\n");
1281 lines_printed = checkpagin(lines_printed, pagin, height);
1287 * support ANSI color if defined
1289 void color(int colornum)
1291 static int is_bold = 0;
1292 static int hold_color, current_color;
1294 if (colornum == COLOR_PUSH) {
1295 hold_color = current_color;
1299 if (colornum == COLOR_POP) {
1304 current_color = colornum;
1306 /* When switching to dim white, actually output an 'original
1307 * pair' sequence -- this looks better on black-on-white
1310 if (colornum == DIM_WHITE)
1311 printf("\033[39;49m");
1313 printf("\033[3%d;40m", (colornum & 7));
1315 if ((colornum >= 8) && (is_bold == 0)) {
1318 } else if ((colornum < 8) && (is_bold == 1)) {
1326 void cls(int colornum)
1329 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1336 * Detect whether ANSI color is available (answerback)
1338 void send_ansi_detect(void)
1340 if (rc_ansi_color == 2) {
1347 void look_for_ansi(void)
1355 if (rc_ansi_color == 0) {
1357 } else if (rc_ansi_color == 1) {
1359 } else if (rc_ansi_color == 2) {
1361 /* otherwise, do the auto-detect */
1366 if ((now - AnsiDetect) < 2)
1375 select(1, &rfds, NULL, NULL, &tv);
1376 if (FD_ISSET(0, &rfds)) {
1377 abuf[strlen(abuf) + 1] = 0;
1378 read(0, &abuf[strlen(abuf)], 1);
1380 } while (FD_ISSET(0, &rfds));
1382 for (a = 0; a < strlen(abuf); ++a) {
1383 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1384 && (abuf[a + 2] == '?')) {
1393 * Display key options (highlight hotkeys inside angle brackets)
1395 void keyopt(char *buf) {
1399 for (i=0; i<strlen(buf); ++i) {
1401 putc(buf[i], stdout);
1402 color(BRIGHT_MAGENTA);
1407 putc(buf[i], stdout);
1416 * Present a key-menu line choice type of thing
1418 char keymenu(char *menuprompt, char *menustring) {
1424 int display_prompt = 1;
1426 choices = num_tokens(menustring, '|');
1428 if (menuprompt != NULL) do_prompt = 1;
1429 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1432 if (display_prompt) {
1434 printf("%s ", menuprompt);
1437 for (i=0; i<choices; ++i) {
1438 extract(buf, menustring, i);
1448 if ( (do_prompt) && (ch=='?') ) {
1449 printf("\rOne of... ");
1451 for (i=0; i<choices; ++i) {
1452 extract(buf, menustring, i);
1461 for (i=0; i<choices; ++i) {
1462 extract(buf, menustring, i);
1463 for (c=1; c<strlen(buf); ++c) {
1464 if ( (ch == tolower(buf[c]))
1466 && (buf[c+1]=='>') ) {
1467 for (a=0; a<strlen(buf); ++a) {
1468 if ( (a!=(c-1)) && (a!=(c+1))) {
1469 putc(buf[a], stdout);