Return a Buf object from VarGetPattern() instead of a char * and a size_t.
Store a Buf object in struct VarPattern instead of a char * and a length. Patch: 7.158 Submitted by: Max Okumoto <okumoto@ucsd.edu>
This commit is contained in:
parent
ce873d22d5
commit
b6c44471ba
@ -681,9 +681,8 @@ SortIncreasing(const void *l, const void *r)
|
||||
* None.
|
||||
*-----------------------------------------------------------------------
|
||||
*/
|
||||
static char *
|
||||
VarGetPattern(VarParser *vp, int delim, int *flags,
|
||||
size_t *length, VarPattern *patt)
|
||||
static Buffer *
|
||||
VarGetPattern(VarParser *vp, int delim, int *flags, VarPattern *patt)
|
||||
{
|
||||
Buffer *buf;
|
||||
|
||||
@ -696,11 +695,7 @@ VarGetPattern(VarParser *vp, int delim, int *flags,
|
||||
*/
|
||||
while (*vp->ptr != '\0') {
|
||||
if (*vp->ptr == delim) {
|
||||
char *result;
|
||||
|
||||
result = (char *)Buf_GetAll(buf, length);
|
||||
Buf_Destroy(buf, FALSE);
|
||||
return (result);
|
||||
return (buf);
|
||||
|
||||
} else if ((vp->ptr[0] == '\\') &&
|
||||
((vp->ptr[1] == delim) ||
|
||||
@ -743,7 +738,7 @@ VarGetPattern(VarParser *vp, int delim, int *flags,
|
||||
vp->ptr += len;
|
||||
}
|
||||
} else if (vp->ptr[0] == '&' && patt != NULL) {
|
||||
Buf_AddBytes(buf, patt->leftLen, (Byte *)patt->lhs);
|
||||
Buf_AppendBuf(buf, patt->lhs);
|
||||
vp->ptr++;
|
||||
} else {
|
||||
Buf_AddByte(buf, (Byte)vp->ptr[0]);
|
||||
@ -751,9 +746,6 @@ VarGetPattern(VarParser *vp, int delim, int *flags,
|
||||
}
|
||||
}
|
||||
|
||||
if (length != NULL) {
|
||||
*length = 0;
|
||||
}
|
||||
Buf_Destroy(buf, TRUE);
|
||||
return (NULL);
|
||||
}
|
||||
@ -927,7 +919,7 @@ modifier_S(VarParser *vp, const char value[], Var *v)
|
||||
vp->ptr++;
|
||||
}
|
||||
|
||||
patt.lhs = VarGetPattern(vp, delim, &patt.flags, &patt.leftLen, NULL);
|
||||
patt.lhs = VarGetPattern(vp, delim, &patt.flags, NULL);
|
||||
if (patt.lhs == NULL) {
|
||||
/*
|
||||
* LHS didn't end with the delim, complain and exit.
|
||||
@ -938,7 +930,7 @@ modifier_S(VarParser *vp, const char value[], Var *v)
|
||||
|
||||
vp->ptr++; /* consume 2nd delim */
|
||||
|
||||
patt.rhs = VarGetPattern(vp, delim, NULL, &patt.rightLen, &patt);
|
||||
patt.rhs = VarGetPattern(vp, delim, NULL, &patt);
|
||||
if (patt.rhs == NULL) {
|
||||
/*
|
||||
* RHS didn't end with the delim, complain and exit.
|
||||
@ -965,7 +957,7 @@ modifier_S(VarParser *vp, const char value[], Var *v)
|
||||
* can only contain the 3 bits we're interested in so we don't have
|
||||
* to mask unrelated bits. We can test for equality.
|
||||
*/
|
||||
if (patt.leftLen == 0 && patt.flags == VAR_SUB_GLOBAL)
|
||||
if (Buf_Size(patt.lhs) == 0 && patt.flags == VAR_SUB_GLOBAL)
|
||||
Fatal("Global substitution of the empty string");
|
||||
|
||||
newValue = VarModify(value, VarSubstitute, &patt);
|
||||
@ -995,7 +987,7 @@ modifier_C(VarParser *vp, char value[], Var *v)
|
||||
|
||||
vp->ptr++; /* consume 1st delim */
|
||||
|
||||
patt.lhs = VarGetPattern(vp, delim, NULL, NULL, NULL);
|
||||
patt.lhs = VarGetPattern(vp, delim, NULL, NULL);
|
||||
if (patt.lhs == NULL) {
|
||||
Fatal("Unclosed substitution for %s (%c missing)",
|
||||
v->name, delim);
|
||||
@ -1003,7 +995,7 @@ modifier_C(VarParser *vp, char value[], Var *v)
|
||||
|
||||
vp->ptr++; /* consume 2st delim */
|
||||
|
||||
patt.rhs = VarGetPattern(vp, delim, NULL, NULL, NULL);
|
||||
patt.rhs = VarGetPattern(vp, delim, NULL, NULL);
|
||||
if (patt.rhs == NULL) {
|
||||
Fatal("Unclosed substitution for %s (%c missing)",
|
||||
v->name, delim);
|
||||
@ -1024,7 +1016,7 @@ modifier_C(VarParser *vp, char value[], Var *v)
|
||||
break;
|
||||
}
|
||||
|
||||
error = regcomp(&patt.re, patt.lhs, REG_EXTENDED);
|
||||
error = regcomp(&patt.re, Buf_Data(patt.lhs), REG_EXTENDED);
|
||||
if (error) {
|
||||
VarREError(error, &patt.re, "RE substitution error");
|
||||
free(patt.rhs);
|
||||
@ -1091,14 +1083,14 @@ sysVvarsub(VarParser *vp, char startc, Var *v, const char value[])
|
||||
/*
|
||||
* Now we break this sucker into the lhs and rhs.
|
||||
*/
|
||||
patt.lhs = VarGetPattern(vp, '=', &patt.flags, &patt.leftLen, NULL);
|
||||
patt.lhs = VarGetPattern(vp, '=', &patt.flags, NULL);
|
||||
if (patt.lhs == NULL) {
|
||||
Fatal("Unclosed substitution for %s (%c missing)",
|
||||
v->name, '=');
|
||||
}
|
||||
vp->ptr++; /* consume '=' */
|
||||
|
||||
patt.rhs = VarGetPattern(vp, endc, NULL, &patt.rightLen, &patt);
|
||||
patt.rhs = VarGetPattern(vp, endc, NULL, &patt);
|
||||
if (patt.rhs == NULL) {
|
||||
Fatal("Unclosed substitution for %s (%c missing)",
|
||||
v->name, endc);
|
||||
|
@ -73,10 +73,8 @@ typedef struct Var {
|
||||
#define VAR_MATCH_END 0x10 /* Match at end of word */
|
||||
|
||||
typedef struct {
|
||||
char *lhs; /* String to match */
|
||||
size_t leftLen; /* Length of string */
|
||||
char *rhs; /* Replacement string (w/ &'s removed) */
|
||||
size_t rightLen; /* Length of replacement */
|
||||
struct Buffer *lhs; /* String to match */
|
||||
struct Buffer *rhs; /* Replacement string (w/ &'s removed) */
|
||||
|
||||
regex_t re;
|
||||
int nsub;
|
||||
|
@ -249,8 +249,8 @@ VarSYSVMatch(const char *word, Boolean addSpace, Buffer *buf, void *patp)
|
||||
|
||||
addSpace = TRUE;
|
||||
|
||||
if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL)
|
||||
Str_SYSVSubst(buf, pat->rhs, ptr, len);
|
||||
if ((ptr = Str_SYSVMatch(word, Buf_Data(pat->lhs), &len)) != NULL)
|
||||
Str_SYSVSubst(buf, Buf_Data(pat->rhs), ptr, len);
|
||||
else
|
||||
Buf_Append(buf, word);
|
||||
|
||||
@ -318,24 +318,23 @@ VarSubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
||||
* and if none of them fits, perform the general substitution case.
|
||||
*/
|
||||
if ((pattern->flags & VAR_MATCH_START) &&
|
||||
(strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
|
||||
(strncmp(word, Buf_Data(pattern->lhs), Buf_Size(pattern->lhs)) == 0)) {
|
||||
/*
|
||||
* Anchored at start and beginning of word matches pattern
|
||||
*/
|
||||
if ((pattern->flags & VAR_MATCH_END) &&
|
||||
(wordLen == pattern->leftLen)) {
|
||||
(wordLen == Buf_Size(pattern->lhs))) {
|
||||
/*
|
||||
* Also anchored at end and matches to the end (word
|
||||
* is same length as pattern) add space and rhs only
|
||||
* if rhs is non-null.
|
||||
*/
|
||||
if (pattern->rightLen != 0) {
|
||||
if (Buf_Size(pattern->rhs) != 0) {
|
||||
if (addSpace) {
|
||||
Buf_AddByte(buf, (Byte)' ');
|
||||
}
|
||||
addSpace = TRUE;
|
||||
Buf_AddBytes(buf, pattern->rightLen,
|
||||
(Byte *)pattern->rhs);
|
||||
Buf_AppendBuf(buf, pattern->rhs);
|
||||
}
|
||||
} else if (pattern->flags & VAR_MATCH_END) {
|
||||
/*
|
||||
@ -346,15 +345,15 @@ VarSubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
||||
/*
|
||||
* Matches at start but need to copy in trailing characters
|
||||
*/
|
||||
if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
|
||||
if ((Buf_Size(pattern->rhs) + wordLen - Buf_Size(pattern->lhs)) != 0){
|
||||
if (addSpace) {
|
||||
Buf_AddByte(buf, (Byte)' ');
|
||||
}
|
||||
addSpace = TRUE;
|
||||
}
|
||||
Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
|
||||
Buf_AddBytes(buf, wordLen - pattern->leftLen,
|
||||
(const Byte *)(word + pattern->leftLen));
|
||||
Buf_AppendBuf(buf, pattern->rhs);
|
||||
Buf_AddBytes(buf, wordLen - Buf_Size(pattern->lhs),
|
||||
(word + Buf_Size(pattern->lhs)));
|
||||
}
|
||||
} else if (pattern->flags & VAR_MATCH_START) {
|
||||
/*
|
||||
@ -368,23 +367,23 @@ VarSubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
||||
* that because the $ will be left at the end of the lhs, we have
|
||||
* to use strncmp.
|
||||
*/
|
||||
cp = word + (wordLen - pattern->leftLen);
|
||||
cp = word + (wordLen - Buf_Size(pattern->lhs));
|
||||
if ((cp >= word) &&
|
||||
(strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
|
||||
(strncmp(cp, Buf_Data(pattern->lhs), Buf_Size(pattern->lhs)) == 0)) {
|
||||
/*
|
||||
* Match found. If we will place characters in the buffer,
|
||||
* add a space before hand as indicated by addSpace, then
|
||||
* stuff in the initial, unmatched part of the word followed
|
||||
* by the right-hand-side.
|
||||
*/
|
||||
if (((cp - word) + pattern->rightLen) != 0) {
|
||||
if (((cp - word) + Buf_Size(pattern->rhs)) != 0) {
|
||||
if (addSpace) {
|
||||
Buf_AddByte(buf, (Byte)' ');
|
||||
}
|
||||
addSpace = TRUE;
|
||||
}
|
||||
Buf_AppendRange(buf, word, cp);
|
||||
Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
|
||||
Buf_AppendBuf(buf, pattern->rhs);
|
||||
} else {
|
||||
/*
|
||||
* Had to match at end and didn't. Copy entire word.
|
||||
@ -409,16 +408,16 @@ VarSubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
||||
done = FALSE;
|
||||
origSize = Buf_Size(buf);
|
||||
while (!done) {
|
||||
cp = strstr(word, pattern->lhs);
|
||||
cp = strstr(word, Buf_Data(pattern->lhs));
|
||||
if (cp != NULL) {
|
||||
if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
|
||||
if (addSpace && (((cp - word) + Buf_Size(pattern->rhs)) != 0)){
|
||||
Buf_AddByte(buf, (Byte)' ');
|
||||
addSpace = FALSE;
|
||||
}
|
||||
Buf_AppendRange(buf, word, cp);
|
||||
Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
|
||||
wordLen -= (cp - word) + pattern->leftLen;
|
||||
word = cp + pattern->leftLen;
|
||||
Buf_AppendBuf(buf, pattern->rhs);
|
||||
wordLen -= (cp - word) + Buf_Size(pattern->lhs);
|
||||
word = cp + Buf_Size(pattern->lhs);
|
||||
if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0){
|
||||
done = TRUE;
|
||||
}
|
||||
@ -502,7 +501,7 @@ VarRESubstitute(const char *word, Boolean addSpace, Buffer *buf, void *patternp)
|
||||
Buf_AddBytes(buf, pat->matches[0].rm_so, (const Byte *)wp);
|
||||
}
|
||||
|
||||
for (rp = pat->rhs; *rp; rp++) {
|
||||
for (rp = Buf_Data(pat->rhs); *rp; rp++) {
|
||||
if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
|
||||
MAYBE_ADD_SPACE();
|
||||
Buf_AddByte(buf, (Byte)rp[1]);
|
||||
|
Loading…
Reference in New Issue
Block a user