Repository navigation
Expand file tree
/
Copy pathparse_args.c
More file actions
1524 lines (1301 loc) · 47.8 KB
/
Copy pathparse_args.c
File metadata and controls
1524 lines (1301 loc) · 47.8 KB
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
/* TODO:
* - Better error messages
*/
#include <parse_argsInt.h>
#define NS "::" PACKAGE_NAME
static struct tracked_instances g_tracked_instances;
// Process-global so that a spec_id identifies a spec even for Tcl_Objs shared
// between interps (option lookups are cached on the arg objs keyed by it)
static atomic_ulong g_spec_id_seq;
struct option_info {
Tcl_Obj* param; // What this param is called in the spec
Tcl_Obj* name; // The name that will store this params value
Tcl_Obj* default_val; // NULL if no default
Tcl_Obj* validator; // NULL if no validator
Tcl_Obj* enum_choices; // NULL if not an enum, also stores multi_choices for a multi
Tcl_Obj* comment; // NULL if no comment
int arg_count; // -1: store the option name in -name if present; -2: comsume all remaining args
int all_idx; // >= 0 - collect all instances of this option as a list, accumulated in a list at this index
int multi_idx; // If this option is part of a multi select
int decl_idx; // Index of this entry in the argspec (options and positionals), -1 for spec->multi entries
bool is_args; // args style processing - consume all remaining arguments
bool required;
bool alias; // boolean
bool end_options; // Treat seeing this option as if -- followed immediately after it
};
struct parse_spec {
struct tracked_instance inst; // Must be first (so that parse_spec can be cast to tracked_instance)
int refcount; // Held by the intrep, and by each bind in progress (a validator could shimmer the spec obj)
unsigned long spec_id; // unique, monotonic (until overflow), assigned from g_spec_id_seq
struct option_info* option; // Ordered for spec_find_option (currently: sorted in strcmp(param) collation order, for bsearch)
int option_count;
int decl_count; // Number of entries in the argspec (options + positionals)
bool has_alias;
Tcl_Obj* usage_msg;
struct option_info* positional;
int positional_arg_count;
struct option_info* multi;
int multi_count;
int all_count; // Number of -all accumulators a bind needs
int min_required_positional; // sum over -required positionals of max(1, arg_count); is_args contributes 0
};
// Micro Tcl_ObjType - enum_choices <<<
static void free_enum_choices_intrep(Tcl_Obj* obj);
static void dup_drop_intrep(Tcl_Obj* src, Tcl_Obj* dst);
static Tcl_ObjType enum_choices_type = {
.name = "parse_spec_enum_choices",
.freeIntRepProc = free_enum_choices_intrep,
.dupIntRepProc = dup_drop_intrep,
};
/* Duplicates of our objtypes whose intreps own resources don't get the intrep:
* they are regenerated on demand from the string rep, which these objtypes
* ensure exists before storing their intreps */
static void dup_drop_intrep(Tcl_Obj* src, Tcl_Obj* dst)
{
(void)src;
(void)dst;
}
static void free_enum_choices_intrep(Tcl_Obj* obj)
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(obj, &enum_choices_type);
if (ir->otherValuePtr) {
ckfree(ir->otherValuePtr);
ir->otherValuePtr = NULL;
}
forget_instance(&g_tracked_instances, obj, NULL);
}
static int GetEnumChoicesFromObj(Tcl_Interp* interp, Tcl_Obj* obj, char*** res)
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(obj, &enum_choices_type);
if (!ir) {
const char** table;
Tcl_Size len;
TEST_OK(Tcl_SplitList(interp, Tcl_GetString(obj), &len, &table));
Tcl_StoreInternalRep(obj, &enum_choices_type, &(Tcl_ObjInternalRep){ .otherValuePtr = table });
register_instance(&g_tracked_instances, obj, NULL);
ir = Tcl_FetchInternalRep(obj, &enum_choices_type);
}
*res = ir->otherValuePtr;
return TCL_OK;
}
// Micro Tcl_ObjType - enum_choices >>>
// Micro Tcl_ObjType - option <<<
/* Using Tcl_GetIndexFromObj to look up arguments against the option name list (and cache those
lookups) has a flaw: the objects are converted to a type that caches the found index for the
table whose cache validity is based on the address of the table, but our tables are dynamic:
they live as long as the parse spec obj, but Tcl (especially in optimized builds) will eagerly
recycle Tcl_Objs and under heavy churn a new parse spec can re-use the address of a previous
one, and so indicies looked up against the old spec (which doesn't match the new one) are returned
without consulting the new which can lead to `-foo` resolving to `-bar` in the old map because
it is in the same slot, or an out-of-bounds reference if `-foo` was at an index beyond the last
element in the new spec. Our performance requirements cannot afford to prevent this caching
(using flag TCL_INDEX_TEMP_TABLE for Tcl_GetIndexFromObj), so we have to implement our own
index caching lookup that is robust for our use case (include a unique global parse_spec monotonic
id in the cache key)
*/
static void dup_option_intrep(Tcl_Obj* src, Tcl_Obj* dst);
static void free_option_intrep(Tcl_Obj* obj);
static Tcl_ObjType option_type = {
.name = "parse_spec_option",
.freeIntRepProc = free_option_intrep,
.dupIntRepProc = dup_option_intrep,
};
static void free_option_intrep(Tcl_Obj* obj)
{
forget_instance(&g_tracked_instances, obj, NULL);
}
static void dup_option_intrep(Tcl_Obj* src, Tcl_Obj* dst)
{
Tcl_StoreInternalRep(dst, &option_type, Tcl_FetchInternalRep(src, &option_type));
register_instance(&g_tracked_instances, dst, NULL);
}
/* Option name lookup <<<
* spec_index_options arranges spec->option at spec compile time, and
* spec_find_option looks up an option by name in it. These are the only places
* that know how options are indexed (currently: sorted, with bsearch), so that
* the strategy can be swapped out (a radish trie, for instance). The order of
* spec->option is otherwise irrelevant: everything that depends on the
* declaration order uses option_info.decl_idx.
*/
static int cmp_opt(const void* a, const void* b)
{
const struct option_info* opt_a = a;
const struct option_info* opt_b = b;
return strcmp(Tcl_GetString(opt_a->param), Tcl_GetString(opt_b->param));
}
static int cmp_opt_key(const void* key, const void* elem)
{
const struct option_info* opt = elem;
return strcmp(key, Tcl_GetString(opt->param));
}
static void spec_index_options(struct parse_spec* spec)
{
qsort(spec->option, spec->option_count, sizeof(spec->option[0]), cmp_opt);
}
static struct option_info* spec_find_option(struct parse_spec* spec, const char* str, Tcl_Size len)
{
(void)len;
return bsearch(str, spec->option, spec->option_count, sizeof(spec->option[0]), cmp_opt_key);
}
// Option name lookup >>>
static void spec_decl_order(const struct parse_spec* spec, const struct option_info* out[]) //<<<
{
// out must have room for spec->decl_count entries
for (int i=0; i<spec->option_count; i++)
out[spec->option[i].decl_idx] = &spec->option[i];
for (int i=0; i<spec->positional_arg_count; i++)
out[spec->positional[i].decl_idx] = &spec->positional[i];
}
//>>>
static int bad_option(Tcl_Interp* interp, const struct parse_spec* spec, const char* str) //<<<
{
// Same message and errorcode as the Tcl_GetIndexFromObj lookup this replaced
if (!interp) return TCL_ERROR;
const struct option_info* decl[spec->decl_count ? spec->decl_count : 1];
spec_decl_order(spec, decl);
Tcl_Obj* msg = Tcl_ObjPrintf("bad option \"%s\": ", str);
if (spec->option_count == 0) {
Tcl_AppendToObj(msg, "no valid options", -1);
} else {
Tcl_AppendToObj(msg, "must be ", -1);
int listed = 0;
for (int d=0; d<spec->decl_count; d++) {
if (Tcl_GetString(decl[d]->param)[0] != '-') continue;
if (listed++)
Tcl_AppendToObj(msg, listed < spec->option_count ? ", " : spec->option_count > 2 ? ", or " : " or ", -1);
Tcl_AppendObjToObj(msg, decl[d]->param);
}
}
Tcl_SetObjResult(interp, msg);
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", "option", str, NULL);
return TCL_ERROR;
}
//>>>
static int GetOptionFromObj(Tcl_Interp* interp, struct parse_spec* spec, Tcl_Obj* opt, struct option_info** option)
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(opt, &option_type);
for (;;) {
if (!ir) {
Tcl_Size len;
const char* str = Tcl_GetStringFromObj(opt, &len);
struct option_info* o = spec_find_option(spec, str, len);
if (!o) return bad_option(interp, spec, str);
Tcl_StoreInternalRep(opt, &option_type, &(Tcl_ObjInternalRep){
.ptrAndLongRep = {
.ptr = o,
.value = spec->spec_id
}
});
register_instance(&g_tracked_instances, opt, NULL);
ir = Tcl_FetchInternalRep(opt, &option_type);
}
if (ir->ptrAndLongRep.value != spec->spec_id) {
// Cached value is stale, force a new lookup
ir = NULL;
continue;
}
*option = ir->ptrAndLongRep.ptr;
break;
}
return TCL_OK;
}
// Micro Tcl_ObjType - option >>>
static void free_internal_rep(Tcl_Obj* obj);
static Tcl_ObjType parse_spec_type = {
.name = "parse_spec",
.freeIntRepProc = free_internal_rep,
.dupIntRepProc = dup_drop_intrep,
};
/* Allocate static Tcl_Objs for these strings for each interp */
static const char* static_str[] = {
"0",
"1",
"idx",
"required",
"default",
"validate",
"choices",
"all",
NULL
};
enum static_objs {
L_FALSE=0,
L_TRUE,
L_IDX,
L_REQUIRED,
L_DEFAULT,
L_VALIDATE,
L_CHOICES,
L_ALL,
L_end
};
struct interp_cx {
Tcl_Obj* obj[L_end];
Tcl_Obj* enums;
};
// Fast unsigned int to string conversion from the talk by Alexandrescu: "Three Optimization Tips for C++" <<<
static uint32_t digits10(uint64_t v) //<<<
{
#define P01 10
#define P02 100
#define P03 1000
#define P04 10000
#define P05 100000
#define P06 1000000
#define P07 10000000
#define P08 100000000
#define P09 1000000000
#define P10 10000000000
#define P11 100000000000
#define P12 1000000000000
if (v < P01) return 1;
if (v < P02) return 2;
if (v < P03) return 3;
if (v < P12) {
if (v < P08) {
if (v < P06) {
if (v < P04) return 4;
return 5 + (v >= P05);
}
return 7 + (v >= P07);
}
if (v < P10) {
return 9 + (v >= P09);
}
return 11 + (v >= P11);
}
return 12 + digits10(v / P12);
}
//>>>
static int u64toa(uint64_t value, char* restrict dst) //<<<
{
// TODO: benchmark this against TclFormatInt and replace the latter with this if it's faster
static const char digits[201] =
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899";
const uint32_t length = digits10(value);
uint32_t next = length-1;
while (value >= 100) {
const int i = (value % 100) * 2;
value /= 100;
memcpy(dst+next-1, digits+i, 2);
//dst[next] = digits[i+1];
//dst[next-1] = digits[i];
next -= 2;
}
if (value < 10) {
dst[next] = '0' + (uint32_t)value;
} else {
const int i = (uint32_t)value * 2;
memcpy(dst+next-1, digits+i, 2);
//dst[next] = digits[i + 1];
//dst[next-1] = digits[i];
}
return length;
}
//>>>
//>>>
static const char* static_numstr(uint64_t v) //<<<
{
static char staticbuf[21]; // 21 - max length of decimal representation of uint64_t + null terminator
staticbuf[u64toa(v, staticbuf)] = 0;
return staticbuf;
}
//>>>
static void free_option_info(struct option_info* option) //<<<
{
if (option) {
replace_tclobj(&option->param, NULL);
replace_tclobj(&option->name, NULL);
replace_tclobj(&option->default_val, NULL);
replace_tclobj(&option->validator, NULL);
replace_tclobj(&option->enum_choices, NULL);
replace_tclobj(&option->comment, NULL);
}
}
//>>>
static void free_parse_spec(struct parse_spec** specPtr) //<<<
{
struct parse_spec* spec = *specPtr;
if (spec) {
// If NULL then we haven't registered the instance yet (or the intrep has been freed)
if (spec->inst.self)
forget_instance(&g_tracked_instances, spec->inst.self, &spec->inst);
if (spec->option != NULL) {
for (int i=0; i < spec->option_count; i++)
free_option_info(&spec->option[i]);
ckfree(spec->option);
spec->option = NULL;
}
replace_tclobj(&spec->usage_msg, NULL);
if (spec->positional) {
for (int i=0; i < spec->positional_arg_count; i++)
free_option_info(&spec->positional[i]);
ckfree(spec->positional);
spec->positional = NULL;
}
if (spec->multi) {
for (int i=0; i < spec->multi_count; i++)
free_option_info(&spec->multi[i]);
ckfree(spec->multi);
spec->multi = NULL;
}
ckfree(spec);
*specPtr = NULL;
}
}
//>>>
static inline void spec_incref(struct parse_spec* spec) //<<<
{
spec->refcount++;
}
//>>>
static void spec_decref(struct parse_spec* spec) //<<<
{
if (--spec->refcount <= 0)
free_parse_spec(&spec);
}
//>>>
static void free_internal_rep(Tcl_Obj* obj) //<<<
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(obj, &parse_spec_type);
struct parse_spec* spec = ir->otherValuePtr;
// The spec can outlive the intrep (if this happens during a bind)
forget_instance(&g_tracked_instances, obj, &spec->inst);
spec_decref(spec);
ir->otherValuePtr = NULL;
}
//>>>
static Tcl_Obj* build_usage_msg(const struct parse_spec* spec) //<<<
{
// Like a proc's usage: options in argspec order, then the positionals
const struct option_info* decl[spec->decl_count ? spec->decl_count : 1];
spec_decl_order(spec, decl);
Tcl_Obj* msg = Tcl_NewObj();
#define APPEND(...) Tcl_AppendStringsToObj(msg, __VA_ARGS__, (char*)NULL)
for (int pass=0; pass<2; pass++) {
for (int d=0; d<spec->decl_count; d++) {
const struct option_info* o = decl[d];
const char* param = Tcl_GetString(o->param);
const bool is_option = param[0] == '-';
if (is_option != (pass == 0)) continue;
if (Tcl_GetCharLength(msg)) APPEND(" ");
if (is_option) {
// A value placeholder: the -enum choices, else the output name
Tcl_Obj* ph = NULL;
defer { replace_tclobj(&ph, NULL); }
if (o->enum_choices) {
Tcl_Size ec;
Tcl_Obj** ev;
if (Tcl_ListObjGetElements(NULL, o->enum_choices, &ec, &ev) == TCL_OK && ec > 0) {
replace_tclobj(&ph, Tcl_NewObj());
for (Tcl_Size e=0; e<ec; e++)
Tcl_AppendStringsToObj(ph, e ? "|" : "", Tcl_GetString(ev[e]), (char*)NULL);
}
}
if (!ph) replace_tclobj(&ph, o->name);
const char* phs = Tcl_GetString(ph);
if (!o->required) APPEND("?");
APPEND(param);
switch (o->arg_count) {
case -2: APPEND(" ", phs, " ..."); break; // -args all
case -1: break; // -multi flag
case 0: break; // -boolean
default:
for (int n=0; n<o->arg_count; n++) APPEND(" ", phs);
}
if (!o->required) APPEND("?");
} else if (o->is_args || o->arg_count == -2) {
APPEND("?", param, " ...?");
} else {
const bool optional = !o->required || o->default_val;
const int n = o->arg_count > 0 ? o->arg_count : 1;
if (optional) APPEND("?");
for (int i=0; i<n; i++) APPEND(i ? " " : "", param);
if (optional) APPEND("?");
}
}
}
#undef APPEND
return msg;
}
//>>>
static int compile_parse_spec(Tcl_Interp* interp, Tcl_Obj* obj, struct parse_spec** res) //<<<
{
struct interp_cx* l = (struct interp_cx*)Tcl_GetAssocData(interp, "parse_args", NULL);
Tcl_Obj* multis = NULL; defer { replace_tclobj(&multis, NULL); }
replace_tclobj(&multis, Tcl_NewDictObj());
Tcl_Size oc;
Tcl_Obj** ov;
TEST_OK(Tcl_ListObjGetElements(interp, obj, &oc, &ov));
if (oc % 2 != 0) THROW_ERROR("argspec must be a dictionary");
struct parse_spec* spec = ckalloc(sizeof(struct parse_spec));
*spec = (struct parse_spec){
.refcount = 1,
.spec_id = atomic_fetch_add(&g_spec_id_seq, 1) + 1,
.decl_count = oc / 2,
};
defer { if (spec) free_parse_spec(&spec); }
{
/* A repeated key would leave the earlier definition unreachable but
* still enforced (-required, -default), so reject it */
Tcl_HashTable seen;
Tcl_InitHashTable(&seen, TCL_STRING_KEYS);
defer { Tcl_DeleteHashTable(&seen); }
for (int i=0; i<oc; i+=2) {
const char* str = Tcl_GetString(ov[i]);
int isnew;
Tcl_CreateHashEntry(&seen, str, &isnew);
if (!isnew) THROW_ERROR("Duplicate argspec entry \"", str, "\"");
if (str[0] == '-') spec->option_count++;
else spec->positional_arg_count++;
}
}
spec->option = ckalloc(sizeof(struct option_info) * spec->option_count);
spec->positional = ckalloc(sizeof(struct option_info) * spec->positional_arg_count);
memset(spec->option, 0, sizeof(struct option_info) * spec->option_count);
memset(spec->positional, 0, sizeof(struct option_info) * spec->positional_arg_count);
int o_i = 0; // Number of options
int p_i = 0; // Number of positional params
for (int i=0; i<oc; i+=2) {
Tcl_Size str_len;
const char* str = Tcl_GetStringFromObj(ov[i], &str_len);
struct option_info* option =
str[0] == '-'
? &spec->option[o_i++]
: &spec->positional[p_i++];
*option = (struct option_info){
.arg_count = 1,
.multi_idx = -1,
.all_idx = -1,
.decl_idx = i / 2,
};
replace_tclobj(&option->param, ov[i]);
Tcl_Size settingc;
Tcl_Obj** settingv;
TEST_OK(Tcl_ListObjGetElements(interp, ov[i+1], &settingc, &settingv));
bool all_specified = false;
for (int j=0; j<settingc; ) {
# define SETTINGS \
X( SETTING_DEFAULT, "-default" ) \
X( SETTING_REQUIRED, "-required" ) \
X( SETTING_VALIDATE, "-validate" ) \
X( SETTING_NAME, "-name" ) \
X( SETTING_BOOLEAN, "-boolean" ) \
X( SETTING_ARGS, "-args" ) \
X( SETTING_ENUM, "-enum" ) \
X( SETTING_COMMENT, "-#" ) \
X( SETTING_MULTI, "-multi" ) \
X( SETTING_ALIAS, "-alias" ) \
X( SETTING_ALL, "-all" ) \
X( SETTING_END, "-end" ) \
/* line intentionally left */
static const char* settings[] = {
# define X(e, s) s,
SETTINGS
# undef X
(char*)NULL
};
enum setting {
# define X(e, s) e,
SETTINGS
# undef X
} setting;
# undef SETTINGS
int settingidx;
TEST_OK(Tcl_GetIndexFromObj(interp, settingv[j], settings, "setting", TCL_EXACT, &settingidx));
setting = settingidx;
j++;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
// Pre-validate that the settings which require a value have one
switch (setting) {
case SETTING_DEFAULT:
case SETTING_VALIDATE:
case SETTING_NAME:
case SETTING_ARGS:
case SETTING_ENUM:
case SETTING_COMMENT:
if (j >= settingc) THROW_ERROR(Tcl_GetString(settingv[j-1]), " needs a value");
break;
}
#pragma GCC diagnostic pop
switch (setting) {
case SETTING_DEFAULT: replace_tclobj(&option->default_val, settingv[j++]); break;
case SETTING_REQUIRED: option->required = true; break;
case SETTING_NAME: replace_tclobj(&option->name, settingv[j++]); break;
case SETTING_COMMENT: replace_tclobj(&option->comment, settingv[j++]); break;
case SETTING_ALIAS: option->alias = spec->has_alias = true; break;
case SETTING_ALL: all_specified = true; break; // Just raise a flag here to prevent multiple -all specs from excessively incrementing all_count
case SETTING_END: option->end_options = true; break;
case SETTING_VALIDATE: //<<<
{
Tcl_Obj* validatorobj = settingv[j++];
Tcl_Obj** vv;
Tcl_Size vc;
TEST_OK(Tcl_ListObjGetElements(interp, validatorobj, &vc, &vv));
if (vc) replace_tclobj(&option->validator, validatorobj);
}
break;
//>>>
case SETTING_BOOLEAN: //<<<
if (str[0] != '-')
THROW_ERROR("Cannot use -boolean on positional argument \"", Tcl_GetString(option->param), "\"");
option->arg_count = 0;
break;
//>>>
case SETTING_ARGS: //<<<
if (strcmp("all", Tcl_GetString(settingv[j])) == 0) {
option->arg_count = -2;
} else {
TEST_OK(Tcl_GetIntFromObj(interp, settingv[j], &option->arg_count));
if (option->arg_count < 0)
THROW_ERROR("-args cannot be negative");
if (option->arg_count == 0 && str[0] != '-')
THROW_ERROR("Cannot use -args 0 on positional argument \"", Tcl_GetString(option->param), "\"");
}
j++;
break;
//>>>
case SETTING_ENUM: //<<<
{
/* enums are validated using Tcl_GetIndexFromObj, which
* shimmers its input obj to record the table and index
* info, so make an effort to unify enums across parse_specs
*/
if (!l->enums) return TCL_ERROR;
Tcl_Obj* enum_choices = settingv[j++];
Tcl_Obj* shared_enum_choices = NULL;
TEST_OK(Tcl_DictObjGet(interp, l->enums, enum_choices, &shared_enum_choices));
if (!shared_enum_choices) {
if (Tcl_IsShared(l->enums))
replace_tclobj(&l->enums, Tcl_DuplicateObj(l->enums));
TEST_OK(Tcl_DictObjPut(interp, l->enums, enum_choices, shared_enum_choices = enum_choices));
}
Tcl_Size size;
TEST_OK(Tcl_DictObjSize(interp, l->enums, &size));
// Paranoia - prevent the speculative enum cache from growing too large
if (size > 1000) replace_tclobj(&l->enums, Tcl_NewDictObj());
replace_tclobj(&option->enum_choices, shared_enum_choices);
}
break;
//>>>
case SETTING_MULTI: //<<<
if (str[0] != '-')
THROW_ERROR("Cannot use -multi on positional argument \"", Tcl_GetString(option->param), "\"");
option->arg_count = -1;
break;
//>>>
default: THROW_PRINTF("Invalid setting: %d", setting);
}
}
if (!option->name) {
replace_tclobj(&option->name,
str[0] == '-'
? Tcl_NewStringObj(str+1, str_len-1)
: option->param
);
}
if (option->arg_count == -1) {
int multi_idx;
Tcl_Obj* multi_choices;
const char* multi_val;
Tcl_Size multi_val_len;
Tcl_Obj* multi_config_loan = NULL;
Tcl_Obj* multi_all = NULL;
TEST_OK(Tcl_DictObjGet(interp, multis, option->name, &multi_config_loan));
if (!multi_config_loan) {
multi_idx = spec->multi_count++;
multi_config_loan = Tcl_NewDictObj();
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_IDX], Tcl_NewIntObj(multi_idx)));
TEST_OK(Tcl_DictObjPut(interp, multis, option->name, multi_config_loan));
multi_choices = Tcl_NewListObj(0, NULL);
} else {
Tcl_Obj* idx_obj = NULL;
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_IDX], &idx_obj));
TEST_OK(Tcl_GetIntFromObj(interp, idx_obj, &multi_idx));
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_CHOICES], &multi_choices));
}
TEST_OK(Tcl_ListObjAppendElement(interp, multi_choices, option->param));
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_CHOICES], multi_choices));
option->multi_idx = multi_idx;
// TODO: throw errors if -boolean or -args are mixed with -multi
if (option->enum_choices) THROW_ERROR("Cannot use -multi and -enum together");
if (option->required)
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_REQUIRED], l->obj[L_TRUE]));
if (option->default_val)
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_DEFAULT], option->default_val));
if (option->validator)
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_VALIDATE], option->validator));
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_ALL], &multi_all));
if (all_specified || multi_all) {
int all_idx;
if (multi_all) {
TEST_OK(Tcl_GetIntFromObj(interp, multi_all, &all_idx));
} else {
all_idx = spec->all_count++;
}
option->all_idx = all_idx;
if (!multi_all)
TEST_OK(Tcl_DictObjPut(interp, multi_config_loan, l->obj[L_ALL], Tcl_NewIntObj(all_idx)));
}
// Repurpose option->default_val to store the value this option will set in the multi output
multi_val = Tcl_GetStringFromObj(option->param, &multi_val_len);
replace_tclobj(&option->default_val, Tcl_NewStringObj(multi_val+1, multi_val_len-1));
} else if (all_specified) {
option->all_idx = spec->all_count++;
}
}
spec_index_options(spec);
// This is only known after parsing the options
if (spec->multi_count > 0) {
spec->multi = ckalloc(sizeof(struct option_info) * spec->multi_count);
memset(spec->multi, 0, sizeof(struct option_info) * spec->multi_count);
Tcl_DictSearch search;
int done;
Tcl_Obj* multi_config_loan = NULL;
Tcl_Obj* name;
TEST_OK(Tcl_DictObjFirst(interp, multis, &search, &name, &multi_config_loan, &done));
defer { Tcl_DictObjDone(&search); }
for (; !done; Tcl_DictObjNext(&search, &name, &multi_config_loan, &done)) {
struct option_info* multi;
Tcl_Obj* idx_obj;
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_IDX], &idx_obj));
int idx;
TEST_OK(Tcl_GetIntFromObj(interp, idx_obj, &idx));
if (idx < 0 || idx >= spec->multi_count) {
THROW_ERROR("Got out of bounds multi_count ", Tcl_GetString(idx_obj), " for option \"", Tcl_GetString(name));
}
multi = &spec->multi[idx];
replace_tclobj(&multi->name, name);
multi->arg_count = -1;
multi->decl_idx = -1;
Tcl_Obj* val;
// set -required
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_REQUIRED], &val));
if (val) multi->required = true;
// set -default
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_DEFAULT], &val));
replace_tclobj(&multi->default_val, val);
// set -validate
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_VALIDATE], &val));
replace_tclobj(&multi->validator, val);
// set multi choices (for error message if required and not set)
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_CHOICES], &val));
replace_tclobj(&multi->enum_choices, val);
TEST_OK(Tcl_DictObjGet(interp, multi_config_loan, l->obj[L_ALL], &val));
if (val) {
TEST_OK(Tcl_GetIntFromObj(interp, val, &multi->all_idx));
} else {
multi->all_idx = -1;
}
}
}
// Create special "args" behaviour for any positional param named "args".
// Earlier this was restricted to the last positional, but now permitting
// it at any position lets callers write specs like {args ... last_arg {-required}}.
// strcmp is safe because Tcl_GetString always gives us a properly
// \0 terminated string
for (int i=0; i < spec->positional_arg_count; i++) {
struct option_info* p = &spec->positional[i];
if (strcmp("args", Tcl_GetString(p->param)) == 0) {
p->is_args = true;
if (!p->default_val) replace_tclobj(&p->default_val, Tcl_NewObj());
}
}
// Validate positional layout: only one is_args/-args all consumer, and any
// positional that follows it must be -required (otherwise it could never
// be reached, since the consumer is left-greedy modulo the suffix reservation).
// Also compute min_required_positional = sum over positionals that need
// args from the caller to succeed. A positional "needs args" iff it is
// -required AND has no -default AND is not is_args/-args all.
{
bool saw_args_consumer = false;
spec->min_required_positional = 0;
for (int i=0; i < spec->positional_arg_count; i++) {
struct option_info* p = &spec->positional[i];
int is_args_like = p->is_args || p->arg_count == -2;
if (saw_args_consumer) {
if (is_args_like)
THROW_ERROR("Cannot have more than one args/-args all positional consumer (\"", Tcl_GetString(p->param), "\")");
if (!p->required || p->default_val)
THROW_ERROR("Positional \"", Tcl_GetString(p->param), "\" cannot follow an args/-args all consumer unless it is -required without a default");
}
if (is_args_like) saw_args_consumer = true;
if (p->required && !is_args_like && !p->default_val)
spec->min_required_positional += (p->arg_count > 0) ? p->arg_count : 1;
}
}
replace_tclobj(&spec->usage_msg, build_usage_msg(spec));
*res = spec;
spec = NULL; // Hand ownership to caller
return TCL_OK;
}
//>>>
static int GetParseSpecFromObj(Tcl_Interp* interp, Tcl_Obj* spec, struct parse_spec** res) //<<<
{
Tcl_ObjInternalRep* ir = Tcl_FetchInternalRep(spec, &parse_spec_type);
if (!ir) {
// Ensure the string rep exists before the intrep replaces whatever it
// could be generated from (a spec built as a pure list, for instance)
Tcl_GetString(spec);
struct parse_spec* compiled_spec = NULL;
TEST_OK(compile_parse_spec(interp, spec, &compiled_spec));
Tcl_StoreInternalRep(spec, &parse_spec_type, &(Tcl_ObjInternalRep){ .otherValuePtr = compiled_spec });
ir = Tcl_FetchInternalRep(spec, &parse_spec_type);
register_instance(&g_tracked_instances, spec, compiled_spec);
}
if (res)
*res = ir->otherValuePtr;
return TCL_OK;
}
//>>>
static int validate(Tcl_Interp* interp, struct option_info* option, Tcl_Obj* val) //<<<
{
if (option->enum_choices) {
int dummy;
char** enum_table;
TEST_OK(GetEnumChoicesFromObj(interp, option->enum_choices, &enum_table));
TEST_OK(Tcl_GetIndexFromObj(interp, val, enum_table, Tcl_GetString(option->param), TCL_EXACT, &dummy));
}
if (option->validator) {
Tcl_Size dontcare;
TEST_OK(Tcl_ListObjLength(interp, option->validator, &dontcare)); // Force to list before we dup (cache command lookup)
Tcl_Obj* valcmd = NULL; defer { replace_tclobj(&valcmd, NULL); }
replace_tclobj(&valcmd, Tcl_DuplicateObj(option->validator));
TEST_OK(Tcl_ListObjAppendElement(interp, valcmd, val));
const int res = Tcl_EvalObjEx(interp, valcmd, TCL_EVAL_DIRECT);
Tcl_Obj* verdict = NULL; defer { replace_tclobj(&verdict, NULL); }
replace_tclobj(&verdict, Tcl_GetObjResult(interp));
int passed;
if (res == TCL_OK) {
Tcl_ResetResult(interp);
if (Tcl_GetCharLength(verdict) == 0) {
// Accept a blank string as a pass
passed = 1;
} else {
if (Tcl_GetBooleanFromObj(interp, verdict, &passed) != TCL_OK) {
Tcl_ResetResult(interp);
passed = 0;
}
}
} else {
passed = 0;
Tcl_ResetResult(interp);
}
if (!passed) {
Tcl_SetErrorCode(interp, "PARSE_ARGS", "VALIDATION", Tcl_GetString(option->param), NULL);
THROW_PRINTF("Validation failed for \"%s\": %s",
Tcl_GetString(option->param),
Tcl_GetString(verdict)
);
}
}
return TCL_OK;
}
//>>>
/* Binding <<<
* bind_args matches the args against a compiled spec and hands each resulting
* value to a sink (bind_put), which stores it: as variables (the parse_args
* command), into a dict (parse_args with a dict var, ParseArgs_BindDict), to a
* C callback (ParseArgs_Bind), or converted into C slots (ParseArgs_BindSlots).
* alias is true when the value is the name of a variable in the caller to alias
* (an -alias option or positional that was supplied).
*/
typedef int (bind_put)(void* cx, Tcl_Interp* interp, struct option_info* option, Tcl_Obj* val, bool alias);
static int wrong_args(Tcl_Interp* interp, const struct parse_spec* spec) //<<<
{
/* No command name: parse_args can't know which one is right (a proc's args,
* the values resumed into a coroutine, a script's argv, ...), callers can
* add that context if they want it */
Tcl_WrongNumArgs(interp, 0, NULL, Tcl_GetString(spec->usage_msg));
return TCL_ERROR;
}
static int bind_args(Tcl_Interp* interp, struct interp_cx* l, struct parse_spec* spec, Tcl_Size ac, Tcl_Obj*const av[], bind_put* put, void* cx) //<<<
{
Tcl_Size i;
// Held, since a validator would otherwise free a fresh (refcount 0) list value
Tcl_Obj* val = NULL; defer { replace_tclobj(&val, NULL); }
// The spec must survive this bind, even if a validator shimmers the spec obj
spec_incref(spec);
defer { spec_decref(spec); }
/* Per-bind state (kept off the spec so that a validator can re-enter a
* command that uses the same spec): which options and multis were supplied,
* and the -all accumulators */
const size_t all_bytes = sizeof(Tcl_Obj*) * spec->all_count;
const size_t state_bytes = all_bytes + spec->option_count + spec->multi_count;
alignas(Tcl_Obj*) char stackbuf[256];
char* state = state_bytes <= sizeof(stackbuf) ? stackbuf : ckalloc(state_bytes);
memset(state, 0, state_bytes);
Tcl_Obj** all = (Tcl_Obj**)state;
bool* supplied = (bool*)(state + all_bytes);
bool* multi_supplied = supplied + spec->option_count;
defer {
for (int a=0; a<spec->all_count; a++)
replace_tclobj(&all[a], NULL);
if (state != stackbuf) ckfree(state);
}
#define OUTPUT_DIRECT(option, val) \
TEST_OK(put(cx, interp, (option), (val), false))
#define OUTPUT(option, val) \
do { \
if ((option)->all_idx >= 0) { \
Tcl_Obj** _acc = &all[(option)->all_idx]; \
if (*_acc == NULL) replace_tclobj(_acc, Tcl_NewListObj(1, NULL)); \
TEST_OK(Tcl_ListObjAppendElement(interp, *_acc, (val))); \
} else { \
OUTPUT_DIRECT((option), (val)); \
} \
} while(0)
#define OUTPUT_ALIASABLE(option, val) \
do { \
if ((option)->alias) { \
TEST_OK(put(cx, interp, (option), (val), true)); \
} else { \
OUTPUT((option), (val)); \
} \
} while(0)
#define VALIDATE(option, val) \
if ((option)->validator != NULL || (option)->enum_choices != NULL) { \
TEST_OK(validate(interp, (option), (val))); \
}
// Option phase: scan av[0..k) for options. Stop when:
// - we run out of args
// - the spec has no options at all