aboutsummaryrefslogtreecommitdiff
path: root/lib/cpt-stdio
blob: 0f19a8ed25eb4e54c36609cac7fd32de7297a180 (plain)
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
# 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: