Strip trailing whitespace before processing line.

This commit is contained in:
Darren 'Tadgy' Austin 2019-07-20 19:19:20 +01:00
commit b1033f942e

103
parse_ini
View file

@ -346,77 +346,25 @@ parse_ini() {
# Strip the trailing whitespace from the line (the leading whitespace has already been stripped by read). # Strip the trailing whitespace from the line (the leading whitespace has already been stripped by read).
LINE="${LINE/%*([[:blank:]])/}" LINE="${LINE/%*([[:blank:]])/}"
printf "<%s>\n" "$LINE"
# Quotes must be balanced.
if { [[ "$LINE" =~ ^[[:blank:]]*[\"\'][[:blank:]]*\[ ]] && [[ ! "$LINE" =~ \][[:blank:]]*[\"\'][[:blank:]]*$ ]]; } || \
{ [[ "$LINE" =~ \][[:blank:]]*[\"\'][[:blank:]]*$ ]] && [[ ! "$LINE" =~ ^[[:blank:]]*[\"\'][[:blank:]]*\[ ]]; }; then
echo "${0##*/}: line $LINENUMBER: unmatched quotes - skipping section" >&2
IGNORE_SECTION=1
continue
elif [[ "$LINE" =~ ^[[:blank:]]*[\"\'][[:blank:]]*\[ ]] && [[ "$LINE" =~ \][[:blank:]]*[\"\'][[:blank:]]*$ ]]; then
# Strip the quotes and whitespace - they're not needed.
LINE="${LINE/#*([[:blank:]])*([\"\'])*([[:blank:]])[/[}"
LINE="${LINE/%]*([[:blank:]])*([\"\'])*([[:blank:]])/]}"
printf "*%s*\n" "$LINE"
elif { [[ "$LINE" =~ ^[[:blank:]]*[\"\'] ]] && [[ ! "$LINE" =~ [\"\'][[:blank:]]*$KEYVALUE_DELIM ]]; } || \
{ [[ "$LINE" =~ ${KEYVALUE_DELIM}[[:blank:]]*[\"\'] ]] && [[ ! "$LINE" =~ [\"\'][[:blank:]]*$ ]]; }; then
echo "${0##*/}: line $LINENUMBER: unmatched quotes - skipping property" >&2
continue
elif [[ "$LINE" =~ ^[[:blank:]]*[\"\'] ]] || [[ "$LINE" =~ [\"\'][[:blank:]]*$ ]]; then
# Strip the quotes and whitespace - they're not needed.
LINE="${LINE/#*([[:blank:]])*([\"\'])*([[:blank:]])}"
LINE="${LINE/*([\"\'])*([[:blank:]])$KEYVALUE_DELIM*([[:blank:]])*([\"\'])/$KEYVALUE_DELIM}"
LINE="${LINE/%*([[:blank:]])*([\"\'])*([[:blank:]])}"
printf "|%s|\n" "$LINE"
elif [[ "$LINE" =~ ^[[:blank:]]*.*[[:blank:]]*$KEYVALUE_DELIM[[:blank:]]*.*[[:blank:]] ]]; then
echo "valid"
else
echo "${0##*/}: line $LINENUMBER: invalid formatting - skipping line" >&2
continue
fi
# exit
# if [[ "${LINE:0:1}" =~ [\"\'] ]]; then
# if [[ "${LINE:0:1}" == "${LINE: -1:1}" ]]; then
# # Strip the quotes as they're not needed.
# LINE="${LINE:1:-1}"
# else
# echo "${0##*/}: line $LINENUMBER: unmatched quotes - skipping property" >&2
# continue
# fi
# fi
# Process the line. # Process the line.
# if [[ "${LINE:0:1}" == "[" ]]; then # Found the beginning of a section definition. if [[ "${LINE:0:1}" == "[" ]]; then # Found the beginning of a section definition.
# # Check the format of the section definition.
# if [[ "${LINE: -1:1}" != "]" ]]; then
if [[ "$LINE" =~ ^[[:blank:]]*\[ ]]; then # Found the beginning of a section definition.
# Check the format of the section definition. # Check the format of the section definition.
if [[ ! "$LINE" =~ \][[:blank:]]*$ ]]; then if [[ "${LINE: -1:1}" != "]" ]]; then
echo "${0##*/}: line $LINENUMBER: unmatched [ in section definition - skipping section" >&2 echo "${0##*/}: line $LINENUMBER: unmatched [ in section definition - ignoring section" >&2
IGNORE_SECTION=1 IGNORE_SECTION=1
continue continue
elif [[ "${LINE:1:-1}" =~ [^$ACCEPTABLE_CHARS\[\]]* ]]; then elif [[ "${LINE:1:-1}" =~ [^$ACCEPTABLE_CHARS\[\]]* ]]; then
#printf "<%s>\n" "$LINE" echo "${0##*/}: line $LINENUMBER: invalid characters in section definition - ignoring section" >&2
#exit
echo "${0##*/}: line $LINENUMBER: invalid characters in section definition - skipping section" >&2
IGNORE_SECTION=1 IGNORE_SECTION=1
continue continue
elif [[ -z "${LINE:1:-1}" ]] || [[ "${LINE:1:-1}" =~ ^[[:blank:]]+$ ]]; then elif [[ -z "${LINE:1:-1}" ]] || [[ "${LINE:1:-1}" =~ ^[[:blank:]]+$ ]]; then
echo "${0##*/}: line $LINENUMBER: empty section definition - skipping section" >&2 echo "${0##*/}: line $LINENUMBER: empty section definition - ignoring section" >&2
IGNORE_SECTION=1 IGNORE_SECTION=1
continue continue
else else
# Strip the []s and any whitespace between the []s and the section name. # Strip the []s and any whitespace between the []s and the section name.
# LINE="${LINE/#\[*([[:blank:]])/}" LINE="${LINE/#\[*([[:space:]])/}"
# LINE="${LINE/%*([[:blank:]])\]/}" LINE="${LINE/%*([[:space:]])\]/}"
#printf "<%s>\n" "$LINE"
LINE="${LINE/#*([[:blank:]])\[*([[:blank:]])/}"
#printf "<%s>\n" "$LINE"
#exit
# LINE="${LINE/%*([[:blank:]])\]*([[:blank:]])/}"
# Squash multiple consecutive blanks into a single space. # Squash multiple consecutive blanks into a single space.
((SQUASH_SPACES == 1)) && LINE="${LINE//+([[:blank:]])/ }" ((SQUASH_SPACES == 1)) && LINE="${LINE//+([[:blank:]])/ }"
@ -442,15 +390,15 @@ printf "|%s|\n" "$LINE"
# Should we process repeat sections? # Should we process repeat sections?
if ((REPEAT_SECTIONS == 0)); then if ((REPEAT_SECTIONS == 0)); then
for SECTION in "${SECTIONS_SEEN[@]}"; do for SECTION in "${SEEN_SECTIONS[@]}"; do
if [[ "$CURRENT_SECTION" == "$SECTION" ]]; then if [[ "$CURRENT_SECTION" == "$SECTION" ]]; then
# It's a section we've seen before - don't process it. # It's a section we've seen before - don't process it.
echo "${0##*/}: line $LINENUMBER: repeated section name - skipping section" >&2 echo "${0##*/}: line $LINENUMBER: repeated section name - ignoring section" >&2
IGNORE_SECTION=1 IGNORE_SECTION=1
continue 2 continue 2
fi fi
done done
SECTIONS_SEEN+=("$CURRENT_SECTION") SEEN_SECTIONS+=("$CURRENT_SECTION")
fi fi
# Reset the ignore flag. # Reset the ignore flag.
@ -461,6 +409,17 @@ printf "|%s|\n" "$LINE"
printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION" printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION"
fi fi
elif ((IGNORE_SECTION == 0)) && [[ "$LINE" != *$KEYVALUE_DELIM* ]]; then # Process the property definition as if it's a boolean. elif ((IGNORE_SECTION == 0)) && [[ "$LINE" != *$KEYVALUE_DELIM* ]]; then # Process the property definition as if it's a boolean.
# If the value starts with a " or ' it must end with same.
if [[ "${LINE:0:1}" =~ [\"\'] ]]; then
if [[ "${LINE:0:1}" == "${VALUE: -1:1}" ]]; then
# Strip the quotes as they're not needed.
LINE="${LINE:1:-1}"
else
echo "${0##*/}: line $LINENUMBER: unmatched quotes - ignoring property" >&2
continue
fi
fi
# Determine the boolean value. # Determine the boolean value.
if [[ "${LINE:0:3}" == "no_" ]]; then if [[ "${LINE:0:3}" == "no_" ]]; then
LINE="${LINE:3:${#LINE} - 1}" LINE="${LINE:3:${#LINE} - 1}"
@ -481,7 +440,7 @@ printf "|%s|\n" "$LINE"
if ((USE_BOOLEANS == 1)); then if ((USE_BOOLEANS == 1)); then
printf "%s%s%s[\"%s\"]=\"%s\"\\n" "$PREFIX" "${PREFIX:+$DELIM}" "$CURRENT_SECTION" "$LINE" "$BOOL_VALUE" printf "%s%s%s[\"%s\"]=\"%s\"\\n" "$PREFIX" "${PREFIX:+$DELIM}" "$CURRENT_SECTION" "$LINE" "$BOOL_VALUE"
else else
echo "${0##*/}: line $LINENUMBER: key without a value - skipping property" >&2 echo "${0##*/}: line $LINENUMBER: key without a value - ignoring property" >&2
continue continue
fi fi
elif ((IGNORE_SECTION == 0)); then # Process the property definition as a key/value pair. elif ((IGNORE_SECTION == 0)); then # Process the property definition as a key/value pair.
@ -499,15 +458,15 @@ printf "|%s|\n" "$LINE"
((SQUASH_SPACES == 1)) && KEY="${KEY//+([[:blank:]])/ }" ((SQUASH_SPACES == 1)) && KEY="${KEY//+([[:blank:]])/ }"
# If the value starts with a " or ' it must end with same. # If the value starts with a " or ' it must end with same.
# if [[ "${VALUE:0:1}" =~ [\"\'] ]]; then if [[ "${VALUE:0:1}" =~ [\"\'] ]]; then
# if [[ "${VALUE:0:1}" == "${VALUE: -1:1}" ]]; then if [[ "${VALUE:0:1}" == "${VALUE: -1:1}" ]]; then
# # Strip the quotes as they're not needed. # Strip the quotes as they're not needed.
# VALUE="${VALUE:1:-1}" VALUE="${VALUE:1:-1}"
# else else
# echo "${0##*/}: line $LINENUMBER: unmatched quotes - skipping property" >&2 echo "${0##*/}: line $LINENUMBER: unmatched quotes - ignoring property" >&2
# continue continue
# fi fi
# fi fi
# Output the associative array element definition. # Output the associative array element definition.
# FIXME: If doing validation only, don't output declaration here. # FIXME: If doing validation only, don't output declaration here.