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
|
# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# shellcheck shell=sh disable=3043
# This file contains a series of function declarations followed by some
# initialization code. Functions intended for internal use shall be prefixed
# with an <underscore> and shall not be considered as being a part of the public
# API. With the exception of those declared by the local builtin, all variables
# intended for internal use shall be prefixed with "genfun_" to indicate so,
# and to reduce the probability of name space conflicts.
#
# Called by ebegin, eerrorn, einfon, and ewarnn.
#
_eprint() {
local color msg
color=$1
shift
# Check whether STDOUT is a terminal, and how capable it is.
_update_tty_level <&1
if [ "${genfun_tty}" -eq 2 ]; then
# If the cursor is not situated on column 1, print a LF
# character. The assumption here is that the last call may have
# been via ebegin, or any of the other printing functions that
# pass a message without a trailing LF.
if [ "${genfun_x}" -ne 1 ]; then
printf '\n'
fi
elif [ "${genfun_is_pending_lf}" -eq 1 ]; then
# We are about to print to a dumb terminal or something other
# than a terminal. Print a LF character because the last printed
# message did not end with one. This technique is not ideal.
# For one thing, it can be thwarted by having called a printing
# function from a subshell or a shell launched by a subprocess,
# because the change to the flag variable would be lost. For
# another, it's possible for the user of the library to be
# directing STDOUT/STDERR to different places between calls.
# Such weaknesses cannot be addressed without some form of IPC.
printf '\n'
fi
msg=$*
if [ "${genfun_tty}" -lt 2 ]; then
if [ "${genfun_tty}" -eq 1 ]; then
# Print but do not attempt to save the cursor position.
printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "${msg}"
else
printf ' * %s%s' "${genfun_indent}" "${msg}"
fi
if _ends_with_newline "${msg}"; then
genfun_is_pending_lf=0
else
# Record the fact that a LF character is pending.
genfun_is_pending_lf=1
fi
elif ! _ends_with_newline "${msg}"; then
# Print the message before saving the cursor position with the
# DECSC sequence. This is a private mode sequence that is not
# defined by ECMA-48. However, it was introduced by DEC for the
# VT100 and can be considered as a de-facto standard.
printf ' %s*%s %s%s\0337' "${color}" "${NORMAL}" "${genfun_indent}" "${msg}"
else
# Strip all trailing LF characters before printing the message.
while true; do
msg=${msg%"${genfun_newline}"}
if ! _ends_with_newline "${msg}"; then
break
fi
done
printf ' %s*%s %s%s' "${color}" "${NORMAL}" "${genfun_indent}" "${msg}"
# Determine the current position of the cursor
_update_cursor_coords <&1
if [ "${genfun_y}" -ne "${genfun_rows}" ]; then
# Have the terminal save the position of the cursor
# with DECSC before printing a LF character to advance
# to the next line.
printf '\0337\n'
else
# The cursor is situated on the last row of the
# terminal, meaning that vertical scrolling will occur.
# Move the cursor up by one row with CUU (ECMA-48 CSI)
# before having the terminal save the position of the
# cursor with DECSC. Finally, print two LF characters to
# advance to the next line.
printf '\033[1A\0337\n\n'
fi
fi
}
#
# hard set the indent used for e-commands.
# num defaults to 0
#
_esetdent()
{
if ! is_int "$1" || [ "$1" -lt 0 ]; then
set -- 0
fi
genfun_indent=$(printf "%${1}s" '')
}
#
# increase the indent used for e-commands.
#
eindent()
{
if ! is_int "$1" || [ "$1" -le 0 ]; then
set -- 2
fi
_esetdent "$(( ${#genfun_indent} + $1 ))"
}
#
# decrease the indent used for e-commands.
#
eoutdent()
{
if ! is_int "$1" || [ "$1" -le 0 ]; then
set -- 2
fi
_esetdent "$(( ${#genfun_indent} - $1 ))"
}
#
# this function was lifted from OpenRC. It returns 0 if the argument or
# the value of the argument is "yes", "true", "on", or "1" or 1
# otherwise.
#
yesno()
{
for _ in 1 2; do
case $1 in
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0|'')
return 1
;;
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
return 0
esac
if [ "$_" -ne 1 ] || ! is_identifier "$1"; then
! break
else
# The value appears to be a legal variable name. Treat
# it as a name reference and try again, once only.
eval "set -- \"\$$1\""
fi
done || vewarn "Invalid argument given to yesno (expected a boolean-like or a legal name)"
return 1
}
#
# use the system logger to log a message
#
esyslog()
{
local pri tag msg
if [ "$#" -lt 2 ]; then
ewarn "Too few arguments for esyslog (got $#, expected at least 2)"
return 1
elif [ -n "${EINFO_LOG}" ] && hash logger 2>/dev/null; then
pri=$1
tag=$2
shift 2
msg=$*
if _is_visible "${msg}"; then
# This is not strictly portable because POSIX defines
# no options whatsoever for logger(1).
logger -p "${pri}" -t "${tag}" -- "${msg}"
fi
fi
}
#
# show an informative message (without a newline)
#
einfon()
{
if ! yesno "${EINFO_QUIET}"; then
_eprint "${GOOD}" "$@"
fi
}
#
# show an informative message (with a newline)
#
einfo()
{
einfon "${*}${genfun_newline}"
}
#
# show a warning message (without a newline) and log it
#
ewarnn()
{
if ! yesno "${EINFO_QUIET}"; then
_eprint "${WARN}" "$@" >&2
esyslog "daemon.warning" "${0##*/}" "$@"
fi
}
#
# show a warning message (with a newline) and log it
#
ewarn()
{
ewarnn "${*}${genfun_newline}"
}
#
# show an error message (without a newline) and log it
#
eerrorn()
{
if ! yesno "${EERROR_QUIET}"; then
_eprint "${BAD}" "$@" >&2
esyslog "daemon.err" "${0##*/}" "$@"
fi
return 1
}
#
# show an error message (with a newline) and log it
#
eerror()
{
eerrorn "${*}${genfun_newline}"
}
#
# show a message indicating the start of a process
#
ebegin()
{
local msg
if ! yesno "${EINFO_QUIET}"; then
msg=$*
while _ends_with_newline "${msg}"; do
msg=${msg%"${genfun_newline}"}
done
_eprint "${GOOD}" "${msg} ...${genfun_newline}"
fi
}
#
# indicate the completion of process, called from eend/ewend
# if error, show errstr via efunc
#
_eend()
{
local efunc indent msg offset retval
efunc=$1
shift
if [ "$#" -eq 0 ]; then
retval=0
elif ! is_int "$1" || [ "$1" -lt 0 ]; then
ewarn "Invalid argument given to ${GENFUN_CALLER} (the exit status code must be an integer >= 0)"
retval=0
msg=
else
retval=$1
shift
msg=$*
fi
# Stash the last known terminal dimensions, if any.
set -- "${genfun_rows}" "${genfun_cols}"
# Check whether STDOUT is a terminal, and how capable it is.
_update_tty_level <&1
if [ "${retval}" -ne 0 ]; then
# If a message was given, print it with the specified function.
if _is_visible "${msg}"; then
"${efunc}" "${msg}"
fi
# Generate an indicator for ebegin's unsuccessful conclusion.
if [ "${genfun_tty}" -eq 0 ]; then
msg="[ !! ]"
else
msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
fi
elif yesno "${EINFO_QUIET}"; then
return "${retval}"
else
# Generate an indicator for ebegin's successful conclusion.
if [ "${genfun_tty}" -eq 0 ]; then
msg="[ ok ]"
else
msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
fi
fi
if [ "${genfun_tty}" -lt 2 ]; then
printf ' %s\n' "${msg}"
genfun_is_pending_lf=0
else
# Provided that the terminal has not since been resized, it may
# be possible to write the indicator on the same row as the
# last printed message, even if it were LF-terminated.
if [ "${genfun_rows}" -eq "$1" ] && [ "${genfun_cols}" -eq "$2" ]; then
# Stash the current position of the cursor.
set -- "${genfun_y}" "${genfun_x}"
# Using the DECRC sequence, restore the cursor position
# to wherever it was just after the last message was
# printed, but before the trailing LF character, if any.
# This is a private mode sequence, and thus not defined
# by ECMA-48. However, it was introduced by DEC for the
# VT100 and can be considered as a de-facto standard.
printf '\0338'
# Determine the position of the cursor again.
_update_cursor_coords <&1
# Check whether the act of restoring the cursor position
# moved it to a different row, excepting the immediately
# preceding row. If it did, assume that scrolling has
# occurred since printing the last message and move the
# cursor back to where it was with CUP (ECMA-48 CSI).
offset=$(( $1 - genfun_y ))
if [ "${offset}" -lt 0 ] || [ "${offset}" -gt 1 ]; then
printf '\033[%d;%dH' "$1" "$2"
genfun_y=$1
genfun_x=$2
fi
fi
# Calculate the column at which the indicator may be printed.
indent=$(( genfun_cols - genfun_x - 6 ))
# Determine whether the cursor needs to be repositioned.
if [ "${indent}" -gt 0 ]; then
# Use CHA (ECMA-48 CSI) to move the cursor to the right.
printf '\033[%dG' "$(( genfun_x + indent ))"
elif [ "${indent}" -lt 0 ]; then
# The indent is negative, meaning that there is not
# enough room. Arrange for the indicator to be printed
# on the next line instead.
printf '\n\033[%dG' "$(( genfun_cols - 6 ))"
fi
# Finally, print the indicator.
printf ' %s\n' "${msg}"
fi
return "${retval}"
}
#
# indicate the completion of process
# if error, show errstr via eerror
#
eend()
{
GENFUN_CALLER=${GENFUN_CALLER:-eend} _eend eerror "$@"
}
#
# indicate the completion of process
# if error, show errstr via ewarn
#
ewend()
{
GENFUN_CALLER=${GENFUN_CALLER:-ewend} _eend ewarn "$@"
}
# v-e-commands honor EINFO_VERBOSE which defaults to no.
veinfo()
{
if yesno "${EINFO_VERBOSE}"; then
einfo "$@"
fi
}
veinfon()
{
if yesno "${EINFO_VERBOSE}"; then
einfon "$@"
fi
}
vewarn()
{
if yesno "${EINFO_VERBOSE}"; then
ewarn "$@"
fi
}
veerror()
{
if yesno "${EINFO_VERBOSE}"; then
eerror "$@"
fi
}
vebegin()
{
if yesno "${EINFO_VERBOSE}"; then
ebegin "$@"
fi
}
veend()
{
if yesno "${EINFO_VERBOSE}"; then
GENFUN_CALLER=veend eend "$@"
elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then
ewarn "Invalid argument given to veend (the exit status code must be an integer >= 0)"
else
return "$1"
fi
}
vewend()
{
if yesno "${EINFO_VERBOSE}"; then
GENFUN_CALLER=vewend ewend "$@"
elif [ "$#" -gt 0 ] && { ! is_int "$1" || [ "$1" -lt 0 ]; }; then
ewarn "Invalid argument given to vewend (the exit status code must be an integer >= 0)"
else
return "$1"
fi
}
veindent()
{
if yesno "${EINFO_VERBOSE}"; then
eindent "$@"
fi
}
veoutdent()
{
if yesno "${EINFO_VERBOSE}"; then
eoutdent "$@"
fi
}
#
# return 0 if gentoo=param was passed to the kernel
#
# EXAMPLE: if get_bootparam "nodevfs" ; then ....
#
get_bootparam()
(
# Gentoo cmdline parameters are comma-delimited, so a search
# string containing a comma must not be allowed to match.
# Similarly, the empty string must not be allowed to match.
case $1 in ''|*,*) return 1 ;; esac
# Reset the value of IFS because there is no telling what it may be.
IFS=$(printf ' \n\t')
if [ "${TEST_GENFUNCS}" = 1 ]; then
read -r cmdline
else
read -r cmdline < /proc/cmdline
fi || return
# Disable pathname expansion. The definition of this function
# is a compound command that incurs a subshell. Therefore, the
# prior state of the option does not need to be recalled.
set -f
for opt in ${cmdline}; do
gentoo_opt=${opt#gentoo=}
if [ "${opt}" != "${gentoo_opt}" ]; then
case ,${gentoo_opt}, in
*,"$1",*) return 0
esac
fi
done
return 1
)
#
# return 0 if any of the files/dirs are newer than
# the reference file
#
# EXAMPLE: if is_older_than a.out *.o ; then ...
is_older_than()
{
local ref has_gfind
if [ "$#" -lt 2 ]; then
ewarn "Too few arguments for is_older_than (got $#, expected at least 2)"
return 1
elif [ -e "$1" ]; then
ref=$1
else
ref=
fi
shift
# Consult the hash table in the present shell, prior to forking.
hash gfind 2>/dev/null; has_gfind=$(( $? == 0 ))
for path; do
if [ -e "${path}" ]; then
printf '%s\0' "${path}"
fi
done |
{
set -- -L -files0-from - ${ref:+-newermm} ${ref:+"${ref}"} -printf '\n' -quit
if [ "${has_gfind}" -eq 1 ]; then
gfind "$@"
else
find "$@"
fi
} |
read -r _
}
#
# Determine whether the first operand is in the form of an integer. A leading
# <hypen-minus> shall be permitted. Thereafter, leading zeroes shall not be
# permitted because the string might later be considered to be octal in an
# arithmetic context, causing the shell to exit if the number be invalid.
#
is_int() {
set -- "${1#-}"
case $1 in
''|*[!0123456789]*)
false
;;
0)
true
;;
*)
test "$1" = "${1#0}"
esac
}
#
# A safe wrapper for the cd builtin. To run cd "$dir" is problematic because:
#
# 1) it may consider its operand as an option
# 2) it will search CDPATH for an operand not beginning with ./, ../ or /
# 3) it will switch to OLDPWD if the operand is -
# 4) cdable_vars causes bash to treat the operand as a potential variable name
#
chdir() {
if [ "$BASH" ]; then
# shellcheck disable=3044
shopt -u cdable_vars
fi
if [ "$1" = - ]; then
set -- ./-
fi
# shellcheck disable=1007,2164
CDPATH= cd -- "$@"
}
#
# Determine whether the first operand contains any visible characters.
#
_is_visible() {
! case $1 in *[[:graph:]]*) false ;; esac
}
#
# Determine whether the first operand is a valid identifier (variable name).
#
is_identifier()
(
LC_ALL=C
case $1 in
''|_|[[:digit:]]*|*[!_[:alnum:]]*) false
esac
)
_has_dumb_terminal() {
! case ${TERM} in *dumb*) false ;; esac
}
_has_monochrome_terminal() {
local colors
# The tput(1) invocation is not portable, though ncurses suffices. In
# this day and age, it is exceedingly unlikely that it will be needed.
if _has_dumb_terminal; then
true
elif colors=$(tput colors 2>/dev/null) && is_int "${colors}"; then
test "${colors}" -eq -1
else
false
fi
}
_ends_with_newline() {
test "${genfun_newline}" \
&& ! case $1 in *"${genfun_newline}") false ;; esac
}
_update_tty_level() {
# Grade the capability of the terminal attached to STDIN (if any) on a
# scale of 0 to 2, assigning the resulting value to genfun_tty. If no
# terminal is detected, the value shall be 0. If a dumb terminal is
# detected, the value shall be 1. If a smart terminal is detected, the
# value shall be 2.
#
# In the case that a smart terminal is detected, its dimensions shall
# be assigned to genfun_cols and genfun_rows, and the position of the
# cursor shall be assigned to genfun_x and genfun_y. Further, it may
# reasonably be assumed that the ECMA-48 CSI and DECSC/DECRC escape
# sequences are supported.
if [ ! -t 0 ]; then
genfun_tty=0
elif _has_dumb_terminal || ! _update_winsize || ! _update_cursor_coords; then
genfun_tty=1
else
genfun_tty=2
fi
}
_update_winsize() {
# The following use of stty(1) is portable as of POSIX Issue 8. It would
# be beneficial to leverage the checkwinsize option in bash but the
# implementation is buggy. Given that Chet has agreed to investigate,
# it may eventually become possible to support it.
# shellcheck disable=2046
set -- $(stty size 2>/dev/null)
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
genfun_rows=$1
genfun_cols=$2
else
genfun_rows=
genfun_cols=
false
fi
}
_update_cursor_coords() {
# shellcheck disable=2046
set -- $(_ecma48_cpr)
if [ "$#" -eq 2 ] && is_int "$1" && is_int "$2"; then
genfun_y=$1
genfun_x=$2
else
genfun_y=
genfun_x=
false
fi
}
_ecma48_cpr() {
@GENTOO_LIBEXEC_DIR@/ecma48-cpr
}
# This is the main script, please add all functions above this point!
# shellcheck disable=2034
RC_GOT_FUNCTIONS="yes"
# Dont output to stdout?
EINFO_QUIET="${EINFO_QUIET:-no}"
EINFO_VERBOSE="${EINFO_VERBOSE:-no}"
# Set the initial value for e-message indentation.
genfun_indent=
# Assign the LF ('\n') character for later expansion. POSIX Issue 8 permits
# $'\n' but it may take years for it to be commonly implemented.
genfun_newline='
'
# Whether the last printed message is pending a concluding LF character.
genfun_is_pending_lf=0
# Should we use color?
if [ -n "${NO_COLOR}" ]; then
# See https://no-color.org/.
RC_NOCOLOR="yes"
else
RC_NOCOLOR="${RC_NOCOLOR:-no}"
for _ in "$@"; do
case $_ in
--nocolor|--nocolour|-C)
RC_NOCOLOR="yes"
break
esac
done
fi
if _has_monochrome_terminal || yesno "${RC_NOCOLOR}"; then
unset -v BAD BRACKET GOOD HILITE NORMAL WARN
else
# Define some ECMA-48 SGR sequences for color support. These variables
# are public, in so far as users of the library may be expanding them.
# Conveniently, these sequences are documented by console_codes(4).
BAD=$(printf '\033[31;01m')
BRACKET=$(printf '\033[34;01m')
GOOD=$(printf '\033[32;01m')
# shellcheck disable=2034
HILITE=$(printf '\033[36;01m')
NORMAL=$(printf '\033[0m')
WARN=$(printf '\033[33;01m')
fi
|