aboutsummaryrefslogtreecommitdiff
path: root/doc/prog.sh
blob: 6a540980aadb32507ac04a7426115675251eac12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

# generate HTML for a program excerpt.
# first arg is file name
# second arg is awk pattern to match start line
# third arg is awk pattern to stop processing
#
# missing third arg means print one line
# third arg "END" means proces rest of file
# missing second arg means process whole file
#
# examples:
#
#	prog.sh foo.go                       # whole file
#	prog.sh foo.go "/^func.main/"        # signature of main
#	prog.sh foo.go "/^func.main/" "/^}/  # body of main
#
# non-blank lines are annotated with line number in file

# line numbers are printed %.2d to make them equal-width for nice formatting.
# the format gives a leading 0.  the format %2d gives a leading space but
# that appears to confuse sanjay's makehtml formatter into bungling quotes
# because it makes some lines look indented.

echo "<pre> <!-- $* -->"

case $# in
3)
	if test "$3" = "END"  # $2 to end of file
	then
		awk '
			function LINE() { printf("%.2d\t%s\n", NR, $0) }
			BEGIN { printing = 0 }
			'$2' { printing = 1; LINE(); getline }
			printing { if($0 ~ /./) { LINE() } else { print "" } }
		'
	else	# $2 through $3
		awk '
			function LINE() { printf("%.2d\t%s\n", NR, $0) }
			BEGIN { printing = 0 }
			'$2' { printing = 1; LINE(); getline }
			'$3' && printing { if(printing) {printing = 0; LINE(); exit} }
			printing { if($0 ~ /./) { LINE() } else { print "" } }
		'
	fi
	;;
2)	# one line
	awk '
		function LINE() { printf("%.2d\t%s\n", NR, $0) }
		'$2' { LINE(); getline; exit }
	'
	;;
1)	# whole file
	awk '
		function LINE() { printf("%.2d\t%s\n", NR, $0) }
		{ if($0 ~ /./) { LINE() } else { print "" } }
	'
	;;
*)
	echo >&2 usage: prog.sh file.go /func.main/ /^}/
esac <$1 |
sed '
	s/&/\&amp;/g
	s/"/\&quot;/g
	s/</\&lt;/g
	s/>/\&gt;/g
'

echo '</pre>'