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
|
/**
* SPM - Simple Package Manager
* @file spm.c
*/
#include <errno.h>
#include "spm.h"
int RUNTIME_INSTALL = 0;
int RUNTIME_ROOTDIR = 0;
const int PACKAGE_MAX = 0xff;
void usage(const char *program_name) {
printf(
"usage: %s [-hVv] [-I|--install {package ...}]\n"
" -h, --help show this help message\n"
" -V, --version show version\n"
" -v, --verbose show more information\n"
" -I, --install install package(s)\n"
, program_name
);
}
int main(int argc, char *argv[]) {
char program_name[strlen(argv[0]) + 1];
memset(program_name, '\0', sizeof(program_name) + 1);
strcpy(program_name, basename(argv[0]));
// not much to see here yet
// at the moment this will all be random tests, for better or worse
// everything here is subject to change without notice
// Initialize configuration data
init_config_global();
// Ensure external programs are available for use.
check_runtime_environment();
char root[PATH_MAX];
memset(root, '\0', PATH_MAX);
char *packages[PACKAGE_MAX];
memset(packages, '\0', sizeof(char *));
if (argc < 2) {
usage(program_name);
exit(1);
}
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
char *arg_next = argv[i + 1] ? argv[i + 1] : NULL;
// options
if (*arg == '-' || strncmp(arg, "--", 2) == 0) {
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
usage(program_name);
exit(0);
}
else if (strcmp(arg, "-V") == 0 || strcmp(arg, "--version") == 0) {
printf("want version\n");
exit(0);
}
else if (strcmp(arg, "-v") == 0 || strcmp(arg, "--verbose") == 0) {
SPM_GLOBAL.verbose = 1;
}
else if (strcmp(arg, "-r") == 0 || strcmp(arg, "--root") == 0) {
RUNTIME_ROOTDIR = 1;
if (!arg_next) {
fprintf(stderr, "-r|--root requires a path\n");
usage(program_name);
exit(1);
}
strcpy(root, arg_next);
i++;
}
else if (strcmp(arg, "-I") == 0 || strcmp(arg, "--install") == 0) {
RUNTIME_INSTALL = 1;
for (int p = 0; i < argc; p++) {
i++;
if (startswith(argv[i], "-") == 0 || startswith(argv[i], "--") == 0) {
if (!p) {
fprintf(stderr, "-I|--install requires at least one package (got: '%s')\n", argv[i]);
exit(1);
}
i--;
break;
}
packages[p] = argv[i];
}
}
}
else {
printf("Unknown option: %s\n", arg);
usage(program_name);
exit(1);
}
}
// Dump configuration
if (SPM_GLOBAL.verbose) {
show_global_config();
}
if (RUNTIME_ROOTDIR && !RUNTIME_INSTALL) {
fprintf(stderr, "-r|--root requires -I|--install\n");
usage(program_name);
exit(1);
}
if (RUNTIME_INSTALL) {
Dependencies *deps = NULL;
dep_init(&deps);
if (isempty(root)) {
printf("Using default installation root\n");
sprintf(root, "%s%c%s", getenv("HOME"), DIRSEP, "spm_root");
}
printf("Installation root: %s\n", root);
printf("Requested packages:\n");
for (int i = 0; i < PACKAGE_MAX; i++) {
if (packages[i] == NULL) {
break;
}
printf(" -> %s\n", packages[i]);
}
printf("Resolving package requirements...\n");
for (int i = 0; i < PACKAGE_MAX; i++) {
char *match = NULL;
char *package = NULL;
if (packages[i] == NULL) {
break;
}
if ((match = find_package(packages[i])) == NULL) {
fprintf(SYSERROR);
exit(1);
}
if ((package = basename(match)) == NULL) {
fprintf(stderr, "Unable to derive package name from package path:\n\t-> %s\n", match);
exit(1);
}
if (dep_all(&deps, package) < 0) {
dep_free(&deps);
free_global_config();
exit(1);
}
}
if (deps) {
// List requirements before installation
for (int i = 0; i < deps->records; i++) {
printf(" -> %s\n", deps->list[i]);
}
printf("Installing package requirements:\n");
for (int i = 0; i < deps->records; i++) {
printf(" -> %s\n", deps->list[i]);
if (install(root, deps->list[i]) < 0) {
fprintf(SYSERROR);
exit(errno);
}
}
}
printf("Installing package:\n");
for (int i = 0; i < PACKAGE_MAX; i++) {
char *match = NULL;
char *package = NULL;
if (!packages[i]) {
break;
}
if ((match = find_package(packages[i])) == NULL) {
fprintf(SYSERROR);
exit(1);
}
if ((package = basename(match)) == NULL) {
fprintf(stderr, "Unable to derive package name from package path:\n\t-> %s\n", match);
exit(1);
}
// If the package was installed as a requirement of another dependency, skip it
if (dep_seen(&deps, package)) {
continue;
}
printf(" -> %s\n", package);
if (install(root, packages[i]) < 0) {
fprintf(SYSERROR);
exit(errno);
}
free(match);
}
dep_free(&deps);
}
free_global_config();
return 0;
}
|