Added tap functionality to UDP
[obnox/wireshark/wip.git] / rdps.c
1 /* rdps.c
2  *
3  * $Id: rdps.c,v 1.5 2002/08/28 21:00:41 jmayer Exp $
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@ethereal.com>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 /* takes the file listed as the first argument and creates the file listed
24 as the second argument. It takes a PostScript file and creates a C program
25 with 2 functions:
26         print_ps_preamble()
27         print_ps_finale()
28
29 */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #define BUFFER_SIZE 1024
40
41 void start_code(FILE *fd, char *func);
42 void write_code(FILE *fd, char *string);
43 void end_code(FILE *fd);
44 void ps_clean_string(unsigned char *out, const unsigned char *in,
45                         int outbuf_size);
46
47 enum ps_state { null, preamble, hex, finale };
48
49 int main(int argc, char **argv)
50 {
51         FILE    *input;
52         FILE    *output;
53         char    buf[BUFFER_SIZE];       /* static sized buffer! */
54         enum ps_state   state = null;
55
56         if (argc != 3) {
57                 fprintf(stderr, "%s: input_file output_file\n", argv[0]);
58                 exit(-1);
59         }
60
61         if (!(input = fopen(argv[1], "r"))) {
62                 fprintf(stderr, "%s: cannot open %s for input.\n", argv[0], argv[1]);
63                 exit(-1);
64         }
65
66         if (!(output = fopen(argv[2], "w"))) {
67                 fprintf(stderr, "%s: cannot open %s for output.\n", argv[0], argv[2]);
68                 exit(-1);
69         }
70
71         fprintf(output, "/* Created by rdps.c. Do not edit! */\n\n"
72           "#include <stdio.h>\n\n"
73           "#include \"ps.h\"\n\n");
74
75         while (fgets(buf, BUFFER_SIZE - 1, input)) {
76
77                 if (state == null) {
78                         if (strcmp(buf, "% ---- ethereal preamble start ---- %\n") == 0) {
79                                 state = preamble;
80                                 start_code(output, "preamble");
81                                 continue;
82                         }
83                         else if (strcmp(buf, "% ---- ethereal finale start ---- %\n") == 0) {
84                                 state = finale;
85                                 start_code(output, "finale");
86                                 continue;
87                         }
88                 }
89                 else if (state == preamble) {
90                         if (strcmp(buf, "% ---- ethereal preamble end ---- %\n") == 0) {
91                                 state = null;
92                                 end_code(output);
93                                 continue;
94                         }
95                         else {
96                                 write_code(output, buf);
97                         }
98                 }
99                 else if (state == hex) {
100                         if (strcmp(buf, "% ---- ethereal hex end ---- %\n") == 0) {
101                                 state = null;
102                                 end_code(output);
103                                 continue;
104                         }
105                         else {
106                                 write_code(output, buf);
107                         }
108                 }
109                 else if (state == finale) {
110                         if (strcmp(buf, "% ---- ethereal finale end ---- %\n") == 0) {
111                                 state = null;
112                                 end_code(output);
113                                 continue;
114                         }
115                         else {
116                                 write_code(output, buf);
117                         }
118                 }
119                 else {
120                         fprintf(stderr, "NO MATCH:%s", buf);
121                         exit(-1);
122                 }
123         }
124         exit(0);
125 }
126
127 void start_code(FILE *fd, char *func)
128 {
129         fprintf(fd, "/* Created by rdps.c. Do not edit! */\n");
130         fprintf(fd, "void print_ps_%s(FILE *fd) {\n", func);
131 }
132
133 void write_code(FILE *fd, char *string)
134 {
135         char psbuf[BUFFER_SIZE];
136         ps_clean_string(psbuf, string, BUFFER_SIZE);
137         fprintf(fd, "\tfprintf(fd, \"%s\");\n", psbuf);
138 }
139
140 void end_code(FILE *fd)
141 {
142         fprintf(fd, "}\n\n\n");
143 }
144
145 void ps_clean_string(unsigned char *out, const unsigned char *in,
146                         int outbuf_size)
147 {
148         int rd, wr;
149         char c;
150
151         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
152                 c = in[rd];
153                 switch (c) {
154                         case '\\':
155                                 out[wr] = '\\';
156                                 out[++wr] = '\\';
157                                 out[++wr] = c;
158                                 break;
159
160                         case '%':
161                                 out[wr] = '%';
162                                 out[++wr] = '%';
163                                 break;
164
165                         case '\n':
166                                 out[wr] = '\\';
167                                 out[++wr] = 'n';
168                                 break;
169
170                         default:
171                                 out[wr] = c;
172                                 break;
173                 }
174
175                 if (c == 0) {
176                         break;
177                 }
178         }
179 }