Integrate basic UCI config file validation support
[openwrt.git] / package / base-files / files / lib / config / validate.sh
1 # Shell script defining validating configuration macros
2 #
3 # Copyright (C) 2006 by Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20
21 validate_spec() {
22         export | grep 'CONFIG_' | cat - "$@" | awk \
23                 -f $UCI_ROOT/lib/config/validate_config.awk \
24                 -f $UCI_ROOT/lib/config/parse_spec.awk \
25                 -f $UCI_ROOT/lib/config/validate_spec.awk
26 }
27
28 validate_config_cb () {
29         local TYPE
30         local res=
31         
32         [ -n "${CONFIG_SECTION}" ] || return 0
33         
34         config_get TYPE ${CONFIG_SECTION} TYPE
35         [ -n "$TYPE" ] || return 0
36         
37         if type validate_${PACKAGE}_${TYPE} >/dev/null 2>&1; then
38                 validate_${PACKAGE}_${TYPE}
39                 res="$?"
40         else 
41                 if [ -f $UCI_ROOT/lib/config/specs/${PACKAGE}.spec ]; then
42                         # no special defined, use default one
43                         validate_spec $UCI_ROOT/lib/config/specs/${PACKAGE}.spec
44                         res="$?"
45                 fi
46         fi
47         
48         VALIDATE_RES="${VALIDATE_RES:-$res}"
49 }
50
51 uci_validate() {(
52         PACKAGE="$1"
53         FILE="$2"
54         VALIDATE_RES=
55
56         [ -z "${PACKAGE}" ] && {
57                 echo "Error: no package defined"
58                 return 1
59         }
60
61         reset_cb
62         config_cb() {
63                 validate_config_cb "$@"
64         }
65         unset NO_EXPORT
66         if [ -n "$FILE" ]; then
67                 . "$FILE"
68                 config
69         else
70                 config_load "$1"
71         fi
72
73         return ${VALIDATE_RES:-0}
74 )}