tasm-options.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Generic Options Support Header File
  3. *
  4. * Copyright (c) 2001 Stanislav Karchebny <berk@madfire.net>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the names of other contributors
  15. * may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <util.h>
  31. #include <ctype.h>
  32. #include "tasm-options.h"
  33. #ifdef __DEBUG__
  34. #define DEBUG(x) fprintf ## x ;
  35. #else
  36. #define DEBUG(x)
  37. #endif
  38. /* Options Parser */
  39. int
  40. parse_cmdline(int argc, char **argv, opt_option *options, size_t nopts,
  41. void (*print_error) (const char *fmt, ...))
  42. {
  43. int errors = 0, warnings = 0;
  44. size_t i;
  45. int got_it;
  46. DEBUG((stderr, "parse_cmdline: entered\n"));
  47. fail:
  48. while (--argc) {
  49. argv++;
  50. if (argv[0][0] == '/' && argv[0][1]) { /* opt */
  51. got_it = 0;
  52. for (i = 0; i < nopts; i++) {
  53. char *cmd = &argv[0][1];
  54. size_t len = strlen(options[i].opt);
  55. if (yasm__strncasecmp(cmd, options[i].opt, len) == 0) {
  56. char *param;
  57. param = &argv[0][1+len];
  58. if (options[i].takes_param) {
  59. if (param[0] == '\0') {
  60. print_error(
  61. _("option `-%c' needs an argument!"),
  62. options[i].opt);
  63. errors++;
  64. goto fail;
  65. } else {
  66. argc--;
  67. argv++;
  68. }
  69. } else
  70. param = NULL;
  71. if (!options[i].handler(cmd, param, options[i].extra))
  72. got_it = 1;
  73. break;
  74. }
  75. }
  76. if (!got_it) {
  77. print_error(_("warning: unrecognized option `%s'"),
  78. argv[0]);
  79. warnings++;
  80. }
  81. } else { /* not an option, then it should be a file or something */
  82. if (not_an_option_handler(argv[0]))
  83. errors++;
  84. }
  85. }
  86. DEBUG((stderr, "parse_cmdline: finished\n"));
  87. return errors;
  88. }
  89. void
  90. help_msg(const char *msg, const char *tail, opt_option *options, size_t nopts)
  91. {
  92. char optbuf[100];
  93. size_t i;
  94. printf("%s", gettext(msg));
  95. for (i = 0; i < nopts; i++) {
  96. optbuf[0] = 0;
  97. if (options[i].takes_param) {
  98. if (options[i].opt)
  99. sprintf(optbuf, "/%s <%s>", options[i].opt,
  100. options[i].param_desc ? options[i].
  101. param_desc : _("param"));
  102. } else {
  103. if (options[i].opt)
  104. sprintf(optbuf, "/%s", options[i].opt);
  105. }
  106. printf(" %-22s %s\n", optbuf, gettext(options[i].description));
  107. }
  108. printf("%s", gettext(tail));
  109. }