remove more C stuff

master
Lukáš Hozda 4 years ago
parent c145d691d0
commit 139a5f1023

@ -28,9 +28,6 @@ use simple_input::input;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
extern "C" { extern "C" {
pub type _IO_wide_data;
pub type _IO_codecvt;
pub type _IO_marker;
#[no_mangle] #[no_mangle]
fn waitpid( fn waitpid(
__pid: __pid_t, __pid: __pid_t,
@ -57,8 +54,6 @@ extern "C" {
__pai: *mut *mut addrinfo, __pai: *mut *mut addrinfo,
) -> libc::c_int; ) -> libc::c_int;
#[no_mangle] #[no_mangle]
fn freeaddrinfo(__ai: *mut addrinfo);
#[no_mangle]
fn open(__file: *const libc::c_char, __oflag: libc::c_int, _: ...) -> libc::c_int; fn open(__file: *const libc::c_char, __oflag: libc::c_int, _: ...) -> libc::c_int;
#[no_mangle] #[no_mangle]
fn close(__fd: libc::c_int) -> libc::c_int; fn close(__fd: libc::c_int) -> libc::c_int;
@ -80,18 +75,8 @@ extern "C" {
#[no_mangle] #[no_mangle]
fn fork() -> __pid_t; fn fork() -> __pid_t;
#[no_mangle] #[no_mangle]
fn unlink(__name: *const libc::c_char) -> libc::c_int;
#[no_mangle]
fn __errno_location() -> *mut libc::c_int; fn __errno_location() -> *mut libc::c_int;
#[no_mangle] #[no_mangle]
static mut stderr: *mut FILE;
#[no_mangle]
fn fclose(__stream: *mut FILE) -> libc::c_int;
#[no_mangle]
fn fopen(_: *const libc::c_char, _: *const libc::c_char) -> *mut FILE;
#[no_mangle]
fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;
#[no_mangle]
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int; fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
#[no_mangle] #[no_mangle]
fn snprintf( fn snprintf(
@ -101,20 +86,10 @@ extern "C" {
_: ... _: ...
) -> libc::c_int; ) -> libc::c_int;
#[no_mangle] #[no_mangle]
fn fgetc(__stream: *mut FILE) -> libc::c_int;
#[no_mangle]
fn fputs(__s: *const libc::c_char, __stream: *mut FILE) -> libc::c_int;
#[no_mangle]
fn puts(__s: *const libc::c_char) -> libc::c_int; fn puts(__s: *const libc::c_char) -> libc::c_int;
#[no_mangle] #[no_mangle]
fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void; fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void;
#[no_mangle] #[no_mangle]
fn free(__ptr: *mut libc::c_void);
#[no_mangle]
fn getenv(__name: *const libc::c_char) -> *mut libc::c_char;
#[no_mangle]
fn mkstemp(__template: *mut libc::c_char) -> libc::c_int;
#[no_mangle]
fn memset( fn memset(
_: *mut libc::c_void, _: *mut libc::c_void,
_: libc::c_int, _: libc::c_int,
@ -179,41 +154,6 @@ pub struct addrinfo {
pub ai_canonname: *mut libc::c_char, pub ai_canonname: *mut libc::c_char,
pub ai_next: *mut addrinfo, pub ai_next: *mut addrinfo,
} }
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
pub _flags: libc::c_int,
pub _IO_read_ptr: *mut libc::c_char,
pub _IO_read_end: *mut libc::c_char,
pub _IO_read_base: *mut libc::c_char,
pub _IO_write_base: *mut libc::c_char,
pub _IO_write_ptr: *mut libc::c_char,
pub _IO_write_end: *mut libc::c_char,
pub _IO_buf_base: *mut libc::c_char,
pub _IO_buf_end: *mut libc::c_char,
pub _IO_save_base: *mut libc::c_char,
pub _IO_backup_base: *mut libc::c_char,
pub _IO_save_end: *mut libc::c_char,
pub _markers: *mut _IO_marker,
pub _chain: *mut _IO_FILE,
pub _fileno: libc::c_int,
pub _flags2: libc::c_int,
pub _old_offset: __off_t,
pub _cur_column: libc::c_ushort,
pub _vtable_offset: libc::c_schar,
pub _shortbuf: [libc::c_char; 1],
pub _lock: *mut libc::c_void,
pub _offset: __off64_t,
pub _codecvt: *mut _IO_codecvt,
pub _wide_data: *mut _IO_wide_data,
pub _freeres_list: *mut _IO_FILE,
pub _freeres_buf: *mut libc::c_void,
pub __pad5: size_t,
pub _mode: libc::c_int,
pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
/* structs */ /* structs */
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -234,7 +174,7 @@ pub struct Config {
pub cmd_player: String, pub cmd_player: String,
pub color_prompt: String, pub color_prompt: String,
pub color_selector: String, pub color_selector: String,
pub verbose: String, pub verbose: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -275,7 +215,7 @@ impl BrowserState {
cmd_player: String::from("mplayer"), cmd_player: String::from("mplayer"),
color_prompt: String::from("1;34"), color_prompt: String::from("1;34"),
color_selector: String::from("1;32"), color_selector: String::from("1;32"),
verbose: String::from("true"), verbose: false,
}, },
} }
} }
@ -292,7 +232,7 @@ impl BrowserState {
"cmd_player" => self.config.cmd_player = words[1].to_string(), "cmd_player" => self.config.cmd_player = words[1].to_string(),
"color_prompt" => self.config.color_prompt = words[1].to_string(), "color_prompt" => self.config.color_prompt = words[1].to_string(),
"color_selector" => self.config.color_selector = words[1].to_string(), "color_selector" => self.config.color_selector = words[1].to_string(),
"verbose" => self.config.verbose = words[1].to_string(), "verbose" => self.config.verbose = words[1].parse().unwrap_or_default(),
x if x.starts_with("bookmark") => self.bookmarks.push(words[1].to_string()), x if x.starts_with("bookmark") => self.bookmarks.push(words[1].to_string()),
x => { x => {
eprintln!("invalid key in config: {}", x); eprintln!("invalid key in config: {}", x);
@ -332,9 +272,7 @@ impl BrowserState {
let mut len: usize = 0; let mut len: usize = 0;
let mut total: usize = 0; let mut total: usize = 0;
let mut buffer: [u8; 4096] = [0; 4096]; let mut buffer: [u8; 4096] = [0; 4096];
if check_option_true( if self.config.verbose
CString::new(self.config.verbose.clone()).unwrap().into_raw(),
) != 0
{ {
println!("downloading [{}]...", selector); println!("downloading [{}]...", selector);
} }
@ -354,17 +292,13 @@ impl BrowserState {
} }
file.write(&buffer[..len]).expect("failed to write to file"); file.write(&buffer[..len]).expect("failed to write to file");
total += len; total += len;
if check_option_true( if self.config.verbose
CString::new(self.config.verbose.clone()).unwrap().into_raw(),
) != 0
{ {
println!("downloading [{}] ({} kb)...", selector, total / 1024); println!("downloading [{}] ({} kb)...", selector, total / 1024);
} }
} }
close(srvfd); close(srvfd);
if check_option_true( if self.config.verbose
CString::new(self.config.verbose.clone()).unwrap().into_raw(),
) != 0
{ {
println!("downloading [{}] complete", selector); println!("downloading [{}] complete", selector);
} }
@ -394,7 +328,7 @@ impl BrowserState {
pub unsafe fn add_link( pub unsafe fn add_link(
&mut self, &mut self,
mut which: libc::c_char, mut which: char,
mut name: String, mut name: String,
mut host: String, mut host: String,
mut port: usize, mut port: usize,
@ -450,58 +384,38 @@ impl BrowserState {
self.history = Some(Box::new(link)); self.history = Some(Box::new(link));
} }
pub unsafe fn handle_directory_line(&mut self, mut line: *mut libc::c_char) { pub unsafe fn handle_directory_line(&mut self, line: &str) {
let mut i: libc::c_int = 0; let fields = {
let mut lp: *mut u8 = 0 as *mut u8; let mut v = line[1..].split('\t').collect::<Vec<_>>();
let mut last: *mut u8 = 0 as *mut u8; v.retain(|x| !x.is_empty());
let mut fields: [*mut u8; 4] = [0 as *mut u8; 4]; v
/* tokenize */ };
i = 0 as libc::c_int;
last = &mut *line.offset(1 as libc::c_int as isize) as *mut i8 as *mut u8;
lp = last;
i = 0 as libc::c_int;
while i < 4 as libc::c_int {
if *lp as libc::c_int == '\t' as i32 || *lp as libc::c_int == '\u{0}' as i32 {
fields[i as usize] = last;
last = lp.offset(1 as libc::c_int as isize);
if *lp as libc::c_int == '\u{0}' as i32 {
break;
}
*lp = '\0' as u8;
i += 1
}
lp = lp.offset(1)
}
/* determine listing type */ /* determine listing type */
match *line.offset(0 as libc::c_int as isize) as libc::c_int { match line.chars().next() {
105 | 51 => { Some('i') | Some('3') => println!(" {}", fields[0]),
printf(b" %s\n\x00" as *const u8 as *const libc::c_char, fields[0]); Some('.') => println!("\0"), // some gopher servers use this
} Some(w @ '0')
46 => { | Some(w @ '1')
/* some gopher servers use this */ | Some(w @ '5')
puts(b"\x00" as *const u8 as *const libc::c_char); | Some(w @ '7')
} | Some(w @ '8')
48 | 49 | 53 | 55 | 56 | 57 | 103 | 73 | 112 | 104 | 115 => { | Some(w @ '9')
| Some(w @ 'g')
| Some(w @ 'I')
| Some(w @ 'p')
| Some(w @ 'h')
| Some(w @ 's') => {
self.add_link( self.add_link(
*line.offset(0 as libc::c_int as isize), w,
CString::from_raw(fields[0] as *mut i8).into_string().unwrap(), fields[0].to_string(),
CString::from_raw(fields[2] as *mut i8).into_string().unwrap(), fields[2].to_string(),
CString::from_raw(fields[3] as *mut i8) fields[3].parse().unwrap_or(70), // todo oof
.into_string() fields[1].to_string(),
.unwrap()
.parse()
.unwrap(),
CString::from_raw(fields[1] as *mut i8).into_string().unwrap(),
); );
} },
_ => { Some(x) => println!("miss [{}]: {}", x, fields[0]),
printf( None => (),
b"miss [%c]: %s\n\x00" as *const u8 as *const libc::c_char, }
*line.offset(0 as libc::c_int as isize) as libc::c_int,
fields[0],
);
}
};
} }
pub unsafe fn view_directory( pub unsafe fn view_directory(
@ -579,8 +493,8 @@ impl BrowserState {
} }
i = 0 as libc::c_int; i = 0 as libc::c_int;
while i < head_read { while i < head_read {
self.handle_directory_line(head[i as usize].as_mut_ptr()); self.handle_directory_line(&CString::from_raw(head[i as usize].as_mut_ptr()).into_string().unwrap());
i += 1 i += 1;
} }
while read_line( while read_line(
srvfd, srvfd,
@ -588,14 +502,14 @@ impl BrowserState {
mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong, mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
) != 0 ) != 0
{ {
self.handle_directory_line(line.as_mut_ptr()); self.handle_directory_line(&CString::from_raw(line.as_mut_ptr()).into_string().unwrap());
} }
close(srvfd); close(srvfd);
} }
pub unsafe fn view_file( pub unsafe fn view_file(
&mut self, &mut self,
mut cmd: *const libc::c_char, mut cmd: &str,
mut host: &str, mut host: &str,
mut port: usize, mut port: usize,
mut selector: &str, mut selector: &str,
@ -607,9 +521,7 @@ impl BrowserState {
let mut buffer: [libc::c_char; 1024] = [0; 1024]; let mut buffer: [libc::c_char; 1024] = [0; 1024];
let mut argv: [*mut libc::c_char; 32] = [0 as *mut libc::c_char; 32]; let mut argv: [*mut libc::c_char; 32] = [0 as *mut libc::c_char; 32];
let mut p: *mut libc::c_char = 0 as *mut libc::c_char; let mut p: *mut libc::c_char = 0 as *mut libc::c_char;
if check_option_true( if self.config.verbose
CString::new(self.config.verbose.clone()).unwrap().into_raw(),
) != 0
{ {
println!("h({}) p({}) s({})", host, port, selector); println!("h({}) p({}) s({})", host, port, selector);
} }
@ -656,15 +568,10 @@ impl BrowserState {
CString::new(self.tmpfilename.clone()).unwrap().into_raw(); CString::new(self.tmpfilename.clone()).unwrap().into_raw();
argv[j as usize] = 0 as *mut libc::c_char; argv[j as usize] = 0 as *mut libc::c_char;
/* fork and execute */ /* fork and execute */
if check_option_true( if self.config.verbose
CString::new(self.config.verbose.clone()).unwrap().into_raw(),
) != 0
{ {
printf( println!("executing: {} {}", cmd, self.tmpfilename);
b"executing: %s %s\n\x00" as *const u8 as *const libc::c_char, /* to wait for browsers etc. that return immediately */
cmd,
self.tmpfilename.as_mut_ptr(),
); /* to wait for browsers etc. that return immediatly */
} }
pid = fork(); pid = fork();
if pid == 0 as libc::c_int { if pid == 0 as libc::c_int {
@ -866,9 +773,7 @@ impl BrowserState {
match w { match w {
'0' => { '0' => {
self.view_file( self.view_file(
CString::new(self.config.cmd_text.clone()) &self.config.cmd_text,
.unwrap()
.into_raw(),
&(*l).host, &(*l).host,
(*l).port, (*l).port,
&(*l).selector, &(*l).selector,
@ -893,9 +798,7 @@ impl BrowserState {
} }
'f' | 'I' | 'p' => { 'f' | 'I' | 'p' => {
self.view_file( self.view_file(
CString::new(self.config.cmd_image.clone()) &self.config.cmd_image,
.unwrap()
.into_raw(),
&(*l).host, &(*l).host,
(*l).port, (*l).port,
&(*l).selector, &(*l).selector,
@ -903,9 +806,7 @@ impl BrowserState {
} }
'h' => { 'h' => {
self.view_file( self.view_file(
CString::new(self.config.cmd_browser.clone()) &self.config.cmd_browser,
.unwrap()
.into_raw(),
&(*l).host, &(*l).host,
(*l).port, (*l).port,
&(*l).selector, &(*l).selector,
@ -913,9 +814,7 @@ impl BrowserState {
} }
's' => { 's' => {
self.view_file( self.view_file(
CString::new(self.config.cmd_player.clone()) &self.config.cmd_player,
.unwrap()
.into_raw(),
&(*l).host, &(*l).host,
(*l).port, (*l).port,
&(*l).selector, &(*l).selector,
@ -1147,11 +1046,6 @@ pub fn banner(to_error: bool) {
println!("cgo 0.6.1 Copyright (c) 2020 Sebastian Steinhauer"); println!("cgo 0.6.1 Copyright (c) 2020 Sebastian Steinhauer");
} }
} }
pub unsafe fn check_option_true(mut option: *const libc::c_char) -> libc::c_int {
return (strcasecmp(option, b"false\x00" as *const u8 as *const libc::c_char) != 0
&& strcasecmp(option, b"off\x00" as *const u8 as *const libc::c_char) != 0)
as libc::c_int;
}
pub unsafe fn dial(mut host: &str, mut port: usize, mut selector: &str) -> libc::c_int { pub unsafe fn dial(mut host: &str, mut port: usize, mut selector: &str) -> libc::c_int {
let mut hints: addrinfo = addrinfo { let mut hints: addrinfo = addrinfo {
@ -1202,7 +1096,6 @@ pub unsafe fn dial(mut host: &str, mut port: usize, mut selector: &str) -> libc:
} }
r = (*r).ai_next r = (*r).ai_next
} }
freeaddrinfo(res);
if r.is_null() { if r.is_null() {
eprintln!("error: cannot connect to host '{}:{}'", host, port); eprintln!("error: cannot connect to host '{}:{}'", host, port);
return -(1 as libc::c_int); return -(1 as libc::c_int);

Loading…
Cancel
Save