]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - utils/parse_directives
utils/parse_directives: new automated parser for directive names.
[nagios-mode.git] / utils / parse_directives
1 #!/bin/sh
2
3 # Parse directive names out of Nagios' xdata/xodtemplate.c.
4
5 EXIT_USAGE=1
6 EXIT_INPUT_FILE_DOESNT_EXIST=2
7
8 posix_mktemp(){
9 # Securely create a temporary file under ${TMPDIR} using the
10 # template "tmpXXXXXX", and echo the result to stdout. This works
11 # around the absence of "mktemp" in the POSIX standard.
12 printf 'mkstemp(template)' | m4 -D template="${TMPDIR:-/tmp}/tmpXXXXXX"
13 }
14
15 if [ $# -lt 1 ]; then
16 echo "Usage: $0 <path-to-xodtemplate.c>"
17 exit $EXIT_USAGE
18 fi
19
20
21 INFILE="${1}"
22 TEMPFILE="$(posix_mktemp)"
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 # The list of weekday names, taken from xdata/xodtemplate.c. The
30 # Nagios strcmp parser doesn't look for them individually. Since
31 # they're unlikely to change, appending them manually sounds
32 # reasonable to me.
33 printf '"sunday"
34 "monday"
35 "tuesday"
36 "wednesday"
37 "thursday"
38 "friday"
39 "saturday"
40 ' >> "${TEMPFILE}"
41
42 # This grabs all of the variable names that are explicitly checked for
43 # with strcmp in xdata/xodtemplate.c. We include these because some
44 # valid variable names (e.g. "hostgroups") are implicitly mapped to
45 # "real" variables whose names differ.
46 #
47 # The "name", "register", and "use" directives are special (literally,
48 # they get the "special" font in nagios-mode.el), so we drop them from
49 # the list.
50 #
51 # Before we parse the names, though, we move each !strcmp to its own
52 # line, which greatly simplifies the parsing.
53 VARCMP_REGEX='.*!strcmp(variable,[[:space:]]*"\([A-Za-z_]\{1,\}\)").*'
54 sed "s/\!strcmp(/\n\!strcmp(/g" < "${INFILE}" \
55 | sed -n "s/${VARCMP_REGEX}/\"\1\"/p" \
56 | sed '/^"name\|use\|register"$/d' \
57 >> "${TEMPFILE}"
58
59 # Finally, sort the list and print it.
60 sort < "${TEMPFILE}" | uniq
61
62 # ... and clean up
63 rm "${TEMPFILE}"