ceb4afaf603b489560d904d082f2f2c5db784c97
[platform/upstream/bash.git] / support / bashbug.sh
1 #!/bin/sh -
2 #
3 # bashbug - create a bug report and mail it to the bug address
4 #
5 # The bug address depends on the release status of the shell.  Versions
6 # with status `devel', `alpha', `beta', or `rc' mail bug reports to
7 # chet@po.cwru.edu and, optionally, to bash-testers@po.cwru.edu.
8 # Other versions send mail to bug-bash@gnu.org.
9 #
10 # Copyright (C) 1996-2002 Free Software Foundation, Inc.
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2, or (at your option)
15 # any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25
26 #
27 # configuration section:
28 #       these variables are filled in by the make target in Makefile
29 #
30 MACHINE="!MACHINE!"
31 OS="!OS!"
32 CC="!CC!"
33 CFLAGS="!CFLAGS!"
34 RELEASE="!RELEASE!"
35 PATCHLEVEL="!PATCHLEVEL!"
36 RELSTATUS="!RELSTATUS!"
37 MACHTYPE="!MACHTYPE!"
38
39 PATH=/bin:/usr/bin:/usr/local/bin:$PATH
40 export PATH
41
42 # If the OS supplies a program to make temp files with semi-random names,
43 # use it.
44 : ${TMPDIR:=/tmp}
45 rm_tmp1=false
46 rm_tmp2=false
47
48 # if we don't have mktemp or tempfile, we don't want to see error messages
49 # like `mktemp: not found', so temporarily redirect stderr using {...} while
50 # trying to run them.  this may fail using old versions of the bourne shell
51 # that run {...} blocks with redirections in subshells; in that case we're
52 # no worse off than previous versions
53
54 { TEMPFILE1=`mktemp "$TMPDIR/bbug.XXXXXX" 2>/dev/null` ; } 2>/dev/null
55 if [ -z "$TEMPFILE1" ]; then
56         { TEMPFILE1=`tempfile --prefix bbug --mode 600 2>/dev/null`; } 2>/dev/null
57 fi
58 if [ -z "$TEMPFILE1" ]; then
59         TEMPFILE1=$TMPDIR/bbug.$$
60         rm_tmp1=true
61 fi
62 { TEMPFILE2=`mktemp "$TMPDIR/bbug.XXXXXX" 2>/dev/null`; } 2>/dev/null
63 if [ -z "$TEMPFILE2" ]; then
64         { TEMPFILE2=`tempfile --prefix bbug --mode 600 2>/dev/null`; } 2>/dev/null
65 fi
66 if [ -z "$TEMPFILE2" ]; then
67         TEMPFILE2="$TMPDIR/bbug.$$.x"
68         rm_tmp2=true
69 fi
70
71 USAGE="Usage: $0 [--help] [--version] [bug-report-email-address]"
72 VERSTR="GNU bashbug, version ${RELEASE}.${PATCHLEVEL}-${RELSTATUS}"
73
74 do_help= do_version=
75
76 while [ $# -gt 0 ]; do
77         case "$1" in
78         --help)         shift ; do_help=y ;;
79         --version)      shift ; do_version=y ;;
80         --)             shift ; break ;;
81         -*)             echo "bashbug: ${1}: invalid option" >&2
82                         echo "$USAGE" >& 2
83                         exit 2 ;;
84         *)              break ;;
85         esac
86 done
87
88 if [ -n "$do_version" ]; then
89         echo "${VERSTR}"
90         exit 0
91 fi
92
93 if [ -n "$do_help" ]; then
94         echo "${VERSTR}"
95         echo "${USAGE}"
96         echo
97         cat << HERE_EOF
98 Bashbug is used to send mail to the Bash maintainers
99 for when Bash doesn't behave like you'd like, or expect.
100
101 Bashbug will start up your editor (as defined by the shell's
102 EDITOR environment variable) with a preformatted bug report
103 template for you to fill in. The report will be mailed to the
104 bash maintainers by default. See the manual for details.
105
106 If you invoke bashbug by accident, just quit your editor without
107 saving any changes to the template, and no bug report will be sent.
108 HERE_EOF
109         exit 0
110 fi
111
112 # Figure out how to echo a string without a trailing newline
113 N=`echo 'hi there\c'`
114 case "$N" in
115 *c)     n=-n c= ;;
116 *)      n= c='\c' ;;
117 esac
118
119 BASHTESTERS="bash-testers@po.cwru.edu"
120
121 case "$RELSTATUS" in
122 alpha*|beta*|devel*|rc*)        BUGBASH=chet@po.cwru.edu ;;
123 *)                              BUGBASH=bug-bash@gnu.org ;;
124 esac
125
126 case "$RELSTATUS" in
127 alpha*|beta*|devel*|rc*)
128                 echo "$0: This is a testing release.  Would you like your bug report"
129                 echo "$0: to be sent to the bash-testers mailing list?"
130                 echo $n "$0: Send to bash-testers? $c"
131                 read ans
132                 case "$ans" in
133                 y*|Y*)  BUGBASH="${BUGBASH},${BASHTESTERS}" ;;
134                 esac ;;
135 esac
136
137 BUGADDR="${1-$BUGBASH}"
138
139 if [ -z "$DEFEDITOR" ] && [ -z "$EDITOR" ]; then
140         if [ -x /usr/bin/editor ]; then
141                 DEFEDITOR=editor
142         elif [ -x /usr/local/bin/ce ]; then
143                 DEFEDITOR=ce
144         elif [ -x /usr/local/bin/emacs ]; then
145                 DEFEDITOR=emacs
146         elif [ -x /usr/contrib/bin/emacs ]; then
147                 DEFEDITOR=emacs
148         elif [ -x /usr/bin/emacs ]; then
149                 DEFEDITOR=emacs
150         elif [ -x /usr/bin/xemacs ]; then
151                 DEFEDITOR=xemacs
152         elif [ -x /usr/contrib/bin/jove ]; then
153                 DEFEDITOR=jove
154         elif [ -x /usr/local/bin/jove ]; then
155                 DEFEDITOR=jove
156         elif [ -x /usr/bin/vi ]; then
157                 DEFEDITOR=vi
158         else
159                 echo "$0: No default editor found: attempting to use vi" >&2
160                 DEFEDITOR=vi
161         fi
162 fi
163
164
165 : ${EDITOR=$DEFEDITOR}
166
167 : ${USER=${LOGNAME-`whoami`}}
168
169 trap 'rm -f "$TEMPFILE1" "$TEMPFILE2"; exit 1' 1 2 3 13 15
170 trap 'rm -f "$TEMPFILE1" "$TEMPFILE2"' 0
171
172 UN=
173 if (uname) >/dev/null 2>&1; then
174         UN=`uname -a`
175 fi
176
177 if [ -f /usr/lib/sendmail ] ; then
178         RMAIL="/usr/lib/sendmail"
179         SMARGS="-i -t"
180 elif [ -f /usr/sbin/sendmail ] ; then
181         RMAIL="/usr/sbin/sendmail"
182         SMARGS="-i -t"
183 else
184         RMAIL=rmail
185         SMARGS="$BUGADDR"
186 fi
187
188 INITIAL_SUBJECT='[50 character or so descriptive subject here (for reference)]'
189
190 # this is raceable unless (hopefully) we used mktemp(1) or tempfile(1)
191 $rm_tmp1 && rm -f "$TEMPFILE1"
192
193 cat > "$TEMPFILE1" <<EOF
194 From: ${USER}
195 To: ${BUGADDR}
196 Subject: ${INITIAL_SUBJECT}
197
198 Configuration Information [Automatically generated, do not change]:
199 Machine: $MACHINE
200 OS: $OS
201 Compiler: $CC
202 Compilation CFLAGS: $CFLAGS
203 uname output: $UN
204 Machine Type: $MACHTYPE
205
206 Bash Version: $RELEASE
207 Patch Level: $PATCHLEVEL
208 Release Status: $RELSTATUS
209
210 Description:
211         [Detailed description of the problem, suggestion, or complaint.]
212
213 Repeat-By:
214         [Describe the sequence of events that causes the problem
215         to occur.]
216
217 Fix:
218         [Description of how to fix the problem.  If you don't know a
219         fix for the problem, don't include this section.]
220 EOF
221
222 # this is still raceable unless (hopefully) we used mktemp(1) or tempfile(1)
223 $rm_tmp2 && rm -f "$TEMPFILE2"
224
225 cp "$TEMPFILE1" "$TEMPFILE2"
226 chmod u+w "$TEMPFILE1"
227
228 trap '' 2               # ignore interrupts while in editor
229
230 edstat=1
231 while [ $edstat -ne 0 ]; do
232         $EDITOR "$TEMPFILE1"
233         edstat=$?
234
235         if [ $edstat -ne 0 ]; then
236                 echo "$0: editor \`$EDITOR' exited with nonzero status."
237                 echo "$0: Perhaps it was interrupted."
238                 echo "$0: Type \`y' to give up, and lose your bug report;"
239                 echo "$0: type \`n' to re-enter the editor."
240                 echo $n "$0: Do you want to give up? $c"
241
242                 read ans
243                 case "$ans" in
244                 [Yy]*) exit 1 ;;
245                 esac
246
247                 continue
248         fi
249
250         # find the subject from the temp file and see if it's been changed
251         CURR_SUB=`grep '^Subject: ' "$TEMPFILE1" | sed 's|^Subject:[    ]*||' | sed 1q`
252
253         case "$CURR_SUB" in
254         "${INITIAL_SUBJECT}")
255                 echo
256                 echo "$0: You have not changed the subject from the default."
257                 echo "$0: Please use a more descriptive subject header."
258                 echo "$0: Type \`y' to give up, and lose your bug report;"
259                 echo "$0: type \`n' to re-enter the editor."
260                 echo $n "$0: Do you want to give up? $c"
261
262                 read ans
263                 case "$ans" in
264                 [Yy]*) exit 1 ;;
265                 esac
266
267                 echo "$0:  The editor will be restarted in five seconds."
268                 sleep 5
269                 edstat=1
270                 ;;
271         esac
272
273 done
274
275 trap 'rm -f "$TEMPFILE1" "$TEMPFILE2"; exit 1' 2        # restore trap on SIGINT
276
277 if cmp -s "$TEMPFILE1" "$TEMPFILE2"
278 then
279         echo "File not changed, no bug report submitted."
280         exit
281 fi
282
283 echo $n "Send bug report? [y/n] $c"
284 read ans
285 case "$ans" in
286 [Nn]*)  exit 0 ;;
287 esac
288
289 ${RMAIL} $SMARGS < "$TEMPFILE1" || {
290         cat "$TEMPFILE1" >> $HOME/dead.bashbug
291         echo "$0: mail failed: report saved in $HOME/dead.bashbug" >&2
292 }
293
294 exit 0