-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtou.yucc
475 lines (411 loc) · 9.27 KB
/
mtou.yucc
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/* -*- c -*- */
#if !defined INCLUDED_yuck_h_
#define INCLUDED_yuck_h_
#include <stddef.h>
#define YUCK_OPTARG_NONE ((void*)0x1U)
enum yuck_cmds_e {
/* value used when no command was specified */
MTOU_CMD_NONE = 0U,
/* actual commands */
/* convenience identifiers */
YUCK_NOCMD = MTOU_CMD_NONE,
YUCK_NCMDS = MTOU_CMD_NONE
};
typedef struct yuck_s yuck_t;
/* generic struct */
struct yuck_s {
enum yuck_cmds_e cmd;
/* left-over arguments,
* the command string is never a part of this */
size_t nargs;
char **args;
/* slots common to all commands */
/* help is handled automatically */
/* version is handled automatically */
char *iface_arg;
char *in_arg;
char *port_arg;
char *oface_arg;
char *out_arg;
char *out_port_arg;
unsigned int verbose_flag;
};
static __attribute__((nonnull(1))) int
yuck_parse(yuck_t*, int argc, char *argv[]);
static __attribute__((nonnull(1))) void yuck_free(yuck_t*);
static __attribute__((nonnull(1))) void yuck_auto_help(const yuck_t*);
static __attribute__((nonnull(1))) void yuck_auto_usage(const yuck_t*);
static __attribute__((nonnull(1))) void yuck_auto_version(const yuck_t*);
/* some hooks */
#if defined yuck_post_help
static __attribute__((nonnull(1))) void yuck_post_help(const yuck_t*);
#endif /* yuck_post_help */
#if defined yuck_post_usage
static __attribute__((nonnull(1))) void yuck_post_usage(const yuck_t*);
#endif /* yuck_post_usage */
#if defined yuck_post_version
static __attribute__((nonnull(1))) void yuck_post_version(const yuck_t*);
#endif /* yuck_post_version */
#endif /* INCLUDED_yuck_h_ */
/* -*- c -*- */
#if defined HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#if defined HAVE_VERSION_H
# include "version.h"
#endif /* HAVE_VERSION_H */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#if defined __INTEL_COMPILER
# pragma warning (disable:177)
# pragma warning (disable:111)
#elif defined __GNUC__
# if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6
# pragma GCC diagnostic push
# endif /* GCC version */
# pragma GCC diagnostic ignored "-Wunused-label"
# pragma GCC diagnostic ignored "-Wunused-variable"
# pragma GCC diagnostic ignored "-Wunused-function"
#endif /* __INTEL_COMPILER */
static inline bool
yuck_streqp(const char *s1, const char *s2)
{
return !strcmp(s1, s2);
}
/* for multi-args */
static inline char**
yuck_append(char **array, size_t n, char *val)
{
if (!(n % 16U)) {
/* resize */
array = realloc(array, (n + 16U) * sizeof(*array));
if (array == NULL) {
return NULL;
}
}
array[n] = val;
return array;
}
static enum yuck_cmds_e yuck_parse_cmd(const char *cmd)
{
if (0) {
;
} else {
/* error here? */
fprintf(stderr, "mtou: invalid command `%s'\n\
Try `--help' for a list of commands.\n", cmd);
}
return (enum yuck_cmds_e)-1;
}
static int yuck_parse(yuck_t tgt[static 1U], int argc, char *argv[])
{
char *op;
int i;
/* we'll have at most this many args */
memset(tgt, 0, sizeof(*tgt));
if ((tgt->args = calloc(argc, sizeof(*tgt->args))) == NULL) {
return -1;
}
for (i = 1; i < argc && tgt->nargs < (size_t)-1; i++) {
op = argv[i];
switch (*op) {
case '-':
/* could be an option */
switch (*++op) {
default:
/* could be glued into one */
for (; *op; op++) {
goto shortopt; back_from_shortopt:;
}
break;
case '-':
if (*++op == '\0') {
i++;
goto dashdash; back_from_dashdash:;
break;
}
goto longopt; back_from_longopt:;
break;
case '\0':
goto plain_dash;
}
break;
default:
plain_dash:
goto arg; back_from_arg:;
break;
}
}
if (i < argc) {
op = argv[i];
if (*op++ == '-' && *op++ == '-' && !*op) {
/* another dashdash, filter out */
i++;
}
}
/* has to be here as the max_pargs condition might drive us here */
dashdash:
{
/* dashdash loop, pile everything on tgt->args
* don't check for subcommands either, this is in accordance to
* the git tool which won't accept commands after -- */
for (; i < argc; i++) {
tgt->args[tgt->nargs++] = argv[i];
}
}
return 0;
longopt:
{
/* split into option and arg part */
char *arg;
if ((arg = strchr(op, '=')) != NULL) {
/* \nul this one out */
*arg++ = '\0';
}
switch (tgt->cmd) {
default:
goto MTOU_CMD_NONE_longopt; back_from_MTOU_CMD_NONE_longopt:;
break;
}
goto back_from_longopt;
MTOU_CMD_NONE_longopt:
{
if (0) {
;
} else if (yuck_streqp(op, "help")) {
/* invoke auto action and exit */
yuck_auto_help(tgt);
goto success;
} else if (yuck_streqp(op, "version")) {
/* invoke auto action and exit */
yuck_auto_version(tgt);
goto success;
} else if (yuck_streqp(op, "iface")) {
tgt->iface_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "in")) {
tgt->in_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "port")) {
tgt->port_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "oface")) {
tgt->oface_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "out")) {
tgt->out_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "out-port")) {
tgt->out_port_arg = arg ?: argv[++i];
} else if (yuck_streqp(op, "verbose")) {
tgt->verbose_flag++;
} else {
/* grml */
fprintf(stderr, "mtou: unrecognized option `--%s'\n", op);
goto failure;
}
goto back_from_MTOU_CMD_NONE_longopt;
}
}
shortopt:
{
char *arg = op + 1U;
switch (tgt->cmd) {
default:
goto MTOU_CMD_NONE_shortopt; back_from_MTOU_CMD_NONE_shortopt:;
break;
}
goto back_from_shortopt;
MTOU_CMD_NONE_shortopt:
{
switch (*op) {
default:
;
;
fprintf(stderr, "mtou: invalid option -%c\n", *op);
goto failure;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* literal treatment of numeral */
goto arg;
case '?':
/* invoke auto action and exit */
yuck_auto_help(tgt);
goto success;
break;
case 'V':
/* invoke auto action and exit */
yuck_auto_version(tgt);
goto success;
break;
case 'I':
tgt->iface_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'i':
tgt->in_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'p':
tgt->port_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'O':
tgt->oface_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'o':
tgt->out_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'P':
tgt->out_port_arg = *arg
? (op += strlen(arg), arg)
: argv[++i];
break;
case 'v':
tgt->verbose_flag++;
break;
}
goto back_from_MTOU_CMD_NONE_shortopt;
}
}
arg:
{
if (tgt->cmd || !YUCK_NCMDS) {
tgt->args[tgt->nargs++] = argv[i];
} else {
/* ah, might be an arg then */
if ((tgt->cmd = yuck_parse_cmd(op)) > YUCK_NCMDS) {
return -1;
}
}
goto back_from_arg;
}
failure:
{
exit(EXIT_FAILURE);
}
success:
{
exit(EXIT_SUCCESS);
}
}
static void yuck_free(yuck_t tgt[static 1U])
{
if (tgt->args != NULL) {
/* free despite const qualifier */
free(tgt->args);
}
/* free mulargs */
switch (tgt->cmd) {
void *ptr;
default:
break;
case MTOU_CMD_NONE:
;
;
;
;
;
;
;
;
;
break;
break;
}
return;
}
static void yuck_auto_usage(const yuck_t src[static 1U])
{
switch (src->cmd) {
default:
YUCK_NOCMD:
puts("Usage: mtou [OPTION]...\n\
\n\
Multicast-unicast forwarding tool.\n\
");
break;
}
#if defined yuck_post_usage
yuck_post_usage(src);
#endif /* yuck_post_usage */
return;
}
static void yuck_auto_help(const yuck_t src[static 1U])
{
yuck_auto_usage(src);
/* leave a not about common options */
if (src->cmd == YUCK_NOCMD) {
;
}
switch (src->cmd) {
default:
case MTOU_CMD_NONE:
puts("\
-?, --help display this help and exit\n\
-V, --version output version information and exit\n\
-I, --iface=IFACE Incoming interface.\n\
-i, --in=ADDR Source address.\n\
-p, --port=PORT Port to listen on.\n\
-O, --oface=IFACE Outgoing interface.\n\
-o, --out=ADDR Destination address.\n\
-P, --out-port=PORT Port to send to.\n\
-v, --verbose Verbose output.\n\
");
break;
}
#if defined yuck_post_help
yuck_post_help(src);
#endif /* yuck_post_help */
#if defined PACKAGE_BUGREPORT
puts("\n\
Report bugs to " PACKAGE_BUGREPORT);
#endif /* PACKAGE_BUGREPORT */
return;
}
static void yuck_auto_version(const yuck_t src[static 1U])
{
switch (src->cmd) {
default:
#if 0
#elif defined package_string
puts(package_string);
#elif defined package_version
printf("mtou %s\n", package_version);
#elif defined PACKAGE_STRING
puts(PACKAGE_STRING);
#elif defined PACKAGE_VERSION
puts("mtou " PACKAGE_VERSION);
#elif defined VERSION
puts("mtou " VERSION);
#else /* !PACKAGE_VERSION, !VERSION */
puts("mtou unknown version");
#endif /* PACKAGE_VERSION */
break;
}
#if defined yuck_post_version
yuck_post_version(src);
#endif /* yuck_post_version */
return;
}
#if defined __INTEL_COMPILER
# pragma warning (default:177)
# pragma warning (default:111)
#elif defined __GNUC__
# if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6
# pragma GCC diagnostic pop
# endif /* GCC version */
#endif /* __INTEL_COMPILER */