Initial revision
[obnox/wireshark/wip.git] / rdps.c
1 /* rdps.c
2  *
3  * Ethereal - Network traffic analyzer
4  * By Gerald Combs <gerald@zing.org>
5  * Copyright 1998 Gerald Combs
6  *
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 3 functions:
26         print_ps_preamble()
27         print_ps_hex()
28         print_ps_finale()
29
30 */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #define BUFFER_SIZE 1024
41
42 void start_code(FILE *fd, char *func);
43 void write_code(FILE *fd, char *string);
44 void end_code(FILE *fd);
45 void ps_clean_string(unsigned char *out, const unsigned char *in,
46                         int outbuf_size);
47
48 enum ps_state { null, preamble, hex, finale };
49
50 int main(int argc, char **argv)
51 {
52         FILE    *input;
53         FILE    *output;
54         char    buf[BUFFER_SIZE];       /* static sized buffer! */
55         enum ps_state   state = null;
56
57         if (argc != 3) {
58                 fprintf(stderr, "%s: input_file output_file\n", argv[0]);
59                 exit(-1);
60         }
61
62         if (!(input = fopen(argv[1], "r"))) {
63                 fprintf(stderr, "%s: cannot open %s for input.\n", argv[0], argv[1]);
64                 exit(-1);
65         }
66
67         if (!(output = fopen(argv[2], "w"))) {
68                 fprintf(stderr, "%s: cannot open %s for output.\n", argv[0], argv[2]);
69                 exit(-1);
70         }
71
72         fprintf(output, "/* Created by rdps.c. Do not edit! */\n\n"
73           "#include <stdio.h>\n\n"
74           "#include <ps.h>\n\n");
75
76         while (fgets(buf, BUFFER_SIZE - 1, input)) {
77
78                 if (state == null) {
79                         if (strcmp(buf, "% ---- ethereal preamble start ---- %\n") == 0) {
80                                 state = preamble;
81                                 start_code(output, "preamble");
82                                 continue;
83                         }
84                         else if (strcmp(buf, "% ---- ethereal hex start ---- %\n") == 0) {
85                                 state = hex;
86                                 start_code(output, "hex");
87                                 continue;
88                         }
89                         else if (strcmp(buf, "% ---- ethereal finale start ---- %\n") == 0) {
90                                 state = finale;
91                                 start_code(output, "finale");
92                                 continue;
93                         }
94                 }
95                 else if (state == preamble) {
96                         if (strcmp(buf, "% ---- ethereal preamble end ---- %\n") == 0) {
97                                 state = null;
98                                 end_code(output);
99                                 continue;
100                         }
101                         else {
102                                 write_code(output, buf);
103                         }
104                 }
105                 else if (state == hex) {
106                         if (strcmp(buf, "% ---- ethereal hex end ---- %\n") == 0) {
107                                 state = null;
108                                 end_code(output);
109                                 continue;
110                         }
111                         else {
112                                 write_code(output, buf);
113                         }
114                 }
115                 else if (state == finale) {
116                         if (strcmp(buf, "% ---- ethereal finale end ---- %\n") == 0) {
117                                 state = null;
118                                 end_code(output);
119                                 continue;
120                         }
121                         else {
122                                 write_code(output, buf);
123                         }
124                 }
125                 else {
126                         fprintf(stderr, "NO MATCH:%s", buf);
127                         exit(-1);
128                 }
129         }
130         exit(0);
131 }
132
133 void start_code(FILE *fd, char *func)
134 {
135         fprintf(fd, "/* Created by rdps.c. Do not edit! */\n");
136         fprintf(fd, "void print_ps_%s(FILE *fd) {\n", func);
137 }
138
139 void write_code(FILE *fd, char *string)
140 {
141         char psbuf[BUFFER_SIZE];
142         ps_clean_string(psbuf, string, BUFFER_SIZE);
143         fprintf(fd, "\tfprintf(fd, \"%s\");\n", psbuf);
144 }
145
146 void end_code(FILE *fd)
147 {
148         fprintf(fd, "}\n\n\n");
149 }
150
151 void ps_clean_string(unsigned char *out, const unsigned char *in,
152                         int outbuf_size)
153 {
154         int rd, wr;
155         char c;
156
157         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
158                 c = in[rd];
159                 switch (c) {
160                         case '\\':
161                                 out[wr] = '\\';
162                                 out[++wr] = '\\';
163                                 out[++wr] = c;
164                                 break;
165
166                         case '%':
167                                 out[wr] = '%';
168                                 out[++wr] = '%';
169                                 break;
170
171                         case '\n':
172                                 out[wr] = '\\';
173                                 out[++wr] = 'n';
174                                 break;
175
176                         default:
177                                 out[wr] = c;
178                                 break;
179                 }
180
181                 if (c == 0) {
182                         break;
183                 }
184         }
185 }