2020-06-24 06:57:00 +03:00
|
|
|
/*
|
2022-03-30 09:49:43 +03:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2020-06-24 06:57:00 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-24 06:57:00 +03:00
|
|
|
*/
|
|
|
|
|
2021-05-15 13:34:40 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-06-24 06:57:00 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
2022-03-30 09:49:43 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-06-24 06:57:00 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
bool g_there_was_an_error = false;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
[[noreturn, gnu::format(printf, 1, 2)]] static void fatal_error(char const* format, ...)
|
2020-06-24 06:57:00 +03:00
|
|
|
{
|
|
|
|
fputs("\033[31m", stderr);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fputs("\033[0m\n", stderr);
|
|
|
|
exit(126);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Condition {
|
|
|
|
public:
|
2022-03-24 06:18:10 +03:00
|
|
|
virtual ~Condition() = default;
|
2020-06-24 06:57:00 +03:00
|
|
|
virtual bool check() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class And : public Condition {
|
|
|
|
public:
|
|
|
|
And(NonnullOwnPtr<Condition> lhs, NonnullOwnPtr<Condition> rhs)
|
|
|
|
: m_lhs(move(lhs))
|
|
|
|
, m_rhs(move(rhs))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
return m_lhs->check() && m_rhs->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullOwnPtr<Condition> m_lhs;
|
|
|
|
NonnullOwnPtr<Condition> m_rhs;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Or : public Condition {
|
|
|
|
public:
|
|
|
|
Or(NonnullOwnPtr<Condition> lhs, NonnullOwnPtr<Condition> rhs)
|
|
|
|
: m_lhs(move(lhs))
|
|
|
|
, m_rhs(move(rhs))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
return m_lhs->check() || m_rhs->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullOwnPtr<Condition> m_lhs;
|
|
|
|
NonnullOwnPtr<Condition> m_rhs;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Not : public Condition {
|
|
|
|
public:
|
|
|
|
Not(NonnullOwnPtr<Condition> cond)
|
|
|
|
: m_cond(move(cond))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
return !m_cond->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullOwnPtr<Condition> m_cond;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileIsOfKind : public Condition {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
BlockDevice,
|
|
|
|
CharacterDevice,
|
|
|
|
Directory,
|
|
|
|
FIFO,
|
|
|
|
Regular,
|
|
|
|
Socket,
|
|
|
|
SymbolicLink,
|
|
|
|
};
|
|
|
|
FileIsOfKind(StringView path, Kind kind)
|
|
|
|
: m_path(path)
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (m_kind == SymbolicLink)
|
|
|
|
rc = stat(m_path.characters(), &statbuf);
|
|
|
|
else
|
|
|
|
rc = lstat(m_path.characters(), &statbuf);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
perror(m_path.characters());
|
|
|
|
g_there_was_an_error = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_kind) {
|
|
|
|
case BlockDevice:
|
|
|
|
return S_ISBLK(statbuf.st_mode);
|
|
|
|
case CharacterDevice:
|
|
|
|
return S_ISCHR(statbuf.st_mode);
|
|
|
|
case Directory:
|
|
|
|
return S_ISDIR(statbuf.st_mode);
|
|
|
|
case FIFO:
|
|
|
|
return S_ISFIFO(statbuf.st_mode);
|
|
|
|
case Regular:
|
|
|
|
return S_ISREG(statbuf.st_mode);
|
|
|
|
case Socket:
|
|
|
|
return S_ISSOCK(statbuf.st_mode);
|
|
|
|
case SymbolicLink:
|
|
|
|
return S_ISLNK(statbuf.st_mode);
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_path;
|
2020-06-24 06:57:00 +03:00
|
|
|
Kind m_kind { Regular };
|
|
|
|
};
|
|
|
|
|
|
|
|
class UserHasPermission : public Condition {
|
|
|
|
public:
|
|
|
|
enum Permission {
|
|
|
|
Any,
|
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
Execute,
|
|
|
|
};
|
|
|
|
UserHasPermission(StringView path, Permission kind)
|
|
|
|
: m_path(path)
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
switch (m_kind) {
|
|
|
|
case Read:
|
|
|
|
return access(m_path.characters(), R_OK) == 0;
|
|
|
|
case Write:
|
|
|
|
return access(m_path.characters(), W_OK) == 0;
|
|
|
|
case Execute:
|
|
|
|
return access(m_path.characters(), X_OK) == 0;
|
|
|
|
case Any:
|
|
|
|
return access(m_path.characters(), F_OK) == 0;
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_path;
|
2020-06-24 06:57:00 +03:00
|
|
|
Permission m_kind { Read };
|
|
|
|
};
|
|
|
|
|
2022-09-30 21:11:49 +03:00
|
|
|
class FileHasFlag : public Condition {
|
|
|
|
public:
|
|
|
|
enum Flag {
|
|
|
|
SGID,
|
|
|
|
SUID,
|
|
|
|
SVTX,
|
|
|
|
};
|
|
|
|
FileHasFlag(StringView path, Flag kind)
|
|
|
|
: m_path(path)
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
int rc = stat(m_path.characters(), &statbuf);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
perror(m_path.characters());
|
|
|
|
g_there_was_an_error = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_kind) {
|
|
|
|
case SGID:
|
|
|
|
return statbuf.st_mode & S_ISGID;
|
|
|
|
case SUID:
|
|
|
|
return statbuf.st_mode & S_ISUID;
|
|
|
|
case SVTX:
|
|
|
|
return statbuf.st_mode & S_ISVTX;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_path;
|
2022-09-30 21:11:49 +03:00
|
|
|
Flag m_kind { SGID };
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileIsOwnedBy : public Condition {
|
|
|
|
public:
|
|
|
|
enum Owner {
|
|
|
|
EffectiveGID,
|
|
|
|
EffectiveUID,
|
|
|
|
};
|
|
|
|
FileIsOwnedBy(StringView path, Owner kind)
|
|
|
|
: m_path(path)
|
|
|
|
, m_kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
int rc = stat(m_path.characters(), &statbuf);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
perror(m_path.characters());
|
|
|
|
g_there_was_an_error = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_kind) {
|
|
|
|
case EffectiveGID:
|
|
|
|
return statbuf.st_gid == getgid();
|
|
|
|
case EffectiveUID:
|
|
|
|
return statbuf.st_uid == getuid();
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_path;
|
2022-09-30 21:11:49 +03:00
|
|
|
Owner m_kind { EffectiveGID };
|
|
|
|
};
|
|
|
|
|
2020-06-24 06:57:00 +03:00
|
|
|
class StringCompare : public Condition {
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
Equal,
|
|
|
|
NotEqual,
|
|
|
|
};
|
|
|
|
|
|
|
|
StringCompare(StringView lhs, StringView rhs, Mode mode)
|
|
|
|
: m_lhs(move(lhs))
|
|
|
|
, m_rhs(move(rhs))
|
|
|
|
, m_mode(mode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
if (m_mode == Equal)
|
|
|
|
return m_lhs == m_rhs;
|
|
|
|
return m_lhs != m_rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView m_lhs;
|
|
|
|
StringView m_rhs;
|
|
|
|
Mode m_mode { Equal };
|
|
|
|
};
|
|
|
|
|
|
|
|
class NumericCompare : public Condition {
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
Equal,
|
|
|
|
Greater,
|
|
|
|
GreaterOrEqual,
|
|
|
|
Less,
|
|
|
|
LessOrEqual,
|
|
|
|
NotEqual,
|
|
|
|
};
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
NumericCompare(DeprecatedString lhs, DeprecatedString rhs, Mode mode)
|
2020-06-24 06:57:00 +03:00
|
|
|
: m_mode(mode)
|
|
|
|
{
|
|
|
|
auto lhs_option = lhs.trim_whitespace().to_int();
|
|
|
|
auto rhs_option = rhs.trim_whitespace().to_int();
|
|
|
|
|
|
|
|
if (!lhs_option.has_value())
|
|
|
|
fatal_error("expected integer expression: '%s'", lhs.characters());
|
|
|
|
|
|
|
|
if (!rhs_option.has_value())
|
|
|
|
fatal_error("expected integer expression: '%s'", rhs.characters());
|
|
|
|
|
|
|
|
m_lhs = lhs_option.value();
|
|
|
|
m_rhs = rhs_option.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
switch (m_mode) {
|
|
|
|
case Equal:
|
|
|
|
return m_lhs == m_rhs;
|
|
|
|
case Greater:
|
|
|
|
return m_lhs > m_rhs;
|
|
|
|
case GreaterOrEqual:
|
|
|
|
return m_lhs >= m_rhs;
|
|
|
|
case Less:
|
|
|
|
return m_lhs < m_rhs;
|
|
|
|
case LessOrEqual:
|
|
|
|
return m_lhs <= m_rhs;
|
|
|
|
case NotEqual:
|
|
|
|
return m_lhs != m_rhs;
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_lhs { 0 };
|
|
|
|
int m_rhs { 0 };
|
|
|
|
Mode m_mode { Equal };
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileCompare : public Condition {
|
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
Same,
|
|
|
|
ModificationTimestampGreater,
|
|
|
|
ModificationTimestampLess,
|
|
|
|
};
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
FileCompare(DeprecatedString lhs, DeprecatedString rhs, Mode mode)
|
2020-06-24 06:57:00 +03:00
|
|
|
: m_lhs(move(lhs))
|
|
|
|
, m_rhs(move(rhs))
|
|
|
|
, m_mode(mode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool check() const override
|
|
|
|
{
|
|
|
|
struct stat statbuf_l;
|
|
|
|
int rc = stat(m_lhs.characters(), &statbuf_l);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
perror(m_lhs.characters());
|
|
|
|
g_there_was_an_error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat statbuf_r;
|
|
|
|
rc = stat(m_rhs.characters(), &statbuf_r);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
perror(m_rhs.characters());
|
|
|
|
g_there_was_an_error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (m_mode) {
|
|
|
|
case Same:
|
|
|
|
return statbuf_l.st_dev == statbuf_r.st_dev && statbuf_l.st_ino == statbuf_r.st_ino;
|
|
|
|
case ModificationTimestampLess:
|
|
|
|
return statbuf_l.st_mtime < statbuf_r.st_mtime;
|
|
|
|
case ModificationTimestampGreater:
|
|
|
|
return statbuf_l.st_mtime > statbuf_r.st_mtime;
|
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString m_lhs;
|
|
|
|
DeprecatedString m_rhs;
|
2020-06-24 06:57:00 +03:00
|
|
|
Mode m_mode { Same };
|
|
|
|
};
|
|
|
|
|
2020-08-11 01:02:25 +03:00
|
|
|
static OwnPtr<Condition> parse_complex_expression(char* argv[]);
|
2020-06-24 06:57:00 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static bool should_treat_expression_as_single_string(StringView arg_after)
|
2020-06-24 06:57:00 +03:00
|
|
|
{
|
|
|
|
return arg_after.is_null() || arg_after == "-a" || arg_after == "-o";
|
|
|
|
}
|
|
|
|
|
2020-08-11 01:02:25 +03:00
|
|
|
static OwnPtr<Condition> parse_simple_expression(char* argv[])
|
2020-06-24 06:57:00 +03:00
|
|
|
{
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView arg { argv[optind], strlen(argv[optind]) };
|
2020-06-24 06:57:00 +03:00
|
|
|
if (arg.is_null()) {
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg == "(") {
|
|
|
|
optind++;
|
|
|
|
auto command = parse_complex_expression(argv);
|
2022-07-11 22:53:29 +03:00
|
|
|
if (command && argv[optind]) {
|
|
|
|
auto const* next_option = argv[++optind];
|
|
|
|
if (StringView { next_option, strlen(next_option) } == ")")
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2020-06-24 06:57:00 +03:00
|
|
|
fatal_error("Unmatched \033[1m(");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to read a unary op.
|
|
|
|
if (arg.starts_with('-') && arg.length() == 2) {
|
2022-10-02 23:45:06 +03:00
|
|
|
if (argv[++optind] == nullptr)
|
|
|
|
fatal_error("expected an argument");
|
2022-07-11 22:53:29 +03:00
|
|
|
if (should_treat_expression_as_single_string({ argv[optind], strlen(argv[optind]) })) {
|
2020-06-24 06:57:00 +03:00
|
|
|
--optind;
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(move(arg), ""sv, StringCompare::NotEqual);
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView value { argv[optind], strlen(argv[optind]) };
|
2020-06-24 06:57:00 +03:00
|
|
|
switch (arg[1]) {
|
|
|
|
case 'b':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::BlockDevice);
|
|
|
|
case 'c':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::CharacterDevice);
|
|
|
|
case 'd':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::Directory);
|
|
|
|
case 'f':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::Regular);
|
|
|
|
case 'h':
|
|
|
|
case 'L':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::SymbolicLink);
|
|
|
|
case 'p':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::FIFO);
|
|
|
|
case 'S':
|
|
|
|
return make<FileIsOfKind>(value, FileIsOfKind::Socket);
|
|
|
|
case 'r':
|
|
|
|
return make<UserHasPermission>(value, UserHasPermission::Read);
|
|
|
|
case 'w':
|
|
|
|
return make<UserHasPermission>(value, UserHasPermission::Write);
|
|
|
|
case 'x':
|
|
|
|
return make<UserHasPermission>(value, UserHasPermission::Execute);
|
|
|
|
case 'e':
|
|
|
|
return make<UserHasPermission>(value, UserHasPermission::Any);
|
2022-09-30 21:11:49 +03:00
|
|
|
case 'g':
|
|
|
|
return make<FileHasFlag>(value, FileHasFlag::SGID);
|
|
|
|
case 'k':
|
|
|
|
return make<FileHasFlag>(value, FileHasFlag::SVTX);
|
|
|
|
case 'u':
|
|
|
|
return make<FileHasFlag>(value, FileHasFlag::SUID);
|
2020-06-24 06:57:00 +03:00
|
|
|
case 'o':
|
|
|
|
case 'a':
|
|
|
|
// '-a' and '-o' are boolean ops, which are part of a complex expression
|
|
|
|
// so we have nothing to parse, simply return to caller.
|
|
|
|
--optind;
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-06-24 06:57:00 +03:00
|
|
|
case 'n':
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(""sv, value, StringCompare::NotEqual);
|
2020-06-24 06:57:00 +03:00
|
|
|
case 'z':
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(""sv, value, StringCompare::Equal);
|
2020-06-24 06:57:00 +03:00
|
|
|
case 'G':
|
2022-09-30 21:11:49 +03:00
|
|
|
return make<FileIsOwnedBy>(value, FileIsOwnedBy::EffectiveGID);
|
2020-06-24 06:57:00 +03:00
|
|
|
case 'O':
|
2022-09-30 21:11:49 +03:00
|
|
|
return make<FileIsOwnedBy>(value, FileIsOwnedBy::EffectiveUID);
|
|
|
|
case 'N':
|
2020-06-24 06:57:00 +03:00
|
|
|
case 's':
|
2022-09-30 21:09:29 +03:00
|
|
|
// 'optind' has been incremented to refer to the argument after the
|
|
|
|
// operator, while we want to print the operator itself.
|
|
|
|
fatal_error("Unsupported operator \033[1m%s", argv[optind - 1]);
|
2020-06-24 06:57:00 +03:00
|
|
|
default:
|
2020-12-11 18:31:48 +03:00
|
|
|
--optind;
|
2020-06-24 06:57:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:53:29 +03:00
|
|
|
auto get_next_arg = [&argv]() -> StringView {
|
|
|
|
auto const* next_arg = argv[++optind];
|
|
|
|
if (next_arg == NULL)
|
|
|
|
return StringView {};
|
|
|
|
return StringView { next_arg, strlen(next_arg) };
|
|
|
|
};
|
|
|
|
|
2020-06-24 06:57:00 +03:00
|
|
|
// Try to read a binary op, this is either a <string> op <string>, <integer> op <integer>, or <file> op <file>.
|
|
|
|
auto lhs = arg;
|
2022-07-11 22:53:29 +03:00
|
|
|
arg = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
|
|
|
|
if (arg == "=") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<StringCompare>(lhs, rhs, StringCompare::Equal);
|
|
|
|
} else if (arg == "!=") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<StringCompare>(lhs, rhs, StringCompare::NotEqual);
|
|
|
|
} else if (arg == "-eq") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::Equal);
|
|
|
|
} else if (arg == "-ge") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::GreaterOrEqual);
|
|
|
|
} else if (arg == "-gt") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::Greater);
|
|
|
|
} else if (arg == "-le") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::LessOrEqual);
|
|
|
|
} else if (arg == "-lt") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::Less);
|
|
|
|
} else if (arg == "-ne") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<NumericCompare>(lhs, rhs, NumericCompare::NotEqual);
|
|
|
|
} else if (arg == "-ef") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<FileCompare>(lhs, rhs, FileCompare::Same);
|
|
|
|
} else if (arg == "-nt") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<FileCompare>(lhs, rhs, FileCompare::ModificationTimestampGreater);
|
|
|
|
} else if (arg == "-ot") {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView rhs = get_next_arg();
|
2020-06-24 06:57:00 +03:00
|
|
|
return make<FileCompare>(lhs, rhs, FileCompare::ModificationTimestampLess);
|
|
|
|
} else if (arg == "-o" || arg == "-a") {
|
|
|
|
// '-a' and '-o' are boolean ops, which are part of a complex expression
|
|
|
|
// put them back and return with lhs as string compare.
|
|
|
|
--optind;
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(""sv, lhs, StringCompare::NotEqual);
|
2020-06-24 06:57:00 +03:00
|
|
|
} else {
|
2021-04-19 02:04:30 +03:00
|
|
|
// Now that we know it's not a well-formed expression, see if it's actually a negation
|
|
|
|
if (lhs == "!") {
|
|
|
|
if (should_treat_expression_as_single_string(arg))
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(move(lhs), ""sv, StringCompare::NotEqual);
|
2021-04-19 02:04:30 +03:00
|
|
|
|
|
|
|
auto command = parse_complex_expression(argv);
|
|
|
|
if (!command)
|
|
|
|
fatal_error("Expected an expression after \x1b[1m!");
|
|
|
|
|
|
|
|
return make<Not>(command.release_nonnull());
|
|
|
|
}
|
2020-06-24 06:57:00 +03:00
|
|
|
--optind;
|
2022-07-11 20:32:29 +03:00
|
|
|
return make<StringCompare>(""sv, lhs, StringCompare::NotEqual);
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 01:02:25 +03:00
|
|
|
static OwnPtr<Condition> parse_complex_expression(char* argv[])
|
2020-06-24 06:57:00 +03:00
|
|
|
{
|
|
|
|
auto command = parse_simple_expression(argv);
|
|
|
|
|
|
|
|
while (argv[optind] && argv[optind + 1]) {
|
|
|
|
if (!command && argv[optind])
|
|
|
|
fatal_error("expected an expression");
|
|
|
|
|
2022-07-11 22:53:29 +03:00
|
|
|
auto const* arg_ptr = argv[++optind];
|
|
|
|
StringView arg { arg_ptr, strlen(arg_ptr) };
|
2020-06-24 06:57:00 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
AndOp,
|
|
|
|
OrOp,
|
|
|
|
} binary_operation { AndOp };
|
|
|
|
|
|
|
|
if (arg == "-a") {
|
2022-10-02 23:45:06 +03:00
|
|
|
if (argv[++optind] == nullptr)
|
|
|
|
fatal_error("expected an expression");
|
2020-06-24 06:57:00 +03:00
|
|
|
binary_operation = AndOp;
|
|
|
|
} else if (arg == "-o") {
|
2022-10-02 23:45:06 +03:00
|
|
|
if (argv[++optind] == nullptr)
|
|
|
|
fatal_error("expected an expression");
|
2020-06-24 06:57:00 +03:00
|
|
|
binary_operation = OrOp;
|
|
|
|
} else {
|
|
|
|
// Ooops, looked too far.
|
|
|
|
optind--;
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
auto rhs = parse_complex_expression(argv);
|
|
|
|
if (!rhs)
|
|
|
|
fatal_error("Missing right-hand side");
|
|
|
|
|
|
|
|
if (binary_operation == AndOp)
|
|
|
|
command = make<And>(command.release_nonnull(), rhs.release_nonnull());
|
|
|
|
else
|
|
|
|
command = make<Or>(command.release_nonnull(), rhs.release_nonnull());
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2022-03-30 09:49:43 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-06-24 06:57:00 +03:00
|
|
|
{
|
2022-03-30 09:49:43 +03:00
|
|
|
auto maybe_error = Core::System::pledge("stdio rpath");
|
|
|
|
if (maybe_error.is_error()) {
|
|
|
|
warnln("{}", maybe_error.error());
|
2020-06-24 06:57:00 +03:00
|
|
|
return 126;
|
|
|
|
}
|
|
|
|
|
2022-03-30 09:49:43 +03:00
|
|
|
int argc = arguments.argc;
|
|
|
|
if (LexicalPath::basename(arguments.strings[0]) == "[") {
|
2020-06-24 06:57:00 +03:00
|
|
|
--argc;
|
2022-03-30 09:49:43 +03:00
|
|
|
if (StringView { arguments.strings[argc] } != "]")
|
2020-06-24 06:57:00 +03:00
|
|
|
fatal_error("test invoked as '[' requires a closing bracket ']'");
|
2022-07-11 23:18:40 +03:00
|
|
|
arguments.strings[argc] = {};
|
2020-06-24 06:57:00 +03:00
|
|
|
}
|
|
|
|
|
2020-07-15 18:00:53 +03:00
|
|
|
// Exit false when no arguments are given.
|
|
|
|
if (argc == 1)
|
|
|
|
return 1;
|
|
|
|
|
2022-03-30 09:49:43 +03:00
|
|
|
auto condition = parse_complex_expression(arguments.argv);
|
2020-06-24 06:57:00 +03:00
|
|
|
if (optind != argc - 1)
|
|
|
|
fatal_error("Too many arguments");
|
|
|
|
auto result = condition ? condition->check() : false;
|
|
|
|
|
|
|
|
if (g_there_was_an_error)
|
|
|
|
return 126;
|
|
|
|
return result ? 0 : 1;
|
|
|
|
}
|