man.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/ByteBuffer.h>
  27. #include <AK/String.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <LibMarkdown/Document.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. int main(int argc, char* argv[])
  34. {
  35. if (pledge("stdio rpath", nullptr) < 0) {
  36. perror("pledge");
  37. return 1;
  38. }
  39. if (unveil("/usr/share/man", "r") < 0) {
  40. perror("unveil");
  41. return 1;
  42. }
  43. unveil(nullptr, nullptr);
  44. const char* section = nullptr;
  45. const char* name = nullptr;
  46. Core::ArgsParser args_parser;
  47. args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No);
  48. args_parser.add_positional_argument(name, "Name of the man page", "name");
  49. args_parser.parse(argc, argv);
  50. auto make_path = [name](const char* section) {
  51. return String::format("/usr/share/man/man%s/%s.md", section, name);
  52. };
  53. if (!section) {
  54. const char* sections[] = {
  55. "1",
  56. "2",
  57. "3",
  58. "4",
  59. "5",
  60. "6",
  61. "7",
  62. "8"
  63. };
  64. for (auto s : sections) {
  65. String path = make_path(s);
  66. if (access(path.characters(), R_OK) == 0) {
  67. section = s;
  68. break;
  69. }
  70. }
  71. if (!section) {
  72. fprintf(stderr, "No man page for %s\n", name);
  73. exit(1);
  74. }
  75. }
  76. auto file = Core::File::construct();
  77. file->set_filename(make_path(section));
  78. if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
  79. perror("Failed to open man page file");
  80. exit(1);
  81. }
  82. if (pledge("stdio", nullptr) < 0) {
  83. perror("pledge");
  84. return 1;
  85. }
  86. dbg() << "Loading man page from " << file->filename();
  87. auto buffer = file->read_all();
  88. auto source = String::copy(buffer);
  89. printf("%s(%s)\t\tSerenityOS manual\n", name, section);
  90. auto document = Markdown::Document::parse(source);
  91. ASSERT(document);
  92. String rendered = document->render_for_terminal();
  93. printf("%s", rendered.characters());
  94. }