Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1100 lines
25 KiB

  1. #!/usr/local/bin/perl
  2. ##
  3. ## Copyright (c) 2000, Intel Corporation
  4. ## All rights reserved.
  5. ##
  6. ## WARRANTY DISCLAIMER
  7. ##
  8. ## THESE MATERIALS ARE PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  9. ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10. ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  11. ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS
  12. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. ## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. ## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. ## PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  16. ## OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING
  17. ## NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THESE
  18. ## MATERIALS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. ##
  20. ## Intel Corporation is the author of the Materials, and requests that all
  21. ## problem reports or change requests be submitted to it directly at
  22. ## http://developer.intel.com/opensource.
  23. ##
  24. require "newgetopt.pl";
  25. &NGetOpt('table', 'Wextra', 'Wmissing', 'post_col_prefix=s', 'rows=s', 'columns=s', 'fields=s', 'regexp=s', 'dir=s', 'prefix=s', 'include=s', 'emdb_file=s', 'emdb_path=s', 's_rows=s', 's_columns=s', 'spec_merge' ) ||
  26. die "emdb_cut.pl error - could not get options\n";
  27. ### MK I have added a new flag/option "spec_merge"
  28. ### If users defines it spec_merge of rows and columns is being performed thr following way
  29. ### Files emdb_<row1>.txt
  30. ### emdb_<rown>.txt is added as rows
  31. ### Files <columni>_<rowj>.txt are added as columns
  32. ### Initialize some variables ###
  33. if (defined $opt_post_col_prefix)
  34. {
  35. $post_col_prefix = $opt_post_col_prefix;
  36. }
  37. else
  38. {
  39. $post_col_prefix = "_";
  40. }
  41. if (defined $opt_prefix)
  42. {
  43. $info_member = $opt_prefix . "_EMDB_info";
  44. $num_inst = $opt_prefix . "_num_inst";
  45. $version = $opt_prefix . "_emdb_version";
  46. }
  47. else
  48. {
  49. $info_member = "EMDB_info";
  50. $num_inst = "num_inst";
  51. $version = "emdb_version";
  52. }
  53. $spec_merge = 0;
  54. if (defined ($opt_spec_merge))
  55. {
  56. $spec_merge = 1;
  57. @s_rows = split(/,/,$opt_s_rows);
  58. @s_columns = split(/,/,$opt_s_columns);
  59. }
  60. if ((defined $opt_emdb_file) && $spec_merge)
  61. {
  62. die "emdb_cut.pl error - spec_merge option works only with emdb.txt\n";
  63. }
  64. if (defined $opt_emdb_file)
  65. {
  66. $emdb_file = $opt_emdb_file;
  67. }
  68. else
  69. {
  70. $emdb_file = "emdb.txt";
  71. }
  72. $num_inst =~ tr/[a-z]/[A-Z]/;
  73. $found_names = 0;
  74. $found_types = 0;
  75. $found_prefix = 0;
  76. $lines = 0;
  77. $index = 0;
  78. &Get_Fields_n_Tables();
  79. #####################
  80. ### Read EMDB.txt ###
  81. #####################
  82. open(EMDB_TXT, "$opt_emdb_path/$emdb_file") ||
  83. die "emdb_cut.pl error - can't open $opt_emdb_path/$emdb_file\n";
  84. LINE:
  85. while(<EMDB_TXT>)
  86. {
  87. ### version line ? ###
  88. if (/^\#+\s+\@\(\#\)\s+EMDB\s+\$Revision:\s*(\d+)\.(\d+)/)
  89. {
  90. $EMDB_major_version = $1;
  91. $EMDB_minor_version = $2;
  92. }
  93. ### Remark/empty line ? ###
  94. if (/^\#/ || /^\s*\n/)
  95. {
  96. ### ignore remark/empty line ###
  97. next LINE;
  98. }
  99. ### propriety line (/)
  100. elsif (/^\//)
  101. {
  102. push (@propiety, $_);
  103. }
  104. ### Format line ? ###
  105. elsif (/^&/)
  106. {
  107. if ($found_names != 0)
  108. {
  109. die "Found format ('&') line which is not first\n";
  110. }
  111. $found_names=1;
  112. chop if (/\n$/);
  113. ### Remove the '&' ###
  114. s/^&//;
  115. @col_names = split(/\s+/, $_);
  116. ### Update %col_index which holds the index of each column ###
  117. $num_columns = $#col_names + 1;
  118. for ($i = 0; $i <= $#col_names; $i++)
  119. {
  120. if (defined $col_index{$col_names[$i]})
  121. {
  122. die "emdb_cut.pl error - field name $col_names[$i] is already defined\n";
  123. }
  124. $col_index{$col_names[$i]} = $i;
  125. }
  126. }
  127. ### Types line ? ###
  128. elsif (/^\?/)
  129. {
  130. if ($found_types != 0)
  131. {
  132. die "Found types ('?') line which is not first\n";
  133. }
  134. $found_types=1;
  135. chop if (/\n$/);
  136. ### Remove the '?' ###
  137. s/^\?//;
  138. @type_names = split(/\s+/, $_);
  139. if ($#type_names != $#col_names)
  140. {
  141. die "emdb_cut.pl error - mismatch between field names and type names in $emdb_file\n";
  142. }
  143. ### Update %col_types which holds the type of each column ###
  144. for ($i = 0; $i <= $#col_names; $i++)
  145. {
  146. $col_types{$col_names[$i]} = $type_names[$i];
  147. }
  148. }
  149. ### Prefix line @ ###
  150. elsif (/^@/)
  151. {
  152. if ($found_prefix != 0)
  153. {
  154. die "Found prefix ('\@') line which is not first\n";
  155. }
  156. $found_prefix=1;
  157. chop if (/\n$/);
  158. ### Remove the '@' ###
  159. s/^@//;
  160. @prefix_names = split(/\s+/, $_);
  161. if ($#prefix_names != $#col_names)
  162. {
  163. die "emdb_cut.pl error - mismatch between field names and prefix names in $emdb_file\n";
  164. }
  165. ### Update %col_prefix which holds the prefix of each column ###
  166. for ($i = 0; $i <= $#col_names; $i++)
  167. {
  168. $col_prefix{$col_names[$i]} = $prefix_names[$i];
  169. }
  170. }
  171. else ### data line ###
  172. {
  173. next LINE if (!$found_types || !$found_names);
  174. chop if (/\n$/);
  175. while (/\s$/)
  176. {
  177. chop;
  178. }
  179. if (s/(\S+)/$1/g != $#col_names+1)
  180. {
  181. die "emdb_cut.pl error - number of fields in file $emdb_file in row **$_**\
  182. is wrong\n";
  183. }
  184. ($id, $remainder) = split(/\s+/, $_, 2);
  185. $row_assoc{$id} = $remainder;
  186. $row_index{$id} = $index;
  187. $index++;
  188. }
  189. }
  190. if (!$found_types || !$found_names)
  191. {
  192. die "emdb_cut.pl error - there is no type or field name line in $emdb_file\n";
  193. }
  194. ###############################
  195. ### Read private row tables ###
  196. ###############################
  197. if (spec_merge)
  198. {
  199. @add_rows = ('emdb',@s_rows);
  200. }
  201. # regular mode
  202. foreach $file (@rows)
  203. {
  204. $found_names = 0;
  205. $found_types = 0;
  206. open(ROWS, $file) || die "emdb_cut.pl error - can't open $file\n";
  207. ROW:
  208. while(<ROWS>)
  209. {
  210. ### Remark/empty line ? ###
  211. if (/^\#/ || /^\s*\n/ || /^\?/ || /^@/)
  212. {
  213. ### ignore remark/empty line ###
  214. next ROW;
  215. }
  216. ### Format line ? ###
  217. elsif (/^&/)
  218. {
  219. if ($found_names != 0)
  220. {
  221. die "Found format ('&') line which is not first\n";
  222. }
  223. $found_names=1;
  224. chop if (/\n$/);
  225. ### Remove the '&' ###
  226. s/^&//;
  227. ### Check that the field names match with EMDB.txt ###
  228. (join(' ', @col_names) eq join(' ', split(/\s+/,$_))) ||
  229. die "emdb_cut.pl error - fields in $file don't match with $emdb_file\n";
  230. }
  231. else ### data line ###
  232. {
  233. next ROW if (!$found_names);
  234. chop if (/\n$/);
  235. if (s/(\S+)/$1/g != $#col_names+1)
  236. {
  237. die "emdb_cut.pl error - number of fields in file $file in row **$_**\
  238. is wrong\n";
  239. }
  240. ($id, $remainder) = split(/\s+/, $_, 2);
  241. $row_assoc{$id} = $remainder;
  242. $row_index{$id} = $index;
  243. $index++;
  244. }
  245. }
  246. if (!$found_names)
  247. {
  248. die "emdb_cut.pl error - there is field name line in $file\n";
  249. }
  250. }
  251. # spec_merge mode
  252. foreach $file (@s_rows)
  253. {
  254. $found_names = 0;
  255. $found_types = 0;
  256. $file = $opt_emdb_path . "/" . "emdb_" . $file . ".txt";
  257. open(ROWS, $file) || die "emdb_cut.pl error - can't open $file\n";
  258. ROW:
  259. while(<ROWS>)
  260. {
  261. ### Remark/empty line ? ###
  262. if (/^\#/ || /^\s*\n/ || /^\?/ || /^@/)
  263. {
  264. ### ignore remark/empty line ###
  265. next ROW;
  266. }
  267. ### Format line ? ###
  268. elsif (/^&/)
  269. {
  270. if ($found_names != 0)
  271. {
  272. die "Found format ('&') line which is not first\n";
  273. }
  274. $found_names=1;
  275. chop if (/\n$/);
  276. ### Remove the '&' ###
  277. s/^&//;
  278. ### Check that the field names match with EMDB.txt ###
  279. (join(' ', @col_names) eq join(' ', split(/\s+/,$_))) ||
  280. die "emdb_cut.pl error - fields in $file don't match with $emdb_file\n";
  281. }
  282. else ### data line ###
  283. {
  284. next ROW if (!$found_names);
  285. chop if (/\n$/);
  286. if (s/(\S+)/$1/g != $#col_names+1)
  287. {
  288. die "emdb_cut.pl error - number of fields in file $file in row **$_**\
  289. is wrong\n";
  290. }
  291. ($id, $remainder) = split(/\s+/, $_, 2);
  292. $row_assoc{$id} = $remainder;
  293. $row_index{$id} = $index;
  294. $index++;
  295. }
  296. }
  297. if (!$found_names)
  298. {
  299. die "emdb_cut.pl error - there is field name line in $file\n";
  300. }
  301. }
  302. ### Open Error file: perfix_emdb_err and write missing and
  303. ### extra lines into it
  304. if (defined $opt_prefix)
  305. {
  306. $str_emdb_err = ">$opt_dir/$opt_prefix" . "_emdb_err";
  307. }
  308. else
  309. {
  310. $str_emdb_err = ">$opt_dir/" . "emdb_err";
  311. }
  312. open(EMDB_ERR, "$str_emdb_err") ||
  313. die "emdb_cut.pl error - can't open $opt_prefix" . "_emdb_err\n";
  314. ##################################
  315. ### Read private column tables ###
  316. ##################################
  317. foreach $file (@s_columns)
  318. {
  319. undef %col_assoc;
  320. $warned_once = 0;
  321. ########################
  322. # separate treatment of spec_merge option
  323. #################################
  324. $emdb_column = 1;
  325. foreach $sp_prefix (@add_rows)
  326. {
  327. $column_file = $opt_emdb_path . "/" . (($emdb_column) ? $file . ".txt" : $file . "_" . $sp_prefix . ".txt");
  328. open(COLUMNS, $column_file) || die "emdb_cut.pl error - can't open $column_file\n";
  329. $found_names = 0;
  330. $found_types = 0;
  331. $found_prefix = 0;
  332. COLUMN:
  333. while(<COLUMNS>)
  334. {
  335. ### Remark/empty line ? ###
  336. if (/^\#/ || /^\s*\n/)
  337. {
  338. ### ignore remark/empty line ###
  339. next COLUMN;
  340. }
  341. ### Format line ? ###
  342. elsif (/^&/)
  343. {
  344. if ($found_names != 0)
  345. {
  346. die "Found format ('&') line which is not first\n";
  347. }
  348. $found_names=1;
  349. if ($emdb_column)
  350. {
  351. chop if (/\n$/);
  352. ### Remove the '&' ###
  353. s/^&//;
  354. @col_names_column = split(/\s+/, $_);
  355. ### Update %col_index which holds the index of each column ###
  356. for ($i = 1 ; $i <= $#col_names_column; $i++)
  357. {
  358. if (defined $col_index{$col_names_column[$i]})
  359. {
  360. die "emdb_cut.pl error - field name $col_names_column[$i] is already defined\n";
  361. }
  362. $col_index{$col_names_column[$i]} = $i + $num_columns - 1;
  363. }
  364. $num_columns += $#col_names_column; # not including the first id field
  365. }
  366. else
  367. {
  368. ### Check that the field names match with EMDB.txt ###
  369. chop if (/\n$/);
  370. ### Remove the '&' ###
  371. s/^&//;
  372. (join(' ', @col_names_column) eq join(' ', split(/\s+/,$_))) ||
  373. die "emdb_cut.pl error - fields in $column_file don't match with $file\n";
  374. }
  375. }
  376. ### Types line ? ###
  377. elsif (/^\?/)
  378. {
  379. if ($found_types != 0)
  380. {
  381. die "Found types ('?') line which is not first\n";
  382. }
  383. $found_types=1;
  384. if ($emdb_column)
  385. {
  386. chop if (/\n$/);
  387. ### Remove the '?' ###
  388. s/^\?//;
  389. @type_names_column = split(/\s+/, $_);
  390. if ($#type_names_column != $#col_names_column)
  391. {
  392. die "emdb_cut.pl error - mismatch between field names and type names in $file\n";
  393. }
  394. ### Update %col_types which holds the type of each column ###
  395. for ($i = 1; $i <= $#col_names_column; $i++)
  396. {
  397. $col_types{$col_names_column[$i]} = $type_names_column[$i];
  398. }
  399. }
  400. }
  401. ### Prefix line @ ###
  402. elsif (/^@/)
  403. {
  404. if ($emdb_column)
  405. {
  406. if ($found_prefix != 0)
  407. {
  408. die "Found prefix ('\@') line which is not first\n";
  409. }
  410. $found_prefix = 1;
  411. chop if (/\n$/);
  412. ### Remove the '@' ###
  413. s/^\@//;
  414. @prefix_names_column = split(/\s+/, $_);
  415. if ($#prefix_names_column != $#col_names_column)
  416. {
  417. die "emdb_cut.pl error - mismatch between field names and prefix names in $file\n";
  418. }
  419. ### Update %col_prefix which holds the prefix of each column ###
  420. for ($i = 1; $i <= $#col_names_column; $i++)
  421. {
  422. $col_prefix{$col_names_column[$i]} = $prefix_names_column[$i];
  423. }
  424. }
  425. }
  426. else ### data line ###
  427. {
  428. next COLUMN if (!$found_types || !$found_names);
  429. chop if (/\n$/);
  430. if (s/(\S+)/$1/g != $#col_names_column+1)
  431. {
  432. die "emdb_cut.pl error - number of fields in file $file in row **$_**is wrong\n";
  433. }
  434. ($id, $remainder) = split(/\s+/, $_, 2);
  435. $col_assoc{$id} = $remainder;
  436. }
  437. }
  438. $emdb_column = 0;
  439. if (!$found_types || !$found_names)
  440. {
  441. die "emdb_cut.pl error - there is no type or field name line in $file\n";
  442. }
  443. }
  444. ### completed loop on all files , regarging one "column" spicification
  445. $missing_lines=0;
  446. foreach $key (keys(%row_assoc))
  447. {
  448. if (!defined($col_assoc{$key})) ### missing line info ###
  449. {
  450. $missing_lines++;
  451. if (!$warned_once)
  452. {
  453. $warned_once = 1;
  454. print EMDB_ERR "Missing inst ids in $file\n";
  455. print EMDB_ERR "------------------------------\n";
  456. }
  457. print EMDB_ERR "Missing inst id: $key\n";
  458. delete $row_assoc{$key};
  459. delete $row_index{$key};
  460. }
  461. else
  462. {
  463. $row_assoc{$key} .= ' '.$col_assoc{$key};
  464. }
  465. }
  466. if ($missing_lines > 0)
  467. {
  468. print EMDB_ERR "\nTotal missing lines in $file ",
  469. "= $missing_lines\n\n";
  470. if (defined $opt_wmissing)
  471. {
  472. warn "emdb_cut.pl warning - missing lines in $file\n";
  473. }
  474. else
  475. {
  476. die "emdb_cut.pl error - missing lines in $file\n";
  477. }
  478. }
  479. $num_rows = keys (%row_assoc);
  480. $num_rows_columns = keys (%col_assoc);
  481. if (($num_rows < $num_rows_columns) && defined($opt_wextra))
  482. {
  483. warn "emdb_cut.pl warning - extra rows in $file\n";
  484. print EMDB_ERR "\nExtra inst ids in $file\n";
  485. print EMDB_ERR "-------------------------\n";
  486. foreach $key (keys (%col_assoc))
  487. {
  488. if (!defined($row_assoc{$key}))
  489. {
  490. print EMDB_ERR "Extra inst id: $key\n";
  491. }
  492. }
  493. $total_extra_lines = $num_rows_columns - $num_rows;
  494. print EMDB_ERR "\nTotal extra lines in $file = ",
  495. "$total_extra_lines\n\n";
  496. }
  497. }
  498. # regular mode
  499. foreach $file (@columns)
  500. {
  501. undef %col_assoc;
  502. $warned_once = 0;
  503. open(COLUMNS, $file) || die "emdb_cut.pl error - can't open $file\n";
  504. $found_names = 0;
  505. $found_types = 0;
  506. $found_prefix = 0;
  507. COLUMN:
  508. while(<COLUMNS>)
  509. {
  510. ### Remark/empty line ? ###
  511. if (/^\#/ || /^\s*\n/)
  512. {
  513. ### ignore remark/empty line ###
  514. next COLUMN;
  515. }
  516. ### Format line ? ###
  517. elsif (/^&/)
  518. {
  519. if ($found_names != 0)
  520. {
  521. die "Found format ('&') line which is not first\n";
  522. }
  523. $found_names=1;
  524. chop if (/\n$/);
  525. ### Remove the '&' ###
  526. s/^&//;
  527. @col_names_column = split(/\s+/, $_);
  528. ### Update %col_index which holds the index of each column ###
  529. for ($i = 1 ; $i <= $#col_names_column; $i++)
  530. {
  531. if (defined $col_index{$col_names_column[$i]})
  532. {
  533. die "emdb_cut.pl error - field name $col_names_column[$i] is already defined\n";
  534. }
  535. $col_index{$col_names_column[$i]} = $i + $num_columns - 1;
  536. }
  537. $num_columns += $#col_names_column; # not including the first id field
  538. }
  539. ### Types line ? ###
  540. elsif (/^\?/)
  541. {
  542. if ($found_types != 0)
  543. {
  544. die "Found types ('?') line which is not first\n";
  545. }
  546. $found_types=1;
  547. chop if (/\n$/);
  548. ### Remove the '?' ###
  549. s/^\?//;
  550. @type_names_column = split(/\s+/, $_);
  551. if ($#type_names_column != $#col_names_column)
  552. {
  553. die "emdb_cut.pl error - mismatch between field names and type names in $file\n";
  554. }
  555. ### Update %col_types which holds the type of each column ###
  556. for ($i = 1; $i <= $#col_names_column; $i++)
  557. {
  558. $col_types{$col_names_column[$i]} = $type_names_column[$i];
  559. }
  560. }
  561. ### Prefix line @ ###
  562. elsif (/^@/)
  563. {
  564. if ($found_prefix != 0)
  565. {
  566. die "Found prefix ('\@') line which is not first\n";
  567. }
  568. $found_prefix = 1;
  569. chop if (/\n$/);
  570. ### Remove the '@' ###
  571. s/^\@//;
  572. @prefix_names_column = split(/\s+/, $_);
  573. if ($#prefix_names_column != $#col_names_column)
  574. {
  575. die "emdb_cut.pl error - mismatch between field names and prefix names in $file\n";
  576. }
  577. ### Update %col_prefix which holds the prefix of each column ###
  578. for ($i = 1; $i <= $#col_names_column; $i++)
  579. {
  580. $col_prefix{$col_names_column[$i]} = $prefix_names_column[$i];
  581. }
  582. }
  583. else ### data line ###
  584. {
  585. next COLUMN if (!$found_types || !$found_names);
  586. chop if (/\n$/);
  587. if (s/(\S+)/$1/g != $#col_names_column+1)
  588. {
  589. die "emdb_cut.pl error - number of fields in file $file in row **$_**is wrong\n";
  590. }
  591. ($id, $remainder) = split(/\s+/, $_, 2);
  592. $col_assoc{$id} = $remainder;
  593. }
  594. }
  595. if (!$found_types || !$found_names)
  596. {
  597. die "emdb_cut.pl error - there is no type or field name line in $file\n";
  598. }
  599. $missing_lines=0;
  600. foreach $key (keys(%row_assoc))
  601. {
  602. if (!defined($col_assoc{$key})) ### missing line info ###
  603. {
  604. $missing_lines++;
  605. if (!$warned_once)
  606. {
  607. $warned_once = 1;
  608. print EMDB_ERR "Missing inst ids in $file\n";
  609. print EMDB_ERR "------------------------------\n";
  610. }
  611. print EMDB_ERR "Missing inst id: $key\n";
  612. delete $row_assoc{$key};
  613. delete $row_index{$key};
  614. }
  615. else
  616. {
  617. $row_assoc{$key} .= ' '.$col_assoc{$key};
  618. }
  619. }
  620. if ($missing_lines > 0)
  621. {
  622. print EMDB_ERR "\nTotal missing lines in $file ",
  623. "= $missing_lines\n\n";
  624. if (defined $opt_wmissing)
  625. {
  626. warn "emdb_cut.pl warning - missing lines in $file\n";
  627. }
  628. else
  629. {
  630. die "emdb_cut.pl error - missing lines in $file\n";
  631. }
  632. }
  633. $num_rows = keys (%row_assoc);
  634. $num_rows_columns = keys (%col_assoc);
  635. if (($num_rows < $num_rows_columns) && defined($opt_wextra))
  636. {
  637. warn "emdb_cut.pl warning - extra rows in $file\n";
  638. print EMDB_ERR "\nExtra inst ids in $file\n";
  639. print EMDB_ERR "-------------------------\n";
  640. foreach $key (keys (%col_assoc))
  641. {
  642. if (!defined($row_assoc{$key}))
  643. {
  644. print EMDB_ERR "Extra inst id: $key\n";
  645. }
  646. }
  647. $total_extra_lines = $num_rows_columns - $num_rows;
  648. print EMDB_ERR "\nTotal extra lines in $file = ",
  649. "$total_extra_lines\n\n";
  650. }
  651. }
  652. ### Check if user specified a non-existent field name ###
  653. foreach $field (@fields)
  654. {
  655. if (!defined($col_index{$field}))
  656. {
  657. die "emdb_cut.pl error - field name $field does not exist\n";
  658. }
  659. }
  660. ###################################
  661. ### Create header file - EMDB.h ###
  662. ###################################
  663. if (! defined($opt_table))
  664. {
  665. if (defined $opt_prefix)
  666. {
  667. $str_emdb_h = ">$opt_dir/$opt_prefix" . "_emdb.h";
  668. $include_emdb_h = "$opt_prefix" . "_emdb.h";
  669. }
  670. else
  671. {
  672. $str_emdb_h = ">$opt_dir/" . "emdb.h";
  673. $include_emdb_h = "emdb.h";
  674. }
  675. open(EMDB_H, "$str_emdb_h") ||
  676. die "emdb_cut.pl error - can't open $opt_prefix" . "_emdb.h\n";
  677. select(EMDB_H);
  678. ### print propiety
  679. foreach $line (@propiety)
  680. {
  681. print $line;
  682. }
  683. if (defined $opt_prefix)
  684. {
  685. $tool_prefix = $opt_prefix . "_";
  686. $tool_prefix =~ tr/[a-z]/[A-Z]/;
  687. }
  688. else
  689. {
  690. $tool_prefix = "";
  691. }
  692. print ("\n/***************************************/\n");
  693. print ("/***** EMDB header file *****/\n");
  694. print ("/***************************************/\n");
  695. $ifndef = sprintf("_%sEMDB_H", $tool_prefix);
  696. print ("#ifndef $ifndef\n");
  697. print ("#define $ifndef\n\n");
  698. print ("#include \"EM.h\"\n\n");
  699. print ("#include \"EM_tools.h\"\n\n");
  700. print ("#include \"emdb_types.h\"\n\n");
  701. print ("#include \"inst_ids.h\"\n\n");
  702. printf ("struct %s_s;\n", $info_member);
  703. printf ("typedef struct %s_s %s_t;\n\n", $info_member, $info_member);
  704. printf ("extern %s_t %s[];\n\n", $info_member, $info_member);
  705. if (defined $opt_include)
  706. {
  707. print ("#include \"$opt_include\" \n");
  708. }
  709. }
  710. else
  711. {
  712. if (defined $opt_prefix)
  713. {
  714. $str_emdb_tab = ">$opt_dir/$opt_prefix" . "_emdb.tab";
  715. }
  716. else
  717. {
  718. $str_emdb_tab = ">$opt_dir/" . "emdb.tab";
  719. }
  720. open(EMDB_TAB, "$str_emdb_tab") ||
  721. die "emdb_cut.pl error - can't open $opt_prefix" . "_emdb.tab\n";
  722. select(EMDB_TAB);
  723. }
  724. if (! defined($opt_table))
  725. {
  726. # print ("\ntypedef enum {\n"); ### for Inst_id enumeration ###
  727. #####################
  728. ### Create EMDB.c ###
  729. #####################
  730. if (defined $opt_prefix)
  731. {
  732. $str_emdb_c = ">$opt_dir/$opt_prefix" . "_emdb.c";
  733. }
  734. else
  735. {
  736. $str_emdb_c = ">$opt_dir/" . "emdb.c";
  737. }
  738. open(EMDB_C, "$str_emdb_c") ||
  739. die "emdb_cut.pl error - can't open $opt_prefix" . "_emdb.c\n";
  740. select(EMDB_C);
  741. ### print propiety
  742. foreach $line (@propiety)
  743. {
  744. print $line;
  745. }
  746. print ("\n/***************************************/\n");
  747. print ("/***** EMDB INSTRUCTION INFO *****/\n");
  748. print ("/***************************************/\n");
  749. print ("\n\n#include \"$include_emdb_h\"\n");
  750. print ("\n\nstruct EM_version_s $version = \{$EMDB_major_version, $EMDB_minor_version\};\n");
  751. ### Print definition of info array. ####
  752. printf("\n\n%s_t %s[] = {\n", $info_member,$info_member);
  753. }
  754. ### Check validity of regular expression ###
  755. if (defined $opt_regexp)
  756. {
  757. $found = 0;
  758. foreach $field (@fields)
  759. {
  760. if (($opt_regexp =~ s/$field/\$data_line[\$col_index{'$field'}]/g) > 0)
  761. {
  762. $found = 1;
  763. }
  764. }
  765. if (!$found)
  766. {
  767. die "emdb_cut.pl error - no field name specified in regular expression\n";
  768. }
  769. }
  770. foreach $id (sort {$row_index{$a} <=> $row_index{$b}} keys(%row_index))
  771. {
  772. &Print_Data($id);
  773. }
  774. print "\n";
  775. if (!defined($opt_table))
  776. {
  777. select(EMDB_C);
  778. if (!defined($opt_table))
  779. {
  780. print "};\n";
  781. }
  782. ### Print definition of info array. ###
  783. select(EMDB_H);
  784. print "\n\#ifndef $num_inst\n";
  785. print "\#define $num_inst $lines\n";
  786. print "\#endif\n";
  787. printf("\n\nstruct %s_s {\n",$info_member);
  788. foreach $field (@fields)
  789. {
  790. printf(" %s %s;\n",$col_types{$field},$field);
  791. }
  792. print("};\n\n");
  793. print("#endif /* $ifndef */\n");
  794. }
  795. ###########################
  796. ### Get_Fields_n_Tables ###
  797. ###########################
  798. sub Get_Fields_n_Tables
  799. {
  800. if (defined $opt_fields)
  801. {
  802. $opt_fields =~ s/\s*$//;
  803. if ($opt_fields =~ /,/) ### , is a seperator
  804. {
  805. $opt_fields =~ s/ops/op1,op2,op3,op4,op5,op6/;
  806. @tmp_fields = split(/,\s*/, $opt_fields);
  807. }
  808. else
  809. {
  810. $opt_fields =~ s/ops/op1 op2 op3 op4 op5 op6/;
  811. @tmp_fields = split(/\s+/, $opt_fields);
  812. }
  813. foreach $field (@tmp_fields)
  814. {
  815. if (!defined $assoc_fields{$field})
  816. {
  817. $assoc_fields{$field} = 1;
  818. push (@fields, $field);
  819. }
  820. }
  821. }
  822. else
  823. {
  824. die "emdb_cut.pl error - no fields names defined\n";
  825. }
  826. if (defined $opt_rows)
  827. {
  828. $opt_rows =~ s/\s*$//;
  829. if ($opt_rows =~ /,/) ### , is a seperator
  830. {
  831. @rows = split(/,\s*/, $opt_rows);
  832. }
  833. else
  834. {
  835. @rows = split(/\s+/, $opt_rows);
  836. }
  837. }
  838. if (defined $opt_columns)
  839. {
  840. $opt_columns =~ s/\s*$//;
  841. if ($opt_columns =~ /,/) ### , is a seperator
  842. {
  843. @columns = split(/,\s*/, $opt_columns);
  844. }
  845. else
  846. {
  847. @columns = split(/\s+/, $opt_columns);
  848. }
  849. }
  850. if (!defined($opt_emdb_path))
  851. {
  852. die "emdb_cut.pl error - -emdb_path is not defined\n";
  853. }
  854. if (defined $opt_dir)
  855. {
  856. die "emdb_cut.pl error - no such directory in -dir option\n"
  857. if (!(-d $opt_dir));
  858. }
  859. else
  860. {
  861. die "emdb_cut.pl error - no directory specified\n";
  862. }
  863. }
  864. ###########################
  865. ### Print_Data ###
  866. ###########################
  867. sub Print_Data
  868. {
  869. undef @out;
  870. local($key) = @_;
  871. @data_line = ($key, split(/\s+/, $row_assoc{$key}));
  872. if (defined $opt_regexp) ### Regular expression ###
  873. {
  874. $insert_line = eval ($opt_regexp);
  875. if (length($@) != 0)
  876. {
  877. die "emdb_cut.pl error - $@ syntax or runtime error in regular expression\n";
  878. }
  879. if (!$insert_line)
  880. {
  881. return;
  882. }
  883. }
  884. foreach $field (@fields)
  885. {
  886. if (!defined ($opt_table))
  887. {
  888. @members = split('\;', $data_line[$col_index{$field}]);
  889. @members_prefix = split('\;', $col_prefix{$field});
  890. if ($#members <= 0)
  891. {
  892. $tmp = $data_line[$col_index{$field}];
  893. if ($field eq "mnemonic")
  894. {
  895. $tmp = "\"$tmp\"";
  896. }
  897. ### Translate flags to hex value ###
  898. if ($col_types{$field} eq "Flags_t")
  899. {
  900. &Flags_to_hex($data_line[$col_index{$field}]);
  901. $tmp = $Xflags;
  902. }
  903. if (index($col_prefix{$field}, "-") < 0)
  904. ### field is prefixed ###
  905. {
  906. $tmp = $col_prefix{$field} . "_". $tmp;
  907. }
  908. }
  909. else
  910. {
  911. if ($#members != $#members_prefix)
  912. {
  913. die "emdb_cut.pl error - number of members in structure $data_line[$col_index{$field}] is mismatched with $col_prefix{$field}\n";
  914. }
  915. for ($j = 0; $j <= $#members; $j++)
  916. {
  917. if (index($members_prefix[$j], "-") < 0)
  918. ### field is prefixed ###
  919. {
  920. $members[$j] = $members_prefix[$j] . $post_col_prefix. $members[$j];
  921. }
  922. }
  923. $tmp = "{";
  924. $first_member = 1;
  925. foreach $member (@members)
  926. {
  927. if (!$first_member)
  928. {
  929. $tmp .= ",";
  930. }
  931. $tmp .= $member;
  932. $first_member = 0;
  933. }
  934. $tmp .= "}";
  935. }
  936. push (@out, $tmp);
  937. }
  938. else
  939. {
  940. push (@out,$data_line[$col_index{$field}]);
  941. }
  942. }
  943. if (defined $opt_table)
  944. {
  945. print "\n" unless $lines == 0;
  946. print join(',', @out);
  947. print ",";
  948. }
  949. else
  950. {
  951. select(EMDB_C);
  952. print ",\n" unless $lines == 0;
  953. print " {", join(',', @out), "}";
  954. }
  955. $lines++;
  956. }
  957. ##########################
  958. #
  959. # Flags_to_hex(flags)
  960. #
  961. ###########################
  962. sub Flags_to_hex
  963. {
  964. local($flags) = 0;
  965. local($digit) = 0;
  966. local($flags_bin) = $_[0];
  967. local(@tmp);
  968. @tmp = split(/ */,$flags_bin);
  969. @tmp = reverse(@tmp);
  970. $flags_bin = join('', @tmp);
  971. ### convert to value ###
  972. for($i = 0; $i <= length($flags_bin); $i++)
  973. {
  974. $char = substr($flags_bin, $i, 1);
  975. if ($char eq "0" || $char eq "1")
  976. {
  977. if ($char eq "1")
  978. {
  979. $flags = $flags + (1 << $digit);
  980. }
  981. $digit++;
  982. }
  983. }
  984. ### convert to HEX ###
  985. $Xflags = sprintf("0x%x",$flags);
  986. }