-
Notifications
You must be signed in to change notification settings - Fork 0
/
msp430macro.sh
119 lines (98 loc) · 2.55 KB
/
msp430macro.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
#!/usr/bin/env bash
##
## msp430macro - grep MSP430 macros/registers in device-specific header file
## Copyright (C) 2020-2023 Daniel Haase
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/gpl.txt>.
##
set -o errexit
set -o nounset
set -o pipefail
set -o noclobber
TITLE="msp430macro"
VERSION="0.3.5"
DEFAULT_DEVICE_NAME="msp430f5529"
DEVICE_NAME="${DEVICE_NAME:-"${DEFAULT_DEVICE_NAME}"}"
INCLUDE_PATH="${INCLUDE_PATH:-"/opt/ti/mspgcc/include"}"
function check_command {
if ! command -v "${1}" &>/dev/null; then
echo >&2 "no such command \"${1}\""
exit 1
fi
}
function print_version {
cat <<-EOF
${TITLE} version ${VERSION}
copyright (c) 2020-2023 Daniel Haase
EOF
}
function print_usage {
print_version
cat <<-EOF
usage: ${TITLE} [<device>] <pattern>
${TITLE} [--version | --help]
<device>
full MSP430 device name (e.g., \"msp430f2013\")
(defaults to \"${DEFAULT_DEVICE_NAME}\")
<pattern>
pattern to search for (e.g., macro, register, comment, ...)
-V | --version
print version information and exit
-h | --help
print this usage description and exit
EOF
}
check_command "cat"
check_command "grep"
declare pattern
if [[ $# -eq 0 ]]; then
print_usage
exit 2
elif [[ $# -eq 1 ]]; then
case "${1}" in
-V | --version)
print_version
exit 0
;;
-h | --help)
print_usage
exit 0
;;
*)
pattern="${1}"
;;
esac
elif [[ $# -ge 2 ]]; then
DEVICE_NAME="${1}"
shift
pattern="${*}"
fi
if [[ ! -d "${INCLUDE_PATH}" ]]; then
echo >&2 "no such include directory \"${INCLUDE_PATH}\""
exit 3
fi
declare -r filepath="${INCLUDE_PATH}/${DEVICE_NAME}.h"
if [[ ! -f "${filepath}" ]]; then
echo >&2 "no such header file \"${filepath}\""
exit 3
fi
if [[ ! -r "${filepath}" ]]; then
echo >&2 "no read permission for file \"${filepath}\""
exit 3
fi
if ! grep --color=auto "${pattern}" "${filepath}" 2>/dev/null; then
echo >&2 "no matches found for pattern \"${pattern}\""
exit 4
fi
exit 0