Ethereal->Wireshark
[obnox/wireshark/wip.git] / docbook / dfilter2xml.pl
1 #!/usr/bin/perl
2 #
3 # Reads the display filter keyword dump produced by 'ethereal -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 ethereal 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_DOUBLE',            'Double-precision floating point',
29         'FT_ABSOLUTE_TIME',     'Date/Time stamp',
30         'FT_RELATIVE_TIME',     'Time duration',
31         'FT_STRING',            'String',
32         'FT_STRINGZ',           'String',
33         'FT_UINT_STRING',       'String',
34         'FT_ETHER',             '6-byte Hardware (MAC) Address',
35         'FT_BYTES',             'Byte array',
36         'FT_IPv4',              'IPv4 address',
37         'FT_IPv6',              'IPv6 address',
38         'FT_IPXNET',            'IPX network or server name',
39 );
40
41 # Read all the data into memory
42 while (<STDIN>) {
43         next unless (/^([PF])/);
44
45         $record_type = $1;
46         chomp($_);
47         $_ =~ s/\&/\&amp\;/g;
48         $_ =~ s/\>/\&gt;/g;
49         $_ =~ s/\</\&lt\;/g;
50
51         # Store protocol information
52         if ($record_type eq 'P') {
53                 ($junk, $name, $abbrev) = split(/\t+/, $_);
54                 $proto_abbrev{$name} = $abbrev;
55         }
56         # Store header field information
57         else {
58                 ($junk, $name, $abbrev, $type, $parent, $blurb) =
59                         split(/\t+/, $_);
60                 push(@{$field_abbrev{$parent}}, $abbrev);
61                 $field_info{$abbrev} = [ $name, $type, $blurb ];
62         }
63 }
64
65 # if there was no input on stdin, bail out
66 if ($record_type ne 'P' and $record_type ne 'F') {
67         exit;
68 }
69
70 $template = shift(@ARGV);
71
72 open(TEMPLATE, $template) || die "Can't open $template for reading: $!\n";
73
74 while (<TEMPLATE>) {
75         if (/=insert_dfilter_table/) {
76                 &create_dfilter_table;
77         }
78         else {
79                 print;
80         }
81 }
82
83 close(TEMPLATE) || die "Can't close $template: $!\n";
84
85 sub create_dfilter_table {
86
87         print "<appendix id=\"AppFiltFields\"><title>Ethereal Display Filter Fields</title>\n";
88         $pn_counter = 1;
89
90         # Print each protocol
91         for $proto_name (sort keys %proto_abbrev) {
92
93                 $ns_proto_name = $proto_name;
94                 $ns_proto_name =~ s/\s//g;
95                 $ns_proto_name =~ s/\)//g;
96                 $ns_proto_name =~ s/\(//g;
97                 $ns_proto_name =~ s/_//g;
98                 $ns_proto_name =~ s/\+/plus/g;
99                 $ns_proto_name =~ s/\//slash/g;
100                 $ns_proto_name =~ s/,/comma/g;
101                 $ns_proto_name =~ s/:/colon/g;
102                 $ns_proto_name =~ s/'/apos/g;
103                 
104                 # The maximum token name length is apparently 44 characters. 
105                 # That's what NAMELEN is defined as in docbook 4.1, at least.
106
107                 if (length ($ns_proto_name) > 41) {  # "SID" and "TID" are prepended below
108                         $ns_proto_name = sprintf ("%s%04d", substr($ns_proto_name, 0,
109                                 37), $pn_counter);
110                         $pn_counter++;
111                 }
112                         
113                 print "<section id=\"SID$ns_proto_name\"><title>$proto_name ($proto_abbrev{$proto_name})</title>\n\n";
114
115                 print "<table id=\"TID$ns_proto_name\"><title>$proto_name ($proto_abbrev{$proto_name})</title>\n";
116                 print "<tgroup cols=\"4\">\n";
117 #               print "<colspec colnum=\"1\" colwidth=\"80pt\">\n";
118 #               print "<colspec colnum=\"2\" colwidth=\"80pt\"\n>";
119                 print "<thead>\n  <row>\n    ";
120                 print "<entry>Field</>\n    <entry>Field Name</>\n    <entry>Type</>\n    <entry>Description</>\n\n";
121
122                 print "  </row>\n</thead>\n<tbody>\n";
123
124                 # If this proto has children fields, print those
125                 if ($field_abbrev{$proto_abbrev{$proto_name}}) {
126
127                         for $field_abbrev (sort @{$field_abbrev{$proto_abbrev{$proto_name}}}) {
128
129                             print "  <row>\n";
130                             print "    <entry>$field_abbrev</entry>\n";
131                             print "    <entry>", $field_info{$field_abbrev}[0], "</entry>\n";
132                             print "    <entry>", $ftenum_names{$field_info{$field_abbrev}[1]}, "</entry>\n";
133                                 print "    <entry>", $field_info{$field_abbrev}[2], "</>\n";
134                             print "  </row>\n\n";
135
136                         }
137
138                 }
139                 else {
140
141                     print "  <row>\n    <entry></entry>\n    <entry></entry>\n    <entry></entry><entry></entry>\n";
142                     print "  </row>\n";
143
144                 }
145
146                 print "</tbody></tgroup></table>\n";
147                 print "</section>\n\n";
148
149         }
150
151         print "</appendix>\n";
152
153 }