-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
238 lines (224 loc) · 4.21 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "dr.h"
#include "db.h"
#include "release.h"
#include "history.h"
#include "list.h"
#include "checkout.h"
#include "diff.h"
#include "patch.h"
#define ARRAYSIZE(arr) (sizeof(arr)/sizeof(arr[0]))
typedef void (cmd_t)(int argc, char *argv[]);
static char *EXEC = NULL;
struct subcmd {
const char *cmd;
cmd_t *handler;
};
static void help(int argc, char *argv[]);
static void
init_h(int argc, char *argv[])
{
(void)argc;
(void)argv;
db_init();
}
static void
history_h(int argc, char *argv[])
{
int c, n = 0;
(void)argc;
(void)argv;
while ((c = getopt(argc, argv, "n:?")) != -1) {
switch (c) {
case 'n':
n = strtol(optarg, NULL, 0);
break;
case '?':
exit(EINVAL);
break;
}
}
history(n);
}
static void
list_h(int argc, char *argv[])
{
(void)argc;
(void)argv;
struct list_args args;
if (argc < 2) {
fprintf(stderr, "usage: %s list hash\n", EXEC);
exit(EINVAL);
}
args.hash = dr_newstr(argv[1]);
list(&args);
dr_unref(args.hash);
return ;
}
static void
release_h(int argc, char *argv[])
{
int c;
struct release_args args;
memset(&args, 0, sizeof(args));
while ((c = getopt(argc, argv, "c:v:m:d:?")) != -1) {
switch (c) {
case 'c':
args.checkout = dr_newstr(optarg);
break;
case 'v':
args.version = dr_newstr(optarg);
break;
case 'm':
args.describe = dr_newstr(optarg);
break;
case 'd':
args.fromdir = dr_newstr(optarg);
break;
case '?':
exit(EINVAL);
break;
}
}
if (args.version == NULL || args.describe == NULL) {
fprintf(stderr, "usage: %s release -v version -m releasenote -d dir\n",
EXEC);
exit(EINVAL);
}
if (args.fromdir == NULL)
args.fromdir = dr_newstr(".");
release(&args);
dr_unref(args.version);
dr_unref(args.describe);
dr_unref(args.fromdir);
dr_unref(args.checkout);
}
static void
checkout_h(int argc, char *argv[])
{
dr_t hash, outdir;
if (argc < 3) {
fprintf(stderr, "usage: %s checkout hash outdir\n", EXEC);
exit(EINVAL);
}
hash = dr_newstr(argv[1]);
outdir = dr_newstr(argv[2]);
checkout(hash, outdir);
dr_unref(hash);
dr_unref(outdir);
return ;
}
static void
diff_h(int argc, char *argv[])
{
int c;
struct diff_args args;
memset(&args, 0, sizeof(args));
while ((c = getopt(argc, argv, "r:a:b:o:?")) != -1){
switch (c) {
case 'r':
args.r = strtol(optarg, NULL, 0);;
break;
case 'a':
args.a = dr_newstr(optarg);
break;
case 'b':
args.b = dr_newstr(optarg);
break;
case 'o':
args.o = dr_newstr(optarg);
break;
case '?':
exit(EINVAL);
break;
}
}
if (!(args.o && ((args.a && args.b) || args.r > 0))) {
fprintf(stderr, "usage: \n"
"\t%s diff -a from -b to -o dir\n"
"\t%s diff -r -o dir\n",
EXEC, EXEC);
exit(EINVAL);
}
diff(&args);
dr_unref(args.a);
dr_unref(args.b);
dr_unref(args.o);
return ;
}
static void
patch_h(int argc, char *argv[])
{
int c;
struct patch_args args;
memset(&args, 0, sizeof(args));
while ((c = getopt(argc, argv, "a:p:o:?")) != -1) {
switch (c) {
case 'a':
args.hash = dr_newstr(optarg);
break;
case 'p':
args.patch = dr_newstr(optarg);
break;
case 'o':
args.output = dr_newstr(optarg);
break;
case '?':
exit(EINVAL);
break;
}
}
if (!(args.hash && args.patch && args.output)) {
fprintf(stderr, "usage: %s patch -a hash -p patch -o output\n",
argv[0]);
exit(EINVAL);
}
patch(&args);
dr_unref(args.hash);
dr_unref(args.patch);
dr_unref(args.output);
return ;
}
static struct subcmd cmds[] = {
{"help", help},
{"init", init_h},
{"history", history_h},
{"list", list_h},
{"release", release_h},
{"checkout", checkout_h},
{"diff", diff_h},
{"patch", patch_h},
};
static void
help(int argc, char *argv[])
{
int i;
(void)argc;
(void)argv;
fprintf(stdout, "sub command list:\n");
for (i = 0; i < ARRAYSIZE(cmds); i++) {
fprintf(stdout, "%s\n", cmds[i].cmd);
}
}
int main(int argc, char *argv[])
{
int i;
if (argc < 2)
goto over;
EXEC = argv[0];
for (i = 0; i < ARRAYSIZE(cmds); i++) {
if (strcmp(cmds[i].cmd, argv[1]) == 0) {
cmds[i].handler(argc-1, &argv[1]);
break;
}
}
if (i >= ARRAYSIZE(cmds)) {
over:
fprintf(stderr, "usage: %s <command> [<args>]\n", argv[0]);
exit(EINVAL);
}
return 0;
}