PageRenderTime 72ms CodeModel.GetById 65ms app.highlight 4ms 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
  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
 23char cvsroot_getopt_c[] = "$Id: getopt.c 11876 2010-02-27 23:53:33Z wsfulton $";
 24
 25#include "swig.h"
 26
 27static char **args;
 28static int numargs;
 29static int *marked;
 30
 31/* -----------------------------------------------------------------------------
 32 * Swig_init_args()
 33 * 
 34 * Initialize the argument list handler.
 35 * ----------------------------------------------------------------------------- */
 36
 37void Swig_init_args(int argc, char **argv) {
 38  int i;
 39  assert(argc > 0);
 40  assert(argv);
 41
 42  numargs = argc;
 43  args = argv;
 44  marked = (int *) malloc(numargs * sizeof(int));
 45  for (i = 0; i < argc; i++) {
 46    marked[i] = 0;
 47  }
 48  marked[0] = 1;
 49}
 50
 51/* -----------------------------------------------------------------------------
 52 * Swig_mark_arg()
 53 * 
 54 * Marks an argument as being parsed.
 55 * ----------------------------------------------------------------------------- */
 56
 57void Swig_mark_arg(int n) {
 58  assert(marked);
 59  assert((n >= 0) && (n < numargs));
 60  marked[n] = 1;
 61}
 62
 63/* -----------------------------------------------------------------------------
 64 * Swig_check_marked()
 65 *
 66 * Checks to see if argument has been picked up.
 67 * ----------------------------------------------------------------------------- */
 68
 69int Swig_check_marked(int n) {
 70  assert((n >= 0) && (n < numargs));
 71  return marked[n];
 72}
 73
 74/* -----------------------------------------------------------------------------
 75 * Swig_check_options()
 76 * 
 77 * Checkers for unprocessed command line options and errors.
 78 * ----------------------------------------------------------------------------- */
 79
 80void Swig_check_options(int check_input) {
 81  int error = 0;
 82  int i;
 83  int max = check_input ? numargs - 1 : numargs;
 84  assert(marked);
 85  for (i = 1; i < max; i++) {
 86    if (!marked[i]) {
 87      Printf(stderr, "swig error : Unrecognized option %s\n", args[i]);
 88      error = 1;
 89    }
 90  }
 91  if (error) {
 92    Printf(stderr, "Use 'swig -help' for available options.\n");
 93    exit(1);
 94  }
 95  if (check_input && marked[numargs - 1]) {
 96    Printf(stderr, "Must specify an input file. Use -help for available options.\n");
 97    exit(1);
 98  }
 99}
100
101/* -----------------------------------------------------------------------------
102 * Swig_arg_error()
103 * 
104 * Generates a generic error message and exits.
105 * ----------------------------------------------------------------------------- */
106
107void Swig_arg_error(void) {
108  Printf(stderr, "SWIG : Unable to parse command line options.\n");
109  Printf(stderr, "Use 'swig -help' for available options.\n");
110  exit(1);
111}