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, "");
692 rc_encrypt = RC_DEFAULT;
693 rc_alt_semantics = 0;
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, "encrypt=", 8)) {
721 if (!strcasecmp(&buf[8], "yes"))
723 else if (!strcasecmp(&buf[8], "no"))
725 else if (!strcasecmp(&buf[8], "default"))
726 rc_encrypt = RC_DEFAULT;
729 if (!strncasecmp(buf, "editor=", 7))
730 strcpy(editor_path, &buf[7]);
732 if (!strncasecmp(buf, "printcmd=", 9))
733 strcpy(printcmd, &buf[9]);
735 if (!strncasecmp(buf, "expcmd=", 7))
736 strcpy(rc_exp_cmd, &buf[7]);
738 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
739 have_xterm = (char) atoi(&buf[24]);
741 if (!strncasecmp(buf, "use_floors=", 11)) {
742 if (!strcasecmp(&buf[11], "yes"))
743 rc_floor_mode = RC_YES;
744 if (!strcasecmp(&buf[11], "no"))
745 rc_floor_mode = RC_NO;
746 if (!strcasecmp(&buf[11], "default"))
747 rc_floor_mode = RC_DEFAULT;
749 if (!strncasecmp(buf, "beep=", 5)) {
750 rc_exp_beep = atoi(&buf[5]);
752 if (!strncasecmp(buf, "allow_attachments=", 18)) {
753 rc_allow_attachments = atoi(&buf[18]);
755 if (!strncasecmp(buf, "remember_passwords=", 19)) {
756 rc_remember_passwords = atoi(&buf[19]);
758 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
759 rc_display_message_numbers = atoi(&buf[24]);
761 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
762 rc_force_mail_prompts = atoi(&buf[19]);
764 if (!strncasecmp(buf, "ansi_color=", 11)) {
765 if (!strncasecmp(&buf[11], "on", 2))
767 if (!strncasecmp(&buf[11], "auto", 4))
768 rc_ansi_color = 2; /* autodetect */
769 if (!strncasecmp(&buf[11], "user", 4))
770 rc_ansi_color = 3; /* user config */
772 if (!strncasecmp(buf, "username=", 9))
773 strcpy(rc_username, &buf[9]);
775 if (!strncasecmp(buf, "password=", 9))
776 strcpy(rc_password, &buf[9]);
778 if (!strncasecmp(buf, "urlcmd=", 7))
779 strcpy(rc_url_cmd, &buf[7]);
781 if (!strncasecmp(buf, "alternate_semantics=", 20)) {
782 if (!strncasecmp(&buf[11], "yes", 3))
783 rc_alt_semantics = 1;
784 if (!strncasecmp(&buf[11], "no", 2))
785 rc_alt_semantics = 0;
787 if (!strncasecmp(buf, "cmd=", 4)) {
788 strcpy(buf, &buf[4]);
790 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
792 cptr->c_cmdnum = atoi(buf);
793 for (d = strlen(buf); d >= 0; --d)
796 strcpy(buf, &buf[b + 1]);
798 cptr->c_axlevel = atoi(buf);
799 for (d = strlen(buf); d >= 0; --d)
802 strcpy(buf, &buf[b + 1]);
804 for (a = 0; a < 5; ++a)
805 cptr->c_keys[a][0] = 0;
809 buf[strlen(buf) + 1] = 0;
810 while (strlen(buf) > 0) {
812 for (d = strlen(buf); d >= 0; --d)
815 strncpy(cptr->c_keys[a], buf, b);
816 cptr->c_keys[a][b] = 0;
818 strcpy(buf, &buf[b + 1]);
828 lastcmd->next = cptr;
838 * return the key associated with a command
840 char keycmd(char *cmdstr)
844 for (a = 0; a < strlen(cmdstr); ++a)
845 if (cmdstr[a] == '&')
846 return (tolower(cmdstr[a + 1]));
852 * Output the string from a key command without the ampersand
853 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
855 char *cmd_expand(char *strbuf, int mode)
863 for (a = 0; a < strlen(exp); ++a) {
864 if (strbuf[a] == '&') {
867 strcpy(&exp[a], &exp[a + 1]);
871 strcpy(buf, &exp[a + 2]);
877 if (!strncmp(&exp[a], "^r", 2)) {
879 strcpy(&exp[a], room_name);
880 strcat(exp, &buf[a + 2]);
882 if (!strncmp(&exp[a], "^c", 2)) {
884 strcpy(&exp[a + 1], &exp[a + 2]);
894 * Comparison function to determine if entered commands match a
895 * command loaded from the config file.
897 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
908 for (a = 0; a < ncomp; ++a) {
909 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
910 || (cptr->c_axlevel > cmdax))
918 * This function returns 1 if a given command requires a string input
920 int requires_string(struct citcmd *cptr, int ncomp)
925 strcpy(buf, cptr->c_keys[ncomp - 1]);
926 for (a = 0; a < strlen(buf); ++a) {
935 * Input a command at the main prompt.
936 * This function returns an integer command number. If the command prompts
937 * for a string then it is placed in the supplied buffer.
939 int getcmd(char *argbuf)
951 * Starting a new command now, so set sigcaught to 0. This variable
952 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
953 * been interrupted by a keypress.
957 /* Switch color support on or off if we're in user mode */
958 if (rc_ansi_color == 3) {
959 if (userflags & US_COLOR)
964 /* if we're running in idiot mode, display a cute little menu */
965 IFNEXPERT formout("mainmenu");
967 print_express(); /* print express messages if there are any */
970 for (a = 0; a < 5; ++a)
972 /* now the room prompt... */
975 printf("\n%s", room_name);
977 printf("%c ", room_prompt(room_flags));
984 /* Handle the backspace key, but only if there's something
985 * to backspace over...
987 if ((ch == 8) && (cmdpos > 0)) {
988 back(cmdspaces[cmdpos - 1] + 1);
992 /* Spacebar invokes "lazy traversal" commands */
993 if ((ch == 32) && (cmdpos == 0)) {
994 this_lazy_cmd = next_lazy_cmd;
995 if (this_lazy_cmd == 13)
997 if (this_lazy_cmd == 5)
999 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1000 if (cptr->c_cmdnum == this_lazy_cmd) {
1001 for (a = 0; a < 5; ++a)
1002 if (cptr->c_keys[a][0] != 0)
1003 printf("%s ", cmd_expand(
1004 cptr->c_keys[a], 0));
1006 return (this_lazy_cmd);
1010 return (this_lazy_cmd);
1012 /* Otherwise, process the command */
1013 cmdbuf[cmdpos] = tolower(ch);
1015 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1016 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1018 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1019 cmdspaces[cmdpos] = strlen(
1020 cmd_expand(cptr->c_keys[cmdpos], 0));
1022 if ((cptr->c_keys[cmdpos + 1]) != 0)
1028 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1029 if (cmdmatch(cmdbuf, cptr, 5)) {
1030 /* We've found our command. */
1031 if (requires_string(cptr, cmdpos)) {
1032 getline(argbuf, 32);
1037 /* If this command is one that changes rooms,
1038 * then the next lazy-command (space bar)
1039 * should be "read new" instead of "goto"
1041 if ((cptr->c_cmdnum == 5)
1042 || (cptr->c_cmdnum == 6)
1043 || (cptr->c_cmdnum == 47)
1044 || (cptr->c_cmdnum == 52)
1045 || (cptr->c_cmdnum == 16)
1046 || (cptr->c_cmdnum == 20))
1049 return (cptr->c_cmdnum);
1055 pprintf("\rOne of ... \n");
1056 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1057 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1058 for (a = 0; a < 5; ++a) {
1059 pprintf("%s ", cmd_expand(cptr->c_keys[a], 1));
1065 pprintf("\n%s%c ", room_name, room_prompt(room_flags));
1067 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1068 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1069 for (a = 0; a < cmdpos; ++a) {
1071 cmd_expand(cptr->c_keys[a], 0));
1086 * set tty modes. commands are:
1088 * 01- set to bbs mode
1089 * 2 - save current settings for later restoral
1090 * 3 - restore saved settings
1092 #ifdef HAVE_TERMIOS_H
1093 void sttybbs(int cmd)
1094 { /* SysV version of sttybbs() */
1095 struct termios live;
1096 static struct termios saved_settings;
1097 static int last_cmd = 0;
1104 if ((cmd == 0) || (cmd == 1)) {
1105 tcgetattr(0, &live);
1106 live.c_iflag = ISTRIP | IXON | IXANY;
1107 live.c_oflag = OPOST | ONLCR;
1108 live.c_lflag = ISIG | NOFLSH;
1110 live.c_cc[VINTR] = (-1);
1111 live.c_cc[VQUIT] = (-1);
1114 live.c_cc[VMIN] = 0;
1115 live.c_cc[VTIME] = 0;
1118 /* do we even need this stuff anymore? */
1119 /* live.c_line=0; */
1120 live.c_cc[VERASE] = 8;
1121 live.c_cc[VKILL] = 24;
1122 live.c_cc[VEOF] = 1;
1123 live.c_cc[VEOL] = 255;
1124 live.c_cc[VEOL2] = 0;
1125 live.c_cc[VSTART] = 0;
1126 tcsetattr(0, TCSADRAIN, &live);
1129 tcgetattr(0, &saved_settings);
1132 tcsetattr(0, TCSADRAIN, &saved_settings);
1136 void sttybbs(int cmd)
1137 { /* BSD version of sttybbs() */
1139 static struct sgttyb saved_settings;
1141 if ((cmd == 0) || (cmd == 1)) {
1143 live.sg_flags |= CBREAK;
1144 live.sg_flags |= CRMOD;
1145 live.sg_flags |= NL1;
1146 live.sg_flags &= ~ECHO;
1148 live.sg_flags |= NOFLSH;
1152 gtty(0, &saved_settings);
1155 stty(0, &saved_settings);
1162 * display_help() - help file viewer
1164 void display_help(char *name)
1171 * fmout() - Citadel text formatter and paginator
1174 int width, /* screen width to use */
1175 FILE *fp, /* file to read from, or NULL to read from server */
1176 char pagin, /* nonzero if we should use the paginator */
1177 int height, /* screen height to use */
1178 int starting_lp,/* starting value for lines_printed, -1 for global */
1179 char subst) /* nonzero if we should use hypertext mode */
1181 int a, b, c, d, old;
1187 num_urls = 0; /* Start with a clean slate of embedded URL's */
1189 if (starting_lp >= 0) {
1190 lines_printed = starting_lp;
1195 c = 1; /* c is the current pos */
1197 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1198 if (fp != NULL) { /* read from file */
1201 if (eof_flag == 0) {
1203 buffer[strlen(buffer) + 1] = 0;
1204 buffer[strlen(buffer)] = a;
1206 } else { /* read from server */
1208 serv_gets(&buffer[d]);
1209 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1210 buffer[strlen(buffer) - 1] = 0;
1211 if (!strcmp(&buffer[d], "000")) {
1214 while (isspace(buffer[strlen(buffer) - 1]))
1215 buffer[strlen(buffer) - 1] = 0;
1223 if ( (!strncasecmp(buffer, "http://", 7))
1224 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1225 safestrncpy(urls[num_urls], buffer, (SIZ-1));
1226 for (a=0; a<strlen(urls[num_urls]); ++a) {
1227 b = urls[num_urls][a];
1228 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1229 || (b==13) || (b==9) || (b=='\"') )
1230 urls[num_urls][a] = 0;
1235 buffer[strlen(buffer) + 1] = 0;
1237 strcpy(buffer, &buffer[1]);
1244 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1246 if (((old == 13) || (old == 10)) && (isspace(real))) {
1249 lines_printed = checkpagin(lines_printed, pagin, height);
1256 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1257 printf("\n%s", aaa);
1261 lines_printed = checkpagin(lines_printed, pagin, height);
1268 if ((strlen(aaa) + c) > (width - 5)) {
1272 lines_printed = checkpagin(lines_printed, pagin, height);
1276 c = c + strlen(aaa);
1280 if ((a == 13) || (a == 10)) {
1281 printf("%s\n", aaa);
1284 lines_printed = checkpagin(lines_printed, pagin, height);
1285 if (sigcaught) goto OOPS;
1291 /* keypress caught; drain the server */
1294 } while (strcmp(aaa, "000"));
1296 FMTEND: printf("\n");
1298 lines_printed = checkpagin(lines_printed, pagin, height);
1304 * support ANSI color if defined
1306 void color(int colornum)
1308 static int is_bold = 0;
1309 static int hold_color, current_color;
1311 if (colornum == COLOR_PUSH) {
1312 hold_color = current_color;
1316 if (colornum == COLOR_POP) {
1321 current_color = colornum;
1323 /* When switching to dim white, actually output an 'original
1324 * pair' sequence -- this looks better on black-on-white
1327 if (colornum == DIM_WHITE)
1328 printf("\033[39;49m");
1330 printf("\033[3%d;40m", (colornum & 7));
1332 if ((colornum >= 8) && (is_bold == 0)) {
1335 } else if ((colornum < 8) && (is_bold == 1)) {
1343 void cls(int colornum)
1346 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1353 * Detect whether ANSI color is available (answerback)
1355 void send_ansi_detect(void)
1357 if (rc_ansi_color == 2) {
1364 void look_for_ansi(void)
1372 if (rc_ansi_color == 0) {
1374 } else if (rc_ansi_color == 1) {
1376 } else if (rc_ansi_color == 2) {
1378 /* otherwise, do the auto-detect */
1383 if ((now - AnsiDetect) < 2)
1392 select(1, &rfds, NULL, NULL, &tv);
1393 if (FD_ISSET(0, &rfds)) {
1394 abuf[strlen(abuf) + 1] = 0;
1395 read(0, &abuf[strlen(abuf)], 1);
1397 } while (FD_ISSET(0, &rfds));
1399 for (a = 0; a < strlen(abuf); ++a) {
1400 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1401 && (abuf[a + 2] == '?')) {
1410 * Display key options (highlight hotkeys inside angle brackets)
1412 void keyopt(char *buf) {
1416 for (i=0; i<strlen(buf); ++i) {
1418 putc(buf[i], stdout);
1419 color(BRIGHT_MAGENTA);
1424 putc(buf[i], stdout);
1433 * Present a key-menu line choice type of thing
1435 char keymenu(char *menuprompt, char *menustring) {
1441 int display_prompt = 1;
1443 choices = num_tokens(menustring, '|');
1445 if (menuprompt != NULL) do_prompt = 1;
1446 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1449 if (display_prompt) {
1451 printf("%s ", menuprompt);
1454 for (i=0; i<choices; ++i) {
1455 extract(buf, menustring, i);
1465 if ( (do_prompt) && (ch=='?') ) {
1466 printf("\rOne of... ");
1468 for (i=0; i<choices; ++i) {
1469 extract(buf, menustring, i);
1478 for (i=0; i<choices; ++i) {
1479 extract(buf, menustring, i);
1480 for (c=1; c<strlen(buf); ++c) {
1481 if ( (ch == tolower(buf[c]))
1483 && (buf[c+1]=='>') ) {
1484 for (a=0; a<strlen(buf); ++a) {
1485 if ( (a!=(c-1)) && (a!=(c+1))) {
1486 putc(buf[a], stdout);