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)
59 char rc_exp_cmd[1024];
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][SIZ];
70 int next_lazy_cmd = 5;
72 int lines_printed = 0; /* line count for paginator */
73 extern int screenwidth, screenheight;
76 struct citcmd *cmdlist = NULL;
79 /* these variables are local to this module */
80 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
81 int ok_to_interrupt = 0; /* print express msgs asynchronously */
82 time_t AnsiDetect; /* when did we send the detect code? */
83 int enable_color = 0; /* nonzero for ANSI color */
89 * If an interesting key has been pressed, return its value, otherwise 0
91 char was_a_key_pressed(void) {
101 retval = select(1, &rfds, NULL, NULL, &tv);
103 /* Careful! Disable keepalives during keyboard polling; we're probably
104 * in the middle of a data transfer from the server, in which case
105 * sending a NOOP would throw the client protocol out of sync.
107 if (FD_ISSET(0, &rfds)) {
108 set_keepalives(KA_NO);
109 the_character = inkey();
110 set_keepalives(KA_YES);
115 return(the_character);
123 * Check to see if we need to pause at the end of a screen.
124 * If we do, we have to disable server keepalives during the pause because
125 * we are probably in the middle of a server operation and the NOOP command
126 * would confuse everything.
128 int checkpagin(int lp, int pagin, int height)
132 if (sigcaught) return(lp);
133 thekey = was_a_key_pressed();
134 if ( (thekey == NEXT_KEY) || (thekey == STOP_KEY)) sigcaught = thekey;
135 if (sigcaught) return(lp);
137 if (!pagin) return(0);
138 if (lp>=(height-1)) {
139 set_keepalives(KA_NO);
141 set_keepalives(KA_YES);
151 * pprintf() ... paginated version of printf()
153 void pprintf(const char *format, ...) {
155 static char buf[4096]; /* static for performance, change if needed */
158 /* If sigcaught is nonzero, a keypress has interrupted this and we
159 * should just drain output.
161 if (sigcaught) return;
163 /* Otherwise, start spewing... */
164 va_start(arg_ptr, format);
165 vsprintf(buf, format, arg_ptr);
168 for (i=0; i<strlen(buf); ++i) {
169 putc(buf[i], stdout);
172 lines_printed = checkpagin(lines_printed,
173 (userflags & US_PAGINATOR),
182 * print_express() - print express messages if there are any
184 void print_express(void)
194 if (express_msgs == 0)
200 if (strlen(rc_exp_cmd) == 0) {
205 while (express_msgs != 0) {
211 express_msgs = extract_int(&buf[4], 0);
212 timestamp = extract_long(&buf[4], 1);
213 flags = extract_int(&buf[4], 2);
214 extract(sender, &buf[4], 3);
215 extract(node, &buf[4], 4);
217 stamp = localtime(×tamp);
219 /* If the page is a Logoff Request, honor it. */
225 if (strlen(rc_exp_cmd) > 0) {
226 outpipe = popen(rc_exp_cmd, "w");
227 if (outpipe != NULL) {
228 /* Header derived from flags */
231 "Please log off now, as requested ");
233 fprintf(outpipe, "Broadcast message ");
235 fprintf(outpipe, "Chat request ");
237 fprintf(outpipe, "Message ");
238 /* Timestamp. Can this be improved? */
239 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
240 fprintf(outpipe, "at 12:%02d%cm",
242 stamp->tm_hour ? 'p' : 'a');
243 else if (stamp->tm_hour > 12) /* pm */
244 fprintf(outpipe, "at %d:%02dpm",
248 fprintf(outpipe, "at %d:%02dam",
249 stamp->tm_hour, stamp->tm_min);
250 fprintf(outpipe, " from %s", sender);
251 if (strncmp(serv_info.serv_nodename, node, 32))
252 fprintf(outpipe, " @%s", node);
253 fprintf(outpipe, ":\n");
254 while (serv_gets(buf), strcmp(buf, "000")) {
255 fprintf(outpipe, "%s\n", buf);
258 if (express_msgs == 0)
263 /* fall back to built-in express message display */
266 /* Header derived from flags */
268 printf("Please log off now, as requested ");
270 printf("Broadcast message ");
272 printf("Chat request ");
276 /* Timestamp. Can this be improved? */
277 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
278 printf("at 12:%02d%cm", stamp->tm_min,
279 stamp->tm_hour ? 'p' : 'a');
280 else if (stamp->tm_hour > 12) /* pm */
281 printf("at %d:%02dpm",
282 stamp->tm_hour - 12, stamp->tm_min);
284 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
287 printf(" from %s", sender);
289 /* Remote node, if any */
290 if (strncmp(serv_info.serv_nodename, node, 32))
291 printf(" @%s", node);
295 while (serv_gets(buf), strcmp(buf, "000")) {
306 void set_keepalives(int s)
308 keepalives_enabled = (char) s;
312 * This loop handles the "keepalive" messages sent to the server when idling.
315 static time_t idlet = 0;
316 static void really_do_keepalive(void) {
320 if (keepalives_enabled == KA_YES) {
325 if (ok_to_interrupt == 1) {
326 printf("\r%64s\r", "");
328 printf("%s%c ", room_name,
329 room_prompt(room_flags));
336 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
337 encapsulated interface; in theory there should be no need to touch these
338 globals outside of the async_ka_* functions. */
340 #ifdef THREADED_CLIENT
341 static pthread_t ka_thr_handle;
342 static int ka_thr_active = 0;
343 static int async_ka_enabled = 0;
345 static void *ka_thread(void *arg)
347 really_do_keepalive();
348 pthread_detach(ka_thr_handle);
353 /* start up a thread to handle a keepalive in the background */
354 static void async_ka_exec(void)
356 if (!ka_thr_active) {
358 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
359 perror("pthread_create");
364 #endif /* THREADED_CLIENT */
366 static void do_keepalive(void)
371 if ((now - idlet) < ((long) S_KEEPALIVE))
374 /* Do a space-backspace to keep telnet sessions from idling out */
378 #ifdef THREADED_CLIENT
379 if (async_ka_enabled)
383 really_do_keepalive();
387 /* Now the actual async-keepalve API that we expose to higher levels:
388 async_ka_start() and async_ka_end(). These do nothing when we don't have
389 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
391 /* wait for a background keepalive to complete. this must be done before
392 attempting any further server requests! */
393 void async_ka_end(void)
395 #ifdef THREADED_CLIENT
397 pthread_join(ka_thr_handle, NULL);
403 /* tell do_keepalive() that keepalives are asynchronous. */
404 void async_ka_start(void)
406 #ifdef THREADED_CLIENT
413 { /* get a character from the keyboard, with */
414 int a; /* the watchdog timer in effect if necessary */
417 time_t start_time, now;
426 /* This loop waits for keyboard input. If the keepalive
427 * timer expires, it sends a keepalive to the server if
428 * necessary and then waits again.
435 tv.tv_sec = S_KEEPALIVE;
439 if (((now - start_time) > SLEEPING)
440 && (SLEEPING != 0) && (getppid() == 1)) {
441 printf("Sleeping? Call again.\n");
444 select(1, &rfds, NULL, NULL, &tv);
445 } while (!FD_ISSET(0, &rfds));
450 /* At this point, there's input, so fetch it.
451 * (There's a hole in the bucket...)
461 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
462 && ((a < 32) || (a > 126)))
470 { /* Returns 1 for yes, 0 for no */
486 /* Returns 1 for yes, 0 for no, arg is default value */
509 /* Gets a line from the terminal */
510 /* string == Pointer to string buffer */
511 /* lim == Maximum length - if negative, no-show */
512 void getline(char *string, int lim)
526 if ((a == 8) && (strlen(string) == 0))
528 if ((a != 13) && (a != 8) && (strlen(string) == lim))
530 if ((a == 8) && (string[0] != 0)) {
531 string[strlen(string) - 1] = 0;
537 if ((a == 13) || (a == 10)) {
557 * strprompt() - prompt for a string, print the existing value and
558 * allow the user to press return to keep it...
560 void strprompt(char *prompt, char *str, int len)
565 printf("%s ", prompt);
568 color(BRIGHT_MAGENTA);
582 * boolprompt() - prompt for a yes/no, print the existing value and
583 * allow the user to press return to keep it...
585 int boolprompt(char *prompt, int prev_val)
590 printf("%s ", prompt);
593 color(BRIGHT_MAGENTA);
594 printf("%s", (prev_val ? "Yes" : "No"));
598 r = (yesno_d(prev_val));
604 * intprompt() - like strprompt(), except for an integer
605 * (note that it RETURNS the new value!)
607 int intprompt(char *prompt, int ival, int imin, int imax)
615 snprintf(buf, sizeof buf, "%d", i);
616 strprompt(prompt, buf, 15);
618 for (p=0; p<strlen(buf); ++p) {
619 if ( (!isdigit(buf[p]))
620 && ( (buf[p]!='-') || (p!=0) ) )
624 printf("*** Must be no less than %d.\n", imin);
626 printf("*** Must be no more than %d.\n", imax);
627 } while ((i < imin) || (i > imax));
632 * newprompt() - prompt for a string with no existing value
633 * (clears out string buffer first)
635 void newprompt(char *prompt, char *str, int len)
637 color(BRIGHT_MAGENTA);
638 printf("%s", prompt);
646 { /* returns a lower case value */
655 * parse the citadel.rc file
657 void load_command_set(void)
662 struct citcmd *lastcmd = NULL;
667 /* first, set up some defaults for non-required variables */
669 strcpy(editor_path, "");
670 strcpy(printcmd, "");
671 strcpy(rc_username, "");
672 strcpy(rc_password, "");
675 rc_allow_attachments = 0;
676 rc_remember_passwords = 0;
677 strcpy(rc_exp_cmd, "");
678 rc_display_message_numbers = 0;
679 rc_force_mail_prompts = 0;
681 strcpy(rc_url_cmd, "");
683 /* now try to open the citadel.rc file */
686 if (getenv("HOME") != NULL) {
687 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
688 ccfile = fopen(buf, "r");
690 if (ccfile == NULL) {
691 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
693 if (ccfile == NULL) {
694 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
695 ccfile = fopen(buf, "r");
697 if (ccfile == NULL) {
698 ccfile = fopen("./citadel.rc", "r");
700 if (ccfile == NULL) {
701 perror("commands: cannot open citadel.rc");
704 while (fgets(buf, sizeof buf, ccfile) != NULL) {
705 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
706 buf[strlen(buf) - 1] = 0;
708 if (!strncasecmp(buf, "editor=", 7))
709 strcpy(editor_path, &buf[7]);
711 if (!strncasecmp(buf, "printcmd=", 9))
712 strcpy(printcmd, &buf[9]);
714 if (!strncasecmp(buf, "expcmd=", 7))
715 strcpy(rc_exp_cmd, &buf[7]);
717 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
718 have_xterm = (char) atoi(&buf[24]);
720 if (!strncasecmp(buf, "use_floors=", 11)) {
721 if (!strcasecmp(&buf[11], "yes"))
722 rc_floor_mode = RC_YES;
723 if (!strcasecmp(&buf[11], "no"))
724 rc_floor_mode = RC_NO;
725 if (!strcasecmp(&buf[11], "default"))
726 rc_floor_mode = RC_DEFAULT;
728 if (!strncasecmp(buf, "beep=", 5)) {
729 rc_exp_beep = atoi(&buf[5]);
731 if (!strncasecmp(buf, "allow_attachments=", 18)) {
732 rc_allow_attachments = atoi(&buf[18]);
734 if (!strncasecmp(buf, "remember_passwords=", 19)) {
735 rc_remember_passwords = atoi(&buf[19]);
737 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
738 rc_display_message_numbers = atoi(&buf[24]);
740 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
741 rc_force_mail_prompts = atoi(&buf[19]);
743 if (!strncasecmp(buf, "ansi_color=", 11)) {
744 if (!strncasecmp(&buf[11], "on", 2))
746 if (!strncasecmp(&buf[11], "auto", 4))
747 rc_ansi_color = 2; /* autodetect */
748 if (!strncasecmp(&buf[11], "user", 4))
749 rc_ansi_color = 3; /* user config */
751 if (!strncasecmp(buf, "username=", 9))
752 strcpy(rc_username, &buf[9]);
754 if (!strncasecmp(buf, "password=", 9))
755 strcpy(rc_password, &buf[9]);
757 if (!strncasecmp(buf, "urlcmd=", 7))
758 strcpy(rc_url_cmd, &buf[7]);
760 if (!strncasecmp(buf, "cmd=", 4)) {
761 strcpy(buf, &buf[4]);
763 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
765 cptr->c_cmdnum = atoi(buf);
766 for (d = strlen(buf); d >= 0; --d)
769 strcpy(buf, &buf[b + 1]);
771 cptr->c_axlevel = atoi(buf);
772 for (d = strlen(buf); d >= 0; --d)
775 strcpy(buf, &buf[b + 1]);
777 for (a = 0; a < 5; ++a)
778 cptr->c_keys[a][0] = 0;
782 buf[strlen(buf) + 1] = 0;
783 while (strlen(buf) > 0) {
785 for (d = strlen(buf); d >= 0; --d)
788 strncpy(cptr->c_keys[a], buf, b);
789 cptr->c_keys[a][b] = 0;
791 strcpy(buf, &buf[b + 1]);
801 lastcmd->next = cptr;
811 * return the key associated with a command
813 char keycmd(char *cmdstr)
817 for (a = 0; a < strlen(cmdstr); ++a)
818 if (cmdstr[a] == '&')
819 return (tolower(cmdstr[a + 1]));
825 * Output the string from a key command without the ampersand
826 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
828 char *cmd_expand(char *strbuf, int mode)
836 for (a = 0; a < strlen(exp); ++a) {
837 if (strbuf[a] == '&') {
840 strcpy(&exp[a], &exp[a + 1]);
844 strcpy(buf, &exp[a + 2]);
850 if (!strncmp(&exp[a], "^r", 2)) {
852 strcpy(&exp[a], room_name);
853 strcat(exp, &buf[a + 2]);
855 if (!strncmp(&exp[a], "^c", 2)) {
857 strcpy(&exp[a + 1], &exp[a + 2]);
867 * Comparison function to determine if entered commands match a
868 * command loaded from the config file.
870 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
881 for (a = 0; a < ncomp; ++a) {
882 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
883 || (cptr->c_axlevel > cmdax))
891 * This function returns 1 if a given command requires a string input
893 int requires_string(struct citcmd *cptr, int ncomp)
898 strcpy(buf, cptr->c_keys[ncomp - 1]);
899 for (a = 0; a < strlen(buf); ++a) {
908 * Input a command at the main prompt.
909 * This function returns an integer command number. If the command prompts
910 * for a string then it is placed in the supplied buffer.
912 int getcmd(char *argbuf)
924 * Starting a new command now, so set sigcaught to 0. This variable
925 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
926 * been interrupted by a keypress.
930 /* Switch color support on or off if we're in user mode */
931 if (rc_ansi_color == 3) {
932 if (userflags & US_COLOR)
937 /* if we're running in idiot mode, display a cute little menu */
938 IFNEXPERT formout("mainmenu");
940 print_express(); /* print express messages if there are any */
943 for (a = 0; a < 5; ++a)
945 /* now the room prompt... */
948 printf("\n%s", room_name);
950 printf("%c ", room_prompt(room_flags));
957 /* Handle the backspace key, but only if there's something
958 * to backspace over...
960 if ((ch == 8) && (cmdpos > 0)) {
961 back(cmdspaces[cmdpos - 1] + 1);
965 /* Spacebar invokes "lazy traversal" commands */
966 if ((ch == 32) && (cmdpos == 0)) {
967 this_lazy_cmd = next_lazy_cmd;
968 if (this_lazy_cmd == 13)
970 if (this_lazy_cmd == 5)
972 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
973 if (cptr->c_cmdnum == this_lazy_cmd) {
974 for (a = 0; a < 5; ++a)
975 if (cptr->c_keys[a][0] != 0)
976 printf("%s ", cmd_expand(
977 cptr->c_keys[a], 0));
979 return (this_lazy_cmd);
983 return (this_lazy_cmd);
985 /* Otherwise, process the command */
986 cmdbuf[cmdpos] = tolower(ch);
988 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
989 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
991 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
992 cmdspaces[cmdpos] = strlen(
993 cmd_expand(cptr->c_keys[cmdpos], 0));
995 if ((cptr->c_keys[cmdpos + 1]) != 0)
1001 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1002 if (cmdmatch(cmdbuf, cptr, 5)) {
1003 /* We've found our command. */
1004 if (requires_string(cptr, cmdpos)) {
1005 getline(argbuf, 32);
1010 /* If this command is one that changes rooms,
1011 * then the next lazy-command (space bar)
1012 * should be "read new" instead of "goto"
1014 if ((cptr->c_cmdnum == 5)
1015 || (cptr->c_cmdnum == 6)
1016 || (cptr->c_cmdnum == 47)
1017 || (cptr->c_cmdnum == 52)
1018 || (cptr->c_cmdnum == 16)
1019 || (cptr->c_cmdnum == 20))
1022 return (cptr->c_cmdnum);
1028 printf("\rOne of ... \n");
1029 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1030 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1031 for (a = 0; a < 5; ++a) {
1032 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
1038 printf("\n%s%c ", room_name, room_prompt(room_flags));
1040 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1041 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1042 for (a = 0; a < cmdpos; ++a) {
1044 cmd_expand(cptr->c_keys[a], 0));
1059 * set tty modes. commands are:
1061 * 01- set to bbs mode
1062 * 2 - save current settings for later restoral
1063 * 3 - restore saved settings
1065 #ifdef HAVE_TERMIOS_H
1066 void sttybbs(int cmd)
1067 { /* SysV version of sttybbs() */
1068 struct termios live;
1069 static struct termios saved_settings;
1070 static int last_cmd = 0;
1077 if ((cmd == 0) || (cmd == 1)) {
1078 tcgetattr(0, &live);
1079 live.c_iflag = ISTRIP | IXON | IXANY;
1080 live.c_oflag = OPOST | ONLCR;
1081 live.c_lflag = ISIG | NOFLSH;
1083 live.c_cc[VINTR] = (-1);
1084 live.c_cc[VQUIT] = (-1);
1087 live.c_cc[VMIN] = 0;
1088 live.c_cc[VTIME] = 0;
1091 /* do we even need this stuff anymore? */
1092 /* live.c_line=0; */
1093 live.c_cc[VERASE] = 8;
1094 live.c_cc[VKILL] = 24;
1095 live.c_cc[VEOF] = 1;
1096 live.c_cc[VEOL] = 255;
1097 live.c_cc[VEOL2] = 0;
1098 live.c_cc[VSTART] = 0;
1099 tcsetattr(0, TCSADRAIN, &live);
1102 tcgetattr(0, &saved_settings);
1105 tcsetattr(0, TCSADRAIN, &saved_settings);
1109 void sttybbs(int cmd)
1110 { /* BSD version of sttybbs() */
1112 static struct sgttyb saved_settings;
1114 if ((cmd == 0) || (cmd == 1)) {
1116 live.sg_flags |= CBREAK;
1117 live.sg_flags |= CRMOD;
1118 live.sg_flags |= NL1;
1119 live.sg_flags &= ~ECHO;
1121 live.sg_flags |= NOFLSH;
1125 gtty(0, &saved_settings);
1128 stty(0, &saved_settings);
1135 * display_help() - help file viewer
1137 void display_help(char *name)
1144 * fmout() - Citadel text formatter and paginator
1147 int width, /* screen width to use */
1148 FILE *fp, /* file to read from, or NULL to read from server */
1149 char pagin, /* nonzero if we should use the paginator */
1150 int height, /* screen height to use */
1151 int starting_lp,/* starting value for lines_printed, -1 for global */
1152 char subst) /* nonzero if we should use hypertext mode */
1154 int a, b, c, d, old;
1160 num_urls = 0; /* Start with a clean slate of embedded URL's */
1162 if (starting_lp >= 0) {
1163 lines_printed = starting_lp;
1168 c = 1; /* c is the current pos */
1170 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1171 if (fp != NULL) { /* read from file */
1174 if (eof_flag == 0) {
1176 buffer[strlen(buffer) + 1] = 0;
1177 buffer[strlen(buffer)] = a;
1179 } else { /* read from server */
1181 serv_gets(&buffer[d]);
1182 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1183 buffer[strlen(buffer) - 1] = 0;
1184 if (!strcmp(&buffer[d], "000")) {
1187 while (isspace(buffer[strlen(buffer) - 1]))
1188 buffer[strlen(buffer) - 1] = 0;
1196 if ( (!strncasecmp(buffer, "http://", 7))
1197 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1198 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1199 for (a=0; a<strlen(urls[num_urls]); ++a) {
1200 b = urls[num_urls][a];
1201 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1202 || (b==13) || (b==9) || (b=='\"') )
1203 urls[num_urls][a] = 0;
1208 buffer[strlen(buffer) + 1] = 0;
1210 strcpy(buffer, &buffer[1]);
1217 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1219 if (((old == 13) || (old == 10)) && (isspace(real))) {
1222 lines_printed = checkpagin(lines_printed, pagin, height);
1229 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1230 printf("\n%s", aaa);
1234 lines_printed = checkpagin(lines_printed, pagin, height);
1241 if ((strlen(aaa) + c) > (width - 5)) {
1245 lines_printed = checkpagin(lines_printed, pagin, height);
1249 c = c + strlen(aaa);
1253 if ((a == 13) || (a == 10)) {
1254 printf("%s\n", aaa);
1257 lines_printed = checkpagin(lines_printed, pagin, height);
1258 if (sigcaught) goto OOPS;
1264 /* keypress caught; drain the server */
1267 } while (strcmp(aaa, "000"));
1269 FMTEND: printf("\n");
1271 lines_printed = checkpagin(lines_printed, pagin, height);
1277 * support ANSI color if defined
1279 void color(int colornum)
1281 static int is_bold = 0;
1282 static int hold_color, current_color;
1284 if (colornum == COLOR_PUSH) {
1285 hold_color = current_color;
1289 if (colornum == COLOR_POP) {
1294 current_color = colornum;
1296 /* When switching to dim white, actually output an 'original
1297 * pair' sequence -- this looks better on black-on-white
1300 if (colornum == DIM_WHITE)
1301 printf("\033[39;49m");
1303 printf("\033[3%d;40m", (colornum & 7));
1305 if ((colornum >= 8) && (is_bold == 0)) {
1308 } else if ((colornum < 8) && (is_bold == 1)) {
1316 void cls(int colornum)
1319 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1326 * Detect whether ANSI color is available (answerback)
1328 void send_ansi_detect(void)
1330 if (rc_ansi_color == 2) {
1337 void look_for_ansi(void)
1345 if (rc_ansi_color == 0) {
1347 } else if (rc_ansi_color == 1) {
1349 } else if (rc_ansi_color == 2) {
1351 /* otherwise, do the auto-detect */
1356 if ((now - AnsiDetect) < 2)
1365 select(1, &rfds, NULL, NULL, &tv);
1366 if (FD_ISSET(0, &rfds)) {
1367 abuf[strlen(abuf) + 1] = 0;
1368 read(0, &abuf[strlen(abuf)], 1);
1370 } while (FD_ISSET(0, &rfds));
1372 for (a = 0; a < strlen(abuf); ++a) {
1373 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1374 && (abuf[a + 2] == '?')) {
1383 * Display key options (highlight hotkeys inside angle brackets)
1385 void keyopt(char *buf) {
1389 for (i=0; i<strlen(buf); ++i) {
1391 putc(buf[i], stdout);
1392 color(BRIGHT_MAGENTA);
1397 putc(buf[i], stdout);
1406 * Present a key-menu line choice type of thing
1408 char keymenu(char *menuprompt, char *menustring) {
1414 int display_prompt = 1;
1416 choices = num_tokens(menustring, '|');
1418 if (menuprompt != NULL) do_prompt = 1;
1419 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1422 if (display_prompt) {
1424 printf("%s ", menuprompt);
1427 for (i=0; i<choices; ++i) {
1428 extract(buf, menustring, i);
1438 if ( (do_prompt) && (ch=='?') ) {
1439 printf("\rOne of... ");
1441 for (i=0; i<choices; ++i) {
1442 extract(buf, menustring, i);
1451 for (i=0; i<choices; ++i) {
1452 extract(buf, menustring, i);
1453 for (c=1; c<strlen(buf); ++c) {
1454 if ( (ch == tolower(buf[c]))
1456 && (buf[c+1]=='>') ) {
1457 for (a=0; a<strlen(buf); ++a) {
1458 if ( (a!=(c-1)) && (a!=(c+1))) {
1459 putc(buf[a], stdout);