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;
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 (strlen(rc_exp_cmd) > 0) {
220 outpipe = popen(rc_exp_cmd, "w");
221 if (outpipe != NULL) {
222 /* Header derived from flags */
225 "Please log off now, as requested ");
227 fprintf(outpipe, "Broadcast message ");
229 fprintf(outpipe, "Chat request ");
231 fprintf(outpipe, "Message ");
232 /* Timestamp. Can this be improved? */
233 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
234 fprintf(outpipe, "at 12:%02d%cm",
236 stamp->tm_hour ? 'p' : 'a');
237 else if (stamp->tm_hour > 12) /* pm */
238 fprintf(outpipe, "at %d:%02dpm",
242 fprintf(outpipe, "at %d:%02dam",
243 stamp->tm_hour, stamp->tm_min);
244 fprintf(outpipe, " from %s", sender);
245 if (strncmp(serv_info.serv_nodename, node, 32))
246 fprintf(outpipe, " @%s", node);
247 fprintf(outpipe, ":\n");
248 while (serv_gets(buf), strcmp(buf, "000")) {
249 fprintf(outpipe, "%s\n", buf);
252 if (express_msgs == 0)
257 /* fall back to built-in express message display */
260 /* Header derived from flags */
262 printf("Please log off now, as requested ");
264 printf("Broadcast message ");
266 printf("Chat request ");
270 /* Timestamp. Can this be improved? */
271 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)/* 12am/12pm */
272 printf("at 12:%02d%cm", stamp->tm_min,
273 stamp->tm_hour ? 'p' : 'a');
274 else if (stamp->tm_hour > 12) /* pm */
275 printf("at %d:%02dpm",
276 stamp->tm_hour - 12, stamp->tm_min);
278 printf("at %d:%02dam", stamp->tm_hour, stamp->tm_min);
281 printf(" from %s", sender);
283 /* Remote node, if any */
284 if (strncmp(serv_info.serv_nodename, node, 32))
285 printf(" @%s", node);
289 while (serv_gets(buf), strcmp(buf, "000")) {
296 /* If the page is a Logoff Request, honor it. */
304 void set_keepalives(int s)
306 keepalives_enabled = (char) s;
310 * This loop handles the "keepalive" messages sent to the server when idling.
313 static time_t idlet = 0;
314 static void really_do_keepalive(void) {
318 if (keepalives_enabled == KA_YES) {
323 if (ok_to_interrupt == 1) {
324 printf("\r%64s\r", "");
326 printf("%s%c ", room_name,
327 room_prompt(room_flags));
334 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
335 encapsulated interface; in theory there should be no need to touch these
336 globals outside of the async_ka_* functions. */
338 #ifdef THREADED_CLIENT
339 static pthread_t ka_thr_handle;
340 static int ka_thr_active = 0;
341 static int async_ka_enabled = 0;
343 static void *ka_thread(void *arg)
345 really_do_keepalive();
346 pthread_detach(ka_thr_handle);
351 /* start up a thread to handle a keepalive in the background */
352 static void async_ka_exec(void)
354 if (!ka_thr_active) {
356 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
357 perror("pthread_create");
362 #endif /* THREADED_CLIENT */
364 static void do_keepalive(void)
369 if ((now - idlet) < ((long) S_KEEPALIVE))
372 /* Do a space-backspace to keep telnet sessions from idling out */
376 #ifdef THREADED_CLIENT
377 if (async_ka_enabled)
381 really_do_keepalive();
385 /* Now the actual async-keepalve API that we expose to higher levels:
386 async_ka_start() and async_ka_end(). These do nothing when we don't have
387 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
389 /* wait for a background keepalive to complete. this must be done before
390 attempting any further server requests! */
391 void async_ka_end(void)
393 #ifdef THREADED_CLIENT
395 pthread_join(ka_thr_handle, NULL);
401 /* tell do_keepalive() that keepalives are asynchronous. */
402 void async_ka_start(void)
404 #ifdef THREADED_CLIENT
411 { /* get a character from the keyboard, with */
412 int a; /* the watchdog timer in effect if necessary */
415 time_t start_time, now;
424 /* This loop waits for keyboard input. If the keepalive
425 * timer expires, it sends a keepalive to the server if
426 * necessary and then waits again.
433 tv.tv_sec = S_KEEPALIVE;
437 if (((now - start_time) > SLEEPING)
438 && (SLEEPING != 0) && (getppid() == 1)) {
439 printf("Sleeping? Call again.\n");
442 select(1, &rfds, NULL, NULL, &tv);
443 } while (!FD_ISSET(0, &rfds));
448 /* At this point, there's input, so fetch it.
449 * (There's a hole in the bucket...)
459 if (((a != 4) && (a != 13) && (a != 8) && (a != NEXT_KEY) && (a != STOP_KEY))
460 && ((a < 32) || (a > 126)))
468 { /* Returns 1 for yes, 0 for no */
484 /* Returns 1 for yes, 0 for no, arg is default value */
507 /* Gets a line from the terminal */
508 /* string == Pointer to string buffer */
509 /* lim == Maximum length - if negative, no-show */
510 void getline(char *string, int lim)
524 if ((a == 8) && (strlen(string) == 0))
526 if ((a != 13) && (a != 8) && (strlen(string) == lim))
528 if ((a == 8) && (string[0] != 0)) {
529 string[strlen(string) - 1] = 0;
535 if ((a == 13) || (a == 10)) {
555 * strprompt() - prompt for a string, print the existing value and
556 * allow the user to press return to keep it...
558 void strprompt(char *prompt, char *str, int len)
563 printf("%s ", prompt);
566 color(BRIGHT_MAGENTA);
580 * boolprompt() - prompt for a yes/no, print the existing value and
581 * allow the user to press return to keep it...
583 int boolprompt(char *prompt, int prev_val)
588 printf("%s ", prompt);
591 color(BRIGHT_MAGENTA);
592 printf("%s", (prev_val ? "Yes" : "No"));
596 r = (yesno_d(prev_val));
602 * intprompt() - like strprompt(), except for an integer
603 * (note that it RETURNS the new value!)
605 int intprompt(char *prompt, int ival, int imin, int imax)
613 snprintf(buf, sizeof buf, "%d", i);
614 strprompt(prompt, buf, 15);
616 for (p=0; p<strlen(buf); ++p) {
617 if ( (!isdigit(buf[p]))
618 && ( (buf[p]!='-') || (p!=0) ) )
622 printf("*** Must be no less than %d.\n", imin);
624 printf("*** Must be no more than %d.\n", imax);
625 } while ((i < imin) || (i > imax));
630 * newprompt() - prompt for a string with no existing value
631 * (clears out string buffer first)
633 void newprompt(char *prompt, char *str, int len)
635 color(BRIGHT_MAGENTA);
636 printf("%s", prompt);
644 { /* returns a lower case value */
653 * parse the citadel.rc file
655 void load_command_set(void)
660 struct citcmd *lastcmd = NULL;
665 /* first, set up some defaults for non-required variables */
667 strcpy(editor_path, "");
668 strcpy(printcmd, "");
669 strcpy(rc_username, "");
670 strcpy(rc_password, "");
673 rc_allow_attachments = 0;
674 rc_remember_passwords = 0;
675 strcpy(rc_exp_cmd, "");
676 rc_display_message_numbers = 0;
677 rc_force_mail_prompts = 0;
679 strcpy(rc_url_cmd, "");
681 /* now try to open the citadel.rc file */
684 if (getenv("HOME") != NULL) {
685 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
686 ccfile = fopen(buf, "r");
688 if (ccfile == NULL) {
689 ccfile = fopen("/usr/local/lib/citadel.rc", "r");
691 if (ccfile == NULL) {
692 snprintf(buf, sizeof buf, "%s/citadel.rc", BBSDIR);
693 ccfile = fopen(buf, "r");
695 if (ccfile == NULL) {
696 ccfile = fopen("./citadel.rc", "r");
698 if (ccfile == NULL) {
699 perror("commands: cannot open citadel.rc");
702 while (fgets(buf, 256, ccfile) != NULL) {
703 while ((strlen(buf) > 0) ? (isspace(buf[strlen(buf) - 1])) : 0)
704 buf[strlen(buf) - 1] = 0;
706 if (!strncasecmp(buf, "editor=", 7))
707 strcpy(editor_path, &buf[7]);
709 if (!strncasecmp(buf, "printcmd=", 9))
710 strcpy(printcmd, &buf[9]);
712 if (!strncasecmp(buf, "expcmd=", 7))
713 strcpy(rc_exp_cmd, &buf[7]);
715 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
716 have_xterm = (char) atoi(&buf[24]);
718 if (!strncasecmp(buf, "use_floors=", 11)) {
719 if (!strcasecmp(&buf[11], "yes"))
720 rc_floor_mode = RC_YES;
721 if (!strcasecmp(&buf[11], "no"))
722 rc_floor_mode = RC_NO;
723 if (!strcasecmp(&buf[11], "default"))
724 rc_floor_mode = RC_DEFAULT;
726 if (!strncasecmp(buf, "beep=", 5)) {
727 rc_exp_beep = atoi(&buf[5]);
729 if (!strncasecmp(buf, "allow_attachments=", 18)) {
730 rc_allow_attachments = atoi(&buf[18]);
732 if (!strncasecmp(buf, "remember_passwords=", 19)) {
733 rc_remember_passwords = atoi(&buf[19]);
735 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
736 rc_display_message_numbers = atoi(&buf[24]);
738 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
739 rc_force_mail_prompts = atoi(&buf[19]);
741 if (!strncasecmp(buf, "ansi_color=", 11)) {
742 if (!strncasecmp(&buf[11], "on", 2))
744 if (!strncasecmp(&buf[11], "auto", 4))
745 rc_ansi_color = 2; /* autodetect */
746 if (!strncasecmp(&buf[11], "user", 4))
747 rc_ansi_color = 3; /* user config */
749 if (!strncasecmp(buf, "username=", 9))
750 strcpy(rc_username, &buf[9]);
752 if (!strncasecmp(buf, "password=", 9))
753 strcpy(rc_password, &buf[9]);
755 if (!strncasecmp(buf, "urlcmd=", 7))
756 strcpy(rc_url_cmd, &buf[7]);
758 if (!strncasecmp(buf, "cmd=", 4)) {
759 strcpy(buf, &buf[4]);
761 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
763 cptr->c_cmdnum = atoi(buf);
764 for (d = strlen(buf); d >= 0; --d)
767 strcpy(buf, &buf[b + 1]);
769 cptr->c_axlevel = atoi(buf);
770 for (d = strlen(buf); d >= 0; --d)
773 strcpy(buf, &buf[b + 1]);
775 for (a = 0; a < 5; ++a)
776 cptr->c_keys[a][0] = 0;
780 buf[strlen(buf) + 1] = 0;
781 while (strlen(buf) > 0) {
783 for (d = strlen(buf); d >= 0; --d)
786 strncpy(cptr->c_keys[a], buf, b);
787 cptr->c_keys[a][b] = 0;
789 strcpy(buf, &buf[b + 1]);
799 lastcmd->next = cptr;
809 * return the key associated with a command
811 char keycmd(char *cmdstr)
815 for (a = 0; a < strlen(cmdstr); ++a)
816 if (cmdstr[a] == '&')
817 return (tolower(cmdstr[a + 1]));
823 * Output the string from a key command without the ampersand
824 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
826 char *cmd_expand(char *strbuf, int mode)
834 for (a = 0; a < strlen(exp); ++a) {
835 if (strbuf[a] == '&') {
838 strcpy(&exp[a], &exp[a + 1]);
842 strcpy(buf, &exp[a + 2]);
848 if (!strncmp(&exp[a], "^r", 2)) {
850 strcpy(&exp[a], room_name);
851 strcat(exp, &buf[a + 2]);
853 if (!strncmp(&exp[a], "^c", 2)) {
855 strcpy(&exp[a + 1], &exp[a + 2]);
865 * Comparison function to determine if entered commands match a
866 * command loaded from the config file.
868 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
879 for (a = 0; a < ncomp; ++a) {
880 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
881 || (cptr->c_axlevel > cmdax))
889 * This function returns 1 if a given command requires a string input
891 int requires_string(struct citcmd *cptr, int ncomp)
896 strcpy(buf, cptr->c_keys[ncomp - 1]);
897 for (a = 0; a < strlen(buf); ++a) {
906 * Input a command at the main prompt.
907 * This function returns an integer command number. If the command prompts
908 * for a string then it is placed in the supplied buffer.
910 int getcmd(char *argbuf)
922 * Starting a new command now, so set sigcaught to 0. This variable
923 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
924 * been interrupted by a keypress.
928 /* Switch color support on or off if we're in user mode */
929 if (rc_ansi_color == 3) {
930 if (userflags & US_COLOR)
935 /* if we're running in idiot mode, display a cute little menu */
936 IFNEXPERT formout("mainmenu");
938 print_express(); /* print express messages if there are any */
941 for (a = 0; a < 5; ++a)
943 /* now the room prompt... */
946 printf("\n%s", room_name);
948 printf("%c ", room_prompt(room_flags));
955 /* Handle the backspace key, but only if there's something
956 * to backspace over...
958 if ((ch == 8) && (cmdpos > 0)) {
959 back(cmdspaces[cmdpos - 1] + 1);
963 /* Spacebar invokes "lazy traversal" commands */
964 if ((ch == 32) && (cmdpos == 0)) {
965 this_lazy_cmd = next_lazy_cmd;
966 if (this_lazy_cmd == 13)
968 if (this_lazy_cmd == 5)
970 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
971 if (cptr->c_cmdnum == this_lazy_cmd) {
972 for (a = 0; a < 5; ++a)
973 if (cptr->c_keys[a][0] != 0)
974 printf("%s ", cmd_expand(
975 cptr->c_keys[a], 0));
977 return (this_lazy_cmd);
981 return (this_lazy_cmd);
983 /* Otherwise, process the command */
984 cmdbuf[cmdpos] = tolower(ch);
986 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
987 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
989 printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
990 cmdspaces[cmdpos] = strlen(
991 cmd_expand(cptr->c_keys[cmdpos], 0));
993 if ((cptr->c_keys[cmdpos + 1]) != 0)
999 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1000 if (cmdmatch(cmdbuf, cptr, 5)) {
1001 /* We've found our command. */
1002 if (requires_string(cptr, cmdpos)) {
1003 getline(argbuf, 32);
1008 /* If this command is one that changes rooms,
1009 * then the next lazy-command (space bar)
1010 * should be "read new" instead of "goto"
1012 if ((cptr->c_cmdnum == 5)
1013 || (cptr->c_cmdnum == 6)
1014 || (cptr->c_cmdnum == 47)
1015 || (cptr->c_cmdnum == 52)
1016 || (cptr->c_cmdnum == 16)
1017 || (cptr->c_cmdnum == 20))
1020 return (cptr->c_cmdnum);
1026 printf("\rOne of ... \n");
1027 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1028 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1029 for (a = 0; a < 5; ++a) {
1030 printf("%s ", cmd_expand(cptr->c_keys[a], 1));
1036 printf("\n%s%c ", room_name, room_prompt(room_flags));
1038 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1039 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1040 for (a = 0; a < cmdpos; ++a) {
1042 cmd_expand(cptr->c_keys[a], 0));
1057 * set tty modes. commands are:
1059 * 01- set to bbs mode
1060 * 2 - save current settings for later restoral
1061 * 3 - restore saved settings
1063 #ifdef HAVE_TERMIOS_H
1064 void sttybbs(int cmd)
1065 { /* SysV version of sttybbs() */
1066 struct termios live;
1067 static struct termios saved_settings;
1068 static int last_cmd = 0;
1075 if ((cmd == 0) || (cmd == 1)) {
1076 tcgetattr(0, &live);
1077 live.c_iflag = ISTRIP | IXON | IXANY;
1078 live.c_oflag = OPOST | ONLCR;
1079 live.c_lflag = ISIG | NOFLSH;
1081 live.c_cc[VINTR] = (-1);
1082 live.c_cc[VQUIT] = (-1);
1085 live.c_cc[VMIN] = 0;
1086 live.c_cc[VTIME] = 0;
1089 /* do we even need this stuff anymore? */
1090 /* live.c_line=0; */
1091 live.c_cc[VERASE] = 8;
1092 live.c_cc[VKILL] = 24;
1093 live.c_cc[VEOF] = 1;
1094 live.c_cc[VEOL] = 255;
1095 live.c_cc[VEOL2] = 0;
1096 live.c_cc[VSTART] = 0;
1097 tcsetattr(0, TCSADRAIN, &live);
1100 tcgetattr(0, &saved_settings);
1103 tcsetattr(0, TCSADRAIN, &saved_settings);
1107 void sttybbs(int cmd)
1108 { /* BSD version of sttybbs() */
1110 static struct sgttyb saved_settings;
1112 if ((cmd == 0) || (cmd == 1)) {
1114 live.sg_flags |= CBREAK;
1115 live.sg_flags |= CRMOD;
1116 live.sg_flags |= NL1;
1117 live.sg_flags &= ~ECHO;
1119 live.sg_flags |= NOFLSH;
1123 gtty(0, &saved_settings);
1126 stty(0, &saved_settings);
1133 * display_help() - help file viewer
1135 void display_help(char *name)
1142 * fmout() - Citadel text formatter and paginator
1145 int width, /* screen width to use */
1146 FILE *fp, /* file to read from, or NULL to read from server */
1147 char pagin, /* nonzero if we should use the paginator */
1148 int height, /* screen height to use */
1149 int starting_lp,/* starting value for lines_printed, -1 for global */
1150 char subst) /* nonzero if we should use hypertext mode */
1152 int a, b, c, d, old;
1158 num_urls = 0; /* Start with a clean slate of embedded URL's */
1160 if (starting_lp >= 0) {
1161 lines_printed = starting_lp;
1166 c = 1; /* c is the current pos */
1168 FMTA: while ((eof_flag == 0) && (strlen(buffer) < 126)) {
1169 if (fp != NULL) { /* read from file */
1172 if (eof_flag == 0) {
1174 buffer[strlen(buffer) + 1] = 0;
1175 buffer[strlen(buffer)] = a;
1177 } else { /* read from server */
1179 serv_gets(&buffer[d]);
1180 while ((!isspace(buffer[d])) && (isspace(buffer[strlen(buffer) - 1])))
1181 buffer[strlen(buffer) - 1] = 0;
1182 if (!strcmp(&buffer[d], "000")) {
1185 while (isspace(buffer[strlen(buffer) - 1]))
1186 buffer[strlen(buffer) - 1] = 0;
1194 if ( (!strncasecmp(buffer, "http://", 7))
1195 || (!strncasecmp(buffer, "ftp://", 6)) ) {
1196 safestrncpy(urls[num_urls], buffer, 255);
1197 for (a=0; a<strlen(urls[num_urls]); ++a) {
1198 b = urls[num_urls][a];
1199 if ( (b==' ') || (b==')') || (b=='>') || (b==10)
1200 || (b==13) || (b==9) || (b=='\"') )
1201 urls[num_urls][a] = 0;
1206 buffer[strlen(buffer) + 1] = 0;
1208 strcpy(buffer, &buffer[1]);
1215 if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
1217 if (((old == 13) || (old == 10)) && (isspace(real))) {
1220 lines_printed = checkpagin(lines_printed, pagin, height);
1227 if (((strlen(aaa) + c) > (width - 5)) && (strlen(aaa) > (width - 5))) {
1228 printf("\n%s", aaa);
1232 lines_printed = checkpagin(lines_printed, pagin, height);
1239 if ((strlen(aaa) + c) > (width - 5)) {
1243 lines_printed = checkpagin(lines_printed, pagin, height);
1247 c = c + strlen(aaa);
1251 if ((a == 13) || (a == 10)) {
1252 printf("%s\n", aaa);
1255 lines_printed = checkpagin(lines_printed, pagin, height);
1256 if (sigcaught) goto OOPS;
1262 /* keypress caught; drain the server */
1265 } while (strcmp(aaa, "000"));
1267 FMTEND: printf("\n");
1269 lines_printed = checkpagin(lines_printed, pagin, height);
1275 * support ANSI color if defined
1277 void color(int colornum)
1279 static int is_bold = 0;
1280 static int hold_color, current_color;
1282 if (colornum == COLOR_PUSH) {
1283 hold_color = current_color;
1287 if (colornum == COLOR_POP) {
1292 current_color = colornum;
1294 /* When switching to dim white, actually output an 'original
1295 * pair' sequence -- this looks better on black-on-white
1298 if (colornum == DIM_WHITE)
1299 printf("\033[39;49m");
1301 printf("\033[3%d;40m", (colornum & 7));
1303 if ((colornum >= 8) && (is_bold == 0)) {
1306 } else if ((colornum < 8) && (is_bold == 1)) {
1314 void cls(int colornum)
1317 printf("\033[4%dm\033[2J\033[H\033[0m", colornum);
1324 * Detect whether ANSI color is available (answerback)
1326 void send_ansi_detect(void)
1328 if (rc_ansi_color == 2) {
1335 void look_for_ansi(void)
1343 if (rc_ansi_color == 0) {
1345 } else if (rc_ansi_color == 1) {
1347 } else if (rc_ansi_color == 2) {
1349 /* otherwise, do the auto-detect */
1354 if ((now - AnsiDetect) < 2)
1363 select(1, &rfds, NULL, NULL, &tv);
1364 if (FD_ISSET(0, &rfds)) {
1365 abuf[strlen(abuf) + 1] = 0;
1366 read(0, &abuf[strlen(abuf)], 1);
1368 } while (FD_ISSET(0, &rfds));
1370 for (a = 0; a < strlen(abuf); ++a) {
1371 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1372 && (abuf[a + 2] == '?')) {
1381 * Display key options (highlight hotkeys inside angle brackets)
1383 void keyopt(char *buf) {
1387 for (i=0; i<strlen(buf); ++i) {
1389 putc(buf[i], stdout);
1390 color(BRIGHT_MAGENTA);
1395 putc(buf[i], stdout);
1404 * Present a key-menu line choice type of thing
1406 char keymenu(char *menuprompt, char *menustring) {
1412 int display_prompt = 1;
1414 choices = num_tokens(menustring, '|');
1416 if (menuprompt != NULL) do_prompt = 1;
1417 if (menuprompt != NULL) if (strlen(menuprompt)==0) do_prompt = 0;
1420 if (display_prompt) {
1422 printf("%s ", menuprompt);
1425 for (i=0; i<choices; ++i) {
1426 extract(buf, menustring, i);
1436 if ( (do_prompt) && (ch=='?') ) {
1437 printf("\rOne of... ");
1439 for (i=0; i<choices; ++i) {
1440 extract(buf, menustring, i);
1449 for (i=0; i<choices; ++i) {
1450 extract(buf, menustring, i);
1451 for (c=1; c<strlen(buf); ++c) {
1452 if ( (ch == tolower(buf[c]))
1454 && (buf[c+1]=='>') ) {
1455 for (a=0; a<strlen(buf); ++a) {
1456 if ( (a!=(c-1)) && (a!=(c+1))) {
1457 putc(buf[a], stdout);