Mount usb filesystem at startup (#1077)
[openwrt.git] / package / base-files / files / 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 hotplug_dev() {
14 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug net
15 }
16
17 append() {
18 local var="$1"
19 local value="$2"
20 local sep="${3:- }"
21
22 eval "export -n -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
23 }
24
25 reset_cb() {
26 config_cb() { return 0; }
27 option_cb() { return 0; }
28 }
29 reset_cb
30
31 config () {
32 local cfgtype="$1"
33 local name="$2"
34
35 CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
36 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
37 append CONFIG_SECTIONS "$name"
38 config_cb "$cfgtype" "$name"
39 CONFIG_SECTION="$name"
40 export -n "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
41 }
42
43 option () {
44 local varname="$1"; shift
45 local value="$*"
46
47 export -n "CONFIG_${CONFIG_SECTION}_${varname}=$value"
48 option_cb "$varname" "$*"
49 }
50
51 config_rename() {
52 local OLD="$1"
53 local NEW="$2"
54 local oldvar
55 local newvar
56
57 [ "$OLD" -a "$NEW" ] || return
58 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
59 sed -e 's/\(.*\)=.*$/\1/'` ; do
60 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
61 eval "export -n \"$newvar=\${$oldvar}\""
62 unset "$oldvar"
63 done
64 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
65
66 [ "$CONFIG_SECTION" = "$OLD" ] && CONFIG_SECTION="$NEW"
67 }
68
69 config_unset() {
70 config_set "$1" "$2" ""
71 }
72
73 config_clear() {
74 local SECTION="$1"
75 local oldvar
76
77 CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , ,")"
78 CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
79
80 for oldvar in `set | grep ^CONFIG_${SECTION:+$SECTION_} | \
81 sed -e 's/\(.*\)=.*$/\1/'` ; do
82 unset $oldvar
83 done
84 }
85
86 config_load() {
87 local file="/etc/config/$1"
88 _C=0
89 CONFIG_SECTIONS=
90 CONFIG_NUM_SECTIONS=0
91 CONFIG_SECTION=
92
93 [ -e "$file" ] && {
94 . $file
95 } || return 1
96
97 ${CONFIG_SECTION:+config_cb}
98 }
99
100 config_get() {
101 case "$3" in
102 "") eval "echo \"\${CONFIG_${1}_${2}}\"";;
103 *) eval "export -n -- \"$1=\${CONFIG_${2}_${3}}\"";;
104 esac
105 }
106
107 config_set() {
108 local section="$1"
109 local option="$2"
110 local value="$3"
111 export -n "CONFIG_${section}_${option}=$value"
112 }
113
114 config_foreach() {
115 local function="$1"
116 local section
117
118 [ -z "$CONFIG_SECTIONS" ] && return 0
119 for section in ${CONFIG_SECTIONS}; do
120 eval "$function \"\$section\""
121 done
122 }
123
124 load_modules() {
125 cd /etc/modules.d
126 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
127 }
128
129 include() {
130 local file
131
132 for file in $(ls $1/*.sh 2>/dev/null); do
133 . $file
134 done
135 }
136
137 find_mtd_part() {
138 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
139
140 PART="${PART##mtd}"
141 echo "${PART:+/dev/mtdblock/$PART}"
142 }
143
144 strtok() { # <string> { <variable> [<separator>] ... }
145 local tmp
146 local val="$1"
147 local count=0
148
149 shift
150
151 while [ $# -gt 1 ]; do
152 tmp="${val%%$2*}"
153
154 [ "$tmp" = "$val" ] && break
155
156 val="${val#$tmp$2}"
157
158 export -n "$1=$tmp"; count=$((count+1))
159 shift 2
160 done
161
162 if [ $# -gt 0 -a "$val" ]; then
163 export -n "$1=$val"; count=$((count+1))
164 fi
165
166 return $count
167 }
This page took 0.058548 seconds and 5 git commands to generate.