Tizen 2.1 base
[external/libgpg-error.git] / src / mkheader.awk
1 # mkheader.awk
2 # Copyright (C) 2003, 2004 g10 Code GmbH
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of
7 # the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 #
18 # As a special exception, g10 Code GmbH gives unlimited permission to
19 # copy, distribute and modify the C source files that are the output
20 # of mkheader.awk.  You need not follow the terms of the GNU General
21 # Public License when using or distributing such scripts, even though
22 # portions of the text of mkheader.awk appear in them.  The GNU
23 # General Public License (GPL) does govern all other use of the material
24 # that constitutes the mkheader.awk program.
25 #
26 # Certain portions of the mkheader.awk source text are designed to be
27 # copied (in certain cases, depending on the input) into the output of
28 # mkheader.awk.  We call these the "data" portions.  The rest of the
29 # mkheader.awk source text consists of comments plus executable code
30 # that decides which of the data portions to output in any given case.
31 # We call these comments and executable code the "non-data" portions.
32 # mkheader.h never copies any of the non-data portions into its output.
33 #
34 # This special exception to the GPL applies to versions of mkheader.awk
35 # released by g10 Code GmbH.  When you make and distribute a modified version
36 # of mkheader.awk, you may extend this special exception to the GPL to
37 # apply to your modified version as well, *unless* your modified version
38 # has the potential to copy into its output some of the text that was the
39 # non-data portion of the version that you started with.  (In other words,
40 # unless your change moves or copies text from the non-data portions to the
41 # data portions.)  If your modification has such potential, you must delete
42 # any notice of this special exception to the GPL from your modified version.
43
44 # This script processes gpg-error.h.in in an awful way.
45 # Its input is, one after another, the content of the err-sources.h.in file,
46 # the err-codes.h.in file, the errnos.in file, and then gpg-error.h.in.
47 # There is nothing fancy about this.
48 #
49 # An alternative would be to use getline to get the content of the first three files,
50 # but then we need to pre-process gpg-error.h.in with configure to get
51 # at the full path of the files in @srcdir@.
52
53 BEGIN {
54   FS = "[\t]+";
55 # sources_nr holds the number of error sources.
56   sources_nr = 0;
57 # codes_nr holds the number of error codes.
58   codes_nr = 0;
59 # errnos_nr holds the number of system errors.
60   errnos_nr = 0;
61
62 # These variables walk us through our input.
63   sources_header = 1;
64   sources_body = 0;
65   between_sources_and_codes = 0;
66   codes_body = 0;
67   between_codes_and_errnos = 0;
68   errnos_body = 0;
69   gpg_error_h = 0;
70
71   print "/* Output of mkheader.awk.  DO NOT EDIT.  */";
72   print "";
73
74 }
75
76 sources_header {
77   if ($1 ~ /^[0123456789]+$/)
78     {
79       sources_header = 0;
80       sources_body = 1;
81     }      
82 }
83
84 sources_body {
85   sub (/\#.+/, "");
86   sub (/[       ]+$/, ""); # Strip trailing space and tab characters.
87
88   if (/^$/)
89     next;
90
91   if ($1 == "")
92     {
93       sources_body = 0;
94       between_sources_and_codes = 1;
95     }
96   else
97     {
98 # Remember the error source number and symbol of each error source.
99       sources_idx[sources_nr] = $1;
100       sources_sym[sources_nr] = $2;
101       sources_nr++;
102     }
103 }
104
105 between_sources_and_codes {
106   if ($1 ~ /^[0123456789]+$/)
107     {
108       between_sources_and_codes = 0;
109       codes_body = 1;
110     }      
111 }
112
113 codes_body {
114   sub (/\#.+/, "");
115   sub (/[       ]+$/, ""); # Strip trailing space and tab characters.
116
117   if (/^$/)
118     next;
119
120   if ($1 == "")
121     {
122       codes_body = 0;
123       between_codes_and_errnos = 1;
124     }
125   else
126     {
127 # Remember the error code number and symbol of each error source.
128       codes_idx[codes_nr] = $1;
129       codes_sym[codes_nr] = $2;
130       codes_nr++;
131     }
132 }
133
134 between_codes_and_errnos {
135   if ($1 ~ /^[0-9]/)
136     {
137       between_codes_and_errnos = 0;
138       errnos_body = 1;
139     }
140 }
141
142 errnos_body {
143   sub (/\#.+/, "");
144   sub (/[       ]+$/, ""); # Strip trailing space and tab characters.
145
146   if (/^$/)
147     next;
148
149   if ($1 !~ /^[0-9]/)
150     {
151 # Note that this assumes that gpg-error.h.in doesn't start with a digit.
152       errnos_body = 0;
153       gpg_error_h = 1;
154     }
155   else
156     {
157       errnos_idx[errnos_nr] = "GPG_ERR_SYSTEM_ERROR | " $1;
158       errnos_sym[errnos_nr] = "GPG_ERR_" $2;
159       errnos_nr++;
160     }
161 }
162
163 gpg_error_h {
164   if ($0 ~ /^@include err-sources/)
165     {
166       for (i = 0; i < sources_nr; i++)
167         {
168           print "    " sources_sym[i] " = " sources_idx[i] ",";
169 #         print "#define " sources_sym[i] " (" sources_idx[i] ")";
170         }
171     }
172   else if ($0 ~ /^@include err-codes/)
173     {
174       for (i = 0; i < codes_nr; i++)
175         {
176           print "    " codes_sym[i] " = " codes_idx[i] ",";
177 #         print "#define " codes_sym[i] " (" codes_idx[i] ")";
178         }
179     }
180   else if ($0 ~ /^@include errnos/)
181     {
182       for (i = 0; i < errnos_nr; i++)
183         {
184           print "    " errnos_sym[i] " = " errnos_idx[i] ",";
185 #         print "#define " errnos_sym[i] " (" errnos_idx[i] ")";
186         }
187     }
188   else
189     print;
190 }