-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetterhelp.cpp
More file actions
227 lines (215 loc) · 5.9 KB
/
Copy pathbetterhelp.cpp
File metadata and controls
227 lines (215 loc) · 5.9 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <filesystem>
#include <fstream>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include "includes/argengine.hpp"
#include "includes/argengine.cpp"
using namespace std;
namespace fs = std::filesystem;
namespace algo = boost::algorithm;
namespace json = boost::json;
#define NOCACHE false
#define PRINT(x) cout << x << endl;
void log(string output, bool verbose) {
if (verbose) {PRINT(output)}
}
vector<string> readnsv(string filepath) {
vector<string> vals;
string temp;
ifstream file(filepath);
while (getline(file, temp)) {
vals.emplace_back(temp);
};
return vals;
}
void createfile(string filepath) {
ofstream file;
file.open(filepath);
if (!file.is_open()) {
cerr << "Error creating file!" << endl;
throw EBADF;
}
cout << "File created successfully." << endl;
file.close();
}
bool exists(string filename) {
fs::path filepath = filename;
return fs::exists(filepath);
}
string runcmd(string command) {
string output;
string cmd = command;
FILE *pipe = popen(cmd.c_str(), "r");
if (!pipe) {
cerr << "Error opening pipe" << endl;
throw "Error opening pipe";
}
char buffer[128];
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
output += buffer;
}
pclose(pipe);
return output;
}
string getdesc(string command) {
string temp;
vector<string> lines;
string desc;
string manpage = runcmd("man "+command);
stringstream manstream(manpage);
while (getline(manstream, temp, '\n')) {
lines.emplace_back(temp);
}
if (lines.size() >= 3) {
desc = lines[3];
algo::trim(desc);
return desc;
}
return "";
}
int main(int argc, char **argv) {
vector<string> programs;
vector<string> commands;
string temp;
bool nocache = false;
bool rebuildcache = false;
bool verbose = false;
bool hasFilter = false;
vector<string> filter;
juzzlin::Argengine parser(argc, argv);
parser.addOption({"-n", "--no-cache", "--nocache"}, [&nocache] {
nocache = true;
});
parser.addOption({"-r", "--rebuild-cache", "--rebuildcache"}, [&rebuildcache] {
rebuildcache = true;
});
parser.addOption({"-v", "--verbose"}, [&verbose] {
verbose = true;
});
parser.setPositionalArgumentCallback([&filter] (vector<string> args) {
filter = args;
});
parser.parse();
if (nocache || NOCACHE) {
if (filter.size() == 0) {
for (auto entry : fs::directory_iterator("/bin")) {
temp = entry.path();
algo::erase_all(temp, "/bin/");
// std::cout << temp << std::endl;
commands.emplace_back(temp);
};
}
else {
hasFilter = true;
commands = filter;
}
for (string name : commands) {
string desc = getdesc(name);
cout << desc << endl;
};
return 0;
};
if (exists(".bhprogs")) {
log("Program cache exists.",verbose);
if (filter.size() == 0) {
commands = readnsv(".bhprogs");
}
else {
hasFilter = true;
commands = filter;
}
}
else {
cout << "Program cache does not exist!" << endl;
try
{
createfile(".bhprogs");
}
catch(int e)
{
// std::cerr << e.what() << '\n';
cerr << "Error creating program cache. If this problem continues, try using the --nocache option." << endl;
return 1;
}
for (auto entry : fs::directory_iterator("/bin")) {
temp = entry.path();
algo::erase_all(temp, "/bin/");
// std::cout << temp << std::endl;
commands.emplace_back(temp);
};
ofstream progcache(".bhprogs");
for (string command : commands)
{
progcache << command << endl;
};
progcache.close();
}
if (rebuildcache) {
goto rebuild_cache;
};
if (hasFilter) {
PRINT(runcmd(filter[0]+" --help"));
return 0;
}
if (exists(".bhmans")) {
log("Description cache exists.",verbose);
json::value jvdescs;
string descs;
string temp;
ifstream file(".bhmans");
json::stream_parser parser;
json::object jodescs;
parser.reset();
while (getline(file, temp)) {
parser.write(temp);
};
parser.finish();
jvdescs = parser.release();
jodescs = jvdescs.as_object();
for (string command : commands) {
temp = jodescs[command].as_string();
algo::trim_left_if(temp, algo::is_any_of("\""));
if (temp != "") {
cout << temp << endl;
};
};
file.close();
}
else {
cout << "Description cache does not exist!" << endl;
rebuild_cache:
try
{
filesystem::remove(".bhmans");
createfile(".bhmans");
}
catch(int e)
{
// std::cerr << e.what() << '\n';
cout << "Error creating description cache." << endl;
return 1;
}
cout << "Building description cache. This may take a while." << endl;
// vector<progdesc> descs;
// progdesc temp;
json::object descs;
for (string name : commands) {
string desc = getdesc(name);
cout << desc << endl;
// temp.prog = name;
// temp.desc = desc;
descs[name] = desc;
};
ofstream bhmans(".bhmans");
bhmans << json::serialize(descs);
bhmans.close();
}
return 0;
}