rdps.py 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env python
#
# rdps.py
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 1998 Gerald Combs
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
21
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 23 24 25
#

'''\
takes the file listed as the first argument and creates the file listed
26
as the second argument. It takes a PostScript file and creates a C source
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
with 2 functions:
	print_ps_preamble()
	print_ps_finale()

Ported to Python from rdps.c.
'''

import sys
import os.path

def ps_clean_string(raw_str):
    ps_str = ''
    for c in raw_str:
        if c == '\\':
            ps_str += '\\\\'
        elif c == '%':
            ps_str += '%%'
        elif c == '\n':
            ps_str += '\\n'
        else:
            ps_str += c
    return ps_str

def start_code(fd, func):
    script_name = os.path.split(__file__)[-1]
    fd.write("void print_ps_%s(FILE *fd) {\n" % func)

def write_code(fd, raw_str):
    ps_str = ps_clean_string(raw_str)
    fd.write("\tfprintf(fd, \"%s\");\n" % ps_str)

def end_code(fd):
    fd.write("}\n\n\n")

61
def exit_err(msg=None, *param):
62
    if msg is not None:
63
        sys.stderr.write(msg % param)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    sys.exit(1)

# Globals
STATE_NULL = 'null'
STATE_PREAMBLE = 'preamble'
STATE_FINALE = 'finale'

def main():
    state = STATE_NULL;

    if len(sys.argv) != 3:
        exit_err("%s: input_file output_file\n", __file__)

    input = open(sys.argv[1], 'r')
    output = open(sys.argv[2], 'w')

80 81
    script_name = os.path.split(__file__)[-1]

82
    output.write('''\
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/* DO NOT EDIT
 *
 * Created by %s.
 *
 * ps.c
 * Definitions for generating PostScript(R) packet output.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
106
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
107
 */
108 109 110 111 112

#include <stdio.h>

#include "ps.h"

113
''' % script_name)
114

115
    for line in input:
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        #line = line.rstrip()
        if state is STATE_NULL:
            if line.startswith("% ---- wireshark preamble start ---- %"):
                state = STATE_PREAMBLE
                start_code(output, "preamble")
                continue
            elif line.startswith("% ---- wireshark finale start ---- %"):
                state = STATE_FINALE
                start_code(output, "finale")
                continue
        elif state is STATE_PREAMBLE:
            if line.startswith("% ---- wireshark preamble end ---- %"):
                state = STATE_NULL
                end_code(output)
                continue
            else:
                write_code(output, line)
        elif state is STATE_FINALE:
            if line.startswith("% ---- wireshark finale end ---- %"):
                state = STATE_NULL
                end_code(output)
                continue
            else:
                write_code(output, line)
        else:
            exit_err("NO MATCH:%s", line)

    sys.exit(0)

if __name__ == "__main__":
    main()

148 149 150 151 152 153 154 155 156 157 158
#
# Editor modelines  -  http://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
#
# vi: set shiftwidth=4 expandtab:
# :indentSize=4:noTabs=true:
#