-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cc
3359 lines (3009 loc) · 111 KB
/
display.cc
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
/* display.c -- readline redisplay facility. */
/* Copyright (C) 1987-2017 Free Software Foundation, Inc.
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
Readline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Readline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Readline. If not, see <http://www.gnu.org/licenses/>.
*/
#define READLINE_LIBRARY
#if defined(HAVE_CONFIG_H)
#include <config.hh>
#endif
#include <sys/types.h>
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "posixstat.hh"
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#else
#include "ansi_stdlib.hh"
#endif /* HAVE_STDLIB_H */
#include <stdio.h>
#ifdef __MSDOS__
#include <pc.h>
#endif
/* System-specific feature definitions and include files. */
#include "rldefs.hh"
#include "rlmbutil.hh"
/* Termcap library stuff. */
#include "tcap.hh"
/* Some standard library routines. */
#include "history.hh"
#include "readline.hh"
#include "rlprivate.hh"
#include "xmalloc.hh"
#if !defined(strchr) && !defined(__STDC__)
extern char *strchr(), *strrchr();
#endif /* !strchr && !__STDC__ */
static void update_line PARAMS((char*, char*, int, int, int, int));
static void space_to_eol PARAMS((int));
static void delete_chars PARAMS((int));
static void insert_some_chars PARAMS((char*, int, int));
static void open_some_spaces PARAMS((int));
static void cr PARAMS((void));
static void redraw_prompt PARAMS((char*));
/* Values for FLAGS */
#define PMT_MULTILINE 0x01
static char* expand_prompt PARAMS((char*, int, int*, int*, int*, int*));
/* State of visible and invisible lines. */
struct line_state
{
char* line;
int* lbreaks;
int lbsize;
#if defined(HANDLE_MULTIBYTE)
int wbsize;
int* wrapped_line;
#endif
};
/* The line display buffers. One is the line currently displayed on
the screen. The other is the line about to be displayed. */
static struct line_state line_state_array[2];
static struct line_state* line_state_visible = &line_state_array[0];
static struct line_state* line_state_invisible= &line_state_array[1];
static int line_structures_initialized= 0;
/* Backwards-compatible names. */
#define inv_lbreaks (line_state_invisible->lbreaks)
#define inv_lbsize (line_state_invisible->lbsize)
#define vis_lbreaks (line_state_visible->lbreaks)
#define vis_lbsize (line_state_visible->lbsize)
#define visible_line (line_state_visible->line)
#define invisible_line (line_state_invisible->line)
#if defined(HANDLE_MULTIBYTE)
static int _rl_col_width PARAMS((const char*, int, int, int));
#else
#define _rl_col_width(l, s, e, f) (((e) <= (s)) ? 0 : (e) - (s))
#endif
/* Heuristic used to decide whether it is faster to move from CUR to _new
by backing up or outputting a carriage return and moving forward. CUR
and _new are either both buffer positions or absolute screen positions.
*/
#define CR_FASTER(_new, cur) (((_new) + 1) < ((cur) - (_new)))
/* _rl_last_c_pos is an absolute cursor position in multibyte locales and a
buffer index in others. This macro is used when deciding whether the
current cursor position is in the middle of a prompt string containing
invisible characters. XXX - might need to take `modmark' into account.
*/
/* XXX - only valid when tested against _rl_last_c_pos; buffer indices need
to use prompt_last_invisible directly. */
#define PROMPT_ENDING_INDEX \
((MB_CUR_MAX > 1 && rl_byte_oriented == 0) ? prompt_physical_chars \
: prompt_last_invisible + 1)
/* **************************************************************** */
/* */
/* Display stuff */
/* */
/* **************************************************************** */
/* This is the stuff that is hard for me. I never seem to write good
display routines in C. Let's see how I do this time. */
/* (PWP) Well... Good for a simple line updater, but totally ignores
the problems of input lines longer than the screen width.
update_line and the code that calls it makes a multiple line,
automatically wrapping line update. Careful attention needs
to be paid to the vertical position variables. */
/* Keep two buffers; one which reflects the current contents of the
screen, and the other to draw what we think the _new contents should
be. Then compare the buffers, and make whatever changes to the
screen itself that we should. Finally, make the buffer that we
just drew into be the one which reflects the current contents of the
screen, and place the cursor where it belongs.
Commands that want to can fix the display themselves, and then let
this function know that the display has been fixed by setting the
RL_DISPLAY_FIXED variable. This is good for efficiency. */
/* Application-specific redisplay function. */
rl_voidfunc_t* rl_redisplay_function= rl_redisplay;
/* Global variables declared here. */
/* What YOU turn on when you have handled all redisplay yourself. */
int rl_display_fixed= 0;
/* The stuff that gets printed out before the actual text of the line.
This is usually pointing to rl_prompt. */
char* rl_display_prompt= (char*)NULL;
/* Variables used to include the editing mode in the prompt. */
char* _rl_emacs_mode_str;
int _rl_emacs_modestr_len;
char* _rl_vi_ins_mode_str;
int _rl_vi_ins_modestr_len;
char* _rl_vi_cmd_mode_str;
int _rl_vi_cmd_modestr_len;
/* Pseudo-global variables declared here. */
/* Hints for other parts of readline to give to the display engine. */
int _rl_suppress_redisplay= 0;
int _rl_want_redisplay = 0;
/* The visible cursor position. If you print some text, adjust this. */
/* NOTE: _rl_last_c_pos is used as a buffer index when not in a locale
supporting multibyte characters, and an absolute cursor position when
in such a locale. This is an artifact of the donated multibyte support.
Care must be taken when modifying its value. */
int _rl_last_c_pos= 0;
int _rl_last_v_pos= 0;
/* Number of physical lines consumed by the current line buffer currently
on screen minus 1. */
int _rl_vis_botlin= 0;
/* This is a hint update_line gives to rl_redisplay that it has adjusted
the value of _rl_last_c_pos *and* taken the presence of any invisible
chars in the prompt into account. rl_redisplay notes this and does not
do the adjustment itself. */
static int cpos_adjusted;
/* The index into the line buffer corresponding to the cursor position */
static int cpos_buffer_position;
/* A flag to note when we're displaying the first line of the prompt */
static int displaying_prompt_first_line;
/* The number of multibyte characters in the prompt, if any */
static int prompt_multibyte_chars;
static int _rl_inv_botlin= 0;
/* Variables used only in this file. */
/* The last left edge of text that was displayed. This is used when
doing horizontal scrolling. It shifts in thirds of a screenwidth. */
static int last_lmargin;
/* A buffer for `modeline' messages. */
static char* msg_buf = 0;
static int msg_bufsiz= 0;
/* Non-zero forces the redisplay even if we thought it was unnecessary. */
static int forced_display;
/* Default and initial buffer size. Can grow. */
static int line_size= 1024;
/* Variables to keep track of the expanded prompt string, which may
include invisible characters. */
static char *local_prompt, *local_prompt_prefix;
static int local_prompt_len;
static int prompt_prefix_length;
/* Number of chars in the buffer that contribute to visible chars on the
screen. This might be different from the number of physical chars in the
presence of multibyte characters */
static int prompt_visible_length;
/* The number of invisible characters in the line currently being
displayed on the screen. */
static int visible_wrap_offset;
/* The number of invisible characters in the prompt string. Static so it
can be shared between rl_redisplay and update_line */
static int wrap_offset;
/* The index of the last invisible character in the prompt string. */
static int prompt_last_invisible;
/* The length (buffer offset) of the first line of the last (possibly
multi-line) buffer displayed on the screen. */
static int visible_first_line_len;
/* Number of invisible characters on the first physical line of the prompt.
Only valid when the number of physical characters in the prompt exceeds
(or is equal to) _rl_screenwidth. */
static int prompt_invis_chars_first_line;
static int prompt_last_screen_line;
static int prompt_physical_chars;
/* An array of indexes into the prompt string where we will break physical
screen lines. It's easier to compute in expand_prompt and use later in
rl_redisplay instead of having rl_redisplay try to guess about invisible
characters in the prompt or use heuristics about where they are. */
static int* local_prompt_newlines;
/* set to a non-zero value by rl_redisplay if we are marking modified
history lines and the current line is so marked. */
static int modmark;
static int line_totbytes;
/* Variables to save and restore prompt and display information. */
/* These are getting numerous enough that it's time to create a struct. */
static char* saved_local_prompt;
static char* saved_local_prefix;
static int* saved_local_prompt_newlines;
static int saved_last_invisible;
static int saved_visible_length;
static int saved_prefix_length;
static int saved_local_length;
static int saved_invis_chars_first_line;
static int saved_physical_chars;
/* Return a string indicating the editing mode, for use in the prompt. */
static char* prompt_modestr(int* lenp)
{
if (rl_editing_mode == emacs_mode)
{
if (lenp)
*lenp= _rl_emacs_mode_str ? _rl_emacs_modestr_len
: RL_EMACS_MODESTR_DEFLEN;
return (char*)(_rl_emacs_mode_str ? _rl_emacs_mode_str
: RL_EMACS_MODESTR_DEFAULT);
}
else if (_rl_keymap == vi_insertion_keymap)
{
if (lenp)
*lenp= _rl_vi_ins_mode_str ? _rl_vi_ins_modestr_len
: RL_VI_INS_MODESTR_DEFLEN;
return (char*)(_rl_vi_ins_mode_str
? _rl_vi_ins_mode_str
: RL_VI_INS_MODESTR_DEFAULT); /* vi insert mode
*/
}
else
{
if (lenp)
*lenp= _rl_vi_cmd_mode_str ? _rl_vi_cmd_modestr_len
: RL_VI_CMD_MODESTR_DEFLEN;
return (char*)(_rl_vi_cmd_mode_str
? _rl_vi_cmd_mode_str
: RL_VI_CMD_MODESTR_DEFAULT); /* vi command
mode */
}
}
/* Expand the prompt string S and return the number of visible
characters in *LP, if LP is not null. This is currently more-or-less
a placeholder for expansion. LIP, if non-null is a place to store the
index of the last invisible character in the returned string. NIFLP,
if non-zero, is a place to store the number of invisible characters in
the first prompt line. The previous are used as byte counts -- indexes
into a character buffer. *VLP gets the number of physical characters in
the expanded prompt (visible length) */
/* Current implementation:
\001 (^A) start non-visible characters
\002 (^B) end non-visible characters
all characters except \001 and \002 (following a \001) are copied to
the returned string; all characters except those between \001 and
\002 are assumed to be `visible'. */
/* Possible values for FLAGS:
PMT_MULTILINE caller indicates that this is part of a multiline prompt
*/
/* This approximates the number of lines the prompt will take when
* displayed */
#define APPROX_DIV(n, d) (((n) < (d)) ? 1 : ((n) / (d)) + 1)
static char* expand_prompt(char* pmt,
int flags,
int* lp,
int* lip,
int* niflp,
int* vlp)
{
char *r, *ret, *p, *igstart, *nprompt, *ms;
int l, rl, last, ignoring, ninvis, invfl, invflset, ind, pind, physchars;
int mlen, newlines, newlines_guess, bound;
int mb_cur_max;
/* We only expand the mode string for the last line of a multiline prompt
(a prompt with embedded newlines). */
ms= (((pmt == rl_prompt) ^ (flags & PMT_MULTILINE)) &&
_rl_show_mode_in_prompt)
? prompt_modestr(&mlen)
: 0;
if (ms)
{
l = strlen(pmt);
nprompt= (char*)xmalloc(l + mlen + 1);
memcpy(nprompt, ms, mlen);
strcpy(nprompt + mlen, pmt);
}
else
nprompt= pmt;
mb_cur_max= MB_CUR_MAX;
if (_rl_screenwidth == 0)
_rl_get_screen_size(0, 0); /* avoid division by zero */
/* Short-circuit if we can. We can do this if we are treating the prompt
as a sequence of bytes and there are no invisible characters in the
prompt to deal with. Since we populate local_prompt_newlines, we have
to run through the rest of the function if this prompt looks like it's
going to be longer than one screen line. */
if ((mb_cur_max <= 1 || rl_byte_oriented) &&
strchr(nprompt, RL_PROMPT_START_IGNORE) == 0)
{
l= strlen(nprompt);
if (l < (_rl_screenwidth > 0 ? _rl_screenwidth : 80))
{
r= (nprompt == pmt) ? savestring(pmt) : nprompt;
if (lp)
*lp= l;
if (lip)
*lip= 0;
if (niflp)
*niflp= 0;
if (vlp)
*vlp= l;
local_prompt_newlines=
(int*)xrealloc(local_prompt_newlines, sizeof(int) * 2);
local_prompt_newlines[0]= 0;
local_prompt_newlines[1]= -1;
return r;
}
}
l= strlen(nprompt); /* XXX */
r= ret= (char*)xmalloc(l + 1);
/* Guess at how many screen lines the prompt will take to size the array
that keeps track of where the line wraps happen */
newlines_guess= (_rl_screenwidth > 0) ? APPROX_DIV(l, _rl_screenwidth)
: APPROX_DIV(l, 80);
local_prompt_newlines= (int*)xrealloc(
local_prompt_newlines, sizeof(int) * (newlines_guess + 1));
local_prompt_newlines[newlines= 0]= 0;
for (rl= 1; rl <= newlines_guess; rl++)
local_prompt_newlines[rl]= -1;
rl= physchars= 0; /* mode string now part of nprompt */
invfl = 0; /* invisible chars in first line of prompt */
invflset = 0; /* we only want to set invfl once */
igstart = 0; /* we're not ignoring any characters yet */
for (ignoring= last= ninvis= 0, p= nprompt; p && *p; p++)
{
/* This code strips the invisible character string markers
RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE */
if (ignoring == 0 &&
*p == RL_PROMPT_START_IGNORE) /* XXX - check ignoring? */
{
ignoring= 1;
igstart = p;
continue;
}
else if (ignoring && *p == RL_PROMPT_END_IGNORE)
{
ignoring= 0;
if (p != (igstart + 1))
last= r - ret - 1;
continue;
}
else
{
#if defined(HANDLE_MULTIBYTE)
if (mb_cur_max > 1 && rl_byte_oriented == 0)
{
pind= p - nprompt;
ind = _rl_find_next_mbchar(nprompt, pind, 1, MB_FIND_NONZERO);
l = ind - pind;
while (l--)
*r++= *p++;
if (!ignoring)
{
/* rl ends up being assigned to prompt_visible_length,
which is the number of characters in the buffer that
contribute to characters on the screen, which might
not be the same as the number of physical characters
on the screen in the presence of multibyte characters */
rl+= ind - pind;
physchars+= _rl_col_width(nprompt, pind, ind, 0);
}
else
ninvis+= ind - pind;
p--; /* compensate for later increment */
}
else
#endif
{
*r++= *p;
if (!ignoring)
{
rl++; /* visible length byte counter */
physchars++;
}
else
ninvis++; /* invisible chars byte counter */
}
if (invflset == 0 && physchars >= _rl_screenwidth)
{
invfl = ninvis;
invflset= 1;
}
if (physchars >= (bound= (newlines + 1) * _rl_screenwidth) &&
local_prompt_newlines[newlines + 1] == -1)
{
int _new;
if (physchars > bound) /* should rarely happen */
{
#if defined(HANDLE_MULTIBYTE)
*r= '\0'; /* need null-termination for strlen */
if (mb_cur_max > 1 && rl_byte_oriented == 0)
_new= _rl_find_prev_mbchar(ret, r - ret, MB_FIND_ANY);
else
#endif
_new= r - ret - (physchars - bound); /* XXX */
}
else
_new= r - ret;
local_prompt_newlines[++newlines]= _new;
}
}
}
if (rl < _rl_screenwidth)
invfl= ninvis;
*r= '\0';
if (lp)
*lp= rl;
if (lip)
*lip= last;
if (niflp)
*niflp= invfl;
if (vlp)
*vlp= physchars;
if (nprompt != pmt)
free(nprompt);
return ret;
}
/* Just strip out RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE from
PMT and return the rest of PMT. */
char* _rl_strip_prompt(char* pmt)
{
char* ret;
ret= expand_prompt(
pmt, 0, (int*)NULL, (int*)NULL, (int*)NULL, (int*)NULL);
return ret;
}
void _rl_reset_prompt(void)
{
rl_visible_prompt_length= rl_expand_prompt(rl_prompt);
}
/*
* Expand the prompt string into the various display components, if
* necessary.
*
* local_prompt = expanded last line of string in rl_display_prompt
* (portion after the final newline)
* local_prompt_prefix = portion before last newline of rl_display_prompt,
* expanded via expand_prompt
* prompt_visible_length = number of visible characters in local_prompt
* prompt_prefix_length = number of visible characters in
*local_prompt_prefix
*
* It also tries to keep track of the number of invisible characters in the
* prompt string, and where they are.
*
* This function is called once per call to readline(). It may also be
* called arbitrarily to expand the primary prompt.
*
* The return value is the number of visible characters on the last line
* of the (possibly multi-line) prompt. In this case, multi-line means
* there are embedded newlines in the prompt string itself, not that the
* number of physical characters exceeds the screen width and the prompt
* wraps.
*/
int rl_expand_prompt(char* prompt)
{
char *p, *t;
int c;
/* Clear out any saved values. */
free(local_prompt);
free(local_prompt_prefix);
local_prompt= local_prompt_prefix= (char*)0;
local_prompt_len = 0;
prompt_last_invisible= prompt_invis_chars_first_line= 0;
prompt_visible_length= prompt_physical_chars= 0;
if (prompt == 0 || *prompt == 0)
return (0);
p= strrchr(prompt, '\n');
if (p == 0)
{
/* The prompt is only one logical line, though it might wrap. */
local_prompt = expand_prompt(prompt,
0,
&prompt_visible_length,
&prompt_last_invisible,
&prompt_invis_chars_first_line,
&prompt_physical_chars);
local_prompt_prefix= (char*)0;
local_prompt_len = local_prompt ? strlen(local_prompt) : 0;
return (prompt_visible_length);
}
else
{
/* The prompt spans multiple lines. */
t = ++p;
local_prompt= expand_prompt(p,
PMT_MULTILINE,
&prompt_visible_length,
&prompt_last_invisible,
&prompt_invis_chars_first_line,
&prompt_physical_chars);
c = *t;
*t = '\0';
/* The portion of the prompt string up to and including the
final newline is now null-terminated. */
local_prompt_prefix= expand_prompt(prompt,
PMT_MULTILINE,
&prompt_prefix_length,
(int*)NULL,
(int*)NULL,
(int*)NULL);
*t = c;
local_prompt_len = local_prompt ? strlen(local_prompt) : 0;
return (prompt_prefix_length);
}
}
/* Initialize the VISIBLE_LINE and INVISIBLE_LINE arrays, and their
associated arrays of line break markers. MINSIZE is the minimum size of
VISIBLE_LINE and INVISIBLE_LINE; if it is greater than LINE_SIZE,
LINE_SIZE is increased. If the lines have already been allocated, this
ensures that they can hold at least MINSIZE characters. */
static void init_line_structures(int minsize)
{
int n;
if (invisible_line == 0) /* initialize it */
{
if (line_size < minsize)
line_size= minsize;
visible_line = (char*)xmalloc(line_size);
invisible_line= (char*)xmalloc(line_size);
}
else if (line_size < minsize) /* ensure it can hold MINSIZE chars */
{
line_size*= 2;
if (line_size < minsize)
line_size= minsize;
visible_line = (char*)xrealloc(visible_line, line_size);
invisible_line= (char*)xrealloc(invisible_line, line_size);
}
for (n= minsize; n < line_size; n++)
{
visible_line[n] = 0;
invisible_line[n]= 1;
}
if (vis_lbreaks == 0)
{
/* should be enough. */
inv_lbsize= vis_lbsize= 256;
#if defined(HANDLE_MULTIBYTE)
line_state_visible->wbsize= vis_lbsize;
line_state_visible->wrapped_line=
(int*)xmalloc(line_state_visible->wbsize * sizeof(int));
line_state_invisible->wbsize= inv_lbsize;
line_state_invisible->wrapped_line=
(int*)xmalloc(line_state_invisible->wbsize * sizeof(int));
#endif
inv_lbreaks = (int*)xmalloc(inv_lbsize * sizeof(int));
vis_lbreaks = (int*)xmalloc(vis_lbsize * sizeof(int));
inv_lbreaks[0]= vis_lbreaks[0]= 0;
}
line_structures_initialized= 1;
}
/* Basic redisplay algorithm. See comments inline. */
void rl_redisplay(void)
{
int in, out, c, linenum, cursor_linenum;
char* line;
int inv_botlin, lb_botlin, lb_linenum, o_cpos;
int newlines, lpos, temp, n0, num, prompt_lines_estimate;
char* prompt_this_line;
int mb_cur_max= MB_CUR_MAX;
#if defined(HANDLE_MULTIBYTE)
wchar_t wc;
size_t wc_bytes;
int wc_width;
mbstate_t ps;
int _rl_wrapped_multicolumn= 0;
#endif
if (_rl_echoing_p == 0)
return;
/* Block keyboard interrupts because this function manipulates global
data structures. */
_rl_block_sigint();
RL_SETSTATE(RL_STATE_REDISPLAYING);
if (!rl_display_prompt)
rl_display_prompt= "";
if (line_structures_initialized == 0)
{
init_line_structures(0);
rl_on_new_line();
}
/* Draw the line into the buffer. */
cpos_buffer_position= -1;
prompt_multibyte_chars= prompt_visible_length - prompt_physical_chars;
line= invisible_line;
out= inv_botlin= 0;
/* Mark the line as modified or not. We only do this for history
lines. */
modmark= 0;
if (_rl_mark_modified_lines && current_history() && rl_undo_list)
{
line[out++]= '*';
line[out] = '\0';
modmark = 1;
}
/* If someone thought that the redisplay was handled, but the currently
visible line has a different modification state than the one about
to become visible, then correct the caller's misconception. */
if (visible_line[0] != invisible_line[0])
rl_display_fixed= 0;
/* If the prompt to be displayed is the `primary' readline prompt (the
one passed to readline()), use the values we have already expanded.
If not, use what's already in rl_display_prompt. WRAP_OFFSET is the
number of non-visible characters in the prompt string. */
/* This is where we output the characters in the prompt before the last
newline, if any. If there aren't any embedded newlines, we don't
write anything. Copy the last line of the prompt string into the line
in any case */
if (rl_display_prompt == rl_prompt || local_prompt)
{
if (local_prompt_prefix && forced_display)
_rl_output_some_chars(local_prompt_prefix,
strlen(local_prompt_prefix));
if (local_prompt_len > 0)
{
temp= local_prompt_len + out + 2;
if (temp >= line_size)
{
line_size = (temp + 1024) - (temp % 1024);
visible_line= (char*)xrealloc(visible_line, line_size);
line= invisible_line= (char*)xrealloc(invisible_line, line_size);
}
strncpy(line + out, local_prompt, local_prompt_len);
out+= local_prompt_len;
}
line[out] = '\0';
wrap_offset= local_prompt_len - prompt_visible_length;
}
else
{
int pmtlen;
prompt_this_line= strrchr(rl_display_prompt, '\n');
if (!prompt_this_line)
prompt_this_line= rl_display_prompt;
else
{
prompt_this_line++;
pmtlen= prompt_this_line - rl_display_prompt; /* temp var */
if (forced_display)
{
_rl_output_some_chars(rl_display_prompt, pmtlen);
/* Make sure we are at column zero even after a newline,
regardless of the state of terminal output processing. */
if (pmtlen < 2 || prompt_this_line[-2] != '\r')
cr();
}
}
prompt_physical_chars= pmtlen= strlen(prompt_this_line);
temp = pmtlen + out + 2;
if (temp >= line_size)
{
line_size = (temp + 1024) - (temp % 1024);
visible_line= (char*)xrealloc(visible_line, line_size);
line= invisible_line= (char*)xrealloc(invisible_line, line_size);
}
strncpy(line + out, prompt_this_line, pmtlen);
out+= pmtlen;
line[out] = '\0';
wrap_offset= prompt_invis_chars_first_line= 0;
}
#if defined(HANDLE_MULTIBYTE)
#define CHECK_INV_LBREAKS() \
do \
{ \
if (newlines >= (inv_lbsize - 2)) \
{ \
inv_lbsize*= 2; \
inv_lbreaks= (int*)xrealloc(inv_lbreaks, inv_lbsize * sizeof(int)); \
} \
if (newlines >= (line_state_invisible->wbsize - 2)) \
{ \
line_state_invisible->wbsize*= 2; \
line_state_invisible->wrapped_line= (int*)xrealloc( \
line_state_invisible->wrapped_line, \
line_state_invisible->wbsize * sizeof(int)); \
} \
} while (0)
#else
#define CHECK_INV_LBREAKS() \
do \
{ \
if (newlines >= (inv_lbsize - 2)) \
{ \
inv_lbsize*= 2; \
inv_lbreaks= (int*)xrealloc(inv_lbreaks, inv_lbsize * sizeof(int)); \
} \
} while (0)
#endif /* !HANDLE_MULTIBYTE */
#if defined(HANDLE_MULTIBYTE)
#define CHECK_LPOS() \
do \
{ \
lpos++; \
if (lpos >= _rl_screenwidth) \
{ \
if (newlines >= (inv_lbsize - 2)) \
{ \
inv_lbsize*= 2; \
inv_lbreaks= \
(int*)xrealloc(inv_lbreaks, inv_lbsize * sizeof(int)); \
} \
inv_lbreaks[++newlines]= out; \
if (newlines >= (line_state_invisible->wbsize - 2)) \
{ \
line_state_invisible->wbsize*= 2; \
line_state_invisible->wrapped_line= (int*)xrealloc( \
line_state_invisible->wrapped_line, \
line_state_invisible->wbsize * sizeof(int)); \
} \
line_state_invisible->wrapped_line[newlines]= \
_rl_wrapped_multicolumn; \
lpos= 0; \
} \
} while (0)
#else
#define CHECK_LPOS() \
do \
{ \
lpos++; \
if (lpos >= _rl_screenwidth) \
{ \
if (newlines >= (inv_lbsize - 2)) \
{ \
inv_lbsize*= 2; \
inv_lbreaks= \
(int*)xrealloc(inv_lbreaks, inv_lbsize * sizeof(int)); \
} \
inv_lbreaks[++newlines]= out; \
lpos = 0; \
} \
} while (0)
#endif
/* inv_lbreaks[i] is where line i starts in the buffer. */
inv_lbreaks[newlines= 0]= 0;
/* lpos is a physical cursor position, so it needs to be adjusted by the
number of invisible characters in the prompt, per line. We compute
the line breaks in the prompt string in expand_prompt, taking
invisible characters into account, and if lpos exceeds the screen
width, we copy the data in the loop below. */
lpos= prompt_physical_chars + modmark;
#if defined(HANDLE_MULTIBYTE)
memset(line_state_invisible->wrapped_line,
0,
line_state_invisible->wbsize * sizeof(int));
num= 0;
#endif
/* prompt_invis_chars_first_line is the number of invisible characters in
the first physical line of the prompt.
wrap_offset - prompt_invis_chars_first_line is usually the number of
invis chars on the second (or, more generally, last) line. */
/* This is zero-based, used to set the newlines */
prompt_lines_estimate= lpos / _rl_screenwidth;
/* what if lpos is already >= _rl_screenwidth before we start drawing the
contents of the command line? */
if (lpos >= _rl_screenwidth)
{
temp= 0;
/* first copy the linebreaks array we computed in expand_prompt */
while (local_prompt_newlines[newlines + 1] != -1)
{
temp = local_prompt_newlines[newlines + 1];
inv_lbreaks[++newlines]= temp;
}
/* Now set lpos from the last newline */
if (mb_cur_max > 1 && rl_byte_oriented == 0 &&
prompt_multibyte_chars > 0)
lpos= _rl_col_width(local_prompt, temp, local_prompt_len, 1) -
(wrap_offset - prompt_invis_chars_first_line);
else
lpos-= (_rl_screenwidth * newlines);
}
prompt_last_screen_line= newlines;
/* Draw the rest of the line (after the prompt) into invisible_line,
keeping track of where the cursor is (cpos_buffer_position), the
number of the line containing the cursor (lb_linenum), the last line
number (lb_botlin and inv_botlin). It maintains an array of line
breaks for display (inv_lbreaks). This handles expanding tabs for
display and displaying meta characters. */
lb_linenum= 0;
#if defined(HANDLE_MULTIBYTE)
in= 0;
if (mb_cur_max > 1 && rl_byte_oriented == 0)
{
memset(&ps, 0, sizeof(mbstate_t));
if (_rl_utf8locale && UTF8_SINGLEBYTE(rl_line_buffer[0]))
{
wc = (wchar_t)rl_line_buffer[0];
wc_bytes= 1;
}
else
wc_bytes= mbrtowc(&wc, rl_line_buffer, rl_end, &ps);
}
else
wc_bytes= 1;
while (in < rl_end)
#else
for (in= 0; in < rl_end; in++)
#endif
{
c= (unsigned char)rl_line_buffer[in];
#if defined(HANDLE_MULTIBYTE)
if (mb_cur_max > 1 && rl_byte_oriented == 0)
{
if (MB_INVALIDCH(wc_bytes))
{
/* Byte sequence is invalid or shortened. Assume that the
first byte represents a character. */
wc_bytes= 1;
/* Assume that a character occupies a single column. */
wc_width= 1;
memset(&ps, 0, sizeof(mbstate_t));
}
else if (MB_NULLWCH(wc_bytes))
break; /* Found '\0' */
else
{
temp = WCWIDTH(wc);
wc_width= (temp >= 0) ? temp : 1;
}
}
#endif
if (out + 8 >= line_size) /* XXX - 8 for \t */
{
line_size*= 2;
visible_line = (char*)xrealloc(visible_line, line_size);
invisible_line= (char*)xrealloc(invisible_line, line_size);
line = invisible_line;
}
if (in == rl_point)
{
cpos_buffer_position= out;
lb_linenum = newlines;
}
#if defined(HANDLE_MULTIBYTE)
if (META_CHAR(c) && _rl_output_meta_chars == 0) /* XXX - clean up */
#else
if (META_CHAR(c))
#endif
{
if (_rl_output_meta_chars == 0)
{
sprintf(line + out, "\\%o", c);
if (lpos + 4 >= _rl_screenwidth)
{
temp= _rl_screenwidth - lpos;