summaryrefslogtreecommitdiff
path: root/contrib/card.in
blob: 2561d463b65eca3f921ab748580a789f65aec178 (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
#! /bin/sh
# -*- ksh -*-
# card --- smartly produce a printed reference card of a program

# Copyright (c) 1998-2000 Akim Demaille, Miguel Santana

# This program 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, or (at your option)
# any later version.
#
# This program 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, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Author: Akim Demaille <Akim.Demaille@freefriends.org>

# Name by which this script was invoked.
program=`echo "$0" | sed -e 's/[^\/]*\///g'`
card_version='1.3'

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
# Some of these, like A2PS, may be inherited from the environment.
a2ps=${A2PS-a2ps}
a2ps_options=-Ecard
commands=
debug=
form_feed=
help="Try \`$program --help' for more information."
LC_ALL="${LC_ALL-C}" export LC_ALL
print_form_feeds=:
RM="/bin/rm -rf"
tmp_dir=`mktemp -d -t card.XXXXXX` || { echo "$program: Cannot create temporary dir!" >&2 ; exit 1; }
tmp_file=$tmp_dir/card
success=false
verbose=:
version_short="card $card_version (@GNU_PACKAGE@ @VERSION@)"

usage="Usage: $program [OPTION]... PROGRAM...
Print a reference card of the PROGRAMs thanks to their inline help.

Options:
 -h, --help           display this help and exit
 -v, --version        display version information and exit
 -o, --output=FILE    save the output in FILE
 -l, --language=LL    print the help in the language LL (default: English)
     --command=CMD    perform pretty-printing on the output of CMD
                      (e.g. --command='netscape -help')

Options for a2ps are given after \`--', for instance

    $ card -lfr a2ps -- -Pdisplay -4

News, updates and documentation: http://www.inf.enst.fr/~demaille/a2ps/
Report bugs to <bug-a2ps@gnu.org>."

version="$version_short
Written by Akim Demaille.

Copyright (c) 1997-99 Akim Demaille, Miguel Santana
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."

# List of the possible ways to get the on line help.
# -flags is used with Solaris' CC.
possible_options="--help -h -help -\? -flags"

# Parse command line arguments.
option_without_arguments='vhsqDf'

# As a first step, fetch all the options meant for a2ps, i.e.
# everything after `--'.
jam="$$card$$"
set dummy "${1+"$@"}" "$jam"
shift
while test "x$1" != "x$jam" && test "x$1" != "x--"; do
  set dummy "${1+"$@"}" "$1"
  shift
  shift
done

# If $[1] is `--', then from here to the JAM are a2ps arguments.
if test "x$1" = x--; then
  shift
  while test "x$1" != "x$jam"
  do
    a2ps_options="$a2ps_options $1"
    shift
  done
fi

# The current $1 is JAM.  Pop up the JAM, pop it up, pop it out.
shift

# Proceed to card's options parsing.
# Push a token among the arguments that will be used to notice when
# we ended options/arguments parsing.
arg_sep="$$--$$"
set dummy ${1+"$@"} "$arg_sep"
shift
while test "x$1" != "x$arg_sep"; do

  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set dummy "$opt" "$val" ${1+"$@"}
      shift
      ;;

    -[$option_without_arguments]?*)
      # Prefix $1 with x to avoid running `echo -na' for instance.
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      rest=`echo "x$1" | sed -e 's/x-.\(.*\)/-\1/'`
      shift
      set dummy "$opt" "$rest" ${1+"$@"}
      shift
      ;;

    # This case needs to be protected so that the case `-??*' does
    # not split long options without arguments
    --*)
      ;;

    # This is an option with argument.  Split apart and put back on argv.
    -??*)
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      arg=`echo "x$1" | sed -e 's/x-.\(.*\)/\1/'`
      shift
      set dummy "$opt" "$arg" ${1+"$@"}
      shift
      ;;
  esac

  # Now, handle the options.  $1 is the option *only*.  If it has an
  # argument, it is now necessarily in $2 etc.  Remember to shift
  # when fetching an argument.
  case "$1" in
    -v | --v*) echo "$version"; exit 0;;
    -h | --h*) echo "$usage"; exit 0;;
    -s|-q|--q*|--s*) verbose=: ;;
    -D | --debug) debug=: ;;
    -o|--output) shift
                 a2ps_options="$a2ps_options --output=$1" ;;
    --lan*|-l) shift; LC_ALL=$1                     ;;
    --com*|-c) shift; commands="$commands
$optarg" ;;
    --no-*|-f) print_form_feeds=                    ;;

    --) # What remains are not options.
      shift
      while test "x$1" != "x$arg_sep"; do
        set dummy ${1+"$@"} "$1"
        shift
	shift
      done
      break;;

    -*)
      echo "$program: Unknown or ambiguous option \`$1'." >&2
      echo "$program: Try \`--help' for more information." >&2
      exit 1;;
    *) set dummy ${1+"$@"} "$1"
       shift
       ;;
   esac
   shift
done
# Pop the token
shift


# ARG now contains both options for a2ps and the true arguments.

if test $# = 0 && test "X$commands" = X; then
  exec 1>&2
  echo "$program: no program given"
  echo "$help"
  exit 1
fi

# Create a tmp dir and be ready to clean up
trap "$RM $tmp_dir" 0 1 2 15

case $LC_ALL in
  fr) footer="Engendr� par $version_short" ;;
  *)  footer="Generated by $version_short" ;;
esac

# Set -x now if debugging
test -n "$debug" && set -x

# The files to process are in "$@"
for file
do
  success=false
  filename=`echo "$file" | sed -e 's/[^\/]*\///g'`
  $verbose "Working on \`$filename'"
  case $LC_ALL in
    fr) title="Carte de r�f�rence pour $filename" ;;
    *)  title="Reference card of $filename"       ;;
  esac

  # Try to find the help message
  for opt in $possible_options
  do
    $verbose "Trying \`$file $opt'"
    ($file $opt >$tmp_dir/foo 2>&1) >/dev/null 2>&1 && success=: && break
    # Netscape for example is stupid enough to `exit 255' when properly fed
    # with -help, as recommanded by -help itself...  If there are really
    # many lines, consider it is still a success.
    if test "$success" = false; then
      if test `wc -l <$tmp_dir/foo` -gt 10; then
        success=: && break
      fi
    fi
  done

  # If the help message has been found, process it with a2ps
  if $success; then
    $verbose "Success"
    if test -n "$form_feed"; then
       echo ""  >>$tmp_file
    fi
    cat <<EOF >>$tmp_file
				card_label($title)
card_title($title)
EOF
    cat $tmp_dir/foo >>$tmp_file
    # Be ready to insert a page break before next argument-program
    form_feed=$print_form_feeds
  else
    echo "$program: could not find help message for $file"
    exit 1
  fi
done

SAVED_IFS="$IFS"
IFS="
"
for command in $commands
do
  IFS="$SAVED_IFS"
  success=false
  case $LC_ALL in
    fr) title="R�sultat de \`$command'" ;;
    *)  title="Result of \`$command'"   ;;
  esac

  (eval $command >$tmp_dir/foo 2>&1) >/dev/null 2>&1 && success=:
  # If the help message has been found, process it with a2ps
  if $success; then
    $verbose "Success"
    if test -n "$form_feed"; then
       echo ""  >>$tmp_file
    fi
    cat <<EOF >>$tmp_file
				card_label($title)
card_title($title)
EOF
    cat $tmp_dir/foo >>$tmp_file
    # Be ready to insert a page break before next argument-program
    form_feed=$print_form_feeds
  else
    echo "$program: command \"$command\" failed"
    exit 1
  fi
done
IFS="$SAVED_IFS"

# All the programs have been treated.  Call a2ps on the produced file
$a2ps $a2ps_options --footer="$footer" $tmp_file || exit 1

exit 0