make_requests 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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" => [ 40, 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. "krnl_cbdata_t" => [ 32, 8, "&dump_krnl_cbdata" ],
  56. );
  57. my @requests = ();
  58. my %replies = ();
  59. my @asserts = ();
  60. my @trace_lines = ();
  61. my $max_req_size = 72;
  62. my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
  63. sub add_padding($$)
  64. {
  65. my ($offset, $padding) = @_;
  66. if ($offset % $padding)
  67. {
  68. my $count = $padding - ($offset % $padding);
  69. print SERVER_PROT " char __pad_$offset\[$count\];\n";
  70. $offset += $count;
  71. }
  72. return $offset;
  73. }
  74. ### Generate a dumping function
  75. sub DO_DUMP_FUNC($$@)
  76. {
  77. my $name = shift;
  78. my $req = shift;
  79. my $prefix = " ";
  80. push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
  81. while ($#_ >= 0)
  82. {
  83. my $type = shift;
  84. my $var = shift;
  85. next if $var =~ /^__pad/;
  86. if (defined($formats{$type}))
  87. {
  88. my $fmt = ${$formats{$type}}[2];
  89. if ($fmt =~ /^&(.*)/)
  90. {
  91. my $func = $1;
  92. push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
  93. }
  94. elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
  95. {
  96. my ($format, $cast) = ($1, $2);
  97. push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
  98. }
  99. else
  100. {
  101. push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
  102. }
  103. }
  104. else # must be some varargs format
  105. {
  106. push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
  107. }
  108. $prefix = ", ";
  109. }
  110. push @trace_lines, "}\n\n";
  111. }
  112. ### Parse the request definitions
  113. sub PARSE_REQUESTS()
  114. {
  115. # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
  116. my $state = 0;
  117. my $offset = 0;
  118. my $name = "";
  119. my @in_struct = ();
  120. my @out_struct = ();
  121. open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
  122. while (<PROTOCOL>)
  123. {
  124. my ($type, $var);
  125. # strip comments
  126. s!/\*.*\*/!!g;
  127. # strip white space at end of line
  128. s/\s+$//;
  129. if (/^\@HEADER/)
  130. {
  131. die "Misplaced \@HEADER" unless $state == 0;
  132. $state++;
  133. next;
  134. }
  135. # ignore everything while in state 0
  136. next if $state == 0;
  137. if (/^\@REQ\(\s*(\w+)\s*\)/)
  138. {
  139. $name = $1;
  140. die "Misplaced \@REQ" unless $state == 1;
  141. # start a new request
  142. @in_struct = ();
  143. @out_struct = ();
  144. $offset = 12;
  145. print SERVER_PROT "struct ${name}_request\n{\n";
  146. print SERVER_PROT " struct request_header __header;\n";
  147. $state++;
  148. next;
  149. }
  150. if (/^\@REPLY/)
  151. {
  152. die "Misplaced \@REPLY" unless $state == 2;
  153. $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
  154. die "request $name too large ($offset)" if ($offset > $max_req_size);
  155. push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
  156. print SERVER_PROT "};\n";
  157. print SERVER_PROT "struct ${name}_reply\n{\n";
  158. print SERVER_PROT " struct reply_header __header;\n";
  159. $offset = 8;
  160. $state++;
  161. next;
  162. }
  163. if (/^\@END/)
  164. {
  165. die "Misplaced \@END" unless ($state == 2 || $state == 3);
  166. $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
  167. print SERVER_PROT "};\n";
  168. if ($state == 2) # build dummy reply struct
  169. {
  170. die "request $name too large ($offset)" if ($offset > $max_req_size);
  171. push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
  172. print SERVER_PROT "struct ${name}_reply\n{\n";
  173. print SERVER_PROT " struct reply_header __header;\n";
  174. print SERVER_PROT "};\n";
  175. }
  176. else
  177. {
  178. die "reply $name too large ($offset)" if ($offset > $max_req_size);
  179. push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
  180. }
  181. # got a complete request
  182. push @requests, $name;
  183. DO_DUMP_FUNC( $name, "request", @in_struct);
  184. if ($#out_struct >= 0)
  185. {
  186. $replies{$name} = 1;
  187. DO_DUMP_FUNC( $name, "reply", @out_struct);
  188. }
  189. $state = 1;
  190. next;
  191. }
  192. if ($state != 1)
  193. {
  194. # skip empty lines (but keep them in output file)
  195. if (/^$/)
  196. {
  197. print SERVER_PROT "\n";
  198. next;
  199. }
  200. if (/^\s*VARARG\((\w+),(\w+),(\d+)\)/)
  201. {
  202. $var = $1;
  203. $type = "dump_varargs_$2( \"%s\", min(cur_size,$3) )";
  204. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  205. }
  206. elsif (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
  207. {
  208. $var = $1;
  209. $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
  210. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  211. }
  212. elsif (/^\s*VARARG\((\w+),(\w+)\)/)
  213. {
  214. $var = $1;
  215. $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
  216. s!(VARARG\(.*\)\s*;)!/* $1 */!;
  217. }
  218. elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
  219. {
  220. $type = $1;
  221. $var = $3;
  222. die "Unrecognized type $type" unless defined($formats{$type});
  223. my @fmt = @{$formats{$type}};
  224. if ($offset & ($fmt[1] - 1))
  225. {
  226. my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
  227. print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
  228. print SERVER_PROT " char __pad_$offset\[$count\];\n";
  229. $offset += $count;
  230. }
  231. if ($state == 2)
  232. {
  233. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
  234. }
  235. else
  236. {
  237. push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
  238. }
  239. $offset += $fmt[0];
  240. }
  241. else
  242. {
  243. die "Unrecognized syntax $_";
  244. }
  245. if ($state == 2) { push @in_struct, $type, $var; }
  246. if ($state == 3) { push @out_struct, $type, $var; }
  247. }
  248. # Pass it through into the output file
  249. print SERVER_PROT $_ . "\n";
  250. }
  251. close PROTOCOL;
  252. }
  253. ### Retrieve the server protocol version from the existing server_protocol.h file
  254. sub GET_PROTOCOL_VERSION()
  255. {
  256. my $protocol = 0;
  257. open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
  258. while (<SERVER_PROT>)
  259. {
  260. if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
  261. }
  262. close SERVER_PROT;
  263. return $protocol;
  264. }
  265. ### Retrieve the list of status and errors used in the server
  266. sub GET_ERROR_NAMES()
  267. {
  268. my %errors = ();
  269. foreach my $f (glob "server/*.c")
  270. {
  271. next if $f eq "server/trace.c";
  272. open FILE, $f or die "Can't open $f";
  273. while (<FILE>)
  274. {
  275. while (/\bSTATUS_(\w+)/g)
  276. {
  277. $errors{$1} = "STATUS_$1" unless ($1 eq "SUCCESS" || $1 eq "WAIT_0");
  278. }
  279. while (/\bset_win32_error\s*\(\s*(\w+)\s*\)/g)
  280. {
  281. $errors{$1} = "0xc0010000 | $1";
  282. }
  283. while (/\breturn\s+(WSA\w+)/g)
  284. {
  285. $errors{$1} = "0xc0010000 | $1";
  286. }
  287. }
  288. close FILE;
  289. }
  290. return %errors;
  291. }
  292. # update a file if changed
  293. sub update_file($)
  294. {
  295. my $file = shift;
  296. my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
  297. if (!$ret)
  298. {
  299. unlink "$file.new";
  300. }
  301. else
  302. {
  303. rename "$file.new", "$file";
  304. print "$file updated\n";
  305. }
  306. return $ret;
  307. }
  308. # replace some lines in a file between two markers
  309. sub replace_in_file($$$@)
  310. {
  311. my $file = shift;
  312. my $start = shift;
  313. my $end = shift;
  314. open NEW_FILE, ">$file.new" or die "cannot create $file.new";
  315. if (defined($start))
  316. {
  317. open OLD_FILE, "$file" or die "cannot open $file";
  318. while (<OLD_FILE>)
  319. {
  320. print NEW_FILE $_;
  321. last if /$start/;
  322. }
  323. }
  324. print NEW_FILE "\n", @_, "\n";
  325. if (defined($end))
  326. {
  327. my $skip=1;
  328. while (<OLD_FILE>)
  329. {
  330. $skip = 0 if /$end/;
  331. print NEW_FILE $_ unless $skip;
  332. }
  333. }
  334. close OLD_FILE if defined($start);
  335. close NEW_FILE;
  336. return update_file($file);
  337. }
  338. ### Main
  339. # Get the server protocol version
  340. my $protocol = GET_PROTOCOL_VERSION();
  341. my %errors = GET_ERROR_NAMES();
  342. ### Create server_protocol.h and print header
  343. open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
  344. print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
  345. print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
  346. print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
  347. print SERVER_PROT " */\n\n";
  348. print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
  349. print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
  350. ### Parse requests to find request/reply structure definitions
  351. PARSE_REQUESTS();
  352. ### Build the request list and structures
  353. print SERVER_PROT "\n\nenum request\n{\n";
  354. foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
  355. print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
  356. print SERVER_PROT "union generic_request\n{\n";
  357. print SERVER_PROT " struct request_max_size max_size;\n";
  358. print SERVER_PROT " struct request_header request_header;\n";
  359. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
  360. print SERVER_PROT "};\n";
  361. print SERVER_PROT "union generic_reply\n{\n";
  362. print SERVER_PROT " struct request_max_size max_size;\n";
  363. print SERVER_PROT " struct reply_header reply_header;\n";
  364. foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
  365. print SERVER_PROT "};\n\n";
  366. print SERVER_PROT "/* ### protocol_version begin ### */\n\n";
  367. printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol;
  368. print SERVER_PROT "/* ### protocol_version end ### */\n\n";
  369. print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
  370. close SERVER_PROT;
  371. if (update_file( "include/wine/server_protocol.h" ))
  372. {
  373. my @version_lines = ();
  374. push @version_lines, sprintf( "#define SERVER_PROTOCOL_VERSION %d\n", $protocol + 1 );
  375. replace_in_file( "include/wine/server_protocol.h",
  376. "### protocol_version begin ###",
  377. "### protocol_version end ###",
  378. @version_lines );
  379. }
  380. ### Output the dumping function tables
  381. push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
  382. foreach my $req (@requests)
  383. {
  384. push @trace_lines, " (dump_func)dump_${req}_request,\n";
  385. }
  386. push @trace_lines, "};\n\n";
  387. push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
  388. foreach my $req (@requests)
  389. {
  390. push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
  391. }
  392. push @trace_lines, "};\n\n";
  393. push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
  394. foreach my $req (@requests)
  395. {
  396. push @trace_lines, " \"$req\",\n";
  397. }
  398. push @trace_lines, "};\n\n";
  399. push @trace_lines, "static const struct\n{\n";
  400. push @trace_lines, " const char *name;\n";
  401. push @trace_lines, " unsigned int value;\n";
  402. push @trace_lines, "} status_names[] =\n{\n";
  403. foreach my $err (sort keys %errors)
  404. {
  405. push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
  406. }
  407. push @trace_lines, " { NULL, 0 }\n";
  408. push @trace_lines, "};\n";
  409. replace_in_file( "server/trace.c",
  410. "### make_requests begin ###",
  411. "### make_requests end ###",
  412. @trace_lines );
  413. ### Output the request handlers list
  414. my @request_lines = ();
  415. foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
  416. push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
  417. push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
  418. push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
  419. foreach my $req (@requests)
  420. {
  421. push @request_lines, " (req_handler)req_$req,\n";
  422. }
  423. push @request_lines, "};\n\n";
  424. foreach my $type (sort keys %formats)
  425. {
  426. my $size = ${$formats{$type}}[0];
  427. push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
  428. }
  429. push @request_lines, @asserts;
  430. push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
  431. replace_in_file( "server/request.h",
  432. "### make_requests begin ###",
  433. "### make_requests end ###",
  434. @request_lines );