--- /dev/null
+#!/bin/bash
+#
+# Parse the macro names out of Nagios' include/macros.h.
+#
+# Operates on whatever file you give it. That should probably be
+# include/macros.h. Just sayin'.
+#
+
+EXIT_USAGE=1
+EXIT_INPUT_FILE_DOESNT_EXIST=2
+EXIT_OUTPUT_FILE_EXISTS=3
+EXIT_TEMP_FILE_EXISTS=4
+
+if [ $# -lt 2 ]; then
+ echo "Usage: $0 <input_file> <output_file>"
+ exit $EXIT_USAGE
+fi
+
+
+INFILE="$1"
+OUTFILE="$2"
+TEMPFILE="${OUTFILE}.tmp"
+
+if [ ! -f "$INFILE" ]; then
+ echo "Error: input file $INFILE doesn't exist."
+ exit $EXIT_INPUT_FILE_DOESNT_EXIST
+fi
+
+if [ -f "$OUTFILE" ]; then
+ echo "Error: output file $OUTFILE already exists."
+ exit $EXIT_OUTPUT_FILE_EXISTS
+fi
+
+if [ -f "$TEMPFILE" ]; then
+ echo "Error: temporary file $TEMPFILE already exists."
+ exit $EXIT_TEMP_FILE_EXISTS
+fi
+
+# Common options. Color screws up the output.
+GREP="grep --extended-regexp --color=never"
+
+# Grep grabs all of the "#define MACRO..." lines, except for
+# MACRO_X_COUNT and MACRO_ENV_VAR_PREFIX which aren't real macros.
+# Sed just removes the "MACRO_" prefixes.
+$GREP --only-matching '[[:space:]]MACRO_[[:alnum:]_]+' "$INFILE" \
+ | $GREP -v 'MACRO_X_COUNT' \
+ | $GREP -v 'MACRO_ENV_VAR_PREFIX' \
+ | sed 's/ MACRO_//g' >> "$OUTFILE"
+
+# We need to include ARG1, ARG2... ARG<max>,
+# where the maximum is defined in macros.h.
+MAXARG=`$GREP 'MAX_COMMAND_ARGUMENTS' "$INFILE" \
+ | $GREP --only-matching '[[:digit:]]+'`
+
+for argnum in `seq 1 $MAXARG`; do
+ echo "ARG${argnum}" >> "$OUTFILE"
+done
+
+# And, the USER1, USER2... USER<max> macros.
+# Same as above.
+MAXUSER=`$GREP 'MAX_USER_MACROS' "$INFILE" \
+ | $GREP --only-matching '[[:digit:]]+'`
+
+for argnum in `seq 1 $MAXUSER`; do
+ echo "USER${argnum}" >> "$OUTFILE"
+done
+
+# Now, we sort the thing.
+sort "$OUTFILE" > "$TEMPFILE"
+
+# Add quotes and '\\$' before and after each macro. This just
+# makes it easy to cut-and-pase into nagios-mode.el.
+sed -e 's/^/\"\\\\\$/g' -e 's/$/\\\\\$\"/g' "$TEMPFILE" > "$OUTFILE"
+rm "$TEMPFILE"