/
mulle-bashfunctions
executable file
·1397 lines (1115 loc) · 28.9 KB
/
mulle-bashfunctions
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 mulle-bash
#! MULLE_BASHFUNCTIONS_VARIANT=minimal
#
# shellcheck shell=bash
# shellcheck disable=SC2236
# shellcheck disable=SC2166
# shellcheck disable=SC2006
#
# Copyright (c) 2015-2017 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Mulle kybernetiK nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
[ "${TRACE}" = 'YES' -o "${MULLE_BASHFUNCTIONS_ENV_TRACE}" = 'YES' ] && set -x && : "${MULLE_EXECUTABLE}" "$@"
usage()
{
[ ! -z "$*" ] && echo "error: $*" >&2
cat <<EOF >&2
Usage:
${MULLE_EXECUTABLE_NAME} [command]
mulle-bashfunctions main purpose is to help load the library functions
into an interactive shell. The library functions are almalgamated in a
file called "mulle-bashfunctions.sh". There are variants though (none,
minimal, all) with different sizes.
But mulle-bashfunctions can also
* embed "mulle-bashfunctions.sh" into another shell script
* show the path to "mulle-bashfunctions.sh"
* locate the "mulle-bashfunctions.sh" install path for a desired variant
* show documentation for any of the defined functions
* list the available libraries, which you can "include"
* run an arbitrary mulle-bashfunction with eval and r-eval
Examples:
Load "mulle-bashfunctions-all.sh" into the current interactive shell:
eval \`mulle-bashfunctions load all\`
Commands:
apropos <f> : search for a function by keywords
embed : embed mulle-bashfunctions into a script
env : print environment needed for "mulle-bashfunctions.sh"
eval <cmd> : evaluate cmd inside of mulle-bashfunctions
functions : list defined functions
hostname : show system hostname as used by mulle-bashfunctions
libraries : list available libraries
load [variant] : use eval \`mulle-bashfunctions load\` to load
man <function> : show documention for function, if available
new <name> : prints a mulle-bash shell script
path [variant] : path to "mulle-bashfunctions.sh"
r-eval <cmd> : evaluate cmd inside of mulle-bashfunctions and print RVAL
username : show username as used by mulle-bashfunctions
uname : show system short name as used by mulle-bashfunctions
version : print currently used version
versions : list installed versions
EOF
exit 1
}
list_versions()
{
log_entry "list_versions" "$@"
[ -z "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}" ] && _internal_fail "MULLE_BASHFUNCTIONS_LIBEXEC_DIR not set"
local i
local found
shell_enable_nullglob
local prefix
prefix="${MULLE_BASHFUNCTIONS_LIBEXEC_DIR%/*}"
for i in "${prefix}"/[0-9]*.[0-9]*.[0-9]*
do
if [ -d "${i}" ]
then
basename -- "${i}"
found="yes"
fi
done
if [ -z "${found}" ]
then
printf "${MULLE_BASHFUNCTIONS_VERSION}\n"
fi
}
r_all_library_paths()
{
log_entry "r_all_library_paths" "$@"
local paths
local library
local name
shell_enable_nullglob
for library in "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}"/mulle-*.sh
do
name="`basename -- "${library}"`"
case "${name}" in
mulle-bash*.sh|mulle-boot.sh)
;;
*)
if [ -z "${paths}" ]
then
paths="${library}"
else
paths="${paths}"$'\n'"${library}"
fi
;;
esac
done
shell_disable_nullglob
RVAL="${paths}"
}
r_library_name()
{
log_entry "r_library_name" "$@"
RVAL="${1##*/}"
RVAL="${RVAL#mulle-}"
RVAL="${RVAL%.sh}"
}
list_libraries()
{
log_entry "list_libraries" "$@"
local paths
r_all_library_paths
paths="${RVAL}"
local library
IFS=$'\n'
for library in ${paths}
do
r_library_name "${library}"
printf "${RVAL}\n"
done
IFS="${DEFAULT_IFS}"
}
parse_library()
{
log_entry "parse_library" "$@"
local library="$1"
local line
local comment
local sep
local color1="${C_RESET_BOLD}"
local color2="${C_RESET}"
IFS=$'\n'
while read -r line
do
#
# special commands RESET,NOCOLOR,PRE,COLOR only work _well_ in the
# TITLE INTRO block
#
case "${line}" in
\#\ RESET)
comment=""
sep=""
;;
\#\ NOCOLOR)
color1=""
color2=""
;;
\#\ PRE)
comment="${comment}${C_SPECIAL_BLUE}"
;;
\#\ \/PRE)
comment="${comment}${C_RESET}${C_FAINT}"
;;
\#\ COLOR)
color1="${C_RESET_BOLD}"
color2="${C_RESET}"
;;
\#\ TITLE*)
printf "${C_INFO}${line#\#\ TITLE\ }\n${C_RESET}"
printf "${C_FAINT}%b${C_RESET}\n\n" "${comment%%$'\n'}"
;;
\#\ SUBTITLE*)
printf "${C_VERBOSE}${line#\#\ SUBTITLE\ }\n${C_RESET}"
printf "${C_FAINT}%b${C_RESET}\n\n" "${comment%%$'\n'}"
;;
\#)
if [ ! -z "${sep}" ]
then
comment="${comment}${sep}"
fi
;;
\#*)
line="${line##\#\ }"
if [ ! -z "${sep}" ]
then
comment="${comment}${sep}${line}"
sep=${color2}$'\n'
else
if [ ! -z "${line}" ]
then
comment="${comment}${sep}${line}"
sep=$'\n'
fi
fi
;;
function\ *\(\)*|alias\ *)
printf "%b\n\n" "${comment%%$'\n'}"
;;
*)
comment=""
sep="${color1}"
;;
esac
done < "${library}"
IFS="${DEFAULT_IFS}"
}
list_library_functions()
{
log_entry "list_library_functions" "$@"
local library="$1"
local namesonly="$2"
if [ ! -z "${namesonly}" ]
then
sed -n 's/^\([a-z0-9A-Z:][a-z0-9A-Z:_]*\)().*/ \1/p' "${library}" | sort
else
parse_library "${library}"
fi
}
list_functions()
{
log_entry "list_functions" "$@"
local library="$1"
local namesonly="$2"
if [ -z "${library}" ]
then
local paths
r_all_library_paths
paths="${RVAL}"
local sep
IFS=$'\n'
for library in ${paths}
do
r_library_name "${library}"
log_info "${sep}${RVAL}:"
list_library_functions "${library}" "${namesonly}"
sep=$'\n'
done
else
library="${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}/mulle-$1.sh"
if [ -f "${library}" ]
then
list_library_functions "${library}" "${namesonly}"
else
fail "Library \"$1\" not found"
fi
fi
}
new_function()
{
local name="${1:-my-script}"
local prefix="${2:-NO}"
include "case"
local upcase_identifier
r_smart_upcase_identifier "${name}"
upcase_identifier="${RVAL}"
local downcase_identifier
r_smart_downcase_identifier "${name}"
downcase_identifier="${RVAL}"
case "${prefix}" in
'YES')
prefix="${prefix}"
;;
'NO')
prefix=""
;;
*)
;;
esac
local EOF_string="EOF"
cat <<EOF
#! /usr/bin/env mulle-bash
#! MULLE_BASHFUNCTIONS_VERSION=${MULLE_BASHFUNCTIONS_VERSION}
# shellcheck shell=bash
#
#
# ${name}.sh
#
# Copyright (c) `date +%Y` ${MULLE_USERNAME}
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of ${ORGANIZATION:-<ORGANIZATION>} nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
[ "\${TRACE}" = 'YES' -o "\${${upcase_identifier}_TRACE}" = 'YES' ] \
&& set -x \
&& : "\$0" "\$@"
### >> START OF mulle-boot.sh >>
### << END OF mulle-boot.sh <<
#
# Versioning of this script
#
MULLE_EXECUTABLE_VERSION="0.0.0"
### >> START OF mulle-bashfunctions-embed.sh >>
### << END OF mulle-bashfunctions-embed.sh <<
${prefix}print_flags()
{
echo " -f : force operation"
##
## ADD YOUR FLAGS DESCRIPTIONS HERE
##
options_technical_flags_usage \
" : "
}
${prefix}usage()
{
[ \$# -ne 0 ] && log_error "\$*"
cat <<EOF >&2
Usage:
x [flags]
##
## ADD YOUR USAGE DESCRIPTION HERE
##
Flags:
${EOF_string}
${prefix}print_flags | LC_ALL=C sort >&2
exit 1
}
${prefix}main()
{
#
# simple option/flag handling
#
local OPTION_VALUE
while [ \$# -ne 0 ]
do
if options_technical_flags "\$1"
then
shift
continue
fi
case "\$1" in
-f|--force)
MULLE_FLAG_MAGNUM_FORCE='YES'
;;
-h*|--help|help)
${prefix}usage
;;
--value)
[ \$# -eq 1 ] && ${prefix}usage "missing argument to \$1"
shift
OPTION_VALUE="\$1"
;;
--version)
printf "%s\n" "\${MULLE_EXECUTABLE_VERSION}"
exit 0
;;
##
## ADD YOUR FLAGS HERE
##
-*)
${prefix}usage "Unknown flag \"\$1\""
;;
*)
break
;;
esac
shift
done
options_setup_trace "\${MULLE_TRACE}" && set -x
##
## ADD YOUR CODE HERE
##
log_info "Does nothing, but will soon"
}
${prefix}main "\$@"
EOF
}
man_library_function()
{
log_entry "man_library_function" "$@"
local library="$1"
local identifier="$2"
local text
local line
while IFS=$'\n' read -r line
do
case "${line}" in
'#'*)
line="${line#\#}"
line="${line#\ }"
text="${text}${sep}${line}"
sep=$'\n'
continue
;;
${identifier}\(\)|function\ ${identifier}\(\))
sed -e 's/^# //' <<< "${text}"
return 0
;;
*)
text=
sep=
;;
esac
done < <( cat "${library}" )
return 1
}
man_function()
{
log_entry "man_function" "$@"
[ $# -eq 0 ] && usage "${MULLE_EXECUTABLE_FAIL_PREFIX}: Missing name"
[ $# -gt 1 ] && shift && usage "${MULLE_EXECUTABLE_FAIL_PREFIX}: Superflous parameter \"$*\""
local name="$1"
local paths
r_all_library_paths
paths="${RVAL}"
local identifier
r_lowercase "${name}"
r_identifier "${RVAL}"
identifier="${RVAL}"
local library
IFS=$'\n'
for library in ${paths}
do
if man_library_function "${library}" "${identifier}"
then
IFS="${DEFAULT_IFS}"
return 0
fi
done
# search for transposed bits
local prefix
prefix="${identifier:0:2}"
case "${prefix}" in
'r_'|'g_')
identifier="${identifier:2}"
;;
*)
prefix=""
;;
esac
#
# cut identifier into pieces and multiply it out
#
# A B C 012
# A C B 021
# B A C 102
# B C A 120
# C A B 201
# C B A 210
#
local n
local piece
local loops
declare -a pieces
IFS="_"
n=0
loops=1
for piece in ${identifier}
do
pieces[$n]="${piece}"
n=$(( n + 1 ))
loops=$(( loops * n ))
done
if [ $n -lt 5 ]
then
local tmp
local i
local next
local flip
for prefix in "" "r_" "g_"
do
i=0
flip=0
while [ $i -lt $loops ]
do
j=0
identifier=""
while [ $j -lt $n ]
do
r_concat "${identifier}" "${pieces[$j]}" "_"
identifier="${RVAL}"
j=$(( j + 1 ))
done
IFS=$'\n'
for library in ${paths}
do
if man_library_function "${library}" "${prefix}${identifier}"
then
return 0
fi
done
if [ $n -gt 1 ]
then
next=$(( (flip + 1) % n ))
if [ $next -eq 0 ]
then
flip=0
next=1
fi
tmp="${pieces[$flip]}"
pieces[$flip]="${pieces[$next]}"
pieces[$next]="${tmp}"
flip=$next
fi
i=$(( i + 1 ))
done
done
fi
IFS="${DEFAULT_IFS}"
log_error "Function ${C_RESET_BOLD}${name}${C_ERROR} is unknown."
}
r_parse_functionname()
{
local line="$1"
RVAL="${line%#*}"
RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}"
RVAL="${RVAL#function}"
RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}"
RVAL="${RVAL%()}"
RVAL="${RVAL%"${RVAL##*[![:space:]]}"}" # remove tail space
RVAL="${RVAL#"${RVAL%%[![:space:]]*}"}" # remove head space
}
print_apropos_text()
{
local text="$1"
local line="$2"
local isep="$3"
r_parse_functionname "${line}"
if [ "${text#${RVAL}}" = "${text}" ]
then
text="`sed -e 's/^/ /' <<< "${text}" `"
printf "%s%s\n\n%s\n" "${isep}" "${RVAL}" "${text}"
else
printf "%s%s\n" "${isep}" "${text}"
fi
}
r_apropos_library_function()
{
log_entry "r_apropos_library_function" "$@"
local library="$1"
local search="$2"
local options="$3"
local isep="$4"
local text
local line
local sep
local space
space=""
sep="${space}"
# TODO: might optionally also search preceeding text
# (see man: how to capture it)
while IFS=$'\n' read -r line
do
case "${line}" in
'#'*)
line="${line#\#}"
line="${line#\ }"
if [ ! -z "${text}" -o ! -z "${line}" ]
then
text="${text}${sep}${line}"
sep=$'\n'"${space}"
fi
continue
;;
*${search}*\(\))
print_apropos_text "${text}" "${line}"
isep=$'\n'
text=
sep="${space}"
;;
*\(\)*)
case ",${options}," in
*',no-text,'*)
;;
*)
if grep -E "${search}" <<< "${text}" > /dev/null
then
print_apropos_text "${text}" "${line}"
isep=$'\n'
fi
;;
esac
text=
sep="${space}"
;;
*)
text=
sep="${space}"
;;
esac
done < <( cat "${library}" )
RVAL="${isep}"
}
apropos_function()
{
log_entry "apropos_function" "$@"
local options
while [ $# -ne 0 ]
do
case "$1" in
-i)
r_comma_concat "${options}" "identifier"
options="${RVAL}"
shift
continue
;;
--no-text)
r_comma_concat "${options}" "no-text"
options="${RVAL}"
shift
continue
;;
esac
break
done
[ $# -eq 0 ] && usage "${MULLE_EXECUTABLE_FAIL_PREFIX}: Missing name"
[ $# -gt 1 ] && shift && usage "${MULLE_EXECUTABLE_FAIL_PREFIX}: Superflous parameter \"$*\""
local name="$1"
local paths
r_all_library_paths
paths="${RVAL}"
local search
search="${name}"
case ",${options}," in
*',identifier,'*)
r_lowercase "${name}"
r_identifier "${RVAL}"
search="${RVAL}"
;;
esac
local library
local isep
IFS=$'\n'
for library in ${paths}
do
if r_apropos_library_function "${library}" "${search}" "${options}" "${isep}"
then
isep="${RVAL}"
fi
done
IFS="${DEFAULT_IFS}"
}
MINIMAL_HEAD_MARKER='### >> START OF mulle-bashfunctions-minimal-embed.sh >>'
MINIMAL_FOOT_MARKER='### << END OF mulle-bashfunctions-minimal-embed.sh <<'
DEFAULT_HEAD_MARKER='### >> START OF mulle-bashfunctions-embed.sh >>'
DEFAULT_FOOT_MARKER='### << END OF mulle-bashfunctions-embed.sh <<'
ALL_HEAD_MARKER='### >> START OF mulle-bashfunctions-all-embed.sh >>'
ALL_FOOT_MARKER='### << END OF mulle-bashfunctions-all-embed.sh <<'
BOOT_HEAD_MARKER='### >> START OF mulle-boot.sh >>'
BOOT_FOOT_MARKER='### << END OF mulle-boot.sh <<'
mulle_bashfunctions_embed_usage()
{
[ $# -ne 0 ] && log_error "$*"
cat <<EOF >&2
Usage:
mulle-bashfunctions embed
This lazy script just reads from stdio and outputs to stdout.
Inserts or updates the mulle-boot.sh code between these two markers:
${BOOT_HEAD_MARKER}
${BOOT_FOOT_MARKER}
If there is a \`#!/usr/bin/env mulle-bash\` line on top, it will be replaced
by \`bin/sh\`. If these markers are missing the embed can not work.
The place should be at the very, very top (below the \${TRACE} = 'YES', if
that exists.
Inserts or updates the mulle-bashfunctions-embed.sh code between these two
markers for minimal:
${MINIMAL_HEAD_MARKER}
${MINIMAL_FOOT_MARKER}
default:
${DEFAULT_HEAD_MARKER}
${DEFAULT_FOOT_MARKER}
all:
${ALL_HEAD_MARKER}
${ALL_FOOT_MARKER}
If these markers are missing in your script the embed can not work. To
change the chose mulle-bashfunctions edit the markers in your script.
A convenient place to place these marker sis right after the booter
(below MULLE_EXECUTABLE_VERSION if that line exists).
Use mulle-bashfunctions boot-embed/boot-extract, if you just want to add
or remove the boot code.
EOF
exit 1
}
mulle_boot_embed()
{
while [ $# -ne 0 ]
do
case "$1" in
*)
mulle_bashfunctions_embed_usage
;;
esac
done
local line
local found='NO'
# initial state, searching for start
while IFS=$'\n' read -r line
do
if [ "${line}" = "#! /usr/bin/env mulle-bash" ]
then
printf "%s\n" "#! /bin/sh"
continue
fi
if [ "${line}" = "${BOOT_HEAD_MARKER}" ]
then
found='YES'
break
fi
printf "%s\n" "${line}"
done
if [ "${found}" = 'NO' ]
then
mulle_bashfunctions_embed_usage "Add boot markers to your source, before running embed"
fi
# start found and consumed now put in embed.sh
cat "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}/mulle-boot.sh" || exit 1
if [ "${found}" = 'YES' ]
then
# now search for end
while IFS=$'\n' read -r line
do
if [ "${line}" = "${BOOT_FOOT_MARKER}" ]
then
break
fi
done
fi
# output rest
while IFS=$'\n' read -r line
do
printf "%s\n" "${line}"
done
}
mulle_bashfunctions_embed()
{
local filename
while [ $# -ne 0 ]
do
case "$1" in
*)
exit 1
;;
esac
done
local line
local found
# initial state, searching for start
while IFS=$'\n' read -r line
do
if [ "${line}" = "${MINIMAL_HEAD_MARKER}" -o \
"${line}" = "${DEFAULT_HEAD_MARKER}" -o \
"${line}" = "${ALL_HEAD_MARKER}" ]
then
local found="${line}"
break
fi
printf "%s\n" "${line}"
done
if [ -z "${found}" ]
then
mulle_bashfunctions_embed_usage "Add embed markers to your source, before running embed"
fi
case "${found}" in
*-all-*)
log_verbose "Determined ${C_MAGENTA}${C_BOLD}all${C_VERBOSE} embedding"
filename="mulle-bashfunctions-all-embed.sh"
;;
*-minimal-*)
log_verbose "Determined ${C_MAGENTA}${C_BOLD}minimal${C_VERBOSE} embedding"