]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - utils/parse_macros
Added a ChangeLog.
[nagios-mode.git] / utils / parse_macros
1 #!/bin/bash
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 EXIT_OUTPUT_FILE_EXISTS=3
12 EXIT_TEMP_FILE_EXISTS=4
13
14 if [ $# -lt 2 ]; then
15 echo "Usage: $0 <input_file> <output_file>"
16 exit $EXIT_USAGE
17 fi
18
19
20 INFILE="$1"
21 OUTFILE="$2"
22 TEMPFILE="${OUTFILE}.tmp"
23
24 if [ ! -f "$INFILE" ]; then
25 echo "Error: input file $INFILE doesn't exist."
26 exit $EXIT_INPUT_FILE_DOESNT_EXIST
27 fi
28
29 if [ -f "$OUTFILE" ]; then
30 echo "Error: output file $OUTFILE already exists."
31 exit $EXIT_OUTPUT_FILE_EXISTS
32 fi
33
34 if [ -f "$TEMPFILE" ]; then
35 echo "Error: temporary file $TEMPFILE already exists."
36 exit $EXIT_TEMP_FILE_EXISTS
37 fi
38
39 # Common options. Color screws up the output.
40 GREP="grep --extended-regexp --color=never"
41
42 # Grep grabs all of the "#define MACRO..." lines, except for
43 # MACRO_X_COUNT and MACRO_ENV_VAR_PREFIX which aren't real macros.
44 # Sed just removes the "MACRO_" prefixes.
45 $GREP --only-matching '[[:space:]]MACRO_[[:alnum:]_]+' "$INFILE" \
46 | $GREP -v 'MACRO_X_COUNT' \
47 | $GREP -v 'MACRO_ENV_VAR_PREFIX' \
48 | sed 's/ MACRO_//g' >> "$OUTFILE"
49
50 # We need to include ARG1, ARG2... ARG<max>,
51 # where the maximum is defined in macros.h.
52 MAXARG=`$GREP 'MAX_COMMAND_ARGUMENTS' "$INFILE" \
53 | $GREP --only-matching '[[:digit:]]+'`
54
55 for argnum in `seq 1 $MAXARG`; do
56 echo "ARG${argnum}" >> "$OUTFILE"
57 done
58
59 # And, the USER1, USER2... USER<max> macros.
60 # Same as above.
61 MAXUSER=`$GREP 'MAX_USER_MACROS' "$INFILE" \
62 | $GREP --only-matching '[[:digit:]]+'`
63
64 for argnum in `seq 1 $MAXUSER`; do
65 echo "USER${argnum}" >> "$OUTFILE"
66 done
67
68 # Now, we sort the thing.
69 sort "$OUTFILE" > "$TEMPFILE"
70
71 # Add quotes and '\\$' before and after each macro. This just
72 # makes it easy to cut-and-pase into nagios-mode.el.
73 sed -e 's/^/\"\\\\\$/g' -e 's/$/\\\\\$\"/g' "$TEMPFILE" > "$OUTFILE"
74 rm "$TEMPFILE"