Przeglądaj źródła

Add empty locale functions

jD91mZM2 6 lat temu
rodzic
commit
996445a6a3

+ 9 - 0
Cargo.lock

@@ -158,6 +158,14 @@ name = "libc"
 version = "0.2.40"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "locale"
+version = "0.1.0"
+dependencies = [
+ "cbindgen 0.5.2",
+ "platform 0.1.0",
+]
+
 [[package]]
 name = "log"
 version = "0.3.9"
@@ -271,6 +279,7 @@ dependencies = [
  "fenv 0.1.0",
  "float 0.1.0",
  "grp 0.1.0",
+ "locale 0.1.0",
  "netinet 0.1.0",
  "platform 0.1.0",
  "semaphore 0.1.0",

+ 1 - 0
Cargo.toml

@@ -21,6 +21,7 @@ fcntl = { path = "src/fcntl" }
 fenv = { path = "src/fenv" }
 float = { path = "src/float" }
 grp = { path = "src/grp" }
+locale = { path = "src/locale" }
 netinet = { path = "src/netinet" }
 platform = { path = "src/platform" }
 semaphore = { path = "src/semaphore" }

+ 7 - 0
include/bits/locale.h

@@ -0,0 +1,7 @@
+#define LC_ALL 0
+#define LC_COLLATE 1
+#define LC_CTYPE 2
+#define LC_MESSAGES 3
+#define LC_MONETARY 4
+#define LC_NUMERIC 5
+#define LC_TIME 6

+ 1 - 0
src/lib.rs

@@ -10,6 +10,7 @@ pub extern crate fcntl;
 pub extern crate fenv;
 pub extern crate float;
 pub extern crate grp;
+pub extern crate locale;
 pub extern crate netinet;
 pub extern crate semaphore;
 pub extern crate stdio;

+ 11 - 0
src/locale/Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "locale"
+version = "0.1.0"
+authors = ["Jeremy Soller <[email protected]>"]
+build = "build.rs"
+
+[build-dependencies]
+cbindgen = { path = "../../cbindgen" }
+
+[dependencies]
+platform = { path = "../platform" }

+ 11 - 0
src/locale/build.rs

@@ -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/locale.h");
+}

+ 6 - 0
src/locale/cbindgen.toml

@@ -0,0 +1,6 @@
+include_guard = "_LOCALE_H"
+trailer = "#include <bits/locale.h>"
+language = "C"
+
+[enum]
+prefix_with_name = true

+ 70 - 0
src/locale/src/lib.rs

@@ -0,0 +1,70 @@
+//! locale implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/locale.h.html
+
+#![no_std]
+#![feature(alloc)]
+
+extern crate platform;
+
+use core::ptr;
+use platform::types::*;
+
+const EMPTY_PTR: *const c_char = "\0" as *const _ as *const c_char;
+
+#[repr(C)]
+#[no_mangle]
+pub struct lconv {
+    currency_symbol: *const c_char,
+    decimal_point: *const c_char,
+    frac_digits: c_char,
+    grouping: *const c_char,
+    int_curr_symbol: *const c_char,
+    int_frac_digits: c_char,
+    mon_decimal_point: *const c_char,
+    mon_grouping: *const c_char,
+    mon_thousands_sep: *const c_char,
+    negative_sign: *const c_char,
+    n_cs_precedes: c_char,
+    n_sep_by_space: c_char,
+    n_sign_posn: c_char,
+    positive_sign: *const c_char,
+    p_cs_precedes: c_char,
+    p_sep_by_space: c_char,
+    p_sign_posn: c_char,
+    thousands_sep: *const c_char,
+}
+unsafe impl Sync for lconv {}
+
+static CURRENT_LOCALE: lconv = lconv {
+    currency_symbol: EMPTY_PTR,
+    decimal_point: ".\0" as *const _ as *const c_char,
+    frac_digits: c_char::max_value(),
+    grouping: EMPTY_PTR,
+    int_curr_symbol: EMPTY_PTR,
+    int_frac_digits: c_char::max_value(),
+    mon_decimal_point: EMPTY_PTR,
+    mon_grouping: EMPTY_PTR,
+    mon_thousands_sep: EMPTY_PTR,
+    negative_sign: EMPTY_PTR,
+    n_cs_precedes: c_char::max_value(),
+    n_sep_by_space: c_char::max_value(),
+    n_sign_posn: c_char::max_value(),
+    positive_sign: EMPTY_PTR,
+    p_cs_precedes: c_char::max_value(),
+    p_sep_by_space: c_char::max_value(),
+    p_sign_posn: c_char::max_value(),
+    thousands_sep: EMPTY_PTR
+};
+
+#[no_mangle]
+pub extern "C" fn localeconv() -> *const lconv {
+    &CURRENT_LOCALE as *const _
+}
+
+#[no_mangle]
+pub extern "C" fn setlocale(_option: c_int, val: *const c_char) -> *const c_char {
+    if val.is_null() {
+        return "C\0".as_ptr() as *const c_char;
+    }
+    // TODO actually implement
+    ptr::null()
+}

+ 0 - 1
src/stdio/src/lib.rs

@@ -2,7 +2,6 @@
 
 #![no_std]
 #![feature(alloc)]
-#![feature(global_allocator)]
 
 extern crate alloc;
 extern crate errno;

+ 1 - 0
tests/.gitignore

@@ -17,6 +17,7 @@
 /getid
 /getc_unget
 /link
+/locale
 /math
 /mem
 /pipe

+ 1 - 0
tests/Makefile

@@ -15,6 +15,7 @@ EXPECT_BINS=\
 	getid \
 	getc_unget \
 	link \
+	locale \
 	math \
 	mem \
 	pipe \

+ 0 - 0
tests/expected/locale.stderr


+ 1 - 0
tests/expected/locale.stdout

@@ -0,0 +1 @@
+success!

+ 13 - 0
tests/locale.c

@@ -0,0 +1,13 @@
+#include <locale.h>
+#include <stdio.h>
+#include <string.h>
+
+int main() {
+    // TODO: Implement locale properly and test it here
+    const char* val = setlocale(LC_ALL, NULL);
+    if (strcmp(val, "C") == 0) {
+        puts("success!");
+    } else {
+        printf("setlocale returned the wrong value: %s", val);
+    }
+}