2 * Option processing and main()
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 _globals globals; /* All global variables */
28 static void do_include (const char *arg)
32 if (!globals.directory)
33 globals.directory = strdup(arg);
35 newIncludes = str_create (3,globals.directory," ",arg);
36 free(globals.directory);
37 globals.directory = newIncludes;
43 static inline const char* strip_ext (const char *str)
45 int len = strlen(str);
46 if (len>4 && strcmp(str+len-4,".dll") == 0)
47 return str_substring (str, str+len-4);
53 static void do_name (const char *arg)
55 globals.dll_name = strip_ext (arg);
59 static void do_spec (const char *arg)
61 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
66 static void do_demangle (const char *arg)
68 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
74 static void do_dump (const char *arg)
76 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
82 static void do_dumpemf(void)
84 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
89 static void do_dumplnk(void)
91 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
96 static void do_code (void)
102 static void do_trace (void)
104 globals.do_trace = 1;
109 static void do_forward (const char *arg)
111 globals.forward_dll = arg;
112 globals.do_trace = 1;
116 static void do_document (void)
118 globals.do_documentation = 1;
121 static void do_cdecl (void)
123 globals.do_cdecl = 1;
127 static void do_quiet (void)
129 globals.do_quiet = 1;
133 static void do_start (const char *arg)
135 globals.start_ordinal = atoi (arg);
136 if (!globals.start_ordinal)
137 fatal ("Invalid -s option (must be numeric)");
141 static void do_end (const char *arg)
143 globals.end_ordinal = atoi (arg);
144 if (!globals.end_ordinal)
145 fatal ("Invalid -e option (must be numeric)");
149 static void do_symfile (const char *arg)
152 char symstring[256]; /* keep count with "%<width>s" below */
153 search_symbol *symbolp,**symbolptail = &globals.search_symbol;
155 if (!(f = fopen(arg, "rt")))
156 fatal ("Cannot open <symfile>");
157 while (1 == fscanf(f, "%255s", symstring)) /* keep count with [<width>] above */
159 symstring[sizeof(symstring)-1] = '\0';
160 if (!(symbolp = malloc(sizeof(*symbolp) + strlen(symstring))))
161 fatal ("Out of memory");
162 strcpy(symbolp->symbolname, symstring);
164 symbolp->next = NULL;
165 *symbolptail = symbolp;
166 symbolptail = &symbolp->next;
169 fatal ("Cannot close <symfile>");
173 static void do_verbose (void)
175 globals.do_verbose = 1;
179 static void do_symdmngl (void)
181 globals.do_demangle = 1;
184 static void do_dumphead (void)
186 globals.do_dumpheader = 1;
189 static void do_dumpsect (const char* arg)
191 globals.dumpsect = arg;
194 static void do_dumpall(void)
196 globals.do_dumpheader = 1;
197 globals.dumpsect = "ALL";
209 static const struct option option_table[] = {
210 {"--help",NONE, 0, do_usage, "--help Display this help message"},
211 {"-h", NONE, 0, do_usage, "-h Synonym for --help"},
212 {"-?", NONE, 0, do_usage, "-? Synonym for --help"},
213 {"sym", DMGL, 0, do_demangle, "sym <sym> Demangle C++ symbol <sym> and exit"},
214 {"spec", SPEC, 0, do_spec, "spec <dll> Use dll for input file and generate implementation code"},
215 {"-I", SPEC, 1, do_include, "-I dir Look for prototypes in 'dir' (implies -c)"},
216 {"-c", SPEC, 0, do_code, "-c Generate skeleton code (requires -I)"},
217 {"-t", SPEC, 0, do_trace, "-t TRACE arguments (implies -c)"},
218 {"-f", SPEC, 1, do_forward, "-f dll Forward calls to 'dll' (implies -t)"},
219 {"-D", SPEC, 0, do_document, "-D Generate documentation"},
220 {"-o", SPEC, 1, do_name, "-o name Set the output dll name (default: dll). note: strips .dll extensions"},
221 {"-C", SPEC, 0, do_cdecl, "-C Assume __cdecl calls (default: __stdcall)"},
222 {"-s", SPEC, 1, do_start, "-s num Start prototype search after symbol 'num'"},
223 {"-e", SPEC, 1, do_end, "-e num End prototype search after symbol 'num'"},
224 {"-S", SPEC, 1, do_symfile, "-S symfile Search only prototype names found in 'symfile'"},
225 {"-q", SPEC, 0, do_quiet, "-q Don't show progress (quiet)."},
226 {"-v", SPEC, 0, do_verbose, "-v Show lots of detail while working (verbose)."},
227 {"dump", DUMP, 0, do_dump, "dump <mod> Dumps the content of the module (dll, exe...) named <mod>"},
228 {"-C", DUMP, 0, do_symdmngl, "-C Turns on symbol demangling"},
229 {"-f", DUMP, 0, do_dumphead, "-f Dumps file header information"},
230 {"-j", DUMP, 1, do_dumpsect, "-j sect_name Dumps only the content of section sect_name (import, export, debug, resource, tls)"},
231 {"-x", DUMP, 0, do_dumpall, "-x Dumps everything"},
232 {"emf", EMF, 0, do_dumpemf, "emf Dumps an Enhanced Meta File"},
233 {"lnk", LNK, 0, do_dumplnk, "lnk Dumps a shortcut (.lnk) file"},
234 {NULL, NONE, 0, NULL, NULL}
239 const struct option *opt;
240 printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <dll> | emf <emf> | lnk <lnk>]\n");
241 printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
242 printf ("\tWhen used in --help mode\n");
243 for (opt = option_table; opt->name; opt++)
244 if (opt->mode == NONE)
245 printf ("\t %s\n", opt->usage);
246 printf ("\tWhen used in sym mode\n");
247 for (opt = option_table; opt->name; opt++)
248 if (opt->mode == DMGL)
249 printf ("\t %s\n", opt->usage);
250 printf ("\tWhen used in spec mode\n");
251 for (opt = option_table; opt->name; opt++)
252 if (opt->mode == SPEC)
253 printf ("\t %s\n", opt->usage);
254 printf ("\tWhen used in dump mode\n");
255 for (opt = option_table; opt->name; opt++)
256 if (opt->mode == DUMP)
257 printf ("\t %s\n", opt->usage);
258 printf ("\tWhen used in emf mode\n");
259 for (opt = option_table; opt->name; opt++)
260 if (opt->mode == EMF)
261 printf ("\t %s\n", opt->usage);
262 printf ("\tWhen used in lnk mode\n");
263 for (opt = option_table; opt->name; opt++)
264 if (opt->mode == LNK)
265 printf ("\t %s\n", opt->usage);
272 /*******************************************************************
275 * Parse options from the argv array
277 static void parse_options (char *argv[])
279 const struct option *opt;
281 const char *arg = NULL;
287 for (opt = option_table; opt->name; opt++)
289 if (globals.mode != NONE && opt->mode != NONE && globals.mode != opt->mode)
291 if (((opt->has_arg == 1) && !strncmp (*ptr, opt->name, strlen (opt->name))) ||
292 ((opt->has_arg == 2) && !strcmp (*ptr, opt->name)))
294 arg = *ptr + strlen (opt->name);
295 if (*arg == '\0') arg = *++ptr;
298 if (!strcmp (*ptr, opt->name))
307 if ((*ptr)[0] == '-')
308 fatal ("Unrecognized option");
309 if (globals.input_name != NULL)
310 fatal ("Only one file can be treated at once");
311 globals.input_name = *ptr;
313 else if (opt->has_arg && arg != NULL)
321 if (globals.mode == SPEC && globals.do_code && !globals.directory)
322 fatal ("-I must be used if generating code");
324 if (VERBOSE && QUIET)
325 fatal ("Options -v and -q are mutually exclusive");
328 static void set_module_name(unsigned setUC)
334 /* FIXME: we shouldn't assume all module extensions are .dll in winedump
335 * in some cases, we could have some .drv for example
337 /* get module name from name */
338 if ((ptr = strrchr (globals.input_name, '/')))
341 ptr = globals.input_name;
343 if (len > 4 && strcmp(ptr + len - 4, ".dll") == 0)
345 buf = malloc(len + 1);
346 memcpy(buf, (const void*)ptr, len);
348 globals.input_module = buf;
349 OUTPUT_UC_DLL_NAME = (setUC) ? str_toupper( strdup (OUTPUT_DLL_NAME)) : "";
352 /* Marks the symbol as 'found'! */
353 /* return: perform-search */
354 static int symbol_searched(int count, const char *symbolname)
356 search_symbol *search_symbol;
358 if (!(count >= globals.start_ordinal
359 && (!globals.end_ordinal || count <= globals.end_ordinal)))
361 if (!globals.search_symbol)
363 for (search_symbol = globals.search_symbol;
365 search_symbol = search_symbol->next)
367 if (!strcmp(symbolname, search_symbol->symbolname))
369 search_symbol->found = 1;
376 /* return: some symbols weren't found */
377 static int symbol_finish(void)
379 const search_symbol *search_symbol;
382 for (search_symbol = globals.search_symbol;
384 search_symbol = search_symbol->next)
386 if (search_symbol->found)
390 /* stderr? not a practice here */
391 puts("These requested <symfile> symbols weren't found:");
394 printf("\t%s\n",search_symbol->symbolname);
399 /*******************************************************************
403 int main (int argc __attribute__((unused)), char *argv[])
405 int main (int argc, char *argv[])
408 parsed_symbol symbol;
412 globals.forward_dll = NULL;
413 globals.input_name = NULL;
415 parse_options (argv);
417 memset (&symbol, 0, sizeof (parsed_symbol));
419 switch (globals.mode)
422 globals.uc_dll_name = "";
425 symbol_init (&symbol, globals.input_name);
426 globals.input_module = "";
427 if (symbol_demangle (&symbol) == -1)
428 fatal( "Symbol hasn't got a mangled name\n");
429 if (symbol.flags & SYM_DATA)
430 printf (symbol.arg_text[0]);
432 output_prototype (stdout, &symbol);
433 fputc ('\n', stdout);
434 symbol_clear(&symbol);
438 if (globals.input_name == NULL)
439 fatal("No file name has been given\n");
441 if (!dll_open (globals.input_name))
444 output_spec_preamble ();
445 output_header_preamble ();
446 output_c_preamble ();
448 while (!dll_next_symbol (&symbol))
453 printf ("Export %3d - '%s' ...%c", count, symbol.symbol,
454 VERBOSE ? '\n' : ' ');
456 if (globals.do_code && symbol_searched(count, symbol.symbol))
458 /* Attempt to get information about the symbol */
459 int result = symbol_demangle (&symbol);
462 result = symbol_search (&symbol);
464 if (!result && symbol.function_name)
465 /* Clean up the prototype */
466 symbol_clean_string (symbol.function_name);
469 puts (result ? "[Not Found]" : "[OK]");
474 output_spec_symbol (&symbol);
475 output_header_symbol (&symbol);
476 output_c_symbol (&symbol);
478 symbol_clear (&symbol);
482 output_install_script ();
485 puts ("Finished, Cleaning up...");
493 if (globals.input_name == NULL)
494 fatal("No file name has been given\n");
496 dump_file(globals.input_name);
499 if (globals.input_name == NULL)
500 fatal("No file name has been given\n");
501 dump_emf(globals.input_name);
504 if (globals.input_name == NULL)
505 fatal("No file name has been given\n");
506 dump_lnk(globals.input_name);