/* * This file is part of the Vars library, copyright (C) Glenn Hutchings * 1996-2006. * * The Vars library comes with ABSOLUTELY NO WARRANTY. This is free * software, and you are welcome to redistribute it under certain * conditions; see the file COPYING for details. */ /*! @defgroup regex Regular expressions @ingroup types A regular expression (or regexp) is a pattern used to match strings. */ /*! @defgroup regex_create Creating and destroying regexps @ingroup regex */ /*! @defgroup regex_match Matching and substituting strings @ingroup regex */ /*! @defgroup regex_misc Other regexp functions @ingroup regex */ /*! @defgroup regex_syntax Regexp syntax @ingroup regex This regexp syntax description is excerpted from the GNU regex manual. Regular expressions have a syntax in which a few characters are special constructs and the rest are "ordinary". An ordinary character is a simple regular expression which matches that character and nothing else. The special characters are $, ^, ., *, +, ?, [, ] and \\. Any other character appearing in a regular expression is ordinary, unless a \\ precedes it. For example, f is not a special character, so it is ordinary, and therefore f is a regular expression that matches the string f and no other string. (It does \e not match the string ff.) Likewise, o is a regular expression that matches only o. Any two regular expressions A and B can be concatenated. The result is a regular expression which matches a string if A matches some amount of the beginning of that string and B matches the rest of the string. As a simple example, we can concatenate the regular expressions f and o to get the regular expression fo, which matches only the string fo. Still trivial. Note: for Unix compatibility, special characters are treated as ordinary ones if they are in contexts where their special meanings make no sense. For example, *foo treats * as ordinary since there is no preceding expression on which the * can act. It is poor practice to depend on this behavior; better to quote the special character anyway, regardless of where is appears. The following are the characters and character sequences which have special meaning within regular expressions. Any character not mentioned here is not special; it stands for exactly itself for the purposes of searching and matching. @par Sequence: . . is a special character that matches anything except a newline. Using concatenation, we can make regular expressions like a.b which matches any three-character string which begins with a and ends with b. @par Sequence: * * is not a construct by itself; it is a suffix, which means the preceding regular expression is to be repeated as many times as possible. In fo*, the * applies to the o, so fo* matches f followed by any number of o's. The case of zero o's is allowed: fo* does match f. * always applies to the \e smallest possible preceding expression. Thus, fo* has a repeating o, not a repeating fo. The matcher processes a * construct by matching, immediately, as many repetitions as can be found. Then it continues with the rest of the pattern. If that fails, backtracking occurs, discarding some of the matches of the *'d construct in case that makes it possible to match the rest of the pattern. For example, matching c[ad]*ar against the string caddaar, the [ad]* first matches addaa, but this does not allow the next a in the pattern to match. So the last of the matches of [ad] is undone and the following a is tried again. Now it succeeds. @par Sequence: + + is like * except that at least one match for the preceding pattern is required for +. Thus, c[ad]+r does not match cr but does match anything else that c[ad]*r would match. ? is like * except that it allows either zero or one match for the preceding pattern. Thus, c[ad]?r matches cr or car or cdr, and nothing else. @par Sequence: [ ... ] [ begins a "character set", which is terminated by a ]. In the simplest case, the characters between the two form the set. Thus, [ad] matches either a or d, and [ad]* matches any string of a's and d's (including the empty string), from which it follows that c[ad]*r matches car, etc. Character ranges can also be included in a character set, by writing two characters with a - between them. Thus, [a-z] matches any lower-case letter. Ranges may be intermixed freely with individual characters, as in [a-z$%.], which matches any lower case letter or $, % or period. Note that the usual special characters are not special any more inside a character set. A completely different set of special characters exists inside character sets: ], - and ^. To include a ] in a character set, you must make it the first character. For example, []a] matches ] or a. To include a -, you must use it in a context where it cannot possibly indicate a range: that is, as the first character, or immediately after a range. @par Sequence: [^ ... ] [^ begins a "complement character set", which matches any character except the ones specified. Thus, [^a-z0-9A-Z] matches all characters \e except letters and digits. ^ is not special in a character set unless it is the first character. The character following the ^ is treated as if it were first (it may be a - or a ]). @par Sequence: ^ ^ is a special character that matches the empty string -- but only if at the beginning of a line in the text being matched. Otherwise it fails to match anything. Thus, ^foo matches a foo which occurs at the beginning of a line. @par Sequence: $ $ is similar to ^ but matches only at the end of a line. Thus, xx*$ matches a string of one or more x's at the end of a line. @par Sequence: \\ \\ has two functions: it quotes the above special characters (including \\), and it introduces additional special constructs. Because \\ quotes special characters, \\$ is a regular expression which matches only $, and \\[ is a regular expression which matches only [, and so on. For the most part, \\ followed by any character matches only that character. However, there are several exceptions: characters which, when preceded by \\, are special constructs. Such characters are always ordinary when encountered on their own. No new special characters will ever be defined. All extensions to the regular expression syntax are made by defining new two-character constructs that begin with \\. @par Sequence: \\| \\| specifies an alternative. Two regular expressions A and B with \\| in between form an expression that matches anything that either A or B will match. Thus, foo\\|bar matches either foo or bar but no other string. \\| applies to the largest possible surrounding expressions. Only a surrounding \\( ... \\) grouping can limit the grouping power of \\|. Full backtracking capability exists when multiple \\|'s are used. @par Sequence: \\( ... \\) \\( ... \\) is a grouping construct that serves three purposes: -# To enclose a set of \\| alternatives for other operations. Thus, \\(foo\\|bar\\)x matches either foox or barx. -# To enclose a complicated expression for the postfix * to operate on. Thus, ba\\(na\\)* matches bananana, etc., with any (zero or more) number of na's. -# To mark a matched substring for future reference. This last application is not a consequence of the idea of a parenthetical grouping; it is a separate feature which happens to be assigned as a second meaning to the same \\( ... \\) construct because there is no conflict in practice between the two meanings. Here is an explanation of this feature: . @par Sequence: \\DIGIT After the end of a \\( ... \\) construct, the matcher remembers the beginning and end of the text matched by that construct. Then, later on in the regular expression, you can use \\ followed by DIGIT to mean "match the same text matched the DIGIT'th time by the \\( ... \\) construct." The \\( ... \\) constructs are numbered in order of commencement in the regexp. The strings matching the first nine \\( ... \\) constructs appearing in a regular expression are assigned numbers 1 through 9 in order of their beginnings. \\1 through \\9 may be used to refer to the text matched by the corresponding \\( ... \\) construct. For example, \\(.*\\)\\1 matches any string that is composed of two identical halves. The \\(.*\\) matches the first half, which may be anything, but the \\1 that follows must match the same exact text. @par Other constructs \\` matches the empty string, but only at the beginning of the string being matched against. \\' matches the empty string, but only at the end of the string being matched against. \\b matches the empty string, but only if it is at the beginning or end of a word. Thus, \\bfoo\\b matches any occurrence of foo as a separate word. \\bball\\(s\\|\\)\\b matches ball or balls as a separate word. \\B matches the empty string, provided it is \e not at the beginning or end of a word. \\< matches the empty string, but only if it is at the beginning of a word. \\> matches the empty string, but only if it is at the end of a word. \\w matches any word-constituent character. \\W matches any character that is not a word-constituent. */ #include #include #include #include #include "vars-config.h" #include "vars-buffer.h" #include "vars-macros.h" #include "vars-memory.h" #include "vars-regex.h" #include "vars-system.h" /* Type definition */ struct v_regex { struct v_header id; /* Type marker */ char *name; /* Regexp string */ struct re_pattern_buffer buf; /* Compiled pattern */ }; /* Type variable */ vtype *vregex_type = NULL; /* Default pattern buffer size */ static int patbufsize = 100; /* Last string matched */ static char *laststring; /* Did last regexp compile? */ static int compiled; /* List of matches from last match */ static struct re_registers matchlist; /* Error message from last match (if any) */ V_NBUF_DECL(errbuf); /*! @brief Return a copy of a regexp. @ingroup regex_create @param r Regexp. @return Copy of the regexp. */ vregex * vr_copy(vregex *r) { return vr_create(r->name); } /*! @brief Create a new regexp. @ingroup regex_create @param str Regexp string. @return Regexp. @retval NULL if string is not a valid regexp. */ vregex * vr_create(char *str) { return vr_create_translate(str, NULL); } /*! @brief Create a new regexp with a case-folding translate table. @ingroup regex_create @param str Regexp string. @return Regexp. Like vr_create(), but set the regexp to ignore upper/lower case differences when matching. */ vregex * vr_create_nocase(char *str) { static char *nocase_table = NULL; int i; if (nocase_table == NULL) { nocase_table = V_ALLOC(char, 256); for (i = 0; i < 0400; i++) nocase_table[i] = i; for (i = 'a'; i <= 'z'; i++) nocase_table[i] = i - 040; } return vr_create_translate(str, nocase_table); } /*! @brief Create a new regexp with a translate table. @ingroup regex_create @param str Regexp string. @param table Translation table. @return Regexp. Specify a translate table to use when matching characters. The table should be a 256-element array, one element per ASCII character, with each entry being the character value to translate to. */ vregex * vr_create_translate(char *str, char *table) { static vheader *id = NULL; const char *error; vregex *r; if (id == NULL) { vr_declare(); id = v_header(vregex_type); } r = V_ALLOC(vregex, 1); r->id = *id; r->name = V_STRDUP(str); r->buf.allocated = patbufsize; r->buf.buffer = V_ALLOC(unsigned char, patbufsize); r->buf.fastmap = V_ALLOC(char, 256); r->buf.translate = table; if ((error = re_compile_pattern(str, strlen(str), &r->buf)) != NULL) { V_NBUF_SET(errbuf, (char *) error); vr_destroy(r); compiled = 0; return NULL; } re_compile_fastmap(&r->buf); compiled = 1; return r; } /* Declare regexp type */ vtype * vr_declare(void) { if (vregex_type == NULL) { vregex_type = v_create("REGEX", "R"); v_copy_func(vregex_type, (void *(*)()) vr_copy); v_read_func(vregex_type, (void *(*)()) vr_read); v_write_func(vregex_type, vr_write); v_freeze_func(vregex_type, vr_freeze); v_thaw_func(vregex_type, (void *(*)()) vr_thaw); v_print_func(vregex_type, vr_print); v_destroy_func(vregex_type, vr_destroy); v_traverse_func(vregex_type, vr_traverse); } return vregex_type; } /*! @brief Deallocate a regexp. @ingroup regex_create @param r Regexp. */ void vr_destroy(vregex *r) { VR_CHECK(r); V_DEALLOC(r->name); V_DEALLOC(r->buf.buffer); if (r->buf.fastmap != NULL) V_DEALLOC(r->buf.fastmap); V_DEALLOC(r); } /*! @brief Return last regexp compilation error. @ingroup regex_misc @return Error string. @retval NULL if there was no error. */ char * vr_error(void) { return (compiled ? NULL : V_NBUF_VAL(errbuf)); } /*! @brief Return bracketed parts of a string that matches a regexp. @ingroup regex_match @param string String to match. @param r Regexp. @return List of matches. @retval NULL if nothing matched. */ vlist * vr_extract(char *string, vregex *r) { char *match; int n = 0; vlist *l; VR_CHECK(r); l = vl_create(); if (!vr_match(string, r)) return NULL; while ((match = vr_matched(n++)) != NULL) vl_spush(l, match); return l; } /* Freeze a regexp to a stream */ int vr_freeze(vregex *r, FILE *fp) { VR_CHECK(r); v_freeze_start(fp); v_freeze_string(r->name, fp); v_freeze_finish(fp); return 1; } /*! @brief Return a sublist of a list which matches a regexp. @ingroup regex_match @param l List of strings. @param r Regexp. @return List of matched strings. */ vlist * vr_grep(vlist *l, vregex *r) { viter iter; vlist *m; VL_CHECK(l); VR_CHECK(r); m = vl_create(); v_iterate(l, iter) if (vr_match(vl_iter_svalref(iter), r)) vl_push(m, vs_copy(vl_iter_val(iter))); return m; } /*! @brief Check for a regexp match. @ingroup regex_match @param str String to match. @param r Regexp. @return Whether it matched. */ int vr_match(char *str, vregex *r) { int result, len = strlen(str); VR_CHECK(r); laststring = str; result = re_search(&r->buf, str, len, 0, len, &matchlist); return (result >= 0); } /*! @brief Return the Nth bracketed match. @ingroup regex_match @param num Match number. @return Matched string. Returns parts of the string that matched in the last vr_match() call. With \c num = 0, returns the whole substring that matched. With \c num > 0, returns the num'th bracket match (part of the regexp enclosed by \\( and \\)). If \c num is greater than the number of brackets in the regexp, returns \c NULL. Note that the string argument to the last vr_match() call should \e not have been modified when you use this function, or chaos may result. */ char * vr_matched(int num) { static vbuffer *b = NULL; int i; /* Initialise */ vb_init(b); /* Get match */ if (num < 0 || num >= RE_NREGS) return NULL; if (matchlist.start[num] < 0) return NULL; for (i = matchlist.start[num]; i < matchlist.end[num]; i++) vb_putc(b, laststring[i]); return vb_get(b); } /* Print contents of a regexp */ void vr_print(vregex *r, FILE *fp) { VR_CHECK(r); v_print_type(vregex_type, r, fp); v_print_start(); v_push_indent(); v_indent(fp); fprintf(fp, "%s\n", r->name); v_pop_indent(); v_print_finish(); } /* Read regexp from a stream */ vregex * vr_read(FILE *fp) { char *pat; /* Read regexp pattern */ if ((pat = v_read_string(fp)) == NULL) return NULL; return vr_create(pat); } /*! @brief Return the regexp string used to build a regexp. @ingroup regex_misc @param r Regexp. @return Regexp string. */ char * vr_regexp(vregex *r) { return r->name; } /*! @brief Return a string with matches substituted for a string. @ingroup regex_match @param str String to match. @param r Regexp @param sub Substitution string. @return Result (pointer to internal buffer). Substitute all matches for a regexp in a string with another string, and return the result. If \c sub is \c NULL, no replacement is done. */ char * vr_subst(char *str, vregex *r, char *sub) { static vbuffer *b = NULL; int len; VR_CHECK(r); /* Initialise */ vb_init(b); while (*str != '\0') { if ((len = re_match(&r->buf, str, strlen(str), 0, NULL)) > 0) { /* Skip over matched part of string */ while (len-- > 0) str++; /* Add substitution to result if required */ if (sub != NULL) vb_puts(b, sub); } else { vb_putc(b, *str++); } } return vb_get(b); } /* Thaw a regexp from file */ vregex * vr_thaw(FILE *fp) { vregex *r; v_thaw_start(); if (!v_thaw_follow(fp, V_TOKEN_STRING, "regexp string")) goto fail; if ((r = vr_create(v_thaw_svalue)) == NULL) { v_thaw_err("invalid regexp: %s (%s)", v_thaw_svalue, vr_error()); goto fail; } v_thaw_finish(); return r; fail: v_thaw_finish(); return NULL; } /* Traverse a regexp */ int vr_traverse(vregex *r, int (*func)(void *ptr)) { int val; VR_CHECK(r); if ((val = func(r)) != 0) return val; v_push_traverse(r); v_pop_traverse(); return 0; } /* Write regexp to a stream */ int vr_write(vregex *r, FILE *fp) { VR_CHECK(r); return v_write_string(r->name, fp); }