summaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
...
* Treat EINFO_LOG as false if equal to RC_SERVICEKerin Millar2024-08-011-2/+6
| | | | | | | | | | | | Consider the value of EINFO_LOG to be false in the event that it is equal to the value of RC_SERVICE. The reason for this is that, as of the time of writing, openrc-run(8) defines and uses EINFO_LOG in a way that is at odds with gentoo-functions. Ideally, the behaviour of OpenRC would be modified so that it becomes possible to jettison this workaround. Fixes: 0dd8364c03c6f8737150ee4f146ddeeec57efee9 Bug: https://bugs.gentoo.org/936613 Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add a comment regarding POSIX XCU compatibilityKerin Millar2024-07-111-0/+5
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Mention that _find0() requires findutils >=4.9Kerin Millar2024-07-111-1/+2
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Replace "Issue 8" with "POSIX-1.2024"Kerin Millar2024-07-101-5/+5
| | | | | | The POSIX-1.2024 specification was published on 2024/06/14. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Check whether local is supportedKerin Millar2024-07-101-29/+53
| | | | | | | | | Test whether the (non-standard) local builtin is present and behaving in a manner considered to be conventional. Should the test fail, refrain from conducting any further tests. This causes the test suite to fail early for ksh - a shell not currently supported by the project. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Mention that _SC_NPROCESSORS_ONLN is now standardKerin Millar2024-07-091-2/+2
| | | | | | https://austingroupbugs.net/view.php?id=339 Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have _update_time() measure in centisecondsKerin Millar2024-07-091-31/+22
| | | | | | | Doing so simplifies the case where /proc/uptime is read. Having one more digit's worth of accuracy is no bad thing either. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Initialise the genfun_bin_true variable lazilyKerin Millar2024-07-081-26/+32
| | | | | | | Also, require for true(1) to be executable in order for it to be deemed usable. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the _find0() helper functionKerin Millar2024-07-082-6/+29
| | | | | | | | | | | The function is a simple wrapper around GNU find(1) which presumes the intent to use the -files0-from option to read NUL-delimited path names from the standard input. The benefit in having it is twofold. Firstly, the _select_by_mtime() and is_older_than() functions are thereby made a little easier to read. Secondly, the genfun_bin_find variable is now initialised lazily. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Render _contains_all() compatible with mawkKerin Millar2024-07-081-6/+5
| | | | | | | | The mawk implementation does not react well to FS containing an invalid ERE at any juncture. Address the issue by composing the pattern in full before assigning it to FS. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have _update_time() use a faster rounding method for shKerin Millar2024-07-081-7/+9
| | | | | | | Directly implement a round half up algorithm for sh, thereby avoiding a command substitution and a potential subshell along with it. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Correct the implementation of contains_all()Kerin Millar2024-07-082-40/+122
| | | | | | | It was not translating IFS to FS correctly. Have it do so and extend the test suite accordingly. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Move fetch() to experimentalKerin Millar2024-07-012-42/+42
| | | | | | | I'm not yet ready to commit to it being among the core functions for the inaugural API level. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the contains_all() and contains_any() functionsKerin Millar2024-07-012-25/+172
| | | | | | | | | | | | | | | | | | Here are some examples which presume the default value of IFS. contains_all " cat mat " cat dog # returns 1 contains_all " cat mat " mat cat # returns 0 contains_any " cat mat " cat dog # returns 0 contains_any " cat mat " dog # returns 1 Here are some examples showing that IFS is taken into account. IFS=, contains_all "cat,mat" cat dog # returns 1 IFS=, contains_all "cat,mat" mat cat # returns 0 IFS=, contains_any "cat,mat" cat dog # returns 0 IFS=, contains_any "cat,mat" dog # returns 1 Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Move substr() to experimentalKerin Millar2024-07-013-42/+42
| | | | | | | Though it works very well, I'm not yet ready to commit to it being among the core functions for the inaugural API level. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Use curly braces around a variable expansion in chdir()Kerin Millar2024-06-281-1/+1
| | | | | | Thus adhering to Gentoo's convention. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the int_between() and str_between() functions to experimentalKerin Millar2024-06-281-0/+42
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the up() function to experimentalKerin Millar2024-06-281-0/+28
| | | | | | As based on the implementation in Maarten Billemont's bashlib library. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Describe the SENTINEL variableKerin Millar2024-06-281-0/+1
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have whenceforce() require the -x option to check the execute bitKerin Millar2024-06-282-29/+60
| | | | | | | | This renders the behaviour of whenceforth() almost equivalent to type -P in the absence of the -x option, the exception being that whenceforth() never consults the hash table. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Implement array membership tests idiomatically in quote_args()Kerin Millar2024-06-281-7/+4
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Align the indentation of inlined awk programs with their respective heredocsKerin Millar2024-06-281-64/+64
| | | | | | | This also makes it more pleasant to read where using the (proper) tab size of 8 spaces. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Rectify a grammatical error in fetch()Kerin Millar2024-06-281-1/+1
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the substr() functionKerin Millar2024-06-282-0/+80
| | | | | | | POSIX sh does not support substring expansion so it may come in handy. The implementation is based on the awk function of the same name. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add an experimental module for staging new ideasKerin Millar2024-06-281-0/+53
| | | | | | | | | | | | | The idea is to be able to write new functions without having to initially commit to their being in the core library or, indeed, any of the other modules. Experimental functions may be promoted or simply dropped without warning if it is decided that they are insufficiently useful to merit incorporation. This initial commit defines the is_interactive() and prepend_ts() functions. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Use printf -v only for bash in _update_time()Kerin Millar2024-06-281-1/+1
| | | | | | | After all, the -v option is an extension. This was a mistake made through copying and pasting during a bout of refactoring and rebasing. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Don't explicitly assign the empty string in is_subset()Kerin Millar2024-06-271-2/+2
| | | | | | While it is correct to do so, the key may be created without assignment. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the trueof_all() and trueof_any() functionsKerin Millar2024-06-252-0/+108
| | | | | | | | | | | | | | These functions allow for it to be determined whether an arbitrary simple command yields true for either all of an arbitrary list of parameters or at least one of them. The simple command shall be given each parameter, one at a time. Some examples are shown below. trueof_all test -d -- / /var/empty # returns 0 trueof_all test -d -- / /dev/null # returns 1 trueof_any test -d -- / /dev/null # returns 0 trueof_any test -d -- /etc/fstab /dev/null # returns 1 Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the fetch() functionKerin Millar2024-06-251-0/+42
| | | | | | | | This function makes it trivial to issue a network request - such as HTTP GET - in a manner that is quiet and which yields a useful exit status value. It prefers curl if present but supports wget also. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have is_subset() permit the empty string as a sentinelKerin Millar2024-06-231-6/+6
| | | | | | I can think of no compelling reason not to. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Rectify several typos among the commentsKerin Millar2024-06-231-4/+4
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Deprecate RC_NOCOLOR and the equivalent use of the positional parametersKerin Millar2024-06-231-1/+4
| | | | | | | | | | | | | Firstly, the "." builtin is not specified to support the passing of arguments; such is a bashism. It is rather presumptuous to act as if the present contents of the positional parameters are for the benefit of gentoo-functions at the time of its initialisation. Secondly, there exists a de-facto standard for suppressing colored output, which is to define NO_COLOR as a non-empty string, per https://no-color.org. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Render gentoo-functions modular in natureKerin Millar2024-06-235-559/+683
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | For many years, the implied purpose of gentoo-functions has been to provided parallel implementations of utilities provided by OpenRC, along with a handful of peripheral functions. It is probably also fair to say that it has not seen much in the way of maintenance until comparatively recently. As of the present day, the status quo is not ideal. For one thing, the library has never been particularly useful beyond this definition. It is my hope that some of the recently added functions will be well received by those needing to write effective shell scripts in Gentoo for a number of relevant tasks. Certainly, there remains ample room for improvement in that regard. For another thing, the implementation of gentoo-functions is presently inflexible. For instance, it is impossible to source the functions from an OpenRC runscript without overriding the OpenRC implementations. Nor may one source the functions from an ebuild or eclass without overriding the Portage implementations. Indeed, it is has become something of a mess. Not only does gentoo-functions implement a number of functions that shadow the OpenRC implementations but so does Portage, owing to the existence of its "isolated-functions.sh" unit. What's more, the various implementations are of varying quality and do not necessarily behave in the same manner. This commit aims to address some of these issues by rendering gentoo-functions modular in nature. It establishes the premise of having a core library, with collections of additional functions being optionally declarable. As such, all of the functions that shadow OpenRC have been relocated to a unit named "rc.sh". This first change encompasses the following public functions: - ebegin - eend - eerrorn - eindent - einfon - eoutdent - esyslog - ewarnn - ewend - get_bootparam - is_older_than - veend - vewend - yesno Similarly, all of the functions that exclusively shadow Portage have been relocated to a unit named "portage.sh". This second change encompasses the following public functions: - die - edo - eqatag - eqawarn The functions that remain in the "functions.sh" unit may now be considered as core functions. To accommodate all of this, a new GENFUN_MODULES variable is supported, whose behaviour is described herewith. If GENFUN_MODULES is found to be set at the time of "functions.sh" being sourced, it shall be taken as a list of zero or more blank-separated words. In turn, these words shall be taken as the basenames of potentially available modules - not including the .sh suffix. Presently, the only supported module names are "rc" and "portage". Should either or both of these names be present, their respective units shall be automatically sourced. If neither are present, no additional units shall be sourced. Consequently, it becomes possible for a consumer of gentoo-functions to request that only the core functions be declared by writing: GENFUN_MODULES= . /lib/gentoo/functions.sh If, on the other hand, GENFUN_MODULES is found not to be set then heuristics shall be employed to determine which of the additional units should be sourced. The intent of these heuristics is twofold. Firstly, to maintain an adequate degree of backward-compatibility and, secondly, to act as is appropriate based on the characteristics of the operating environment. The exact behaviour of these heuristics is as follows. If the present shell is neither executing a runscript nor a subprocess of one, the ensuing behaviour shall be as if "rc" had initially been among the names defined by the GENFUN_MODULES variable. If the present shell is not a subprocess of portage, the ensuing behaviour shall be as if "portage" had initially been among the names defined by the GENFUN_MODULES variable. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the is_anyof() and is_subset() functionsKerin Millar2024-06-142-0/+111
| | | | | | | | | | | | Examples follow. is_anyof y x y z # returns 0 is_anoyf y x w z # returns 1 is_subset x y -- x y z # returns 0 is_subset x y z -- z y x # returns 0 is_subset x y -- x w z # returns 1 Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the from_unit() functionKerin Millar2024-06-141-0/+9
| | | | | | | | The function determines whether the current shell is a subprocess of a systemd unit that handles a service, socket, mount point or swap device, per systemd.exec(5). Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the from_runscript() functionKerin Millar2024-06-141-0/+9
| | | | | | | The function determines whether the current shell is executing an OpenRC runscript, or is a subprocess of one. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the from_portage() functionKerin Millar2024-06-141-15/+24
| | | | | | | The function determines whether the current shell appears to be a subprocess of portage. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Rectify a typo for _update_tty_level()Kerin Millar2024-06-141-1/+1
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Throttle the rate at which genfun_cols may be refreshedKerin Millar2024-06-141-3/+78
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Limit the rate at which genfun_cols can be refreshed to intervals that are no shorter than 5 decisconds (half a second). Doing so renders repeated calls to the _eend() function at short intervals faster by an order of magnitude. On one of my fastest available machines, I performed the following benchmark with bash 5.2.26. $ time for ((i=0; i<50000; i++)); do ebegin "$i"; eend; done The results were as follows. BEFORE real 0m56.897s user 0m16.990s sys 0m28.540s AFTER real 0m6.663s user 0m6.435s sys 0m0.191s On one of my slowest available machines, I performed the following benchmark with bash 5.2.26. $ time for ((i=0; i<15000; i++)); do ebegin "$i"; eend; done The results were as follows. BEFORE real 0m52.557s user 0m34.484s sys 0m18.231s AFTER real 0m11.422s user 0m10.654s sys 0m0.535s On that same (slow) machine, I performed the following benchmark with dash 0.5.12. $ time dash -c ' . ./functions.sh while :; do ebegin $((i+=1)); eend; [ $i -eq 15000 ] && exit; done' The results were as follows. BEFORE real 0m43.762s user 0m22.202s sys 0m21.415s AFTER real 0m3.470s user 0m2.535s sys 0m0.736s Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have _eend() assume 80 columns in the absence of a smart terminalKerin Millar2024-06-121-3/+6
| | | | | | | | | | | In cases where the value of genfun_tty is lower than 2, it is implied that the standard output refers either to a terminal insufficiently capable of handling ECMA-48 CSI sequences or to a resource that is not a terminal. Use <space> characters to indent the indicator to the extent that the last character falls on the 80th column. Previously, the indicator was indented by only one <space>. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Never print CHA sequences containing negative numbers in _eend()Kerin Millar2024-06-121-2/+3
| | | | | | | | | In cases where the value of genfun_tty is 2, render it impossible for _eend() ever to produce a CHA sequence that incorporates a negative column number. Some terminal emulators handle it sensibly whereas others do not. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Clean up the jq detection procedure in eqatag()Kerin Millar2024-06-121-7/+2
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Don't use ewarn to display a diagnostic in eqatag()Kerin Millar2024-06-121-1/+1
| | | | | | | I'd rather leave the use of ewarn() to the consumers of gentoo-functions. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have quote_args() respect POSIXLY_CORRECT for Issue 7 conformanceKerin Millar2024-06-121-4/+14
| | | | | | Also, markedly improve the comment that documents the function. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Reposition the declaration of quote_args()Kerin Millar2024-06-121-53/+53
| | | | | | So as to maintain an alphabetical order. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Promote _print_args() to the public function, quote_args()Kerin Millar2024-06-122-17/+17
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the GENFUN_API_LEVEL variable declarationKerin Millar2024-06-121-0/+5
| | | | | | | This allows for simple API capability checks to be performed by users of the library. It might also obsolete the RC_GOT_FUNCTIONS variable. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the parallel_run() functionKerin Millar2024-06-122-1/+64
| | | | | | | | | | | | | | Here is an example of how to use it. $ parallel_run 0 sha256sum dir/* Simple commands of a greater level of sophistication can easily be composed with the aid of functions. $ sm3sum() { cksum -a sm3 "$@"; } $ parallel_run 0 sm3sum dir/* Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the get_nprocs() functionKerin Millar2024-06-122-0/+32
| | | | | | | It stands a good chance of printing a useful value, even in the case that nproc(1) from coreutils is unavailable. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Add the whenceforth() function as a type -P alternativeKerin Millar2024-06-122-2/+85
| | | | | | | It acts much as type -P does in bash. I would have liked to name it whence but ksh and zsh already have builtins by that name. Signed-off-by: Kerin Millar <kfm@plushkava.net>