(server-switch-buffer): Make first arg optional too;
[emacs.git] / lib-src / rcs2log
blob831010e77c7b016d12b48806cbc968e7c2ad0109
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.20 1995/03/21 23:14:41 eggert Exp eggert $
17 # Copyright 1992, 1993, 1994, 1995 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 mailaddresses 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 # Log into $rlogout the revisions checked in since the first ChangeLog entry.
121 date=1970
122 if test -s ChangeLog
123 then
124 # Add 1 to seconds to avoid duplicating most recent log.
126 /^... ... [ 0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9]+ /{
127 '"$month_data"'
128 year = $5
129 for (i=0; i<=11; i++) if (m[i] == $2) break
130 dd = $3
131 hh = substr($0,12,2)
132 mm = substr($0,15,2)
133 ss = substr($0,18,2)
134 ss++
135 if (ss == 60) {
136 ss = 0
137 mm++
138 if (mm == 60) {
139 mm = 0
140 hh++
141 if (hh == 24) {
142 hh = 0
143 dd++
144 monthdays = mo[i+1] - mo[i]
145 if (i == 1 && year%4 == 0 && (year%100 != 0 || year%400 == 0)) monthdays++
146 if (dd == monthdays + 1) {
147 dd = 1
149 if (i == 12) {
150 i = 0
151 year++
157 printf "%d/%02d/%02d %02d:%02d:%02d\n", year, i+1, dd, hh, mm, ss
158 exit
161 d=`$AWK "$e" <ChangeLog` || exit
162 case $d in
163 ?*) date=$d
164 esac
166 datearg="-d>$date"
168 repository=
169 rlog=rlog
170 case $CVSROOT in
172 if test -d "$CVSROOT" && test -f CVS/Repository
173 then
174 r=`cat <CVS/Repository` || exit
175 if test -d "$CVSROOT/$r"
176 then
177 repository=$CVSROOT/$r
178 rlog='cvs log'
181 esac
183 # With no arguments, examine all files under the RCS directory.
184 case $# in
186 case $repository in
188 oldIFS=$IFS
189 IFS=$nl
190 case $recursive in
192 RCSdirs=`find . -name RCS -type d -print`
193 filesFromRCSfiles='s|,v$||; s|/RCS/|/|; s|^\./||'
194 files=`
196 case $RCSdirs in
197 ?*) find $RCSdirs -type f -print
198 esac
199 find . -name '*,v' -print
201 sort -u |
202 sed "$filesFromRCSfiles"
205 files=
206 for file in RCS/.* RCS/* .*,v *,v
208 case $file in
209 RCS/. | RCS/..) continue;;
210 RCS/.\* | RCS/\* | .\*,v | \*,v) test -f "$file" || continue
211 esac
212 files=$files$nl$file
213 done
214 case $files in
215 '') exit 0
216 esac
217 esac
218 set x $files
219 shift
220 IFS=$oldIFS
221 esac
222 esac
224 llogout=$TMPDIR/rcs2log$$l
225 rlogout=$TMPDIR/rcs2log$$r
226 trap exit 1 2 13 15
227 trap "rm -f $llogout $rlogout; exit 1" 0
229 $rlog "$datearg" $rlog_options ${1+"$@"} >$rlogout || exit
232 # Get the full name of each author the logs mention, and set initialize_fullname
233 # to awk code that initializes the `fullname' awk associative array.
234 # Warning: foreign authors (i.e. not known in the passwd file) are mishandled;
235 # you have to fix the resulting output by hand.
237 initialize_fullname=
238 initialize_mailaddr=
240 case $loginFullnameMailaddrs in
242 case $loginFullnameMailaddrs in
243 *\"* | *\\*)
244 sed 's/["\\]/\\&/g' >$llogout <<EOF || exit
245 $loginFullnameMailaddrs
247 loginFullnameMailaddrs=`cat $llogout`
248 esac
250 oldIFS=$IFS
251 IFS=$nl
252 for loginFullnameMailaddr in $loginFullnameMailaddrs
254 case $loginFullnameMailaddr in
255 *"$tab"*) IFS=$tab;;
256 *) IFS=:
257 esac
258 set x $loginFullnameMailaddr
259 login=$2
260 fullname=$3
261 mailaddr=$4
262 initialize_fullname="$initialize_fullname
263 fullname[\"$login\"] = \"$fullname\""
264 initialize_mailaddr="$initialize_mailaddr
265 mailaddr[\"$login\"] = \"$mailaddr\""
266 done
267 IFS=$oldIFS
268 esac
270 case $llogout in
271 ?*) sort -u -o $llogout <<EOF || exit
272 $logins
274 esac
275 output_authors='/^date: / {
276 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 ~ /^[^;]*;$/) {
277 print substr($5, 1, length($5)-1)
280 authors=`
281 $AWK "$output_authors" <$rlogout |
282 case $llogout in
283 '') sort -u;;
284 ?*) sort -u | comm -23 - $llogout
285 esac
287 case $authors in
289 cat >$llogout <<EOF || exit
290 $authors
292 initialize_author_script='s/["\\]/\\&/g; s/.*/author[\"&\"] = 1/'
293 initialize_author=`sed -e "$initialize_author_script" <$llogout`
294 awkscript='
295 BEGIN {
296 alphabet = "abcdefghijklmnopqrstuvwxyz"
297 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
298 '"$initialize_author"'
301 if (author[$1]) {
302 fullname = $5
303 if (fullname ~ /[0-9]+-[^(]*\([0-9]+\)$/) {
304 # Remove the junk from fullnames like "0000-Admin(0000)".
305 fullname = substr(fullname, index(fullname, "-") + 1)
306 fullname = substr(fullname, 1, index(fullname, "(") - 1)
308 if (fullname ~ /,[^ ]/) {
309 # Some sites put comma-separated junk after the fullname.
310 # Remove it, but leave "Bill Gates, Jr" alone.
311 fullname = substr(fullname, 1, index(fullname, ",") - 1)
313 abbr = index(fullname, "&")
314 if (abbr) {
315 a = substr($1, 1, 1)
316 A = a
317 i = index(alphabet, a)
318 if (i) A = substr(ALPHABET, i, 1)
319 fullname = substr(fullname, 1, abbr-1) A substr($1, 2) substr(fullname, abbr+1)
322 # Quote quotes and backslashes properly in full names.
323 # Do not use gsub; traditional awk lacks it.
324 quoted = ""
325 rest = fullname
326 for (;;) {
327 p = index(rest, "\\")
328 q = index(rest, "\"")
329 if (p) {
330 if (q && q<p) p = q
331 } else {
332 if (!q) break
333 p = q
335 quoted = quoted substr(rest, 1, p-1) "\\" substr(rest, p, 1)
336 rest = substr(rest, p+1)
339 printf "fullname[\"%s\"] = \"%s%s\"\n", $1, quoted, rest
340 author[$1] = 0
345 initialize_fullname=`
346 (cat /etc/passwd; ypmatch $authors passwd) 2>/dev/null |
347 $AWK -F: "$awkscript"
348 `$initialize_fullname
349 esac
352 # Function to print a single log line.
353 # We don't use awk functions, to stay compatible with old awk versions.
354 # `Log' is the log message (with \n replaced by \r).
355 # `files' contains the affected files.
356 printlogline='{
358 # Following the GNU coding standards, rewrite
359 # * file: (function): comment
360 # to
361 # * file (function): comment
362 if (Log ~ /^\([^)]*\): /) {
363 i = index(Log, ")")
364 files = files " " substr(Log, 1, i)
365 Log = substr(Log, i+3)
368 # If "label: comment" is too long, break the line after the ":".
369 sep = " "
370 if ('"$length"' <= '"$indent"' + 1 + length(files) + index(Log, CR)) sep = "\n" indent_string
372 # Print the label.
373 printf "%s*%s:", indent_string, files
375 # Print each line of the log, transliterating \r to \n.
376 while ((i = index(Log, CR)) != 0) {
377 logline = substr(Log, 1, i-1)
378 if (logline ~ /[^'"$tab"' ]/) {
379 printf "%s%s\n", sep, logline
380 } else {
381 print ""
383 sep = indent_string
384 Log = substr(Log, i+1)
388 case $hostname in
390 hostname=`(
391 hostname || uname -n || uuname -l || cat /etc/whoami
392 ) 2>/dev/null` || {
393 echo >&2 "$0: cannot deduce hostname"
394 exit 1
396 esac
399 # Process the rlog output, generating ChangeLog style entries.
401 # First, reformat the rlog output so that each line contains one log entry.
402 # Transliterate \n to \r so that multiline entries fit on a single line.
403 # Discard irrelevant rlog output.
404 $AWK <$rlogout '
405 BEGIN { repository = "'"$repository"'" }
406 /^RCS file:/ {
407 if (repository != "") {
408 filename = $3
409 if (substr(filename, 1, length(repository) + 1) == repository "/") {
410 filename = substr(filename, length(repository) + 2)
412 if (filename ~ /,v$/) {
413 filename = substr(filename, 1, length(filename) - 2)
417 /^Working file:/ { if (repository == "") filename = $3 }
418 /^date: /, /^(-----------*|===========*)$/ {
419 if ($0 ~ /^branches: /) { next }
420 if ($0 ~ /^date: [0-9][- +\/0-9:]*;/) {
421 date = $2
422 if (date ~ /-/) {
423 # An ISO format date. Replace all "-"s with "/"s.
424 newdate = ""
425 while ((i = index(date, "-")) != 0) {
426 newdate = newdate substr(date, 1, i-1) "/"
427 date = substr(date, i+1)
429 date = newdate date
431 # Ignore any time zone; ChangeLog has no room for it.
432 time = substr($3, 1, 8)
433 author = substr($5, 1, length($5)-1)
434 printf "%s %s %s %s %c", filename, date, time, author, 13
435 next
437 if ($0 ~ /^(-----------*|===========*)$/) { print ""; next }
438 printf "%s%c", $0, 13
442 # Now each line is of the form
443 # FILENAME YYYY/MM/DD HH:MM:SS AUTHOR \rLOG
444 # where \r stands for a carriage return,
445 # and each line of the log is terminated by \r instead of \n.
446 # Sort the log entries, first by date+time (in reverse order),
447 # then by author, then by log entry, and finally by file name (just in case).
448 sort +1 -3r +3 +0 |
450 # Finally, reformat the sorted log entries.
451 $AWK '
452 BEGIN {
453 # Some awks do not understand "\r" or "\013", so we have to
454 # put a carriage return directly in the file.
455 CR=" " # <-- There is a single CR between the " chars here.
457 # Initialize the fullname and mailaddr associative arrays.
458 '"$initialize_fullname"'
459 '"$initialize_mailaddr"'
461 # Initialize indent string.
462 indent_string = ""
463 i = '"$indent"'
464 if (0 < '"$tabwidth"')
465 for (; '"$tabwidth"' <= i; i -= '"$tabwidth"')
466 indent_string = indent_string "\t"
467 while (1 <= i--)
468 indent_string = indent_string " "
470 # Set up date conversion tables.
471 # RCS uses a nice, clean, sortable format,
472 # but ChangeLog wants the traditional, ugly ctime format.
474 # January 1, 0 AD (Gregorian) was Saturday = 6
475 EPOCH_WEEKDAY = 6
476 # Of course, there was no 0 AD, but the algorithm works anyway.
478 w[0]="Sun"; w[1]="Mon"; w[2]="Tue"; w[3]="Wed"
479 w[4]="Thu"; w[5]="Fri"; w[6]="Sat"
481 '"$month_data"'
485 newlog = substr($0, 1 + index($0, CR))
487 # Ignore log entries prefixed by "#".
488 if (newlog ~ /^#/) { next }
490 if (Log != newlog || date != $2 || author != $4) {
492 # The previous log and this log differ.
494 # Print the old log.
495 if (date != "") '"$printlogline"'
497 # Logs that begin with "{clumpname} " should be grouped together,
498 # and the clumpname should be removed.
499 # Extract the new clumpname from the log header,
500 # and use it to decide whether to output a blank line.
501 newclumpname = ""
502 sep = "\n"
503 if (date == "") sep = ""
504 if (newlog ~ /^\{[^'"$tab"' }]*}['"$tab"' ]/) {
505 i = index(newlog, "}")
506 newclumpname = substr(newlog, 1, i)
507 while (substr(newlog, i+1) ~ /^['"$tab"' ]/) i++
508 newlog = substr(newlog, i+1)
509 if (clumpname == newclumpname) sep = ""
511 printf sep
512 clumpname = newclumpname
514 # Get ready for the next log.
515 Log = newlog
516 if (files != "")
517 for (i in filesknown)
518 filesknown[i] = 0
519 files = ""
521 if (date != $2 || author != $4) {
522 # The previous date+author and this date+author differ.
523 # Print the new one.
524 date = $2
525 author = $4
527 # Convert nice RCS date like "1992/01/03 00:03:44"
528 # into ugly ctime date like "Fri Jan 3 00:03:44 1992".
529 # Calculate day of week from Gregorian calendar.
530 i = index($2, "/")
531 year = substr($2, 1, i-1) + 0
532 monthday = substr($2, i+1)
533 i = index(monthday, "/")
534 month = substr(monthday, 1, i-1) + 0
535 day = substr(monthday, i+1) + 0
536 leap = 0
537 if (2 < month && year%4 == 0 && (year%100 != 0 || year%400 == 0)) leap = 1
538 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
540 # Print "date fullname (email address)".
541 # Get fullname and email address from associative arrays;
542 # default to author and author@hostname if not in arrays.
543 if (fullname[author])
544 auth = fullname[author]
545 else
546 auth = author
547 printf "%s %s %2d %s %d %s ", w[days_since_Sunday_before_epoch%7], m[month-1], day, $3, year, auth
548 if (mailaddr[author])
549 printf "<%s>\n\n", mailaddr[author]
550 else
551 printf "<%s@%s>\n\n", author, "'"$hostname"'"
553 if (! filesknown[$1]) {
554 filesknown[$1] = 1
555 if (files == "") files = " " $1
556 else files = files ", " $1
559 END {
560 # Print the last log.
561 if (date != "") {
562 '"$printlogline"'
563 printf "\n"
566 ' &&
569 # Exit successfully.
571 exec rm -f $llogout $rlogout