3
#########################################################################
5
# Script ID: configurable-options.sh #
6
# Author: Copyright (C) 2021 Mark Grant #
8
# Released under the GPLv3 only. #
9
# SPDX-License-Identifier: GPL-3.0 #
12
# It make sense in many code-bases to allow the override of some #
13
# constants which in certain circumstances may benefit from a different #
14
# value. This script allows such overrides to be passed to the #
15
# AutoTools system and used in the compile commands. The modified #
16
# values are presented as a string of -DConstant=value constructs. #
18
# Syntax: configurable-options.sh <File or named pipe> #
19
# Without the optional file, output is to stdout. #
22
# Bash standard Exit Codes: 0 - success #
23
# 1 - general failure #
24
# User-defined exit code range is 64 - 113 #
25
# C/C++ Semi-standard exit codes from sysexits.h range is 64 - 78 #
26
# EX_USAGE 64 command line usage error #
27
# EX_DATAERR 65 data format error #
28
# EX_NOINPUT 66 cannot open input #
29
# EX_NOUSER 67 addressee unknown #
30
# EX_NOHOST 68 host name unknown #
31
# EX_UNAVAILABLE 69 service unavailable #
32
# EX_SOFTWARE 70 internal software error #
33
# EX_OSERR 71 system error (e.g., can't fork) #
34
# EX_OSFILE 72 critical OS file missing #
35
# EX_CANTCREAT 73 can't create (user) output file #
36
# EX_IOERR 74 input/output error #
37
# EX_TEMPFAIL 75 temp failure; user is invited #
39
# EX_PROTOCOL 76 remote error in protocol #
40
# EX_NOPERM 77 permission denied #
41
# EX_CONFIG 78 configuration error #
42
# User-defined (here) exit codes range 79 - 113: #
47
#########################################################################
49
#########################################################################
53
# Date Author Version Description #
55
# 15/04/2021 MG 1.0.1 Created. #
56
# 31/05/2021 MG 1.0.2 Re-write to improve and to use Dialog. #
58
#########################################################################
65
readonly version=1.0.2 # set version variable
67
output_target=$1 # Filename for the results
71
BUF_UNUSED_DEF_SIZE_MULT=3
72
BUF_MAX_UNREACH_PERCENT=33
75
# Define the dialog exit status codes
87
# Standard function to emit messages depending on various parameters.
88
# Parameters - $1 What:- The message to emit.
89
# $2 Where:- stdout == 0
97
printf "%s\n" "$1" 1>&2
101
# Standard function to tidy up and return exit code.
102
# Parameters - $1 is the exit code.
109
# Standard function to test command error and exit if non-zero.
110
# Parameters - $1 is the exit code, (normally $? from the preceding command).
112
std_cmd_err_handler()
119
# Standard trap exit function.
124
local -i exit_code=$?
127
msg="Script terminating with exit code $exit_code due to trap received."
129
script_exit $exit_code
133
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM
135
# Check dialog is available
137
# Returns zero on success 69 on failure
140
which dialog > /dev/null
142
printf "%s\n" "Please install dialog first." 1>&2
148
# Validate Default Buffer Size
149
# %1 is the value to check
150
# Returns 0 if valid, 1 if invalid
151
validate_def_buf_size()
155
if [[ ! ($1 =~ ^[0-9]*$) ]]; then
157
dialog --backtitle "Compile-time Values" \
158
--title "Error Message" --msgbox \
159
"Buffer Size must be numeric" 10 60
160
elif (( $1 < 256 || $1 > 10485760 )); then
161
dialog --backtitle "Compile-time Values" \
162
--title "Error Message" --msgbox \
163
"Invalid Buffer Size, must be between 256 Bytes and 10485760 Bytes" 10 60
166
if (( $status == 0 )); then
172
# Validate Buffer Unused Default Size Multiplier
173
# %1 is the value to check
174
# Returns 0 if valid, 1 if invalid
175
validate_buf_unused_def_size_mult()
179
if [[ ! ($1 =~ ^[0-9]*$) ]]; then
181
dialog --backtitle "Compile-time Values" \
182
--title "Error Message" \
184
"Buffer Unused Default Size Multiplier must be numeric"\
186
elif (( $1 < 2 || $1 > 10 )); then
187
dialog --backtitle "Compile-time Values" \
188
--title "Error Message" \
190
"Invalid Buffer Unused Default Size Multiplier, must be between 2 and 10" 10 60
193
if (( $status == 0 )); then
194
BUF_UNUSED_DEF_SIZE_MULT=$1
199
# Validate Buffer Max Unreachable Percentage
200
# %1 is the value to check
201
# Returns 0 if valid, 1 if invalid
202
validate_buf_max_unreach_percent()
206
if [[ ! ($1 =~ ^[0-9]*$) ]]; then
208
dialog --backtitle "Compile-time Values" \
209
--title "Error Message" \
211
"Buffer Max Unreachable Percentage must be numeric" \
213
elif (( $1 < 20 || $1 > 50 )); then
214
dialog --backtitle "Compile-time Values" \
215
--title "Error Message" \
217
"Invalid Buffer Max Unreachable Percentage, must be between 20 and 50" 10 60
220
if (( $status == 0 )); then
221
BUF_MAX_UNREACH_PERCENT=$1
226
# Process Buffer values
231
local form_help_txt="The answers entered here must be numeric and lie between the minimum and maximum values specified in the individual prompts."
232
local form_txt="You can use the UP/DOWN arrow keys to select a value.\nOK retains the values displayed and returns up the menu hierarchy.\nCancel ends the input session and returns up the menu hierarchy without retaining any changed values.\nESC cancels this configuration session entirely and does not pass back any values at all.\n\nSelect The Values To Amend:"
242
values=$(dialog --help-button \
243
--backtitle "Compile-time Values" \
244
--title "Buffer values" \
247
"Buffer Size. (256 <= X <= 10485760 Bytes)" 1 1 "$DEF_BUF_SIZE" 1 49 9 0 \
248
"Unused buffer size multiplier. (2 <= X <= 10)" 2 1 "$BUF_UNUSED_DEF_SIZE_MULT" 2 49 3 0 \
249
"Maximum unreachable percentage. (20 <= X <= 50)" 3 1 "$BUF_MAX_UNREACH_PERCENT" 3 49 3 0 \
257
for value in $values; do
258
if (( i == 0 )); then
259
validate_def_buf_size "$value"
261
elif (( $i == 1 )); then
262
validate_buf_unused_def_size_mult \
265
elif (( $i == 2 )); then
266
validate_buf_max_unreach_percent \
272
if (( $status == 0 )); then
280
dialog --title "Main menu help" \
281
--backtitle "Compile-time Values" \
282
--msgbox "$form_help_txt" 10 50
291
# Validate Default Message Size
292
# %1 is the value to check
293
# Returns 0 if valid, 1 if invalid
294
validate_def_msg_size()
298
if [[ ! ($1 =~ ^[0-9]*$) ]]; then
300
dialog --backtitle "Compile-time Values" \
301
--title "Error Message" --msgbox \
302
"Message Size must be numeric" 10 60
303
elif (( $1 < 256 || $1 > 10485760 )); then
304
dialog --backtitle "Compile-time Values" \
305
--title "Error Message" --msgbox \
306
"Invalid Message Size, must be between 256 Bytes and 10485760 Bytes" 10 60
309
if (( $status == 0 )); then
315
# Process Message values
320
local input_help_txt="The answers entered here must be numeric and lie between the minimum and maximum values specified in the individual prompts."
321
local input_txt="Please enter a value.\nOK retains the values displayed and returns up the menu hierarchy.\nCancel ends the input session and returns up the menu hierarchy without retaining any changed values.\nESC cancels this configuration session entirely and does not pass back any values at all.\n\nMessage Size. (256 <= X <= 10485760 Bytes)"
330
value=$(dialog --help-button \
331
--backtitle "Compile-time Values" \
332
--title "Message values" \
333
--inputbox "$input_txt" 15 60 "$DEF_MSG_SIZE" \
340
validate_def_msg_size "$value"
342
if (( $status == 0 )); then
350
dialog --title "Main menu help" \
351
--backtitle "Compile-time Values" \
352
--msgbox "$input_help_txt" 10 50
361
# Process level 0 menu
366
local menu_help_txt="It make sense in many code-bases to allow the override of some constants which in certain circumstances may benefit from a different value. This script allows such overrides to be passed to the AutoTools system and used in the compile commands. The modified values are presented as a string of -DConstant=value constructs."
368
local menu_txt="You can use the UP/DOWN arrow keys or the number keys to choose an option.\nOK invokes the menu option.\nExit ends the configuration session passing back the values entered.\nESC cancels this configuration session and does not pass back any values at all.\n\nSelect The Area To Amend:"
375
menu_item=$(dialog --help-button \
376
--backtitle "Compile-time Values" \
377
--title "Main Menu" \
378
--cancel-label "Exit" \
381
2 1 Buffer 2 Message \
401
dialog --title "Main menu help" \
402
--backtitle "Compile-time Values" \
403
--msgbox "$menu_help_txt" 10 60
410
ret_string=" CLA_DEF_CPP_VALUES=\" -DDEF_BUF_SIZE=$DEF_BUF_SIZE"
411
ret_string+=" -DBUF_UNUSED_DEF_SIZE_MULT=$BUF_UNUSED_DEF_SIZE_MULT"
412
ret_string+=" -DBUF_MAX_UNREACH_PERCENT=$BUF_MAX_UNREACH_PERCENT"
413
ret_string+=" -DDEF_MSG_SIZE=$DEF_MSG_SIZE\""
414
if [[ -n $output_target ]]; then
415
printf "%s\n" "$ret_string" > $output_target
417
printf "%s\n" "$ret_string"
427
std_cmd_err_handler $?
430
std_cmd_err_handler $?