mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
find: Add the -regex
and -iregex
options
These options match a given regular expression against the whole file path. `-iregex` is identical to `-regex` except it is case-insensitive.
This commit is contained in:
parent
2a86bd8c91
commit
0a02c7a926
2 changed files with 34 additions and 0 deletions
|
@ -92,6 +92,7 @@ target_link_libraries(disasm PRIVATE LibX86)
|
||||||
target_link_libraries(expr PRIVATE LibRegex)
|
target_link_libraries(expr PRIVATE LibRegex)
|
||||||
target_link_libraries(fdtdump PRIVATE LibDeviceTree)
|
target_link_libraries(fdtdump PRIVATE LibDeviceTree)
|
||||||
target_link_libraries(file PRIVATE LibGfx LibIPC LibArchive LibCompress LibAudio)
|
target_link_libraries(file PRIVATE LibGfx LibIPC LibArchive LibCompress LibAudio)
|
||||||
|
target_link_libraries(find PRIVATE LibRegex)
|
||||||
target_link_libraries(functrace PRIVATE LibDebug LibX86)
|
target_link_libraries(functrace PRIVATE LibDebug LibX86)
|
||||||
target_link_libraries(glsl-compiler PRIVATE LibGLSL)
|
target_link_libraries(glsl-compiler PRIVATE LibGLSL)
|
||||||
target_link_libraries(gml-format PRIVATE LibGUI)
|
target_link_libraries(gml-format PRIVATE LibGUI)
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
#include <LibRegex/Regex.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -267,6 +268,24 @@ private:
|
||||||
CaseSensitivity m_case_sensitivity { CaseSensitivity::CaseSensitive };
|
CaseSensitivity m_case_sensitivity { CaseSensitivity::CaseSensitive };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class RegexCommand : public Command {
|
||||||
|
public:
|
||||||
|
RegexCommand(Regex<PosixBasic>&& regex)
|
||||||
|
: m_regex(move(regex))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool evaluate(FileData& file_data) const override
|
||||||
|
{
|
||||||
|
auto haystack = file_data.full_path.string();
|
||||||
|
auto match_result = m_regex.match(haystack);
|
||||||
|
return match_result.success;
|
||||||
|
}
|
||||||
|
|
||||||
|
Regex<PosixBasic> m_regex;
|
||||||
|
};
|
||||||
|
|
||||||
class AccessCommand final : public Command {
|
class AccessCommand final : public Command {
|
||||||
public:
|
public:
|
||||||
AccessCommand(mode_t mode)
|
AccessCommand(mode_t mode)
|
||||||
|
@ -426,6 +445,20 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
|
||||||
if (args.is_empty())
|
if (args.is_empty())
|
||||||
fatal_error("-iname: requires additional arguments");
|
fatal_error("-iname: requires additional arguments");
|
||||||
return make<NameCommand>(args.take_first(), CaseSensitivity::CaseInsensitive);
|
return make<NameCommand>(args.take_first(), CaseSensitivity::CaseInsensitive);
|
||||||
|
} else if (arg == "-regex") {
|
||||||
|
if (args.is_empty())
|
||||||
|
fatal_error("-regex: requires additional arguments");
|
||||||
|
Regex<PosixBasic> regex { args.take_first(), PosixFlags::Default };
|
||||||
|
if (regex.parser_result.error != regex::Error::NoError)
|
||||||
|
fatal_error("{}", regex.error_string());
|
||||||
|
return make<RegexCommand>(move(regex));
|
||||||
|
} else if (arg == "-iregex") {
|
||||||
|
if (args.is_empty())
|
||||||
|
fatal_error("-iregex: requires additional arguments");
|
||||||
|
Regex<PosixBasic> regex { args.take_first(), PosixFlags::Insensitive };
|
||||||
|
if (regex.parser_result.error != regex::Error::NoError)
|
||||||
|
fatal_error("{}", regex.error_string());
|
||||||
|
return make<RegexCommand>(move(regex));
|
||||||
} else if (arg == "-readable") {
|
} else if (arg == "-readable") {
|
||||||
return make<AccessCommand>(R_OK);
|
return make<AccessCommand>(R_OK);
|
||||||
} else if (arg == "-writable") {
|
} else if (arg == "-writable") {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue