-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparser.cpp
More file actions
164 lines (138 loc) · 5 KB
/
argparser.cpp
File metadata and controls
164 lines (138 loc) · 5 KB
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
#include "argparser.hpp"
#include "keychain.hpp"
#include "xmsg.hpp"
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdbool>
#define argparser_arguments ARGS
void cmd_help(int argc, char* argv[]);
void cmd_version(int argc, char* argv[]);
void cmd_debug(int argc, char* argv[]);
void cmd_key(int argc, char* argv[]);
void cmd_dumpkeys(int argc, char* argv[]);
void cmd_encrypt(int argc, char* argv[]);
void cmd_decrypt(int argc, char* argv[]);
void cmd_createkey(int argc, char* argv[]);
void cmd_deletekey(int argc, char* argv[]);
// Defined as an extern variable in the H file
const struct ARGPARSER_CmdArgument_t ARGS[] = {
{ "-h", "--help", "Provides a list of possible arguments that can be passed to the program.", (void*)&cmd_help },
{ "-v", "--version", "display xmsg version.", (void*)&cmd_version },
{ "-D", "--debug", "enable debug messages.", (void*)&cmd_debug },
{ "-k", "--key", "set encryption key to use.", (void*)&cmd_key },
{ "-K", "--dumpkeys", "dumps available encryption keys.", (void*)&cmd_dumpkeys },
{ "-e", "--encrypt", "enables encryption mode.", (void*)&cmd_encrypt },
{ "-d", "--decrypt", "enables decryption mode.", (void*)&cmd_decrypt },
{ "", "--createkey", "create encryption key.", (void*)&cmd_createkey },
{ "", "--deletekey", "delete encryption key.", (void*)&cmd_deletekey }
};
struct ARGPARSER_Context_t argparser_context;
void ARGPARSER_parseProgramArguments(int argc, char* argv[], char* overflow[], size_t overflow_cb, unsigned* overflow_count) {
unsigned c_overflow = 0;
int subargs_count;
const char** subargs;
subargs = (const char**)malloc(sizeof(char*) * argc);
subargs_count = 0;
for (int i = 0; i < argc; i++) {
bool found = false;
std::string str;
for (unsigned j = 0; j < sizeof(ARGS) / sizeof(ARGS[0]); j++) {
int type = 0;
str.clear();
if (strcmp(argv[i], ARGS[j].alias) == 0)
{
for (int k = i + 1; k < argc; k++) {
// If the next argument starts with '-', break
if (argv[k][0] == '-') break;
subargs[subargs_count] = argv[k];
subargs_count++;
}
type = 1;
found = true;
}
else if (strlen(ARGS[j].cmd) > 1 &&
strncmp(argv[i], ARGS[j].cmd, strlen(ARGS[j].cmd)) == 0)
{
for (int k = 2; argv[i][k] >= '0' && argv[i][k] <= '9'; k++) {
str.push_back(argv[i][k]);
}
type = 2;
found = true;
}
// Call the callback function if it's not NULL to avoid segfaults
if (ARGS[j].callback == NULL) continue;
switch (type) {
case 1:
i += subargs_count;
((void(*)(int,const char*[]))ARGS[j].callback)(subargs_count, subargs);
break;
case 2:
if (str.length() > 0) {
subargs[0] = str.data();
subargs_count = 1;
}
((void(*)(int,const char*[]))ARGS[j].callback)(subargs_count, subargs);
break;
}
if (type != 0) break;
}
// Push argument to overflow buffer if no match is found
if (!found && overflow) {
// Ensure there are no segfaults
if ((c_overflow + 1) * sizeof(char*) > overflow_cb) {
continue;
}
overflow[c_overflow++] = argv[i];
if (overflow_count != (unsigned*)NULL) {
*overflow_count = c_overflow;
}
}
}
free(subargs);
}
void cmd_help(int argc, char* argv[]) {
for (unsigned i = 0; i < sizeof(ARGS) / sizeof(ARGS[1]); i++) {
printf("%s %s %s\n", ARGS[i].cmd, ARGS[i].alias, ARGS[i].description);
}
exit(0);
}
void cmd_version(int argc, char* argv[]) {
printf("xmsg version %.1f\n", _xmsg_version);
exit(0);
}
void cmd_debug(int argc, char* argv[]) {
argparser_context.debug = true;
}
void cmd_key(int argc, char* argv[]) {
if (argc != 1) {
fprintf(stderr, "Invalid number of paramaters for --key (argc=%i).\n", argc);
for (int i = 0; i < argc; i++) {
printf("%i: %s\n", i, argv[i]);
}
exit(1);
}
sscanf(argv[0], "%d", &argparser_context.key);
}
void cmd_dumpkeys(int argc, char* argv[]) {
// Creates Keychain object with invalid key ID.
// Prints out all the available encryption keys
Keychain keychain(-1);
exit(0);
}
void cmd_encrypt(int argc, char* argv[]) {
argparser_context.encrypt = true;
}
void cmd_decrypt(int argc, char* argv[]) {
argparser_context.decrypt = true;
}
void cmd_createkey(int argc, char* argv[]) {
Keychain::createKey();
exit(0);
}
void cmd_deletekey(int argc, char* argv[]) {
Keychain* keychain = new Keychain(0);
keychain->deleteKey();
exit(0);
}