[BACK]Return to regsub.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/regsub.c, Revision 1.2

1.2     ! nicm        1: /* $OpenBSD: regsub.c,v 1.1 2019/06/13 19:46:00 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <regex.h>
                     22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: static void
                     27: regsub_copy(char **buf, size_t *len, const char *text, size_t start,
                     28:     size_t end)
                     29: {
                     30:        size_t  add = end - start;
                     31:
                     32:        *buf = xrealloc(*buf, (*len) + add + 1);
                     33:        memcpy((*buf) + *len, text + start, add);
                     34:        (*len) += add;
                     35: }
                     36:
                     37: static void
                     38: regsub_expand(char **buf, size_t *len, const char *with, const char *text,
                     39:     regmatch_t *m, u_int n)
                     40: {
                     41:        const char      *cp;
                     42:        u_int            i;
                     43:
                     44:        for (cp = with; *cp != '\0'; cp++) {
                     45:                if (*cp == '\\') {
                     46:                        cp++;
                     47:                        if (*cp >= '0' && *cp <= '9') {
                     48:                                i = *cp - '0';
                     49:                                if (i < n && m[i].rm_so != m[i].rm_eo) {
                     50:                                        regsub_copy(buf, len, text, m[i].rm_so,
                     51:                                            m[i].rm_eo);
                     52:                                        continue;
                     53:                                }
                     54:                        }
                     55:                }
                     56:                *buf = xrealloc(*buf, (*len) + 2);
                     57:                (*buf)[(*len)++] = *cp;
                     58:        }
                     59: }
                     60:
                     61: char *
                     62: regsub(const char *pattern, const char *with, const char *text, int flags)
                     63: {
                     64:        regex_t          r;
                     65:        regmatch_t       m[10];
1.2     ! nicm       66:        ssize_t          start, end, last, len = 0;
        !            67:        int              empty = 0;
1.1       nicm       68:        char            *buf = NULL;
                     69:
                     70:        if (*text == '\0')
                     71:                return (xstrdup(""));
                     72:        if (regcomp(&r, pattern, flags) != 0)
                     73:                return (NULL);
                     74:
                     75:        start = 0;
1.2     ! nicm       76:        last = 0;
1.1       nicm       77:        end = strlen(text);
                     78:
1.2     ! nicm       79:        while (start <= end) {
1.1       nicm       80:                m[0].rm_so = start;
                     81:                m[0].rm_eo = end;
                     82:
                     83:                if (regexec(&r, text, nitems(m), m, REG_STARTEND) != 0) {
                     84:                        regsub_copy(&buf, &len, text, start, end);
                     85:                        break;
                     86:                }
1.2     ! nicm       87:
        !            88:                /*
        !            89:                 * Append any text not part of this match (from the end of the
        !            90:                 * last match).
        !            91:                 */
        !            92:                regsub_copy(&buf, &len, text, last, m[0].rm_so);
        !            93:
        !            94:                /*
        !            95:                 * If the last match was empty and this one isn't (it is either
        !            96:                 * later or has matched text), expand this match. If it is
        !            97:                 * empty, move on one character and try again from there.
        !            98:                 */
        !            99:                if (empty || m[0].rm_so != last || m[0].rm_so != m[0].rm_eo) {
        !           100:                        regsub_expand(&buf, &len, with, text, m, nitems(m));
        !           101:
        !           102:                        last = m[0].rm_eo;
        !           103:                        start = m[0].rm_eo;
        !           104:                        empty = 0;
        !           105:                } else {
        !           106:                        last = m[0].rm_eo;
        !           107:                        start = m[0].rm_eo + 1;
        !           108:                        empty = 1;
1.1       nicm      109:                }
                    110:        }
                    111:        buf[len] = '\0';
                    112:
                    113:        regfree(&r);
                    114:        return (buf);
                    115: }