remove all instances of unsafe, plus minor changes

master
Lukáš Hozda 4 years ago
parent 3d540d8a3d
commit 77c3f6db7b

@ -1,5 +1,3 @@
#![feature(main)]
extern crate tempfile;
extern crate simple_input;
@ -80,7 +78,7 @@ impl BrowserState {
}
}
unsafe fn parse_config_line(&mut self, line: &str) {
fn parse_config_line(&mut self, line: &str) {
let trimmed_line = line.trim_start();
let words = trimmed_line.split_whitespace().take(2).collect::<Vec<_>>();
@ -101,7 +99,7 @@ impl BrowserState {
}
}
pub unsafe fn load_config<P: AsRef<Path>>(&mut self, filename: P) {
pub fn load_config<P: AsRef<Path>>(&mut self, filename: P) {
let file = match File::open(filename) {
Ok(f) => BufReader::new(f),
Err(_) => {
@ -114,7 +112,7 @@ impl BrowserState {
.for_each(|line| self.parse_config_line(&line))
}
pub unsafe fn init_config(&mut self) {
pub fn init_config(&mut self) {
/* read configs */
self.load_config("/etc/cgorc"); /* ignore incomplete selectors */
if let Some(dir) = env::home_dir() {
@ -122,7 +120,7 @@ impl BrowserState {
}
}
pub unsafe fn download_file(
pub fn download_file(
&mut self,
host: &str,
port: u16,
@ -152,7 +150,7 @@ impl BrowserState {
true
}
pub unsafe fn download_temp(
pub fn download_temp(
&mut self,
host: &str,
port: u16,
@ -173,7 +171,7 @@ impl BrowserState {
return true;
}
pub unsafe fn add_link(
pub fn add_link(
&mut self,
which: char,
name: String,
@ -203,12 +201,12 @@ impl BrowserState {
println!("{}{}{} {}", a, b, c, name);
}
pub unsafe fn clear_links(&mut self) {
pub fn clear_links(&mut self) {
self.links = None;
self.link_key = 0;
}
pub unsafe fn add_history(&mut self) {
pub fn add_history(&mut self) {
let link: Link = Link {
which: None,
key: 0,
@ -220,7 +218,7 @@ impl BrowserState {
self.history = Some(Box::new(link));
}
pub unsafe fn handle_directory_line(&mut self, line: &str) {
pub fn handle_directory_line(&mut self, line: &str) {
let fields = {
let mut v = line[1..].split('\t').collect::<Vec<_>>();
v.retain(|x| !x.is_empty());
@ -270,7 +268,7 @@ impl BrowserState {
}
}
pub unsafe fn view_directory(
pub fn view_directory(
&mut self,
host: &str,
port: u16,
@ -318,7 +316,7 @@ impl BrowserState {
}
}
pub unsafe fn view_file(
pub fn view_file(
&mut self,
cmd: &str,
host: &str,
@ -349,7 +347,7 @@ impl BrowserState {
fs::remove_file(&self.tmpfilename).expect("failed to delete temp file");
}
pub unsafe fn view_download(
pub fn view_download(
&mut self,
host: &str,
port: u16,
@ -384,7 +382,7 @@ impl BrowserState {
}
}
pub unsafe fn view_search(
pub fn view_search(
&mut self,
host: &str,
port: u16,
@ -400,7 +398,7 @@ impl BrowserState {
self.view_directory(host, port, &search_selector, true);
}
pub unsafe fn view_history(&mut self, key: Option<usize>) {
pub fn view_history(&mut self, key: Option<usize>) {
let mut history_key: usize = 0;
let mut a: char = '\0';
let mut b: char = '\0';
@ -440,7 +438,7 @@ impl BrowserState {
};
}
pub unsafe fn view_bookmarks(&mut self, key: Option<usize>) {
pub fn view_bookmarks(&mut self, key: Option<usize>) {
let mut a: char = '\0';
let mut b: char = '\0';
let mut c: char = '\0';
@ -474,21 +472,19 @@ impl BrowserState {
None => println!("(empty history)"),
Some(h) => {
/* reload page from history (and don't count as history) */
unsafe {
self.view_directory(
&(*h).host,
(*h).port,
&(*h).selector,
false,
);
}
self.view_directory(
&(*h).host,
(*h).port,
&(*h).selector,
false,
);
/* history is history... :) */
self.history = h.next.clone();
}
}
}
pub unsafe fn follow_link(&mut self, key: usize) -> bool {
pub fn follow_link(&mut self, key: usize) -> bool {
let mut link: Option<Box<Link>> = self.links.clone();
while let Some(ref l) = link {
@ -556,7 +552,7 @@ impl BrowserState {
return false;
}
pub unsafe fn download_link(&mut self, key: usize) {
pub fn download_link(&mut self, key: usize) {
let mut link: Option<Box<Link>> = self.links.clone();
while let Some(l) = link {
if (*l).key != key {
@ -570,7 +566,7 @@ impl BrowserState {
}
/* function prototypes */
pub unsafe fn parse_uri(&mut self, uri: &str) -> bool {
pub fn parse_uri(&mut self, uri: &str) -> bool {
let mut tmp: &str = &uri[..];
/* strip gopher:// */
if uri.starts_with("gopher://") {
@ -613,7 +609,7 @@ impl BrowserState {
true
}
pub unsafe fn init(&mut self, argc: usize, argv: Vec<String>) -> i32 {
pub fn init(&mut self, argc: usize, argv: Vec<String>) -> i32 {
let mut i: usize = 1;
/* copy defaults */
self.init_config();
@ -738,7 +734,7 @@ pub fn banner(to_error: bool) {
}
}
pub unsafe fn dial(
pub fn dial(
host: &str,
port: u16,
selector: &str,
@ -759,7 +755,7 @@ pub unsafe fn dial(
Some(stream)
}
pub unsafe fn make_key(c1: char, c2: char, c3: char) -> Option<usize> {
pub fn make_key(c1: char, c2: char, c3: char) -> Option<usize> {
if c1 == '\0' || c2 == '\0' {
return None;
}
@ -778,7 +774,7 @@ pub unsafe fn make_key(c1: char, c2: char, c3: char) -> Option<usize> {
)
}
}
pub unsafe fn make_key_str(
pub fn make_key_str(
key: usize,
c1: &mut char,
c2: &mut char,
@ -807,7 +803,7 @@ pub unsafe fn make_key_str(
};
}
pub unsafe fn is_valid_directory_entry(kind: char) -> bool {
pub fn is_valid_directory_entry(kind: char) -> bool {
match kind {
'i' | '3' | '.' | '0' | '1' | '5' | '7' | '8' | '9' | 'g' | 'I' | 'p' | 'h'
| 's' => true,
@ -815,7 +811,7 @@ pub unsafe fn is_valid_directory_entry(kind: char) -> bool {
}
}
pub unsafe fn view_telnet(host: &str, port: u16) {
pub fn view_telnet(host: &str, port: u16) {
println!("executing: telnet {} {}", host, port);
// TODO check stdio
@ -829,9 +825,8 @@ pub unsafe fn view_telnet(host: &str, port: u16) {
println!("(done)");
}
#[main]
pub fn main() {
fn main() {
let mut state = BrowserState::new();
let args: Vec<String> = env::args().collect();
unsafe { exit(state.init(args.len() - 1, args) as i32) }
exit(state.init(args.len() - 1, args) as i32)
}

Loading…
Cancel
Save