forked from segfault-bilibili/command-not-found
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command-not-found.cpp
212 lines (187 loc) · 6.26 KB
/
command-not-found.cpp
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <cstring>
#include <map>
#include <list>
#include <sys/cdefs.h>
#include <sys/stat.h>
#ifndef __TERMUX_PREFIX__
#error "__TERMUX_PREFIX__ not defined"
#endif
using namespace std;
list<string> main_commands = {
#ifdef __aarch64__
# include "commands-aarch64-termux-main.h"
#elif defined __arm__
# include "commands-arm-termux-main.h"
#elif defined __i686__
# include "commands-i686-termux-main.h"
#elif defined __x86_64__
# include "commands-x86_64-termux-main.h"
#else
# error Failed to detect arch
#endif
};
list<string> root_commands = {
#ifdef __aarch64__
# include "commands-aarch64-termux-root.h"
#elif defined __arm__
# include "commands-arm-termux-root.h"
#elif defined __i686__
# include "commands-i686-termux-root.h"
#elif defined __x86_64__
# include "commands-x86_64-termux-root.h"
#else
# error Failed to detect arch
#endif
};
list<string> x11_commands = {
#ifdef __aarch64__
# include "commands-aarch64-termux-x11.h"
#elif defined __arm__
# include "commands-arm-termux-x11.h"
#elif defined __i686__
# include "commands-i686-termux-x11.h"
#elif defined __x86_64__
# include "commands-x86_64-termux-x11.h"
#else
# error Failed to detect arch
#endif
};
struct info {string binary, repository;};
/* https://stackoverflow.com/a/12774387 */
inline bool file_exists (const string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
inline int termux_min3(int a, int b, int c) {
return (a < b ? (a < c ? a : c) : (b < c ? b : c));
}
int termux_levenshtein_distance(char const* s1, char const* s2) {
int s1len = strlen(s1);
int s2len = strlen(s2);
int x, y;
int **matrix;
int distance;
matrix = (int **) malloc(sizeof *matrix * (s2len+1));
if (!matrix) {
cerr << "Memory allocation seem to have failed" << endl;
return -2;
}
matrix[0] = (int *) malloc(sizeof *matrix[0] * (s1len+1));
if (!matrix[0]) {
cerr << "Memory allocation seem to have failed" << endl;
return -3;
}
matrix[0][0] = 0;
for (x = 1; x <= s2len; x++) {
matrix[x] = (int *) malloc(sizeof *matrix[x] * (s1len+1));
if (!matrix[x]) {
cerr << "Memory allocation seem to have failed" << endl;
return -4;
}
matrix[x][0] = matrix[x-1][0] + 1;
}
for (y = 1; y <= s1len; y++) {
matrix[0][y] = matrix[0][y-1] + 1;
}
for (x = 1; x <= s2len; x++) {
for (y = 1; y <= s1len; y++) {
matrix[x][y] = termux_min3(matrix[x-1][y] + 1, matrix[x][y-1] + 1, matrix[x-1][y-1] + (s1[y-1] == s2[x-1] ? 0 : 1));
}
}
distance = matrix[s2len][s1len];
for (x = 0; x <= s2len; x++) {
free(matrix[x]);
}
free(matrix);
return distance;
}
int termux_look_for_packages(const char* command_not_found, list<string>* cmds, int* best_distance, void* guesses_at_best_distance, const char repository[]) {
string current_package;
string current_binary;
int distance;
map<string, info>* pkg_map = (map<string, info>*) guesses_at_best_distance;
for (list<string>::iterator it = (*cmds).begin(); it != (*cmds).end(); ++it) {
string current_line = *it;
if (current_line[0] != ' ') {
current_package = current_line;
} else {
current_binary = current_line.substr(1);
distance = termux_levenshtein_distance(command_not_found, current_binary.c_str());
if (distance < -1) {
// malloc failed, return the error code
return -distance;
} else if (*best_distance == distance) {
// As good as our previously best match
(*pkg_map).insert(pair<string,info>(current_package, {current_binary, repository}));
} else if (*best_distance == -1 || distance < *best_distance) {
// New best match
(*pkg_map).clear();
*best_distance = distance;
(*pkg_map).insert(pair<string,info>(current_package, {current_binary, repository}));
}
}
}
return 0;
}
int main(int argc, const char *argv[]) {
if (argc != 2) {
cerr << "usage: command-not-found <command>" << endl;
return 1;
}
const char *command = argv[1];
int best_distance = -1;
struct info {string binary, repository;};
map <string, info> package_map;
map <string, info>::iterator it;
int res;
string sources_prefix = string(__TERMUX_PREFIX__) + "/etc/apt/sources.list.d/";
res = termux_look_for_packages(command, &main_commands, &best_distance, &package_map, "");
if (res != 0) { return res; }
res = termux_look_for_packages(command, &root_commands, &best_distance, &package_map, "root");
if (res != 0) { return res; }
res = termux_look_for_packages(command, &x11_commands, &best_distance, &package_map, "x11");
if (res != 0) { return res; }
if (best_distance == -1 || best_distance > 3) {
cerr << command << ": command not found" << endl;
} else if (best_distance == 0) {
cerr << "The program " << command << " is not installed. Install it by executing:" << endl;
for (it=package_map.begin(); it!=package_map.end(); ++it) {
cerr << " pkg install " << it->first;
if (it->second.repository != "" &&
!file_exists(sources_prefix + it->second.repository + ".list")) {
cerr << ", after running pkg install " << it->second.repository << "-repo" << endl;
} else {
cerr << endl;
}
if (next(it) != package_map.end()) {
cerr << "or" << endl;
}
}
} else {
cerr << "No command " << command << " found, did you mean:" << endl;
for (it=package_map.begin(); it!=package_map.end(); ++it) {
cerr << " Command " << it->second.binary << " in package " << it->first;
if (it->second.repository != "" &&
!file_exists(sources_prefix + it->second.repository + ".list")) {
cerr << " from the " << it->second.repository << "-repo repository" << endl;
} else {
cerr << endl;
}
}
}
return 127;
}