-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselector.c
170 lines (137 loc) · 3.71 KB
/
selector.c
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
/* File: selector.c
Copyright (C) 2013 David Hauweele <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include "version.h"
#include "pcap-list.h"
#include "pcap-write.h"
#include "text-ui.h"
#include "help.h"
#define TARGET "Frame-Selector"
static void (*warn_ui)(const char *fmt) = warn_text_ui;
static void (*init_ui)(void (*exit_cb)(void),
void (*save_cb)(void),
void (*save_as_cb)(const char *),
void (*open_cb)(const char *)) = init_text_ui;
static void (*main_ui)(void) = main_text_ui;
static void (*exit_ui)(void) = exit_text_ui;
/* The filename of the current list. This variable can be NULL which
means that no file has been opened. */
static const char *filename;
static bool pcap_write_action(const struct pcap_node *node, void *data)
{
pcap_append_frame(node->data, node->size);
return true;
}
static void save_as_cb(const char *path)
{
open_writing_pcap(path);
pcap_list_for_each(pcap_write_action, NULL);
close_writing_pcap();
filename = path;
}
static void save_cb(void)
{
if(filename && pcap_list_dirty())
save_as_cb(filename);
}
static void exit_cb(void)
{
save_cb();
}
static void open_cb(const char *path)
{
pcap_list_load_from_file(path);
filename = path;
}
int main(int argc, char *argv[])
{
const char *name;
int exit_status = EXIT_FAILURE;
name = (const char *)strrchr(argv[0], '/');
name = name ? (name + 1) : argv[0];
enum opt {
OPT_COMMIT = 0x100
};
struct opt_help helps[] = {
{ 'h', "help", "Show this help message" },
{ 'V', "version", "Print version information" },
#ifdef COMMIT
{ 0, "commit", "Display commit information" },
#endif /* COMMIT */
{ 0, NULL, NULL }
};
struct option opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
#ifdef COMMIT
{ "commit", no_argument, NULL, OPT_COMMIT },
#endif /* COMMIT */
{ NULL, 0, NULL, 0 }
};
while(1) {
int c = getopt_long(argc, argv, "hV", opts, NULL);
if(c == -1)
break;
switch(c) {
#ifdef COMMIT
case(OPT_COMMIT):
commit();
exit_status = EXIT_SUCCESS;
goto EXIT;
#endif /* COMMIT */
case('V'):
version(TARGET);
exit_status = EXIT_SUCCESS;
goto EXIT;
case('h'):
exit_status = EXIT_SUCCESS;
default:
help(name, "[OPTIONS] ... [PCAP FILE]", helps);
goto EXIT;
}
}
/* Initialise the UI. */
init_ui(exit_cb,
save_cb,
save_as_cb,
open_cb);
pcap_list_init(warn_ui);
if((argc - optind) == 1) {
int ret;
filename = argv[optind];
ret = access(filename, R_OK);
switch(ret) {
case 0:
pcap_list_load_from_file(filename);
break;
case ENOENT:
break;
default:
err(EXIT_FAILURE, "cannot access %s", filename);
}
}
/* Start the UI. */
main_ui();
exit_status = EXIT_SUCCESS;
EXIT:
exit_ui();
return exit_status;
}