strmatch.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* D. G. Korn
* G. S. Fowler
* AT&T Research
*
* match shell file patterns
* this interface is a wrapper on regex
*
* sh pattern egrep RE description
* ---------- -------- -----------
* * .* 0 or more chars
* ? . any single char
* [.] [.] char class
* [!.] [^.] negated char class
* [[:.:]] [[:.:]] ctype class
* [[=.=]] [[=.=]] equivalence class
* [[...]] [[...]] collation element
* *(.) (.)* 0 or more of
* +(.) (.)+ 1 or more of
* ?(.) (.)? 0 or 1 of
* (.) (.) 1 of
* @(.) (.) 1 of
* a|b a|b a or b
* \# () subgroup back reference [1-9]
* a&b a and b
* !(.) none of
*
* \ used to escape metacharacters
*
* *, ?, (, |, &, ), [, \ must be \'d outside of [...]
* only ] must be \'d inside [...]
*
*/
#include <ast.h>
#include <regex.h>
static struct State_s
{
int nmatch;
} matchstate;
#define STR_INT 040000
/*
* subgroup match
* 0 returned if no match
* otherwise number of subgroups matched returned
* match group begin offsets are even elements of sub
* match group end offsets are odd elements of sub
* the matched string is from s+sub[0] up to but not
* including s+sub[1]
*/
int
{
register int i;
register regflags_t reflags;
/*
* 0 and empty patterns are special
*/
if (!p || !b)
{
if (!p && !b)
return 0;
}
if (!*p)
{
if (sub && n > 0)
{
{
}
else
}
return *b == 0;
}
/*
* convert flags
*/
if (flags & REG_ADVANCE)
else
{
if (!(flags & STR_MAXIMAL))
reflags |= REG_MINIMAL;
}
if (!sub || n <= 0)
return 0;
if (n > matchstate.nmatch)
{
return 0;
matchstate.nmatch = n;
}
if (regexec(re, b, n, matchstate.match, reflags & ~(REG_MINIMAL|REG_SHELL_GROUP|REG_LEFT|REG_RIGHT|REG_ICASE)))
return 0;
if (!sub || n <= 0)
return 1;
{
{
}
}
else
{
{
}
}
return i + 1;
}
/*
* compare the string s with the shell pattern p
* returns 1 for match 0 otherwise
*/
int
strmatch(const char* s, const char* p)
{
}
/*
* leading substring match
* first char after end of substring returned
* 0 returned if no match
*
* OBSOLETE: use strgrpmatch()
*/
char*
strsubmatch(const char* s, const char* p, int flags)
{
return strgrpmatch(s, p, match, 1, (flags ? STR_MAXIMAL : 0)|STR_LEFT) ? (char*)s + match[1] : (char*)0;
}
#if _map_libc
#define strgrpmatch _ast_strgrpmatch
#endif
int
{
}