]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - utils/parse_macros
2f63b7304a3f71519df6252c4efc961ef7f2520e
[nagios-mode.git] / utils / parse_macros
1 #!/bin/sh
2 #
3 # Parse the macro names out of Nagios' include/macros.h.
4 #
5 # Operates on whatever file you give it. That should probably be
6 # include/macros.h. Just sayin'.
7 #
8
9 EXIT_USAGE=1
10 EXIT_INPUT_FILE_DOESNT_EXIST=2
11
12 posix_mktemp(){
13 # Securely create a temporary file under ${TMPDIR} using the
14 # template "tmpXXXXXX", and echo the result to stdout. This works
15 # around the absence of "mktemp" in the POSIX standard.
16 printf 'mkstemp(template)' | m4 -D template="${TMPDIR:-/tmp}/tmpXXXXXX"
17 }
18
19 if [ $# -lt 1 ]; then
20 echo "Usage: $0 <input_file>"
21 exit $EXIT_USAGE
22 fi
23
24
25 INFILE="$1"
26 OUTFILE="$2"
27 TEMPFILE="$(posix_mktemp)"
28
29 if [ ! -f "$INFILE" ]; then
30 echo "Error: input file $INFILE doesn't exist."
31 exit $EXIT_INPUT_FILE_DOESNT_EXIST
32 fi
33
34 # Grabs all of the "#define MACRO..." lines, except for MACRO_X_COUNT
35 # and MACRO_ENV_VAR_PREFIX which aren't real macros, and outputs the
36 # macro name (everything after "MACRO_") enclosed in quotes and dollar
37 # signs, appropriate to be copy/pasted directly into nagios-mode.el.
38 MACRO_REGEX='#define[[:space:]]\{1,\}MACRO_\([^[:space:]]\{1,\}\)[[:space:]]\{1,\}.*'
39 sed -n "s/${MACRO_REGEX}/\"\$\1\$\"/p" "${INFILE}" \
40 | sed '/X_COUNT\|ENV_VAR_PREFIX/d' \
41 >> "${TEMPFILE}"
42
43 # Add "$ARG1$", "$ARG2$",... "$ARG<max>$" to the list, where <max> is
44 # defined in macros.h.
45 MAXARG_REGEX='#define[[:space:]]\{1,\}MAX_COMMAND_ARGUMENTS[[:space:]]\{1,\}\([1-9][0-9]*\).*'
46 MAXARG=$(sed -n "s/${MAXARG_REGEX}/\1/p" "${INFILE}")
47
48 for argnum in `seq 1 $MAXARG`; do
49 echo "\"\$ARG${argnum}\$\"" >> "${TEMPFILE}"
50 done
51
52 # Also the "$USER<N>$" macros, similar to the above.
53 MAXUSER_REGEX='#define[[:space:]]\{1,\}MAX_USER_MACROS[[:space:]]\{1,\}\([1-9][0-9]*\).*'
54 MAXUSER=$(sed -n "s/${MAXUSER_REGEX}/\1/p" "${INFILE}")
55
56 for argnum in `seq 1 $MAXUSER`; do
57 echo "\"\$USER${argnum}\$\"" >> "${TEMPFILE}"
58 done
59
60 # Finally, sort the list and print it.
61 sort < "${TEMPFILE}"
62
63 # ... and clean up
64 rm "${TEMPFILE}"