OSDN Git Service

#xxxxx DTXViewerのプロジェクトを追加。
[dtxmania/dtxmania.git] / DTXViewerプロジェクト / @libpngソリューション / lpng1623 / scripts / dfn.awk
1 #!/bin/awk -f\r
2 # scripts/dfn.awk - process a .dfn file\r
3 #\r
4 # last changed in libpng version 1.5.19 - August 21, 2014\r
5 #\r
6 # Copyright (c) 2013-2014 Glenn Randers-Pehrson\r
7 #\r
8 # This code is released under the libpng license.\r
9 # For conditions of distribution and use, see the disclaimer\r
10 # and license in png.h\r
11 \r
12 # The output of this script is written to the file given by\r
13 # the variable 'out', which should be set on the command line.\r
14 # Error messages are printed to stdout and if any are printed\r
15 # the script will exit with error code 1.\r
16 \r
17 BEGIN{\r
18    out="/dev/null"       # as a flag\r
19    out_count=0           # count of output lines\r
20    err=0                 # set if an error occurred\r
21    sort=0                # sort the output\r
22    array[""]=""\r
23 }\r
24 \r
25 # The output file must be specified before any input:\r
26 NR==1 && out == "/dev/null" {\r
27    print "out=output.file must be given on the command line"\r
28    # but continue without setting the error code; this allows the\r
29    # script to be checked easily\r
30 }\r
31 \r
32 # Output can be sorted; two lines are recognized\r
33 $1 == "PNG_DFN_START_SORT"{\r
34    sort=0+$2\r
35    next\r
36 }\r
37 \r
38 $1 ~ /^PNG_DFN_END_SORT/{\r
39    # Do a very simple, slow, sort; notice that blank lines won't be\r
40    # output by this\r
41    for (entry in array) {\r
42       while (array[entry] != "") {\r
43          key = entry\r
44          value = array[key]\r
45          array[key] = ""\r
46 \r
47          for (alt in array) {\r
48             if (array[alt] != "" && alt < key) {\r
49                array[key] = value\r
50                value = array[alt]\r
51                key = alt\r
52                array[alt] = ""\r
53             }\r
54          }\r
55 \r
56          print value >out\r
57       }\r
58    }\r
59    sort=0\r
60    next\r
61 }\r
62 \r
63 /^[^"]*PNG_DFN *".*"[^"]*$/{\r
64    # A definition line, apparently correctly formatted; extract the\r
65    # definition then replace any doubled "" that remain with a single\r
66    # double quote.  Notice that the original doubled double quotes\r
67    # may have been split by tokenization\r
68    #\r
69    # Sometimes GCC splits the PNG_DFN lines; we know this has happened\r
70    # if the quotes aren't closed and must read another line.  In this\r
71    # case it is essential to reject lines that start with '#' because those\r
72    # are introduced #line directives.\r
73    orig=$0\r
74    line=$0\r
75    lineno=FNR\r
76    if (lineno == "") lineno=NR\r
77 \r
78    if (sub(/^[^"]*PNG_DFN *"/,"",line) != 1) {\r
79         print "line", lineno ": processing failed:"\r
80         print orig\r
81         err=1\r
82        next\r
83    } else {\r
84         ++out_count\r
85    }\r
86 \r
87    # Now examine quotes within the value:\r
88    #\r
89    #   @" - delete this and any following spaces\r
90    #   "@ - delete this and any preceding spaces\r
91    #   @' - replace this by a double quote\r
92    #\r
93    # This allows macro substitution by the C compiler thus:\r
94    #\r
95    #   #define first_name John\r
96    #   #define last_name Smith\r
97    #\r
98    #    PNG_DFN"#define name @'@" first_name "@ @" last_name "@@'"\r
99    #\r
100    # Might get C preprocessed to:\r
101    #\r
102    #   PNG_DFN "#define foo @'@" John "@ @" Smith "@@'"\r
103    #\r
104    # Which this script reduces to:\r
105    #\r
106    #    #define name "John Smith"\r
107    #\r
108    while (1) {\r
109       # While there is an @" remove it and the next "@\r
110       if (line ~ /@"/) {\r
111          if (line ~ /@".*"@/) {\r
112             # Do this special case first to avoid swallowing extra spaces\r
113             # before or after the @ stuff:\r
114             if (!sub(/@" *"@/, "", line)) {\r
115                # Ok, do it in pieces - there has to be a non-space between the\r
116                # two.  NOTE: really weird things happen if a leading @" is\r
117                # lost - the code will error out below (I believe).\r
118                if (!sub(/@" */, "", line) || !sub(/ *"@/, "", line)) {\r
119                   print "line", lineno, ": internal error:", orig\r
120                   exit 1\r
121                }\r
122             }\r
123          }\r
124 \r
125          # There is no matching "@.  Assume a split line\r
126          else while (1) {\r
127             if (getline nextline) {\r
128                # If the line starts with '#' it is a preprocesor line directive\r
129                # from cc -E; skip it:\r
130                if (nextline !~ /^#/) {\r
131                   line = line " " nextline\r
132                   break\r
133                }\r
134             } else {\r
135                # This is end-of-input - probably a missing "@ on the first line:\r
136                print "line", lineno ": unbalanced @\" ... \"@ pair"\r
137                err=1\r
138                next\r
139             }\r
140          }\r
141 \r
142          # Keep going until all the @" have gone\r
143          continue\r
144       }\r
145 \r
146       # Attempt to remove a trailing " (not preceded by '@') - if this can\r
147       # be done, stop now; if not assume a split line again\r
148       if (sub(/"[^"]*$/, "", line))\r
149          break\r
150 \r
151       # Read another line\r
152       while (1) {\r
153          if (getline nextline) {\r
154             if (nextline !~ /^#/) {\r
155                line = line " " nextline\r
156                # Go back to stripping @" "@ pairs\r
157                break\r
158             }\r
159          } else {\r
160             print "line", lineno ": unterminated PNG_DFN string"\r
161             err=1\r
162             next\r
163          }\r
164       }\r
165    }\r
166 \r
167    # Put any needed double quotes in (at the end, because these would otherwise\r
168    # interfere with the processing above.)\r
169    gsub(/@'/,"\"", line)\r
170 \r
171    # Remove any trailing spaces (not really required, but for\r
172    # editorial consistency\r
173    sub(/ *$/, "", line)\r
174 \r
175    # Remove trailing CR\r
176    sub(/\r$/, "", line)\r
177 \r
178    if (sort) {\r
179       if (split(line, parts) < sort) {\r
180          print "line", lineno ": missing sort field:", line\r
181          err=1\r
182       } else\r
183          array[parts[sort]] = line\r
184    }\r
185 \r
186    else\r
187       print line >out\r
188    next\r
189 }\r
190 \r
191 /PNG_DFN/{\r
192    print "line", NR, "incorrectly formatted PNG_DFN line:"\r
193    print $0\r
194    err = 1\r
195 }\r
196 \r
197 END{\r
198    if (out_count > 0 || err > 0)\r
199         exit err\r
200 \r
201    print "no definition lines found"\r
202    exit 1\r
203 }\r