summaryrefslogtreecommitdiffstats
path: root/extract-copyright-comments
blob: 87d0d3ba2ea814db7495e004aa8b0cad47da4d95 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
#
# Extract comments containing copyright notices from C/C++ files
#
# Copyright (C) 2020  Patrick McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -eu

LF='
'

bufc=
bufi=
c=

getc()
{
	if [ ${bufi} -ge ${bufc} ]; then
		c=''
	else
		eval "c=\${bufv_${bufi}}"
		bufi=$((${bufi} + 1))
	fi
}

extract_cxx_comment()
{
	local comment='//'

	while :; do
		getc
		case "${c}" in
			"${LF}")
				break
				;;
			'')
				printf '\tError: Unterminated C++ comment\n' \
					1>&2
				return 1
				;;
			*)
				comment="${comment}${c}"
				;;
		esac
	done

	if printf '%s' "${comment}" | grep -Eqi 'copyright|\(c\)'; then
		printf '\t%s\n' "${comment}"
	fi

	return 0
}

extract_c_comment()
{
	local comment='/*'
	local asterisk=false

	while :; do
		getc
		case "${c}" in
			'*')
				asterisk=true
				comment="${comment}${c}"
				;;
			'/')
				comment="${comment}${c}"
				if ${asterisk}; then
					break
				fi
				;;
			'')
				printf '\tError: Unterminated C comment\n' 1>&2
				return 1
				;;
			*)
				asterisk=false
				comment="${comment}${c}"
				;;
		esac
	done

	if printf '%s' "${comment}" | grep -Eqi 'copyright|\(c\)'; then
		printf '%s\n' "${comment}" | sed 's/^/\t/'
	fi

	return 0
}

extract()
{
	local fn="${1}"
	shift 1
	local quote=

	printf '%s\n' "${fn}"

	# Read file into array
	eval "$(awk -v FS='' -v j=0 -v squote="'" -v esc_squote="'\\\\''" '
		{
			for (i = 1; i <= NF; ++i) {
				sub(squote, esc_squote, $i);
				printf("bufv_%d=" squote "%s" squote "\n",
					j++, $i);
			};
			printf("bufv_%d=" squote "\n" squote "\n", j++);
		}
		END {
			printf("bufc=%d", j);
		}
		')"
	bufi=0

	while :; do
		getc
		case "${c}" in
			'/')
				getc
				case "${c}" in
					'/')
						extract_cxx_comment || return 1
						;;
					'*')
						extract_c_comment || return 1
						;;
				esac
				;;
			"'" | '"')
				quote="${c}"
				while :; do
					getc
					case "${c}" in
						"${quote}")
							break
							;;
						\\)
							# This doesn't
							# explicitly handle
							# octal, hexadecimal, or
							# Unicode sequences; but
							# it's good enough to
							# handle escaped quotes.
							getc
							;;
					esac
				done
				;;
			'')
				break
				;;
		esac
	done

	return 0
}

main()
{
	local f=

	if [ ${#} -eq 0 ]; then
		extract 'INPUT' || return 1
	else
		for f in "${@}"; do
			if [ x"${f}" = x'-' ]; then
				extract 'INPUT' || return 1
			else
				extract "${f}" 0<"${f}" || return 1
			fi
		done
	fi

	return 0
}

main "${@}"