-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
154 lines (130 loc) · 4 KB
/
client.cpp
File metadata and controls
154 lines (130 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifdef DEBUG
#include <sys/prctl.h>
#include <sanitizer/asan_interface.h>
#endif
#include "connection.h"
#include "menu.h"
#include "display.h"
#include "history.h"
#include "menu_item.h"
#include "guri.h"
#include <iostream>
#include <string>
#include <stdint.h>
#include <stdexcept>
using std::string;
using std::cout;
using std::endl;
using std::cin;
using std::runtime_error;
void print_usage(char* argv0){
cout << "Usage: " << argv0 << " [gopher://]<domain>[:port][/resource]" << endl;
return;
}
void get_args(int argc, char** argv, GURI& uri, Display& disp){
if(argc == 2){
string arg = string(argv[1]);
if(!arg.compare("-h") || !arg.compare("--help")){
disp.~Display();
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
try{
uri.set_uri(arg);
return;
}catch(runtime_error &e){
disp.~Display();
cout << e.what() << endl;
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
disp.~Display();
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
int main(int argc, char** argv){
#ifdef DEBUG
//When debug mode enabled, allow any process to attach
prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
//Write address sanitizer errors to log file
__sanitizer_set_report_path("asan.log");
#endif
Display disp;
Menu::set_display_width(disp.get_display_width());
GURI uri;
get_args(argc, argv, uri, disp);
disp.print_text("Welcome to Phrowse!\n");
Connection conn(uri.get_formatted_host('\t'));
MenuItem firstpage(uri);
Menu men;
try{
men = Menu(conn.request(firstpage));
}catch(runtime_error &e){
men = Menu(e);
}
disp.set_menu(men);
disp.draw_menu();
BrowserHistory hist;
hist.add_item(firstpage);
HistoryItem to_visit;
while((to_visit = HistoryItem(disp.get_item())).get_item() != Menu::no_item){
if(!to_visit.get_item().can_select()){
continue;
}
if(to_visit.get_item() == Menu::prev_item){
to_visit = hist.go_back();
hist.set_fut_indices(disp.get_last_sel());
}else if(to_visit.get_item() == Menu::next_item){
to_visit = hist.go_forward();
}else if(to_visit.get_item() == Menu::url_item){
hist.clear_future();
hist.set_hist_indices(disp.get_last_sel());
string url = "";
string prompt = "Enter a gopher URL to visit:";
while(true){
try{
if(!disp.get_string(prompt, url)){
throw runtime_error("Please enter a URL");
}
GURI new_page(url);
to_visit.get_item() = MenuItem(new_page);
break;
}catch(runtime_error &e){
prompt = e.what();
prompt.append("\nEnter a gopher URL to visit:");
}
}
}else{
hist.clear_future();
hist.set_hist_indices(disp.get_last_sel());
if(to_visit.get_item().get_type() == '7'){
string query;
if(!disp.get_string("Enter a search query:", query)){
break;
}
to_visit.get_item().set_search(query);
}
}
//If the user is trying to go fwd/back but there is no history,
//do nothing
if(to_visit.get_item() == Menu::no_item){
continue;
}
try{
string response = conn.request(to_visit.get_item());
if(to_visit.get_item().is_binary()){
men = Menu(response, '1');
}else{
men = Menu(response, to_visit.get_item().get_type());
}
}catch(runtime_error &e){
men = Menu(e);
}
hist.add_item(to_visit);
disp.set_menu(men);
disp.jump_to(to_visit.get_indices());
disp.draw_menu();
}
return 0;
}