| 221 | |
| 222 | #[no_mangle] |
| 223 | fn main() { |
| 224 | // A vector of previous pages visited |
| 225 | let mut back: Vec<Page> = Vec::new(); |
| 226 | |
| 227 | // Pages in the forward direction |
| 228 | let mut forward: Vec<Page> = Vec::new(); |
| 229 | |
| 230 | // Set the landing page |
| 231 | let mut current_page = Page::new( |
| 232 | "gopher.floodgap.com", "", 70, true); |
| 233 | loop { |
| 234 | let mut path: String = String::from("/tcp/"); |
| 235 | path.push_str(¤t_page.host); |
| 236 | path.push_str("/"); |
| 237 | path.push_str(¤t_page.port.to_string()); |
| 238 | |
| 239 | let (mem_handle, length) = gopher(&path, ¤t_page.selector).expect("Couldn't gopher"); |
| 240 | let data = str::from_utf8(mem_handle.as_slice::<u8>(length as usize)).expect("invalid utf8"); |
| 241 | |
| 242 | current_page = match display_text(data, |
| 243 | ¤t_page.host, |
| 244 | current_page.is_map) { |
| 245 | Command::Quit => { |
| 246 | println!("[gopher] Bye!"); |
| 247 | return; |
| 248 | } |
| 249 | Command::Back => { |
| 250 | // Remove last page from history |
| 251 | if let Some(page) = back.pop() { |
| 252 | // Save the current page so we can go forward |
| 253 | forward.push(current_page); |
| 254 | page |
| 255 | } else { |
| 256 | // Can't go back |
| 257 | current_page |
| 258 | } |
| 259 | } |
| 260 | Command::Forward => { |
| 261 | if let Some(page) = forward.pop() { |
| 262 | back.push(current_page); |
| 263 | page |
| 264 | } else { |
| 265 | // Can't go forward |
| 266 | current_page |
| 267 | } |
| 268 | } |
| 269 | Command::Link(link_str) => { |
| 270 | // Link |
| 271 | let (_display, selector, hostname, port_str) = { |
| 272 | let mut sections = link_str[1..].split('\t'); |
| 273 | (sections.next().unwrap_or(""), |
| 274 | sections.next().unwrap_or(""), |
| 275 | sections.next().unwrap_or(""), |
| 276 | sections.next().unwrap_or(""))}; |
| 277 | |
| 278 | if let Ok(port) = port_str.parse::<u16>() { |
| 279 | // Save the curent page and reset forward pages |
| 280 | back.push(current_page); |