Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / typelib / xpidl / xpidl.c
blob145059690076b072f9e4afff4c0e34e281f51df1
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * Main xpidl program entry point.
42 #include "xpidl.h"
44 static ModeData modes[] = {
45 {"header", "Generate C++ header", "h", xpidl_header_dispatch},
46 {"typelib", "Generate XPConnect typelib", "xpt", xpidl_typelib_dispatch},
47 {"doc", "Generate HTML documentation", "html", xpidl_doc_dispatch},
48 {"java", "Generate Java interface", "java", xpidl_java_dispatch},
49 {0, 0, 0, 0}
52 static ModeData *
53 FindMode(char *mode)
55 int i;
56 for (i = 0; modes[i].mode; i++) {
57 if (!strcmp(modes[i].mode, mode))
58 return &modes[i];
60 return NULL;
63 gboolean enable_debug = FALSE;
64 gboolean enable_warnings = FALSE;
65 gboolean verbose_mode = FALSE;
66 gboolean emit_typelib_annotations = FALSE;
67 gboolean explicit_output_filename = FALSE;
68 FILE *deps = NULL;
70 /* The following globals are explained in xpt_struct.h */
71 PRUint8 major_version = XPT_MAJOR_VERSION;
72 PRUint8 minor_version = XPT_MINOR_VERSION;
74 static char xpidl_usage_str[] =
75 "Usage: %s -m mode [-w] [-v] [-t version number] [-d filename.pp]\n"
76 " [-I path] [-o basename | -e filename.ext] filename.idl\n"
77 " -a emit annotations to typelib\n"
78 " -w turn on warnings (recommended)\n"
79 " -v verbose mode (NYI)\n"
80 " -t create a typelib of a specific version number\n"
81 " -I add entry to start of include path for ``#include \"nsIThing.idl\"''\n"
82 " -o use basename (e.g. ``/tmp/nsIThing'') for output\n"
83 " -e use explicit output filename\n"
84 " -d write dependencies (requires -e)\n"
85 " -m specify output mode:\n";
87 static void
88 xpidl_usage(int argc, char *argv[])
90 int i;
91 fprintf(stderr, xpidl_usage_str, argv[0]);
92 for (i = 0; modes[i].mode; i++) {
93 fprintf(stderr, " %-12s %-30s (.%s)\n", modes[i].mode,
94 modes[i].modeInfo, modes[i].suffix);
98 int main(int argc, char *argv[])
100 int i;
101 IncludePathEntry *inc, *inc_head, **inc_tail;
102 char *file_basename = NULL;
103 ModeData *mode = NULL;
104 gboolean create_old_typelib = FALSE;
106 /* turn this on for extra checking of our code */
107 /* IDL_check_cast_enable(TRUE); */
109 inc_head = xpidl_malloc(sizeof *inc);
110 inc_head->directory = ".";
111 inc_head->next = NULL;
112 inc_tail = &inc_head->next;
114 for (i = 1; i < argc; i++) {
115 if (argv[i][0] != '-')
116 break;
117 switch (argv[i][1]) {
118 case '-':
119 argc++; /* pretend we didn't see this */
120 /* fall through */
121 case 0: /* - is a legal input filename (stdin) */
122 goto done_options;
123 case 'a':
124 emit_typelib_annotations = TRUE;
125 break;
126 case 'w':
127 enable_warnings = TRUE;
128 break;
129 case 'v':
130 verbose_mode = TRUE;
131 break;
132 case 't':
134 /* Parse for "-t version number" and store it into global boolean
135 * and string variables.
137 const gchar* typelib_version_string = NULL;
140 * If -t is the last argument on the command line, we have a problem
143 if (i + 1 == argc) {
144 fprintf(stderr, "ERROR: missing version number after -t\n");
145 xpidl_usage(argc, argv);
146 return 1;
149 /* Do not allow more than one "-t" definition */
150 if (create_old_typelib) {
151 fprintf(stderr,
152 "ERROR: -t argument used twice. "
153 "Cannot specify more than one version\n");
154 xpidl_usage(argc, argv);
155 return 1;
159 * Assume that the argument after "-t" is the version number string
160 * and search for it in our internal list of acceptable version
161 * numbers.
163 switch (XPT_ParseVersionString(argv[++i], &major_version,
164 &minor_version)) {
165 case XPT_VERSION_CURRENT:
166 break;
167 case XPT_VERSION_OLD:
168 create_old_typelib = TRUE;
169 break;
170 case XPT_VERSION_UNSUPPORTED:
171 fprintf(stderr, "ERROR: version \"%s\" not supported.\n",
172 argv[i]);
173 xpidl_usage(argc, argv);
174 return 1;
175 case XPT_VERSION_UNKNOWN:
176 default:
177 fprintf(stderr, "ERROR: version \"%s\" not recognised.\n",
178 argv[i]);
179 xpidl_usage(argc, argv);
180 return 1;
182 break;
184 case 'I':
185 if (argv[i][2] == '\0' && i == argc) {
186 fputs("ERROR: missing path after -I\n", stderr);
187 xpidl_usage(argc, argv);
188 return 1;
190 inc = xpidl_malloc(sizeof *inc);
191 if (argv[i][2] == '\0') {
192 /* is it the -I foo form? */
193 inc->directory = argv[++i];
194 } else {
195 /* must be the -Ifoo form. Don't preincrement i. */
196 inc->directory = argv[i] + 2;
198 #ifdef DEBUG_shaver_includes
199 fprintf(stderr, "adding %s to include path\n", inc->directory);
200 #endif
201 inc->next = NULL;
202 *inc_tail = inc;
203 inc_tail = &inc->next;
204 break;
205 case 'o':
206 if (i == argc) {
207 fprintf(stderr, "ERROR: missing basename after -o\n");
208 xpidl_usage(argc, argv);
209 return 1;
211 file_basename = argv[++i];
212 explicit_output_filename = FALSE;
213 break;
214 case 'e':
215 if (i == argc) {
216 fprintf(stderr, "ERROR: missing basename after -e\n");
217 xpidl_usage(argc, argv);
218 return 1;
220 file_basename = argv[++i];
221 explicit_output_filename = TRUE;
222 break;
223 case 'd':
224 if (!explicit_output_filename) {
225 fprintf(stderr, "ERROR: -d requires -e\n");
226 xpidl_usage(argc, argv);
227 return 1;
229 if (i == argc) {
230 fprintf(stderr, "ERROR: missing filename after -d\n");
231 xpidl_usage(argc, argv);
232 return 1;
234 deps = fopen(argv[++i], "w");
235 if (deps)
236 fprintf(deps, "%s:", file_basename);
237 break;
238 case 'm':
239 if (i + 1 == argc) {
240 fprintf(stderr, "ERROR: missing modename after -m\n");
241 xpidl_usage(argc, argv);
242 return 1;
244 if (mode) {
245 fprintf(stderr,
246 "ERROR: must specify exactly one mode "
247 "(first \"%s\", now \"%s\")\n", mode->mode,
248 argv[i + 1]);
249 xpidl_usage(argc, argv);
250 return 1;
252 mode = FindMode(argv[++i]);
253 if (!mode) {
254 fprintf(stderr, "ERROR: unknown mode \"%s\"\n", argv[i]);
255 xpidl_usage(argc, argv);
256 return 1;
258 break;
259 default:
260 fprintf(stderr, "unknown option %s\n", argv[i]);
261 xpidl_usage(argc, argv);
262 return 1;
265 done_options:
266 if (!mode) {
267 fprintf(stderr, "ERROR: must specify output mode\n");
268 xpidl_usage(argc, argv);
269 return 1;
273 * Don't try to process multiple files, given that we don't handle -o
274 * multiply.
276 if (i < argc) {
277 i = xpidl_process_idl(argv[i], inc_head, file_basename, mode);
278 } else {
279 if (argc > i + 1) {
280 fprintf(stderr, "ERROR: extra arguments after input file\n");
281 } else {
282 fprintf(stderr, "ERROR: no file to process\n");
284 return 1;
287 if (deps) {
288 fprintf(deps, "\n");
289 fclose(deps);
292 return !i;