busybox/networking/libiproute/ip_parse_common_args.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * This program is free software; you can redistribute it and/or
   4 * modify it under the terms of the GNU General Public License
   5 * as published by the Free Software Foundation; either version
   6 * 2 of the License, or (at your option) any later version.
   7 *
   8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
   9 *
  10 * Changes:
  11 *
  12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  13 */
  14
  15#include "ip_common.h"  /* #include "libbb.h" is inside */
  16#include "utils.h"
  17
  18family_t preferred_family = AF_UNSPEC;
  19smallint oneline;
  20char _SL_;
  21
  22char** FAST_FUNC ip_parse_common_args(char **argv)
  23{
  24        static const char ip_common_commands[] ALIGN1 =
  25                "oneline" "\0"
  26                "family" "\0"
  27                "4" "\0"
  28                "6" "\0"
  29                "0" "\0"
  30                ;
  31        enum {
  32                ARG_oneline,
  33                ARG_family,
  34                ARG_IPv4,
  35                ARG_IPv6,
  36                ARG_packet,
  37        };
  38        static const family_t af_numbers[] = { AF_INET, AF_INET6, AF_PACKET };
  39        int arg;
  40
  41        while (*argv) {
  42                char *opt = *argv;
  43
  44                if (opt[0] != '-')
  45                        break;
  46                opt++;
  47                if (opt[0] == '-') {
  48                        opt++;
  49                        if (!opt[0]) { /* "--" */
  50                                argv++;
  51                                break;
  52                        }
  53                }
  54                arg = index_in_substrings(ip_common_commands, opt);
  55                if (arg < 0)
  56                        bb_show_usage();
  57                if (arg == ARG_oneline) {
  58                        oneline = 1;
  59                        argv++;
  60                        continue;
  61                }
  62                if (arg == ARG_family) {
  63                        static const char families[] ALIGN1 =
  64                                "inet" "\0" "inet6" "\0" "link" "\0";
  65                        argv++;
  66                        if (!*argv)
  67                                bb_show_usage();
  68                        arg = index_in_strings(families, *argv);
  69                        if (arg < 0)
  70                                invarg(*argv, "protocol family");
  71                        /* now arg == 0, 1 or 2 */
  72                } else {
  73                        arg -= ARG_IPv4;
  74                        /* now arg == 0, 1 or 2 */
  75                }
  76                preferred_family = af_numbers[arg];
  77                argv++;
  78        }
  79        _SL_ = oneline ? '\\' : '\n';
  80        return argv;
  81}
  82