Commit 8a62ba99 authored by Thomas Schöpping's avatar Thomas Schöpping
Browse files

Bash scripts enhanced:

- setuplib.sh: readUserInput function now allows to optionally use the escape key to abort input.
               The function has been renamed to readUserSelection to force incompatibility.
- All other scripts have been updated accordingly.
- It is now possible to use user input 'q' in interactive menus.
- All script parameters '-q' (short for --quit) have been removed.
parent 36e94a25
......@@ -74,7 +74,7 @@ function printHelp {
printf " Enter IDE setup.\n"
printf " -c, --compiler\n"
printf " Enter compiler setup.\n"
printf " -q, --quit\n"
printf " --quit\n"
printf " Quit the script.\n"
printf " --log=<file>\n"
printf " Specify a log file.\n"
......@@ -111,7 +111,7 @@ function stm32flashSetup {
if [ ! -z "$(ls -A $stm32flashdir)" ]; then
printWarning "$stm32flashdir is not empty. Delete and reinitialize? [y/n]\n"
local userinput=""
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
printInfo "wiping ${stm32flashdir}...\n"
......@@ -149,7 +149,7 @@ function stm32flashSetup {
while [ ${PIPESTATUS[0]} -ne 0 ]; do
printWarning "initialitaion failed. Retry? [y/n]\n"
local userinput=""
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE;;
......@@ -210,7 +210,7 @@ function serialBootSetup {
if [ -d "${serialbootdir}/build/" ]; then
printWarning "SerialBoot has been built before. Delete and rebuild? [y/n]\n"
local userinput=""
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
printInfo "deleting ${serialbootdir}/build/...\n"
......@@ -344,7 +344,7 @@ function main {
ideSetup; printf "\n"; shift 1;;
-c|--compiler)
compilerSetup; printf "\n"; shift 1;;
-q|--quit)
--quit)
quitScript; shift 1;;
--log=*|--LOG=*) # already handled; ignore
shift 1;;
......@@ -366,13 +366,13 @@ function main {
# main menu info prompt and selection
printInfo "AMiRo-BLT setup main menu\n"
printf "Please select one of the following actions:\n"
printf " [F] - get and build stm32flash tool\n"
printf " [S] - build SerialBoot tool\n"
printf " [E] - IDE project setup\n"
printf " [C] - enter compiler setup\n"
printf " [Q] - quit this setup\n"
printf " [F] - get and build stm32flash tool\n"
printf " [S] - build SerialBoot tool\n"
printf " [E] - IDE project setup\n"
printf " [C] - enter compiler setup\n"
printf " [ESC] - quit this setup\n"
local userinput=""
readUserInput "FfSsEeCcQq" userinput
readUserSelection "FfSsEeCc" 'y' userinput
printf "\n"
# evaluate user selection
......@@ -385,14 +385,18 @@ function main {
ideSetup; printf "\n";;
C|c)
compilerSetup; printf "\n";;
Q|q)
$'\e')
quitScript;;
*) # sanity check (exit with error)
printError "unexpected argument: $userinput\n";;
printError "unexpected argument: $userinput\n"
exit 1
;;
esac
done
exit 0
# the script should never reach this point
printError "script control flow corruppted.\n"
exit 255
}
################################################################################
......
......@@ -108,29 +108,56 @@ function quitScript {
exit 0
}
### read a user input ##########################################################
### read a user selection ######################################################
# Reads a single character user input from a set up <options> and stores it in
# a given <return> variable.
#
# usage: readUserInput <options> <return>
# usage: readUserSelection <options> <abort> <return>
# arguments: <options>
# String definiing the set of valid characters.
# String definiing the set of valid characters/options.
# If the string is empty, the user can input any character.
# <abort>
# Flag indoicating whether the user may abort this function via
# the ESCAPE key. Must be 'y' or 'n' (defaults to 'n').
# <return>
# Variable to store the selected character to.
# return: n/a
#
function readUserInput {
function readUserSelection {
local input=""
local abort=''
# check <abort> argument
case $2 in
y|n)
abort=$2;;
*)
printError "<abort> parameter invalid ($2). Defauting to 'n'.\n"
abort='n';;
esac
# read user input
while [ -z "$input" ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do
read -p "your selection: " -n 1 -e input
if [ -z "$input" ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then
while [ -z "$input" ]; do
# read character but do not echo
read -p "your selection: " -n 1 -s input
# echo input
case $input in
$'\e')
printf "ESC\n";;
*)
printf "$input\n";;
esac
# check validity
if [ -z "$input" ] || (( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ) && ( [ $abort = 'n' ] || [ ! $input = $'\e' ] )); then
# transform '\e' to printable format
if [ $input = $'\e' ]; then
input="ESC"
fi
printWarning "[$input] is no valid action\n"
input=""
fi
done
printLog "[$input] has been selected\n"
eval $2="$input"
eval $3="$input"
}
### check whether argument is an option ########################################
......@@ -218,12 +245,12 @@ function setLogFile {
printf " [A] - append log\n"
printf " [R] - restart log (delete existing file)\n"
printf " [N] - no log\n"
readUserInput "AaRrNn" userinput
readUserSelection "AaRrNn" 'n' userinput
option=${userinput,,}
fi
# evaluate option
case "$option" in
a|c)
A|a|c)
if [ $quiet = false ]; then
printInfo "appending log to $filepath\n"
fi
......@@ -233,13 +260,13 @@ function setLogFile {
printf "\n" >> $filepath
fi
;;
r)
R|r)
echo -n "" > $filepath
if [ $quiet = false ]; then
printInfo "content of $filepath wiped\n"
fi
;;
n)
N|n)
if [ $quiet = false ]; then
printInfo "no log file will be generated\n"
fi
......
......@@ -72,7 +72,7 @@ function printHelp {
printf " Unistall a version.\n"
printf " -c, --change\n"
printf " Change the default version.\n"
printf " -q, --quit\n"
printf " --quit\n"
printf " Quit the script.\n"
printf " --log=<file>\n"
printf " Specify a log file.\n"
......@@ -223,7 +223,7 @@ function installNewVersion {
if [ -f "$armgcc_tarball" ]; then
printWarning "$armgcc_tarball already exists. Delete and redownload? [y/n]\n"
local userinput=""
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
rm "$armgcc_tarball"
......@@ -256,7 +256,7 @@ function installNewVersion {
if [ -d "$installdir" ]; then
printWarning "$installdir already exists. Overwrite? [y/n]\n"
local userinput=""
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
;;
......@@ -430,7 +430,7 @@ function uninstallVersion {
printf "\n"
# prompt selected and aks user for confirmation
printInfo "${versions[$idx]} will be removed. Continue? [y/n]\n"
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
;;
......@@ -628,7 +628,7 @@ function main {
printError "no installation detected\n"
fi
printf "\n"; shift 1;;
-q|--quit)
--quit)
quitScript; shift 1;;
--log=*|--LOG=*) # already handled; ignore
shift 1;;
......@@ -654,15 +654,15 @@ function main {
printInfo "GCC setup main menu\n"
printf "Please select one of the following actions:\n"
if [ ${#installedversions[@]} -eq 0 ]; then
printf " [I] - install a first compiler\n"
printf " [I] - install a first compiler\n"
else
printf " [I] - install another version\n"
printf " [I] - install another version\n"
fi
printf " [U] - uninstall a version\n"
printf " [C] - change default version\n"
printf " [Q] - quit this setup\n"
printf " [U] - uninstall a version\n"
printf " [C] - change default version\n"
printf " [ESC] - quit this setup\n"
local userinput=""
readUserInput "IiUuCcQq" userinput
readUserSelection "IiUuCc" 'y' userinput
printf "\n"
# evaluate user selection
......@@ -690,14 +690,18 @@ function main {
printError "no installation detected\n"
fi
printf "\n";;
Q|q)
$'\e')
quitScript;;
*) # sanity check (exit with error)
printError "unexpected argument: $userinput\n";;
printError "unexpected argument: $userinput\n"
exit 1
;;
esac
done
exit 0
# the script should never reach this point
printError "script control flow corruppted.\n"
exit 255
}
################################################################################
......@@ -705,3 +709,4 @@ function main {
################################################################################
main "$@"
......@@ -68,7 +68,7 @@ function printHelp {
printf " Print this help text.\n"
printf " --GCC\n"
printf " Enter GCC setup.\n"
printf " -q, --quit\n"
printf " --quit\n"
printf " Quit the script.\n"
printf " --log=<file>\n"
printf " Specify a log file.\n"
......@@ -150,7 +150,7 @@ function main {
shift 1;;
--GCC)
gccSetup; printf "\n"; shift 1;;
-q|--quit)
--quit)
quitScript; printf "\n"; shift 1;;
--log=*|--LOG=*) # already handled; ignore
shift 1;;
......@@ -172,24 +172,28 @@ function main {
# main menu info prompt and selection
printInfo "AMiRo-BLT compiler setup main menu\n"
printf "Please select one of the following actions:\n"
printf " [G] - enter GCC setup\n"
printf " [Q] - quit this setup\n"
printf " [G] - enter GCC setup\n"
printf " [ESC] - quit this setup\n"
local userinput=""
readUserInput "GgQq" userinput
readUserSelection "Gg" 'y' userinput
printf "\n"
# evaluate user selection
case "$userinput" in
G|g)
gccSetup; printf "\n";;
Q|q)
$'\e')
quitScript;;
*) # sanity check (exit with error)
printError "unexpected argument: $userinput\n"; printf "\n";;
printError "unexpected argument: $userinput\n"; printf "\n"
exit 1
;;
esac
done
exit 0
# the script should never reach this point
printError "script control flow corruppted.\n"
exit 255
}
################################################################################
......@@ -197,3 +201,4 @@ function main {
################################################################################
main "$@"
......@@ -74,7 +74,7 @@ function printHelp {
printf " Delete project files.\n"
printf " -w, --wipe\n"
printf " Delete project and .user files.\n"
printf " -q, --quit\n"
printf " --quit\n"
printf " Quit the script.\n"
printf " --log=<file>\n"
printf " Specify a log file.\n"
......@@ -317,7 +317,7 @@ function createModuleProject {
done
local userinput=""
printInfo "Continue and overwrite? [y/n]\n"
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "$userinput" in
Y|y)
;;
......@@ -523,7 +523,7 @@ function createAllProjects {
done
local userinput=""
printInfo "Continue and overwrite? [y/n]\n"
readUserInput "YyNn" userinput
readUserSelection "YyNn" 'n' userinput
case "${userinput}" in
Y|y)
for pfile in ${projectfiles[@]}; do
......@@ -737,7 +737,7 @@ function main {
deleteProjects; printf "\n"; shift 1;;
-w|--wipe)
deleteProjects --wipe; printf "\n"; shift 1;;
-q|--quit)
--quit)
quitScript; shift 1;;
--log=*|--LOG=*) # already handled; ignore
shift 1;;
......@@ -759,13 +759,13 @@ function main {
# main menu info prompt and selection
printInfo "QtCreator setup main menu\n"
printf "Please select one of the following actions:\n"
printf " [M] - create a project for a single module\n"
printf " [A] - create a project for all modules\n"
printf " [C] - clean all project files\n"
printf " [W] - wipe all project and .user files\n"
printf " [Q] - quit this setup\n"
printf " [M] - create a project for a single module\n"
printf " [A] - create a project for all modules\n"
printf " [C] - clean all project files\n"
printf " [W] - wipe all project and .user files\n"
printf " [ESC] - quit this setup\n"
local userinput=""
readUserInput "MmAaCcWwQq" userinput
readUserSelection "MmAaCcWw" 'y' userinput
printf "\n"
# evaluate user selection
......@@ -778,14 +778,18 @@ function main {
deleteProjects; printf "\n";;
W|w)
deleteProjects --wipe; printf "\n";;
Q|q)
$'\e')
quitScript;;
*) # sanity check (exit with error)
printError "unexpected argument: $userinput\n";;
printError "unexpected argument: $userinput\n"
exit 1
;;
esac
done
exit 0
# the script should never reach this point
printError "script control flow corruppted.\n"
exit 255
}
################################################################################
......
......@@ -68,7 +68,7 @@ function printHelp {
printf " Print this help text.\n"
printf " --QtCreator\n"
printf " Enter QtCreator setup.\n"
printf " -q, --quit\n"
printf " --quit\n"
printf " Quit the script.\n"
printf " --log=<file>\n"
printf " Specify a log file.\n"
......@@ -150,7 +150,7 @@ function main {
shift 1;;
--QtCreator)
qtCreatorSetup; printf "\n"; shift 1;;
-q|--quit)
--quit)
quitScript; printf "\n"; shift 1;;
--log=*|--LOG=*) # already handled; ignore
shift 1;;
......@@ -172,24 +172,28 @@ function main {
# main menu info prompt and selection
printInfo "AMiRo-BLT IDE setup main menu\n"
printf "Please select one of the following actions:\n"
printf " [C] - enter QtCreator setup\n"
printf " [Q] - quit this setup\n"
printf " [Q] - enter QtCreator setup\n"
printf " [ESC] - quit this setup\n"
local userinput=""
readUserInput "CcQq" userinput
readUserSelection "Qq" 'y' userinput
printf "\n"
# evaluate user selection
case "$userinput" in
C|c)
qtCreatorSetup; printf "\n";;
Q|q)
qtCreatorSetup; printf "\n";;
$'\e')
quitScript;;
*) # sanity check (exit with error)
printError "unexpected argument: $userinput\n";;
printError "unexpected argument: $userinput\n"
exit 1
;;
esac
done
exit 0
# the script should never reach this point
printError "script control flow corruppted.\n"
exit 255
}
################################################################################
......@@ -197,3 +201,4 @@ function main {
################################################################################
main "$@"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment