2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-03-10 17:23:06 +00:00
|
|
|
* Copyright (c) 2019-2020, Marios Prokopakis <mariosprokopakis@gmail.com>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-18 15:04:05 +00:00
|
|
|
#include <AK/QuickSort.h>
|
2020-03-02 12:08:23 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-01-04 21:32:11 +00:00
|
|
|
#include <AK/String.h>
|
2019-08-18 15:04:05 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-11-09 00:54:31 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2019-08-18 15:04:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
struct Range {
|
|
|
|
size_t m_from { 1 };
|
|
|
|
size_t m_to { SIZE_MAX };
|
2020-03-02 12:08:23 +00:00
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
[[nodiscard]] bool intersects(const Range& other) const
|
|
|
|
{
|
2020-03-02 12:08:23 +00:00
|
|
|
return !(other.m_from > m_to || other.m_to < m_from);
|
|
|
|
}
|
2019-08-18 15:04:05 +00:00
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
void merge(const Range& other)
|
|
|
|
{
|
|
|
|
// Can't merge two ranges that are disjoint.
|
|
|
|
VERIFY(intersects(other));
|
2020-03-02 12:08:23 +00:00
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
m_from = min(m_from, other.m_from);
|
|
|
|
m_to = max(m_to, other.m_to);
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
2021-11-09 02:07:42 +00:00
|
|
|
};
|
2019-08-18 15:04:05 +00:00
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
static bool expand_list(String& list, Vector<Range>& ranges)
|
2019-08-18 15:04:05 +00:00
|
|
|
{
|
2021-11-09 01:06:38 +00:00
|
|
|
Vector<String> tokens = list.split(',');
|
|
|
|
|
2019-08-18 15:04:05 +00:00
|
|
|
for (auto& token : tokens) {
|
|
|
|
if (token.length() == 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: byte/character positions are numbered from 1");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token == "-") {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid range with no endpoint: {}", token);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token[0] == '-') {
|
2021-11-09 02:07:42 +00:00
|
|
|
auto index = token.substring(1, token.length() - 1).to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!index.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte/character position '{}'", token);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
if (index.value() == 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: byte/character positions are numbered from 1");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
ranges.append({ 1, index.value() });
|
2019-08-18 15:04:05 +00:00
|
|
|
} else if (token[token.length() - 1] == '-') {
|
2021-11-09 02:07:42 +00:00
|
|
|
auto index = token.substring(0, token.length() - 1).to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!index.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte/character position '{}'", token);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
if (index.value() == 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: byte/character positions are numbered from 1");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
2021-11-09 02:07:42 +00:00
|
|
|
|
|
|
|
ranges.append({ index.value(), SIZE_MAX });
|
2019-08-18 15:04:05 +00:00
|
|
|
} else {
|
|
|
|
auto range = token.split('-');
|
|
|
|
if (range.size() == 2) {
|
2021-11-09 02:07:42 +00:00
|
|
|
auto index1 = range[0].to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!index1.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte/character position '{}'", range[0]);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
auto index2 = range[1].to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!index2.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte/character position '{}'", range[1]);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
if (index1.value() > index2.value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid decreasing range");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2020-06-12 19:07:52 +00:00
|
|
|
} else if (index1.value() == 0 || index2.value() == 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: byte/character positions are numbered from 1");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
ranges.append({ index1.value(), index2.value() });
|
2019-08-18 15:04:05 +00:00
|
|
|
} else if (range.size() == 1) {
|
2021-11-09 02:07:42 +00:00
|
|
|
auto index = range[0].to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!index.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte/character position '{}'", range[0]);
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
if (index.value() == 0) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: byte/character positions are numbered from 1");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
ranges.append({ index.value(), index.value() });
|
2019-08-18 15:04:05 +00:00
|
|
|
} else {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: invalid byte or character range");
|
2021-11-09 00:54:31 +00:00
|
|
|
return false;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 00:54:31 +00:00
|
|
|
|
|
|
|
return true;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:16:09 +00:00
|
|
|
static void cut_file(const String& file, const Vector<Range>& ranges, void (*process_line)(char*, size_t, const Vector<Range>&))
|
2020-01-04 21:32:11 +00:00
|
|
|
{
|
2020-07-03 00:59:25 +00:00
|
|
|
FILE* fp = stdin;
|
|
|
|
if (!file.is_null()) {
|
|
|
|
fp = fopen(file.characters(), "r");
|
|
|
|
if (!fp) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("cut: Could not open file '{}'", file);
|
2020-07-03 00:59:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-04 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* line = nullptr;
|
|
|
|
ssize_t line_length = 0;
|
|
|
|
size_t line_capacity = 0;
|
|
|
|
while ((line_length = getline(&line, &line_capacity, fp)) != -1) {
|
2021-11-09 02:07:42 +00:00
|
|
|
if (line_length < 0) {
|
|
|
|
warnln("cut: Failed to read line from file '{}'", file);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-04 21:32:11 +00:00
|
|
|
line[line_length - 1] = '\0';
|
|
|
|
line_length--;
|
2021-11-09 02:07:42 +00:00
|
|
|
|
2021-11-09 02:16:09 +00:00
|
|
|
process_line(line, line_length, ranges);
|
2020-01-04 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
free(line);
|
2020-07-03 00:59:25 +00:00
|
|
|
|
|
|
|
if (!file.is_null())
|
|
|
|
fclose(fp);
|
2020-01-04 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:16:09 +00:00
|
|
|
static void process_line_bytes(char* line, size_t length, const Vector<Range>& ranges)
|
|
|
|
{
|
|
|
|
for (auto& i : ranges) {
|
|
|
|
if (i.m_from >= length)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
auto to = min(i.m_to, length);
|
|
|
|
auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1);
|
|
|
|
out("{}", sub_string);
|
|
|
|
}
|
|
|
|
outln();
|
|
|
|
}
|
|
|
|
|
2019-08-18 15:04:05 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
String byte_list = "";
|
2021-11-09 01:06:38 +00:00
|
|
|
|
2020-01-04 21:32:11 +00:00
|
|
|
Vector<String> files;
|
2019-08-18 15:04:05 +00:00
|
|
|
|
2021-11-09 00:54:31 +00:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(files, "file(s) to cut", "file", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.add_option(byte_list, "select only these bytes", "bytes", 'b', "list");
|
|
|
|
args_parser.parse(argc, argv);
|
2019-08-18 15:04:05 +00:00
|
|
|
|
2021-11-09 00:54:31 +00:00
|
|
|
if (byte_list == "") {
|
|
|
|
warnln("cut: you must specify a list of bytes");
|
|
|
|
args_parser.print_usage(stderr, argv[0]);
|
|
|
|
return 1;
|
2019-08-18 15:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
Vector<Range> byte_vector;
|
2021-11-09 01:06:38 +00:00
|
|
|
auto expansion_successful = expand_list(byte_list, byte_vector);
|
2021-11-09 00:54:31 +00:00
|
|
|
|
|
|
|
if (!expansion_successful) {
|
|
|
|
args_parser.print_usage(stderr, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:01:37 +00:00
|
|
|
quick_sort(byte_vector, [](auto& a, auto& b) { return a.m_from < b.m_from; });
|
2020-07-03 00:59:25 +00:00
|
|
|
|
2021-11-09 02:07:42 +00:00
|
|
|
Vector<Range> disjoint_ranges;
|
|
|
|
for (auto& range : byte_vector) {
|
|
|
|
if (disjoint_ranges.is_empty()) {
|
|
|
|
disjoint_ranges.append(range);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Range& last_range = disjoint_ranges.last();
|
|
|
|
|
|
|
|
if (!last_range.intersects(range)) {
|
|
|
|
disjoint_ranges.append(range);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_range.merge(range);
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:59:25 +00:00
|
|
|
if (files.is_empty())
|
|
|
|
files.append(String());
|
|
|
|
|
2020-01-04 21:32:11 +00:00
|
|
|
/* Process each file */
|
2020-03-02 12:08:23 +00:00
|
|
|
for (auto& file : files)
|
2021-11-09 02:16:09 +00:00
|
|
|
cut_file(file, disjoint_ranges, process_line_bytes);
|
2019-08-18 15:04:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|