-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_kdb
2785 lines (2781 loc) · 88.4 KB
/
.bash_kdb
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
#!/usr/bin/env bash
# kdb mvp edition 660f 20240404
#
# Benevolent Collaboration and Professional Use (BCPU) License
#
# Copyright (C) 2015-2024 George Georgalis <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to use, copy, modify, and/or merge copies of the Software for either
# personal use or to enhance their performance at their place of employment,
# subject to the following conditions:
#
# 1) The above copyright notice and this permission notice
# shall be included in all copies, substantial portions, or
# redistributions of the Software, as well as any derived
# algorithms, data structures, workflows, user interfaces,
# or functional components.
#
# 2) This license does not apply to any data managed by
# the Software, which remains under the copyright of its
# respective owners.
#
# 3) If the Software is deployed as a service or if multiple
# users share the same data store, a multi-user license must
# be obtained from the copyright holder.
#
# 4) The Software will enter the public domain 10 years
# after the date of its last revision, at which point
# this license will no longer apply, and the Software
# may be used, modified, and distributed freely without
# restriction.
#
# A separate collaboration license is available free of charge, and includes
# documentation, and update notifications. Multi-user licenses for commercial
# use are commission-based. Request a license from the copyright holder.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#ks knowledge motive curation query cache pointer db
# 5b9402df %ks curation database cdb (kdb lql) spec
# 5b6cc5aa %ks k4db prefix for development
# 5b56228b %ks design kdb attachments user guide, template spec
# 5afb26f0 %ks mesh chatter (coprocedures)
# 5ad27718 %ks knowledgendian kdb lql spec
# 5a8c6db4 %ks period scheduler and task supervisor spec
# 59dab11b %ks sub kdb idk tools shell functions scraps hex tables
# 59d94184 %ks isbe k4 schema, LQL, knowledge database, k4db Literate Query Language spec
# 59c7febc %ks isbe k4 markdown mermaid mmd spec
# 59bc5f4a %ks isbe kdb tables, types, path, files hist
# 20150807 the only epoch ever used for intervals
export LC_ALL="C"
export LANG="en_US.US-ASCII"
export verb="${verb:=chkwrn}"
export vverb="${vverb:=devnul}"
export vvverb="${vvverb:=devnull}"
export verbb=devnul
export kdb_verb="${verb?=devnul}"
export kdb_verbb="${verbb?=devnul}"
export kdb_verbbb='echo "${FUNCNAME[@}"'
export base32re="[$(seq 0 31 | while read a ; do base 32 $a ; done | tr -d '\n')]"
export base32rep="[/.$(seq 0 31 | while read a ; do base 32 $a ; done | tr -d '\n')]"
export kdb_log=','
export kdb_tmp='%'
export kdb_var='v'
export kdb_pool="p/${USER}"
export kdb_cache='c'
export k4_ht_strftime="+%Y%m%d %a %H%M %Z %z"
export k4_search='^%(ks|kd|kt|kk|km)\ '
export k4_graph='^%(kb|kc|kj|kl|km|kp|kr|kk)\ '
export k4_value='^%(ke|ki|km|kq|kv|kx|kz)\ '
export k4_topic="^%(ks|kd|kt|kk)\ "
export kdb_vptr="k4mi"
export kdb_locksleep="3"
export kdbeph_mask=5
export kdblog="kdblog"
export kdblog2="devnul"
export kdbo="${USER}@$(hostname -s):$(sed "s;^$HOME/;;" <<<$kdb)"
export k4_ko="${USER}@$(hostname -s)"
[ "$(which "$EDITOR" 2>/dev/null)" ] || export EDITOR="vi"
[ "$OS" ] || export OS="$(uname)"
export _kdb_fn_trace='_kdb_fn_trace'
export _kdb_fn_entry='_kdb_log'
export _kdb_fn_error='_kdb_logerr'
export _kdb_fn_warn='_kdb_logwrn'
export _kdb_fn_exit='_kdb_nil'
export _kdb_log='_kdb_log'
_kdb_0_lock ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
local fn="$(kdb_0_flaten_filename "$*")";
local vn="${kdb}/0/v/${fn}";
local arg="${vn}.arg";
local lock="${vn}.pid";
find "${kdb}/0/v" -type f -name \*.pid -mtime +1 2> /dev/null | while IFS= read a; do
$_kdb_log "$FUNCNAME : stale $a (62f16771)";
done;
[ -e "$lock" ] && {
$_kdb_fn_error "x : $invoke";
return 1
} || {
echo "$*" > "$arg";
echo "$$" > "$lock"
}
}
_kdb_0_lockun ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c8981;
local fn="$(kdb_0_flaten_filename "$*")";
local vn="${kdb}/0/v/${fn}";
local arg="${vn}.arg";
local lock="${vn}.pid";
rm -i -f "$lock";
local e="$?";
echo "$*" > "$arg";
return "$e"
}
_kdb_0_lockwait ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
local fn="$(kdb_0_flaten_filename "$*")";
local vn="${kdb}/0/v/${fn}";
local arg="${vn}.arg";
local lock="${vn}.pid";
while [ -e "$lock" ]; do
chkwrn "$lock";
find "$lock" -mmin +2 -exec rm \{\} \; 2> /dev/null && $_kdb_log "x $invoke : lock sleep 2min $lock" || true;
$_kdb_log "= $invoke : (6563f474) lock $lock";
sleep "$kdb_locksleep";
done;
_kdb_0_lock $@ || {
sleep 2;
$FUNCNAME $@
}
}
_kdb_chkerr ()
{
[ "$*" ] && {
_kdb_stderr ">>> $*";
return $?
} || true
}
_kdb_chkstd ()
{
[ "$*" ] && echo "$*" || true
}
_kdb_chktrue ()
{
[ "$*" ] && {
_kdb_stderr "><> $*";
true
} || return 2
}
_kdb_chkwrn ()
{
[ "$*" ] && {
_kdb_stderr "^^^ $*";
return $?
} || true
}
_kdb_file_headg ()
{
local e="$1" f fs a _k4;
shift || true;
[ $# -gt 0 ] && fs="$1";
shift;
[ $# -gt 0 ] && while [ $# -gt 0 ]; do
fs="$(printf "%s\n%s\n" "$fs" "$1")";
shift;
done;
[ "$fs" ] || fs="$(cat)";
echo "$fs" | while IFS= read _k4; do
awk 'NR==1,/^$/' "$_k4" | awk '!/^$/' | grep -E "$e";
done
}
_kdb_file_headv ()
{
local e="$1" k4="$2";
awk 'NR==1,/^$/' "$k4" | awk '!/^$/' | grep -vE "$e"
}
_kdb_fn ()
{
_kdb_chktrue "fn:${FUNCNAME[1]}@${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
}
_kdb_fn_stack ()
{
sed "s/^$FUNCNAME /fn: /" <<< "${FUNCNAME[@]}"
}
_kdb_fn_trace ()
{
local a;
for ((a=1; a<${#FUNCNAME[@]}; a++ ))
do
_kdb_chktrue ${FUNCNAME[$a]}@${BASH_SOURCE[$a]}:${BASH_LINENO[$a-1]};
done
}
_kdb_help_skel ()
{
echo ' eval "$(curl -fsSL --insecure https://raw.githubusercontent.com/georgalis/pub/master/skel/.profile)"' 1>&2;
echo ' export -f devnul stderr chkstd chkwrn logwrn chkerr logerr chktrue chkexit logexit siff siffx validfn' 1>&2
}
_kdb_help_sub ()
{
echo ' eval "$(curl -fsSL --insecure https://raw.githubusercontent.com/georgalis/pub/master/sub/fn.bash)"' 1>&2;
echo 'export -f spin2 base ct ' 1>&2
}
_kdb_k4_default_squash ()
{
sed -E -e '
s/%%.*//
/^%k. $/d
/^%kr # matrix reason/d
/^%kp # hier superord/d
/^%kj # motive subord/d
/^%kl # like link /d
/^%kb # better subject/d
/^%kc # topic class /d
/^%kq # value relative/d
/^%kr # matrix reason/d
/^ %k. .*ord #$/d
/^ %k. .*def #$/d
/^ %k. .*root #$/d
/^ %kl .*link #$/d
/^%kk .*# class define #$/d
/^%kt $/d
/^%k(l|r|p)_(%kl )5.* isbe #/d
/^%k(l|r|p)_(%kl )5.* self #/d
/^%k(l|r|p)_(%kl )5.* self [ ]*# (super|related)/d
/^%k(l|r|p)_5.* self [ ]*# (super|related|linked)/d
/^%kd_.*index.md$/d
/^%kc # class/d
/^%kc # hist class/d
/^%kc blah/d
/^%kc hist/d
/^%kc hist # hist class/d
/^%kc hist exam # class onto/d
/^%kc hist exmp # class note/d
/^%kc hist ignore # hist class/d
/^%kj motive/d
s/ 00be 00000000 # value key #$//
'
}
_kdb_log ()
{
local a= lines_in= lines_f="${kdb}/0/${kdb_log}/log";
read a < <(find "$lines_f" -size +100k -o -mtime +3 2> /dev/null) || true;
test "$a" && {
mv -i "$lines_f" "${lines_f}."$(xss);
ls -d "${lines_f}."* | sed -n -e :a -e '$q;N;2,20ba' -e 'P;D' | while read a; do
find "$a" -maxdepth 0 -mtime +9 -exec rm \{\} \;;
done
};
[ $# -gt 0 ] && lines_in="$*" || read -d '' lines_in < <(cat) || true;
[ "$lines_in" ] && {
tai64n <<< "$lines_in" >> "${lines_f}";
$verbb "${FUNCNAME[@]} \ $*"
} || {
_kdb_fn_trace;
return 1
}
}
_kdb_logerr ()
{
local a='' logf='';
read logf < <(sed -E "s:^(.{4}).*:${kdb}/0/${kdb_log}/\1.log:" < <(kdbtx));
[ "$*" ] && {
tai64n >> "${logf}" <<< "$* fn:${FUNCNAME[1]}@${BASH_SOURCE[1]}:${BASH_LINENO[0]}";
kdbflow "$* fn:${FUNCNAME[1]}@${BASH_SOURCE[1]}:${BASH_LINENO[0]}";
_kdb_fn_trace;
_kdb_fn_stack;
_kdb_chkerr "$*"
} || {
while IFS= read a; do
$FUNCNAME "$a";
done
}
}
_kdb_logwrn ()
{
local a='' logf='';
read logf < <(sed -E "s:^(.{4}).*:${kdb}/0/${kdb_log}/\1.log:" < <(kdbtx));
[ "$*" ] && {
tai64n >> "${logf}" <<< "$* fn:${FUNCNAME[1]}@${BASH_SOURCE[1]}:${BASH_LINENO[0]}";
_kdb_chkwrn "$*";
kdbflow "$*"
} || {
while IFS= read a; do
$FUNCNAME "$a";
done
}
}
_kdb_nil ()
{
return 0
}
_kdb_pools_size ()
{
awk 'END{print NR}' < <(find "${kdb}/0/p/$USER/_/xssptr" -type f)
}
_kdb_squeeze ()
{
tr -s '\r\n\t' ' ' | sed -e 's/ [ ]*/ /g' -e 's/[ ]*$//'
}
_kdb_stderr ()
{
[ "$*" ] && echo "$*" 1>&2 || true
}
_kdb_term_hline ()
{
local char="${1::1}";
[ "$char" ] || char="-";
[ -t 1 ] && {
local cols="$(tput cols)";
tput bold;
printf "%*s\n" "$cols" '' | tr ' ' "$char";
tput sgr0
} || true
}
_kdb_term_pcenter ()
{
local str="$1" char="${2::1}";
strn="${#str}";
[ "$char" ] || char="-";
[ -t 1 ] && {
local cols="$(tput cols)";
local lstrn="$(( 2 + strn / 2 ))";
local rstrn="$(( 2 + strn - lstrn ))";
local lcols="$(( cols / 2 - lstrn ))";
local rcols="$(( cols - lcols - lstrn - rstrn ))";
tput bold;
printf "%*s" "$lcols" '' | tr ' ' "$char";
echo -n " $str ";
printf "%*s\n" "$rcols" '' | tr ' ' "$char";
tput sgr0
} || true
}
_kdb_term_pleft ()
{
local str="$1" char="${2::1}";
strn="${#str}";
[ "$char" ] || char="-";
[ -t 1 ] && {
local cols="$(( $(tput cols) - strn - 4 ))";
tput bold;
echo -n "$char$char $str ";
printf "%*s\n" "$cols" '' | tr ' ' "$char";
tput sgr0
} || true
}
_kdb_term_pright ()
{
local str="$1" char="${2::1}";
strn="${#str}";
[ "$char" ] || char="-";
[ -t 1 ] && {
local cols="$(( $(tput cols) - strn - 4 ))";
tput dim;
printf "%*s" "$cols" '' | tr ' ' "$char";
echo " $str $char$char";
tput sgr0
} || true
}
_kdb_valid_k4 ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
local k4="$1";
test -f "$k4" > /dev/null 2>&1 || {
$_kdb_fn_error "x $invoke : not a regular file '$k4' (5bda5b1f)";
return 1
};
expr "$k4" : "^/.*" > /dev/null || {
$_kdb_fn_error "x $invoke : not full path (611bf112)";
return 1
};
local k4m=;
IFS= read -d '' k4m < <(awk 'NR==1,/^$/{if(length)print}' "$k4");
awk '/^%ks / {a += 1}; END{if (a != 1) exit 1}' <<< "$k4m" || {
$_kdb_fn_error "x $invoke : Invalid k4 %ks line count '$k4' (62ef284c)";
return 1
};
awk '/^%kk / {a += 1}; END{if (a != 1) exit 1}' <<< "$k4m" || {
$_kdb_fn_error "x $invoke : Invalid k4 %kk line count '$k4' (64cae9c5)";
return 1
};
awk '/^%km / {a += 1}; END{if (a != 1) exit 1}' <<< "$k4m" || {
$_kdb_fn_error "x $invoke : Invalid k4 %km line count '$k4' (62ef284d)";
return 1
};
awk '/^%ki / {a += 1}; END{if (a != 1) exit 1}' <<< "$k4m" || {
$_kdb_fn_error "x $invoke : Invalid k4 %ki line count '$k4' (6326110f)";
return 1
};
awk '/^%km / {a = $6}; /^%kk / {b=$2}; END{ if (a = b) exit 0 ; print a,b } ' <<< "$k4m" 1>&2 || {
$_kdb_fn_error "x $invoke : Invalid k4 %kk %km epoch mismatch '$k4' (65149a5b)";
return 1
};
read a < <(awk '/^%km /' <<< "$k4m");
[[ "$a" =~ ^%km\ [[:xdigit:]]+\ [[:xdigit:]]+\ [[:xdigit:]]+\ ]] || {
$_kdb_fn_error "x $invoke unexpected %km timestamp '$k4' (658328fb)";
return 1
};
$_kdb_fn_exit "< $invoke"
}
_kdb_valid_pool ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c8f89;
[ "$1" ] || {
$_kdb_fn_error "x $invoke : no pool (arg1) provided (65635457)";
return 1
};
local pools="${kdb}/0/p/${USER}" pool="$1";
local poolpath="${pools}/${pool}";
[ -d "${poolpath}" ] || {
$_kdb_fn_error "x $invoke not a pool : pp='$poolpath' (62f1552e)";
return 1
};
[[ "$pool" =~ ^[A-Za-z0-9_]+[A-Za-z0-9_-]*$ ]] || {
$_kdb_fn_error "x $invoke : irregular pool name '$pool' =~ ^[A-Za-z0-9_]+[A-Za-z0-9_.+=-]*$ (65641126)";
return 1
};
local a b c n;
read n < <(awk 'END{print NR}' < <(find "$poolpath/knxptr" -type f -not -name 0));
for a in xss knx;
do
for b in ptr hln;
do
kdb_valid_dir "${poolpath}/${a}${b}" || {
$_kdb_fn_error "x $invoke irregular pool dir : ${poolpath}/${a}${b} (62f1550a)";
return 1
};
read c < <(awk 'END{print NR}' < <(find "$poolpath/${a}${b}" -type f -not -name 0));
[ "$n" -eq "$c" ] || {
$_kdb_fn_error "x $invoke ${pool} irregular count $n/knxptr:$c/${a}${b} (610867e2)";
return 1
};
done;
done;
kdb_valid_dir "${poolpath}/${kdb_tmp}";
kdb_valid_dir "${poolpath}/${kdb_var}";
$_kdb_fn_exit "< $invoke"
}
_kdb_valid_vptr ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c8f93;
local pat=;
read pat < <(declare -f kdb_pool_vptr_xssptr | grep ')$' | tr -dc '[[:alpha:]]\n' | tr '\n' '|' | sed -e 's/||/)/' -e 's/^/(/'
echo);
[[ "$1" =~ $pat ]] || {
$_kdb_fn_error "x $invoke arg1 not a vptr '$1' (65634f7d) ";
return 1
};
$_kdb_fn_exit "< $invoke"
}
_kdb_xss_sec_diff ()
{
local b="${1}" m="${2}";
local t0="${b}.0" t1="${b}.1";
[ "$m" = "0" ] && {
xssdaysec > "$t0";
return 0
};
[ "$m" = "1" ] && {
xssdaysec > "$t1";
return 0
};
[ -e "$t0" -a -e "$t1" ] || {
chkerr "$FUNCNAME $1 : Invalid input $t0 $t1 (5bc4dcae)";
return 1
};
shift;
local pass="$*" v;
set $(sed -e 's/_/ /' "$t1" "$t0" | awk '{print $1,$2}' | tr ' ' '\n' | while read v; do
printf '%d\n' "0x$v";
done | tr '\n' ' ');
dc -e "4k $1 $3 - $2 $4 - 1006632959 / + n";
echo -n " $pass"
}
argn ()
{
[ "$1" -a "$1" -gt 0 ] || {
$_kdb_fn_error "x $FUNCNAME : expected positive integer arg1, '$1' (660dfd69)";
return 1
};
awk '{printf "%s\n",'$1'}' < <([ "$*" ] && echo "$*" || cat)
}
dropf ()
{
awk -vn="$1" '{for(i=n;i<=NF;i++)printf("%s%s",(i==n)?"":" ",$i);print""}'
}
k ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c881c;
local k="$1";
test "$k" || k=0;
set $(kdb_pool_ptr "$k") || {
chkerr "$invoke : cannot retrive '$k' (bfd7c86)";
return 1
};
export k4="$1";
export xss="$2";
export knx="$3";
kdbflow $EDITOR "$k4" 660c8d1f;
$EDITOR "$k4";
kdb_rcs "$k4" "$invoke : edit";
_kdb_valid_k4 "$k4" || {
chkerr "$FUNCNAME : editor returned invalid $k4 (6102d15c)";
return 1
};
kdbflow kdb_k4_head_seq $k4 660c8d29;
kdb_k4_head_seq "$k4";
kdb_k4_to_active_pool "$k4" || {
chkerr "$invoke ; kdb_k4_to_active_pool $k4 (62ee088e)";
return 1
};
kdb_cache_upd "$k4" || {
chkerr "$invoke ; kdb_cache_upd $k4 (62ee088f)";
return 1
};
_kdb_term_hline '~';
_kdb_term_hline '-';
kdb_k4_synopsis "$k4";
_kdb_term_hline '~';
$_kdb_fn_exit "< $invoke"
}
kcd ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c867f;
cd "$(kdb_pool_ptr "$1" | sed 's:/[^/]*$::')" || {
ckkerr "$FUNCNAME : cannot cd to pool ptr '$1' (652ec04c)";
return 1
}
}
kd ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c8632;
local k4d="$1";
[ "$k4d" ] || k4d='.';
[ -d "$k4d" ] || mkdir "$k4d" || {
chkerr "$FUNCNAME : cannot create directory '$k4d' from '$(pwd -P)' (6525da29)";
return 1
};
local k="$(find "$k4d" -maxdepth 1 -type f -name \.\*\.kdb 2> /dev/null)";
local count=$(wc -l <<< "$k");
case "${count##* }" in
0)
chktrue "no existing, make new k4 in $k4d";
set $(kdbtx);
export xs=$1 ss=$2;
local cache_dir="${kdb}/0/${kdb_cache}";
local cache_km="${cache_dir}/k4db_km,"
;;
1)
chktrue "use $k in $k4d";
kdb_pool_ptr "$k"
;;
*)
chkerr "$FUNCNAME : multiple k4 found in '$k4d'"
;;
esac;
$_kdb_fn_exit "< $invoke"
}
kdb_0_flaten_filename ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
local n= s=;
[ "$*" ] && {
read n <<< "$*"
} || IFS= read n;
read s < <(cksum <<< "$n");
tr '[:upper:]' '[:lower:]' < <(tr -c '[:alnum:]-' '_' <<< "_${n}_${s}")
}
kdb_argn ()
{
[ "$1" -a "$1" -gt 0 ] || {
$_kdb_fn_error "x $FUNCNAME : expected positive integer arg1, '$1' (60fb6ccb)";
return 1
};
shift;
awk -v n="$1" '{printf "%s\n",$n}' < <([ "$*" ] && echo "$*" || cat)
}
kdb_cache_upd ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c8903;
_kdb_0_lockwait "$FUNCNAME";
local k4s='' k4='';
local cache_dir="${kdb}/0/${kdb_cache}";
kdb_valid_dir "$cache_dir" || {
$_kdb_fn_error "x $invoke : irregular cache dir '$cache_dir' (62eda81e)";
return 1
};
local cache_4_kk="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "^%kk\ " | tr '|' '\n' | sort -u | tr '\n' ',')";
local cache_5_km="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "^%km\ " | tr '|' '\n' | sort -u | tr '\n' ',')";
local cache_6_search="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "$k4_search" | tr '|' '\n' | sort -u | tr '\n' ',')";
local cache_7_graph="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "$k4_graph" | tr '|' '\n' | sort -u | tr '\n' ',')";
local cache_8_value="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "$k4_value" | tr '|' '\n' | sort -u | tr '\n' ',')";
local cache_9_topic="$cache_dir/k4db_$(tr -d '[)(% ^\\]' <<< "$k4_topic" | tr '|' '\n' | sort -u | tr '\n' ',')";
[ -e "${cache_4_kk}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_4_kk}%" (6525e1af)";
return 1
};
[ -e "${cache_5_km}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_5_km}%" (62eda9a9)";
return 1
};
[ -e "${cache_6_search}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_6_search}%" (62eda9aa)";
return 1
};
[ -e "${cache_7_graph}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_7_graph}%" (62eda9ab)";
return 1
};
[ -e "${cache_8_value}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_8_value}%" (62eda9ac)";
return 1
};
[ -e "${cache_9_topic}%" ] && {
chkwrn "$FUNCNAME : cache locked "${cache_9_topic}%" (62eda9ad)";
return 1
};
exec 4>> "${cache_4_kk}%" 5>> "${cache_5_km}%" 6>> "${cache_6_search}%" 7>> "${cache_7_graph}%" 8>> "${cache_8_value}%" 9>> "${cache_9_topic}%";
[ $# -gt 0 ] && {
k4s="$1";
shift
};
while [ $# -gt 0 ]; do
k4s="$(printf "%s\n%s\n" "$k4s" "$1")";
shift;
done;
{
[ "$k4s" ] && echo "$k4s" || kdb_find_k4
} | while IFS= read k4; do
local k4h="$(awk 'NR==1,/^$/ {if (length) print}' "${k4}")";
grep '^%km ' <<< "$k4h" | awk -v k4="$k4" 'END{printf "%s %s ",$6,k4}' | multitee 0,4,5,6,7,8,9;
{
grep -E "^%kk\ " <<< "$k4h" | tr '\n' ' ';
echo
} 1>&4;
{
grep -E "^%km\ " <<< "$k4h" | tr '\n' ' ';
echo
} 1>&5;
{
grep -E "$k4_search" <<< "$k4h" | tr '\n' ' ';
echo
} 1>&6;
{
grep -E "$k4_graph" <<< "$k4h" | tr '\n' ' ';
echo
} 1>&7;
{
grep -E "$k4_value" <<< "$k4h" | tr '\n' ' ';
echo
} 1>&8;
{
grep -E "$k4_topic" <<< "$k4h" | tr '\n' ' ';
echo
} 1>&9;
done;
exec 4>&- 5>&- 6>&- 7>&- 8>&- 9>&-;
[ "$k4s" ] && {
cat "${cache_4_kk}%" >> "${cache_4_kk}";
cat "${cache_5_km}%" >> "${cache_5_km}";
cat "${cache_6_search}%" >> "${cache_6_search}";
cat "${cache_7_graph}%" >> "${cache_7_graph}";
cat "${cache_8_value}%" >> "${cache_8_value}";
cat "${cache_9_topic}%" >> "${cache_9_topic}";
rm -i -f "${cache_4_kk}%" "${cache_5_km}%" "${cache_6_search}%" "${cache_7_graph}%" "${cache_8_value}%" "${cache_9_topic}%"
} || {
mv -i -f "${cache_4_kk}%" "${cache_4_kk}";
mv -i -f "${cache_5_km}%" "${cache_5_km}";
mv -i -f "${cache_6_search}%" "${cache_6_search}";
mv -i -f "${cache_7_graph}%" "${cache_7_graph}";
mv -i -f "${cache_8_value}%" "${cache_8_value}";
mv -i -f "${cache_9_topic}%" "${cache_9_topic}"
};
touch "${cache_dir}/nulltime";
_kdb_0_lockun "$FUNCNAME";
$_kdb_fn_exit "< $invoke"
}
kdb_decs2touchfmt ()
{
[ "$1" ] && {
awk '{ printf "%x",$1 }' <<< "$1" | kdb_xs2touchfmt
} || {
awk '{ printf "%x",$1 }' | kdb_xs2touchfmt
}
}
kdb_epoch ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
kdbflow $invoke ${BASH_SOURCE[0]} 660c891f;
test "$1" && local kdb="$1" || local kdb="${kdb}";
shift || true;
local xs="$1" ss="$2";
shift 2 || shift || true;
local pass="$*";
[ "${xs}" ] || {
set $(kdbtx);
xs=$1;
ss=$2
};
local cache_dir="${kdb}/0/${kdb_cache}";
local cache_km="${cache_dir}/k4db_km,";
local res="$(sed -e 's:.*/.:&/:' -e 's://$:/z:' <<< "/${xs}${ss}")";
local a mask="${kdb_epoch_mask:=4}";
for a in $(seq 2 $mask);
do
res="$(sed -e 's:.*/.:&/:' -e 's://$:/z:' <<< "$res")";
done;
while grep "^$(sed -e 's:/::g' <<< "${res%/*}") " "$cache_km" > /dev/null; do
res="$(sed -e 's:.*/.:&/:' -e 's://$:/z:' <<< "$res")";
mask=$(( mask + 1 ));
done;
while [ -d "${kdb}/${res%/*}" ]; do
res="$(sed -e 's:.*/.:&/:' -e 's://$:/z:' <<< "$res")";
mask=$(( mask + 1 ));
done;
while grep "^$(sed -e 's:/::g' <<< "${res%/*}") " "$cache_km" > /dev/null; do
res="$(sed -e 's:.*/.:&/:' -e 's://$:/z:' <<< "$res")";
mask=$(( mask + 1 ));
done;
local dir=$(sed -e 's:/[^/]*$:/v:' <<< "${kdb}/${res}");
local eph="$(sed -e 's:/::g' <<< "${res%/*}")";
local span="${res##*/}";
maskx="$(printf %0x $mask)";
echo "$dir" "$xs" "$ss" "$eph" "$span" "$maskx" "$pass"
}
kdb_find_k4 ()
{
$_kdb_log "find \"${kdb}\" \\( -path \"${kdb}/0\" -o -path \"${kdb}.*/.git\" \\) -prune -false -o -type f -name \\.\\*\\.kdb \$@";
kdbflow "($FUNCNAME $@)";
kdbflow "find \"${kdb}\" \\( -path \"${kdb}/0\" -o -path \"${kdb}.*/.git\" \\) -prune -false -o -type f -name \\.\\*\\.kdb \$@";
find "${kdb}" \( -path "${kdb}/0" -o -path "${kdb}.*/.git" \) -prune -false -o -type f -name \.\*\.kdb $@
}
kdb_find_kj ()
{
local _k4 f fs a inpath;
[ $# -gt 0 ] && {
fs="$1";
shift
};
while [ $# -gt 0 ]; do
fs="$(printf "%s\n%s\n" "$fs" "$1")";
shift;
done;
[ "$fs" ] || fs="$(cat)";
echo "$fs" | while IFS= read _k4; do
_kdb_valid_k4 "$_k4" || {
$_kdb_fn_error "x $invoke : not valid k4 from arg1 or stdin : '$_k4' (62b1405e)";
return 1
};
inpath="${_k4%/*}";
find "$inpath" -mindepth 2 -type f -name \*.kdb | _kdb_file_headg '%km ' | awk '{print $6}' | sort -u;
done | sort -u
}
kdb_find_kl ()
{
local _k4 f fs a inpath eph;
[ $# -gt 0 ] && {
fs="$1";
shift
};
while [ $# -gt 0 ]; do
fs="$(printf "%s\n%s\n" "$fs" "$1")";
shift;
done;
[ "$fs" ] || fs="$(cat)";
echo "$fs" | while IFS= read _k4; do
_kdb_valid_k4 "$_k4" || {
$_kdb_fn_error "x $invoke : not valid k4 from arg1 or stdin : '$_k4' (62b1405f)";
return 1
};
inpath="${_k4%/*}";
find "$inpath" -mindepth 1 -maxdepth 1 -type f -name \*.kdb | grep -v "$_k4" | _kdb_file_headg '%km ' | awk '{print $6}' | sort -u;
done | sort -u
}
kdb_find_kp ()
{
local _k4 f fs a inpath;
[ $# -gt 0 ] && {
fs="$1";
shift
};
while [ $# -gt 0 ]; do
fs="$(printf "%s\n%s\n" "$fs" "$1")";
shift;
done;
[ "$fs" ] || fs="$(cat)";
echo "$fs" | while IFS= read _k4; do
_kdb_valid_k4 "$_k4" || {
chkerr "x $invoke : not valid k4 from arg1 or stdin : '$_k4' (62b12359)";
return 1
};
inpath="${_k4%/*}";
inpath="${inpath%/*}";
while [ -d "$inpath" ]; do
find "$inpath" -maxdepth 1 -type f -name \*.kdb | _kdb_file_headg '%km ' | awk '{print $6}';
inpath="${inpath%/*}";
done;
done | sort -u
}
kdb_help ()
{
_kdb_term_pcenter "kdb help" '=';
cat <<EOF
# assured record, arbitrary discrete elements of subject matter
# novel date based directory for file directories and metadata markdown
# revision tracking
# quick indexed searches
# short key identifiers
# results pool manuliplation
# filter, associate, re-value
# intuitative knoledge connections, discover
# tags, super-ordinates, sub-ordinates, matrix, serial, value, interval alignment, class, definition, identity, initilization, expiration, begin, end
# trivial metadata connection expansion
# parameterized search and element reporting
EOF
_kdb_term_pcenter 'Glossary';
cat <<EOF
# knx index id of pool record, usually 2 or 3 characters
# eph epzch meta value of directory
# xs 0x unix seconds
# ss 0x unix subseconds
# xss 0s unix seconds and subseconds
# ref pool reference {knx|k4|xs|xss}
EOF
_kdb_term_pleft 'Basic Functions';
cat <<EOF
# kj regex regex cache to pool
# kjt regex regex cache kt ks to pool
# ks arg* new k4 with subj (squashed) args
# kl list pool by acquisition
# klr list pool by reverse acquisition
# kt list pool by modification
# ktr list pool by reverse modification
# kg regex grep regex from pool
# kv regex grep -v regex from pool
# k ref edit k4 (default last)
# krst {args} delete current pool, set defaults iff args then follow with kj args
EOF
_kdb_term_pleft 'Complex Functions';
cat <<EOF
# kn arg1 matter end newer epzch, 0x{epoch) or 0d{unix}
# ka arg1 matter begin after epzch, 0x{epoch) or 0d{unix}
# ksp ref matter aspell
# kk {mtime} find matter -mtime {arg1}
# kq {atime} find matter -atime {arg1}
# kh {n} show {number} lines of command history
# kp {pool} use pool, list pools on null
# kpt {tag} add tags to all k4 in pool pool tag, list pool tags on null
# kpv {vptr} iff vptr arg1 supported, assign all ptr in pool and backstore ptr type, else list supported vptr
# kz {pool} iff arg@ is a valid pool, combine with current
# krst reset (delete pool and kdb_init) current pool
# kd {nx|epzch|xss} graph linked, super and related
# kw {name} name current pool, mv 0/p/{kdb_pool} to 0/p/{name} and set kdb_pool={name}'
# kdb_pool_knx re-knx pool from xssptr with kdb_k4_to_active_pool
EOF
cat > /dev/null <<EOF
# @@ ## ### ##### ######## Extract
# k_ {days} k4 mod k4since days to ^pool
# k__ {hours} k4 mod k4since hours to ^pool
# @@ ## ### ##### ######## Refine
# @@ ## ### ##### ######## Pools
# @@ ## ### ##### ######## Misc
# k4k {nx|xs|xss} extract k4 link(s) default ^pool
# kh {nx|xs|xss} k4 head (default 0), limit to k4_search
# kc {nx|xs|xss} k4 cat (default 0) to {PAGER} or stdout to pipe
#,khx regex {nx|xs|xss} k4 headx (default k4_search)
#,km {nx|xs|xss} k4 markdown (default 0) to {PAGER} or stdout to pipe
# kjq regex quiet beckground k4db regex to ^pool
#,kjtq regex quiet background tags regex to ^pool
# @@ ## ### ##### ######## Edge
#,ke depth {nx|xs|xss} k4 edge walk depth (diagram?)
#,kel depth {nx|xs|xss} k4 link walk depth
#,kep depth {nx|xs|xss} k4 super walk depth
#,ker depth {nx|xs|xss} k4 related walk depth
# future feature
# present catagorial tags, per selection, present sub catagorial tags, kwds
# optionally present existing catagorial tag tree
# kdb in half-octal
# port to python
# port to lua
EOF
cat > /dev/null <<EOF
pool listing runthrough...
#export kdb_vptr="k4mi" # default vptr setting, kdb_init
${kdb}/0/p/$USER/$pool/.vptr # active vptr backstore
#kpv {vptr} # iff vptr arg1 supported, assign all ptr and backstore, else list
#kpv xs # on supported, assign vptr arg1 to all ptr and backstore
kl klr # list pool by xss (kdb_topic: kll kllr ; k4_search klll klllr)
klt kltr # list pool by mod
#klv klvr # list pool by vptr
knx and %ks are always displayed
arg1 # expands addl %k fields in listing, v in arg1 expands vptr value
double and tripple l applies kdb_topic and k4_search as arg1
kll == kl tkjm
klll == tkrpjlnomcdviez
kpv #U> checks and sets arg1 vptr OR lists supported
kdb_pool_vptr () #0> Set the pool ptr mtime and vptrf, per type from arg1
kdb_pool_ptr_vset () #0> Set vptr arg1 on ptr arg2
kdb_print_vxssptr () #0> render vptr arg1 for xssptr arg2
kpv -> kdb_pool_vptr ->> kdb_pool_ptr_vset
klv ->> kdb_print_vxssptr
EOF
_kdb_term_pleft 'root files';
_kdb_term_pcenter "" "-";
_kdb_term_pleft "Configuration $kdb_conf_bash";
cat $kdb_conf_bash;
_kdb_term_pleft "$vkdb/kdb.*.fn.bash";
find "$vkdb" -maxdepth 1 -type f -name kdb\.\*fn\.bash -exec sed -e '/^[_[:alpha:]][_[:alnum:]]* () /!d' -e 's/ was:.*//' -e 's/\ ()\ {/ () `/' \{\} \; | sort | column -s'`' -t;
_kdb_term_pleft "$kdb_init_bash";
find "$vkdb" -maxdepth 1 -type f -name "$kdb_init_bash" -exec sed -e '/^[_[:alpha:]][_[:alnum:]]* () /!d' -e 's/ was:.*//' -e 's/\ ()\ {/ () `/' \{\} \; | sort | column -s'`' -t;
_kdb_term_pcenter "k4 Header Types";
declare -f kdb_k4_head_seq | grep '^# %';
local types="
#B> Beta development
#V> Validation action
#L> Log action
#N> Numeric processing
#K> k4 manipulation
#0> Internal functions
#P> POSIX action
#X> Text processing
#T> Time function
#U> User command
";
_kdb_term_pcenter "Function Types";
echo "$types";
tre="^%($(echo "$types" | sed -e '/^$/d' | awk '{printf "%s",$1"|"}' | sed -e 's/%//g' -e 's/|$//'))\\ ";
order="$(echo "$types" | sed -e '/^$/d' | awk '{print $1}')";
for kind in $order;
do
_kdb_term_pleft "$(echo "$types" | grep "$kind")";
grep --no-filename "^[_[:alpha:]][_[:alnum:]]* () .*$kind" "$vkdb/"*.bash | sed -e 's/ was:.*//' -e 's/\ ()\ {/ () `/' | sort | column -s'`' -t;
done;
$_kdb_fn_exit "< $invoke"
}
kdb_init_k4db ()
{
local invoke="#$$ ($FUNCNAME $@)";
$_kdb_fn_entry "> $invoke";
_kdb_0_lockwait "$FUNCNAME";
kdbflow valid posix bin 66046b94;
ps | grep "^[ ]*$$ " | grep -q bash 2> /dev/null || {
$_kdb_fn_error "x $invoke : Not bash shell (fc4768) <<<" 1>&2;
return 1
};
which ci > /dev/null || {
$_kdb_fn_error "x $invoke : ci (rcs) : not in path (6108e185)";
return 1
};
which find > /dev/null || {
$_kdb_fn_error "x $invoke : find : not in path (61083eb5)";
return 1
};
strings $(which find) | grep -q BSD || {
chkwrn "x $invoke : find not BSD (61083f5c)"
};
which lua5.4 > /dev/null || {
$_kdb_fn_error "x $invoke : lua5.4 : not in path (655bda5a)";
return 1
};
which nawk > /dev/null || {
$_kdb_fn_error "x $invoke : nawk : not in path (655bd5cc)";
return 1
};
strings $(which nawk) | grep -q POSIXLY_CORRECT || {
chkwrn "x $invoke : unexpected nawk (655bd5cd)"
};
which tai64n > /dev/null || {
$_kdb_fn_error "x $invoke : tai64n not in path (657f4e46)";