-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfed.c
102 lines (87 loc) · 2.27 KB
/
fed.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
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
// TODO check all imports
#include "commands/add.h"
#include "commands/help.h"
#include "commands/ls.h"
#include "commands/clean.h"
#include "commands/cd.h"
#include "commands/open.h"
#include "commands/rename.h"
#include "definitions.h"
#include "utils.h"
struct cmd_struct
{
const char *cmd; /* Command name */
int (*fn)(int, const char **); /* Command func to run that takes argc and argv */
};
/* List of Command structures */
static struct cmd_struct commands[] = {
{"add", cmd_add},
{"ls", cmd_ls},
{"cd", cmd_cd},
{"clean", cmd_clean},
{"open", cmd_open},
{"rename", cmd_rename},
{"version", cmd_version},
{"help", cmd_help},
};
/* Returning a pointer to Command structure with given command name */
static struct cmd_struct *get_builtin(const char *s)
{
int i;
for (i = 0; i < ARRAY_SIZE(commands); i++)
{
struct cmd_struct *p = commands + i;
if (!strcmp(s, p->cmd))
return p;
}
return NULL;
}
static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
{
int status;
status = p->fn(argc, argv);
return status;
}
int main(int argc, const char *argv[], char **env)
{
const char *cmd;
struct cmd_struct *builtin;
/* Look for flags.. */
argv++;
argc--;
int conf;
conf = create_config();
if (!conf)
{
fprintf(stderr, "%s: Couldn't create a config file in HOME dir.", CLI_NAME);
return 1;
}
if (!argc)
{
/* The user didn't specify a command; give them help */
cmd_help(argc, argv);
return 0;
}
if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0]))
argv[0] = "version";
else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0]))
argv[0] = "help";
cmd = argv[0];
/* Getting pointer to command or NULL */
builtin = get_builtin(cmd);
if (builtin)
{
/* if command exists in cmd_struct commands[] */
/* run the command with argv */
argc--;
argv++;
return run_builtin(builtin, argc, argv);
}
fprintf(stderr, "%s: %s is not a fed command. See 'fed --help'.\n", CLI_NAME, cmd);
return 0;
}