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
23 _globals globals
; /* All global variables */
26 static void do_include (const char *arg
)
28 globals
.directory
= arg
;
33 static inline const char* strip_ext (const char *str
)
35 char *ext
= strstr(str
, ".dll");
37 return str_substring (str
, ext
);
43 static void do_name (const char *arg
)
45 globals
.dll_name
= strip_ext (arg
);
49 static void do_spec (const char *arg
)
51 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
56 static void do_demangle (const char *arg
)
58 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
64 static void do_dump (const char *arg
)
66 if (globals
.mode
!= NONE
) fatal("Only one mode can be specified\n");
72 static void do_code (void)
78 static void do_trace (void)
85 static void do_forward (const char *arg
)
87 globals
.forward_dll
= arg
;
92 static void do_document (void)
94 globals
.do_documentation
= 1;
97 static void do_cdecl (void)
103 static void do_quiet (void)
105 globals
.do_quiet
= 1;
109 static void do_start (const char *arg
)
111 globals
.start_ordinal
= atoi (arg
);
112 if (!globals
.start_ordinal
)
113 fatal ("Invalid -s option (must be numeric)");
117 static void do_end (const char *arg
)
119 globals
.end_ordinal
= atoi (arg
);
120 if (!globals
.end_ordinal
)
121 fatal ("Invalid -e option (must be numeric)");
125 static void do_verbose (void)
127 globals
.do_verbose
= 1;
131 static void do_symdmngl (void)
133 globals
.do_demangle
= 1;
136 static void do_dumphead (void)
138 globals
.do_dumpheader
= 1;
141 static void do_dumpsect (const char* arg
)
143 globals
.dumpsect
= arg
;
146 static void do_dumpall(void)
148 globals
.do_dumpheader
= 1;
149 globals
.dumpsect
= "ALL";
161 static const struct option option_table
[] = {
162 {"-h", NONE
, 0, do_usage
, "-h Display this help message"},
163 {"sym", DMGL
, 0, do_demangle
, "sym <sym> Demangle C++ symbol <sym> and exit"},
164 {"spec", SPEC
, 0, do_spec
, "spec <dll> Use dll for input file and generate implementation code"},
165 {"-I", SPEC
, 1, do_include
, "-I dir Look for prototypes in 'dir' (implies -c)"},
166 {"-c", SPEC
, 0, do_code
, "-c Generate skeleton code (requires -I)"},
167 {"-t", SPEC
, 0, do_trace
, "-t TRACE arguments (implies -c)"},
168 {"-f", SPEC
, 1, do_forward
, "-f dll Forward calls to 'dll' (implies -t)"},
169 {"-D", SPEC
, 0, do_document
, "-D Generate documentation"},
170 {"-o", SPEC
, 1, do_name
, "-o name Set the output dll name (default: dll)"},
171 {"-C", SPEC
, 0, do_cdecl
, "-C Assume __cdecl calls (default: __stdcall)"},
172 {"-s", SPEC
, 1, do_start
, "-s num Start prototype search after symbol 'num'"},
173 {"-e", SPEC
, 1, do_end
, "-e num End prototype search after symbol 'num'"},
174 {"-q", SPEC
, 0, do_quiet
, "-q Don't show progress (quiet)."},
175 {"-v", SPEC
, 0, do_verbose
, "-v Show lots of detail while working (verbose)."},
176 {"dump", DUMP
, 0, do_dump
, "dump <mod> Dumps the content of the module (dll, exe...) named <mod>"},
177 {"-C", DUMP
, 0, do_symdmngl
, "-C Turns on symbol demangling"},
178 {"-f", DUMP
, 0, do_dumphead
, "-f Dumps file header information"},
179 {"-j", DUMP
, 1, do_dumpsect
, "-j sect_name Dumps only the content of section sect_name (import, export, debug)"},
180 {"-x", DUMP
, 0, do_dumpall
, "-x Dumps everything"},
181 {NULL
, NONE
, 0, NULL
, NULL
}
186 const struct option
*opt
;
187 printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <dll>]\n");
188 printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
189 printf ("\tWhen used in -h mode\n");
190 for (opt
= option_table
; opt
->name
; opt
++)
191 if (opt
->mode
== NONE
)
192 printf ("\t %s\n", opt
->usage
);
193 printf ("\tWhen used in sym mode\n");
194 for (opt
= option_table
; opt
->name
; opt
++)
195 if (opt
->mode
== DMGL
)
196 printf ("\t %s\n", opt
->usage
);
197 printf ("\tWhen used in spec mode\n");
198 for (opt
= option_table
; opt
->name
; opt
++)
199 if (opt
->mode
== SPEC
)
200 printf ("\t %s\n", opt
->usage
);
201 printf ("\tWhen used in dump mode\n");
202 for (opt
= option_table
; opt
->name
; opt
++)
203 if (opt
->mode
== DUMP
)
204 printf ("\t %s\n", opt
->usage
);
211 /*******************************************************************
214 * Parse options from the argv array
216 static void parse_options (char *argv
[])
218 const struct option
*opt
;
220 const char *arg
= NULL
;
226 for (opt
= option_table
; opt
->name
; opt
++)
228 if (globals
.mode
!= NONE
&& opt
->mode
!= NONE
&& globals
.mode
!= opt
->mode
)
230 if (((opt
->has_arg
== 1) && !strncmp (*ptr
, opt
->name
, strlen (opt
->name
))) ||
231 ((opt
->has_arg
== 2) && !strcmp (*ptr
, opt
->name
)))
233 arg
= *ptr
+ strlen (opt
->name
);
234 if (*arg
== '\0') arg
= *++ptr
;
237 if (!strcmp (*ptr
, opt
->name
))
246 if ((*ptr
)[0] == '-')
247 fatal ("Unrecognized option");
248 if (globals
.input_name
!= NULL
)
249 fatal ("Only one file can be treated at once");
250 globals
.input_name
= *ptr
;
252 else if (opt
->has_arg
&& arg
!= NULL
)
260 if (globals
.mode
== SPEC
&& globals
.do_code
&& !globals
.directory
)
261 fatal ("-I must be used if generating code");
263 if (VERBOSE
&& QUIET
)
264 fatal ("Options -v and -q are mutually exclusive");
267 static void set_module_name(unsigned setUC
)
273 /* FIXME: we shouldn't assume all module extensions are .dll in winedump
274 * in some cases, we could have some .drv for example
276 /* get module name from name */
277 if ((ptr
= strrchr (globals
.input_name
, '/')))
280 ptr
= globals
.input_name
;
282 if (len
> 4 && strcmp(ptr
+ len
- 4, ".dll") == 0)
284 buf
= malloc(len
+ 1);
285 memcpy(buf
, (void*)ptr
, len
);
287 globals
.input_module
= buf
;
288 OUTPUT_UC_DLL_NAME
= (setUC
) ? str_toupper( strdup (OUTPUT_DLL_NAME
)) : "";
291 /*******************************************************************
295 int main (int argc
__attribute__((unused
)), char *argv
[])
297 int main (int argc
, char *argv
[])
300 parsed_symbol symbol
;
304 globals
.forward_dll
= NULL
;
305 globals
.input_name
= NULL
;
307 parse_options (argv
);
309 memset (&symbol
, 0, sizeof (parsed_symbol
));
311 switch (globals
.mode
)
314 globals
.uc_dll_name
= "";
317 symbol_init (&symbol
, globals
.input_name
);
318 globals
.input_module
= "";
319 if (symbol_demangle (&symbol
) == -1)
320 fatal( "Symbol hasn't got a mangled name\n");
321 if (symbol
.flags
& SYM_DATA
)
322 printf (symbol
.arg_text
[0]);
324 output_prototype (stdout
, &symbol
);
325 fputc ('\n', stdout
);
326 symbol_clear(&symbol
);
331 dll_open (globals
.input_name
);
333 output_spec_preamble ();
334 output_header_preamble ();
335 output_c_preamble ();
337 while (!dll_next_symbol (&symbol
))
342 printf ("Export %3d - '%s' ...%c", count
, symbol
.symbol
,
343 VERBOSE
? '\n' : ' ');
345 if (globals
.do_code
&& count
>= globals
.start_ordinal
346 && (!globals
.end_ordinal
|| count
<= globals
.end_ordinal
))
348 /* Attempt to get information about the symbol */
349 int result
= symbol_demangle (&symbol
);
352 result
= symbol_search (&symbol
);
354 if (!result
&& symbol
.function_name
)
355 /* Clean up the prototype */
356 symbol_clean_string (symbol
.function_name
);
359 puts (result
? "[Not Found]" : "[OK]");
364 output_spec_symbol (&symbol
);
365 output_header_symbol (&symbol
);
366 output_c_symbol (&symbol
);
368 symbol_clear (&symbol
);
372 output_install_script ();
375 puts ("Finished, Cleaning up...");
382 dump_file(globals
.input_name
);