|
Packit |
209faa |
#! /bin/sh
|
|
Packit |
209faa |
# Copyright 2014 The Kyua Authors.
|
|
Packit |
209faa |
# All rights reserved.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Redistribution and use in source and binary forms, with or without
|
|
Packit |
209faa |
# modification, are permitted provided that the following conditions are
|
|
Packit |
209faa |
# met:
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# * Redistributions of source code must retain the above copyright
|
|
Packit |
209faa |
# notice, this list of conditions and the following disclaimer.
|
|
Packit |
209faa |
# * Redistributions in binary form must reproduce the above copyright
|
|
Packit |
209faa |
# notice, this list of conditions and the following disclaimer in the
|
|
Packit |
209faa |
# documentation and/or other materials provided with the distribution.
|
|
Packit |
209faa |
# * Neither the name of Google Inc. nor the names of its contributors
|
|
Packit |
209faa |
# may be used to endorse or promote products derived from this software
|
|
Packit |
209faa |
# without specific prior written permission.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
209faa |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
209faa |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
209faa |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
209faa |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
209faa |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
209faa |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
209faa |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
209faa |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
209faa |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
209faa |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# \file doc/manbuild.sh
|
|
Packit |
209faa |
# Generates a manual page from a source file.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Input files can have __VAR__-style patterns in them that are replaced
|
|
Packit |
209faa |
# with the values provided by the caller via the -v VAR=VALUE flag.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Input files can also include other files using the __include__ directive,
|
|
Packit |
209faa |
# which takes a relative path to the file to include plus an optional
|
|
Packit |
209faa |
# collection of additional variables to replace in the included file.
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Name of the running program for error reporting purposes.
|
|
Packit |
209faa |
Prog_Name="${0##*/}"
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Prints an error message and exits.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Args:
|
|
Packit |
209faa |
# ...: The error message to print. Multiple arguments are joined with a
|
|
Packit |
209faa |
# single space separator.
|
|
Packit |
209faa |
err() {
|
|
Packit |
209faa |
echo "${Prog_Name}: ${*}" 1>&2
|
|
Packit |
209faa |
exit 1
|
|
Packit |
209faa |
}
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Invokes sed(1) translating input variables to expressions.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Args:
|
|
Packit |
209faa |
# ...: List of var=value pairs to replace.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Returns:
|
|
Packit |
209faa |
# True if the operation succeeds; false otherwise.
|
|
Packit |
209faa |
sed_with_vars() {
|
|
Packit |
209faa |
local vars="${*}"
|
|
Packit |
209faa |
|
|
Packit |
209faa |
set --
|
|
Packit |
209faa |
for pair in ${vars}; do
|
|
Packit |
209faa |
local var="$(echo "${pair}" | cut -d = -f 1)"
|
|
Packit |
209faa |
local value="$(echo "${pair}" | cut -d = -f 2-)"
|
|
Packit |
209faa |
set -- "${@}" -e"s&__${var}__&${value}&g"
|
|
Packit |
209faa |
done
|
|
Packit |
209faa |
|
|
Packit |
209faa |
if [ "${#}" -gt 0 ]; then
|
|
Packit |
209faa |
sed "${@}"
|
|
Packit |
209faa |
else
|
|
Packit |
209faa |
cat
|
|
Packit |
209faa |
fi
|
|
Packit |
209faa |
}
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Generates the manual page reading from stdin and dumping to stdout.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Args:
|
|
Packit |
209faa |
# include_dir: Path to the directory containing the include files.
|
|
Packit |
209faa |
# ...: List of var=value pairs to replace in the manpage.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Returns:
|
|
Packit |
209faa |
# True if the generation succeeds; false otherwise.
|
|
Packit |
209faa |
generate() {
|
|
Packit |
209faa |
local include_dir="${1}"; shift
|
|
Packit |
209faa |
|
|
Packit |
209faa |
while :; do
|
|
Packit |
209faa |
local read_ok=yes
|
|
Packit |
209faa |
local oldifs="${IFS}"
|
|
Packit |
209faa |
IFS=
|
|
Packit |
209faa |
read -r line || read_ok=no
|
|
Packit |
209faa |
IFS="${oldifs}"
|
|
Packit |
209faa |
[ "${read_ok}" = yes ] || break
|
|
Packit |
209faa |
|
|
Packit |
209faa |
case "${line}" in
|
|
Packit |
209faa |
__include__*)
|
|
Packit |
209faa |
local file="$(echo "${line}" | cut -d ' ' -f 2)"
|
|
Packit |
209faa |
local extra_vars="$(echo "${line}" | cut -d ' ' -f 3-)"
|
|
Packit |
209faa |
# If we fail to output the included file, just leave the line as
|
|
Packit |
209faa |
# is. validate_file() will later error out.
|
|
Packit |
209faa |
[ -f "${include_dir}/${file}" ] || echo "${line}"
|
|
Packit |
209faa |
generate <"${include_dir}/${file}" "${include_dir}" \
|
|
Packit |
209faa |
"${@}" ${extra_vars} || echo "${line}"
|
|
Packit |
209faa |
;;
|
|
Packit |
209faa |
|
|
Packit |
209faa |
*)
|
|
Packit |
209faa |
echo "${line}"
|
|
Packit |
209faa |
;;
|
|
Packit |
209faa |
esac
|
|
Packit |
209faa |
done | sed_with_vars "${@}"
|
|
Packit |
209faa |
}
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Validates that the manual page has been properly generated.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# In particular, this checks if any directives or common replacement patterns
|
|
Packit |
209faa |
# have been left in place.
|
|
Packit |
209faa |
#
|
|
Packit |
209faa |
# Returns:
|
|
Packit |
209faa |
# True if the manual page is valid; false otherwise.
|
|
Packit |
209faa |
validate_file() {
|
|
Packit |
209faa |
local filename="${1}"
|
|
Packit |
209faa |
|
|
Packit |
209faa |
if grep '__[A-Za-z0-9]*__' "${filename}" >/dev/null; then
|
|
Packit |
209faa |
return 1
|
|
Packit |
209faa |
else
|
|
Packit |
209faa |
return 0
|
|
Packit |
209faa |
fi
|
|
Packit |
209faa |
}
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
# Program entry point.
|
|
Packit |
209faa |
main() {
|
|
Packit |
209faa |
local vars=
|
|
Packit |
209faa |
|
|
Packit |
209faa |
while getopts :v: arg; do
|
|
Packit |
209faa |
case "${arg}" in
|
|
Packit |
209faa |
v)
|
|
Packit |
209faa |
vars="${vars} ${OPTARG}"
|
|
Packit |
209faa |
;;
|
|
Packit |
209faa |
|
|
Packit |
209faa |
\?)
|
|
Packit |
209faa |
err "Unknown option -${OPTARG}"
|
|
Packit |
209faa |
;;
|
|
Packit |
209faa |
esac
|
|
Packit |
209faa |
done
|
|
Packit |
209faa |
shift $((${OPTIND} - 1))
|
|
Packit |
209faa |
|
|
Packit |
209faa |
[ ${#} -eq 2 ] || err "Must provide input and output names as arguments"
|
|
Packit |
209faa |
local input="${1}"; shift
|
|
Packit |
209faa |
local output="${1}"; shift
|
|
Packit |
209faa |
|
|
Packit |
209faa |
trap "rm -f '${output}.tmp'" EXIT HUP INT TERM
|
|
Packit |
209faa |
generate "$(dirname "${input}")" ${vars} \
|
|
Packit |
209faa |
<"${input}" >"${output}.tmp" \
|
|
Packit |
209faa |
|| err "Failed to generate ${output}"
|
|
Packit |
209faa |
if validate_file "${output}.tmp"; then
|
|
Packit |
209faa |
:
|
|
Packit |
209faa |
else
|
|
Packit |
209faa |
err "Failed to generate ${output}; some patterns were left unreplaced"
|
|
Packit |
209faa |
fi
|
|
Packit |
209faa |
mv "${output}.tmp" "${output}"
|
|
Packit |
209faa |
}
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
main "${@}"
|