ip_parse_common_args.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. int preferred_family = AF_UNSPEC;
  20. smallint oneline;
  21. char _SL_;
  22. void ip_parse_common_args(int *argcp, char ***argvp)
  23. {
  24. int argc = *argcp;
  25. char **argv = *argvp;
  26. static const char ip_common_commands[] ALIGN1 =
  27. "-family\0""inet\0""inet6\0""link\0"
  28. "-4\0""-6\0""-0\0""-oneline\0";
  29. enum {
  30. ARG_family = 1,
  31. ARG_inet,
  32. ARG_inet6,
  33. ARG_link,
  34. ARG_IPv4,
  35. ARG_IPv6,
  36. ARG_packet,
  37. ARG_oneline
  38. };
  39. smalluint arg;
  40. while (argc > 1) {
  41. char *opt = argv[1];
  42. if (strcmp(opt,"--") == 0) {
  43. argc--;
  44. argv++;
  45. break;
  46. }
  47. if (opt[0] != '-')
  48. break;
  49. if (opt[1] == '-')
  50. opt++;
  51. arg = index_in_strings(ip_common_commands, opt) + 1;
  52. if (arg == ARG_family) {
  53. argc--;
  54. argv++;
  55. if (!argv[1])
  56. bb_show_usage();
  57. arg = index_in_strings(ip_common_commands, argv[1]) + 1;
  58. if (arg == ARG_inet)
  59. preferred_family = AF_INET;
  60. else if (arg == ARG_inet6)
  61. preferred_family = AF_INET6;
  62. else if (arg == ARG_link)
  63. preferred_family = AF_PACKET;
  64. else
  65. invarg(argv[1], "protocol family");
  66. } else if (arg == ARG_IPv4) {
  67. preferred_family = AF_INET;
  68. } else if (arg == ARG_IPv6) {
  69. preferred_family = AF_INET6;
  70. } else if (arg == ARG_packet) {
  71. preferred_family = AF_PACKET;
  72. } else if (arg == ARG_oneline) {
  73. ++oneline;
  74. } else {
  75. bb_show_usage();
  76. }
  77. argc--;
  78. argv++;
  79. }
  80. _SL_ = oneline ? '\\' : '\n';
  81. *argcp = argc;
  82. *argvp = argv;
  83. }