about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2022-07-08 22:41:31 -0500
committerCase Duckworth2022-07-08 22:41:31 -0500
commit0d881509ee9918d52a47f64717eab75c5a2d5c7a (patch)
tree369a6ea885c7a8dac4b2ea9a3807ee261eceaf9a
parentAllow for simple catting of a license (diff)
downloadlicensor-0d881509ee9918d52a47f64717eab75c5a2d5c7a.tar.gz
licensor-0d881509ee9918d52a47f64717eab75c5a2d5c7a.zip
Refactor license_convert v1.0.1
-rwxr-xr-xlicensor162
1 files changed, 80 insertions, 82 deletions
diff --git a/licensor b/licensor index 5e784bb..3e0e065 100755 --- a/licensor +++ b/licensor
@@ -182,100 +182,98 @@ put() { printf '%s\n' "$*"; }
182log() { $_quiet || put "$PRGN: $*" >&2; } 182log() { $_quiet || put "$PRGN: $*" >&2; }
183 183
184license_convert() { 184license_convert() {
185 copyright="$1" # "Copyright (C) 2022 Case Duckworth <acdw@acdw.net>" 185 copyright="$1"
186 show_optional="${2:-0}" # 0 186 show_optional="${2:-0}"
187 fold_output="${3:-1}" # 1 187 fold_output="${3:-1}"
188 fold_width="${4:-70}" # 70 188 fold_width="${4:-70}"
189 awk ' 189 awk 'BEGIN {
190BEGIN {
191 foldOutput = '"$fold_output"' 190 foldOutput = '"$fold_output"'
192 foldWidth = '"$fold_width"' 191 foldWidth = '"$fold_width"'
193 showOptional = '"$show_optional"' 192 showOptional = '"$show_optional"'
194 copyright = "'"$copyright"'" 193 FIELDS["copyright"] = "'"$copyright"'"
194 optional = 0
195 buffer = ""
196 begOptRx = showOptional ? "<<beginOptional[^>]*>>" : "<<beginOptional[^>]*>>.*"
197 endOptRx = showOptional ? "<<endOptional[^>]*>>" : ".*<<endOptional[^>]*>>"
195} 198}
196{ buf = buf "\n" $0 } 199
197END { 200/<<var.*>>/ {
198 split(buf, b, "") 201 match($0, /<<var.*>>/)
199 c = 1 202 split(substr($0, RSTART + 6, RLENGTH - 8), _rule, ";")
200 out = "" 203 for (r in _rule) {
201 optstr = "" 204 key = substr(_rule[r], 1, index(_rule[r], "=") - 1)
202 opt = 0 205 val = substr(_rule[r], index(_rule[r], "=") + 1)
203 while (c <= length(b)) { 206 rule[key] = val
204 if (b[c] == "<" && b[c + 1] == "<") { 207 }
205 tok = "" 208 for (r in rule) {
206 c += 2 209 quoted = match(rule[r], /^".*"$/)
207 closed = 0 210 if (quoted) {
208 while (! closed) { 211 rule[r] = substr(rule[r], RSTART + 1, RLENGTH - 2)
209 tok = tok b[c++]
210 if (b[c] == "<" && b[c + 1] == "<") {
211 closed--
212 c += 2
213 if (closed < 0) {
214 closed = 0
215 }
216 }
217 if (b[c] == ">" && b[c + 1] == ">") {
218 closed++
219 c += 2
220 }
221 }
222 if (tok ~ /^beginOptional/) {
223 opt = 1
224 } else if (tok == "endOptional") {
225 if (showOptional) {
226 out = out optstr
227 }
228 opt = 0
229 optstr = ""
230 } else if (tok ~ /^var/) {
231 split(tok, ta, ";")
232 name = ""
233 original = ""
234 for (a in ta) {
235 if (ta[a] ~ "^name") {
236 match(ta[a], /=".*/)
237 name = substr(ta[a], RSTART + 2, RLENGTH - 3)
238 } else if (ta[a] ~ "^original") {
239 match(ta[a], /=".*/)
240 original = substr(ta[a], RSTART + 2, RLENGTH - 3)
241 } else if (ta[a] ~ "^match") {
242 # currently not used
243 }
244 }
245 if (name == "copyright") {
246 out = out copyright
247 } else {
248 out = out original
249 }
250 }
251 continue
252 } 212 }
253 if (opt) { 213 # print r, rule[r]
254 optstr = optstr b[c++] 214 }
255 } else { 215 name = rule["name"]
256 out = out b[c++] 216 # print FIELDS[name]
217 sub(/<<var.*>>/, (FIELDS[name] ? FIELDS[name] : rule["original"]), $0)
218}
219
220/<<beginOptional.*>>/ {
221 optional = 1
222 if (match($0,/<<beginOptional.*>>.*<<endOptional.*>>/)) {
223 optional = 0
224 if (! showOptional) {
225 sub(/<<beginOptional.*>>.*<<endOptional.*>>/, "", $0)
257 } 226 }
258 } 227 }
228 sub(begOptRx, "", $0)
229}
230
231/<<endOptional.*>>/ {
232 sub(endOptRx, "", $0)
233 optional = 0
234}
235
236optional && ! showOptional {
237 next
238}
239
240{
241 bufput()
242}
243
244END {
259 # "Fold" the output 245 # "Fold" the output
260 if (foldOutput) { 246 if (foldOutput) {
261 split(out, oa, "\n") 247 buffer = fold(buffer, foldWidth)
262 out = "" 248 }
263 for (l in oa) { 249 print buffer
264 split(oa[l], la, FS) 250}
265 lc = 0 251
266 for (w in la) { 252
267 lc += length(la[w]) + 1 253function bufput(str, sep)
268 if (lc >= foldWidth) { 254{
269 out = out "\n" la[w] " " 255 buffer = buffer (buffer ? (sep ? sep : "\n") : "") (str ? str : $0)
270 lc = length(la[w]) + 1 256}
271 } else { 257
272 out = out la[w] " " 258function fold(out, foldWidth)
273 } 259{
260 split(out, oa, "\n")
261 out = ""
262 for (l in oa) {
263 split(oa[l], la, FS)
264 lc = 0
265 for (w in la) {
266 lc += length(la[w]) + 1
267 if (lc >= foldWidth) {
268 out = out "\n" la[w] " "
269 lc = length(la[w]) + 1
270 } else {
271 out = out la[w] " "
274 } 272 }
275 out = out "\n"
276 } 273 }
274 out = out "\n"
277 } 275 }
278 print out 276 return out
279} 277}
280' 278'
281} 279}