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 {"-h", NONE, 0, do_usage, "-h Display this help message"},
211 {"sym", DMGL, 0, do_demangle, "sym <sym> Demangle C++ symbol <sym> and exit"},
212 {"spec", SPEC, 0, do_spec, "spec <dll> Use dll for input file and generate implementation code"},
213 {"-I", SPEC, 1, do_include, "-I dir Look for prototypes in 'dir' (implies -c)"},
214 {"-c", SPEC, 0, do_code, "-c Generate skeleton code (requires -I)"},
215 {"-t", SPEC, 0, do_trace, "-t TRACE arguments (implies -c)"},
216 {"-f", SPEC, 1, do_forward, "-f dll Forward calls to 'dll' (implies -t)"},
217 {"-D", SPEC, 0, do_document, "-D Generate documentation"},
218 {"-o", SPEC, 1, do_name, "-o name Set the output dll name (default: dll). note: strips .dll extensions"},
219 {"-C", SPEC, 0, do_cdecl, "-C Assume __cdecl calls (default: __stdcall)"},
220 {"-s", SPEC, 1, do_start, "-s num Start prototype search after symbol 'num'"},
221 {"-e", SPEC, 1, do_end, "-e num End prototype search after symbol 'num'"},
222 {"-S", SPEC, 1, do_symfile, "-S symfile Search only prototype names found in 'symfile'"},
223 {"-q", SPEC, 0, do_quiet, "-q Don't show progress (quiet)."},
224 {"-v", SPEC, 0, do_verbose, "-v Show lots of detail while working (verbose)."},
225 {"dump", DUMP, 0, do_dump, "dump <mod> Dumps the content of the module (dll, exe...) named <mod>"},
226 {"-C", DUMP, 0, do_symdmngl, "-C Turns on symbol demangling"},
227 {"-f", DUMP, 0, do_dumphead, "-f Dumps file header information"},
228 {"-j", DUMP, 1, do_dumpsect, "-j sect_name Dumps only the content of section sect_name (import, export, debug, resource, tls)"},
229 {"-x", DUMP, 0, do_dumpall, "-x Dumps everything"},
230 {"emf", EMF, 0, do_dumpemf, "emf Dumps an Enhanced Meta File"},
231 {"lnk", LNK, 0, do_dumplnk, "lnk Dumps a shortcut (.lnk) file"},
232 {NULL, NONE, 0, NULL, NULL}
237 const struct option *opt;
238 printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <dll>]\n");
239 printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
240 printf ("\tWhen used in -h mode\n");
241 for (opt = option_table; opt->name; opt++)
242 if (opt->mode == NONE)
243 printf ("\t %s\n", opt->usage);
244 printf ("\tWhen used in sym mode\n");
245 for (opt = option_table; opt->name; opt++)
246 if (opt->mode == DMGL)
247 printf ("\t %s\n", opt->usage);
248 printf ("\tWhen used in spec mode\n");
249 for (opt = option_table; opt->name; opt++)
250 if (opt->mode == SPEC)
251 printf ("\t %s\n", opt->usage);
252 printf ("\tWhen used in dump mode\n");
253 for (opt = option_table; opt->name; opt++)
254 if (opt->mode == DUMP)
255 printf ("\t %s\n", opt->usage);
256 printf ("\tWhen used in emf mode\n");
257 for (opt = option_table; opt->name; opt++)
258 if (opt->mode == EMF)
259 printf ("\t %s\n", opt->usage);
260 printf ("\tWhen used in lnk mode\n");
261 for (opt = option_table; opt->name; opt++)
262 if (opt->mode == LNK)
263 printf ("\t %s\n", opt->usage);
270 /*******************************************************************
273 * Parse options from the argv array
275 static void parse_options (char *argv[])
277 const struct option *opt;
279 const char *arg = NULL;
285 for (opt = option_table; opt->name; opt++)
287 if (globals.mode != NONE && opt->mode != NONE && globals.mode != opt->mode)
289 if (((opt->has_arg == 1) && !strncmp (*ptr, opt->name, strlen (opt->name))) ||
290 ((opt->has_arg == 2) && !strcmp (*ptr, opt->name)))
292 arg = *ptr + strlen (opt->name);
293 if (*arg == '\0') arg = *++ptr;
296 if (!strcmp (*ptr, opt->name))
305 if ((*ptr)[0] == '-')
306 fatal ("Unrecognized option");
307 if (globals.input_name != NULL)
308 fatal ("Only one file can be treated at once");
309 globals.input_name = *ptr;
311 else if (opt->has_arg && arg != NULL)
319 if (globals.mode == SPEC && globals.do_code && !globals.directory)
320 fatal ("-I must be used if generating code");
322 if (VERBOSE && QUIET)
323 fatal ("Options -v and -q are mutually exclusive");
326 static void set_module_name(unsigned setUC)
332 /* FIXME: we shouldn't assume all module extensions are .dll in winedump
333 * in some cases, we could have some .drv for example
335 /* get module name from name */
336 if ((ptr = strrchr (globals.input_name, '/')))
339 ptr = globals.input_name;
341 if (len > 4 && strcmp(ptr + len - 4, ".dll") == 0)
343 buf = malloc(len + 1);
344 memcpy(buf, (const void*)ptr, len);
346 globals.input_module = buf;
347 OUTPUT_UC_DLL_NAME = (setUC) ? str_toupper( strdup (OUTPUT_DLL_NAME)) : "";
350 /* Marks the symbol as 'found'! */
351 /* return: perform-search */
352 static int symbol_searched(int count, const char *symbolname)
354 search_symbol *search_symbol;
356 if (!(count >= globals.start_ordinal
357 && (!globals.end_ordinal || count <= globals.end_ordinal)))
359 if (!globals.search_symbol)
361 for (search_symbol = globals.search_symbol;
363 search_symbol = search_symbol->next)
365 if (!strcmp(symbolname, search_symbol->symbolname))
367 search_symbol->found = 1;
374 /* return: some symbols weren't found */
375 static int symbol_finish(void)
377 const search_symbol *search_symbol;
380 for (search_symbol = globals.search_symbol;
382 search_symbol = search_symbol->next)
384 if (search_symbol->found)
388 /* stderr? not a practice here */
389 puts("These requested <symfile> symbols weren't found:");
392 printf("\t%s\n",search_symbol->symbolname);
397 /*******************************************************************
401 int main (int argc __attribute__((unused)), char *argv[])
403 int main (int argc, char *argv[])
406 parsed_symbol symbol;
410 globals.forward_dll = NULL;
411 globals.input_name = NULL;
413 parse_options (argv);
415 memset (&symbol, 0, sizeof (parsed_symbol));
417 switch (globals.mode)
420 globals.uc_dll_name = "";
423 symbol_init (&symbol, globals.input_name);
424 globals.input_module = "";
425 if (symbol_demangle (&symbol) == -1)
426 fatal( "Symbol hasn't got a mangled name\n");
427 if (symbol.flags & SYM_DATA)
428 printf (symbol.arg_text[0]);
430 output_prototype (stdout, &symbol);
431 fputc ('\n', stdout);
432 symbol_clear(&symbol);
436 if (globals.input_name == NULL)
437 fatal("No file name has been given\n");
439 if (!dll_open (globals.input_name))
442 output_spec_preamble ();
443 output_header_preamble ();
444 output_c_preamble ();
446 while (!dll_next_symbol (&symbol))
451 printf ("Export %3d - '%s' ...%c", count, symbol.symbol,
452 VERBOSE ? '\n' : ' ');
454 if (globals.do_code && symbol_searched(count, symbol.symbol))
456 /* Attempt to get information about the symbol */
457 int result = symbol_demangle (&symbol);
460 result = symbol_search (&symbol);
462 if (!result && symbol.function_name)
463 /* Clean up the prototype */
464 symbol_clean_string (symbol.function_name);
467 puts (result ? "[Not Found]" : "[OK]");
472 output_spec_symbol (&symbol);
473 output_header_symbol (&symbol);
474 output_c_symbol (&symbol);
476 symbol_clear (&symbol);
480 output_install_script ();
483 puts ("Finished, Cleaning up...");
491 if (globals.input_name == NULL)
492 fatal("No file name has been given\n");
494 dump_file(globals.input_name);
497 if (globals.input_name == NULL)
498 fatal("No file name has been given\n");
499 dump_emf(globals.input_name);
502 if (globals.input_name == NULL)
503 fatal("No file name has been given\n");
504 dump_lnk(globals.input_name);