Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ##/%% variable matching code ripped out of ash shell for code sharing |
| 3 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 4 | * This code is derived from software contributed to Berkeley by |
| 5 | * Kenneth Almquist. |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 8 | * |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 9 | * Copyright (c) 1989, 1991, 1993, 1994 |
| 10 | * The Regents of the University of California. All rights reserved. |
| 11 | * |
| 12 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
| 13 | * was re-ported from NetBSD and debianized. |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 14 | */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 15 | #ifdef STANDALONE |
| 16 | # include <stdbool.h> |
| 17 | # include <stdio.h> |
| 18 | # include <stdlib.h> |
| 19 | # include <string.h> |
| 20 | # include <unistd.h> |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 21 | # define FAST_FUNC /* nothing */ |
| 22 | # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */ |
| 23 | # define POP_SAVED_FUNCTION_VISIBILITY /* nothing */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 24 | #else |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 25 | # include "libbb.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 26 | #endif |
| 27 | #include <fnmatch.h> |
| 28 | #include "match.h" |
| 29 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 30 | char* FAST_FUNC scan_and_match(char *string, const char *pattern, unsigned flags) |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 31 | { |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 32 | char *loc; |
| 33 | char *end; |
| 34 | unsigned len = strlen(string); |
| 35 | int early_exit; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 36 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 37 | /* We can stop the scan early only if the string part |
| 38 | * we are matching against is shrinking, and the pattern has |
| 39 | * an unquoted "star" at the corresponding end. There are two cases. |
| 40 | * Case 1: |
| 41 | * "qwerty" does not match against pattern "*zy", |
| 42 | * no point in trying to match "werty", "erty" etc: |
| 43 | */ |
| 44 | early_exit = (flags == (SCAN_MOVE_FROM_LEFT + SCAN_MATCH_RIGHT_HALF) && pattern[0] == '*'); |
| 45 | |
| 46 | if (flags & SCAN_MOVE_FROM_LEFT) { |
| 47 | loc = string; |
| 48 | end = string + len + 1; |
| 49 | } else { |
| 50 | loc = string + len; |
| 51 | end = string - 1; |
| 52 | if (flags == (SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF)) { |
| 53 | /* Case 2: |
| 54 | * "qwerty" does not match against pattern "qz*", |
| 55 | * no point in trying to match "qwert", "qwer" etc: |
| 56 | */ |
| 57 | const char *p = pattern + strlen(pattern); |
| 58 | if (--p >= pattern && *p == '*') { |
| 59 | early_exit = 1; |
| 60 | while (--p >= pattern && *p == '\\') |
| 61 | early_exit ^= 1; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | while (loc != end) { |
| 67 | char c; |
Denys Vlasenko | 2d8187c | 2010-09-12 15:06:42 +0200 | [diff] [blame] | 68 | int r; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 69 | |
| 70 | c = *loc; |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 71 | if (flags & SCAN_MATCH_LEFT_HALF) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 72 | *loc = '\0'; |
Denys Vlasenko | 2d8187c | 2010-09-12 15:06:42 +0200 | [diff] [blame] | 73 | r = fnmatch(pattern, string, 0); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 74 | //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, string, r); |
Denys Vlasenko | e298ce6 | 2010-09-04 19:52:44 +0200 | [diff] [blame] | 75 | *loc = c; |
| 76 | } else { |
Denys Vlasenko | 2d8187c | 2010-09-12 15:06:42 +0200 | [diff] [blame] | 77 | r = fnmatch(pattern, loc, 0); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 78 | //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, loc, r); |
Denys Vlasenko | e298ce6 | 2010-09-04 19:52:44 +0200 | [diff] [blame] | 79 | } |
Denys Vlasenko | 2d8187c | 2010-09-12 15:06:42 +0200 | [diff] [blame] | 80 | if (r == 0) /* match found */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 81 | return loc; |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 82 | if (early_exit) { |
| 83 | #ifdef STANDALONE |
| 84 | printf("(early exit) "); |
| 85 | #endif |
| 86 | break; |
Denys Vlasenko | e298ce6 | 2010-09-04 19:52:44 +0200 | [diff] [blame] | 87 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 88 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 89 | if (flags & SCAN_MOVE_FROM_LEFT) { |
| 90 | loc++; |
| 91 | } else { |
| 92 | loc--; |
| 93 | } |
| 94 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | #ifdef STANDALONE |
| 99 | int main(int argc, char *argv[]) |
| 100 | { |
| 101 | char *string; |
| 102 | char *op; |
| 103 | char *pattern; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 104 | char *loc; |
| 105 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 106 | setvbuf(stdout, NULL, _IONBF, 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 107 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 108 | if (!argv[1]) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 109 | puts( |
| 110 | "Usage: match <test> [test...]\n\n" |
| 111 | "Where a <test> is the form: <string><op><match>\n" |
| 112 | "This is to test the shell ${var#val} expression type.\n\n" |
| 113 | "e.g. `match 'abc#a*'` -> bc" |
| 114 | ); |
| 115 | return 1; |
| 116 | } |
| 117 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 118 | while (*++argv) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 119 | size_t off; |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 120 | unsigned scan_flags; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 121 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 122 | string = *argv; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 123 | off = strcspn(string, "#%"); |
| 124 | if (!off) { |
| 125 | printf("invalid format\n"); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | op = string + off; |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 129 | scan_flags = pick_scan(op[0], op[1]); |
| 130 | |
| 131 | printf("'%s': flags:%x, ", string, scan_flags); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 132 | pattern = op + 1; |
| 133 | if (op[0] == op[1]) |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 134 | pattern++; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 135 | op[0] = '\0'; |
| 136 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 137 | loc = scan_and_match(string, pattern, scan_flags); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 138 | |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 139 | if (scan_flags & SCAN_MATCH_LEFT_HALF) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 140 | printf("'%s'\n", loc); |
| 141 | } else { |
Denys Vlasenko | 701e127 | 2010-09-04 21:21:07 +0200 | [diff] [blame] | 142 | if (loc) |
| 143 | *loc = '\0'; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 144 | printf("'%s'\n", string); |
| 145 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | #endif |