Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / send-pr / send-pr.sh
1 #!/bin/sh
2 # Submit a problem report to a GNATS site.
3 # Copyright (C) 1993 Free Software Foundation, Inc.
4 # Contributed by Brendan Kehoe (brendan@cygnus.com), based on a
5 # version written by Heinz G. Seidl (hgs@cygnus.com).
6 #
7 # This file is part of GNU GNATS.
8 #
9 # GNU GNATS is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
12 # any later version.
13 #
14 # GNU GNATS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GNU GNATS; see the file COPYING.  If not, write to
21 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 #
23 # $FreeBSD: src/gnu/usr.bin/send-pr/send-pr.sh,v 1.13.2.11 2002/03/19 00:33:35 des Exp $
24
25 # The version of this send-pr.
26 VERSION=3.113
27
28 # The submitter-id for your site.
29 # "current-users" is the only allowable value for FreeBSD.
30 SUBMITTER="current-users"
31
32 # Where the GNATS directory lives, if at all.
33 [ -z "$GNATS_ROOT" ] && 
34 GNATS_ROOT=
35
36 # The default mail address for PR submissions. 
37 GNATS_ADDR=FreeBSD-gnats-submit@freebsd.org
38
39 # Where the gnats category tree lives.
40 DATADIR=@DATADIR@
41
42 # If we've been moved around, try using GCC_EXEC_PREFIX.
43 [ ! -d $DATADIR/gnats -a -d "$GCC_EXEC_PREFIX" ] && DATADIR="$GCC_EXEC_PREFIX"
44
45 # The default release for this host.
46 DEFAULT_RELEASE="@DEFAULT_RELEASE@"
47
48 # The default organization.
49 DEFAULT_ORGANIZATION=
50
51 # The default site to look for.
52 GNATS_SITE=freefall
53
54 # Newer config information?
55 [ -f ${GNATS_ROOT}/gnats-adm/config ] && . ${GNATS_ROOT}/gnats-adm/config
56
57 # What mailer to use.  This must come after the config file, since it is
58 # host-dependent.
59 MAIL_AGENT="${MAIL_AGENT:-/usr/sbin/sendmail -oi -t}"
60
61 # Path to pw(8)
62 PW="/usr/sbin/pw"
63
64 ECHON=bsd
65
66 if [ $ECHON = bsd ] ; then
67   ECHON1="echo -n"
68   ECHON2=
69 elif [ $ECHON = sysv ] ; then
70   ECHON1=echo
71   ECHON2='\c'
72 else
73   ECHON1=echo
74   ECHON2=
75 fi
76
77 #\f
78
79 # find a user name
80 if [ "$LOGNAME" = "" ]; then
81         if [ "$USER" != "" ]; then
82                 LOGNAME="$USER"
83         else
84                 LOGNAME="UNKNOWN"
85         fi
86 fi
87
88 # Find out the name of the originator of this PR.
89 if [ -n "$NAME" ]; then
90   ORIGINATOR="$NAME"
91 elif [ -f $HOME/.fullname ]; then
92   ORIGINATOR="`sed -e '1q' $HOME/.fullname`"
93 else
94   PTEMP=`mktemp -t p` || exit 1
95   # Must use temp file due to incompatibilities in quoting behavior
96   # and to protect shell metacharacters in the expansion of $LOGNAME
97   $PW usershow $LOGNAME | awk -F: '{ print $8 }' | sed -e 's/,.*//' > $PTEMP
98   ORIGINATOR="`cat $PTEMP`"
99   rm -f $PTEMP
100 fi
101
102 FROM="$ORIGINATOR <$LOGNAME>"
103 REPLY_TO="$ORIGINATOR <${REPLY_TO:-${REPLYTO:-$LOGNAME}}>"
104
105 if [ -n "$ORGANIZATION" ]; then
106   if [ -f "$ORGANIZATION" ]; then
107     ORGANIZATION="`cat $ORGANIZATION`"
108   fi
109 else
110   if [ -n "$DEFAULT_ORGANIZATION" ]; then
111     ORGANIZATION="$DEFAULT_ORGANIZATION"
112   elif [ -f $HOME/.organization ]; then
113     ORGANIZATION="`cat $HOME/.organization`"
114   fi
115 fi
116
117 # If they don't have a preferred editor set, then use
118 if [ -z "$VISUAL" ]; then
119   if [ -z "$EDITOR" ]; then
120     EDIT=vi
121   else
122     EDIT="$EDITOR"
123   fi
124 else
125   EDIT="$VISUAL"
126 fi
127
128 # Find out some information.
129 SYSTEM=`( [ -f /bin/uname ] && /bin/uname -a ) || \
130         ( [ -f /usr/bin/uname ] && /usr/bin/uname -a ) || echo ""`
131 ARCH=`[ -f /bin/arch ] && /bin/arch`
132 MACHINE=`[ -f /bin/machine ] && /bin/machine`
133
134 COMMAND=`echo $0 | sed -e 's,.*/,,'`
135 USAGE="Usage: $COMMAND [-PVL] [-t address] [-f filename] [-s severity]
136        [-c address] [-a file] [--version]"
137 REMOVE=
138 BATCH=
139 CC=
140 SEVERITY_C=
141
142 while [ $# -gt 0 ]; do
143   case "$1" in
144     -r) ;;              # Ignore for backward compat.
145     -t | --to) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
146         shift ; GNATS_ADDR="$1"
147         EXPLICIT_GNATS_ADDR=true
148         ;;
149     -f | --file) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
150         shift ; IN_FILE="$1"
151         if [ "$IN_FILE" != "-" -a ! -r "$IN_FILE" ]; then
152           echo "$COMMAND: cannot read $IN_FILE"
153           exit 1
154         fi
155         ;;
156     -b | --batch) BATCH=true ;;
157     -c | --cc) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
158         shift ; CC="$1"
159         ;;
160     -s | --severity) if [ $# -eq 1 ]; then echo "$USAGE"; exit 1; fi
161         shift ; SEVERITY_C="$1"
162         ;;
163     -p | -P | --print) PRINT=true ;;
164     -L | --list) FORMAT=norm ;;
165     -l | -CL | --lisp) FORMAT=lisp ;;
166     -h | --help) echo "$USAGE"; exit 0 ;;
167     -V | --version) echo "$VERSION"; exit 0 ;;
168     -a | --attach) if [ -z "$2" ]; then
169           echo "$USAGE" ; exit 1; 
170         fi
171         if [ -e "$2" -a ! -d "$2" ]; then
172           PRETTY_NAME=`basename $2`
173           if file $2 | grep "text" >/dev/null 2>/dev/null ; then
174             ATTACHED_FILES="$ATTACHED_FILES
175 --- $PRETTY_NAME begins here ---
176 `cat \"$2\"`
177 --- $PRETTY_NAME ends here ---
178 "
179           else
180             ATTACHED_FILES="$ATTACHED_FILES
181 `uuencode \"$PRETTY_NAME\" < \"$2\"`
182 "
183           fi
184           shift;
185         fi;
186         ;;
187     -*) echo "$USAGE" ; exit 1 ;;
188     *) if [ -z "$USER_GNATS_SITE" ]; then
189          if [ ! -r "$DATADIR/gnats/$1" ]; then
190            echo "$COMMAND: the GNATS site $1 does not have a categories list."
191            exit 1
192          else
193            # The site name is the alias they'll have to have created.
194            USER_GNATS_SITE=$1
195          fi
196        else
197          echo "$USAGE" ; exit 1
198        fi
199        ;;
200  esac
201  shift
202 done
203
204 if [ -n "$USER_GNATS_SITE" ] && [ "$USER_GNATS_SITE" != "$GNATS_SITE" ]; then
205   GNATS_SITE=$USER_GNATS_SITE
206   GNATS_ADDR=$USER_GNATS_SITE-gnats
207 fi
208
209 if [ "$SUBMITTER" = "unknown" -a -z "$IN_FILE" ]; then
210   SUBMITTER="current-users"
211 fi
212
213 if [ -r "$DATADIR/gnats/$GNATS_SITE" ]; then
214   CATEGORIES=`grep -v '^#' $DATADIR/gnats/$GNATS_SITE | sort`
215 else
216   echo "$COMMAND: could not read $DATADIR/gnats/$GNATS_SITE for categories list."
217   exit 1
218 fi
219
220 if [ -z "$CATEGORIES" ]; then
221   echo "$COMMAND: the categories list for $GNATS_SITE was empty!"
222   exit 1
223 fi
224
225 case "$FORMAT" in
226   lisp) echo "$CATEGORIES" | \
227         awk 'BEGIN {printf "( "} {printf "(\"%s\") ",$0} END {printf ")\n"}'
228         exit 0
229         ;;
230   norm) l=`echo "$CATEGORIES" | \
231         awk 'BEGIN {max = 0; } { if (length($0) > max) { max = length($0); } }
232              END {print max + 1;}'`
233         c=`expr 70 / $l`
234         if [ $c -eq 0 ]; then c=1; fi
235         echo "$CATEGORIES" | \
236         awk 'BEGIN {print "Known categories:"; i = 0 }
237           { printf ("%-'$l'.'$l's", $0); if ((++i % '$c') == 0) { print "" } }
238             END { print ""; }'
239         exit 0
240         ;;
241 esac
242
243 ORIGINATOR_C='<name of the PR author (one line)>'
244 ORGANIZATION_C='<organization of PR author (multiple lines)>'
245 CONFIDENTIAL_C='no <FreeBSD PRs are public data>'
246 SYNOPSIS_C='<synopsis of the problem (one line)>'
247 if [ -z "$SEVERITY_C" ]; then
248   SEVERITY_C='<[ non-critical | serious | critical ] (one line)>'
249 fi
250 PRIORITY_C='<[ low | medium | high ] (one line)>'
251 CATEGORY_C='<choose from the list of categories above (one line)>'
252 CLASS_C='<[ sw-bug | doc-bug | change-request | update | maintainer-update ] (one line)>'
253 RELEASE_C='<release number or tag (one line)>'
254 ENVIRONMENT_C='<machine, os, target, libraries (multiple lines)>'
255 DESCRIPTION_C='<precise description of the problem (multiple lines)>'
256 HOW_TO_REPEAT_C='<code/input/activities to reproduce the problem (multiple lines)>'
257 FIX_C='<how to correct or work around the problem, if known (multiple lines)>'
258
259 # Create temporary files, safely
260 REF=`mktemp -t pf` || exit 1
261 TEMP=`mktemp -t pf` || exit 1
262 # Catch some signals. ($xs kludge needed by Sun /bin/sh)
263 xs=0
264 trap 'rm -f $REF $TEMP; exit $xs' 0
265 trap 'echo "$COMMAND: Aborting ..."; rm -f $REF $TEMP; xs=1; exit' 1 2 3 13 15
266
267 # If they told us to use a specific file, then do so.
268 if [ -n "$IN_FILE" ]; then
269   if [ "$IN_FILE" = "-" ]; then
270     # The PR is coming from the standard input.
271     if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
272       sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" > $TEMP
273     else
274       cat > $TEMP
275     fi
276   else
277     # Use the file they named.
278     if [ -n "$EXPLICIT_GNATS_ADDR" ]; then
279       sed -e "s;^[Tt][Oo]:.*;To: $GNATS_ADDR;" $IN_FILE > $TEMP
280     else
281       cat $IN_FILE > $TEMP
282     fi
283   fi
284 else
285
286   if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
287     # If their PR_FORM points to a bogus entry, then bail.
288     if [ ! -f "$PR_FORM" -o ! -r "$PR_FORM" -o ! -s "$PR_FORM" ]; then
289       echo "$COMMAND: can't seem to read your template file (\`$PR_FORM'), ignoring PR_FORM"
290       sleep 1
291       PRINT_INTERN=bad_prform
292     fi
293   fi
294
295   if [ -n "$PR_FORM" -a -z "$PRINT_INTERN" ]; then
296     cp $PR_FORM $TEMP || 
297       ( echo "$COMMAND: could not copy $PR_FORM" ; xs=1; exit )
298     [ -n "$ATTACHED_FILES" ] && echo "$ATTACHED_FILES" >> $TEMP
299   else
300     for file in $TEMP $REF ; do
301       cat  > $file << '__EOF__'
302 SEND-PR: -*- send-pr -*-
303 SEND-PR: Lines starting with `SEND-PR' will be removed automatically, as
304 SEND-PR: will all comments (text enclosed in `<' and `>').
305 SEND-PR:
306 SEND-PR: Please consult the following URL if you are not sure how to
307 SEND-PR: fill out a problem report:
308 SEND-PR: http://www.freebsd.org/doc/en/articles/problem-reports/article.html
309 SEND-PR:
310 SEND-PR: Note that the Synopsis field is mandatory.
311 SEND-PR:
312 SEND-PR: Please note that (unless you state otherwise) if your report 
313 SEND-PR: includes a patch then it will be taken under the same license as 
314 SEND-PR: the one on the file(s) you want to change.
315 SEND-PR:
316 SEND-PR: BE ADVISED THAT FREEBSD PROBLEM REPORTS ARE PUBLIC INFORMATION AND
317 SEND-PR: WILL BE PUBLISHED AS-IS ON THE PROJECT'S MAILING LISTS AND WEB SITES.
318 SEND-PR: DO NOT SUBMIT ANY INFORMATION YOU DO NOT WANT MADE PUBLIC.
319 SEND-PR:
320 SEND-PR: For sensitive security issues, consider contacting the FreeBSD
321 SEND-PR: security officer team (security-officer@freebsd.org) directly.
322 SEND-PR:
323 SEND-PR: Choose from the following categories:
324 SEND-PR:
325 __EOF__
326
327       # Format the categories so they fit onto lines.
328         l=`echo "$CATEGORIES" | \
329         awk 'BEGIN {max = 0; } { if (length($0) > max) { max = length($0); } }
330              END {print max + 1;}'`
331         c=`expr 61 / $l`
332         if [ $c -eq 0 ]; then c=1; fi
333         echo "$CATEGORIES" | \
334         awk 'BEGIN {printf "SEND-PR: "; i = 0 }
335           { printf ("%-'$l'.'$l's", $0);
336             if ((++i % '$c') == 0) { printf "\nSEND-PR: " } }
337             END { printf "\nSEND-PR:\n"; }' >> $file
338
339       cat >> $file << __EOF__
340 To: $GNATS_ADDR
341 From: $FROM
342 Reply-To: $REPLY_TO
343 Cc: $CC
344 X-send-pr-version: $VERSION
345 X-GNATS-Notify: 
346
347
348 >Submitter-Id:  $SUBMITTER
349 >Originator:    $ORIGINATOR
350 >Organization:  ${ORGANIZATION-$ORGANIZATION_C}
351 >Confidential:  $CONFIDENTIAL_C
352 >Synopsis:      $SYNOPSIS_C
353 >Severity:      $SEVERITY_C
354 >Priority:      $PRIORITY_C
355 >Category:      $CATEGORY_C
356 >Class:         $CLASS_C
357 >Release:       ${DEFAULT_RELEASE-$RELEASE_C}
358 >Environment:
359 `[ -n "$SYSTEM" ] && echo System: $SYSTEM`
360 `[ -n "$ARCH" ] && echo Architecture: $ARCH`
361 `[ -n "$MACHINE" ] && echo Machine: $MACHINE`
362         $ENVIRONMENT_C
363 >Description:
364         $DESCRIPTION_C
365 >How-To-Repeat:
366         $HOW_TO_REPEAT_C
367 >Fix:
368
369         $FIX_C
370 $ATTACHED_FILES
371
372 __EOF__
373
374     done
375   fi
376
377   if [ "$PRINT" = true -o "$PRINT_INTERN" = true ]; then
378     cat $TEMP
379     xs=0; exit
380   fi
381
382   chmod u+w $TEMP
383   eval $EDIT $TEMP
384
385   if cmp -s $REF $TEMP ; then
386     echo "$COMMAND: problem report not filled out, therefore not sent"
387     xs=1; exit
388   fi
389 fi
390
391 #\f
392 #       Check the enumeration fields
393
394 # This is a "sed-subroutine" with one keyword parameter 
395 # (with workaround for Sun sed bug)
396 #
397 SED_CMD='{
398 s|||
399 s|<.*>||
400 s|^[    ]*||
401 s|[     ]*$||
402 p
403 q
404 }'
405
406
407 while true; do
408   CNT=0
409
410   # 1) Confidential
411   #
412   PATTERN=">Confidential:"
413   CONFIDENTIAL=`eval sed -n -e "\"/$PATTERN/$SED_CMD\"" $TEMP`
414   case "$CONFIDENTIAL" in
415     ""|no) CNT=`expr $CNT + 1` ;;
416     *) echo "$COMMAND: \`$CONFIDENTIAL' is not a valid value for \`Confidential'." ;;
417   esac
418   #
419   # 2) Severity
420   #
421   PATTERN=">Severity:"
422   SEVERITY=`eval sed -n -e "\"/$PATTERN/$SED_CMD\"" $TEMP`
423   case "$SEVERITY" in
424     ""|non-critical|serious|critical) CNT=`expr $CNT + 1` ;;
425     *)  echo "$COMMAND: \`$SEVERITY' is not a valid value for \`Severity'."
426   esac
427   #
428   # 3) Priority
429   #
430   PATTERN=">Priority:"
431   PRIORITY=`eval sed -n -e "\"/$PATTERN/$SED_CMD\"" $TEMP`
432   case "$PRIORITY" in
433     ""|low|medium|high) CNT=`expr $CNT + 1` ;;
434     *)  echo "$COMMAND: \`$PRIORITY' is not a valid value for \`Priority'."
435   esac
436   #
437   # 4) Category
438   #
439   PATTERN=">Category:"
440   CATEGORY=`eval sed -n -e "\"/$PATTERN/$SED_CMD\"" $TEMP`
441   FOUND=
442   for C in $CATEGORIES
443   do
444     if [ "$C" = "$CATEGORY" ]; then FOUND=true ; break ; fi
445   done
446   if [ -n "$FOUND" ]; then
447     CNT=`expr $CNT + 1` 
448   else
449     if [ -z "$CATEGORY" ]; then
450       echo "$COMMAND: you must include a Category: field in your report."
451     else
452       echo "$COMMAND: \`$CATEGORY' is not a known category."
453     fi
454   fi
455   #
456   # 5) Class
457   #
458   PATTERN=">Class:"
459   CLASS=`eval sed -n -e "\"/$PATTERN/$SED_CMD\"" $TEMP`
460   case "$CLASS" in
461     ""|sw-bug|doc-bug|change-request|update|maintainer-update) CNT=`expr $CNT + 1` ;;
462     *)  echo "$COMMAND: \`$CLASS' is not a valid value for \`Class'."
463   esac
464   #
465   # 6) Check that synopsis is not empty
466   #
467   if grep "^>Synopsis:[         ]*${SYNOPSIS_C}\$" $TEMP > /dev/null
468   then
469     echo "$COMMAND: Synopsis must not be empty."
470   else
471     CNT=`expr $CNT + 1`
472   fi
473
474   [ $CNT -lt 6 -a -z "$BATCH" ] && 
475     echo "Errors were found with the problem report."
476
477   while true; do
478     if [ -z "$BATCH" ]; then
479       $ECHON1 "s)end, e)dit or a)bort? $ECHON2"
480       read input
481     else
482       if [ $CNT -eq 6 ]; then
483         input=s
484       else
485         input=a
486       fi
487     fi
488     case "$input" in
489       a*)
490         if [ -z "$BATCH" ]; then
491           BAD=`mktemp -t pbad`
492           echo "$COMMAND: the problem report remains in $BAD and is not sent."
493           mv $TEMP $BAD
494         else
495           echo "$COMMAND: the problem report is not sent."
496         fi
497         xs=1; exit
498         ;;
499       e*)
500         eval $EDIT $TEMP
501         continue 2
502         ;;
503       s*)
504         break 2
505         ;;
506     esac
507   done
508 done
509
510 #
511 # Remove the subject field if one is already there.  There's no reason
512 # for it to be any different than the synopsis.
513 #
514 if grep '^Subject:' $TEMP > /dev/null
515 then
516   ed -s $TEMP << __EOF__
517 /^Subject:/d
518 w
519 q
520 __EOF__
521 fi
522
523 #
524 # Add the subject field with the value of $SYNOPSIS.  We use the To:
525 # field as an anchor, which had better be there.
526 #
527 SYNOPSIS=`grep '^>Synopsis:' $TEMP | sed -e 's/^>Synopsis:[     ]*//' |
528     sed -e "s;$SYNOPSIS_C;;"`
529 ed -s $TEMP << __EOF__
530 /^To:/a
531 Subject: $SYNOPSIS
532 .
533 w
534 q
535 __EOF__
536
537 #
538 #       Remove comments and send the problem report
539 #       (we have to use patterns, where the comment contains regex chars)
540 #
541 # /^>Originator:/s;$ORIGINATOR;;
542 sed  -e "
543 /^SEND-PR:/d
544 /^>Organization:/,/^>[A-Za-z-]*:/s;$ORGANIZATION_C;;
545 /^>Confidential:/s;<.*>;;
546 /^>Synopsis:/s;$SYNOPSIS_C;;
547 /^>Severity:/s;<.*>;;
548 /^>Priority:/s;<.*>;;
549 /^>Category:/s;$CATEGORY_C;;
550 /^>Class:/s;<.*>;;
551 /^>Release:/,/^>[A-Za-z-]*:/s;$RELEASE_C;;
552 /^>Environment:/,/^>[A-Za-z-]*:/s;$ENVIRONMENT_C;;
553 /^>Description:/,/^>[A-Za-z-]*:/s;$DESCRIPTION_C;;
554 /^>How-To-Repeat:/,/^>[A-Za-z-]*:/s;$HOW_TO_REPEAT_C;;
555 /^>Fix:/,/^>[A-Za-z-]*:/s;$FIX_C;;
556 " $TEMP > $REF
557
558 if $MAIL_AGENT < $REF; then
559   echo "$COMMAND: problem report sent"
560   xs=0; exit
561 else
562   echo "$COMMAND: mysterious mail failure."
563   if [ -z "$BATCH" ]; then
564     BAD=`mktemp -t pbad`
565     echo "$COMMAND: the problem report remains in $BAD and is not sent."
566     mv $REF $BAD
567   else
568     echo "$COMMAND: the problem report is not sent."
569   fi
570   xs=1; exit
571 fi