-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc_util
More file actions
executable file
·339 lines (327 loc) · 9.79 KB
/
misc_util
File metadata and controls
executable file
·339 lines (327 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env bash
# #region ########## DON'T GEN DOCUMENTATION ##########
if [ "$1" = '--help' ]; then
exit 1
elif [ "$1" = '--version' ]; then
exit 1
fi
# #endregion ########## DON'T GEN DOCUMENTATION ##########
##region########################################### GET PATH, DIR, & NAME ###########################################
readonly MY_PATH=$(realpath "$0")
MY_DIR=''
MY_NAME=''
if [[ "$MY_PATH" =~ ^(.*)/([^/]+)$ ]]; then
readonly MY_DIR="${BASH_REMATCH[1]}" MY_NAME="${BASH_REMATCH[2]}"
else
readonly MY_DIR="$HOME" MY_NAME="$0"
echo "$MY_NAME: Failed to get dir" 1>&2
exit 1
fi
##endregion######################################## GET PATH, DIR, & NAME ###########################################
##region########################################### CONSTANTS ###########################################
declare -ri MAX_INT=$(( 2**63 - 1 ))
declare -ri MAX_READ_INT=$(( 2**31 - 1))
# https://unix.stackexchange.com/questions/592159/how-to-read-file-in-bash-using-a-fixed-number-of-character#comment1104513_592159
# https://www.reddit.com/r/bash/comments/1cest8z/comment/l1o17vt/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
# https://www.reddit.com/r/bash/comments/1cest8z/comment/l1orak7/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
declare -ri MAX_READ_BUFFER=4096
# #region ########## SIGNALS ##########
# cspell:disable
# https://man7.org/linux/man-pages/man7/signal.7.html
readonly -A SIGNAL=(
[HUP]=1 [SIGHUP]=1
[INT]=2 [SIGINT]=2
[QUIT]=3 [SIGQUIT]=3
[ILL]=4 [SIGILL]=4
[TRAP]=5 [SIGTRAP]=5
[ABRT]=6 [SIGABRT]=6
[IOT]=6 [SIGIOT]=6
[BUS]=7 [SIGBUS]=7
[FPE]=8 [SIGFPE]=8
[KILL]=9 [SIGKILL]=9
[USR1]=10 [SIGUSR1]=10
[SEGV]=11 [SIGSEGV]=11
[USR2]=12 [SIGUSR2]=12
[PIPE]=13 [SIGPIPE]=13
[ALRM]=14 [SIGALRM]=14
[TERM]=15 [SIGTERM]=15
[STKFLT]=16 [SIGSTKFLT]=16
[CHLD]=17 [SIGCHLD]=17
[CONT]=18 [SIGCONT]=18
[STOP]=19 [SIGSTOP]=19
[TSTP]=20 [SIGTSTP]=20
[TTIN]=21 [SIGTTIN]=21
[TTOU]=22 [SIGTTOU]=22
[URG]=23 [SIGURG]=23
[XCPU]=24 [SIGXCPU]=24
[XFSZ]=25 [SIGXFSZ]=25
[VTALRM]=26 [SIGVTALRM]=26
[PROF]=27 [SIGPROF]=27
[WINCH]=28 [SIGWINCH]=28
[IO]=29 [SIGIO]=29 [POLL]=29 [SIGPOLL]=29
[PWR]=30 [SIGPWR]=30
[SYS]=31 [SIGSYS]=31
)
# readonly SIGEMT=7 # Emulator trap; weird definitions
# readonly SIGCLD=SIGCHLD # A synonym for SIGCHLD on MIPS
# readonly SIGINFO=SIGPWR # A synonym for SIGPWR; only on Alpha
# readonly SIGLOST=29 # File lock lost (unused); only on SPARC
# readonly SIGUNUSED=SIGSYS # Where defined, SIGUNUSED is synonymous with SIGSYS. Since glibc 2.26, SIGUNUSED is no longer defined on any architecture.
# cspell:enable
# #endregion ########## SIGNALS ##########
##region################### TPUT COLORS ###################
# https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
declare -rA TPUT_COLOR=(
[BLACK]=0
[RED]=1
[GREEN]=2
[YELLOW]=3
[BLUE]=4
[MAGENTA]=5
[CYAN]=6
[WHITE]=7
[BRIGHT_BLACK]=8
[BRIGHT_RED]=9
[BRIGHT_GREEN]=10
[BRIGHT_YELLOW]=11
[BRIGHT_BLUE]=12
[BRIGHT_MAGENTA]=13
[BRIGHT_CYAN]=14
[BRIGHT_WHITE]=15
[black]=0
[red]=1
[green]=2
[yellow]=3
[blue]=4
[magenta]=5
[cyan]=6
[white]=7
[brightBlack]=8
[brightRed]=9
[brightGreen]=10
[brightYellow]=11
[brightBlue]=12
[brightMagenta]=13
[brightCyan]=14
[brightWhite]=15
)
declare -rA SET_COLOR_FOREGROUND=(
[BLACK]="$(tput setaf 0)"
[RED]="$(tput setaf 1)"
[GREEN]="$(tput setaf 2)"
[YELLOW]="$(tput setaf 3)"
[BLUE]="$(tput setaf 4)"
[MAGENTA]="$(tput setaf 5)"
[CYAN]="$(tput setaf 6)"
[WHITE]="$(tput setaf 7)"
[BRIGHT_BLACK]="$(tput setaf 8)"
[BRIGHT_RED]="$(tput setaf 9)"
[BRIGHT_GREEN]="$(tput setaf 10)"
[BRIGHT_YELLOW]="$(tput setaf 11)"
[BRIGHT_BLUE]="$(tput setaf 12)"
[BRIGHT_MAGENTA]="$(tput setaf 13)"
[BRIGHT_CYAN]="$(tput setaf 14)"
[BRIGHT_WHITE]="$(tput setaf 15)"
[RESET]=$'\e[39m'
[DEFAULT]=$'\e[39m'
)
declare -r RESET_COLOR_BACKGROUND=$'\e[49m'
declare -r RESET_COLOR_FOREGROUND=$'\e[39m'
declare -r TPUT_RESET_COLORS="$(tput op)"
##endregion################ TPUT COLORS ###################
# /etc/inputrc
# https://unix.stackexchange.com/questions/116562/key-bindings-table
declare -Ar CTR_SEQ=(
[home]=$'\e[H'
[end]=$'\e[F'
[up]=$'\e[A'
[down]=$'\e[B'
[right]=$'\e[C'
[left]=$'\e[D'
)
fUsed='false'
while (($# > 0)); do
case "$1" in
TPUT_RESET_COLORS) echo -n "${TPUT_RESET_COLORS@A};"; fUsed='true';;
RESET_COLOR_BACKGROUND) echo -n "${RESET_COLOR_BACKGROUND@A};"; fUsed='true';;
RESET_COLOR_FOREGROUND) echo -n "${RESET_COLOR_FOREGROUND@A};"; fUsed='true';;
MAX_INT) echo -n "${MAX_INT@A};"; fUsed='true';;
MAX_READ_INT) echo -n "${MAX_READ_INT@A};"; fUsed='true';;
CTR_SEQ) echo -n "${CTR_SEQ[*]@A};"; fUsed='true';;
TPUT_COLOR) echo -n "${TPUT_COLOR[*]@A};"; fUsed='true';;
SIGNAL) echo -n "${SIGNAL[*]@A};"; fUsed='true';;
*) break;;
esac
shift
done; if eval "$fUsed"; then exit 0; fi; unset fUsed
##endregion######################################## CONSTANTS ###########################################
# New uses $LINENO/BASH_LINENO
##region################################# OLD EXIT WITH ERROR AND LINE #################################
# readonly MATCH_LINE='^[[:blank:]]*exit_error .*?'
# exit_error_old() {
# if [[ "$1" =~ ^[[:digit:]]+$ ]]; then local -ir errorCode=$1; shift; else local -ir errorCode=1; fi
# line=$(grep -m 1 -n -E -e "$MATCH_LINE($1)" "$MY_PATH")
# if [[ "$line" =~ ^([[:digit:]]+) ]]; then
# line="${BASH_REMATCH[1]}: "
# else
# line=''
# fi
# echo "$MY_NAME: $line$*" 1>&2
# exit $errorCode
# }
##endregion############################## OLD EXIT WITH ERROR AND LINE #################################
##region################################# NEW EXIT WITH ERROR AND LINE #################################
exit_error() {
if [[ "$1" =~ ^[[:digit:]]+$ ]]; then local -ir errorCode=$1; shift; else local -ir errorCode=1; fi
mapfile -t val < caller 0
# echo "$MY_NAME: $line$*" 1>&2
t_out="$MY_NAME: $*"$'\n'"${val[0]}"
echo "$t_out" # echo "$t_out" 1>&2
exit $errorCode
} 1>&2 # }
##endregion############################## NEW EXIT WITH ERROR AND LINE #################################
#########################region GET OPTIONS#########################
# #region ########## METHOD 1 ##########
useLabels='false'
useValues='true'
while [ "$1" != "" ] && [ "$1" != '--' ]; do
#If it's an option
if [[ $1 =~ ^\- ]]; then
useLabels='false'
if [[ ${#VALUES[@]} -gt 0 ]]; then
useValues='false'
fi
fi
case $1 in
# #region Flags
-l | --labels )
useLabels='true'
;;
-t | --passthrough )
passthrough='true'
;;
-z | --zero_based )
zero_based='true'
;;
-r | --return_decoded )
decode_value_before_return='true'
;;
# #endregion Flags
# #region Values
-d | --decode | --decode_method )
shift
decode_method=$1
;;
-p | --prompt )
shift
prompt=$1
;;
-c | --option_count_format )
shift
option_count_format=$1
;;
-b | --blank_label )
shift
blank_label=$1
;;
--replace_blank_label_with )
shift
replace_blank_label_with=$1
;;
-f | --return_on_fail | --return_on_failure )
shift;
return_on_failure=$1
;;
# #endregion Values
-h | --help ) _show_help; exit;;
-v | --version ) _show_version; exit;;
* )
if $($useLabels); then
LABELS[${#LABELS[@]}]=$1
elif $($useValues); then
VALUES[${#VALUES[@]}]=$1
else
_show_help
exit 1
fi
;;
esac
shift
done
# #endregion ########## METHOD 1 ##########
# #region ########## ORDERED OR LABELED ##########
declare -n opt1=testCommand opt2=testPath opt3=testFile
while [[ -n "${1+x}" ]]; do
case "$1" in
(-h | --help) _show_help; exit;;
(-v | --version) _show_version; exit;;
# (-c | --coarseness)
# shift
# coarseInput="$1"
# shift
# ;;
# (-d | --decimal)
# shift
# separator="$1"
# shift
# ;;
(*)
if [[ -n "${opt1+x}" ]]; then
if [[ -n "${opt2+x}" ]]; then
if [[ -n "${opt3+x}" ]]; then
exit 1
else
opt3="$1"
fi
else
opt2="$1"
fi
else
opt1="$1"
fi
shift
;;
esac
done
# #endregion ########## ORDERED OR LABELED ##########
#########################endregion GET OPTIONS#########################
##region################## SHOW_HELP/VERSION TEMPLATE ####################
_show_help() {
# or: $0 [-c COARSENESS] [-d SEPARATOR] NUMERATOR DENOMINATOR
# -c, --coarseness COARSENESS: precision. Defaults to $DEFAULT_COARSENESS.
# -d, --decimal SEPARATOR: the fractional separator. Defaults to $DEFAULT_DECIMAL
cat << __EOF__
A template I didn't replace.
Usage: $0
or: $0 --help
or: $0 --version
Options:
-h, --help: Shows this help text.
-v, --version: Shows version info.
Examples:
input description
__EOF__
}
_show_version() {
cat << __EOF__
$0 1.0.0
Copyright (C) 2025 Justin Morris
__EOF__
}
##endregion################ SHOW_HELP/VERSION TEMPLATE ###################
##region########################################### SETUP FD W/ NAMED PIPE ###########################################
declare -ri INPUT_FD=3
declare streamFile="$(mktemp --dry-run "$HOME/Desktop/j_type_inputStream_XXXX")"
if ! [[ "$streamFile" =~ ^$HOME/Desktop/j_type_inputStream_ ]]; then doExit 1 "Cannot safely rm -f $streamFile"; fi
mkfifo "$streamFile"
exec 3<>"$streamFile"
rm -f "$streamFile"
unset streamFile
# shellcheck disable=SC2064
trap "exec $INPUT_FD>&- $INPUT_FD<&-" EXIT
exec 3<&0
##endregion######################################## SETUP FD W/ NAMED PIPE ###########################################
# Check for super user
if sudo --stdin --validate 0<&- &>/dev/null; then echo "You are a Super User"; else echo "You are not a Super User"; fi
# Remove trailing `/` from path
if [[ "$path" =~ ^(.+)/$ ]]; then path="${BASH_REMATCH[1]}"; fi