ip_parse_common_args.c 1.7 KB

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