Witness: set col_info for interfaceInfo_state
[wireshark-wip.git] / tools / rdps.py
blob8f6a958bfc073400e4328f49a5543dbcd2975c64
1 #!/usr/bin/env python
3 # rdps.py
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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()
33 Ported to Python from rdps.c.
34 '''
36 import sys
37 import os.path
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
52 def start_code(fd, func):
53 script_name = os.path.split(__file__)[-1]
54 fd.write("void print_ps_%s(FILE *fd) {\n" % func)
56 def write_code(fd, raw_str):
57 ps_str = ps_clean_string(raw_str)
58 fd.write("\tfprintf(fd, \"%s\");\n" % ps_str)
60 def end_code(fd):
61 fd.write("}\n\n\n")
63 def exit_err(msg=None, *param):
64 if msg is not None:
65 sys.stderr.write(msg % param)
66 sys.exit(1)
68 # Globals
69 STATE_NULL = 'null'
70 STATE_PREAMBLE = 'preamble'
71 STATE_FINALE = 'finale'
73 def main():
74 state = STATE_NULL;
76 if len(sys.argv) != 3:
77 exit_err("%s: input_file output_file\n", __file__)
79 input = open(sys.argv[1], 'r')
80 output = open(sys.argv[2], 'w')
82 script_name = os.path.split(__file__)[-1]
84 output.write('''\
85 /* DO NOT EDIT
87 * Created by %s.
89 * ps.c
90 * Definitions for generating PostScript(R) packet output.
92 * Wireshark - Network traffic analyzer
93 * By Gerald Combs <gerald@wireshark.org>
94 * Copyright 1998 Gerald Combs
96 * This program is free software; you can redistribute it and/or
97 * modify it under the terms of the GNU General Public License
98 * as published by the Free Software Foundation; either version 2
99 * of the License, or (at your option) any later version.
101 * This program is distributed in the hope that it will be useful,
102 * but WITHOUT ANY WARRANTY; without even the implied warranty of
103 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
104 * GNU General Public License for more details.
106 * You should have received a copy of the GNU General Public License
107 * along with this program; if not, write to the Free Software
108 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
111 #include <stdio.h>
113 #include "ps.h"
115 ''' % script_name)
117 for line in input:
118 #line = line.rstrip()
119 if state is STATE_NULL:
120 if line.startswith("% ---- wireshark preamble start ---- %"):
121 state = STATE_PREAMBLE
122 start_code(output, "preamble")
123 continue
124 elif line.startswith("% ---- wireshark finale start ---- %"):
125 state = STATE_FINALE
126 start_code(output, "finale")
127 continue
128 elif state is STATE_PREAMBLE:
129 if line.startswith("% ---- wireshark preamble end ---- %"):
130 state = STATE_NULL
131 end_code(output)
132 continue
133 else:
134 write_code(output, line)
135 elif state is STATE_FINALE:
136 if line.startswith("% ---- wireshark finale end ---- %"):
137 state = STATE_NULL
138 end_code(output)
139 continue
140 else:
141 write_code(output, line)
142 else:
143 exit_err("NO MATCH:%s", line)
145 sys.exit(0)
147 if __name__ == "__main__":
148 main()