From 0a7498abb0168cea08e49be33c76e2d02e548012 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 30 Oct 2015 18:25:02 -0400 Subject: tools/shmin.sh: Detect syntax errors --- 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 -- cgit v0.9.1