* Rewrite support for specific SSL encryption protocols, including
[alpine.git] / alpine / status.c
blobffdbf95e18c7e63aa6d353041b5d2e219142d14c
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: status.c 840 2007-12-01 01:34:49Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2018 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 /*======================================================================
20 status.c
21 Functions that manage the status line (third from the bottom)
22 - put messages on the queue to be displayed
23 - display messages on the queue with timers
24 - check queue to figure out next timeout
25 - prompt for yes/no type of questions
26 ====*/
28 #include "headers.h"
29 #include "status.h"
30 #include "keymenu.h"
31 #include "mailview.h"
32 #include "mailcmd.h"
33 #include "busy.h"
34 #include "after.h"
36 #include "../pith/charconv/utf8.h"
38 #include "../pith/conf.h"
39 #include "../pith/bitmap.h"
43 * Internal queue of messages. The circular, double-linked list's
44 * allocated on demand, and cleared as each message is displayed.
46 typedef struct message {
47 char *text;
48 unsigned flags:8;
49 unsigned shown:1;
50 unsigned saw_it:1;
51 unsigned pending_removal:1;
52 int min_display_time, max_display_time;
53 struct message *next, *prev;
54 } SMQ_T;
58 * Internal prototypes
60 void pause_for_and_dq_cur_msg(void);
61 SMQ_T *top_of_queue(void);
62 int new_info_msg_need_not_be_queued(void);
63 int is_last_message(SMQ_T *);
64 void d_q_status_message(void);
65 int output_message(SMQ_T *, int);
66 int output_message_modal(SMQ_T *, int);
67 void delay_cmd_cue(int);
68 int modal_bogus_input(UCS);
69 int messages_in_queue(void);
70 int status_message_remaining_nolock(void);
71 void set_saw_it_to_zero();
72 void mark_modals_done();
73 SMQ_T *copy_status_queue(SMQ_T *);
77 /*----------------------------------------------------------------------
78 Manage the second line from the bottom where status and error messages
79 are displayed. A small queue is set up and messages are put on the queue
80 by calling one of the q_status_message routines. Even though this is a queue
81 most of the time message will go right on through. The messages are
82 displayed just before the read for the next command, or when a read times
83 out. Read timeouts occur every minute or so for new mail checking and every
84 few seconds when there are still messages on the queue. Hopefully, this scheme
85 will not let messages fly past that the user can't see.
86 ----------------------------------------------------------------------*/
89 static SMQ_T *message_queue = NULL;
90 static short needs_clearing = 0, /* Flag set by want_to()
91 and optionally_enter() */
92 prevstartcol, prevendcol;
93 static char prevstatusbuf[6*MAX_SCREEN_COLS+1];
94 static time_t displayed_time;
97 /*----------------------------------------------------------------------
98 Put a message for the status line on the queue
100 Args: time -- the min time in seconds to display the message
101 message -- message string
103 Result: queues message on queue represented by static variables
105 This puts a single message on the queue to be shown.
106 ----------*/
107 void
108 q_status_message(int flags, int min_time, int max_time, char *message)
110 SMQ_T *new, *q;
111 char *clean_msg;
112 size_t mlen;
114 status_message_lock();
117 * If there is already a message queued and
118 * new message is just informational, discard it.
120 if(flags & SM_INFO && new_info_msg_need_not_be_queued()){
121 status_message_unlock();
122 return;
126 * By convention, we have min_time equal to zero in messages which we
127 * think are not as important, so-called comfort messages. We have
128 * min_time >= 3 for messages which we think the user should see for
129 * sure. Some users don't like to wait so we've provided a way for them
130 * to live on the edge.
131 * status_msg_delay == -1 => min time == MIN(0, min_time)
132 * status_msg_delay == -2 => min time == MIN(1, min_time)
133 * status_msg_delay == -3 => min time == MIN(2, min_time)
134 * ...
136 if(ps_global->status_msg_delay < 0)
137 min_time = MIN(-1 - ps_global->status_msg_delay, min_time);
139 /* The 40 is room for 40 escaped control characters */
140 mlen = strlen(message) + 40;
141 clean_msg = (char *)fs_get(mlen + 1);
142 iutf8ncpy(clean_msg, message, mlen); /* does the cleaning */
144 clean_msg[mlen] = '\0';
146 if((q = message_queue) != NULL){ /* the queue exists */
149 * Scan through all of the messages currently in the queue.
150 * If the new message is already queued, don't add it again.
152 do {
153 if(!q->pending_removal && q->text && !strcmp(q->text, clean_msg)){
154 q->shown = 0;
155 if(q->min_display_time < min_time)
156 q->min_display_time = min_time;
158 if(q->max_display_time < max_time)
159 q->max_display_time = max_time;
161 dprint((9, "q_status_message(%s): skipping duplicate msg\n",
162 clean_msg ? clean_msg : "?"));
164 if(clean_msg)
165 fs_give((void **)&clean_msg);
167 status_message_unlock();
168 return;
171 q = q->next;
173 } while(q != message_queue);
176 new = (SMQ_T *)fs_get(sizeof(SMQ_T));
177 memset(new, 0, sizeof(SMQ_T));
178 new->text = clean_msg;
179 new->min_display_time = min_time;
180 new->max_display_time = max_time;
181 new->flags = flags;
182 if(message_queue){
183 new->next = message_queue;
184 new->prev = message_queue->prev;
185 new->prev->next = message_queue->prev = new;
187 else
188 message_queue = new->next = new->prev = new;
190 status_message_unlock();
192 dprint((9, "q_status_message(%s)\n",
193 clean_msg ? clean_msg : "?"));
197 /*----------------------------------------------------------------------
198 Mark the status line as dirty so it gets cleared next chance
199 ----*/
200 void
201 mark_status_dirty(void)
203 mark_status_unknown();
204 needs_clearing++;
208 /*----------------------------------------------------------------------
209 Cause status line drawing optimization to be turned off, because we
210 don't know what the status line looks like.
211 ----*/
212 void
213 mark_status_unknown(void)
215 prevstartcol = -1;
216 prevendcol = -1;
217 prevstatusbuf[0] = '\0';
221 /*----------------------------------------------------------------------
222 Wait a suitable amount of time for the currently displayed message
223 ----*/
224 void
225 pause_for_and_dq_cur_msg(void)
227 if(top_of_queue()){
228 int w;
230 if((w = status_message_remaining_nolock()) != 0){
231 delay_cmd_cue(1);
232 /* protect user from changes in the clock. If the clock
233 * changes during this process (for example, going from
234 * Standard time to Delayed time) this may result in big
235 * values for w. In those cases, reset w.
237 w = (w > 0 ? (w > 5 ? 5 : w) : 0);
238 sleep(w);
239 delay_cmd_cue(0);
242 d_q_status_message();
248 * This relies on the global displayed_time being properly set
249 * for this message.
251 void
252 pause_for_and_mark_specific_msg(SMQ_T *msg)
254 if(msg){
255 int w;
257 w = (int) (displayed_time - time(0)) + msg->min_display_time;
258 w = (w > 0) ? (w > 5 ? 5 : w): 0;
259 if(w){
260 delay_cmd_cue(1);
261 sleep(w);
262 delay_cmd_cue(0);
265 msg->pending_removal = 1;
270 /*----------------------------------------------------------------------
271 Time remaining for current message's minimum display
272 ----*/
274 status_message_remaining(void)
276 int ret;
278 status_message_lock();
279 ret = status_message_remaining_nolock();
280 status_message_unlock();
282 return(ret);
287 status_message_remaining_nolock(void)
289 SMQ_T *q;
290 int d = 0;
292 if((q = top_of_queue()) != NULL)
293 d = (int) (displayed_time - time(0)) + q->min_display_time;
295 return((d > 0) ? d : 0);
300 * Return first message in queue that isn't pending_removal.
302 SMQ_T *
303 top_of_queue(void)
305 SMQ_T *p;
307 if((p = message_queue) != NULL){
309 if(!p->pending_removal)
310 return(p);
311 while((p = p->next) != message_queue);
314 return(NULL);
319 new_info_msg_need_not_be_queued(void)
321 SMQ_T *q;
323 if(status_message_remaining_nolock() > 0)
324 return 1;
326 if((q = top_of_queue()) != NULL && (q = q->next) != message_queue){
328 if(!q->pending_removal && !(q->flags & SM_INFO))
329 return 1;
330 while((q = q->next) != message_queue);
333 return 0;
337 /*----------------------------------------------------------------------
338 Find out how many messages are queued for display
340 Args: dtime -- will get set to minimum display time for current message
342 Result: number of messages in the queue.
344 ---------*/
346 messages_queued(long int *dtime)
348 SMQ_T *q;
349 int ret;
351 status_message_lock();
352 if(dtime && (q = top_of_queue()) != NULL)
353 *dtime = (long) MAX(q->min_display_time, 1L);
355 ret = ps_global->in_init_seq ? 0 : messages_in_queue();
357 status_message_unlock();
359 return(ret);
363 /*----------------------------------------------------------------------
364 Return number of messages in queue
365 ---------*/
367 messages_in_queue(void)
369 int n = 0;
370 SMQ_T *p;
372 if((p = message_queue) != NULL){
374 if(!p->pending_removal)
375 n++;
376 while((p = p->next) != message_queue);
379 return(n);
383 /*----------------------------------------------------------------------
384 Return last message queued
385 ---------*/
386 char *
387 last_message_queued(void)
389 SMQ_T *p, *r = NULL;
390 char *ret = NULL;
392 status_message_lock();
393 if((p = message_queue) != NULL){
395 if(p->flags & SM_ORDER && !p->pending_removal)
396 r = p;
397 while((p = p->next) != message_queue);
400 ret = (r && r->text) ? cpystr(r->text) : NULL;
402 status_message_unlock();
404 return(ret);
409 is_last_message(SMQ_T *msg)
411 SMQ_T *p, *r = NULL;
413 if(msg && !msg->pending_removal){
414 if((p = message_queue) != NULL){
416 if(!p->pending_removal)
417 r = p;
418 while((p = p->next) != message_queue);
422 return(r && msg && (r == msg));
426 /*----------------------------------------------------------------------
427 Update status line, clearing or displaying a message
429 Arg: command -- The command that is about to be executed
431 Result: status line cleared or
432 next message queued is displayed or
433 current message is redisplayed.
434 if next message displayed, it's min display time
435 is returned else if message already displayed, it's
436 time remaining on the display is returned, else 0.
438 This is called when ready to display the next message, usually just
439 before reading the next command from the user. We pass in the nature
440 of the command because it affects what we do here. If the command just
441 executed by the user is a redraw screen, we don't want to reset or go to
442 next message because it might not have been seen. Also if the command
443 is just a noop, which are usually executed when checking for new mail
444 and happen every few minutes, we don't clear the message.
446 If it was really a command and there's nothing more to show, then we
447 clear, because we know the user has seen the message. In this case the
448 user might be typing commands very quickly and miss a message, so
449 there is a time stamp and time check that each message has been on the
450 screen for a few seconds. If it hasn't we just return and let it be
451 taken care of next time.
452 ----------------------------------------------------------------------*/
454 display_message(UCS command)
456 SMQ_T *q, *copy_of_q;
457 int need_to_unlock;
458 int ding;
460 if(ps_global == NULL || ps_global->ttyo == NULL
461 || ps_global->ttyo->screen_rows < 1 || ps_global->in_init_seq)
462 return(0);
464 status_message_lock();
466 /*---- Deal with any previously displayed message ----*/
467 if((q = top_of_queue()) != NULL && q->shown){
468 int rv = -1;
470 if(command == ctrl('L')){ /* just repaint it, and go on */
471 mark_status_unknown();
472 mark_keymenu_dirty();
473 mark_titlebar_dirty();
474 rv = 0;
476 else{ /* ensure sufficient time's passed */
477 time_t now;
478 int diff;
480 now = time(0);
481 diff = (int)(displayed_time - now)
482 + ((command == NO_OP_COMMAND || command == NO_OP_IDLE)
483 ? q->max_display_time
484 : q->min_display_time);
485 dprint((9,
486 "STATUS: diff:%d, displayed: %ld, now: %ld\n",
487 diff, (long) displayed_time, (long) now));
488 if(diff > 0)
489 rv = diff; /* check again next time */
490 else if(is_last_message(q)
491 && (command == NO_OP_COMMAND || command == NO_OP_IDLE)
492 && q->max_display_time)
493 rv = 0; /* last msg, no cmd, has max */
496 if(rv >= 0){ /* leave message displayed? */
497 if(prevstartcol < 0){ /* need to redisplay it? */
498 ding = q->flags & SM_DING;
499 q->flags &= ~SM_DING;
500 if(q->flags & SM_MODAL && !q->shown){
501 copy_of_q = copy_status_queue(q);
502 mark_modals_done();
503 status_message_unlock();
504 output_message_modal(copy_of_q, ding);
506 else{
507 output_message(q, ding);
508 status_message_unlock();
511 else
512 status_message_unlock();
514 return(rv);
517 d_q_status_message(); /* remove it from queue and */
518 needs_clearing++; /* clear the line if needed */
521 if(!top_of_queue() && (command == ctrl('L') || needs_clearing)){
522 int inverse;
523 struct variable *vars = ps_global->vars;
524 char *last_bg = NULL;
526 dprint((9, "Clearing status line\n"));
527 inverse = InverseState(); /* already in inverse? */
528 if(inverse && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
529 VAR_STATUS_BACK_COLOR){
530 last_bg = pico_get_last_bg_color();
531 pico_set_nbg_color(); /* so ClearLine will clear in bg color */
534 ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
535 if(last_bg){
536 (void)pico_set_bg_color(last_bg);
537 if(last_bg)
538 fs_give((void **)&last_bg);
541 mark_status_unknown();
542 if(command == ctrl('L')){
543 mark_keymenu_dirty();
544 mark_titlebar_dirty();
548 need_to_unlock = 1;
550 /* display next message, weeding out 0 display times */
551 for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
552 if(q->min_display_time || is_last_message(q)){
553 displayed_time = time(0);
554 ding = q->flags & SM_DING;
555 q->flags &= ~SM_DING;
556 if(q->flags & SM_MODAL && !q->shown){
557 copy_of_q = copy_status_queue(q);
558 mark_modals_done();
559 status_message_unlock();
560 output_message_modal(copy_of_q, ding);
561 need_to_unlock = 0;
563 else
564 output_message(q, ding);
566 break;
568 /* zero display time message, log it, delete it */
569 else{
570 if(q->text){
571 char buf[1000];
572 char *append = " [not actually shown]";
573 char *ptr;
574 size_t len;
576 len = strlen(q->text) + strlen(append);
577 if(len < sizeof(buf))
578 ptr = buf;
579 else
580 ptr = (char *) fs_get((len+1) * sizeof(char));
582 strncpy(ptr, q->text, len);
583 ptr[len] = '\0';
584 strncat(ptr, append, len+1-1-strlen(ptr));
585 ptr[len] = '\0';
586 add_review_message(ptr, -1);
587 if(ptr != buf)
588 fs_give((void **) &ptr);
591 d_q_status_message();
595 needs_clearing = 0; /* always cleared or written */
596 fflush(stdout);
598 if(need_to_unlock)
599 status_message_unlock();
601 return(0);
605 /*----------------------------------------------------------------------
606 Display all the messages on the queue as quickly as possible
607 ----*/
608 void
609 flush_status_messages(int skip_last_pause)
611 SMQ_T *q, *copy_of_q;
612 int ding;
614 start_over:
615 status_message_lock();
617 for(q = top_of_queue(); q; q = top_of_queue()){
618 /* don't have to wait for this one */
619 if(is_last_message(q) && skip_last_pause && q->shown)
620 break;
622 if(q->shown)
623 pause_for_and_dq_cur_msg();
625 /* find next one we need to show and show it */
626 for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
627 if((q->min_display_time || is_last_message(q))){
628 displayed_time = time(0);
629 ding = q->flags & SM_DING;
630 q->flags &= ~SM_DING;
631 if(q->flags & SM_MODAL){
632 copy_of_q = copy_status_queue(q);
633 mark_modals_done();
634 status_message_unlock();
635 output_message_modal(copy_of_q, ding);
638 * Because we unlock the message queue in order
639 * to display modal messages we have to worry
640 * about the queue being changed while we have
641 * it unlocked. So start the whole process over.
643 goto start_over;
645 else
646 output_message(q, ding);
648 else{
649 d_q_status_message();
654 status_message_unlock();
658 /*----------------------------------------------------------------------
659 Make sure any and all SM_ORDER messages get displayed.
661 Note: This flags the message line as having nothing displayed.
662 The idea is that it's a function called by routines that want
663 the message line for a prompt or something, and that they're
664 going to obliterate the message anyway.
665 ----*/
666 void
667 flush_ordered_messages(void)
669 SMQ_T *q, *copy_of_q;
670 int firsttime = 1;
671 int ding;
673 status_message_lock();
675 set_saw_it_to_zero();
677 start_over2:
678 if(!firsttime)
679 status_message_lock();
680 else
681 firsttime = 0;
683 if((q = message_queue) != NULL){
685 if(q->pending_removal || q->saw_it || q->shown){
686 if(!q->pending_removal && q->shown)
687 pause_for_and_mark_specific_msg(q);
689 q->saw_it = 1;
690 q = (q->next != message_queue) ? q->next : NULL;
692 else{
693 /* find next one we need to show and show it */
695 if(q->pending_removal || q->saw_it || q->shown){
696 q->saw_it = 1;
697 q = (q->next != message_queue) ? q->next : NULL;
699 else{
700 if((q->flags & (SM_ORDER | SM_MODAL))
701 && q->min_display_time){
702 displayed_time = time(0);
703 ding = q->flags & SM_DING;
704 q->flags &= ~SM_DING;
705 if(q->flags & SM_MODAL){
706 copy_of_q = copy_status_queue(q);
707 mark_modals_done();
708 q->saw_it = 1;
709 status_message_unlock();
710 output_message_modal(copy_of_q, ding);
711 goto start_over2;
713 else{
714 output_message(q, ding);
717 else{
718 q->saw_it = 1;
719 if(!(q->flags & SM_ASYNC))
720 q->pending_removal = 1;
722 q = (q->next != message_queue) ? q->next : NULL;
725 }while(q && !q->shown);
727 }while(q);
730 status_message_unlock();
734 void
735 set_saw_it_to_zero()
737 SMQ_T *q;
739 /* set saw_it to zero */
740 if((q = message_queue) != NULL){
742 q->saw_it = 0;
743 q = (q->next != message_queue) ? q->next : NULL;
744 }while(q);
749 void
750 mark_modals_done()
752 SMQ_T *q;
754 /* set shown to one */
755 if((q = message_queue) != NULL){
757 if(q->flags & SM_MODAL){
758 q->shown = 1;
759 q->pending_removal = 1;
762 q = (q->next != message_queue) ? q->next : NULL;
763 }while(q);
769 * Caller needs to free the memory.
771 SMQ_T *
772 copy_status_queue(SMQ_T *start)
774 SMQ_T *q, *new, *head = NULL;
776 if((q = start) != NULL){
778 new = (SMQ_T *) fs_get(sizeof(SMQ_T));
779 *new = *q;
781 if(q->text)
782 new->text = cpystr(q->text);
783 else
784 new->text = NULL;
786 if(head){
787 new->next = head;
788 new->prev = head->prev;
789 new->prev->next = head->prev = new;
791 else
792 head = new->next = new->prev = new;
794 q = (q->next != start) ? q->next : NULL;
795 }while(q);
798 return(head);
802 /*----------------------------------------------------------------------
803 Removes the first message from the message queue.
804 Returns 0 if all was well, -1 if had to skip the removal and
805 message_queue is unchanged.
806 ----*/
807 void
808 d_q_status_message(void)
810 int the_last_one = 0;
811 SMQ_T *q, *p, *last_one;
813 /* mark the top one for removal */
814 if((p = top_of_queue()) != NULL)
815 p->pending_removal = 1;
817 if(message_queue){
819 /* flush out pending removals */
820 the_last_one = 0; /* loop control */
821 q = message_queue;
822 last_one = message_queue->prev;
823 while(!the_last_one){
824 if(q == last_one)
825 the_last_one++;
827 if(q->pending_removal){
828 p = q;
829 if(q == q->next){ /* last item in queue */
830 q = message_queue = NULL;
832 else{
833 p->next->prev = p->prev;
834 q = p->prev->next = p->next; /* here's the increment */
835 if(message_queue == p)
836 message_queue = q;
839 if(p){
840 if(p->text)
841 fs_give((void **) &p->text);
843 fs_give((void **) &p);
846 else
847 q = q->next;
853 /*----------------------------------------------------------------------
854 Actually output the message to the screen
856 Args: message -- The message to output
857 from_alarm_handler -- Called from alarm signal handler.
858 We don't want to add this message to the review
859 message list since we may mess with the malloc
860 arena here, and the interrupt may be from
861 the middle of something malloc'ing.
862 ----*/
864 status_message_write(char *message, int from_alarm_handler)
866 int col, row, max_width, invert;
867 int bytes;
868 char newstatusbuf[6*MAX_SCREEN_COLS + 1];
869 struct variable *vars = ps_global->vars;
870 COLOR_PAIR *lastc = NULL, *newc;
872 if(!from_alarm_handler)
873 add_review_message(message, -1);
875 invert = !InverseState(); /* already in inverse? */
876 row = MAX(0, ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
878 /* Put [] around message and truncate to screen width */
879 max_width = ps_global->ttyo != NULL ? ps_global->ttyo->screen_cols : 80;
880 max_width = MIN(max_width, MAX_SCREEN_COLS);
881 newstatusbuf[0] = '[';
882 newstatusbuf[1] = '\0';
884 bytes = utf8_to_width(newstatusbuf+1, message, sizeof(newstatusbuf)-1, max_width-2, NULL);
885 newstatusbuf[1+bytes] = ']';
886 newstatusbuf[1+bytes+1] = '\0';
888 if(prevstartcol == -1 || strcmp(newstatusbuf, prevstatusbuf)){
889 UCS *prevbuf = NULL, *newbuf = NULL;
890 size_t plen;
892 if(prevstartcol != -1){
893 prevbuf = utf8_to_ucs4_cpystr(prevstatusbuf);
894 newbuf = utf8_to_ucs4_cpystr(newstatusbuf);
898 * Simple optimization. If the strings are the same length
899 * and width just skip leading and trailing strings of common
900 * characters and only write whatever's in between. Otherwise,
901 * write out the whole thing.
902 * We could do something more complicated but the odds of
903 * getting any optimization goes way down if they aren't the
904 * same length and width and the complexity goes way up.
906 if(prevbuf && newbuf
907 && (plen=ucs4_strlen(prevbuf)) == ucs4_strlen(newbuf)
908 && ucs4_str_width(prevbuf) == ucs4_str_width(newbuf)){
909 UCS *start_of_unequal, *end_of_unequal;
910 UCS *pprev, *endnewbuf;
911 char *to_screen = NULL;
912 int column;
914 pprev = prevbuf;
915 start_of_unequal = newbuf;
916 endnewbuf = newbuf + plen;
917 col = column = prevstartcol;
919 while(start_of_unequal < endnewbuf && (*start_of_unequal) == (*pprev)){
920 int w;
922 w = wcellwidth(*start_of_unequal);
923 if(w >= 0)
924 column += w;
926 pprev++;
927 start_of_unequal++;
930 end_of_unequal = endnewbuf-1;
931 pprev = prevbuf + plen - 1;
933 while(end_of_unequal > start_of_unequal && (*end_of_unequal) == (*pprev)){
934 *end_of_unequal = '\0';
935 pprev--;
936 end_of_unequal--;
939 if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
940 VAR_STATUS_BACK_COLOR &&
941 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
942 pico_is_good_color(VAR_STATUS_BACK_COLOR)){
943 lastc = pico_get_cur_color();
944 if(lastc){
945 newc = new_color_pair(VAR_STATUS_FORE_COLOR,
946 VAR_STATUS_BACK_COLOR);
947 (void)pico_set_colorp(newc, PSC_NONE);
948 free_color_pair(&newc);
951 else if(invert)
952 StartInverse();
954 /* PutLine wants UTF-8 string */
955 if(start_of_unequal && (*start_of_unequal))
956 to_screen = ucs4_to_utf8_cpystr(start_of_unequal);
958 if(to_screen){
959 PutLine0(row, column, to_screen);
960 fs_give((void **) &to_screen);
962 strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
963 prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
966 if(lastc){
967 (void)pico_set_colorp(lastc, PSC_NONE);
968 free_color_pair(&lastc);
970 else if(invert)
971 EndInverse();
973 else{
974 if(pico_usingcolor())
975 lastc = pico_get_cur_color();
977 if(!invert && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
978 VAR_STATUS_BACK_COLOR &&
979 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
980 pico_is_good_color(VAR_STATUS_BACK_COLOR))
981 pico_set_nbg_color(); /* so ClearLine uses bg color */
983 ClearLine(row);
985 if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
986 VAR_STATUS_BACK_COLOR &&
987 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
988 pico_is_good_color(VAR_STATUS_BACK_COLOR)){
989 if(lastc){
990 newc = new_color_pair(VAR_STATUS_FORE_COLOR,
991 VAR_STATUS_BACK_COLOR);
992 (void)pico_set_colorp(newc, PSC_NONE);
993 free_color_pair(&newc);
996 else if(invert){
997 if(lastc)
998 free_color_pair(&lastc);
1000 StartInverse();
1003 col = Centerline(row, newstatusbuf);
1005 if(lastc){
1006 (void)pico_set_colorp(lastc, PSC_NONE);
1007 free_color_pair(&lastc);
1009 else if(invert)
1010 EndInverse();
1012 strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
1013 prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
1014 prevstartcol = col;
1015 prevendcol = col + utf8_width(prevstatusbuf) - 1;
1018 /* move cursor to a consistent position */
1019 if(F_ON(F_SHOW_CURSOR, ps_global))
1020 MoveCursor(row, MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
1021 else
1022 MoveCursor(row, 0);
1024 fflush(stdout);
1026 if(prevbuf)
1027 fs_give((void **) &prevbuf);
1029 if(newbuf)
1030 fs_give((void **) &newbuf);
1032 else
1033 col = prevstartcol;
1035 return(col);
1039 /*----------------------------------------------------------------------
1040 Write the given status message to the display.
1042 Args: mq_entry -- pointer to message queue entry to write.
1044 ----*/
1045 int
1046 output_message(SMQ_T *mq_entry, int ding)
1048 int col = 0;
1050 dprint((9, "output_message(%s)\n",
1051 (mq_entry && mq_entry->text) ? mq_entry->text : "?"));
1053 if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
1054 Writechar(BELL, 0); /* ring bell */
1055 fflush(stdout);
1058 if(!(mq_entry->flags & SM_MODAL)){
1059 col = status_message_write(mq_entry->text, 0);
1060 if(ps_global->status_msg_delay > 0){
1061 if(F_ON(F_SHOW_CURSOR, ps_global))
1062 /* col+1 because col is "[" character */
1063 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
1064 MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
1065 else
1066 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);
1068 fflush(stdout);
1069 sleep(ps_global->status_msg_delay);
1072 mq_entry->shown = 1;
1075 return(col);
1080 * This is split off from output_message due to the locking
1081 * on the status message queue data. This calls scrolltool
1082 * which can call back into q_status and so on. So instead of
1083 * keeping the queue locked for a long time we copy the
1084 * data, unlock the queue, and display the data.
1086 int
1087 output_message_modal(SMQ_T *mq_entry, int ding)
1089 int rv = 0;
1090 SMQ_T *m, *mnext;
1092 if(!mq_entry)
1093 return(rv);
1095 dprint((9, "output_message_modal(%s)\n",
1096 (mq_entry && mq_entry->text) ? mq_entry->text : "?"));
1098 if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
1099 Writechar(BELL, 0); /* ring bell */
1100 fflush(stdout);
1103 if(!mq_entry->shown){
1104 int i = 0,
1105 pad = MAX(0, (ps_global->ttyo->screen_cols - 59) / 2);
1106 char *p, *q, *s, *t;
1107 SCROLL_S sargs;
1109 /* Count the number of modal messsages and add up their lengths. */
1110 for(m = mq_entry->next; m != mq_entry; m = m->next)
1111 if((m->flags & SM_MODAL) && !m->shown){
1112 i++;
1115 snprintf(tmp_20k_buf, SIZEOF_20KBUF, "%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n\n",
1116 /* 1 2 3 4 5 6*/
1117 /*23456789012345678901234567890123456789012345678901234567890*/
1118 pad, "",
1119 "***********************************************************",
1120 pad, "", i ?
1121 "* What follows are advisory messages. After reading them *" :
1122 "* What follows is an advisory message. After reading it *",
1124 pad, "",
1125 "* simply hit \"Return\" to continue your Alpine session. *",
1126 pad, "",
1127 "* *",
1128 pad, "", i ?
1129 "* To review these messages later, press 'J' from the *" :
1130 "* To review this message later, press 'J' from the *",
1131 pad, "",
1132 "* MAIN MENU. *",
1133 pad, "",
1134 "***********************************************************");
1135 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1136 t = tmp_20k_buf + strlen(tmp_20k_buf);
1138 m = mq_entry;
1140 if((m->flags & SM_MODAL) && !m->shown){
1141 int indent;
1143 indent = ps_global->ttyo->screen_cols > 80
1144 ? (ps_global->ttyo->screen_cols - 80) / 3 : 0;
1146 if(t - tmp_20k_buf > 19000){
1147 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * * Running out of buffer space * * *", indent, "");
1148 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1149 t += strlen(t);
1150 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * * Press RETURN for more messages * * *", indent, "");
1151 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1152 break;
1155 add_review_message(m->text, -1);
1157 if((p = strstr(m->text, "[ALERT]")) != NULL){
1158 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "%*.*s\n", (int)(indent + p - m->text), (int) (p - m->text), m->text);
1159 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1160 t += strlen(t);
1162 for(p += 7; *p && isspace((unsigned char)*p); p++)
1164 indent += 8;
1166 else{
1167 p = m->text;
1170 while(strlen(p) > ps_global->ttyo->screen_cols - 2 * indent){
1171 for(q = p + ps_global->ttyo->screen_cols - 2 * indent;
1172 q > p && !isspace((unsigned char)*q); q--)
1175 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*.*s", (int) (indent + q - p), (int) (q - p), p);
1176 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1177 t += strlen(t);
1178 p = q + 1;
1181 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s%s", indent, "", p);
1182 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1183 t += strlen(t);
1185 if(i--){
1186 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n\n%*s\n", pad + 30, "- - -");
1187 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1188 t += strlen(t);
1191 m = m->next;
1192 } while(m != mq_entry);
1194 s = cpystr(tmp_20k_buf);
1195 ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
1197 memset(&sargs, 0, sizeof(SCROLL_S));
1198 sargs.text.text = s;
1199 sargs.text.src = CharStar;
1200 sargs.bar.title = "Status Message";
1201 sargs.bogus_input = modal_bogus_input;
1202 sargs.no_stat_msg = 1;
1203 sargs.keys.menu = &modal_message_keymenu;
1204 setbitmap(sargs.keys.bitmap);
1206 scrolltool(&sargs);
1208 fs_give((void **)&s);
1209 ps_global->mangled_screen = 1;
1212 /* free the passed in queue */
1213 m = mq_entry;
1215 if(m->text)
1216 fs_give((void **) &m->text);
1218 mnext = m->next;
1219 fs_give((void **) &m);
1220 m = mnext;
1221 } while(m != mq_entry);
1223 return(rv);
1228 /*----------------------------------------------------------------------
1229 Write or clear delay cue
1231 Args: on -- whether to turn it on or not
1233 ----*/
1234 void
1235 delay_cmd_cue(int on)
1237 COLOR_PAIR *lastc;
1238 struct variable *vars = ps_global->vars;
1240 if(prevstartcol >= 0 && prevendcol < ps_global->ttyo->screen_cols){
1241 MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
1242 MAX(prevstartcol - 1, 0));
1243 lastc = pico_set_colors(VAR_STATUS_FORE_COLOR, VAR_STATUS_BACK_COLOR,
1244 PSC_REV|PSC_RET);
1245 Write_to_screen(on ? (prevstartcol ? "[>" : ">")
1246 : (prevstartcol ? " [" : "["));
1248 MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
1249 prevendcol);
1251 Write_to_screen(on ? (prevendcol < ps_global->ttyo->screen_cols-1 ? "<]" : "<")
1252 : (prevendcol < ps_global->ttyo->screen_cols-1 ? "] " : "]"));
1254 if(lastc){
1255 (void)pico_set_colorp(lastc, PSC_NONE);
1256 free_color_pair(&lastc);
1259 if(F_ON(F_SHOW_CURSOR, ps_global))
1260 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
1261 MIN(MAX(0,(prevstartcol + on ? 2 : 1)),ps_global->ttyo->screen_cols-1));
1262 else
1263 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);
1266 fflush(stdout);
1267 #ifdef _WINDOWS
1268 mswin_setcursor ((on) ? MSWIN_CURSOR_BUSY : MSWIN_CURSOR_ARROW);
1269 #endif
1274 * modal_bogus_input - used by scrolltool to complain about
1275 * invalid user input.
1278 modal_bogus_input(UCS ch)
1280 char s[MAX_SCREEN_COLS+1];
1282 snprintf(s, sizeof(s), _("Command \"%s\" not allowed. Press RETURN to continue Alpine."),
1283 pretty_command(ch));
1284 s[sizeof(s)-1] = '\0';
1285 status_message_write(s, 0);
1286 Writechar(BELL, 0);
1287 return(0);
1290 void
1291 free_message_queue(void)
1293 SMQ_T *m, *mnext;
1295 if(message_queue == NULL)
1296 return;
1298 m = message_queue;
1300 if(m->text)
1301 fs_give((void **) &m->text);
1303 mnext = m->next;
1304 fs_give((void **) &m);
1305 m = mnext;
1306 } while(m != message_queue);
1307 message_queue = NULL;