-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.c
180 lines (143 loc) · 5.39 KB
/
search.c
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
/*
* This file is part of aurinstall.
*
* aurinstall is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aurinstall is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with aurinstall. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2023 Hasan Zahra
* https://github.com/prismz/aurinstall
*/
#include "search.h"
#include "requests.h"
#include "install.h"
#include "output.h"
#include "alloc.h"
#include "json.h"
#include "rpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static char *levenshtein_cmp_str;
/* https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C */
int levenshtein(char *s1, char *s2) {
unsigned int s1len, s2len, x, y, lastdiag, olddiag;
s1len = strlen(s1);
s2len = strlen(s2);
unsigned int column[s1len + 1];
for (y = 1; y <= s1len; y++)
column[y] = y;
for (x = 1; x <= s2len; x++) {
column[0] = x;
for (y = 1, lastdiag = x - 1; y <= s1len; y++) {
olddiag = column[y];
column[y] = MIN3(column[y] + 1, column[y - 1] + 1,
lastdiag + (s1[y-1] == s2[x - 1] ? 0 : 1));
lastdiag = olddiag;
}
}
return column[s1len];
}
int package_qsort_levenshtein(const void *one, const void *two)
{
struct json *pkg_one = (struct json *)*(const struct json **)one;
struct json *pkg_two = (struct json *)*(const struct json **)two;
char *str = levenshtein_cmp_str;
char *one_str = json_get_dict_string(pkg_one, "Name");
char *two_str = json_get_dict_string(pkg_two, "Name");
int diff = levenshtein(one_str, str) - levenshtein(two_str, str);
return diff;
}
void print_search_result(bool istty, char *name, char *desc,
char *ver, int ood, bool installed)
{
if (!istty) {
printf("aur/%s %s", name, ver);
if (ood)
printf(" (OUT OF DATE)");
if (installed)
printf(" [INSTALLED]");
printf("\n");
if (desc != NULL)
printf(" %s\n", desc);
return;
}
printf("%s%saur/%s%s%s %s%s%s%s", BOLD, RED, ENDC, name, ENDC, ENDC,
GREEN, ver, ENDC);
if (ood)
printf(" %s(OUT OF DATE)%s", RED, ENDC);
if (installed)
printf(" %s[INSTALLED]%s", BLUE, ENDC);
printf("\n");
if (desc != NULL)
indent_print(desc, 4);
}
int search_aur(int n, char **terms)
{
if (n <= 0)
return 1;
HashMap *installed = get_installed_packages();
char *url = safe_malloc(1024);
snprintf(url, 1024,
"https://aur.archlinux.org/rpc/?v=5&type=search&arg=%s",
terms[0]);
struct rpc_data *data = make_rpc_request(url);
free(url);
if (data == NULL) {
return 1;
}
if (data->type != rpc_search) {
return 1;
}
bool istty = stdout_is_tty();
/*
* We sort the results using the Levenshtein sorting algorithm.
* Create an array to store all results to sort later.
*/
int packages_i = 0;
struct json **packages = safe_calloc(data->resultcount,
sizeof(struct json));
for (size_t i = 0; i < data->resultcount; i++) {
struct json *pkg = json_get_array_item(data->results, i);
char *name = json_get_dict_string(pkg, "Name");
bool valid = true;
for (int j = 1; j < n; j++) {
if (strstr(name, terms[j]) == NULL) {
valid = false;
break;
}
}
if (valid)
packages[packages_i++] = pkg;
}
levenshtein_cmp_str = terms[0];
qsort(packages, packages_i, sizeof(struct json *),
package_qsort_levenshtein);
for (int i = packages_i - 1; i >= 0; i--) {
struct json *pkg = packages[i];
char *name = json_get_dict_string(pkg, "Name");
char *desc = json_get_dict_string(pkg, "Description");
char *ver = json_get_dict_string(pkg, "Version");
int ood = json_get_dict_number(pkg, "OutOfDate");
bool is_installed = false;
if (installed && hashmap_index(installed, name) != NULL) {
is_installed = true;
}
print_search_result(istty, name, desc, ver, ood,
is_installed);
}
free(packages);
free_rpc_data(data);
free_hashmap(installed);
return 0;
}