blog.php 2.6 KB

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