-
Notifications
You must be signed in to change notification settings - Fork 50
/
smenu.c
executable file
·16973 lines (14668 loc) · 542 KB
/
smenu.c
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
/* ################################################################### */
/* Copyright 2015, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <locale.h>
#include <langinfo.h>
#if (defined(__sun) && defined(__SVR4)) || defined(_AIX)
#include <curses.h>
#endif
#include <term.h>
#include <termios.h>
#include <regex.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <wchar.h>
#include "xmalloc.h"
#include "list.h"
#include "index.h"
#include "utf8.h"
#include "fgetc.h"
#include "utils.h"
#include "ctxopt.h"
#include "usage.h"
#include "safe.h"
#define BUF_MALLOC xmalloc
#define BUF_REALLOC xrealloc
#include "tinybuf.h"
#include "smenu.h"
/* ***************** */
/* Extern variables. */
/* ***************** */
extern ll_t *tst_search_list;
/* ***************** */
/* Global variables. */
/* ***************** */
word_t *word_a; /* array containing words data (size: count). */
long count = 0; /* number of words read from stdin. */
long current; /* index the current selection under the cursor). */
long new_current; /* final cur. position, (used in search function). */
long prev_current; /* prev. position stored when using direct access. */
long *line_nb_of_word_a; /* array containing the line number (from 0) *
| of each word read. */
long *first_word_in_line_a; /* array containing the index of the first *
| word of each lines. */
long *shift_right_sym_pos_a; /* screen column number of the right *
| scrolling symbol if any when in line or *
| column mode. */
int forgotten_timer = -1;
int help_timer = -1;
int winch_timer = -1;
int daccess_timer = -1;
int search_timer = -1;
search_mode_t search_mode = NONE;
search_mode_t old_search_mode = NONE;
int help_mode = 0; /* 1 if help is displayed else 0. */
int marked = -1; /* Index of the marked word or -1. */
char *word_buffer;
int (*my_isprint)(int);
int (*my_isempty)(const unsigned char *);
/* UTF-8 useful symbols. */
/* """"""""""""""""""""" */
/* clang-format off */
char * left_arrow = "\xe2\x86\x90"; /* ← leftwards arrow. */
char * up_arrow = "\xe2\x86\x91"; /* ↑ upwards arrow. */
char * right_arrow = "\xe2\x86\x92"; /* → rightwards arrow. */
char * down_arrow = "\xe2\x86\x93"; /* ↓ downwards arrow. */
char * vertical_bar = "\xe2\x94\x82"; /* │ box drawings light vertical. */
char * shift_left_sym = "\xe2\x97\x80"; /* ◀ black left-pointing triangle. */
char * shift_right_sym = "\xe2\x96\xb6"; /* ▶ black right-pointing triangle. */
char * sbar_line = "\xe2\x94\x82"; /* │ box drawings light vertical. */
char * hbar_line = "\xe2\x94\x80"; /* ─ box drawings light horizontal. */
char * hbar_left = "\xe2\x97\x80"; /* ◀ black left-pointing triangle. */
char * hbar_right = "\xe2\x96\xb6"; /* ▶ black right-pointing triangle. */
char * sbar_top = "\xe2\x94\x90"; /* ┐ box drawings light down and l. */
char * sbar_down = "\xe2\x94\x98"; /* ┘ box drawings light up and l. */
char * hbar_begin = "\xe2\x94\x94"; /* └ box drawings light up and r. */
char * hbar_end = "\xe2\x94\x98"; /* ┘ box drawings light up and l. */
char * sbar_curs = "\xe2\x94\x83"; /* ┃ box drawings heavy vertical. */
char * hbar_curs = "\xe2\x94\x81"; /* ━ box drawings heavy horizontal. */
char * sbar_arr_up = "\xe2\x96\xb2"; /* ▲ black up pointing triangle. */
char * sbar_arr_down = "\xe2\x96\xbc"; /* ▼ black down pointing triangle. */
char * msg_arr_down = "\xe2\x96\xbc"; /* ▼ black down pointing triangle. */
/* clang-format on */
/* Mouse tracking. */
/* """"""""""""""" */
char *mouse_trk_on;
char *mouse_trk_off;
/* Variables used to manage the direct access entries. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
daccess_t daccess;
char *daccess_stack;
int daccess_stack_head;
/* Variables used for fuzzy and substring searching. */
/* """"""""""""""""""""""""""""""""""""""""""""""""" */
long *matching_words_da = NULL; /* Array containing the index of all *
| matching words. */
long *best_matching_words_da = NULL; /* Array containing the index of *
| matching words containing a *
| consecutive suite of matching *
| glyphs. */
long *alt_matching_words_da = NULL; /* Alternate array to contain only *
| the matching candidates having *
| potentially a starting/ending *
| pattern. */
/* Variables used in signal handlers. */
/* """""""""""""""""""""""""""""""""" */
volatile sig_atomic_t got_winch = 0;
volatile sig_atomic_t got_winch_alrm = 0;
volatile sig_atomic_t got_forgotten_alrm = 0;
volatile sig_atomic_t got_help_alrm = 0;
volatile sig_atomic_t got_daccess_alrm = 0;
volatile sig_atomic_t got_search_alrm = 0;
volatile sig_atomic_t got_timeout_tick = 0;
volatile sig_atomic_t got_sigpipe = 0;
volatile sig_atomic_t got_sigsegv = 0;
volatile sig_atomic_t got_sigterm = 0;
volatile sig_atomic_t got_sighup = 0;
/* Variables used when a timeout is set (option -x). */
/* """"""""""""""""""""""""""""""""""""""""""""""""" */
timeout_t timeout;
char *timeout_word; /* printed word when the timeout type is WORD. */
char *timeout_seconds; /* string containing the number of remaining *
| seconds. */
int quiet_timeout = 0; /* 1 when we want no message to be displayed. */
/* *************** */
/* Help functions. */
/* *************** */
/* ============================================================ */
/* Parse, create and add an help entry. */
/* - attributes are preprocessed for a future quicker display. */
/* - special entries t,m and c allow to ignore entries when not */
/* in tag mode, when the mouse is not usable or when not in */
/* column mode. */
/* */
/* entries IN array of help data. */
/* help_items_da OUT dynamic array to be updated. */
/* */
/* Return the number of help entries added in help_items_da. */
/* ============================================================ */
int
help_add_entries(win_t *win,
term_t *term,
toggle_t *toggles,
help_attr_entry_t ***help_items_da,
help_entry_t *entries,
int entries_nb)
{
int index;
int nb = 0;
help_attr_entry_t *attr_entry;
for (index = 0; index < entries_nb; index++)
{
/* Do not create a tag/pin related entry if tagging/pinning is not */
/* enabled. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (strchr(entries[index].flags, 't') && !toggles->taggable
&& !toggles->pinable)
continue;
/* Do not create a mouse related entry if -no_mouse is activated. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (strchr(entries[index].flags, 'm') && toggles->no_mouse)
continue;
/* Do not create a column/row related entry if the column or line_mode */
/* is not activated. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (strchr(entries[index].flags, 'c') && !win->line_mode && !win->col_mode)
continue;
/* Allocate and initialize a new attribute. */
/* """""""""""""""""""""""""""""""""""""""" */
attr_entry = xmalloc(sizeof(help_attr_entry_t));
attr_entry->str = xstrdup(entries[index].str);
attr_entry->len = entries[index].len;
attr_entry->attr = attr_new();
if (term->colors > 0)
parse_attr(entries[index].main_attr_str, attr_entry->attr, term->colors);
else
parse_attr(entries[index].alt_attr_str, attr_entry->attr, term->colors);
/* Add it into a dynamic array. */
/* """""""""""""""""""""""""""" */
BUF_PUSH(*help_items_da, attr_entry);
nb++;
}
return nb;
}
/* ======================================================== */
/* Create an array of lines to be displayed by disp_help. */
/* Created lines will have a dynamic length with leading */
/* spaces removed. */
/* */
/* last_line IN last logical line number (from 0). */
/* help_items_da OUT dynamic array to be updated. */
/* */
/* Return the dynamic array containing help data with */
/* attributes processed. */
/* ======================================================== */
help_attr_entry_t ***
init_help(win_t *win,
term_t *term,
toggle_t *toggles,
long last_line,
help_attr_entry_t ***help_items_da)
{
int index; /* used to identify the objects long the help line. */
int entries_nb; /* number of all potentially displayable help *
| entries. */
int entries_total_nb; /* number of help entries to display. */
int line = 0; /* number of windows lines used by the help line. */
int len = 0; /* length of the help line. */
int max_col; /* when to split the help line. */
int forced_nl;
int first_in_line;
/* array of arrays containing help items. */
/* the content of this darray will be returned by this function. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
help_attr_entry_t ***help_lines_da = NULL;
/* array of help items, element of help_lines_da. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
help_attr_entry_t **items_da = NULL;
entries_nb = BUF_LEN(*help_items_da);
if (entries_nb == 0)
{
char *Cleft_arrow = concat("^", left_arrow, (char *)0);
char *Cright_arrow = concat("^", right_arrow, (char *)0);
char *du_arrows = concat(down_arrow, up_arrow, (char *)0);
char *arrows = concat(left_arrow,
down_arrow,
up_arrow,
right_arrow,
(char *)0);
help_entry_t header_entries[] = {
{ "Start", 5, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "of", 2, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "quick", 5, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "help.", 5, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "jk", 2, "bu", "bu", "" }, { "/", 1, "u", "u", "" },
{ du_arrows, 2, "bu", "bu", "" }, { " ", 1, "u", "u", "" },
{ "to", 2, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "scroll.", 7, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "\"", 1, "u", "u", "" }, { "man", 3, "bu", "bu", "" },
{ " ", 1, "u", "u", "" }, { "smenu", 5, "bu", "bu", "" },
{ "\"", 1, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "for", 3, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "more", 4, "u", "u", "" }, { " ", 1, "u", "u", "" },
{ "help.", 5, "u", "u", "" },
};
help_entry_t generic_entries[] = {
{ "\n", 0, "", "", "" },
{ "Move:", 5, "5+b", "r", "" },
{ "Mouse:", 6, "2", "u", "m" },
{ "B1", 2, "b", "b", "m" },
{ ",", 1, "", "", "m" },
{ "B3", 2, "b", "b", "m" },
{ ",", 1, "", "", "m" },
{ "Wheel", 5, "b", "b", "m" },
{ " ", 1, "", "", "m" },
{ "Keyb:", 5, "2", "u", "m" },
{ "hjkl", 4, "b", "b", "" },
{ ",", 1, "", "", "" },
{ arrows, 4, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "PgUp", 4, "b", "b", "" },
{ "/", 1, "", "", "" },
{ "Dn", 2, "b", "b", "" },
{ ",", 1, "", "", "c" },
{ "S-HOME", 6, "b", "b", "c" },
{ "/", 1, "", "", "c" },
{ Cleft_arrow, 2, "b", "b", "c" },
{ "/", 1, "", "", "c" },
{ "H", 1, "b", "b", "c" },
{ ",", 1, "", "", "c" },
{ "S-END", 5, "b", "b", "c" },
{ "/", 1, "", "", "c" },
{ Cright_arrow, 2, "b", "b", "c" },
{ "/", 1, "", "", "c" },
{ "L", 1, "b", "b", "c" },
{ "\n", 0, "", "", "c" },
{ "Shift:", 6, "5+b", "r", "c" },
{ "<", 1, "b", "b", "c" },
{ ",", 1, "", "", "c" },
{ ">", 1, "b", "b", "c" },
{ "\n", 0, "", "", "" },
{ "Abort:", 6, "5+b", "r", "" },
{ "q", 1, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "^C", 2, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Cancel:", 7, "5+b", "r", "" },
{ "ESC", 3, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Select:", 7, "5+b", "r", "" },
{ "Mouse:", 6, "2", "u", "m" },
{ "Double-B1", 9, "b", "b", "m" },
{ " ", 1, "", "", "m" },
{ "Keyb:", 5, "2", "u", "m" },
{ "CR", 2, "b", "b", "" },
{ "\n", 0, "", "", "" },
{ "Search:", 7, "5+b", "r", "" },
{ "Def.", 4, "3", "i", "" },
{ " ", 1, "", "", "" },
{ "(fuzzy):", 8, "3", "i", "" },
{ "/", 1, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Substr:", 7, "3", "i", "" },
{ "\"", 2, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "\'", 1, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Fuzzy:", 6, "3", "i", "" },
{ "~", 1, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "*", 1, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "HOME", 4, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "^A", 2, "b", "b", "" },
{ ",", 1, "", "", "" },
{ " ", 1, "", "", "" },
{ "END", 3, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "^Z", 2, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Prefix:", 7, "3", "i", "" },
{ "=", 1, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "^", 1, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Validate:", 9, "3", "i", "" },
{ "CR", 2, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Next:", 5, "3", "i", "" },
{ "SP", 2, "b", "b", "" },
{ ",", 1, "", "", "" },
{ "n", 1, "b", "b", "" },
{ " ", 1, "", "", "" },
{ "Prev:", 5, "3", "i", "" },
{ "N", 1, "b", "b", "" },
{ "\n", 0, "", "", "t" },
{ "Mark:", 5, "5+b", "r", "t" },
{ "Mouse:", 6, "2", "u", "tm" },
{ "^B1", 3, "b", "b", "tm" },
{ "/", 1, "b", "b", "tm" },
{ "B3", 2, "b", "b", "tm" },
{ " ", 1, "", "", "tm" },
{ "Keyb:", 5, "2", "u", "tm" },
{ "mM", 2, "b", "b", "t" },
{ "\n", 0, "", "", "t" },
{ "Tag:", 4, "5+b", "r", "t" },
{ "Mouse:", 6, "2", "u", "tm" },
{ "B3", 2, "B", "B", "tm" },
{ " ", 1, "", "", "tm" },
{ "Zone:", 5, "3", "i", "ctm" },
{ "/", 1, "b", "b", "ctm" },
{ "Row:", 4, "3", "i", "ctm" },
{ "/", 1, "b", "b", "ctm" },
{ "Col:", 4, "3", "i", "ctm" },
{ "^B3", 3, "B", "B", "ctm" },
{ " ", 1, "b", "b", "ct" },
{ "Keyb:", 5, "2", "u", "tm" },
{ "t", 1, "b", "b", "t" },
{ ",", 1, "", "", "t" },
{ "u", 1, "b", "b", "ct" },
{ " ", 1, "", "", "ct" },
{ "r", 1, "b", "b", "ct" },
{ ",", 1, "", "", "ct" },
{ "c", 1, "b", "b", "ct" },
{ " ", 1, "", "", "ct" },
{ "Cont:", 5, "3", "i", "t" },
{ "T", 1, "b", "b", "t" },
{ "...", 3, "", "", "t" },
{ "T", 1, "b", "b", "t" },
{ ",", 1, "", "", "t" },
{ "Zone:", 5, "3", "i", "t" },
{ "Z", 1, "b", "b", "t" },
{ "...", 3, "", "", "t" },
{ "Z", 1, "b", "b", "t" },
{ ",", 1, "", "", "ct" },
{ "Row:", 4, "3", "i", "ct" },
{ "R", 1, "b", "b", "ct" },
{ "...", 3, "", "", "ct" },
{ "R", 1, "b", "b", "ct" },
{ ",", 1, "", "", "ct" },
{ "Col:", 4, "3", "i", "ct" },
{ "C", 1, "b", "b", "ct" },
{ "...", 3, "", "", "ct" },
{ "C", 1, "b", "b", "ct" },
{ " ", 1, "", "", "t" },
{ "Undo:", 5, "3", "i", "t" },
{ "U", 1, "b", "b", "t" },
{ ",", 1, "", "", "t" },
{ "^T", 2, "b", "b", "t" },
{ " ", 1, "", "", "t" },
};
help_entry_t trailer_entries[] = {
{ "\n", 0, "", "", "" }, { "End", 3, "u", "u", "" },
{ " ", 1, "u", "u", "" }, { "of", 2, "u", "u", "" },
{ " ", 1, "u", "u", "" }, { "quick", 5, "u", "u", "" },
{ " ", 1, "u", "u", "" }, { "help.", 5, "u", "u", "" },
};
entries_total_nb = 0;
entries_nb = sizeof(header_entries) / sizeof(help_entry_t);
entries_total_nb += help_add_entries(win,
term,
toggles,
help_items_da,
header_entries,
entries_nb);
entries_nb = sizeof(generic_entries) / sizeof(help_entry_t);
entries_total_nb += help_add_entries(win,
term,
toggles,
help_items_da,
generic_entries,
entries_nb);
entries_nb = sizeof(trailer_entries) / sizeof(help_entry_t);
entries_total_nb += help_add_entries(win,
term,
toggles,
help_items_da,
trailer_entries,
entries_nb);
}
else
entries_total_nb = entries_nb;
/* Determine when to split the help line if necessary. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
max_col = term->ncolumns - 1;
help_lines_da = NULL;
items_da = NULL;
first_in_line = 0;
/* Fill the darray of darrays, each line is a darray of help_attr_entry_t */
/* A new line is added each time the next entry does not fit in the */
/* width of the window. This is done as long as there is space left in */
/* the window. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
for (index = 0; index < entries_total_nb; index++)
{
/* Ignore item if its length cannot be fully displayed. */
/* '''''''''''''''''''''''''''''''''''''''''''''''''''' */
if ((*help_items_da)[index]->len >= max_col)
continue;
/* increase the total length of displayed items in the line. */
/* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
len += (*help_items_da)[index]->len;
/* Set a flag if we need to start a new line. */
/* '''''''''''''''''''''''''''''''''''''''''' */
if (strcmp((*help_items_da)[index]->str, "\n") == 0)
forced_nl = 1;
else
forced_nl = 0;
if (len >= max_col || forced_nl)
{
line++;
first_in_line = 1;
/* Exit early if we do not have enough space. */
/* '''''''''''''''''''''''''''''''''''''''''' */
if (line > last_line)
break;
len = 0;
/* Dynamically push the current items_da in help_lines_da */
/* And initialize a new items_da. */
/* '''''''''''''''''''''''''''''''''''''''''''''''''''''' */
BUF_PUSH(help_lines_da, items_da);
items_da = NULL;
/* Do not put the '\n' in the current items_da. */
/* '''''''''''''''''''''''''''''''''''''''''''' */
if (forced_nl)
continue;
}
/* Skip ' ' and ',' when they appear first in a line. */
/* '''''''''''''''''''''''''''''''''''''''''''''''''' */
if (first_in_line)
if (strcmp((*help_items_da)[index]->str, " ") == 0
|| strcmp((*help_items_da)[index]->str, ",") == 0)
continue;
first_in_line = 0;
BUF_PUSH(items_da, (*help_items_da)[index]);
}
if (items_da != NULL)
BUF_PUSH(help_lines_da, items_da);
return help_lines_da;
}
/* ================================================= */
/* Display the quick help content. */
/* */
/* help_lines_da IN array of help lines. */
/* fst_disp_help_line IN first help line to display. */
/* ================================================= */
void
disp_help(win_t *win,
term_t *term,
help_attr_entry_t ***help_lines_da,
int fst_disp_help_line)
{
int index; /* used to identify the objects long the help line. */
int max_col; /* when to split the help line. */
int displayed_lines;
int total_lines;
help_attr_entry_t **items_da;
/* Determine when to split the help line if necessary. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""" */
max_col = term->ncolumns - 1;
/* Save the position of the terminal cursor so that it can be */
/* put back there after printing of the help line. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
(void)tputs(TPARM1(save_cursor), 1, outch);
displayed_lines = 0;
total_lines = BUF_LEN(help_lines_da);
/* Print the different objects forming the help lines. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
for (index = fst_disp_help_line; index < total_lines; index++)
{
int i;
int item;
int nb_items;
int len;
if (displayed_lines == win->max_lines)
break;
displayed_lines++;
len = 0;
items_da = help_lines_da[index];
nb_items = BUF_LEN(items_da);
(void)tputs(TPARM1(exit_attribute_mode), 1, outch);
for (item = 0; item < nb_items; item++)
{
help_attr_entry_t *entry;
entry = items_da[item];
len += entry->len;
(void)tputs(TPARM1(exit_attribute_mode), 1, outch);
apply_attr(term, *(entry->attr));
fputs_safe(entry->str, stdout);
(void)tputs(TPARM1(exit_attribute_mode), 1, outch);
}
/* Fill the remaining space with spaces. */
/* """"""""""""""""""""""""""""""""""""" */
for (i = len; i < max_col; i++)
fputc_safe(' ', stdout);
if (displayed_lines < win->max_lines)
fputc_safe('\n', stdout);
}
/* Put back the cursor to its saved position. */
/* """""""""""""""""""""""""""""""""""""""""" */
(void)tputs(TPARM1(restore_cursor), 1, outch);
}
/* *********************************** */
/* Attributes string parsing function. */
/* *********************************** */
/* =========================================================== */
/* Allocation and initialization of a new attribute structure. */
/* */
/* Return the newly created attribute. */
/* =========================================================== */
attrib_t *
attr_new(void)
{
attrib_t *attr;
attr = xmalloc(sizeof(attrib_t));
attr->is_set = UNSET;
attr->fg = -1;
attr->bg = -1;
attr->bold = (signed char)-1;
attr->dim = (signed char)-1;
attr->reverse = (signed char)-1;
attr->standout = (signed char)-1;
attr->underline = (signed char)-1;
attr->italic = (signed char)-1;
attr->invis = (signed char)-1;
attr->blink = (signed char)-1;
return attr;
}
/* ============================================ */
/* Decode attributes toggles if any. */
/* b -> bold */
/* d -> dim */
/* r -> reverse */
/* s -> standout */
/* u -> underline */
/* i -> italic */
/* x -> invis */
/* l -> blink */
/* */
/* Return 0 if some unexpected toggle is found, */
/* 1 otherwise. */
/* ============================================ */
int
decode_attr_toggles(char *s, attrib_t *attr)
{
int rc = 1;
attr->bold = (signed char)0;
attr->dim = (signed char)0;
attr->reverse = (signed char)0;
attr->standout = (signed char)0;
attr->underline = (signed char)0;
attr->italic = (signed char)0;
attr->invis = (signed char)0;
attr->blink = (signed char)0;
while (*s != '\0')
{
switch (*s)
{
case 'b':
attr->bold = (signed char)1;
attr->is_set = SET;
break;
case 'd':
attr->dim = (signed char)1;
attr->is_set = SET;
break;
case 'r':
attr->reverse = (signed char)1;
attr->is_set = SET;
break;
case 's':
attr->standout = (signed char)1;
attr->is_set = SET;
break;
case 'u':
attr->underline = (signed char)1;
attr->is_set = SET;
break;
case 'i':
attr->italic = (signed char)1;
attr->is_set = SET;
break;
case 'n':
attr->invis = (signed char)1;
attr->is_set = SET;
break;
case 'l':
attr->blink = (signed char)1;
attr->is_set = SET;
break;
default:
rc = 0;
break;
}
s++;
}
return rc;
}
/* =============================================================*/
/* Parse attributes in str in the form [fg][/bg][[,.+]toggles] */
/* where: */
/* fg and bg are short representing a color value */
/* toggles is an array of toggles (see decode_attr_toggles) */
/* Returns 1 on success else 0. */
/* attr will be filled by the function. */
/* =============================================================*/
int
parse_attr(char *str, attrib_t *attr, short colors)
{
int n;
char *pos;
char s1[12] = { (char)0 }; /* For the colors. */
char s2[9] = { (char)0 }; /* For the attributes. */
short d1 = -1, d2 = -1; /* colors. */
int rc = 1;
char c = '\0';
/* 11: 4 type+colon,2x3 for colors, 1 for slash. */
/* 8 : max size for the concatenation of attributes. */
/* ''''''''''''''''''''''''''''''''''''''''''''''''' */
n = sscanf(str, "%11[^,.+]%*[,.+]%8s%c", s1, s2, &c);
if (n == 0 || c != '\0')
goto error;
if ((pos = strchr(s1, '/')))
{
if (pos == s1) /* s1 starts with a / */
{
d1 = -1;
if (sscanf(s1 + 1, "%hd", &d2) == 0)
{
d2 = -1;
if (n == 1)
goto error;
}
else if (d2 < 0)
goto error;
}
else if (sscanf(s1, "%hd/%hd", &d1, &d2) < 2)
{
d1 = d2 = -1;
if (n == 1)
goto error;
}
else if (d1 < 0 || d2 < 0)
goto error;
}
else /* no / in the first string. */
{
d2 = -1;
if (sscanf(s1, "%hd", &d1) == 0)
{
d1 = -1;
if (n == 2 || decode_attr_toggles(s1, attr) == 0)
goto error;
}
}
if (colors == 0) /* Monochrome. */
{
attr->fg = -1;
attr->bg = -1;
}
else
{
attr->fg = d1 < colors ? d1 : -1;
attr->bg = d2 < colors ? d2 : -1;
}
if (n == 2)
rc = decode_attr_toggles(s2, attr);
return rc;
error:
return 0;
}
/* ============================================== */
/* Set the terminal attributes according to attr. */
/* ============================================== */
void
apply_attr(term_t *term, attrib_t attr)
{
if (attr.fg >= 0)
set_foreground_color(term, attr.fg);
if (attr.bg >= 0)
set_background_color(term, attr.bg);
if (attr.bold > (signed char)0)
(void)tputs(TPARM1(enter_bold_mode), 1, outch);
if (attr.dim > (signed char)0)
(void)tputs(TPARM1(enter_dim_mode), 1, outch);
if (attr.reverse > (signed char)0)
(void)tputs(TPARM1(enter_reverse_mode), 1, outch);
if (attr.standout > (signed char)0)
(void)tputs(TPARM1(enter_standout_mode), 1, outch);
if (attr.underline > (signed char)0)
(void)tputs(TPARM1(enter_underline_mode), 1, outch);
if (attr.italic > (signed char)0)
(void)tputs(TPARM1(enter_italics_mode), 1, outch);
if (attr.invis > (signed char)0)
(void)tputs(TPARM1(enter_secure_mode), 1, outch);
if (attr.blink > (signed char)0)
(void)tputs(TPARM1(enter_blink_mode), 1, outch);
}
/* ********************* */
/* ini parsing function. */
/* ********************* */
/* ===================================================== */
/* Callback function called when parsing each non-header */
/* line of the ini file. */
/* Returns 0 if OK, 1 if not. */
/* ===================================================== */
int
ini_cb(win_t *win,
term_t *term,
limit_t *limits,
ticker_t *timers,
misc_t *misc,
mouse_t *mouse,
const char *section,
const char *name,
char *value)
{
int error = 0;
int has_colors = term->colors > 7;
if (strcmp(section, "colors") == 0)
{
attrib_t v = { UNSET,
/* fg */ -1,
/* bg */ -1,
/* bold */ (signed char)-1,
/* dim */ (signed char)-1,
/* reverse */ (signed char)-1,
/* standout */ (signed char)-1,
/* underline */ (signed char)-1,
/* italic */ (signed char)-1,
/* invis */ (signed char)-1,
/* blink */ (signed char)-1 };
#define CHECK_ATTR(x) \
else if (strcmp(name, #x) == 0) \
{ \
error = !parse_attr(value, &v, term->colors); \
if (error) \
goto out; \
else \
{ \
if (win->x##_attr.is_set != FORCED) \
{ \
win->x##_attr.is_set = SET; \
if (v.fg >= 0) \
win->x##_attr.fg = v.fg; \
if (v.bg >= 0) \
win->x##_attr.bg = v.bg; \
if (v.bold >= (signed char)0) \
win->x##_attr.bold = v.bold; \
if (v.dim >= (signed char)0) \
win->x##_attr.dim = v.dim; \
if (v.reverse >= (signed char)0) \
win->x##_attr.reverse = v.reverse; \
if (v.standout >= (signed char)0) \
win->x##_attr.standout = v.standout; \
if (v.underline >= (signed char)0) \
win->x##_attr.underline = v.underline; \
if (v.italic >= (signed char)0) \
win->x##_attr.italic = v.italic; \
if (v.invis >= (signed char)0) \
win->x##_attr.invis = v.invis; \
if (v.blink >= (signed char)0) \
win->x##_attr.blink = v.blink; \
} \
} \
}
#define CHECK_ATT_ATTR(x, y) \
else if (strcmp(name, #x #y) == 0) \
{ \
error = !parse_attr(value, &v, term->colors); \
if (error) \
goto out; \
else \
{ \
if (win->x##_attr[y - 1].is_set != FORCED) \
{ \
win->x##_attr[y - 1].is_set = SET; \
if (v.fg >= 0) \
win->x##_attr[y - 1].fg = v.fg; \
if (v.bg >= 0) \
win->x##_attr[y - 1].bg = v.bg; \
if (v.bold >= (signed char)0) \
win->x##_attr[y - 1].bold = v.bold; \
if (v.dim >= (signed char)0) \
win->x##_attr[y - 1].dim = v.dim; \
if (v.reverse >= (signed char)0) \
win->x##_attr[y - 1].reverse = v.reverse; \
if (v.standout >= (signed char)0) \
win->x##_attr[y - 1].standout = v.standout; \
if (v.underline >= (signed char)0) \
win->x##_attr[y - 1].underline = v.underline; \
if (v.italic >= (signed char)0) \
win->x##_attr[y - 1].italic = v.italic; \
if (v.invis >= (signed char)0) \
win->x##_attr[y - 1].invis = v.invis; \
if (v.blink >= (signed char)0) \
win->x##_attr[y - 1].blink = v.blink; \
} \
} \
}
/* [colors] section. */
/* """"""""""""""""" */
if (has_colors)
{
if (strcmp(name, "method") == 0)
{
if (strcmp(value, "classic") == 0)
term->color_method = CLASSIC;
else if (strcmp(value, "ansi") == 0)
term->color_method = ANSI;
else
{
error = 1;
goto out;
}
}
/* clang-format off */
CHECK_ATTR(cursor)
CHECK_ATTR(bar)
CHECK_ATTR(shift)
CHECK_ATTR(message)
CHECK_ATTR(search_field)
CHECK_ATTR(search_text)
CHECK_ATTR(match_field)
CHECK_ATTR(match_text)
CHECK_ATTR(match_err_field)
CHECK_ATTR(match_err_text)
CHECK_ATTR(include)
CHECK_ATTR(exclude)
CHECK_ATTR(tag)
CHECK_ATTR(cursor_on_tag)
CHECK_ATTR(daccess)
CHECK_ATT_ATTR(special, 1)
CHECK_ATT_ATTR(special, 2)