aboutsummaryrefslogtreecommitdiffstats
path: root/func.c
diff options
context:
space:
mode:
authorTatsuya Kinoshita <tats@debian.org>2021-01-02 00:20:37 +0000
committerTatsuya Kinoshita <tats@debian.org>2021-01-02 00:20:37 +0000
commit1d0ba25a660483da1272a31dd077ed94441e3d9f (patch)
tree1d8dee52cd1e3d340fe178a8193dc96c4496db84 /func.c
parentMerge branch 'cvstrunk' into upstream (diff)
downloadw3m-upstream/0.5.3+git20210102.tar.gz
w3m-upstream/0.5.3+git20210102.zip
New upstream version 0.5.3+git20210102upstream/0.5.3+git20210102upstream
Diffstat (limited to 'func.c')
-rw-r--r--func.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/func.c b/func.c
index f389e00..8b5deac 100644
--- a/func.c
+++ b/func.c
@@ -8,6 +8,7 @@
#include "fm.h"
#include "func.h"
#include "myctype.h"
+#include "regex.h"
#include "funcname.c"
#include "functable.c"
@@ -434,6 +435,93 @@ getQWord(char **str)
return tmp->ptr;
}
+/* This extracts /regex/i or m@regex@i from the given string.
+ * Then advances *str to the end of regex.
+ * If the input does not seems to be a regex, this falls back to getQWord().
+ *
+ * Returns a word (no matter whether regex or not) in the give string.
+ * If regex_ret is non-NULL, compiles the regex and stores there.
+ *
+ * XXX: Actually this is unrelated to func.c.
+ */
+char *
+getRegexWord(const char **str, Regex **regex_ret)
+{
+ char *word = NULL;
+ const char *p, *headp, *bodyp, *tailp;
+ char delimiter;
+ int esc;
+ int igncase = 0;
+
+ p = *str;
+ SKIP_BLANKS(p);
+ headp = p;
+
+ /* Get the opening delimiter */
+ if (p[0] == 'm' && IS_PRINT(p[1]) && !IS_ALNUM(p[1]) && p[1] != '\\') {
+ delimiter = p[1];
+ p += 2;
+ }
+ else if (p[0] == '/') {
+ delimiter = '/';
+ p += 1;
+ }
+ else {
+ goto not_regex;
+ }
+ bodyp = p;
+
+ /* Scan the end of the expression */
+ for (esc = 0; *p; ++p) {
+ if (esc) {
+ esc = 0;
+ } else {
+ if (*p == delimiter)
+ break;
+ else if (*p == '\\')
+ esc = 1;
+ }
+ }
+ if (!*p && *headp == '/')
+ goto not_regex;
+ tailp = p;
+
+ /* Check the modifiers */
+ if (*p == delimiter) {
+ while (*++p && !IS_SPACE(*p)) {
+ switch (*p) {
+ case 'i':
+ igncase = 1;
+ break;
+ }
+ /* ignore unknown modifiers */
+ }
+ }
+
+ /* Save the expression */
+ word = allocStr(headp, p - headp);
+
+ /* Compile */
+ if (regex_ret) {
+ if (*tailp == delimiter)
+ word[tailp - headp] = 0;
+ *regex_ret = newRegex(word + (bodyp - headp), igncase, NULL, NULL);
+ if (*tailp == delimiter)
+ word[tailp - headp] = delimiter;
+ }
+ goto last;
+
+not_regex:
+ p = headp;
+ word = getQWord((char **)&p);
+ if (regex_ret)
+ *regex_ret = NULL;
+
+last:
+ *str = p;
+ return word;
+}
+
#ifdef USE_MOUSE
static MouseAction default_mouse_action = {
NULL,