add some partially crazy, but effective optimizations to webif
authorFelix Fietkau <nbd@openwrt.org>
Sun, 9 Oct 2005 23:03:47 +0000 (23:03 +0000)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 9 Oct 2005 23:03:47 +0000 (23:03 +0000)
SVN-Revision: 2090

openwrt/package/webif/files/usr/lib/webif/common.awk
openwrt/package/webif/files/usr/lib/webif/form.awk
openwrt/package/webif/files/usr/lib/webif/webif.sh
openwrt/package/webif/files/www/cgi-bin/webif/config.sh
openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh
openwrt/package/webif/files/www/cgi-bin/webif/info.sh
openwrt/package/webif/files/www/cgi-bin/webif/lan.sh
openwrt/package/webif/files/www/cgi-bin/webif/system.sh
openwrt/package/webif/files/www/cgi-bin/webif/wan.sh
openwrt/package/webif/files/www/cgi-bin/webif/wireless-config.sh

index d7e50e24614674ca95ac2399785c5f26db4cfe1a..c6f180b729c6ece5bf782c6172e350379ce54460 100644 (file)
@@ -1,5 +1,5 @@
 function start_form(title, field_opts) {
-       print "<div class=\"settings\">"
+       print "<div class=\"settings\"" field_opts ">"
        if (title != "") print "<div class=\"settings-title\"><h3><strong>" title "</strong></h3></div>"
        print "<div class=\"settings-content\">"
 }
index c6bf747a4a6d62fbafc2900830992bb1c47dcb5f..844a56c4b46c50f86b26214e5cc2c6720e4c0c5e 100644 (file)
@@ -14,7 +14,7 @@ BEGIN {
        gsub(/^[ \t]+/,"",$1)
 }
 
-($1 != "") && ($1 !~ /^option/) {
+($1 != "") && ($1 !~ /^option/) && (select_open == 1) {
        select_open = 0
        print "</select>"
 }
index cd8115b7e43f835cd1d23a8da37384b7c0ff530a..973099cd0be908fc6139893222b62bb323b6172b 100644 (file)
@@ -4,7 +4,7 @@ cgidir=/www/cgi-bin/webif
 rootdir=/cgi-bin/webif
 indexpage=index.sh
 
-# workarounds for stupid busybox fork/exec on [ ]
+# workarounds for stupid busybox slowness on [ ]
 empty() {
        case "$1" in
                "") return 0 ;;
@@ -17,6 +17,10 @@ equal() {
                *) return 255 ;;
        esac
 }
+# very crazy, but also very fast :-)
+exists() {
+       ( < $1 ) 2>&-
+}
 
 categories() {
        grep '##WEBIF:' $cgidir/.categories $cgidir/*.sh 2>/dev/null | \
@@ -54,7 +58,7 @@ header() {
        _uptime="${_uptime#*up }"
        _uptime="${_uptime%%,*}"
        _hostname=$(cat /proc/sys/kernel/hostname)
-       _version=$(cat /etc/banner | grep "(")
+       _version=$( grep "(" /etc/banner )
        _version="${_version%% ---*}"
        _head="${3:+<div class=\"settings-block-title\"><h2>$3$_saved_title</h2></div>}"
        _form="${5:+<form enctype=\"multipart/form-data\" action=\"$5\" method=\"post\"><input type="hidden" name="submit" value="1" />}"
@@ -175,14 +179,18 @@ apply_passwd() {
        case ${SERVER_SOFTWARE%% *} in
                busybox)
                        echo -n '/cgi-bin/webif:' > /etc/httpd.conf
-                       cat /etc/passwd | grep root | cut -d: -f1,2 >> /etc/httpd.conf
+                       grep root /etc/passwd | cut -d: -f1,2 >> /etc/httpd.conf
                        killall -HUP httpd
                        ;;
        esac
 }
 
 display_form() {
-       echo "$1" | awk -F'|' -f /usr/lib/webif/common.awk -f /usr/lib/webif/form.awk
+       if empty "$1"; then
+               awk -F'|' -f /usr/lib/webif/common.awk -f /usr/lib/webif/form.awk
+       else
+               echo "$1" | awk -F'|' -f /usr/lib/webif/common.awk -f /usr/lib/webif/form.awk
+       fi
 }
 
 list_remove() {
@@ -229,26 +237,35 @@ handle_list() {
 }
 
 load_settings() {
-       [ \! "$1" = "nvram" -a -f /etc/config/$1 ] && . /etc/config/$1
-       [ -f /tmp/.webif/config-$1 ] && . /tmp/.webif/config-$1
+       equal "$1" "nvram" || {
+               exists /etc/config/$1 && . /etc/config/$1
+       }
+       exists /tmp/.webif/config-$1 && . /tmp/.webif/config-$1
 }
 
 validate() {
-       eval "$(echo "$1" | awk -f /usr/lib/webif/validate.awk)"
+       if empty "$1"; then
+               eval "$(awk -f /usr/lib/webif/validate.awk)"
+       else
+               eval "$(echo "$1" | awk -f /usr/lib/webif/validate.awk)"
+       fi
 }
 
 
 save_setting() {
-       mkdir -p /tmp/.webif
+       exists /tmp/.webif/* || mkdir -p /tmp/.webif
        oldval=$(eval "echo \${$2}")
        oldval=${oldval:-$(nvram get "$2")}
        grep "^$2=" /tmp/.webif/config-$1 >&- 2>&- && {
-               mv /tmp/.webif/config-$1 /tmp/.webif/config-$1-old 2>&- >&-
-               grep -v "^$2=" /tmp/.webif/config-$1-old > /tmp/.webif/config-$1 2>&- 
+               grep -v "^$2=" /tmp/.webif/config-$1 > /tmp/.webif/config-$1-new 2>&- 
+               mv /tmp/.webif/config-$1-new /tmp/.webif/config-$1 2>&- >&-
                oldval=""
        }
        equal "$oldval" "$3" || echo "$2=\"$3\"" >> /tmp/.webif/config-$1
-       rm -f /tmp/.webif/config-$1-old
 }
 
 
+is_bcm947xx() {
+       read _systype < /proc/cpuinfo
+       equal "${_systype##* }" "BCM947XX"
+}
index b024561203c4b2f506946bfd8b9e83fc9eb9b1a0..373508d09c85d7e55e4034e6da8b6ceb0843df4a 100755 (executable)
@@ -26,8 +26,8 @@ case "$FORM_mode" in
                        }
                done
                CONFIGFILES=""
-               for configname in $(ls file-* 2>&-); do
-                       CONFIGFILES="$CONFIGFILES ${configname#file-}"
+               for configname in file-*; do
+                       exists "$configname" && CONFIGFILES="$CONFIGFILES ${configname#file-}"
                done
                CONFIGFILES="${CONFIGFILES:+<h3 style="display:inline">Config files: </h3>$CONFIGFILES<br />}"
                echo $CONFIGFILES
index 72c7c62176b055addbf0b0c7836cc16c6bb2e55f..ff7d9dcbf04fce74b0b2a85ed01d142ca28815a0 100755 (executable)
@@ -2,12 +2,13 @@
 <? 
 . /usr/lib/webif/webif.sh
 
-[ -f /tmp/.webif/file-hosts ] && HOSTS_FILE=/tmp/.webif/file-hosts || HOSTS_FILE=/etc/hosts
-[ -f /tmp/.webif/file-ethers ] && ETHERS_FILE=/tmp/.webif/file-ethers || ETHERS_FILE=/etc/ethers
-touch $HOSTS_FILE $ETHERS_FILE >&- 2>&-
+exists /tmp/.webif/file-hosts  && HOSTS_FILE=/tmp/.webif/file-hosts || HOSTS_FILE=/etc/hosts
+exists /tmp/.webif/file-ethers  && ETHERS_FILE=/tmp/.webif/file-ethers || ETHERS_FILE=/etc/ethers
+exists $HOSTS_FILE || touch $HOSTS_FILE >&- 2>&-
+exists $ETHERS_FILE || touch $ETHERS_FILE >&- 2>&-
 
 update_hosts() {
-       mkdir -p /tmp/.webif
+       exists /tmp/.webif/* || mkdir -p /tmp/.webif
        awk -v "mode=$1" -v "ip=$2" -v "name=$3" '
 BEGIN {
        FS="[ \t]"
@@ -37,13 +38,13 @@ processed == 0 {
 }
 END {
        if ((mode == "add") && (host_added == 0)) print ip "    " name
-}' - < "$HOSTS_FILE" > /tmp/.webif/file-hosts-new
+}' "$HOSTS_FILE" > /tmp/.webif/file-hosts-new
        mv "/tmp/.webif/file-hosts-new" "/tmp/.webif/file-hosts"
        HOSTS_FILE=/tmp/.webif/file-hosts
 }
 
 update_ethers() {
-       mkdir -p /tmp/.webif
+       exists /tmp/.webif/* || mkdir -p /tmp/.webif
        case "$1" in
                add)
                        grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new
@@ -60,14 +61,21 @@ update_ethers() {
 
 empty "$FORM_add_host" || {
        # add a host to /etc/hosts
-       validate "ip|FORM_host_ip|IP Address|required|$FORM_host_ip
-hostname|FORM_host_name|Hostname|required|$FORM_host_name" && update_hosts add "$FORM_host_ip" "$FORM_host_name"
+       validate <<EOF
+ip|FORM_host_ip|IP Address|required|$FORM_host_ip
+hostname|FORM_host_name|Hostname|required|$FORM_host_name
+EOF
+       equal "$?" 0 && update_hosts add "$FORM_host_ip" "$FORM_host_name"
 }
 empty "$FORM_add_dhcp" || {
        # add a host to /etc/ethers
-       validate "mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac
-ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip" && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip"
+       validate <<EOF
+mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac
+ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip
+EOF
+       equal "$?" 0 && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip"
 }
+
 empty "$FORM_remove_host" || update_hosts del "$FORM_remove_ip" "$FORM_remove_name"
 empty "$FORM_remove_dhcp" || update_ethers del "$FORM_remove_mac"
 
index 10e3e8c754b07f7c1976a81be298bd07e2fa4f3c..36b1449da94e4aed6f51afbc0c34cb0d31217ff3 100755 (executable)
@@ -7,7 +7,7 @@ header "Info" "Router Info" "Router Info"
 <tbody>
        <tr>
                <td>Firmware Version</td>
-               <td><? cat /etc/banner | grep "(" | cut -b-20 | cut -b2- ?></td>
+               <td><? grep "(" /etc/banner | cut -b-20 | cut -b2- ?></td>
        </tr>
        <tr>
                <td>Kernel Version</td>
index 8ce03b4b570a9c502f69fd84bfebac9d71438162..b853a7b6ebfdc79c826b0ffb2b707f549aef0ece 100755 (executable)
@@ -17,10 +17,12 @@ if empty "$FORM_submit"; then
        FORM_lan_gateway=${lan_gateway:-$(nvram get lan_gateway)}
 else 
        SAVED=1
-       validate "
+       validate <<EOF
 ip|FORM_lan_ipaddr|LAN IP|required|$FORM_lan_ipaddr
 netmask|FORM_lan_netmask|LAN network mask|required|$FORM_lan_netmask
-ip|FORM_lan_gateway|LAN gateway||$FORM_lan_gateway" && {
+ip|FORM_lan_gateway|LAN gateway||$FORM_lan_gateway
+EOF
+       equal "$?" 0 && {
                save_setting network lan_ipaddr $FORM_lan_ipaddr
                save_setting network lan_netmask $FORM_lan_netmask
                save_setting network lan_gateway $FORM_lan_gateway
@@ -29,7 +31,8 @@ fi
 
 header "Network" "LAN" "LAN settings" '' "$SCRIPT_NAME"
 
-display_form "start_form|LAN Configuration
+display_form <<EOF
+start_form|LAN Configuration
 field|IP Address
 text|lan_ipaddr|$FORM_lan_ipaddr
 field|Netmask
@@ -41,7 +44,8 @@ start_form|DNS Servers
 listedit|dns|$SCRIPT_NAME?|$FORM_dns|$FORM_dnsadd
 helpitem|Note
 helptext|You should save your settings on this page before adding/removing DNS servers
-end_form" 
+end_form
+EOF
 
 footer ?>
 <!--
index 212b3d65fa5ba7dba6153c2d1914d4c2593dfd1c..ffddf19c70fa01fe4480d71d973761dbd5508022 100755 (executable)
@@ -7,15 +7,18 @@ load_settings nvram
 if empty "$FORM_submit"; then
        FORM_hostname=${wan_hostname:-$(nvram get wan_hostname)}
        FORM_hostname=${FORM_hostname:-OpenWrt}
-       grep BCM947 /proc/cpuinfo 2>&- >&- && {
+       is_bcm947xx && {
                FORM_boot_wait=${boot_wait:-$(nvram get boot_wait)}
                FORM_boot_wait=${FORM_boot_wait:-off}
        }
 else
        SAVED=1
-       validate "hostname|FORM_hostname|Hostname|nodots required|$FORM_hostname" && {
+       validate <<EOF
+hostname|FORM_hostname|Hostname|nodots required|$FORM_hostname
+EOF
+       equal "$?" 0 && {
                save_setting system wan_hostname $FORM_hostname
-               grep BCM947 /proc/cpuinfo 2>&- >&- && {
+               is_bcm947xx && {
                        case "$FORM_boot_wait" in
                                on|off) save_setting nvram boot_wait $FORM_boot_wait;;
                        esac
@@ -25,16 +28,18 @@ fi
 
 header "System" "Settings" "System settings" '' "$SCRIPT_NAME"
 
-grep BCM947 /proc/cpuinfo 2>&- >&- && bootwait_form="field|boot_wait
+is_bcm947xx && bootwait_form="field|boot_wait
 radio|boot_wait|$FORM_boot_wait|on|On<br />
 radio|boot_wait|$FORM_boot_wait|off|Off"
 
-display_form "start_form|System settings
+display_form <<EOF
+start_form|System settings
 field|Hostname
 text|hostname|$FORM_hostname
 $bootwait_form
 field
-end_form"
+end_form
+EOF
 
 footer ?>
 
index 1d6202906c2ec1e8b1523c0806de2e1f145bafdb..656bdbc7a68792392552854c38fe8a1222f7cc6f 100755 (executable)
@@ -69,12 +69,13 @@ else
                        ;;
        esac
 
-       # FIXME: add validation for DNS server list
-       validate "
+validate <<EOF
 ip|FORM_wan_ipaddr|IP address|$V_IP|$FORM_wan_ipaddr
 netmask|FORM_wan_netmask|network mask|$V_NM|$FORM_wan_netmask
 ip|FORM_wan_gateway|gateway address||$FORM_wan_gateway
-ip|FORM_pptp_server_ip|PPTP server IP|$V_PPTP|$FORM_pptp_server_ip" && {
+ip|FORM_pptp_server_ip|PPTP server IP|$V_PPTP|$FORM_pptp_server_ip
+EOF
+       equal "$?" 0 && {
                save_setting network wan_proto $FORM_wan_proto
                
                # Settings specific to one protocol type
@@ -116,7 +117,8 @@ ip|FORM_pptp_server_ip|PPTP server IP|$V_PPTP|$FORM_pptp_server_ip" && {
 fi
 
 header "Network" "WAN" "WAN settings" ' onLoad="modechange()" ' "$SCRIPT_NAME"
-?>
+
+cat <<EOF
 <script type="text/javascript" src="/webif.js "></script>
 <script type="text/javascript">
 <!--
@@ -143,11 +145,14 @@ function modechange()
 }
 -->
 </script>
-<? display_form "start_form|WAN Configuration
+EOF
+
+display_form <<EOF
+start_form|WAN Configuration
 field|Internet Connection Type
-radio|wan_proto|$FORM_wan_proto|none|None<br />|onchange=\"modechange()\"
-radio|wan_proto|$FORM_wan_proto|dhcp|DHCP<br />|onchange=\"modechange()\"
-radio|wan_proto|$FORM_wan_proto|static|Static IP<br />|onchange=\"modechange()\"
+radio|wan_proto|$FORM_wan_proto|none|None<br />|onchange="modechange()"
+radio|wan_proto|$FORM_wan_proto|dhcp|DHCP<br />|onchange="modechange()"
+radio|wan_proto|$FORM_wan_proto|static|Static IP<br />|onchange="modechange()"
 $PPPOE_OPTION
 $PPTP_OPTION
 end_form
@@ -170,8 +175,8 @@ end_form
 
 start_form|PPP Settings|ppp_settings|hidden
 field|PPP Redial Policy|ppp_redial|hidden
-radio|ppp_redial|$FORM_ppp_redial|demand|Connect on Demand<br />|onChange=\"modechange()\"
-radio|ppp_redial|$FORM_ppp_redial|persist|Keep Alive|onChange=\"modechange()\"
+radio|ppp_redial|$FORM_ppp_redial|demand|Connect on Demand<br />|onChange="modechange()"
+radio|ppp_redial|$FORM_ppp_redial|persist|Keep Alive|onChange="modechange()"
 field|Maximum Idle Time|ppp_demand_idletime|hidden
 text|ppp_idletime|$FORM_ppp_idletime
 field|Redial Timeout|ppp_persist_redialperiod|hidden
@@ -182,7 +187,8 @@ field|PPP Password|ppp_passwd|hidden
 text|ppp_passwd|$FORM_ppp_passwd
 field|PPP MTU|ppp_mtu|hidden
 text|ppp_mtu|$FORM_ppp_mtu
-end_form" 
+end_form
+EOF
 
 footer ?>
 <!--
index 36bf1b4473885d61dd98ba97661ffb4f845bccb4..fd3d726d6dae9c3f6a9783ede817ad60f877db8a 100755 (executable)
@@ -101,7 +101,7 @@ else
                psk) V_PSK="required";;
        esac
 
-       validate "
+       validate <<EOF
 ip|FORM_radius_ipaddr|RADIUS IP address|$V_RADIUS|$FORM_radius_ipaddr
 wep|FORM_key1|WEP key 1||$FORM_key1
 wep|FORM_key2|WEP key 2||$FORM_key2
@@ -110,7 +110,9 @@ wep|FORM_key4|WEP key 4||$FORM_key4
 string|FORM_wpa_psk|WPA pre-shared key|min=8 max=63 $V_PSK|$FORM_wpa_psk
 string|FORM_radius_key|RADIUS server key|min=4 max=63 $V_RADIUS|$FORM_radius_key
 string|FORM_ssid|ESSID|required|$FORM_ssid
-int|FORM_channel|Channel|required min=1 max=$CHANNEL_MAX|$FORM_channel" && {
+int|FORM_channel|Channel|required min=1 max=$CHANNEL_MAX|$FORM_channel
+EOF
+       equal "$?" 0 && {
 
                if equal "$FORM_mode" adhoc; then
                        FORM_mode=sta
@@ -211,7 +213,8 @@ function modechange()
 
 EOF
 
-display_form "start_form|Wireless Configuration
+display_form <<EOF
+start_form|Wireless Configuration
 field|ESSID
 text|ssid|$FORM_ssid
 helpitem|ESSID
@@ -220,20 +223,20 @@ field|Channel
 select|channel|$FORM_channel
 $F_CHANNELS
 field|Mode
-radio|mode|$FORM_mode|ap|Access Point<br />|onChange=\"modechange()\
-radio|mode|$FORM_mode|sta|Client <br />|onChange=\"modechange()\
-radio|mode|$FORM_mode|wet|Bridge <br />|onChange=\"modechange()\
-radio|mode|$FORM_mode|adhoc|Ad-Hoc|onChange=\"modechange()\
+radio|mode|$FORM_mode|ap|Access Point<br />|onChange="modechange()
+radio|mode|$FORM_mode|sta|Client <br />|onChange="modechange()
+radio|mode|$FORM_mode|wet|Bridge <br />|onChange="modechange()
+radio|mode|$FORM_mode|adhoc|Ad-Hoc|onChange="modechange()
 helpitem|Mode
 helptext|Operation mode
 helplink|http://wiki.openwrt.org/OpenWrtDocs/Configuration#head-7126c5958e237d603674b3a9739c9d23bdfdb293
 end_form
 start_form|Encryption settings
 field|Encryption type
-radio|encryption|$FORM_encryption|off|Disabled <br />|onChange=\"modechange()\"
-radio|encryption|$FORM_encryption|wep|WEP <br />|onChange=\"modechange()\"
-radio|encryption|$FORM_encryption|psk|WPA (preshared key) <br />|onChange=\"modechange()\"
-radio|encryption|$FORM_encryption|wpa|WPA (RADIUS)|onChange=\"modechange()\"
+radio|encryption|$FORM_encryption|off|Disabled <br />|onChange="modechange()"
+radio|encryption|$FORM_encryption|wep|WEP <br />|onChange="modechange()"
+radio|encryption|$FORM_encryption|psk|WPA (preshared key) <br />|onChange="modechange()"
+radio|encryption|$FORM_encryption|wpa|WPA (RADIUS)|onChange="modechange()"
 field|WPA support|wpa_support|hidden
 checkbox|wpa1|$FORM_wpa1|wpa1|WPA1
 checkbox|wpa2|$FORM_wpa2|wpa2|WPA2
@@ -260,7 +263,8 @@ start_form|WDS connections
 listedit|wds|$SCRIPT_NAME?|$FORM_wds|$FORM_wdsadd
 helpitem|Note
 helptext|You should save your settings on this page before adding/removing WDS links
-end_form"
+end_form
+EOF
 
 footer ?>
 <!--