Browse Source

functions: Simplify write_state_file

There is really no reason to have a wrapper function to extract the names
when we're always using it the same way. So let's get rid of the long
function name for the wrapper function, and just parse defaults.sh directly
in write_state_file.

While we're at it, use a single awk script instead of a grep/sed combo to
extract the variables, and purge any duplicates.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
Toke Høiland-Jørgensen 3 years ago
parent
commit
804d42724d
2 changed files with 5 additions and 16 deletions
  1. 3 14
      src/functions.sh
  2. 2 2
      src/start-sqm

+ 3 - 14
src/functions.sh

@@ -234,24 +234,13 @@ do_modules() {
 # Write a state file to the filename given as $1. This version will extract all
 # variable names defined in defaults.sh and since defaults.sh should contain all
 # used variables this should be the complete set.
-write_defaults_vars_to_state_file() {
-    local filename
-    local defaultsFQN
-    local ALL_SQM_DEFAULTS_VARS
-    filename=$1
-    defaultsFQN=$2
-    ALL_SQM_DEFAULTS_VARS=$( grep -r -o -e "[[:alnum:][:punct:]]*=" ${defaultsFQN} | sed 's/=//' )
-
-    write_state_file ${filename} ${ALL_SQM_DEFAULTS_VARS}
-}
-
-# Write a state file to the filename given as $1. The remaining arguments are
-# variable names that should be written to the state file.
 write_state_file() {
     local filename
+    local awkscript
+    awkscript='match($0, /[A-Z0-9_]+=/) {print substr($0, RSTART, RLENGTH-1)}'
     filename=$1
     shift
-    for var in "$@"; do
+    awk "$awkscript" ${SQM_LIB_DIR}/defaults.sh | sort -u | while read var; do
         val=$(eval echo '$'$var)
         echo "$var=\"$val\""
     done > $filename

+ 2 - 2
src/start-sqm

@@ -57,10 +57,10 @@ sqm_log "Starting SQM script: ${SCRIPT} on ${IFACE}, in: ${DOWNLINK} Kbps, out:
 
 if fn_exists sqm_start ; then
     sqm_debug "Using script specific sqm_start function overriding the generic sqm_start_default."
-    sqm_start && write_defaults_vars_to_state_file ${STATE_FILE} ${SQM_LIB_DIR}/defaults.sh && sqm_log "${SCRIPT} was started on ${IFACE} successfully"
+    sqm_start && write_state_file ${STATE_FILE} && sqm_log "${SCRIPT} was started on ${IFACE} successfully"
 else
     sqm_debug "Using generic sqm_start_default function."
-    sqm_start_default && write_defaults_vars_to_state_file ${STATE_FILE} ${SQM_LIB_DIR}/defaults.sh && sqm_log "${SCRIPT} was started on ${IFACE} successfully"
+    sqm_start_default && write_state_file ${STATE_FILE} && sqm_log "${SCRIPT} was started on ${IFACE} successfully"
 fi
 
 exit 0