-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
192 lines (165 loc) · 4.47 KB
/
options.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
181
182
183
184
185
186
187
188
189
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "main.h"
#include "utils.h"
#include "options.h"
typedef void (*getopts_t)(int, char**);
static void del_options(int, char**);
static void install_options(int, char**);
static void search_options(int, char**);
static void usage(int, char**);
struct cmd {
char* cmd;
getopts_t parse;
char* descr;
};
static struct cmd cmds[] = {
{ "delete", del_options, "delete package" },
{ "install", install_options, "install package" },
{ "help", usage, "print this text and exit" },
{ "search", search_options, "search package" },
{ "usage", usage, "print this text and exit" },
{ NULL, NULL, NULL }
};
static void del_options(int argc, char** argv) {
int opt;
char *opt_str = "v:n:";
pkg_t* pkg = (pkg_t*)xcalloc(1, sizeof(pkg_t));
pkg_t* gpkg; // Pointer for get_pkgs()
struct option long_opts[] = {
{ "name", required_argument, NULL, 'n' },
{ "version", required_argument, NULL, 'v' },
{ NULL, no_argument, NULL, 0 }, };
int option_index = 0;
while((opt = getopt_long(argc, argv, opt_str, long_opts, &option_index)) != -1)
switch(opt) {
case 'n':
pkg->name = optarg;
break;
case 'v':
pkg->version = optarg;
break;
}
gpkg = get_pkgs(pkg);
if(gpkg->name) {
printf("Deleting package:..\n");
show_pkg(gpkg);
if(!del_pkg(pkg))
printf("Done!\n");
else
fprintf(stderr, "Delete function returned an error!!\n");
}
else
printf("ABORT! This package is NOT installed.\n");
return;
}
static void install_options(int argc, char** argv) {
int opt;
char *opt_str = "v:n:d:l:f:";
char *filename = NULL;
pkg_t* pkg = (pkg_t*)xcalloc(1, sizeof(pkg_t));
pkg_t* gpkg; // Pointer for get_pkgs()
struct option long_opts[] = {
{ "name", required_argument, NULL, 'n' },
{ "version", required_argument, NULL, 'v' },
{ "descr", required_argument, NULL, 'd' },
{ "license", required_argument, NULL, 'l' },
{ "file", required_argument, NULL, 'f' },
{ NULL, no_argument, NULL, 0 }, };
int option_index = 0;
while((opt = getopt_long(argc, argv, opt_str, long_opts, &option_index)) != -1)
switch(opt) {
case 'n':
pkg->name = optarg;
break;
case 'v':
pkg->version = optarg;
break;
case 'd':
pkg->descr = optarg;
break;
case 'l':
pkg->license = optarg;
break;
case 'f':
filename = optarg;
break;
}
gpkg = get_pkgs(pkg);
if(!gpkg->name) {
printf("Installation package:..\n");
pkg->itime = get_cur_time();
show_pkg(pkg);
if(!install_pkg(pkg, filename))
printf("Done!\n");
else
printf("Install function returned an error!!\n");
}
else {
printf("ABORT! This package is ALREADY installed:\n");
show_pkg(pkg);
}
}
static void search_options(int argc, char** argv) {
int opt;
int i = 0;
char *opt_str = "v:n:d:l:";
pkg_t* pkg = (pkg_t*)xcalloc(1, sizeof(pkg_t));
pkg_t* gpkg; // Pointer for get_pkgs()
struct option long_opts[] = {
{ "name", required_argument, NULL, 'n' },
{ "version", required_argument, NULL, 'v' },
{ "descr", required_argument, NULL, 'd' },
{ "license", required_argument, NULL, 'l' },
{ NULL, no_argument, NULL, 0 }, };
int option_index = 0;
while((opt = getopt_long(argc, argv, opt_str, long_opts, &option_index)) != -1)
switch(opt) {
case 'n':
pkg->name = optarg;
break;
case 'v':
pkg->version = optarg;
break;
case 'd':
pkg->descr = optarg;
break;
case 'l':
pkg->license = optarg;
break;
}
gpkg = get_pkgs(pkg);
while(gpkg[i].name != NULL) {
printf("Package #%d\n", i + 1);
show_pkg(&gpkg[i]);
i++;
}
if(!i)
printf("No matches found\n");
}
static void usage(int argc, char** argv) {
struct cmd *p = cmds;
puts("Package manager - version 0.0.0-dev");
while(p->cmd != NULL) {
printf(" %-7s - %s\n", p->cmd, p->descr);
p++;
}
}
void parse_opts(int argc, char **argv) {
getopts_t parse = NULL;
if(argc < 2)
parse = usage;
else {
int i;
for(i = 0; cmds[i].cmd; i++)
if(!strcmp(cmds[i].cmd, argv[1])) {
parse = cmds[i].parse;
break;
}
}
if(!parse)
parse = usage;
parse(argc, argv);
}