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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
26 _globals globals
; /* All global variables */
29 static void do_include (const char *arg
)
33 if (!globals
.directory
)
34 globals
.directory
= strdup(arg
);
36 newIncludes
= str_create (3,globals
.directory
," ",arg
);
37 free(globals
.directory
);
38 globals
.directory
= newIncludes
;
44 static inline const char* strip_ext (const char *str
)
46 int len
= strlen(str
);
47 if (len
>4 && strcmp(str
+len
-4,".dll") == 0)
48 return str_substring (str
, str
+len
-4);
54 static void do_name (const char *arg
)
56 globals
.dll_name
= strip_ext (arg
);
60 static void do_spec (const char *arg
)
62 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
67 static void do_demangle (const char *arg
)
69 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
75 static void do_dump (const char *arg
)
77 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
83 static void do_code (void)
89 static void do_trace (void)
96 static void do_forward (const char *arg
)
98 globals
.forward_dll
= arg
;
103 static void do_document (void)
105 globals
.do_documentation
= 1;
108 static void do_cdecl (void)
110 globals
.do_cdecl
= 1;
114 static void do_quiet (void)
116 globals
.do_quiet
= 1;
120 static void do_start (const char *arg
)
122 globals
.start_ordinal
= atoi (arg
);
123 if (!globals
.start_ordinal
)
124 fatal ("Invalid -s option (must be numeric)");
128 static void do_end (const char *arg
)
130 globals
.end_ordinal
= atoi (arg
);
131 if (!globals
.end_ordinal
)
132 fatal ("Invalid -e option (must be numeric)");
136 static void do_symfile (const char *arg
)
139 char symstring
[256]; /* keep count with "%<width>s" below */
140 search_symbol
*symbolp
,**symbolptail
= &globals
.search_symbol
;
142 if (!(f
= fopen(arg
, "rt")))
143 fatal ("Cannot open <symfile>");
144 while (1 == fscanf(f
, "%255s", symstring
)) /* keep count with [<width>] above */
146 symstring
[sizeof(symstring
)-1] = '\0';
147 if (!(symbolp
= malloc(sizeof(*symbolp
) + strlen(symstring
))))
148 fatal ("Out of memory");
149 strcpy(symbolp
->symbolname
, symstring
);
151 symbolp
->next
= NULL
;
152 *symbolptail
= symbolp
;
153 symbolptail
= &symbolp
->next
;
156 fatal ("Cannot close <symfile>");
160 static void do_verbose (void)
162 globals
.do_verbose
= 1;
166 static void do_symdmngl (void)
168 globals
.do_demangle
= 1;
171 static void do_dumphead (void)
173 globals
.do_dumpheader
= 1;
176 static void do_dumpsect (const char* arg
)
178 globals
.dumpsect
= arg
;
181 static void do_rawdebug (void)
183 globals
.do_debug
= 1;
186 static void do_dumpall(void)
188 globals
.do_dumpheader
= 1;
189 globals
.do_dump_rawdata
= 1;
190 globals
.dumpsect
= "ALL";
202 static const struct my_option option_table
[] = {
203 {"--help",NONE
, 0, do_usage
, "--help Display this help message"},
204 {"-h", NONE
, 0, do_usage
, "-h Synonym for --help"},
205 {"-?", NONE
, 0, do_usage
, "-? Synonym for --help"},
206 {"sym", DMGL
, 0, do_demangle
, "sym <sym> Demangle C++ symbol <sym> and exit"},
207 {"spec", SPEC
, 0, do_spec
, "spec <dll> Use dll for input file and generate implementation code"},
208 {"-I", SPEC
, 1, do_include
, "-I dir Look for prototypes in 'dir' (implies -c)"},
209 {"-c", SPEC
, 0, do_code
, "-c Generate skeleton code (requires -I)"},
210 {"-t", SPEC
, 0, do_trace
, "-t TRACE arguments (implies -c)"},
211 {"-f", SPEC
, 1, do_forward
, "-f dll Forward calls to 'dll' (implies -t)"},
212 {"-D", SPEC
, 0, do_document
, "-D Generate documentation"},
213 {"-o", SPEC
, 1, do_name
, "-o name Set the output dll name (default: dll). note: strips .dll extensions"},
214 {"-C", SPEC
, 0, do_cdecl
, "-C Assume __cdecl calls (default: __stdcall)"},
215 {"-s", SPEC
, 1, do_start
, "-s num Start prototype search after symbol 'num'"},
216 {"-e", SPEC
, 1, do_end
, "-e num End prototype search after symbol 'num'"},
217 {"-S", SPEC
, 1, do_symfile
, "-S symfile Search only prototype names found in 'symfile'"},
218 {"-q", SPEC
, 0, do_quiet
, "-q Don't show progress (quiet)."},
219 {"-v", SPEC
, 0, do_verbose
, "-v Show lots of detail while working (verbose)."},
220 {"dump", DUMP
, 0, do_dump
, "dump <file> Dumps the contents of a file (dll, exe, lib...)"},
221 {"-C", DUMP
, 0, do_symdmngl
, "-C Turns on symbol demangling"},
222 {"-f", DUMP
, 0, do_dumphead
, "-f Dumps file header information"},
223 {"-G", DUMP
, 0, do_rawdebug
, "-G Dumps raw debug information"},
224 {"-j", DUMP
, 1, do_dumpsect
, "-j sect_name Dumps only the content of section sect_name (import, export, debug, resource, tls, clr)"},
225 {"-x", DUMP
, 0, do_dumpall
, "-x Dumps everything"},
226 {NULL
, NONE
, 0, NULL
, NULL
}
231 const struct my_option
*opt
;
232 printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <file>]\n");
233 printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
234 printf ("\tWhen used in --help mode\n");
235 for (opt
= option_table
; opt
->name
; opt
++)
236 if (opt
->mode
== NONE
)
237 printf ("\t %s\n", opt
->usage
);
238 printf ("\tWhen used in sym mode\n");
239 for (opt
= option_table
; opt
->name
; opt
++)
240 if (opt
->mode
== DMGL
)
241 printf ("\t %s\n", opt
->usage
);
242 printf ("\tWhen used in spec mode\n");
243 for (opt
= option_table
; opt
->name
; opt
++)
244 if (opt
->mode
== SPEC
)
245 printf ("\t %s\n", opt
->usage
);
246 printf ("\tWhen used in dump mode\n");
247 for (opt
= option_table
; opt
->name
; opt
++)
248 if (opt
->mode
== DUMP
)
249 printf ("\t %s\n", opt
->usage
);
256 /*******************************************************************
259 * Parse options from the argv array
261 static void parse_options (char *argv
[])
263 const struct my_option
*opt
;
265 const char *arg
= NULL
;
271 for (opt
= option_table
; opt
->name
; opt
++)
273 if (globals
.mode
!= NONE
&& opt
->mode
!= NONE
&& globals
.mode
!= opt
->mode
)
275 if (((opt
->has_arg
== 1) && !strncmp (*ptr
, opt
->name
, strlen (opt
->name
))) ||
276 ((opt
->has_arg
== 2) && !strcmp (*ptr
, opt
->name
)))
278 arg
= *ptr
+ strlen (opt
->name
);
279 if (*arg
== '\0') arg
= *++ptr
;
282 if (!strcmp (*ptr
, opt
->name
))
291 if ((*ptr
)[0] == '-')
292 fatal ("Unrecognized option");
293 if (globals
.input_name
!= NULL
)
294 fatal ("Only one file can be treated at once");
295 globals
.input_name
= *ptr
;
297 else if (opt
->has_arg
&& arg
!= NULL
)
305 if (globals
.mode
== SPEC
&& globals
.do_code
&& !globals
.directory
)
306 fatal ("-I must be used if generating code");
308 if (VERBOSE
&& QUIET
)
309 fatal ("Options -v and -q are mutually exclusive");
311 if (globals
.mode
== NONE
)
315 static void set_module_name(unsigned setUC
)
321 /* FIXME: we shouldn't assume all module extensions are .dll in winedump
322 * in some cases, we could have some .drv for example
324 /* get module name from name */
325 if ((ptr
= strrchr (globals
.input_name
, '/')))
328 ptr
= globals
.input_name
;
330 if (len
> 4 && strcmp(ptr
+ len
- 4, ".dll") == 0)
332 buf
= malloc(len
+ 1);
333 memcpy(buf
, (const void*)ptr
, len
);
335 globals
.input_module
= buf
;
336 OUTPUT_UC_DLL_NAME
= (setUC
) ? str_toupper( strdup (OUTPUT_DLL_NAME
)) : "";
339 /* Marks the symbol as 'found'! */
340 /* return: perform-search */
341 static int symbol_searched(int count
, const char *symbolname
)
343 search_symbol
*search_symbol
;
345 if (!(count
>= globals
.start_ordinal
346 && (!globals
.end_ordinal
|| count
<= globals
.end_ordinal
)))
348 if (!globals
.search_symbol
)
350 for (search_symbol
= globals
.search_symbol
;
352 search_symbol
= search_symbol
->next
)
354 if (!strcmp(symbolname
, search_symbol
->symbolname
))
356 search_symbol
->found
= 1;
363 /* return: some symbols weren't found */
364 static int symbol_finish(void)
366 const search_symbol
*search_symbol
;
369 for (search_symbol
= globals
.search_symbol
;
371 search_symbol
= search_symbol
->next
)
373 if (search_symbol
->found
)
377 /* stderr? not a practice here */
378 puts("These requested <symfile> symbols weren't found:");
381 printf("\t%s\n",search_symbol
->symbolname
);
386 /*******************************************************************
390 int main (int argc
__attribute__((unused
)), char *argv
[])
392 int main (int argc
, char *argv
[])
395 parsed_symbol symbol
;
399 globals
.forward_dll
= NULL
;
400 globals
.input_name
= NULL
;
401 globals
.dumpsect
= NULL
;
403 parse_options (argv
);
405 memset (&symbol
, 0, sizeof (parsed_symbol
));
407 switch (globals
.mode
)
412 if (globals
.input_name
== NULL
)
413 fatal("No symbol name has been given\n");
414 printf("%s\n", get_symbol_str(globals
.input_name
));
418 if (globals
.input_name
== NULL
)
419 fatal("No file name has been given\n");
421 if (!dll_open (globals
.input_name
))
424 output_spec_preamble ();
425 output_header_preamble ();
426 output_c_preamble ();
428 while (!dll_next_symbol (&symbol
))
433 printf ("Export %3d - '%s' ...%c", count
, symbol
.symbol
,
434 VERBOSE
? '\n' : ' ');
436 if (globals
.do_code
&& symbol_searched(count
, symbol
.symbol
))
438 /* Attempt to get information about the symbol */
439 int result
= symbol_demangle (&symbol
);
442 result
= symbol_search (&symbol
);
444 if (!result
&& symbol
.function_name
)
445 /* Clean up the prototype */
446 symbol_clean_string (symbol
.function_name
);
449 puts (result
? "[Not Found]" : "[OK]");
454 output_spec_symbol (&symbol
);
455 output_header_symbol (&symbol
);
456 output_c_symbol (&symbol
);
458 symbol_clear (&symbol
);
464 puts ("Finished, Cleaning up...");
472 if (globals
.input_name
== NULL
)
473 fatal("No file name has been given\n");
475 dump_file(globals
.input_name
);