--- a/configure.sh +++ b/configure.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # # configure.sh Generates interactively a config.h from config.in # @@ -25,20 +25,9 @@ # Foundation; either version 2 of the License, or (at # your option) any later version. # -# -# Make sure we're really running bash. -# -# I would really have preferred to write this script in a language with -# better string handling, but alas, bash is the only scripting language -# that I can be reasonable sure everybody has on their Linux machine. -# - CONFIG=config.h MAKECONFIG=config.make - -[ -z "$BASH" ] && { echo "configure.sh requires bash" 1>&2; exit 1; } - cat <&2 exit 1 fi -exec 7<$1 -config_fd_redir='<&7' +exec 7<"$1" # # readln reads a line into $ans. # # readln prompt default # -function readln() +readln() { - echo -n "$1" - IFS='@' read ans || exit 1 + printf '%s' "$1" + IFS='@' read -r ans || exit 1 [ -z "$ans" ] && ans=$2 } @@ -75,21 +62,21 @@ # # bool tail # -function bool() +bool() { - # Slimier hack to get bash to rescan a line. + # Re-scan the line so shell quoting from config.in is preserved. eval "set -- $1" ans="" - while [ "$ans" != "y" -a "$ans" != "n" ] + while [ "$ans" != "y" ] && [ "$ans" != "n" ] do readln "$1 ($2) [$3] " "$3" done if [ "$ans" = "y" ]; then - echo "#define $2 1" >>${CONFIG} - echo "$2=1" >>${MAKECONFIG} + echo "#define $2 1" >>"${CONFIG}" + echo "$2=1" >>"${MAKECONFIG}" else - echo "#define $2 0" >>${CONFIG} - echo "# $2=0" >> ${MAKECONFIG} + echo "#define $2 0" >>"${CONFIG}" + echo "# $2=0" >>"${MAKECONFIG}" fi raw_input_line="bool '$1' $2 $ans" eval "$2=$ans" @@ -99,16 +86,42 @@ # # int tail # -function int() +is_integer() { - # Slimier hack to get bash to rescan a line. + case $1 in + ''|*[!0123456789+-]*|*[-+]*[-+]*) + return 1 + ;; + [+-]) + return 1 + ;; + esac + + case $1 in + [+-]*) + set -- "${1#?}" + ;; + esac + + case $1 in + ''|*[!0123456789]*) + return 1 + ;; + esac + + return 0 +} + +int() +{ + # Re-scan the line so shell quoting from config.in is preserved. eval "set -- $1" ans="x" - while [ $[$ans+0] != "$ans" ]; + while ! is_integer "$ans" do readln "$1 ($2) [$3] " "$3" done - echo "#define $2 ($ans)" >>${CONFIG} + echo "#define $2 ($ans)" >>"${CONFIG}" raw_input_line="int '$1' $2 $ans" eval "$2=$ans" } @@ -123,10 +136,10 @@ stack='' branch='t' - while IFS='@' eval read raw_input_line ${config_fd_redir} + while IFS='@' read -r raw_input_line <&7 do - # Slimy hack to get bash to rescan a line. - read cmd rest <<-END_OF_COMMAND + # Re-scan the line so shell quoting from config.in is preserved. + IFS=' ' read -r cmd rest <<-END_OF_COMMAND $raw_input_line END_OF_COMMAND @@ -135,18 +148,18 @@ echo "$raw_input_line" # echo "# $rest" >>$CONFIG if [ "$prevcmd" != "*" ]; then - echo >>${CONFIG} - echo "/* $rest" >>${CONFIG} + echo >>"${CONFIG}" + echo "/* $rest" >>"${CONFIG}" else - echo " * $rest" >>${CONFIG} + echo " * $rest" >>"${CONFIG}" fi prevcmd="*" fi else - [ "$prevcmd" = "*" ] && echo " */" >>${CONFIG} + [ "$prevcmd" = "*" ] && echo " */" >>"${CONFIG}" prevcmd="" case "$cmd" in - =) [ "$branch" = "t" ] && echo "$rest" >>${CONFIG};; + =) [ "$branch" = "t" ] && echo "$rest" >>"${CONFIG}";; :) [ "$branch" = "t" ] && echo "$raw_input_line" ;; int) [ "$branch" = "t" ] && int "$rest" ;; bool) [ "$branch" = "t" ] && bool "$rest" ;; @@ -160,12 +173,12 @@ else) if [ "$branch" = "t" ]; then branch=f else - read branch rest <<-END_OF_STACK - $stack - END_OF_STACK - fi ;; + IFS=' ' read -r branch rest <<-END_OF_STACK + $stack + END_OF_STACK + fi ;; fi) [ -z "$stack" ] && echo "Error! Extra fi." 1>&2 - read branch stack <<-END_OF_STACK + IFS=' ' read -r branch stack <<-END_OF_STACK $stack END_OF_STACK ;; @@ -173,7 +186,7 @@ fi echo "$raw_input_line" >>config.new done - [ "$prevcmd" = "*" ] && echo " */" >>${CONFIG} + [ "$prevcmd" = "*" ] && echo " */" >>"${CONFIG}" [ -z "$stack" ] || echo "Error! Unterminated if." 1>&2