Create files safely when mktemp is not available.
[kopensolaris-gnu/glibc.git] / glibcbug.in
1 #! /bin/sh
2 #
3 # glibcbug - create a bug report and mail it to the bug address.
4 #
5 # configuration section:
6 #       these variables are filled in by configure
7 #
8 VERSION="@VERSION@"
9 BUGGLIBC="bugs@gnu.org"
10 ADDONS="@subdirs@"
11 HOST="@host@"
12 CC='@CC@'
13 CFLAGS="@CFLAGS@"
14 VERSIONING="@VERSIONING@"
15 BUILD_STATIC="@static@"
16 BUILD_SHARED="@shared@"
17 BUILD_PIC_DEFAULT="@pic_default@"
18 BUILD_PROFILE="@profile@"
19 BUILD_OMITFP="@omitfp@"
20 BUILD_BOUNDED="@bounded@"
21 BUILD_STATIC_NSS="@static_nss@"
22 STDIO="@stdio@"
23
24 PATH=/bin:/usr/bin:/usr/local/bin:$PATH
25 export PATH
26
27 TEMP=`mktemp -q /tmp/glibcbugXXXXXX 2>/dev/null`
28 if test $? -ne 0; then
29   TEMP=/tmp/glibcbug.$$
30   echo > $TEMP
31   chmod 600 $TEMP
32 fi
33 TEMPx=`mktemp -q /tmp/glibcbugXXXXXX 2>/dev/null`
34 if test $? -ne 0; then
35   TEMPx=/tmp/glibcbug.$$.x
36   echo > $TEMPx
37   chmod 600 $TEMPx
38 fi
39
40 BUGADDR=${1-$BUGGLIBC}
41 ENVIRONMENT=`uname -a`
42
43 : ${EDITOR=emacs}
44
45 : ${USER=${LOGNAME-`whoami`}}
46
47 trap 'rm -f $TEMP $TEMPx; exit 1' 1 2 3 13 15
48 trap 'rm -f $TEMP $TEMPx' 0
49
50
51 # How to read the passwd database.
52 PASSWD="cat /etc/passwd"
53
54 if [ -f /usr/lib/sendmail ] ; then
55         MAIL_AGENT="/usr/lib/sendmail -oi -t"
56 elif [ -f /usr/sbin/sendmail ] ; then
57         MAIL_AGENT="/usr/sbin/sendmail -oi -t"
58 else
59         MAIL_AGENT=rmail
60 fi
61
62 # Figure out how to echo a string without a trailing newline
63 N=`echo 'hi there\c'`
64 case "$N" in
65 *c)     ECHON1='echo -n' ECHON2= ;;
66 *)      ECHON1=echo ECHON2='\c' ;;
67 esac
68
69 # Find out the name of the originator of this PR.
70 if [ -n "$NAME" ]; then
71   ORIGINATOR="$NAME"
72 elif [ -f $HOME/.fullname ]; then
73   ORIGINATOR="`sed -e '1q' $HOME/.fullname`"
74 else
75   # Must use temp file due to incompatibilities in quoting behavior
76   # and to protect shell metacharacters in the expansion of $LOGNAME
77   $PASSWD | grep "^$LOGNAME:" | awk -F: '{print $5}' | sed -e 's/,.*//' > $TEMP
78   ORIGINATOR="`cat $TEMP`"
79   rm -f $TEMP
80 fi
81
82 if [ -n "$ORGANIZATION" ]; then
83   if [ -f "$ORGANIZATION" ]; then
84     ORGANIZATION="`cat $ORGANIZATION`"
85   fi
86 else
87   if [ -f $HOME/.organization ]; then
88     ORGANIZATION="`cat $HOME/.organization`"
89   elif [ -f $HOME/.signature ]; then
90     ORGANIZATION=`sed -e "s/^/  /" $HOME/.signature; echo ">"`
91   fi
92 fi
93
94 # If they don't have a preferred editor set, then use
95 if [ -z "$VISUAL" ]; then
96   if [ -z "$EDITOR" ]; then
97     EDIT=vi
98   else
99     EDIT="$EDITOR"
100   fi
101 else
102   EDIT="$VISUAL"
103 fi
104
105 # Find out some information.
106 SYSTEM=`( [ -f /bin/uname ] && /bin/uname -a ) || \
107         ( [ -f /usr/bin/uname ] && /usr/bin/uname -a ) || echo ""`
108 ARCH=`[ -f /bin/arch ] && /bin/arch`
109 MACHINE=`[ -f /bin/machine ] && /bin/machine`
110
111 ORGANIZATION_C='<organization of PR author (multiple lines)>'
112 SYNOPSIS_C='<synopsis of the problem (one line)>'
113 SEVERITY_C='<[ non-critical | serious | critical ] (one line)>'
114 PRIORITY_C='<[ low | medium | high ] (one line)>'
115 CLASS_C='<[ sw-bug | doc-bug | change-request | support ] (one line)>'
116 RELEASE_C='<release number or tag (one line)>'
117 ENVIRONMENT_C='<machine, os, target, libraries (multiple lines)>'
118 DESCRIPTION_C='<precise description of the problem (multiple lines)>'
119 HOW_TO_REPEAT_C='<code/input/activities to reproduce the problem (multiple lines)>'
120 FIX_C='<how to correct or work around the problem, if known (multiple lines)>'
121
122
123 cat > $TEMP <<EOF
124 SEND-PR: -*- send-pr -*-
125 SEND-PR: Lines starting with \`SEND-PR' will be removed automatically, as
126 SEND-PR: will all comments (text enclosed in \`<' and \`>').
127 SEND-PR:
128 From: ${USER}
129 To: ${BUGADDR}
130 Subject: [50 character or so descriptive subject here (for reference)]
131
132 >Submitter-Id:  net
133 >Originator:    ${ORIGINATOR}
134 >Organization:
135 ${ORGANIZATION- $ORGANIZATION_C}
136 >Confidential:  no
137 >Synopsis:      $SYNOPSIS_C
138 >Severity:      $SEVERITY_C
139 >Priority:      $PRIORITY_C
140 >Category:      libc
141 >Class:         $CLASS_C
142 >Release:       libc-${VERSION}
143 >Environment:
144         $ENVIRONMENT_C
145 `[ -n "$HOST" ] && echo Host type: $HOST`
146 `[ -n "$SYSTEM" ] && echo System: $SYSTEM`
147 `[ -n "$ARCH" ] && echo Architecture: $ARCH`
148 `[ -n "$MACHINE" ] && echo Machine: $MACHINE`
149 `[ -n "$ADDONS" ] && echo Addons: $ADDONS`
150 `[ -n "$CFLAGS" ] && echo Build CFLAGS: $CFLAGS`
151 `[ -n "$CC" ] && echo Build CC: $CC`
152 `[ -n "$VERSIONING" ] && echo Versioning: $VERSIONING`
153 `[ -n "$BUILD_STATIC" ] && echo Build static: $BUILD_STATIC`
154 `[ -n "$BUILD_SHARED" ] && echo Build shared: $BUILD_SHARED`
155 `[ -n "$BUILD_PIC_DEFAULT" ] && echo Build pic-default: $BUILD_PIC_DEFAULT`
156 `[ -n "$BUILD_PROFILE" ] && echo Build profile: $BUILD_PROFILE`
157 `[ -n "$BUILD_OMITFP" ] && echo Build omitfp: $BUILD_OMITFP`
158 `[ -n "$BUILD_BOUNDED" ] && echo Build bounded: $BUILD_BOUNDED`
159 `[ -n "$BUILD_STATIC_NSS" ] && echo Build static-nss: $BUILD_STATIC_NSS`
160 `[ -n "$STDIO" ] && echo Stdio: $STDIO`
161
162 >Description:
163         $DESCRIPTION_C
164 >How-To-Repeat:
165         $HOW_TO_REPEAT_C
166 >Fix:
167         $FIX_C
168 EOF
169
170 chmod u+w $TEMP
171 cp $TEMP $TEMPx
172
173 eval $EDIT $TEMP
174
175 if cmp -s $TEMP $TEMPx; then
176         echo "File not changed, no bug report submitted."
177         exit 1
178 fi
179
180 #\f
181 #       Check the enumeration fields
182
183 # This is a "sed-subroutine" with one keyword parameter
184 # (with workaround for Sun sed bug)
185 #
186 SED_CMD='
187 /$PATTERN/{
188 s|||
189 s|<.*>||
190 s|^[    ]*||
191 s|[     ]*$||
192 p
193 q
194 }'
195
196
197 while :; do
198   CNT=0
199
200   #
201   # 1) Severity
202   #
203   PATTERN=">Severity:"
204   SEVERITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
205   case "$SEVERITY" in
206     ""|non-critical|serious|critical) CNT=`expr $CNT + 1` ;;
207     *)  echo "$COMMAND: \`$SEVERITY' is not a valid value for \`Severity'."
208   esac
209   #
210   # 2) Priority
211   #
212   PATTERN=">Priority:"
213   PRIORITY=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
214   case "$PRIORITY" in
215     ""|low|medium|high) CNT=`expr $CNT + 1` ;;
216     *)  echo "$COMMAND: \`$PRIORITY' is not a valid value for \`Priority'."
217   esac
218   #
219   # 3) Class
220   #
221   PATTERN=">Class:"
222   CLASS=`eval sed -n -e "\"$SED_CMD\"" $TEMP`
223   case "$CLASS" in
224     ""|sw-bug|doc-bug|change-request|support) CNT=`expr $CNT + 1` ;;
225     *)  echo "$COMMAND: \`$CLASS' is not a valid value for \`Class'."
226   esac
227
228   [ $CNT -lt 3 ] &&
229     echo "Errors were found with the problem report."
230
231   while :; do
232     $ECHON1 "a)bort, e)dit or s)end? $ECHON2"
233     read input
234     case "$input" in
235       a*)
236         echo "$COMMAND: problem report saved in $HOME/dead.glibcbug."
237         cat $TEMP >> $HOME/dead.glibcbug
238         xs=1; exit
239         ;;
240       e*)
241         eval $EDIT $TEMP
242         continue 2
243         ;;
244       s*)
245         break 2
246         ;;
247     esac
248   done
249 done
250 #
251 #       Remove comments and send the problem report
252 #       (we have to use patterns, where the comment contains regex chars)
253 #
254 # /^>Originator:/s;$ORIGINATOR;;
255 sed  -e "
256 /^SEND-PR:/d
257 /^>Organization:/,/^>[A-Za-z-]*:/s;$ORGANIZATION_C;;
258 /^>Confidential:/s;<.*>;;
259 /^>Synopsis:/s;$SYNOPSIS_C;;
260 /^>Severity:/s;<.*>;;
261 /^>Priority:/s;<.*>;;
262 /^>Class:/s;<.*>;;
263 /^>Release:/,/^>[A-Za-z-]*:/s;$RELEASE_C;;
264 /^>Environment:/,/^>[A-Za-z-]*:/s;$ENVIRONMENT_C;;
265 /^>Description:/,/^>[A-Za-z-]*:/s;$DESCRIPTION_C;;
266 /^>How-To-Repeat:/,/^>[A-Za-z-]*:/s;$HOW_TO_REPEAT_C;;
267 /^>Fix:/,/^>[A-Za-z-]*:/s;$FIX_C;;
268 " $TEMP > $TEMPx
269
270 if $MAIL_AGENT < $TEMPx; then
271   echo "$COMMAND: problem report sent"
272   xs=0; exit
273 else
274   echo "$COMMAND: mysterious mail failure, report not sent."
275   echo "$COMMAND: problem report saved in $HOME/dead.glibcbug."
276   cat $TEMP >> $HOME/dead.glibcbug
277 fi
278
279 exit 0