2 * ##/%% variable matching code ripped out of ash shell for code sharing
4 * This code is derived from software contributed to Berkeley by
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 * Copyright (c) 1989, 1991, 1993, 1994
10 * The Regents of the University of California. All rights reserved.
12 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
13 * was re-ported from NetBSD and debianized.
27 #define pmatch(a, b) !fnmatch((a), (b), 0)
29 char *scanleft(char *string, char *pattern, bool match_at_left)
40 match = pmatch(pattern, string);
43 match = pmatch(pattern, loc);
53 char *scanright(char *string, char *pattern, bool match_at_left)
56 char *loc = string + strlen(string);
58 while (loc >= string) {
64 match = pmatch(pattern, string);
67 match = pmatch(pattern, loc);
78 int main(int argc, char *argv[])
90 "Usage: match <test> [test...]\n\n"
91 "Where a <test> is the form: <string><op><match>\n"
92 "This is to test the shell ${var#val} expression type.\n\n"
93 "e.g. `match 'abc#a*'` -> bc"
98 for (i = 1; i < argc; ++i) {
102 printf("'%s': ", argv[i]);
104 string = strdup(argv[i]);
105 off = strcspn(string, "#%");
107 printf("invalid format\n");
112 scan = pick_scan(op[0], op[1], &match_at_left);
115 op[1] = '\0', ++pattern;
118 loc = scan(string, pattern, match_at_left);
121 printf("'%s'\n", loc);
124 printf("'%s'\n", string);