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