Use "strchr()" rather than "index()" - the ANSI C standard specifies
[obnox/wireshark/wip.git] / editcap.c
1 /* Edit capture files.  We can delete records, or simply convert from one 
2  * format to another format.
3  *
4  * $Id: editcap.c,v 1.6 2000/01/17 20:21:40 guy Exp $
5  *
6  * Originally written by Richard Sharpe.
7  * Improved by Guy Harris.
8  * Further improved by Richard Sharpe.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <glib.h>
14 #include <unistd.h>
15 #include <sys/time.h>
16 #include <string.h>
17 #include "wtap.h"
18
19 /*
20  * Some globals so we can pass things to various routines
21  */
22
23 struct select_item {
24
25   int inclusive;
26   int first, second;
27
28 } select_item;
29
30 struct select_item selectfrm[100];
31 int max_selected = -1;
32 static int count = 1;
33 static int keep_em = 0;
34 static int out_file_type = WTAP_FILE_PCAP;   /* default to "libpcap"   */
35 static int out_frame_type = -2;              /* Leave frame type alone */
36 static int verbose = 0;                      /* Not so verbose         */
37
38 /* Add a selection item, a simple parser for now */
39
40 void add_selection(char *sel) 
41 {
42   char *locn;
43   char *next;
44
45   if (max_selected == (sizeof(selectfrm)/sizeof(struct select_item)) - 1)
46     return;
47
48   printf("Add_Selected: %s\n", sel);
49
50   if ((locn = strchr(sel, '-')) == NULL) { /* No dash, so a single number? */
51
52     printf("Not inclusive ...");
53
54     max_selected++;
55     selectfrm[max_selected].inclusive = 0;
56     selectfrm[max_selected].first = atoi(sel);
57
58     printf(" %i\n", selectfrm[max_selected].first);
59
60   }
61   else {
62
63     printf("Inclusive ...");
64
65     next = locn + 1;
66     max_selected++;
67     selectfrm[max_selected].inclusive = 1;
68     selectfrm[max_selected].first = atoi(sel);
69     selectfrm[max_selected].second = atoi(next);
70
71     printf(" %i, %i\n", selectfrm[max_selected].first, selectfrm[max_selected].second);
72
73   }
74
75
76 }
77
78 /* Was the record selected? */
79
80 int selected(int recno)
81 {
82   int i = 0;
83
84   for (i = 0; i<= max_selected; i++) {
85
86     if (selectfrm[i].inclusive) {
87       if (selectfrm[i].first <= recno && selectfrm[i].second >= recno)
88         return 1;
89     }
90     else {
91       if (recno == selectfrm[i].first)
92         return 1;
93     }
94   }
95
96   return 0;
97
98 }
99
100 /* An argument to the callback routine */
101
102 typedef struct {
103         char    *filename;
104         wtap_dumper *pdh;
105 } callback_arg;
106
107 /*
108  *The callback routine that is called for each frame in the input file
109  */
110
111 static void
112 edit_callback(u_char *user, const struct wtap_pkthdr *phdr, int offset,
113     const u_char *buf) 
114 {
115   callback_arg *argp = (callback_arg *)user;
116   int err;
117
118   if ((!selected(count) && !keep_em) ||
119       (selected(count) && keep_em)) {
120
121     if (verbose)
122       printf("Record: %u\n", count);
123
124     /* We simply write it, we could do other things, like modify it */
125
126     if (!wtap_dump(argp->pdh, phdr, buf, &err)) {
127
128       fprintf(stderr, "editcap: Error writing to %s: %s\n", argp->filename,
129         wtap_strerror(err));
130       exit(1);
131
132     }
133
134   }
135
136   count++;
137
138 }
139
140 void usage()
141 {
142   int i;
143   const char *string;
144
145   fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>] <infile>\\\n"); 
146   fprintf(stderr, "                <outfile> [ <record#>[-<record#>] ... ]\n");
147   fprintf(stderr, "  where\t-r specifies that the records specified should be kept, not deleted, \n");
148   fprintf(stderr, "                           default is to delete\n");
149   fprintf(stderr, "       \t-v specifies verbose operation, default is silent\n");
150   fprintf(stderr, "       \t-h produces this help listing.\n");
151   fprintf(stderr, "       \t-T <encap type> specifies the encapsulation type to use:\n");
152   for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
153       string = wtap_encap_short_string(i);
154       if (string != NULL)
155         fprintf(stderr, "       \t    %s - %s\n",
156           string, wtap_encap_string(i));
157   }
158   fprintf(stderr, "       \t    default is the same as the input file\n");
159   fprintf(stderr, "       \t-F <capture type> specifies the capture file type to write:\n");
160   for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
161     if (wtap_dump_can_open(i))
162       fprintf(stderr, "       \t    %s - %s\n",
163         wtap_file_type_short_string(i), wtap_file_type_string(i));
164   }
165   fprintf(stderr, "       \t    default is libpcap\n");
166   fprintf(stderr, "\n      \t    A range of records can be specified as well\n");
167 }
168
169 int main(int argc, char *argv[])
170
171 {
172   wtap *wth;
173   int i, err;
174   callback_arg args;
175   extern char *optarg;
176   extern int optind, opterr, optopt;
177   char opt;
178
179   /* Process the options first */
180
181   while ((opt = getopt(argc, argv, "T:F:rv")) != EOF) {
182
183     switch (opt) {
184
185     case 'T':
186       out_frame_type = wtap_short_string_to_encap(optarg);
187       if (out_frame_type < 0) {
188         fprintf(stderr, "editcap: \"%s\" is not a valid encapsulation type\n",
189             optarg);
190         exit(1);
191       }
192       break;
193       
194     case 'F':
195       out_file_type = wtap_short_string_to_file_type(optarg);
196       if (out_file_type < 0) {
197         fprintf(stderr, "editcap: \"%s\" is not a valid capture file type\n",
198             optarg);
199         exit(1);
200       }
201       break;
202
203     case 'v':
204       verbose = !verbose;  /* Just invert */
205       break;
206
207     case 'r':
208       keep_em = !keep_em;  /* Just invert */
209       break;
210
211     case 'h':
212       usage();
213       exit(1);
214       break;
215
216     case '?':              /* Bad options if GNU getopt */
217       usage();
218       exit(1);
219       break;
220
221     }
222
223   }
224
225 #ifdef DEBUG
226   printf("Optind = %i, argc = %i\n", optind, argc);
227 #endif
228
229   if ((argc - optind) < 1) {
230
231     usage();
232     exit(1);
233
234   }
235
236   wth = wtap_open_offline(argv[optind], &err);
237
238   if (!wth) {
239
240     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
241         wtap_strerror(err));
242     exit(1);
243
244   }
245
246   if (verbose) {
247
248     fprintf(stderr, "File %s is a %s capture file.\n", argv[optind],
249             wtap_file_type_string(wtap_file_type(wth)));
250
251   }
252
253   /*
254    * Now, process the rest, if any ... we only write if there is an extra
255    * argument or so ...
256    */
257
258   if ((argc - optind) >= 2) {
259
260     args.filename = argv[optind + 1];
261     if (out_frame_type == -2)
262       out_frame_type = wtap_file_encap(wth);
263
264     args.pdh = wtap_dump_open(argv[optind + 1], out_file_type,
265                               out_frame_type, wtap_snapshot_length(wth), &err);
266     if (args.pdh == NULL) {
267
268       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
269               wtap_strerror(err));
270       exit(1);
271
272     }
273
274     for (i = optind + 2; i < argc; i++)
275       add_selection(argv[i]);
276
277     wtap_loop(wth, 0, edit_callback, (char *)&args, &err);
278
279     if (!wtap_dump_close(args.pdh, &err)) {
280
281       fprintf(stderr, "editcap: Error writing to %s: %s\n", argv[2],
282               wtap_strerror(err));
283       exit(1);
284
285     }
286   }
287
288   exit(0);
289   return 0;  /* Silence compiler warnings */
290 }
291