-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlistree.c
1306 lines (1140 loc) · 42.9 KB
/
listree.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
/*
* j2 - A simple concatenative programming language that can understand
* the structure of and dynamically link with compatible C binaries.
*
* Copyright (C) 2011 Jason Nyberg <jasonnyberg@gmail.com>
* Copyright (C) 2018 Jason Nyberg <jasonnyberg@gmail.com> (dual-licensed)
* (C) Copyright 2019 Hewlett Packard Enterprise Development LP.
*
* This file is part of j2.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or
*
* * the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or both in parallel, as here.
*
* j2 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 copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define __USE_GNU // strndupa, stpcpy
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "listree.h"
#include "reflect.h"
#include "compile.h"
#include "trace.h" // lttng
int show_ref=0;
int lti_count=0,ltvr_count=0,ltv_count=0;
//////////////////////////////////////////////////
// LisTree
//////////////////////////////////////////////////
// homebrew implementation of Arne Andersson's BST
// http://user.it.uu.se/~arnea/ps/simp.pdf
static LTI aa_sentinel={.lnk={&aa_sentinel,&aa_sentinel},.level=0};
int LTI_invalid(LTI *lti) { return lti==NULL || lti==&aa_sentinel; }
static void aa_rot(LTI **t,int dir) {
LTI *temp=(*t);
(*t)=(*t)->lnk[!dir];
temp->lnk[!dir]=(*t)->lnk[dir];
(*t)->lnk[dir]=temp;
}
static void aa_skew(LTI **t) {
if ((*t)->lnk[LEFT]->level==(*t)->level)
aa_rot(t,RIGHT);
}
static void aa_split(LTI **t) {
if ((*t)->lnk[RIGHT]->lnk[RIGHT]->level==(*t)->level) {
aa_rot(t,LEFT);
(*t)->level++;
}
}
static LTI *aa_most(LTI *t,int dir) {
if (t==&aa_sentinel)
return t;
LTI *lti=aa_most(t->lnk[dir],dir);
return lti==&aa_sentinel?t:lti;
}
// find "name" in "t"; walk until we hit target or find sentinel; optionally inserting there
static LTI *aa_find(LTI **t,char *name,int len,int *insert) {
LTI *lti=NULL;
int iter=(*insert)&ITER;
int dir=(*insert)&1;
if ((*t)==&aa_sentinel) // at edge... either insert or return NULL
lti=((*insert)&INSERT)?(*t)=LTI_init(NEW(LTI),name,len):NULL;
else {
int delta=0;
for (int i=0;delta==0 && i<PREVIEWLEN && i<len && i<(*t)->len;i++)
delta=name[i]-(*t)->preview[i];
if (!delta) // preview matched, compare full strings
delta=strnncmp(name,len,(*t)->name,(*t)->len);
int deltadir=delta<0?LEFT:RIGHT; // turn LTZ/Z/GTZ into left/right/right
if (delta) {
if (LTI_invalid(lti=aa_find(&(*t)->lnk[deltadir],name,len,insert)) && iter && dir!=deltadir)
lti=*t;
}
else if (iter) // matches, but find next smaller/larger
lti=aa_most((*t)->lnk[dir],!dir);
else // return match
lti=(*t),(*insert)=0;
}
if ((*insert)&INSERT) { // rebalance if necessary
aa_skew(t);
aa_split(t);
}
return lti;
}
// remove is non-aa-canonical...
// a: traverse down to leaf, looking for matches along the way
// b: if there's a match, clip leaf and return thru to matching node
// c: matching node swaps, and then returns matching node to caller
static LTI *aa_remove(LTI **t,char *name,int len,int match) {
LTI *result=NULL;
if (LTI_invalid(*t))
return &aa_sentinel;
// descend tree, finding matching node ("toremove") and then "next-greater" leaf ("tokeep")
int delta=0;
for (int i=0;delta==0 && i<PREVIEWLEN && i<len && i<(*t)->len;i++)
delta=name[i]-(*t)->preview[i];
if (!delta) // preview matched, compare full strings
delta=strnncmp(name,len,(*t)->name,(*t)->len);
result=aa_remove(&(*t)->lnk[delta<0?LEFT:RIGHT],name,len,match|!delta);
if (!delta) { // matching node
if (result!=&aa_sentinel) { // swap with leaf...
result->lnk[LEFT]=(*t)->lnk[LEFT];
result->lnk[RIGHT]=(*t)->lnk[RIGHT];
(*t)->lnk[LEFT]=&aa_sentinel;
(*t)->lnk[RIGHT]=&aa_sentinel;
}
LTI *newresult=(*t);
(*t)=result;
result=newresult;
} else if (match && result==&aa_sentinel) {
result=*t;
*t=&aa_sentinel;
}
cleanup:
// on the way back, re rebalance
if (!LTI_invalid(*t)) {
if (((*t)->lnk[LEFT]->level < ((*t)->level-1)) || ((*t)->lnk[RIGHT]->level < ((*t)->level-1))) {
(*t)->level--;
if ((*t)->lnk[RIGHT]->level > (*t)->level)
(*t)->lnk[RIGHT]->level = (*t)->level;
aa_skew(t);
aa_skew(&(*t)->lnk[RIGHT]);
aa_skew(&(*t)->lnk[RIGHT]->lnk[RIGHT]);
aa_split(t);
aa_split(&(*t)->lnk[RIGHT]);
}
}
done:
return result;
}
static void *aa_metamap(LTI **lti,LTI_METAOP op,int dir) {
int status=0;
void *rval=NULL;
if (LTI_invalid(*lti))
goto done;
int order=dir&1;
switch(dir&TREEDIR) {
case PREFIX: if ((rval=op(lti)) || (rval=aa_metamap(&(*lti)->lnk[ !order],op,dir)) || (rval=aa_metamap(&(*lti)->lnk[order],op,dir))) {} break;
case INFIX: if ((rval=aa_metamap(&(*lti)->lnk[ !order],op,dir)) || (rval=op(lti)) || (rval=aa_metamap(&(*lti)->lnk[order],op,dir))) {} break;
case POSTFIX: if ((rval=aa_metamap(&(*lti)->lnk[ !order],op,dir)) || (rval=aa_metamap(&(*lti)->lnk[order],op,dir)) || (rval=op(lti))) {} break;
}
done:
return rval;
}
static void *aa_map(LTI *lti,LTI_OP op,int dir)
{
void *metaop(LTI **lti) { op(*lti); }
return aa_metamap(<i,metaop,dir);
}
LTI *LTV_find(LTV *ltv,char *name,int len,int insert)
{
int status;
LTI *lti=NULL;
STRY(!ltv || !name || (ltv->flags<_LIST),"validating LTV_find parameters");
if (len==-1)
len=strlen(name);
insert=insert?INSERT:0; // true/false -> INSERT/0
lti=aa_find(<v->sub.ltis,name,len,&insert);
done:
return LTI_invalid(lti)?NULL:lti;
}
LTI *LTV_remove(LTV *ltv,char *name,int len)
{
LTI *result=ltv->flags<_LIST?NULL:aa_remove(<v->sub.ltis,name,len,false);
return result;
}
// release old data if present and configure with new data if present
LTV *LTV_renew(LTV *ltv,void *data,int len,LTV_FLAGS flags)
{
flags|=ltv->flags<_META; // need to preserve the original metaflags
if (ltv->data && (ltv->flags<_FREE) && !(ltv->flags<_NAP))
RELEASE(ltv->data);
ltv->len=(len<0 && !(flags<_NSTR))?(int) strlen((char *) data):len;
ltv->data=data;
if (flags<_DUP) ltv->data=bufdup(ltv->data,ltv->len);
if (flags<_ESC) strstrip(ltv->data,<v->len);
ltv->flags=flags;
return ltv;
}
// init and prepare for insertions
LTV *LTV_init(LTV *ltv,void *data,int len,LTV_FLAGS flags)
{
if (ltv && ((flags<_NAP) || data)) { // null ptr is error
ltv_count++;
ZERO(*ltv);
if (flags<_LIST)
CLL_init(<v->sub.ltvs);
else
ltv->sub.ltis=&aa_sentinel;
LTV_renew(ltv,data,len,flags);
}
TALLOC(ltv,sizeof(LTV),"LTV");
return ltv;
}
void LTV_free(LTV *ltv)
{
TDEALLOC(ltv,"LTV");
if (ltv) {
LTV_renew(ltv,NULL,0,0);
RELEASE(ltv);
ltv_count--;
}
}
void *LTV_map(LTV *ltv,int dir,LTI_OP lti_op,CLL_OP cll_op)
{
void *result=NULL;
if (ltv) {
if (ltv->flags<_LIST && cll_op)
result=CLL_map(<v->sub.ltvs,dir,cll_op);
else if (lti_op)
result=aa_map(ltv->sub.ltis,lti_op,dir|INFIX);
}
return result;
}
// get a new LTVR
LTVR *LTVR_init(LTVR *ltvr,LTV *ltv)
{
if (ltvr && ltv) {
ltvr_count++;
ZERO(*ltvr);
CLL_init(<vr->lnk);
ltvr->ltv=ltv;
ltv->refs++;
}
TALLOC(ltvr,sizeof(LTVR),"LTVR");
return ltvr;
}
LTV *LTVR_free(LTVR *ltvr)
{
LTV *ltv=NULL;
TDEALLOC(ltvr,"LTVR");
if (ltvr) {
if (!CLL_EMPTY(<vr->lnk)) { CLL_cut(<vr->lnk); }
if ((ltv=ltvr->ltv))
ltv->refs--;
RELEASE(ltvr);
ltvr_count--;
}
return ltv;
}
// get a new LTI and prepare for insertion
LTI *LTI_init(LTI *lti,char *name,int len)
{
if (lti==NULL)
lti=NEW(LTI);
if (lti && name) {
lti_count++;
ZERO(*lti);
lti->lnk[LEFT]=lti->lnk[RIGHT]=&aa_sentinel;
lti->level=1;
lti->len=len;
lti->name=bufdup(name,len);
for (int i=0;i<PREVIEWLEN;i++)
lti->preview[i]=len>i?name[i]:0;
CLL_init(<i->ltvs);
}
TALLOC(lti,sizeof(LTI),"LTI");
return lti;
}
void LTI_free(LTI *lti)
{
TDEALLOC(lti,"LTI");
if (lti) {
RELEASE(lti->name);
RELEASE(lti);
lti_count--;
}
}
//////////////////////////////////////////////////
// Tag Team of release methods for LT elements
//////////////////////////////////////////////////
void LTV_release(LTV *ltv)
{
void *op(LTI *lti) { LTI_release(lti); return (void *) NULL; }
if (ltv) TALLOC(ltv,ltv->refs,"LTV_release (ptr/refs)");
if (ltv && !(ltv->refs) && !(ltv->flags<_RO)) {
if (ltv->flags<_REFS) REF_delete(ltv); // cleans out REFS
else if (ltv->flags<_LIST) CLL_release(<v->sub.ltvs,LTVR_release);
else aa_map(ltv->sub.ltis,op,POSTFIX);
LTV_free(ltv);
}
}
void LTVR_release(CLL *lnk) { LTV_release(LTVR_free((LTVR *) lnk)); }
void LTI_release(LTI *lti) {
if (lti) {
CLL_release(<i->ltvs,LTVR_release);
LTI_free(lti);
}
}
//////////////////////////////////////////////////
// Tag Team of traverse methods for LT elements
//////////////////////////////////////////////////
// add to preop to avoid repeat visits in listree traverse
void *listree_acyclic(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
if ((*flags<_TRAVERSE_LTV) && (*ltv)->flags&(LT_AVIS|LT_RVIS))
*flags|=LT_TRAVERSE_HALT;
return (*flags)<_TRAVERSE_HALT?NON_NULL:NULL;
}
void *listree_traverse(CLL *ltvs,LTOBJ_OP preop,LTOBJ_OP postop)
{
int depth=0,cleanup=0;
void *rval=NULL;
void *descend_ltv(LTI *parent_lti,LTVR *parent_ltvr,LTV *ltv) {
void *descend_lti(LTI *lti) {
void *descend_ltvr(CLL *lnk) { // for normal-form (ltv->lti->ltvr) form ltv
LTVR *ltvr=(LTVR *) lnk;
return descend_ltv(lti,ltvr,ltvr->ltv);
}
if (!lti) goto done;
LTV *parent=ltv;
LT_TRAVERSE_FLAGS flags=LT_TRAVERSE_LTI;
if (cleanup) CLL_map(<i->ltvs,FWD,descend_ltvr);
else if (preop && (rval=preop(<i,NULL,&parent,depth,&flags)) ||
((flags<_TRAVERSE_HALT) || (rval=CLL_map(<i->ltvs,(flags<_TRAVERSE_REVERSE)?REV:FWD,descend_ltvr))) ||
postop && ((flags|=LT_TRAVERSE_POST),(rval=postop(<i,NULL,&parent,depth,&flags))))
goto done;
done:
return rval;
}
void *descend_ltvr(CLL *lnk) { // for list-form ltv
LTVR *ltvr=(LTVR *) lnk;
return descend_ltv(NULL,ltvr,ltvr->ltv); // LTI is null in this case
}
if (!ltv) goto done;
LTI *child=parent_lti;
LT_TRAVERSE_FLAGS flags=LT_TRAVERSE_LTV;
if (cleanup) // only descends (and cleans up) LTVs w/absolute visited flag
return (ltv->flags<_AVIS && !((ltv->flags&=~LT_AVIS)<_AVIS) && !(ltv->flags<_REFS))? LTV_map(ltv,FWD,descend_lti,descend_ltvr):NULL;
else {
if (preop && (rval=preop(&child,parent_ltvr,<v,depth,&flags))) goto done;
if ((flags<_TRAVERSE_HALT) || (ltv->flags<_REFS)) goto done;
ltv->flags|=LT_RVIS;
depth++;
rval=(child!=parent_lti)?descend_lti(child):LTV_map(ltv,(flags<_TRAVERSE_REVERSE)?REV:FWD,descend_lti,descend_ltvr);
depth--;
ltv->flags&=~LT_RVIS;
if (rval) goto done;
flags|=LT_TRAVERSE_POST;
if (postop && ((rval=postop(&parent_lti,parent_ltvr,<v,depth,&flags)))) goto done;
}
done:
if (ltv) ltv->flags|=LT_AVIS;
return rval;
}
TSTART(0,"listree_traverse");
void *traverse(CLL *lnk) { LTVR *ltvr=(LTVR *) lnk; return descend_ltv(NULL,ltvr,ltvr->ltv); }
rval=CLL_map(ltvs,FWD,traverse);
cleanup=1;
preop=postop=NULL;
CLL_map(ltvs,FWD,traverse); // clean up "visited" flags
TFINISH(rval!=0,"listree_traverse");
return rval;
}
void *ltv_traverse(LTV *ltv,LTOBJ_OP preop,LTOBJ_OP postop)
{
CLL ltvs;
CLL_init(<vs);
LTV_enq(<vs,ltv,HEAD);
void *result=listree_traverse(<vs,preop,postop);
LTV_deq(<vs,HEAD);
return result;
}
//////////////////////////////////////////////////
// Basic LT insert/remove
//////////////////////////////////////////////////
extern LTI *LTI_first(LTV *ltv) { return (!ltv || (ltv->flags<_LIST))?NULL:aa_most(ltv->sub.ltis,LEFT); }
extern LTI *LTI_last(LTV *ltv) { return (!ltv || (ltv->flags<_LIST))?NULL:aa_most(ltv->sub.ltis,RIGHT); }
extern LTI *LTI_iter(LTV *ltv,LTI *lti,int dir) { dir|=ITER; return ltv&&!LTI_invalid(lti)? aa_find(<v->sub.ltis,lti->name,lti->len,&dir):NULL; }
LTI *LTI_lookup(LTV *ltv,LTV *name,int insert)
{
LTI *lti=NULL;
TLOOKUP(ltv,name->data,name->len,insert);
if (LTV_wildcard(name))
for (lti=LTI_first(ltv); !LTI_invalid(lti) && fnmatch_len(name->data,name->len,lti->name,-1); lti=LTI_iter(ltv,lti,FWD));
else
lti=LTV_find(ltv,name->data,name->len,insert);
done:
return lti;
}
LTI *LTI_find(LTV *ltv,char *name,int insert,int flags)
{
LTV *nameltv=LTV_init(NEW(LTV),name,-1,flags);
LTI *lti=LTI_lookup(ltv,nameltv,insert);
LTV_free(nameltv);
return lti;
}
LTI *LTI_resolve(LTV *ltv,char *name,int insert) { return LTV_find(ltv,name,-1,insert); }
int LTV_empty(LTV *ltv)
{
if (!ltv) return true;
else if (ltv->flags<_LIST) return CLL_EMPTY(<v->sub.ltvs);
else return LTI_invalid(ltv->sub.ltis);
}
LTV *LTV_put(CLL *ltvs,LTV *ltv,int end,LTVR **ltvr_ret)
{
int status=0;
LTVR *ltvr=NULL;
if (ltv && ltvs && (ltvr=LTVR_init(NEW(LTVR),ltv))) {
if (CLL_put(ltvs,<vr->lnk,end)) {
if (ltvr_ret) *ltvr_ret=ltvr;
return ltv; //!!
}
else LTVR_free(ltvr);
}
return NULL;
}
LTV *LTV_get(CLL *ltvs,int pop,int dir,LTV *match,LTVR **ltvr_ret)
{
void *ltv_match(CLL *lnk) {
LTVR *ltvr=(LTVR *) lnk;
if (!ltvr || !ltvr->ltv || ltvr->ltv->flags<_NAP || fnmatch_len(ltvr->ltv->data,ltvr->ltv->len,match->data,match->len)) return (void *) NULL;
else return (void *) lnk;
}
LTVR *ltvr=NULL;
LTV *ltv=NULL;
if (!(ltvr=(LTVR *) match?
CLL_mapfrom(ltvs,((ltvr_ret && (*ltvr_ret))?&(*ltvr_ret)->lnk:NULL),dir,ltv_match):
CLL_next(ltvs,(ltvr_ret && (*ltvr_ret))?&(*ltvr_ret)->lnk:NULL,dir)))
goto done;
ltv=ltvr->ltv;
if (pop) {
CLL_cut(<vr->lnk);
LTVR_free(ltvr);
ltvr=NULL;
}
done:
if (ltvr_ret) (*ltvr_ret)=ltvr;
return ltv;
}
// extract and release an lti from an ltv
void LTV_erase(LTV *ltv,LTI *lti) { if (ltv && lti) LTI_release(LTV_remove(ltv,lti->name,lti->len)); }
LTV *LTV_dup(LTV *ltv)
{
if (!ltv) return NULL;
int flags=ltv->flags & ~LT_NDUP;
if (!(flags<_NAP))
flags |= LT_DUP;
return LTV_init(NEW(LTV),ltv->data,ltv->len,flags);
}
LTV *LTV_copy(LTV *ltv,int maxdepth)
{
LTV *index=LTV_NULL,*dupes=LTV_NULL;
char buf[32];
int index_ltv(LTV *ltv) {
LTV *dup=NULL;
sprintf(buf,"%p",ltv);
if (!LT_get(index,buf,HEAD,KEEP)) {
LT_put(index,buf,HEAD,ltv);
if (!(ltv->flags&(LT_CVAR|LT_REFS)))
dup=LTV_dup(ltv);
if (dup)
LT_put(dupes,buf,HEAD,LTV_dup(ltv));
}
}
void *index_ltvs(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
if (!((*flags)<_TRAVERSE_HALT) && ((*flags)<_TRAVERSE_LTV) && depth<=maxdepth) {
index_ltv(*ltv);
if ((*ltv)->flags&(LT_CVAR|LT_REFS))
(*flags)|=LT_TRAVERSE_HALT;
}
if (depth==maxdepth)
(*flags)|=LT_TRAVERSE_HALT;
return (void *) NULL;
}
ltv_traverse(ltv,index_ltvs,NULL)!=NULL,LTV_NULL;
LTV *new_or_used(LTV *ltv) {
char buf[32];
sprintf(buf,"%p",ltv);
LTV *rval=NULL;
return ((rval=LT_get(dupes,buf,HEAD,KEEP)))?rval:ltv;
}
void *descend_lti(LTI *lti) {
LTV *orig=LTV_peek(<i->ltvs,HEAD);
LTV *dupe=LT_get(dupes,lti->name,HEAD,KEEP);
void *copy_children(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
if (((*flags)<_TRAVERSE_LTV) && depth==1 && (*lti)) {
LT_put(dupe,(*lti)->name,TAIL,new_or_used(*ltv));
(*flags)|=LT_TRAVERSE_HALT;
}
return (void *) NULL;
}
ltv_traverse(orig,copy_children,NULL)!=NULL,LTV_NULL;
done:
return (void *) NULL;
}
LTV_map(index,FWD,descend_lti,NULL);
LTV *result=new_or_used(ltv);
LTV_release(index);
LTV_release(dupes);
done:
return result;
}
LTV *LTV_concat(LTV *a,LTV *b)
{
int status=0;
STRY(!a || !b,"validating args");
char *buf=NULL;
LTV *ltv=NULL;
STRY(!(buf=mymalloc(a->len+b->len)),"validating buf allocation");
STRY(!(ltv=LTV_init(NEW(LTV),buf,a->len+b->len,LT_OWN)),"validating ltv allocation");
strncpy(buf,a->data,a->len);
strncpy(buf+a->len,b->data,b->len);
done:
return status?NULL:ltv;
}
int LTV_wildcard(LTV *ltv)
{
if (ltv->flags<_NOWC)
return false;
int tlen=series(ltv->data,ltv->len,NULL,"*?",NULL);
return tlen < ltv->len;
}
int LTI_hide(LTI *lti)
{
int len=strlen(lti->name);
return series(lti->name,len,NULL," ",NULL)<len && !show_ref;
}
int LTV_hide(LTV *ltv) { return !show_ref && ((ltv->flags<_TYPE)); }
void print_ltvs(FILE *ofile,char *pre,CLL *ltvs,char *post,int maxdepth)
{
void *preop(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
switch ((*flags)<_TRAVERSE_TYPE) {
case LT_TRAVERSE_LTI:
if (maxdepth && depth>=maxdepth || LTI_hide(*lti))
(*flags)|=LT_TRAVERSE_HALT;
else
fprintf(ofile,"%*c\"%s\"\n",MAX(0,depth*4-2),' ',(*lti)->name);
break;
case LT_TRAVERSE_LTV:
if (pre) fprintf(ofile,"%*c%s",depth*4,' ',pre);
else fprintf(ofile,"%*c[",depth*4,' ');
if ((*ltv)->flags<_REFS) { REF_printall(ofile,(*ltv),"REFS:\n"); fstrnprint(ofile,(*ltv)->data,(*ltv)->len); }
else if ((*ltv)->flags<_BC) disassemble(ofile,(*ltv));
else if ((*ltv)->flags<_CVAR) cif_print_cvar(ofile,(*ltv),depth);
else if ((*ltv)->flags<_IMM) fprintf(ofile,"IMM 0x%x",(*ltv)->data);
else if ((*ltv)->flags<_NULL) fprintf(ofile,"<null>");
else if ((*ltv)->flags<_BIN) hexdump(ofile,(*ltv)->data,(*ltv)->len);
else fstrnprint(ofile,(*ltv)->data,(*ltv)->len);
if (post) fprintf(ofile,"%s",post);
else fprintf(ofile,"]\n");
if (((*flags)<_TRAVERSE_HALT)) // already visited
fprintf(ofile,"%*c (subtree omitted...)\n",MAX(0,depth*4-2),' ');
if (LTV_hide(*ltv))
(*flags)|=LT_TRAVERSE_HALT;
break;
default:
break;
}
done:
return (void *) NULL;
}
if (ltvs)
listree_traverse(ltvs,preop,NULL);
else
fprintf(ofile,"NULL");
fflush(ofile);
}
void print_ltv(FILE *ofile,char *pre,LTV *ltv,char *post,int maxdepth)
{
CLL ltvs;
CLL_init(<vs);
LTV_enq(<vs,ltv,HEAD);
print_ltvs(ofile,pre,<vs,post,maxdepth);
LTV_deq(<vs,HEAD);
}
void ltvs2dot(FILE *ofile,CLL *ltvs,int maxdepth,char *label) {
void *lnk2dot(CLL *lnk) {
fprintf(ofile,"\"%x\" [label=\"\" shape=point color=brown penwidth=2.0]\n",lnk);
if (lnk->lnk[TAIL]!=lnk)
fprintf(ofile,"\"%x\" -> \"%x\" [color=brown penwidth=2.0]\n",lnk->lnk[TAIL],lnk);
return (void *) NULL;
}
void *ltvr2dot(CLL *lnk) {
LTVR *ltvr=(LTVR *) lnk;
fprintf(ofile,"\"%x\" -> \"LTV%x\" [color=purple len=0.1]\n",lnk,ltvr->ltv);
return lnk2dot(lnk);
}
void cll2dot(CLL *cll,char *label) {
if (label)
fprintf(ofile,"\"%1$s_%2$x\" [label=\"%1$s\" shape=ellipse style=filled fillcolor=gray]\n\"%1$s_%2$x\" -> \"%2$x\"\n",label,cll);
lnk2dot(cll);
}
void lti2dot(LTV *ltv,LTI *lti) {
fprintf(ofile,"\"LTI%x\" [label=\"%s\" shape=ellipse]\n",lti,lti->name);
fprintf(ofile,"\"LTV%x\" -> \"LTI%x\" [color=blue]\n",ltv,lti);
fprintf(ofile,"\"LTI%x\" -> \"%x\"\n",lti,<i->ltvs);
cll2dot(<i->ltvs,NULL);
}
void lti_ltvr2dot(LTI *lti,LTVR *ltvr) { ltvr2dot(<vr->lnk); }
void ltv_ltvr2dot(LTV *ltv,LTVR *ltvr) { ltvr2dot(<vr->lnk); }
void ltv_descend(LTV *ltv) {
if (!LTV_empty(ltv)) {
if (ltv->flags<_LIST) {
if (ltv->flags<_REFS)
REF_dot(ofile,ltv,"REFS");
fprintf(ofile,"\"LTV%x\" -> \"%x\" [color=blue]\n",ltv,<v->sub.ltvs);
// cll2dot(<v->sub.ltvs,NULL);
} else {
fprintf(ofile,"subgraph cluster_%x { subgraph { /*rank=same*/\n",ltv);
for (LTI *lti=LTI_first(ltv);!LTI_invalid(lti);lti=LTI_iter(ltv,lti,FWD))
fprintf(ofile,"\"LTI%x\"\n",lti);
fprintf(ofile,"}}\n");
fprintf(ofile,"\"LTV%x\" -> \"LTI%x\" [color=blue]\n",ltv,ltv->sub.ltis);
}
}
}
void ltv2dot(LTVR *ltvr,LTV *ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
char *color=ltv->flags<_RO?"red":"black";
if (ltv->len && !(ltv->flags<_NSTR)) {
fprintf(ofile,"\"LTV%x\" [shape=box style=filled fillcolor=lightsteelblue color=%s label=\"",ltv,color);
fstrnprint(ofile,ltv->data,ltv->len);
fprintf(ofile,"\"]\n");
}
else if (ltv->flags<_REFS)
fprintf(ofile,"\"LTV%x\" [label=\"REFS(%x)\" shape=box style=filled fillcolor=yellow color=%s]\n",ltv,ltv->data,color),
REF_dot(ofile,ltv,"REFS");
else if (ltv->flags<_CVAR)
cif_dot_cvar(ofile, ltv); // invoke reflection, customize label
else if (ltv->flags<_IMM)
fprintf(ofile,"\"LTV%x\" [label=\"%x (imm)\" shape=box style=filled fillcolor=gold color=%s]\n",ltv,ltv->data,color);
else if (ltv->flags<_NULL)
fprintf(ofile,"\"LTV%x\" [label=\"\" shape=point style=filled fillcolor=purple fillcolor=pink color=%s]\n",ltv,color);
else
fprintf(ofile,"\"LTV%x\" [label=\"\" shape=box style=filled height=.1 width=.3 fillcolor=gray color=%s]\n",ltv,color);
}
void *preop(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
if ((*flags)<_TRAVERSE_LTI) {
if (maxdepth && depth>=maxdepth || LTI_hide(*lti))
*flags|=LT_TRAVERSE_HALT;
else
lti2dot(*ltv,*lti);
} else if ((*flags)<_TRAVERSE_LTV) {
if (*lti)
lti_ltvr2dot(*lti,ltvr);
else if (ltvr)
ltv_ltvr2dot(*ltv,ltvr);
if (!((*flags)<_TRAVERSE_HALT)) {
ltv2dot(ltvr,*ltv,depth,flags);
if (LTV_hide(*ltv))
*flags|=LT_TRAVERSE_HALT;
else
ltv_descend(*ltv);
}
}
return (void *) NULL;
}
cll2dot(ltvs,label);
CLL_map(ltvs,FWD,ltvr2dot);
listree_traverse(ltvs,preop,NULL);
}
void ltvs2dot_simple(FILE *ofile,CLL *ltvs,int maxdepth,char *label) {
int i=0;
void lti2dot(LTV *ltv,LTI *lti) {
fprintf(ofile,"\"LTI%x\" [label=\"%s\" shape=ellipse color=red4]\n",lti,lti->name);
fprintf(ofile,"\"LTV%x\" -> \"LTI%x\" [color=blue]\n",ltv,lti);
}
void lti_ltvr2dot(LTI *lti,LTVR *ltvr) { fprintf(ofile,"\"LTI%x\" -> \"LTV%x\" [color=purple]\n",lti,ltvr->ltv); }
void ltv_ltvr2dot(LTV *ltv,LTVR *ltvr) { fprintf(ofile,"\"LTV%x\" -> \"LTV%x\" [color=red]\n",ltv,ltvr->ltv); }
void ltv_descend(LTV *ltv) {
if (!LTV_empty(ltv) && !(ltv->flags<_LIST)) {
fprintf(ofile,"subgraph cluster_%d { subgraph { /*rank=same*/\n",i++);
for (LTI *lti=LTI_first(ltv);!LTI_invalid(lti);lti=LTI_iter(ltv,lti,FWD))
fprintf(ofile,"\"LTI%x\"\n",lti);
fprintf(ofile,"}}\n");
}
}
void ltv2dot(LTVR *ltvr,LTV *ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
char *color = ltv->flags & LT_RO ? "red" : "black";
if (ltv->len && !(ltv->flags & LT_NSTR)) {
fprintf(ofile,"\"LTV%x\" [shape=box style=filled color=%s fillcolor=tan label=\"", ltv, color);
fstrnprint(ofile,ltv->data,ltv->len);
fprintf(ofile,"\"]\n");
} else if (ltv->flags & LT_REFS)
fprintf(ofile, "\"LTV%x\" [label=\"REFS(%x)\" shape=box style=filled color=%s]\n", ltv, ltv->data, color),
REF_dot(ofile, ltv, "REFS");
else if (ltv->flags<_CVAR)
cif_dot_cvar(ofile, ltv); // invoke reflection, customize label
else if (ltv->flags<_IMM)
fprintf(ofile, "\"LTV%x\" [label=\"I(%x)\" shape=box style=filled color=%s]\n", ltv, ltv->data, color);
else if (ltv->flags==LT_NULL)
fprintf(ofile, "\"LTV%x\" [label=\"\" shape=point style=filled color=%s]\n", ltv, color);
else
fprintf(ofile, "\"LTV%x\" [label=\"\" shape=box style=filled color=%s height=.1 width=.3]\n", ltv, color);
}
void *preop(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
if ((*flags)<_TRAVERSE_LTI) {
if (maxdepth && depth>=maxdepth || LTI_hide(*lti))
*flags|=LT_TRAVERSE_HALT;
else
lti2dot(*ltv,*lti);
} else if ((*flags)<_TRAVERSE_LTV) {
if (*lti)
lti_ltvr2dot(*lti,ltvr);
else if (ltvr)
ltv_ltvr2dot(*ltv,ltvr);
if (!((*flags)<_TRAVERSE_HALT)) {
ltv2dot(ltvr,*ltv,depth,flags);
if (LTV_hide(*ltv))
*flags|=LT_TRAVERSE_HALT;
else
ltv_descend(*ltv);
}
}
return (void *) NULL;
}
listree_traverse(ltvs,preop,NULL);
}
void graph_ltvs(FILE *ofile,CLL *ltvs,int maxdepth,char *label) {
fprintf(ofile,"digraph iftree\n{\ngraph [rankdir=\"LR\", ratio=compress, concentrate=true] node [shape=record] edge []\n");
ltvs2dot_simple(ofile,ltvs,maxdepth,label);
fprintf(ofile,"}\n");
}
void graph_ltvs_to_file(char *filename,CLL *ltvs,int maxdepth,char *label) {
FILE *ofile=fopen(filename,"w");
graph_ltvs(ofile,ltvs,maxdepth,label);
fclose(ofile);
}
void graph_ltv_to_file(char *filename,LTV *ltv,int maxdepth,char *label) {
CLL ltvs;
CLL_init(<vs);
LTV_enq(<vs,ltv,HEAD);
graph_ltvs_to_file(filename,<vs,maxdepth,label);
LTV_deq(<vs,HEAD);
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
int pickle_ltvs(FILE *ofile,char *pre,CLL *ltvs,char *post,int maxdepth)
{
void *write_data(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
switch ((*flags)<_TRAVERSE_TYPE) {
case LT_TRAVERSE_LTI:
if (maxdepth && depth>=maxdepth || LTI_hide(*lti))
(*flags)|=LT_TRAVERSE_HALT;
else
fprintf(ofile,"%*c\"%s\"\n",MAX(0,depth*4-2),' ',(*lti)->name);
break;
case LT_TRAVERSE_LTV:
if (pre) fprintf(ofile,"%*c%s",depth*4,' ',pre);
else fprintf(ofile,"%*c[",depth*4,' ');
if ((*ltv)->flags<_REFS) { REF_printall(ofile,(*ltv),"REFS:\n"); fstrnprint(ofile,(*ltv)->data,(*ltv)->len); }
else if ((*ltv)->flags<_BC) disassemble(ofile,(*ltv));
else if ((*ltv)->flags<_CVAR) cif_print_cvar(ofile,(*ltv),depth);
else if ((*ltv)->flags<_IMM) fprintf(ofile,"IMM 0x%x",(*ltv)->data);
else if ((*ltv)->flags<_NULL) fprintf(ofile,"<null>");
else if ((*ltv)->flags<_BIN) hexdump(ofile,(*ltv)->data,(*ltv)->len);
else fstrnprint(ofile,(*ltv)->data,(*ltv)->len);
if (post) fprintf(ofile,"%s",post);
else fprintf(ofile,"]\n");
if (LTV_hide(*ltv))
(*flags)|=LT_TRAVERSE_HALT;
break;
default:
break;
}
done:
return (void *) NULL;
}
void *write_links(LTI **lti,LTVR *ltvr,LTV **ltv,int depth,LT_TRAVERSE_FLAGS *flags) {
listree_acyclic(lti,ltvr,ltv,depth,flags);
switch ((*flags)<_TRAVERSE_TYPE) {
case LT_TRAVERSE_LTI:
if (maxdepth && depth>=maxdepth || LTI_hide(*lti))
(*flags)|=LT_TRAVERSE_HALT;
else
fprintf(ofile,"%*c\"%s\"\n",MAX(0,depth*4-2),' ',(*lti)->name);
break;
case LT_TRAVERSE_LTV:
if (pre) fprintf(ofile,"%*c%s",depth*4,' ',pre);
else fprintf(ofile,"%*c[",depth*4,' ');
if ((*ltv)->flags<_REFS) { REF_printall(ofile,(*ltv),"REFS:\n"); fstrnprint(ofile,(*ltv)->data,(*ltv)->len); }
else if ((*ltv)->flags<_BC) disassemble(ofile,(*ltv));
else if ((*ltv)->flags<_CVAR) cif_print_cvar(ofile,(*ltv),depth);
else if ((*ltv)->flags<_IMM) fprintf(ofile,"IMM 0x%x",(*ltv)->data);
else if ((*ltv)->flags<_NULL) fprintf(ofile,"<null>");
else if ((*ltv)->flags<_BIN) hexdump(ofile,(*ltv)->data,(*ltv)->len);
else fstrnprint(ofile,(*ltv)->data,(*ltv)->len);
if (post) fprintf(ofile,"%s",post);
else fprintf(ofile,"]\n");
break;
default:
break;
}
done:
return (void *) NULL;
}
listree_traverse(ltvs,write_data,NULL);
listree_traverse(ltvs,write_links,NULL);
fflush(ofile);
}
int pickle_ltv(FILE *ofile,char *pre,LTV *ltv,char *post,int maxdepth)
{
CLL ltvs;
CLL_init(<vs);
LTV_enq(<vs,ltv,HEAD);
pickle_ltvs(ofile,pre,<vs,post,maxdepth);
LTV_deq(<vs,HEAD);
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
CLL *LTV_list(LTV *ltv) { return (ltv && ltv->flags<_LIST)? <v->sub.ltvs:NULL; }
LTV *LTV_enq(CLL *ltvs,LTV *ltv,int end) { return LTV_put((ltvs),(ltv),(end),NULL); }
LTV *LTV_deq(CLL *ltvs,int end) { return LTV_get((ltvs),POP,(end),NULL,NULL); }
LTV *LTV_peek(CLL *ltvs,int end) { return LTV_get((ltvs),KEEP,(end),NULL,NULL); }
LTV *LT_put(LTV *parent,char *name,int end,LTV *child) {
if (parent && name && child) {
LTI *lti=LTI_resolve(parent,name,true);
return lti?LTV_enq(<i->ltvs,child,end):NULL;
}
else
return NULL;
}
LTV *LT_get(LTV *parent,char *name,int end,int pop) {
if (parent && name) {
LTI *lti=LTI_resolve(parent,name,false);
return lti?(pop?LTV_deq(<i->ltvs,end):LTV_peek(<i->ltvs,end)):NULL;
}
else
return NULL;
}
//////////////////////////////////////////////////
// REF
// special case; client creates ref, which acts as sentinel
//////////////////////////////////////////////////
int ref_count=0;
REF *REF_HEAD(LTV *ltv) { return ((REF *) CLL_HEAD(<v->sub.ltvs)); }
REF *REF_TAIL(LTV *ltv) { return ((REF *) CLL_TAIL(<v->sub.ltvs)); }
REF *REF_next(LTV *ltv,REF *ref) { return ((REF *) CLL_next(<v->sub.ltvs,&ref->lnk,HEAD)); }
REF *refpush(CLL *cll,REF *ref) { return (REF *) CLL_put(cll,&ref->lnk,HEAD); }
REF *refpop(CLL *cll) { return (REF *) CLL_get(cll,POP,HEAD); }
LTV *REF_root(REF *ref) { return ref?LTV_peek(&ref->root,HEAD):NULL; }
REF *REF_init(REF *ref,char *data,int len)
{
int quote=(len>1 && data[0]=='\'' && data[len-1]=='\'');
if (quote) {
data+=1;
len-=2;
}
int rev=data[0]=='-';
if (len-rev==0)
return NULL;
if (ref && CLL_init(&ref->lnk))
{
CLL_init(&ref->keys);
LTV_enq(&ref->keys,LTV_init(NEW(LTV),data+rev,len-rev,LT_DUP|(quote?LT_NOWC:LT_ESC)),HEAD);
CLL_init(&ref->root);
ref->lti=NULL;
ref->ltvr=NULL;
ref->cvar=NULL;
ref->reverse=rev;
ref_count++;
}