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# Instructions for using this script are in epan/dissectors/README.X11
8#
9# Copyright 2000, Christophe Tronche <ch.tronche[AT]computer.org>
10#
11# Wireshark - Network traffic analyzer
12# By Gerald Combs <gerald@wireshark.org>
13# Copyright 1998 Gerald Combs
14#
15# SPDX-License-Identifier: GPL-2.0-or-later
16#
17
18use File::Spec;
19
20my $srcdir = shift;
21die "'$srcdir' is not a directory" unless -d $srcdir;
22
23open(DECL, "> $srcdir/x11-declarations.h") || die;
24open(REG, "> $srcdir/x11-register-info.h") || die;
25
26my $script_name = File::Spec->abs2rel ($0,  $srcdir);
27
28sub add_generated_header {
29    my ($out) = @_;
30
31    print $out <<eot
32/* Do not modify this file. */
33/* It was automatically generated by $script_name. */
34eot
35    ;
36
37    # Add license text
38    print $out <<eot
39/*
40 * Copyright 2000, Christophe Tronche <ch.tronche[AT]computer.org>
41 *
42 * Wireshark - Network traffic analyzer
43 * By Gerald Combs <gerald[AT]wireshark.org>
44 * Copyright 1998 Gerald Combs
45 *
46 * SPDX-License-Identifier: GPL-2.0-or-later
47 */
48
49eot
50    ;
51}
52
53add_generated_header(DECL);
54add_generated_header(REG);
55
56$prefix = '';
57$subfieldStringLength = 0;
58
59while(<>) {
60    s/#.*$//go;
61    next if /^\s*$/o;
62    s/^(\s*)//o;
63    $subfield = $1;
64
65    if (length $subfield != $subfieldStringLength) {
66        if (!length $subfield) {
67            $prefix = '';
68        } elsif (length $subfield > $subfieldStringLength) {
69            $prefix .= "$lastAbbrev.";
70        } else {
71            $prefix =~ s/^(.*)\.[^\.]+\.$/$1./o;
72        }
73        $subfieldStringLength = length $subfield;
74    }
75
76    @fields = split /\s+/o ;
77    if ($fields[0] eq '#') {
78        #
79        # If the line begins with "#", treat it as a comment, by
80        # ignoring it.
81        #
82        # (We don't support comments at the end of a line; that would
83        # require some more pain in our simple parser.)
84        #
85        next;
86    }
87    $abbrev = shift @fields;
88    $type = shift @fields;
89    $lastAbbrev = $abbrev;
90
91    $field = $prefix.$abbrev;
92
93    if ($fields[0] =~ /^\d+$/o) {
94        #
95        # This is presumably a Boolean bitfield, and this is the number
96        # of bits in the parent field.
97        #
98        $fieldDisplay = shift @fields;
99    } else {
100        #
101        # The next token is the base for the field.
102        #
103        $fieldDisplay = "BASE_".shift @fields;
104    }
105
106    if ($fields[0] eq 'VALS') {
107        #
108        # It's an enumerated field, with the value_string table having a
109        # name based on the field's name.
110        #
111        shift @fields;
112        $fieldStrings = "VALS(${abbrev}_vals)";
113        $fieldStrings =~ s/-/_/go;
114    } elsif ($fields[0] =~ /^VALS\(/o) {
115        #
116        # It's an enumerated field, with a specified name for the
117        # value_string table.
118        #
119        $fieldStrings = shift @fields;
120        $fieldStrings =~ s/\)/_vals\)/o;
121    } else {
122        #
123        # It's not an enumerated field.
124        #
125        $fieldStrings = 'NULL';
126    }
127
128    if ($fields[0] =~ /^0x/) {
129        #
130        # The next token looks like a bitmask for a bitfield.
131        #
132        $mask = shift @fields;
133    } else {
134        $mask = 0;
135    }
136
137    $rest = join(' ', @fields);
138    $longName = uc $name;
139    $longName = $rest if ($rest);
140    # Don't allow empty blurbs
141    $longName = $longName eq "" ? "NULL" : "\"$longName\"";
142
143    $variable = $field;
144    $variable =~ s/-/_/go;
145    $variable =~ s/\./_/go;
146
147    print DECL "static int hf_x11_$variable = -1;\n";
148
149    print REG <<END;
150{ &hf_x11_$variable, { "$abbrev", "x11.$field", FT_$type, $fieldDisplay, $fieldStrings, $mask, $longName, HFILL }},
151END
152}
153
154#
155#  Editor modelines
156#
157#  Local Variables:
158#  c-basic-offset: 4
159#  tab-width: 8
160#  indent-tabs-mode: nil
161#  End:
162#
163#  ex: set shiftwidth=4 tabstop=8 expandtab:
164#  :indentSize=4:tabSize=8:noTabs=true:
165#
166