Add support for determining BASE_EXT_STRING.
[metze/wireshark/wip.git] / tools / process-x11-fields.pl
1 #!/usr/bin/perl
2 #
3 # Script to convert "x11-fields" file, listing fields for
4 # X11 dissector, into header files declaring field-index
5 # values and field definitions for those fields.
6 #
7 # Copyright 2000, Christophe Tronche <ch.tronche[AT]computer.org>
8 #
9 # $Id$
10 #
11 # Wireshark - Network traffic analyzer
12 # By Gerald Combs <gerald@wireshark.org>
13 # Copyright 1998 Gerald Combs
14 #
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #
29
30 open(DECL, ">x11-declarations.h") || die;
31 open(REG, ">x11-register-info.h") || die;
32
33 sub add_generated_header {
34     my ($out) = @_;
35
36     print $out <<eot
37 /* Do not modify this file. */
38 /* It was automatically generated by $0. */
39 eot
40     ;
41     # Since this file is checked in, add its SVN revision
42     print $out "/* \$"."Id"."\$ */\n\n";
43
44     # Add license text
45     print $out <<eot
46 /*
47  * Copyright 2000, Christophe Tronche <ch.tronche[AT]computer.org>
48  *
49  * Wireshark - Network traffic analyzer
50  * By Gerald Combs <gerald[AT]wireshark.org>
51  * Copyright 1998 Gerald Combs
52  *
53  * This program is free software; you can redistribute it and/or modify
54  * it under the terms of the GNU General Public License as published by
55  * the Free Software Foundation; either version 2 of the License, or
56  * (at your option) any later version.
57  *
58  * This program is distributed in the hope that it will be useful,
59  * but WITHOUT ANY WARRANTY; without even the implied warranty of
60  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
61  * GNU General Public License for more details.
62  *
63  * You should have received a copy of the GNU General Public License along
64  * with this program; if not, write to the Free Software Foundation, Inc.,
65  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
66  */
67
68 eot
69     ;
70 }
71
72 add_generated_header(DECL);
73 add_generated_header(REG);
74
75 $prefix = '';
76 $subfieldStringLength = 0;
77
78 while(<>) {
79     s/#.*$//go;
80     next if /^\s*$/o;
81     s/^(\s*)//o;
82     $subfield = $1;
83
84     if (length $subfield != $subfieldStringLength) {
85         if (!length $subfield) {
86             $prefix = '';
87         } elsif (length $subfield > $subfieldStringLength) {
88             $prefix .= "$lastAbbrev.";
89         } else {
90             $prefix =~ s/^(.*)\.[^\.]+\.$/$1./o;
91         }
92         $subfieldStringLength = length $subfield;
93     }
94
95     @fields = split /\s+/o ;
96     if ($fields[0] eq '#') {
97         #
98         # If the line begins with "#", treat it as a comment, by
99         # ignoring it.
100         #
101         # (We don't support comments at the end of a line; that would
102         # require some more pain in our simple parser.)
103         #
104         next;
105     }
106     $abbrev = shift @fields;
107     $type = shift @fields;
108     $lastAbbrev = $abbrev;
109
110     $field = $prefix.$abbrev;
111
112     if ($fields[0] =~ /^\d+$/o) {
113         #
114         # This is presumably a Boolean bitfield, and this is the number
115         # of bits in the parent field.
116         #
117         $fieldDisplay = shift @fields;
118     } else {
119         #
120         # The next token is the base for the field.
121         #
122         $fieldDisplay = "BASE_".shift @fields;
123     }
124
125     if ($fields[0] eq 'VALS') {
126         #
127         # It's an enumerated field, with the value_string table having a
128         # name based on the field's name.
129         #
130         shift @fields;
131         $fieldStrings = "VALS(${abbrev}_vals)";
132         $fieldStrings =~ s/-/_/go;
133     } elsif ($fields[0] =~ /^VALS\(/o) {
134         #
135         # It's an enumerated field, with a specified name for the
136         # value_string table.
137         #
138         $fieldStrings = shift @fields;
139         $fieldStrings =~ s/\)/_vals\)/o;
140     } else {
141         #
142         # It's not an enumerated field.
143         #
144         $fieldStrings = 'NULL';
145     }
146
147     if ($fields[0] =~ /^0x/) {
148         #
149         # The next token looks like a bitmask for a bitfield.
150         #
151         $mask = shift @fields;
152     } else {
153         $mask = 0;
154     }
155
156     $rest = join(' ', @fields);
157     $longName = uc $name;
158     $longName = $rest if ($rest);
159     # Don't allow empty blurbs
160     $longName = $longName eq "" ? "NULL" : "\"$longName\"";
161
162     $variable = $field;
163     $variable =~ s/-/_/go;
164     $variable =~ s/\./_/go;
165
166     print DECL "static int hf_x11_$variable = -1;\n";
167
168     print REG <<END;
169 { &hf_x11_$variable, { "$abbrev", "x11.$field", FT_$type, $fieldDisplay, $fieldStrings, $mask, $longName, HFILL }},
170 END
171 }