Port rdps from C to Python. This makes it easier to cross-compile
authorgerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 11 Mar 2009 22:12:05 +0000 (22:12 +0000)
committergerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 11 Mar 2009 22:12:05 +0000 (22:12 +0000)
Wireshark on Windows. The GNU toolchain changes have not been tested.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@27704 f5534014-38df-0310-8fa8-9805f1628bb7

Makefile.am
Makefile.nmake
configure.in
ps.h
rdps.c [deleted file]
rdps.py [new file with mode: 0644]

index dc8c23b2e88a755239f239f58590226c6c7b377f..a9e86e4a9b15c5361d4739ad19d685b7f5997ec3 100644 (file)
@@ -519,11 +519,8 @@ tshark-tap-register.c: $(TSHARK_TAP_SRC) $(srcdir)/make-tapreg-dotc
        @echo Making tshark-tap-register.c
        @$(srcdir)/make-tapreg-dotc tshark-tap-register.c $(srcdir) $(TSHARK_TAP_SRC)
 
-ps.c: print.ps rdps
-       ./rdps $(srcdir)/print.ps ps.c
-
-rdps: rdps.c
-       $(CC) $(CFLAGS) -o rdps $(srcdir)/rdps.c
+ps.c: print.ps rdps.py
+       $(PYTHON) $(srcdir)/rdps.py $(srcdir)/print.ps ps.c
 
 #
 # XXX - "svnversion.h" is distributed in the release tarball; should
@@ -532,7 +529,6 @@ rdps: rdps.c
 #
 CLEANFILES =           \
        svnversion.h    \
-       @rdps_bin@      \
        idl2wrs         \
        *~              \
        vgcore.*
@@ -793,7 +789,7 @@ EXTRA_DIST = \
        $(tpncp_DATA)           \
        $(wimaxasncp_DATA)      \
        randpkt.c               \
-       rdps.                 \
+       rdps.py                 \
        smi_modules             \
        text2pcap-scanner.l     \
        text2pcap.c             \
index eabdbf33c0ff646aa3e0a9b5ebb9ee3a1157f791..bab2f441b2d8a7c02d1b8b1413b95d30c691409b 100644 (file)
@@ -355,10 +355,9 @@ config.h   : config.h.win32 config.nmake
            -e "s/@INET6@/$(INET6_CONFIG)/" \
            < config.h.win32 > $@
 
-ps.c   : rdps.exe print.ps
-       rdps print.ps ps.c
-
 
+ps.c   : rdps.py print.ps
+       $(PYTHON) rdps.py print.ps ps.c
 #
 # Build the version string
 #
index 9a7f837e593af4ecb3ee17488f52619654056110..b611f679b016e35e40ad781087b3dd7621d59fdc 100644 (file)
@@ -703,9 +703,6 @@ fi
 AC_SUBST(wireshark_bin)
 AC_SUBST(wireshark_man)
 
-rdps_bin="rdps\$(EXEEXT)"
-AC_SUBST(rdps_bin)
-
 
 # Enable/disable tshark
 
diff --git a/ps.h b/ps.h
index e28d8ca125c1e752d441172c6e0a75f1dc16e3ce..cc742d4bdcc7d15c1cbd354901ab5b1ffa26460d 100644 (file)
--- a/ps.h
+++ b/ps.h
@@ -27,7 +27,7 @@
 #ifndef __PS_H__
 #define __PS_H__
 
-/* Functions in ps.c; automatically generated by rdps */
+/* Functions in ps.c; automatically generated by rdps.py */
 
 void print_ps_preamble(FILE *);
 void print_ps_finale(FILE *);
diff --git a/rdps.c b/rdps.c
deleted file mode 100644 (file)
index 9836044..0000000
--- a/rdps.c
+++ /dev/null
@@ -1,170 +0,0 @@
-/* rdps.c
- *
- * $Id$
- * 
- * 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
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
- */
-
-/* takes the file listed as the first argument and creates the file listed
-as the second argument. It takes a PostScript file and creates a C program
-with 2 functions:
-       print_ps_preamble()
-       print_ps_finale()
-
-*/
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define BUFFER_SIZE 1024
-
-void start_code(FILE *fd, const char *func);
-void write_code(FILE *fd, char *string);
-void end_code(FILE *fd);
-void ps_clean_string(char *out, const char *in,
-                       int outbuf_size);
-
-enum ps_state { null, preamble, finale };
-
-int main(int argc, char **argv)
-{
-       FILE    *input;
-       FILE    *output;
-       char    buf[BUFFER_SIZE];       /* static sized buffer! */
-       enum ps_state   state = null;
-
-       if (argc != 3) {
-               fprintf(stderr, "%s: input_file output_file\n", argv[0]);
-               exit(-1);
-       }
-
-       if (!(input = fopen(argv[1], "r"))) {
-               fprintf(stderr, "%s: cannot open %s for input.\n", argv[0], argv[1]);
-               exit(-1);
-       }
-
-       if (!(output = fopen(argv[2], "w"))) {
-               fprintf(stderr, "%s: cannot open %s for output.\n", argv[0], argv[2]);
-               exit(-1);
-       }
-
-       fprintf(output, "/* Created by rdps.c. Do not edit! */\n\n"
-          "#include <stdio.h>\n\n"
-          "#include \"ps.h\"\n\n");
-
-       while (fgets(buf, BUFFER_SIZE - 1, input)) {
-
-               if (state == null) {
-                       if (strcmp(buf, "% ---- wireshark preamble start ---- %\n") == 0) {
-                               state = preamble;
-                               start_code(output, "preamble");
-                               continue;
-                       }
-                       else if (strcmp(buf, "% ---- wireshark finale start ---- %\n") == 0) {
-                               state = finale;
-                               start_code(output, "finale");
-                               continue;
-                       }
-               }
-               else if (state == preamble) {
-                       if (strcmp(buf, "% ---- wireshark preamble end ---- %\n") == 0) {
-                               state = null;
-                               end_code(output);
-                               continue;
-                       }
-                       else {
-                               write_code(output, buf);
-                       }
-               }
-               else if (state == finale) {
-                       if (strcmp(buf, "% ---- wireshark finale end ---- %\n") == 0) {
-                               state = null;
-                               end_code(output);
-                               continue;
-                       }
-                       else {
-                               write_code(output, buf);
-                       }
-               }
-               else {
-                       fprintf(stderr, "NO MATCH:%s", buf);
-                       exit(-1);
-               }
-       }
-        return(0);
-}
-
-void start_code(FILE *fd, const char *func)
-{
-       fprintf(fd, "/* Created by rdps.c. Do not edit! */\n");
-       fprintf(fd, "void print_ps_%s(FILE *fd) {\n", func);
-}
-
-void write_code(FILE *fd, char *string)
-{
-       char psbuf[BUFFER_SIZE];
-       ps_clean_string(psbuf, string, BUFFER_SIZE);
-       fprintf(fd, "\tfprintf(fd, \"%s\");\n", psbuf);
-}
-
-void end_code(FILE *fd)
-{
-       fprintf(fd, "}\n\n\n");
-}
-
-void ps_clean_string(char *out, const char *in,
-                       int outbuf_size)
-{
-       int rd, wr;
-       char c;
-
-       for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
-               c = in[rd];
-               switch (c) {
-                       case '\\':
-                               out[wr] = '\\';
-                               out[++wr] = '\\';
-                               out[++wr] = c;
-                               break;
-
-                       case '%':
-                               out[wr] = '%';
-                               out[++wr] = '%';
-                               break;
-
-                       case '\n':
-                               out[wr] = '\\';
-                               out[++wr] = 'n';
-                               break;
-
-                       default:
-                               out[wr] = c;
-                               break;
-               }
-
-               if (c == 0) {
-                       break;
-               }
-       }
-}
diff --git a/rdps.py b/rdps.py
new file mode 100644 (file)
index 0000000..4c4ac04
--- /dev/null
+++ b/rdps.py
@@ -0,0 +1,125 @@
+#!/usr/bin/env python
+#
+# rdps.py
+#
+# $Id$
+# 
+# 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
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+
+'''\
+takes the file listed as the first argument and creates the file listed
+as the second argument. It takes a PostScript file and creates a C program
+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("/* Created by %s. Do not edit! */\n" % script_name)
+    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")
+
+def exit_err(msg=None):
+    if msg is not None:
+        sys.stderr.write(msg)
+    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')
+
+    output.write('''\
+/* Created by rdps.c. Do not edit! */
+
+#include <stdio.h>
+
+#include "ps.h"
+
+''')
+
+    for line in input.xreadlines():
+        #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()
+