makehelp.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <bdlib/src/String.h>
  2. #include <bdlib/src/Stream.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. int skipline (const char *line, int *skip) {
  8. static int multi = 0;
  9. if ((!strncmp(line, "//", 2))) {
  10. (*skip)++;
  11. } else if ( (strstr(line, "/*")) && (strstr(line, "*/")) ) {
  12. multi = 0;
  13. (*skip)++;
  14. } else if ( (strstr(line, "/*")) ) {
  15. (*skip)++;
  16. multi = 1;
  17. } else if ( (strstr(line, "*/")) ) {
  18. multi = 0;
  19. } else {
  20. if (!multi) (*skip) = 0;
  21. }
  22. return (*skip);
  23. }
  24. #define type(hub, leaf) (hub ? 1 : (leaf ? 2 : 0))
  25. int parse_help(bd::String infile, bd::String outfile) {
  26. bd::Stream in, out;
  27. bd::String buffer, cmd, buf;
  28. size_t pos = 0;
  29. int skip = 0, leaf = 0, hub = 0;
  30. in.loadFile(infile);
  31. printf("Parsing help file '%s'", infile.c_str());
  32. out << buf.printf("/* DO NOT EDIT THIS FILE, EDIT doc/help.txt INSTEAD */\n#ifndef HELP_H\n\
  33. #define HELP_H\n\
  34. #include \"cmds.h\"\n\
  35. \n\
  36. help_t help[] = \n\
  37. { \n");
  38. while (in.tell() < in.length()) {
  39. buffer = in.getline().chomp();
  40. if ((skipline(buffer.c_str(), &skip))) continue;
  41. if (buffer[0] == ':') { /* New cmd */
  42. bd::String ifdef(buffer.length());
  43. int cl = 0, doleaf = 0, dohub = 0;
  44. ++buffer;
  45. ifdef = newsplit(buffer, ':');
  46. if (ifdef.length()) {
  47. if (ifdef == "leaf") {
  48. if (hub) { cl = 1; hub = 0; }
  49. if (!leaf) {
  50. doleaf = leaf = 1;
  51. }
  52. } else if (ifdef == "hub") {
  53. if (leaf) { cl = 1; hub = 0; }
  54. if (!hub) {
  55. dohub = hub = 1;
  56. }
  57. }
  58. } else { if (leaf || hub) { cl = 1; } leaf = 0; hub = 0; }
  59. if (cmd.length()) { /* CLOSE LAST CMD */
  60. if (cmd.find(':') != bd::String::npos) /* garbled */
  61. out << "\")},\n";
  62. else
  63. out << "\"},\n";
  64. cmd.clear();
  65. }
  66. cmd = newsplit(buffer, ':');
  67. if (cmd != "end") { /* NEXT CMD */
  68. printf(".");
  69. if ((pos = cmd.find(':')) != bd::String::npos)
  70. cmd = cmd(0, pos);
  71. out << buf.printf(" {%d, \"%s\", 0, \"", type(dohub, doleaf), cmd.c_str());
  72. } else { /* END */
  73. out << " {0, NULL, 0, NULL}\n};\n";
  74. }
  75. } else { /* CMD HELP INFO */
  76. out << buf.printf("%s\\n", buffer.sub("\"", "\\\"").c_str());
  77. }
  78. }
  79. out << "#endif /* HELP_H */\n";
  80. printf(" Success\n");
  81. out.writeFile(outfile);
  82. return 0;
  83. }
  84. int main(int argc, char **argv) {
  85. if (argc < 3) return 1;
  86. bd::String in(argv[1]), out(argv[2]);
  87. return parse_help(in, out);
  88. }