-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.c
1594 lines (1471 loc) · 35.8 KB
/
edit.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 2017-2020,2024 Thomas E. Dickey
** Copyright 1997-2012,2017 Free Software Foundation, Inc.
**
** This file is part of TACK.
**
** TACK 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, version 2.
**
** TACK 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 TACK; see the file COPYING. If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA
*/
#include <stdlib.h>
#include <time.h>
#include <tack.h>
MODULE_ID("$Id: edit.c,v 1.51 2024/05/01 20:42:06 tom Exp $")
/*
* These are adapted from tic.h
*/
typedef struct {
const char *nt_name;
NAME_TYPE nt_type;
int nt_index;
} NAME_TABLE;
#define NAME_ENTRY_DATA 1
/*
* Terminfo edit features
*/
#if TACK_CAN_EDIT
#define SHOW_VALUE 1
#define SHOW_EDIT 2
#define SHOW_DELETE 3
static char change_pad_text[MAX_CHANGES][80];
static TestList change_pad_list[MAX_CHANGES] =
{
{MENU_LAST, 0, 0, 0, 0, 0, 0}
};
static TERMTYPE original_term; /* terminal type description */
#endif
static char *flag_boolean; /* flags for booleans */
static char *flag_numbers; /* flags for numerics */
static char *flag_strings; /* flags for strings */
static int *label_strings;
static int xon_index; /* Subscript for (xon) */
static int xon_shadow;
#if TACK_CAN_EDIT
static int start_display; /* the display has just started */
static int display_lines; /* number of lines displayed */
#endif
static void
alloc_arrays(void)
{
if (flag_boolean == 0) {
flag_boolean = (char *) calloc((size_t) MAX_BOOLEAN, sizeof(char));
}
if (flag_numbers == 0) {
flag_numbers = (char *) calloc((size_t) MAX_NUMBERS, sizeof(char));
}
if (flag_strings == 0) {
label_strings = (int *) calloc((size_t) MAX_STRINGS, sizeof(int));
flag_strings = (char *) calloc((size_t) MAX_STRINGS, sizeof(char));
}
}
static int
compare_capability(const void *a, const void *b)
{
const NAME_TABLE *p = (const NAME_TABLE *) a;
const NAME_TABLE *q = (const NAME_TABLE *) b;
return strcmp(p->nt_name, q->nt_name);
}
#if (defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES))
#define DATA(index,name,type) { name,type,index }
static NAME_TABLE name_table[] =
{
#include <tackgen.h>
};
#undef DATA
#define SIZEOF_NAME_TABLE (sizeof(name_table) / sizeof(name_table[0]))
#define alloc_name_table() /* nothing */
#else
static NAME_TABLE *name_table;
static size_t sizeof_name_table;
#define SIZEOF_NAME_TABLE sizeof_name_table
static int
compare_data(const void *a, const void *b)
{
const NAME_TABLE *p = (const NAME_TABLE *) a;
const NAME_TABLE *q = (const NAME_TABLE *) b;
return strcmp(p->nt_name, q->nt_name);
}
static void
alloc_name_table(void)
{
if (name_table == 0) {
size_t s;
size_t d = 0;
sizeof_name_table = (size_t) (max_booleans + max_numbers + max_strings);
name_table = calloc(sizeof_name_table, sizeof(NAME_TABLE));
for (s = 0; s < max_booleans; ++s) {
name_table[d].nt_name = boolnames[s];
name_table[d].nt_type = BOOLEAN;
name_table[d].nt_index = (int) s;
++d;
}
for (s = 0; s < max_numbers; ++s) {
name_table[d].nt_name = numnames[s];
name_table[d].nt_type = NUMBER;
name_table[d].nt_index = (int) s;
++d;
}
for (s = 0; s < max_strings; ++s) {
name_table[d].nt_name = strnames[s];
name_table[d].nt_type = STRING;
name_table[d].nt_index = (int) s;
++d;
}
qsort(name_table, sizeof_name_table, sizeof(NAME_TABLE), compare_data);
}
}
#endif
static NAME_TABLE const *
find_capability(const char *name)
{
NAME_TABLE key;
NAME_TABLE *lookup;
alloc_name_table();
memset(&key, 0, sizeof(key));
key.nt_name = name;
lookup = bsearch(&key, name_table,
SIZEOF_NAME_TABLE,
sizeof(name_table[0]),
compare_capability);
return lookup ? lookup : 0;
}
#if !TACK_CAN_EDIT
/*
* This is used to relate array-index to capability name/type.
*/
static NAME_TABLE const *
find_cap_by_index(int capIndex, NAME_TYPE capType)
{
static NAME_TABLE const *result = 0;
size_t n;
alloc_name_table();
for (n = 0; n < SIZEOF_NAME_TABLE; ++n) {
if (name_table[n].nt_index == capIndex
&& name_table[n].nt_type == capType) {
result = &name_table[n];
break;
}
}
return result;
}
#endif
static NAME_TABLE const *
find_string_cap_by_name(const char *name)
{
NAME_TABLE const *result = find_capability(name);
if (result != 0 && result->nt_type != STRING)
result = 0;
return result;
}
#if TACK_CAN_EDIT
#define set_saved_boolean(num, value) original_term.Booleans[num] = (char)value
#define set_saved_number(num, value) original_term.Numbers[num] = (short)value
#define set_saved_string(num, value) original_term.Strings[num] = value
#define get_saved_boolean(num) original_term.Booleans[num]
#define get_saved_number(num) original_term.Numbers[num]
#define get_saved_string(num) original_term.Strings[num]
#define set_newer_boolean(num, value) CUR Booleans[num] = (char)value
#define set_newer_number(num, value) CUR Numbers[num] = (short)value
#define set_newer_string(num, value) CUR Strings[num] = value
#define get_newer_boolean(num) CUR Booleans[num]
#define get_newer_number(num) CUR Numbers[num]
#define get_newer_string(num) CUR Strings[num]
#else
#define set_saved_boolean(num, value) /* nothing */
#define set_saved_number(num, value) /* nothing */
#define set_saved_string(num, value) /* nothing */
#define set_newer_boolean(num, value) /* nothing */
#define set_newer_number(num, value) /* nothing */
#define set_newer_string(num, value) /* nothing */
#if 0
static int
get_newer_boolean(int num)
{
int result = 0;
const NAME_TABLE *p = find_cap_by_index(num, BOOLEAN);
if (p != 0)
result = tigetflag((char *) p->nt_name);
return result;
}
static int
get_newer_number(int num)
{
int result = 0;
const NAME_TABLE *p = find_cap_by_index(num, NUMBER);
if (p != 0)
result = tigetnum((char *) p->nt_name);
return result;
}
#endif
static char *
get_newer_string(int num)
{
char *result = 0;
const NAME_TABLE *p = find_cap_by_index(num, STRING);
if (p != 0)
result = tigetstr((NCURSES_CONST char *) p->nt_name);
return result;
}
#endif
/*
** get_string_cap_byname(name, long_name)
**
** Given a cap name, find the value
** Errors are quietly ignored.
*/
const char *
get_string_cap_byname(
const char *name,
const char **long_name)
{
NAME_TABLE const *nt;
if ((nt = find_string_cap_by_name(name)) != 0) {
#ifdef HAVE_CURSES_DATA_BOOLFNAMES
*long_name = strfnames[nt->nt_index];
#endif
return (get_newer_string(nt->nt_index));
}
*long_name = "??";
return (char *) 0;
}
/*
** get_string_cap_byvalue(value)
**
** Given a capability string, find its position in the data base.
** Return the index or -1 if not found.
*/
int
get_string_cap_byvalue(
const char *value)
{
if (value) {
int i;
for (i = 0; i < (int) MAX_STRINGS; i++) {
/* FIXME - this implies ncurses... */
if (get_newer_string(i) == value) {
return i;
}
}
/* search for translated strings */
for (i = 0; i < TM_last; i++) {
if (TM_string[i].value == value) {
return TM_string[i].index;
}
}
}
return -1;
}
/*
** user_modified()
**
** Return TRUE if the user has modified the terminfo
*/
#if TACK_CAN_EDIT
int
user_modified(void)
{
int i;
for (i = 0; i < MAX_BOOLEAN; i++) {
int v = (i == xon_index) ? xon_shadow : get_newer_boolean(i);
if (get_saved_boolean(i) != v) {
return TRUE;
}
}
for (i = 0; i < MAX_NUMBERS; i++) {
if (get_saved_number(i) != get_newer_number(i)) {
return TRUE;
}
}
for (i = 0; i < (int) MAX_STRINGS; i++) {
const char *a, *b;
if ((a = get_saved_string(i)) == 0)
a = "";
if ((b = get_newer_string(i)) == 0)
b = "";
if (strcmp(a, b)) {
return TRUE;
}
}
return FALSE;
}
#endif
/*****************************************************************************
*
* Maintain the list of capabilities that can be tested
*
*****************************************************************************/
/*
** mark_cap(name, flag)
**
** Mark the cap data base with the flag provided.
*/
static void
mark_cap(
const char *name,
int flag)
{
NAME_TABLE const *nt;
alloc_arrays();
if ((nt = find_capability(name)) != 0) {
switch (nt->nt_type) {
case BOOLEAN:
flag_boolean[nt->nt_index] = ((char)
(flag_boolean[nt->nt_index]
| flag));
break;
case STRING:
flag_strings[nt->nt_index] = ((char)
(flag_strings[nt->nt_index]
| flag));
break;
case NUMBER:
flag_numbers[nt->nt_index] = ((char)
(flag_numbers[nt->nt_index]
| flag));
break;
default:
sprintf(temp, "unknown cap type (%.*s)", NAME_SIZE, name);
ptextln(temp);
break;
}
} else {
#ifdef HAVE_CURSES_DATA_BOOLNAMES
sprintf(temp, "Cap not found: %.*s", NAME_SIZE, name);
ptextln(temp);
(void) wait_here();
#endif
}
}
/*
** can_test(name-list, flags)
**
** Scan the name list and get the names.
** Enter each name into the can-test data base.
** <space> ( and ) may be used as separators.
*/
void
can_test(
const char *s,
int flags)
{
if (s) {
int ch, j;
char name[NAME_SIZE];
for (j = 0; (ch = name[j] = *s); s++) {
if (ch == ' ' || ch == ')' || ch == '(') {
if (j) {
name[j] = '\0';
mark_cap(name, flags);
}
j = 0;
} else {
j++;
}
}
if (j) {
mark_cap(name, flags);
}
}
}
/*
** cap_index(name-list, index-list)
**
** Scan the name list and return a list of indexes.
** <space> ( and ) may be used as separators.
** This list is terminated with -1.
*/
void
cap_index(
const char *s,
int *inx)
{
if (s) {
int j;
char name[NAME_SIZE];
for (j = 0;; s++) {
int ch = name[j] = *s;
if (ch == ' ' || ch == ')' || ch == '(' || ch == 0) {
if (j) {
NAME_TABLE const *nt;
name[j] = '\0';
if ((nt = find_string_cap_by_name(name)) != 0) {
*inx++ = nt->nt_index;
}
}
if (ch == 0) {
break;
}
j = 0;
} else {
j++;
}
}
}
*inx = -1;
}
/*
** cap_match(name-list, cap)
**
** Scan the name list and see if the cap is in the list.
** Return TRUE if we find an exact match.
** <space> ( and ) may be used as separators.
*/
int
cap_match(
const char *names,
const char *cap)
{
if (names) {
int l = (int) strlen(cap);
const char *s;
while ((s = strstr(names, cap))) {
int c = (names == s) ? 0 : *(s - 1);
int t = s[l];
if ((c == 0 || c == ' ' || c == '(') &&
(t == 0 || t == ' ' || t == ')')) {
return TRUE;
}
if (t == 0) {
break;
}
names = s + l;
}
}
return FALSE;
}
/*
** show_report(test_list, status, ch)
**
** Display a list of caps that can be tested
*/
void
show_report(
TestList * t,
int *state GCC_UNUSED,
int *ch)
{
int i, j, nc, flag;
const char *s;
size_t count = (size_t) (MAX_BOOLEAN + MAX_NUMBERS + MAX_STRINGS);
const char **nx = (const char **) calloc(sizeof(const char *), count);
alloc_arrays();
flag = t->flags & 255;
nc = 0;
for (i = 0; i < MAX_BOOLEAN; i++) {
if (flag_boolean[i] & flag) {
nx[nc++] = boolnames[i];
}
}
for (i = 0; i < MAX_NUMBERS; i++) {
if (flag_numbers[i] & flag) {
nx[nc++] = numnames[i];
}
}
for (i = 0; i < (int) MAX_STRINGS; i++) {
if (flag_strings[i] & flag) {
nx[nc++] = STR_NAME(i);
}
}
/* sort */
for (i = 0; i < nc - 1; i++) {
for (j = i + 1; j < nc; j++) {
if (strcmp(nx[i], nx[j]) > 0) {
s = nx[i];
nx[i] = nx[j];
nx[j] = s;
}
}
}
if (flag & FLAG_FUNCTION_KEY) {
ptextln("The following function keys can be tested:");
} else if (flag & FLAG_CAN_TEST) {
ptextln("The following capabilities can be tested:");
} else if (flag & FLAG_TESTED) {
ptextln("The following capabilities have been tested:");
}
put_crlf();
for (i = 0; i < nc; i++) {
sprintf(temp, "%s ", nx[i]);
ptext(temp);
}
put_newlines(1);
*ch = REQUEST_PROMPT;
free(nx);
}
#ifdef NCURSES_VERSION
#if TACK_CAN_EDIT
static size_t
safe_length(const char *value)
{
size_t result = 0;
if (VALID_STRING(value))
result = strlen(value) + 1;
return result;
}
static size_t
safe_copy(char *target, const char *source)
{
size_t result = safe_length(source);
if (result) {
memcpy(target, source, result);
}
return result;
}
static void
copy_termtype(TERMTYPE *target, TERMTYPE *source)
{
size_t need;
int n;
#if NCURSES_XNAMES
int num_Names = (source->ext_Booleans + source->ext_Numbers + source->ext_Strings);
#endif
memset(target, 0, sizeof(*target));
#define copy_array(member,count) \
target->member = calloc((size_t)(count), sizeof(target->member[0])); \
memcpy(target->member, source->member, (size_t)(count) * sizeof(target->member[0]))
copy_array(Booleans, MAX_BOOLEAN);
copy_array(Numbers, MAX_NUMBERS);
copy_array(Strings, MAX_STRINGS);
need = safe_length(source->term_names);
for (n = 0; n < STRCOUNT; ++n) {
need += safe_length(source->Strings[n]);
}
if (need) {
size_t have = 0;
target->term_names =
target->str_table = malloc(need);
have = safe_copy(target->term_names, source->term_names);
for (n = 0; n < STRCOUNT; ++n) {
if (VALID_STRING(source->Strings[n])) {
target->Strings[n] = target->str_table + have;
have = safe_copy(target->Strings[n], source->Strings[n]);
}
}
}
#if NCURSES_XNAMES
target->num_Booleans = source->num_Booleans;
target->num_Numbers = source->num_Numbers;
target->num_Strings = source->num_Strings;
target->ext_Booleans = source->ext_Booleans;
target->ext_Numbers = source->ext_Numbers;
target->ext_Strings = source->ext_Strings;
need = 0;
for (n = 0; n < source->ext_Strings; ++n) {
need += safe_length(source->ext_str_table + need);
}
for (n = 0; n < num_Names; ++n) {
need += safe_length(source->ext_Names[n]);
}
if (need) {
size_t have = 0;
target->ext_Names = calloc((size_t) num_Names,
sizeof(target->ext_Names[0]));
target->ext_str_table = malloc(need);
for (n = STRCOUNT; n < source->num_Strings; ++n) {
if (safe_length(source->Strings[n])) {
target->Strings[n] = target->ext_str_table + have;
have += safe_copy(target->Strings[n], source->Strings[n]);
}
}
for (n = 0; n < num_Names; ++n) {
target->ext_Names[n] = target->ext_str_table + have;
have += safe_copy(target->ext_Names[n], source->ext_Names[n]);
}
}
#endif /* NCURSES_XNAMES */
}
#endif /* TACK_CAN_EDIT */
#if NO_LEAKS
static void
free_termtype(TERMTYPE *tp)
{
free(tp->Booleans);
free(tp->Numbers);
free(tp->Strings);
free(tp->str_table);
#if NCURSES_XNAMES
free(tp->ext_str_table);
free(tp->ext_Names);
#endif
memset(tp, 0, sizeof(*tp));
}
#endif /* NO_LEAKS */
#endif /* NCURSES_VERSION */
/*
** edit_init()
**
** Initialize the function key data base
*/
void
edit_init(void)
{
int i, j, lc;
char *lab;
NAME_TABLE const *nt;
alloc_arrays();
#if TACK_CAN_EDIT
copy_termtype(&original_term, CUR_TP);
for (i = 0; i < MAX_BOOLEAN; i++) {
set_saved_boolean(i, get_newer_boolean(i));
}
for (i = 0; i < MAX_NUMBERS; i++) {
set_saved_number(i, get_newer_number(i));
}
#endif
/* scan for labels */
for (i = lc = 0; i < (int) MAX_STRINGS; i++) {
set_saved_string(i, get_newer_string(i));
if (strncmp(STR_NAME(i), "lf", (size_t) 2) == 0) {
flag_strings[i] |= FLAG_LABEL;
if (get_newer_string(i)) {
label_strings[lc++] = i;
}
}
}
/* scan for function keys */
for (i = 0; i < (int) MAX_STRINGS; i++) {
const char *this_name = STR_NAME(i);
if ((this_name[0] == 'k') && strcmp(this_name, "kmous")) {
flag_strings[i] |= FLAG_FUNCTION_KEY;
lab = (char *) 0;
for (j = 0; j < lc; j++) {
if (!strcmp(this_name,
STR_NAME(label_strings[j]))) {
lab = get_newer_string(label_strings[j]);
break;
}
}
enter_key(this_name, get_newer_string(i), lab);
}
}
/* Lookup the translated strings */
for (i = 0; i < TM_last; i++) {
if ((nt = find_string_cap_by_name(TM_string[i].name)) != 0) {
TM_string[i].index = nt->nt_index;
} else {
sprintf(temp, "TM_string lookup failed for: %s",
TM_string[i].name);
ptextln(temp);
}
}
if ((nt = find_capability("xon")) != 0) {
xon_index = nt->nt_index;
}
xon_shadow = xon_xoff;
FreeIfNeeded(label_strings);
}
#if TACK_CAN_EDIT
/*
** save_info_string(str, fp)
**
** Write the terminfo string prefixed by the correct separator
*/
static void
save_info_string(
const char *str,
FILE *fp)
{
int len;
len = (int) strlen(str);
if (len + display_lines >= 77) {
if (display_lines > 0) {
(void) fprintf(fp, "\n\t");
}
display_lines = 8;
} else if (display_lines > 0) {
(void) fprintf(fp, " ");
display_lines++;
} else {
(void) fprintf(fp, "\t");
display_lines = 8;
}
(void) fprintf(fp, "%s,", str);
display_lines += len + 1;
}
/*
* This is adapted (and reduced) from ncurses' _nc_tic_expand function.
*/
/* this deals with differences over whether 0x7f and 0x80..0x9f are controls */
#define REALPRINT(s) (UChar(*(s)) < 127 && isprint(UChar(*(s))))
#define P_LIMIT(p) (length - (size_t)(p))
#define L_BRACE '{'
#define S_QUOTE '\''
#define R_BRACE '}'
static char *
form_terminfo(const char *srcp)
{
static char *buffer;
static size_t length;
int bufp;
const char *str = VALID_STRING(srcp) ? srcp : "\0\0";
size_t need = (2 + strlen(str)) * 4;
int ch;
if (srcp == 0) {
#if NO_LEAKS
if (buffer != 0) {
free(buffer);
buffer = 0;
length = 0;
}
#endif
return 0;
}
if (buffer == 0 || need > length) {
char *tofree = buffer;
if ((buffer = (char *) realloc(buffer, length = need)) == 0) {
free(tofree);
return 0;
}
}
bufp = 0;
while ((ch = UChar(*str)) != 0) {
if (ch == '%' && REALPRINT(str + 1)) {
buffer[bufp++] = *str++;
/*
* If we have a "%{number}", try to translate it into a "%'char'"
* form, since that will run a little faster when we are
* interpreting it. Having one form for the constant makes it
* simpler to compare terminal descriptions.
*/
if (str[0] == L_BRACE
&& isdigit(UChar(str[1]))) {
char *dst = 0;
long value = strtol(str + 1, &dst, 0);
if (dst != 0
&& *dst == R_BRACE
&& value < 127
&& value != '\\'
&& isprint((int) value)) {
ch = (int) value;
buffer[bufp++] = S_QUOTE;
if (ch == S_QUOTE)
buffer[bufp++] = '\\';
buffer[bufp++] = (char) ch;
buffer[bufp++] = S_QUOTE;
str = dst;
} else {
buffer[bufp++] = *str;
}
} else {
buffer[bufp++] = *str;
}
} else if (ch == 128) {
buffer[bufp++] = '\\';
buffer[bufp++] = '0';
} else if (ch == '\033') {
buffer[bufp++] = '\\';
buffer[bufp++] = 'E';
} else if (ch == '\\' && (str == srcp || str[-1] != '^')) {
buffer[bufp++] = '\\';
buffer[bufp++] = '\\';
} else if (ch == ' ' && (str == srcp || strcspn(str, " \t") == 0)) {
buffer[bufp++] = '\\';
buffer[bufp++] = 's';
} else if (ch == ',' || ch == ':' || ch == '^') {
buffer[bufp++] = '\\';
buffer[bufp++] = (char) ch;
} else if (REALPRINT(str)
&& (ch != ','
&& ch != ':'
&& ch != '^')) {
buffer[bufp++] = (char) ch;
} else if (ch == '\r') {
buffer[bufp++] = '\\';
buffer[bufp++] = 'r';
} else if (ch == '\n') {
buffer[bufp++] = '\\';
buffer[bufp++] = 'n';
} else if (UChar(ch) < 32
&& isdigit(UChar(str[1]))) {
sprintf(&buffer[bufp], "^%c", ch + '@');
bufp += 2;
} else {
sprintf(&buffer[bufp], "\\%03o", ch);
bufp += 4;
}
str++;
}
buffer[bufp] = '\0';
return (buffer);
}
/*
** save_info(test_list, status, ch)
**
** Write the current terminfo to a file
*/
void
save_info(
TestList * t,
int *state,
int *ch)
{
int i;
FILE *fp;
time_t now;
char buf[TEMP_SIZE];
if ((fp = fopen(tty_basename, "w")) == (FILE *) NULL) {
(void) sprintf(temp, "can't open: %s", tty_basename);
ptextln(temp);
generic_done_message(t, state, ch);
return;
}
time(&now);
/* Note: ctime() returns a newline at the end of the string */
(void) fprintf(fp, "# Terminfo created by TACK for TERM=%s on %s",
tty_basename, ctime(&now));
(void) fprintf(fp, "%s|%s,\n", tty_basename, longname());
display_lines = 0;
for (i = 0; i < MAX_BOOLEAN; i++) {
if (i == xon_index ? xon_shadow : get_newer_boolean(i)) {
save_info_string(boolnames[i], fp);
}
}
for (i = 0; i < MAX_NUMBERS; i++) {
if (get_newer_number(i) >= 0) {
sprintf(buf, "%s#%d", numnames[i], get_newer_number(i));
save_info_string(buf, fp);
}
}
for (i = 0; i < (int) MAX_STRINGS; i++) {
const char *value = get_newer_string(i);
if (value) {
sprintf(buf, "%s=%s", STR_NAME(i), form_terminfo(value));
save_info_string(buf, fp);
}
}
(void) fprintf(fp, "\n");
(void) fclose(fp);
sprintf(temp, "Terminfo saved as file: %s", tty_basename);
ptextln(temp);
}
/*
** send_info_string(str)
**
** Return the terminfo string prefixed by the correct separator
*/
static void
send_info_string(
const char *str,
int *ch)
{
int len;
if (display_lines == -1) {
return;
}
len = (int) strlen(str);
if (len + char_count + 3 >= columns) {
if (start_display == 0) {
put_str(",");
}
put_crlf();
if (++display_lines > lines) {
ptext("-- more -- ");
*ch = wait_here();
if (*ch == 'q') {
display_lines = -1;
return;
}
display_lines = 0;
}
if (len >= columns) {
/* if the terminal does not (am) then this loses */
if (columns) {
display_lines += (((int) strlen(str) + 3) / columns) + 1;
}
put_str(" ");
put_str(str);
start_display = 0;
return;
}
ptext(" ");
} else if (start_display == 0) {
ptext(", ");
} else {
ptext(" ");
}
ptext(str);
start_display = 0;
}
/*
** show_info(test_list, status, ch)
**
** Display the current terminfo
*/
static void
show_info(
TestList * t GCC_UNUSED,
int *state GCC_UNUSED,
int *ch)
{
int i;
char buf[TEMP_SIZE];
display_lines = 1;