Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options/posix: port musl regex engine #511

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ABI_BREAKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ This document lists the ABI breaks that were made in each mlibc major version.
## Version 3

- [#452](https://github.com/managarm/mlibc/pull/452): The functions `FD_{CLR,ISSET,SET,ZERO}` were renamed to `__FD_{CLR,ISSET,SET,ZERO}` and replaced by macros to match Wine's assumptions.

- [#511](https://github.com/managarm/mlibc/pull/511): Musl's regex engine was added, implementing `regcomp` and `regexec`. This required some changes to the `regex_t` struct.
23 changes: 0 additions & 23 deletions options/posix/generic/regex.cpp

This file was deleted.

4 changes: 3 additions & 1 deletion options/posix/include/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ typedef ptrdiff_t regoff_t;

typedef struct re_pattern_buffer {
size_t re_nsub;
void *__impl;
void *__opaque, *__padding[4];
size_t __nsub2;
char __padding2;
} regex_t;

typedef struct {
Expand Down
19 changes: 15 additions & 4 deletions options/posix/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ libc_sources += files(
'generic/posix_time.cpp',
'generic/pthread-stubs.cpp',
'generic/pwd-stubs.cpp',
'generic/regex.cpp',
'generic/resolv_conf.cpp',
'generic/sched-stubs.cpp',
'generic/spawn-stubs.cpp',
Expand Down Expand Up @@ -54,11 +53,23 @@ libc_sources += files(
'generic/sys-msg.cpp',
'generic/sys-sem.cpp',
'generic/sys-ipc.cpp',
'generic/wordexp-stubs.cpp',
'musl-generic/fnmatch.cpp',
'musl-generic/glob.cpp',
'generic/wordexp-stubs.cpp'
)

if not headers_only
libc_sublibs += static_library('musl-generic-regex',
'musl-generic-regex/fnmatch.c',
'musl-generic-regex/glob.c',
'musl-generic-regex/regcomp.c',
'musl-generic-regex/regerror.c',
'musl-generic-regex/regexec.c',
'musl-generic-regex/tre-mem.c',
pic: true,
include_directories: libc_include_dirs,
c_args: ['-Wno-unused', '-Wno-implicit', '-Wno-parentheses', '-Wno-sign-compare', '-Wno-attributes', '-Wno-unknown-pragmas', '-Wno-implicit-fallthrough']
)
endif

if not no_headers
install_headers(
'include/byteswap.h',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
//#include "locale_impl.h"
// #include "locale_impl.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just simply remove this?


#define END 0
#define UNMATCHABLE -2
Expand All @@ -32,7 +32,7 @@ static int str_next(const char *str, size_t n, size_t *step)
*step = 0;
return 0;
}
if ((unsigned char)str[0] >= 128U) {
if (str[0] >= 128U) {
wchar_t wc;
int k = mbtowc(&wc, str, n);
if (k<0) {
Expand Down Expand Up @@ -85,7 +85,7 @@ static int pat_next(const char *pat, size_t m, size_t *step, int flags)
if (pat[0] == '?')
return QUESTION;
escaped:
if ((unsigned char)pat[0] >= 128U) {
if (pat[0] >= 128U) {
wchar_t wc;
int k = mbtowc(&wc, pat, m);
if (k<0) {
Expand Down Expand Up @@ -127,8 +127,8 @@ static int match_bracket(const char *p, int k, int kfold)
int l = mbtowc(&wc2, p+1, 4);
if (l < 0) return 0;
if (wc <= wc2)
if ((unsigned)k-wc <= (unsigned)wc2-wc ||
(unsigned)kfold-wc <= (unsigned)wc2-wc)
if ((unsigned)k-wc <= wc2-wc ||
(unsigned)kfold-wc <= wc2-wc)
return !inv;
p += l-1;
continue;
Expand All @@ -148,7 +148,7 @@ static int match_bracket(const char *p, int k, int kfold)
}
continue;
}
if ((unsigned char)*p < 128U) {
if (*p < 128U) {
wc = (unsigned char)*p;
} else {
int l = mbtowc(&wc, p, 4);
Expand Down Expand Up @@ -230,7 +230,7 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
* On illegal sequences we may get it wrong, but in that case
* we necessarily have a matching failure anyway. */
for (s=endstr; s>str && tailcnt; tailcnt--) {
if ((unsigned char)s[-1] < 128U || MB_CUR_MAX==1) s--;
if (s[-1] < 128U || MB_CUR_MAX==1) s--;
else while ((unsigned char)*--s-0x80U<0x40 && s>str);
}
if (tailcnt) return FNM_NOMATCH;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _BSD_SOURCE
#include <glob.h>
#include <fnmatch.h>
#include <sys/stat.h>
Expand All @@ -18,16 +19,16 @@ struct match

static int append(struct match **tail, const char *name, size_t len, int mark)
{
struct match *m = reinterpret_cast<struct match *>(malloc(sizeof(struct match) + len + 2));
if (!m) return -1;
(*tail)->next = m;
m->next = NULL;
memcpy(m->name, name, len+1);
struct match *new = malloc(sizeof(struct match) + len + 2);
if (!new) return -1;
(*tail)->next = new;
new->next = NULL;
memcpy(new->name, name, len+1);
if (mark && len && name[len-1]!='/') {
m->name[len] = '/';
m->name[len+1] = 0;
new->name[len] = '/';
new->name[len+1] = 0;
}
*tail = m;
*tail = new;
return 0;
}

Expand Down Expand Up @@ -150,13 +151,13 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*
/* With GLOB_PERIOD, don't allow matching . or .. unless
* fnmatch would match them with FNM_PERIOD rules in effect. */
if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
&& (!de->d_name[1] || (de->d_name[1]=='.' && !de->d_name[2]))
&& (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
&& fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD))
continue;

memcpy(buf+pos, de->d_name, l+1);
if (p2) *p2 = saved_sep;
int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : const_cast<char *>(""), flags, errfunc, tail);
int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail);
if (r) {
closedir(dir);
return r;
Expand All @@ -171,7 +172,7 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*
return 0;
}

static int ignore_err(const char *, int)
static int ignore_err(const char *path, int err)
{
return 0;
}
Expand Down Expand Up @@ -223,14 +224,14 @@ static int expand_tilde(char **pat, char *buf, size_t *pos)
return 0;
}

int glob(const char *__restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *__restrict g)
int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
{
struct match head = { .next = NULL }, *tail = &head;
size_t cnt, i;
size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
int error = 0;
char buf[PATH_MAX];

if (!errfunc) errfunc = ignore_err;

if (!(flags & GLOB_APPEND)) {
Expand All @@ -256,7 +257,7 @@ int glob(const char *__restrict pat, int flags, int (*errfunc)(const char *path,
freelist(&head);
return error;
}

for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
if (!cnt) {
if (flags & GLOB_NOCHECK) {
Expand All @@ -269,15 +270,15 @@ int glob(const char *__restrict pat, int flags, int (*errfunc)(const char *path,
}

if (flags & GLOB_APPEND) {
char **pathv = reinterpret_cast<char **>(realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)));
char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
if (!pathv) {
freelist(&head);
return GLOB_NOSPACE;
}
g->gl_pathv = pathv;
offs += g->gl_pathc;
} else {
g->gl_pathv = reinterpret_cast<char **>(malloc((offs + cnt + 1) * sizeof(char *)));
g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
if (!g->gl_pathv) {
freelist(&head);
return GLOB_NOSPACE;
Expand All @@ -292,7 +293,7 @@ int glob(const char *__restrict pat, int flags, int (*errfunc)(const char *path,

if (!(flags & GLOB_NOSORT))
qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);

return error;
}

Expand All @@ -305,3 +306,6 @@ void globfree(glob_t *g)
g->gl_pathc = 0;
g->gl_pathv = NULL;
}

// weak_alias(glob, glob64);
// weak_alias(globfree, globfree64);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need these?

Loading