-
Notifications
You must be signed in to change notification settings - Fork 0
/
regina-synchk
81 lines (64 loc) · 2.67 KB
/
regina-synchk
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
#!/usr/bin/env bash
clean () {
rm -f ${stripped} ${numbered} ${log}
}
# Ensure single, non-empty string argument passed
[ $# -ne 1 -o -z "${1}" ] && { echo "Usage: ${0} <Rexx file>" ; exit 1 ; }
# Input file: Rexx script sans extension
source="${1##*/}" ; source="${source%.*}"
# Check whether source file exists
[ -f "${source}.rexx" ] || { echo "Source file not found" ; exit 1 ; }
# Check whether Regina executable file exists
which regina >/dev/null || { echo "Regina executable not in path" ; exit 1 ; }
# Temporary files
stripped="${source}.stripped.rexx"
numbered="${source}.numbered.rexx"
log="${source}.log.txt"
# Strip all comments from script
sed -E 's/^\s*(.*)\/\*.*\*\/\s*(.*)/\1\2/g' "${source}.rexx" \
| sed -E 's/^\s*(.*)\/\*.*\*\/\s*(.*)/\1\2/g' \
| sed '/\/\*/,/*\//d;/^\s*$/d' > "${stripped}"
# Number the stripped script
cat -n "${stripped}" > "${numbered}"
# Execute script capturing error output
regina -tO "${stripped}" 2> "${log}" 1> /dev/null
# Remove any stack trace lines from the log file
sed -i '/\+\+\+/d' "${log}"
# Determine number of lines in the log file
nlines=$(wc -l "${log}" | cut -d' ' -f 1)
# Handle special case of 'command not found' /
# 'not recognized as an internal or external command'
chkline=$(head -1 "${log}") ; echo "${chkline}" | grep 'not found' 2>&1 >/dev/null
if [ $? -eq 0 ] ; then
cmdname=$(echo "${chkline}" | cut -d' ' -f2 | cut -d':' -f1)
else
echo "${chkline}" | grep 'external command' 2>&1 >/dev/null
[ $? -eq 0 ] && cmdname=$(echo "${chkline}" | cut -d' ' -f1 | tr -d "'")
fi
if [ -z "${cmdname}" ] ; then
# Conditionaliy parse log output
case "${nlines}" in
0) clean ; exit 0 ;;
1) logdata=$(sed -E 's/^Error\s*([0-9]+)\s*.*,\s*line\s*([0-9]+)\:\s*(.*)/\2#\3/g' "${log}") ;;
2) echo "${chkline}" | grep '\[.*\]' 2>&1 >/dev/null
[ $? -eq 0 ] \
&& logdata=$(tail -1 "${log}" | sed -E 's/.*\[(.*) at line\s*([0-9]+).*/\2#\1/g') \
|| logdata=$(echo "${chkline}" | sed -E 's/^Error\s*([0-9]+)\s*.*,\s*line\s*([0-9]+)\:\s*(.*)/\2#\3/g') ;;
*) printf 'Corrupted log file contents' ; clean ; exit 1 ;;
esac
lineno=$(echo "${logdata}" | cut -d'#' -f1)
msg=$(echo "${logdata}" | cut -d'#' -f2)
line=$(grep -E "^\s*${lineno}" "${numbered}" | tr '\t' ' ' | tr -s ' ')
else
line=$(grep -i "${cmdname}" "${numbered}" | tr '\t' ' ' | tr -s ' ')
lineno=$(echo "${line}" | cut -d' ' -f2)
msg='Procedure or function name is probably misspelled'
fi
linecode=$(echo "${line}" | cut -d' ' -f3-)
# Emit source code for context, and highlight source of problem
cat "${numbered}"
printf "\n${msg} on LINE: ${lineno} -> ${linecode}\n"
# Remove temporary files
clean
# Assume a syntax error occurred
exit 1