xref: /aosp_15_r20/external/bc/scripts/manpage.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#! /bin/sh
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Worker# Print the usage and exit with an error.
31*5a6e8488SAndroid Build Coastguard Workerusage() {
32*5a6e8488SAndroid Build Coastguard Worker	printf "usage: %s manpage\n" "$0" 1>&2
33*5a6e8488SAndroid Build Coastguard Worker	exit 1
34*5a6e8488SAndroid Build Coastguard Worker}
35*5a6e8488SAndroid Build Coastguard Worker
36*5a6e8488SAndroid Build Coastguard Worker# Generate a manpage and print it to a file.
37*5a6e8488SAndroid Build Coastguard Worker# @param md   The markdown manual to generate a manpage for.
38*5a6e8488SAndroid Build Coastguard Worker# @param out  The file to print the manpage to.
39*5a6e8488SAndroid Build Coastguard Workergen_manpage() {
40*5a6e8488SAndroid Build Coastguard Worker
41*5a6e8488SAndroid Build Coastguard Worker	_gen_manpage_md="$1"
42*5a6e8488SAndroid Build Coastguard Worker	shift
43*5a6e8488SAndroid Build Coastguard Worker
44*5a6e8488SAndroid Build Coastguard Worker	_gen_manpage_out="$1"
45*5a6e8488SAndroid Build Coastguard Worker	shift
46*5a6e8488SAndroid Build Coastguard Worker
47*5a6e8488SAndroid Build Coastguard Worker	cat "$manualsdir/header.txt" > "$_gen_manpage_out"
48*5a6e8488SAndroid Build Coastguard Worker	cat "$manualsdir/header_${manpage}.txt" >> "$_gen_manpage_out"
49*5a6e8488SAndroid Build Coastguard Worker
50*5a6e8488SAndroid Build Coastguard Worker	pandoc -f commonmark_x -t man "$_gen_manpage_md" >> "$_gen_manpage_out"
51*5a6e8488SAndroid Build Coastguard Worker}
52*5a6e8488SAndroid Build Coastguard Worker
53*5a6e8488SAndroid Build Coastguard Worker# Generate a manual from a template and print it to a file before generating
54*5a6e8488SAndroid Build Coastguard Worker# its manpage.
55*5a6e8488SAndroid Build Coastguard Worker# param args  The type of markdown manual to generate. This is a string that
56*5a6e8488SAndroid Build Coastguard Worker#             corresponds to build type (see the Build Type section of the
57*5a6e8488SAndroid Build Coastguard Worker#             manuals/build.md manual).
58*5a6e8488SAndroid Build Coastguard Workergen_manual() {
59*5a6e8488SAndroid Build Coastguard Worker
60*5a6e8488SAndroid Build Coastguard Worker	_gen_manual_args="$1"
61*5a6e8488SAndroid Build Coastguard Worker	shift
62*5a6e8488SAndroid Build Coastguard Worker
63*5a6e8488SAndroid Build Coastguard Worker	# Set up some local variables. $manualsdir and $manpage from from the
64*5a6e8488SAndroid Build Coastguard Worker	# variables outside the function.
65*5a6e8488SAndroid Build Coastguard Worker	_gen_manual_out="$manualsdir/$manpage/$_gen_manual_args.1"
66*5a6e8488SAndroid Build Coastguard Worker	_gen_manual_md="$manualsdir/$manpage/$_gen_manual_args.1.md"
67*5a6e8488SAndroid Build Coastguard Worker
68*5a6e8488SAndroid Build Coastguard Worker	# Remove the files that will be generated.
69*5a6e8488SAndroid Build Coastguard Worker	rm -rf "$_gen_manual_out" "$_gen_manual_md"
70*5a6e8488SAndroid Build Coastguard Worker
71*5a6e8488SAndroid Build Coastguard Worker	# Filter the text for the build type.
72*5a6e8488SAndroid Build Coastguard Worker	filter_text "$manualsdir/${manpage}.1.md.in" "$_gen_manual_md" "$_gen_manual_args"
73*5a6e8488SAndroid Build Coastguard Worker
74*5a6e8488SAndroid Build Coastguard Worker	# Generate the manpage.
75*5a6e8488SAndroid Build Coastguard Worker	gen_manpage "$_gen_manual_md" "$_gen_manual_out"
76*5a6e8488SAndroid Build Coastguard Worker}
77*5a6e8488SAndroid Build Coastguard Worker
78*5a6e8488SAndroid Build Coastguard Workerset -e
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Workerscript="$0"
81*5a6e8488SAndroid Build Coastguard Workerscriptdir=$(dirname "$script")
82*5a6e8488SAndroid Build Coastguard Workermanualsdir="$scriptdir/../manuals"
83*5a6e8488SAndroid Build Coastguard Worker
84*5a6e8488SAndroid Build Coastguard Worker. "$scriptdir/functions.sh"
85*5a6e8488SAndroid Build Coastguard Worker
86*5a6e8488SAndroid Build Coastguard Worker# Constants for use later. If the set of build types is changed, $ARGS must be
87*5a6e8488SAndroid Build Coastguard Worker# updated.
88*5a6e8488SAndroid Build Coastguard WorkerARGS="A E H N EH EN HN EHN"
89*5a6e8488SAndroid Build Coastguard Worker
90*5a6e8488SAndroid Build Coastguard Worker# Process command-line arguments.
91*5a6e8488SAndroid Build Coastguard Workertest "$#" -eq 1 || usage
92*5a6e8488SAndroid Build Coastguard Worker
93*5a6e8488SAndroid Build Coastguard Workermanpage="$1"
94*5a6e8488SAndroid Build Coastguard Workershift
95*5a6e8488SAndroid Build Coastguard Worker
96*5a6e8488SAndroid Build Coastguard Workerif [ "$manpage" != "bcl" ]; then
97*5a6e8488SAndroid Build Coastguard Worker
98*5a6e8488SAndroid Build Coastguard Worker	# Generate a manual and manpage for each build type.
99*5a6e8488SAndroid Build Coastguard Worker	for a in $ARGS; do
100*5a6e8488SAndroid Build Coastguard Worker		gen_manual "$a"
101*5a6e8488SAndroid Build Coastguard Worker	done
102*5a6e8488SAndroid Build Coastguard Worker
103*5a6e8488SAndroid Build Coastguard Workerelse
104*5a6e8488SAndroid Build Coastguard Worker	# For bcl, just generate the manpage.
105*5a6e8488SAndroid Build Coastguard Worker	gen_manpage "$manualsdir/${manpage}.3.md" "$manualsdir/${manpage}.3"
106*5a6e8488SAndroid Build Coastguard Workerfi
107