Witness: enum witness_interface_state
[metze/wireshark/wip.git] / tools / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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("void print_ps_%s(FILE *fd) {\n" % func)
55
56 def write_code(fd, raw_str):
57     ps_str = ps_clean_string(raw_str)
58     fd.write("\tfprintf(fd, \"%s\");\n" % ps_str)
59
60 def end_code(fd):
61     fd.write("}\n\n\n")
62
63 def exit_err(msg=None, *param):
64     if msg is not None:
65         sys.stderr.write(msg % param)
66     sys.exit(1)
67
68 # Globals
69 STATE_NULL = 'null'
70 STATE_PREAMBLE = 'preamble'
71 STATE_FINALE = 'finale'
72
73 def main():
74     state = STATE_NULL;
75
76     if len(sys.argv) != 3:
77         exit_err("%s: input_file output_file\n", __file__)
78
79     input = open(sys.argv[1], 'r')
80     output = open(sys.argv[2], 'w')
81
82     script_name = os.path.split(__file__)[-1]
83
84     output.write('''\
85 /* DO NOT EDIT
86  *
87  * Created by %s.
88  *
89  * ps.c
90  * Definitions for generating PostScript(R) packet output.
91  *
92  * Wireshark - Network traffic analyzer
93  * By Gerald Combs <gerald@wireshark.org>
94  * Copyright 1998 Gerald Combs
95  *
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.
100  *
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.
105  *
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.
109  */
110
111 #include <stdio.h>
112
113 #include "ps.h"
114
115 ''' % script_name)
116
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)
144
145     sys.exit(0)
146
147 if __name__ == "__main__":
148     main()
149
150 #
151 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
152 #
153 # Local variables:
154 # c-basic-offset: 4
155 # indent-tabs-mode: nil
156 # End:
157 #
158 # vi: set shiftwidth=4 expandtab:
159 # :indentSize=4:noTabs=true:
160 #