From Paul Erkkila:
[obnox/wireshark/wip.git] / rdps.c
1 /* rdps.c
2  *
3  * $Id$
4  * 
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 /* takes the file listed as the first argument and creates the file listed
25 as the second argument. It takes a PostScript file and creates a C program
26 with 2 functions:
27         print_ps_preamble()
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, const char *func);
43 void write_code(FILE *fd, char *string);
44 void end_code(FILE *fd);
45 void ps_clean_string(char *out, const char *in,
46                         int outbuf_size);
47
48 enum ps_state { null, preamble, 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, "% ---- wireshark preamble start ---- %\n") == 0) {
80                                 state = preamble;
81                                 start_code(output, "preamble");
82                                 continue;
83                         }
84                         else if (strcmp(buf, "% ---- wireshark finale start ---- %\n") == 0) {
85                                 state = finale;
86                                 start_code(output, "finale");
87                                 continue;
88                         }
89                 }
90                 else if (state == preamble) {
91                         if (strcmp(buf, "% ---- wireshark preamble end ---- %\n") == 0) {
92                                 state = null;
93                                 end_code(output);
94                                 continue;
95                         }
96                         else {
97                                 write_code(output, buf);
98                         }
99                 }
100                 else if (state == finale) {
101                         if (strcmp(buf, "% ---- wireshark finale end ---- %\n") == 0) {
102                                 state = null;
103                                 end_code(output);
104                                 continue;
105                         }
106                         else {
107                                 write_code(output, buf);
108                         }
109                 }
110                 else {
111                         fprintf(stderr, "NO MATCH:%s", buf);
112                         exit(-1);
113                 }
114         }
115         exit(0);
116 }
117
118 void start_code(FILE *fd, const char *func)
119 {
120         fprintf(fd, "/* Created by rdps.c. Do not edit! */\n");
121         fprintf(fd, "void print_ps_%s(FILE *fd) {\n", func);
122 }
123
124 void write_code(FILE *fd, char *string)
125 {
126         char psbuf[BUFFER_SIZE];
127         ps_clean_string(psbuf, string, BUFFER_SIZE);
128         fprintf(fd, "\tfprintf(fd, \"%s\");\n", psbuf);
129 }
130
131 void end_code(FILE *fd)
132 {
133         fprintf(fd, "}\n\n\n");
134 }
135
136 void ps_clean_string(char *out, const char *in,
137                         int outbuf_size)
138 {
139         int rd, wr;
140         char c;
141
142         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
143                 c = in[rd];
144                 switch (c) {
145                         case '\\':
146                                 out[wr] = '\\';
147                                 out[++wr] = '\\';
148                                 out[++wr] = c;
149                                 break;
150
151                         case '%':
152                                 out[wr] = '%';
153                                 out[++wr] = '%';
154                                 break;
155
156                         case '\n':
157                                 out[wr] = '\\';
158                                 out[++wr] = 'n';
159                                 break;
160
161                         default:
162                                 out[wr] = c;
163                                 break;
164                 }
165
166                 if (c == 0) {
167                         break;
168                 }
169         }
170 }