2 * This file contains functions which implement parts of the
3 * text-mode user interface.
5 * Copyright (c) 1987-2010 by the citadel.org team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
48 #ifdef HAVE_SYS_SELECT_H
49 #include <sys/select.h>
52 #ifdef THREADED_CLIENT
59 #include <libcitadel.h>
61 #include "citadel_ipc.h"
64 #include "citadel_decls.h"
66 #include "routines2.h"
68 #include "client_chat.h"
69 #include "citadel_dirs.h"
83 #define IFNEXPERT if ((userflags&US_EXPERT)==0)
87 char rc_exp_cmd[1024];
88 int rc_allow_attachments;
89 int rc_display_message_numbers;
90 int rc_force_mail_prompts;
91 int rc_remember_passwords;
94 int rc_prompt_control = 0;
95 time_t rc_idle_threshold = (time_t)900;
97 char rc_open_cmd[SIZ];
98 char rc_gotmail_cmd[SIZ];
101 int next_lazy_cmd = 5;
103 extern int screenwidth, screenheight;
105 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
107 struct citcmd *cmdlist = NULL;
110 /* these variables are local to this module */
111 char keepalives_enabled = KA_YES; /* send NOOPs to server when idle */
112 int ok_to_interrupt = 0; /* print instant msgs asynchronously */
113 time_t AnsiDetect; /* when did we send the detect code? */
114 int enable_color = 0; /* nonzero for ANSI color */
120 * If an interesting key has been pressed, return its value, otherwise 0
122 char was_a_key_pressed(void) {
132 retval = select(1, &rfds, NULL, NULL, &tv);
134 /* Careful! Disable keepalives during keyboard polling; we're probably
135 * in the middle of a data transfer from the server, in which case
136 * sending a NOOP would throw the client protocol out of sync.
138 if (FD_ISSET(0, &rfds)) {
139 set_keepalives(KA_NO);
140 the_character = inkey();
141 set_keepalives(KA_YES);
146 return(the_character);
154 * print_instant() - print instant messages if there are any
156 void print_instant(void)
165 char *listing = NULL;
166 int r; /* IPC result code */
168 if (instant_msgs == 0)
174 if (IsEmptyStr(rc_exp_cmd)) {
179 while (instant_msgs != 0) {
180 r = CtdlIPCGetInstantMessage(ipc_for_signal_handlers, &listing, buf);
184 instant_msgs = extract_int(buf, 0);
185 timestamp = extract_long(buf, 1);
186 flags = extract_int(buf, 2);
187 extract_token(sender, buf, 3, '|', sizeof sender);
188 extract_token(node, buf, 4, '|', sizeof node);
189 strcpy(last_paged, sender);
191 localtime_r(×tamp, &stamp);
193 /* If the page is a Logoff Request, honor it. */
199 if (!IsEmptyStr(rc_exp_cmd)) {
200 outpipe = popen(rc_exp_cmd, "w");
201 if (outpipe != NULL) {
202 /* Header derived from flags */
205 "Please log off now, as requested ");
207 fprintf(outpipe, "Broadcast message ");
209 fprintf(outpipe, "Chat request ");
211 fprintf(outpipe, "Message ");
212 /* Timestamp. Can this be improved? */
213 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)
214 fprintf(outpipe, "at 12:%02d%cm",
216 stamp.tm_hour ? 'p' : 'a');
217 else if (stamp.tm_hour > 12) /* pm */
218 fprintf(outpipe, "at %d:%02dpm",
222 fprintf(outpipe, "at %d:%02dam",
223 stamp.tm_hour, stamp.tm_min);
224 fprintf(outpipe, " from %s", sender);
225 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
226 fprintf(outpipe, " @%s", node);
227 fprintf(outpipe, ":\n%s\n", listing);
229 if (instant_msgs == 0)
234 /* fall back to built-in instant message display */
237 /* Header derived from flags */
239 scr_printf("Please log off now, as requested ");
241 scr_printf("Broadcast message ");
243 scr_printf("Chat request ");
245 scr_printf("Message ");
247 /* Timestamp. Can this be improved? */
248 if (stamp.tm_hour == 0 || stamp.tm_hour == 12)/* 12am/12pm */
249 scr_printf("at 12:%02d%cm", stamp.tm_min,
250 stamp.tm_hour ? 'p' : 'a');
251 else if (stamp.tm_hour > 12) /* pm */
252 scr_printf("at %d:%02dpm",
253 stamp.tm_hour - 12, stamp.tm_min);
255 scr_printf("at %d:%02dam", stamp.tm_hour, stamp.tm_min);
258 scr_printf(" from %s", sender);
260 /* Remote node, if any */
261 if (strncmp(ipc_for_signal_handlers->ServInfo.nodename, node, 32))
262 scr_printf(" @%s", node);
265 fmout(screenwidth, NULL, listing, NULL, 0);
269 scr_printf("\n---\n");
276 void set_keepalives(int s)
278 keepalives_enabled = (char) s;
282 * This loop handles the "keepalive" messages sent to the server when idling.
285 static time_t idlet = 0;
286 static void really_do_keepalive(void) {
287 int r; /* IPC response code */
291 /* This may sometimes get called before we are actually connected
292 * to the server. Don't do anything if we aren't connected. -IO
294 if (!ipc_for_signal_handlers)
297 /* If full keepalives are enabled, send a NOOP to the server and
298 * wait for a response.
300 if (keepalives_enabled == KA_YES) {
301 r = CtdlIPCNoop(ipc_for_signal_handlers);
302 if (instant_msgs > 0) {
303 if (ok_to_interrupt == 1) {
304 scr_printf("\r%64s\r", "");
306 scr_printf("%s%c ", room_name,
307 room_prompt(room_flags));
313 /* If half keepalives are enabled, send a QNOP to the server (if the
314 * server supports it) and then do nothing.
316 if ( (keepalives_enabled == KA_HALF)
317 && (ipc_for_signal_handlers->ServInfo.supports_qnop > 0) ) {
318 CtdlIPC_chat_send(ipc_for_signal_handlers, "QNOP");
322 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
323 encapsulated interface; in theory there should be no need to touch these
324 globals outside of the async_ka_* functions. */
326 #ifdef THREADED_CLIENT
327 static pthread_t ka_thr_handle;
328 static int ka_thr_active = 0;
329 static int async_ka_enabled = 0;
331 static void *ka_thread(void *arg)
333 #ifdef HAVE_BACKTRACE
334 char threadName[256];
337 sprintf(threadName, "ka_Thread n");
339 // Register for tracing
340 eCrash_RegisterThread(threadName, 0);
342 really_do_keepalive();
343 pthread_detach(ka_thr_handle);
346 #ifdef HAVE_BACKTRACE
347 eCrash_UnregisterThread();
352 /* start up a thread to handle a keepalive in the background */
353 static void async_ka_exec(void)
355 if (!ka_thr_active) {
357 if (pthread_create(&ka_thr_handle, NULL, ka_thread, NULL)) {
358 perror("pthread_create");
363 #endif /* THREADED_CLIENT */
365 /* I changed this from static to not because I need to call it from
366 screen.c, either that or make something in screen.c not static.
367 Fix it how you like. Why all the staticness? stu */
369 void do_keepalive(void)
374 if ((now - idlet) < ((long) S_KEEPALIVE))
377 /* Do a space-backspace to keep telnet sessions from idling out */
378 scr_printf(" %c", 8);
381 #ifdef THREADED_CLIENT
382 if (async_ka_enabled)
386 really_do_keepalive();
390 /* Now the actual async-keepalve API that we expose to higher levels:
391 async_ka_start() and async_ka_end(). These do nothing when we don't have
392 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
394 /* wait for a background keepalive to complete. this must be done before
395 attempting any further server requests! */
396 void async_ka_end(void)
398 #ifdef THREADED_CLIENT
400 pthread_join(ka_thr_handle, NULL);
406 /* tell do_keepalive() that keepalives are asynchronous. */
407 void async_ka_start(void)
409 #ifdef THREADED_CLIENT
416 { /* get a character from the keyboard, with */
417 int a; /* the watchdog timer in effect if necessary */
426 /* This loop waits for keyboard input. If the keepalive
427 * timer expires, it sends a keepalive to the server if
428 * necessary and then waits again.
435 tv.tv_sec = S_KEEPALIVE;
438 select(1, &rfds, NULL, NULL, &tv);
439 } while (!FD_ISSET(0, &rfds));
441 /* At this point, there's input, so fetch it.
442 * (There's a hole in the bucket...)
444 a = scr_getc(SCR_BLOCK);
457 { /* Returns 1 for yes, 0 for no */
473 /* Returns 1 for yes, 0 for no, arg is default value */
496 /* Gets a line from the terminal */
497 /* string == Pointer to string buffer */
498 /* lim == Maximum length - if negative, no-show */
499 void ctdl_getline(char *string, int lim)
513 /* a = (a & 127); ** commented out because it isn't just an ASCII world anymore */
514 if ((a == 8 || a == 23) && (IsEmptyStr(string)))
516 if ((a != 10) && (a != 8) && (strlen(string) == lim))
518 if ((a == 8) && (string[0] != 0)) {
519 string[strlen(string) - 1] = 0;
520 scr_putc(8); scr_putc(32); scr_putc(8);
523 if ((a == 23) && (string[0] != 0)) {
525 string[strlen(string) - 1] = 0;
526 scr_putc(8); scr_putc(32); scr_putc(8);
527 } while (!IsEmptyStr(string) && string[strlen(string) - 1] != ' ');
549 * strprompt() - prompt for a string, print the existing value and
550 * allow the user to press return to keep it...
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, 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)
640 color(BRIGHT_MAGENTA);
641 scr_printf("%s", prompt);
643 ctdl_getline(str, len);
649 { /* returns a lower case value */
658 * parse the citadel.rc file
660 void load_command_set(void)
664 char editor_key[100];
666 struct citcmd *lastcmd = NULL;
672 /* first, set up some defaults for non-required variables */
674 for (i = 0; i < MAX_EDITORS; i++)
675 strcpy(editor_paths[i], "");
676 strcpy(printcmd, "");
677 strcpy(imagecmd, "");
678 strcpy(rc_username, "");
679 strcpy(rc_password, "");
682 rc_allow_attachments = 0;
683 rc_remember_passwords = 0;
684 strcpy(rc_exp_cmd, "");
685 rc_display_message_numbers = 0;
686 rc_force_mail_prompts = 0;
689 strcpy(rc_url_cmd, "");
690 strcpy(rc_open_cmd, "");
691 strcpy(rc_gotmail_cmd, "");
693 rc_encrypt = RC_DEFAULT;
695 rc_alt_semantics = 0;
697 /* now try to open the citadel.rc file */
700 if (getenv("HOME") != NULL) {
701 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
702 ccfile = fopen(buf, "r");
704 if (ccfile == NULL) {
705 ccfile = fopen(file_citadel_rc, "r");
707 if (ccfile == NULL) {
708 ccfile = fopen("/etc/citadel.rc", "r");
710 if (ccfile == NULL) {
711 ccfile = fopen("./citadel.rc", "r");
713 if (ccfile == NULL) {
714 perror("commands: cannot open citadel.rc");
717 while (fgets(buf, sizeof buf, ccfile) != NULL) {
718 while ((!IsEmptyStr(buf)) ? (isspace(buf[strlen(buf) - 1])) : 0)
719 buf[strlen(buf) - 1] = 0;
721 if (!strncasecmp(buf, "encrypt=", 8)) {
722 if (!strcasecmp(&buf[8], "yes")) {
726 fprintf(stderr, "citadel.rc requires encryption support but citadel is not compiled with OpenSSL");
731 else if (!strcasecmp(&buf[8], "no")) {
734 else if (!strcasecmp(&buf[8], "default")) {
735 rc_encrypt = RC_DEFAULT;
740 if (!strncasecmp(buf, "editor=", 7))
741 strcpy(editor_paths[0], &buf[7]);
743 for (i = 0; i < MAX_EDITORS; i++)
745 sprintf(editor_key, "editor%d=", i);
746 if (!strncasecmp(buf, editor_key, strlen(editor_key)))
747 strcpy(editor_paths[i], &buf[strlen(editor_key)]);
750 if (!strncasecmp(buf, "printcmd=", 9))
751 strcpy(printcmd, &buf[9]);
753 if (!strncasecmp(buf, "imagecmd=", 9))
754 strcpy(imagecmd, &buf[9]);
756 if (!strncasecmp(buf, "expcmd=", 7))
757 strcpy(rc_exp_cmd, &buf[7]);
759 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
760 have_xterm = (char) atoi(&buf[24]);
762 if (!strncasecmp(buf, "use_floors=", 11)) {
763 if (!strcasecmp(&buf[11], "yes"))
764 rc_floor_mode = RC_YES;
765 if (!strcasecmp(&buf[11], "no"))
766 rc_floor_mode = RC_NO;
767 if (!strcasecmp(&buf[11], "default"))
768 rc_floor_mode = RC_DEFAULT;
770 if (!strncasecmp(buf, "beep=", 5)) {
771 rc_exp_beep = atoi(&buf[5]);
773 if (!strncasecmp(buf, "allow_attachments=", 18)) {
774 rc_allow_attachments = atoi(&buf[18]);
776 if (!strncasecmp(buf, "idle_threshold=", 15)) {
777 rc_idle_threshold = atol(&buf[15]);
779 if (!strncasecmp(buf, "remember_passwords=", 19)) {
780 rc_remember_passwords = atoi(&buf[19]);
782 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
783 rc_display_message_numbers = atoi(&buf[24]);
785 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
786 rc_force_mail_prompts = atoi(&buf[19]);
788 if (!strncasecmp(buf, "ansi_color=", 11)) {
789 if (!strncasecmp(&buf[11], "on", 2))
791 if (!strncasecmp(&buf[11], "auto", 4))
792 rc_ansi_color = 2; /* autodetect */
793 if (!strncasecmp(&buf[11], "user", 4))
794 rc_ansi_color = 3; /* user config */
796 if (!strncasecmp(buf, "use_background=", 15)) {
797 if (!strncasecmp(&buf[15], "on", 2))
800 if (!strncasecmp(buf, "prompt_control=", 15)) {
801 if (!strncasecmp(&buf[15], "on", 2))
802 rc_prompt_control = 1;
803 if (!strncasecmp(&buf[15], "user", 4))
804 rc_prompt_control = 3; /* user config */
806 if (!strncasecmp(buf, "username=", 9))
807 strcpy(rc_username, &buf[9]);
809 if (!strncasecmp(buf, "password=", 9))
810 strcpy(rc_password, &buf[9]);
812 if (!strncasecmp(buf, "urlcmd=", 7))
813 strcpy(rc_url_cmd, &buf[7]);
815 if (!strncasecmp(buf, "opencmd=", 7))
816 strcpy(rc_open_cmd, &buf[8]);
818 if (!strncasecmp(buf, "gotmailcmd=", 11))
819 strcpy(rc_gotmail_cmd, &buf[11]);
821 if (!strncasecmp(buf, "alternate_semantics=", 20)) {
822 if (!strncasecmp(&buf[20], "yes", 3)) {
823 rc_alt_semantics = 1;
826 rc_alt_semantics = 0;
830 if (!strncasecmp(buf, "cmd=", 4)) {
831 strcpy(buf, &buf[4]);
833 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
835 cptr->c_cmdnum = atoi(buf);
836 for (d = strlen(buf); d >= 0; --d)
839 strcpy(buf, &buf[b + 1]);
841 cptr->c_axlevel = atoi(buf);
842 for (d = strlen(buf); d >= 0; --d)
845 strcpy(buf, &buf[b + 1]);
847 for (a = 0; a < 5; ++a)
848 cptr->c_keys[a][0] = 0;
852 buf[strlen(buf) + 1] = 0;
853 while (!IsEmptyStr(buf)) {
855 for (d = strlen(buf); d >= 0; --d)
858 strncpy(cptr->c_keys[a], buf, b);
859 cptr->c_keys[a][b] = 0;
861 strcpy(buf, &buf[b + 1]);
871 lastcmd->next = cptr;
881 * return the key associated with a command
883 char keycmd(char *cmdstr)
887 for (a = 0; !IsEmptyStr(&cmdstr[a]); ++a)
888 if (cmdstr[a] == '&')
889 return (tolower(cmdstr[a + 1]));
895 * Output the string from a key command without the ampersand
896 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
898 char *cmd_expand(char *strbuf, int mode)
906 for (a = 0; exp[a]; ++a) {
907 if (strbuf[a] == '&') {
909 /* dont echo these non mnemonic command keys */
910 int noecho = strbuf[a+1] == '<' || strbuf[a+1] == '>' || strbuf[a+1] == '+' || strbuf[a+1] == '-';
913 strcpy(&exp[a], &exp[a + 1 + noecho]);
917 strcpy(buf, &exp[a + 2]);
923 if (!strncmp(&exp[a], "^r", 2)) {
925 strcpy(&exp[a], room_name);
926 strcat(exp, &buf[a + 2]);
928 if (!strncmp(&exp[a], "^c", 2)) {
930 strcpy(&exp[a + 1], &exp[a + 2]);
940 * Comparison function to determine if entered commands match a
941 * command loaded from the config file.
943 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
954 for (a = 0; a < ncomp; ++a) {
955 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
956 || (cptr->c_axlevel > cmdax))
964 * This function returns 1 if a given command requires a string input
966 int requires_string(struct citcmd *cptr, int ncomp)
971 strcpy(buf, cptr->c_keys[ncomp - 1]);
972 for (a = 0; !IsEmptyStr(&buf[a]); ++a) {
981 * Input a command at the main prompt.
982 * This function returns an integer command number. If the command prompts
983 * for a string then it is placed in the supplied buffer.
985 int getcmd(CtdlIPC *ipc, char *argbuf)
997 * Starting a new command now, so set sigcaught to 0. This variable
998 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
999 * been interrupted by a keypress.
1003 /* Switch color support on or off if we're in user mode */
1004 if (rc_ansi_color == 3) {
1005 if (userflags & US_COLOR)
1010 /* if we're running in idiot mode, display a cute little menu */
1011 IFNEXPERT formout(ipc, "mainmenu");
1016 for (a = 0; a < 5; ++a)
1018 /* now the room prompt... */
1019 ok_to_interrupt = 1;
1020 color(BRIGHT_WHITE);
1021 scr_printf("\n%s", room_name);
1023 scr_printf("%c ", room_prompt(room_flags));
1027 ok_to_interrupt = 0;
1029 /* Handle the backspace key, but only if there's something
1030 * to backspace over...
1032 if ((ch == 8) && (cmdpos > 0)) {
1033 back(cmdspaces[cmdpos - 1] + 1);
1037 /* Spacebar invokes "lazy traversal" commands */
1038 if ((ch == 32) && (cmdpos == 0)) {
1039 this_lazy_cmd = next_lazy_cmd;
1040 if (this_lazy_cmd == 13)
1042 if (this_lazy_cmd == 5)
1044 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1045 if (cptr->c_cmdnum == this_lazy_cmd) {
1046 for (a = 0; a < 5; ++a)
1047 if (cptr->c_keys[a][0] != 0)
1048 scr_printf("%s ", cmd_expand(
1049 cptr->c_keys[a], 0));
1051 return (this_lazy_cmd);
1055 return (this_lazy_cmd);
1057 /* Otherwise, process the command */
1058 cmdbuf[cmdpos] = tolower(ch);
1060 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1061 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1063 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1064 cmdspaces[cmdpos] = strlen(
1065 cmd_expand(cptr->c_keys[cmdpos], 0));
1067 if ((cptr->c_keys[cmdpos + 1]) != 0)
1073 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1074 if (cmdmatch(cmdbuf, cptr, 5)) {
1075 /* We've found our command. */
1076 if (requires_string(cptr, cmdpos)) {
1077 ctdl_getline(argbuf, 64);
1082 /* If this command is one that changes rooms,
1083 * then the next lazy-command (space bar)
1084 * should be "read new" instead of "goto"
1086 if ((cptr->c_cmdnum == 5)
1087 || (cptr->c_cmdnum == 6)
1088 || (cptr->c_cmdnum == 47)
1089 || (cptr->c_cmdnum == 52)
1090 || (cptr->c_cmdnum == 16)
1091 || (cptr->c_cmdnum == 20))
1094 /* If this command is "read new"
1095 * then the next lazy-command (space bar)
1098 if (cptr->c_cmdnum == 13)
1101 return (cptr->c_cmdnum);
1107 scr_printf("\rOne of ... \n");
1108 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1109 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1110 for (a = 0; a < 5; ++a) {
1111 keyopt(cmd_expand(cptr->c_keys[a], 1));
1119 scr_printf("\n%s%c ", room_name, room_prompt(room_flags));
1121 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1122 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1123 for (a = 0; a < cmdpos; ++a) {
1125 cmd_expand(cptr->c_keys[a], 0));
1140 * set tty modes. commands are:
1142 * 01- set to Citadel mode
1143 * 2 - save current settings for later restoral
1144 * 3 - restore saved settings
1146 #ifdef HAVE_TERMIOS_H
1147 void stty_ctdl(int cmd)
1148 { /* SysV version of stty_ctdl() */
1149 struct termios live;
1150 static struct termios saved_settings;
1151 static int last_cmd = 0;
1158 if ((cmd == 0) || (cmd == 1)) {
1159 tcgetattr(0, &live);
1160 live.c_iflag = ISTRIP | IXON | IXANY;
1161 live.c_oflag = OPOST | ONLCR;
1162 live.c_lflag = ISIG | NOFLSH;
1164 live.c_cc[VINTR] = 0;
1165 live.c_cc[VQUIT] = 0;
1168 live.c_cc[VMIN] = 0;
1169 live.c_cc[VTIME] = 0;
1172 /* do we even need this stuff anymore? */
1173 /* live.c_line=0; */
1174 live.c_cc[VERASE] = 8;
1175 live.c_cc[VKILL] = 24;
1176 live.c_cc[VEOF] = 1;
1177 live.c_cc[VEOL] = 255;
1178 live.c_cc[VEOL2] = 0;
1179 live.c_cc[VSTART] = 0;
1180 tcsetattr(0, TCSADRAIN, &live);
1183 tcgetattr(0, &saved_settings);
1186 tcsetattr(0, TCSADRAIN, &saved_settings);
1191 void stty_ctdl(int cmd)
1192 { /* BSD version of stty_ctdl() */
1194 static struct sgttyb saved_settings;
1195 static int last_cmd = 0;
1202 if ((cmd == 0) || (cmd == 1)) {
1204 live.sg_flags |= CBREAK;
1205 live.sg_flags |= CRMOD;
1206 live.sg_flags |= NL1;
1207 live.sg_flags &= ~ECHO;
1209 live.sg_flags |= NOFLSH;
1213 gtty(0, &saved_settings);
1216 stty(0, &saved_settings);
1223 * display_help() - help file viewer
1225 void display_help(CtdlIPC *ipc, char *name)
1232 * fmout() - Citadel text formatter and paginator
1235 int width, /* screen width to use */
1236 FILE *fpin, /* file to read from, or NULL to format given text */
1237 char *text, /* text to be formatted (when fpin is NULL */
1238 FILE *fpout, /* file to write to, or NULL to write to screen */
1239 int subst) /* nonzero if we should use hypertext mode */
1241 char *buffer = NULL; /* The current message */
1242 char *word = NULL; /* What we are about to actually print */
1243 char *e; /* Pointer to position in text */
1244 char old = 0; /* The previous character */
1245 int column = 0; /* Current column */
1246 size_t i; /* Generic counter */
1248 /* Space for a single word, which can be at most screenwidth */
1249 word = (char *)calloc(1, width);
1251 scr_printf("Can't alloc memory to print message: %s!\n",
1256 /* Read the entire message body into memory */
1258 buffer = load_message_from_file(fpin);
1260 scr_printf("Can't print message: %s!\n",
1269 /* Run the message body */
1271 /* Catch characters that shouldn't be there at all */
1276 /* First, are we looking at a newline? */
1279 if (*e == ' ') { /* Paragraph */
1281 fprintf(fpout, "\n");
1286 } else if (old != ' ') {/* Don't print two spaces */
1288 fprintf(fpout, " ");
1298 /* Are we looking at a nonprintable?
1299 * (This section is now commented out because we could be displaying
1300 * a character set like UTF-8 or ISO-8859-1.)
1301 if ( (*e < 32) || (*e > 126) ) {
1306 /* Or are we looking at a space? */
1309 if (column >= width - 1) {
1310 /* Are we in the rightmost column? */
1312 fprintf(fpout, "\n");
1317 } else if (!(column == 0 && old == ' ')) {
1318 /* Eat only the first space on a line */
1320 fprintf(fpout, " ");
1326 /* ONLY eat the FIRST space on a line */
1332 /* Read a word, slightly messy */
1335 if (!isprint(e[i]) && !isspace(e[i]))
1342 /* We should never see these, but... slightly messy */
1343 if (e[i] == '\t' || e[i] == '\f' || e[i] == '\v')
1346 /* Break up really long words */
1347 /* TODO: auto-hyphenation someday? */
1350 strncpy(word, e, i);
1353 /* Decide where to print the word */
1354 if (column + i >= width) {
1355 /* Wrap to the next line */
1357 fprintf(fpout, "\n");
1364 /* Print the word */
1366 fprintf(fpout, "%s", word);
1368 scr_printf("%s", word);
1371 e += i; /* Start over with the whitepsace! */
1375 if (fpin) /* We allocated this, remember? */
1378 /* Is this necessary? It makes the output kind of spacey. */
1380 fprintf(fpout, "\n");
1390 * support ANSI color if defined
1392 void color(int colornum)
1394 static int hold_color;
1395 static int current_color;
1397 if (colornum == COLOR_PUSH) {
1398 hold_color = current_color;
1402 if (colornum == COLOR_POP) {
1407 current_color = colornum;
1409 /* When switching to dim white, actually output an 'original
1410 * pair' sequence -- this looks better on black-on-white
1411 * terminals. - Changed to ORIGINAL_PAIR as this actually
1412 * wound up looking horrible on black-on-white terminals, not
1413 * to mention transparent terminals.
1415 if (colornum == ORIGINAL_PAIR)
1416 printf("\033[0;39;49m");
1418 printf("\033[%d;3%d;4%dm",
1419 (colornum & 8) ? 1 : 0,
1426 void cls(int colornum)
1429 printf("\033[4%dm\033[2J\033[H\033[0m",
1430 colornum ? colornum : rc_color_use_bg);
1436 * Detect whether ANSI color is available (answerback)
1438 void send_ansi_detect(void)
1440 if (rc_ansi_color == 2) {
1447 void look_for_ansi(void)
1455 if (rc_ansi_color == 0) {
1457 } else if (rc_ansi_color == 1) {
1459 } else if (rc_ansi_color == 2) {
1461 /* otherwise, do the auto-detect */
1466 if ((now - AnsiDetect) < 2)
1475 select(1, &rfds, NULL, NULL, &tv);
1476 if (FD_ISSET(0, &rfds)) {
1477 abuf[strlen(abuf) + 1] = 0;
1478 rv = read(0, &abuf[strlen(abuf)], 1);
1480 } while (FD_ISSET(0, &rfds));
1482 for (a = 0; !IsEmptyStr(&abuf[a]); ++a) {
1483 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1484 && (abuf[a + 2] == '?')) {
1493 * Display key options (highlight hotkeys inside angle brackets)
1495 void keyopt(char *buf) {
1499 for (i=0; !IsEmptyStr(&buf[i]); ++i) {
1501 scr_printf("%c", buf[i]);
1502 color(BRIGHT_MAGENTA);
1504 if (buf[i]=='>'&& buf[i+1] != '>') {
1507 scr_printf("%c", buf[i]);
1516 * Present a key-menu line choice type of thing
1518 char keymenu(char *menuprompt, char *menustring) {
1524 int display_prompt = 1;
1526 choices = num_tokens(menustring, '|');
1528 if (menuprompt != NULL) do_prompt = 1;
1529 if ((menuprompt != NULL) && (IsEmptyStr(menuprompt))) do_prompt = 0;
1532 if (display_prompt) {
1534 scr_printf("%s ", menuprompt);
1537 for (i=0; i<choices; ++i) {
1538 extract_token(buf, menustring, i, '|', sizeof buf);
1548 if ( (do_prompt) && (ch=='?') ) {
1549 scr_printf("\rOne of... ");
1551 for (i=0; i<choices; ++i) {
1552 extract_token(buf, menustring, i, '|', sizeof buf);
1561 for (i=0; i<choices; ++i) {
1562 extract_token(buf, menustring, i, '|', sizeof buf);
1563 for (c=1; !IsEmptyStr(&buf[c]); ++c) {
1564 if ( (ch == tolower(buf[c]))
1566 && (buf[c+1]=='>') ) {
1567 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
1568 if ( (a!=(c-1)) && (a!=(c+1))) {