openpvn: Split out config parsing code for reuse
[openwrt/openwrt.git] / package / network / services / openvpn / files / lib / functions / openvpn.sh
diff --git a/package/network/services/openvpn/files/lib/functions/openvpn.sh b/package/network/services/openvpn/files/lib/functions/openvpn.sh
new file mode 100644 (file)
index 0000000..83fb1bb
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+get_openvpn_option() {
+       local config="$1"
+       local variable="$2"
+       local option="$3"
+
+       local value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+(([^ \t\\]|\\.)+)[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
+       [ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+'"'([^']+)'"'[ \t]*$/\1/p' "$config" | tail -n1)"
+       [ -n "$value" ] || value="$(sed -rne 's/^[ \t]*'"$option"'[ \t]+"(([^"\\]|\\.)+)"[ \t]*$/\1/p' "$config" | tail -n1 | sed -re 's/\\(.)/\1/g')"
+       [ -n "$value" ] || return 1
+
+       export -n "$variable=$value"
+       return 0
+}
+