Allow RAR UL Request field to be broken down (as in 36.213).
[obnox/wireshark/wip.git] / rdps.py
1 #!/usr/bin/env python
2 #
3 # rdps.py
4 #
5 # $Id$
6
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 1998 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 #
25
26 '''\
27 takes the file listed as the first argument and creates the file listed
28 as the second argument. It takes a PostScript file and creates a C source
29 with 2 functions:
30         print_ps_preamble()
31         print_ps_finale()
32
33 Ported to Python from rdps.c.
34 '''
35
36 import sys
37 import os.path
38
39 def ps_clean_string(raw_str):
40     ps_str = ''
41     for c in raw_str:
42         if c == '\\':
43             ps_str += '\\\\'
44         elif c == '%':
45             ps_str += '%%'
46         elif c == '\n':
47             ps_str += '\\n'
48         else:
49             ps_str += c
50     return ps_str
51
52 def start_code(fd, func):
53     script_name = os.path.split(__file__)[-1]
54     fd.write("/* Created by %s. Do not edit! */\n" % script_name)
55     fd.write("void print_ps_%s(FILE *fd) {\n" % func)
56
57 def write_code(fd, raw_str):
58     ps_str = ps_clean_string(raw_str)
59     fd.write("\tfprintf(fd, \"%s\");\n" % ps_str)
60
61 def end_code(fd):
62     fd.write("}\n\n\n")
63
64 def exit_err(msg=None, *param):
65     if msg is not None:
66         sys.stderr.write(msg % param)
67     sys.exit(1)
68
69 # Globals
70 STATE_NULL = 'null'
71 STATE_PREAMBLE = 'preamble'
72 STATE_FINALE = 'finale'
73
74 def main():
75     state = STATE_NULL;
76
77     if len(sys.argv) != 3:
78         exit_err("%s: input_file output_file\n", __file__)
79
80     input = open(sys.argv[1], 'r')
81     output = open(sys.argv[2], 'w')
82
83     script_name = os.path.split(__file__)[-1]
84
85     output.write('''\
86 /* Created by %s. Do not edit! */
87
88 #include <stdio.h>
89
90 #include "ps.h"
91
92 ''' % script_name)
93
94     for line in input:
95         #line = line.rstrip()
96         if state is STATE_NULL:
97             if line.startswith("% ---- wireshark preamble start ---- %"):
98                 state = STATE_PREAMBLE
99                 start_code(output, "preamble")
100                 continue
101             elif line.startswith("% ---- wireshark finale start ---- %"):
102                 state = STATE_FINALE
103                 start_code(output, "finale")
104                 continue
105         elif state is STATE_PREAMBLE:
106             if line.startswith("% ---- wireshark preamble end ---- %"):
107                 state = STATE_NULL
108                 end_code(output)
109                 continue
110             else:
111                 write_code(output, line)
112         elif state is STATE_FINALE:
113             if line.startswith("% ---- wireshark finale end ---- %"):
114                 state = STATE_NULL
115                 end_code(output)
116                 continue
117             else:
118                 write_code(output, line)
119         else:
120             exit_err("NO MATCH:%s", line)
121
122     sys.exit(0)
123
124 if __name__ == "__main__":
125     main()
126