forked from jonas/tig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
1380 lines (1112 loc) · 34.9 KB
/
options.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 (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* This program 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.
*/
#include "tig/tig.h"
#include "tig/types.h"
#include "tig/argv.h"
#include "tig/io.h"
#include "tig/repo.h"
#include "tig/refdb.h"
#include "tig/options.h"
#include "tig/request.h"
#include "tig/line.h"
#include "tig/keys.h"
#include "tig/view.h"
/*
* Option variables.
*/
#define DEFINE_OPTION_VARIABLES(name, type, flags) type opt_##name;
OPTION_INFO(DEFINE_OPTION_VARIABLES)
static struct option_info option_info[] = {
#define DEFINE_OPTION_INFO(name, type, flags) { #name, STRING_SIZE(#name), #type, &opt_##name },
OPTION_INFO(DEFINE_OPTION_INFO)
};
struct option_info *
find_option_info(struct option_info *option, size_t options, const char *prefix, const char *name)
{
size_t namelen = strlen(name);
char prefixed[SIZEOF_STR];
int i;
if (*prefix && namelen == strlen(prefix) &&
!string_enum_compare(prefix, name, namelen)) {
name = "display";
namelen = strlen(name);
}
for (i = 0; i < options; i++) {
if (!strcmp(option[i].type, "view_settings") &&
enum_equals_prefix(option[i], name, namelen))
return &option[i];
if (enum_equals(option[i], name, namelen))
return &option[i];
if (enum_name_prefixed(prefixed, sizeof(prefixed), prefix, option[i].name) &&
namelen == strlen(prefixed) &&
!string_enum_compare(prefixed, name, namelen))
return &option[i];
}
return NULL;
}
static struct option_info *
find_option_info_by_value(void *value)
{
int i;
for (i = 0; i < ARRAY_SIZE(option_info); i++)
if (option_info[i].value == value)
return &option_info[i];
return NULL;
}
static void
mark_option_seen(void *value)
{
struct option_info *option = find_option_info_by_value(value);
if (option)
option->seen = true;
}
struct option_info *
find_column_option_info(enum view_column_type type, union view_column_options *opts,
const char *option, struct option_info *column_info,
const char **column_name)
{
#define DEFINE_COLUMN_OPTION_INFO(name, type, flags) \
{ #name, STRING_SIZE(#name), #type, &opt->name, flags },
#define DEFINE_COLUMN_OPTION_INFO_CHECK(name, id, options) \
if (type == VIEW_COLUMN_##id) { \
struct name##_options *opt = &opts->name; \
struct option_info info[] = { \
options(DEFINE_COLUMN_OPTION_INFO) \
}; \
struct option_info *match; \
match = find_option_info(info, ARRAY_SIZE(info), #name, option); \
if (match) { \
*column_info = *match; \
*column_name = #name; \
return column_info; \
} \
}
COLUMN_OPTIONS(DEFINE_COLUMN_OPTION_INFO_CHECK);
*column_name = NULL;
return NULL;
}
/*
* State variables.
*/
iconv_t opt_iconv_out = ICONV_NONE;
char opt_editor[SIZEOF_STR] = "";
const char **opt_cmdline_args = NULL;
/*
* Mapping between options and command argument mapping.
*/
const char *
diff_context_arg()
{
static char opt_diff_context_arg[9] = "";
if (opt_diff_context < 0 ||
!string_format(opt_diff_context_arg, "-U%u", opt_diff_context))
return "";
return opt_diff_context_arg;
}
const char *
use_mailmap_arg()
{
return opt_mailmap ? "--use-mailmap" : "";
}
const char *
log_custom_pretty_arg(void)
{
return opt_mailmap
? "--pretty=format:commit %m %H %P%x00%aN <%aE> %ad%x00%s"
: "--pretty=format:commit %m %H %P%x00%an <%ae> %ad%x00%s";
}
#define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
static const struct enum_map_entry ignore_space_arg_map[] = {
ENUM_ARG(IGNORE_SPACE_NO, ""),
ENUM_ARG(IGNORE_SPACE_ALL, "--ignore-all-space"),
ENUM_ARG(IGNORE_SPACE_SOME, "--ignore-space-change"),
ENUM_ARG(IGNORE_SPACE_AT_EOL, "--ignore-space-at-eol"),
};
const char *
ignore_space_arg()
{
return ignore_space_arg_map[opt_ignore_space].name;
}
static const struct enum_map_entry commit_order_arg_map[] = {
ENUM_ARG(COMMIT_ORDER_AUTO, ""),
ENUM_ARG(COMMIT_ORDER_DEFAULT, ""),
ENUM_ARG(COMMIT_ORDER_TOPO, "--topo-order"),
ENUM_ARG(COMMIT_ORDER_DATE, "--date-order"),
ENUM_ARG(COMMIT_ORDER_AUTHOR_DATE, "--author-date-order"),
ENUM_ARG(COMMIT_ORDER_REVERSE, "--reverse"),
};
const char *
commit_order_arg()
{
return commit_order_arg_map[opt_commit_order].name;
}
const char *
commit_order_arg_with_graph(enum graph_display graph_display)
{
enum commit_order commit_order = opt_commit_order;
if (commit_order == COMMIT_ORDER_AUTO &&
graph_display != GRAPH_DISPLAY_NO)
commit_order = COMMIT_ORDER_TOPO;
return commit_order_arg_map[commit_order].name;
}
/* Use --show-notes to support Git >= 1.7.6 */
#define NOTES_ARG "--show-notes"
#define NOTES_EQ_ARG NOTES_ARG "="
static char opt_notes_arg[SIZEOF_STR] = NOTES_ARG;
const char *
show_notes_arg()
{
if (opt_show_notes)
return opt_notes_arg;
/* Notes are disabled by default when passing --pretty args. */
return "";
}
void
update_options_from_argv(const char *argv[])
{
int next, flags_pos;
for (next = flags_pos = 0; argv[next]; next++) {
const char *flag = argv[next];
int value = -1;
if (map_enum(&value, commit_order_arg_map, flag)) {
opt_commit_order = value;
mark_option_seen(&opt_commit_order);
continue;
}
if (map_enum(&value, ignore_space_arg_map, flag)) {
opt_ignore_space = value;
mark_option_seen(&opt_ignore_space);
continue;
}
if (!strcmp(flag, "--no-notes")) {
opt_show_notes = false;
mark_option_seen(&opt_show_notes);
continue;
}
if (!prefixcmp(flag, "--show-notes") ||
!prefixcmp(flag, "--notes")) {
opt_show_notes = true;
string_ncopy(opt_notes_arg, flag, strlen(flag));
mark_option_seen(&opt_show_notes);
continue;
}
if (!prefixcmp(flag, "-U")
&& parse_int(&value, flag + 2, 0, 999999) == SUCCESS) {
opt_diff_context = value;
mark_option_seen(&opt_diff_context);
continue;
}
argv[flags_pos++] = flag;
}
argv[flags_pos] = NULL;
}
/*
* User config file handling.
*/
static const struct enum_map_entry color_map[] = {
#define COLOR_MAP(name) ENUM_MAP_ENTRY(#name, COLOR_##name)
COLOR_MAP(DEFAULT),
COLOR_MAP(BLACK),
COLOR_MAP(BLUE),
COLOR_MAP(CYAN),
COLOR_MAP(GREEN),
COLOR_MAP(MAGENTA),
COLOR_MAP(RED),
COLOR_MAP(WHITE),
COLOR_MAP(YELLOW),
};
static const struct enum_map_entry attr_map[] = {
#define ATTR_MAP(name) ENUM_MAP_ENTRY(#name, A_##name)
ATTR_MAP(NORMAL),
ATTR_MAP(BLINK),
ATTR_MAP(BOLD),
ATTR_MAP(DIM),
ATTR_MAP(REVERSE),
ATTR_MAP(STANDOUT),
ATTR_MAP(UNDERLINE),
};
#define set_attribute(attr, name) map_enum(attr, attr_map, name)
enum status_code
parse_step(double *opt, const char *arg)
{
int value = atoi(arg);
if (!value && !isdigit(*arg))
return error("Invalid double or percentage");
*opt = value;
if (!strchr(arg, '%'))
return SUCCESS;
/* "Shift down" so 100% and 1 does not conflict. */
*opt /= 100;
if (*opt >= 1.0) {
*opt = 0.99;
return error("Percentage is larger than 100%%");
}
if (*opt < 0.0) {
*opt = 1;
return error("Percentage is less than 0%%");
}
return SUCCESS;
}
enum status_code
parse_int(int *opt, const char *arg, int min, int max)
{
int value = atoi(arg);
if (min <= value && value <= max) {
*opt = value;
return SUCCESS;
}
return error("Value must be between %d and %d", min, max);
}
static bool
set_color(int *color, const char *name)
{
if (map_enum(color, color_map, name))
return true;
/* Git expects a plain int w/o prefix, however, color<int> is
* the preferred Tig color notation. */
if (!prefixcmp(name, "color"))
name += 5;
return string_isnumber(name) &&
parse_int(color, name, 0, 255) == SUCCESS;
}
#define is_quoted(c) ((c) == '"' || (c) == '\'')
static enum status_code
parse_color_name(const char *color, struct line_rule *rule, const char **prefix_ptr)
{
const char *prefixend = is_quoted(*color) ? NULL : strchr(color, '.');
if (prefixend) {
struct keymap *keymap = get_keymap(color, prefixend - color);
if (!keymap)
return error("Unknown key map: %.*s", (int) (prefixend - color), color);
if (prefix_ptr)
*prefix_ptr = keymap->name;
color = prefixend + 1;
}
memset(rule, 0, sizeof(*rule));
if (is_quoted(*color)) {
rule->line = color + 1;
rule->linelen = strlen(color) - 2;
} else {
rule->name = color;
rule->namelen = strlen(color);
}
return SUCCESS;
}
static int
find_remapped(const char *remapped[][2], size_t remapped_size, const char *arg)
{
size_t arglen = strlen(arg);
int i;
for (i = 0; i < remapped_size; i++) {
const char *name = remapped[i][0];
size_t namelen = strlen(name);
if (arglen == namelen &&
!string_enum_compare(arg, name, namelen))
return i;
}
return -1;
}
/* Wants: object fgcolor bgcolor [attribute] */
static enum status_code
option_color_command(int argc, const char *argv[])
{
struct line_rule rule = {0};
const char *prefix = NULL;
struct line_info *info;
enum status_code code;
if (argc < 3)
return error("Invalid color mapping: color area fgcolor bgcolor [attrs]");
code = parse_color_name(argv[0], &rule, &prefix);
if (code != SUCCESS)
return code;
info = add_line_rule(prefix, &rule);
if (!info) {
static const char *obsolete[][2] = {
{ "acked", "' Acked-by'" },
{ "diff-copy-from", "'copy from '" },
{ "diff-copy-to", "'copy to '" },
{ "diff-deleted-file-mode", "'deleted file mode '" },
{ "diff-dissimilarity", "'dissimilarity '" },
{ "diff-rename-from", "'rename from '" },
{ "diff-rename-to", "'rename to '" },
{ "diff-tree", "'diff-tree '" },
{ "filename", "file" },
{ "help-keymap", "help.section" },
{ "main-revgraph", "" },
{ "pp-adate", "'AuthorDate: '" },
{ "pp-author", "'Author: '" },
{ "pp-cdate", "'CommitDate: '" },
{ "pp-commit", "'Commit: '" },
{ "pp-date", "'Date: '" },
{ "reviewed", "' Reviewed-by'" },
{ "signoff", "' Signed-off-by'" },
{ "stat-head", "status.header" },
{ "stat-section", "status.section" },
{ "tested", "' Tested-by'" },
{ "tree-dir", "tree.directory" },
{ "tree-file", "tree.file" },
{ "tree-head", "tree.header" },
};
int index;
index = find_remapped(obsolete, ARRAY_SIZE(obsolete), rule.name);
if (index != -1) {
if (!*obsolete[index][1])
return error("%s is obsolete", argv[0]);
/* Keep the initial prefix if defined. */
code = parse_color_name(obsolete[index][1], &rule, prefix ? NULL : &prefix);
if (code != SUCCESS)
return code;
info = add_line_rule(prefix, &rule);
}
if (!info)
return error("Unknown color name: %s", argv[0]);
code = error("%s has been replaced by %s",
obsolete[index][0], obsolete[index][1]);
}
if (!set_color(&info->fg, argv[1]))
return error("Unknown color: %s", argv[1]);
if (!set_color(&info->bg, argv[2]))
return error("Unknown color: %s", argv[2]);
info->attr = 0;
while (argc-- > 3) {
int attr;
if (!set_attribute(&attr, argv[argc]))
return error("Unknown color attribute: %s", argv[argc]);
info->attr |= attr;
}
return code;
}
static enum status_code
parse_bool(bool *opt, const char *arg)
{
*opt = (!strcmp(arg, "1") || !strcmp(arg, "true") || !strcmp(arg, "yes"))
? true : false;
if (*opt || !strcmp(arg, "0") || !strcmp(arg, "false") || !strcmp(arg, "no"))
return SUCCESS;
return error("Non-boolean value treated as false: %s", arg);
}
static enum status_code
parse_enum(const char *name, unsigned int *opt, const char *arg,
const struct enum_map *map)
{
bool is_true;
enum status_code code;
assert(map->size > 1);
if (map_enum_do(map->entries, map->size, (int *) opt, arg))
return SUCCESS;
code = parse_bool(&is_true, arg);
*opt = is_true ? map->entries[1].value : map->entries[0].value;
if (code == SUCCESS)
return code;
return error("'%s' is not a valid value for %s; using %s",
arg, name, enum_name(map->entries[*opt].name));
}
static enum status_code
parse_string(char *opt, const char *arg, size_t optsize)
{
int arglen = strlen(arg);
switch (arg[0]) {
case '\"':
case '\'':
if (arglen == 1 || arg[arglen - 1] != arg[0])
return ERROR_UNMATCHED_QUOTATION;
arg += 1; arglen -= 2;
default:
string_ncopy_do(opt, optsize, arg, arglen);
return SUCCESS;
}
}
static enum status_code
parse_encoding(struct encoding **encoding_ref, const char *arg, bool priority)
{
char buf[SIZEOF_STR];
enum status_code code = parse_string(buf, arg, sizeof(buf));
if (code == SUCCESS) {
struct encoding *encoding = *encoding_ref;
if (encoding && !priority)
return code;
encoding = encoding_open(buf);
if (encoding)
*encoding_ref = encoding;
}
return code;
}
static enum status_code
parse_args(const char ***args, const char *argv[])
{
if (!argv_copy(args, argv))
return ERROR_OUT_OF_MEMORY;
return SUCCESS;
}
enum status_code
parse_option(struct option_info *option, const char *prefix, const char *arg)
{
char name[SIZEOF_STR];
if (!enum_name_prefixed(name, sizeof(name), prefix, option->name))
return error("Failed to parse option");
if (!strcmp("show-notes", name)) {
bool *value = option->value;
enum status_code res;
if (parse_bool(option->value, arg) == SUCCESS)
return SUCCESS;
*value = true;
string_copy(opt_notes_arg, NOTES_EQ_ARG);
res = parse_string(opt_notes_arg + STRING_SIZE(NOTES_EQ_ARG), arg,
sizeof(opt_notes_arg) - STRING_SIZE(NOTES_EQ_ARG));
if (res == SUCCESS && !opt_notes_arg[STRING_SIZE(NOTES_EQ_ARG)])
opt_notes_arg[STRING_SIZE(NOTES_ARG)] = 0;
return res;
}
if (!strcmp(option->type, "bool"))
return parse_bool(option->value, arg);
if (!strcmp(option->type, "double"))
return parse_step(option->value, arg);
if (!strncmp(option->type, "enum", 4)) {
const char *type = option->type + STRING_SIZE("enum ");
const struct enum_map *map = find_enum_map(type);
return parse_enum(name, option->value, arg, map);
}
if (!strcmp(option->type, "int")) {
if (strstr(name, "title-overflow")) {
bool enabled = false;
int *value = option->value;
/* We try to parse it as a boolean (and set the
* value to 0 if fale), otherwise we parse it as
* an integer and use the given value. */
if (parse_bool(&enabled, arg) == SUCCESS) {
if (!enabled) {
*value = 0;
return SUCCESS;
}
arg = "50";
}
}
if (!strcmp(name, "line-number-interval") ||
!strcmp(name, "tab-size"))
return parse_int(option->value, arg, 1, 1024);
else if (!strcmp(name, "id-width"))
return parse_int(option->value, arg, 0, SIZEOF_REV - 1);
else
return parse_int(option->value, arg, 0, 1024);
}
return error("Unhandled option: %s", name);
}
static enum status_code
parse_view_settings(struct view_column **view_column, const char *name_, const char *argv[])
{
char buf[SIZEOF_STR];
const char *name = enum_name_copy(buf, sizeof(buf), name_) ? buf : name_;
const char *prefixed;
if ((prefixed = strstr(name, "-view-"))) {
const char *column_name = prefixed + STRING_SIZE("-view-");
size_t column_namelen = strlen(column_name);
enum view_column_type type;
for (type = 0; type < view_column_type_map->size; type++) {
const struct enum_map_entry *column = &view_column_type_map->entries[type];
if (enum_equals(*column, column_name, column_namelen))
return parse_view_column_config(name, type, NULL, argv);
if (enum_equals_prefix(*column, column_name, column_namelen))
return parse_view_column_config(name, type,
column_name + column->namelen + 1,
argv);
}
}
return parse_view_config(view_column, name, argv);
}
/* Wants: name = value */
static enum status_code
option_set_command(int argc, const char *argv[])
{
struct option_info *option;
enum status_code code;
if (argc < 2)
return error("Invalid set command: set option = value");
if (strcmp(argv[1], "="))
return error("No value assigned to %s", argv[0]);
option = find_option_info(option_info, ARRAY_SIZE(option_info), "", argv[0]);
if (option) {
if (option->seen)
return SUCCESS;
if (!strcmp(option->type, "const char **"))
return parse_args(option->value, argv + 2);
if (argc < 3)
return error("Invalid set command: set option = value");
if (!strcmp(option->type, "view_settings"))
return parse_view_settings(option->value, argv[0], argv + 2);
if (!strcmp(option->type, "struct ref_format **"))
return parse_ref_formats(option->value, argv + 2);
code = parse_option(option, "", argv[2]);
if (code == SUCCESS && argc != 3)
return error("Option %s only takes one value", argv[0]);
return code;
}
{
const char *obsolete[][2] = {
{ "author-width", "author" },
{ "filename-width", "file-name" },
{ "line-number-interval", "line-number" },
{ "show-author", "author" },
{ "show-date", "date" },
{ "show-file-size", "file-size" },
{ "show-filename", "file-name" },
{ "show-id", "id" },
{ "show-line-numbers", "line-number" },
{ "show-refs", "commit-title" },
{ "show-rev-graph", "commit-title" },
{ "title-overflow", "commit-title and text" },
};
int index = find_remapped(obsolete, ARRAY_SIZE(obsolete), argv[0]);
if (index != -1)
return error("%s is obsolete; see tigrc(5) for how to set the %s column option",
obsolete[index][0], obsolete[index][1]);
if (!strcmp(argv[0], "read-git-colors"))
return error("read-git-colors has been obsoleted by the git-colors option");
if (!strcmp(argv[0], "cmdline-args"))
return error("cmdline-args is obsolete; use view-specific options instead, e.g. main-options");
}
return error("Unknown option name: %s", argv[0]);
}
/* Wants: mode request key */
static enum status_code
option_bind_command(int argc, const char *argv[])
{
struct key key[16];
size_t keys = 0;
enum request request;
struct keymap *keymap;
const char *key_arg;
if (argc < 3)
return error("Invalid key binding: bind keymap key action");
if (!(keymap = get_keymap(argv[0], strlen(argv[0])))) {
if (!strcmp(argv[0], "branch"))
keymap = get_keymap("refs", strlen("refs"));
if (!keymap)
return error("Unknown key map: %s", argv[0]);
}
for (keys = 0, key_arg = argv[1]; *key_arg && keys < ARRAY_SIZE(key); keys++) {
enum status_code code = get_key_value(&key_arg, &key[keys]);
if (code != SUCCESS)
return code;
}
if (*key_arg && keys == ARRAY_SIZE(key))
return error("Except for <Esc> combos only one key is allowed "
"in key combos: %s", argv[1]);
request = get_request(argv[2]);
if (request == REQ_UNKNOWN) {
static const char *obsolete[][2] = {
{ "view-branch", "view-refs" },
};
static const char *toggles[][2] = {
{ "diff-context-down", "diff-context" },
{ "diff-context-up", "diff-context" },
{ "stage-next", ":/^@@" },
{ "toggle-author", "author" },
{ "toggle-changes", "show-changes" },
{ "toggle-commit-order", "show-commit-order" },
{ "toggle-date", "date" },
{ "toggle-files", "file-filter" },
{ "toggle-file-filter", "file-filter" },
{ "toggle-file-size", "file-size" },
{ "toggle-filename", "filename" },
{ "toggle-graphic", "show-graphic" },
{ "toggle-id", "id" },
{ "toggle-ignore-space", "show-ignore-space" },
{ "toggle-lineno", "line-number" },
{ "toggle-refs", "commit-title-refs" },
{ "toggle-rev-graph", "commit-title-graph" },
{ "toggle-show-changes", "show-changes" },
{ "toggle-sort-field", "sort-field" },
{ "toggle-sort-order", "sort-order" },
{ "toggle-title-overflow", "commit-title-overflow" },
{ "toggle-untracked-dirs", "status-untracked-dirs" },
{ "toggle-vertical-split", "show-vertical-split" },
};
int alias;
alias = find_remapped(obsolete, ARRAY_SIZE(obsolete), argv[2]);
if (alias != -1) {
const char *action = obsolete[alias][1];
add_keybinding(keymap, get_request(action), key, keys);
return error("%s has been renamed to %s",
obsolete[alias][0], action);
}
alias = find_remapped(toggles, ARRAY_SIZE(toggles), argv[2]);
if (alias != -1) {
const char *action = toggles[alias][0];
const char *arg = prefixcmp(action, "diff-context-")
? NULL : (strstr(action, "-down") ? "-1" : "+1");
const char *mapped = toggles[alias][1];
const char *toggle[] = { ":toggle", mapped, arg, NULL};
const char *other[] = { mapped, NULL };
const char **prompt = *mapped == ':' ? other : toggle;
enum status_code code = add_run_request(keymap, key, keys, prompt);
if (code == SUCCESS)
code = error("%s has been replaced by `%s%s%s%s'",
action, prompt == other ? mapped : ":toggle ",
prompt == other ? "" : mapped,
arg ? " " : "", arg ? arg : "");
return code;
}
}
if (request == REQ_UNKNOWN)
return add_run_request(keymap, key, keys, argv + 2);
return add_keybinding(keymap, request, key, keys);
}
static enum status_code load_option_file(const char *path);
static enum status_code
option_source_command(int argc, const char *argv[])
{
enum status_code code;
if (argc < 1)
return error("Invalid source command: source path");
code = load_option_file(argv[0]);
return code == ERROR_FILE_DOES_NOT_EXIST
? error("File does not exist: %s", argv[0]) : code;
}
enum status_code
set_option(const char *opt, int argc, const char *argv[])
{
if (!strcmp(opt, "color"))
return option_color_command(argc, argv);
if (!strcmp(opt, "set"))
return option_set_command(argc, argv);
if (!strcmp(opt, "bind"))
return option_bind_command(argc, argv);
if (!strcmp(opt, "source"))
return option_source_command(argc, argv);
return error("Unknown option command: %s", opt);
}
struct config_state {
const char *path;
size_t lineno;
bool errors;
};
static enum status_code
read_option(char *opt, size_t optlen, char *value, size_t valuelen, void *data)
{
struct config_state *config = data;
enum status_code status = ERROR_NO_OPTION_VALUE;
/* Check for comment markers, since read_properties() will
* only ensure opt and value are split at first " \t". */
optlen = strcspn(opt, "#");
if (optlen == 0)
return SUCCESS;
if (opt[optlen] == 0) {
/* Look for comment endings in the value. */
size_t len = strcspn(value, "#");
const char *argv[SIZEOF_ARG];
int argc = 0;
if (len < valuelen) {
valuelen = len;
value[valuelen] = 0;
}
if (!argv_from_string(argv, &argc, value))
status = error("Too many option arguments for %s", opt);
else
status = set_option(opt, argc, argv);
}
if (status != SUCCESS) {
warn("%s:%zu: %s", config->path, config->lineno,
get_status_message(status));
config->errors = true;
}
/* Always keep going if errors are encountered. */
return SUCCESS;
}
static enum status_code
load_option_file(const char *path)
{
struct config_state config = { path, 0, false };
struct io io;
char buf[SIZEOF_STR];
/* Do not read configuration from stdin if set to "" */
if (!path || !strlen(path))
return SUCCESS;
if (!prefixcmp(path, "~/")) {
const char *home = getenv("HOME");
if (!home || !string_format(buf, "%s/%s", home, path + 2))
return error("Failed to expand ~ to user home directory");
path = buf;
}
/* It's OK that the file doesn't exist. */
if (!io_open(&io, "%s", path)) {
/* XXX: Must return ERROR_FILE_DOES_NOT_EXIST so missing
* system tigrc is detected properly. */
if (io_error(&io) == ENOENT)
return ERROR_FILE_DOES_NOT_EXIST;
return error("Error loading file %s: %s", path, io_strerror(&io));
}
if (io_load_span(&io, " \t", &config.lineno, read_option, &config) != SUCCESS ||
config.errors == true)
warn("Errors while loading %s.", path);
return SUCCESS;
}
extern const char *builtin_config;
enum status_code
load_options(void)
{
const char *tigrc_user = getenv("TIGRC_USER");
const char *tigrc_system = getenv("TIGRC_SYSTEM");
const char *tig_diff_opts = getenv("TIG_DIFF_OPTS");
const bool diff_opts_from_args = !!opt_diff_options;
bool custom_tigrc_system = !!tigrc_system;
opt_file_filter = true;
if (!find_option_info_by_value(&opt_diff_context)->seen)
opt_diff_context = -3;
if (!custom_tigrc_system)
tigrc_system = SYSCONFDIR "/tigrc";
if (!*tigrc_system ||
(load_option_file(tigrc_system) == ERROR_FILE_DOES_NOT_EXIST && !custom_tigrc_system)) {
struct config_state config = { "<built-in>", 0, false };
struct io io;
if (!io_from_string(&io, builtin_config))
return error("Failed to get built-in config");
if (io_load_span(&io, " \t", &config.lineno, read_option, &config) != SUCCESS || config.errors == true)
return error("Error in built-in config");
}
if (!tigrc_user)
tigrc_user = "~/.tigrc";
load_option_file(tigrc_user);
if (!diff_opts_from_args && tig_diff_opts && *tig_diff_opts) {
static const char *diff_opts[SIZEOF_ARG] = { NULL };
char buf[SIZEOF_STR];
int argc = 0;
if (!string_format(buf, "%s", tig_diff_opts) ||
!argv_from_string(diff_opts, &argc, buf))
return error("TIG_DIFF_OPTS contains too many arguments");
else if (!argv_copy(&opt_diff_options, diff_opts))
return error("Failed to format TIG_DIFF_OPTS arguments");
}
return SUCCESS;
}
const char *
format_option_value(const struct option_info *option, char buf[], size_t bufsize)
{
buf[0] = 0;
if (!strcmp(option->type, "bool")) {
bool *opt = option->value;
if (string_nformat(buf, bufsize, NULL, "%s", *opt ? "yes" : "no"))
return buf;
} else if (!strncmp(option->type, "enum", 4)) {
const char *type = option->type + STRING_SIZE("enum ");
enum author *opt = option->value;
const struct enum_map *map = find_enum_map(type);
if (enum_name_copy(buf, bufsize, map->entries[*opt].name))
return buf;
} else if (!strcmp(option->type, "int")) {
int *opt = option->value;
if (opt == &opt_diff_context && *opt < 0)
*opt = -*opt;
if (string_nformat(buf, bufsize, NULL, "%d", *opt))
return buf;
} else if (!strcmp(option->type, "double")) {
double *opt = option->value;
if (*opt >= 1) {
if (string_nformat(buf, bufsize, NULL, "%d", (int) *opt))