summaryrefslogtreecommitdiff
path: root/alpine/status.c
blob: c066f7bb6985246b6e857e1e7b0214ee665ec6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: status.c 840 2007-12-01 01:34:49Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2020 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

/*======================================================================
     status.c
     Functions that manage the status line (third from the bottom)
       - put messages on the queue to be displayed
       - display messages on the queue with timers 
       - check queue to figure out next timeout
       - prompt for yes/no type of questions
  ====*/

#include "headers.h"
#include "status.h"
#include "keymenu.h"
#include "mailview.h"
#include "mailcmd.h"
#include "busy.h"
#include "after.h"

#include "../pith/charconv/utf8.h"

#include "../pith/conf.h"
#include "../pith/bitmap.h"


/*
 * Internal queue of messages.  The circular, double-linked list's 
 * allocated on demand, and cleared as each message is displayed.
 */
typedef struct message {
    char	   *text;
    unsigned	    flags:8;
    unsigned	    shown:1;
    unsigned	    saw_it:1;
    unsigned	    pending_removal:1;
    int		    min_display_time, max_display_time;
    struct message *next, *prev;
} SMQ_T;


/*
 * Internal prototypes
 */
void      pause_for_and_dq_cur_msg(void);
SMQ_T    *top_of_queue(void);
int       new_info_msg_need_not_be_queued(void);
int       is_last_message(SMQ_T *);
void      d_q_status_message(void);
int       output_message(SMQ_T *, int);
int       output_message_modal(SMQ_T *, int);
void      delay_cmd_cue(int);
int       modal_bogus_input(UCS);
int	  messages_in_queue(void);
int       status_message_remaining_nolock(void);
void      set_saw_it_to_zero();
void      mark_modals_done();
SMQ_T    *copy_status_queue(SMQ_T *);



/*----------------------------------------------------------------------
     Manage the second line from the bottom where status and error messages
are displayed. A small queue is set up and messages are put on the queue
by calling one of the q_status_message routines. Even though this is a queue
most of the time message will go right on through. The messages are 
displayed just before the read for the next command, or when a read times
out. Read timeouts occur every minute or so for new mail checking and every
few seconds when there are still messages on the queue. Hopefully, this scheme 
will not let messages fly past that the user can't see.
  ----------------------------------------------------------------------*/


static SMQ_T *message_queue = NULL;
static short  needs_clearing = 0, /* Flag set by want_to()
                                              and optionally_enter() */
	      prevstartcol, prevendcol;
static char   prevstatusbuf[6*MAX_SCREEN_COLS+1];
static time_t displayed_time;


/*----------------------------------------------------------------------
        Put a message for the status line on the queue

  Args: time    -- the min time in seconds to display the message
        message -- message string

  Result: queues message on queue represented by static variables

    This puts a single message on the queue to be shown.
  ----------*/
void
q_status_message(int flags, int min_time, int max_time, char *message)
{
    SMQ_T *new, *q;
    char  *clean_msg;
    size_t mlen;

    status_message_lock();

    /*
     * If there is already a message queued and
     * new message is just informational, discard it.
     */
    if(flags & SM_INFO && new_info_msg_need_not_be_queued()){
	status_message_unlock();
	return;
    }

    /*
     * By convention, we have min_time equal to zero in messages which we
     * think are not as important, so-called comfort messages. We have
     * min_time >= 3 for messages which we think the user should see for
     * sure. Some users don't like to wait so we've provided a way for them
     * to live on the edge.
     *    status_msg_delay == -1  => min time == MIN(0, min_time)
     *    status_msg_delay == -2  => min time == MIN(1, min_time)
     *    status_msg_delay == -3  => min time == MIN(2, min_time)
     *    ...
     */
    if(ps_global->status_msg_delay < 0)
      min_time = MIN(-1 - ps_global->status_msg_delay, min_time);

    /* The 40 is room for 40 escaped control characters */
    mlen = strlen(message) + 40;
    clean_msg = (char *)fs_get(mlen + 1);
    iutf8ncpy(clean_msg, message, mlen);		/* does the cleaning */

    clean_msg[mlen] = '\0';

    if((q = message_queue) != NULL){	/* the queue exists */

	/*
	 * Scan through all of the messages currently in the queue.
	 * If the new message is already queued, don't add it again.
	 */
	do {
	    if(!q->pending_removal && q->text && !strcmp(q->text, clean_msg)){
		q->shown = 0;
		if(q->min_display_time < min_time)
		  q->min_display_time = min_time;

		if(q->max_display_time < max_time)
		  q->max_display_time = max_time;

		dprint((9, "q_status_message(%s): skipping duplicate msg\n",
		       clean_msg ? clean_msg : "?"));

		if(clean_msg)
		  fs_give((void **)&clean_msg);

		status_message_unlock();
		return;
	    }

	    q = q->next;

	} while(q != message_queue);
    }

    new = (SMQ_T *)fs_get(sizeof(SMQ_T));
    memset(new, 0, sizeof(SMQ_T));
    new->text = clean_msg;
    new->min_display_time = min_time;
    new->max_display_time = max_time;
    new->flags            = flags;
    if(message_queue){
	new->next = message_queue;
	new->prev = message_queue->prev;
	new->prev->next = message_queue->prev = new;
    }
    else
      message_queue = new->next = new->prev = new;

    status_message_unlock();

    dprint((9, "q_status_message(%s)\n",
	   clean_msg ? clean_msg : "?"));
}


/*----------------------------------------------------------------------
     Mark the status line as dirty so it gets cleared next chance
 ----*/
void
mark_status_dirty(void)
{
    mark_status_unknown();
    needs_clearing++;
}


/*----------------------------------------------------------------------
    Cause status line drawing optimization to be turned off, because we
    don't know what the status line looks like.
 ----*/
void
mark_status_unknown(void)
{
    prevstartcol = -1;
    prevendcol = -1;
    prevstatusbuf[0]  = '\0';
}


/*----------------------------------------------------------------------
     Wait a suitable amount of time for the currently displayed message
 ----*/
void
pause_for_and_dq_cur_msg(void)
{
    if(top_of_queue()){
	int w;

	if((w = status_message_remaining_nolock()) != 0){
	    delay_cmd_cue(1);
	    /* protect user from changes in the clock. If the clock
	     * changes during this process (for example, going from 
	     * Standard time to Delayed time) this may result in big 
	     * values for w. In those cases, reset w.
	     */
	    w = (w > 0 ? (w > 5 ? 5 : w) : 0);
	    if(ps_global && !ps_global->initial_cmds) sleep(w);
	    delay_cmd_cue(0);
	}

	d_q_status_message();
    }
}


/*
 * This relies on the global displayed_time being properly set
 * for this message.
 */
void
pause_for_and_mark_specific_msg(SMQ_T *msg)
{
    if(msg){
	int w;

	w = (int) (displayed_time - time(0)) + msg->min_display_time;
	w = (w > 0) ? (w > 5 ? 5 : w): 0;
	if(w){
	    delay_cmd_cue(1);
	    if (ps_global && !ps_global->initial_cmds) sleep(w);
	    delay_cmd_cue(0);
	}

	msg->pending_removal = 1;
    }
}


/*----------------------------------------------------------------------
    Time remaining for current message's minimum display
 ----*/
int
status_message_remaining(void)
{
    int ret;

    status_message_lock();
    ret = status_message_remaining_nolock();
    status_message_unlock();

    return(ret);
}


int
status_message_remaining_nolock(void)
{
    SMQ_T *q;
    int d = 0;

    if((q = top_of_queue()) != NULL)
      d = (int) (displayed_time - time(0)) + q->min_display_time;

    return((d > 0) ? d : 0);
}


/*
 * Return first message in queue that isn't pending_removal.
 */
SMQ_T *
top_of_queue(void)
{
    SMQ_T *p;

    if((p = message_queue) != NULL){
	do
	  if(!p->pending_removal)
	    return(p);
	while((p = p->next) != message_queue);
    }

    return(NULL);
}


int
new_info_msg_need_not_be_queued(void)
{
    SMQ_T *q;

    if(status_message_remaining_nolock() > 0)
      return 1;

    if((q = top_of_queue()) != NULL && (q = q->next) != message_queue){
	do
	  if(!q->pending_removal && !(q->flags & SM_INFO))
	    return 1;
	while((q = q->next) != message_queue);
    }

    return 0;
}


/*----------------------------------------------------------------------
        Find out how many messages are queued for display

  Args:   dtime -- will get set to minimum display time for current message

  Result: number of messages in the queue.

  ---------*/
int
messages_queued(long int *dtime)
{
    SMQ_T *q;
    int ret;

    status_message_lock();
    if(dtime && (q = top_of_queue()) != NULL)
      *dtime = (long) MAX(q->min_display_time, 1L);

    ret = ps_global->in_init_seq ? 0 : messages_in_queue();

    status_message_unlock();

    return(ret);
}


/*----------------------------------------------------------------------
       Return number of messages in queue
  ---------*/
int
messages_in_queue(void)
{
    int	   n = 0;
    SMQ_T *p;

    if((p = message_queue) != NULL){
	do
	  if(!p->pending_removal)
	    n++;
	while((p = p->next) != message_queue);
    }

    return(n);
}


/*----------------------------------------------------------------------
     Return last message queued
  ---------*/
char *
last_message_queued(void)
{
    SMQ_T *p, *r = NULL;
    char  *ret = NULL;

    status_message_lock();
    if((p = message_queue) != NULL){
	do
	  if(p->flags & SM_ORDER && !p->pending_removal)
	    r = p;
	while((p = p->next) != message_queue);
    }

    ret = (r && r->text) ? cpystr(r->text) : NULL;

    status_message_unlock();

    return(ret);
}


int
is_last_message(SMQ_T *msg)
{
    SMQ_T *p, *r = NULL;

    if(msg && !msg->pending_removal){
	if((p = message_queue) != NULL){
	    do
	      if(!p->pending_removal)
		r = p;
	    while((p = p->next) != message_queue);
	}
    }

    return(r && msg && (r == msg));
}


/*----------------------------------------------------------------------
       Update status line, clearing or displaying a message

   Arg: command -- The command that is about to be executed

  Result: status line cleared or
             next message queued is displayed or
             current message is redisplayed.
	     if next message displayed, it's min display time
	     is returned else if message already displayed, it's
	     time remaining on the display is returned, else 0.

   This is called when ready to display the next message, usually just
before reading the next command from the user. We pass in the nature
of the command because it affects what we do here. If the command just
executed by the user is a redraw screen, we don't want to reset or go to 
next message because it might not have been seen.  Also if the command
is just a noop, which are usually executed when checking for new mail 
and happen every few minutes, we don't clear the message.

If it was really a command and there's nothing more to show, then we
clear, because we know the user has seen the message. In this case the
user might be typing commands very quickly and miss a message, so
there is a time stamp and time check that each message has been on the
screen for a few seconds.  If it hasn't we just return and let it be
taken care of next time.
----------------------------------------------------------------------*/
int
display_message(UCS command)
{
    SMQ_T *q, *copy_of_q;
    int need_to_unlock;
    int ding;

    if(ps_global == NULL || ps_global->ttyo == NULL
       || ps_global->ttyo->screen_rows < 1 || ps_global->in_init_seq)
      return(0);

    status_message_lock();

    /*---- Deal with any previously displayed message ----*/
    if((q = top_of_queue()) != NULL && q->shown){
	int rv = -1;

	if(command == ctrl('L')){	/* just repaint it, and go on */
	    mark_status_unknown();
	    mark_keymenu_dirty();
	    mark_titlebar_dirty();
	    rv = 0;
	}
	else{				/* ensure sufficient time's passed */
	    time_t now;
	    int    diff;

	    now  = time(0);
	    diff = (int)(displayed_time - now)
			+ ((command == NO_OP_COMMAND || command == NO_OP_IDLE)
			    ? q->max_display_time
			    : q->min_display_time);
            dprint((9,
		       "STATUS: diff:%d, displayed: %ld, now: %ld\n",
		       diff, (long) displayed_time, (long) now));
            if(diff > 0)
	      rv = diff;			/* check again next time  */
	    else if(is_last_message(q)
		    && (command == NO_OP_COMMAND || command == NO_OP_IDLE)
		    && q->max_display_time)
	      rv = 0;				/* last msg, no cmd, has max */
	}

	if(rv >= 0){				/* leave message displayed? */
	    if(prevstartcol < 0){		/* need to redisplay it? */
		ding = q->flags & SM_DING;
		q->flags &= ~SM_DING;
		if(q->flags & SM_MODAL && !q->shown){
		    copy_of_q = copy_status_queue(q);
		    mark_modals_done();
		    status_message_unlock();
		    output_message_modal(copy_of_q, ding);
		}
		else{
		    output_message(q, ding);
		    status_message_unlock();
		}
	    }
	    else
	      status_message_unlock();

	    return(rv);
	}
	  
	d_q_status_message();		/* remove it from queue and */
	needs_clearing++;		/* clear the line if needed */
    }

    if(!top_of_queue() && (command == ctrl('L') || needs_clearing)){
	int inverse;
	struct variable *vars = ps_global->vars;
	char *last_bg = NULL;

	dprint((9, "Clearing status line\n"));
	inverse = InverseState();	/* already in inverse? */
	if(inverse && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
	   VAR_STATUS_BACK_COLOR){
	    last_bg = pico_get_last_bg_color();
	    pico_set_nbg_color();   /* so ClearLine will clear in bg color */
	}

	ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
	if(last_bg){
	    (void)pico_set_bg_color(last_bg);
	    if(last_bg)
	      fs_give((void **)&last_bg);
	}

	mark_status_unknown();
	if(command == ctrl('L')){
	    mark_keymenu_dirty();
	    mark_titlebar_dirty();
	}
    }

    need_to_unlock = 1;

    /* display next message, weeding out 0 display times */
    for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
	if(q->min_display_time || is_last_message(q)){
	    displayed_time = time(0);
	    ding = q->flags & SM_DING;
	    q->flags &= ~SM_DING;
	    if(q->flags & SM_MODAL && !q->shown){
		copy_of_q = copy_status_queue(q);
		mark_modals_done();
		status_message_unlock();
		output_message_modal(copy_of_q, ding);
		need_to_unlock = 0;
	    }
	    else
	      output_message(q, ding);

	    break;
	}
	/* zero display time message, log it, delete it */
	else{
	    if(q->text){
		char   buf[1000];
		char  *append = " [not actually shown]";
		char  *ptr;
		size_t len;

		len = strlen(q->text) + strlen(append);
		if(len < sizeof(buf))
		  ptr = buf;
		else
		  ptr = (char *) fs_get((len+1) * sizeof(char));

		strncpy(ptr, q->text, len);
		ptr[len] = '\0';
		strncat(ptr, append, len+1-1-strlen(ptr));
		ptr[len] = '\0';
		add_review_message(ptr, -1);
		if(ptr != buf)
		  fs_give((void **) &ptr);
	    }

	    d_q_status_message();
	}
    }

    needs_clearing = 0;				/* always cleared or written */
    fflush(stdout);

    if(need_to_unlock)
      status_message_unlock();

    return(0);
}


/*----------------------------------------------------------------------
     Display all the messages on the queue as quickly as possible
  ----*/
void
flush_status_messages(int skip_last_pause)
{
    SMQ_T *q, *copy_of_q;
    int ding;

start_over:
    status_message_lock();

    for(q = top_of_queue(); q; q = top_of_queue()){
	/* don't have to wait for this one */
	if(is_last_message(q) && skip_last_pause && q->shown)
	  break;

	if(q->shown)
	  pause_for_and_dq_cur_msg();

	/* find next one we need to show and show it */
	for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
	    if((q->min_display_time || is_last_message(q))){
		displayed_time = time(0);
		ding = q->flags & SM_DING;
		q->flags &= ~SM_DING;
		if(q->flags & SM_MODAL){
		    copy_of_q = copy_status_queue(q);
		    mark_modals_done();
		    status_message_unlock();
		    output_message_modal(copy_of_q, ding);

		    /*
		     * Because we unlock the message queue in order
		     * to display modal messages we have to worry
		     * about the queue being changed while we have
		     * it unlocked. So start the whole process over.
		     */
		    goto start_over;
		}
		else
		  output_message(q, ding);
	    }
	    else{
		d_q_status_message();
	    }
	}
    }

    status_message_unlock();
}


/*----------------------------------------------------------------------
     Make sure any and all SM_ORDER messages get displayed.

     Note: This flags the message line as having nothing displayed.
           The idea is that it's a function called by routines that want
	   the message line for a prompt or something, and that they're
	   going to obliterate the message anyway.
 ----*/
void
flush_ordered_messages(void)
{
    SMQ_T *q, *copy_of_q;
    int firsttime = 1;
    int ding;

    status_message_lock();

    set_saw_it_to_zero();

start_over2:
    if(!firsttime)
      status_message_lock();
    else
      firsttime = 0;

    if((q = message_queue) != NULL){
	do{
	    if(q->pending_removal || q->saw_it || q->shown){
		if(!q->pending_removal && q->shown)
		  pause_for_and_mark_specific_msg(q);

		q->saw_it = 1;
		q = (q->next != message_queue) ? q->next : NULL;
	    }
	    else{
		/* find next one we need to show and show it */
		do{
		    if(q->pending_removal || q->saw_it || q->shown){
			q->saw_it = 1;
			q = (q->next != message_queue) ? q->next : NULL;
		    }
		    else{
			if((q->flags & (SM_ORDER | SM_MODAL))
			   && q->min_display_time){
			    displayed_time = time(0);
			    ding = q->flags & SM_DING;
			    q->flags &= ~SM_DING;
			    if(q->flags & SM_MODAL){
				copy_of_q = copy_status_queue(q);
				mark_modals_done();
				q->saw_it = 1;
				status_message_unlock();
				output_message_modal(copy_of_q, ding);
				goto start_over2;
			    }
			    else{
				output_message(q, ding);
			    }
			}
			else{
			    q->saw_it = 1;
			    if(!(q->flags & SM_ASYNC))
			      q->pending_removal = 1;

			    q = (q->next != message_queue) ? q->next : NULL;
			}
		    }
		}while(q && !q->shown);
	    }
	}while(q);
    }

    status_message_unlock();
}


void
set_saw_it_to_zero()
{
    SMQ_T *q;

    /* set saw_it to zero */
    if((q = message_queue) != NULL){
	do{
	    q->saw_it = 0;
	    q = (q->next != message_queue) ? q->next : NULL;
	}while(q);
    }
}


void
mark_modals_done()
{
    SMQ_T *q;

    /* set shown to one */
    if((q = message_queue) != NULL){
	do{
	    if(q->flags & SM_MODAL){
		q->shown = 1;
		q->pending_removal = 1;
	    }

	    q = (q->next != message_queue) ? q->next : NULL;
	}while(q);
    }
}


/*
 * Caller needs to free the memory.
 */
SMQ_T *
copy_status_queue(SMQ_T *start)
{
    SMQ_T *q, *new, *head = NULL;

    if((q = start) != NULL){
	do{
	    new = (SMQ_T *) fs_get(sizeof(SMQ_T));
	    *new = *q;

	    if(q->text)
	      new->text = cpystr(q->text);
	    else
	      new->text = NULL;

	    if(head){
		new->next = head;
		new->prev = head->prev;
		new->prev->next = head->prev = new;
	    }
	    else
	      head = new->next = new->prev = new;

	    q = (q->next != start) ? q->next : NULL;
	}while(q);
    }

    return(head);
}


/*----------------------------------------------------------------------
      Removes the first message from the message queue.
      Returns 0 if all was well, -1 if had to skip the removal and
      message_queue is unchanged.
  ----*/
void
d_q_status_message(void)
{
    int the_last_one = 0;
    SMQ_T *q, *p, *last_one;

    /* mark the top one for removal */
    if((p = top_of_queue()) != NULL)
      p->pending_removal = 1;

    if(message_queue){

	/* flush out pending removals */
	the_last_one = 0;	/* loop control */
	q = message_queue;
	last_one = message_queue->prev;
	while(!the_last_one){
	    if(q == last_one)
	      the_last_one++;

	    if(q->pending_removal){
		p = q;
		if(q == q->next){	/* last item in queue */
		    q = message_queue = NULL;
		}
		else{
		    p->next->prev = p->prev;
		    q = p->prev->next = p->next;	/* here's the increment */
		    if(message_queue == p)
		      message_queue = q;
		}

		if(p){
		    if(p->text)
		      fs_give((void **) &p->text);

		    fs_give((void **) &p);
		}
	    }
	    else
	      q = q->next;
	}
    }
}


/*----------------------------------------------------------------------
    Actually output the message to the screen

  Args: message            -- The message to output
	from_alarm_handler -- Called from alarm signal handler.
			      We don't want to add this message to the review
			      message list since we may mess with the malloc
			      arena here, and the interrupt may be from
			      the middle of something malloc'ing.
 ----*/
int
status_message_write(char *message, int from_alarm_handler)
{
    int  col, row, max_width, invert;
    int bytes;
    char newstatusbuf[6*MAX_SCREEN_COLS + 1];
    struct variable *vars = ps_global->vars;
    COLOR_PAIR *lastc = NULL, *newc;

    if(!from_alarm_handler)
      add_review_message(message, -1);

    invert = !InverseState();	/* already in inverse? */
    row = MAX(0, ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));

    /* Put [] around message and truncate to screen width */
    max_width = ps_global->ttyo != NULL ? ps_global->ttyo->screen_cols : 80;
    max_width = MIN(max_width, MAX_SCREEN_COLS);
    newstatusbuf[0] = '[';
    newstatusbuf[1] = '\0';

    bytes = utf8_to_width(newstatusbuf+1, message, sizeof(newstatusbuf)-1, max_width-2, NULL);
    newstatusbuf[1+bytes] = ']';
    newstatusbuf[1+bytes+1] = '\0';

    if(prevstartcol == -1 || strcmp(newstatusbuf, prevstatusbuf)){
	UCS *prevbuf = NULL, *newbuf = NULL;
	size_t plen;

	if(prevstartcol != -1){
	    prevbuf = utf8_to_ucs4_cpystr(prevstatusbuf);
	    newbuf  = utf8_to_ucs4_cpystr(newstatusbuf);
	}

	/*
	 * Simple optimization.  If the strings are the same length
	 * and width just skip leading and trailing strings of common
	 * characters and only write whatever's in between. Otherwise,
	 * write out the whole thing.
	 * We could do something more complicated but the odds of
	 * getting any optimization goes way down if they aren't the
	 * same length and width and the complexity goes way up.
	 */
	if(prevbuf && newbuf
	   && (plen=ucs4_strlen(prevbuf)) == ucs4_strlen(newbuf)
	   && ucs4_str_width(prevbuf) == ucs4_str_width(newbuf)){
	    UCS *start_of_unequal, *end_of_unequal;
	    UCS *pprev, *endnewbuf;
	    char *to_screen = NULL;
	    int column;

	    pprev = prevbuf;
	    start_of_unequal = newbuf;
	    endnewbuf = newbuf + plen;
	    col = column = prevstartcol;

	    while(start_of_unequal < endnewbuf && (*start_of_unequal) == (*pprev)){
		int w;

		w = wcellwidth(*start_of_unequal);
		if(w >= 0)
		  column += w;

		pprev++;
		start_of_unequal++;
	    }

	    end_of_unequal = endnewbuf-1;
	    pprev = prevbuf + plen - 1;

	    while(end_of_unequal > start_of_unequal && (*end_of_unequal) == (*pprev)){
		*end_of_unequal = '\0';
		pprev--;
		end_of_unequal--;
	    }

	    if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
	       VAR_STATUS_BACK_COLOR &&
	       pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
	       pico_is_good_color(VAR_STATUS_BACK_COLOR)){
		lastc = pico_get_cur_color();
		if(lastc){
		    newc = new_color_pair(VAR_STATUS_FORE_COLOR,
					  VAR_STATUS_BACK_COLOR);
		    (void)pico_set_colorp(newc, PSC_NONE);
		    free_color_pair(&newc);
		}
	    }
	    else if(invert)
	      StartInverse();

	    /* PutLine wants UTF-8 string */
	    if(start_of_unequal && (*start_of_unequal))
	      to_screen = ucs4_to_utf8_cpystr(start_of_unequal);

	    if(to_screen){
		PutLine0(row, column, to_screen);
		fs_give((void **) &to_screen);

		strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
		prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
	    }

	    if(lastc){
		(void)pico_set_colorp(lastc, PSC_NONE);
		free_color_pair(&lastc);
	    }
	    else if(invert)
	      EndInverse();
	}
	else{
	    if(pico_usingcolor())
	      lastc = pico_get_cur_color();

	    if(!invert && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
	       VAR_STATUS_BACK_COLOR &&
	       pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
	       pico_is_good_color(VAR_STATUS_BACK_COLOR))
	      pico_set_nbg_color();	/* so ClearLine uses bg color */

	    ClearLine(row);

	    if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
	       VAR_STATUS_BACK_COLOR &&
	       pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
	       pico_is_good_color(VAR_STATUS_BACK_COLOR)){
		if(lastc){
		    newc = new_color_pair(VAR_STATUS_FORE_COLOR,
					  VAR_STATUS_BACK_COLOR);
		    (void)pico_set_colorp(newc, PSC_NONE);
		    free_color_pair(&newc);
		}
	    }
	    else if(invert){
		if(lastc)
		  free_color_pair(&lastc);

		StartInverse();
	    }

	    col = Centerline(row, newstatusbuf);

	    if(lastc){
		(void)pico_set_colorp(lastc, PSC_NONE);
		free_color_pair(&lastc);
	    }
	    else if(invert)
	      EndInverse();

	    strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
	    prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
	    prevstartcol = col;
	    prevendcol = col + utf8_width(prevstatusbuf) - 1;
	}

	/* move cursor to a consistent position */
	if(F_ON(F_SHOW_CURSOR, ps_global))
	  MoveCursor(row, MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
	else
	  MoveCursor(row, 0);

	fflush(stdout);

	if(prevbuf)
	  fs_give((void **) &prevbuf);

	if(newbuf)
	  fs_give((void **) &newbuf);
    }
    else
      col = prevstartcol;

    return(col);
}


/*----------------------------------------------------------------------
    Write the given status message to the display.

  Args: mq_entry -- pointer to message queue entry to write.
 
 ----*/
int 
output_message(SMQ_T *mq_entry, int ding)
{
    int col = 0;

    dprint((9, "output_message(%s)\n",
	   (mq_entry && mq_entry->text) ? mq_entry->text : "?"));

    if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
	Writechar(BELL, 0);			/* ring bell */
	fflush(stdout);
    }

    if(!(mq_entry->flags & SM_MODAL)){
	col = status_message_write(mq_entry->text, 0);
    	if(ps_global->status_msg_delay > 0){
	    if(F_ON(F_SHOW_CURSOR, ps_global))
	      /* col+1 because col is "[" character */
	      MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
			 MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
	    else
	      MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);

	    fflush(stdout);
	    if (ps_global && !ps_global->initial_cmds)
		sleep(ps_global->status_msg_delay);
	}

	mq_entry->shown = 1;
    }

    return(col);
}


/*
 * This is split off from output_message due to the locking
 * on the status message queue data. This calls scrolltool
 * which can call back into q_status and so on. So instead of
 * keeping the queue locked for a long time we copy the
 * data, unlock the queue, and display the data.
 */
int 
output_message_modal(SMQ_T *mq_entry, int ding)
{
    int rv = 0;
    SMQ_T *m, *mnext;

    if(!mq_entry)
      return(rv);

    dprint((9, "output_message_modal(%s)\n",
	   (mq_entry && mq_entry->text) ? mq_entry->text : "?"));

    if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
	Writechar(BELL, 0);			/* ring bell */
	fflush(stdout);
    }

    if(!mq_entry->shown){
	int	  i      = 0,
		  pad    = MAX(0, (ps_global->ttyo->screen_cols - 59) / 2);
	char	 *p, *q, *s, *t;
	SCROLL_S  sargs;
	
	/* Count the number of modal messages and add up their lengths. */
	for(m = mq_entry->next; m != mq_entry; m = m->next)
	  if((m->flags & SM_MODAL) && !m->shown){
	      i++;
	  }

	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", 
		/*        1         2         3         4         5         6*/
		/*23456789012345678901234567890123456789012345678901234567890*/
		pad, "",
		"***********************************************************",
		pad, "", i ? 
		"* What follows are advisory messages.  After reading them *" :
		"* What follows is an advisory message.  After reading it  *", 
		
		pad, "",
		"* simply hit \"Return\" to continue your Alpine session.    *", 
		pad, "",
		"*                                                         *",
		pad, "", i ?
		"* To review these messages later, press 'J' from the      *" :
		"* To review this message later, press 'J' from the        *",
		pad, "",
		"* MAIN MENU.                                              *",
		pad, "",
		"***********************************************************");
	tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
	t = tmp_20k_buf + strlen(tmp_20k_buf);	

	m = mq_entry;
	do{
	    if((m->flags & SM_MODAL) && !m->shown){
		int   indent;

		indent = ps_global->ttyo->screen_cols > 80 
		         ? (ps_global->ttyo->screen_cols - 80) / 3 : 0;
		
		if(t - tmp_20k_buf > 19000){
		    snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * *  Running out of buffer space   * * *", indent, "");
		    tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		    t += strlen(t);
		    snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * * Press RETURN for more messages * * *", indent, "");
		    tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		    break;
		}
		
		add_review_message(m->text, -1);
		
		if((p = strstr(m->text, "[ALERT]")) != NULL){
		    snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "%*.*s\n", (int)(indent + p - m->text), (int) (p - m->text), m->text);
		    tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		    t += strlen(t);

		    for(p += 7; *p && isspace((unsigned char)*p); p++)
		      ;
		    indent += 8;
		}
		else{
		    p = m->text;
		}
		
		while(strlen(p) > ps_global->ttyo->screen_cols - 2 * indent){
		    for(q = p + ps_global->ttyo->screen_cols - 2 * indent; 
			q > p && !isspace((unsigned char)*q); q--)
		      ;

		    snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*.*s", (int) (indent + q - p), (int) (q - p), p);
		    tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		    t += strlen(t);
		    p = q + 1;
		}

		snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s%s", indent, "", p);
		tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		t += strlen(t);
		
		if(i--){
		    snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n\n%*s\n", pad + 30, "-  -  -");
		    tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
		    t += strlen(t);
		}
	    }
	    m = m->next;
	} while(m != mq_entry);

	s = cpystr(tmp_20k_buf);
	ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));

	memset(&sargs, 0, sizeof(SCROLL_S));
	sargs.text.text	  = s;
	sargs.text.src	  = CharStar;
	sargs.bar.title	  = "Status Message";
	sargs.bogus_input = modal_bogus_input;
	sargs.no_stat_msg = 1;
	sargs.keys.menu   = &modal_message_keymenu;
	setbitmap(sargs.keys.bitmap);

	scrolltool(&sargs);

	fs_give((void **)&s);
	ps_global->mangled_screen = 1;
    }

    /* free the passed in queue */
    m = mq_entry;
    do{
	if(m->text)
	  fs_give((void **) &m->text);

	mnext = m->next;
	fs_give((void **) &m);
	m = mnext;
    } while(m != mq_entry);

    return(rv);
}



/*----------------------------------------------------------------------
    Write or clear delay cue

  Args: on -- whether to turn it on or not
 
 ----*/
void
delay_cmd_cue(int on)
{
    COLOR_PAIR *lastc;
    struct variable *vars = ps_global->vars;

    if(prevstartcol >= 0 && prevendcol < ps_global->ttyo->screen_cols){
	MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
		   MAX(prevstartcol - 1, 0));
	lastc = pico_set_colors(VAR_STATUS_FORE_COLOR, VAR_STATUS_BACK_COLOR,
				PSC_REV|PSC_RET);
	Write_to_screen(on ? (prevstartcol ? "[>" : ">")
			   : (prevstartcol ? " [" : "["));

	MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
		   prevendcol);

	Write_to_screen(on ? (prevendcol < ps_global->ttyo->screen_cols-1 ? "<]" : "<")
			   : (prevendcol < ps_global->ttyo->screen_cols-1 ? "] " : "]"));

	if(lastc){
	    (void)pico_set_colorp(lastc, PSC_NONE);
	    free_color_pair(&lastc);
	}

	if(F_ON(F_SHOW_CURSOR, ps_global))
	  MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
		     MIN(MAX(0,(prevstartcol + on ? 2 : 1)),ps_global->ttyo->screen_cols-1));
	else
	  MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);
    }

    fflush(stdout);
#ifdef	_WINDOWS
    mswin_setcursor ((on) ? MSWIN_CURSOR_BUSY : MSWIN_CURSOR_ARROW);
#endif
}


/*
 * modal_bogus_input - used by scrolltool to complain about
 *		       invalid user input.
 */
int
modal_bogus_input(UCS ch)
{
    char s[MAX_SCREEN_COLS+1];
		
    snprintf(s, sizeof(s), _("Command \"%s\" not allowed.  Press RETURN to continue Alpine."),
	     pretty_command(ch));
    s[sizeof(s)-1] = '\0';
    status_message_write(s, 0);
    Writechar(BELL, 0);
    return(0);
}

void
free_message_queue(void)
{
   SMQ_T *m, *mnext;

   if(message_queue == NULL)
     return;

    m = message_queue;
    do{
      if(m->text)
        fs_give((void **) &m->text);
     
      mnext = m->next;
      fs_give((void **) &m);
      m = mnext;
    } while(m != message_queue);
    message_queue = NULL;
}