2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-08-25 18:57:02 +03:00
|
|
|
#include <AK/CharacterTypes.h>
|
|
|
|
#include <AK/GenericLexer.h>
|
2020-07-29 02:43:43 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-25 23:09:48 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-04-25 05:42:09 +03:00
|
|
|
#include <stdio.h>
|
2020-02-18 12:47:23 +03:00
|
|
|
#include <unistd.h>
|
2019-04-25 05:42:09 +03:00
|
|
|
|
2021-08-25 18:57:02 +03:00
|
|
|
static u8 parse_octal_number(GenericLexer& lexer)
|
2021-08-17 17:27:29 +03:00
|
|
|
{
|
2021-08-25 18:57:02 +03:00
|
|
|
u32 value = 0;
|
|
|
|
for (size_t count = 0; count < 3; ++count) {
|
|
|
|
auto c = lexer.peek();
|
|
|
|
if (!(c >= '0' && c <= '7'))
|
|
|
|
break;
|
|
|
|
value = value * 8 + (c - '0');
|
|
|
|
lexer.consume();
|
|
|
|
}
|
|
|
|
clamp(value, 0, 255);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Optional<u8> parse_hex_number(GenericLexer& lexer)
|
|
|
|
{
|
|
|
|
u8 value = 0;
|
|
|
|
for (size_t count = 0; count < 2; ++count) {
|
|
|
|
auto c = lexer.peek();
|
|
|
|
if (!is_ascii_hex_digit(c))
|
|
|
|
return {};
|
|
|
|
value = value * 16 + parse_ascii_hex_digit(c);
|
|
|
|
lexer.consume();
|
2021-08-17 17:27:29 +03:00
|
|
|
}
|
2021-08-25 18:57:02 +03:00
|
|
|
return value;
|
2021-08-17 17:27:29 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
static DeprecatedString interpret_backslash_escapes(StringView string, bool& no_trailing_newline)
|
2021-08-17 17:27:29 +03:00
|
|
|
{
|
2021-08-25 18:57:02 +03:00
|
|
|
static constexpr auto escape_map = "a\ab\be\ef\fn\nr\rt\tv\v"sv;
|
|
|
|
static constexpr auto unescaped_chars = "\a\b\e\f\n\r\t\v\\"sv;
|
|
|
|
|
2021-08-17 17:27:29 +03:00
|
|
|
StringBuilder builder;
|
2021-08-25 18:57:02 +03:00
|
|
|
GenericLexer lexer { string };
|
2021-08-17 17:27:29 +03:00
|
|
|
|
2021-08-25 18:57:02 +03:00
|
|
|
while (!lexer.is_eof()) {
|
|
|
|
auto this_index = lexer.tell();
|
|
|
|
auto this_char = lexer.consume();
|
|
|
|
if (this_char == '\\') {
|
|
|
|
if (lexer.is_eof()) {
|
|
|
|
builder.append('\\');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto next_char = lexer.peek();
|
|
|
|
if (next_char == 'c') {
|
|
|
|
no_trailing_newline = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next_char == '0') {
|
|
|
|
lexer.consume();
|
|
|
|
auto octal_number = parse_octal_number(lexer);
|
|
|
|
builder.append(octal_number);
|
|
|
|
} else if (next_char == 'x') {
|
|
|
|
lexer.consume();
|
|
|
|
auto maybe_hex_number = parse_hex_number(lexer);
|
|
|
|
if (!maybe_hex_number.has_value()) {
|
|
|
|
auto bad_substring = string.substring_view(this_index, lexer.tell() - this_index);
|
|
|
|
builder.append(bad_substring);
|
|
|
|
} else {
|
|
|
|
builder.append(maybe_hex_number.release_value());
|
|
|
|
}
|
|
|
|
} else if (next_char == 'u') {
|
|
|
|
lexer.retreat();
|
|
|
|
auto maybe_code_point = lexer.consume_escaped_code_point();
|
|
|
|
if (maybe_code_point.is_error()) {
|
|
|
|
auto bad_substring = string.substring_view(this_index, lexer.tell() - this_index);
|
|
|
|
builder.append(bad_substring);
|
|
|
|
} else {
|
|
|
|
builder.append_code_point(maybe_code_point.release_value());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lexer.retreat();
|
|
|
|
auto consumed_char = lexer.consume_escaped_character('\\', escape_map);
|
|
|
|
if (!unescaped_chars.contains(consumed_char))
|
|
|
|
builder.append('\\');
|
|
|
|
builder.append(consumed_char);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
builder.append(this_char);
|
2021-08-17 17:27:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:58:09 +03:00
|
|
|
return builder.to_deprecated_string();
|
2021-08-17 17:27:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-25 23:09:48 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-04-25 05:42:09 +03:00
|
|
|
{
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-02-18 12:47:23 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
Vector<DeprecatedString> text;
|
2020-07-29 02:43:43 +03:00
|
|
|
bool no_trailing_newline = false;
|
2021-08-17 17:27:29 +03:00
|
|
|
bool should_interpret_backslash_escapes = false;
|
2020-07-29 02:43:43 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(no_trailing_newline, "Do not output a trailing newline", nullptr, 'n');
|
2021-08-17 17:27:29 +03:00
|
|
|
args_parser.add_option(should_interpret_backslash_escapes, "Interpret backslash escapes", nullptr, 'e');
|
2021-08-25 18:57:02 +03:00
|
|
|
args_parser.add_positional_argument(text, "Text to print out", "text", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.set_stop_on_first_non_option(true);
|
2021-11-25 23:09:48 +03:00
|
|
|
args_parser.parse(arguments);
|
2020-07-29 02:43:43 +03:00
|
|
|
|
2021-08-25 18:57:02 +03:00
|
|
|
if (text.is_empty()) {
|
2021-11-01 05:19:12 +03:00
|
|
|
if (!no_trailing_newline)
|
|
|
|
outln();
|
2021-08-25 18:57:02 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
auto output = DeprecatedString::join(' ', text);
|
2021-08-17 17:27:29 +03:00
|
|
|
if (should_interpret_backslash_escapes)
|
2021-08-25 18:57:02 +03:00
|
|
|
output = interpret_backslash_escapes(output, no_trailing_newline);
|
2021-08-17 17:27:29 +03:00
|
|
|
out("{}", output);
|
2020-07-29 02:43:43 +03:00
|
|
|
if (!no_trailing_newline)
|
2021-05-31 17:43:25 +03:00
|
|
|
outln();
|
2019-04-25 05:42:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|