man.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <sys/ioctl.h>
  33. #include <unistd.h>
  34. int main(int argc, char* argv[])
  35. {
  36. int view_width = 0;
  37. if (isatty(STDOUT_FILENO)) {
  38. struct winsize ws;
  39. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
  40. view_width = ws.ws_col;
  41. }
  42. if (view_width == 0)
  43. view_width = 80;
  44. if (pledge("stdio rpath", nullptr) < 0) {
  45. perror("pledge");
  46. return 1;
  47. }
  48. if (unveil("/usr/share/man", "r") < 0) {
  49. perror("unveil");
  50. return 1;
  51. }
  52. unveil(nullptr, nullptr);
  53. const char* section = nullptr;
  54. const char* name = nullptr;
  55. Core::ArgsParser args_parser;
  56. args_parser.set_general_help("Read manual pages. Try 'man man' to get started.");
  57. args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No);
  58. args_parser.add_positional_argument(name, "Name of the man page", "name");
  59. args_parser.parse(argc, argv);
  60. auto make_path = [name](const char* section) {
  61. return String::formatted("/usr/share/man/man{}/{}.md", section, name);
  62. };
  63. if (!section) {
  64. const char* sections[] = {
  65. "1",
  66. "2",
  67. "3",
  68. "4",
  69. "5",
  70. "6",
  71. "7",
  72. "8"
  73. };
  74. for (auto s : sections) {
  75. String path = make_path(s);
  76. if (access(path.characters(), R_OK) == 0) {
  77. section = s;
  78. break;
  79. }
  80. }
  81. if (!section) {
  82. fprintf(stderr, "No man page for %s\n", name);
  83. exit(1);
  84. }
  85. }
  86. auto file = Core::File::construct();
  87. file->set_filename(make_path(section));
  88. if (!file->open(Core::IODevice::OpenMode::ReadOnly)) {
  89. perror("Failed to open man page file");
  90. exit(1);
  91. }
  92. if (pledge("stdio", nullptr) < 0) {
  93. perror("pledge");
  94. return 1;
  95. }
  96. dbgln("Loading man page from {}", file->filename());
  97. auto buffer = file->read_all();
  98. auto source = String::copy(buffer);
  99. printf("%s(%s)\t\tSerenityOS manual\n", name, section);
  100. auto document = Markdown::Document::parse(source);
  101. ASSERT(document);
  102. String rendered = document->render_for_terminal(view_width);
  103. printf("%s", rendered.characters());
  104. }