blob: fee3cf2a80d432dbd2423d1060cfd4cda1548c3f [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 Vlasenkoe298ce62010-09-04 19:52:44 +020074 *loc = c;
75 } else {
Denys Vlasenko2d8187c2010-09-12 15:06:42 +020076 r = fnmatch(pattern, loc, 0);
Denys Vlasenkoe298ce62010-09-04 19:52:44 +020077 }
Denys Vlasenko2d8187c2010-09-12 15:06:42 +020078 if (r == 0) /* match found */
Mike Frysingera4f331d2009-04-07 06:03:22 +000079 return loc;
Denys Vlasenko701e1272010-09-04 21:21:07 +020080 if (early_exit) {
81#ifdef STANDALONE
82 printf("(early exit) ");
83#endif
84 break;
Denys Vlasenkoe298ce62010-09-04 19:52:44 +020085 }
Mike Frysingera4f331d2009-04-07 06:03:22 +000086
Denys Vlasenko701e1272010-09-04 21:21:07 +020087 if (flags & SCAN_MOVE_FROM_LEFT) {
88 loc++;
89 } else {
90 loc--;
91 }
92 }
Mike Frysingera4f331d2009-04-07 06:03:22 +000093 return NULL;
94}
95
96#ifdef STANDALONE
97int main(int argc, char *argv[])
98{
99 char *string;
100 char *op;
101 char *pattern;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000102 char *loc;
103
Denys Vlasenko701e1272010-09-04 21:21:07 +0200104 setvbuf(stdout, NULL, _IONBF, 0);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000105
Denys Vlasenko701e1272010-09-04 21:21:07 +0200106 if (!argv[1]) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000107 puts(
108 "Usage: match <test> [test...]\n\n"
109 "Where a <test> is the form: <string><op><match>\n"
110 "This is to test the shell ${var#val} expression type.\n\n"
111 "e.g. `match 'abc#a*'` -> bc"
112 );
113 return 1;
114 }
115
Denys Vlasenko701e1272010-09-04 21:21:07 +0200116 while (*++argv) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000117 size_t off;
Denys Vlasenko701e1272010-09-04 21:21:07 +0200118 unsigned scan_flags;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000119
Denys Vlasenko701e1272010-09-04 21:21:07 +0200120 string = *argv;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000121 off = strcspn(string, "#%");
122 if (!off) {
123 printf("invalid format\n");
Mike Frysingera4f331d2009-04-07 06:03:22 +0000124 continue;
125 }
126 op = string + off;
Denys Vlasenko701e1272010-09-04 21:21:07 +0200127 scan_flags = pick_scan(op[0], op[1]);
128
129 printf("'%s': flags:%x, ", string, scan_flags);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000130 pattern = op + 1;
131 if (op[0] == op[1])
Denys Vlasenko701e1272010-09-04 21:21:07 +0200132 pattern++;
Mike Frysingera4f331d2009-04-07 06:03:22 +0000133 op[0] = '\0';
134
Denys Vlasenko701e1272010-09-04 21:21:07 +0200135 loc = scan_and_match(string, pattern, scan_flags);
Mike Frysingera4f331d2009-04-07 06:03:22 +0000136
Denys Vlasenko701e1272010-09-04 21:21:07 +0200137 if (scan_flags & SCAN_MATCH_LEFT_HALF) {
Mike Frysingera4f331d2009-04-07 06:03:22 +0000138 printf("'%s'\n", loc);
139 } else {
Denys Vlasenko701e1272010-09-04 21:21:07 +0200140 if (loc)
141 *loc = '\0';
Mike Frysingera4f331d2009-04-07 06:03:22 +0000142 printf("'%s'\n", string);
143 }
Mike Frysingera4f331d2009-04-07 06:03:22 +0000144 }
145
146 return 0;
147}
148#endif