Declare SetLayeredWindowAttributes.
[wine/hacks.git] / tools / winedump / main.c
blobd92d011fc8dbc1c41f8018439682e88e190711ae
1 /*
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
21 #include "config.h"
23 #include "winedump.h"
25 _globals globals; /* All global variables */
28 static void do_include (const char *arg)
30 char *newIncludes;
32 if (!globals.directory)
33 globals.directory = strdup(arg);
34 else {
35 newIncludes = str_create (3,globals.directory," ",arg);
36 free(globals.directory);
37 globals.directory = newIncludes;
39 globals.do_code = 1;
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);
48 else
49 return strdup (str);
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");
62 globals.mode = SPEC;
66 static void do_demangle (const char *arg)
68 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
69 globals.mode = DMGL;
70 globals.do_code = 1;
74 static void do_dump (const char *arg)
76 if (globals.mode != NONE) fatal("Only one mode can be specified\n");
77 globals.mode = DUMP;
78 globals.do_code = 1;
82 static void do_code (void)
84 globals.do_code = 1;
88 static void do_trace (void)
90 globals.do_trace = 1;
91 globals.do_code = 1;
95 static void do_forward (const char *arg)
97 globals.forward_dll = arg;
98 globals.do_trace = 1;
99 globals.do_code = 1;
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)
137 FILE *f;
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);
149 symbolp->found = 0;
150 symbolp->next = NULL;
151 *symbolptail = symbolp;
152 symbolptail = &symbolp->next;
154 if (fclose(f))
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";
186 struct option
188 const char *name;
189 Mode mode;
190 int has_arg;
191 void (*func) ();
192 const char *usage;
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}
219 void do_usage (void)
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);
241 puts ("\n");
242 exit (1);
246 /*******************************************************************
247 * parse_options
249 * Parse options from the argv array
251 static void parse_options (char *argv[])
253 const struct option *opt;
254 char *const *ptr;
255 const char *arg = NULL;
257 ptr = argv + 1;
259 while (*ptr != NULL)
261 for (opt = option_table; opt->name; opt++)
263 if (globals.mode != NONE && opt->mode != NONE && globals.mode != opt->mode)
264 continue;
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;
270 break;
272 if (!strcmp (*ptr, opt->name))
274 arg = NULL;
275 break;
279 if (!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)
288 opt->func (arg);
289 else
290 opt->func ("");
292 ptr++;
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)
304 const char* ptr;
305 char* buf;
306 int len;
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, '/')))
313 ptr++;
314 else
315 ptr = globals.input_name;
316 len = strlen(ptr);
317 if (len > 4 && strcmp(ptr + len - 4, ".dll") == 0)
318 len -= 4;
319 buf = malloc(len + 1);
320 memcpy(buf, (const void*)ptr, len);
321 buf[len] = 0;
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)))
334 return 0;
335 if (!globals.search_symbol)
336 return 1;
337 for (search_symbol = globals.search_symbol;
338 search_symbol;
339 search_symbol = search_symbol->next)
341 if (!strcmp(symbolname, search_symbol->symbolname))
343 search_symbol->found = 1;
344 return 1;
347 return 0;
350 /* return: some symbols weren't found */
351 static int symbol_finish(void)
353 const search_symbol *search_symbol;
354 int started = 0;
356 for (search_symbol = globals.search_symbol;
357 search_symbol;
358 search_symbol = search_symbol->next)
360 if (search_symbol->found)
361 continue;
362 if (!started)
364 /* stderr? not a practice here */
365 puts("These requested <symfile> symbols weren't found:");
366 started = 1;
368 printf("\t%s\n",search_symbol->symbolname);
370 return started;
373 /*******************************************************************
374 * main
376 #ifdef __GNUC__
377 int main (int argc __attribute__((unused)), char *argv[])
378 #else
379 int main (int argc, char *argv[])
380 #endif
382 parsed_symbol symbol;
383 int count = 0;
385 globals.mode = NONE;
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)
395 case DMGL:
396 globals.uc_dll_name = "";
397 VERBOSE = 1;
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]);
405 else
406 output_prototype (stdout, &symbol);
407 fputc ('\n', stdout);
408 symbol_clear(&symbol);
409 break;
411 case SPEC:
412 if (globals.input_name == NULL)
413 fatal("No file name has been given\n");
414 set_module_name(1);
415 if (!dll_open (globals.input_name))
416 break;
418 output_spec_preamble ();
419 output_header_preamble ();
420 output_c_preamble ();
422 while (!dll_next_symbol (&symbol))
424 count++;
426 if (NORMAL)
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);
435 if (result)
436 result = symbol_search (&symbol);
438 if (!result && symbol.function_name)
439 /* Clean up the prototype */
440 symbol_clean_string (symbol.function_name);
442 if (NORMAL)
443 puts (result ? "[Not Found]" : "[OK]");
445 else if (NORMAL)
446 puts ("[Ignoring]");
448 output_spec_symbol (&symbol);
449 output_header_symbol (&symbol);
450 output_c_symbol (&symbol);
452 symbol_clear (&symbol);
455 output_makefile ();
456 output_install_script ();
458 if (VERBOSE)
459 puts ("Finished, Cleaning up...");
460 if (symbol_finish())
461 return 1;
462 break;
463 case NONE:
464 do_usage();
465 break;
466 case DUMP:
467 if (globals.input_name == NULL)
468 fatal("No file name has been given\n");
469 set_module_name(0);
470 dump_file(globals.input_name);
471 break;
474 return 0;