Skip to content

Commit 486b731

Browse files
nordic-krchnashif
authored andcommitted
ext: lib: Add fnmatch library
Library will be used by new shell implementation. Signed-off-by: Krzysztof Chruscinski <[email protected]> Signed-off-by: Jakub Rzeszutko <[email protected]>
1 parent ba01a39 commit 486b731

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

ext/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ source "ext/lib/crypto/Kconfig"
1414

1515
source "ext/lib/encoding/Kconfig"
1616

17+
source "ext/lib/fnmatch/Kconfig"
18+
1719
source "ext/lib/ipc/open-amp/Kconfig"
1820

1921
source "ext/lib/mgmt/Kconfig"

ext/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ add_subdirectory(crypto)
22
add_subdirectory(encoding)
33
add_subdirectory(ipc)
44
add_subdirectory(mgmt)
5+
add_subdirectory_ifdef(CONFIG_FNMATCH fnmatch)

ext/lib/fnmatch/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2018 Nordic Semiconductor
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
zephyr_include_directories_ifdef(
7+
CONFIG_FNMATCH
8+
.
9+
)
10+
11+
zephyr_sources_ifdef(
12+
CONFIG_FNMATCH
13+
fnmatch.c
14+
)
15+

ext/lib/fnmatch/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2018 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
config FNMATCH
8+
bool
9+
prompt "Fnmatch Support"
10+
help
11+
This option enables the fnmatch library
12+

ext/lib/fnmatch/fnmatch.c

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/* $NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos Exp $ */
2+
3+
/*
4+
* Copyright (c) 1989, 1993, 1994
5+
* The Regents of the University of California. All rights reserved.
6+
*
7+
* This code is derived from software contributed to Berkeley by
8+
* Guido van Rossum.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in the
17+
* documentation and/or other materials provided with the distribution.
18+
* 3. Neither the name of the University nor the names of its contributors
19+
* may be used to endorse or promote products derived from this software
20+
* without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
* SUCH DAMAGE.
33+
*/
34+
35+
/*
36+
* Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
37+
* Compares a filename or pathname to a pattern.
38+
*/
39+
40+
#include <ctype.h>
41+
#include "fnmatch.h"
42+
#include <string.h>
43+
44+
#define EOS '\0'
45+
46+
static inline int foldcase(int ch, int flags)
47+
{
48+
49+
if ((flags & FNM_CASEFOLD) != 0 && isupper(ch))
50+
return tolower(ch);
51+
return ch;
52+
}
53+
54+
#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags))
55+
56+
static const char * rangematch(const char *pattern, int test, int flags)
57+
{
58+
int negate, ok, need;
59+
char c, c2;
60+
61+
if (pattern == NULL)
62+
{
63+
return NULL;
64+
}
65+
66+
/*
67+
* A bracket expression starting with an unquoted circumflex
68+
* character produces unspecified results (IEEE 1003.2-1992,
69+
* 3.13.2). This implementation treats it like '!', for
70+
* consistency with the regular expression syntax.
71+
* J.T. Conklin ([email protected])
72+
*/
73+
if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
74+
++pattern;
75+
76+
need = 1;
77+
for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']' || need;) {
78+
need = 0;
79+
if (c == '/')
80+
return (void *)-1;
81+
if (c == '\\' && !(flags & FNM_NOESCAPE))
82+
c = FOLDCASE(*pattern++, flags);
83+
if (c == EOS)
84+
return NULL;
85+
if (*pattern == '-'
86+
&& (c2 = FOLDCASE(*(pattern + 1), flags)) != EOS &&
87+
c2 != ']') {
88+
pattern += 2;
89+
if (c2 == '\\' && !(flags & FNM_NOESCAPE))
90+
c2 = FOLDCASE(*pattern++, flags);
91+
if (c2 == EOS)
92+
return NULL;
93+
if (c <= test && test <= c2)
94+
ok = 1;
95+
} else if (c == test)
96+
ok = 1;
97+
}
98+
return ok == negate ? NULL : pattern;
99+
}
100+
101+
102+
static int fnmatchx(const char *pattern, const char *string, int flags, size_t recursion)
103+
{
104+
const char *stringstart, *r;
105+
char c, test;
106+
107+
if ((pattern == NULL) || (string == NULL))
108+
{
109+
return FNM_NOMATCH;
110+
}
111+
112+
if (recursion-- == 0)
113+
return FNM_NORES;
114+
115+
for (stringstart = string;;) {
116+
switch (c = FOLDCASE(*pattern++, flags)) {
117+
case EOS:
118+
if ((flags & FNM_LEADING_DIR) && *string == '/')
119+
return 0;
120+
return *string == EOS ? 0 : FNM_NOMATCH;
121+
case '?':
122+
if (*string == EOS)
123+
return FNM_NOMATCH;
124+
if (*string == '/' && (flags & FNM_PATHNAME))
125+
return FNM_NOMATCH;
126+
if (*string == '.' && (flags & FNM_PERIOD) &&
127+
(string == stringstart ||
128+
((flags & FNM_PATHNAME) && *(string - 1) == '/')))
129+
return FNM_NOMATCH;
130+
++string;
131+
break;
132+
case '*':
133+
c = FOLDCASE(*pattern, flags);
134+
/* Collapse multiple stars. */
135+
while (c == '*')
136+
c = FOLDCASE(*++pattern, flags);
137+
138+
if (*string == '.' && (flags & FNM_PERIOD) &&
139+
(string == stringstart ||
140+
((flags & FNM_PATHNAME) && *(string - 1) == '/')))
141+
return FNM_NOMATCH;
142+
143+
/* Optimize for pattern with * at end or before /. */
144+
if (c == EOS) {
145+
if (flags & FNM_PATHNAME)
146+
return (flags & FNM_LEADING_DIR) ||
147+
strchr(string, '/') == NULL ?
148+
0 : FNM_NOMATCH;
149+
else
150+
return 0;
151+
} else if (c == '/' && flags & FNM_PATHNAME) {
152+
if ((string = strchr(string, '/')) == NULL)
153+
return FNM_NOMATCH;
154+
break;
155+
}
156+
157+
/* General case, use recursion. */
158+
while ((test = FOLDCASE(*string, flags)) != EOS) {
159+
int e;
160+
switch ((e = fnmatchx(pattern, string,
161+
flags & ~FNM_PERIOD, recursion))) {
162+
case FNM_NOMATCH:
163+
break;
164+
default:
165+
return e;
166+
}
167+
if (test == '/' && flags & FNM_PATHNAME)
168+
break;
169+
++string;
170+
}
171+
return FNM_NOMATCH;
172+
case '[':
173+
if (*string == EOS)
174+
return FNM_NOMATCH;
175+
if (*string == '/' && flags & FNM_PATHNAME)
176+
return FNM_NOMATCH;
177+
if ((r = rangematch(pattern,
178+
FOLDCASE(*string, flags), flags)) == NULL)
179+
return FNM_NOMATCH;
180+
if (r == (void *)-1) {
181+
if (*string != '[')
182+
return FNM_NOMATCH;
183+
} else
184+
pattern = r;
185+
++string;
186+
break;
187+
case '\\':
188+
if (!(flags & FNM_NOESCAPE)) {
189+
if ((c = FOLDCASE(*pattern++, flags)) == EOS) {
190+
c = '\0';
191+
--pattern;
192+
}
193+
}
194+
/* FALLTHROUGH */
195+
default:
196+
if (c != FOLDCASE(*string++, flags))
197+
return FNM_NOMATCH;
198+
break;
199+
}
200+
}
201+
/* NOTREACHED */
202+
}
203+
204+
int fnmatch(const char *pattern, const char *string, int flags)
205+
{
206+
return fnmatchx(pattern, string, flags, 64);
207+
}
208+

ext/lib/fnmatch/fnmatch.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* $NetBSD: fnmatch.h,v 1.12.50.1 2011/02/08 16:18:55 bouyer Exp $ */
2+
3+
/*-
4+
* Copyright (c) 1992, 1993
5+
* The Regents of the University of California. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* 3. Neither the name of the University nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29+
* SUCH DAMAGE.
30+
*
31+
* @(#)fnmatch.h 8.1 (Berkeley) 6/2/93
32+
*/
33+
34+
#ifndef _FNMATCH_H_
35+
#define _FNMATCH_H_
36+
37+
#define FNM_NOMATCH 1 /* Match failed. */
38+
#define FNM_NOSYS 2 /* Function not implemented. */
39+
#define FNM_NORES 3 /* Out of resources */
40+
41+
#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
42+
#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
43+
#define FNM_PERIOD 0x04 /* Period must be matched by period. */
44+
#define FNM_CASEFOLD 0x08 /* Pattern is matched case-insensitive */
45+
#define FNM_LEADING_DIR 0x10 /* Ignore /<tail> after Imatch. */
46+
47+
int fnmatch(const char *, const char *, int);
48+
49+
#endif /* !_FNMATCH_H_ */
50+

0 commit comments

Comments
 (0)