File: options.c

package info (click to toggle)
inetutils 2%3A2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,684 kB
  • sloc: ansic: 132,371; sh: 12,498; yacc: 1,651; makefile: 725; perl: 72
file content (698 lines) | stat: -rw-r--r-- 19,031 bytes parent folder | download | duplicates (3)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/* options.c -- process the command line options
  Copyright (C) 2001-2025 Free Software Foundation, Inc.

  This file is part of GNU Inetutils.

  GNU Inetutils 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.

  GNU Inetutils 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/'. */

/* Written by Marcus Brinkmann.  */

#include <config.h>

#include <stdio.h>
#include <errno.h>

#if STDC_HEADERS
# include <stdlib.h>
#else
# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
#endif

#include <string.h>

#include <sys/socket.h>
#include <net/if.h>

#include <attribute.h>

#include "ifconfig.h"

/* List available interfaces.  */
int list_mode;

/* Be verbose about actions.  */
int verbose;

/* Flags asked for, possibly still pending application.  */
static int pending_setflags = 0;
static int pending_clrflags = 0;
static int pending_valid = 0;

/* Array of all interfaces on the command line.  */
struct ifconfig *ifs;

/* Size of IFS.  */
int nifs;

static struct ifconfig ifconfig_initializer = {
  NULL,				/* name */
  0,				/* valid */
  NULL, NULL, 0, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NULL
};

struct format formats[] = {
  /* This is the default output format if no system_default_format is
     specified.  */
  {"default",
   "Default format.  Equivalent to \"gnu\".",
   "${format}{gnu}"},
  /* This is the standard GNU output.  */
  {"gnu",
   "Standard GNU output format.",
   "${ifdisplay?}{${first?}{}{${\\n}}${format}{gnu-one-entry}}"},
  {"gnu-one-entry",
   "Same as GNU, but without additional newlines between the entries.",
   "${format}{check-existence}"
   "${ifdisplay?}{"
   "${name} (${index}):${\\n}"
   "${addr?}{  inet address ${tab}{16}${addr}${\\n}}"
   "${netmask?}{  netmask ${tab}{16}${netmask}${\\n}}"
   "${brdaddr?}{  broadcast ${tab}{16}${brdaddr}${\\n}}"
   "${dstaddr?}{  peer address ${tab}{16}${dstaddr}${\\n}}"
   "${exists?}{tunnel?}{"
   "${tunnel?}{  tunnel src ${tab}{16}${tunsrc}${\\n}}}"
   "${exists?}{tunnel?}{"
   "${tunnel?}{  tunnel dst ${tab}{16}${tundst}${\\n}}}"
   "${flags?}{  flags ${tab}{16}${flags}${\\n}}"
   "${mtu?}{  mtu ${tab}{16}${mtu}${\\n}}"
   "${metric?}{  metric ${tab}{16}${metric}${\\n}}"
   "${exists?}{hwtype?}{${hwtype?}{  link encap ${tab}{16}${hwtype}${\\n}}}"
   "${exists?}{hwaddr?}{${hwaddr?}{  hardware addr ${tab}{16}${hwaddr}${\\n}}}"
   "${media?}{  link medium ${tab}{16}${media}${\\n}}"
   "${status?}{  link status ${tab}{16}${status}${\\n}}"
   "${exists?}{txqlen?}{${txqlen?}{  tx queue len ${tab}{16}${txqlen}${\\n}}}"
   "}"},
  /* Resembles the output of ifconfig 1.39 (1999-03-19) in net-tools 1.52.  */
  {"net-tools",
   "Similar to the output of net-tools.  Default for GNU/Linux.",
   "${format}{check-existence}"
   "${ifdisplay?}{"
   "${name}${exists?}{hwtype?}{${hwtype?}{${tab}{10}Link encap:${hwtype}}"
   "${hwaddr?}{  HWaddr ${hwaddr}}}${\\n}"
   "${addr?}{${tab}{10}inet addr:${addr}"
   "${dstaddr?}{  P-t-P:${dstaddr}}"
   "${brdaddr?}{  Bcast:${brdaddr}}"
   "${netmask?}{  Mask:${netmask}}"
   "${newline}}"
   "${tab}{10}${flags}"
   "${mtu?}{  MTU:${mtu}}"
   "${metric?}{  Metric:${metric}}"
   "${media?}{"
   "${newline}${tab}{10}Media: ${media}"
   "${status?}{, ${status}}}"
   "${exists?}{ifstat?}{"
   "${ifstat?}{"
   "${newline}          RX packets:${rxpackets}"
   " errors:${rxerrors} dropped:${rxdropped} overruns:${rxfifoerr}"
   " frame:${rxframeerr}"
   "${newline}          TX packets:${txpackets}"
   " errors:${txerrors} dropped:${txdropped} overruns:${txfifoerr}"
   " carrier:${txcarrier}"
   "${newline}"
   "          collisions:${collisions}"
   "${exists?}{txqlen?}{${txqlen?}{ txqueuelen:${txqlen}}}"
   "${newline}"
   "          RX bytes:${rxbytes}  TX bytes:${txbytes}"
   "}}{"
   "${exists?}{txqlen?}{${txqlen?}{ ${tab}{10}txqueuelen:${txqlen}}${\\n}}}"
   "${newline}"
   "${exists?}{map?}{${map?}{${irq?}{"
   "          Interrupt:${irq}"
   "${baseaddr?}{ Base address:0x${baseaddr}{%x}}"
   "${memstart?}{ Memory:${memstart}{%lx}-${memend}{%lx}}"
   "${dma?}{ DMA chan:${dma}{%x}}" "${newline}" "}}}" "${newline}" "}"},
  {"netstat",
   "Terse output, similar to that of \"netstat -i\".",
   "${first?}{Iface    MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg${newline}}"
   "${format}{check-existence}" "${name}${tab}{6}${mtu}{%6d} ${metric}{%3d}"
   /* Insert blanks without ifstat.  */
   "${exists?}{ifstat?}{"
   "${ifstat?}{"
   " ${rxpackets}{%8lu} ${rxerrors}{%6lu} ${rxdropped}{%6lu} ${rxfifoerr}{%6lu}"
   " ${txpackets}{%8lu} ${txerrors}{%6lu} ${txdropped}{%6lu} ${txfifoerr}{%6lu}"
   "}{   - no statistics available -}}"
   "${tab}{76} ${flags?}{${flags}{short}}{[NO FLAGS]}" "${newline}"},
  /* Resembles the output of ifconfig shipped with unix systems like
     Solaris 2.7 or HPUX 10.20.  */
  {"unix",
   "Traditional UNIX interface listing.  Default for Solaris, BSD and HPUX.",
   "${format}{check-existence}"
   "${ifdisplay?}{"
   "${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>"
   "${metric?}{ metric ${metric}}" "${mtu?}{ mtu ${mtu}}${\\n}"
   /* Print only if hwtype emits something.  */
   "${exists?}{hwtype?}{"
   "${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{"
   "${hwaddr?}{ ${hwaddr}}}${\\n}}}"
   "${exists?}{tunnel?}{"
   "${tunnel?}{${\\t}tunnel inet ${tunsrc} --> ${tundst}${\\n}}}"
   "${addr?}{${\\t}inet ${addr}"
   "${dstaddr?}{ --> ${dstaddr}}"
   " netmask ${netmask}{0}{%#02x}${netmask}{1}{%02x}"
   "${netmask}{2}{%02x}${netmask}{3}{%02x}"
   "${brdaddr?}{ broadcast ${brdaddr}}${\\n}}"
   "${media?}{${\\t}media: ${media}${\\n}}"
   "${status?}{${\\t}status: ${status}${\\n}}" "}"},
  /* Resembles the output of ifconfig shipped with OSF 4.0g.  */
  {"osf",
   "OSF-style output.",
   "${format}{check-existence}"
   "${ifdisplay?}{"
   "${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>${\\n}"
   "${exists?}{tunnel?}{"
   "${tunnel?}{${\\t}inet tunnel src ${tunsrc} tunnel dst ${tundst}${\\n}}}"
   "${addr?}{${\\t}inet ${addr}"
   "${dstaddr?}{ --> ${dstaddr}}"
   " netmask ${netmask}{0}{%02x}${netmask}{1}{%02x}"
   "${netmask}{2}{%02x}${netmask}{3}{%02x}"
   "${brdaddr?}{ broadcast ${brdaddr}}" "${mtu?}{ ipmtu ${mtu}}${\\n}}"
   "${exists?}{hwtype?}{"
   "${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{"
   "${hwaddr?}{ ${hwaddr}}}${\\n}}}" "}"},
  {"check",
   "Shorthand for `check-existence'.",
   "${format}{check-existence}"},
  /* If interface does not exist, print error message and exit. */
  {"check-existence",
   "If interface does not exist, print error message and exit.",
   "${index?}{}"
   "{${error}{${progname}: error: interface `${name}' does not exist${\\n}}"
   "${exit}{1}}"},
  {"?",
   "Synonym for `help'.",
   "${format}{help}"},
  {"help",
   "Display this help output.",
   "${foreachformat}{"
   "${name}:${tab}{17}${docstr}"
   "${verbose?}{"
   "${newline}"
   "${rep}{79}{-}"
   "${newline}" "${defn}" "${newline}" "}" "${newline}" "}" "${exit}{0}"},
  {NULL, NULL, NULL}
};

/* Default format.  */
const char *default_format;
/* Display all interfaces, even if down */
int all_option;
/* True if interfaces were given in the command line */
int ifs_cmdline;

enum
{
  METRIC_OPTION = 256,
  FORMAT_OPTION,
  UP_OPTION,
  DOWN_OPTION,
};

static struct argp_option argp_options[] = {
#define GRP 10
  {"verbose", 'v', NULL, 0,
   "output information when configuring interface", GRP},
  {"all", 'a', NULL, 0,
   "display all available interfaces", GRP},
  {"interface", 'i', "NAME", 0,
   "configure network interface NAME", GRP},
  {"address", 'A', "ADDR", 0,
   "set interface address to ADDR", GRP},
  {"netmask", 'm', "MASK", 0,
   "set netmask to MASK", GRP},
  {"dstaddr", 'd', "ADDR", 0,
   "set destination (peer) address to ADDR", GRP},
  {"peer", 'p', "ADDR", OPTION_ALIAS,
   "synonym for dstaddr", GRP},
  {"broadcast", 'B', "ADDR", 0,
   "set broadcast address to ADDR", GRP},
  {"brdaddr", 'b', NULL, OPTION_ALIAS,
   "synonym for broadcast", GRP},	/* FIXME: Do we really need it? */
  {"mtu", 'M', "N", 0,
   "set mtu of interface to N", GRP},
  {"metric", METRIC_OPTION, "N", 0,
   "set metric of interface to N", GRP},
  {"format", FORMAT_OPTION, "FORMAT", 0,
   "select output format; set to `help' for info", GRP},
  {"up", UP_OPTION, NULL, 0,
   "activate the interface", GRP},
  {"down", DOWN_OPTION, NULL, 0,
   "shut the interface down", GRP},
  {"flags", 'F', "FLAG[,FLAG...]", 0,
   "set interface flags", GRP},
  {"list", 'l', NULL, 0,
   "list available or selected interfaces", GRP},
  {"short", 's', NULL, 0,
   "short output format", GRP},
#undef GRP
  {NULL, 0, NULL, 0, NULL, 0}
};

const char doc[] = "Configure network interfaces.";
const char *program_authors[] = {
  "Marcus Brinkmann",
  NULL
};

struct format *
format_find (const char *name)
{
  struct format *frm;

  for (frm = formats; frm->name; frm++)
    {
      if (strcmp (frm->name, name) == 0)
	return frm;
    }
  return NULL;
}

struct ifconfig *
parse_opt_new_ifs (char *name)
{
  struct ifconfig *ifp;

  ifs_cmdline = 1;
  ifs = realloc (ifs, ++nifs * sizeof (struct ifconfig));
  if (!ifs)
    error (EXIT_FAILURE, errno,
	   "can't get memory for interface configuration");
  ifp = &ifs[nifs - 1];
  *ifp = ifconfig_initializer;
  ifp->name = name;
  return ifp;
}

#define PARSE_OPT_SET_ADDR(field, fname, fvalid) \
void								\
parse_opt_set_##field (struct ifconfig *ifp, char *addr)	\
{								\
  if (!ifp)							\
    error (EXIT_FAILURE, 0,                                     \
	   "no interface specified for %s `%s'", #fname, addr); \
  if (ifp->valid & IF_VALID_##fvalid)				\
    error (EXIT_FAILURE, 0,                                     \
	   "only one %s allowed for interface `%s'",		\
	   #fname, ifp->name);				        \
  ifp->field = addr;						\
  ifp->valid |= IF_VALID_##fvalid;				\
}

PARSE_OPT_SET_ADDR (address, address, ADDR)
PARSE_OPT_SET_ADDR (netmask, netmask, NETMASK)
PARSE_OPT_SET_ADDR (dstaddr, destination / peer address, DSTADDR)
PARSE_OPT_SET_ADDR (brdaddr, broadcast address, BRDADDR)
PARSE_OPT_SET_ADDR (hwaddr, hardware address, HWADDR)
#define PARSE_OPT_SET_INT(field, fname, fvalid) \
void								\
parse_opt_set_##field (struct ifconfig *ifp, char *arg)		\
{								\
  char *end;							\
  if (!ifp)							\
    error (EXIT_FAILURE, 0,                                     \
	   "no interface specified for %s `%s'\n",              \
	   #fname, arg);                                        \
  if (ifp->valid & IF_VALID_##fvalid)				\
    error (EXIT_FAILURE, 0,                                     \
	   "only one %s allowed for interface `%s'",		\
	   #fname, ifp->name);				        \
  ifp->field =  strtol (arg, &end, 0);				\
  if (*arg == '\0' || *end != '\0')				\
    error (EXIT_FAILURE, 0,                                     \
	   "mtu value `%s' for interface `%s' is not a number",	\
	   arg, ifp->name);			                \
  ifp->valid |= IF_VALID_##fvalid;				\
}
  PARSE_OPT_SET_INT (mtu, mtu value, MTU)
PARSE_OPT_SET_INT (metric, metric value, METRIC)
     void parse_opt_set_af (struct ifconfig *ifp, char *af)
{
  if (!ifp)
    error (EXIT_FAILURE, 0,
	   "no interface specified for address family `%s'", af);

  if (!strcasecmp (af, "inet"))
    ifp->af = AF_INET;
  else
    error (EXIT_FAILURE, 0,
	   "unknown address family `%s' for interface `%s': is not a number",
	   af, ifp->name);
  ifp->valid |= IF_VALID_AF;
}

void
parse_opt_set_flag (struct ifconfig *ifp MAYBE_UNUSED, int flag, int rev)
{
  if (ifp)
    ifp->valid |= IF_VALID_FLAGS;
  else
    pending_valid |= IF_VALID_FLAGS;

  if (rev)
    {
      pending_clrflags |= flag;
      pending_setflags &= ~flag;
    }
  else
    {
      pending_setflags |= flag;
      pending_clrflags &= ~flag;
    }
}

void
parse_opt_flag_list (struct ifconfig *ifp, const char *name)
{
  while (*name)
    {
      int mask, rev;
      char *p = strchr (name, ',');
      size_t len;

      if (p)
	len = p - name;
      else
	len = strlen (name);

      if ((mask = if_nametoflag (name, len, &rev)) == 0)
	error (EXIT_FAILURE, 0, "unknown flag %*.*s",
	       (int) len, (int) len, name);
      parse_opt_set_flag (ifp, mask, rev);

      name += len;
      if (p)
	name++;
    }
}

void
parse_opt_set_point_to_point (struct ifconfig *ifp, char *addr)
{
  parse_opt_set_dstaddr (ifp, addr);
  parse_opt_set_flag (ifp, IFF_POINTOPOINT, 0);
}

void
parse_opt_set_default_format (const char *format)
{
  struct format *frm;

  if (!format)
    format = system_default_format ? system_default_format : "default";

  for (frm = formats; frm->name; frm++)
    if (!strcmp (format, frm->name))
      break;
  if (frm == NULL || frm->templ == NULL)
    error (EXIT_FAILURE, 0, "%s: unknown output format", format);
  default_format = frm->templ;
}

static int
is_comment_line (const char *p, size_t len)
{
  while (len--)
    {
      int c = *p++;
      switch (c)
	{
	case ' ':
	case '\t':
	  continue;

	case '#':
	  return 1;

	default:
	  return 0;
	}
    }
  return 0;
}

void
parse_opt_set_default_format_from_file (const char *file)
{
  static struct obstack stk;
  FILE *fp;
  char *buf = NULL;
  size_t size = 0;

  fp = fopen (file, "r");
  if (!fp)
    error (EXIT_FAILURE, errno, "cannot open format file %s", file);

  obstack_init (&stk);
  while (getline (&buf, &size, fp) > 0)
    {
      size_t len = strlen (buf);

      if (len >= 1 && buf[len - 1] == '\n')
	len--;

      if (len == 0 || is_comment_line (buf, len))
	continue;
      obstack_grow (&stk, buf, len);
    }
  free (buf);
  fclose (fp);
  obstack_1grow (&stk, 0);
  default_format = obstack_finish (&stk);
}

/* Must be reentrant!  */
void
parse_opt_finalize (struct ifconfig *ifp)
{
  /* The flags `--up' and `--down' are allowed early.  */
  if (ifp && pending_valid)
    {
      ifp->valid |= pending_valid;
      pending_valid = 0;
    }

  /* Only the empty set of actions, i.e., only the interface name
   * is present on the command line, merits printout of status.
   */
  if (ifp && !ifp->valid)
    {
      ifp->valid = IF_VALID_FORMAT;
      ifp->format = default_format;
    }

  if (ifp && (pending_setflags | pending_clrflags))
    {
      ifp->setflags |= pending_setflags;
      ifp->clrflags |= pending_clrflags;
      pending_setflags = pending_clrflags = 0;
    }
}

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct ifconfig *ifp = *(struct ifconfig **) state->input;

  switch (key)
    {
    case ARGP_KEY_INIT:
      state->child_inputs[0] = state->input;
      break;

    case 'i':			/* Interface name.  */
      parse_opt_finalize (ifp);
      ifp = parse_opt_new_ifs (arg);
      *(struct ifconfig **) state->input = ifp;
      break;

    case 'a':
      all_option = 1;
      break;

    case 'A':			/* Interface address.  */
      {
	char *netlen = strchr (arg, '/');

	if (netlen)
	  {
	    char *end, *str;
	    unsigned n;
	    struct in_addr addr;

	    *netlen++ = 0;

	    n = strtol (netlen, &end, 10);
	    if (!(*netlen && !*end) || n < 0 || n > 32)
	      error (EXIT_FAILURE, 0, "Wrong netmask length %s", netlen);

	    addr.s_addr =
	      n ? htonl (INADDR_BROADCAST << (32 - n)) : INADDR_ANY;
	    str = strdup (inet_ntoa (addr));
	    parse_opt_set_netmask (ifp, str);
	  }

	parse_opt_set_address (ifp, arg);
	break;
      }

    case 'm':			/* Interface netmask.  */
      parse_opt_set_netmask (ifp, arg);
      break;

    case 'd':			/* Interface dstaddr.  */
    case 'p':
      parse_opt_set_point_to_point (ifp, arg);
      break;

    case 'b':			/* Interface broadcast address.  */
    case 'B':
      parse_opt_set_brdaddr (ifp, arg);
      break;

    case 'F':
      parse_opt_flag_list (ifp, arg);
      break;

    case 'M':			/* Interface MTU.  */
      parse_opt_set_mtu (ifp, arg);
      break;

    case 's':
      parse_opt_set_default_format ("netstat");
      break;

    case 'l':
      list_mode++;
      break;

    case 'v':
      verbose++;
      break;

    case METRIC_OPTION:	/* Interface metric.  */
      parse_opt_set_metric (ifp, arg);
      break;

    case FORMAT_OPTION:	/* Output format.  */
      if (arg && arg[0] == '@')
	parse_opt_set_default_format_from_file (arg + 1);
      else
	parse_opt_set_default_format (arg);
      break;

    case UP_OPTION:
      parse_opt_set_flag (ifp, IFF_UP | IFF_RUNNING, 0);
      break;

    case DOWN_OPTION:
      parse_opt_set_flag (ifp, IFF_UP, 1);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return 0;
}

static char *
default_help_filter (int key, const char *text, void *input MAYBE_UNUSED)
{
  char *s;

  switch (key)
    {
    default:
      s = (char *) text;
      break;

    case ARGP_KEY_HELP_EXTRA:
      s = if_list_flags ("Known flags are: ");
    }
  return s;
}

static struct argp_child argp_children[2];
static struct argp argp = {
  argp_options,
  parse_opt,
  NULL,
  doc,
  NULL,
  default_help_filter,
  NULL
};

static int
cmp_if_name (const void *a, const void *b)
{
  const struct ifconfig *ifa = a;
  const struct ifconfig *ifb = b;

  return strcmp (ifa->name, ifb->name);
}

void
parse_cmdline (int argc, char *argv[])
{
  int index;
  struct ifconfig *ifp = ifs;

  parse_opt_set_default_format (NULL);
  iu_argp_init ("ifconfig", program_authors);
  argp_children[0] = system_argp_child;
  argp.children = argp_children;
  argp.args_doc = system_help;
  argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &index, &ifp);

  parse_opt_finalize (ifp);

  if (index < argc)
    {
      if (!system_parse_opt_rest (&ifp, argc - index, &argv[index]))
	error (EXIT_FAILURE, 0, "invalid arguments");
      parse_opt_finalize (ifp);
    }
  if (!ifs)
    {
      /* No interfaces specified.  Get a list of all interfaces.  */
      struct if_nameindex *ifnx, *ifnxp;

      ifnx = ifnxp = system_if_nameindex ();
      if (!ifnx)
	error (EXIT_FAILURE, 0, "could not get list of interfaces");
      while (ifnxp->if_index != 0 || ifnxp->if_name != NULL)
	{
	  struct ifconfig *ifp;

	  ifs = realloc (ifs, ++nifs * sizeof (struct ifconfig));
	  if (!ifs)
	    error (EXIT_FAILURE, errno,
		   "can't get memory for interface configuration");
	  ifp = &ifs[nifs - 1];
	  *ifp = ifconfig_initializer;
	  ifp->name = strdup (ifnxp->if_name);
	  if (!ifs)
	    error (EXIT_FAILURE, errno,
		   "can't get memory for interface configuration name");
	  ifp->valid = IF_VALID_FORMAT;
	  ifp->format = default_format;
	  ifnxp++;
	}
      if_freenameindex (ifnx);
      qsort (ifs, nifs, sizeof (ifs[0]), cmp_if_name);
    }
}