2 * This file contains functions which implement parts of the
3 * text-mode user interface.
5 * Copyright (c) 1987-2012 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
23 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
42 #ifdef HAVE_SYS_SELECT_H
43 #include <sys/select.h>
46 #ifdef THREADED_CLIENT
53 #include <libcitadel.h>
55 #include "citadel_ipc.h"
58 #include "citadel_decls.h"
60 #include "routines2.h"
62 #include "client_chat.h"
63 #include "citadel_dirs.h"
64 #include "tuiconfig.h"
78 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
82 char rc_exp_cmd[1024];
83 int rc_allow_attachments;
84 int rc_display_message_numbers;
85 int rc_force_mail_prompts;
86 int rc_remember_passwords;
89 int rc_prompt_control = 0;
90 time_t rc_idle_threshold = (time_t)900;
92 char rc_open_cmd[SIZ];
93 char rc_gotmail_cmd[SIZ];
96 int next_lazy_cmd = 5;
98 extern int screenwidth, screenheight;
100 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
102 struct citcmd *cmdlist = NULL;
105 /* these variables are local to this module */
106 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
107 int ok_to_interrupt = 0; /* print instant msgs asynchronously */
108 time_t AnsiDetect; /* when did we send the detect code? */
109 int enable_color = 0; /* nonzero for ANSI color */
115 * If an interesting key has been pressed, return its value, otherwise 0
117 char was_a_key_pressed(void) {
127 retval = select(1, &rfds, NULL, NULL, &tv);
129 /* Careful! Disable keepalives during keyboard polling; we're probably
130 * in the middle of a data transfer from the server, in which case
131 * sending a NOOP would throw the client protocol out of sync.
133 if ((retval > 0) && FD_ISSET(0, &rfds)) {
134 set_keepalives(KA_NO);
135 the_character = inkey();
136 set_keepalives(KA_YES);
141 return(the_character);
149 * print_instant() - print instant messages if there are any
151 void print_instant(void)
160 char *listing = NULL;
161 int r; /* IPC result code */
163 if (instant_msgs == 0)
169 if (IsEmptyStr(rc_exp_cmd)) {
174 while (instant_msgs != 0) {
175 r = CtdlIPCGetInstantMessage(ipc_for_signal_handlers, &listing, buf);
179 instant_msgs = extract_int(buf, 0);
180 timestamp = extract_long(buf, 1);
181 flags = extract_int(buf, 2);
182 extract_token(sender, buf, 3, '|', sizeof sender);
183 extract_token(node, buf, 4, '|', sizeof node);
184 strcpy(last_paged, sender);
186 localtime_r(×tamp, &stamp);
188 /* If the page is a Logoff Request, honor it. */
194 if (!IsEmptyStr(rc_exp_cmd)) {
195 outpipe = popen(rc_exp_cmd, "w");
196 if (outpipe != NULL) {
197 /* Header derived from flags */
200 "Please log off now, as requested ");
202 fprintf(outpipe, "Broadcast message ");
204 fprintf(outpipe, "Chat request ");
206 fprintf(outpipe, "Message ");
207 /* Timestamp. Can this be improved? */
208 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
209 fprintf(outpipe, "at 12:%02d%cm",
211 stamp.tm_hour ? 'p' : 'a');
212 else if (stamp.tm_hour > 12) /* pm */
213 fprintf(outpipe, "at %d:%02dpm",
217 fprintf(outpipe, "at %d:%02dam",
218 stamp.tm_hour, stamp.tm_min);
219 fprintf(outpipe, " from %s", sender);
220 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
221 fprintf(outpipe, " @%s", node);
222 fprintf(outpipe, ":\n%s\n", listing);
224 if (instant_msgs == 0)
229 /* fall back to built-in instant message display */
232 /* Header derived from flags */
234 scr_printf("Please log off now, as requested ");
236 scr_printf("Broadcast message ");
238 scr_printf("Chat request ");
240 scr_printf("Message ");
242 /* Timestamp. Can this be improved? */
243 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)/* 12am/12pm */
244 scr_printf("at 12:%02d%cm", stamp.tm_min,
245 stamp.tm_hour ? 'p' : 'a');
246 else if (stamp.tm_hour > 12) /* pm */
247 scr_printf("at %d:%02dpm",
248 stamp.tm_hour - 12, stamp.tm_min);
250 scr_printf("at %d:%02dam", stamp.tm_hour, stamp.tm_min);
253 scr_printf(" from %s", sender);
255 /* Remote node, if any */
256 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
257 scr_printf(" @%s", node);
260 fmout(screenwidth, NULL, listing, NULL, 0);
264 scr_printf("\n---\n");
271 void set_keepalives(int s)
273 keepalives_enabled = (char) s;
277 * This loop handles the "keepalive" messages sent to the server when idling.
280 static time_t idlet = 0;
281 static void really_do_keepalive(void) {
285 /* This may sometimes get called before we are actually connected
286 * to the server. Don't do anything if we aren't connected. -IO
288 if (!ipc_for_signal_handlers)
291 /* If full keepalives are enabled, send a NOOP to the server and
292 * wait for a response.
294 if (keepalives_enabled == KA_YES) {
295 CtdlIPCNoop(ipc_for_signal_handlers);
296 if (instant_msgs > 0) {
297 if (ok_to_interrupt == 1) {
298 scr_printf("\r%64s\r", "");
300 scr_printf("%s%c ", room_name,
301 room_prompt(room_flags));
307 /* If half keepalives are enabled, send a QNOP to the server (if the
308 * server supports it) and then do nothing.
310 if ( (keepalives_enabled == KA_HALF)
311 && (ipc_for_signal_handlers->ServInfo.supports_qnop > 0) ) {
312 CtdlIPC_chat_send(ipc_for_signal_handlers, "QNOP");
316 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
317 encapsulated interface; in theory there should be no need to touch these
318 globals outside of the async_ka_* functions. */
320 #ifdef THREADED_CLIENT
321 static pthread_t ka_thr_handle;
322 static int ka_thr_active = 0;
323 static int async_ka_enabled = 0;
325 static void *ka_thread(void *arg)
327 #ifdef HAVE_BACKTRACE
328 char threadName[256];
331 sprintf(threadName, "ka_Thread n");
333 // Register for tracing
334 eCrash_RegisterThread(threadName, 0);
336 really_do_keepalive();
337 pthread_detach(ka_thr_handle);
340 #ifdef HAVE_BACKTRACE
341 eCrash_UnregisterThread();
346 /* start up a thread to handle a keepalive in the background */
347 static void async_ka_exec(void)
349 if (!ka_thr_active) {
351 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
352 perror("pthread_create");
357 #endif /* THREADED_CLIENT */
359 /* I changed this from static to not because I need to call it from
360 screen.c, either that or make something in screen.c not static.
361 Fix it how you like. Why all the staticness? stu */
363 void do_keepalive(void)
368 if ((now - idlet) < ((long) S_KEEPALIVE))
371 /* Do a space-backspace to keep telnet sessions from idling out */
372 scr_printf(" %c", 8);
375 #ifdef THREADED_CLIENT
376 if (async_ka_enabled)
380 really_do_keepalive();
384 /* Now the actual async-keepalve API that we expose to higher levels:
385 async_ka_start() and async_ka_end(). These do nothing when we don't have
386 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
388 /* wait for a background keepalive to complete. this must be done before
389 attempting any further server requests! */
390 void async_ka_end(void)
392 #ifdef THREADED_CLIENT
394 pthread_join(ka_thr_handle, NULL);
400 /* tell do_keepalive() that keepalives are asynchronous. */
401 void async_ka_start(void)
403 #ifdef THREADED_CLIENT
410 { /* get a character from the keyboard, with */
411 int a; /* the watchdog timer in effect if necessary */
420 /* This loop waits for keyboard input. If the keepalive
421 * timer expires, it sends a keepalive to the server if
422 * necessary and then waits again.
429 tv.tv_sec = S_KEEPALIVE;
432 select(1, &rfds, NULL, NULL, &tv);
433 } while (!FD_ISSET(0, &rfds));
435 /* At this point, there's input, so fetch it.
436 * (There's a hole in the bucket...)
438 a = scr_getc(SCR_BLOCK);
451 { /* Returns 1 for yes, 0 for no */
467 /* Returns 1 for yes, 0 for no, arg is default value */
491 * Function to read a line of text from the terminal.
493 * string Pointer to string buffer
494 * lim Maximum length - if negative, echo asterisks instead of characters
496 void ctdl_getline(char *string, int lim)
510 if ((a == 8 || a == 23) && (IsEmptyStr(string)))
512 if ((a != 10) && (a != 8) && (strlen(string) == lim))
514 if ((a == 8) && (string[0] != 0)) {
515 string[strlen(string) - 1] = 0;
516 scr_putc(8); scr_putc(32); scr_putc(8);
519 if ((a == 23) && (string[0] != 0)) {
521 string[strlen(string) - 1] = 0;
522 scr_putc(8); scr_putc(32); scr_putc(8);
523 } while (!IsEmptyStr(string) && string[strlen(string) - 1] != ' ');
548 * strprompt() - prompt for a string, print the existing value and
549 * allow the user to press return to keep it...
551 void strprompt(char *prompt, char *str, int len)
558 scr_printf("%s ", prompt);
561 color(BRIGHT_MAGENTA);
564 scr_printf("%s", str);
567 for (i=0; !IsEmptyStr(&str[i]); ++i) {
577 ctdl_getline(buf, len);
585 * boolprompt() - prompt for a yes/no, print the existing value and
586 * allow the user to press return to keep it...
588 int boolprompt(char *prompt, int prev_val)
593 scr_printf("%s ", prompt);
596 color(BRIGHT_MAGENTA);
597 scr_printf("%s", (prev_val ? "Yes" : "No"));
601 r = (yesno_d(prev_val));
607 * intprompt() - like strprompt(), except for an integer
608 * (note that it RETURNS the new value!)
610 int intprompt(char *prompt, int ival, int imin, int imax)
618 snprintf(buf, sizeof buf, "%d", i);
619 strprompt(prompt, buf, 15);
621 for (p=0; !IsEmptyStr(&buf[p]); ++p) {
622 if ( (!isdigit(buf[p]))
623 && ( (buf[p]!='-') || (p!=0) ) )
627 scr_printf("*** Must be no less than %d.\n", imin);
629 scr_printf("*** Must be no more than %d.\n", imax);
630 } while ((i < imin) || (i > imax));
635 * newprompt() - prompt for a string with no existing value
636 * (clears out string buffer first)
638 void newprompt(char *prompt, char *str, int len)
641 color(BRIGHT_MAGENTA);
642 scr_printf("%s", prompt);
644 ctdl_getline(str, len);
650 { /* returns a lower case value */
659 * parse the citadel.rc file
661 void load_command_set(void)
666 struct citcmd *lastcmd = NULL;
670 /* first, set up some defaults for non-required variables */
672 strcpy(editor_path, "");
673 strcpy(printcmd, "");
674 strcpy(imagecmd, "");
675 strcpy(rc_username, "");
676 strcpy(rc_password, "");
679 rc_allow_attachments = 0;
680 rc_remember_passwords = 0;
681 strcpy(rc_exp_cmd, "");
682 rc_display_message_numbers = 0;
683 rc_force_mail_prompts = 0;
686 strcpy(rc_url_cmd, "");
687 strcpy(rc_open_cmd, "");
688 strcpy(rc_gotmail_cmd, "");
690 rc_encrypt = RC_DEFAULT;
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(file_citadel_rc, "r");
703 if (ccfile == NULL) {
704 ccfile = fopen("/etc/citadel.rc", "r");
706 if (ccfile == NULL) {
707 ccfile = fopen("./citadel.rc", "r");
709 if (ccfile == NULL) {
710 perror("commands: cannot open citadel.rc");
713 while (fgets(buf, sizeof buf, ccfile) != NULL) {
714 while ((!IsEmptyStr(buf)) ? (isspace(buf[strlen(buf) - 1])) : 0)
715 buf[strlen(buf) - 1] = 0;
717 if (!strncasecmp(buf, "encrypt=", 8)) {
718 if (!strcasecmp(&buf[8], "yes")) {
722 fprintf(stderr, "citadel.rc requires encryption support but citadel is not compiled with OpenSSL");
727 else if (!strcasecmp(&buf[8], "no")) {
730 else if (!strcasecmp(&buf[8], "default")) {
731 rc_encrypt = RC_DEFAULT;
736 if (!strncasecmp(buf, "editor=", 7)) {
737 strcpy(editor_path, &buf[7]);
740 if (!strncasecmp(buf, "printcmd=", 9))
741 strcpy(printcmd, &buf[9]);
743 if (!strncasecmp(buf, "imagecmd=", 9))
744 strcpy(imagecmd, &buf[9]);
746 if (!strncasecmp(buf, "expcmd=", 7))
747 strcpy(rc_exp_cmd, &buf[7]);
749 if (!strncasecmp(buf, "use_floors=", 11)) {
750 if (!strcasecmp(&buf[11], "yes"))
751 rc_floor_mode = RC_YES;
752 if (!strcasecmp(&buf[11], "no"))
753 rc_floor_mode = RC_NO;
754 if (!strcasecmp(&buf[11], "default"))
755 rc_floor_mode = RC_DEFAULT;
757 if (!strncasecmp(buf, "beep=", 5)) {
758 rc_exp_beep = atoi(&buf[5]);
760 if (!strncasecmp(buf, "allow_attachments=", 18)) {
761 rc_allow_attachments = atoi(&buf[18]);
763 if (!strncasecmp(buf, "idle_threshold=", 15)) {
764 rc_idle_threshold = atol(&buf[15]);
766 if (!strncasecmp(buf, "remember_passwords=", 19)) {
767 rc_remember_passwords = atoi(&buf[19]);
769 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
770 rc_display_message_numbers = atoi(&buf[24]);
772 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
773 rc_force_mail_prompts = atoi(&buf[19]);
775 if (!strncasecmp(buf, "ansi_color=", 11)) {
776 if (!strncasecmp(&buf[11], "on", 2))
778 if (!strncasecmp(&buf[11], "auto", 4))
779 rc_ansi_color = 2; /* autodetect */
780 if (!strncasecmp(&buf[11], "user", 4))
781 rc_ansi_color = 3; /* user config */
783 if (!strncasecmp(buf, "status_line=", 12)) {
784 if (!strncasecmp(&buf[12], "on", 2))
785 enable_status_line = 1;
787 if (!strncasecmp(buf, "use_background=", 15)) {
788 if (!strncasecmp(&buf[15], "on", 2))
791 if (!strncasecmp(buf, "prompt_control=", 15)) {
792 if (!strncasecmp(&buf[15], "on", 2))
793 rc_prompt_control = 1;
794 if (!strncasecmp(&buf[15], "user", 4))
795 rc_prompt_control = 3; /* user config */
797 if (!strncasecmp(buf, "username=", 9))
798 strcpy(rc_username, &buf[9]);
800 if (!strncasecmp(buf, "password=", 9))
801 strcpy(rc_password, &buf[9]);
803 if (!strncasecmp(buf, "urlcmd=", 7))
804 strcpy(rc_url_cmd, &buf[7]);
806 if (!strncasecmp(buf, "opencmd=", 7))
807 strcpy(rc_open_cmd, &buf[8]);
809 if (!strncasecmp(buf, "gotmailcmd=", 11))
810 strcpy(rc_gotmail_cmd, &buf[11]);
812 if (!strncasecmp(buf, "cmd=", 4)) {
813 strcpy(buf, &buf[4]);
815 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
817 cptr->c_cmdnum = atoi(buf);
818 for (d = strlen(buf); d >= 0; --d)
821 strcpy(buf, &buf[b + 1]);
823 cptr->c_axlevel = atoi(buf);
824 for (d = strlen(buf); d >= 0; --d)
827 strcpy(buf, &buf[b + 1]);
829 for (a = 0; a < 5; ++a)
830 cptr->c_keys[a][0] = 0;
834 buf[strlen(buf) + 1] = 0;
835 while (!IsEmptyStr(buf)) {
837 for (d = strlen(buf); d >= 0; --d)
840 strncpy(cptr->c_keys[a], buf, b);
841 cptr->c_keys[a][b] = 0;
843 strcpy(buf, &buf[b + 1]);
853 lastcmd->next = cptr;
863 * return the key associated with a command
865 char keycmd(char *cmdstr)
869 for (a = 0; !IsEmptyStr(&cmdstr[a]); ++a)
870 if (cmdstr[a] == '&')
871 return (tolower(cmdstr[a + 1]));
877 * Output the string from a key command without the ampersand
878 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
880 char *cmd_expand(char *strbuf, int mode)
888 for (a = 0; exp[a]; ++a) {
889 if (strbuf[a] == '&') {
891 /* dont echo these non mnemonic command keys */
892 int noecho = strbuf[a+1] == '<' || strbuf[a+1] == '>' || strbuf[a+1] == '+' || strbuf[a+1] == '-';
895 strcpy(&exp[a], &exp[a + 1 + noecho]);
899 strcpy(buf, &exp[a + 2]);
905 if (!strncmp(&exp[a], "^r", 2)) {
907 strcpy(&exp[a], room_name);
908 strcat(exp, &buf[a + 2]);
910 if (!strncmp(&exp[a], "^c", 2)) {
912 strcpy(&exp[a + 1], &exp[a + 2]);
922 * Comparison function to determine if entered commands match a
923 * command loaded from the config file.
925 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
936 for (a = 0; a < ncomp; ++a) {
937 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
938 || (cptr->c_axlevel > cmdax))
946 * This function returns 1 if a given command requires a string input
948 int requires_string(struct citcmd *cptr, int ncomp)
953 strcpy(buf, cptr->c_keys[ncomp - 1]);
954 for (a = 0; !IsEmptyStr(&buf[a]); ++a) {
963 * Input a command at the main prompt.
964 * This function returns an integer command number. If the command prompts
965 * for a string then it is placed in the supplied buffer.
967 int getcmd(CtdlIPC *ipc, char *argbuf)
979 * Starting a new command now, so set sigcaught to 0. This variable
980 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
981 * been interrupted by a keypress.
985 /* Switch color support on or off if we're in user mode */
986 if (rc_ansi_color == 3) {
987 if (userflags & US_COLOR)
992 /* if we're running in idiot mode, display a cute little menu */
993 IFNEXPERT formout(ipc, "mainmenu");
998 for (a = 0; a < 5; ++a)
1000 /* now the room prompt... */
1001 ok_to_interrupt = 1;
1002 color(BRIGHT_WHITE);
1003 scr_printf("\n%s", room_name);
1005 scr_printf("%c ", room_prompt(room_flags));
1009 ok_to_interrupt = 0;
1011 /* Handle the backspace key, but only if there's something
1012 * to backspace over...
1014 if ((ch == 8) && (cmdpos > 0)) {
1015 back(cmdspaces[cmdpos - 1] + 1);
1019 /* Spacebar invokes "lazy traversal" commands */
1020 if ((ch == 32) && (cmdpos == 0)) {
1021 this_lazy_cmd = next_lazy_cmd;
1022 if (this_lazy_cmd == 13)
1024 if (this_lazy_cmd == 5)
1026 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1027 if (cptr->c_cmdnum == this_lazy_cmd) {
1028 for (a = 0; a < 5; ++a)
1029 if (cptr->c_keys[a][0] != 0)
1030 scr_printf("%s ", cmd_expand(
1031 cptr->c_keys[a], 0));
1033 return (this_lazy_cmd);
1037 return (this_lazy_cmd);
1039 /* Otherwise, process the command */
1040 cmdbuf[cmdpos] = tolower(ch);
1042 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1043 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1045 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1046 cmdspaces[cmdpos] = strlen(
1047 cmd_expand(cptr->c_keys[cmdpos], 0));
1049 if ((cptr->c_keys[cmdpos + 1]) != 0)
1055 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1056 if (cmdmatch(cmdbuf, cptr, 5)) {
1057 /* We've found our command. */
1058 if (requires_string(cptr, cmdpos)) {
1060 ctdl_getline(argbuf, 64);
1065 /* If this command is one that changes rooms,
1066 * then the next lazy-command (space bar)
1067 * should be "read new" instead of "goto"
1069 if ((cptr->c_cmdnum == 5)
1070 || (cptr->c_cmdnum == 6)
1071 || (cptr->c_cmdnum == 47)
1072 || (cptr->c_cmdnum == 52)
1073 || (cptr->c_cmdnum == 16)
1074 || (cptr->c_cmdnum == 20))
1077 /* If this command is "read new"
1078 * then the next lazy-command (space bar)
1081 if (cptr->c_cmdnum == 13)
1084 return (cptr->c_cmdnum);
1090 scr_printf("\rOne of ... \n");
1091 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1092 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1093 for (a = 0; a < 5; ++a) {
1094 keyopt(cmd_expand(cptr->c_keys[a], 1));
1102 scr_printf("\n%s%c ", room_name, room_prompt(room_flags));
1104 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1105 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1106 for (a = 0; a < cmdpos; ++a) {
1108 cmd_expand(cptr->c_keys[a], 0));
1123 * set tty modes. commands are:
1125 * 01- set to Citadel mode
1126 * 2 - save current settings for later restoral
1127 * 3 - restore saved settings
1129 #ifdef HAVE_TERMIOS_H
1130 void stty_ctdl(int cmd)
1131 { /* SysV version of stty_ctdl() */
1132 struct termios live;
1133 static struct termios saved_settings;
1134 static int last_cmd = 0;
1141 if ((cmd == 0) || (cmd == 1)) {
1142 tcgetattr(0, &live);
1143 live.c_iflag = ISTRIP | IXON | IXANY;
1144 live.c_oflag = OPOST | ONLCR;
1145 live.c_lflag = ISIG | NOFLSH;
1147 live.c_cc[VINTR] = 0;
1148 live.c_cc[VQUIT] = 0;
1151 live.c_cc[VMIN] = 0;
1152 live.c_cc[VTIME] = 0;
1155 /* do we even need this stuff anymore? */
1156 /* live.c_line=0; */
1157 live.c_cc[VERASE] = 8;
1158 live.c_cc[VKILL] = 24;
1159 live.c_cc[VEOF] = 1;
1160 live.c_cc[VEOL] = 255;
1161 live.c_cc[VEOL2] = 0;
1162 live.c_cc[VSTART] = 0;
1163 tcsetattr(0, TCSADRAIN, &live);
1166 tcgetattr(0, &saved_settings);
1169 tcsetattr(0, TCSADRAIN, &saved_settings);
1174 void stty_ctdl(int cmd)
1175 { /* BSD version of stty_ctdl() */
1177 static struct sgttyb saved_settings;
1178 static int last_cmd = 0;
1185 if ((cmd == 0) || (cmd == 1)) {
1187 live.sg_flags |= CBREAK;
1188 live.sg_flags |= CRMOD;
1189 live.sg_flags |= NL1;
1190 live.sg_flags &= ~ECHO;
1192 live.sg_flags |= NOFLSH;
1196 gtty(0, &saved_settings);
1199 stty(0, &saved_settings);
1206 * display_help() - help file viewer
1208 void display_help(CtdlIPC *ipc, char *name)
1215 * fmout() - Citadel text formatter and paginator
1218 int width, /* screen width to use */
1219 FILE *fpin, /* file to read from, or NULL to format given text */
1220 char *text, /* text to be formatted (when fpin is NULL */
1221 FILE *fpout, /* file to write to, or NULL to write to screen */
1222 int subst) /* nonzero if we should use hypertext mode */
1224 char *buffer = NULL; /* The current message */
1225 char *word = NULL; /* What we are about to actually print */
1226 char *e; /* Pointer to position in text */
1227 char old = 0; /* The previous character */
1228 int column = 0; /* Current column */
1229 size_t i; /* Generic counter */
1231 /* Space for a single word, which can be at most screenwidth */
1232 word = (char *)calloc(1, width);
1234 scr_printf("Can't alloc memory to print message: %s!\n",
1239 /* Read the entire message body into memory */
1241 buffer = load_message_from_file(fpin);
1243 scr_printf("Can't print message: %s!\n",
1252 /* Run the message body */
1254 /* Catch characters that shouldn't be there at all */
1259 /* First, are we looking at a newline? */
1262 if (*e == ' ') { /* Paragraph */
1264 fprintf(fpout, "\n");
1269 } else if (old != ' ') {/* Don't print two spaces */
1271 fprintf(fpout, " ");
1281 /* Are we looking at a nonprintable?
1282 * (This section is now commented out because we could be displaying
1283 * a character set like UTF-8 or ISO-8859-1.)
1284 if ( (*e < 32) || (*e > 126) ) {
1289 /* Or are we looking at a space? */
1292 if (column >= width - 1) {
1293 /* Are we in the rightmost column? */
1295 fprintf(fpout, "\n");
1300 } else if (!(column == 0 && old == ' ')) {
1301 /* Eat only the first space on a line */
1303 fprintf(fpout, " ");
1309 /* ONLY eat the FIRST space on a line */
1315 /* Read a word, slightly messy */
1318 if (!isprint(e[i]) && !isspace(e[i]))
1325 /* We should never see these, but... slightly messy */
1326 if (e[i] == '\t' || e[i] == '\f' || e[i] == '\v')
1329 /* Break up really long words */
1330 /* TODO: auto-hyphenation someday? */
1333 strncpy(word, e, i);
1336 /* Decide where to print the word */
1337 if (column + i >= width) {
1338 /* Wrap to the next line */
1340 fprintf(fpout, "\n");
1347 /* Print the word */
1349 fprintf(fpout, "%s", word);
1351 scr_printf("%s", word);
1354 e += i; /* Start over with the whitepsace! */
1358 if (fpin) /* We allocated this, remember? */
1361 /* Is this necessary? It makes the output kind of spacey. */
1363 fprintf(fpout, "\n");
1373 * support ANSI color if defined
1375 void color(int colornum)
1377 static int hold_color;
1378 static int current_color;
1380 if (colornum == COLOR_PUSH) {
1381 hold_color = current_color;
1385 if (colornum == COLOR_POP) {
1390 current_color = colornum;
1392 /* When switching to dim white, actually output an 'original
1393 * pair' sequence -- this looks better on black-on-white
1394 * terminals. - Changed to ORIGINAL_PAIR as this actually
1395 * wound up looking horrible on black-on-white terminals, not
1396 * to mention transparent terminals.
1398 if (colornum == ORIGINAL_PAIR)
1399 printf("\033[0;39;49m");
1401 printf("\033[%d;3%d;4%dm",
1402 (colornum & 8) ? 1 : 0,
1409 void cls(int colornum)
1412 printf("\033[4%dm\033[2J\033[H\033[0m",
1413 colornum ? colornum : rc_color_use_bg);
1419 * Detect whether ANSI color is available (answerback)
1421 void send_ansi_detect(void)
1423 if (rc_ansi_color == 2) {
1430 void look_for_ansi(void)
1438 if (rc_ansi_color == 0) {
1440 } else if (rc_ansi_color == 1) {
1442 } else if (rc_ansi_color == 2) {
1444 /* otherwise, do the auto-detect */
1449 if ((now - AnsiDetect) < 2)
1458 select(1, &rfds, NULL, NULL, &tv);
1459 if (FD_ISSET(0, &rfds)) {
1460 abuf[strlen(abuf) + 1] = 0;
1461 rv = read(0, &abuf[strlen(abuf)], 1);
1463 scr_printf("failed to read after select: %s",
1468 } while (FD_ISSET(0, &rfds));
1470 for (a = 0; !IsEmptyStr(&abuf[a]); ++a) {
1471 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1472 && (abuf[a + 2] == '?')) {
1481 * Display key options (highlight hotkeys inside angle brackets)
1483 void keyopt(char *buf) {
1487 for (i=0; !IsEmptyStr(&buf[i]); ++i) {
1489 scr_printf("%c", buf[i]);
1490 color(BRIGHT_MAGENTA);
1492 if (buf[i]=='>'&& buf[i+1] != '>') {
1495 scr_printf("%c", buf[i]);
1504 * Present a key-menu line choice type of thing
1506 char keymenu(char *menuprompt, char *menustring) {
1512 int display_prompt = 1;
1514 choices = num_tokens(menustring, '|');
1516 if (menuprompt != NULL) do_prompt = 1;
1517 if ((menuprompt != NULL) && (IsEmptyStr(menuprompt))) do_prompt = 0;
1520 if (display_prompt) {
1522 scr_printf("%s ", menuprompt);
1525 for (i=0; i<choices; ++i) {
1526 extract_token(buf, menustring, i, '|', sizeof buf);
1536 if ( (do_prompt) && (ch=='?') ) {
1537 scr_printf("\rOne of... ");
1539 for (i=0; i<choices; ++i) {
1540 extract_token(buf, menustring, i, '|', sizeof buf);
1549 for (i=0; i<choices; ++i) {
1550 extract_token(buf, menustring, i, '|', sizeof buf);
1551 for (c=1; !IsEmptyStr(&buf[c]); ++c) {
1552 if ( (ch == tolower(buf[c]))
1554 && (buf[c+1]=='>') ) {
1555 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
1556 if ( (a!=(c-1)) && (a!=(c+1))) {