2016-04-20 10:49:15 +08:00
|
|
|
// Implementation of the string builtin.
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "config.h"
|
2015-09-13 04:36:22 +08:00
|
|
|
|
2015-09-13 03:59:40 +08:00
|
|
|
#define PCRE2_CODE_UNIT_WIDTH WCHAR_T_BITS
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define PCRE2_STATIC
|
|
|
|
#endif
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <errno.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <memory>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2015-09-13 04:36:22 +08:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
2015-09-13 04:36:22 +08:00
|
|
|
#include "parse_util.h"
|
2016-04-20 10:49:15 +08:00
|
|
|
#include "pcre2.h"
|
2015-09-13 04:36:22 +08:00
|
|
|
#include "wgetopt.h"
|
2015-09-13 03:59:40 +08:00
|
|
|
#include "wildcard.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
class parser_t;
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
#define STRING_ERR_MISSING _(L"%ls: Expected argument\n")
|
2015-09-13 04:36:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
enum { BUILTIN_STRING_OK = 0, BUILTIN_STRING_NONE = 1, BUILTIN_STRING_ERROR = 2 };
|
2015-09-13 04:36:22 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static void string_error(io_streams_t &streams, const wchar_t *fmt, ...) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append(L"string ");
|
2015-09-13 03:59:40 +08:00
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.err.append_formatv(fmt, va);
|
2015-09-13 03:59:40 +08:00
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static void string_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *subcmd,
|
|
|
|
const wchar_t *opt) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_UNKNOWN, subcmd, opt);
|
|
|
|
builtin_print_help(parser, streams, L"string", streams.err);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// We read from stdin if we are the second or later process in a pipeline.
|
|
|
|
static bool string_args_from_stdin(const io_streams_t &streams) {
|
2015-09-26 05:17:53 +08:00
|
|
|
return streams.stdin_is_directly_redirected;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static const wchar_t *string_get_arg_stdin(wcstring *storage, const io_streams_t &streams) {
|
2015-09-13 03:59:40 +08:00
|
|
|
std::string arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2015-09-13 03:59:40 +08:00
|
|
|
char ch = '\0';
|
2015-09-22 02:24:49 +08:00
|
|
|
long rc = read_blocked(streams.stdin_fd, &ch, 1);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (rc < 0) { // failure
|
2015-09-13 03:59:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (rc == 0) { // EOF
|
|
|
|
if (arg.empty()) {
|
2015-09-13 03:59:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
break;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (ch == '\n') {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg += ch;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
2015-09-13 07:43:51 +08:00
|
|
|
*storage = str2wcstring(arg);
|
|
|
|
return storage->c_str();
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static const wchar_t *string_get_arg_argv(int *argidx, wchar_t **argv) {
|
2016-05-05 06:19:47 +08:00
|
|
|
return argv && argv[*argidx] ? argv[(*argidx)++] : 0;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static const wchar_t *string_get_arg(int *argidx, wchar_t **argv, wcstring *storage,
|
|
|
|
const io_streams_t &streams) {
|
|
|
|
if (string_args_from_stdin(streams)) {
|
2015-09-22 02:24:49 +08:00
|
|
|
return string_get_arg_stdin(storage, streams);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return string_get_arg_argv(argidx, argv);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L"n";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"no-quoted", no_argument, 0, 'n'}, {0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
escape_flags_t flags = ESCAPE_ALL;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2016-10-30 08:25:48 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-10-30 08:25:48 +08:00
|
|
|
if (opt == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
|
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2015-09-13 03:59:40 +08:00
|
|
|
flags |= ESCAPE_NO_QUOTED;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nesc = 0;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(escape(arg, flags));
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
nesc++;
|
|
|
|
}
|
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
return nesc > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_join(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L"q";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"quiet", no_argument, 0, 'q'}, {0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
bool quiet = false;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2016-10-30 08:25:48 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
|
|
|
if (opt == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
|
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
|
|
|
const wchar_t *sep;
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((sep = string_get_arg_argv(&i, argv)) == 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nargs = 0;
|
|
|
|
const wchar_t *arg;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
|
|
|
if (!quiet) {
|
|
|
|
if (nargs > 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(sep);
|
|
|
|
}
|
|
|
|
streams.out.append(arg);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (nargs > 0 && !quiet) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.push_back(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
return nargs > 1 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_length(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L"q";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"quiet", no_argument, 0, 'q'}, {0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
bool quiet = false;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2016-10-30 08:25:48 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-10-30 08:25:48 +08:00
|
|
|
if (opt == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *arg;
|
|
|
|
int nnonempty = 0;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
size_t n = wcslen(arg);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (n > 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
nnonempty++;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!quiet) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(to_string(n));
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
return nnonempty > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
struct match_options_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
bool all;
|
|
|
|
bool ignore_case;
|
|
|
|
bool index;
|
2016-04-08 10:18:58 +08:00
|
|
|
bool invert_match;
|
2015-09-13 03:59:40 +08:00
|
|
|
bool quiet;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
match_options_t()
|
|
|
|
: all(false), ignore_case(false), index(false), invert_match(false), quiet(false) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class string_matcher_t {
|
|
|
|
protected:
|
2015-09-13 03:59:40 +08:00
|
|
|
match_options_t opts;
|
2015-09-22 02:24:49 +08:00
|
|
|
io_streams_t &streams;
|
2015-09-13 03:59:40 +08:00
|
|
|
int total_matched;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2015-09-22 02:24:49 +08:00
|
|
|
string_matcher_t(const match_options_t &opts_, io_streams_t &streams_)
|
2016-04-20 10:49:15 +08:00
|
|
|
: opts(opts_), streams(streams_), total_matched(0) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
virtual ~string_matcher_t() {}
|
2015-09-13 03:59:40 +08:00
|
|
|
virtual bool report_matches(const wchar_t *arg) = 0;
|
|
|
|
int match_count() { return total_matched; }
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class wildcard_matcher_t : public string_matcher_t {
|
|
|
|
private:
|
2015-09-13 03:59:40 +08:00
|
|
|
wcstring wcpattern;
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
wildcard_matcher_t(const wchar_t * /*argv0*/, const wchar_t *pattern,
|
|
|
|
const match_options_t &opts, io_streams_t &streams)
|
|
|
|
: string_matcher_t(opts, streams), wcpattern(parse_util_unescape_wildcards(pattern)) {
|
|
|
|
if (opts.ignore_case) {
|
|
|
|
for (size_t i = 0; i < wcpattern.length(); i++) {
|
2015-09-13 03:59:40 +08:00
|
|
|
wcpattern[i] = towlower(wcpattern[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
virtual ~wildcard_matcher_t() {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool report_matches(const wchar_t *arg) {
|
|
|
|
// Note: --all is a no-op for glob matching since the pattern is always matched against the
|
|
|
|
// entire argument.
|
2015-09-13 03:59:40 +08:00
|
|
|
bool match;
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (opts.ignore_case) {
|
2015-09-13 03:59:40 +08:00
|
|
|
wcstring s = arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (size_t i = 0; i < s.length(); i++) {
|
2015-09-13 03:59:40 +08:00
|
|
|
s[i] = towlower(s[i]);
|
|
|
|
}
|
|
|
|
match = wildcard_match(s, wcpattern, false);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-13 03:59:40 +08:00
|
|
|
match = wildcard_match(arg, wcpattern, false);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (match ^ opts.invert_match) {
|
2015-09-13 03:59:40 +08:00
|
|
|
total_matched++;
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!opts.quiet) {
|
|
|
|
if (opts.index) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append_format(L"1 %lu\n", wcslen(arg));
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(arg);
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static wcstring pcre2_strerror(int err_code) {
|
2015-09-13 07:52:38 +08:00
|
|
|
wchar_t buf[128];
|
2015-09-13 03:59:40 +08:00
|
|
|
pcre2_get_error_message(err_code, (PCRE2_UCHAR *)buf, sizeof(buf) / sizeof(wchar_t));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
struct compiled_regex_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
pcre2_code *code;
|
|
|
|
pcre2_match_data *match;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
compiled_regex_t(const wchar_t *argv0, const wchar_t *pattern, bool ignore_case,
|
|
|
|
io_streams_t &streams)
|
|
|
|
: code(0), match(0) {
|
|
|
|
// Disable some sequences that can lead to security problems.
|
2015-09-13 03:59:40 +08:00
|
|
|
uint32_t options = PCRE2_NEVER_UTF;
|
|
|
|
#if PCRE2_CODE_UNIT_WIDTH < 32
|
|
|
|
options |= PCRE2_NEVER_BACKSLASH_C;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int err_code = 0;
|
|
|
|
PCRE2_SIZE err_offset = 0;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
code =
|
|
|
|
pcre2_compile(PCRE2_SPTR(pattern), PCRE2_ZERO_TERMINATED,
|
|
|
|
options | (ignore_case ? PCRE2_CASELESS : 0), &err_code, &err_offset, 0);
|
|
|
|
if (code == 0) {
|
|
|
|
string_error(streams, _(L"%ls: Regular expression compile error: %ls\n"), argv0,
|
|
|
|
pcre2_strerror(err_code).c_str());
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, L"%ls: %ls\n", argv0, pattern);
|
|
|
|
string_error(streams, L"%ls: %*ls\n", argv0, err_offset, L"^");
|
2015-09-13 03:59:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match = pcre2_match_data_create_from_pattern(code, 0);
|
2017-02-14 12:37:27 +08:00
|
|
|
assert(match);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
~compiled_regex_t() {
|
|
|
|
if (match != 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
pcre2_match_data_free(match);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (code != 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
pcre2_code_free(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class pcre2_matcher_t : public string_matcher_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *argv0;
|
|
|
|
compiled_regex_t regex;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
int report_match(const wchar_t *arg, int pcre2_rc) {
|
|
|
|
// Return values: -1 = error, 0 = no match, 1 = match.
|
|
|
|
if (pcre2_rc == PCRE2_ERROR_NOMATCH) {
|
|
|
|
if (opts.invert_match && !opts.quiet) {
|
2016-06-01 05:14:03 +08:00
|
|
|
if (opts.index) {
|
|
|
|
streams.out.append_format(L"1 %lu\n", wcslen(arg));
|
|
|
|
} else {
|
|
|
|
streams.out.append(arg);
|
|
|
|
streams.out.push_back(L'\n');
|
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return opts.invert_match ? 1 : 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (pcre2_rc < 0) {
|
|
|
|
string_error(streams, _(L"%ls: Regular expression match error: %ls\n"), argv0,
|
|
|
|
pcre2_strerror(pcre2_rc).c_str());
|
2015-09-13 03:59:40 +08:00
|
|
|
return -1;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (pcre2_rc == 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
// The output vector wasn't big enough. Should not happen.
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, _(L"%ls: Regular expression internal error\n"), argv0);
|
2015-09-13 03:59:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
|
|
|
|
else if (opts.invert_match)
|
|
|
|
return 0;
|
|
|
|
|
2015-09-13 03:59:40 +08:00
|
|
|
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(regex.match);
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (int j = 0; j < pcre2_rc; j++) {
|
|
|
|
PCRE2_SIZE begin = ovector[2 * j];
|
|
|
|
PCRE2_SIZE end = ovector[2 * j + 1];
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (begin != PCRE2_UNSET && end != PCRE2_UNSET && !opts.quiet) {
|
|
|
|
if (opts.index) {
|
|
|
|
streams.out.append_format(L"%lu %lu", (unsigned long)(begin + 1),
|
|
|
|
(unsigned long)(end - begin));
|
|
|
|
} else if (end > begin) // may have end < begin if \K is used
|
2016-04-08 10:18:58 +08:00
|
|
|
{
|
|
|
|
streams.out.append(wcstring(&arg[begin], end - begin));
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
streams.out.push_back(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
|
|
|
|
return opts.invert_match ? 0 : 1;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
|
|
|
pcre2_matcher_t(const wchar_t *argv0_, const wchar_t *pattern, const match_options_t &opts,
|
|
|
|
io_streams_t &streams)
|
2015-09-22 02:24:49 +08:00
|
|
|
: string_matcher_t(opts, streams),
|
2015-09-13 03:59:40 +08:00
|
|
|
argv0(argv0_),
|
2016-04-20 10:49:15 +08:00
|
|
|
regex(argv0_, pattern, opts.ignore_case, streams) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
virtual ~pcre2_matcher_t() {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool report_matches(const wchar_t *arg) {
|
|
|
|
// A return value of true means all is well (even if no matches were found), false indicates
|
|
|
|
// an unrecoverable error.
|
|
|
|
if (regex.code == 0) {
|
|
|
|
// pcre2_compile() failed.
|
2015-09-13 03:59:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int matched = 0;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// See pcre2demo.c for an explanation of this logic.
|
2015-09-13 03:59:40 +08:00
|
|
|
PCRE2_SIZE arglen = wcslen(arg);
|
2016-04-20 10:49:15 +08:00
|
|
|
int rc = report_match(
|
|
|
|
arg, pcre2_match(regex.code, PCRE2_SPTR(arg), arglen, 0, 0, regex.match, 0));
|
|
|
|
if (rc < 0) { // pcre2 match error.
|
2015-09-13 03:59:40 +08:00
|
|
|
return false;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (rc == 0) { // no match
|
2015-09-13 03:59:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
matched++;
|
|
|
|
total_matched++;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (opts.invert_match) {
|
2016-04-08 10:18:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Report any additional matches.
|
2015-09-13 03:59:40 +08:00
|
|
|
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(regex.match);
|
2016-04-20 10:49:15 +08:00
|
|
|
while (opts.all || matched == 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
uint32_t options = 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
PCRE2_SIZE offset = ovector[1]; // start at end of previous match
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (ovector[0] == ovector[1]) {
|
|
|
|
if (ovector[0] == arglen) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
rc = report_match(arg, pcre2_match(regex.code, PCRE2_SPTR(arg), arglen, offset, options,
|
|
|
|
regex.match, 0));
|
|
|
|
if (rc < 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
if (options == 0) { // all matches found
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ovector[1] = offset + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
matched++;
|
|
|
|
total_matched++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2016-04-08 10:18:58 +08:00
|
|
|
const wchar_t *short_options = L"ainvqr";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"all", no_argument, 0, 'a'},
|
|
|
|
{L"ignore-case", no_argument, 0, 'i'},
|
|
|
|
{L"index", no_argument, 0, 'n'},
|
|
|
|
{L"invert", no_argument, 0, 'v'},
|
|
|
|
{L"quiet", no_argument, 0, 'q'},
|
|
|
|
{L"regex", no_argument, 0, 'r'},
|
|
|
|
{0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
match_options_t opts;
|
|
|
|
bool regex = false;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2016-10-30 08:25:48 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-10-30 08:25:48 +08:00
|
|
|
if (opt == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.all = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'i': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.ignore_case = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'n': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.index = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'v': {
|
2016-04-08 10:18:58 +08:00
|
|
|
opts.invert_match = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'r': {
|
2015-09-13 03:59:40 +08:00
|
|
|
regex = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
|
|
|
const wchar_t *pattern;
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((pattern = string_get_arg_argv(&i, argv)) == 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-01-22 06:53:29 +08:00
|
|
|
std::unique_ptr<string_matcher_t> matcher;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (regex) {
|
2017-01-22 07:02:41 +08:00
|
|
|
matcher = make_unique<pcre2_matcher_t>(argv[0], pattern, opts, streams);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2017-01-22 07:02:41 +08:00
|
|
|
matcher = make_unique<wildcard_matcher_t>(argv[0], pattern, opts, streams);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *arg;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
|
|
|
if (!matcher->report_matches(arg)) {
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 10:47:02 +08:00
|
|
|
return matcher->match_count() > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
struct replace_options_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
bool all;
|
|
|
|
bool ignore_case;
|
|
|
|
bool quiet;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
replace_options_t() : all(false), ignore_case(false), quiet(false) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class string_replacer_t {
|
|
|
|
protected:
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *argv0;
|
|
|
|
replace_options_t opts;
|
|
|
|
int total_replaced;
|
2015-09-22 02:24:49 +08:00
|
|
|
io_streams_t &streams;
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2015-09-22 02:24:49 +08:00
|
|
|
string_replacer_t(const wchar_t *argv0_, const replace_options_t &opts_, io_streams_t &streams_)
|
2016-04-20 10:49:15 +08:00
|
|
|
: argv0(argv0_), opts(opts_), total_replaced(0), streams(streams_) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
virtual ~string_replacer_t() {}
|
|
|
|
virtual bool replace_matches(const wchar_t *arg) = 0;
|
|
|
|
int replace_count() { return total_replaced; }
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class literal_replacer_t : public string_replacer_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *pattern;
|
|
|
|
const wchar_t *replacement;
|
2015-09-13 07:43:51 +08:00
|
|
|
size_t patlen;
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2015-09-13 03:59:40 +08:00
|
|
|
literal_replacer_t(const wchar_t *argv0, const wchar_t *pattern_, const wchar_t *replacement_,
|
2016-04-20 10:49:15 +08:00
|
|
|
const replace_options_t &opts, io_streams_t &streams)
|
2015-09-22 02:24:49 +08:00
|
|
|
: string_replacer_t(argv0, opts, streams),
|
2016-04-20 10:49:15 +08:00
|
|
|
pattern(pattern_),
|
|
|
|
replacement(replacement_),
|
|
|
|
patlen(wcslen(pattern)) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
virtual ~literal_replacer_t() {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
bool replace_matches(const wchar_t *arg) {
|
2015-09-13 03:59:40 +08:00
|
|
|
wcstring result;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (patlen == 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
result = arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-13 03:59:40 +08:00
|
|
|
int replaced = 0;
|
|
|
|
const wchar_t *cur = arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
while (*cur != L'\0') {
|
2015-09-13 03:59:40 +08:00
|
|
|
if ((opts.all || replaced == 0) &&
|
2016-04-20 10:49:15 +08:00
|
|
|
(opts.ignore_case ? wcsncasecmp(cur, pattern, patlen)
|
|
|
|
: wcsncmp(cur, pattern, patlen)) == 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
result += replacement;
|
|
|
|
cur += patlen;
|
|
|
|
replaced++;
|
|
|
|
total_replaced++;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-13 03:59:40 +08:00
|
|
|
result += *cur;
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!opts.quiet) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(result);
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
class regex_replacer_t : public string_replacer_t {
|
2015-09-13 03:59:40 +08:00
|
|
|
compiled_regex_t regex;
|
|
|
|
wcstring replacement;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static wcstring interpret_escapes(const wchar_t *orig) {
|
2015-09-13 03:59:40 +08:00
|
|
|
wcstring result;
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
while (*orig != L'\0') {
|
|
|
|
if (*orig == L'\\') {
|
2015-09-13 03:59:40 +08:00
|
|
|
orig += read_unquoted_escape(orig, &result, true, false);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-13 03:59:40 +08:00
|
|
|
result += *orig;
|
|
|
|
orig++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
public:
|
2015-09-13 03:59:40 +08:00
|
|
|
regex_replacer_t(const wchar_t *argv0, const wchar_t *pattern, const wchar_t *replacement_,
|
2016-04-20 10:49:15 +08:00
|
|
|
const replace_options_t &opts, io_streams_t &streams)
|
2015-09-22 02:24:49 +08:00
|
|
|
: string_replacer_t(argv0, opts, streams),
|
|
|
|
regex(argv0, pattern, opts.ignore_case, streams),
|
2016-04-20 10:49:15 +08:00
|
|
|
replacement(interpret_escapes(replacement_)) {}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-11-02 11:00:09 +08:00
|
|
|
bool replace_matches(const wchar_t *arg);
|
|
|
|
};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-11-02 11:00:09 +08:00
|
|
|
/// A return value of true means all is well (even if no replacements were performed), false
|
|
|
|
/// indicates an unrecoverable error.
|
|
|
|
bool regex_replacer_t::replace_matches(const wchar_t *arg) {
|
|
|
|
if (regex.code == 0) {
|
|
|
|
// pcre2_compile() failed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t options = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH | PCRE2_SUBSTITUTE_EXTENDED |
|
|
|
|
(opts.all ? PCRE2_SUBSTITUTE_GLOBAL : 0);
|
|
|
|
size_t arglen = wcslen(arg);
|
|
|
|
PCRE2_SIZE bufsize = (arglen == 0) ? 16 : 2 * arglen;
|
|
|
|
wchar_t *output = (wchar_t *)malloc(sizeof(wchar_t) * bufsize);
|
|
|
|
int pcre2_rc;
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
2017-02-14 12:37:27 +08:00
|
|
|
assert(output);
|
|
|
|
|
2016-11-02 11:00:09 +08:00
|
|
|
PCRE2_SIZE outlen = bufsize;
|
|
|
|
pcre2_rc = pcre2_substitute(regex.code, PCRE2_SPTR(arg), arglen,
|
|
|
|
0, // start offset
|
|
|
|
options, regex.match,
|
|
|
|
0, // match context
|
|
|
|
PCRE2_SPTR(replacement.c_str()), PCRE2_ZERO_TERMINATED,
|
|
|
|
(PCRE2_UCHAR *)output, &outlen);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-11-02 11:00:09 +08:00
|
|
|
if (pcre2_rc != PCRE2_ERROR_NOMEMORY || bufsize >= outlen) {
|
|
|
|
done = true;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2016-11-02 11:00:09 +08:00
|
|
|
bufsize = outlen;
|
2016-11-15 13:31:51 +08:00
|
|
|
wchar_t *new_output = (wchar_t *)realloc(output, sizeof(wchar_t) * bufsize);
|
|
|
|
if (new_output) output = new_output;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-11-02 11:00:09 +08:00
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-11-02 11:00:09 +08:00
|
|
|
bool rc = true;
|
|
|
|
if (pcre2_rc < 0) {
|
|
|
|
string_error(streams, _(L"%ls: Regular expression substitute error: %ls\n"), argv0,
|
|
|
|
pcre2_strerror(pcre2_rc).c_str());
|
|
|
|
rc = false;
|
|
|
|
} else {
|
|
|
|
if (!opts.quiet) {
|
|
|
|
streams.out.append(output);
|
|
|
|
streams.out.append(L'\n');
|
|
|
|
}
|
|
|
|
total_replaced += pcre2_rc;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-11-02 11:00:09 +08:00
|
|
|
|
|
|
|
free(output);
|
|
|
|
return rc;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_replace(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L"aiqr";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"all", no_argument, 0, 'a'},
|
|
|
|
{L"ignore-case", no_argument, 0, 'i'},
|
|
|
|
{L"quiet", no_argument, 0, 'q'},
|
|
|
|
{L"regex", no_argument, 0, 'r'},
|
|
|
|
{0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
replace_options_t opts;
|
|
|
|
bool regex = false;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2016-10-30 08:25:48 +08:00
|
|
|
int opt = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-10-30 08:25:48 +08:00
|
|
|
if (opt == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
switch (opt) {
|
2016-04-20 10:49:15 +08:00
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'a': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.all = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'i': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.ignore_case = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
opts.quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'r': {
|
2015-09-13 03:59:40 +08:00
|
|
|
regex = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
|
|
|
const wchar_t *pattern, *replacement;
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((pattern = string_get_arg_argv(&i, argv)) == 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((replacement = string_get_arg_argv(&i, argv)) == 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-01-22 07:47:12 +08:00
|
|
|
std::unique_ptr<string_replacer_t> replacer;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (regex) {
|
2017-01-22 07:47:12 +08:00
|
|
|
replacer = make_unique<regex_replacer_t>(argv[0], pattern, replacement, opts, streams);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2017-01-22 07:47:12 +08:00
|
|
|
replacer = make_unique<literal_replacer_t>(argv[0], pattern, replacement, opts, streams);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *arg;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
|
|
|
if (!replacer->replace_matches(arg)) {
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 10:47:02 +08:00
|
|
|
return replacer->replace_count() > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// Given iterators into a string (forward or reverse), splits the haystack iterators
|
|
|
|
/// about the needle sequence, up to max times. Inserts splits into the output array.
|
|
|
|
/// If the iterators are forward, this does the normal thing.
|
|
|
|
/// If the iterators are backward, this returns reversed strings, in reversed order!
|
|
|
|
/// If the needle is empty, split on individual elements (characters).
|
|
|
|
template <typename ITER>
|
|
|
|
void split_about(ITER haystack_start, ITER haystack_end, ITER needle_start, ITER needle_end,
|
|
|
|
wcstring_list_t *output, long max) {
|
2015-09-13 16:11:49 +08:00
|
|
|
long remaining = max;
|
|
|
|
ITER haystack_cursor = haystack_start;
|
2016-04-20 10:49:15 +08:00
|
|
|
while (remaining > 0 && haystack_cursor != haystack_end) {
|
2015-09-13 16:11:49 +08:00
|
|
|
ITER split_point;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (needle_start == needle_end) { // empty needle, we split on individual elements
|
2015-09-13 16:11:49 +08:00
|
|
|
split_point = haystack_cursor + 1;
|
2016-04-20 10:49:15 +08:00
|
|
|
} else {
|
2015-09-13 16:11:49 +08:00
|
|
|
split_point = std::search(haystack_cursor, haystack_end, needle_start, needle_end);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (split_point == haystack_end) { // not found
|
2015-09-13 16:11:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
output->push_back(wcstring(haystack_cursor, split_point));
|
|
|
|
remaining--;
|
2016-04-20 10:49:15 +08:00
|
|
|
// Need to skip over the needle for the next search note that the needle may be empty.
|
2015-09-13 16:11:49 +08:00
|
|
|
haystack_cursor = split_point + std::distance(needle_start, needle_end);
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
// Trailing component, possibly empty.
|
2015-09-13 16:11:49 +08:00
|
|
|
output->push_back(wcstring(haystack_cursor, haystack_end));
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_split(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L":m:qr";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"max", required_argument, 0, 'm'},
|
|
|
|
{L"quiet", no_argument, 0, 'q'},
|
|
|
|
{L"right", no_argument, 0, 'r'},
|
|
|
|
{0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
long max = LONG_MAX;
|
|
|
|
bool quiet = false;
|
|
|
|
bool right = false;
|
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2015-09-13 03:59:40 +08:00
|
|
|
int c = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (c == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (c) {
|
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'm': {
|
2016-11-23 12:24:03 +08:00
|
|
|
max = fish_wcstol(w.woptarg);
|
|
|
|
if (errno) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'r': {
|
2015-09-13 03:59:40 +08:00
|
|
|
right = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
|
|
|
const wchar_t *sep;
|
2016-04-20 10:49:15 +08:00
|
|
|
if ((sep = string_get_arg_argv(&i, argv)) == NULL) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
2015-09-13 16:11:49 +08:00
|
|
|
const wchar_t *sep_end = sep + wcslen(sep);
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2015-09-13 16:11:49 +08:00
|
|
|
wcstring_list_t splits;
|
|
|
|
size_t arg_count = 0;
|
|
|
|
wcstring storage;
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
2015-09-13 16:11:49 +08:00
|
|
|
const wchar_t *arg_end = arg + wcslen(arg);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (right) {
|
2015-09-13 16:11:49 +08:00
|
|
|
typedef std::reverse_iterator<const wchar_t *> reverser;
|
2016-04-20 10:49:15 +08:00
|
|
|
split_about(reverser(arg_end), reverser(arg), reverser(sep_end), reverser(sep), &splits,
|
|
|
|
max);
|
|
|
|
} else {
|
|
|
|
split_about(arg, arg_end, sep, sep_end, &splits, max);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2015-09-13 16:11:49 +08:00
|
|
|
arg_count++;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
2015-09-13 16:11:49 +08:00
|
|
|
// If we are from the right, split_about gave us reversed strings, in reversed order!
|
2016-04-20 10:49:15 +08:00
|
|
|
if (right) {
|
|
|
|
for (size_t j = 0; j < splits.size(); j++) {
|
2016-04-08 10:18:58 +08:00
|
|
|
std::reverse(splits[j].begin(), splits[j].end());
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2015-09-13 16:11:49 +08:00
|
|
|
std::reverse(splits.begin(), splits.end());
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
|
|
|
|
if (!quiet) {
|
|
|
|
for (wcstring_list_t::const_iterator si = splits.begin(); si != splits.end(); ++si) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(*si);
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// We split something if we have more split values than args.
|
2016-05-05 06:19:47 +08:00
|
|
|
return splits.size() > arg_count ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_sub(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L":l:qs:";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"length", required_argument, 0, 'l'},
|
|
|
|
{L"quiet", no_argument, 0, 'q'},
|
|
|
|
{L"start", required_argument, 0, 's'},
|
|
|
|
{0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2015-09-13 17:15:37 +08:00
|
|
|
long start = 0;
|
|
|
|
long length = -1;
|
2015-09-13 03:59:40 +08:00
|
|
|
bool quiet = false;
|
|
|
|
wgetopter_t w;
|
2016-11-23 12:24:03 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2015-09-13 03:59:40 +08:00
|
|
|
int c = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (c == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (c) {
|
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2016-11-23 12:24:03 +08:00
|
|
|
length = fish_wcstol(w.woptarg);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (length < 0 || errno == ERANGE) {
|
|
|
|
string_error(streams, _(L"%ls: Invalid length value '%ls'\n"), argv[0],
|
|
|
|
w.woptarg);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-11-23 12:24:03 +08:00
|
|
|
} else if (errno) {
|
|
|
|
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
|
|
|
|
return BUILTIN_STRING_ERROR;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 's': {
|
2016-11-23 12:24:03 +08:00
|
|
|
start = fish_wcstol(w.woptarg);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start == 0 || start == LONG_MIN || errno == ERANGE) {
|
|
|
|
string_error(streams, _(L"%ls: Invalid start value '%ls'\n"), argv[0],
|
|
|
|
w.woptarg);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-11-23 12:24:03 +08:00
|
|
|
} else if (errno) {
|
|
|
|
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
|
|
|
|
return BUILTIN_STRING_ERROR;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nsub = 0;
|
|
|
|
const wchar_t *arg;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != NULL) {
|
2015-09-13 17:15:37 +08:00
|
|
|
typedef wcstring::size_type size_type;
|
|
|
|
size_type pos = 0;
|
|
|
|
size_type count = wcstring::npos;
|
2015-09-13 03:59:40 +08:00
|
|
|
wcstring s(arg);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (start > 0) {
|
2015-09-13 17:15:37 +08:00
|
|
|
pos = static_cast<size_type>(start - 1);
|
2016-04-20 10:49:15 +08:00
|
|
|
} else if (start < 0) {
|
|
|
|
assert(start != LONG_MIN); // checked above
|
2015-09-13 17:15:37 +08:00
|
|
|
size_type n = static_cast<size_type>(-start);
|
2015-09-13 03:59:40 +08:00
|
|
|
pos = n > s.length() ? 0 : s.length() - n;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (pos > s.length()) {
|
2015-09-13 03:59:40 +08:00
|
|
|
pos = s.length();
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (length >= 0) {
|
2015-09-13 17:15:37 +08:00
|
|
|
count = static_cast<size_type>(length);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// Note that std::string permits count to extend past end of string.
|
|
|
|
if (!quiet) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(s.substr(pos, count));
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
nsub++;
|
|
|
|
}
|
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
return nsub > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static int string_trim(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *short_options = L":c:lqr";
|
2016-04-20 10:49:15 +08:00
|
|
|
const struct woption long_options[] = {{L"chars", required_argument, 0, 'c'},
|
|
|
|
{L"left", no_argument, 0, 'l'},
|
|
|
|
{L"quiet", no_argument, 0, 'q'},
|
|
|
|
{L"right", no_argument, 0, 'r'},
|
|
|
|
{0, 0, 0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2015-09-13 08:15:08 +08:00
|
|
|
bool do_left = 0, do_right = 0;
|
2015-09-13 03:59:40 +08:00
|
|
|
bool quiet = false;
|
2015-09-13 08:15:08 +08:00
|
|
|
wcstring chars_to_trim = L" \f\n\r\t";
|
2015-09-13 03:59:40 +08:00
|
|
|
wgetopter_t w;
|
2016-04-20 10:49:15 +08:00
|
|
|
for (;;) {
|
2015-09-13 03:59:40 +08:00
|
|
|
int c = w.wgetopt_long(argc, argv, short_options, long_options, 0);
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (c == -1) {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
switch (c) {
|
|
|
|
case 0: {
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'c': {
|
2015-09-13 08:15:08 +08:00
|
|
|
chars_to_trim = w.woptarg;
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'l': {
|
2015-09-13 08:15:08 +08:00
|
|
|
do_left = true;
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'q': {
|
2015-09-13 03:59:40 +08:00
|
|
|
quiet = true;
|
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case 'r': {
|
2015-09-13 08:15:08 +08:00
|
|
|
do_right = true;
|
2015-09-13 03:59:40 +08:00
|
|
|
break;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case ':': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, STRING_ERR_MISSING, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
|
|
|
case '?': {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
2016-04-20 10:49:15 +08:00
|
|
|
}
|
2016-10-30 08:25:48 +08:00
|
|
|
default: {
|
|
|
|
DIE("unexpected opt");
|
|
|
|
break;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = w.woptind;
|
2016-04-20 10:49:15 +08:00
|
|
|
if (string_args_from_stdin(streams) && argc > i) {
|
2015-09-22 02:24:49 +08:00
|
|
|
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, argv[0]);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
// If neither left or right is specified, we do both.
|
|
|
|
if (!do_left && !do_right) {
|
2015-09-13 08:15:08 +08:00
|
|
|
do_left = true;
|
|
|
|
do_right = true;
|
|
|
|
}
|
2015-09-13 03:59:40 +08:00
|
|
|
|
|
|
|
const wchar_t *arg;
|
2015-09-13 08:15:08 +08:00
|
|
|
size_t ntrim = 0;
|
2016-04-20 10:49:15 +08:00
|
|
|
|
2015-09-13 08:15:08 +08:00
|
|
|
wcstring argstr;
|
2015-09-13 07:43:51 +08:00
|
|
|
wcstring storage;
|
2016-04-20 10:49:15 +08:00
|
|
|
while ((arg = string_get_arg(&i, argv, &storage, streams)) != 0) {
|
2015-09-13 08:15:08 +08:00
|
|
|
argstr = arg;
|
2016-04-20 10:49:15 +08:00
|
|
|
// Begin and end are respectively the first character to keep on the left, and first
|
|
|
|
// character to trim on the right. The length is thus end - start.
|
2015-09-13 08:15:08 +08:00
|
|
|
size_t begin = 0, end = argstr.size();
|
2016-04-20 10:49:15 +08:00
|
|
|
if (do_right) {
|
2015-09-13 08:15:08 +08:00
|
|
|
size_t last_to_keep = argstr.find_last_not_of(chars_to_trim);
|
|
|
|
end = (last_to_keep == wcstring::npos) ? 0 : last_to_keep + 1;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (do_left) {
|
2015-09-13 08:15:08 +08:00
|
|
|
size_t first_to_keep = argstr.find_first_not_of(chars_to_trim);
|
|
|
|
begin = (first_to_keep == wcstring::npos ? end : first_to_keep);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2015-09-13 08:15:08 +08:00
|
|
|
assert(begin <= end && end <= argstr.size());
|
|
|
|
ntrim += argstr.size() - (end - begin);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (!quiet) {
|
2015-09-22 02:24:49 +08:00
|
|
|
streams.out.append(wcstring(argstr, begin, end - begin));
|
|
|
|
streams.out.append(L'\n');
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
return ntrim > 0 ? BUILTIN_STRING_OK : BUILTIN_STRING_NONE;
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
static const struct string_subcommand {
|
2015-09-13 03:59:40 +08:00
|
|
|
const wchar_t *name;
|
2016-11-02 10:12:14 +08:00
|
|
|
int (*handler)(parser_t &, io_streams_t &, int argc, //!OCLINT(unused param)
|
|
|
|
wchar_t **argv); //!OCLINT(unused param)
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|
2016-04-08 10:18:58 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
string_subcommands[] = {
|
|
|
|
{L"escape", &string_escape}, {L"join", &string_join}, {L"length", &string_length},
|
|
|
|
{L"match", &string_match}, {L"replace", &string_replace}, {L"split", &string_split},
|
|
|
|
{L"sub", &string_sub}, {L"trim", &string_trim}, {0, 0}};
|
2015-09-13 03:59:40 +08:00
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
/// The string builtin, for manipulating strings.
|
|
|
|
int builtin_string(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2015-09-13 03:59:40 +08:00
|
|
|
int argc = builtin_count_args(argv);
|
2016-04-20 10:49:15 +08:00
|
|
|
if (argc <= 1) {
|
2015-09-25 09:21:09 +08:00
|
|
|
streams.err.append_format(_(L"string: Expected subcommand\n"));
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, L"string", streams.err);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:49:15 +08:00
|
|
|
if (wcscmp(argv[1], L"-h") == 0 || wcscmp(argv[1], L"--help") == 0) {
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, L"string", streams.err);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string_subcommand *subcmd = &string_subcommands[0];
|
2016-04-20 10:49:15 +08:00
|
|
|
while (subcmd->name != 0 && wcscmp(subcmd->name, argv[1]) != 0) {
|
2015-09-13 03:59:40 +08:00
|
|
|
subcmd++;
|
|
|
|
}
|
2016-04-20 10:49:15 +08:00
|
|
|
if (subcmd->handler == 0) {
|
2015-09-25 09:21:09 +08:00
|
|
|
streams.err.append_format(_(L"string: Unknown subcommand '%ls'\n"), argv[1]);
|
2015-09-22 02:24:49 +08:00
|
|
|
builtin_print_help(parser, streams, L"string", streams.err);
|
2015-09-13 03:59:40 +08:00
|
|
|
return BUILTIN_STRING_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2015-09-22 02:24:49 +08:00
|
|
|
return subcmd->handler(parser, streams, argc, argv);
|
2015-09-13 03:59:40 +08:00
|
|
|
}
|