3
0

ip_parse_common_args.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "ip_common.h" /* #include "libbb.h" is inside */
  15. #include "utils.h"
  16. family_t preferred_family = AF_UNSPEC;
  17. smallint oneline;
  18. char _SL_;
  19. char** FAST_FUNC ip_parse_common_args(char **argv)
  20. {
  21. static const char ip_common_commands[] ALIGN1 =
  22. "oneline" "\0"
  23. "family" "\0"
  24. "4" "\0"
  25. "6" "\0"
  26. "0" "\0"
  27. ;
  28. enum {
  29. ARG_oneline,
  30. ARG_family,
  31. ARG_IPv4,
  32. ARG_IPv6,
  33. ARG_packet,
  34. };
  35. static const family_t af_numbers[] = { AF_INET, AF_INET6, AF_PACKET };
  36. int arg;
  37. while (*argv) {
  38. char *opt = *argv;
  39. if (opt[0] != '-')
  40. break;
  41. opt++;
  42. if (opt[0] == '-') {
  43. opt++;
  44. if (!opt[0]) { /* "--" */
  45. argv++;
  46. break;
  47. }
  48. }
  49. arg = index_in_substrings(ip_common_commands, opt);
  50. if (arg < 0)
  51. bb_show_usage();
  52. if (arg == ARG_oneline) {
  53. oneline = 1;
  54. argv++;
  55. continue;
  56. }
  57. if (arg == ARG_family) {
  58. static const char families[] ALIGN1 =
  59. "inet" "\0" "inet6" "\0" "link" "\0";
  60. argv++;
  61. if (!*argv)
  62. bb_show_usage();
  63. arg = index_in_strings(families, *argv);
  64. if (arg < 0)
  65. invarg_1_to_2(*argv, "family");
  66. /* now arg == 0, 1 or 2 */
  67. } else {
  68. arg -= ARG_IPv4;
  69. /* now arg == 0, 1 or 2 */
  70. }
  71. preferred_family = af_numbers[arg];
  72. argv++;
  73. }
  74. _SL_ = oneline ? '\\' : '\n';
  75. return argv;
  76. }