bstruct.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. BStruct generator
  4. Copyright (C) Ambroz Bizjak, 2010
  5. This file is part of BadVPN.
  6. BadVPN is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License version 2
  8. as published by the Free Software Foundation.
  9. BadVPN is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. require_once "lime/parse_engine.php";
  18. require_once "StructParser.php";
  19. require_once "bstruct_functions.php";
  20. function assert_failure ($script, $line, $message)
  21. {
  22. if ($message == "") {
  23. fatal_error("Assertion failure at {$script}:{$line}");
  24. } else {
  25. fatal_error("Assertion failure at {$script}:{$line}: {$message}");
  26. }
  27. }
  28. assert_options(ASSERT_CALLBACK, "assert_failure");
  29. function print_help ($name)
  30. {
  31. echo <<<EOD
  32. Usage: {$name}
  33. --input-file <file> Message file to generate source for.
  34. --output-dir <dir> Destination directory for generated files.
  35. [--file-prefix <string>] Name prefix for generated files. Default: "struct_".
  36. EOD;
  37. }
  38. $name = "";
  39. $input_file = "";
  40. $output_dir = "";
  41. for ($i = 1; $i < $argc;) {
  42. $arg = $argv[$i++];
  43. switch ($arg) {
  44. case "--name":
  45. $name = $argv[$i++];
  46. break;
  47. case "--input-file":
  48. $input_file = $argv[$i++];
  49. break;
  50. case "--output-dir":
  51. $output_dir = $argv[$i++];
  52. break;
  53. case "--help":
  54. print_help($argv[0]);
  55. exit(0);
  56. default:
  57. fatal_error("Unknown option: {$arg}");
  58. }
  59. }
  60. if ($name == "") {
  61. fatal_error("--name missing");
  62. }
  63. if ($input_file == "") {
  64. fatal_error("--input-file missing");
  65. }
  66. if ($output_dir == "") {
  67. fatal_error("--output-dir missing");
  68. }
  69. if (($data = file_get_contents($input_file)) === FALSE) {
  70. fatal_error("Failed to read input file");
  71. }
  72. if (!tokenize($data, $tokens)) {
  73. fatal_error("Failed to tokenize");
  74. }
  75. $parser = new parse_engine(new StructParser());
  76. try {
  77. foreach ($tokens as $token) {
  78. $parser->eat($token[0], $token[1]);
  79. }
  80. $parser->eat_eof();
  81. } catch (parse_error $e) {
  82. fatal_error("$input_file: Parse error: ".$e->getMessage());
  83. }
  84. $data = generate_header($name, $parser->semantic["directives"], $parser->semantic["structures"]);
  85. if (file_put_contents("{$output_dir}/{$name}.h", $data) === NULL) {
  86. fatal_error("{$input_file}: Failed to write .h file");
  87. }