diff options
author | P. J. McDermott <pj@pehjota.net> | 2015-10-30 18:25:02 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2015-10-30 18:25:02 (EDT) |
commit | 0a7498abb0168cea08e49be33c76e2d02e548012 (patch) | |
tree | 22b76b81ffea6aa677dfd77ee6c5ae241d1ed206 | |
parent | 1cc3c0f2fbc80b6382388e438ad29b17f261f593 (diff) | |
download | firman.sh-0a7498abb0168cea08e49be33c76e2d02e548012.zip firman.sh-0a7498abb0168cea08e49be33c76e2d02e548012.tar.gz firman.sh-0a7498abb0168cea08e49be33c76e2d02e548012.tar.bz2 |
tools/shmin.sh: Detect syntax errors
-rwxr-xr-x | tools/shmin.sh | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/tools/shmin.sh b/tools/shmin.sh index 4c3c40b..d6f35ea 100755 --- a/tools/shmin.sh +++ b/tools/shmin.sh @@ -45,17 +45,11 @@ fgetc() c="${c%.}" } -minify() +parse() { - local input="${1}" local buffer='' local escaped=false - # Open input file. - if ! exec 3<"${input}"; then - die 'Cannot open file "%s"' "${input}" - fi - # Check for magic number or other first-line comment. fgetc if [ "x${c}" = 'x#' ]; then @@ -112,6 +106,7 @@ minify() # Eat trailing whitespace. break ;; + '') return 1;; *) # Condense whitespace. buffer="${buffer} " @@ -127,6 +122,7 @@ minify() if [ "x${c}" = "x${LF}" ]; then break fi + [ "x${c}" = 'x' ] && return 1 done fi if [ "x${c}" = "x${LF}" ]; then @@ -136,6 +132,7 @@ minify() case "${c}" in ' ' | "${HT}" | "${LF}") ;; + '') return 1;; *) buffer="${buffer}${LF}" break @@ -148,6 +145,7 @@ minify() buffer="${buffer}${c}" fgetc buffer="${buffer}${c}" + [ "x${c}" = 'x' ] && return 1 continue fi if [ "x${c}" = "x'" ]; then @@ -159,6 +157,7 @@ minify() if [ "x${c}" = "x'" ]; then break fi + [ "x${c}" = 'x' ] && return 1 done fgetc continue @@ -180,6 +179,7 @@ minify() if [ "x${c}" = 'x"' ]; then break fi + [ "x${c}" = 'x' ] && return 1 done fgetc continue @@ -188,13 +188,32 @@ minify() fgetc done + printf '%s' "${buffer}" +} + +minify() +{ + local input="${1}" + local buffer='' + + # Open input file. + if ! exec 3<"${input}"; then + die 'Cannot open file "%s"' "${input}" + fi + + # Parse input. + if ! buffer="$(parse)"; then + die 'Syntax error in input' + exec 3<&- + fi + # We made it. Now close input file. if ! exec 3<&-; then die 'Cannot close file "%s"' "${input}" fi # Write the output. - if ! printf '%s' "${buffer}" >"${output}~"; then + if ! printf '%s\n' "${buffer}" >"${output}~"; then die 'Cannot write file "%s"' "${output}~" fi |