man.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/String.h>
  27. #include <LibCore/CFile.h>
  28. #include <LibMarkdown/MDDocument.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. int main(int argc, char* argv[])
  32. {
  33. if (pledge("stdio rpath", nullptr) < 0) {
  34. perror("pledge");
  35. return 1;
  36. }
  37. if (unveil("/usr/share/man", "r") < 0) {
  38. perror("unveil");
  39. return 1;
  40. }
  41. unveil(nullptr, nullptr);
  42. String name;
  43. String section;
  44. if (argc < 2 || argc > 3) {
  45. fprintf(stderr, "Usage:\t%s <name>\n\t%s <section <name>\n", argv[0], argv[0]);
  46. exit(1);
  47. }
  48. if (argc == 2) {
  49. name = argv[1];
  50. } else {
  51. section = argv[1];
  52. name = argv[2];
  53. }
  54. auto make_path = [&](String s) {
  55. return String::format("/usr/share/man/man%s/%s.md", s.characters(), name.characters());
  56. };
  57. if (section.is_null()) {
  58. String sections[] = {
  59. "1",
  60. "2",
  61. "3",
  62. "4",
  63. "5",
  64. "6",
  65. "7",
  66. "8"
  67. };
  68. for (auto& s : sections) {
  69. String path = make_path(s);
  70. if (access(path.characters(), R_OK) == 0) {
  71. section = s;
  72. break;
  73. }
  74. }
  75. if (section.is_null()) {
  76. fprintf(stderr, "No man page for %s\n", name.characters());
  77. exit(1);
  78. }
  79. }
  80. auto file = CFile::construct();
  81. file->set_filename(make_path(section));
  82. if (!file->open(CIODevice::OpenMode::ReadOnly)) {
  83. perror("Failed to open man page file");
  84. exit(1);
  85. }
  86. if (pledge("stdio", nullptr) < 0) {
  87. perror("pledge");
  88. return 1;
  89. }
  90. dbg() << "Loading man page from " << file->filename();
  91. auto buffer = file->read_all();
  92. String source { (const char*)buffer.data(), (size_t)buffer.size() };
  93. printf("%s(%s)\t\tSerenity manual\n", name.characters(), section.characters());
  94. MDDocument document;
  95. bool success = document.parse(source);
  96. ASSERT(success);
  97. String rendered = document.render_for_terminal();
  98. printf("%s", rendered.characters());
  99. }