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];
95 int next_lazy_cmd = 5;
97 extern int screenwidth, screenheight;
99 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
101 struct citcmd *cmdlist = NULL;
104 /* these variables are local to this module */
105 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
106 int ok_to_interrupt = 0; /* print instant msgs asynchronously */
107 time_t AnsiDetect; /* when did we send the detect code? */
108 int enable_color = 0; /* nonzero for ANSI color */
114 * If an interesting key has been pressed, return its value, otherwise 0
116 char was_a_key_pressed(void) {
126 retval = select(1, &rfds, NULL, NULL, &tv);
128 /* Careful! Disable keepalives during keyboard polling; we're probably
129 * in the middle of a data transfer from the server, in which case
130 * sending a NOOP would throw the client protocol out of sync.
132 if ((retval > 0) && FD_ISSET(0, &rfds)) {
133 set_keepalives(KA_NO);
134 the_character = inkey();
135 set_keepalives(KA_YES);
140 return(the_character);
148 * print_instant() - print instant messages if there are any
150 void print_instant(void)
159 char *listing = NULL;
160 int r; /* IPC result code */
162 if (instant_msgs == 0)
168 if (IsEmptyStr(rc_exp_cmd)) {
173 while (instant_msgs != 0) {
174 r = CtdlIPCGetInstantMessage(ipc_for_signal_handlers, &listing, buf);
178 instant_msgs = extract_int(buf, 0);
179 timestamp = extract_long(buf, 1);
180 flags = extract_int(buf, 2);
181 extract_token(sender, buf, 3, '|', sizeof sender);
182 extract_token(node, buf, 4, '|', sizeof node);
183 strcpy(last_paged, sender);
185 localtime_r(×tamp, &stamp);
187 /* If the page is a Logoff Request, honor it. */
193 if (!IsEmptyStr(rc_exp_cmd)) {
194 outpipe = popen(rc_exp_cmd, "w");
195 if (outpipe != NULL) {
196 /* Header derived from flags */
199 "Please log off now, as requested ");
201 fprintf(outpipe, "Broadcast message ");
203 fprintf(outpipe, "Chat request ");
205 fprintf(outpipe, "Message ");
206 /* Timestamp. Can this be improved? */
207 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
208 fprintf(outpipe, "at 12:%02d%cm",
210 stamp.tm_hour ? 'p' : 'a');
211 else if (stamp.tm_hour > 12) /* pm */
212 fprintf(outpipe, "at %d:%02dpm",
216 fprintf(outpipe, "at %d:%02dam",
217 stamp.tm_hour, stamp.tm_min);
218 fprintf(outpipe, " from %s", sender);
219 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
220 fprintf(outpipe, " @%s", node);
221 fprintf(outpipe, ":\n%s\n", listing);
223 if (instant_msgs == 0)
228 /* fall back to built-in instant message display */
231 /* Header derived from flags */
233 scr_printf("Please log off now, as requested ");
235 scr_printf("Broadcast message ");
237 scr_printf("Chat request ");
239 scr_printf("Message ");
241 /* Timestamp. Can this be improved? */
242 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)/* 12am/12pm */
243 scr_printf("at 12:%02d%cm", stamp.tm_min,
244 stamp.tm_hour ? 'p' : 'a');
245 else if (stamp.tm_hour > 12) /* pm */
246 scr_printf("at %d:%02dpm",
247 stamp.tm_hour - 12, stamp.tm_min);
249 scr_printf("at %d:%02dam", stamp.tm_hour, stamp.tm_min);
252 scr_printf(" from %s", sender);
254 /* Remote node, if any */
255 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
256 scr_printf(" @%s", node);
259 fmout(screenwidth, NULL, listing, NULL, 0);
263 scr_printf("\n---\n");
270 void set_keepalives(int s)
272 keepalives_enabled = (char) s;
276 * This loop handles the "keepalive" messages sent to the server when idling.
279 static time_t idlet = 0;
280 static void really_do_keepalive(void) {
284 /* This may sometimes get called before we are actually connected
285 * to the server. Don't do anything if we aren't connected. -IO
287 if (!ipc_for_signal_handlers)
290 /* If full keepalives are enabled, send a NOOP to the server and
291 * wait for a response.
293 if (keepalives_enabled == KA_YES) {
294 CtdlIPCNoop(ipc_for_signal_handlers);
295 if (instant_msgs > 0) {
296 if (ok_to_interrupt == 1) {
297 scr_printf("\r%64s\r", "");
299 scr_printf("%s%c ", room_name,
300 room_prompt(room_flags));
306 /* If half keepalives are enabled, send a QNOP to the server (if the
307 * server supports it) and then do nothing.
309 if ( (keepalives_enabled == KA_HALF)
310 && (ipc_for_signal_handlers->ServInfo.supports_qnop > 0) ) {
311 CtdlIPC_chat_send(ipc_for_signal_handlers, "QNOP");
315 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
316 encapsulated interface; in theory there should be no need to touch these
317 globals outside of the async_ka_* functions. */
319 #ifdef THREADED_CLIENT
320 static pthread_t ka_thr_handle;
321 static int ka_thr_active = 0;
322 static int async_ka_enabled = 0;
324 static void *ka_thread(void *arg)
326 #ifdef HAVE_BACKTRACE
327 char threadName[256];
330 sprintf(threadName, "ka_Thread n");
332 // Register for tracing
333 eCrash_RegisterThread(threadName, 0);
335 really_do_keepalive();
336 pthread_detach(ka_thr_handle);
339 #ifdef HAVE_BACKTRACE
340 eCrash_UnregisterThread();
345 /* start up a thread to handle a keepalive in the background */
346 static void async_ka_exec(void)
348 if (!ka_thr_active) {
350 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
351 perror("pthread_create");
356 #endif /* THREADED_CLIENT */
358 /* I changed this from static to not because I need to call it from
359 screen.c, either that or make something in screen.c not static.
360 Fix it how you like. Why all the staticness? stu */
362 void do_keepalive(void)
367 if ((now - idlet) < ((long) S_KEEPALIVE))
370 /* Do a space-backspace to keep telnet sessions from idling out */
371 scr_printf(" %c", 8);
374 #ifdef THREADED_CLIENT
375 if (async_ka_enabled)
379 really_do_keepalive();
383 /* Now the actual async-keepalve API that we expose to higher levels:
384 async_ka_start() and async_ka_end(). These do nothing when we don't have
385 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
387 /* wait for a background keepalive to complete. this must be done before
388 attempting any further server requests! */
389 void async_ka_end(void)
391 #ifdef THREADED_CLIENT
393 pthread_join(ka_thr_handle, NULL);
399 /* tell do_keepalive() that keepalives are asynchronous. */
400 void async_ka_start(void)
402 #ifdef THREADED_CLIENT
409 { /* get a character from the keyboard, with */
410 int a; /* the watchdog timer in effect if necessary */
419 /* This loop waits for keyboard input. If the keepalive
420 * timer expires, it sends a keepalive to the server if
421 * necessary and then waits again.
428 tv.tv_sec = S_KEEPALIVE;
431 select(1, &rfds, NULL, NULL, &tv);
432 } while (!FD_ISSET(0, &rfds));
434 /* At this point, there's input, so fetch it.
435 * (There's a hole in the bucket...)
437 a = scr_getc(SCR_BLOCK);
450 { /* Returns 1 for yes, 0 for no */
466 /* Returns 1 for yes, 0 for no, arg is default value */
490 * Function to read a line of text from the terminal.
492 * string Pointer to string buffer
494 * noshow Echo asterisks instead of keystrokes?
495 * bs Allow backspacing out of the prompt? (returns -1 if this happens)
497 * returns: string length
499 int ctdl_getline(char *string, int lim, int noshow, int bs)
501 int pos = strlen(string);
505 scr_printf("%s", string);
510 if ((ch == 8) && (pos > 0)) { /* backspace */
512 scr_putc(8); scr_putc(32); scr_putc(8);
515 else if ((ch == 8) && (pos == 0) && (bs)) { /* backspace out of the prompt */
520 else if ((ch == 23) && (pos > 0)) { /* Ctrl-W deletes a word */
521 while ((pos > 0) && !isspace(string[pos])) {
523 scr_putc(8); scr_putc(32); scr_putc(8);
525 while ((pos > 0) && !isspace(string[pos-1])) {
527 scr_putc(8); scr_putc(32); scr_putc(8);
531 else if (ch == 10) { /* return */
538 else if (isprint(ch)) { /* payload characters */
539 scr_putc((noshow ? '*' : ch));
548 * newprompt() prompt for a string, print the existing value, and
549 * allow the user to press return to keep it...
550 * If len is negative, pass the "noshow" flag to ctdl_getline()
552 void strprompt(char *prompt, char *str, int len)
559 scr_printf("%s ", prompt);
562 color(BRIGHT_MAGENTA);
565 scr_printf("%s", str);
568 for (i=0; !IsEmptyStr(&str[i]); ++i) {
578 ctdl_getline(buf, abs(len), (len<0), 0);
586 * boolprompt() - prompt for a yes/no, print the existing value and
587 * allow the user to press return to keep it...
589 int boolprompt(char *prompt, int prev_val)
594 scr_printf("%s ", prompt);
597 color(BRIGHT_MAGENTA);
598 scr_printf("%s", (prev_val ? "Yes" : "No"));
602 r = (yesno_d(prev_val));
608 * intprompt() - like strprompt(), except for an integer
609 * (note that it RETURNS the new value!)
611 int intprompt(char *prompt, int ival, int imin, int imax)
619 snprintf(buf, sizeof buf, "%d", i);
620 strprompt(prompt, buf, 15);
622 for (p=0; !IsEmptyStr(&buf[p]); ++p) {
623 if ( (!isdigit(buf[p]))
624 && ( (buf[p]!='-') || (p!=0) ) )
628 scr_printf("*** Must be no less than %d.\n", imin);
630 scr_printf("*** Must be no more than %d.\n", imax);
631 } while ((i < imin) || (i > imax));
636 * newprompt() prompt for a string with no existing value
637 * (clears out string buffer first)
638 * If len is negative, pass the "noshow" flag to ctdl_getline()
640 void newprompt(char *prompt, char *str, int len)
643 color(BRIGHT_MAGENTA);
644 scr_printf("%s", prompt);
646 ctdl_getline(str, abs(len), (len<0), 0);
652 { /* returns a lower case value */
661 * parse the citadel.rc file
663 void load_command_set(void)
668 struct citcmd *lastcmd = NULL;
672 /* first, set up some defaults for non-required variables */
674 strcpy(editor_path, "");
675 strcpy(printcmd, "");
676 strcpy(imagecmd, "");
677 strcpy(rc_username, "");
678 strcpy(rc_password, "");
681 rc_allow_attachments = 0;
682 rc_remember_passwords = 0;
683 strcpy(rc_exp_cmd, "");
684 rc_display_message_numbers = 0;
685 rc_force_mail_prompts = 0;
688 strcpy(rc_url_cmd, "");
689 strcpy(rc_open_cmd, "");
690 strcpy(rc_gotmail_cmd, "");
692 rc_encrypt = RC_DEFAULT;
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(file_citadel_rc, "r");
705 if (ccfile == NULL) {
706 ccfile = fopen("/etc/citadel.rc", "r");
708 if (ccfile == NULL) {
709 ccfile = fopen("./citadel.rc", "r");
711 if (ccfile == NULL) {
712 perror("commands: cannot open citadel.rc");
715 while (fgets(buf, sizeof buf, ccfile) != NULL) {
716 while ((!IsEmptyStr(buf)) ? (isspace(buf[strlen(buf) - 1])) : 0)
717 buf[strlen(buf) - 1] = 0;
719 if (!strncasecmp(buf, "encrypt=", 8)) {
720 if (!strcasecmp(&buf[8], "yes")) {
724 fprintf(stderr, "citadel.rc requires encryption support but citadel is not compiled with OpenSSL");
729 else if (!strcasecmp(&buf[8], "no")) {
732 else if (!strcasecmp(&buf[8], "default")) {
733 rc_encrypt = RC_DEFAULT;
738 if (!strncasecmp(buf, "editor=", 7)) {
739 strcpy(editor_path, &buf[7]);
742 if (!strncasecmp(buf, "printcmd=", 9))
743 strcpy(printcmd, &buf[9]);
745 if (!strncasecmp(buf, "imagecmd=", 9))
746 strcpy(imagecmd, &buf[9]);
748 if (!strncasecmp(buf, "expcmd=", 7))
749 strcpy(rc_exp_cmd, &buf[7]);
751 if (!strncasecmp(buf, "use_floors=", 11)) {
752 if (!strcasecmp(&buf[11], "yes"))
753 rc_floor_mode = RC_YES;
754 if (!strcasecmp(&buf[11], "no"))
755 rc_floor_mode = RC_NO;
756 if (!strcasecmp(&buf[11], "default"))
757 rc_floor_mode = RC_DEFAULT;
759 if (!strncasecmp(buf, "beep=", 5)) {
760 rc_exp_beep = atoi(&buf[5]);
762 if (!strncasecmp(buf, "allow_attachments=", 18)) {
763 rc_allow_attachments = atoi(&buf[18]);
765 if (!strncasecmp(buf, "idle_threshold=", 15)) {
766 rc_idle_threshold = atol(&buf[15]);
768 if (!strncasecmp(buf, "remember_passwords=", 19)) {
769 rc_remember_passwords = atoi(&buf[19]);
771 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
772 rc_display_message_numbers = atoi(&buf[24]);
774 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
775 rc_force_mail_prompts = atoi(&buf[19]);
777 if (!strncasecmp(buf, "ansi_color=", 11)) {
778 if (!strncasecmp(&buf[11], "on", 2))
780 if (!strncasecmp(&buf[11], "auto", 4))
781 rc_ansi_color = 2; /* autodetect */
782 if (!strncasecmp(&buf[11], "user", 4))
783 rc_ansi_color = 3; /* user config */
785 if (!strncasecmp(buf, "status_line=", 12)) {
786 if (!strncasecmp(&buf[12], "on", 2))
787 enable_status_line = 1;
789 if (!strncasecmp(buf, "use_background=", 15)) {
790 if (!strncasecmp(&buf[15], "on", 2))
793 if (!strncasecmp(buf, "prompt_control=", 15)) {
794 if (!strncasecmp(&buf[15], "on", 2))
795 rc_prompt_control = 1;
796 if (!strncasecmp(&buf[15], "user", 4))
797 rc_prompt_control = 3; /* user config */
799 if (!strncasecmp(buf, "username=", 9))
800 strcpy(rc_username, &buf[9]);
802 if (!strncasecmp(buf, "password=", 9))
803 strcpy(rc_password, &buf[9]);
805 if (!strncasecmp(buf, "urlcmd=", 7))
806 strcpy(rc_url_cmd, &buf[7]);
808 if (!strncasecmp(buf, "opencmd=", 7))
809 strcpy(rc_open_cmd, &buf[8]);
811 if (!strncasecmp(buf, "gotmailcmd=", 11))
812 strcpy(rc_gotmail_cmd, &buf[11]);
814 if (!strncasecmp(buf, "cmd=", 4)) {
815 strcpy(buf, &buf[4]);
817 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
819 cptr->c_cmdnum = atoi(buf);
820 for (d = strlen(buf); d >= 0; --d)
823 strcpy(buf, &buf[b + 1]);
825 cptr->c_axlevel = atoi(buf);
826 for (d = strlen(buf); d >= 0; --d)
829 strcpy(buf, &buf[b + 1]);
831 for (a = 0; a < 5; ++a)
832 cptr->c_keys[a][0] = 0;
836 buf[strlen(buf) + 1] = 0;
837 while (!IsEmptyStr(buf)) {
839 for (d = strlen(buf); d >= 0; --d)
842 strncpy(cptr->c_keys[a], buf, b);
843 cptr->c_keys[a][b] = 0;
845 strcpy(buf, &buf[b + 1]);
855 lastcmd->next = cptr;
865 * return the key associated with a command
867 char keycmd(char *cmdstr)
871 for (a = 0; !IsEmptyStr(&cmdstr[a]); ++a)
872 if (cmdstr[a] == '&')
873 return (tolower(cmdstr[a + 1]));
879 * Output the string from a key command without the ampersand
880 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
882 char *cmd_expand(char *strbuf, int mode)
890 for (a = 0; exp[a]; ++a) {
891 if (strbuf[a] == '&') {
893 /* dont echo these non mnemonic command keys */
894 int noecho = strbuf[a+1] == '<' || strbuf[a+1] == '>' || strbuf[a+1] == '+' || strbuf[a+1] == '-';
897 strcpy(&exp[a], &exp[a + 1 + noecho]);
901 strcpy(buf, &exp[a + 2]);
907 if (!strncmp(&exp[a], "^r", 2)) {
909 strcpy(&exp[a], room_name);
910 strcat(exp, &buf[a + 2]);
912 if (!strncmp(&exp[a], "^c", 2)) {
914 strcpy(&exp[a + 1], &exp[a + 2]);
924 * Comparison function to determine if entered commands match a
925 * command loaded from the config file.
927 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
938 for (a = 0; a < ncomp; ++a) {
939 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
940 || (cptr->c_axlevel > cmdax))
948 * This function returns 1 if a given command requires a string input
950 int requires_string(struct citcmd *cptr, int ncomp)
955 strcpy(buf, cptr->c_keys[ncomp - 1]);
956 for (a = 0; !IsEmptyStr(&buf[a]); ++a) {
965 * Input a command at the main prompt.
966 * This function returns an integer command number. If the command prompts
967 * for a string then it is placed in the supplied buffer.
969 int getcmd(CtdlIPC *ipc, char *argbuf)
981 * Starting a new command now, so set sigcaught to 0. This variable
982 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
983 * been interrupted by a keypress.
987 /* Switch color support on or off if we're in user mode */
988 if (rc_ansi_color == 3) {
989 if (userflags & US_COLOR)
994 /* if we're running in idiot mode, display a cute little menu */
995 IFNEXPERT formout(ipc, "mainmenu");
1000 for (a = 0; a < 5; ++a)
1002 /* now the room prompt... */
1003 ok_to_interrupt = 1;
1004 color(BRIGHT_WHITE);
1005 scr_printf("\n%s", room_name);
1007 scr_printf("%c ", room_prompt(room_flags));
1011 ok_to_interrupt = 0;
1013 /* Handle the backspace key, but only if there's something
1014 * to backspace over...
1016 if ((ch == 8) && (cmdpos > 0)) {
1017 back(cmdspaces[cmdpos - 1] + 1);
1021 /* Spacebar invokes "lazy traversal" commands */
1022 if ((ch == 32) && (cmdpos == 0)) {
1023 this_lazy_cmd = next_lazy_cmd;
1024 if (this_lazy_cmd == 13)
1026 if (this_lazy_cmd == 5)
1028 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1029 if (cptr->c_cmdnum == this_lazy_cmd) {
1030 for (a = 0; a < 5; ++a)
1031 if (cptr->c_keys[a][0] != 0)
1032 scr_printf("%s ", cmd_expand(
1033 cptr->c_keys[a], 0));
1035 return (this_lazy_cmd);
1039 return (this_lazy_cmd);
1041 /* Otherwise, process the command */
1042 cmdbuf[cmdpos] = tolower(ch);
1044 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1045 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1047 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1048 cmdspaces[cmdpos] = strlen(
1049 cmd_expand(cptr->c_keys[cmdpos], 0));
1051 if ((cptr->c_keys[cmdpos + 1]) != 0)
1057 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1058 if (cmdmatch(cmdbuf, cptr, 5)) {
1059 /* We've found our command. */
1060 if (requires_string(cptr, cmdpos)) {
1062 ctdl_getline(argbuf, 64, 0, 0);
1067 /* If this command is one that changes rooms,
1068 * then the next lazy-command (space bar)
1069 * should be "read new" instead of "goto"
1071 if ((cptr->c_cmdnum == 5)
1072 || (cptr->c_cmdnum == 6)
1073 || (cptr->c_cmdnum == 47)
1074 || (cptr->c_cmdnum == 52)
1075 || (cptr->c_cmdnum == 16)
1076 || (cptr->c_cmdnum == 20))
1079 /* If this command is "read new"
1080 * then the next lazy-command (space bar)
1083 if (cptr->c_cmdnum == 13)
1086 return (cptr->c_cmdnum);
1092 scr_printf("\rOne of ... \n");
1093 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1094 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1095 for (a = 0; a < 5; ++a) {
1096 keyopt(cmd_expand(cptr->c_keys[a], 1));
1104 scr_printf("\n%s%c ", room_name, room_prompt(room_flags));
1106 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1107 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1108 for (a = 0; a < cmdpos; ++a) {
1110 cmd_expand(cptr->c_keys[a], 0));
1125 * set tty modes. commands are:
1127 * 01- set to Citadel mode
1128 * 2 - save current settings for later restoral
1129 * 3 - restore saved settings
1131 #ifdef HAVE_TERMIOS_H
1132 void stty_ctdl(int cmd)
1133 { /* SysV version of stty_ctdl() */
1134 struct termios live;
1135 static struct termios saved_settings;
1136 static int last_cmd = 0;
1143 if ((cmd == 0) || (cmd == 1)) {
1144 tcgetattr(0, &live);
1145 live.c_iflag = ISTRIP | IXON | IXANY;
1146 live.c_oflag = OPOST | ONLCR;
1147 live.c_lflag = ISIG | NOFLSH;
1149 live.c_cc[VINTR] = 0;
1150 live.c_cc[VQUIT] = 0;
1153 live.c_cc[VMIN] = 0;
1154 live.c_cc[VTIME] = 0;
1157 /* do we even need this stuff anymore? */
1158 /* live.c_line=0; */
1159 live.c_cc[VERASE] = 8;
1160 live.c_cc[VKILL] = 24;
1161 live.c_cc[VEOF] = 1;
1162 live.c_cc[VEOL] = 255;
1163 live.c_cc[VEOL2] = 0;
1164 live.c_cc[VSTART] = 0;
1165 tcsetattr(0, TCSADRAIN, &live);
1168 tcgetattr(0, &saved_settings);
1171 tcsetattr(0, TCSADRAIN, &saved_settings);
1176 void stty_ctdl(int cmd)
1177 { /* BSD version of stty_ctdl() */
1179 static struct sgttyb saved_settings;
1180 static int last_cmd = 0;
1187 if ((cmd == 0) || (cmd == 1)) {
1189 live.sg_flags |= CBREAK;
1190 live.sg_flags |= CRMOD;
1191 live.sg_flags |= NL1;
1192 live.sg_flags &= ~ECHO;
1194 live.sg_flags |= NOFLSH;
1198 gtty(0, &saved_settings);
1201 stty(0, &saved_settings);
1208 * display_help() - help file viewer
1210 void display_help(CtdlIPC *ipc, char *name)
1217 * fmout() - Citadel text formatter and paginator
1220 int width, /* screen width to use */
1221 FILE *fpin, /* file to read from, or NULL to format given text */
1222 char *text, /* text to be formatted (when fpin is NULL */
1223 FILE *fpout, /* file to write to, or NULL to write to screen */
1224 int subst) /* nonzero if we should use hypertext mode */
1226 char *buffer = NULL; /* The current message */
1227 char *word = NULL; /* What we are about to actually print */
1228 char *e; /* Pointer to position in text */
1229 char old = 0; /* The previous character */
1230 int column = 0; /* Current column */
1231 size_t i; /* Generic counter */
1233 /* Space for a single word, which can be at most screenwidth */
1234 word = (char *)calloc(1, width);
1236 scr_printf("Can't alloc memory to print message: %s!\n",
1241 /* Read the entire message body into memory */
1243 buffer = load_message_from_file(fpin);
1245 scr_printf("Can't print message: %s!\n",
1254 /* Run the message body */
1256 /* Catch characters that shouldn't be there at all */
1261 /* First, are we looking at a newline? */
1264 if (*e == ' ') { /* Paragraph */
1266 fprintf(fpout, "\n");
1271 } else if (old != ' ') {/* Don't print two spaces */
1273 fprintf(fpout, " ");
1283 /* Are we looking at a nonprintable?
1284 * (This section is now commented out because we could be displaying
1285 * a character set like UTF-8 or ISO-8859-1.)
1286 if ( (*e < 32) || (*e > 126) ) {
1291 /* Or are we looking at a space? */
1294 if (column >= width - 1) {
1295 /* Are we in the rightmost column? */
1297 fprintf(fpout, "\n");
1302 } else if (!(column == 0 && old == ' ')) {
1303 /* Eat only the first space on a line */
1305 fprintf(fpout, " ");
1311 /* ONLY eat the FIRST space on a line */
1317 /* Read a word, slightly messy */
1320 if (!isprint(e[i]) && !isspace(e[i]))
1327 /* We should never see these, but... slightly messy */
1328 if (e[i] == '\t' || e[i] == '\f' || e[i] == '\v')
1331 /* Break up really long words */
1332 /* TODO: auto-hyphenation someday? */
1335 strncpy(word, e, i);
1338 /* Decide where to print the word */
1339 if (column + i >= width) {
1340 /* Wrap to the next line */
1342 fprintf(fpout, "\n");
1349 /* Print the word */
1351 fprintf(fpout, "%s", word);
1353 scr_printf("%s", word);
1356 e += i; /* Start over with the whitepsace! */
1360 if (fpin) /* We allocated this, remember? */
1363 /* Is this necessary? It makes the output kind of spacey. */
1365 fprintf(fpout, "\n");
1375 * support ANSI color if defined
1377 void color(int colornum)
1379 static int hold_color;
1380 static int current_color;
1382 if (colornum == COLOR_PUSH) {
1383 hold_color = current_color;
1387 if (colornum == COLOR_POP) {
1392 current_color = colornum;
1394 /* When switching to dim white, actually output an 'original
1395 * pair' sequence -- this looks better on black-on-white
1396 * terminals. - Changed to ORIGINAL_PAIR as this actually
1397 * wound up looking horrible on black-on-white terminals, not
1398 * to mention transparent terminals.
1400 if (colornum == ORIGINAL_PAIR)
1401 printf("\033[0;39;49m");
1403 printf("\033[%d;3%d;4%dm",
1404 (colornum & 8) ? 1 : 0,
1411 void cls(int colornum)
1414 printf("\033[4%dm\033[2J\033[H\033[0m",
1415 colornum ? colornum : rc_color_use_bg);
1421 * Detect whether ANSI color is available (answerback)
1423 void send_ansi_detect(void)
1425 if (rc_ansi_color == 2) {
1432 void look_for_ansi(void)
1440 if (rc_ansi_color == 0) {
1442 } else if (rc_ansi_color == 1) {
1444 } else if (rc_ansi_color == 2) {
1446 /* otherwise, do the auto-detect */
1451 if ((now - AnsiDetect) < 2)
1460 select(1, &rfds, NULL, NULL, &tv);
1461 if (FD_ISSET(0, &rfds)) {
1462 abuf[strlen(abuf) + 1] = 0;
1463 rv = read(0, &abuf[strlen(abuf)], 1);
1465 scr_printf("failed to read after select: %s",
1470 } while (FD_ISSET(0, &rfds));
1472 for (a = 0; !IsEmptyStr(&abuf[a]); ++a) {
1473 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1474 && (abuf[a + 2] == '?')) {
1483 * Display key options (highlight hotkeys inside angle brackets)
1485 void keyopt(char *buf) {
1489 for (i=0; !IsEmptyStr(&buf[i]); ++i) {
1491 scr_printf("%c", buf[i]);
1492 color(BRIGHT_MAGENTA);
1494 if (buf[i]=='>'&& buf[i+1] != '>') {
1497 scr_printf("%c", buf[i]);
1506 * Present a key-menu line choice type of thing
1508 char keymenu(char *menuprompt, char *menustring) {
1514 int display_prompt = 1;
1516 choices = num_tokens(menustring, '|');
1518 if (menuprompt != NULL) do_prompt = 1;
1519 if ((menuprompt != NULL) && (IsEmptyStr(menuprompt))) do_prompt = 0;
1522 if (display_prompt) {
1524 scr_printf("%s ", menuprompt);
1527 for (i=0; i<choices; ++i) {
1528 extract_token(buf, menustring, i, '|', sizeof buf);
1538 if ( (do_prompt) && (ch=='?') ) {
1539 scr_printf("\rOne of... ");
1541 for (i=0; i<choices; ++i) {
1542 extract_token(buf, menustring, i, '|', sizeof buf);
1551 for (i=0; i<choices; ++i) {
1552 extract_token(buf, menustring, i, '|', sizeof buf);
1553 for (c=1; !IsEmptyStr(&buf[c]); ++c) {
1554 if ( (ch == tolower(buf[c]))
1556 && (buf[c+1]=='>') ) {
1557 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
1558 if ( (a!=(c-1)) && (a!=(c+1))) {