-
Notifications
You must be signed in to change notification settings - Fork 5
/
menu-dialog.sh
executable file
·373 lines (352 loc) · 10.3 KB
/
menu-dialog.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env sh
#
# SweetAda configuration and Makefile front-end (dialog version).
#
# Copyright (C) 2020-2024 Gabriele Galeotti
#
# This work is licensed under the terms of the MIT License.
# Please consult the LICENSE.txt file located in the top-level directory.
#
#
# Arguments:
# -h = help
# -p = pause before exiting
# <action> = action to perform: "configure", "all", etc
#
# Environment variables:
# PLATFORM
# SUBPLATFORM
#
################################################################################
# Script initialization. #
# #
################################################################################
SCRIPT_FILENAME=$(basename "$0")
LOG_FILENAME=""
if [ "x${LOG_FILENAME}" != "x" ] ; then
rm -f "${LOG_FILENAME}"
touch "${LOG_FILENAME}"
fi
################################################################################
# log_print() #
# #
################################################################################
log_print()
{
if [ "x${LOG_FILENAME}" != "x" ] ; then
printf "%s\n" "$1" | tee -a "${LOG_FILENAME}"
else
printf "%s\n" "$1"
fi
return 0
}
################################################################################
# log_print_error() #
# #
################################################################################
log_print_error()
{
if [ "x${LOG_FILENAME}" != "x" ] ; then
printf "%s\n" "$1" | tee -a "${LOG_FILENAME}" 1>&2
else
printf "%s\n" "$1" 1>&2
fi
return 0
}
################################################################################
# dialog_menu() #
# #
################################################################################
dialog_menu()
{
_dialog_items_string=""
_nitems=0
_dialog_height=50
_dialog_width=64
for _s in $2 ; do
# tag item status
_dialog_items_string="${_dialog_items_string} \"${_s}\" \"\" off"
_nitems=$((_nitems+1))
done
_dialog_result=$(printf "%s\n" "${_dialog_items_string}" | xargs \
dialog \
--stdout \
${ERASE_ON_EXIT} \
--radiolist \
"\"$1\"" \
${_dialog_height} \
${_dialog_width} \
${_nitems} \
2> /dev/null)
printf "%s\n" ""
eval $3=${_dialog_result}
return 0
}
################################################################################
# createkernelcfg() #
# #
################################################################################
createkernelcfg()
{
if [ "x${PLATFORM}" = "x" ] ; then
# discard SUBPLATFORM
SUBPLATFORM=""
# no PLATFORM supplied, select from menu
_platforms=$(cd platforms && ls -A -d * 2> /dev/null)
dialog_menu "Select Platform" "${_platforms}" "PLATFORM"
_platform_from_command_line="N"
else
_platform_from_command_line="Y"
fi
# check whether PLATFORM is available, either from command line or the select menu
if [ "x${PLATFORM}" != "x" ] ; then
_subplatforms=$(cd platforms/${PLATFORM} && ls -A -d platform-* 2> /dev/null | sed -e "s|platform-||g" -e "s|/\$||g")
if [ "x${_subplatforms}" != "x" ] ; then
# subplatforms exist
if [ "x${SUBPLATFORM}" = "x" ] ; then
# no SUBPLATFORM supplied
if [ "${_platform_from_command_line}" = "N" ] ; then
dialog_menu "Select sub-Platform" "${_subplatforms}" "SUBPLATFORM"
fi
# if no SUBPLATFORM supplied, stop configuring
if [ "x${SUBPLATFORM}" = "x" ] ; then
log_print "Available subplatforms are:"
log_print "${_subplatforms}"
return 1
fi
fi
else
# no subplatforms
if [ "${_platform_from_command_line}" = "Y" ] ; then
# wrongly entered as a command line parameter
log_print "${SCRIPT_FILENAME}: *** Warning: no subplatform \"${SUBPLATFORM}\" for platform \"${PLATFORM}\", ignoring."
fi
SUBPLATFORM=""
fi
return 0
fi
return 1
}
################################################################################
# make_tee() #
# #
# $1 make target #
# $2 make errors file #
# $3 tee logfile #
################################################################################
make_tee()
{
exec 4>&1
_exit_status=$( \
{ \
{ \
${MAKE} "$1" 2> "$2" 3>&- ; \
printf "%s" "$?" 1>&3 ; \
} 4>&- | tee "$3" 1>&4 ; \
} 3>&1 \
)
exec 4>&-
return ${_exit_status}
}
################################################################################
# log_build_errors() #
# #
################################################################################
log_build_errors()
{
if [ -s make.errors.log ] ; then
printf "%s\n" ""
printf "%s\n" "Detected errors and/or warnings:"
printf "%s\n" "--------------------------------"
cat make.errors.log
fi
return 0
}
################################################################################
# action_execute() #
# #
################################################################################
action_execute()
{
case $1 in
"createkernelcfg")
rm -f make.log make.errors.log
createkernelcfg
if [ $? -eq 0 ] ; then
PLATFORM=${PLATFORM} SUBPLATFORM=${SUBPLATFORM} "${MAKE}" createkernelcfg
_exit_status=$?
log_build_errors
fi
# invalidate PLATFORM and SUBPLATFORM
PLATFORM=
SUBPLATFORM=
;;
"configure")
rm -f make.log make.errors.log
"${MAKE}" configure
_exit_status=$?
log_build_errors
;;
"all")
rm -f make.log make.errors.log
make_tee all make.errors.log make.log
_exit_status=$?
log_build_errors
;;
"kernel")
rm -f make.log make.errors.log
make_tee kernel make.errors.log make.log
_exit_status=$?
log_build_errors
;;
"postbuild")
rm -f make.log make.errors.log
make_tee postbuild make.errors.log make.log
_exit_status=$?
log_build_errors
;;
"session-start")
"${MAKE}" session-start
_exit_status=$?
;;
"session-end")
"${MAKE}" session-end
_exit_status=$?
;;
"run")
"${MAKE}" run
_exit_status=$?
;;
"debug")
"${MAKE}" debug
_exit_status=$?
;;
"clean")
"${MAKE}" clean
_exit_status=$?
;;
"distclean")
"${MAKE}" distclean
_exit_status=$?
;;
"rts")
rm -f make.log make.errors.log
make_tee rts make.errors.log make.log
_exit_status=$?
log_build_errors
;;
*)
usage
_exit_status=1
;;
esac
return ${_exit_status}
}
################################################################################
# usage() #
# #
################################################################################
usage()
{
printf "%s\n" "${SCRIPT_FILENAME} [-h] [-p] <action>"
printf "%s\n" "-h = help"
printf "%s\n" "-p = pause after execution"
printf "%s\n" "actions:"
printf "%s\n" " createkernelcfg"
printf "%s\n" " configure"
printf "%s\n" " all"
printf "%s\n" " kernel"
printf "%s\n" " postbuild"
printf "%s\n" " session-start"
printf "%s\n" " session-end"
printf "%s\n" " run"
printf "%s\n" " debug"
printf "%s\n" " clean"
printf "%s\n" " distclean"
printf "%s\n" " rts"
return 0
}
################################################################################
# Main loop. #
# #
################################################################################
#
# Some ancient versions of dialog do not have this option.
#
DIALOG_VERSION=$(dialog --version | sed -e "s|Version: \([0-9]*[.]*\)*-||")
if [ "${DIALOG_VERSION}" -ge 20201126 ] ; then
ERASE_ON_EXIT="--erase-on-exit"
else
ERASE_ON_EXIT=""
fi
if [ "x${MAKE}" = "x" ] ; then
MAKE=make
fi
PLATFORM=
SUBPLATFORM=
CPU=
ACTIONS="createkernelcfg configure all kernel postbuild session-start session-end run debug clean distclean rts"
ACTION=""
PAUSE=""
#
# Parse command line arguments.
#
token_seen=N
while [ $# -gt 0 ] ; do
if [ "x${1%${1#?}}" = "x-" ] ; then
# "-" option
argument=${1#?}
case ${argument} in
"h")
usage ; exit $?
;;
"p")
PAUSE="Y"
;;
*)
printf "%s\n" "Unknown option \"${argument}\"." 1>&2
usage
exit 1
;;
esac
else
# no "-" option
if [ "x${token_seen}" = "xN" ] ; then
token_seen=Y
ACTION="$1"
else
printf "%s\n" "Multiple actions specified." 1>&2
usage
exit 1
fi
fi
shift
done
#
# Main logic.
#
if [ "x${ACTION}" = "x" ] ; then
while true ; do
dialog_menu "Select action" "${ACTIONS}" "RESULT"
if [ "x${RESULT}" = "x" ] ; then
exit_status=0
break
fi
action_execute ${RESULT}
exit_status=$?
if [ "${exit_status}" -eq 0 ] ; then
printf "%s" "Press <ENTER> to continue: "
read answer
else
break
fi
done
else
action_execute ${ACTION}
exit_status=$?
if [ "x${PAUSE}" = "xY" ] ; then
printf "%s" "Press <ENTER> to continue: "
read answer
fi
fi
exit ${exit_status}