aboutsummaryrefslogtreecommitdiffstats
path: root/rdps.c
blob: 25e4e62c79ccc3c37f955e8bc40b52432c338312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* rdps.c
 *
 * $Id$
 * 
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * 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, hex, 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, "% ---- ethereal preamble start ---- %\n") == 0) {
				state = preamble;
				start_code(output, "preamble");
				continue;
			}
			else if (strcmp(buf, "% ---- ethereal finale start ---- %\n") == 0) {
				state = finale;
				start_code(output, "finale");
				continue;
			}
		}
		else if (state == preamble) {
			if (strcmp(buf, "% ---- ethereal preamble end ---- %\n") == 0) {
				state = null;
				end_code(output);
				continue;
			}
			else {
				write_code(output, buf);
			}
		}
		else if (state == hex) {
			if (strcmp(buf, "% ---- ethereal hex end ---- %\n") == 0) {
				state = null;
				end_code(output);
				continue;
			}
			else {
				write_code(output, buf);
			}
		}
		else if (state == finale) {
			if (strcmp(buf, "% ---- ethereal finale end ---- %\n") == 0) {
				state = null;
				end_code(output);
				continue;
			}
			else {
				write_code(output, buf);
			}
		}
		else {
			fprintf(stderr, "NO MATCH:%s", buf);
			exit(-1);
		}
	}
        exit(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;
		}
	}
}