2009-04-07 11:33:22 +05:30
|
|
|
/*
|
|
|
|
* ##/%% variable matching code ripped out of ash shell for code sharing
|
|
|
|
*
|
2010-01-13 02:41:24 +05:30
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2010-01-13 02:41:24 +05:30
|
|
|
*
|
2009-04-07 11:33:22 +05:30
|
|
|
* Copyright (c) 1989, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
* was re-ported from NetBSD and debianized.
|
|
|
|
*/
|
|
|
|
#ifdef STANDALONE
|
|
|
|
# include <stdbool.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <unistd.h>
|
2010-09-05 00:51:07 +05:30
|
|
|
# define FAST_FUNC /* nothing */
|
|
|
|
# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */
|
|
|
|
# define POP_SAVED_FUNCTION_VISIBILITY /* nothing */
|
2009-04-07 11:33:22 +05:30
|
|
|
#else
|
2009-04-09 19:45:57 +05:30
|
|
|
# include "libbb.h"
|
2009-04-07 11:33:22 +05:30
|
|
|
#endif
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include "match.h"
|
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
char* FAST_FUNC scan_and_match(char *string, const char *pattern, unsigned flags)
|
2009-04-07 11:33:22 +05:30
|
|
|
{
|
2010-09-05 00:51:07 +05:30
|
|
|
char *loc;
|
|
|
|
char *end;
|
|
|
|
unsigned len = strlen(string);
|
|
|
|
int early_exit;
|
|
|
|
|
|
|
|
/* We can stop the scan early only if the string part
|
|
|
|
* we are matching against is shrinking, and the pattern has
|
|
|
|
* an unquoted "star" at the corresponding end. There are two cases.
|
|
|
|
* Case 1:
|
|
|
|
* "qwerty" does not match against pattern "*zy",
|
|
|
|
* no point in trying to match "werty", "erty" etc:
|
|
|
|
*/
|
|
|
|
early_exit = (flags == (SCAN_MOVE_FROM_LEFT + SCAN_MATCH_RIGHT_HALF) && pattern[0] == '*');
|
|
|
|
|
|
|
|
if (flags & SCAN_MOVE_FROM_LEFT) {
|
|
|
|
loc = string;
|
|
|
|
end = string + len + 1;
|
|
|
|
} else {
|
|
|
|
loc = string + len;
|
|
|
|
end = string - 1;
|
|
|
|
if (flags == (SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF)) {
|
|
|
|
/* Case 2:
|
|
|
|
* "qwerty" does not match against pattern "qz*",
|
|
|
|
* no point in trying to match "qwert", "qwer" etc:
|
|
|
|
*/
|
|
|
|
const char *p = pattern + strlen(pattern);
|
|
|
|
if (--p >= pattern && *p == '*') {
|
|
|
|
early_exit = 1;
|
|
|
|
while (--p >= pattern && *p == '\\')
|
|
|
|
early_exit ^= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
while (loc != end) {
|
2010-09-12 18:36:42 +05:30
|
|
|
int r;
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
if (flags & SCAN_MATCH_LEFT_HALF) {
|
2021-07-27 21:23:55 +05:30
|
|
|
char c = *loc;
|
2009-04-07 11:33:22 +05:30
|
|
|
*loc = '\0';
|
2010-09-12 18:36:42 +05:30
|
|
|
r = fnmatch(pattern, string, 0);
|
2018-03-02 22:42:12 +05:30
|
|
|
//bb_error_msg("fnmatch('%s','%s',0):%d", pattern, string, r);
|
2010-09-04 23:22:44 +05:30
|
|
|
*loc = c;
|
|
|
|
} else {
|
2010-09-12 18:36:42 +05:30
|
|
|
r = fnmatch(pattern, loc, 0);
|
2018-03-03 01:18:36 +05:30
|
|
|
//bb_error_msg("fnmatch('%s','%s',0):%d", pattern, loc, r);
|
2010-09-04 23:22:44 +05:30
|
|
|
}
|
2010-09-12 18:36:42 +05:30
|
|
|
if (r == 0) /* match found */
|
2009-04-07 11:33:22 +05:30
|
|
|
return loc;
|
2010-09-05 00:51:07 +05:30
|
|
|
if (early_exit) {
|
|
|
|
#ifdef STANDALONE
|
|
|
|
printf("(early exit) ");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
if (flags & SCAN_MOVE_FROM_LEFT) {
|
|
|
|
loc++;
|
2010-09-04 23:22:44 +05:30
|
|
|
} else {
|
2010-09-05 00:51:07 +05:30
|
|
|
loc--;
|
2010-09-04 23:22:44 +05:30
|
|
|
}
|
2009-04-07 11:33:22 +05:30
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
2022-05-01 20:36:00 +05:30
|
|
|
int main(int argc, char **argv)
|
2009-04-07 11:33:22 +05:30
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
char *op;
|
|
|
|
char *pattern;
|
|
|
|
char *loc;
|
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
if (!argv[1]) {
|
2009-04-07 11:33:22 +05:30
|
|
|
puts(
|
|
|
|
"Usage: match <test> [test...]\n\n"
|
|
|
|
"Where a <test> is the form: <string><op><match>\n"
|
|
|
|
"This is to test the shell ${var#val} expression type.\n\n"
|
|
|
|
"e.g. `match 'abc#a*'` -> bc"
|
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
while (*++argv) {
|
2009-04-07 11:33:22 +05:30
|
|
|
size_t off;
|
2010-09-05 00:51:07 +05:30
|
|
|
unsigned scan_flags;
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
string = *argv;
|
2009-04-07 11:33:22 +05:30
|
|
|
off = strcspn(string, "#%");
|
|
|
|
if (!off) {
|
|
|
|
printf("invalid format\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
op = string + off;
|
2010-09-05 00:51:07 +05:30
|
|
|
scan_flags = pick_scan(op[0], op[1]);
|
|
|
|
|
|
|
|
printf("'%s': flags:%x, ", string, scan_flags);
|
2009-04-07 11:33:22 +05:30
|
|
|
pattern = op + 1;
|
|
|
|
if (op[0] == op[1])
|
2010-09-05 00:51:07 +05:30
|
|
|
pattern++;
|
2009-04-07 11:33:22 +05:30
|
|
|
op[0] = '\0';
|
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
loc = scan_and_match(string, pattern, scan_flags);
|
2009-04-07 11:33:22 +05:30
|
|
|
|
2010-09-05 00:51:07 +05:30
|
|
|
if (scan_flags & SCAN_MATCH_LEFT_HALF) {
|
2009-04-07 11:33:22 +05:30
|
|
|
printf("'%s'\n", loc);
|
|
|
|
} else {
|
2010-09-05 00:51:07 +05:30
|
|
|
if (loc)
|
|
|
|
*loc = '\0';
|
2009-04-07 11:33:22 +05:30
|
|
|
printf("'%s'\n", string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|