Serenity Operating System
at portability 134 lines 4.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@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/JsonArray.h> 28#include <AK/JsonObject.h> 29#include <AK/JsonValue.h> 30#include <AK/StringBuilder.h> 31#include <LibCore/File.h> 32#include <stdio.h> 33 34static bool use_color = false; 35static void print(const String& name, const JsonValue&, Vector<String>& trail); 36 37static const char* color_name = ""; 38static const char* color_index = ""; 39static const char* color_brace = ""; 40static const char* color_bool = ""; 41static const char* color_null = ""; 42static const char* color_string = ""; 43static const char* color_off = ""; 44 45int main(int argc, char** argv) 46{ 47 if (pledge("stdio tty rpath", nullptr) < 0) { 48 perror("pledge"); 49 return 1; 50 } 51 52 if (isatty(STDOUT_FILENO)) 53 use_color = true; 54 55 if (pledge("stdio rpath", nullptr) < 0) { 56 perror("pledge"); 57 return 1; 58 } 59 60 if (argc != 2) { 61 fprintf(stderr, "usage: gron <file>\n"); 62 return 0; 63 } 64 auto file = Core::File::construct(argv[1]); 65 if (!file->open(Core::IODevice::ReadOnly)) { 66 fprintf(stderr, "Couldn't open %s for reading: %s\n", argv[1], file->error_string()); 67 return 1; 68 } 69 70 if (pledge("stdio", nullptr) < 0) { 71 perror("pledge"); 72 return 1; 73 } 74 75 auto file_contents = file->read_all(); 76 auto json = JsonValue::from_string(file_contents); 77 78 if (use_color) { 79 color_name = "\033[33;1m"; 80 color_index = "\033[35;1m"; 81 color_brace = "\033[36m"; 82 color_bool = "\033[32;1m"; 83 color_string = "\033[31;1m"; 84 color_null = "\033[34;1m"; 85 color_off = "\033[0m"; 86 } 87 88 Vector<String> trail; 89 print("json", json, trail); 90 return 0; 91} 92 93static void print(const String& name, const JsonValue& value, Vector<String>& trail) 94{ 95 for (size_t i = 0; i < trail.size(); ++i) 96 printf("%s", trail[i].characters()); 97 98 printf("%s%s%s = ", color_name, name.characters(), color_off); 99 100 if (value.is_object()) { 101 printf("%s{}%s;\n", color_brace, color_off); 102 trail.append(String::format("%s%s%s.", color_name, name.characters(), color_off)); 103 value.as_object().for_each_member([&](auto& on, auto& ov) { print(on, ov, trail); }); 104 trail.take_last(); 105 return; 106 } 107 if (value.is_array()) { 108 printf("%s[]%s;\n", color_brace, color_off); 109 trail.append(String::format("%s%s%s", color_name, name.characters(), color_off)); 110 for (int i = 0; i < value.as_array().size(); ++i) { 111 auto element_name = String::format("%s%s[%s%s%d%s%s]%s", color_off, color_brace, color_off, color_index, i, color_off, color_brace, color_off); 112 print(element_name, value.as_array()[i], trail); 113 } 114 trail.take_last(); 115 return; 116 } 117 switch (value.type()) { 118 case JsonValue::Type::Null: 119 case JsonValue::Type::Undefined: 120 printf("%s", color_null); 121 break; 122 case JsonValue::Type::Bool: 123 printf("%s", color_bool); 124 break; 125 case JsonValue::Type::String: 126 printf("%s", color_string); 127 break; 128 default: 129 printf("%s", color_index); 130 break; 131 } 132 133 printf("%s%s;\n", value.serialized<StringBuilder>().characters(), color_off); 134}