kernel32: Add a short test for GetOverlappedResult.
[wine/dcerpc.git] / tools / winedump / output.c
blobea11de4a1d9ca5309e5095d8c2ff965586fa1bc0
1 /*
2 * Code generation functions
4 * Copyright 2000-2002 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"
22 #include "wine/port.h"
24 #include "winedump.h"
26 /* Output files */
27 static FILE *specfile = NULL;
28 static FILE *hfile = NULL;
29 static FILE *cfile = NULL;
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);
39 /*******************************************************************
40 * output_spec_preamble
42 * Write the first part of the .spec file
44 void output_spec_preamble (void)
46 specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
48 atexit (output_spec_postamble);
50 if (VERBOSE)
51 puts ("Creating .spec preamble");
53 fprintf (specfile,
54 "# Generated from %s by winedump\n\n", globals.input_name);
58 /*******************************************************************
59 * output_spec_symbol
61 * Write a symbol to the .spec file
63 void output_spec_symbol (const parsed_symbol *sym)
65 char ord_spec[16];
67 assert (specfile);
68 assert (sym && sym->symbol);
70 if (sym->ordinal >= 0)
71 snprintf(ord_spec, 8, "%d", sym->ordinal);
72 else
74 ord_spec[0] = '@';
75 ord_spec[1] = '\0';
77 if (sym->flags & SYM_THISCALL)
78 strcat (ord_spec, " -i386"); /* For binary compatibility only */
80 if (!globals.do_code || !sym->function_name)
82 if (sym->flags & SYM_DATA)
84 if (globals.forward_dll)
85 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
86 globals.forward_dll, sym->symbol);
88 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
89 sym->arg_name[0]);
90 return;
93 if (globals.forward_dll)
94 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
95 globals.forward_dll, sym->symbol);
96 else
97 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
99 else
101 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
103 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
104 symbol_get_call_convention(sym), sym->symbol);
106 for (; i < sym->argc; i++)
107 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
109 if (sym->argc)
110 fputc (' ', specfile);
111 fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
113 if (sym->flags & SYM_THISCALL)
114 fputs (" # __thiscall", specfile);
116 fputc ('\n',specfile);
121 /*******************************************************************
122 * output_spec_postamble
124 * Write the last part of the .spec file
126 static void output_spec_postamble (void)
128 if (specfile)
129 fclose (specfile);
130 specfile = NULL;
134 /*******************************************************************
135 * output_header_preamble
137 * Write the first part of the .h file
139 void output_header_preamble (void)
141 hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
143 atexit (output_header_postamble);
145 fprintf (hfile,
146 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
147 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
148 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
149 "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
150 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
151 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
152 OUTPUT_UC_DLL_NAME);
156 /*******************************************************************
157 * output_header_symbol
159 * Write a symbol to the .h file
161 void output_header_symbol (const parsed_symbol *sym)
163 assert (hfile);
164 assert (sym && sym->symbol);
166 if (!globals.do_code)
167 return;
169 if (sym->flags & SYM_DATA)
170 return;
172 if (!sym->function_name)
173 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
174 OUTPUT_UC_DLL_NAME, sym->symbol);
175 else
177 output_prototype (hfile, sym);
178 fputs (";\n", hfile);
183 /*******************************************************************
184 * output_header_postamble
186 * Write the last part of the .h file
188 static void output_header_postamble (void)
190 if (hfile)
192 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
193 OUTPUT_UC_DLL_NAME);
194 fclose (hfile);
195 hfile = NULL;
200 /*******************************************************************
201 * output_c_preamble
203 * Write the first part of the .c file
205 void output_c_preamble (void)
207 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
209 atexit (output_c_postamble);
211 fprintf (cfile,
212 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
213 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
214 "\n\n#include \"config.h\"\n#include \"%s_dll.h\"\n\n"
215 "WINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
216 OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
217 OUTPUT_DLL_NAME);
219 if (globals.forward_dll)
221 if (VERBOSE)
222 puts ("Creating a forwarding DLL");
224 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
227 fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
228 " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);
230 fprintf (cfile,
231 "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
232 "lpvReserved)\n{\n\tTRACE(\"(0x%%p, %%ld, %%p)\\n\",hinstDLL,"
233 "fdwReason,lpvReserved);\n\n\t"
234 "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t");
236 if (globals.forward_dll)
238 fprintf (cfile,
239 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
240 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
241 globals.forward_dll, globals.forward_dll);
243 else
244 fputs ("/* FIXME: Initialisation */", cfile);
246 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
247 cfile);
249 if (globals.forward_dll)
251 fprintf (cfile,
252 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
253 " freed\\n\");", globals.forward_dll);
255 else
256 fputs ("/* FIXME: Cleanup */", cfile);
258 fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
262 #define CPP_END if (sym->flags & SYM_THISCALL) \
263 fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
264 #define GET_THIS if (sym->flags & SYM_THISCALL) \
265 fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
267 /*******************************************************************
268 * output_c_symbol
270 * Write a symbol to the .c file
272 void output_c_symbol (const parsed_symbol *sym)
274 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
275 int is_void;
277 assert (cfile);
278 assert (sym && sym->symbol);
280 if (!globals.do_code)
281 return;
283 if (sym->flags & SYM_DATA)
285 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
286 sym->arg_text[0]);
287 return;
290 if (sym->flags & SYM_THISCALL)
291 fputs ("#ifdef __i386__\n", cfile);
293 output_c_banner(sym);
295 if (!sym->function_name)
297 /* #ifdef'd dummy */
298 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
299 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
300 globals.forward_dll ? "@forward" : "@stub");
301 CPP_END;
302 return;
305 is_void = !strcmp (sym->return_text, "void");
307 output_prototype (cfile, sym);
308 fputs ("\n{\n", cfile);
310 if (!globals.do_trace)
312 GET_THIS;
313 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
314 if (!is_void)
315 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
316 fputs ("}\n", cfile);
317 CPP_END;
318 return;
321 /* Tracing, maybe forwarding as well */
322 if (globals.forward_dll)
324 /* Write variables for calling */
325 if (sym->varargs)
326 fputs("\tva_list valist;\n", cfile);
328 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
329 symbol_get_call_convention(sym));
331 for (i = start; i < sym->argc; i++)
332 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
334 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
335 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
337 if (!is_void)
338 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
340 GET_THIS;
342 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
343 sym->symbol);
346 /* TRACE input arguments */
347 fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
349 for (i = 0; i < sym->argc; i++)
350 fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
351 get_format_str (sym->arg_type [i]));
353 fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
354 globals.forward_dll ? "forward" : "stub");
356 for (i = 0; i < sym->argc; i++)
357 if (sym->arg_type[i] != ARG_STRUCT)
358 fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
359 sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
360 sym->arg_name[i],
361 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
363 fputs (");\n", cfile);
365 if (!globals.forward_dll)
367 if (!is_void)
368 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
369 fputs ("}\n", cfile);
370 CPP_END;
371 return;
374 /* Call the DLL */
375 if (sym->varargs)
376 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
378 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
380 for (i = 0; i < sym->argc; i++)
381 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
383 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
385 /* TRACE return value */
386 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
387 get_format_str (sym->return_type));
389 if (!is_void)
391 if (sym->return_type == ARG_WIDE_STRING)
392 fputs (",debugstr_w(retVal)", cfile);
393 else
394 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
395 sym->return_type == ARG_STRUCT ? "" : "retVal");
396 fputs (");\n\treturn retVal;\n", cfile);
398 else
399 fputs (");\n", cfile);
401 fputs ("}\n", cfile);
402 CPP_END;
406 /*******************************************************************
407 * output_c_postamble
409 * Write the last part of the .c file
411 static void output_c_postamble (void)
413 if (cfile)
414 fclose (cfile);
415 cfile = NULL;
419 /*******************************************************************
420 * output_makefile
422 * Write a Wine compatible makefile.in
424 void output_makefile (void)
426 FILE *makefile = open_file ("Makefile", ".in", "w");
428 if (VERBOSE)
429 puts ("Creating makefile");
431 fprintf (makefile,
432 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
433 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
434 "MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
436 fprintf (makefile, "IMPORTS = kernel32");
437 if (globals.forward_dll)
438 fprintf (makefile, " %s", globals.forward_dll);
440 fprintf (makefile,
441 "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
442 OUTPUT_DLL_NAME);
444 if (globals.forward_dll)
445 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
447 fclose (makefile);
451 /*******************************************************************
452 * output_install_script
454 * Write a script to insert the DLL into Wine
456 * Rather than using diff/patch, several sed calls are generated
457 * so the script can be re-run at any time without breaking.
459 void output_install_script (void)
461 char cmd[128];
462 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
464 if (VERBOSE)
465 puts ("Creating install script");
467 fprintf (install_file,
468 "#!/bin/bash\n"
469 "# Generated from %s.dll by winedump.\n\n"
470 "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f $1/AUTHORS ]; then\n"
471 "\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
472 "\techo \"Usage: $0 wine-base-dir\"\n"
473 "\texit 1\n"
474 "fi\n\n"
475 "if [ -d $1/dlls/%s ]; then\n"
476 "\techo \"DLL is already present\"\n"
477 "\texit 1\n"
478 "fi\n\n"
479 "echo Adding DLL %s to Wine build tree...\n\n"
480 "mkdir $1/dlls/%s\n"
481 "cp %s.spec $1/dlls/%s\n"
482 "cp %s_main.c $1/dlls/%s\n"
483 "cp %s_dll.h $1/dlls/%s\n"
484 "cp Makefile.in $1/dlls/%s/Makefile.in\n"
485 "echo Copied DLL files\n\n"
486 "cd $1\n\n"
487 "sed '/dlls\\/x11drv\\/Makefile/"
488 "{G;s/$/dlls\\/%s\\/Makefile/;}' configure.ac >t.tmp\n"
489 "mv -f t.tmp configure.ac\n"
490 "echo Patched configure.ac\n\n"
491 "cd $1/dlls && ./make_dlls\n\n"
492 "echo Run \\'autoconf\\', \\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
493 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
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);
497 fclose (install_file);
498 snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
499 system (cmd);
503 /*******************************************************************
504 * output_prototype
506 * Write a C prototype for a parsed symbol
508 void output_prototype (FILE *file, const parsed_symbol *sym)
510 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
512 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
513 OUTPUT_UC_DLL_NAME, sym->function_name);
515 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
516 fputs ("void", file);
517 else
518 for (i = start; i < sym->argc; i++)
519 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
520 sym->arg_name [i]);
521 if (sym->varargs)
522 fputs (", ...", file);
523 fputc (')', file);
527 /*******************************************************************
528 * output_c_banner
530 * Write a function banner to the .c file
532 void output_c_banner (const parsed_symbol *sym)
534 char ord_spec[16];
535 size_t i;
537 if (sym->ordinal >= 0)
538 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
539 else
541 ord_spec[0] = '@';
542 ord_spec[1] = '\0';
545 fprintf (cfile, "/*********************************************************"
546 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
547 OUTPUT_UC_DLL_NAME, ord_spec);
549 if (globals.do_documentation && sym->function_name)
551 fputs (" *\n * PARAMS\n *\n", cfile);
553 if (!sym->argc)
554 fputs (" * None.\n *\n", cfile);
555 else
557 for (i = 0; i < sym->argc; i++)
558 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
559 get_in_or_out(sym, i),
560 strcmp (sym->arg_name [i], "_this") ? "" :
561 " Pointer to the class object (in ECX)");
563 if (sym->varargs)
564 fputs (" * ...[I]\n", cfile);
565 fputs (" *\n", cfile);
568 fputs (" * RETURNS\n *\n", cfile);
570 if (sym->return_text && !strcmp (sym->return_text, "void"))
571 fputs (" * Nothing.\n", cfile);
572 else
573 fprintf (cfile, " * %s\n", sym->return_text);
575 fputs (" *\n */\n", cfile);
579 /*******************************************************************
580 * get_format_str
582 * Get a string containing the correct format string for a type
584 static const char *get_format_str (int type)
586 switch (type)
588 case ARG_VOID: return "void";
589 case ARG_FLOAT: return "%f";
590 case ARG_DOUBLE: return "%g";
591 case ARG_POINTER: return "%p";
592 case ARG_WIDE_STRING:
593 case ARG_STRING: return "%s";
594 case ARG_LONG: return "%ld";
595 case ARG_STRUCT: return "struct";
597 assert (0);
598 return "";
602 /*******************************************************************
603 * get_in_or_out
605 * Determine if a parameter is In or In/Out
607 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
609 assert (sym && arg < sym->argc);
610 assert (globals.do_documentation);
612 if (sym->arg_flag [arg] & CT_CONST)
613 return "In";
615 switch (sym->arg_type [arg])
617 case ARG_FLOAT:
618 case ARG_DOUBLE:
619 case ARG_LONG:
620 case ARG_STRUCT: return "In";
621 case ARG_POINTER:
622 case ARG_WIDE_STRING:
623 case ARG_STRING: return "In/Out";
625 assert (0);
626 return "";