Skip to content

Add bindings for POSIX regexes #1719

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

Merged
merged 1 commit into from
Apr 13, 2020
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
6 changes: 6 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn test_apple(target: &str) {
"poll.h",
"pthread.h",
"pwd.h",
"regex.h",
"resolv.h",
"sched.h",
"semaphore.h",
Expand Down Expand Up @@ -303,6 +304,7 @@ fn test_openbsd(target: &str) {
"netinet/tcp.h",
"netinet/udp.h",
"net/bpf.h",
"regex.h",
"resolv.h",
"pthread.h",
"dlfcn.h",
Expand Down Expand Up @@ -903,6 +905,7 @@ fn test_netbsd(target: &str) {
"poll.h",
"pthread.h",
"pwd.h",
"regex.h",
"resolv.h",
"sched.h",
"semaphore.h",
Expand Down Expand Up @@ -1098,6 +1101,7 @@ fn test_dragonflybsd(target: &str) {
"pthread.h",
"pthread_np.h",
"pwd.h",
"regex.h",
"resolv.h",
"sched.h",
"semaphore.h",
Expand Down Expand Up @@ -1643,6 +1647,7 @@ fn test_freebsd(target: &str) {
"pthread.h",
"pthread_np.h",
"pwd.h",
"regex.h",
"resolv.h",
"sched.h",
"semaphore.h",
Expand Down Expand Up @@ -2248,6 +2253,7 @@ fn test_linux(target: &str) {
"pthread.h",
"pty.h",
"pwd.h",
"regex.h",
"resolv.h",
"sched.h",
"semaphore.h",
Expand Down
3 changes: 3 additions & 0 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,9 @@ pub const TIME_OOP: ::c_int = 3;
pub const TIME_WAIT: ::c_int = 4;
pub const TIME_ERROR: ::c_int = 5;

pub const REG_ENOSYS: ::c_int = -1;
pub const REG_ILLSEQ: ::c_int = 17;

f! {
pub fn WIFCONTINUED(status: ::c_int) -> bool {
status == 0x13
Expand Down
71 changes: 71 additions & 0 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type socklen_t = u32;
pub type sa_family_t = u8;
pub type pthread_t = ::uintptr_t;
pub type nfds_t = ::c_uint;
pub type regoff_t = off_t;

s! {
pub struct sockaddr {
Expand Down Expand Up @@ -101,6 +102,18 @@ s! {
pub if_index: ::c_uint,
pub if_name: *mut ::c_char,
}

pub struct regex_t {
__re_magic: ::c_int,
__re_nsub: ::size_t,
__re_endp: *const ::c_char,
__re_g: *mut ::c_void,
}

pub struct regmatch_t {
pub rm_so: regoff_t,
pub rm_eo: regoff_t,
}
}

s_no_extra_traits! {
Expand Down Expand Up @@ -450,6 +463,41 @@ pub const BIOCGHDRCMPLT: ::c_ulong = 0x40044274;
pub const BIOCSHDRCMPLT: ::c_ulong = 0x80044275;
pub const SIOCGIFADDR: ::c_ulong = 0xc0206921;

pub const REG_BASIC: ::c_int = 0o0000;
pub const REG_EXTENDED: ::c_int = 0o0001;
pub const REG_ICASE: ::c_int = 0o0002;
pub const REG_NOSUB: ::c_int = 0o0004;
pub const REG_NEWLINE: ::c_int = 0o0010;
pub const REG_NOSPEC: ::c_int = 0o0020;
pub const REG_PEND: ::c_int = 0o0040;
pub const REG_DUMP: ::c_int = 0o0200;

pub const REG_NOMATCH: ::c_int = 1;
pub const REG_BADPAT: ::c_int = 2;
pub const REG_ECOLLATE: ::c_int = 3;
pub const REG_ECTYPE: ::c_int = 4;
pub const REG_EESCAPE: ::c_int = 5;
pub const REG_ESUBREG: ::c_int = 6;
pub const REG_EBRACK: ::c_int = 7;
pub const REG_EPAREN: ::c_int = 8;
pub const REG_EBRACE: ::c_int = 9;
pub const REG_BADBR: ::c_int = 10;
pub const REG_ERANGE: ::c_int = 11;
pub const REG_ESPACE: ::c_int = 12;
pub const REG_BADRPT: ::c_int = 13;
pub const REG_EMPTY: ::c_int = 14;
pub const REG_ASSERT: ::c_int = 15;
pub const REG_INVARG: ::c_int = 16;
pub const REG_ATOI: ::c_int = 255;
pub const REG_ITOA: ::c_int = 0o0400;

pub const REG_NOTBOL: ::c_int = 0o00001;
pub const REG_NOTEOL: ::c_int = 0o00002;
pub const REG_STARTEND: ::c_int = 0o00004;
pub const REG_TRACE: ::c_int = 0o00400;
pub const REG_LARGE: ::c_int = 0o01000;
pub const REG_BACKR: ::c_int = 0o02000;

f! {
pub fn CMSG_FIRSTHDR(mhdr: *const ::msghdr) -> *mut ::cmsghdr {
if (*mhdr).msg_controllen as usize >= ::mem::size_of::<::cmsghdr>() {
Expand Down Expand Up @@ -785,6 +833,29 @@ extern "C" {
options: ::c_int,
rusage: *mut ::rusage,
) -> ::pid_t;

pub fn regcomp(
preg: *mut regex_t,
pattern: *const ::c_char,
cflags: ::c_int,
) -> ::c_int;

pub fn regexec(
preg: *const regex_t,
input: *const ::c_char,
nmatch: ::size_t,
pmatch: *mut regmatch_t,
eflags: ::c_int,
) -> ::c_int;

pub fn regerror(
errcode: ::c_int,
preg: *const regex_t,
errbuf: *mut ::c_char,
errbuf_size: ::size_t,
) -> ::size_t;

pub fn regfree(preg: *mut regex_t);
}

cfg_if! {
Expand Down
2 changes: 2 additions & 0 deletions src/unix/bsd/netbsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ pub const SF_APPEND: ::c_ulong = 0x00040000;

pub const TIMER_ABSTIME: ::c_int = 1;

pub const REG_ENOSYS: ::c_int = 17;

#[link(name = "util")]
extern "C" {
pub fn setgrent();
Expand Down
18 changes: 18 additions & 0 deletions src/unix/linux_like/linux/gnu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub type pthread_t = c_ulong;
pub type __priority_which_t = ::c_uint;
pub type __rlimit_resource_t = ::c_uint;
pub type Lmid_t = ::c_long;
pub type regoff_t = ::c_int;

s! {
pub struct statx {
Expand Down Expand Up @@ -273,6 +274,17 @@ s! {
pub __glibc_reserved3: ::c_long,
pub __glibc_reserved4: ::c_long,
}

pub struct regex_t {
__buffer: *mut ::c_void,
__allocated: ::size_t,
__used: ::size_t,
__syntax: ::c_ulong,
__fastmap: *mut ::c_char,
__translate: *mut ::c_char,
__re_nsub: ::size_t,
__bitfield: u8,
}
}

impl siginfo_t {
Expand Down Expand Up @@ -1127,6 +1139,12 @@ cfg_if! {
}
pub const PTHREAD_MUTEX_ADAPTIVE_NP: ::c_int = 3;

pub const REG_STARTEND: ::c_int = 4;

pub const REG_EEND: ::c_int = 14;
pub const REG_ESIZE: ::c_int = 15;
pub const REG_ERPAREN: ::c_int = 16;

extern "C" {
pub fn fgetspent_r(
fp: *mut ::FILE,
Expand Down
51 changes: 51 additions & 0 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ s! {
pub svm_cid: ::c_uint,
pub svm_zero: [u8; 4]
}

pub struct regmatch_t {
pub rm_so: regoff_t,
pub rm_eo: regoff_t,
}
}

s_no_extra_traits! {
Expand Down Expand Up @@ -2512,6 +2517,29 @@ pub const LINUX_REBOOT_CMD_RESTART2: ::c_int = 0xA1B2C3D4;
pub const LINUX_REBOOT_CMD_SW_SUSPEND: ::c_int = 0xD000FCE2;
pub const LINUX_REBOOT_CMD_KEXEC: ::c_int = 0x45584543;

pub const REG_EXTENDED: ::c_int = 1;
pub const REG_ICASE: ::c_int = 2;
pub const REG_NEWLINE: ::c_int = 4;
pub const REG_NOSUB: ::c_int = 8;

pub const REG_NOTBOL: ::c_int = 1;
pub const REG_NOTEOL: ::c_int = 2;

pub const REG_ENOSYS: ::c_int = -1;
pub const REG_NOMATCH: ::c_int = 1;
pub const REG_BADPAT: ::c_int = 2;
pub const REG_ECOLLATE: ::c_int = 3;
pub const REG_ECTYPE: ::c_int = 4;
pub const REG_EESCAPE: ::c_int = 5;
pub const REG_ESUBREG: ::c_int = 6;
pub const REG_EBRACK: ::c_int = 7;
pub const REG_EPAREN: ::c_int = 8;
pub const REG_EBRACE: ::c_int = 9;
pub const REG_BADBR: ::c_int = 10;
pub const REG_ERANGE: ::c_int = 11;
pub const REG_ESPACE: ::c_int = 12;
pub const REG_BADRPT: ::c_int = 13;

f! {
pub fn NLA_ALIGN(len: ::c_int) -> ::c_int {
return ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1)
Expand Down Expand Up @@ -3365,6 +3393,29 @@ extern "C" {
mask: u32,
) -> ::c_int;
pub fn fanotify_init(flags: ::c_uint, event_f_flags: ::c_uint) -> ::c_int;

pub fn regcomp(
preg: *mut ::regex_t,
pattern: *const ::c_char,
cflags: ::c_int,
) -> ::c_int;

pub fn regexec(
preg: *const ::regex_t,
input: *const ::c_char,
nmatch: ::size_t,
pmatch: *mut regmatch_t,
eflags: ::c_int,
) -> ::c_int;

pub fn regerror(
errcode: ::c_int,
preg: *const ::regex_t,
errbuf: *mut ::c_char,
errbuf_size: ::size_t,
) -> ::size_t;

pub fn regfree(preg: *mut ::regex_t);
}

cfg_if! {
Expand Down
1 change: 1 addition & 0 deletions src/unix/linux_like/linux/musl/b32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub type c_ulong = u32;
pub type nlink_t = u32;
pub type blksize_t = ::c_long;
pub type __u64 = ::c_ulonglong;
pub type regoff_t = ::c_int;

s! {
pub struct pthread_attr_t {
Expand Down
1 change: 1 addition & 0 deletions src/unix/linux_like/linux/musl/b64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub type c_long = i64;
pub type c_ulong = u64;
pub type regoff_t = ::c_long;

s! {
pub struct statfs64 {
Expand Down
10 changes: 10 additions & 0 deletions src/unix/linux_like/linux/musl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ s! {
pub l_len: ::off_t,
pub l_pid: ::pid_t,
}

pub struct regex_t {
__re_nsub: ::size_t,
__opaque: *mut ::c_void,
__padding: [*mut ::c_void; 4usize],
__nsub2: ::size_t,
__padding2: ::c_char,
}
}

s_no_extra_traits! {
Expand Down Expand Up @@ -391,6 +399,8 @@ pub const RLIMIT_MSGQUEUE: ::c_int = 12;
pub const RLIMIT_NICE: ::c_int = 13;
pub const RLIMIT_RTPRIO: ::c_int = 14;

pub const REG_OK: ::c_int = 0;

extern "C" {
pub fn sendmmsg(
sockfd: ::c_int,
Expand Down