-
Notifications
You must be signed in to change notification settings - Fork 1
/
aurinstall.c
172 lines (138 loc) · 5.2 KB
/
aurinstall.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
/*
* 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 "json.h"
#include "alloc.h"
#include "requests.h"
#include "output.h"
#include "install.h"
#include "search.h"
#include "util.h"
#include "rpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static char *user_cache_path;
#define VERSION "3.0"
void init(void)
{
char *userhome = get_user_home();
user_cache_path = safe_malloc(sizeof(char) * PATH_MAX);
snprintf(user_cache_path, PATH_MAX, "%s/.cache/aurinstall", userhome);
if (!dir_exists(user_cache_path)) {
/* remove just in case the path exists but is not a folder. */
snsystem("rm -rf %s", PATH_MAX + 32, user_cache_path);
printf("Cache dir does not exist. Creating one at %s.\n",
user_cache_path);
snsystem("mkdir -p \"%s\"", PATH_MAX + 32, user_cache_path);
}
free(userhome);
}
void usage(void)
{
printf("Usage: aurinstall [PACKAGE1] [PACKAGE2] [OPTION]...\n\n");
printf(" --search search the AUR using one or more searchterms.\n");
printf(" --update update currently installed AUR packages.\n");
printf(" --remove remove one or more packages.\n");
printf(" --clean clean the cache of downloaded packages.\n");
printf(" --help/usage print this message and exit.\n");
printf(" --version print version and copyright info and exit.\n\n");
printf("https://github.com/prismz/aurinstall\n");
exit(0);
}
void version(void)
{
printf("aurinstall %s\n", VERSION);
printf("Copyright (C) 2023 Hasan Zahra\n");
printf("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n");
printf("This is free software: you are free to change and redistribute it.\n");
printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
printf("Written by Hasan Zahra.\n");
printf("https://github.com/prismz/aurinstall\n");
exit(0);
}
int main(int argc, char **argv)
{
init();
if (argc == 1) {
usage();
}
/* parameters to pass to function */
char *parameters[argc + 1];
int parameters_i = 0;
for (int i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
parameters[parameters_i++] = argv[i];
}
}
char *operation = argv[1];
if (strcmp(operation, "--search") == 0) {
if (parameters_i == 0) {
fprintf(stderr,
"please provide at least one searchterm.\n");
return 1;
}
if (search_aur(parameters_i, parameters)) {
fprintf(stderr, "search failed. ");
fprintf(stderr, "Maybe your terms were too broad?\n");
return 1;
}
return 0;
}
if (strcmp(operation, "--update") == 0) {
if (update_packages(user_cache_path)) {
fprintf(stderr, "failed to update packages.\n");
return 1;
}
return 0;
}
if (strcmp(operation, "--remove") == 0) {
if (remove_packages(parameters_i, parameters)) {
fprintf(stderr, "failed to remove packages.\n");
return 1;
}
return 0;
}
if (strcmp(operation, "--clean") == 0) {
if (clean_cache(user_cache_path)) {
fprintf(stderr, "failed to clean cache.\n");
return 1;
}
return 0;
}
if (strcmp(operation, "--help") == 0
|| strcmp(operation, "--usage") == 0) {
usage();
return 0;
}
if (strcmp(operation, "--version") == 0) {
version();
return 0;
}
if (operation[0] == '-') { /* invalid operation */
fprintf(stderr, "error: invalid operation.\n");
return 1;
}
for (int i = 0; i < parameters_i; i++) {
char *param = parameters[i];
if (install_package(param, user_cache_path))
return 1;
}
}