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 ((retval > 0) && 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) {
290 /* This may sometimes get called before we are actually connected
291 * to the server. Don't do anything if we aren't connected. -IO
293 if (!ipc_for_signal_handlers)
296 /* If full keepalives are enabled, send a NOOP to the server and
297 * wait for a response.
299 if (keepalives_enabled == KA_YES) {
300 CtdlIPCNoop(ipc_for_signal_handlers);
301 if (instant_msgs > 0) {
302 if (ok_to_interrupt == 1) {
303 scr_printf("\r%64s\r", "");
305 scr_printf("%s%c ", room_name,
306 room_prompt(room_flags));
312 /* If half keepalives are enabled, send a QNOP to the server (if the
313 * server supports it) and then do nothing.
315 if ( (keepalives_enabled == KA_HALF)
316 && (ipc_for_signal_handlers->ServInfo.supports_qnop > 0) ) {
317 CtdlIPC_chat_send(ipc_for_signal_handlers, "QNOP");
321 /* threaded nonblocking keepalive stuff starts here. I'm going for a simple
322 encapsulated interface; in theory there should be no need to touch these
323 globals outside of the async_ka_* functions. */
325 #ifdef THREADED_CLIENT
326 static pthread_t ka_thr_handle;
327 static int ka_thr_active = 0;
328 static int async_ka_enabled = 0;
330 static void *ka_thread(void *arg)
332 #ifdef HAVE_BACKTRACE
333 char threadName[256];
336 sprintf(threadName, "ka_Thread n");
338 // Register for tracing
339 eCrash_RegisterThread(threadName, 0);
341 really_do_keepalive();
342 pthread_detach(ka_thr_handle);
345 #ifdef HAVE_BACKTRACE
346 eCrash_UnregisterThread();
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 /* I changed this from static to not because I need to call it from
365 screen.c, either that or make something in screen.c not static.
366 Fix it how you like. Why all the staticness? stu */
368 void do_keepalive(void)
373 if ((now - idlet) < ((long) S_KEEPALIVE))
376 /* Do a space-backspace to keep telnet sessions from idling out */
377 scr_printf(" %c", 8);
380 #ifdef THREADED_CLIENT
381 if (async_ka_enabled)
385 really_do_keepalive();
389 /* Now the actual async-keepalve API that we expose to higher levels:
390 async_ka_start() and async_ka_end(). These do nothing when we don't have
391 threading enabled, so we avoid sprinkling ifdef's throughout the code. */
393 /* wait for a background keepalive to complete. this must be done before
394 attempting any further server requests! */
395 void async_ka_end(void)
397 #ifdef THREADED_CLIENT
399 pthread_join(ka_thr_handle, NULL);
405 /* tell do_keepalive() that keepalives are asynchronous. */
406 void async_ka_start(void)
408 #ifdef THREADED_CLIENT
415 { /* get a character from the keyboard, with */
416 int a; /* the watchdog timer in effect if necessary */
425 /* This loop waits for keyboard input. If the keepalive
426 * timer expires, it sends a keepalive to the server if
427 * necessary and then waits again.
434 tv.tv_sec = S_KEEPALIVE;
437 select(1, &rfds, NULL, NULL, &tv);
438 } while (!FD_ISSET(0, &rfds));
440 /* At this point, there's input, so fetch it.
441 * (There's a hole in the bucket...)
443 a = scr_getc(SCR_BLOCK);
456 { /* Returns 1 for yes, 0 for no */
472 /* Returns 1 for yes, 0 for no, arg is default value */
495 /* Gets a line from the terminal */
496 /* string == Pointer to string buffer */
497 /* lim == Maximum length - if negative, no-show */
498 void ctdl_getline(char *string, int lim)
512 /* a = (a & 127); ** commented out because it isn't just an ASCII world anymore */
513 if ((a == 8 || a == 23) && (IsEmptyStr(string)))
515 if ((a != 10) && (a != 8) && (strlen(string) == lim))
517 if ((a == 8) && (string[0] != 0)) {
518 string[strlen(string) - 1] = 0;
519 scr_putc(8); scr_putc(32); scr_putc(8);
522 if ((a == 23) && (string[0] != 0)) {
524 string[strlen(string) - 1] = 0;
525 scr_putc(8); scr_putc(32); scr_putc(8);
526 } 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);
584 * boolprompt() - prompt for a yes/no, print the existing value and
585 * allow the user to press return to keep it...
587 int boolprompt(char *prompt, int prev_val)
592 scr_printf("%s ", prompt);
595 color(BRIGHT_MAGENTA);
596 scr_printf("%s", (prev_val ? "Yes" : "No"));
600 r = (yesno_d(prev_val));
606 * intprompt() - like strprompt(), except for an integer
607 * (note that it RETURNS the new value!)
609 int intprompt(char *prompt, int ival, int imin, int imax)
617 snprintf(buf, sizeof buf, "%d", i);
618 strprompt(prompt, buf, 15);
620 for (p=0; !IsEmptyStr(&buf[p]); ++p) {
621 if ( (!isdigit(buf[p]))
622 && ( (buf[p]!='-') || (p!=0) ) )
626 scr_printf("*** Must be no less than %d.\n", imin);
628 scr_printf("*** Must be no more than %d.\n", imax);
629 } while ((i < imin) || (i > imax));
634 * newprompt() - prompt for a string with no existing value
635 * (clears out string buffer first)
637 void newprompt(char *prompt, char *str, int len)
639 color(BRIGHT_MAGENTA);
640 scr_printf("%s", prompt);
642 ctdl_getline(str, len);
648 { /* returns a lower case value */
657 * parse the citadel.rc file
659 void load_command_set(void)
663 char editor_key[100];
665 struct citcmd *lastcmd = NULL;
671 /* first, set up some defaults for non-required variables */
673 for (i = 0; i < MAX_EDITORS; i++)
674 strcpy(editor_paths[i], "");
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;
694 rc_alt_semantics = 0;
696 /* now try to open the citadel.rc file */
699 if (getenv("HOME") != NULL) {
700 snprintf(buf, sizeof buf, "%s/.citadelrc", getenv("HOME"));
701 ccfile = fopen(buf, "r");
703 if (ccfile == NULL) {
704 ccfile = fopen(file_citadel_rc, "r");
706 if (ccfile == NULL) {
707 ccfile = fopen("/etc/citadel.rc", "r");
709 if (ccfile == NULL) {
710 ccfile = fopen("./citadel.rc", "r");
712 if (ccfile == NULL) {
713 perror("commands: cannot open citadel.rc");
716 while (fgets(buf, sizeof buf, ccfile) != NULL) {
717 while ((!IsEmptyStr(buf)) ? (isspace(buf[strlen(buf) - 1])) : 0)
718 buf[strlen(buf) - 1] = 0;
720 if (!strncasecmp(buf, "encrypt=", 8)) {
721 if (!strcasecmp(&buf[8], "yes")) {
725 fprintf(stderr, "citadel.rc requires encryption support but citadel is not compiled with OpenSSL");
730 else if (!strcasecmp(&buf[8], "no")) {
733 else if (!strcasecmp(&buf[8], "default")) {
734 rc_encrypt = RC_DEFAULT;
739 if (!strncasecmp(buf, "editor=", 7))
740 strcpy(editor_paths[0], &buf[7]);
742 for (i = 0; i < MAX_EDITORS; i++)
744 sprintf(editor_key, "editor%d=", i);
745 if (!strncasecmp(buf, editor_key, strlen(editor_key)))
746 strcpy(editor_paths[i], &buf[strlen(editor_key)]);
749 if (!strncasecmp(buf, "printcmd=", 9))
750 strcpy(printcmd, &buf[9]);
752 if (!strncasecmp(buf, "imagecmd=", 9))
753 strcpy(imagecmd, &buf[9]);
755 if (!strncasecmp(buf, "expcmd=", 7))
756 strcpy(rc_exp_cmd, &buf[7]);
758 if (!strncasecmp(buf, "local_screen_dimensions=", 24))
759 have_xterm = (char) atoi(&buf[24]);
761 if (!strncasecmp(buf, "use_floors=", 11)) {
762 if (!strcasecmp(&buf[11], "yes"))
763 rc_floor_mode = RC_YES;
764 if (!strcasecmp(&buf[11], "no"))
765 rc_floor_mode = RC_NO;
766 if (!strcasecmp(&buf[11], "default"))
767 rc_floor_mode = RC_DEFAULT;
769 if (!strncasecmp(buf, "beep=", 5)) {
770 rc_exp_beep = atoi(&buf[5]);
772 if (!strncasecmp(buf, "allow_attachments=", 18)) {
773 rc_allow_attachments = atoi(&buf[18]);
775 if (!strncasecmp(buf, "idle_threshold=", 15)) {
776 rc_idle_threshold = atol(&buf[15]);
778 if (!strncasecmp(buf, "remember_passwords=", 19)) {
779 rc_remember_passwords = atoi(&buf[19]);
781 if (!strncasecmp(buf, "display_message_numbers=", 24)) {
782 rc_display_message_numbers = atoi(&buf[24]);
784 if (!strncasecmp(buf, "force_mail_prompts=", 19)) {
785 rc_force_mail_prompts = atoi(&buf[19]);
787 if (!strncasecmp(buf, "ansi_color=", 11)) {
788 if (!strncasecmp(&buf[11], "on", 2))
790 if (!strncasecmp(&buf[11], "auto", 4))
791 rc_ansi_color = 2; /* autodetect */
792 if (!strncasecmp(&buf[11], "user", 4))
793 rc_ansi_color = 3; /* user config */
795 if (!strncasecmp(buf, "use_background=", 15)) {
796 if (!strncasecmp(&buf[15], "on", 2))
799 if (!strncasecmp(buf, "prompt_control=", 15)) {
800 if (!strncasecmp(&buf[15], "on", 2))
801 rc_prompt_control = 1;
802 if (!strncasecmp(&buf[15], "user", 4))
803 rc_prompt_control = 3; /* user config */
805 if (!strncasecmp(buf, "username=", 9))
806 strcpy(rc_username, &buf[9]);
808 if (!strncasecmp(buf, "password=", 9))
809 strcpy(rc_password, &buf[9]);
811 if (!strncasecmp(buf, "urlcmd=", 7))
812 strcpy(rc_url_cmd, &buf[7]);
814 if (!strncasecmp(buf, "opencmd=", 7))
815 strcpy(rc_open_cmd, &buf[8]);
817 if (!strncasecmp(buf, "gotmailcmd=", 11))
818 strcpy(rc_gotmail_cmd, &buf[11]);
820 if (!strncasecmp(buf, "alternate_semantics=", 20)) {
821 if (!strncasecmp(&buf[20], "yes", 3)) {
822 rc_alt_semantics = 1;
825 rc_alt_semantics = 0;
829 if (!strncasecmp(buf, "cmd=", 4)) {
830 strcpy(buf, &buf[4]);
832 cptr = (struct citcmd *) malloc(sizeof(struct citcmd));
834 cptr->c_cmdnum = atoi(buf);
835 for (d = strlen(buf); d >= 0; --d)
838 strcpy(buf, &buf[b + 1]);
840 cptr->c_axlevel = atoi(buf);
841 for (d = strlen(buf); d >= 0; --d)
844 strcpy(buf, &buf[b + 1]);
846 for (a = 0; a < 5; ++a)
847 cptr->c_keys[a][0] = 0;
851 buf[strlen(buf) + 1] = 0;
852 while (!IsEmptyStr(buf)) {
854 for (d = strlen(buf); d >= 0; --d)
857 strncpy(cptr->c_keys[a], buf, b);
858 cptr->c_keys[a][b] = 0;
860 strcpy(buf, &buf[b + 1]);
870 lastcmd->next = cptr;
880 * return the key associated with a command
882 char keycmd(char *cmdstr)
886 for (a = 0; !IsEmptyStr(&cmdstr[a]); ++a)
887 if (cmdstr[a] == '&')
888 return (tolower(cmdstr[a + 1]));
894 * Output the string from a key command without the ampersand
895 * "mode" should be set to 0 for normal or 1 for <C>ommand key highlighting
897 char *cmd_expand(char *strbuf, int mode)
905 for (a = 0; exp[a]; ++a) {
906 if (strbuf[a] == '&') {
908 /* dont echo these non mnemonic command keys */
909 int noecho = strbuf[a+1] == '<' || strbuf[a+1] == '>' || strbuf[a+1] == '+' || strbuf[a+1] == '-';
912 strcpy(&exp[a], &exp[a + 1 + noecho]);
916 strcpy(buf, &exp[a + 2]);
922 if (!strncmp(&exp[a], "^r", 2)) {
924 strcpy(&exp[a], room_name);
925 strcat(exp, &buf[a + 2]);
927 if (!strncmp(&exp[a], "^c", 2)) {
929 strcpy(&exp[a + 1], &exp[a + 2]);
939 * Comparison function to determine if entered commands match a
940 * command loaded from the config file.
942 int cmdmatch(char *cmdbuf, struct citcmd *cptr, int ncomp)
953 for (a = 0; a < ncomp; ++a) {
954 if ((tolower(cmdbuf[a]) != keycmd(cptr->c_keys[a]))
955 || (cptr->c_axlevel > cmdax))
963 * This function returns 1 if a given command requires a string input
965 int requires_string(struct citcmd *cptr, int ncomp)
970 strcpy(buf, cptr->c_keys[ncomp - 1]);
971 for (a = 0; !IsEmptyStr(&buf[a]); ++a) {
980 * Input a command at the main prompt.
981 * This function returns an integer command number. If the command prompts
982 * for a string then it is placed in the supplied buffer.
984 int getcmd(CtdlIPC *ipc, char *argbuf)
996 * Starting a new command now, so set sigcaught to 0. This variable
997 * is set to nonzero (usually NEXT_KEY or STOP_KEY) if a command has
998 * been interrupted by a keypress.
1002 /* Switch color support on or off if we're in user mode */
1003 if (rc_ansi_color == 3) {
1004 if (userflags & US_COLOR)
1009 /* if we're running in idiot mode, display a cute little menu */
1010 IFNEXPERT formout(ipc, "mainmenu");
1015 for (a = 0; a < 5; ++a)
1017 /* now the room prompt... */
1018 ok_to_interrupt = 1;
1019 color(BRIGHT_WHITE);
1020 scr_printf("\n%s", room_name);
1022 scr_printf("%c ", room_prompt(room_flags));
1026 ok_to_interrupt = 0;
1028 /* Handle the backspace key, but only if there's something
1029 * to backspace over...
1031 if ((ch == 8) && (cmdpos > 0)) {
1032 back(cmdspaces[cmdpos - 1] + 1);
1036 /* Spacebar invokes "lazy traversal" commands */
1037 if ((ch == 32) && (cmdpos == 0)) {
1038 this_lazy_cmd = next_lazy_cmd;
1039 if (this_lazy_cmd == 13)
1041 if (this_lazy_cmd == 5)
1043 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1044 if (cptr->c_cmdnum == this_lazy_cmd) {
1045 for (a = 0; a < 5; ++a)
1046 if (cptr->c_keys[a][0] != 0)
1047 scr_printf("%s ", cmd_expand(
1048 cptr->c_keys[a], 0));
1050 return (this_lazy_cmd);
1054 return (this_lazy_cmd);
1056 /* Otherwise, process the command */
1057 cmdbuf[cmdpos] = tolower(ch);
1059 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1060 if (cmdmatch(cmdbuf, cptr, cmdpos + 1)) {
1062 scr_printf("%s", cmd_expand(cptr->c_keys[cmdpos], 0));
1063 cmdspaces[cmdpos] = strlen(
1064 cmd_expand(cptr->c_keys[cmdpos], 0));
1066 if ((cptr->c_keys[cmdpos + 1]) != 0)
1072 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1073 if (cmdmatch(cmdbuf, cptr, 5)) {
1074 /* We've found our command. */
1075 if (requires_string(cptr, cmdpos)) {
1076 ctdl_getline(argbuf, 64);
1081 /* If this command is one that changes rooms,
1082 * then the next lazy-command (space bar)
1083 * should be "read new" instead of "goto"
1085 if ((cptr->c_cmdnum == 5)
1086 || (cptr->c_cmdnum == 6)
1087 || (cptr->c_cmdnum == 47)
1088 || (cptr->c_cmdnum == 52)
1089 || (cptr->c_cmdnum == 16)
1090 || (cptr->c_cmdnum == 20))
1093 /* If this command is "read new"
1094 * then the next lazy-command (space bar)
1097 if (cptr->c_cmdnum == 13)
1100 return (cptr->c_cmdnum);
1106 scr_printf("\rOne of ... \n");
1107 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1108 if (cmdmatch(cmdbuf, cptr, cmdpos)) {
1109 for (a = 0; a < 5; ++a) {
1110 keyopt(cmd_expand(cptr->c_keys[a], 1));
1118 scr_printf("\n%s%c ", room_name, room_prompt(room_flags));
1120 for (cptr = cmdlist; cptr != NULL; cptr = cptr->next) {
1121 if ((got == 0) && (cmdmatch(cmdbuf, cptr, cmdpos))) {
1122 for (a = 0; a < cmdpos; ++a) {
1124 cmd_expand(cptr->c_keys[a], 0));
1139 * set tty modes. commands are:
1141 * 01- set to Citadel mode
1142 * 2 - save current settings for later restoral
1143 * 3 - restore saved settings
1145 #ifdef HAVE_TERMIOS_H
1146 void stty_ctdl(int cmd)
1147 { /* SysV version of stty_ctdl() */
1148 struct termios live;
1149 static struct termios saved_settings;
1150 static int last_cmd = 0;
1157 if ((cmd == 0) || (cmd == 1)) {
1158 tcgetattr(0, &live);
1159 live.c_iflag = ISTRIP | IXON | IXANY;
1160 live.c_oflag = OPOST | ONLCR;
1161 live.c_lflag = ISIG | NOFLSH;
1163 live.c_cc[VINTR] = 0;
1164 live.c_cc[VQUIT] = 0;
1167 live.c_cc[VMIN] = 0;
1168 live.c_cc[VTIME] = 0;
1171 /* do we even need this stuff anymore? */
1172 /* live.c_line=0; */
1173 live.c_cc[VERASE] = 8;
1174 live.c_cc[VKILL] = 24;
1175 live.c_cc[VEOF] = 1;
1176 live.c_cc[VEOL] = 255;
1177 live.c_cc[VEOL2] = 0;
1178 live.c_cc[VSTART] = 0;
1179 tcsetattr(0, TCSADRAIN, &live);
1182 tcgetattr(0, &saved_settings);
1185 tcsetattr(0, TCSADRAIN, &saved_settings);
1190 void stty_ctdl(int cmd)
1191 { /* BSD version of stty_ctdl() */
1193 static struct sgttyb saved_settings;
1194 static int last_cmd = 0;
1201 if ((cmd == 0) || (cmd == 1)) {
1203 live.sg_flags |= CBREAK;
1204 live.sg_flags |= CRMOD;
1205 live.sg_flags |= NL1;
1206 live.sg_flags &= ~ECHO;
1208 live.sg_flags |= NOFLSH;
1212 gtty(0, &saved_settings);
1215 stty(0, &saved_settings);
1222 * display_help() - help file viewer
1224 void display_help(CtdlIPC *ipc, char *name)
1231 * fmout() - Citadel text formatter and paginator
1234 int width, /* screen width to use */
1235 FILE *fpin, /* file to read from, or NULL to format given text */
1236 char *text, /* text to be formatted (when fpin is NULL */
1237 FILE *fpout, /* file to write to, or NULL to write to screen */
1238 int subst) /* nonzero if we should use hypertext mode */
1240 char *buffer = NULL; /* The current message */
1241 char *word = NULL; /* What we are about to actually print */
1242 char *e; /* Pointer to position in text */
1243 char old = 0; /* The previous character */
1244 int column = 0; /* Current column */
1245 size_t i; /* Generic counter */
1247 /* Space for a single word, which can be at most screenwidth */
1248 word = (char *)calloc(1, width);
1250 scr_printf("Can't alloc memory to print message: %s!\n",
1255 /* Read the entire message body into memory */
1257 buffer = load_message_from_file(fpin);
1259 scr_printf("Can't print message: %s!\n",
1268 /* Run the message body */
1270 /* Catch characters that shouldn't be there at all */
1275 /* First, are we looking at a newline? */
1278 if (*e == ' ') { /* Paragraph */
1280 fprintf(fpout, "\n");
1285 } else if (old != ' ') {/* Don't print two spaces */
1287 fprintf(fpout, " ");
1297 /* Are we looking at a nonprintable?
1298 * (This section is now commented out because we could be displaying
1299 * a character set like UTF-8 or ISO-8859-1.)
1300 if ( (*e < 32) || (*e > 126) ) {
1305 /* Or are we looking at a space? */
1308 if (column >= width - 1) {
1309 /* Are we in the rightmost column? */
1311 fprintf(fpout, "\n");
1316 } else if (!(column == 0 && old == ' ')) {
1317 /* Eat only the first space on a line */
1319 fprintf(fpout, " ");
1325 /* ONLY eat the FIRST space on a line */
1331 /* Read a word, slightly messy */
1334 if (!isprint(e[i]) && !isspace(e[i]))
1341 /* We should never see these, but... slightly messy */
1342 if (e[i] == '\t' || e[i] == '\f' || e[i] == '\v')
1345 /* Break up really long words */
1346 /* TODO: auto-hyphenation someday? */
1349 strncpy(word, e, i);
1352 /* Decide where to print the word */
1353 if (column + i >= width) {
1354 /* Wrap to the next line */
1356 fprintf(fpout, "\n");
1363 /* Print the word */
1365 fprintf(fpout, "%s", word);
1367 scr_printf("%s", word);
1370 e += i; /* Start over with the whitepsace! */
1374 if (fpin) /* We allocated this, remember? */
1377 /* Is this necessary? It makes the output kind of spacey. */
1379 fprintf(fpout, "\n");
1389 * support ANSI color if defined
1391 void color(int colornum)
1393 static int hold_color;
1394 static int current_color;
1396 if (colornum == COLOR_PUSH) {
1397 hold_color = current_color;
1401 if (colornum == COLOR_POP) {
1406 current_color = colornum;
1408 /* When switching to dim white, actually output an 'original
1409 * pair' sequence -- this looks better on black-on-white
1410 * terminals. - Changed to ORIGINAL_PAIR as this actually
1411 * wound up looking horrible on black-on-white terminals, not
1412 * to mention transparent terminals.
1414 if (colornum == ORIGINAL_PAIR)
1415 printf("\033[0;39;49m");
1417 printf("\033[%d;3%d;4%dm",
1418 (colornum & 8) ? 1 : 0,
1425 void cls(int colornum)
1428 printf("\033[4%dm\033[2J\033[H\033[0m",
1429 colornum ? colornum : rc_color_use_bg);
1435 * Detect whether ANSI color is available (answerback)
1437 void send_ansi_detect(void)
1439 if (rc_ansi_color == 2) {
1446 void look_for_ansi(void)
1454 if (rc_ansi_color == 0) {
1456 } else if (rc_ansi_color == 1) {
1458 } else if (rc_ansi_color == 2) {
1460 /* otherwise, do the auto-detect */
1465 if ((now - AnsiDetect) < 2)
1474 select(1, &rfds, NULL, NULL, &tv);
1475 if (FD_ISSET(0, &rfds)) {
1476 abuf[strlen(abuf) + 1] = 0;
1477 rv = read(0, &abuf[strlen(abuf)], 1);
1479 scr_printf("failed to read after select: %s",
1484 } while (FD_ISSET(0, &rfds));
1486 for (a = 0; !IsEmptyStr(&abuf[a]); ++a) {
1487 if ((abuf[a] == 27) && (abuf[a + 1] == '[')
1488 && (abuf[a + 2] == '?')) {
1497 * Display key options (highlight hotkeys inside angle brackets)
1499 void keyopt(char *buf) {
1503 for (i=0; !IsEmptyStr(&buf[i]); ++i) {
1505 scr_printf("%c", buf[i]);
1506 color(BRIGHT_MAGENTA);
1508 if (buf[i]=='>'&& buf[i+1] != '>') {
1511 scr_printf("%c", buf[i]);
1520 * Present a key-menu line choice type of thing
1522 char keymenu(char *menuprompt, char *menustring) {
1528 int display_prompt = 1;
1530 choices = num_tokens(menustring, '|');
1532 if (menuprompt != NULL) do_prompt = 1;
1533 if ((menuprompt != NULL) && (IsEmptyStr(menuprompt))) do_prompt = 0;
1536 if (display_prompt) {
1538 scr_printf("%s ", menuprompt);
1541 for (i=0; i<choices; ++i) {
1542 extract_token(buf, menustring, i, '|', sizeof buf);
1552 if ( (do_prompt) && (ch=='?') ) {
1553 scr_printf("\rOne of... ");
1555 for (i=0; i<choices; ++i) {
1556 extract_token(buf, menustring, i, '|', sizeof buf);
1565 for (i=0; i<choices; ++i) {
1566 extract_token(buf, menustring, i, '|', sizeof buf);
1567 for (c=1; !IsEmptyStr(&buf[c]); ++c) {
1568 if ( (ch == tolower(buf[c]))
1570 && (buf[c+1]=='>') ) {
1571 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
1572 if ( (a!=(c-1)) && (a!=(c+1))) {