aboutsummaryrefslogtreecommitdiff
path: root/lib/cpt-stdio
diff options
context:
space:
mode:
authormerakor <cem@ckyln.com>2021-01-06 10:51:25 +0000
committermerakor <cem@ckyln.com>2021-01-06 10:51:25 +0000
commit4494a117ed31897513235090b8282c2b033b6407 (patch)
tree3825c70cb3d5db34db2d4e39672b29e961a14445 /lib/cpt-stdio
parentca1d37bfdd5d936a183989d7a49ad31d1d8911c9 (diff)
downloadcpt-4494a117ed31897513235090b8282c2b033b6407.tar.gz
lazyload experiment: separate libraries
FossilOrigin-Name: 8660559c9b7b280bd5113dfcb54bb4a0a2ef0bda65036c0fbe6c1a4dfc9ba8a1
Diffstat (limited to 'lib/cpt-stdio')
-rw-r--r--lib/cpt-stdio72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/cpt-stdio b/lib/cpt-stdio
new file mode 100644
index 0000000..0f19a8e
--- /dev/null
+++ b/lib/cpt-stdio
@@ -0,0 +1,72 @@
+# Text and output related functions
+
+out() {
+ # Print a message as is.
+ printf '%s\n' "$@"
+}
+
+die() {
+ # Print a message and exit with '1' (error).
+ log "$1" "$2" "!>"
+ exit 1
+}
+
+warn() {
+ # Print a warning message
+ log "$1" "$2" "${3:-WARNING}"
+}
+
+sepchar() (
+ # Seperate every character on the given string without resorting to external
+ # processes.
+ [ "$1" ] || return 0; str=$1; set --
+ while [ "$str" ]; do
+ str_tmp=$str
+ for i in $(_seq $(( ${#str} - 1 ))); do
+ str_tmp=${str_tmp%?}
+ done
+ set -- "$@" "$str_tmp"
+ str=${str#$str_tmp}
+ done
+ printf '%s\n' "$@"
+)
+
+contains() {
+ # Check if a "string list" contains a word.
+ case " $1 " in *" $2 "*) return 0; esac; return 1
+}
+
+regesc() {
+ # Escape special regular expression characters as
+ # defined in POSIX BRE. '$.*[\^'
+ printf '%s\n' "$1" |
+ sed 's|\\|\\\\|g;s|\[|\\[|g;s|\$|\\$|g;s|\.|\\.|g;s|\*|\\*|g;s|\^|\\^|g'
+}
+
+prompt() {
+ # If CPT_PROMPT is set to 0, continue This can be useful for installation
+ # scripts and bootstrapping.
+ [ "$CPT_PROMPT" = 0 ] && return 0
+
+ # Ask the user for some input.
+ [ "$1" ] && log "$1"
+ log "Continue?: Press Enter to continue or Ctrl+C to abort here"
+
+ # POSIX 'read' has none of the "nice" options like '-n', '-p'
+ # etc etc. This is the most basic usage of 'read'.
+ # '_' is used as 'dash' errors when no variable is given to 'read'.
+ read -r _ || return 1
+}
+
+pop() {
+ # Remove an item from a "string list". This allows us
+ # to remove a 'sed' call and reuse this code throughout.
+ del=$1
+ shift 2
+
+ for i do [ "$i" = "$del" ] || printf %s " $i "; done
+}
+
+# Local Variables:
+# mode: sh
+# End: