BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 <?php
2 /**
3 * @file bproto.php
4 * @author Ambroz Bizjak <ambrop7@gmail.com>
5 *
6 * @section LICENSE
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30  
31 require_once "lime/parse_engine.php";
32 require_once "ProtoParser.php";
33 require_once "bproto_functions.php";
34  
35 function assert_failure ($script, $line, $message)
36 {
37 if ($message == "") {
38 fatal_error("Assertion failure at {$script}:{$line}");
39 } else {
40 fatal_error("Assertion failure at {$script}:{$line}: {$message}");
41 }
42 }
43  
44 assert_options(ASSERT_CALLBACK, "assert_failure");
45  
46 function print_help ($name)
47 {
48 echo <<<EOD
49 Usage: {$name}
50 --name <string> Output file prefix.
51 --input-file <file> Message file to generate source for.
52 --output-dir <dir> Destination directory for generated files.
53  
54 EOD;
55 }
56  
57 $name = "";
58 $input_file = "";
59 $output_dir = "";
60  
61 for ($i = 1; $i < $argc;) {
62 $arg = $argv[$i++];
63 switch ($arg) {
64 case "--name":
65 $name = $argv[$i++];
66 break;
67 case "--input-file":
68 $input_file = $argv[$i++];
69 break;
70 case "--output-dir":
71 $output_dir = $argv[$i++];
72 break;
73 case "--help":
74 print_help($argv[0]);
75 exit(0);
76 default:
77 fatal_error("Unknown option: {$arg}");
78 }
79 }
80  
81 if ($name == "") {
82 fatal_error("--name missing");
83 }
84  
85 if ($input_file == "") {
86 fatal_error("--input-file missing");
87 }
88  
89 if ($output_dir == "") {
90 fatal_error("--output-dir missing");
91 }
92  
93 if (($data = file_get_contents($input_file)) === FALSE) {
94 fatal_error("Failed to read input file");
95 }
96  
97 if (!tokenize($data, $tokens)) {
98 fatal_error("Failed to tokenize");
99 }
100  
101 $parser = new parse_engine(new ProtoParser());
102  
103 try {
104 foreach ($tokens as $token) {
105 $parser->eat($token[0], $token[1]);
106 }
107 $parser->eat_eof();
108 } catch (parse_error $e) {
109 fatal_error("$input_file: Parse error: ".$e->getMessage());
110 }
111  
112 $data = generate_header($name, $parser->semantic["directives"], $parser->semantic["messages"]);
113 if (file_put_contents("{$output_dir}/{$name}.h", $data) === NULL) {
114 fatal_error("{$input_file}: Failed to write .h file");
115 }