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
|
/**
* @file internal_cmd.c
*/
#include "spm.h"
/**
* List of valid internal commands
*/
static char *internal_commands[] = {
"mkprefixbin", "generate prefix manifest (binary)",
"mkprefixtext", "generate prefix manifest (text)",
NULL, NULL,
};
/**
* Show a listing of valid internal commands
*/
void internal_command_list(void) {
printf("possible commands:\n");
for (int i = 0; internal_commands[i] != NULL; i++) {
printf(" %-20s - %-20s\n", internal_commands[i], internal_commands[i + 1]);
i++;
}
}
void mkprefix_usage(void) {
printf("usage: mkprefix[bin|text] {output_file} {dir} {prefix ...}\n");
}
/**
* Execute an internal command
* @param argc
* @param argv
* @return success=0, failure=1, error=-1
*/
int internal_cmd(int argc, char **argv) {
int command_valid = 0;
char *command = argv[1];
if (argc < 2) {
internal_command_list();
return 1;
}
for (int i = 0; internal_commands[i] != NULL; i++) {
if (strcmp(internal_commands[i], command) == 0) {
command_valid = 1;
break;
}
}
if (!command_valid) {
fprintf(stderr, "error: '%s' is not a valid command\n", command);
internal_command_list();
return 1;
}
if (strcmp(command, "mkprefixbin") == 0 || strcmp(command, "mkprefixtext") == 0) {
char *outfile = argv[2];
char *tree = argv[3];
size_t prefix_start = 4;
size_t prefixes = 0;
for (size_t i = prefix_start; i < argc; i++) {
prefixes = i;
}
// Check arguments
if (!outfile) {
fprintf(stderr, "error: missing output file name\n");
mkprefix_usage();
return -1;
}
if (!tree) {
fprintf(stderr, "error: missing directory path\n");
mkprefix_usage();
return -1;
}
if (!prefixes) {
fprintf(stderr, "error: missing prefix string(s)\n");
mkprefix_usage();
return -1;
}
char **prefix = (char **) calloc(prefixes + 1, sizeof(char *));
if (!prefix) {
perror("prefix array");
fprintf(SYSERROR);
return -1;
}
// Populate array of prefixes; reusing pointers from argv
for (int i = 0; (i + prefix_start) < argc; i++) {
prefix[i] = argv[(i + prefix_start)];
}
if (SPM_GLOBAL.verbose) {
printf("Generating prefix manifest: %s\n", outfile);
}
if (strcmp(command, "mkprefixbin") == 0) {
prefixes_write(outfile, PREFIX_WRITE_BIN, prefix, tree);
} else if (strcmp(command, "mkprefixtext") == 0) {
prefixes_write(outfile, PREFIX_WRITE_TEXT, prefix, tree);
}
}
return 0;
}
|