Rainer Weikusat
2024-02-05 15:37:48 UTC
Debian (and derived distros) use a system called Debconf for prompting
users for configuration information during package installation. In the
advanced section, the manpage suggests that config scripts should support
moving backwards through multiple question so that users can fix mistakes
and then states
There are several ways to write the control structures of your
program so it can jump back to previous questions when
necessary. You can write goto-laden spaghetti code. Or you can
create several functions and use recursion. But perhaps the
cleanest and easiest way is to construct a state machine. Here is
a skeleton of a state machine that you can fill out and expand.
This is followed by a 40 line code example using case (Bourne shell) to
implement a form of computed goto in a loop with lots of explicit state
handling and switching code.
Considering the problem, there's a set of questions which needs to be
asked in order. If a user OKs an answer, the next question should be
asked, if he cancels it, the previous question should be asked
again. This absolutely cries for a recursive solution and there's a
really simple and general one which needs much less code than the
example in the manpage.
First, define a helper function for actually asking a question:
ask()
{
db_input high "$PREFIX/$1"
db_go
}
Given that, a function for asking a set of question which can back up to
the previous one can be written as
ask_all()
( # run in forked subshell so that variables are local
# termination condition: called w/ no argumnets,
# ergo, nothing to do, return success
test "$1" || return 0
# remove our question from argument list
q="$1"
shift
while true;
do
# ask question, return failure on failure (2 arbitrary)
ask "$q" || return 2
# invoke ask_all again to ask remaining question, return success on success
ask_all "$@" && return 0
done
)
users for configuration information during package installation. In the
advanced section, the manpage suggests that config scripts should support
moving backwards through multiple question so that users can fix mistakes
and then states
There are several ways to write the control structures of your
program so it can jump back to previous questions when
necessary. You can write goto-laden spaghetti code. Or you can
create several functions and use recursion. But perhaps the
cleanest and easiest way is to construct a state machine. Here is
a skeleton of a state machine that you can fill out and expand.
This is followed by a 40 line code example using case (Bourne shell) to
implement a form of computed goto in a loop with lots of explicit state
handling and switching code.
Considering the problem, there's a set of questions which needs to be
asked in order. If a user OKs an answer, the next question should be
asked, if he cancels it, the previous question should be asked
again. This absolutely cries for a recursive solution and there's a
really simple and general one which needs much less code than the
example in the manpage.
First, define a helper function for actually asking a question:
ask()
{
db_input high "$PREFIX/$1"
db_go
}
Given that, a function for asking a set of question which can back up to
the previous one can be written as
ask_all()
( # run in forked subshell so that variables are local
# termination condition: called w/ no argumnets,
# ergo, nothing to do, return success
test "$1" || return 0
# remove our question from argument list
q="$1"
shift
while true;
do
# ask question, return failure on failure (2 arbitrary)
ask "$q" || return 2
# invoke ask_all again to ask remaining question, return success on success
ask_all "$@" && return 0
done
)