-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_line.cpp
More file actions
203 lines (154 loc) · 7.26 KB
/
Copy pathcommand_line.cpp
File metadata and controls
203 lines (154 loc) · 7.26 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* File role: command-line parsing for dsfemon.
*
* Handles --help, --version, adapter selection, subadapter limits, refresh
* interval, parser errors, and the stable usage text before ncurses starts.
*/
#include "command_line.h"
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
// Store a formatted parser error and use a simple non-zero return convention.
static int command_line_error(char *buffer, size_t buffer_size, const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(buffer, buffer_size, format, args);
va_end(args);
return -1;
}
// Parse a non-negative decimal integer without accepting trailing text.
static bool parse_non_negative_int(const char *text, int *value) {
char *end = NULL;
if (text == NULL || *text == '\0')
return false;
errno = 0;
long parsed = strtol(text, &end, 10);
if (errno == ERANGE || end == text || *end != '\0')
return false;
if (parsed < 0 || parsed > INT_MAX)
return false;
*value = (int)parsed;
return true;
}
// Validate one adapter number against the statically allocated device table.
static int validate_adapter(int adapter, char *error_buffer, size_t error_buffer_size) {
if (adapter >= 0 && adapter < DVB_MAX_ADAPTERS)
return 0;
return command_line_error(error_buffer, error_buffer_size, "Adapter must be between 0 and %d.", DVB_MAX_ADAPTERS - 1);
}
// Parse and validate the monitor refresh interval in milliseconds.
static int parse_refresh_interval(const char *text, int *refresh_interval_ms, char *error_buffer, size_t error_buffer_size) {
if (!parse_non_negative_int(text, refresh_interval_ms))
return command_line_error(error_buffer, error_buffer_size, "Refresh interval must be a decimal number of milliseconds.");
if (*refresh_interval_ms < DSFEMON_MIN_REFRESH_INTERVAL_MS || *refresh_interval_ms > DSFEMON_MAX_REFRESH_INTERVAL_MS)
return command_line_error(error_buffer,
error_buffer_size,
"Refresh interval must be between %d and %d ms.",
DSFEMON_MIN_REFRESH_INTERVAL_MS,
DSFEMON_MAX_REFRESH_INTERVAL_MS);
return 0;
}
// Parse a comma-separated adapter selection such as "0" or "0,2,5".
static int parse_adapter_list(const char *text, struct dvb_scan_config *scan_config, char *error_buffer, size_t error_buffer_size) {
const char *part_start = text;
if (text == NULL || *text == '\0')
return command_line_error(error_buffer, error_buffer_size, "Adapter selection must not be empty.");
scan_config->adapter_filter_enabled = true;
memset(scan_config->adapter_enabled, 0, sizeof(scan_config->adapter_enabled));
while (part_start != NULL) {
const char *comma = strchr(part_start, ',');
char adapter_text[32];
int adapter;
size_t adapter_len = comma == NULL ? strlen(part_start) : (size_t)(comma - part_start);
if (adapter_len == 0)
return command_line_error(error_buffer, error_buffer_size, "Adapter selection contains an empty item.");
if (adapter_len >= sizeof(adapter_text))
return command_line_error(error_buffer, error_buffer_size, "Adapter number is too long.");
memcpy(adapter_text, part_start, adapter_len);
adapter_text[adapter_len] = '\0';
if (!parse_non_negative_int(adapter_text, &adapter))
return command_line_error(error_buffer, error_buffer_size, "Adapter selection must contain decimal numbers.");
if (validate_adapter(adapter, error_buffer, error_buffer_size) != 0)
return -1;
scan_config->adapter_enabled[adapter] = true;
part_start = comma == NULL ? NULL : comma + 1;
}
return 0;
}
// Fill defaults before parsing optional command-line overrides.
void init_command_line_options(struct command_line_options *options) {
options->action = COMMAND_LINE_RUN;
options->scan_config.min_adapter = DVB_DEFAULT_MIN_ADAPTER;
options->scan_config.max_adapter = DVB_DEFAULT_MAX_ADAPTER;
options->scan_config.max_subadapter = DVB_MAX_SUBADAPTERS;
options->scan_config.adapter_filter_enabled = false;
options->refresh_interval_ms = DSFEMON_DEFAULT_REFRESH_INTERVAL_MS;
memset(options->scan_config.adapter_enabled, 0, sizeof(options->scan_config.adapter_enabled));
}
// Parse CLI options before ncurses starts so help/errors remain ordinary text.
int parse_command_line(int argc, char **argv, struct command_line_options *options, char *error_buffer, size_t error_buffer_size) {
static const struct option long_options[] = {
{"adapters", required_argument, NULL, 'a'},
{"subadapters", required_argument, NULL, 's'},
{"interval", required_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}};
optind = 1;
opterr = 0;
int option;
while ((option = getopt_long(argc, argv, ":a:s:i:hv", long_options, NULL)) != -1) {
switch (option) {
case 'a':
if (parse_adapter_list(optarg, &options->scan_config, error_buffer, error_buffer_size) != 0)
return -1;
break;
case 's': {
int subadapters;
if (!parse_non_negative_int(optarg, &subadapters))
return command_line_error(error_buffer, error_buffer_size, "Subadapter count must be a decimal number.");
if (subadapters < 1 || subadapters > DVB_MAX_SUBADAPTERS)
return command_line_error(error_buffer, error_buffer_size, "Subadapter count must be between 1 and %d.", DVB_MAX_SUBADAPTERS);
options->scan_config.max_subadapter = subadapters;
break;
}
case 'i': {
int refresh_interval_ms;
if (parse_refresh_interval(optarg, &refresh_interval_ms, error_buffer, error_buffer_size) != 0)
return -1;
options->refresh_interval_ms = refresh_interval_ms;
break;
}
case 'h':
options->action = COMMAND_LINE_HELP;
break;
case 'v':
options->action = COMMAND_LINE_VERSION;
break;
case ':':
return command_line_error(error_buffer, error_buffer_size, "Option '-%c' requires an argument.", optopt);
case '?':
default:
if (optopt != 0)
return command_line_error(error_buffer, error_buffer_size, "Unknown option '-%c'.", optopt);
return command_line_error(error_buffer, error_buffer_size, "Unknown option '%s'.", argv[optind - 1]);
}
}
if (optind < argc)
return command_line_error(error_buffer, error_buffer_size, "Unexpected positional argument '%s'.", argv[optind]);
return 0;
}
// Print the stable user-facing CLI help text.
void print_usage(FILE *stream, const char *program_name) {
fprintf(stream, "Usage: %s [options]\n", program_name);
fprintf(stream, "\n");
fprintf(stream, "Options:\n");
fprintf(stream, " -a, --adapters LIST scan only selected adapters, for example 0 or 0,2,5\n");
fprintf(stream, " -s, --subadapters N scan N frontends per adapter, default %d\n", DVB_MAX_SUBADAPTERS);
fprintf(stream, " -i, --interval MS refresh interval, %d-%d ms, default %d\n", DSFEMON_MIN_REFRESH_INTERVAL_MS, DSFEMON_MAX_REFRESH_INTERVAL_MS, DSFEMON_DEFAULT_REFRESH_INTERVAL_MS);
fprintf(stream, " -h, --help show this help\n");
fprintf(stream, " -v, --version show program version\n");
}