blob: ee8abb2db2dddad97fa82c6e9f32d83bc576dfc9 [file] [log] [blame]
Mike Frysingera4f331d2009-04-07 06:03:22 +00001/*
2 * ##/%% variable matching code ripped out of ash shell for code sharing
3 *
Denys Vlasenko73067272010-01-12 22:11:24 +01004 * This code is derived from software contributed to Berkeley by
5 * Kenneth Almquist.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko73067272010-01-12 22:11:24 +01008 *
Mike Frysingera4f331d2009-04-07 06:03:22 +00009 * 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 Frysingera4f331d2009-04-07 06:03:22 +000014 */
Mike Frysingera4f331d2009-04-07 06:03:22 +000015#ifdef STANDALONE
16# include <stdbool.h>
17# include <stdio.h>
18# include <stdlib.h>
19# include <string.h>
20# include <unistd.h>
Denys Vlasenko701e1272010-09-04 21:21:07 +020021# define FAST_FUNC /* nothing */
22# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */
23# define POP_SAVED_FUNCTION_VISIBILITY /* nothing */
Mike Frysingera4f331d2009-04-07 06:03:22 +000024#else
Denis Vlasenko1943aec2009-04-09 14:15:57 +000025# include "libbb.h"
Mike Frysingera4f331d2009-04-07 06:03:22 +000026#endif
27#include <fnmatch.h>
28#include "match.h"
29
Denys Vlasenko701e1272010-09-04 21:21:07 +020030char* FAST_FUNC scan_and_match(char *string, const char *pattern, unsigned flags)
Mike Frysingera4f331d2009-04-07 06:03:22 +000031{
Denys Vlasenko701e1272010-09-04 21:21:07 +020032 char *loc;
33 char *end;
34 unsigned len = strlen(string);
35 int early_exit;
Mike Frysingera4f331d2009-04-07 06:03:22 +000036
Denys Vlasenko701e1272010-09-04 21:21:07 +020037 /* 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 Vlasenko2d8187c2010-09-12 15:06:42 +020068 int r;
Mike Frysingera4f331d2009-04-07 06:03:22 +000069
70 c = *loc;
Denys Vlasenko701e1272010-09-04 21:21:07 +020071 if (flags & SCAN_MATCH_LEFT_HALF) {
Mike Frysingera4f331d2009-04-07 06:03:22 +000072 *loc = '\0';
Denys Vlasenko2d8187c2010-09-12 15:06:42 +020073 r = fnmatch(pattern, string, 0);
Denys Vlasenko55f81332018-03-02 18:12:12 +010074 //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, string, r);
Denys Vlasenkoe298ce62010-09-04 19:52:44 +020075 *loc = c;
76 } else {
Denys Vlasenko2d8187c2010-09-12 15:06:42 +020077 r = fnmatch(pattern, loc, 0);
Denys Vlasenkod4802c62018-03-02 20:48:36 +010078 //bb_error_msg("fnmatch('%s','%s',0):%d", pattern, loc, r);
Denys Vlasenkoe298ce62010-09-04 19:52:44 +020079 }
Denys Vlasenko2d8187c2010-09-12 15:06:42 +020080 if (r == 0) /* match found */
Mike Frysingera4f331d2009-04-07 06:03:22 +000081 return loc;
Denys Vlasenko701e1272010-09-04 21:21:07 +020082 if (early_exit) {
83#ifdef STANDALONE
84 printf("(early exit) ");
85#endif
86 break;
Denys Vlasenkoe298ce62010-09-04 19:52:44 +020087 }
Mike Frysingera4f331d2009-04-07 06:03:22 +000088
Denys Vlasenko701e1272010-09-04 21:21:07 +020089 if (flags & SCAN_MOVE_FROM_LEFT) {
90 loc++;
91 } else {
92 loc--;
93 }
94 }
Mike Frysingera4f331d2009-04-07 06:03:22 +000095 return NULL;
96}
97
98#ifdef STANDALONE
99int main(int argc, char *argv[])
100{
101 char *string;
102 char *op;
103 char *pattern;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000104 char *loc;
105
Denys Vlasenko701e1272010-09-04 21:21:07 +0200106 setvbuf(stdout, NULL, _IONBF, 0);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000107
Denys Vlasenko701e1272010-09-04 21:21:07 +0200108 if (!argv[1]) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000109 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 Vlasenko701e1272010-09-04 21:21:07 +0200118 while (*++argv) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000119 size_t off;
Denys Vlasenko701e1272010-09-04 21:21:07 +0200120 unsigned scan_flags;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000121
Denys Vlasenko701e1272010-09-04 21:21:07 +0200122 string = *argv;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000123 off = strcspn(string, "#%");
124 if (!off) {
125 printf("invalid format\n");
Mike Frysingera4f331d2009-04-07 06:03:22 +0000126 continue;
127 }
128 op = string + off;
Denys Vlasenko701e1272010-09-04 21:21:07 +0200129 scan_flags = pick_scan(op[0], op[1]);
130
131 printf("'%s': flags:%x, ", string, scan_flags);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000132 pattern = op + 1;
133 if (op[0] == op[1])
Denys Vlasenko701e1272010-09-04 21:21:07 +0200134 pattern++;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000135 op[0] = '\0';
136
Denys Vlasenko701e1272010-09-04 21:21:07 +0200137 loc = scan_and_match(string, pattern, scan_flags);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000138
Denys Vlasenko701e1272010-09-04 21:21:07 +0200139 if (scan_flags & SCAN_MATCH_LEFT_HALF) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000140 printf("'%s'\n", loc);
141 } else {
Denys Vlasenko701e1272010-09-04 21:21:07 +0200142 if (loc)
143 *loc = '\0';
Mike Frysingera4f331d2009-04-07 06:03:22 +0000144 printf("'%s'\n", string);
145 }
Mike Frysingera4f331d2009-04-07 06:03:22 +0000146 }
147
148 return 0;
149}
150#endif