wip porting

master
Lukáš Hozda 4 years ago
parent e5ea26ff17
commit aafde299a5

@ -210,12 +210,12 @@ pub type _IO_lock_t = ();
pub type FILE = _IO_FILE; pub type FILE = _IO_FILE;
/* structs */ /* structs */
#[derive(Copy, Clone)] #[derive(Clone)]
pub struct Link { pub struct Link {
pub next: *mut Link, pub next: Option<Box<Link>>,
pub which: libc::c_char, pub which: Option<char>,
pub key: libc::c_short, pub key: libc::c_short,
pub host: *mut libc::c_char, pub host: String,
pub port: *mut libc::c_char, pub port: *mut libc::c_char,
pub selector: *mut libc::c_char, pub selector: *mut libc::c_char,
} }
@ -232,8 +232,8 @@ pub struct Config {
} }
pub static mut tmpfilename: [libc::c_char; 256] = [0; 256]; pub static mut tmpfilename: [libc::c_char; 256] = [0; 256];
pub static mut links: *mut Link = 0 as *const Link as *mut Link; pub static mut links: Option<Box<Link>> = None;
pub static mut history: *mut Link = 0 as *const Link as *mut Link; pub static mut history: Option<Box<Link>> = None;
pub static mut link_key: libc::c_int = 0; pub static mut link_key: libc::c_int = 0;
pub static mut current_host: [libc::c_char; 512] = [0; 512]; pub static mut current_host: [libc::c_char; 512] = [0; 512];
pub static mut current_port: [libc::c_char; 64] = [0; 64]; pub static mut current_port: [libc::c_char; 64] = [0; 64];
@ -472,7 +472,7 @@ pub unsafe extern "C" fn init_config() {
}; };
} }
pub unsafe extern "C" fn dial( pub unsafe extern "C" fn dial(
mut host: *const libc::c_char, mut host: &str,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
) -> libc::c_int { ) -> libc::c_int {
@ -706,35 +706,31 @@ pub unsafe extern "C" fn make_key_str(
as libc::c_char as libc::c_char
}; };
} }
pub unsafe extern "C" fn add_link( pub unsafe fn add_link(
mut which: libc::c_char, mut which: libc::c_char,
mut name: *const libc::c_char, mut name: *const libc::c_char,
mut host: *const libc::c_char, mut host: String,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
) { ) {
let mut link: *mut Link = 0 as *mut Link;
let mut a: libc::c_char = 0 as libc::c_int as libc::c_char; let mut a: libc::c_char = 0 as libc::c_int as libc::c_char;
let mut b: libc::c_char = 0 as libc::c_int as libc::c_char; let mut b: libc::c_char = 0 as libc::c_int as libc::c_char;
let mut c: libc::c_char = 0 as libc::c_int as libc::c_char; let mut c: libc::c_char = 0 as libc::c_int as libc::c_char;
if host.is_null() || port.is_null() || selector.is_null() { if host.is_empty() || port.is_null() || selector.is_null() {
return; return;
} }
link = calloc( let mut link = Link {
1 as libc::c_int as libc::c_ulong, which: Some(which as u8 as char),
mem::size_of::<Link>() as libc::c_ulong, key: link_key as libc::c_short,
) as *mut Link; host: host,
(*link).which = which; port: strdup(port),
(*link).key = link_key as libc::c_short; selector: strdup(selector),
(*link).host = strdup(host); next: if links.is_none() {
(*link).port = strdup(port); None
(*link).selector = strdup(selector); } else { links.clone() }
if links.is_null() { };
(*link).next = 0 as *mut Link
} else { links = Some(Box::new(link));
(*link).next = links
}
links = link;
let fresh4 = link_key; let fresh4 = link_key;
link_key = link_key + 1; link_key = link_key + 1;
make_key_str(fresh4, &mut a, &mut b, &mut c); make_key_str(fresh4, &mut a, &mut b, &mut c);
@ -749,37 +745,23 @@ pub unsafe extern "C" fn add_link(
); );
} }
pub unsafe extern "C" fn clear_links() { pub unsafe extern "C" fn clear_links() {
let mut link: *mut Link = 0 as *mut Link; links = None;
let mut next: *mut Link = 0 as *mut Link;
link = links;
while !link.is_null() {
next = (*link).next;
free((*link).host as *mut libc::c_void);
free((*link).port as *mut libc::c_void);
free((*link).selector as *mut libc::c_void);
free(link as *mut libc::c_void);
link = next
}
links = 0 as *mut Link;
link_key = 0 as libc::c_int; link_key = 0 as libc::c_int;
} }
pub unsafe extern "C" fn add_history() { pub unsafe extern "C" fn add_history() {
let mut link: *mut Link = 0 as *mut Link; let mut link: Link = Link {
link = calloc( which: None,
1 as libc::c_int as libc::c_ulong, key: 0 as libc::c_int as libc::c_short,
mem::size_of::<Link>() as libc::c_ulong, host: CString::new(current_host.iter().take_while(|x| **x != 0).map(|x| *x as u8).collect::<Vec<_>>()).unwrap().into_string().unwrap(),
) as *mut Link; port: strdup(current_port.as_mut_ptr()),
(*link).host = strdup(current_host.as_mut_ptr()); selector: strdup(current_selector.as_mut_ptr()),
(*link).port = strdup(current_port.as_mut_ptr()); next: if history.is_none() {
(*link).selector = strdup(current_selector.as_mut_ptr()); None
(*link).which = 0 as libc::c_int as libc::c_char;
(*link).key = 0 as libc::c_int as libc::c_short;
if history.is_null() {
(*link).next = 0 as *mut Link
} else { } else {
(*link).next = history history.clone()
} }
history = link; };
history = Some(Box::new(link));
} }
pub unsafe extern "C" fn handle_directory_line(mut line: *mut libc::c_char) { pub unsafe extern "C" fn handle_directory_line(mut line: *mut libc::c_char) {
let mut i: libc::c_int = 0; let mut i: libc::c_int = 0;
@ -822,10 +804,10 @@ pub unsafe extern "C" fn handle_directory_line(mut line: *mut libc::c_char) {
48 | 49 | 53 | 55 | 56 | 57 | 103 | 73 | 112 | 104 | 115 => { 48 | 49 | 53 | 55 | 56 | 57 | 103 | 73 | 112 | 104 | 115 => {
add_link( add_link(
*line.offset(0 as libc::c_int as isize), *line.offset(0 as libc::c_int as isize),
fields[0 as libc::c_int as usize], fields[0],
fields[2 as libc::c_int as usize], CString::from_raw(fields[2]).into_string().unwrap(),
fields[3 as libc::c_int as usize], fields[3],
fields[1 as libc::c_int as usize], fields[1],
); );
} }
_ => { _ => {
@ -849,7 +831,7 @@ pub unsafe extern "C" fn is_valid_directory_entry(
}; };
} }
pub unsafe extern "C" fn view_directory( pub unsafe extern "C" fn view_directory(
mut host: *const libc::c_char, mut host: String,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
mut make_current: libc::c_int, mut make_current: libc::c_int,
@ -936,7 +918,7 @@ pub unsafe extern "C" fn view_directory(
} }
pub unsafe extern "C" fn view_file( pub unsafe extern "C" fn view_file(
mut cmd: *const libc::c_char, mut cmd: *const libc::c_char,
mut host: *const libc::c_char, mut host: &str,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
) { ) {
@ -1019,7 +1001,7 @@ pub unsafe extern "C" fn view_file(
unlink(tmpfilename.as_mut_ptr()); unlink(tmpfilename.as_mut_ptr());
} }
pub unsafe extern "C" fn view_telnet( pub unsafe extern "C" fn view_telnet(
mut host: *const libc::c_char, mut host: &str,
mut port: *const libc::c_char, mut port: *const libc::c_char,
) { ) {
let mut pid: pid_t = 0; let mut pid: pid_t = 0;
@ -1049,7 +1031,7 @@ pub unsafe extern "C" fn view_telnet(
puts(b"(done)\x00" as *const u8 as *const libc::c_char); puts(b"(done)\x00" as *const u8 as *const libc::c_char);
} }
pub unsafe extern "C" fn view_download( pub unsafe extern "C" fn view_download(
mut host: *const libc::c_char, mut host: &str,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
) { ) {
@ -1102,7 +1084,7 @@ pub unsafe extern "C" fn view_download(
}; };
} }
pub unsafe extern "C" fn view_search( pub unsafe extern "C" fn view_search(
mut host: *const libc::c_char, mut host: &str,
mut port: *const libc::c_char, mut port: *const libc::c_char,
mut selector: *const libc::c_char, mut selector: *const libc::c_char,
) { ) {
@ -1132,15 +1114,15 @@ pub unsafe extern "C" fn view_history(mut key: libc::c_int) {
let mut a: libc::c_char = 0; let mut a: libc::c_char = 0;
let mut b: libc::c_char = 0; let mut b: libc::c_char = 0;
let mut c: libc::c_char = 0; let mut c: libc::c_char = 0;
let mut link: *mut Link = 0 as *mut Link; let mut link: Option<Box<Link>> = None;
if history.is_null() { if history.is_none() {
puts(b"(empty history)\x00" as *const u8 as *const libc::c_char); println!("(empty history)");
return; return;
} }
if key < 0 as libc::c_int { if key < 0 as libc::c_int {
puts(b"(history)\x00" as *const u8 as *const libc::c_char); puts(b"(history)\x00" as *const u8 as *const libc::c_char);
link = history; link = history.clone();
while !link.is_null() { while let Some(l) = link {
let fresh10 = history_key; let fresh10 = history_key;
history_key = history_key + 1; history_key = history_key + 1;
make_key_str(fresh10, &mut a, &mut b, &mut c); make_key_str(fresh10, &mut a, &mut b, &mut c);
@ -1151,29 +1133,29 @@ pub unsafe extern "C" fn view_history(mut key: libc::c_int) {
a as libc::c_int, a as libc::c_int,
b as libc::c_int, b as libc::c_int,
c as libc::c_int, c as libc::c_int,
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
); );
link = (*link).next link = (*l).next
} }
} else { } else {
/* traverse history list */ /* traverse history list */
link = history; link = history.clone();
while !link.is_null() { while let Some(l) = link {
if history_key == key { if history_key == key {
view_directory( view_directory(
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
0 as libc::c_int, 0 as libc::c_int,
); );
return; return;
} }
link = (*link).next; link = (*l).next;
history_key += 1 history_key += 1
} }
puts(b"history item not found\x00" as *const u8 as *const libc::c_char); println!("history item not found");
}; };
} }
pub unsafe extern "C" fn view_bookmarks(mut key: libc::c_int) { pub unsafe extern "C" fn view_bookmarks(mut key: libc::c_int) {
@ -1214,7 +1196,7 @@ pub unsafe extern "C" fn view_bookmarks(mut key: libc::c_int) {
) != 0 ) != 0
{ {
view_directory( view_directory(
parsed_host.as_mut_ptr(), CString::from_raw(parsed_host).into_string().unwrap(),
parsed_port.as_mut_ptr(), parsed_port.as_mut_ptr(),
parsed_selector.as_mut_ptr(), parsed_selector.as_mut_ptr(),
0 as libc::c_int, 0 as libc::c_int,
@ -1233,96 +1215,91 @@ pub unsafe extern "C" fn view_bookmarks(mut key: libc::c_int) {
} }
}; };
} }
pub unsafe extern "C" fn pop_history() { pub fn pop_history() {
let mut next: *mut Link = 0 as *mut Link; match unsafe { &history } {
if history.is_null() { None => println!("(empty history)"),
puts(b"(empty history)\x00" as *const u8 as *const libc::c_char); Some(h) => {
return;
}
/* reload page from history (and don't count as history) */ /* reload page from history (and don't count as history) */
view_directory( unsafe { view_directory(
(*history).host, (*h).host,
(*history).port, (*h).port,
(*history).selector, (*h).selector,
0 as libc::c_int, 0 as libc::c_int,
); ); }
/* history is history... :) */ /* history is history... :) */
next = (*history).next; unsafe { history = h.next.clone(); }
free((*history).host as *mut libc::c_void); },
free((*history).port as *mut libc::c_void); }
free((*history).selector as *mut libc::c_void);
free(history as *mut libc::c_void);
history = next;
} }
pub unsafe extern "C" fn follow_link(mut key: libc::c_int) -> libc::c_int { pub unsafe extern "C" fn follow_link(mut key: libc::c_int) -> libc::c_int {
let mut link: *mut Link = 0 as *mut Link; let mut link: Option<Box<Link>> = links.clone();
link = links;
while !link.is_null() { while let Some(ref l) = link {
if (*link).key as libc::c_int != key { if (*l).key as libc::c_int != key {
link = (*link).next link = (*l).next.clone()
} else { } else if let Some(w) = (*l).which {
match (*link).which as libc::c_int { match w {
48 => { '0' => {
view_file( view_file(
CString::new(config.cmd_text.clone()) CString::new(config.cmd_text.clone())
.unwrap() .unwrap()
.into_raw(), .into_raw(),
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
); );
} }
49 => { '1' => {
view_directory( view_directory(
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
1 as libc::c_int, 1 as libc::c_int,
); );
} }
55 => { '7' => {
view_search((*link).host, (*link).port, (*link).selector); view_search((*l).host, (*l).port, (*l).selector);
} }
53 | 57 => { '5' | '9' => {
view_download((*link).host, (*link).port, (*link).selector); view_download((*l).host, (*l).port, (*l).selector);
} }
56 => { '8' => {
view_telnet((*link).host, (*link).port); view_telnet((*l).host, (*l).port);
} }
103 | 73 | 112 => { 'f' | 'I' | 'p' => {
view_file( view_file(
CString::new(config.cmd_image.clone()) CString::new(config.cmd_image.clone())
.unwrap() .unwrap()
.into_raw(), .into_raw(),
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
); );
} }
104 => { 'h' => {
view_file( view_file(
CString::new(config.cmd_browser.clone()) CString::new(config.cmd_browser.clone())
.unwrap() .unwrap()
.into_raw(), .into_raw(),
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
); );
} }
115 => { 's' => {
view_file( view_file(
CString::new(config.cmd_player.clone()) CString::new(config.cmd_player.clone())
.unwrap() .unwrap()
.into_raw(), .into_raw(),
(*link).host, (*l).host,
(*link).port, (*l).port,
(*link).selector, (*l).selector,
); );
} }
_ => { _ => {
printf( printf(
b"missing handler [%c]\n\x00" as *const u8 as *const libc::c_char, b"missing handler [%c]\n\x00" as *const u8 as *const libc::c_char,
(*link).which as libc::c_int, w as u8 as libc::c_int,
); );
} }
} }
@ -1333,17 +1310,16 @@ pub unsafe extern "C" fn follow_link(mut key: libc::c_int) -> libc::c_int {
return 0 as libc::c_int; return 0 as libc::c_int;
} }
pub unsafe extern "C" fn download_link(mut key: libc::c_int) { pub unsafe extern "C" fn download_link(mut key: libc::c_int) {
let mut link: *mut Link = 0 as *mut Link; let mut link: Option<Box<Link>> = links.clone();
link = links; while let Some(l) = link {
while !link.is_null() { if (*l).key as libc::c_int != key {
if (*link).key as libc::c_int != key { link = (*l).next
link = (*link).next
} else { } else {
view_download((*link).host, (*link).port, (*link).selector); view_download((*l).host, (*l).port, (*l).selector);
return; return;
} }
} }
puts(b"link not found\x00" as *const u8 as *const libc::c_char); println!("link not found");
} }
/* function prototypes */ /* function prototypes */
pub unsafe extern "C" fn parse_uri(mut uri: *const libc::c_char) -> libc::c_int { pub unsafe extern "C" fn parse_uri(mut uri: *const libc::c_char) -> libc::c_int {

Loading…
Cancel
Save