]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - utils/parse_macros
utils/parse_macros: drop obsolete $OUTFILE variable.
[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 TEMPFILE="$(posix_mktemp)"
27
28 if [ ! -f "$INFILE" ]; then
29 echo "Error: input file $INFILE doesn't exist."
30 exit $EXIT_INPUT_FILE_DOESNT_EXIST
31 fi
32
33 # Grabs all of the "#define MACRO..." lines, except for MACRO_X_COUNT
34 # and MACRO_ENV_VAR_PREFIX which aren't real macros, and outputs the
35 # macro name (everything after "MACRO_") enclosed in quotes and dollar
36 # signs, appropriate to be copy/pasted directly into nagios-mode.el.
37 MACRO_REGEX='#define[[:space:]]\{1,\}MACRO_\([^[:space:]]\{1,\}\)[[:space:]]\{1,\}.*'
38 sed -n "s/${MACRO_REGEX}/\"\$\1\$\"/p" "${INFILE}" \
39 | sed '/X_COUNT\|ENV_VAR_PREFIX/d' \
40 >> "${TEMPFILE}"
41
42 # Add "$ARG1$", "$ARG2$",... "$ARG<max>$" to the list, where <max> is
43 # defined in macros.h.
44 MAXARG_REGEX='#define[[:space:]]\{1,\}MAX_COMMAND_ARGUMENTS[[:space:]]\{1,\}\([1-9][0-9]*\).*'
45 MAXARG=$(sed -n "s/${MAXARG_REGEX}/\1/p" "${INFILE}")
46
47 for argnum in `seq 1 $MAXARG`; do
48 echo "\"\$ARG${argnum}\$\"" >> "${TEMPFILE}"
49 done
50
51 # Also the "$USER<N>$" macros, similar to the above.
52 MAXUSER_REGEX='#define[[:space:]]\{1,\}MAX_USER_MACROS[[:space:]]\{1,\}\([1-9][0-9]*\).*'
53 MAXUSER=$(sed -n "s/${MAXUSER_REGEX}/\1/p" "${INFILE}")
54
55 for argnum in `seq 1 $MAXUSER`; do
56 echo "\"\$USER${argnum}\$\"" >> "${TEMPFILE}"
57 done
58
59 # Finally, sort the list and print it.
60 sort < "${TEMPFILE}"
61
62 # ... and clean up
63 rm "${TEMPFILE}"