PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/Swig/getopt.c

#
C | 111 lines | 51 code | 14 blank | 46 comment | 8 complexity | d905b6d85a6f9e4a377afb207b48f254 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * This file is part of SWIG, which is licensed as a whole under version 3
  3. * (or any later version) of the GNU General Public License. Some additional
  4. * terms also apply to certain portions of SWIG. The full details of the SWIG
  5. * license and copyrights can be found in the LICENSE and COPYRIGHT files
  6. * included with the SWIG source code as distributed by the SWIG developers
  7. * and at http://www.swig.org/legal.html.
  8. *
  9. * getopt.c
  10. *
  11. * Handles the parsing of command line options. This is particularly nasty
  12. * compared to other utilities given that command line options can potentially
  13. * be read by many different modules within SWIG. Thus, in order to make sure
  14. * there are no unrecognized options, each module is required to "mark"
  15. * the options that it uses. Afterwards, we can make a quick scan to make
  16. * sure there are no unmarked options.
  17. *
  18. * TODO:
  19. * - This module needs to be modified so that it doesn't call exit().
  20. * Should have cleaner error handling in general.
  21. * ----------------------------------------------------------------------------- */
  22. char cvsroot_getopt_c[] = "$Id: getopt.c 11876 2010-02-27 23:53:33Z wsfulton $";
  23. #include "swig.h"
  24. static char **args;
  25. static int numargs;
  26. static int *marked;
  27. /* -----------------------------------------------------------------------------
  28. * Swig_init_args()
  29. *
  30. * Initialize the argument list handler.
  31. * ----------------------------------------------------------------------------- */
  32. void Swig_init_args(int argc, char **argv) {
  33. int i;
  34. assert(argc > 0);
  35. assert(argv);
  36. numargs = argc;
  37. args = argv;
  38. marked = (int *) malloc(numargs * sizeof(int));
  39. for (i = 0; i < argc; i++) {
  40. marked[i] = 0;
  41. }
  42. marked[0] = 1;
  43. }
  44. /* -----------------------------------------------------------------------------
  45. * Swig_mark_arg()
  46. *
  47. * Marks an argument as being parsed.
  48. * ----------------------------------------------------------------------------- */
  49. void Swig_mark_arg(int n) {
  50. assert(marked);
  51. assert((n >= 0) && (n < numargs));
  52. marked[n] = 1;
  53. }
  54. /* -----------------------------------------------------------------------------
  55. * Swig_check_marked()
  56. *
  57. * Checks to see if argument has been picked up.
  58. * ----------------------------------------------------------------------------- */
  59. int Swig_check_marked(int n) {
  60. assert((n >= 0) && (n < numargs));
  61. return marked[n];
  62. }
  63. /* -----------------------------------------------------------------------------
  64. * Swig_check_options()
  65. *
  66. * Checkers for unprocessed command line options and errors.
  67. * ----------------------------------------------------------------------------- */
  68. void Swig_check_options(int check_input) {
  69. int error = 0;
  70. int i;
  71. int max = check_input ? numargs - 1 : numargs;
  72. assert(marked);
  73. for (i = 1; i < max; i++) {
  74. if (!marked[i]) {
  75. Printf(stderr, "swig error : Unrecognized option %s\n", args[i]);
  76. error = 1;
  77. }
  78. }
  79. if (error) {
  80. Printf(stderr, "Use 'swig -help' for available options.\n");
  81. exit(1);
  82. }
  83. if (check_input && marked[numargs - 1]) {
  84. Printf(stderr, "Must specify an input file. Use -help for available options.\n");
  85. exit(1);
  86. }
  87. }
  88. /* -----------------------------------------------------------------------------
  89. * Swig_arg_error()
  90. *
  91. * Generates a generic error message and exits.
  92. * ----------------------------------------------------------------------------- */
  93. void Swig_arg_error(void) {
  94. Printf(stderr, "SWIG : Unable to parse command line options.\n");
  95. Printf(stderr, "Use 'swig -help' for available options.\n");
  96. exit(1);
  97. }