Debian Codegraph @

build-aux/x-to-1.in

raw
1 #! /bin/sh
2 #
3 # Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 #
18
19 # This program creates a program's manual from the .x skeleton and its --help
20 # output.
21 #
22 # Usage: x-to-1 [OPTIONS] PERL HELP2MAN [HELP2MAN-OPTIONS] EXECUTABLE PROGRAM.x PROGRAM.1
23 # where
24 #
25 # PERL The file name of the perl program.
26 # HELP2MAN Either the file name of the help2man perl script, or a complete
27 # command such as "$PERL -w -- help2man".
28 # HELP2MAN-OPTIONS Options to pass to the help2man perl script.
29 # EXECUTABLE The file name of the program to invoke with --help.
30 # PROGRAM The name of the program.
31 # PROGRAM.x The .x skeleton is a file containing manual page text that is
32 # not part of the --help output. It is passed to help2man via
33 # its --include option. Its format is described in the help2man
34 # documentation.
35 # PROGRAM.1 The output file, a manual page in mandoc format.
36 #
37 # Options:
38 # --update Don't overwrite the output if nothing would change.
39 #
40 # Configure-time prerequisites:
41 #
42 # PERL Can be set through
43 # ac_aux_dir_abs=`cd $ac_aux_dir && pwd`
44 # AC_PATH_PROG([PERL], [perl], [$ac_aux_dir_abs/missing perl])
45 # or, alternatively, by the Gnulib 'perl' module.
46 #
47 # CROSS_COMPILING Can be set through
48 # CROSS_COMPILING=$cross_compiling
49 # AC_SUBST([CROSS_COMPILING])
50 # The variable 'cross_compiling' is set by AC_PROG_CC.
51
52 update=
53 while true; do
54 case "$1" in
55 --update) update=yes; shift;;
56 *) break;;
57 esac
58 done
59
60 if test $# -lt 5; then
61 echo "Usage: x-to-1 [OPTIONS] PERL HELP2MAN [HELP2MAN-OPTIONS] executable program.x program.1" 1>&2
62 exit 1
63 fi
64
65 # In order to construct a command that invokes HELP2MAN [HELP2MAN-OPTIONS] ...
66 # we need 'eval'.
67 command_for_print=
68 command_for_eval=
69 # Protecting special characters, hiding them from 'eval':
70 # Double each backslash.
71 sed_protect_1='s/\\/\\\\/g'
72 # Escape each dollar, backquote, double-quote.
73 sed_protect_2a='s/\$/\\$/g'
74 sed_protect_2b='s/`/\\`/g'
75 sed_protect_2c='s/"/\\"/g'
76 # Add double-quotes at the beginning and end of the word.
77 sed_protect_3a='1s/^/"/'
78 sed_protect_3b='$s/$/"/'
79 func_add_word_to_command ()
80 {
81 command_for_print="${command_for_print:+$command_for_print }$1"
82 word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
83 command_for_eval="${command_for_eval:+$command_for_eval }$word_protected"
84 }
85
86 PERL="$1"; shift
87 HELP2MAN="$1"; shift
88
89 # Accommodate both possible forms of the HELP2MAN argument.
90 case "$HELP2MAN" in
91 "$PERL "*) ;;
92 *) HELP2MAN="$PERL $HELP2MAN" ;;
93 esac
94
95 # Do word-splitting of $HELP2MAN, but carefully.
96 for word in $HELP2MAN; do
97 func_add_word_to_command "$word"
98 done
99
100 # Now grok the HELP2MAN-OPTIONS.
101 while test $# -gt 3; do
102 arg="$1"; shift
103 func_add_word_to_command "$arg"
104 done
105
106 # Grok the final arguments.
107 executable="$1"
108 aux="$2"
109 output="$3"
110
111 progname=`basename $aux .x`
112 # configure determined whether perl exists.
113 case "$PERL" in
114 *"/missing perl")
115 perlok=no
116 ;;
117 *)
118 # Determine whether all the perl modules that help2man needs are installed.
119 if $HELP2MAN --help >/dev/null 2>/dev/null; then
120 perlok=yes
121 else
122 perlok=no
123 fi
124 ;;
125 esac
126 if test @CROSS_COMPILING@ = no && test -f $executable && test $perlok = yes; then
127 echo "Updating man page $output"
128 func_add_word_to_command "--include=$aux"
129 func_add_word_to_command "$executable"
130 echo "$command_for_print > $output"
131 rm -f t-$progname.1
132 eval "$command_for_eval" > t-$progname.1 || exit 1
133 if test -n "$update"; then
134 # In --update mode, don't overwrite the output if nothing would change.
135 if cmp t-$progname.1 $output >/dev/null 2>&1; then
136 rm -f t-$progname.1
137 else
138 mv t-$progname.1 $output
139 fi
140 else
141 mv t-$progname.1 $output
142 fi
143 else
144 echo "WARNING: The man page $output cannot be updated yet."
145 fi