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_code (void)
88 static void do_trace (void)
95 static void do_forward (const char *arg
)
97 globals
.forward_dll
= arg
;
102 static void do_document (void)
104 globals
.do_documentation
= 1;
107 static void do_cdecl (void)
109 globals
.do_cdecl
= 1;
113 static void do_quiet (void)
115 globals
.do_quiet
= 1;
119 static void do_start (const char *arg
)
121 globals
.start_ordinal
= atoi (arg
);
122 if (!globals
.start_ordinal
)
123 fatal ("Invalid -s option (must be numeric)");
127 static void do_end (const char *arg
)
129 globals
.end_ordinal
= atoi (arg
);
130 if (!globals
.end_ordinal
)
131 fatal ("Invalid -e option (must be numeric)");
135 static void do_symfile (const char *arg
)
138 char symstring
[256]; /* keep count with "%<width>s" below */
139 search_symbol
*symbolp
,**symbolptail
= &globals
.search_symbol
;
141 if (!(f
= fopen(arg
, "rt")))
142 fatal ("Cannot open <symfile>");
143 while (1 == fscanf(f
, "%255s", symstring
)) /* keep count with [<width>] above */
145 symstring
[sizeof(symstring
)-1] = '\0';
146 if (!(symbolp
= malloc(sizeof(*symbolp
) + strlen(symstring
))))
147 fatal ("Out of memory");
148 strcpy(symbolp
->symbolname
, symstring
);
150 symbolp
->next
= NULL
;
151 *symbolptail
= symbolp
;
152 symbolptail
= &symbolp
->next
;
155 fatal ("Cannot close <symfile>");
159 static void do_verbose (void)
161 globals
.do_verbose
= 1;
165 static void do_symdmngl (void)
167 globals
.do_demangle
= 1;
170 static void do_dumphead (void)
172 globals
.do_dumpheader
= 1;
175 static void do_dumpsect (const char* arg
)
177 globals
.dumpsect
= arg
;
180 static void do_dumpall(void)
182 globals
.do_dumpheader
= 1;
183 globals
.dumpsect
= "ALL";
195 static const struct option option_table
[] = {
196 {"-h", NONE
, 0, do_usage
, "-h Display this help message"},
197 {"sym", DMGL
, 0, do_demangle
, "sym <sym> Demangle C++ symbol <sym> and exit"},
198 {"spec", SPEC
, 0, do_spec
, "spec <dll> Use dll for input file and generate implementation code"},
199 {"-I", SPEC
, 1, do_include
, "-I dir Look for prototypes in 'dir' (implies -c)"},
200 {"-c", SPEC
, 0, do_code
, "-c Generate skeleton code (requires -I)"},
201 {"-t", SPEC
, 0, do_trace
, "-t TRACE arguments (implies -c)"},
202 {"-f", SPEC
, 1, do_forward
, "-f dll Forward calls to 'dll' (implies -t)"},
203 {"-D", SPEC
, 0, do_document
, "-D Generate documentation"},
204 {"-o", SPEC
, 1, do_name
, "-o name Set the output dll name (default: dll). note: strips .dll extensions"},
205 {"-C", SPEC
, 0, do_cdecl
, "-C Assume __cdecl calls (default: __stdcall)"},
206 {"-s", SPEC
, 1, do_start
, "-s num Start prototype search after symbol 'num'"},
207 {"-e", SPEC
, 1, do_end
, "-e num End prototype search after symbol 'num'"},
208 {"-S", SPEC
, 1, do_symfile
, "-S symfile Search only prototype names found in 'symfile'"},
209 {"-q", SPEC
, 0, do_quiet
, "-q Don't show progress (quiet)."},
210 {"-v", SPEC
, 0, do_verbose
, "-v Show lots of detail while working (verbose)."},
211 {"dump", DUMP
, 0, do_dump
, "dump <mod> Dumps the content of the module (dll, exe...) named <mod>"},
212 {"-C", DUMP
, 0, do_symdmngl
, "-C Turns on symbol demangling"},
213 {"-f", DUMP
, 0, do_dumphead
, "-f Dumps file header information"},
214 {"-j", DUMP
, 1, do_dumpsect
, "-j sect_name Dumps only the content of section sect_name (import, export, debug, resource, tls)"},
215 {"-x", DUMP
, 0, do_dumpall
, "-x Dumps everything"},
216 {NULL
, NONE
, 0, NULL
, NULL
}
221 const struct option
*opt
;
222 printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <dll>]\n");
223 printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
224 printf ("\tWhen used in -h mode\n");
225 for (opt
= option_table
; opt
->name
; opt
++)
226 if (opt
->mode
== NONE
)
227 printf ("\t %s\n", opt
->usage
);
228 printf ("\tWhen used in sym mode\n");
229 for (opt
= option_table
; opt
->name
; opt
++)
230 if (opt
->mode
== DMGL
)
231 printf ("\t %s\n", opt
->usage
);
232 printf ("\tWhen used in spec mode\n");
233 for (opt
= option_table
; opt
->name
; opt
++)
234 if (opt
->mode
== SPEC
)
235 printf ("\t %s\n", opt
->usage
);
236 printf ("\tWhen used in dump mode\n");
237 for (opt
= option_table
; opt
->name
; opt
++)
238 if (opt
->mode
== DUMP
)
239 printf ("\t %s\n", opt
->usage
);
246 /*******************************************************************
249 * Parse options from the argv array
251 static void parse_options (char *argv
[])
253 const struct option
*opt
;
255 const char *arg
= NULL
;
261 for (opt
= option_table
; opt
->name
; opt
++)
263 if (globals
.mode
!= NONE
&& opt
->mode
!= NONE
&& globals
.mode
!= opt
->mode
)
265 if (((opt
->has_arg
== 1) && !strncmp (*ptr
, opt
->name
, strlen (opt
->name
))) ||
266 ((opt
->has_arg
== 2) && !strcmp (*ptr
, opt
->name
)))
268 arg
= *ptr
+ strlen (opt
->name
);
269 if (*arg
== '\0') arg
= *++ptr
;
272 if (!strcmp (*ptr
, opt
->name
))
281 if ((*ptr
)[0] == '-')
282 fatal ("Unrecognized option");
283 if (globals
.input_name
!= NULL
)
284 fatal ("Only one file can be treated at once");
285 globals
.input_name
= *ptr
;
287 else if (opt
->has_arg
&& arg
!= NULL
)
295 if (globals
.mode
== SPEC
&& globals
.do_code
&& !globals
.directory
)
296 fatal ("-I must be used if generating code");
298 if (VERBOSE
&& QUIET
)
299 fatal ("Options -v and -q are mutually exclusive");
302 static void set_module_name(unsigned setUC
)
308 /* FIXME: we shouldn't assume all module extensions are .dll in winedump
309 * in some cases, we could have some .drv for example
311 /* get module name from name */
312 if ((ptr
= strrchr (globals
.input_name
, '/')))
315 ptr
= globals
.input_name
;
317 if (len
> 4 && strcmp(ptr
+ len
- 4, ".dll") == 0)
319 buf
= malloc(len
+ 1);
320 memcpy(buf
, (const void*)ptr
, len
);
322 globals
.input_module
= buf
;
323 OUTPUT_UC_DLL_NAME
= (setUC
) ? str_toupper( strdup (OUTPUT_DLL_NAME
)) : "";
326 /* Marks the symbol as 'found'! */
327 /* return: perform-search */
328 static int symbol_searched(int count
, const char *symbolname
)
330 search_symbol
*search_symbol
;
332 if (!(count
>= globals
.start_ordinal
333 && (!globals
.end_ordinal
|| count
<= globals
.end_ordinal
)))
335 if (!globals
.search_symbol
)
337 for (search_symbol
= globals
.search_symbol
;
339 search_symbol
= search_symbol
->next
)
341 if (!strcmp(symbolname
, search_symbol
->symbolname
))
343 search_symbol
->found
= 1;
350 /* return: some symbols weren't found */
351 static int symbol_finish(void)
353 const search_symbol
*search_symbol
;
356 for (search_symbol
= globals
.search_symbol
;
358 search_symbol
= search_symbol
->next
)
360 if (search_symbol
->found
)
364 /* stderr? not a practice here */
365 puts("These requested <symfile> symbols weren't found:");
368 printf("\t%s\n",search_symbol
->symbolname
);
373 /*******************************************************************
377 int main (int argc
__attribute__((unused
)), char *argv
[])
379 int main (int argc
, char *argv
[])
382 parsed_symbol symbol
;
386 globals
.forward_dll
= NULL
;
387 globals
.input_name
= NULL
;
389 parse_options (argv
);
391 memset (&symbol
, 0, sizeof (parsed_symbol
));
393 switch (globals
.mode
)
396 globals
.uc_dll_name
= "";
399 symbol_init (&symbol
, globals
.input_name
);
400 globals
.input_module
= "";
401 if (symbol_demangle (&symbol
) == -1)
402 fatal( "Symbol hasn't got a mangled name\n");
403 if (symbol
.flags
& SYM_DATA
)
404 printf (symbol
.arg_text
[0]);
406 output_prototype (stdout
, &symbol
);
407 fputc ('\n', stdout
);
408 symbol_clear(&symbol
);
412 if (globals
.input_name
== NULL
)
413 fatal("No file name has been given\n");
415 if (!dll_open (globals
.input_name
))
418 output_spec_preamble ();
419 output_header_preamble ();
420 output_c_preamble ();
422 while (!dll_next_symbol (&symbol
))
427 printf ("Export %3d - '%s' ...%c", count
, symbol
.symbol
,
428 VERBOSE
? '\n' : ' ');
430 if (globals
.do_code
&& symbol_searched(count
, symbol
.symbol
))
432 /* Attempt to get information about the symbol */
433 int result
= symbol_demangle (&symbol
);
436 result
= symbol_search (&symbol
);
438 if (!result
&& symbol
.function_name
)
439 /* Clean up the prototype */
440 symbol_clean_string (symbol
.function_name
);
443 puts (result
? "[Not Found]" : "[OK]");
448 output_spec_symbol (&symbol
);
449 output_header_symbol (&symbol
);
450 output_c_symbol (&symbol
);
452 symbol_clear (&symbol
);
456 output_install_script ();
459 puts ("Finished, Cleaning up...");
467 if (globals
.input_name
== NULL
)
468 fatal("No file name has been given\n");
470 dump_file(globals
.input_name
);