From 60bd7c0a19d66984c76fa8721a20521f8db1eab7 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 1 May 2019 19:11:20 +0200 Subject: [PATCH] hammer time --- examples/repl/cmdline.rs | 8 ++++--- examples/repl/main.rs | 45 +++++++++++++++++----------------------- misc.c | 14 ++++++------- src/types.rs | 2 -- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/examples/repl/cmdline.rs b/examples/repl/cmdline.rs index fddfab0ba..6abd069e1 100644 --- a/examples/repl/cmdline.rs +++ b/examples/repl/cmdline.rs @@ -1,3 +1,5 @@ +use std::ffi::CString; + use deltachat::constants::*; use deltachat::dc_aheader::*; use deltachat::dc_apeerstate::*; @@ -600,17 +602,17 @@ unsafe extern "C" fn chat_prefix(mut chat: *const dc_chat_t) -> *const libc::c_c #[no_mangle] pub unsafe extern "C" fn dc_cmdline( mut context: *mut dc_context_t, - mut cmdline: *const libc::c_char, + cmdline: &str, ) -> *mut libc::c_char { let mut cmd: *mut libc::c_char = 0 as *mut libc::c_char; let mut arg1: *mut libc::c_char = 0 as *mut libc::c_char; let mut ret: *mut libc::c_char = 1i32 as *mut libc::c_char; let mut sel_chat: *mut dc_chat_t = 0 as *mut dc_chat_t; - if !(context.is_null() || cmdline.is_null() || *cmdline.offset(0isize) as libc::c_int == 0i32) { + if !context.is_null() { if 0 != (*context).cmdline_sel_chat_id { sel_chat = dc_get_chat(context, (*context).cmdline_sel_chat_id) } - cmd = dc_strdup(cmdline); + cmd = dc_strdup(CString::new(cmdline).unwrap().as_ptr()); arg1 = strchr(cmd, ' ' as i32); if !arg1.is_null() { *arg1 = 0i32 as libc::c_char; diff --git a/examples/repl/main.rs b/examples/repl/main.rs index efdb3edf8..dc2fe7093 100644 --- a/examples/repl/main.rs +++ b/examples/repl/main.rs @@ -19,6 +19,9 @@ non_snake_case )] +use std::ffi::CString; +use std::io::{self, Write}; + use deltachat::constants::*; use deltachat::dc_aheader::*; use deltachat::dc_apeerstate::*; @@ -375,21 +378,13 @@ unsafe extern "C" fn stop_threads(mut context: *mut dc_context_t) { /* ****************************************************************************** * The main loop ******************************************************************************/ -#[cfg(not(target_os = "android"))] -unsafe extern "C" fn read_cmd() -> *mut libc::c_char { - printf(b"> \x00" as *const u8 as *const libc::c_char); - static mut cmdbuffer: [libc::c_char; 1024] = [0; 1024]; - fgets(cmdbuffer.as_mut_ptr(), 1000i32, __stdinp); - while strlen(cmdbuffer.as_mut_ptr()) > 0 - && (cmdbuffer[strlen(cmdbuffer.as_mut_ptr()).wrapping_sub(1) as usize] as libc::c_int - == '\n' as i32 - || cmdbuffer[strlen(cmdbuffer.as_mut_ptr()).wrapping_sub(1) as usize] as libc::c_int - == ' ' as i32) - { - cmdbuffer[strlen(cmdbuffer.as_mut_ptr()).wrapping_sub(1) as usize] = - '\u{0}' as i32 as libc::c_char - } - return cmdbuffer.as_mut_ptr(); +fn read_cmd() -> String { + print!("> "); + io::stdout().flush(); + + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + input.trim_end().to_string() } #[cfg(not(target_os = "android"))] @@ -427,9 +422,9 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib printf(b"Delta Chat Core is awaiting your commands.\n\x00" as *const u8 as *const libc::c_char); loop { /* read command */ - let mut cmdline: *const libc::c_char = read_cmd(); + let cmdline = read_cmd(); free(cmd as *mut libc::c_void); - cmd = dc_strdup(cmdline); + cmd = dc_strdup(CString::new(cmdline.clone()).unwrap().as_ptr()); let mut arg1: *mut libc::c_char = strchr(cmd, ' ' as i32); if !arg1.is_null() { *arg1 = 0i32 as libc::c_char; @@ -524,7 +519,7 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib break; } if !(*cmd.offset(0isize) as libc::c_int == 0i32) { - let mut execute_result: *mut libc::c_char = dc_cmdline(context, cmdline); + let mut execute_result: *mut libc::c_char = dc_cmdline(context, &cmdline); if !execute_result.is_null() { printf( b"%s\n\x00" as *const u8 as *const libc::c_char, @@ -543,7 +538,6 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib return 0i32; } -#[cfg(not(target_os = "android"))] pub fn main() { let mut args: Vec<*mut libc::c_char> = Vec::new(); for arg in ::std::env::args() { @@ -554,13 +548,12 @@ pub fn main() { ); } args.push(::std::ptr::null_mut()); - unsafe { - ::std::process::exit(main_0( + + let res = unsafe { + main_0( (args.len() - 1) as libc::c_int, args.as_mut_ptr() as *mut *mut libc::c_char, - ) as i32) - } + ) + }; + ::std::process::exit(res) } - -#[cfg(target_os = "android")] -fn main() {} diff --git a/misc.c b/misc.c index 3dc81e59e..83a825a80 100644 --- a/misc.c +++ b/misc.c @@ -6,7 +6,7 @@ #include "misc.h" -static char* dc_strdup(const char* s) /* strdup(NULL) is undefined, save_strdup(NULL) returns an empty string in this case */ +static char* internal_dc_strdup(const char* s) /* strdup(NULL) is undefined, save_strdup(NULL) returns an empty string in this case */ { char* ret = NULL; if (s) { @@ -37,13 +37,13 @@ char* dc_mprintf(const char* format, ...) va_end(argp); if (char_cnt_without_zero < 0) { va_end(argp_copy); - return dc_strdup("ErrFmt"); + return internal_dc_strdup("ErrFmt"); } buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */); if (buf==NULL) { va_end(argp_copy); - return dc_strdup("ErrMem"); + return internal_dc_strdup("ErrMem"); } vsnprintf(buf, char_cnt_without_zero+1, format, argp_copy); @@ -66,7 +66,7 @@ char* dc_mprintf(const char* format, ...) * pointer is equal to dc_strbuilder_t::buf. * If the given text is NULL, NULL is returned and the string-builder-object is not modified. */ -static char* dc_strbuilder_cat(dc_strbuilder_t* strbuilder, const char* text) +static char* internal_dc_strbuilder_cat(dc_strbuilder_t* strbuilder, const char* text) { // this function MUST NOT call logging functions as it is used to output the log if (strbuilder==NULL || text==NULL) { @@ -126,20 +126,20 @@ void dc_strbuilder_catf(dc_strbuilder_t* strbuilder, const char* format, ...) va_end(argp); if (char_cnt_without_zero < 0) { va_end(argp_copy); - dc_strbuilder_cat(strbuilder, "ErrFmt"); + internal_dc_strbuilder_cat(strbuilder, "ErrFmt"); return; } buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */); if (buf==NULL) { va_end(argp_copy); - dc_strbuilder_cat(strbuilder, "ErrMem"); + internal_dc_strbuilder_cat(strbuilder, "ErrMem"); return; } vsnprintf(buf, char_cnt_without_zero+1, format, argp_copy); va_end(argp_copy); - dc_strbuilder_cat(strbuilder, buf); + internal_dc_strbuilder_cat(strbuilder, buf); free(buf); } diff --git a/src/types.rs b/src/types.rs index a1df7cd7d..272430039 100644 --- a/src/types.rs +++ b/src/types.rs @@ -17,8 +17,6 @@ extern "C" { pub type _telldir; pub type mailstream_cancel; - #[cfg(not(target_os = "android"))] - pub static mut __stdinp: *mut FILE; } pub type sqlite_int64 = libc::int64_t;