-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
493 lines (415 loc) · 10.5 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#include "headers/options.h"
#include "headers/fileutils.h"
#include "headers/objutils.h"
#include "headers/commands.h"
#include "headers/nodeutils.h"
static void parse_command(char* cmd_name, Argp* argp, ArgpState* state);
static error_t parse_global_opt(int key, char* arg, ArgpState* state);
static error_t parse_add_opt(int key, char* arg, ArgpState* state);
static error_t parse_branch_opt(int key, char* arg, ArgpState* state);
static error_t parse_cat_opt(int key, char* arg, ArgpState* state);
static error_t parse_checkout_opt(int key, char* arg, ArgpState* state);
static error_t parse_commit_opt(int key, char* arg, ArgpState* state);
static error_t parse_init_opt(int key, char* arg, ArgpState* state);
static error_t parse_log_opt(int key, char* arg, ArgpState* state);
static error_t parse_restore_opt(int key, char* arg, ArgpState* state);
static error_t parse_revert_opt(int key, char* arg, ArgpState* state);
static error_t parse_status_opt(int key, char* arg, ArgpState* state);
static error_t parse_unstage_opt(int key, char* arg, ArgpState* state);
static void log_state(ArgpState* state);
static char* append_argv0(char* str, char* append);
/* global command */
static char global_doc[] = "\
9it - The version control system that is totally not git\v\
Commands:\n\
add\n\
branch\n\
cat\n\
checkout\n\
commit\n\
init\n\
log\n\
restore\n\
revert\n\
status\n\
unstage\n\
";
static ArgpOption global_options[] = {
{0}
};
Argp global_argp = {
global_options,
parse_global_opt,
0,
global_doc
};
/* ADD subcommand */
static char add_doc[] = "stages files for commit";
static ArgpOption add_options[] = {
{"update", 'u', 0, 0, "stages all tracked files"},
{0}
};
Argp add_argp = {
add_options,
parse_add_opt,
0,
add_doc
};
/* BRANCH subcommand */
static char branch_doc[] = "branch stuff";
static ArgpOption branch_options[] = {
{"delete", 'd', "branch-name", 0, "deletes a branch"},
{0}
};
Argp branch_argp = {
branch_options,
parse_branch_opt,
0,
branch_doc
};
/* CAT subcommand */
static char cat_doc[] = "print out a 9it object as readable text";
static ArgpOption cat_options[] = {
{"type", 't', 0, 0, "get the type of the object"},
{"print", 'p', "obj_path", 0, "get the type of the object"},
{0}
};
Argp cat_argp = {
cat_options,
parse_cat_opt,
0,
cat_doc
};
/* CHECKOUT subcommand */
static char checkout_doc[] = "switches branches";
static ArgpOption checkout_options[] = {
{0}
};
Argp checkout_argp = {
checkout_options,
parse_checkout_opt,
0,
checkout_doc
};
/* COMMIT subcommand */
static char commit_doc[] = "create a commit";
static ArgpOption commit_options[] = {
{"message", 'm', "msg", 0, "commit message"},
{0}
};
Argp commit_argp = {
commit_options,
parse_commit_opt,
0,
commit_doc
};
/* INIT subcommand */
static char init_doc[] = "initialize the 9it repo";
static ArgpOption init_options[] = {
{"bare", 'b', 0, 0, "bare grylls"},
{0}
};
Argp init_argp = {
init_options,
parse_init_opt,
0,
init_doc
};
/* LOG subcommand */
static char log_doc[] = "prints out all the commits";
static ArgpOption log_options[] = {
{0}
};
Argp log_argp = {
log_options,
parse_log_opt,
0,
log_doc
};
/* RESTORE subcommand */
static char restore_doc[] = "restores lol";
static ArgpOption restore_options[] = {
{0}
};
Argp restore_argp = {
restore_options,
parse_restore_opt,
0,
restore_doc
};
/* REVERT subcommand */
static char revert_doc[] = "reverts lol";
static ArgpOption revert_options[] = {
{0}
};
Argp revert_argp = {
revert_options,
parse_revert_opt,
0,
revert_doc
};
/* STATUS subcommand */
static char status_doc[] = "displays information about the index and tracked files";
static ArgpOption status_options[] = {
{0}
};
Argp status_argp = {
status_options,
parse_status_opt,
0,
status_doc
};
/* UNSTAGE subcommand */
static char unstage_doc[] = "removes items from the index";
static ArgpOption unstage_options[] = {
{0}
};
Argp unstage_argp = {
unstage_options,
parse_unstage_opt,
0,
unstage_doc
};
static void
parse_command(char* cmd_name, Argp* argp, ArgpState* state)
{
char* argv0;
int next;
int argc;
char** argv;
argv0 = state->argv[0]; // save argv0 value
next = state->next;
argc = state->argc-next+1; // keep argv[0]
argv = &state->argv[next-1];
/* change argv[0] to name of subcommand */
argv[0] = append_argv0(argv0, cmd_name);
argp_parse(argp, argc, argv, ARGP_IN_ORDER, 0, 0);
/* restore argv[0] */
free(argv[0]);
argv[0] = argv0;
/* empty the rest of args */
/* state->next += argc - 1; */
state->next = state->argc;
return;
}
static error_t
parse_global_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
if (strcmp(arg, "add") == 0 || strcmp(arg, "stage") == 0) {
parse_command("add", &add_argp, state);
} else if (strcmp(arg, "branch") == 0) {
parse_command("branch", &branch_argp, state);
} else if (strcmp(arg, "cat") == 0) {
parse_command("cat", &cat_argp, state);
} else if (strcmp(arg, "checkout") == 0) {
parse_command("checkout", &checkout_argp, state);
} else if (strcmp(arg, "commit") == 0) {
parse_command("commit", &commit_argp, state);
} else if (strcmp(arg, "init") == 0) {
parse_command("init", &init_argp, state);
} else if (strcmp(arg, "log") == 0) {
parse_command("log", &log_argp, state);
} else if (strcmp(arg, "restore") == 0) {
parse_command("restore", &restore_argp, state);
} else if (strcmp(arg, "revert") == 0) {
parse_command("revert", &revert_argp, state);
} else if (strcmp(arg, "status") == 0) {
parse_command("status", &status_argp, state);
} else if (strcmp(arg, "unstage") == 0) {
parse_command("unstage", &unstage_argp, state);
} else {
/* argp_error(state, "%s is not a valid command", arg); */
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_add_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case 'u':
add_update();
break;
case ARGP_KEY_ARG:
/* add_index_item(arg); */
add(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_branch_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
branch(arg);
break;
case 'd':
branch_delete(arg);
break;
case ARGP_KEY_NO_ARGS:
if (state->argc == 1) {
branch_info();
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_cat_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case 't':
printf("print out object type\n");
break;
case 'p':
cat(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_checkout_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
checkout(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_commit_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case 'm':
/* printf("Called commit with -m flag with message: %s\n", arg); */
commit(arg);
break;
case ARGP_KEY_NO_ARGS:
if (state->argc == 1) {
char* editor;
editor = getenv("EDITOR");
if (editor == NULL) {
printf("Please set the EDITOR environment variable or run 9it commit -m [message]");
exit(1);
}
/* open buffer with editor */
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_init_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case 'b':
init("");
break;
case ARGP_KEY_NO_ARGS:
// handle no args
if (state->argc == 1) {
init(NON_BARE_DIR);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_log_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_NO_ARGS:
if (state->argc == 1) {
log_horizon();
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_restore_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
restore(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_revert_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
revert(arg);
/* state->next = state->argc; */
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_status_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_NO_ARGS:
if (state->argc == 1) {
status();
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t
parse_unstage_opt(int key, char* arg, ArgpState* state)
{
switch (key) {
case ARGP_KEY_ARG:
unstage(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static void
log_state(ArgpState* state)
{
printf("next: %d\n", state->next);
}
static char*
append_argv0(char* str, char* append)
{
char* dest;
// account for space and null character
dest = malloc(strlen(str)+1+strlen(append)+1);
sprintf(dest, "%s %s", str, append);
return dest;
}