Stub implementations for AbortPrinter, AddPortEx{A,W},
[wine] / tools / winedump / main.c
1 /*
2  *  Option processing and main()
3  *
4  *  Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "winedump.h"
24
25 _globals globals; /* All global variables */
26
27
28 static void do_include (const char *arg)
29 {
30   char *newIncludes;
31
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;
38   }
39   globals.do_code = 1;
40 }
41
42
43 static inline const char* strip_ext (const char *str)
44 {
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);
50 }
51
52
53 static void do_name (const char *arg)
54 {
55   globals.dll_name = strip_ext (arg);
56 }
57
58
59 static void do_spec (const char *arg)
60 {
61     if (globals.mode != NONE) fatal("Only one mode can be specified\n");
62     globals.mode = SPEC;
63 }
64
65
66 static void do_demangle (const char *arg)
67 {
68     if (globals.mode != NONE) fatal("Only one mode can be specified\n");
69     globals.mode = DMGL;
70     globals.do_code = 1;
71 }
72
73
74 static void do_dump (const char *arg)
75 {
76     if (globals.mode != NONE) fatal("Only one mode can be specified\n");
77     globals.mode = DUMP;
78     globals.do_code = 1;
79 }
80
81
82 static void do_dumpemf(void)
83 {
84     if (globals.mode != NONE) fatal("Only one mode can be specified\n");
85     globals.mode = EMF;
86 }
87
88
89 static void do_dumplnk(void)
90 {
91     if (globals.mode != NONE) fatal("Only one mode can be specified\n");
92     globals.mode = LNK;
93 }
94
95
96 static void do_code (void)
97 {
98   globals.do_code = 1;
99 }
100
101
102 static void do_trace (void)
103 {
104   globals.do_trace = 1;
105   globals.do_code = 1;
106 }
107
108
109 static void do_forward (const char *arg)
110 {
111   globals.forward_dll = arg;
112   globals.do_trace = 1;
113   globals.do_code = 1;
114 }
115
116 static void do_document (void)
117 {
118   globals.do_documentation = 1;
119 }
120
121 static void do_cdecl (void)
122 {
123   globals.do_cdecl = 1;
124 }
125
126
127 static void do_quiet (void)
128 {
129   globals.do_quiet = 1;
130 }
131
132
133 static void do_start (const char *arg)
134 {
135   globals.start_ordinal = atoi (arg);
136   if (!globals.start_ordinal)
137     fatal ("Invalid -s option (must be numeric)");
138 }
139
140
141 static void do_end (const char *arg)
142 {
143   globals.end_ordinal = atoi (arg);
144   if (!globals.end_ordinal)
145     fatal ("Invalid -e option (must be numeric)");
146 }
147
148
149 static void do_symfile (const char *arg)
150 {
151   FILE *f;
152   char symstring[256];    /* keep count with "%<width>s" below */
153   search_symbol *symbolp,**symbolptail = &globals.search_symbol;
154
155   if (!(f = fopen(arg, "rt")))
156     fatal ("Cannot open <symfile>");
157   while (1 == fscanf(f, "%255s", symstring))    /* keep count with [<width>] above */
158   {
159     symstring[sizeof(symstring)-1] = '\0';
160     if (!(symbolp = malloc(sizeof(*symbolp) + strlen(symstring))))
161       fatal ("Out of memory");
162     strcpy(symbolp->symbolname, symstring);
163     symbolp->found = 0;
164     symbolp->next = NULL;
165     *symbolptail = symbolp;
166     symbolptail = &symbolp->next;
167   }
168   if (fclose(f))
169     fatal ("Cannot close <symfile>");
170 }
171
172
173 static void do_verbose (void)
174 {
175   globals.do_verbose = 1;
176 }
177
178
179 static void do_symdmngl (void)
180 {
181     globals.do_demangle = 1;
182 }
183
184 static void do_dumphead (void)
185 {
186     globals.do_dumpheader = 1;
187 }
188
189 static void do_dumpsect (const char* arg)
190 {
191     globals.dumpsect = arg;
192 }
193
194 static void do_dumpall(void)
195 {
196     globals.do_dumpheader = 1;
197     globals.dumpsect = "ALL";
198 }
199
200 struct option
201 {
202   const char *name;
203   Mode mode;
204   int   has_arg;
205   void  (*func) ();
206   const char *usage;
207 };
208
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}
233 };
234
235 void do_usage (void)
236 {
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);
264
265     puts ("\n");
266     exit (1);
267 }
268
269
270 /*******************************************************************
271  *          parse_options
272  *
273  * Parse options from the argv array
274  */
275 static void parse_options (char *argv[])
276 {
277   const struct option *opt;
278   char *const *ptr;
279   const char *arg = NULL;
280
281   ptr = argv + 1;
282
283   while (*ptr != NULL)
284   {
285     for (opt = option_table; opt->name; opt++)
286     {
287      if (globals.mode != NONE && opt->mode != NONE && globals.mode != opt->mode)
288        continue;
289      if (((opt->has_arg == 1) && !strncmp (*ptr, opt->name, strlen (opt->name))) ||
290          ((opt->has_arg == 2) && !strcmp (*ptr, opt->name)))
291       {
292         arg = *ptr + strlen (opt->name);
293         if (*arg == '\0') arg = *++ptr;
294         break;
295       }
296       if (!strcmp (*ptr, opt->name))
297       {
298         arg = NULL;
299         break;
300       }
301     }
302
303     if (!opt->name)
304     {
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;
310     }
311     else if (opt->has_arg && arg != NULL)
312         opt->func (arg);
313     else
314         opt->func ("");
315
316     ptr++;
317   }
318
319   if (globals.mode == SPEC && globals.do_code && !globals.directory)
320     fatal ("-I must be used if generating code");
321
322   if (VERBOSE && QUIET)
323     fatal ("Options -v and -q are mutually exclusive");
324 }
325
326 static void set_module_name(unsigned setUC)
327 {
328     const char* ptr;
329     char*       buf;
330     int         len;
331
332     /* FIXME: we shouldn't assume all module extensions are .dll in winedump
333      * in some cases, we could have some .drv for example
334      */
335     /* get module name from name */
336     if ((ptr = strrchr (globals.input_name, '/')))
337         ptr++;
338     else
339         ptr = globals.input_name;
340     len = strlen(ptr);
341     if (len > 4 && strcmp(ptr + len - 4, ".dll") == 0)
342         len -= 4;
343     buf = malloc(len + 1);
344     memcpy(buf, (const void*)ptr, len);
345     buf[len] = 0;
346     globals.input_module = buf;
347     OUTPUT_UC_DLL_NAME = (setUC) ? str_toupper( strdup (OUTPUT_DLL_NAME)) : "";
348 }
349
350 /* Marks the symbol as 'found'! */
351 /* return: perform-search */
352 static int symbol_searched(int count, const char *symbolname)
353 {
354     search_symbol *search_symbol;
355
356     if (!(count >= globals.start_ordinal
357           && (!globals.end_ordinal || count <= globals.end_ordinal)))
358         return 0;
359     if (!globals.search_symbol)
360         return 1;
361     for (search_symbol = globals.search_symbol;
362          search_symbol;
363          search_symbol = search_symbol->next)
364     {
365         if (!strcmp(symbolname, search_symbol->symbolname))
366         {
367             search_symbol->found = 1;
368             return 1;
369         }
370     }
371     return 0;
372 }
373
374 /* return: some symbols weren't found */
375 static int symbol_finish(void)
376 {
377     const search_symbol *search_symbol;
378     int started = 0;
379
380     for (search_symbol = globals.search_symbol;
381          search_symbol;
382          search_symbol = search_symbol->next)
383     {
384         if (search_symbol->found)
385             continue;
386         if (!started)
387         {
388             /* stderr? not a practice here */
389             puts("These requested <symfile> symbols weren't found:");
390             started = 1;
391         }
392         printf("\t%s\n",search_symbol->symbolname);
393     }
394     return started;
395 }
396
397 /*******************************************************************
398  *         main
399  */
400 #ifdef __GNUC__
401 int   main (int argc __attribute__((unused)), char *argv[])
402 #else
403 int   main (int argc, char *argv[])
404 #endif
405 {
406     parsed_symbol symbol;
407     int count = 0;
408
409     globals.mode = NONE;
410     globals.forward_dll = NULL;
411     globals.input_name = NULL;
412
413     parse_options (argv);
414
415     memset (&symbol, 0, sizeof (parsed_symbol));
416
417     switch (globals.mode)
418     {
419     case DMGL:
420         globals.uc_dll_name = "";
421         VERBOSE = 1;
422
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]);
429         else
430             output_prototype (stdout, &symbol);
431         fputc ('\n', stdout);
432         symbol_clear(&symbol);
433         break;
434
435     case SPEC:
436         if (globals.input_name == NULL)
437             fatal("No file name has been given\n");
438         set_module_name(1);
439         if (!dll_open (globals.input_name))
440             break;
441
442         output_spec_preamble ();
443         output_header_preamble ();
444         output_c_preamble ();
445
446         while (!dll_next_symbol (&symbol))
447         {
448             count++;
449
450             if (NORMAL)
451                 printf ("Export %3d - '%s' ...%c", count, symbol.symbol,
452                         VERBOSE ? '\n' : ' ');
453
454             if (globals.do_code && symbol_searched(count, symbol.symbol))
455             {
456                 /* Attempt to get information about the symbol */
457                 int result = symbol_demangle (&symbol);
458
459                 if (result)
460                     result = symbol_search (&symbol);
461
462                 if (!result && symbol.function_name)
463                     /* Clean up the prototype */
464                     symbol_clean_string (symbol.function_name);
465
466                 if (NORMAL)
467                     puts (result ? "[Not Found]" : "[OK]");
468             }
469             else if (NORMAL)
470                 puts ("[Ignoring]");
471
472             output_spec_symbol (&symbol);
473             output_header_symbol (&symbol);
474             output_c_symbol (&symbol);
475
476             symbol_clear (&symbol);
477         }
478
479         output_makefile ();
480         output_install_script ();
481
482         if (VERBOSE)
483             puts ("Finished, Cleaning up...");
484         if (symbol_finish())
485             return 1;
486         break;
487     case NONE:
488         do_usage();
489         break;
490     case DUMP:
491         if (globals.input_name == NULL)
492             fatal("No file name has been given\n");
493         set_module_name(0);
494         dump_file(globals.input_name);
495         break;
496     case EMF:
497         if (globals.input_name == NULL)
498             fatal("No file name has been given\n");
499         dump_emf(globals.input_name);
500         break;
501     case LNK:
502         if (globals.input_name == NULL)
503             fatal("No file name has been given\n");
504         dump_lnk(globals.input_name);
505         break;
506     }
507
508     return 0;
509 }