1
0

command -> mode.

This commit is contained in:
Alexander Andreev 2024-03-31 20:24:07 +04:00
parent 719624e4c2
commit a998c73362
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -27,7 +27,7 @@ struct options {
const char *addressbook_path; const char *addressbook_path;
const char *key_path; const char *key_path;
bool is_alias_passed; bool is_alias_passed;
char command; char mode;
}; };
int parse_argv(int argc, const char **argv, struct options *opts); int parse_argv(int argc, const char **argv, struct options *opts);
@ -72,7 +72,7 @@ int main(int argc, const char **argv) {
fclose(addrbook_file); fclose(addrbook_file);
} }
if (opts.command == 'c' && opts.is_alias_passed) { if (opts.mode == 'c' && opts.is_alias_passed) {
addrbook_entry_t *aec = addrbook_get_by_alias(&ab, opts.addr); addrbook_entry_t *aec = addrbook_get_by_alias(&ab, opts.addr);
if (aec == NULL) { if (aec == NULL) {
fprintf(stderr, "An alias \"%s\" is not in an addressbook!\n", opts.addr); fprintf(stderr, "An alias \"%s\" is not in an addressbook!\n", opts.addr);
@ -128,13 +128,13 @@ int main(int argc, const char **argv) {
fclose(skey_file); fclose(skey_file);
} }
(opts.command == 'c') ? (opts.mode == 'c') ?
(result = net_client_init(&n, opts.addr, opts.port)) (result = net_client_init(&n, opts.addr, opts.port))
: (result = net_server_init(&n, opts.addr , opts.port)); : (result = net_server_init(&n, opts.addr , opts.port));
if (result != 0) if (result != 0)
goto cleanup; goto cleanup;
(opts.command == 'c') ? (opts.mode == 'c') ?
(result = client_handshake(&n, &s, &ok, &rk)) (result = client_handshake(&n, &s, &ok, &rk))
: (result = server_handshake(&n, &s, &ok, &rk)); : (result = server_handshake(&n, &s, &ok, &rk));
if (result != 0) if (result != 0)
@ -152,19 +152,19 @@ int main(int argc, const char **argv) {
// audio_init_default(&aud, CHANNELS, SAMPLE_RATE, FRAMES_PER_BUFFFER); // audio_init_default(&aud, CHANNELS, SAMPLE_RATE, FRAMES_PER_BUFFFER);
// (opts.command == 'c') ? // (opts.mode == 'c') ?
// (audio_stream_input_toggle(&aud)) // (audio_stream_input_toggle(&aud))
// : (audio_stream_output_toggle(&aud)); // : (audio_stream_output_toggle(&aud));
// for (size_t i = 0; i < (20 * SAMPLE_RATE)/FRAMES_PER_BUFFFER; ++i) { // for (size_t i = 0; i < (20 * SAMPLE_RATE)/FRAMES_PER_BUFFFER; ++i) {
// if (opts.command == 'c') { // if (opts.mode == 'c') {
// audio_read(&aud, aud_buf, AUDIO_BUF_SIZE); // audio_read(&aud, aud_buf, AUDIO_BUF_SIZE);
// } else { // } else {
// // // //
// } // }
// } // }
// (opts.command == 'c') ? // (opts.mode == 'c') ?
// (audio_stream_input_toggle(&aud)) // (audio_stream_input_toggle(&aud))
// : (audio_stream_output_toggle(&aud)); // : (audio_stream_output_toggle(&aud));
@ -281,7 +281,7 @@ int server_handshake(net_t *const n, crypt_session_t *const s, crypt_key_t *cons
int parse_argv(int argc, const char **argv, struct options *opts) { int parse_argv(int argc, const char **argv, struct options *opts) {
if (argc == 1) if (argc == 1)
return -1; return -1;
int command_arg_num = 0; int mode_arg_num = 0;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
@ -306,17 +306,17 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
default: return -1; default: return -1;
} }
} else { } else {
if (opts->command == 'c' || opts->command == 's') { if (opts->mode == 'c' || opts->mode == 's') {
if (command_arg_num == 0) if (mode_arg_num == 0)
opts->addr = argv[i]; opts->addr = argv[i];
else if (command_arg_num == 1) else if (mode_arg_num == 1)
opts->port = argv[i]; opts->port = argv[i];
++command_arg_num; ++mode_arg_num;
} else { } else {
switch (argv[i][0]) { switch (argv[i][0]) {
case 'c': case 'c':
case 's': case 's':
opts->command = argv[i][0]; opts->mode = argv[i][0];
break; break;
default: return -1; default: return -1;
} }
@ -330,10 +330,10 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
r = inet_pton(AF_INET6, opts->addr, &test); r = inet_pton(AF_INET6, opts->addr, &test);
opts->is_alias_passed = r == 0; opts->is_alias_passed = r == 0;
if (opts->command == 's' && opts->is_alias_passed) if (opts->mode == 's' && opts->is_alias_passed)
return -1; return -1;
if (opts->command == '\0' || opts->addr[0] == '\0' if (opts->mode == '\0' || opts->addr[0] == '\0'
|| (!opts->is_alias_passed && opts->port == NULL)) || (!opts->is_alias_passed && opts->port == NULL))
return -1; return -1;