"ssn_range" needs to be a copy of "global_ssn_range", so that it's not
[obnox/wireshark/wip.git] / rdps.c
1 /* rdps.c
2  *
3  * $Id$
4  * 
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
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, 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, 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 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, "% ---- ethereal 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 == hex) {
101                         if (strcmp(buf, "% ---- ethereal hex end ---- %\n") == 0) {
102                                 state = null;
103                                 end_code(output);
104                                 continue;
105                         }
106                         else {
107                                 write_code(output, buf);
108                         }
109                 }
110                 else if (state == finale) {
111                         if (strcmp(buf, "% ---- ethereal finale end ---- %\n") == 0) {
112                                 state = null;
113                                 end_code(output);
114                                 continue;
115                         }
116                         else {
117                                 write_code(output, buf);
118                         }
119                 }
120                 else {
121                         fprintf(stderr, "NO MATCH:%s", buf);
122                         exit(-1);
123                 }
124         }
125         exit(0);
126 }
127
128 void start_code(FILE *fd, char *func)
129 {
130         fprintf(fd, "/* Created by rdps.c. Do not edit! */\n");
131         fprintf(fd, "void print_ps_%s(FILE *fd) {\n", func);
132 }
133
134 void write_code(FILE *fd, char *string)
135 {
136         char psbuf[BUFFER_SIZE];
137         ps_clean_string(psbuf, string, BUFFER_SIZE);
138         fprintf(fd, "\tfprintf(fd, \"%s\");\n", psbuf);
139 }
140
141 void end_code(FILE *fd)
142 {
143         fprintf(fd, "}\n\n\n");
144 }
145
146 void ps_clean_string(char *out, const char *in,
147                         int outbuf_size)
148 {
149         int rd, wr;
150         char c;
151
152         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
153                 c = in[rd];
154                 switch (c) {
155                         case '\\':
156                                 out[wr] = '\\';
157                                 out[++wr] = '\\';
158                                 out[++wr] = c;
159                                 break;
160
161                         case '%':
162                                 out[wr] = '%';
163                                 out[++wr] = '%';
164                                 break;
165
166                         case '\n':
167                                 out[wr] = '\\';
168                                 out[++wr] = 'n';
169                                 break;
170
171                         default:
172                                 out[wr] = c;
173                                 break;
174                 }
175
176                 if (c == 0) {
177                         break;
178                 }
179         }
180 }