-/* $OpenBSD: eval.c,v 1.51 2017/05/01 19:05:49 millert Exp $ */
+/* $OpenBSD: eval.c,v 1.52 2017/07/04 07:29:32 anton Exp $ */
/*
* Expansion - quoting, separation, substitution, globbing
static int varsub(Expand *, char *, char *, int *, int *);
static int comsub(Expand *, char *);
+static char *strsub(char *, char *, int);
static char *trimsub(char *, char *, int);
static void glob(char *, XPtrV *, int);
static void globit(XString *, char **, char *, XPtrV *, int);
short f; /* saved value of f (DOPAT, etc) */
struct tbl *var; /* variable for ${var..} */
short quote; /* saved value of quote (for ${..[%#]..}) */
+ int strsub; /* set to 1 if pat in /pat/rep has been ended */
struct SubType *prev; /* old type */
struct SubType *next; /* poped type (to avoid re-allocating) */
} SubType;
break;
case CHAR:
c = *sp++;
+ if (st->strsub == 0 &&
+ (st->stype & 0x7f) == '/' && c == '/') {
+ st->strsub = 1;
+ /* Write end of pattern. */
+ *dp++ = MAGIC;
+ *dp++ = ')';
+ *dp++ = '\0';
+ /*
+ * Reset quote and flags for the
+ * upcoming replacement.
+ */
+ quote = 0;
+ f = 0;
+ continue;
+ }
break;
case QCHAR:
quote |= 2; /* temporary quote */
switch (stype & 0x7f) {
case '#':
case '%':
+ case '/':
/* ! DOBLANK,DOBRACE_,DOTILDE */
f = DOPAT | (f&DONTRUNCOMMAND) |
DOTEMP_;
case '%':
/* Append end-pattern */
*dp++ = MAGIC; *dp++ = ')'; *dp = '\0';
+ /* FALLTHROUGH */
+ case '/':
dp = Xrestpos(ds, dp, st->base);
/* Must use st->var since calling
* global would break things
* like x[i+=1].
*/
- x.str = trimsub(str_val(st->var),
- dp, st->stype);
+ if ((st->stype & 0x7f) == '/')
+ x.str = strsub(str_val(st->var),
+ dp, st->stype);
+ else
+ x.str = trimsub(
+ str_val(st->var),
+ dp, st->stype);
if (x.str[0] != '\0' || st->quote)
type = XSUB;
else
stype = 0x80;
c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
}
+ if (c == '/' && Flag(FPOSIX))
+ return -1;
+
if (ctype(c, C_SUBOP1)) {
slen += 2;
stype |= c;
return XCOM;
}
+static char *
+strsub(char *str, char *pat, int how)
+{
+ char *actpat, *dst, *prepat, *rep, *src;
+ size_t beg, dstlen, dstsiz, end, match, len, patlen, replen;
+
+ len = strlen(str);
+ if (len == 0)
+ return str;
+ src = str;
+
+ dstlen = 0;
+ dstsiz = len + 1; /* NUL */
+ dst = alloc(dstsiz, ATEMP);
+
+ actpat = pat;
+ patlen = strlen(actpat) + 1; /* NUL */
+ prepat = alloc(patlen + 2, ATEMP); /* make room for wildcard */
+ /*
+ * Copy actpat to prepat and add a wildcard after the open pattern
+ * prefix.
+ */
+ memcpy(prepat, actpat, 2);
+ prepat[2] = MAGIC;
+ prepat[3] = '*';
+ memcpy(&prepat[4], &actpat[2], patlen - 2);
+
+ rep = &actpat[patlen];
+ replen = strlen(rep);
+
+ for (;;) {
+ /*
+ * Find the wildcard prefix in prepat followed by actpat.
+ * This allows occurrences of actpat to be found anywhere in the
+ * string.
+ */
+ match = 0;
+ for (end = 1; end <= len; end++)
+ if (gnmatch(src, end, prepat, 0))
+ match = end;
+ else if (match)
+ break;
+ if (!match)
+ break;
+ end = match;
+
+ /*
+ * Find the prefix, if any, that was matched by the wildcard in
+ * prepat.
+ */
+ match = 0;
+ for (beg = 0; beg < end; beg++)
+ if ((match = gnmatch(src + beg, end - beg, actpat, 0)))
+ break;
+
+ /*
+ * At this point, [src, beg) contains the prefix that is present
+ * before the actual pattern and [beg, end) what was matched by
+ * the actual pattern.
+ * The first range will be copied over to dst and the latter
+ * replaced with rep.
+ */
+ if (match && beg > 0) {
+ if (beg + dstlen >= dstsiz) {
+ dst = areallocarray(dst, 1, dstsiz + beg + 1,
+ ATEMP);
+ dstsiz += beg + 1;
+ }
+ memcpy(&dst[dstlen], src, beg);
+ dstlen += beg;
+ }
+
+ if (replen + dstlen >= dstsiz) {
+ dst = areallocarray(dst, 1, dstsiz + replen + 1, ATEMP);
+ dstsiz += replen + 1;
+ }
+ memcpy(&dst[dstlen], rep, replen);
+ dstlen += replen;
+
+ src += end;
+ len -= end;
+ if (len == 0 || how == '/')
+ break;
+ }
+
+ afree(prepat, ATEMP);
+
+ if (str == src) {
+ /* No substitutions performed. */
+ afree(dst, ATEMP);
+
+ return str;
+ }
+
+ /* Copy unmatched suffix from src. */
+ if (len > 0) {
+ if (len + dstlen >= dstsiz) {
+ dst = areallocarray(dst, 1, dstsiz + len + 1, ATEMP);
+ dstsiz += len + 1;
+ }
+ memcpy(&dst[dstlen], src, len);
+ dstlen += len;
+ }
+ dst[dstlen] = '\0';
+
+ return dst;
+}
+
/*
* perform #pattern and %pattern substitution in ${}
*/
-/* $OpenBSD: misc.c,v 1.55 2016/03/20 00:01:21 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.56 2017/07/04 07:29:32 anton Exp $ */
/*
* Miscellaneous functions
setctypes("*@#!$-?", C_VAR1);
setctypes(" \t\n", C_IFSWS);
setctypes("=-+?", C_SUBOP1);
- setctypes("#%", C_SUBOP2);
+ setctypes("#%/", C_SUBOP2);
setctypes(" \n\t\"#$&'()*;<>?[\\`|", C_QUOTE);
}
(const unsigned char *) p, (const unsigned char *) pe);
}
+int
+gnmatch(char *s, size_t n, const char *p, int isfile)
+{
+ int c, match;
+
+ c = s[n];
+ s[n] = '\0';
+ match = gmatch(s, p, isfile);
+ s[n] = c;
+
+ return match;
+}
+
/* Returns if p is a syntacticly correct globbing pattern, false
* if it contains no pattern characters or if there is a syntax error.
* Syntax errors are:
-/* $OpenBSD: sh.h,v 1.59 2017/06/29 16:49:58 martijn Exp $ */
+/* $OpenBSD: sh.h,v 1.60 2017/07/04 07:29:32 anton Exp $ */
/*
* Public Domain Bourne/Korn shell
#define C_VAR1 BIT(3) /* *@#!$-? */
#define C_IFSWS BIT(4) /* \t \n (IFS white space) */
#define C_SUBOP1 BIT(5) /* "=-+?" */
-#define C_SUBOP2 BIT(6) /* "#%" */
+#define C_SUBOP2 BIT(6) /* "#%/" */
#define C_IFS BIT(7) /* $IFS */
#define C_QUOTE BIT(8) /* \n\t"#$&'()*;<>?[\`| (needing quoting) */
int getn(const char *, int *);
int bi_getn(const char *, int *);
int gmatch(const char *, const char *, int);
+int gnmatch(char *, size_t, const char *, int);
int has_globbing(const char *, const char *);
const unsigned char *pat_scan(const unsigned char *, const unsigned char *,
int);