Merge pull request #23 from deltachat/fix-linking

Fix linking
This commit is contained in:
Lars-Magnus Skog
2019-05-01 19:35:58 +02:00
committed by GitHub
6 changed files with 33 additions and 50 deletions

View File

@@ -18,9 +18,7 @@ fn add_search_path(p: &str) {
fn build_tools() {
let mut config = cc::Build::new();
config.file("misc.h");
config.file("misc.c");
config.compile("libtools.a");
config.file("misc.c").compile("libtools.a");
println!("rerun-if-changed=build.rs");
println!("rerun-if-changed=misc.h");

View File

@@ -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;

View File

@@ -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().unwrap();
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() {}

14
misc.c
View File

@@ -6,7 +6,7 @@
#include "misc.h"
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.
*/
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);
}

10
misc.h
View File

@@ -2,12 +2,6 @@
typedef struct _dc_strbuilder dc_strbuilder_t;
char* dc_mprintf (const char* format, ...); /* The result must be free()'d. */
char* dc_strdup(const char* s);
struct _dc_strbuilder
{
char* buf;
@@ -17,7 +11,5 @@ struct _dc_strbuilder
};
//void dc_strbuilder_init (dc_strbuilder_t*, int init_bytes);
char* dc_strbuilder_cat (dc_strbuilder_t*, const char* text);
char* dc_mprintf (const char* format, ...); /* The result must be free()'d. */
void dc_strbuilder_catf (dc_strbuilder_t*, const char* format, ...);
//void dc_strbuilder_empty (dc_strbuilder_t*);

View File

@@ -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;