import source from 1.3.40
[external/swig.git] / Source / Swig / getopt.c
1 /* ----------------------------------------------------------------------------- 
2  * See the LICENSE file for information on copyright, usage and redistribution
3  * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4  *
5  * getopt.c
6  *
7  * Handles the parsing of command line options.  This is particularly nasty
8  * compared to other utilities given that command line options can potentially
9  * be read by many different modules within SWIG.  Thus, in order to make sure
10  * there are no unrecognized options, each module is required to "mark"
11  * the options that it uses.  Afterwards, we can make a quick scan to make
12  * sure there are no unmarked options.
13  * 
14  * TODO: 
15  *     - This module needs to be modified so that it doesn't call exit().
16  *       Should have cleaner error handling in general.
17  * ----------------------------------------------------------------------------- */
18
19 char cvsroot_getopt_c[] = "$Id: getopt.c 10926 2008-11-11 22:17:40Z wsfulton $";
20
21 #include "swig.h"
22
23 static char **args;
24 static int numargs;
25 static int *marked;
26
27 /* -----------------------------------------------------------------------------
28  * Swig_init_args()
29  * 
30  * Initialize the argument list handler.
31  * ----------------------------------------------------------------------------- */
32
33 void Swig_init_args(int argc, char **argv) {
34   int i;
35   assert(argc > 0);
36   assert(argv);
37
38   numargs = argc;
39   args = argv;
40   marked = (int *) malloc(numargs * sizeof(int));
41   for (i = 0; i < argc; i++) {
42     marked[i] = 0;
43   }
44   marked[0] = 1;
45 }
46
47 /* -----------------------------------------------------------------------------
48  * Swig_mark_arg()
49  * 
50  * Marks an argument as being parsed.
51  * ----------------------------------------------------------------------------- */
52
53 void Swig_mark_arg(int n) {
54   assert(marked);
55   assert((n >= 0) && (n < numargs));
56   marked[n] = 1;
57 }
58
59 /* -----------------------------------------------------------------------------
60  * Swig_check_marked()
61  *
62  * Checks to see if argument has been picked up.
63  * ----------------------------------------------------------------------------- */
64
65 int Swig_check_marked(int n) {
66   assert((n >= 0) && (n < numargs));
67   return marked[n];
68 }
69
70 /* -----------------------------------------------------------------------------
71  * Swig_check_options()
72  * 
73  * Checkers for unprocessed command line options and errors.
74  * ----------------------------------------------------------------------------- */
75
76 void Swig_check_options(int check_input) {
77   int error = 0;
78   int i;
79   int max = check_input ? numargs - 1 : numargs;
80   assert(marked);
81   for (i = 1; i < max; i++) {
82     if (!marked[i]) {
83       Printf(stderr, "swig error : Unrecognized option %s\n", args[i]);
84       error = 1;
85     }
86   }
87   if (error) {
88     Printf(stderr, "Use 'swig -help' for available options.\n");
89     exit(1);
90   }
91   if (check_input && marked[numargs - 1]) {
92     Printf(stderr, "Must specify an input file. Use -help for available options.\n");
93     exit(1);
94   }
95 }
96
97 /* -----------------------------------------------------------------------------
98  * Swig_arg_error()
99  * 
100  * Generates a generic error message and exits.
101  * ----------------------------------------------------------------------------- */
102
103 void Swig_arg_error(void) {
104   Printf(stderr, "SWIG : Unable to parse command line options.\n");
105   Printf(stderr, "Use 'swig -help' for available options.\n");
106   exit(1);
107 }