make_requests 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #! /usr/bin/perl -w
  2. #
  3. # Build the server/trace.c and server/request.h files
  4. # from the contents of server/protocol.def.
  5. #
  6. # Copyright (C) 1998 Alexandre Julliard
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. #
  22. use strict;
  23. my %formats =
  24. ( # size align format
  25. "int" => [ 4, 4, "%d" ],
  26. "short int" => [ 2, 2, "%d" ],
  27. "char" => [ 1, 1, "%c" ],
  28. "unsigned char" => [ 1, 1, "%02x" ],
  29. "unsigned short"=> [ 2, 2, "%04x" ],
  30. "unsigned int" => [ 4, 4, "%08x" ],
  31. "data_size_t" => [ 4, 4, "%u" ],
  32. "obj_handle_t" => [ 4, 4, "%04x" ],
  33. "atom_t" => [ 4, 4, "%04x" ],
  34. "user_handle_t" => [ 4, 4, "%08x" ],
  35. "process_id_t" => [ 4, 4, "%04x" ],
  36. "thread_id_t" => [ 4, 4, "%04x" ],
  37. "client_ptr_t" => [ 8, 8, "&dump_uint64" ],
  38. "mod_handle_t" => [ 8, 8, "&dump_uint64" ],
  39. "lparam_t" => [ 8, 8, "&dump_uint64" ],
  40. "apc_param_t" => [ 8, 8, "&dump_uint64" ],
  41. "file_pos_t" => [ 8, 8, "&dump_uint64" ],
  42. "mem_size_t" => [ 8, 8, "&dump_uint64" ],
  43. "affinity_t" => [ 8, 8, "&dump_uint64" ],
  44. "timeout_t" => [ 8, 8, "&dump_timeout" ],
  45. "abstime_t" => [ 8, 8, "&dump_abstime" ],
  46. "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
  47. "apc_call_t" => [ 48, 8, "&dump_apc_call" ],
  48. "apc_result_t" => [ 40, 8, "&dump_apc_result" ],
  49. "async_data_t" => [ 40, 8, "&dump_async_data" ],
  50. "irp_params_t" => [ 32, 8, "&dump_irp_params" ],
  51. "struct luid" => [ 8, 4, "&dump_luid" ],
  52. "generic_map_t" => [ 16, 4, "&dump_generic_map" ],
  53. "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
  54. "hw_input_t" => [ 40, 8, "&dump_hw_input" ],
  55. );
  56. my @requests = ();
  57. my %replies = ();
  58. my @asserts = ();
  59. my @trace_lines = ();
  60. my $max_req_size = 64;
  61. my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
  62. sub add_padding($$)
  63. {
  64. my ($offset, $padding) = @_;
  65. if ($offset % $padding)
  66. {
  67. my $count = $padding - ($offset % $padding);
  68. print SERVER_PROT " char __pad_$offset\[$count\];\n";
  69. $offset += $count;
  70. }
  71. return $offset;
  72. }
  73. ### Generate a dumping function
  74. sub DO_DUMP_FUNC($$@)
  75. {
  76. my $name = shift;
  77. my $req = shift;
  78. my $prefix = " ";
  79. push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
  80. while ($#_ >= 0)
  81. {
  82. my $type = shift;
  83. my $var = shift;
  84. next if $var =~ /^__pad/;
  85. if (defined($formats{$type}))
  86. {
  87. my $fmt = ${$formats{$type}}[2];
  88. if ($fmt =~ /^&(.*)/)
  89. {
  90. my $func = $1;
  91. push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
  92. }
  93. elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
  94. {
  95. my ($format, $cast) = ($1, $2);
  96. push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
  97. }
  98. else
  99. {
  100. push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
  101. }
  102. }
  103. else # must be some varargs format
  104. {
  105. push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
  106. }
  107. $prefix = ", ";
  108. }
  109. push @trace_lines, "}\n\n";
  110. }
  111. ### Parse the request definitions
  112. sub PARSE_REQUESTS()
  113. {
  114. # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
  115. my $state = 0;
  116. my $offset = 0;
  117. my $name = "";
  118. my @in_struct = ();
  119. my @out_struct = ();
  120. open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
  121. while (<PROTOCOL>)
  122. {
  123. my ($type, $var);
  124. # strip comments
  125. s!/\*.*\*/!!g;
  126. # strip white space at end of line
  127. s/\s+$//;
  128. if (/^\@HEADER/)
  129. {
  130. die "Misplaced \@HEADER" unless $state == 0;
  131. $state++;
  132. next;
  133. }
  134. # ignore everything while in state 0
  135. next if $state == 0;
  136. if (/^\@REQ\(\s*(\w+)\s*\)/)
  137. {
  138. $name = $1;
  139. die "Misplaced \@REQ" unless $state == 1;
  140. # start a new request
  141. @in_struct = ();
  142. @out_struct = ();
  143. $offset = 12;
  144. print SERVER_PROT "struct ${name}_request\n{\n";
  145. print SERVER_PROT " struct request_header __header;\n";
  146. $state++;
  147. next;
  148. }
  149. if (/^\@REPLY/)
  150. {
  151. die "Misplaced \@REPLY" unless $state == 2;
  152. $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
  153. die "request $name too large ($offset)" if ($offset > $max_req_size);
  154. push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
  155. print SERVER_PROT "};\n";
  156. print SERVER_PROT "struct ${name}_reply\n{\n";
  157. print SERVER_PROT " struct reply_header __header;\n";
  158. $offset = 8;
  159. $state++;
  160. next;
  161. }
  162. if (/^\@END/)
  163. {
  164. die "Misplaced \@END" unless ($state == 2 || $state == 3);
  165. $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
  166. print SERVER_PROT "};\n";
  167. if ($state == 2) # build dummy reply struct
  168. {
  169. die "request $name too large ($offset)" if ($offset > $max_req_size);
  170. push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
  171. print SERVER_PROT "struct ${name}_reply\n{\n";
  172. print SERVER_PROT " struct reply_header __header;\n";
  173. print SERVER_PROT "};\n";
  174. }
  175. else
  176. {
  177. die "reply $name too large ($offset)" if ($offset > $max_req_size);
  178. push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
  179. }
  180. # got a complete request
  181. push @requests, $name;
  182. DO_DUMP_FUNC( $name, "request", @in_struct);
  183. if ($#out_struct >= 0)
  184. {
  185. $replies{$name} = 1;
  186. DO_DUMP_FUNC( $name, "reply", @out_struct);
  187. }
  188. $state = 1;
  189. next;
  190. }
  191. if ($state != 1)
  192. {
  193. # skip empty lines (but keep them in output file)
  194. if (/^$/)
  195. {
  196. print SERVER_PROT "\n";
  197. next;
  198. }
  199. if (/^\s*VARARG\((\w+),(\w+),(\d+)\)/)
  200. {
  201. $var = $1;
  202. $type = "dump_varargs_$2( \"%s\", min(cur_size,$3) )";
  203. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  204. }
  205. elsif (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
  206. {
  207. $var = $1;
  208. $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
  209. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  210. }
  211. elsif (/^\s*VARARG\((\w+),(\w+)\)/)
  212. {
  213. $var = $1;
  214. $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
  215. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  216. }
  217. elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
  218. {
  219. $type = $1;
  220. $var = $3;
  221. die "Unrecognized type $type" unless defined($formats{$type});
  222. my @fmt = @{$formats{$type}};
  223. if ($offset & ($fmt[1] - 1))
  224. {
  225. my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
  226. print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
  227. print SERVER_PROT " char __pad_$offset\[$count\];\n";
  228. $offset += $count;
  229. }
  230. if ($state == 2)
  231. {
  232. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
  233. }
  234. else
  235. {
  236. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
  237. }
  238. $offset += $fmt[0];
  239. }
  240. else
  241. {
  242. die "Unrecognized syntax $_";
  243. }
  244. if ($state == 2) { push @in_struct, $type, $var; }
  245. if ($state == 3) { push @out_struct, $type, $var; }
  246. }
  247. # Pass it through into the output file
  248. print SERVER_PROT $_ . "\n";
  249. }
  250. close PROTOCOL;
  251. }
  252. ### Retrieve the server protocol version from the existing server_protocol.h file
  253. sub GET_PROTOCOL_VERSION()
  254. {
  255. my $protocol = 0;
  256. open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
  257. while (<SERVER_PROT>)
  258. {
  259. if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
  260. }
  261. close SERVER_PROT;
  262. return $protocol;
  263. }
  264. ### Retrieve the list of status and errors used in the server
  265. sub GET_ERROR_NAMES()
  266. {
  267. my %errors = ();
  268. foreach my $f (glob "server/*.c")
  269. {
  270. next if $f eq "server/trace.c";
  271. open FILE, $f or die "Can't open $f";
  272. while (<FILE>)
  273. {
  274. while (/\bSTATUS_(\w+)/g)
  275. {
  276. $errors{$1} = "STATUS_$1" unless ($1 eq "SUCCESS" || $1 eq "WAIT_0");
  277. }
  278. while (/\bset_win32_error\s*\(\s*(\w+)\s*\)/g)
  279. {
  280. $errors{$1} = "0xc0010000 | $1";
  281. }
  282. while (/\breturn\s+(WSA\w+)/g)
  283. {
  284. $errors{$1} = "0xc0010000 | $1";
  285. }
  286. }
  287. close FILE;
  288. }
  289. return %errors;
  290. }
  291. # update a file if changed
  292. sub update_file($)
  293. {
  294. my $file = shift;
  295. my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
  296. if (!$ret)
  297. {
  298. unlink "$file.new";
  299. }
  300. else
  301. {
  302. rename "$file.new", "$file";
  303. print "$file updated\n";
  304. }
  305. return $ret;
  306. }
  307. # replace some lines in a file between two markers
  308. sub replace_in_file($$$@)
  309. {
  310. my $file = shift;
  311. my $start = shift;
  312. my $end = shift;
  313. open NEW_FILE, ">$file.new" or die "cannot create $file.new";
  314. if (defined($start))
  315. {
  316. open OLD_FILE, "$file" or die "cannot open $file";
  317. while (<OLD_FILE>)
  318. {
  319. print NEW_FILE $_;
  320. last if /$start/;
  321. }
  322. }
  323. print NEW_FILE "\n", @_, "\n";
  324. if (defined($end))
  325. {
  326. my $skip=1;
  327. while (<OLD_FILE>)
  328. {
  329. $skip = 0 if /$end/;
  330. print NEW_FILE $_ unless $skip;
  331. }
  332. }
  333. close OLD_FILE if defined($start);
  334. close NEW_FILE;
  335. return update_file($file);
  336. }
  337. ### Main
  338. # Get the server protocol version
  339. my $protocol = GET_PROTOCOL_VERSION();
  340. my %errors = GET_ERROR_NAMES();
  341. ### Create server_protocol.h and print header
  342. open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
  343. print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
  344. print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
  345. print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
  346. print SERVER_PROT " */\n\n";
  347. print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
  348. print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
  349. ### Parse requests to find request/reply structure definitions
  350. PARSE_REQUESTS();
  351. ### Build the request list and structures
  352. print SERVER_PROT "\n\nenum request\n{\n";
  353. foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
  354. print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
  355. print SERVER_PROT "union generic_request\n{\n";
  356. print SERVER_PROT " struct request_max_size max_size;\n";
  357. print SERVER_PROT " struct request_header request_header;\n";
  358. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
  359. print SERVER_PROT "};\n";
  360. print SERVER_PROT "union generic_reply\n{\n";
  361. print SERVER_PROT " struct request_max_size max_size;\n";
  362. print SERVER_PROT " struct reply_header reply_header;\n";
  363. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
  364. print SERVER_PROT "};\n\n";
  365. print SERVER_PROT "/* ### protocol_version begin ### */\n\n";
  366. printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol;
  367. print SERVER_PROT "/* ### protocol_version end ### */\n\n";
  368. print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
  369. close SERVER_PROT;
  370. if (update_file( "include/wine/server_protocol.h" ))
  371. {
  372. my @version_lines = ();
  373. push @version_lines, sprintf( "#define SERVER_PROTOCOL_VERSION %d\n", $protocol + 1 );
  374. replace_in_file( "include/wine/server_protocol.h",
  375. "### protocol_version begin ###",
  376. "### protocol_version end ###",
  377. @version_lines );
  378. }
  379. ### Output the dumping function tables
  380. push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
  381. foreach my $req (@requests)
  382. {
  383. push @trace_lines, " (dump_func)dump_${req}_request,\n";
  384. }
  385. push @trace_lines, "};\n\n";
  386. push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
  387. foreach my $req (@requests)
  388. {
  389. push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
  390. }
  391. push @trace_lines, "};\n\n";
  392. push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
  393. foreach my $req (@requests)
  394. {
  395. push @trace_lines, " \"$req\",\n";
  396. }
  397. push @trace_lines, "};\n\n";
  398. push @trace_lines, "static const struct\n{\n";
  399. push @trace_lines, " const char *name;\n";
  400. push @trace_lines, " unsigned int value;\n";
  401. push @trace_lines, "} status_names[] =\n{\n";
  402. foreach my $err (sort keys %errors)
  403. {
  404. push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
  405. }
  406. push @trace_lines, " { NULL, 0 }\n";
  407. push @trace_lines, "};\n";
  408. replace_in_file( "server/trace.c",
  409. "### make_requests begin ###",
  410. "### make_requests end ###",
  411. @trace_lines );
  412. ### Output the request handlers list
  413. my @request_lines = ();
  414. foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
  415. push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
  416. push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
  417. push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
  418. foreach my $req (@requests)
  419. {
  420. push @request_lines, " (req_handler)req_$req,\n";
  421. }
  422. push @request_lines, "};\n\n";
  423. foreach my $type (sort keys %formats)
  424. {
  425. my $size = ${$formats{$type}}[0];
  426. push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
  427. }
  428. push @request_lines, @asserts;
  429. push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
  430. replace_in_file( "server/request.h",
  431. "### make_requests begin ###",
  432. "### make_requests end ###",
  433. @request_lines );