Implement fnmatch.h

This commit is contained in:
jD91mZM2
2018-08-05 19:17:46 +02:00
parent b5adee798d
commit b20307dca0
15 changed files with 295 additions and 5 deletions
+10
View File
@@ -0,0 +1,10 @@
[package]
name = "fnmatch"
version = "0.1.0"
authors = ["jD91mZM2 <me@krake.one>"]
[build-dependencies]
cbindgen = { path = "../../cbindgen" }
[dependencies]
platform = { path = "../platform" }
+11
View File
@@ -0,0 +1,11 @@
extern crate cbindgen;
use std::{env, fs};
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
fs::create_dir_all("../../target/include").expect("failed to create include directory");
cbindgen::generate(crate_dir)
.expect("failed to generate bindings")
.write_to_file("../../target/include/fnmatch.h");
}
+6
View File
@@ -0,0 +1,6 @@
include_guard = "_FNMATCH_H"
language = "C"
style = "Tag"
[enum]
prefix_with_name = true
+166
View File
@@ -0,0 +1,166 @@
//! fnmatch implementation
#![no_std]
#![feature(alloc)]
extern crate alloc;
extern crate platform;
use alloc::vec::Vec;
use platform::types::*;
pub const FNM_NOMATCH: c_int = 1;
pub const FNM_NOESCAPE: c_int = 1;
pub const FNM_PATHNAME: c_int = 2;
pub const FNM_PERIOD: c_int = 4;
pub const FNM_CASEFOLD: c_int = 8;
#[derive(Debug)]
enum Token {
Any,
Char(u8),
Match(bool, Vec<u8>),
Wildcard,
// TODO: FNM_EXTMATCH, which is basically a whole another custom regex
// format that's ambigious and ugh. The C standard library is really bloaty
// and I sure hope we can get away with delaying this as long as possible.
// If you need to implement this, you can contact jD91mZM2 for assistance
// in reading this code in case it's ugly.
}
unsafe fn next_token(pattern: &mut *const c_char, flags: c_int) -> Option<Token> {
let c = **pattern as u8;
if c == 0 {
return None;
}
*pattern = pattern.offset(1);
Some(match c {
b'\\' if flags & FNM_NOESCAPE == FNM_NOESCAPE => {
let c = **pattern as u8;
if c == 0 {
// Trailing backslash. Maybe error here?
return None;
}
*pattern = pattern.offset(1);
Token::Char(c)
},
b'?' => Token::Any,
b'*' => Token::Wildcard,
b'[' => {
let mut matches = Vec::new();
let invert = if **pattern as u8 == b'!' {
*pattern = pattern.offset(1);
true
} else { false };
loop {
let mut c = **pattern as u8;
if c == 0 {
break;
}
*pattern = pattern.offset(1);
match c {
b']' => break,
b'\\' => {
c = **pattern as u8;
*pattern = pattern.offset(1);
if c == 0 {
// Trailing backslash. Maybe error?
break;
}
},
_ => ()
}
if matches.len() >= 2 && matches[matches.len() - 1] == b'-' {
let len = matches.len();
let start = matches[len - 2];
matches.drain(len - 2..);
// Exclusive range because we'll push C later
for c in start..c {
matches.push(c);
}
}
matches.push(c);
}
// Otherwise, there was no closing ]. Maybe error?
Token::Match(invert, matches)
},
c => Token::Char(c)
})
}
#[no_mangle]
pub unsafe extern "C" fn fnmatch(mut pattern: *const c_char, mut input: *const c_char, flags: c_int) -> c_int {
let pathname = flags & FNM_PATHNAME == FNM_PATHNAME;
let casefold = flags & FNM_CASEFOLD == FNM_CASEFOLD;
let mut leading = true;
loop {
if *input == 0 {
return if *pattern == 0 { 0 } else { FNM_NOMATCH };
}
if leading && flags & FNM_PERIOD == FNM_PERIOD {
if *input as u8 == b'.' && *pattern as u8 != b'.' {
return FNM_NOMATCH;
}
}
leading = false;
match next_token(&mut pattern, flags) {
Some(Token::Any) => {
if pathname && *input as u8 == b'/' {
return FNM_NOMATCH;
}
input = input.offset(1);
},
Some(Token::Char(c)) => {
let mut a = *input as u8;
if casefold && a >= b'a' && a <= b'z' {
a -= b'a' - b'A';
}
let mut b = c;
if casefold && b >= b'a' && b <= b'z' {
b -= b'a' - b'A';
}
if a != b {
return FNM_NOMATCH;
}
if pathname && a == b'/' {
leading = true;
}
input = input.offset(1);
},
Some(Token::Match(invert, matches)) => {
if (pathname && *input as u8 == b'/') || matches.contains(&(*input as u8)) == invert {
// Found it, but it's inverted! Or vise versa.
return FNM_NOMATCH;
}
input = input.offset(1);
},
Some(Token::Wildcard) => {
loop {
let c = *input as u8;
if c == 0 {
return if *pattern == 0 { 0 } else { FNM_NOMATCH };
}
let ret = fnmatch(pattern, input, flags);
if ret == FNM_NOMATCH {
input = input.offset(1);
} else {
// Either an error or a match. Forward the return.
return ret;
}
if pathname && c == b'/' {
// End of segment, no match yet
return FNM_NOMATCH;
}
}
unreachable!("nothing should be able to break out of the loop");
},
None => return FNM_NOMATCH // Pattern ended but there's still some input
}
}
}
+1
View File
@@ -13,6 +13,7 @@ pub extern crate errno;
pub extern crate fcntl;
pub extern crate fenv;
pub extern crate float;
pub extern crate fnmatch;
pub extern crate grp;
pub extern crate locale;
pub extern crate netinet;
-1
View File
@@ -2,7 +2,6 @@ use alloc::String;
use alloc::Vec;
use platform::types::*;
use platform::Read;
#[cfg(not(test))]
use vl::VaList;
#[derive(PartialEq, Eq)]
+1
View File
@@ -1,3 +1,4 @@
sys_includes = ["sys/types.h"]
include_guard = "_SYS_TIMES_H"
language = "C"
style = "Tag"
+12 -4
View File
@@ -24,11 +24,15 @@ pub unsafe fn strftime<W: Write>(
}};
(byte $b:expr) => {{
w!(reserve 1);
w.write_u8($b);
if w.write_u8($b).is_err() {
return !0;
}
}};
(char $chr:expr) => {{
w!(reserve $chr.len_utf8());
w.write_char($chr);
if w.write_char($chr).is_err() {
return !0;
}
}};
(recurse $fmt:expr) => {{
let mut fmt = String::with_capacity($fmt.len() + 1);
@@ -44,7 +48,9 @@ pub unsafe fn strftime<W: Write>(
}};
($str:expr) => {{
w!(reserve $str.len());
w.write_str($str);
if w.write_str($str).is_err() {
return !0;
}
}};
($fmt:expr, $($args:expr),+) => {{
// Would use write!() if I could get the length written
@@ -140,7 +146,9 @@ pub unsafe fn strftime<W: Write>(
}
if toplevel {
// nul byte is already counted in written
w.write_u8(0);
if w.write_u8(0).is_err() {
return !0;
}
}
written
}