Advertisement
Guest User

Untitled

a guest
Jan 25th, 2022
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.92 KB | None
  1. / # cat /etc/preinit
  2. #!/bin/sh
  3. # Copyright (C) 2006 OpenWrt.org
  4. # Copyright (C) 2010 Vertical Communications
  5.  
  6. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  7. pi_ifname=
  8. pi_ip=192.168.1.1
  9. pi_broadcast=192.168.1.255
  10. pi_netmask=255.255.255.0
  11.  
  12. fs_failsafe_ifname=
  13. fs_failsafe_ip=192.168.1.1
  14. fs_failsafe_broadcast=192.168.1.255
  15. fs_failsafe_netmask=255.255.255.0
  16.  
  17. fs_failsafe_wait_timeout=2
  18.  
  19. pi_suppress_stderr="n"
  20. pi_init_suppress_stderr="n"
  21. pi_init_path="/bin:/sbin:/usr/bin:/usr/sbin"
  22. pi_init_cmd="/sbin/init"
  23.  
  24. . /lib/functions.sh
  25. . /lib/functions/boot.sh
  26.  
  27. boot_hook_init preinit_essential
  28. boot_hook_init preinit_main
  29. boot_hook_init failsafe
  30. boot_hook_init initramfs
  31. boot_hook_init preinit_mount_root
  32.  
  33. for pi_source_file in /lib/preinit/*; do
  34.     . $pi_source_file
  35. done
  36.  
  37. boot_run_hook preinit_essential
  38.  
  39. pi_mount_skip_next=false
  40. pi_jffs2_mount_success=false
  41. pi_ext4_mount_success=false
  42. pi_ubifs_mount_success=false
  43. pi_failsafe_net_message=false
  44.  
  45. boot_run_hook preinit_main
  46.  
  47. / # cat /lib/functions.sh
  48. #!/bin/sh
  49. # Copyright (C) 2006-2011 OpenWrt.org
  50. # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
  51.  
  52.  
  53. debug () {
  54.     ${DEBUG:-:} "$@"
  55. }
  56. mount() {
  57.     busybox mount "$@"
  58. }
  59.  
  60. # newline
  61. N="
  62. "
  63.  
  64. _C=0
  65. NO_EXPORT=1
  66. LOAD_STATE=1
  67. LIST_SEP=" "
  68.  
  69. hotplug_dev() {
  70.     env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
  71. }
  72.  
  73. append() {
  74.     local var="$1"
  75.     local value="$2"
  76.     local sep="${3:- }"
  77.  
  78.     eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  79. }
  80.  
  81. list_contains() {
  82.     local var="$1"
  83.     local str="$2"
  84.     local val
  85.  
  86.     eval "val=\" \${$var} \""
  87.     [ "${val%% $str *}" != "$val" ]
  88. }
  89.  
  90. list_remove() {
  91.     local var="$1"
  92.     local remove="$2"
  93.     local val
  94.  
  95.     eval "val=\" \${$var} \""
  96.     val1="${val%% $remove *}"
  97.     [ "$val1" = "$val" ] && return
  98.     val2="${val##* $remove }"
  99.     [ "$val2" = "$val" ] && return
  100.     val="${val1## } ${val2%% }"
  101.     val="${val%% }"
  102.     eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
  103. }
  104.  
  105. config_load() {
  106.     [ -n "$IPKG_INSTROOT" ] && return 0
  107.     uci_load "$@"
  108. }
  109.  
  110. reset_cb() {
  111.     config_cb() { return 0; }
  112.     option_cb() { return 0; }
  113.     list_cb() { return 0; }
  114. }
  115. reset_cb
  116.  
  117. package() {
  118.     return 0
  119. }
  120.  
  121. config () {
  122.     local cfgtype="$1"
  123.     local name="$2"
  124.  
  125.     export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
  126.     name="${name:-cfg$CONFIG_NUM_SECTIONS}"
  127.     append CONFIG_SECTIONS "$name"
  128.     [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
  129.     export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
  130.     export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
  131. }
  132.  
  133. option () {
  134.     local varname="$1"; shift
  135.     local value="$*"
  136.  
  137.     export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
  138.     [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
  139. }
  140.  
  141. list() {
  142.     local varname="$1"; shift
  143.     local value="$*"
  144.     local len
  145.  
  146.     config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
  147.     [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
  148.     len=$(($len + 1))
  149.     config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
  150.     config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
  151.     append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
  152.     list_cb "$varname" "$*"
  153. }
  154.  
  155. config_rename() {
  156.     local OLD="$1"
  157.     local NEW="$2"
  158.     local oldvar
  159.     local newvar
  160.  
  161.     [ -n "$OLD" -a -n "$NEW" ] || return
  162.     for oldvar in `set | grep ^CONFIG_${OLD}_ | \
  163.         sed -e 's/\(.*\)=.*$/\1/'` ; do
  164.         newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
  165.         eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
  166.         unset "$oldvar"
  167.     done
  168.     export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
  169.  
  170.     [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
  171. }
  172.  
  173. config_unset() {
  174.     config_set "$1" "$2" ""
  175. }
  176.  
  177. config_clear() {
  178.     local SECTION="$1"
  179.     local oldvar
  180.  
  181.     list_remove CONFIG_SECTIONS "$SECTION"
  182.     export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
  183.  
  184.     for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
  185.         sed -e 's/\(.*\)=.*$/\1/'` ; do
  186.         unset $oldvar
  187.     done
  188. }
  189.  
  190. # config_get <variable> <section> <option> [<default>]
  191. # config_get <section> <option>
  192. config_get() {
  193.     case "$3" in
  194.         "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
  195.         *)  eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
  196.     esac
  197. }
  198.  
  199. # config_get_bool <variable> <section> <option> [<default>]
  200. config_get_bool() {
  201.     local _tmp
  202.     config_get _tmp "$2" "$3" "$4"
  203.     case "$_tmp" in
  204.         1|on|true|enabled) _tmp=1;;
  205.         0|off|false|disabled) _tmp=0;;
  206.         *) _tmp="$4";;
  207.     esac
  208.     export ${NO_EXPORT:+-n} "$1=$_tmp"
  209. }
  210.  
  211. config_set() {
  212.     local section="$1"
  213.     local option="$2"
  214.     local value="$3"
  215.     local old_section="$CONFIG_SECTION"
  216.  
  217.     CONFIG_SECTION="$section"
  218.     option "$option" "$value"
  219.     CONFIG_SECTION="$old_section"
  220. }
  221.  
  222. config_foreach() {
  223.     local ___function="$1"
  224.     [ "$#" -ge 1 ] && shift
  225.     local ___type="$1"
  226.     [ "$#" -ge 1 ] && shift
  227.     local section cfgtype
  228.  
  229.     [ -z "$CONFIG_SECTIONS" ] && return 0
  230.     for section in ${CONFIG_SECTIONS}; do
  231.         config_get cfgtype "$section" TYPE
  232.         [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
  233.         eval "$___function \"\$section\" \"\$@\""
  234.     done
  235. }
  236.  
  237. config_list_foreach() {
  238.     [ "$#" -ge 3 ] || return 0
  239.     local section="$1"; shift
  240.     local option="$1"; shift
  241.     local function="$1"; shift
  242.     local val
  243.     local len
  244.     local c=1
  245.  
  246.     config_get len "${section}" "${option}_LENGTH"
  247.     [ -z "$len" ] && return 0
  248.     while [ $c -le "$len" ]; do
  249.         config_get val "${section}" "${option}_ITEM$c"
  250.         eval "$function \"\$val\" \"$@\""
  251.         c="$(($c + 1))"
  252.     done
  253. }
  254.  
  255. load_modules() {
  256.     [ -d /etc/modules.d ] && {
  257.         cd /etc/modules.d
  258.         sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
  259.     }
  260. }
  261.  
  262. include() {
  263.     local file
  264.  
  265.     for file in $(ls $1/*.sh 2>/dev/null); do
  266.         . $file
  267.     done
  268. }
  269.  
  270. find_mtd_part() {
  271.     local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
  272.     local PREFIX=/dev/mtdblock
  273.  
  274.     PART="${PART##mtd}"
  275.     [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
  276.     echo "${PART:+$PREFIX$PART}"
  277. }
  278.  
  279. find_mmc_part() {
  280.     local DEVNAME PARTNAME
  281.  
  282.     for DEVNAME in $(find /sys/block/mmcblk*/ -name 'mmcblk*p*'); do
  283.         PARTNAME=$(grep PARTNAME ${DEVNAME}/uevent | cut -f2 -d'=')
  284.         [ "$PARTNAME" = "$1" ] && echo "/dev/$(basename $DEVNAME)" && return 0
  285.     done
  286. }
  287.  
  288. strtok() { # <string> { <variable> [<separator>] ... }
  289.     local tmp
  290.     local val="$1"
  291.     local count=0
  292.  
  293.     shift
  294.  
  295.     while [ $# -gt 1 ]; do
  296.         tmp="${val%%$2*}"
  297.  
  298.         [ "$tmp" = "$val" ] && break
  299.  
  300.         val="${val#$tmp$2}"
  301.  
  302.         export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
  303.         shift 2
  304.     done
  305.  
  306.     if [ $# -gt 0 -a -n "$val" ]; then
  307.         export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
  308.     fi
  309.  
  310.     return $count
  311. }
  312.  
  313.  
  314. jffs2_mark_erase() {
  315.     local part="$(find_mtd_part "$1")"
  316.     [ -z "$part" ] && {
  317.         echo Partition not found.
  318.         return 1
  319.     }
  320.     echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
  321. }
  322.  
  323. uci_apply_defaults() {
  324.     cd /etc/uci-defaults || return 0
  325.     files="$(ls)"
  326.     [ -z "$files" ] && return 0
  327.     mkdir -p /tmp/.uci
  328.     for file in $files; do
  329.         ( . "./$(basename $file)" ) && rm -f "$file"
  330.     done
  331.     uci commit
  332. }
  333.  
  334. group_add() {
  335.     local name="$1"
  336.     local gid="$2"
  337.     local rc
  338.     [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
  339.     [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
  340.     echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
  341.     rc=$?
  342.     [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
  343.     return $rc
  344. }
  345.  
  346. group_exists() {
  347.     grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
  348. }
  349.  
  350. user_add() {
  351.     local name="${1}"
  352.     local uid="${2}"
  353.     local gid="${3:-$2}"
  354.     local desc="${4:-$1}"
  355.     local home="${5:-/var/run/$1}"
  356.     local shell="${6:-/bin/false}"
  357.     local rc
  358.     [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
  359.     [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
  360.     echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
  361.     echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
  362.     rc=$?
  363.     [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
  364.     return $rc
  365. }
  366.  
  367. user_exists() {
  368.     grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
  369. }
  370.  
  371.  
  372. pi_include() {
  373.     if [ -f "/tmp/overlay/$1" ]; then
  374.         . "/tmp/overlay/$1"
  375.     elif [ -f "$1" ]; then
  376.         . "$1"
  377.     elif [ -d "/tmp/overlay/$1" ]; then
  378.         if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then
  379.             for src_script in /tmp/overlay/$1/*.sh; do
  380.                 . "$src_script"
  381.             done
  382.         fi
  383.     elif [ -d "$1" ]; then
  384.         if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then
  385.             for src_script in $1/*.sh; do
  386.                 . "$src_script"
  387.             done
  388.         fi
  389.     else
  390.         echo "WARNING: $1 not found"
  391.         return 1
  392.     fi
  393.     return 0
  394. }
  395.  
  396. [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
  397.  
  398. / # cat /lib/functions/boot.sh
  399. #!/bin/sh
  400. # Copyright (C) 2006-2010 OpenWrt.org
  401. # Copyright (C) 2010 Vertical Communications
  402.  
  403. mount() {
  404.     /bin/busybox mount -o noatime "$@"
  405. }
  406.  
  407. boot_hook_splice_start() {
  408.     export -n PI_HOOK_SPLICE=1
  409. }
  410.  
  411. boot_hook_splice_finish() {
  412.     local hook
  413.     for hook in $PI_STACK_LIST; do
  414.         local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
  415.         export -n "${hook}=${v% }"
  416.         export -n "${hook}_splice="
  417.     done
  418.     export -n PI_HOOK_SPLICE=
  419. }
  420.  
  421. boot_hook_init() {
  422.     local hook="${1}_hook"
  423.     export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
  424.     export -n "$hook="
  425. }
  426.  
  427. boot_hook_add() {
  428.     local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
  429.     local func="${2}"
  430.  
  431.     [ -n "$func" ] && {
  432.         local v; eval "v=\$$hook"
  433.         export -n "$hook=${v:+$v }$func"
  434.     }
  435. }
  436.  
  437. boot_hook_shift() {
  438.     local hook="${1}_hook"
  439.     local rvar="${2}"
  440.  
  441.     local v; eval "v=\$$hook"
  442.     [ -n "$v" ] && {
  443.         local first="${v%% *}"
  444.  
  445.         [ "$v" != "${v#* }" ] && \
  446.             export -n "$hook=${v#* }" || \
  447.             export -n "$hook="
  448.  
  449.         export -n "$rvar=$first"
  450.         return 0
  451.     }
  452.  
  453.     return 1
  454. }
  455.  
  456. boot_run_hook() {
  457.     local hook="$1"
  458.     local func
  459.  
  460.     while boot_hook_shift "$hook" func; do
  461.         local ran; eval "ran=\$PI_RAN_$func"
  462.         [ -n "$ran" ] || {
  463.             export -n "PI_RAN_$func=1"
  464.             $func "$1" "$2"
  465.         }
  466.     done
  467. }
  468.  
  469. find_mtd_part() {
  470.     local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
  471.     local PREFIX=/dev/mtdblock
  472.  
  473.     PART="${PART##mtd}"
  474.     [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
  475.     echo "${PART:+$PREFIX$PART}"
  476. }
  477.  
  478. find_mmc_part() {
  479.     local DEVNAME PARTNAME
  480.  
  481.     for DEVNAME in $(find /sys/block/mmcblk*/ -name 'mmcblk*p*'); do
  482.         PARTNAME=$(grep PARTNAME ${DEVNAME}/uevent | cut -f2 -d'=')
  483.         [ "$PARTNAME" = "$1" ] && echo "/dev/$(basename $DEVNAME)" && return 0
  484.     done
  485. }
  486.  
  487. jffs2_ready () {
  488.     mtdpart="$(find_mtd_part rootfs_data)"
  489.     [ -z "$mtdpart" ] && return 1
  490.     magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
  491.     [ "$magic" != "deadc0de" ]
  492. }
  493.  
  494. dupe() { # <new_root> <old_root>
  495.     cd $1
  496.     echo -n "creating directories... "
  497.     {
  498.         cd $2
  499.         find . -xdev -type d
  500.         echo "./dev ./overlay ./mnt ./proc ./tmp"
  501.         # xdev skips mounted directories
  502.         cd $1
  503.     } | xargs mkdir -p
  504.     echo "done"
  505.  
  506.     echo -n "setting up symlinks... "
  507.     for file in $(cd $2; find . -xdev -type f;); do
  508.         case "$file" in
  509.         ./rom/note) ;; #nothing
  510.         ./etc/config*|\
  511.         ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
  512.         *) ln -sf /rom/${file#./*} $file;;
  513.         esac
  514.     done
  515.     for file in $(cd $2; find . -xdev -type l;); do
  516.         cp -af $2/${file#./*} $file
  517.     done
  518.     echo "done"
  519. }
  520.  
  521. pivot() { # <new_root> <old_root>
  522.     mount -o move /proc $1/proc && \
  523.     pivot_root $1 $1$2 && {
  524.         mount -o move $2/dev /dev
  525.         mount -o move $2/tmp /tmp
  526.         mount -o move $2/sys /sys 2>&-
  527.         mount -o move $2/overlay /overlay 2>&-
  528.         return 0
  529.     }
  530. }
  531.  
  532. fopivot() { # <rw_root> <ro_root> <dupe?>
  533.     root=$1
  534.     {
  535.         if grep -q overlay /proc/filesystems; then
  536.             mount -t overlayfs -olowerdir=/,upperdir=$1 "overlayfs:$1" /mnt && root=/mnt
  537.         elif grep -q mini_fo /proc/filesystems; then
  538.             mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
  539.         else
  540.             mount --bind / /mnt
  541.             mount --bind -o union "$1" /mnt && root=/mnt
  542.         fi
  543.     } || {
  544.         [ "$3" = "1" ] && {
  545.         mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
  546.         dupe $1 $rom
  547.         }
  548.     }
  549.     pivot $root $2
  550. }
  551.  
  552. ramoverlay() {
  553.     mkdir -p /tmp/root
  554.     mount -t tmpfs -o mode=0755 root /tmp/root
  555.     fopivot /tmp/root /rom 1
  556. }
  557.  
  558. / # /lib/config/uci.sh
  559. / # cat /lib/config/uci.sh
  560. #!/bin/sh
  561. # Shell script compatibility wrappers for /sbin/uci
  562. #
  563. # Copyright (c) 2013 The Linux Foundation. All rights reserved.
  564. # Copyright (C) 2008-2010  OpenWrt.org
  565. # Copyright (C) 2008  Felix Fietkau <nbd@openwrt.org>
  566. #
  567. # This program is free software; you can redistribute it and/or modify
  568. # it under the terms of the GNU General Public License as published by
  569. # the Free Software Foundation; either version 2 of the License, or
  570. # (at your option) any later version.
  571. #
  572. # This program is distributed in the hope that it will be useful,
  573. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  574. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  575. # General Public License for more details.
  576. #
  577. # You should have received a copy of the GNU General Public License
  578. # along with this program; if not, write to the Free Software
  579. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  580.  
  581. CONFIG_APPEND=
  582. uci_load() {
  583.     local PACKAGE="$1"
  584.     local DATA
  585.     local RET
  586.     local VAR
  587.  
  588.     _C=0
  589.     if [ -z "$CONFIG_APPEND" ]; then
  590.         for VAR in $CONFIG_LIST_STATE; do
  591.             export ${NO_EXPORT:+-n} CONFIG_${VAR}=
  592.             export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
  593.         done
  594.         export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
  595.         export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
  596.         export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
  597.         export ${NO_EXPORT:+-n} CONFIG_SECTION=
  598.     fi
  599.  
  600.     DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
  601.     RET="$?"
  602.     [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
  603.     unset DATA
  604.  
  605.     ${CONFIG_SECTION:+config_cb}
  606.     return "$RET"
  607. }
  608.  
  609. uci_set_default() {
  610.     local PACKAGE="$1"
  611.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -q show "$PACKAGE" > /dev/null && return 0
  612.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} import "$PACKAGE"
  613.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit "$PACKAGE"
  614. }
  615.  
  616. uci_revert_state() {
  617.     local PACKAGE="$1"
  618.     local CONFIG="$2"
  619.     local OPTION="$3"
  620.  
  621.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
  622. }
  623.  
  624. uci_set_state() {
  625.     local PACKAGE="$1"
  626.     local CONFIG="$2"
  627.     local OPTION="$3"
  628.     local VALUE="$4"
  629.  
  630.     [ "$#" = 4 ] || return 0
  631.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
  632. }
  633.  
  634. uci_toggle_state() {
  635.     uci_revert_state "$1" "$2" "$3"
  636.     uci_set_state "$1" "$2" "$3" "$4"
  637. }
  638.  
  639. uci_set() {
  640.     local PACKAGE="$1"
  641.     local CONFIG="$2"
  642.     local OPTION="$3"
  643.     local VALUE="$4"
  644.  
  645.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
  646. }
  647.  
  648. uci_add_list() {
  649.     local PACKAGE="$1"
  650.     local CONFIG="$2"
  651.     local OPTION="$3"
  652.     local VALUE="$4"
  653.  
  654.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} add_list "$PACKAGE.$CONFIG.$OPTION=$VALUE"
  655. }
  656.  
  657. uci_get_state() {
  658.     uci_get "$1" "$2" "$3" "$4" "/var/state"
  659. }
  660.  
  661. uci_get() {
  662.     local PACKAGE="$1"
  663.     local CONFIG="$2"
  664.     local OPTION="$3"
  665.     local DEFAULT="$4"
  666.     local STATE="$5"
  667.  
  668.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${STATE:+-P $STATE} -q get "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
  669.     RET="$?"
  670.     [ "$RET" -ne 0 ] && [ -n "$DEFAULT" ] && echo "$DEFAULT"
  671.     return "$RET"
  672. }
  673.  
  674. uci_add() {
  675.     local PACKAGE="$1"
  676.     local TYPE="$2"
  677.     local CONFIG="$3"
  678.  
  679.     if [ -z "$CONFIG" ]; then
  680.         export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
  681.     else
  682.         /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} set "$PACKAGE.$CONFIG=$TYPE"
  683.         export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
  684.     fi
  685. }
  686.  
  687. uci_rename() {
  688.     local PACKAGE="$1"
  689.     local CONFIG="$2"
  690.     local VALUE="$3"
  691.  
  692.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} rename "$PACKAGE.$CONFIG=$VALUE"
  693. }
  694.  
  695. uci_remove() {
  696.     local PACKAGE="$1"
  697.     local CONFIG="$2"
  698.     local OPTION="$3"
  699.  
  700.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
  701. }
  702.  
  703. uci_commit() {
  704.     local PACKAGE="$1"
  705.     /sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} commit $PACKAGE
  706. }
  707.  
Advertisement
RAW Paste Data Copied
Advertisement