(lwlib_toolkit_type): New variable.
[bpt/emacs.git] / lib-src / rcs2log
1 #!/bin/sh
2
3 # RCS to ChangeLog generator
4
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.
8
9 # Ignore log entries that start with `#'.
10 # Clump together log entries that start with `{topic} ',
11 # where `topic' contains neither white space nor `}'.
12
13 # Author: Paul Eggert <eggert@twinsun.com>
14
15 # $Id: rcs2log,v 1.22 1995/04/30 15:34:52 eggert Exp $
16
17 # Copyright 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
18
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.
23 #
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.
28 #
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.
32
33 tab=' '
34 nl='
35 '
36
37 # Parse options.
38
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
50
51 while :
52 do
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;;
74 *)
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"*)
82 ;;
83 *)
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
91 ;;
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
104
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"
110
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
116 '
117
118
119 # Put rlog output into $rlogout.
120
121 # If no rlog options are given,
122 # log the revisions checked in since the first ChangeLog entry.
123 case $rlog_options in
124 '')
125 date=1970
126 if test -s ChangeLog
127 then
128 # Add 1 to seconds to avoid duplicating most recent log.
129 e='
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
152 i++
153 if (i == 12) {
154 i = 0
155 year++
156 }
157 }
158 }
159 }
160 }
161 printf "%d/%02d/%02d %02d:%02d:%02d\n", year,i+1,dd,hh,mm,ss
162 exit
163 }
164 '
165 d=`$AWK "$e" <ChangeLog` || exit
166 case $d in
167 ?*) date=$d
168 esac
169 fi
170 datearg="-d>$date"
171 esac
172
173 # If CVS is in use, examine its repository, not the normal RCS files.
174 if test ! -f CVS/Repository
175 then
176 rlog=rlog
177 repository=
178 else
179 rlog='cvs log'
180 repository=`sed 1q <CVS/Repository` || exit
181 case $repository in
182 /*) ;;
183 *) repository=${CVSROOT?}/$repository
184 esac
185 if test ! -d "$repository"
186 then
187 echo >&2 "$0: $repository: bad repository (see CVS/Repository)"
188 exit 1
189 fi
190 fi
191
192 # With no arguments, examine all files under the RCS directory.
193 case $# in
194 0)
195 case $repository in
196 '')
197 oldIFS=$IFS
198 IFS=$nl
199 case $recursive in
200 t)
201 RCSdirs=`find . -name RCS -type d -print`
202 filesFromRCSfiles='s|,v$||; s|/RCS/|/|; s|^\./||'
203 files=`
204 {
205 case $RCSdirs in
206 ?*) find $RCSdirs -type f -print
207 esac
208 find . -name '*,v' -print
209 } |
210 sort -u |
211 sed "$filesFromRCSfiles"
212 `;;
213 *)
214 files=
215 for file in RCS/.* RCS/* .*,v *,v
216 do
217 case $file in
218 RCS/. | RCS/..) continue;;
219 RCS/.\* | RCS/\* | .\*,v | \*,v) test -f "$file" || continue
220 esac
221 files=$files$nl$file
222 done
223 case $files in
224 '') exit 0
225 esac
226 esac
227 set x $files
228 shift
229 IFS=$oldIFS
230 esac
231 esac
232
233 llogout=$TMPDIR/rcs2log$$l
234 rlogout=$TMPDIR/rcs2log$$r
235 trap exit 1 2 13 15
236 trap "rm -f $llogout $rlogout; exit 1" 0
237
238 case $rlog_options in
239 ?*) $rlog $rlog_options ${1+"$@"} >$rlogout;;
240 '') $rlog "$datearg" ${1+"$@"} >$rlogout
241 esac || exit
242
243
244 # Get the full name of each author the logs mention, and set initialize_fullname
245 # to awk code that initializes the `fullname' awk associative array.
246 # Warning: foreign authors (i.e. not known in the passwd file) are mishandled;
247 # you have to fix the resulting output by hand.
248
249 initialize_fullname=
250 initialize_mailaddr=
251
252 case $loginFullnameMailaddrs in
253 ?*)
254 case $loginFullnameMailaddrs in
255 *\"* | *\\*)
256 sed 's/["\\]/\\&/g' >$llogout <<EOF || exit
257 $loginFullnameMailaddrs
258 EOF
259 loginFullnameMailaddrs=`cat $llogout`
260 esac
261
262 oldIFS=$IFS
263 IFS=$nl
264 for loginFullnameMailaddr in $loginFullnameMailaddrs
265 do
266 case $loginFullnameMailaddr in
267 *"$tab"*) IFS=$tab;;
268 *) IFS=:
269 esac
270 set x $loginFullnameMailaddr
271 login=$2
272 fullname=$3
273 mailaddr=$4
274 initialize_fullname="$initialize_fullname
275 fullname[\"$login\"] = \"$fullname\""
276 initialize_mailaddr="$initialize_mailaddr
277 mailaddr[\"$login\"] = \"$mailaddr\""
278 done
279 IFS=$oldIFS
280 esac
281
282 case $llogout in
283 ?*) sort -u -o $llogout <<EOF || exit
284 $logins
285 EOF
286 esac
287 output_authors='/^date: / {
288 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 ~ /^[^;]*;$/) {
289 print substr($5, 1, length($5)-1)
290 }
291 }'
292 authors=`
293 $AWK "$output_authors" <$rlogout |
294 case $llogout in
295 '') sort -u;;
296 ?*) sort -u | comm -23 - $llogout
297 esac
298 `
299 case $authors in
300 ?*)
301 cat >$llogout <<EOF || exit
302 $authors
303 EOF
304 initialize_author_script='s/["\\]/\\&/g; s/.*/author[\"&\"] = 1/'
305 initialize_author=`sed -e "$initialize_author_script" <$llogout`
306 awkscript='
307 BEGIN {
308 alphabet = "abcdefghijklmnopqrstuvwxyz"
309 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
310 '"$initialize_author"'
311 }
312 {
313 if (author[$1]) {
314 fullname = $5
315 if (fullname ~ /[0-9]+-[^(]*\([0-9]+\)$/) {
316 # Remove the junk from fullnames like "0000-Admin(0000)".
317 fullname = substr(fullname, index(fullname, "-") + 1)
318 fullname = substr(fullname, 1, index(fullname, "(") - 1)
319 }
320 if (fullname ~ /,[^ ]/) {
321 # Some sites put comma-separated junk after the fullname.
322 # Remove it, but leave "Bill Gates, Jr" alone.
323 fullname = substr(fullname, 1, index(fullname, ",") - 1)
324 }
325 abbr = index(fullname, "&")
326 if (abbr) {
327 a = substr($1, 1, 1)
328 A = a
329 i = index(alphabet, a)
330 if (i) A = substr(ALPHABET, i, 1)
331 fullname = substr(fullname, 1, abbr-1) A substr($1, 2) substr(fullname, abbr+1)
332 }
333
334 # Quote quotes and backslashes properly in full names.
335 # Do not use gsub; traditional awk lacks it.
336 quoted = ""
337 rest = fullname
338 for (;;) {
339 p = index(rest, "\\")
340 q = index(rest, "\"")
341 if (p) {
342 if (q && q<p) p = q
343 } else {
344 if (!q) break
345 p = q
346 }
347 quoted = quoted substr(rest, 1, p-1) "\\" substr(rest, p, 1)
348 rest = substr(rest, p+1)
349 }
350
351 printf "fullname[\"%s\"] = \"%s%s\"\n", $1, quoted, rest
352 author[$1] = 0
353 }
354 }
355 '
356
357 initialize_fullname=`
358 (cat /etc/passwd; ypmatch $authors passwd) 2>/dev/null |
359 $AWK -F: "$awkscript"
360 `$initialize_fullname
361 esac
362
363
364 # Function to print a single log line.
365 # We don't use awk functions, to stay compatible with old awk versions.
366 # `Log' is the log message (with \n replaced by \r).
367 # `files' contains the affected files.
368 printlogline='{
369
370 # Following the GNU coding standards, rewrite
371 # * file: (function): comment
372 # to
373 # * file (function): comment
374 if (Log ~ /^\([^)]*\): /) {
375 i = index(Log, ")")
376 files = files " " substr(Log, 1, i)
377 Log = substr(Log, i+3)
378 }
379
380 # If "label: comment" is too long, break the line after the ":".
381 sep = " "
382 if ('"$length"' <= '"$indent"' + 1 + length(files) + index(Log, CR)) sep = "\n" indent_string
383
384 # Print the label.
385 printf "%s*%s:", indent_string, files
386
387 # Print each line of the log, transliterating \r to \n.
388 while ((i = index(Log, CR)) != 0) {
389 logline = substr(Log, 1, i-1)
390 if (logline ~ /[^'"$tab"' ]/) {
391 printf "%s%s\n", sep, logline
392 } else {
393 print ""
394 }
395 sep = indent_string
396 Log = substr(Log, i+1)
397 }
398 }'
399
400 case $hostname in
401 '')
402 hostname=`(
403 hostname || uname -n || uuname -l || cat /etc/whoami
404 ) 2>/dev/null` || {
405 echo >&2 "$0: cannot deduce hostname"
406 exit 1
407 }
408 esac
409
410
411 # Process the rlog output, generating ChangeLog style entries.
412
413 # First, reformat the rlog output so that each line contains one log entry.
414 # Transliterate \n to \r so that multiline entries fit on a single line.
415 # Discard irrelevant rlog output.
416 $AWK <$rlogout '
417 BEGIN { repository = "'"$repository"'" }
418 /^RCS file:/ {
419 if (repository != "") {
420 filename = $3
421 if (substr(filename, 1, length(repository) + 1) == repository "/") {
422 filename = substr(filename, length(repository) + 2)
423 }
424 if (filename ~ /,v$/) {
425 filename = substr(filename, 1, length(filename) - 2)
426 }
427 }
428 }
429 /^Working file:/ { if (repository == "") filename = $3 }
430 /^date: /, /^(-----------*|===========*)$/ {
431 if ($0 ~ /^branches: /) { next }
432 if ($0 ~ /^date: [0-9][- +\/0-9:]*;/) {
433 date = $2
434 if (date ~ /-/) {
435 # An ISO format date. Replace all "-"s with "/"s.
436 newdate = ""
437 while ((i = index(date, "-")) != 0) {
438 newdate = newdate substr(date, 1, i-1) "/"
439 date = substr(date, i+1)
440 }
441 date = newdate date
442 }
443 # Ignore any time zone; ChangeLog has no room for it.
444 time = substr($3, 1, 8)
445 author = substr($5, 1, length($5)-1)
446 printf "%s %s %s %s %c", filename, date, time, author, 13
447 next
448 }
449 if ($0 ~ /^(-----------*|===========*)$/) { print ""; next }
450 printf "%s%c", $0, 13
451 }
452 ' |
453
454 # Now each line is of the form
455 # FILENAME YYYY/MM/DD HH:MM:SS AUTHOR \rLOG
456 # where \r stands for a carriage return,
457 # and each line of the log is terminated by \r instead of \n.
458 # Sort the log entries, first by date+time (in reverse order),
459 # then by author, then by log entry, and finally by file name (just in case).
460 sort +1 -3r +3 +0 |
461
462 # Finally, reformat the sorted log entries.
463 $AWK '
464 BEGIN {
465 # Some awk variants do not understand "\r" or "\013", so we have to
466 # put a carriage return directly in the file.
467 CR=" " # <-- There is a single CR between the " chars here.
468
469 # Initialize the fullname and mailaddr associative arrays.
470 '"$initialize_fullname"'
471 '"$initialize_mailaddr"'
472
473 # Initialize indent string.
474 indent_string = ""
475 i = '"$indent"'
476 if (0 < '"$tabwidth"')
477 for (; '"$tabwidth"' <= i; i -= '"$tabwidth"')
478 indent_string = indent_string "\t"
479 while (1 <= i--)
480 indent_string = indent_string " "
481
482 # Set up date conversion tables.
483 # RCS uses a nice, clean, sortable format,
484 # but ChangeLog wants the traditional, ugly ctime format.
485
486 # January 1, 0 AD (Gregorian) was Saturday = 6
487 EPOCH_WEEKDAY = 6
488 # Of course, there was no 0 AD, but the algorithm works anyway.
489
490 w[0]="Sun"; w[1]="Mon"; w[2]="Tue"; w[3]="Wed"
491 w[4]="Thu"; w[5]="Fri"; w[6]="Sat"
492
493 '"$month_data"'
494 }
495
496 {
497 newlog = substr($0, 1 + index($0, CR))
498
499 # Ignore log entries prefixed by "#".
500 if (newlog ~ /^#/) { next }
501
502 if (Log != newlog || date != $2 || author != $4) {
503
504 # The previous log and this log differ.
505
506 # Print the old log.
507 if (date != "") '"$printlogline"'
508
509 # Logs that begin with "{clumpname} " should be grouped together,
510 # and the clumpname should be removed.
511 # Extract the new clumpname from the log header,
512 # and use it to decide whether to output a blank line.
513 newclumpname = ""
514 sep = "\n"
515 if (date == "") sep = ""
516 if (newlog ~ /^\{[^'"$tab"' }]*}['"$tab"' ]/) {
517 i = index(newlog, "}")
518 newclumpname = substr(newlog, 1, i)
519 while (substr(newlog, i+1) ~ /^['"$tab"' ]/) i++
520 newlog = substr(newlog, i+1)
521 if (clumpname == newclumpname) sep = ""
522 }
523 printf sep
524 clumpname = newclumpname
525
526 # Get ready for the next log.
527 Log = newlog
528 if (files != "")
529 for (i in filesknown)
530 filesknown[i] = 0
531 files = ""
532 }
533 if (date != $2 || author != $4) {
534 # The previous date+author and this date+author differ.
535 # Print the new one.
536 date = $2
537 author = $4
538
539 # Convert nice RCS date like "1992/01/03 00:03:44"
540 # into ugly ctime date like "Fri Jan 3 00:03:44 1992".
541 # Calculate day of week from Gregorian calendar.
542 i = index($2, "/")
543 year = substr($2, 1, i-1) + 0
544 monthday = substr($2, i+1)
545 i = index(monthday, "/")
546 month = substr(monthday, 1, i-1) + 0
547 day = substr(monthday, i+1) + 0
548 leap = 0
549 if (2 < month && year%4 == 0 && (year%100 != 0 || year%400 == 0)) leap = 1
550 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
551
552 # Print "date fullname (email address)".
553 # Get fullname and email address from associative arrays;
554 # default to author and author@hostname if not in arrays.
555 if (fullname[author])
556 auth = fullname[author]
557 else
558 auth = author
559 printf "%s %s %2d %s %d %s ", w[days_since_Sunday_before_epoch%7], m[month-1], day, $3, year, auth
560 if (mailaddr[author])
561 printf "<%s>\n\n", mailaddr[author]
562 else
563 printf "<%s@%s>\n\n", author, "'"$hostname"'"
564 }
565 if (! filesknown[$1]) {
566 filesknown[$1] = 1
567 if (files == "") files = " " $1
568 else files = files ", " $1
569 }
570 }
571 END {
572 # Print the last log.
573 if (date != "") {
574 '"$printlogline"'
575 printf "\n"
576 }
577 }
578 ' &&
579
580
581 # Exit successfully.
582
583 exec rm -f $llogout $rlogout