Skip to content

Commit

Permalink
Shell scripts: removed bashisms, made them dash-compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-galeotti committed Jan 4, 2023
1 parent dc3aa38 commit 4f54263
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 124 deletions.
117 changes: 45 additions & 72 deletions libutils/createconfiguregpr.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Create "configure.gpr" GNATMAKE project file.
Expand All @@ -15,6 +15,7 @@
# $2 = configure filename
#
# Environment variables:
# OSTYPE
# SWEETADA_PATH
# TOOLCHAIN_PREFIX
# TOOLCHAIN_NAME
Expand Down Expand Up @@ -80,12 +81,14 @@ return 0
################################################################################
print_I()
{
local _is
local _il
local _is
_il=${INDENTATION_LEVEL}
_is=""
for ((_il=1 ; _il<=INDENTATION_LEVEL ; _il++)) ; do
while [ "$((_il))" -gt 0 ] ; do
# Ada 3-space indentation style
_is+=" "
_is="${_is} "
_il=$((_il-1))
done
printf "%s\n" "${_is}$1" >> "${CONFIGURE_FILENAME}"
return 0
Expand All @@ -102,6 +105,30 @@ printf "%s\n" "" >> "${CONFIGURE_FILENAME}"
return 0
}

################################################################################
# print_list() #
# #
# Print a list of items item with indentation. #
################################################################################
print_list()
{
AWK_SCRIPT_FUNCTION='\
{ \
ii = ""; \
for (i = 0; i < il; i++) \
{ \
ii = ii " " \
} \
for (i = 1; i <= NF; i++) \
{ \
printf("%s%s\"%s\"%s\n", ii, is, $i, i != NF ? "," : "") \
} \
} \
'
printf "%s\n" "$1" | awk -v il="$2" -v is="$3" "${AWK_SCRIPT_FUNCTION}" >> "${CONFIGURE_FILENAME}"
return 0
}

################################################################################
# Main loop. #
# #
Expand Down Expand Up @@ -140,7 +167,7 @@ print_V
# Declare project.
#
print_I "project ${CONFIGURE_PROJECT} is"
((INDENTATION_LEVEL++))
INDENTATION_LEVEL=$((INDENTATION_LEVEL+1))
print_V

#
Expand All @@ -156,82 +183,28 @@ print_I "Ada_Mode := \"${ADA_MODE}\";"
print_I "Platform := \"${PLATFORM}\";"
print_I "Cpu := \"${CPU}\";"
print_I "ADAC_Switches_RTS := ("
ADAC_SWITCHES_RTS_ARRAY=(${ADAC_SWITCHES_RTS})
imax=$((${#ADAC_SWITCHES_RTS_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${ADAC_SWITCHES_RTS_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${ADAC_SWITCHES_RTS}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "GCC_Platform_Switches := ("
GCC_SWITCHES_PLATFORM_ARRAY=(${GCC_SWITCHES_PLATFORM})
imax=$((${#GCC_SWITCHES_PLATFORM_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${GCC_SWITCHES_PLATFORM_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${GCC_SWITCHES_PLATFORM}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "Include_Directories := ("
INCLUDE_DIRECTORIES_ARRAY=(${INCLUDE_DIRECTORIES})
imax=$((${#INCLUDE_DIRECTORIES_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${INCLUDE_DIRECTORIES_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${INCLUDE_DIRECTORIES}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "Implicit_ALI_Units := ("
IMPLICIT_ALI_UNITS_ARRAY=(${IMPLICIT_ALI_UNITS})
imax=$((${#IMPLICIT_ALI_UNITS_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${IMPLICIT_ALI_UNITS_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${IMPLICIT_ALI_UNITS}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "ADAC_Switches_Warning := ("
ADAC_SWITCHES_WARNING_ARRAY=(${ADAC_SWITCHES_WARNING})
imax=$((${#ADAC_SWITCHES_WARNING_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${ADAC_SWITCHES_WARNING_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${ADAC_SWITCHES_WARNING}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "ADAC_Switches_Style := ("
ADAC_SWITCHES_STYLE_ARRAY=(${ADAC_SWITCHES_STYLE})
imax=$((${#ADAC_SWITCHES_STYLE_ARRAY[@]}-1))
if [ "${imax}" -ge "0" ] ; then
for i in $(seq 0 $((imax))) ; do
s="\"${ADAC_SWITCHES_STYLE_ARRAY[$i]}\""
if [ "${i}" -ne "${imax}" ] ; then
s+=","
fi
print_I " ${s}"
done
fi
INDENTN=" "
print_list "${ADAC_SWITCHES_STYLE}" "${INDENTATION_LEVEL}" "${INDENTN}"
print_I " );"
print_I "Object_Directory := \"${OBJECT_DIRECTORY}\";"
print_I "Optimization_Level := \"${OPTIMIZATION_LEVEL}\";"
Expand All @@ -240,7 +213,7 @@ print_V
#
# Close project.
#
((INDENTATION_LEVEL--))
INDENTATION_LEVEL=$((INDENTATION_LEVEL-1))
print_I "end ${CONFIGURE_PROJECT};"

log_print "${SCRIPT_FILENAME}: ${CONFIGURE_FILENAME}: done."
Expand Down
2 changes: 1 addition & 1 deletion libutils/createdefinitions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Create a GNATPREP definitions file.
Expand Down
8 changes: 5 additions & 3 deletions libutils/creategnatadc.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Create "gnat.adc" file.
Expand Down Expand Up @@ -92,8 +92,10 @@ while IFS= read -r textline ; do
if [ "x${textline_woc}" != "x" ] ; then
pragma=$(printf "%s" "${textline_woc}" | sed -e "s|^\(pragma.*;\)\(.*\)|\1|")
profiles=$(printf "%s" "${textline_woc}" | sed -e "s|^\(pragma.*--\)\(.*\)|\2|")
printf "%s" "${profiles}" | grep -q -w "${PROFILE}" 2> /dev/null
[ ${PIPESTATUS[1]} -eq 0 ] && printf "%s\n" "${pragma}" >> "${GNATADC_FILENAME}"
profile_check=$(printf "%s" "${profiles}" | grep -c -w "${PROFILE}" 2> /dev/null)
if [ "x${profile_check}" != "x0" ] ; then
printf "%s\n" "${pragma}" >> "${GNATADC_FILENAME}"
fi
fi
done < "${GNATADC_FILENAME}.in"

Expand Down
2 changes: 1 addition & 1 deletion libutils/createsymlink.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Create a filesystem symbolic/soft link.
Expand Down
4 changes: 2 additions & 2 deletions libutils/filepad.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Pad a file.
Expand Down Expand Up @@ -117,7 +117,7 @@ else
PADCOUNT=$((REQUESTEDSIZE-FILESIZE))
fi

if [ ${PADCOUNT} -gt 0 ] ; then
if [ "${PADCOUNT}" -gt 0 ] ; then
dd if=/dev/zero ibs=1 count="${PADCOUNT}" >> "$1" 2> /dev/null
if [ $? -ne 0 ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: dd."
Expand Down
8 changes: 4 additions & 4 deletions libutils/filepatch.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Patch a file.
Expand Down Expand Up @@ -87,11 +87,11 @@ OFFSET=$((16#$2))

BYTES_STRING=""
for BYTE in $3 ; do
BYTES_STRING+="\x${BYTE}"
BYTES_STRING="${BYTES_STRING}\x${BYTE}"
done

printf "%s" "${BYTES_STRING}" | dd of="$1" bs=1 seek="${OFFSET}" conv=notrunc 2> /dev/null
if [ ${PIPESTATUS[1]} -ne 0 ] ; then
(printf "%s" "${BYTES_STRING}" | dd of="$1" bs=1 seek="${OFFSET}" conv=notrunc 2> /dev/null)
if [ $? -ne 0 ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: dd."
exit 1
fi
Expand Down
15 changes: 7 additions & 8 deletions libutils/processcfg.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Process a configuration template file.
Expand Down Expand Up @@ -80,7 +80,7 @@ fi
#
# Gather "@"-delimited symbols from input file.
#
SYMBOLS=$(grep --binary -o "@[_A-Za-z][_A-Za-z0-9]*@" "${INPUT_FILENAME}" 2> /dev/null)
SYMBOLS=$(grep -U -o "@[_A-Za-z][_A-Za-z0-9]*@" "${INPUT_FILENAME}" 2> /dev/null)
if [ $? -eq 2 ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: in processing input file \"${INPUT_FILENAME}\"."
exit 1
Expand All @@ -90,17 +90,16 @@ fi
# Perform variable substitution.
#
if [ "x${SYMBOLS}" != "x" ] ; then
sed_command_string=()
sed_command_string="sed"
for symbol in ${SYMBOLS} ; do
variable=$(echo "${symbol}" | sed -e "s|^\(.*@\)\(.*\)\(@.*\)\$|\2|")
value="${!variable}"
variable=$(printf "%s" "${symbol}" | sed -e "s|^\(.*@\)\(.*\)\(@.*\)\$|\2|")
value=$(eval printf "%s" "\$${variable}")
if [ "x${value}" = "x" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Warning: variable \"${variable}\" has no value."
fi
sed_command_string+=("-e")
sed_command_string+=("s|${symbol}|${value}|")
sed_command_string="${sed_command_string} -e \"s|${symbol}|${value}|\""
done
sed "${sed_command_string[@]}" "${INPUT_FILENAME}" > "${OUTPUT_FILENAME}" 2> /dev/null
eval $sed_command_string "${INPUT_FILENAME}" > "${OUTPUT_FILENAME}" 2> /dev/null
if [ $? -ne 0 ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: in processing input file \"${INPUT_FILENAME}\"."
exit 1
Expand Down
2 changes: 1 addition & 1 deletion libutils/qemu-ifdown.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# QEMU "qemu-ifdown.sh" network script.
Expand Down
2 changes: 1 addition & 1 deletion libutils/qemu-ifup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# QEMU "qemu-ifup.sh" network script.
Expand Down
Loading

0 comments on commit 4f54263

Please sign in to comment.