make_requests 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
  46. "char_info_t" => [ 4, 2, "&dump_char_info" ],
  47. "apc_call_t" => [ 40, 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. "luid_t" => [ 8, 4, "&dump_luid" ],
  52. "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
  53. "client_cpu_t" => [ 4, 4, "&dump_client_cpu" ],
  54. "hw_input_t" => [ 32, 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+),(\w+)\)/)
  200. {
  201. $var = $1;
  202. $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
  203. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  204. }
  205. elsif (/^\s*VARARG\((\w+),(\w+)\)/)
  206. {
  207. $var = $1;
  208. $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
  209. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  210. }
  211. elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
  212. {
  213. $type = $1;
  214. $var = $3;
  215. die "Unrecognized type $type" unless defined($formats{$type});
  216. my @fmt = @{$formats{$type}};
  217. if ($offset & ($fmt[1] - 1))
  218. {
  219. my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
  220. print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
  221. print SERVER_PROT " char __pad_$offset\[$count\];\n";
  222. $offset += $count;
  223. }
  224. if ($state == 2)
  225. {
  226. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
  227. }
  228. else
  229. {
  230. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
  231. }
  232. $offset += $fmt[0];
  233. }
  234. else
  235. {
  236. die "Unrecognized syntax $_";
  237. }
  238. if ($state == 2) { push @in_struct, $type, $var; }
  239. if ($state == 3) { push @out_struct, $type, $var; }
  240. }
  241. # Pass it through into the output file
  242. print SERVER_PROT $_ . "\n";
  243. }
  244. close PROTOCOL;
  245. }
  246. ### Retrieve the server protocol version from the existing server_protocol.h file
  247. sub GET_PROTOCOL_VERSION()
  248. {
  249. my $protocol = 0;
  250. open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
  251. while (<SERVER_PROT>)
  252. {
  253. if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
  254. }
  255. close SERVER_PROT;
  256. return $protocol;
  257. }
  258. ### Retrieve the list of status and errors used in the server
  259. sub GET_ERROR_NAMES()
  260. {
  261. my %errors = ();
  262. foreach my $f (glob "server/*.c")
  263. {
  264. next if $f eq "server/trace.c";
  265. open FILE, $f or die "Can't open $f";
  266. while (<FILE>)
  267. {
  268. while (/\bSTATUS_(\w+)/g)
  269. {
  270. $errors{$1} = "STATUS_$1" unless ($1 eq "SUCCESS" || $1 eq "WAIT_0");
  271. }
  272. while (/\bset_win32_error\s*\(\s*(\w+)\s*\)/g)
  273. {
  274. $errors{$1} = "0xc0010000 | $1";
  275. }
  276. while (/\breturn\s+(WSA\w+)/g)
  277. {
  278. $errors{$1} = "0xc0010000 | $1";
  279. }
  280. }
  281. close FILE;
  282. }
  283. return %errors;
  284. }
  285. # update a file if changed
  286. sub update_file($)
  287. {
  288. my $file = shift;
  289. my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
  290. if (!$ret)
  291. {
  292. unlink "$file.new";
  293. }
  294. else
  295. {
  296. rename "$file.new", "$file";
  297. print "$file updated\n";
  298. }
  299. return $ret;
  300. }
  301. # replace some lines in a file between two markers
  302. sub replace_in_file($$$@)
  303. {
  304. my $file = shift;
  305. my $start = shift;
  306. my $end = shift;
  307. open NEW_FILE, ">$file.new" or die "cannot create $file.new";
  308. if (defined($start))
  309. {
  310. open OLD_FILE, "$file" or die "cannot open $file";
  311. while (<OLD_FILE>)
  312. {
  313. print NEW_FILE $_;
  314. last if /$start/;
  315. }
  316. }
  317. print NEW_FILE "\n", @_, "\n";
  318. if (defined($end))
  319. {
  320. my $skip=1;
  321. while (<OLD_FILE>)
  322. {
  323. $skip = 0 if /$end/;
  324. print NEW_FILE $_ unless $skip;
  325. }
  326. }
  327. close OLD_FILE if defined($start);
  328. close NEW_FILE;
  329. return update_file($file);
  330. }
  331. ### Main
  332. # Get the server protocol version
  333. my $protocol = GET_PROTOCOL_VERSION();
  334. my %errors = GET_ERROR_NAMES();
  335. ### Create server_protocol.h and print header
  336. open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
  337. print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
  338. print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
  339. print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
  340. print SERVER_PROT " */\n\n";
  341. print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
  342. print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
  343. ### Parse requests to find request/reply structure definitions
  344. PARSE_REQUESTS();
  345. ### Build the request list and structures
  346. print SERVER_PROT "\n\nenum request\n{\n";
  347. foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
  348. print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
  349. print SERVER_PROT "union generic_request\n{\n";
  350. print SERVER_PROT " struct request_max_size max_size;\n";
  351. print SERVER_PROT " struct request_header request_header;\n";
  352. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
  353. print SERVER_PROT "};\n";
  354. print SERVER_PROT "union generic_reply\n{\n";
  355. print SERVER_PROT " struct request_max_size max_size;\n";
  356. print SERVER_PROT " struct reply_header reply_header;\n";
  357. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
  358. print SERVER_PROT "};\n\n";
  359. printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
  360. print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
  361. close SERVER_PROT;
  362. update_file( "include/wine/server_protocol.h" );
  363. ### Output the dumping function tables
  364. push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
  365. foreach my $req (@requests)
  366. {
  367. push @trace_lines, " (dump_func)dump_${req}_request,\n";
  368. }
  369. push @trace_lines, "};\n\n";
  370. push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
  371. foreach my $req (@requests)
  372. {
  373. push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
  374. }
  375. push @trace_lines, "};\n\n";
  376. push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
  377. foreach my $req (@requests)
  378. {
  379. push @trace_lines, " \"$req\",\n";
  380. }
  381. push @trace_lines, "};\n\n";
  382. push @trace_lines, "static const struct\n{\n";
  383. push @trace_lines, " const char *name;\n";
  384. push @trace_lines, " unsigned int value;\n";
  385. push @trace_lines, "} status_names[] =\n{\n";
  386. foreach my $err (sort keys %errors)
  387. {
  388. push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
  389. }
  390. push @trace_lines, " { NULL, 0 }\n";
  391. push @trace_lines, "};\n";
  392. replace_in_file( "server/trace.c",
  393. "### make_requests begin ###",
  394. "### make_requests end ###",
  395. @trace_lines );
  396. ### Output the request handlers list
  397. my @request_lines = ();
  398. foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
  399. push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
  400. push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
  401. push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
  402. foreach my $req (@requests)
  403. {
  404. push @request_lines, " (req_handler)req_$req,\n";
  405. }
  406. push @request_lines, "};\n\n";
  407. foreach my $type (sort keys %formats)
  408. {
  409. my $size = ${$formats{$type}}[0];
  410. push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
  411. }
  412. push @request_lines, @asserts;
  413. push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
  414. replace_in_file( "server/request.h",
  415. "### make_requests begin ###",
  416. "### make_requests end ###",
  417. @request_lines );