add new /bin/uci script and api for manipulating buildroot-ng config files
[openwrt.git] / package / base-files / default / etc / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4
5 alias debug=${DEBUG:-:}
6
7 # newline
8 N="
9 "
10
11 _C=0
12
13 # valid interface?
14 if_valid () (
15 ifconfig "$1" >&- 2>&- ||
16 [ "${1%%[0-9]}" = "br" ] ||
17 { debug "# missing interface '$1' ignored"; false; }
18 )
19
20 hotplug_dev() {
21 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
22 }
23
24 append() {
25 local var="$1"
26 local value="$2"
27 local sep="${3:- }"
28 eval "export ${var}=\"\${${var}:+\${${var}}${value:+$sep}}\$value\""
29 }
30
31 reset_cb() {
32 config_cb() {
33 return 0
34 }
35 option_cb() {
36 return 0
37 }
38 }
39 reset_cb
40
41 config () {
42 local cfgtype="$1"
43 local name="$2"
44 _C=$(($_C + 1))
45 name="${name:-cfg${_C}}"
46 config_cb "$cfgtype" "$name"
47 export CONFIG_SECTION="$name"
48 export CONFIG_${CONFIG_SECTION}_TYPE="$cfgtype"
49 }
50
51 option () {
52 local varname="$1"; shift
53 export CONFIG_${CONFIG_SECTION}_${varname}="$*"
54 option_cb "$varname" "$*"
55 }
56
57 config_rename() {
58 local OLD="$1"
59 local NEW="$2"
60 local oldsetting
61 local newvar
62
63 [ -z "$OLD" -o -z "$NEW" ] && return
64 for oldsetting in `set | grep ^CONFIG_${OLD}_ | \
65 sed -e 's/\(.*\)=.*$/\1/'` ; do
66 newvar="CONFIG_${NEW}_${oldsetting##CONFIG_${OLD}_}"
67 eval "${newvar}=\${$oldsetting}"
68 unset "$oldsetting"
69 done
70 [ "$CONFIG_SECTION" = "$OLD" ] && CONFIG_SECTION="$NEW"
71 }
72
73 config_unset() {
74 config_set "$1" "$2" ""
75 }
76
77 config_clear() {
78 [ -z "$CONFIG_SECTION" ] && return
79 for oldsetting in `set | grep ^CONFIG_${CONFIG_SECTION}_ | \
80 sed -e 's/\(.*\)=.*$/\1/'` ; do
81 unset $oldsetting
82 done
83 unset CONFIG_SECTION
84 }
85
86 config_load() {
87 local DIR="./"
88 _C=0
89 [ \! -e "$1" -a -e "/etc/config/$1" ] && {
90 DIR="/etc/config/"
91 }
92 [ -e "$DIR$1" ] && {
93 CONFIG_FILENAME="$DIR$1"
94 . ${CONFIG_FILENAME}
95 } || return 1
96 ${CD:+cd -} >/dev/null
97 ${CONFIG_SECTION:+config_cb}
98 }
99
100 config_get() {
101 case "$3" in
102 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
103 *) eval "$1=\"\${CONFIG_${2}_${3}}\"";;
104 esac
105 }
106
107 config_set() {
108 export CONFIG_${1}_${2}="${3}"
109 }
110
111 load_modules() {
112 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
113 }
114
115 include() {
116 for file in $(ls $1/*.sh 2>/dev/null); do
117 . $file
118 done
119 }
This page took 0.048123 seconds and 5 git commands to generate.