summaryrefslogtreecommitdiffstats
path: root/libsh/alias.sh
blob: 2945910fa347d3edc7a2170639d45946d2a3b31d (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
# Copyright (C) 2016  Patrick "P. J." McDermott
#
# This file is part of libsh.
#
# libsh is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# libsh is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with libsh.  If not, see
# <http://www.gnu.org/licenses/>.

static check_args()
{
	local params="${1}"
	shift 1
	local p=

	IFS=' '
	for p in ${params}; do
		unset IFS
		if ! [ ${#} -gt 0 ]; then
			return 1
		fi
		case "${p}" in
			'string')
				;;
			'int')
				case "${1}" in *[!0-9]*) return 1;; esac
				;;
		esac
		shift 1
	done
	unset IFS

	if ! [ ${#} -eq 0 ]; then
		return 1
	fi

	return 0
}

__alias()
{
	local fn="${1}"
	local alias="${2}"
	local params="${*}"

	eval "${alias}() { ${check_args} \"\${params}\" \"\${@}\" && ${fn}; }"
}