~m-grant-prg/libmgec/focal-trunk

« back to all changes in this revision

Viewing changes to configurable-options.sh

  • Committer: Mark Grant
  • Date: 2021-06-15 09:01:39 UTC
  • mto: This revision was merged to the branch mainline in revision 104.
  • Revision ID: m.grant.prg@gmail.com-20210615090139-bs4h96lge4s2c38i
Tags: upstream-1.3.4
ImportĀ upstreamĀ versionĀ 1.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env bash
 
2
 
 
3
#########################################################################
 
4
#                                                                       #
 
5
# Script ID: configurable-options.sh                                    #
 
6
# Author: Copyright (C) 2021  Mark Grant                                #
 
7
#                                                                       #
 
8
# Released under the GPLv3 only.                                        #
 
9
# SPDX-License-Identifier: GPL-3.0                                      #
 
10
#                                                                       #
 
11
# Purpose:                                                              #
 
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.      #
 
17
#                                                                       #
 
18
# Syntax:       configurable-options.sh <File or named pipe>            #
 
19
#               Without the optional file, output is to stdout.         #
 
20
#                                                                       #
 
21
# Exit codes used:-                                                     #
 
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   #
 
38
#                                       to retry                        #
 
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:                  #
 
43
#               None                                                    #
 
44
#                                                                       #
 
45
# Further Info:                                                         #
 
46
#                                                                       #
 
47
#########################################################################
 
48
 
 
49
#########################################################################
 
50
#                                                                       #
 
51
# Changelog                                                             #
 
52
#                                                                       #
 
53
# Date          Author  Version Description                             #
 
54
#                                                                       #
 
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.  #
 
57
#                                                                       #
 
58
#########################################################################
 
59
 
 
60
 
 
61
##################
 
62
# Init variables #
 
63
##################
 
64
 
 
65
readonly version=1.0.2                  # set version variable
 
66
 
 
67
output_target=$1                        # Filename for the results
 
68
 
 
69
# Set default values
 
70
DEF_BUF_SIZE=256
 
71
BUF_UNUSED_DEF_SIZE_MULT=3
 
72
BUF_MAX_UNREACH_PERCENT=33
 
73
DEF_MSG_SIZE=256
 
74
 
 
75
# Define the dialog exit status codes
 
76
: ${DIALOG_OK=0}
 
77
: ${DIALOG_CANCEL=1}
 
78
: ${DIALOG_EXIT=1}
 
79
: ${DIALOG_HELP=2}
 
80
: ${DIALOG_ESC=255}
 
81
 
 
82
 
 
83
#############
 
84
# Functions #
 
85
#############
 
86
 
 
87
# Standard function to emit messages depending on various parameters.
 
88
# Parameters -  $1 What:-       The message to emit.
 
89
#               $2 Where:-      stdout == 0
 
90
#                               stderr == 1
 
91
# No return value.
 
92
output()
 
93
{
 
94
        if (( !$2 )); then
 
95
                printf "%s\n" "$1"
 
96
        else
 
97
                printf "%s\n" "$1" 1>&2
 
98
        fi
 
99
}
 
100
 
 
101
# Standard function to tidy up and return exit code.
 
102
# Parameters -  $1 is the exit code.
 
103
# No return value.
 
104
script_exit()
 
105
{
 
106
        exit $1
 
107
}
 
108
 
 
109
# Standard function to test command error and exit if non-zero.
 
110
# Parameters -  $1 is the exit code, (normally $? from the preceding command).
 
111
# No return value.
 
112
std_cmd_err_handler()
 
113
{
 
114
        if (( $1 )); then
 
115
                script_exit $1
 
116
        fi
 
117
}
 
118
 
 
119
# Standard trap exit function.
 
120
# No parameters.
 
121
# No return value.
 
122
trap_exit()
 
123
{
 
124
        local -i exit_code=$?
 
125
        local msg
 
126
 
 
127
        msg="Script terminating with exit code $exit_code due to trap received."
 
128
        output "$msg" 1
 
129
        script_exit $exit_code
 
130
}
 
131
 
 
132
# Setup trap
 
133
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM
 
134
 
 
135
# Check dialog is available
 
136
# No parameters
 
137
# Returns zero on success 69 on failure
 
138
dialog_reqd()
 
139
{
 
140
        which dialog > /dev/null
 
141
        if (( $? )); then
 
142
                printf "%s\n" "Please install dialog first." 1>&2
 
143
                return 69
 
144
        fi
 
145
        return 0
 
146
}
 
147
 
 
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()
 
152
{
 
153
        declare -i status=0
 
154
 
 
155
        if [[ ! ($1 =~ ^[0-9]*$) ]]; then
 
156
                status=1
 
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
 
164
                status=1
 
165
        fi
 
166
        if (( $status == 0 )); then
 
167
                DEF_BUF_SIZE=$1
 
168
        fi
 
169
        return $status
 
170
}
 
171
 
 
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()
 
176
{
 
177
        declare -i status=0
 
178
 
 
179
        if [[ ! ($1 =~ ^[0-9]*$) ]]; then
 
180
                status=1
 
181
                dialog --backtitle "Compile-time Values" \
 
182
                        --title "Error Message" \
 
183
                        --msgbox \
 
184
                        "Buffer Unused Default Size Multiplier must be numeric"\
 
185
                        10 60
 
186
        elif (( $1 < 2 || $1 > 10 )); then
 
187
                dialog --backtitle "Compile-time Values" \
 
188
                        --title "Error Message" \
 
189
                        --msgbox \
 
190
                        "Invalid Buffer Unused Default Size Multiplier, must be between 2 and 10" 10 60
 
191
                status=1
 
192
        fi
 
193
        if (( $status == 0 )); then
 
194
                BUF_UNUSED_DEF_SIZE_MULT=$1
 
195
        fi
 
196
        return $status
 
197
}
 
198
 
 
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()
 
203
{
 
204
        declare -i status=0
 
205
 
 
206
        if [[ ! ($1 =~ ^[0-9]*$) ]]; then
 
207
                status=1
 
208
                dialog --backtitle "Compile-time Values" \
 
209
                        --title "Error Message" \
 
210
                        --msgbox \
 
211
                        "Buffer Max Unreachable Percentage must be numeric" \
 
212
                        10 60
 
213
        elif (( $1 < 20 || $1 > 50 )); then
 
214
                dialog --backtitle "Compile-time Values" \
 
215
                        --title "Error Message" \
 
216
                        --msgbox \
 
217
                        "Invalid Buffer Max Unreachable Percentage, must be between 20 and 50" 10 60
 
218
                status=1
 
219
        fi
 
220
        if (( $status == 0 )); then
 
221
                BUF_MAX_UNREACH_PERCENT=$1
 
222
        fi
 
223
        return $status
 
224
}
 
225
 
 
226
# Process Buffer values
 
227
# No parameters
 
228
# No return value
 
229
proc_buffer()
 
230
{
 
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:"
 
233
        declare -i i=0
 
234
        local ret_string
 
235
        declare -i retval=1
 
236
        declare -i status
 
237
        local value
 
238
        local values
 
239
 
 
240
        while true; do
 
241
                exec 3>&1
 
242
                values=$(dialog --help-button \
 
243
                --backtitle "Compile-time Values" \
 
244
                --title "Buffer values" \
 
245
                --form "$form_txt" \
 
246
                18 65 0 \
 
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 \
 
250
                2>&1 1>&3)
 
251
                retval=$?
 
252
                exec 3>&-
 
253
 
 
254
                case $retval in
 
255
                $DIALOG_OK)
 
256
                        i=0
 
257
                        for value in $values; do
 
258
                                if (( i == 0 )); then
 
259
                                        validate_def_buf_size "$value"
 
260
                                        status=$?
 
261
                                elif (( $i == 1 )); then
 
262
                                        validate_buf_unused_def_size_mult \
 
263
                                                "$value"
 
264
                                        status+=$?
 
265
                                elif (( $i == 2 )); then
 
266
                                        validate_buf_max_unreach_percent \
 
267
                                                "$value"
 
268
                                        status+=$?
 
269
                                fi
 
270
                                (( i++ ))
 
271
                        done
 
272
                        if (( $status == 0 )); then
 
273
                                break
 
274
                        fi
 
275
                        ;;
 
276
                $DIALOG_CANCEL)
 
277
                        break
 
278
                        ;;
 
279
                $DIALOG_HELP)
 
280
                        dialog --title "Main menu help" \
 
281
                        --backtitle "Compile-time Values" \
 
282
                        --msgbox "$form_help_txt" 10 50
 
283
                        ;;
 
284
                $DIALOG_ESC)
 
285
                        script_exit 0
 
286
                        ;;
 
287
                esac
 
288
        done
 
289
}
 
290
 
 
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()
 
295
{
 
296
        declare -i status=0
 
297
 
 
298
        if [[ ! ($1 =~ ^[0-9]*$) ]]; then
 
299
                status=1
 
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
 
307
                status=1
 
308
        fi
 
309
        if (( $status == 0 )); then
 
310
                DEF_MSG_SIZE=$1
 
311
        fi
 
312
        return $status
 
313
}
 
314
 
 
315
# Process Message values
 
316
# No parameters
 
317
# No return value
 
318
proc_message()
 
319
{
 
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)"
 
322
        declare -i i=0
 
323
        local ret_string
 
324
        declare -i retval=1
 
325
        declare -i status
 
326
        local value
 
327
 
 
328
        while true; do
 
329
                exec 3>&1
 
330
                value=$(dialog --help-button \
 
331
                --backtitle "Compile-time Values" \
 
332
                --title "Message values" \
 
333
                --inputbox "$input_txt" 15 60 "$DEF_MSG_SIZE" \
 
334
                2>&1 1>&3)
 
335
                retval=$?
 
336
                exec 3>&-
 
337
 
 
338
                case $retval in
 
339
                $DIALOG_OK)
 
340
                        validate_def_msg_size "$value"
 
341
                        status=$?
 
342
                        if (( $status == 0 )); then
 
343
                                break
 
344
                        fi
 
345
                        ;;
 
346
                $DIALOG_CANCEL)
 
347
                        break
 
348
                        ;;
 
349
                $DIALOG_HELP)
 
350
                        dialog --title "Main menu help" \
 
351
                        --backtitle "Compile-time Values" \
 
352
                        --msgbox "$input_help_txt" 10 50
 
353
                        ;;
 
354
                $DIALOG_ESC)
 
355
                        script_exit 0
 
356
                        ;;
 
357
                esac
 
358
        done
 
359
}
 
360
 
 
361
# Process level 0 menu
 
362
# No parameters
 
363
# No return value
 
364
proc_menu_0()
 
365
{
 
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."
 
367
        local menu_item
 
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:"
 
369
        local ret_string
 
370
        declare -i retval=1
 
371
 
 
372
 
 
373
        while true; do
 
374
                exec 3>&1
 
375
                menu_item=$(dialog --help-button \
 
376
                --backtitle "Compile-time Values" \
 
377
                --title "Main Menu" \
 
378
                --cancel-label "Exit" \
 
379
                --menu "$menu_txt" \
 
380
                18 60 \
 
381
                2 1 Buffer 2 Message \
 
382
                2>&1 1>&3)
 
383
                retval=$?
 
384
                exec 3>&-
 
385
 
 
386
                case $retval in
 
387
                $DIALOG_OK)
 
388
                        case $menu_item in
 
389
                        1)
 
390
                                proc_buffer
 
391
                                ;;
 
392
                        2)
 
393
                                proc_message
 
394
                                ;;
 
395
                        esac
 
396
                        ;;
 
397
                $DIALOG_EXIT)
 
398
                        break
 
399
                        ;;
 
400
                $DIALOG_HELP)
 
401
                        dialog --title "Main menu help" \
 
402
                        --backtitle "Compile-time Values" \
 
403
                        --msgbox "$menu_help_txt" 10 60
 
404
                        ;;
 
405
                $DIALOG_ESC)
 
406
                        script_exit 0
 
407
                        ;;
 
408
                esac
 
409
        done
 
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
 
416
        else
 
417
                printf "%s\n" "$ret_string"
 
418
        fi
 
419
}
 
420
 
 
421
 
 
422
########
 
423
# Main #
 
424
########
 
425
 
 
426
dialog_reqd
 
427
std_cmd_err_handler $?
 
428
 
 
429
proc_menu_0
 
430
std_cmd_err_handler $?
 
431
 
 
432
script_exit 0
 
433