Found by clang 3.0 (built from trunk): various implicit conversion from
[obnox/wireshark/wip.git] / docbook / dfilter2xml.pl
1 #!/usr/bin/perl
2 #
3 # Reads the display filter keyword dump produced by 'tshark -G' and
4 # formats it for a pod document. The pod document is then used to
5 # make a manpage
6 #
7 # STDIN is the wireshark glossary
8 # arg1 is the pod template file. The =insert_dfilter_table token
9 #      will be replaced by the pod-formatted glossary
10 # STDOUT is the output
11 #
12 # $Id$
13
14 %ftenum_names = (
15         'FT_NONE',              'No value',
16         'FT_PROTOCOL',          'Protocol',
17         'FT_BOOLEAN',           'Boolean',
18         'FT_UINT8',             'Unsigned 8-bit integer',
19         'FT_UINT16',            'Unsigned 16-bit integer',
20         'FT_UINT24',            'Unsigned 24-bit integer',
21         'FT_UINT32',            'Unsigned 32-bit integer',
22         'FT_UINT64',            'Unsigned 64-bit integer',
23         'FT_INT8',              'Signed 8-bit integer',
24         'FT_INT16',             'Signed 16-bit integer',
25         'FT_INT24',             'Signed 24-bit integer',
26         'FT_INT32',             'Signed 32-bit integer',
27         'FT_INT64',             'Signed 64-bit integer',
28         'FT_FLOAT',             'Single-precision floating point',
29         'FT_DOUBLE',            'Double-precision floating point',
30         'FT_ABSOLUTE_TIME',     'Date/Time stamp',
31         'FT_RELATIVE_TIME',     'Time duration',
32         'FT_STRING',            'String',
33         'FT_STRINGZ',           'NULL terminated string',
34         'FT_EBCDIC',            'EBCDIC string',
35         'FT_UINT_STRING',       'Length string pair',
36         'FT_ETHER',             '6-byte Hardware (MAC) Address',
37         'FT_BYTES',             'Byte array',
38         'FT_UINT_BYTES',        'Length byte array pair',
39         'FT_IPv4',              'IPv4 address',
40         'FT_IPv6',              'IPv6 address',
41         'FT_IPXNET',            'IPX network or server name',
42         'FT_FRAMENUM',          'Frame number',
43         'FT_PCRE',              'Perl Compatible Regular Expression',
44         'FT_GUID',              'Globally Unique Identifier',
45         'FT_OID',               'Object Identifier',
46 );
47
48 # Read all the data into memory
49 while (<STDIN>) {
50         next unless (/^([PF])/);
51
52         $record_type = $1;
53         # Strip the line from its line-end sequence
54         # chomp($_) won't work on Win32/CygWin as it leaves the '\r' character.
55         $_ =~ s/[\r\n]//g;
56         $_ =~ s/\&/\&amp\;/g;
57         $_ =~ s/\>/\&gt;/g;
58         $_ =~ s/\</\&lt\;/g;
59
60         # Store protocol information
61         if ($record_type eq 'P') {
62                 ($junk, $name, $abbrev) = split(/\t+/, $_);
63                 $proto_abbrev{$name} = $abbrev;
64         }
65         # Store header field information
66         else {
67                 ($junk, $name, $abbrev, $type, $parent, $blurb) =
68                         split(/\t+/, $_);
69                 push(@{$field_abbrev{$parent}}, $abbrev);
70                 $field_info{$abbrev} = [ $name, $type, $blurb ];
71         }
72 }
73
74 # if there was no input on stdin, bail out
75 if ($record_type ne 'P' and $record_type ne 'F') {
76         exit;
77 }
78
79 $template = shift(@ARGV);
80
81 open(TEMPLATE, $template) || die "Can't open $template for reading: $!\n";
82
83 while (<TEMPLATE>) {
84         if (/=insert_dfilter_table/) {
85                 &create_dfilter_table;
86         }
87         else {
88                 print;
89         }
90 }
91
92 close(TEMPLATE) || die "Can't close $template: $!\n";
93
94 sub create_dfilter_table {
95
96         print "<appendix id=\"AppFiltFields\"><title>Wireshark Display Filter Fields</title>\n";
97         $pn_counter = 1;
98
99         # Print each protocol
100         for $proto_name (sort keys %proto_abbrev) {
101
102                 $ns_proto_name = $proto_name;
103                 $ns_proto_name =~ s/\s//g;
104                 $ns_proto_name =~ s/\)//g;
105                 $ns_proto_name =~ s/\(//g;
106                 $ns_proto_name =~ s/_//g;
107                 $ns_proto_name =~ s/\+/plus/g;
108                 $ns_proto_name =~ s/\//slash/g;
109                 $ns_proto_name =~ s/,/comma/g;
110                 $ns_proto_name =~ s/:/colon/g;
111                 $ns_proto_name =~ s/'/apos/g;
112                 
113                 # The maximum token name length is apparently 44 characters. 
114                 # That's what NAMELEN is defined as in docbook 4.1, at least.
115
116                 if (length ($ns_proto_name) > 41) {  # "SID" and "TID" are prepended below
117                         $ns_proto_name = sprintf ("%s%04d", substr($ns_proto_name, 0,
118                                 37), $pn_counter);
119                         $pn_counter++;
120                 }
121
122                 print "<section id=\"SID$ns_proto_name\"><title>$proto_name ($proto_abbrev{$proto_name})</title>\n\n";
123
124                 print "<table id=\"TID$ns_proto_name\"><title>$proto_name ($proto_abbrev{$proto_name})</title>\n";
125                 print "<tgroup cols=\"4\">\n";
126 #               print "<colspec colnum=\"1\" colwidth=\"80pt\">\n";
127 #               print "<colspec colnum=\"2\" colwidth=\"80pt\"\n>";
128                 print "<thead>\n  <row>\n    ";
129                 print "<entry>Field</>\n    <entry>Field Name</>\n    <entry>Type</>\n    <entry>Description</>\n\n";
130
131                 print "  </row>\n</thead>\n<tbody>\n";
132
133                 # If this proto has children fields, print those
134                 if ($field_abbrev{$proto_abbrev{$proto_name}}) {
135
136                         for $field_abbrev (sort @{$field_abbrev{$proto_abbrev{$proto_name}}}) {
137
138                                 print "  <row>\n";
139                                 print "    <entry>$field_abbrev</entry>\n";
140                                 print "    <entry>", $field_info{$field_abbrev}[0], "</entry>\n";
141                                 print "    <entry>", $ftenum_names{$field_info{$field_abbrev}[1]}, "</entry>\n";
142                                 print "    <entry>", $field_info{$field_abbrev}[2], "</>\n";
143                                 print "  </row>\n\n";
144
145                         }
146
147                 }
148                 else {
149
150                         print "  <row>\n    <entry></entry>\n    <entry></entry>\n    <entry></entry><entry></entry>\n";
151                         print "  </row>\n";
152
153                 }
154
155                 print "</tbody></tgroup></table>\n";
156                 print "</section>\n\n";
157
158         }
159
160         print "</appendix>\n";
161
162 }