Added regedit unit test, a couple minor changes to regedit.
[wine] / tools / winedump / output.c
1 /*
2  *  Code generation functions
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 #include "wine/port.h"
23
24 #include "winedump.h"
25
26 /* Output files */
27 static FILE *specfile = NULL;
28 static FILE *hfile    = NULL;
29 static FILE *cfile    = NULL;
30
31 static void  output_spec_postamble (void);
32 static void  output_header_postamble (void);
33 static void  output_c_postamble (void);
34 static void  output_c_banner (const parsed_symbol *sym);
35 static const char *get_format_str (int type);
36 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
37
38
39 /*******************************************************************
40  *         output_spec_preamble
41  *
42  * Write the first part of the .spec file
43  */
44 void  output_spec_preamble (void)
45 {
46   specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
47
48   atexit (output_spec_postamble);
49
50   if (VERBOSE)
51     puts ("Creating .spec preamble");
52
53   fprintf (specfile,
54            "# Generated from %s by winedump\nname    %s\n"
55            "type    win32\ninit    %s_Init\n\nimport kernel32.dll\n"
56            "import ntdll.dll\n", globals.input_name, OUTPUT_DLL_NAME,
57            OUTPUT_UC_DLL_NAME);
58
59   if (globals.forward_dll)
60     fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
61
62   fprintf (specfile, "\n\ndebug_channels (%s)\n\n", OUTPUT_DLL_NAME);
63 }
64
65
66 /*******************************************************************
67  *         output_spec_symbol
68  *
69  * Write a symbol to the .spec file
70  */
71 void  output_spec_symbol (const parsed_symbol *sym)
72 {
73   char ord_spec[16];
74
75   assert (specfile);
76   assert (sym && sym->symbol);
77
78   if (sym->ordinal >= 0)
79     snprintf(ord_spec, 8, "%d", sym->ordinal);
80   else
81   {
82     ord_spec[0] = '@';
83     ord_spec[1] = '\0';
84   }
85   if (sym->flags & SYM_THISCALL)
86     strcat (ord_spec, " -i386"); /* For binary compatibility only */
87
88   if (!globals.do_code || !sym->function_name)
89   {
90     if (sym->flags & SYM_DATA)
91     {
92       if (globals.forward_dll)
93         fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
94                  globals.forward_dll, sym->symbol);
95
96       fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
97                sym->arg_name[0]);
98       return;
99     }
100
101     if (globals.forward_dll)
102       fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
103                globals.forward_dll, sym->symbol);
104     else
105       fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
106   }
107   else
108   {
109     unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
110
111     fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
112              symbol_get_call_convention(sym), sym->symbol);
113
114     for (; i < sym->argc; i++)
115       fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
116
117     if (sym->argc)
118       fputc (' ', specfile);
119     fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
120
121     if (sym->flags & SYM_THISCALL)
122       fputs (" # __thiscall", specfile);
123
124     fputc ('\n',specfile);
125   }
126 }
127
128
129 /*******************************************************************
130  *         output_spec_postamble
131  *
132  * Write the last part of the .spec file
133  */
134 static void output_spec_postamble (void)
135 {
136   if (specfile)
137     fclose (specfile);
138   specfile = NULL;
139 }
140
141
142 /*******************************************************************
143  *         output_header_preamble
144  *
145  * Write the first part of the .h file
146  */
147 void  output_header_preamble (void)
148 {
149   hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
150
151   atexit (output_header_postamble);
152
153   fprintf (hfile,
154            "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
155            " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
156            "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
157            "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
158            "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
159            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
160            OUTPUT_UC_DLL_NAME);
161 }
162
163
164 /*******************************************************************
165  *         output_header_symbol
166  *
167  * Write a symbol to the .h file
168  */
169 void  output_header_symbol (const parsed_symbol *sym)
170 {
171   assert (hfile);
172   assert (sym && sym->symbol);
173
174   if (!globals.do_code)
175     return;
176
177   if (sym->flags & SYM_DATA)
178     return;
179
180   if (!sym->function_name)
181     fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
182              OUTPUT_UC_DLL_NAME, sym->symbol);
183   else
184   {
185     output_prototype (hfile, sym);
186     fputs (";\n", hfile);
187   }
188 }
189
190
191 /*******************************************************************
192  *         output_header_postamble
193  *
194  * Write the last part of the .h file
195  */
196 static void output_header_postamble (void)
197 {
198   if (hfile)
199   {
200     fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
201              OUTPUT_UC_DLL_NAME);
202     fclose (hfile);
203     hfile = NULL;
204   }
205 }
206
207
208 /*******************************************************************
209  *         output_c_preamble
210  *
211  * Write the first part of the .c file
212  */
213 void  output_c_preamble (void)
214 {
215   cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
216
217   atexit (output_c_postamble);
218
219   fprintf (cfile,
220            "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
221            " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
222            "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
223            OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
224            OUTPUT_DLL_NAME);
225
226   if (globals.forward_dll)
227   {
228     if (VERBOSE)
229       puts ("Creating a forwarding DLL");
230
231     fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
232   }
233
234   fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
235          " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);
236
237   fprintf (cfile,
238            "BOOL WINAPI %s_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
239            "lpvReserved)\n{\n\tTRACE(\"(0x%%08x, %%ld, %%p)\\n\",hinstDLL,"
240            "fdwReason,lpvReserved);\n\n\t"
241            "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t",
242            OUTPUT_UC_DLL_NAME);
243
244   if (globals.forward_dll)
245   {
246     fprintf (cfile,
247              "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
248              "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
249              globals.forward_dll, globals.forward_dll);
250   }
251   else
252     fputs ("/* FIXME: Initialisation */", cfile);
253
254   fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
255          cfile);
256
257   if (globals.forward_dll)
258   {
259     fprintf (cfile,
260              "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
261              " freed\\n\");", globals.forward_dll);
262   }
263   else
264     fputs ("/* FIXME: Cleanup */", cfile);
265
266   fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
267 }
268
269
270 #define CPP_END  if (sym->flags & SYM_THISCALL) \
271   fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
272 #define GET_THIS if (sym->flags & SYM_THISCALL) \
273   fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
274
275 /*******************************************************************
276  *         output_c_symbol
277  *
278  * Write a symbol to the .c file
279  */
280 void  output_c_symbol (const parsed_symbol *sym)
281 {
282   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
283   int is_void;
284
285   assert (cfile);
286   assert (sym && sym->symbol);
287
288   if (!globals.do_code)
289     return;
290
291   if (sym->flags & SYM_DATA)
292   {
293     fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
294              sym->arg_text[0]);
295     return;
296   }
297
298   if (sym->flags & SYM_THISCALL)
299     fputs ("#ifdef __i386__\n", cfile);
300
301   output_c_banner(sym);
302
303   if (!sym->function_name)
304   {
305     /* #ifdef'd dummy */
306     fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
307              symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
308              globals.forward_dll ? "@forward" : "@stub");
309     CPP_END;
310     return;
311   }
312
313   is_void = !strcmp (sym->return_text, "void");
314
315   output_prototype (cfile, sym);
316   fputs ("\n{\n", cfile);
317
318   if (!globals.do_trace)
319   {
320     GET_THIS;
321     fputs ("\tFIXME(\":stub\\n\");\n", cfile);
322     if (!is_void)
323         fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
324     fputs ("}\n", cfile);
325     CPP_END;
326     return;
327   }
328
329   /* Tracing, maybe forwarding as well */
330   if (globals.forward_dll)
331   {
332     /* Write variables for calling */
333     if (sym->varargs)
334       fputs("\tva_list valist;\n", cfile);
335
336     fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
337              symbol_get_call_convention(sym));
338
339     for (i = start; i < sym->argc; i++)
340       fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
341
342     fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
343              sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
344
345     if (!is_void)
346       fprintf (cfile, "\t%s retVal;\n", sym->return_text);
347
348     GET_THIS;
349
350     fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
351              sym->symbol);
352   }
353
354   /* TRACE input arguments */
355   fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
356
357   for (i = 0; i < sym->argc; i++)
358     fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
359              get_format_str (sym->arg_type [i]));
360
361   fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
362            globals.forward_dll ? "forward" : "stub");
363
364   for (i = 0; i < sym->argc; i++)
365     if (sym->arg_type[i] != ARG_STRUCT)
366       fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
367               sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
368               sym->arg_name[i],
369               sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
370
371   fputs (");\n", cfile);
372
373   if (!globals.forward_dll)
374   {
375     if (!is_void)
376       fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
377     fputs ("}\n", cfile);
378     CPP_END;
379     return;
380   }
381
382   /* Call the DLL */
383   if (sym->varargs)
384     fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
385
386   fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
387
388   for (i = 0; i < sym->argc; i++)
389     fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
390
391   fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
392
393   /* TRACE return value */
394   fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
395            get_format_str (sym->return_type));
396
397   if (!is_void)
398   {
399     if (sym->return_type == ARG_WIDE_STRING)
400       fputs (",debugstr_w(retVal)", cfile);
401     else
402       fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
403                sym->return_type == ARG_STRUCT ? "" : "retVal");
404     fputs (");\n\treturn retVal;\n", cfile);
405   }
406   else
407     fputs (");\n", cfile);
408
409   fputs ("}\n", cfile);
410   CPP_END;
411 }
412
413
414 /*******************************************************************
415  *         output_c_postamble
416  *
417  * Write the last part of the .c file
418  */
419 static void output_c_postamble (void)
420 {
421   if (cfile)
422     fclose (cfile);
423   cfile = NULL;
424 }
425
426
427 /*******************************************************************
428  *         output_makefile
429  *
430  * Write a Wine compatible makefile.in
431  */
432 void  output_makefile (void)
433 {
434   FILE *makefile = open_file ("Makefile", ".in", "w");
435
436   if (VERBOSE)
437     puts ("Creating makefile");
438
439   fprintf (makefile,
440            "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
441            "TOPOBJDIR = ../..\nSRCDIR    = @srcdir@\nVPATH     = @srcdir@\n"
442            "MODULE    = %s\nEXTRALIBS = $(LIBUNICODE)\n\n"
443            "LDDLLFLAGS = @LDDLLFLAGS@\nSYMBOLFILE = $(MODULE).tmp.o\n\n"
444            "C_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
445            globals.input_name, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
446
447   fclose (makefile);
448 }
449
450
451 /*******************************************************************
452  *         output_install_script
453  *
454  * Write a script to insert the DLL into Wine
455  *
456  * Rather than using diff/patch, several sed calls are generated
457  * so the script can be re-run at any time without breaking.
458  */
459 void  output_install_script (void)
460 {
461   char cmd[128];
462   FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
463
464   if (VERBOSE)
465     puts ("Creating install script");
466
467   fprintf (install_file,
468            "#!/bin/bash\n# Generated from %s.dll by winedump.\n\n"
469            "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f"
470            " $1/AUTHORS ]; then\n\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
471            "\techo \"Usage: $0 wine-base-dir\"\n\texit 1\nfi\n\n"
472            "if [ -d $1/dlls/%s ]; then\n\techo \"DLL is already present\"\n"
473            "\texit 1\nfi\n\necho Adding DLL %s to Wine build tree...\n"
474            "echo\n\nmkdir $1/dlls/%s\ncp %s.spec $1/dlls/%s\n"
475            "cp %s_main.c $1/dlls/%s\ncp %s_dll.h $1/dlls/%s\n"
476            "cp Makefile.in $1/dlls/%s/Makefile.in\necho Copied DLL files\n\n"
477            "cd $1\n\nsed '/dlls\\/"
478            "x11drv\\/Makefile/{G;s/$/dlls\\/%s\\/Makefile/;}' configure.in"
479            " >t.tmp\nmv -f t.tmp configure.in\necho Patched configure.in\n\n"
480            "sed '/all:/{G;s/$/\\^lib%s.so \\\\/;}'"
481            " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
482            "sed '/SUBDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
483            "\nsed '/Map library name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
484            "(LN_S\\) %s\\/lib%s.\\$(LIBEXT) \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
485            " > t.tmp\nsed '/Map library name /{G;s/$/lib%s.\\$(LIBEXT): "
486            "%s\\/lib%s.\\$(LIBEXT)/;}' t.tmp > t.tmp2\nsed '/dll "
487            "dependencies/{G;s/$/^\\@cd %s \\&\\& \\$(MAKE) lib%s.\\$(LIBEXT)"
488            "/;}' t.tmp2 | tr ^ \\\\t > t.tmp\nsed '/dll "
489            "dependencies/{G;s/$/%s\\/lib%s.\\$(LIBEXT)\\: libkernel32."
490            "\\$(LIBEXT) libntdll.\\$(LIBEXT)/;}' t.tmp > t.tmp2\n"
491            "mv -f t.tmp2 dlls/Makefile.in\nrm -f t.tmp\necho Patched dlls/"
492            "Makefile.in\n\necho\necho ...done.\necho Run \\'autoconf\\', "
493            "\\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
494            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
495            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
496            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
497            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
498            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
499            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
500
501   fclose (install_file);
502   snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
503   system (cmd);
504 }
505
506
507 /*******************************************************************
508  *         output_prototype
509  *
510  * Write a C prototype for a parsed symbol
511  */
512 void  output_prototype (FILE *file, const parsed_symbol *sym)
513 {
514   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
515
516   fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
517            OUTPUT_UC_DLL_NAME, sym->function_name);
518
519   if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
520     fputs ("void", file);
521   else
522     for (i = start; i < sym->argc; i++)
523       fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
524                sym->arg_name [i]);
525   if (sym->varargs)
526     fputs (", ...", file);
527   fputc (')', file);
528 }
529
530
531 /*******************************************************************
532  *         output_c_banner
533  *
534  * Write a function banner to the .c file
535  */
536 void  output_c_banner (const parsed_symbol *sym)
537 {
538   char ord_spec[16];
539   size_t i;
540
541   if (sym->ordinal >= 0)
542     snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
543   else
544   {
545     ord_spec[0] = '@';
546     ord_spec[1] = '\0';
547   }
548
549   fprintf (cfile, "/*********************************************************"
550            "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
551            OUTPUT_UC_DLL_NAME, ord_spec);
552
553   if (globals.do_documentation && sym->function_name)
554   {
555     fputs (" *\n * PARAMS\n *\n", cfile);
556
557     if (!sym->argc)
558       fputs (" *  None.\n *\n", cfile);
559     else
560     {
561       for (i = 0; i < sym->argc; i++)
562         fprintf (cfile, " *  %s [%s]%s\n", sym->arg_name [i],
563                  get_in_or_out(sym, i),
564                  strcmp (sym->arg_name [i], "_this") ? "" :
565                  "     Pointer to the class object (in ECX)");
566
567       if (sym->varargs)
568         fputs (" *  ...[I]\n", cfile);
569       fputs (" *\n", cfile);
570     }
571
572     fputs (" * RETURNS\n *\n", cfile);
573
574     if (sym->return_text && !strcmp (sym->return_text, "void"))
575       fputs (" *  Nothing.\n", cfile);
576     else
577       fprintf (cfile, " *  %s\n", sym->return_text);
578   }
579   fputs (" *\n */\n", cfile);
580 }
581
582
583 /*******************************************************************
584  *         get_format_str
585  *
586  * Get a string containing the correct format string for a type
587  */
588 static const char *get_format_str (int type)
589 {
590   switch (type)
591   {
592   case ARG_VOID:        return "void";
593   case ARG_FLOAT:       return "%f";
594   case ARG_DOUBLE:      return "%g";
595   case ARG_POINTER:     return "%p";
596   case ARG_WIDE_STRING:
597   case ARG_STRING:      return "%s";
598   case ARG_LONG:        return "%ld";
599   case ARG_STRUCT:      return "struct";
600   }
601   assert (0);
602   return "";
603 }
604
605
606 /*******************************************************************
607  *         get_in_or_out
608  *
609  * Determine if a parameter is In or In/Out
610  */
611 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
612 {
613   assert (sym && arg < sym->argc);
614   assert (globals.do_documentation);
615
616   if (sym->arg_flag [arg] & CT_CONST)
617     return "In";
618
619   switch (sym->arg_type [arg])
620   {
621   case ARG_FLOAT:
622   case ARG_DOUBLE:
623   case ARG_LONG:
624   case ARG_STRUCT:      return "In";
625   case ARG_POINTER:
626   case ARG_WIDE_STRING:
627   case ARG_STRING:      return "In/Out";
628   }
629   assert (0);
630   return "";
631 }