]> git.ipfire.org Git - people/stevee/network.git/blob - functions.cli
3866495cdbfa260e0a449d983c7d9eda4dc2e9e4
[people/stevee/network.git] / functions.cli
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 function cli_help_requested() {
23 local argument="${1}"
24
25 if [ -n "${argument}" ]; then
26 if listmatch ${argument} help -h --help; then
27 return ${EXIT_OK}
28 fi
29 fi
30
31 return ${EXIT_ERROR}
32 }
33
34 function cli_status_headline() {
35 local zone=${1}
36
37 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
38 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
39
40 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
41 }
42
43 function cli_headline() {
44 echo
45 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
46 }
47
48 function cli_yesno() {
49 local message="$@ [y/n] "
50 local yesno
51
52 while true; do
53 printf "\n${message}"
54 read yesno
55
56 # Check for "yes".
57 if listmatch ${yesno} y Y yes YES Yes; then
58 return ${EXIT_TRUE}
59
60 # Check for "no".
61 elif listmatch ${yesno} n N no NO No; then
62 return ${EXIT_FALSE}
63 fi
64 done
65 }
66
67 function cli_get_key() {
68 local key="${1%%=*}"
69 echo "${key/--/}"
70 }
71
72 function cli_get_val() {
73 echo "${@##*=}"
74 }
75
76 function cli_usage() {
77 local command="$@"
78 local basename="$(basename ${0})"
79
80 if ! isset command; then
81 command="${basename} help"
82 fi
83
84 echo "The given command was not understood by ${basename}." >&2
85 echo "Please run '${command}' for detailed help." >&2
86 }
87
88 function cli_show_man() {
89 local manpage=${1}
90 assert isset manpage
91
92 if ! binary_exists man; then
93 error "The man package is not installed on this system."
94 error "Please install 'man' in order to view the help."
95 exit ${EXIT_ERROR}
96 fi
97
98 man ${manpage}
99 }