From b6e946a3a087884ecaa250960103a54b1d81169a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 14 Nov 2022 08:26:15 -0500 Subject: [PATCH] utils/parse_directives: new automated parser for directive names. This probably isn't perfect, but it finds all of the directives we already had (and some new ones), so it doesn't make things worse. --- utils/parse_directives | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 utils/parse_directives diff --git a/utils/parse_directives b/utils/parse_directives new file mode 100755 index 0000000..5d71c6f --- /dev/null +++ b/utils/parse_directives @@ -0,0 +1,63 @@ +#!/bin/sh + +# Parse directive names out of Nagios' xdata/xodtemplate.c. + +EXIT_USAGE=1 +EXIT_INPUT_FILE_DOESNT_EXIST=2 + +posix_mktemp(){ + # Securely create a temporary file under ${TMPDIR} using the + # template "tmpXXXXXX", and echo the result to stdout. This works + # around the absence of "mktemp" in the POSIX standard. + printf 'mkstemp(template)' | m4 -D template="${TMPDIR:-/tmp}/tmpXXXXXX" +} + +if [ $# -lt 1 ]; then + echo "Usage: $0 " + exit $EXIT_USAGE +fi + + +INFILE="${1}" +TEMPFILE="$(posix_mktemp)" + +if [ ! -f "$INFILE" ]; then + echo "Error: input file $INFILE doesn't exist." + exit $EXIT_INPUT_FILE_DOESNT_EXIST +fi + +# The list of weekday names, taken from xdata/xodtemplate.c. The +# Nagios strcmp parser doesn't look for them individually. Since +# they're unlikely to change, appending them manually sounds +# reasonable to me. +printf '"sunday" +"monday" +"tuesday" +"wednesday" +"thursday" +"friday" +"saturday" +' >> "${TEMPFILE}" + +# This grabs all of the variable names that are explicitly checked for +# with strcmp in xdata/xodtemplate.c. We include these because some +# valid variable names (e.g. "hostgroups") are implicitly mapped to +# "real" variables whose names differ. +# +# The "name", "register", and "use" directives are special (literally, +# they get the "special" font in nagios-mode.el), so we drop them from +# the list. +# +# Before we parse the names, though, we move each !strcmp to its own +# line, which greatly simplifies the parsing. +VARCMP_REGEX='.*!strcmp(variable,[[:space:]]*"\([A-Za-z_]\{1,\}\)").*' +sed "s/\!strcmp(/\n\!strcmp(/g" < "${INFILE}" \ + | sed -n "s/${VARCMP_REGEX}/\"\1\"/p" \ + | sed '/^"name\|use\|register"$/d' \ + >> "${TEMPFILE}" + +# Finally, sort the list and print it. +sort < "${TEMPFILE}" | uniq + +# ... and clean up +rm "${TEMPFILE}" -- 2.43.2