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