aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluis <luis>2007-08-19 17:03:16 +0000
committerluis <luis>2007-08-19 17:03:16 +0000
commitea67cf748060ac542497d8ca41e8ebf3c88a67da (patch)
tree14d6c7255e2ea7222a61dd5eb3d80cdae0b566e5
parenta7e6b926b39c26b76b585fde53f4be6fc03a6998 (diff)
downloadfirma-ea67cf748060ac542497d8ca41e8ebf3c88a67da.tar.gz
firma-ea67cf748060ac542497d8ca41e8ebf3c88a67da.tar.bz2
Splitted command line parsing in: check command line args for errors and parse valid options. Looks more manageable and easier to extend this way.
-rwxr-xr-xfirma313
1 files changed, 188 insertions, 125 deletions
diff --git a/firma b/firma
index eac7e8f..402c9a4 100755
--- a/firma
+++ b/firma
@@ -62,7 +62,7 @@ function Version {
echo "\
firma $VERSION
-Copyright (C) 2005-2007 A Firma, Inc.
+Copyright (C) 2005-2007 A Firma
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the GNU General Public License
@@ -2489,165 +2489,228 @@ declare -r \
OPTION="$1" \
ARG="$2"
-# command line parsing:
-# first check number of arguments, then check what was entered
-# start main case
-case $NUM_OF_ARGS in
+# check command line arguments for errors
+#+(missing arguments, invalid option, etc)
+case "$NUM_OF_ARGS" in
0)
+
echo >&2 "$BASENAME: missing arguments"
Usage
EXIT_CODE=1
;;
+
1)
- # start case #1
- case $OPTION in
- -h|--help)
- Usage
- EXIT_CODE=0
+ case "$OPTION" in
+
+ # valid short option
+ -h|-v)
;;
- -v|--version)
- Version
- EXIT_CODE=0
+
+ # valid long option
+ --help|--version)
;;
- # valid option called without its required argument
- -a|--admin-task|-e|--email-admin-task|-c|--create-newlist|-p|--process-message)
+
+ # valid short option called without its required argument
+ -a|-c|-e|-p)
echo >&2 "$BASENAME: missing arguments"
Usage
EXIT_CODE=1
;;
+
+ # valid long option called without its required argument
+ --admin-task|--create-newlist|--email-admin-task|--process-message)
+ echo >&2 "$BASENAME: missing arguments"
+ Usage
+ EXIT_CODE=1
+ ;;
+
+ # invalid option
*)
echo >&2 "$BASENAME: invalid option -- $OPTION"
Usage
EXIT_CODE=1
;;
- # end case #1
+
esac
;;
2)
- # if firma.conf exists
- if [[ -f "$FIRMA_CONFIG_FILE" ]]; then
-
- # evaluate its parameters
- SourceFirmaConfig
-
- # and finally check firma.conf parameters and permissions
- if CheckFirmaConfigFile && CheckPermission $FIRMA_CONFIG_FILE; then
-
- LIST_NAME="$ARG"
- LIST_PATH="$LISTS_DIR/$LIST_NAME"
- LIST_CONFIG_FILE="$LIST_PATH/$LIST_NAME.conf"
-
- # start case #2
- case $OPTION in
- -c|--create-newlist)
- NewList
- EXIT_CODE=$?
- ;;
- # options that depend on the list configuration file
- -a|--admin-task|-e|--email-admin-task|-p|--process-message)
-
- # if config file exists but has wrong permissions or ownership
- if [[ -f "$LIST_CONFIG_FILE" ]]; then
-
- # eval list parameters
- SourceListConfig
-
- CheckListPermissions $LIST_CONFIG_FILE
-
- # get gpg parameters
- DeclareGpgVars
-
- # check the list configuration file
- if CheckListConfigFile; then
-
- # start case #3
- case $OPTION in
- -a|--admin-task)
-
- MODE="admin-interactive"
- # while a "quit" command isn't entered (returns 3), read STDIN
- while (( $EXIT_CODE != 3 )) && read -rep "Command> " STDIN; do
- # if line is not empty or commented, process command
- if [[ -n "$STDIN" && "$STDIN" != "#"* ]]; then
- ListAdministration $STDIN
- EXIT_CODE=$?
- fi
- done
-
- # since quit was entered, exit without error
- EXIT_CODE=0
-
- ;;
- -p|--process-message)
- MODE="list-message"
- ProcessMessage
- EXIT_CODE=$?
- ;;
- -e|--email-admin-task)
- MODE="admin-non-interactive"
- ProcessMessage
- EXIT_CODE=$?
- ;;
- # end case #3
- esac
- # else, list configuration file checking returned an error
- else
- EXIT_CODE=$?
- fi
- # else, list configuration file could not be found
- else
- LogMessage "FATAL: Cannot source \`$LIST_CONFIG_FILE': No such file or directory"
- EXIT_CODE=1
- fi
- ;;
- # valid option called with too many arguments
- -h|--help|-v|--version)
- if [[ "$OPTION" == "-h" || "$OPTION" == "--help" ]]; then
- ListAdministration $ARG help
- EXIT_CODE=$?
- else
- echo >&2 "$BASENAME: too many arguments -- $@"
- Usage
- EXIT_CODE=1
- fi
- ;;
- *)
- echo >&2 "$BASENAME: invalid option -- $OPTION"
- Usage
- EXIT_CODE=1
- ;;
- # end case #2
- esac
- # else, firma.conf checking returned an error
- else
- EXIT_CODE=$?
- fi
- # else, firma.conf could not be found
- else
- LogMessage "FATAL: Cannot source \`$FIRMA_CONFIG_FILE': No such file or directory"
- EXIT_CODE=1
- fi
+ case "$OPTION" in
+
+ # valid short option
+ -a|-c|-e|-p)
+ ;;
+
+ # valid long option
+ --admin-task|--create-newlist|--email-admin-task|--process-message)
+ ;;
+
+ # valid short option called with too many arguments
+ -h|-v)
+ echo >&2 "$BASENAME: too many arguments -- $@"
+ Usage
+ EXIT_CODE=1
+ ;;
+
+ # valid long option called with too many arguments
+ -help|-version)
+ echo >&2 "$BASENAME: too many arguments -- $@"
+ Usage
+ EXIT_CODE=1
+ ;;
+
+ # invalid option
+ *)
+ echo >&2 "$BASENAME: invalid option -- $OPTION"
+ Usage
+ EXIT_CODE=1
+ ;;
+
+ esac
;;
*)
- # start case #4
- case $OPTION in
- # again, valid option called with too many arguments
- -a|--admin-task|-e|--email-admin-task|-c|--create-newlist|-h|--help|-p|--process-message|-v|--version)
+ case "$OPTION" in
+
+ # valid short option called with too many arguments
+ -h|-v|-a|-c|-e|-p)
+ echo >&2 "$BASENAME: too many arguments -- $@"
+ Usage
+ EXIT_CODE=1
+ ;;
+
+ # valid long option called with too many arguments
+ --help|--version|--admin-task|--create-newlist|--email-admin-task|--process-message)
echo >&2 "$BASENAME: too many arguments -- $@"
Usage
EXIT_CODE=1
;;
+
+ # invalid option
*)
echo >&2 "$BASENAME: invalid option -- $OPTION"
Usage
EXIT_CODE=1
;;
- # end case #4
+
esac
;;
-# end main case
esac
+# parse valid command line arguments
+if [[ "$EXIT_CODE" == "0" ]]; then
+
+ case "$NUM_OF_ARGS" in
+
+ 1)
+ case "$OPTION" in
+
+ -h|--help)
+ Usage
+ EXIT_CODE=0
+ ;;
+
+ -v|--version)
+ Version
+ EXIT_CODE=0
+ ;;
+
+ esac
+ ;;
+ 2)
+ # if firma.conf exists
+ if [[ -f "$FIRMA_CONFIG_FILE" ]]; then
+
+ # evaluate its parameters
+ SourceFirmaConfig
+
+ # and finally check firma.conf parameters and permissions
+ if CheckFirmaConfigFile && CheckPermission $FIRMA_CONFIG_FILE; then
+
+ LIST_NAME="$ARG"
+ LIST_PATH="$LISTS_DIR/$LIST_NAME"
+ LIST_CONFIG_FILE="$LIST_PATH/$LIST_NAME.conf"
+
+ case "$OPTION" in
+
+ -c|--create-newlist)
+ NewList
+ EXIT_CODE=$?
+ ;;
+
+ # options that depend on the list configuration file
+ -a|--admin-task|-e|--email-admin-task|-p|--process-message)
+
+ # if config file exists
+ if [[ -f "$LIST_CONFIG_FILE" ]]; then
+
+ # eval list parameters
+ SourceListConfig
+
+ # check its permissions
+ CheckListPermissions $LIST_CONFIG_FILE
+
+ # get gpg parameters
+ DeclareGpgVars
+
+ # check the list configuration file
+ if CheckListConfigFile; then
+
+ case "$OPTION" in
+
+ -a|--admin-task)
+
+ MODE="admin-interactive"
+ # while a "quit" command isn't entered (returns 3), read STDIN
+ while (( $EXIT_CODE != 3 )) && read -rep "Command> " STDIN; do
+ # if line is not empty or commented, process command
+ if [[ -n "$STDIN" && "$STDIN" != "#"* ]]; then
+ ListAdministration $STDIN
+ EXIT_CODE=$?
+ fi
+ done
+
+ # since quit was entered, exit without error
+ EXIT_CODE=0
+
+ ;;
+
+ -p|--process-message)
+ MODE="list-message"
+ ProcessMessage
+ EXIT_CODE=$?
+ ;;
+ -e|--email-admin-task)
+ MODE="admin-non-interactive"
+ ProcessMessage
+ EXIT_CODE=$?
+ ;;
+
+ esac
+ # else, list configuration file checking returned an error
+ else
+ EXIT_CODE=$?
+ fi
+ # else, list configuration file could not be found
+ else
+ LogMessage "FATAL: Cannot source \`$LIST_CONFIG_FILE': No such file or directory"
+ EXIT_CODE=1
+ fi
+ ;;
+
+ esac
+ # else, firma.conf checking returned an error
+ else
+ EXIT_CODE=$?
+ fi
+ # else, firma.conf could not be found
+ else
+ LogMessage "FATAL: Cannot source \`$FIRMA_CONFIG_FILE': No such file or directory"
+ EXIT_CODE=1
+ fi
+ ;;
+ esac
+
+fi
+
# exit
exit $EXIT_CODE