File indexing completed on 2024-04-21 05:40:21

0001 #!/bin/awk
0002 #
0003 # Copyright (c) 2019, Michal Policht. This file is dually licensed under terms of
0004 # either WTFPL or BEER-WARE LICENSE. You may obtain the copy of WTFPL or BEER-WARE
0005 # LICENSE by googling it. NO WARRANTY. YOU WILL PAY ALL COSTS FOR ANY REPAIRS.
0006 #
0007 # Script deletes lines starting with "//(c):QUALIFIER" or "#(c):QUALIFIER" and
0008 # empty lines above and below.
0009 #
0010 # usage: awk [-v comment="type"] [-v qualifier="QUALIFIER"] -f striplic.awk [file1 [file2 ... [fileN]]]
0011 # example: awk -v comment="hash" -v qualifier="MYID" -f striplic.awk file1.cpp file1.hpp
0012 #
0013 # parameters:
0014 # qualifier - copyright notice qualifier. Value is appended to search pattern.
0015 # comment - comment type. Following values are available:
0016 #           "dslash" - "//(c):QUALIFIER" pattern is used (default).
0017 #           "hash" - "#(c):QUALIFIER" pattern is used.
0018 #           "xml" - "<!--(c):QUALIFIER" pattern is used.
0019 #
0020 # required tools: awk.
0021 
0022 BEGIN {
0023     if (comment == "hash")
0024         lic_comment = "^#\\(c\\)" qualifier ":"
0025     else if (comment == "xml")
0026         lic_comment = "^<!--\\(c\\)" qualifier ":"
0027     else
0028         lic_comment = "^//\\(c\\)" qualifier ":"
0029     nl_buf = ""; # New lines buffer.
0030 }
0031 
0032 # New line (CR+LF or LF).
0033 /^\r?$/ {
0034     nl_buf = nl_buf $0 ORS; # Might consider using RT instead of ORS, but RT is gawk specific.
0035 }
0036 
0037 # Everything except new line
0038 ! /^\r?$/ {
0039     if (match($0, lic_comment))
0040         nl_buf = ""; # New lines above license comment are eaten.
0041     else {
0042         print nl_buf $0;
0043         nl_buf = "";
0044     }
0045 }