main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "winedump.h"
  22. _globals globals; /* All global variables */
  23. static void do_include (const char *arg)
  24. {
  25. if (!globals.directory)
  26. globals.directory = xstrdup(arg);
  27. else
  28. globals.directory = strmake( "%s %s", globals.directory, arg );
  29. globals.do_code = TRUE;
  30. }
  31. static inline const char* strip_ext (const char *str)
  32. {
  33. int len = strlen(str);
  34. if (len>4 && strcmp(str+len-4,".dll") == 0)
  35. return str_substring (str, str+len-4);
  36. else
  37. return xstrdup (str);
  38. }
  39. static void do_name (const char *arg)
  40. {
  41. globals.dll_name = strip_ext (arg);
  42. }
  43. static void do_spec (const char *arg)
  44. {
  45. if (globals.mode != NONE) fatal("Only one mode can be specified\n");
  46. globals.mode = SPEC;
  47. }
  48. static void do_demangle (const char *arg)
  49. {
  50. if (globals.mode != NONE) fatal("Only one mode can be specified\n");
  51. globals.mode = DMGL;
  52. globals.do_code = TRUE;
  53. globals.do_demangle = TRUE;
  54. }
  55. static void do_dump (const char *arg)
  56. {
  57. if (globals.mode != NONE) fatal("Only one mode can be specified\n");
  58. globals.mode = DUMP;
  59. globals.do_code = TRUE;
  60. }
  61. static void do_code (const char *arg)
  62. {
  63. globals.do_code = TRUE;
  64. }
  65. static void do_trace (const char *arg)
  66. {
  67. globals.do_trace = TRUE;
  68. globals.do_code = TRUE;
  69. }
  70. static void do_forward (const char *arg)
  71. {
  72. globals.forward_dll = arg;
  73. globals.do_trace = TRUE;
  74. globals.do_code = TRUE;
  75. }
  76. static void do_document (const char *arg)
  77. {
  78. globals.do_documentation = TRUE;
  79. }
  80. static void do_cdecl (const char *arg)
  81. {
  82. globals.do_cdecl = TRUE;
  83. }
  84. static void do_quiet (const char *arg)
  85. {
  86. globals.do_quiet = TRUE;
  87. }
  88. static void do_start (const char *arg)
  89. {
  90. globals.start_ordinal = atoi (arg);
  91. if (!globals.start_ordinal)
  92. fatal ("Invalid -s option (must be numeric)");
  93. }
  94. static void do_end (const char *arg)
  95. {
  96. globals.end_ordinal = atoi (arg);
  97. if (!globals.end_ordinal)
  98. fatal ("Invalid -e option (must be numeric)");
  99. }
  100. static void do_symfile (const char *arg)
  101. {
  102. FILE *f;
  103. char symstring[256]; /* keep count with "%<width>s" below */
  104. search_symbol *symbolp,**symbolptail = &globals.search_symbol;
  105. if (!(f = fopen(arg, "rt")))
  106. fatal ("Cannot open <symfile>");
  107. while (1 == fscanf(f, "%255s", symstring)) /* keep count with [<width>] above */
  108. {
  109. symstring[sizeof(symstring)-1] = '\0';
  110. symbolp = xmalloc(sizeof(*symbolp) + strlen(symstring));
  111. strcpy(symbolp->symbolname, symstring);
  112. symbolp->found = FALSE;
  113. symbolp->next = NULL;
  114. *symbolptail = symbolp;
  115. symbolptail = &symbolp->next;
  116. }
  117. if (fclose(f))
  118. fatal ("Cannot close <symfile>");
  119. }
  120. static void do_verbose (const char *arg)
  121. {
  122. globals.do_verbose = TRUE;
  123. }
  124. static void do_symdmngl (const char *arg)
  125. {
  126. globals.do_demangle = TRUE;
  127. }
  128. static void do_dumphead (const char *arg)
  129. {
  130. globals.do_dumpheader = TRUE;
  131. }
  132. static void do_dumpsect (const char* arg)
  133. {
  134. globals.dumpsect = arg;
  135. }
  136. static void do_rawdebug (const char *arg)
  137. {
  138. globals.do_debug = TRUE;
  139. }
  140. static void do_dumpall(const char *arg)
  141. {
  142. globals.do_dumpheader = TRUE;
  143. globals.do_dump_rawdata = TRUE;
  144. globals.do_symbol_table = TRUE;
  145. globals.dumpsect = "ALL";
  146. }
  147. static void do_symtable(const char* arg)
  148. {
  149. globals.do_symbol_table = TRUE;
  150. }
  151. struct my_option
  152. {
  153. const char *name;
  154. Mode mode;
  155. int has_arg;
  156. void (*func)(const char *arg);
  157. const char *usage;
  158. };
  159. static const struct my_option option_table[] = {
  160. {"--help",NONE, 0, do_usage, "--help Display this help message"},
  161. {"-h", NONE, 0, do_usage, "-h Synonym for --help"},
  162. {"-?", NONE, 0, do_usage, "-? Synonym for --help"},
  163. {"dump", DUMP, 0, do_dump, "dump <file> Dump the contents of 'file' (dll, exe, lib...)"},
  164. {"-C", DUMP, 0, do_symdmngl, "-C Turn on symbol demangling"},
  165. {"-f", DUMP, 0, do_dumphead, "-f Dump file header information"},
  166. {"-G", DUMP, 0, do_rawdebug, "-G Dump raw debug information"},
  167. {"-j", DUMP, 1, do_dumpsect, "-j <sect_name> Dump only the content of section 'sect_name'\n"
  168. " (import, export, debug, resource, tls, loadcfg, clr, reloc, except, apiset)"},
  169. {"-t", DUMP, 0, do_symtable, "-t Dump symbol table"},
  170. {"-x", DUMP, 0, do_dumpall, "-x Dump everything"},
  171. {"sym", DMGL, 0, do_demangle, "sym <sym> Demangle C++ symbol <sym> and exit"},
  172. {"spec", SPEC, 0, do_spec, "spec <dll> Use 'dll' for input file and generate implementation code"},
  173. {"-I", SPEC, 1, do_include, "-I <dir> Look for prototypes in 'dir' (implies -c)"},
  174. {"-c", SPEC, 0, do_code, "-c Generate skeleton code (requires -I)"},
  175. {"-t", SPEC, 0, do_trace, "-t TRACE arguments (implies -c)"},
  176. {"-f", SPEC, 1, do_forward, "-f <dll> Forward calls to 'dll' (implies -t)"},
  177. {"-D", SPEC, 0, do_document, "-D Generate documentation"},
  178. {"-o", SPEC, 1, do_name, "-o <name> Set the output dll name (default: dll). Note: strips .dll extensions"},
  179. {"-C", SPEC, 0, do_cdecl, "-C Assume __cdecl calls (default: __stdcall)"},
  180. {"-s", SPEC, 1, do_start, "-s <num> Start prototype search after symbol 'num'"},
  181. {"-e", SPEC, 1, do_end, "-e <num> End prototype search after symbol 'num'"},
  182. {"-S", SPEC, 1, do_symfile, "-S <symfile> Search only prototype names found in 'symfile'"},
  183. {"-q", SPEC, 0, do_quiet, "-q Don't show progress (quiet)."},
  184. {"-v", SPEC, 0, do_verbose, "-v Show lots of detail while working (verbose)."},
  185. {NULL, NONE, 0, NULL, NULL}
  186. };
  187. void do_usage (const char *arg)
  188. {
  189. const struct my_option *opt;
  190. printf ("Usage: winedump [-h | sym <sym> | spec <dll> | dump <file>]\n");
  191. printf ("Mode options (can be put as the mode (sym/spec/dump...) is declared):\n");
  192. printf (" When used in --help mode\n");
  193. for (opt = option_table; opt->name; opt++)
  194. if (opt->mode == NONE)
  195. printf (" %s\n", opt->usage);
  196. printf (" When used in sym mode\n");
  197. for (opt = option_table; opt->name; opt++)
  198. if (opt->mode == DMGL)
  199. printf (" %s\n", opt->usage);
  200. printf (" When used in spec mode\n");
  201. for (opt = option_table; opt->name; opt++)
  202. if (opt->mode == SPEC)
  203. printf (" %s\n", opt->usage);
  204. printf (" When used in dump mode\n");
  205. for (opt = option_table; opt->name; opt++)
  206. if (opt->mode == DUMP)
  207. printf (" %s\n", opt->usage);
  208. puts ("");
  209. exit (1);
  210. }
  211. /*******************************************************************
  212. * parse_cmdline
  213. *
  214. * Parse options from the argv array
  215. */
  216. static void parse_cmdline (char *argv[])
  217. {
  218. const struct my_option *opt;
  219. char *const *ptr;
  220. const char *arg = NULL;
  221. ptr = argv + 1;
  222. while (*ptr != NULL)
  223. {
  224. for (opt = option_table; opt->name; opt++)
  225. {
  226. if (globals.mode != NONE && opt->mode != NONE && globals.mode != opt->mode)
  227. continue;
  228. if (((opt->has_arg == 1) && !strncmp (*ptr, opt->name, strlen (opt->name))) ||
  229. ((opt->has_arg == 2) && !strcmp (*ptr, opt->name)))
  230. {
  231. arg = *ptr + strlen (opt->name);
  232. if (*arg == '\0') arg = *++ptr;
  233. break;
  234. }
  235. if (!strcmp (*ptr, opt->name))
  236. {
  237. arg = NULL;
  238. break;
  239. }
  240. }
  241. if (!opt->name)
  242. {
  243. if ((*ptr)[0] == '-')
  244. fatal ("Unrecognized option");
  245. if (globals.input_name != NULL)
  246. fatal ("Only one file can be treated at once");
  247. globals.input_name = *ptr;
  248. }
  249. else if (opt->has_arg && arg != NULL)
  250. opt->func (arg);
  251. else
  252. opt->func ("");
  253. ptr++;
  254. }
  255. if (globals.mode == SPEC && globals.do_code && !globals.directory)
  256. fatal ("-I must be used if generating code");
  257. if (VERBOSE && QUIET)
  258. fatal ("Options -v and -q are mutually exclusive");
  259. if (globals.mode == NONE)
  260. do_dump("");
  261. }
  262. static void set_module_name(BOOL setUC)
  263. {
  264. /* FIXME: we shouldn't assume all module extensions are .dll in winedump
  265. * in some cases, we could have some .drv for example
  266. */
  267. globals.input_module = replace_extension( get_basename( globals.input_name ), ".dll", "" );
  268. OUTPUT_UC_DLL_NAME = (setUC) ? str_toupper( xstrdup (OUTPUT_DLL_NAME)) : "";
  269. }
  270. /* Marks the symbol as 'found'! */
  271. /* return: perform-search */
  272. static BOOL symbol_searched(int count, const char *symbolname)
  273. {
  274. search_symbol *search_symbol;
  275. if (!(count >= globals.start_ordinal
  276. && (!globals.end_ordinal || count <= globals.end_ordinal)))
  277. return FALSE;
  278. if (!globals.search_symbol)
  279. return TRUE;
  280. for (search_symbol = globals.search_symbol;
  281. search_symbol;
  282. search_symbol = search_symbol->next)
  283. {
  284. if (!strcmp(symbolname, search_symbol->symbolname))
  285. {
  286. search_symbol->found = TRUE;
  287. return TRUE;
  288. }
  289. }
  290. return FALSE;
  291. }
  292. /* return: some symbols weren't found */
  293. static BOOL symbol_finish(void)
  294. {
  295. const search_symbol *search_symbol;
  296. BOOL started = FALSE;
  297. for (search_symbol = globals.search_symbol;
  298. search_symbol;
  299. search_symbol = search_symbol->next)
  300. {
  301. if (search_symbol->found)
  302. continue;
  303. if (!started)
  304. {
  305. /* stderr? not a practice here */
  306. puts("These requested <symfile> symbols weren't found:");
  307. started = TRUE;
  308. }
  309. printf("\t%s\n",search_symbol->symbolname);
  310. }
  311. return started;
  312. }
  313. /*******************************************************************
  314. * main
  315. */
  316. #ifdef __GNUC__
  317. int main (int argc __attribute__((unused)), char *argv[])
  318. #else
  319. int main (int argc, char *argv[])
  320. #endif
  321. {
  322. parsed_symbol symbol;
  323. int count = 0;
  324. globals.mode = NONE;
  325. globals.forward_dll = NULL;
  326. globals.input_name = NULL;
  327. globals.dumpsect = NULL;
  328. parse_cmdline (argv);
  329. memset (&symbol, 0, sizeof (parsed_symbol));
  330. switch (globals.mode)
  331. {
  332. case DMGL:
  333. VERBOSE = TRUE;
  334. if (globals.input_name == NULL)
  335. fatal("No symbol name has been given\n");
  336. printf("%s\n", get_symbol_str(globals.input_name));
  337. break;
  338. case SPEC:
  339. if (globals.input_name == NULL)
  340. fatal("No file name has been given\n");
  341. set_module_name(TRUE);
  342. if (!dll_open (globals.input_name))
  343. break;
  344. output_spec_preamble ();
  345. output_header_preamble ();
  346. output_c_preamble ();
  347. while (dll_next_symbol (&symbol))
  348. {
  349. count++;
  350. if (NORMAL)
  351. printf ("Export %3d - '%s' ...%c", count, symbol.symbol,
  352. VERBOSE ? '\n' : ' ');
  353. if (globals.do_code && symbol_searched(count, symbol.symbol))
  354. {
  355. /* Attempt to get information about the symbol */
  356. BOOL result = symbol_demangle (&symbol) || symbol_search(&symbol);
  357. if (result && symbol.function_name)
  358. /* Clean up the prototype */
  359. symbol_clean_string (symbol.function_name);
  360. if (NORMAL)
  361. puts (result ? "[OK]" : "[Not Found]");
  362. }
  363. else if (NORMAL)
  364. puts ("[Ignoring]");
  365. output_spec_symbol (&symbol);
  366. output_header_symbol (&symbol);
  367. output_c_symbol (&symbol);
  368. symbol_clear (&symbol);
  369. }
  370. output_makefile ();
  371. if (VERBOSE)
  372. puts ("Finished, Cleaning up...");
  373. if (symbol_finish())
  374. return 1;
  375. break;
  376. case NONE:
  377. do_usage(0);
  378. break;
  379. case DUMP:
  380. if (globals.input_name == NULL)
  381. fatal("No file name has been given\n");
  382. set_module_name(FALSE);
  383. dump_file(globals.input_name);
  384. break;
  385. }
  386. return 0;
  387. }