You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2003-2007 Jim Paris <jim@jtan.com>
  3. *
  4. * This is free software; you can redistribute it and/or modify it and
  5. * it is provided under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation; see COPYING.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "opt.h"
  12. void opt_init(int *optind)
  13. {
  14. *optind = 0;
  15. }
  16. char
  17. opt_parse(int argc, char **argv, int *optind, char **optarg,
  18. struct options *opt)
  19. {
  20. char c;
  21. int i;
  22. (*optind)++;
  23. if (*optind >= argc)
  24. return 0;
  25. if (argv[*optind][0] == '-' &&
  26. argv[*optind][1] != '-' && argv[*optind][1] != 0) {
  27. /* Short option (or a bunch of 'em) */
  28. /* Save this and shift others over */
  29. c = argv[*optind][1];
  30. for (i = 2; argv[*optind][i] != 0; i++)
  31. argv[*optind][i - 1] = argv[*optind][i];
  32. argv[*optind][i - 1] = 0;
  33. if (argv[*optind][1] != 0)
  34. (*optind)--;
  35. /* Now find it */
  36. for (i = 0; opt[i].shortopt != 0; i++)
  37. if (opt[i].shortopt == c)
  38. break;
  39. if (opt[i].shortopt == 0) {
  40. fprintf(stderr, "Error: unknown option '-%c'\n", c);
  41. return '?';
  42. }
  43. if (opt[i].arg == NULL)
  44. return c;
  45. (*optind)++;
  46. if (*optind >= argc || (argv[*optind][0] == '-' &&
  47. argv[*optind][1] != 0)) {
  48. fprintf(stderr, "Error: option '-%c' requires an "
  49. "argument\n", c);
  50. return '?';
  51. }
  52. (*optarg) = argv[*optind];
  53. return c;
  54. } else if (argv[*optind][0] == '-' &&
  55. argv[*optind][1] == '-' && argv[*optind][2] != 0) {
  56. /* Long option */
  57. for (i = 0; (c = opt[i].shortopt) != 0; i++)
  58. if (strcmp(opt[i].longopt, argv[*optind] + 2) == 0)
  59. break;
  60. if (opt[i].shortopt == 0) {
  61. fprintf(stderr, "Error: unknown option '%s'\n",
  62. argv[*optind]);
  63. return '?';
  64. }
  65. if (opt[i].arg == NULL)
  66. return c;
  67. (*optind)++;
  68. if (*optind >= argc || (argv[*optind][0] == '-' &&
  69. argv[*optind][1] != 0)) {
  70. fprintf(stderr, "Error: option '%s' requires an "
  71. "argument\n", argv[*optind - 1]);
  72. return '?';
  73. }
  74. (*optarg) = argv[*optind];
  75. return c;
  76. } else {
  77. /* End of options */
  78. return 0;
  79. }
  80. }
  81. void opt_help(struct options *opt, FILE * out)
  82. {
  83. int i;
  84. int printed;
  85. for (i = 0; opt[i].shortopt != 0; i++) {
  86. fprintf(out, " -%c, --%s%n", opt[i].shortopt,
  87. opt[i].longopt, &printed);
  88. fprintf(out, " %-*s%s\n", 30 - printed,
  89. opt[i].arg ? opt[i].arg : "", opt[i].help);
  90. }
  91. }