(tmm-add-prompt): If tmm-completion-prompt is nil,
[emacs.git] / lib-src / rcs2log
blob8de10c2e1f00d910c63b13debffdabc58b5c31c7
1 #! /bin/sh
3 # RCS to ChangeLog generator
5 # Generate a change log prefix from RCS files and the ChangeLog (if any).
6 # Output the new prefix to standard output.
7 # You can edit this prefix by hand, and then prepend it to ChangeLog.
9 # Ignore log entries that start with `#'.
10 # Clump together log entries that start with `{topic} ',
11 # where `topic' contains neither white space nor `}'.
13 # Author: Paul Eggert <eggert@twinsun.com>
15 # $Id: rcs2log,v 1.27 1996/01/15 01:17:03 eggert Exp $
17 # Copyright 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2, or (at your option)
22 # any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; see the file COPYING. If not, write to
31 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
33 tab=' '
34 nl='
37 # Parse options.
39 # defaults
40 : ${AWK=awk}
41 : ${TMPDIR=/tmp}
42 hostname= # name of local host (if empty, will deduce it later)
43 indent=8 # indent of log line
44 length=79 # suggested max width of log line
45 logins= # login names for people we know fullnames and mailaddrs of
46 loginFullnameMailaddrs= # login<tab>fullname<tab>mailaddr triplets
47 recursive= # t if we want recursive rlog
48 rlog_options= # options to pass to rlog
49 tabwidth=8 # width of horizontal tab
51 while :
53 case $1 in
54 -i) indent=${2?}; shift;;
55 -h) hostname=${2?}; shift;;
56 -l) length=${2?}; shift;;
57 -[nu]) # -n is obsolescent; it is replaced by -u.
58 case $1 in
59 -n) case ${2?}${3?}${4?} in
60 *"$tab"* | *"$nl"*)
61 echo >&2 "$0: -n '$2' '$3' '$4': tabs, newlines not allowed"
62 exit 1
63 esac
64 loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2$tab$3$tab$4
65 shift; shift; shift;;
66 -u)
67 # If $2 is not tab-separated, use colon for separator.
68 case ${2?} in
69 *"$nl"*)
70 echo >&2 "$0: -u '$2': newlines not allowed"
71 exit 1;;
72 *"$tab"*)
73 t=$tab;;
75 t=:
76 esac
77 case $2 in
78 *"$t"*"$t"*"$t"*)
79 echo >&2 "$0: -u '$2': too many fields"
80 exit 1;;
81 *"$t"*"$t"*)
84 echo >&2 "$0: -u '$2': not enough fields"
85 exit 1
86 esac
87 loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2
88 shift
89 esac
90 logins=$logins$nl$login
92 -r) rlog_options=$rlog_options$nl${2?}; shift;;
93 -R) recursive=t;;
94 -t) tabwidth=${2?}; shift;;
95 -*) echo >&2 "$0: usage: $0 [options] [file ...]
96 Options:
97 [-h hostname] [-i indent] [-l length] [-R] [-r rlog_option]
98 [-t tabwidth] [-u 'login<TAB>fullname<TAB>mailaddr']..."
99 exit 1;;
100 *) break
101 esac
102 shift
103 done
105 month_data='
106 m[0]="Jan"; m[1]="Feb"; m[2]="Mar"
107 m[3]="Apr"; m[4]="May"; m[5]="Jun"
108 m[6]="Jul"; m[7]="Aug"; m[8]="Sep"
109 m[9]="Oct"; m[10]="Nov"; m[11]="Dec"
111 # days in non-leap year thus far, indexed by month (0-12)
112 mo[0]=0; mo[1]=31; mo[2]=59; mo[3]=90
113 mo[4]=120; mo[5]=151; mo[6]=181; mo[7]=212
114 mo[8]=243; mo[9]=273; mo[10]=304; mo[11]=334
115 mo[12]=365
119 # Put rlog output into $rlogout.
121 # If no rlog options are given,
122 # log the revisions checked in since the first ChangeLog entry.
123 case $rlog_options in
125 date=1970
126 if test -s ChangeLog
127 then
128 # Add 1 to seconds to avoid duplicating most recent log.
130 /^... ... [ 0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9]+ /{
131 '"$month_data"'
132 year = $5
133 for (i=0; i<=11; i++) if (m[i] == $2) break
134 dd = $3
135 hh = substr($0,12,2)
136 mm = substr($0,15,2)
137 ss = substr($0,18,2)
138 ss++
139 if (ss == 60) {
140 ss = 0
141 mm++
142 if (mm == 60) {
143 mm = 0
144 hh++
145 if (hh == 24) {
146 hh = 0
147 dd++
148 monthdays = mo[i+1] - mo[i]
149 if (i == 1 && year%4 == 0 && (year%100 != 0 || year%400 == 0)) monthdays++
150 if (dd == monthdays + 1) {
151 dd = 1
153 if (i == 12) {
154 i = 0
155 year++
161 # Output comma instead of space to avoid CVS 1.5 bug.
162 printf "%d/%02d/%02d,%02d:%02d:%02d\n", year,i+1,dd,hh,mm,ss
163 exit
166 d=`$AWK "$e" <ChangeLog` || exit
167 case $d in
168 ?*) date=$d
169 esac
171 datearg="-d>$date"
172 esac
174 # If CVS is in use, examine its repository, not the normal RCS files.
175 if test ! -f CVS/Repository
176 then
177 rlog=rlog
178 repository=
179 else
180 rlog='cvs log'
181 repository=`sed 1q <CVS/Repository` || exit
182 test ! -f CVS/Root || CVSROOT=`cat <CVS/Root` || exit
183 case $CVSROOT in
184 *:/*)
185 # remote repository
188 # local repository
189 case $repository in
190 /*) ;;
191 *) repository=${CVSROOT?}/$repository
192 esac
193 if test ! -d "$repository"
194 then
195 echo >&2 "$0: $repository: bad repository (see CVS/Repository)"
196 exit 1
198 esac
201 # With no arguments, examine all files under the RCS directory.
202 case $# in
204 case $repository in
206 oldIFS=$IFS
207 IFS=$nl
208 case $recursive in
210 RCSdirs=`find . -name RCS -type d -print`
211 filesFromRCSfiles='s|,v$||; s|/RCS/|/|; s|^\./||'
212 files=`
214 case $RCSdirs in
215 ?*) find $RCSdirs -type f -print
216 esac
217 find . -name '*,v' -print
219 sort -u |
220 sed "$filesFromRCSfiles"
223 files=
224 for file in RCS/.* RCS/* .*,v *,v
226 case $file in
227 RCS/. | RCS/..) continue;;
228 RCS/.\* | RCS/\* | .\*,v | \*,v) test -f "$file" || continue
229 esac
230 files=$files$nl$file
231 done
232 case $files in
233 '') exit 0
234 esac
235 esac
236 set x $files
237 shift
238 IFS=$oldIFS
239 esac
240 esac
242 llogout=$TMPDIR/rcs2log$$l
243 rlogout=$TMPDIR/rcs2log$$r
244 trap exit 1 2 13 15
245 trap "rm -f $llogout $rlogout; exit 1" 0
247 case $rlog_options in
248 ?*) $rlog $rlog_options ${1+"$@"} >$rlogout;;
249 '') $rlog "$datearg" ${1+"$@"} >$rlogout
250 esac || exit
253 # Get the full name of each author the logs mention, and set initialize_fullname
254 # to awk code that initializes the `fullname' awk associative array.
255 # Warning: foreign authors (i.e. not known in the passwd file) are mishandled;
256 # you have to fix the resulting output by hand.
258 initialize_fullname=
259 initialize_mailaddr=
261 case $loginFullnameMailaddrs in
263 case $loginFullnameMailaddrs in
264 *\"* | *\\*)
265 sed 's/["\\]/\\&/g' >$llogout <<EOF || exit
266 $loginFullnameMailaddrs
268 loginFullnameMailaddrs=`cat $llogout`
269 esac
271 oldIFS=$IFS
272 IFS=$nl
273 for loginFullnameMailaddr in $loginFullnameMailaddrs
275 case $loginFullnameMailaddr in
276 *"$tab"*) IFS=$tab;;
277 *) IFS=:
278 esac
279 set x $loginFullnameMailaddr
280 login=$2
281 fullname=$3
282 mailaddr=$4
283 initialize_fullname="$initialize_fullname
284 fullname[\"$login\"] = \"$fullname\""
285 initialize_mailaddr="$initialize_mailaddr
286 mailaddr[\"$login\"] = \"$mailaddr\""
287 done
288 IFS=$oldIFS
289 esac
291 case $llogout in
292 ?*) sort -u -o $llogout <<EOF || exit
293 $logins
295 esac
296 output_authors='/^date: / {
297 if ($2 ~ /^[0-9]*[-\/][0-9][0-9][-\/][0-9][0-9]$/ && $3 ~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9][-+0-9:]*;$/ && $4 == "author:" && $5 ~ /^[^;]*;$/) {
298 print substr($5, 1, length($5)-1)
301 authors=`
302 $AWK "$output_authors" <$rlogout |
303 case $llogout in
304 '') sort -u;;
305 ?*) sort -u | comm -23 - $llogout
306 esac
308 case $authors in
310 cat >$llogout <<EOF || exit
311 $authors
313 initialize_author_script='s/["\\]/\\&/g; s/.*/author[\"&\"] = 1/'
314 initialize_author=`sed -e "$initialize_author_script" <$llogout`
315 awkscript='
316 BEGIN {
317 alphabet = "abcdefghijklmnopqrstuvwxyz"
318 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
319 '"$initialize_author"'
322 if (author[$1]) {
323 fullname = $5
324 if (fullname ~ /[0-9]+-[^(]*\([0-9]+\)$/) {
325 # Remove the junk from fullnames like "0000-Admin(0000)".
326 fullname = substr(fullname, index(fullname, "-") + 1)
327 fullname = substr(fullname, 1, index(fullname, "(") - 1)
329 if (fullname ~ /,[^ ]/) {
330 # Some sites put comma-separated junk after the fullname.
331 # Remove it, but leave "Bill Gates, Jr" alone.
332 fullname = substr(fullname, 1, index(fullname, ",") - 1)
334 abbr = index(fullname, "&")
335 if (abbr) {
336 a = substr($1, 1, 1)
337 A = a
338 i = index(alphabet, a)
339 if (i) A = substr(ALPHABET, i, 1)
340 fullname = substr(fullname, 1, abbr-1) A substr($1, 2) substr(fullname, abbr+1)
343 # Quote quotes and backslashes properly in full names.
344 # Do not use gsub; traditional awk lacks it.
345 quoted = ""
346 rest = fullname
347 for (;;) {
348 p = index(rest, "\\")
349 q = index(rest, "\"")
350 if (p) {
351 if (q && q<p) p = q
352 } else {
353 if (!q) break
354 p = q
356 quoted = quoted substr(rest, 1, p-1) "\\" substr(rest, p, 1)
357 rest = substr(rest, p+1)
360 printf "fullname[\"%s\"] = \"%s%s\"\n", $1, quoted, rest
361 author[$1] = 0
366 initialize_fullname=`
368 cat /etc/passwd
369 for author in $authors
370 do nismatch $author passwd.org_dir
371 done
372 ypmatch $authors passwd
373 ) 2>/dev/null |
374 $AWK -F: "$awkscript"
375 `$initialize_fullname
376 esac
379 # Function to print a single log line.
380 # We don't use awk functions, to stay compatible with old awk versions.
381 # `Log' is the log message (with \n replaced by \r).
382 # `files' contains the affected files.
383 printlogline='{
385 # Following the GNU coding standards, rewrite
386 # * file: (function): comment
387 # to
388 # * file (function): comment
389 if (Log ~ /^\([^)]*\): /) {
390 i = index(Log, ")")
391 files = files " " substr(Log, 1, i)
392 Log = substr(Log, i+3)
395 # If "label: comment" is too long, break the line after the ":".
396 sep = " "
397 if ('"$length"' <= '"$indent"' + 1 + length(files) + index(Log, CR)) sep = "\n" indent_string
399 # Print the label.
400 printf "%s*%s:", indent_string, files
402 # Print each line of the log, transliterating \r to \n.
403 while ((i = index(Log, CR)) != 0) {
404 logline = substr(Log, 1, i-1)
405 if (logline ~ /[^'"$tab"' ]/) {
406 printf "%s%s\n", sep, logline
407 } else {
408 print ""
410 sep = indent_string
411 Log = substr(Log, i+1)
415 case $hostname in
417 hostname=`(
418 hostname || uname -n || uuname -l || cat /etc/whoami
419 ) 2>/dev/null` || {
420 echo >&2 "$0: cannot deduce hostname"
421 exit 1
424 case $hostname in
425 *.*) ;;
427 domainname=`(domainname) 2>/dev/null` &&
428 case $domainname in
429 *.*) hostname=$hostname.$domainname
430 esac
431 esac
432 esac
435 # Process the rlog output, generating ChangeLog style entries.
437 # First, reformat the rlog output so that each line contains one log entry.
438 # Transliterate \n to \r so that multiline entries fit on a single line.
439 # Discard irrelevant rlog output.
440 $AWK <$rlogout '
441 BEGIN { repository = "'"$repository"'" }
442 /^RCS file:/ {
443 if (repository != "") {
444 filename = $3
445 if (substr(filename, 1, length(repository) + 1) == repository "/") {
446 filename = substr(filename, length(repository) + 2)
448 if (filename ~ /,v$/) {
449 filename = substr(filename, 1, length(filename) - 2)
453 /^Working file:/ { if (repository == "") filename = $3 }
454 /^date: /, /^(-----------*|===========*)$/ {
455 if ($0 ~ /^branches: /) { next }
456 if ($0 ~ /^date: [0-9][- +\/0-9:]*;/) {
457 date = $2
458 if (date ~ /-/) {
459 # An ISO format date. Replace all "-"s with "/"s.
460 newdate = ""
461 while ((i = index(date, "-")) != 0) {
462 newdate = newdate substr(date, 1, i-1) "/"
463 date = substr(date, i+1)
465 date = newdate date
467 # Ignore any time zone; ChangeLog has no room for it.
468 time = substr($3, 1, 8)
469 author = substr($5, 1, length($5)-1)
470 printf "%s %s %s %s %c", filename, date, time, author, 13
471 next
473 if ($0 ~ /^(-----------*|===========*)$/) { print ""; next }
474 printf "%s%c", $0, 13
478 # Now each line is of the form
479 # FILENAME YYYY/MM/DD HH:MM:SS AUTHOR \rLOG
480 # where \r stands for a carriage return,
481 # and each line of the log is terminated by \r instead of \n.
482 # Sort the log entries, first by date+time (in reverse order),
483 # then by author, then by log entry, and finally by file name (just in case).
484 sort +1 -3r +3 +0 |
486 # Finally, reformat the sorted log entries.
487 $AWK '
488 BEGIN {
489 # Some awk variants do not understand "\r" or "\013", so we have to
490 # put a carriage return directly in the file.
491 CR=" " # <-- There is a single CR between the " chars here.
493 # Initialize the fullname and mailaddr associative arrays.
494 '"$initialize_fullname"'
495 '"$initialize_mailaddr"'
497 # Initialize indent string.
498 indent_string = ""
499 i = '"$indent"'
500 if (0 < '"$tabwidth"')
501 for (; '"$tabwidth"' <= i; i -= '"$tabwidth"')
502 indent_string = indent_string "\t"
503 while (1 <= i--)
504 indent_string = indent_string " "
506 # Set up date conversion tables.
507 # RCS uses a nice, clean, sortable format,
508 # but ChangeLog wants the traditional, ugly ctime format.
510 # January 1, 0 AD (Gregorian) was Saturday = 6
511 EPOCH_WEEKDAY = 6
512 # Of course, there was no 0 AD, but the algorithm works anyway.
514 w[0]="Sun"; w[1]="Mon"; w[2]="Tue"; w[3]="Wed"
515 w[4]="Thu"; w[5]="Fri"; w[6]="Sat"
517 '"$month_data"'
521 newlog = substr($0, 1 + index($0, CR))
523 # Ignore log entries prefixed by "#".
524 if (newlog ~ /^#/) { next }
526 if (Log != newlog || date != $2 || author != $4) {
528 # The previous log and this log differ.
530 # Print the old log.
531 if (date != "") '"$printlogline"'
533 # Logs that begin with "{clumpname} " should be grouped together,
534 # and the clumpname should be removed.
535 # Extract the new clumpname from the log header,
536 # and use it to decide whether to output a blank line.
537 newclumpname = ""
538 sep = "\n"
539 if (date == "") sep = ""
540 if (newlog ~ /^\{[^'"$tab"' }]*}['"$tab"' ]/) {
541 i = index(newlog, "}")
542 newclumpname = substr(newlog, 1, i)
543 while (substr(newlog, i+1) ~ /^['"$tab"' ]/) i++
544 newlog = substr(newlog, i+1)
545 if (clumpname == newclumpname) sep = ""
547 printf sep
548 clumpname = newclumpname
550 # Get ready for the next log.
551 Log = newlog
552 if (files != "")
553 for (i in filesknown)
554 filesknown[i] = 0
555 files = ""
557 if (date != $2 || author != $4) {
558 # The previous date+author and this date+author differ.
559 # Print the new one.
560 date = $2
561 author = $4
563 # Convert nice RCS date like "1992/01/03 00:03:44"
564 # into ugly ctime date like "Fri Jan 3 00:03:44 1992".
565 # Calculate day of week from Gregorian calendar.
566 i = index($2, "/")
567 year = substr($2, 1, i-1) + 0
568 monthday = substr($2, i+1)
569 i = index(monthday, "/")
570 month = substr(monthday, 1, i-1) + 0
571 day = substr(monthday, i+1) + 0
572 leap = 0
573 if (2 < month && year%4 == 0 && (year%100 != 0 || year%400 == 0)) leap = 1
574 days_since_Sunday_before_epoch = EPOCH_WEEKDAY + year * 365 + int((year + 3) / 4) - int((year + 99) / 100) + int((year + 399) / 400) + mo[month-1] + leap + day - 1
576 # Print "date fullname (email address)".
577 # Get fullname and email address from associative arrays;
578 # default to author and author@hostname if not in arrays.
579 if (fullname[author])
580 auth = fullname[author]
581 else
582 auth = author
583 printf "%s %s %2d %s %d %s ", w[days_since_Sunday_before_epoch%7], m[month-1], day, $3, year, auth
584 if (mailaddr[author])
585 printf "<%s>\n\n", mailaddr[author]
586 else
587 printf "<%s@%s>\n\n", author, "'"$hostname"'"
589 if (! filesknown[$1]) {
590 filesknown[$1] = 1
591 if (files == "") files = " " $1
592 else files = files ", " $1
595 END {
596 # Print the last log.
597 if (date != "") {
598 '"$printlogline"'
599 printf "\n"
602 ' &&
605 # Exit successfully.
607 exec rm -f $llogout $rlogout