BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 <?php
2  
3 require_once "blog_functions.php";
4  
5 function assert_failure ($script, $line, $message)
6 {
7 if ($message == "") {
8 fatal_error("Assertion failure at {$script}:{$line}");
9 } else {
10 fatal_error("Assertion failure at {$script}:{$line}: {$message}");
11 }
12 }
13  
14 assert_options(ASSERT_CALLBACK, "assert_failure");
15  
16 function print_help ($name)
17 {
18 echo <<<EOD
19 Usage: {$name}
20 --input-file <file> Input channels file.
21 --output-dir <dir> Destination directory for generated files.
22  
23 EOD;
24 }
25  
26 $input_file = "";
27 $output_dir = "";
28  
29 for ($i = 1; $i < $argc;) {
30 $arg = $argv[$i++];
31 switch ($arg) {
32 case "--input-file":
33 $input_file = $argv[$i++];
34 break;
35 case "--output-dir":
36 $output_dir = $argv[$i++];
37 break;
38 case "--help":
39 print_help($argv[0]);
40 exit(0);
41 default:
42 fatal_error("Unknown option: {$arg}");
43 }
44 }
45  
46 if ($input_file == "") {
47 fatal_error("--input-file missing");
48 }
49  
50 if ($output_dir == "") {
51 fatal_error("--output-dir missing");
52 }
53  
54 if (($data = file_get_contents($input_file)) === FALSE) {
55 fatal_error("Failed to read input file");
56 }
57  
58 if (!tokenize($data, $tokens)) {
59 fatal_error("Failed to tokenize");
60 }
61  
62 $i = 0;
63 $channels_defines = "";
64 $channels_list = "";
65  
66 reset($tokens);
67  
68 while (1) {
69 if (($ch_name = current($tokens)) === FALSE) {
70 break;
71 }
72 next($tokens);
73 if (($ch_priority = current($tokens)) === FALSE) {
74 fatal_error("missing priority");
75 }
76 next($tokens);
77 if ($ch_name[0] != "name") {
78 fatal_error("name is not a name");
79 }
80 if ($ch_priority[0] != "number") {
81 fatal_error("priority is not a number");
82 }
83  
84 $channel_file = <<<EOD
85 #ifdef BLOG_CURRENT_CHANNEL
86 #undef BLOG_CURRENT_CHANNEL
87 #endif
88 #define BLOG_CURRENT_CHANNEL BLOG_CHANNEL_{$ch_name[1]}
89  
90 EOD;
91  
92 $channels_defines .= <<<EOD
93 #define BLOG_CHANNEL_{$ch_name[1]} {$i}
94  
95 EOD;
96  
97 $channels_list .= <<<EOD
98 {"{$ch_name[1]}", {$ch_priority[1]}},
99  
100 EOD;
101  
102 if (file_put_contents("{$output_dir}/blog_channel_{$ch_name[1]}.h", $channel_file) === NULL) {
103 fatal_error("{$input_file}: Failed to write channel file");
104 }
105  
106 $i++;
107 }
108  
109 $channels_defines .= <<<EOD
110 #define BLOG_NUM_CHANNELS {$i}
111  
112 EOD;
113  
114 if (file_put_contents("{$output_dir}/blog_channels_defines.h", $channels_defines) === NULL) {
115 fatal_error("{$input_file}: Failed to write channels defines file");
116 }
117  
118 if (file_put_contents("{$output_dir}/blog_channels_list.h", $channels_list) === NULL) {
119 fatal_error("{$input_file}: Failed to write channels list file");
120 }
121