-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
share: Introduce the
sh
directory, mktemp
implementation
This commit installs an `sh` directory in the `share` directory, in which implementations of popular tools that might not be available on a user's system sit. In order for a shell scope to use those implementations if the regular binaries are unavailable, the `PATH` variable has to be modified as follows, at the beginning of the scope: ``` %sh{ export PATH="${PATH}:${kak_runtime}/sh" <code> } ```
- Loading branch information
Showing
13 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/bin/sh | ||
## mktemp fallback implementation | ||
## Options supported: | ||
## -u, --dry-run: only print the path to a temporary file without generating it | ||
## -d, --directory: create a directory | ||
## -p, --tmpdir: use $TMPDIR as base path if possible, /tmp otherwise | ||
## this flag does NOT take any argument | ||
|
||
error() { | ||
printf 'mktemp: %s\n' "$*" >&2 | ||
exit 1 | ||
} | ||
|
||
main() { | ||
flag_dry_run=0 | ||
flag_directory=0 | ||
flag_tmpdir=0 | ||
|
||
for arg in "$@"; do | ||
case "${arg}" in | ||
-u|--dry-run) flag_dry_run=1;; | ||
-d|--directory) flag_directory=1;; | ||
-p|--tmpdir) flag_tmpdir=1;; | ||
-*) error "Unsupported flag ${arg}";; | ||
*) break;; | ||
esac | ||
|
||
shift | ||
done | ||
|
||
if [ $# -eq 0 ]; then | ||
template="tmp.XXXXXXXXXX" | ||
flag_tmpdir=1 | ||
else | ||
template="${1}" | ||
if ! printf %s\\n "${template}" | grep -q 'XXX$'; then | ||
error "The template must end with at least 3 consecutive 'X's" | ||
fi | ||
fi | ||
|
||
if [ ${flag_tmpdir} -ne 0 ]; then | ||
dir="${TMPDIR:-/tmp}/" | ||
fi | ||
|
||
template=$(printf %s\\n "${template}" | awk '{ | ||
idx = match($0, /X+$/) - 1 | ||
printf "%." idx "s", $0 | ||
system("cat /dev/urandom | tr -dc a-zA-Z0-9 | fold -w " RLENGTH " | head -n 1") | ||
}') | ||
|
||
path="${dir}${template}" | ||
if [ ${flag_dry_run} -eq 0 ]; then | ||
if [ ${flag_directory} -ne 0 ]; then | ||
mkdir -p "${path}" | ||
chmod u+rwx "${path}" | ||
else | ||
if [ -n "${dir}" ]; then | ||
mkdir -p "${dir}" | ||
fi | ||
touch "${path}" | ||
chmod u+rw "${path}" | ||
fi | ||
fi | ||
|
||
printf %s\\n "${path}" | ||
} | ||
|
||
main "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters