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 if (((now - start_time) > SLEEPING)
452 && (SLEEPING != 0) && (getppid() == 1)) {
453 printf("Sleeping? Call again.\n");
456 select(1, &rfds, NULL, NULL, &tv);
457 } while (!FD_ISSET(0, &rfds));
462 /* At this point, there's input, so fetch it.
463 * (There's a hole in the bucket...)
473 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
474 && ((a < 32) || (a > 126)))
482 { /* Returns 1 for yes, 0 for no */
498 /* Returns 1 for yes, 0 for no, arg is default value */
521 /* Gets a line from the terminal */
522 /* string == Pointer to string buffer */
523 /* lim == Maximum length - if negative, no-show */
524 void getline(char *string, int lim)
538 if ((a == 8) && (strlen(string) == 0))
540 if ((a != 13) && (a != 8) && (strlen(string) == lim))
542 if ((a == 8) && (string[0] != 0)) {
543 string[strlen(string) - 1] = 0;
549 if ((a == 13) || (a == 10)) {
569 * strprompt() - prompt for a string, print the existing value and
570 * allow the user to press return to keep it...
572 void strprompt(char *prompt, char *str, int len)
577 printf("%s ", prompt);
580 color(BRIGHT_MAGENTA);
594 * boolprompt() - prompt for a yes/no, print the existing value and
595 * allow the user to press return to keep it...
597 int boolprompt(char *prompt, int prev_val)
602 printf("%s ", prompt);
605 color(BRIGHT_MAGENTA);
606 printf("%s", (prev_val ? "Yes" : "No"));
610 r = (yesno_d(prev_val));
616 * intprompt() - like strprompt(), except for an integer
617 * (note that it RETURNS the new value!)
619 int intprompt(char *prompt, int ival, int imin, int imax)
627 snprintf(buf, sizeof buf, "%d", i);
628 strprompt(prompt, buf, 15);
630 for (p=0; p<strlen(buf); ++p) {
631 if ( (!isdigit(buf[p]))
632 && ( (buf[p]!='-') || (p!=0) ) )
636 printf("*** Must be no less than %d.\n", imin);
638 printf("*** Must be no more than %d.\n", imax);
639 } while ((i < imin) || (i > imax));
644 * newprompt() - prompt for a string with no existing value
645 * (clears out string buffer first)
647 void newprompt(char *prompt, char *str, int len)
649 color(BRIGHT_MAGENTA);
650 printf("%s", prompt);
658 { /* returns a lower case value */
667 * parse the citadel.rc file
669 void load_command_set(void)
674 struct citcmd *lastcmd = NULL;
679 /* first, set up some defaults for non-required variables */
681 strcpy(editor_path, "");
682 strcpy(printcmd, "");
683 strcpy(rc_username, "");
684 strcpy(rc_password, "");
687 rc_allow_attachments = 0;
688 rc_remember_passwords = 0;
689 strcpy(rc_exp_cmd, "");
690 rc_display_message_numbers = 0;
691 rc_force_mail_prompts = 0;
693 strcpy(rc_url_cmd, "");
695 /* now try to open the citadel.rc file */
698 if (getenv("HOME") != NULL) {
699 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
700 ccfile = fopen(buf, "r");
702 if (ccfile == NULL) {
703 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
705 if (ccfile == NULL) {
706 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
707 ccfile = fopen(buf, "r");
709 if (ccfile == NULL) {
710 ccfile = fopen("./citadel.rc", "r");
712 if (ccfile == NULL) {
713 perror("commands: cannot open citadel.rc");
716 while (fgets(buf, sizeof buf, ccfile) != NULL) {
717 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
718 buf[strlen(buf) - 1] = 0;
720 if (!strncasecmp(buf, "editor=", 7))
721 strcpy(editor_path, &buf[7]);
723 if (!strncasecmp(buf, "printcmd=", 9))
724 strcpy(printcmd, &buf[9]);
726 if (!strncasecmp(buf, "expcmd=", 7))
727 strcpy(rc_exp_cmd, &buf[7]);
729 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
730 have_xterm = (char) atoi(&buf[24]);
732 if (!strncasecmp(buf, "use_floors=", 11)) {
733 if (!strcasecmp(&buf[11], "yes"))
734 rc_floor_mode = RC_YES;
735 if (!strcasecmp(&buf[11], "no"))
736 rc_floor_mode = RC_NO;
737 if (!strcasecmp(&buf[11], "default"))
738 rc_floor_mode = RC_DEFAULT;
740 if (!strncasecmp(buf, "beep=", 5)) {
741 rc_exp_beep = atoi(&buf[5]);
743 if (!strncasecmp(buf, "allow_attachments=", 18)) {
744 rc_allow_attachments = atoi(&buf[18]);
746 if (!strncasecmp(buf, "remember_passwords=", 19)) {
747 rc_remember_passwords = atoi(&buf[19]);
749 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
750 rc_display_message_numbers = atoi(&buf[24]);
752 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
753 rc_force_mail_prompts = atoi(&buf[19]);
755 if (!strncasecmp(buf, "ansi_color=", 11)) {
756 if (!strncasecmp(&buf[11], "on", 2))
758 if (!strncasecmp(&buf[11], "auto", 4))
759 rc_ansi_color = 2; /* autodetect */
760 if (!strncasecmp(&buf[11], "user", 4))
761 rc_ansi_color = 3; /* user config */
763 if (!strncasecmp(buf, "username=", 9))
764 strcpy(rc_username, &buf[9]);
766 if (!strncasecmp(buf, "password=", 9))
767 strcpy(rc_password, &buf[9]);
769 if (!strncasecmp(buf, "urlcmd=", 7))
770 strcpy(rc_url_cmd, &buf[7]);
772 if (!strncasecmp(buf, "cmd=", 4)) {
773 strcpy(buf, &buf[4]);
775 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
777 cptr->c_cmdnum = atoi(buf);
778 for (d = strlen(buf); d >= 0; --d)
781 strcpy(buf, &buf[b + 1]);
783 cptr->c_axlevel = atoi(buf);
784 for (d = strlen(buf); d >= 0; --d)
787 strcpy(buf, &buf[b + 1]);
789 for (a = 0; a < 5; ++a)
790 cptr->c_keys[a][0] = 0;
794 buf[strlen(buf) + 1] = 0;
795 while (strlen(buf) > 0) {
797 for (d = strlen(buf); d >= 0; --d)
800 strncpy(cptr->c_keys[a], buf, b);
801 cptr->c_keys[a][b] = 0;
803 strcpy(buf, &buf[b + 1]);
813 lastcmd->next = cptr;
823 * return the key associated with a command
825 char keycmd(char *cmdstr)
829 for (a = 0; a < strlen(cmdstr); ++a)
830 if (cmdstr[a] == '&')
831 return (tolower(cmdstr[a + 1]));
837 * Output the string from a key command without the ampersand
838 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
840 char *cmd_expand(char *strbuf, int mode)
848 for (a = 0; a < strlen(exp); ++a) {
849 if (strbuf[a] == '&') {
852 strcpy(&exp[a], &exp[a + 1]);
856 strcpy(buf, &exp[a + 2]);
862 if (!strncmp(&exp[a], "^r", 2)) {
864 strcpy(&exp[a], room_name);
865 strcat(exp, &buf[a + 2]);
867 if (!strncmp(&exp[a], "^c", 2)) {
869 strcpy(&exp[a + 1], &exp[a + 2]);
879 * Comparison function to determine if entered commands match a
880 * command loaded from the config file.
882 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
893 for (a = 0; a < ncomp; ++a) {
894 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
895 || (cptr->c_axlevel > cmdax))
903 * This function returns 1 if a given command requires a string input
905 int requires_string(struct citcmd *cptr, int ncomp)
910 strcpy(buf, cptr->c_keys[ncomp - 1]);
911 for (a = 0; a < strlen(buf); ++a) {
920 * Input a command at the main prompt.
921 * This function returns an integer command number. If the command prompts
922 * for a string then it is placed in the supplied buffer.
924 int getcmd(char *argbuf)
936 * Starting a new command now, so set sigcaught to 0. This variable
937 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
938 * been interrupted by a keypress.
942 /* Switch color support on or off if we're in user mode */
943 if (rc_ansi_color == 3) {
944 if (userflags & US_COLOR)
949 /* if we're running in idiot mode, display a cute little menu */
950 IFNEXPERT formout("mainmenu");
952 print_express(); /* print express messages if there are any */
955 for (a = 0; a < 5; ++a)
957 /* now the room prompt... */
960 printf("\n%s", room_name);
962 printf("%c ", room_prompt(room_flags));
969 /* Handle the backspace key, but only if there's something
970 * to backspace over...
972 if ((ch == 8) && (cmdpos > 0)) {
973 back(cmdspaces[cmdpos - 1] + 1);
977 /* Spacebar invokes "lazy traversal" commands */
978 if ((ch == 32) && (cmdpos == 0)) {
979 this_lazy_cmd = next_lazy_cmd;
980 if (this_lazy_cmd == 13)
982 if (this_lazy_cmd == 5)
984 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
985 if (cptr->c_cmdnum == this_lazy_cmd) {
986 for (a = 0; a < 5; ++a)
987 if (cptr->c_keys[a][0] != 0)
988 printf("%s ", cmd_expand(
989 cptr->c_keys[a], 0));
991 return (this_lazy_cmd);
995 return (this_lazy_cmd);
997 /* Otherwise, process the command */
998 cmdbuf[cmdpos] = tolower(ch);
1000 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1001 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1003 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1004 cmdspaces[cmdpos] = strlen(
1005 cmd_expand(cptr->c_keys[cmdpos], 0));
1007 if ((cptr->c_keys[cmdpos + 1]) != 0)
1013 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1014 if (cmdmatch(cmdbuf, cptr, 5)) {
1015 /* We've found our command. */
1016 if (requires_string(cptr, cmdpos)) {
1017 getline(argbuf, 32);
1022 /* If this command is one that changes rooms,
1023 * then the next lazy-command (space bar)
1024 * should be "read new" instead of "goto"
1026 if ((cptr->c_cmdnum == 5)
1027 || (cptr->c_cmdnum == 6)
1028 || (cptr->c_cmdnum == 47)
1029 || (cptr->c_cmdnum == 52)
1030 || (cptr->c_cmdnum == 16)
1031 || (cptr->c_cmdnum == 20))
1034 return (cptr->c_cmdnum);
1040 pprintf("\rOne of ... \n");
1041 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1042 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1043 for (a = 0; a < 5; ++a) {
1044 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1050 pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1052 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1053 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1054 for (a = 0; a < cmdpos; ++a) {
1056 cmd_expand(cptr->c_keys[a], 0));
1071 * set tty modes. commands are:
1073 * 01- set to bbs mode
1074 * 2 - save current settings for later restoral
1075 * 3 - restore saved settings
1077 #ifdef HAVE_TERMIOS_H
1078 void sttybbs(int cmd)
1079 { /* SysV version of sttybbs() */
1080 struct termios live;
1081 static struct termios saved_settings;
1082 static int last_cmd = 0;
1089 if ((cmd == 0) || (cmd == 1)) {
1090 tcgetattr(0, &live);
1091 live.c_iflag = ISTRIP | IXON | IXANY;
1092 live.c_oflag = OPOST | ONLCR;
1093 live.c_lflag = ISIG | NOFLSH;
1095 live.c_cc[VINTR] = (-1);
1096 live.c_cc[VQUIT] = (-1);
1099 live.c_cc[VMIN] = 0;
1100 live.c_cc[VTIME] = 0;
1103 /* do we even need this stuff anymore? */
1104 /* live.c_line=0; */
1105 live.c_cc[VERASE] = 8;
1106 live.c_cc[VKILL] = 24;
1107 live.c_cc[VEOF] = 1;
1108 live.c_cc[VEOL] = 255;
1109 live.c_cc[VEOL2] = 0;
1110 live.c_cc[VSTART] = 0;
1111 tcsetattr(0, TCSADRAIN, &live);
1114 tcgetattr(0, &saved_settings);
1117 tcsetattr(0, TCSADRAIN, &saved_settings);
1121 void sttybbs(int cmd)
1122 { /* BSD version of sttybbs() */
1124 static struct sgttyb saved_settings;
1126 if ((cmd == 0) || (cmd == 1)) {
1128 live.sg_flags |= CBREAK;
1129 live.sg_flags |= CRMOD;
1130 live.sg_flags |= NL1;
1131 live.sg_flags &= ~ECHO;
1133 live.sg_flags |= NOFLSH;
1137 gtty(0, &saved_settings);
1140 stty(0, &saved_settings);
1147 * display_help() - help file viewer
1149 void display_help(char *name)
1156 * fmout() - Citadel text formatter and paginator
1159 int width, /* screen width to use */
1160 FILE *fp, /* file to read from, or NULL to read from server */
1161 char pagin, /* nonzero if we should use the paginator */
1162 int height, /* screen height to use */
1163 int starting_lp,/* starting value for lines_printed, -1 for global */
1164 char subst) /* nonzero if we should use hypertext mode */
1166 int a, b, c, d, old;
1172 num_urls = 0; /* Start with a clean slate of embedded URL's */
1174 if (starting_lp >= 0) {
1175 lines_printed = starting_lp;
1180 c = 1; /* c is the current pos */
1182 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1183 if (fp != NULL) { /* read from file */
1186 if (eof_flag == 0) {
1188 buffer[strlen(buffer) + 1] = 0;
1189 buffer[strlen(buffer)] = a;
1191 } else { /* read from server */
1193 serv_gets(&buffer[d]);
1194 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1195 buffer[strlen(buffer) - 1] = 0;
1196 if (!strcmp(&buffer[d], "000")) {
1199 while (isspace(buffer[strlen(buffer) - 1]))
1200 buffer[strlen(buffer) - 1] = 0;
1208 if ( (!strncasecmp(buffer, "http://", 7))
1209 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1210 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1211 for (a=0; a<strlen(urls[num_urls]); ++a) {
1212 b = urls[num_urls][a];
1213 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1214 || (b==13) || (b==9) || (b=='\"') )
1215 urls[num_urls][a] = 0;
1220 buffer[strlen(buffer) + 1] = 0;
1222 strcpy(buffer, &buffer[1]);
1229 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1231 if (((old == 13) || (old == 10)) && (isspace(real))) {
1234 lines_printed = checkpagin(lines_printed, pagin, height);
1241 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1242 printf("\n%s", aaa);
1246 lines_printed = checkpagin(lines_printed, pagin, height);
1253 if ((strlen(aaa) + c) > (width - 5)) {
1257 lines_printed = checkpagin(lines_printed, pagin, height);
1261 c = c + strlen(aaa);
1265 if ((a == 13) || (a == 10)) {
1266 printf("%s\n", aaa);
1269 lines_printed = checkpagin(lines_printed, pagin, height);
1270 if (sigcaught) goto OOPS;
1276 /* keypress caught; drain the server */
1279 } while (strcmp(aaa, "000"));
1281 FMTEND: printf("\n");
1283 lines_printed = checkpagin(lines_printed, pagin, height);
1289 * support ANSI color if defined
1291 void color(int colornum)
1293 static int is_bold = 0;
1294 static int hold_color, current_color;
1296 if (colornum == COLOR_PUSH) {
1297 hold_color = current_color;
1301 if (colornum == COLOR_POP) {
1306 current_color = colornum;
1308 /* When switching to dim white, actually output an 'original
1309 * pair' sequence -- this looks better on black-on-white
1312 if (colornum == DIM_WHITE)
1313 printf("\033[39;49m");
1315 printf("\033[3%d;40m", (colornum & 7));
1317 if ((colornum >= 8) && (is_bold == 0)) {
1320 } else if ((colornum < 8) && (is_bold == 1)) {
1328 void cls(int colornum)
1331 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1338 * Detect whether ANSI color is available (answerback)
1340 void send_ansi_detect(void)
1342 if (rc_ansi_color == 2) {
1349 void look_for_ansi(void)
1357 if (rc_ansi_color == 0) {
1359 } else if (rc_ansi_color == 1) {
1361 } else if (rc_ansi_color == 2) {
1363 /* otherwise, do the auto-detect */
1368 if ((now - AnsiDetect) < 2)
1377 select(1, &rfds, NULL, NULL, &tv);
1378 if (FD_ISSET(0, &rfds)) {
1379 abuf[strlen(abuf) + 1] = 0;
1380 read(0, &abuf[strlen(abuf)], 1);
1382 } while (FD_ISSET(0, &rfds));
1384 for (a = 0; a < strlen(abuf); ++a) {
1385 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1386 && (abuf[a + 2] == '?')) {
1395 * Display key options (highlight hotkeys inside angle brackets)
1397 void keyopt(char *buf) {
1401 for (i=0; i<strlen(buf); ++i) {
1403 putc(buf[i], stdout);
1404 color(BRIGHT_MAGENTA);
1409 putc(buf[i], stdout);
1418 * Present a key-menu line choice type of thing
1420 char keymenu(char *menuprompt, char *menustring) {
1426 int display_prompt = 1;
1428 choices = num_tokens(menustring, '|');
1430 if (menuprompt != NULL) do_prompt = 1;
1431 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1434 if (display_prompt) {
1436 printf("%s ", menuprompt);
1439 for (i=0; i<choices; ++i) {
1440 extract(buf, menustring, i);
1450 if ( (do_prompt) && (ch=='?') ) {
1451 printf("\rOne of... ");
1453 for (i=0; i<choices; ++i) {
1454 extract(buf, menustring, i);
1463 for (i=0; i<choices; ++i) {
1464 extract(buf, menustring, i);
1465 for (c=1; c<strlen(buf); ++c) {
1466 if ( (ch == tolower(buf[c]))
1468 && (buf[c+1]=='>') ) {
1469 for (a=0; a<strlen(buf); ++a) {
1470 if ( (a!=(c-1)) && (a!=(c+1))) {
1471 putc(buf[a], stdout);