-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path_call
executable file
·282 lines (230 loc) · 9.8 KB
/
_call
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
#!/usr/bin/env bash
#Universal debugging filesystem.
_user_log_anchor() {
# DANGER Do NOT create automatically, or reference any existing directory!
! [[ -d "$HOME"/.ubcore/userlog ]] && cat - > /dev/null 2>&1 && return 0
#Terminal session may be used - the sessionid may be set through .bashrc/.ubcorerc .
if [[ "$sessionid" != "" ]]
then
cat - >> "$HOME"/.ubcore/userlog/a-"$sessionid".log
return 0
fi
cat - >> "$HOME"/.ubcore/userlog/a-undef.log
return 0
}
#Cyan. Harmless status messages.
_messagePlain_nominal() {
echo -e -n '\E[0;36m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Blue. Diagnostic instrumentation.
_messagePlain_probe() {
echo -e -n '\E[0;34m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Blue. Diagnostic instrumentation.
_messagePlain_probe_expr() {
echo -e -n '\E[0;34m '
echo -e -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Green. Working as expected.
_messagePlain_good() {
echo -e -n '\E[0;32m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Yellow. May or may not be a problem.
_messagePlain_warn() {
echo -e -n '\E[1;33m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Red. Will result in missing functionality, reduced performance, etc, but not necessarily program failure overall.
_messagePlain_bad() {
echo -e -n '\E[0;31m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
##Parameters
#"--shell", ""
#"--profile"
#"--parent", "--embed", "--return", "--devenv"
#"--call", "--script" "--bypass"
ub_import=
ub_import_param=
ub_import_script=
ub_loginshell=
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && ub_import="true"
([[ "$1" == '--profile' ]] || [[ "$1" == '--script' ]] || [[ "$1" == '--call' ]] || [[ "$1" == '--return' ]] || [[ "$1" == '--devenv' ]] || [[ "$1" == '--shell' ]] || [[ "$1" == '--bypass' ]] || [[ "$1" == '--parent' ]] || [[ "$1" == '--embed' ]]) && ub_import_param="$1" && shift
([[ "$0" == "/bin/bash" ]] || [[ "$0" == "-bash" ]] || [[ "$0" == "/usr/bin/bash" ]] || [[ "$0" == "bash" ]]) && ub_loginshell="true" #Importing ubiquitous bash into a login shell with "~/.bashrc" is the only known cause for "_getScriptAbsoluteLocation" to return a result such as "/bin/bash".
[[ "$ub_import" == "true" ]] && ! [[ "$ub_loginshell" == "true" ]] && ub_import_script="true"
_messagePlain_probe_expr '$0= '"$0"'\n ''$1= '"$1"'\n ''ub_import= '"$ub_import"'\n ''ub_import_param= '"$ub_import_param"'\n ''ub_import_script= '"$ub_import_script"'\n ''ub_loginshell= '"$ub_loginshell" | _user_log_anchor
# DANGER Prohibited import from login shell. Use _setupUbiquitous, call from another script, or manually set importScriptLocation.
# WARNING Import from shell can be detected. Import from script cannot. Asserting that script has been imported is possible. Asserting that script has not been imported is not possible. Users may be protected from interactive mistakes. Script developers are NOT protected.
if [[ "$ub_import_param" == "--profile" ]]
then
([[ "$profileScriptLocation" == "" ]] || [[ "$profileScriptFolder" == "" ]]) && _messagePlain_bad 'import: profile: missing: profileScriptLocation, missing: profileScriptFolder' | _user_log_anchor && return 1
elif ([[ "$ub_import_param" == "--parent" ]] || [[ "$ub_import_param" == "--embed" ]] || [[ "$ub_import_param" == "--return" ]] || [[ "$ub_import_param" == "--devenv" ]])
then
([[ "$scriptAbsoluteLocation" == "" ]] || [[ "$scriptAbsoluteFolder" == "" ]] || [[ "$sessionid" == "" ]]) && _messagePlain_bad 'import: parent: missing: scriptAbsoluteLocation, missing: scriptAbsoluteFolder, missing: sessionid' | _user_log_anchor && return 1
elif [[ "$ub_import_param" == "--call" ]] || [[ "$ub_import_param" == "--script" ]] || [[ "$ub_import_param" == "--bypass" ]] || [[ "$ub_import_param" == "--shell" ]] || ([[ "$ub_import" == "true" ]] && [[ "$ub_import_param" == "" ]])
then
([[ "$importScriptLocation" == "" ]] || [[ "$importScriptFolder" == "" ]]) && _messagePlain_bad 'import: call: missing: importScriptLocation, missing: importScriptFolder' | _user_log_anchor && return 1
elif [[ "$ub_import" != "true" ]] #"--shell", ""
then
_messagePlain_warn 'import: undetected: cannot determine if imported' | _user_log_anchor
true #no problem
else #FAIL, implies [[ "$ub_import" == "true" ]]
_messagePlain_bad 'import: fall: fail' | _user_log_anchor && return 1
exit 1
fi
if [[ "$ub_import_param" == "--profile" ]]
then
ub_import=true
elif ([[ "$ub_import_param" == "--parent" ]] || [[ "$ub_import_param" == "--embed" ]] || [[ "$ub_import_param" == "--return" ]] || [[ "$ub_import_param" == "--devenv" ]]) && [[ "$scriptAbsoluteLocation" != "" ]] && [[ "$scriptAbsoluteFolder" != "" ]] && [[ "$sessionid" != "" ]]
then
ub_import=true
elif [[ "$ub_import_param" == "--call" ]] || [[ "$ub_import_param" == "--script" ]] || [[ "$ub_import_param" == "--bypass" ]] || [[ "$ub_import_param" == "--shell" ]] || ([[ "$ub_import" == "true" ]] && [[ "$ub_import_param" == "" ]])
then
ub_import=true
fi
[[ "$ub_import" != "true" ]] && ub_import_param=
#Critical prerequsites.
_getAbsolute_criticalDep() {
! type realpath > /dev/null 2>&1 && return 1
! type readlink > /dev/null 2>&1 && return 1
! type dirname > /dev/null 2>&1 && return 1
#Known issue on Mac. See https://github.com/mirage335/ubiquitous_bash/issues/1 .
! realpath -L . > /dev/null 2>&1 && return 1
return 0
}
! _getAbsolute_criticalDep && exit 1
#####Utilities.
#Retrieves absolute path of current script, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#However, will dereference symlinks IF the script location itself is a symlink. This is to allow symlinking to scripts to function normally.
#Suitable for allowing scripts to find other scripts they depend on. May look like an ugly hack, but it has proven reliable over the years.
_getScriptAbsoluteLocation() {
if [[ "$0" == "-"* ]]
then
return 1
fi
local absoluteLocation
if [[ (-e $PWD\/$0) && ($0 != "") ]] && [[ "$0" != "/"* ]]
then
absoluteLocation="$PWD"\/"$0"
absoluteLocation=$(realpath -L -s "$absoluteLocation")
else
absoluteLocation=$(realpath -L "$0")
fi
if [[ -h "$absoluteLocation" ]]
then
absoluteLocation=$(readlink -f "$absoluteLocation")
absoluteLocation=$(realpath -L "$absoluteLocation")
fi
echo $absoluteLocation
}
#Retrieves absolute path of current script, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#Suitable for allowing scripts to find other scripts they depend on.
_getScriptAbsoluteFolder() {
if [[ "$0" == "-"* ]]
then
return 1
fi
dirname "$(_getScriptAbsoluteLocation)"
}
#Retrieves absolute path of parameter, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#Suitable for finding absolute paths, when it is desirable not to interfere with symlink specified folder structure.
_getAbsoluteLocation() {
if [[ "$1" == "-"* ]]
then
return 1
fi
if [[ "$1" == "" ]]
then
echo
return
fi
local absoluteLocation
if [[ (-e $PWD\/$1) && ($1 != "") ]] && [[ "$1" != "/"* ]]
then
absoluteLocation="$PWD"\/"$1"
absoluteLocation=$(realpath -L -s "$absoluteLocation")
else
absoluteLocation=$(realpath -L "$1")
fi
echo $absoluteLocation
}
#Generates random alphanumeric characters, default length 18.
_uid() {
local uidLength
! [[ -z "$1" ]] && uidLength="$1" || uidLength=18
cat /dev/urandom 2> /dev/null | base64 2> /dev/null | tr -dc 'a-zA-Z0-9' 2> /dev/null | head -c "$uidLength" 2> /dev/null
}
#Demarcate major steps.
_messageNormal() {
echo -e -n '\E[1;32;46m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
#Demarcate major failures.
_messageError() {
echo -e -n '\E[1;33;41m '
echo -n "$@"
echo -e -n ' \E[0m'
echo
return 0
}
export anchorScriptAbsoluteLocation=$(_getScriptAbsoluteLocation)
export anchorScriptAbsoluteFolder=$(_getScriptAbsoluteFolder)
export anchorName=$(basename "$anchorScriptAbsoluteLocation")
_readLocked_trivial() {
[[ -e "$anchorScriptAbsoluteFolder"/anchor.lck ]] && _messageError 'LOCK' && exit 1
}
#External lock should only be required for unusual filesystems like SSHFS, which do not support filesystem lock mechanisms, with programs that must lock the document undergoing editing.
_createLocked_trivial() {
_readLocked_trivial
echo -n "$anchorSessionID" > "$anchorScriptAbsoluteFolder"/anchor.lck
}
_removeLocked_trivial() {
cat "$anchorScriptAbsoluteFolder"/anchor.lck 2>/dev/null | grep "$anchorSessionID" > /dev/null 2>&1 && rm -f "$anchorScriptAbsoluteFolder"/anchor.lck > /dev/null 2>&1
}
_launch_IfExists() {
! type "$1" >/dev/null 2>&1 && return
#_createLocked_trivial
local launchExitStatus
"$@"
launchExitStatus="$?"
#_removeLocked_trivial
exit "$launchExitStatus"
}
#_messagePlain_nominal 'Lock message warning within application usually may be disregarded. External lock implemented by script as SSHFS workaround.'
#_messagePlain_probe 'PWD= '"$PWD"
#"$anchorName"
#"$anchorScriptAbsoluteFolder"
#"$ub_import" (true || false)
#"$ub_import_param"
#_launch_IfExists ../_index/"$anchorName" "$anchorScriptAbsoluteFolder"
#_launch_IfExists "$HOME"/core/lab/labName/_index/"$anchorName" "$anchorScriptAbsoluteFolder"/document.txt
#_launch_IfExists "$HOME"/core/lab/labNameMini/_index/"$anchorName" "$anchorScriptAbsoluteFolder"/document.txt
#_launch_IfExists "$HOME"/core/extra/lab/labName/_index/"$anchorName" "$anchorScriptAbsoluteFolder"/document.txt
#_launch_IfExists "$HOME"/core/extra/lab/labNameMini/_index/"$anchorName" "$anchorScriptAbsoluteFolder"/document.txt
#_launch_IfExists "$HOME"/bin/"$anchorName" "$anchorScriptAbsoluteFolder"/document.txt
#Unlike Anchor scripts, Call scripts may reasonably use any combination of user-defined command string alternatives as appropriate.
_launch_IfExists "$anchorScriptAbsoluteFolder"/ubiquitous_bash.sh _echo PASS "$@"