summaryrefslogtreecommitdiff
path: root/contrib/groffer/shell/roff2.sh
blob: ac226b48ba923a77e60ce214d38d9604dcf00d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#! /bin/sh

# roff2* - transform roff files into other formats

# Source file position: <groff-source>/contrib/groffer/shell/roff2.sh
# Installed position: <prefix>/bin/roff2*

# Copyright (C) 2006, 2009 Free Software Foundation, Inc.
# Written by Bernd Warken

# Last update: 5 Jan 2009

# This file is part of `groffer', which is part of `groff'.

# `groff' is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# `groff' is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

########################################################################


##############
# echo1 (<text>*)
#
# Output to stdout with final line break.
#
# Arguments : arbitrary text including `-'.
#
echo1()
{
  cat <<EOF
$@
EOF
} # echo1()


##############
# echo2 (<text>*)
#
# Output to stderr with final line break.
#
# Arguments : arbitrary text including `-'.
#
echo2()
{
  cat >&2 <<EOF
$@
EOF
} # echo2()


error_no_groffer='no';
error_no_groffer() {
  if test _"$error_no_groffer" = _yes
  then
    return;
  fi;
  error_no_groffer='yes';
  echo2 "$name: groffer is not available.";
}


error_no_options='no';
error_no_options() {
  if test _"$error_no_options" = _yes
  then
    return;
  fi;
  error_no_groffer='yes';
  echo2 "$name: groffer options are not allowed.";
}


usage_with_groffer() {
  cat <<EOF
usage: $Name [option]... [--] [filespec]...

-h | --help     print usage information
-v | --version  print version information

All other options are arbitrary options of "groffer"; the options
override the behavior of this program.

"filespec"s are the same as in "groffer": either the names of
existing, readable files or "-" for standard input or a search pattern
for man pages.  No "filespec" assumes standard input automatically.
EOF
}

usage_without_groffer() {
  cat <<EOF
usage: $Name [option]... [--] [filespec]...

-h | --help     print usage information
-v | --version  print version information

No other options are allowed because "groffer" is not available.

The only "filespec"s allowed are the names of existing, readable files
or "-" for standard input.  No "filespec" assumes standard input
automatically.
EOF
}


where_is_prog() {
  for p in `echo $PATH|sed "s/:/ /g"`
  do
    f="${p}/$1";
    if test -f "$f" && test -x "$f"
    then
      echo1 "$f";
      return;
    fi;
  done;
}


########################################################################

export NULL_DEV;
if test -c /dev/null
then
  NULL_DEV='/dev/null';
else
  NULL_DEV='NUL';
fi;

name="$(echo1 "$0" | sed 's|^.*//*\([^/]*\)$|\1|')";

case "$name" in
roff2[a-z]*)
  mode="$(echo1 "$name" | sed 's/^roff2//')";
  ;;
*)
  echo2 "wrong program name: $name";
  exit 1;
  ;;
esac;

groff_version="$(groff --version 2>$NULL_DEV)";
if test $? -gt 0
then
  echo2 "$name error: groff does not work.";
  exit 1;
fi;
groffer_version="$(groffer --version 2>$NULL_DEV)";
if test $? -gt 0
then
  has_groffer='no';
else
  has_groffer='yes';
fi;

if test _"${has_groffer}" = _yes
then
  for i
  do
    case $i in
    -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
      echo1 "$name in $groffer_version";
      exit 0;
      ;;
    -h|--h|--he|--hel|--help)
      usage_with_groffer;
      exit 0;
      ;;
    esac;
  done;
  groffer --to-stdout --$mode "$@";
else				# not has_groffer
  reset=no;
  double_minus=no;
  for i
  do
    if test _"${reset}" = _no
    then
      set --;
      reset=yes;
    fi;
    if test _"${double_minus}" = _yes
    then
      set -- "$@" "$i";
      continue;
    fi;
    case "$i" in
    --)
      double_minus=yes;
      continue;
      ;;
    -)
      set -- "$@" '-';
      continue;
      ;;
    -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
      echo1 "$name in $groff_version";
      exit 0;
      ;;
    -h|--h|--he|--hel|--help)
      usage_without_groffer;
      exit 0;
      ;;
    -*)
      error_no_groffer;
      error_no_options;
      ;;
    *)
      if test -f "$i" && test -r "$i"
      then
        set -- "$@" "$i";
      else
        error_no_groffer;
        echo2 "$i is not an existing, readable file.";
      fi;
      continue;
      ;;
    esac;
  done;				# for i

  if test $# -eq 0
  then
    set -- '-';
  fi;
  has_stdin=no;
  for i
  do
    case "$i" in
    -)
      has_stdin=yes;
      break;
      ;;
    esac;
  done;

  if test _$has_stdin = _yes
  then
    umask 0077;
    tempdir='';
    for d in "${GROFF_TMPDIR}" "${TMPDIR}" "${TMP}" "${TEMP}" \
           "${TEMPDIR}" "${HOME}"'/tmp' '/tmp' "${HOME}" '.'
    do
      if test _"$d" = _ || ! test -d "$d" || ! test -w "$d"
      then
        continue;
      fi;
      case "$d" in
      */)
        tempdir="$d";
        ;;
      *)
        tempdir="$d"'/';
        ;;
      esac;
    done;
    if test _$tempdir = _
    then
      echo2 "${name}: could not find a temporary directory."
      exit 1;
    fi;
    stdin=${tempdir}${name}_$$;
    if test -e "$stdin"
    then
      rm -f "$stdin";
      n=0;
      f="${stdin}_$n";
      while test -e "$f"
      do
	rm -f "$f";
        if ! test -e "$f"
        then
          break;
        fi;
        n="$(expr $n + 1)";
        f="${stdin}_$n";
      done;
      stdin="$f";
    fi;
    reset=no;
    for i
    do
      if test _"${reset}" = _no
      then
        set --;
        reset=yes;
      fi;
      case "$i" in
      -)
        set -- "$@" "$stdin";
        ;;
      *)
        set -- "$@" "$i";
        ;;
      esac;
    done;
    cat>"$stdin";
  fi;				# if has_stdin

  case "$mode" in
  x)
    groff_options='-TX75-12 -Z';
    ;;
  text)
    groff_options='-Tlatin1';
    ;;
  pdf)
    ps2pdf="$(where_is_prog ps2pdf)";
    if test _"$ps2pdf" = _
    then
      ps2pdf="$(where_is_prog gs)";
      if test _"$ps2pdf" = _
      then
        echo2 "$name: cannot transform to pdf format.";
        exit 1;
      fi;
      ps2pdf="$ps2pdf -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite '-sOutputFile=- -c save pop -f -";
    else
      ps2pdf="$ps2pdf -";
    fi;
    grog="$(grog -Tps "$@")";
    eval $grog | ${ps2pdf};
    exit $?;
    ;;
  *)
    groff_options="-T$mode";
    ;;
  esac;
  grog="$(grog $groff_options "$@")";
  eval $grog;
  exit $?;
fi;				# not has_groffer