| 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|---|
| 2 | /* ***** BEGIN LICENSE BLOCK ***** |
|---|
| 3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
|---|
| 4 | * |
|---|
| 5 | * The contents of this file are subject to the Netscape Public License |
|---|
| 6 | * Version 1.1 (the "License"); you may not use this file except in |
|---|
| 7 | * compliance with the License. You may obtain a copy of the License at |
|---|
| 8 | * http://www.mozilla.org/NPL/ |
|---|
| 9 | * |
|---|
| 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. |
|---|
| 14 | * |
|---|
| 15 | * The Original Code is mozilla.org code. |
|---|
| 16 | * |
|---|
| 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. |
|---|
| 21 | * |
|---|
| 22 | * Contributor(s): |
|---|
| 23 | * Itou Takanori <necottie@nesitive.net> |
|---|
| 24 | * |
|---|
| 25 | * Alternatively, the contents of this file may be used under the terms of |
|---|
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
|---|
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|---|
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
|---|
| 29 | * of those above. If you wish to allow use of your version of this file only |
|---|
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
|---|
| 31 | * use your version of this file under the terms of the NPL, indicate your |
|---|
| 32 | * decision by deleting the provisions above and replace them with the notice |
|---|
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
|---|
| 34 | * the provisions above, a recipient may use your version of this file under |
|---|
| 35 | * the terms of any one of the NPL, the GPL or the LGPL. |
|---|
| 36 | * |
|---|
| 37 | * ***** END LICENSE BLOCK ***** */ |
|---|
| 38 | |
|---|
| 39 | /* |
|---|
| 40 | * Main xpidl program entry point. |
|---|
| 41 | */ |
|---|
| 42 | |
|---|
| 43 | #include "xpidlpas.h" |
|---|
| 44 | |
|---|
| 45 | static ModeData modes[] = { |
|---|
| 46 | {"stdcall", "Generate stdcall classes", "pas", xpidl_stdcall_dispatch}, |
|---|
| 47 | {"safecall", "Generate safecall classes","pas", xpidl_safecall_dispatch}, |
|---|
| 48 | {0, 0, 0, 0} |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | static ModeData * |
|---|
| 52 | FindMode(char *mode) |
|---|
| 53 | { |
|---|
| 54 | int i; |
|---|
| 55 | for (i = 0; modes[i].mode; i++) { |
|---|
| 56 | if (!strcmp(modes[i].mode, mode)) |
|---|
| 57 | return &modes[i]; |
|---|
| 58 | } |
|---|
| 59 | return NULL; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | gboolean enable_debug = FALSE; |
|---|
| 63 | gboolean enable_warnings = FALSE; |
|---|
| 64 | gboolean verbose_mode = FALSE; |
|---|
| 65 | gboolean emit_typelib_annotations = FALSE; |
|---|
| 66 | gboolean explicit_output_filename = FALSE; |
|---|
| 67 | gint comment_level = 0; |
|---|
| 68 | |
|---|
| 69 | /* The following globals are explained in xpt_struct.h */ |
|---|
| 70 | PRUint8 major_version = XPT_MAJOR_VERSION; |
|---|
| 71 | PRUint8 minor_version = XPT_MINOR_VERSION; |
|---|
| 72 | |
|---|
| 73 | GSList *uses_units = NULL; |
|---|
| 74 | |
|---|
| 75 | static char xpidl_usage_str[] = |
|---|
| 76 | "Usage: %s -m mode [-w] [-v] [-t version number] [-c comment level]\n" |
|---|
| 77 | " [-u unitname]\n" |
|---|
| 78 | " [-I path] [-o basename | -e filename.ext] filename.idl\n" |
|---|
| 79 | " -a emit annotations to typelib\n" |
|---|
| 80 | " -w turn on warnings (recommended)\n" |
|---|
| 81 | " -v verbose mode (NYI)\n" |
|---|
| 82 | " -t create a typelib of a specific version number\n" |
|---|
| 83 | " -c change comment leve:\n" |
|---|
| 84 | " 0:nothing 1:file name only 2:full comment\n" |
|---|
| 85 | " -u unit name for 'USES'\n" |
|---|
| 86 | " -I add entry to start of include path for ``#include \"nsIThing.idl\"''\n" |
|---|
| 87 | " -o use basename (e.g. ``/tmp/nsIThing'') for output\n" |
|---|
| 88 | " -e use explicit output filename\n" |
|---|
| 89 | " -m specify output mode:\n"; |
|---|
| 90 | |
|---|
| 91 | static void |
|---|
| 92 | xpidl_usage(int argc, char *argv[]) |
|---|
| 93 | { |
|---|
| 94 | int i; |
|---|
| 95 | fprintf(stderr, xpidl_usage_str, argv[0]); |
|---|
| 96 | for (i = 0; modes[i].mode; i++) { |
|---|
| 97 | fprintf(stderr, " %-12s %-30s (.%s)\n", modes[i].mode, |
|---|
| 98 | modes[i].modeInfo, modes[i].suffix); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | #if defined(XP_MAC) && defined(XPIDL_PLUGIN) |
|---|
| 103 | #define main xpidl_main |
|---|
| 104 | int xpidl_main(int argc, char *argv[]); |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | int main(int argc, char *argv[]) |
|---|
| 108 | { |
|---|
| 109 | int i; |
|---|
| 110 | IncludePathEntry *inc, *inc_head, **inc_tail; |
|---|
| 111 | char *file_basename = NULL; |
|---|
| 112 | ModeData *mode = NULL; |
|---|
| 113 | gboolean create_old_typelib = FALSE; |
|---|
| 114 | GSList *files = NULL; |
|---|
| 115 | gboolean change_comment_level = FALSE; |
|---|
| 116 | |
|---|
| 117 | /* turn this on for extra checking of our code */ |
|---|
| 118 | /* IDL_check_cast_enable(TRUE); */ |
|---|
| 119 | |
|---|
| 120 | inc_head = xpidl_malloc(sizeof *inc); |
|---|
| 121 | #ifndef XP_MAC |
|---|
| 122 | inc_head->directory = "."; |
|---|
| 123 | #else |
|---|
| 124 | inc_head->directory = ""; |
|---|
| 125 | #endif |
|---|
| 126 | inc_head->next = NULL; |
|---|
| 127 | inc_tail = &inc_head->next; |
|---|
| 128 | |
|---|
| 129 | for (i = 1; i < argc; i++) { |
|---|
| 130 | if (argv[i][0] != '-') |
|---|
| 131 | break; |
|---|
| 132 | switch (argv[i][1]) { |
|---|
| 133 | case '-': |
|---|
| 134 | argc++; /* pretend we didn't see this */ |
|---|
| 135 | /* fall through */ |
|---|
| 136 | case 0: /* - is a legal input filename (stdin) */ |
|---|
| 137 | goto done_options; |
|---|
| 138 | case 'a': |
|---|
| 139 | emit_typelib_annotations = TRUE; |
|---|
| 140 | break; |
|---|
| 141 | case 'w': |
|---|
| 142 | enable_warnings = TRUE; |
|---|
| 143 | break; |
|---|
| 144 | case 'v': |
|---|
| 145 | verbose_mode = TRUE; |
|---|
| 146 | break; |
|---|
| 147 | case 't': |
|---|
| 148 | { |
|---|
| 149 | /* Parse for "-t version number" and store it into global boolean |
|---|
| 150 | * and string variables. |
|---|
| 151 | */ |
|---|
| 152 | const gchar* typelib_version_string = NULL; |
|---|
| 153 | |
|---|
| 154 | /* |
|---|
| 155 | * If -t is the last argument on the command line, we have a problem |
|---|
| 156 | */ |
|---|
| 157 | |
|---|
| 158 | if (i + 1 == argc) { |
|---|
| 159 | fprintf(stderr, "ERROR: missing version number after -t\n"); |
|---|
| 160 | xpidl_usage(argc, argv); |
|---|
| 161 | return 1; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /* Do not allow more than one "-t" definition */ |
|---|
| 165 | if (create_old_typelib) { |
|---|
| 166 | fprintf(stderr, |
|---|
| 167 | "ERROR: -t argument used twice. " |
|---|
| 168 | "Cannot specify more than one version\n"); |
|---|
| 169 | xpidl_usage(argc, argv); |
|---|
| 170 | return 1; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /* |
|---|
| 174 | * Assume that the argument after "-t" is the version number string |
|---|
| 175 | * and search for it in our internal list of acceptable version |
|---|
| 176 | * numbers. |
|---|
| 177 | */ |
|---|
| 178 | switch (XPT_ParseVersionString(argv[++i], &major_version, |
|---|
| 179 | &minor_version)) { |
|---|
| 180 | case XPT_VERSION_CURRENT: |
|---|
| 181 | break; |
|---|
| 182 | case XPT_VERSION_OLD: |
|---|
| 183 | create_old_typelib = TRUE; |
|---|
| 184 | break; |
|---|
| 185 | case XPT_VERSION_UNSUPPORTED: |
|---|
| 186 | fprintf(stderr, "ERROR: version \"%s\" not supported.\n", |
|---|
| 187 | argv[i]); |
|---|
| 188 | xpidl_usage(argc, argv); |
|---|
| 189 | return 1; |
|---|
| 190 | case XPT_VERSION_UNKNOWN: |
|---|
| 191 | default: |
|---|
| 192 | fprintf(stderr, "ERROR: version \"%s\" not recognised.\n", |
|---|
| 193 | argv[i]); |
|---|
| 194 | xpidl_usage(argc, argv); |
|---|
| 195 | return 1; |
|---|
| 196 | } |
|---|
| 197 | break; |
|---|
| 198 | } |
|---|
| 199 | case 'c': |
|---|
| 200 | { |
|---|
| 201 | /* Parse for "-c comment level" and store it into global boolean |
|---|
| 202 | * and string variables. |
|---|
| 203 | */ |
|---|
| 204 | gint new_comment_level; |
|---|
| 205 | |
|---|
| 206 | /* |
|---|
| 207 | * If -t is the last argument on the command line, we have a problem |
|---|
| 208 | */ |
|---|
| 209 | |
|---|
| 210 | if (i + 1 == argc) { |
|---|
| 211 | fprintf(stderr, "ERROR: missing comment level after -c\n"); |
|---|
| 212 | xpidl_usage(argc, argv); |
|---|
| 213 | return 1; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /* Do not allow more than one "-c" definition */ |
|---|
| 217 | if (change_comment_level) { |
|---|
| 218 | fprintf(stderr, |
|---|
| 219 | "ERROR: -c argument used twice. " |
|---|
| 220 | "Cannot specify more than one level\n"); |
|---|
| 221 | xpidl_usage(argc, argv); |
|---|
| 222 | return 1; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /* |
|---|
| 226 | * Assume that the argument after "-t" is the version number string |
|---|
| 227 | * and search for it in our internal list of acceptable version |
|---|
| 228 | * numbers. |
|---|
| 229 | */ |
|---|
| 230 | new_comment_level = atoi(argv[++i]); |
|---|
| 231 | if (new_comment_level<0 || 2<new_comment_level) { |
|---|
| 232 | fprintf(stderr, |
|---|
| 233 | "ERROR: comment level must be 0 to 2. \n"); |
|---|
| 234 | xpidl_usage(argc, argv); |
|---|
| 235 | return 1; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | comment_level = new_comment_level; |
|---|
| 239 | change_comment_level = TRUE; |
|---|
| 240 | |
|---|
| 241 | break; |
|---|
| 242 | } |
|---|
| 243 | case 'I': |
|---|
| 244 | if (argv[i][2] == '\0' && i == argc) { |
|---|
| 245 | fputs("ERROR: missing path after -I\n", stderr); |
|---|
| 246 | xpidl_usage(argc, argv); |
|---|
| 247 | return 1; |
|---|
| 248 | } |
|---|
| 249 | inc = xpidl_malloc(sizeof *inc); |
|---|
| 250 | if (argv[i][2] == '\0') { |
|---|
| 251 | /* is it the -I foo form? */ |
|---|
| 252 | inc->directory = argv[++i]; |
|---|
| 253 | } else { |
|---|
| 254 | /* must be the -Ifoo form. Don't preincrement i. */ |
|---|
| 255 | inc->directory = argv[i] + 2; |
|---|
| 256 | } |
|---|
| 257 | #ifdef DEBUG_shaver_includes |
|---|
| 258 | fprintf(stderr, "adding %s to include path\n", inc->directory); |
|---|
| 259 | #endif |
|---|
| 260 | inc->next = NULL; |
|---|
| 261 | *inc_tail = inc; |
|---|
| 262 | inc_tail = &inc->next; |
|---|
| 263 | break; |
|---|
| 264 | case 'o': |
|---|
| 265 | if (i == argc) { |
|---|
| 266 | fprintf(stderr, "ERROR: missing basename after -o\n"); |
|---|
| 267 | xpidl_usage(argc, argv); |
|---|
| 268 | return 1; |
|---|
| 269 | } |
|---|
| 270 | file_basename = argv[++i]; |
|---|
| 271 | explicit_output_filename = FALSE; |
|---|
| 272 | break; |
|---|
| 273 | case 'e': |
|---|
| 274 | if (i == argc) { |
|---|
| 275 | fprintf(stderr, "ERROR: missing basename after -e\n"); |
|---|
| 276 | xpidl_usage(argc, argv); |
|---|
| 277 | return 1; |
|---|
| 278 | } |
|---|
| 279 | file_basename = argv[++i]; |
|---|
| 280 | explicit_output_filename = TRUE; |
|---|
| 281 | break; |
|---|
| 282 | case 'm': |
|---|
| 283 | if (i + 1 == argc) { |
|---|
| 284 | fprintf(stderr, "ERROR: missing modename after -m\n"); |
|---|
| 285 | xpidl_usage(argc, argv); |
|---|
| 286 | return 1; |
|---|
| 287 | } |
|---|
| 288 | if (mode) { |
|---|
| 289 | fprintf(stderr, |
|---|
| 290 | "ERROR: must specify exactly one mode " |
|---|
| 291 | "(first \"%s\", now \"%s\")\n", mode->mode, |
|---|
| 292 | argv[i + 1]); |
|---|
| 293 | xpidl_usage(argc, argv); |
|---|
| 294 | return 1; |
|---|
| 295 | } |
|---|
| 296 | mode = FindMode(argv[++i]); |
|---|
| 297 | if (!mode) { |
|---|
| 298 | fprintf(stderr, "ERROR: unknown mode \"%s\"\n", argv[i]); |
|---|
| 299 | xpidl_usage(argc, argv); |
|---|
| 300 | return 1; |
|---|
| 301 | } |
|---|
| 302 | break; |
|---|
| 303 | case 'u': |
|---|
| 304 | if (i + 1 == argc) { |
|---|
| 305 | fprintf(stderr, "ERROR: missing unitname after -u\n"); |
|---|
| 306 | xpidl_usage(argc, argv); |
|---|
| 307 | return 1; |
|---|
| 308 | } |
|---|
| 309 | uses_units = g_slist_append(uses_units, argv[++i]); |
|---|
| 310 | break; |
|---|
| 311 | default: |
|---|
| 312 | fprintf(stderr, "unknown option %s\n", argv[i]); |
|---|
| 313 | xpidl_usage(argc, argv); |
|---|
| 314 | return 1; |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | done_options: |
|---|
| 318 | if (!mode) { |
|---|
| 319 | fprintf(stderr, "ERROR: must specify output mode\n"); |
|---|
| 320 | xpidl_usage(argc, argv); |
|---|
| 321 | return 1; |
|---|
| 322 | } |
|---|
| 323 | if (!file_basename) { |
|---|
| 324 | fprintf(stderr, "ERROR: must specify output basefile\n"); |
|---|
| 325 | xpidl_usage(argc, argv); |
|---|
| 326 | return 1; |
|---|
| 327 | } |
|---|
| 328 | /*if (argc != i + 1) { |
|---|
| 329 | fprintf(stderr, "ERROR: extra arguments after input file\n"); |
|---|
| 330 | }*/ |
|---|
| 331 | |
|---|
| 332 | /* |
|---|
| 333 | * Don't try to process multiple files, given that we don't handle -o |
|---|
| 334 | * multiply. |
|---|
| 335 | */ |
|---|
| 336 | |
|---|
| 337 | for(; i<argc; i++) { |
|---|
| 338 | files = xpidlpas_append_process_files(files, argv[i]); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | files = xpidlpas_sort_files(files, inc_head); |
|---|
| 342 | |
|---|
| 343 | if (xpidlpas_process_files(files, inc_head, file_basename, mode)) |
|---|
| 344 | return 0; |
|---|
| 345 | |
|---|
| 346 | /*if (xpidl_process_idl(argv[i], inc_head, file_basename, mode)) |
|---|
| 347 | return 0;*/ |
|---|
| 348 | |
|---|
| 349 | return 1; |
|---|
| 350 | } |
|---|