CLI discover Subcommand

This commit is contained in:
Florian Märkl 2019-08-11 19:36:45 +02:00
commit c7e4fd778b
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
4 changed files with 146 additions and 5 deletions

View file

@ -1,5 +1,11 @@
set(SOURCE
src/main.c)
include/chiaki-cli.h
src/discover.c)
add_executable(chiaki_cli ${SOURCE})
add_library(chiaki-cli-lib STATIC ${SOURCE})
target_include_directories(chiaki-cli-lib PUBLIC "include")
target_link_libraries(chiaki-cli-lib chiaki-lib)
add_executable(chiaki-cli src/main.c)
target_link_libraries(chiaki-cli chiaki-cli-lib)

33
cli/include/chiaki-cli.h Normal file
View file

@ -0,0 +1,33 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_CHIAKI_CLI_H
#define CHIAKI_CHIAKI_CLI_H
#include <chiaki/common.h>
#ifdef __cplusplus
extern "C" {
#endif
CHIAKI_EXPORT int chiaki_cli_cmd_discover(int argc, char *argv[]);
#ifdef __cplusplus
}
#endif
#endif //CHIAKI_CHIAKI_CLI_H

74
cli/src/discover.c Normal file
View file

@ -0,0 +1,74 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <chiaki-cli.h>
#include <argp.h>
#include <stdio.h>
static char doc[] = "Send a PS4 discovery request.";
static struct argp_option options[] = {
{ "ip", 'i', "IP", 0, "IP to send discovery request to", 0 },
{ 0 }
};
typedef struct arguments
{
char *ip;
} Arguments;
static int parse_opt(int key, char *arg, struct argp_state *state)
{
Arguments *arguments = state->input;
switch(key)
{
case 'i':
arguments->ip = arg;
break;
case ARGP_KEY_ARG:
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, 0, doc, 0, 0, 0 };
CHIAKI_EXPORT int chiaki_cli_cmd_discover(int argc, char *argv[])
{
Arguments arguments = { 0 };
error_t r = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, &arguments);
if(r != 0)
return 1;
if(!arguments.ip)
{
fprintf(stderr, "ip for discovery is not set.\n");
return 1;
}
// TODO
printf("run discovery\n");
return 0;
}

View file

@ -15,25 +15,53 @@
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
#include <chiaki-cli.h>
#include <argp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const char doc[] =
"CLI for Chiaki (PS4 Remote Play Client)"
"\v"
"Supported commands are:\n"
" discover Discover Consoled.\n";
" discover Discover Consoles.\n";
static struct argp_option options[] = {
{ 0 }
};
static int call_subcmd(struct argp_state *state, const char *name, int (*subcmd)(int argc, char *argv[]))
{
if(state->next < 1 || state->argc < state->next)
return 1;
int argc = state->argc - state->next + 1;
char **argv = &state->argv[state->next - 1];
size_t l = strlen(state->name) + strlen(name) + 2;
argv[0] = malloc(l);
if(!argv[0])
return 1;
snprintf(argv[0], l, "%s %s", state->name, name);
int r = subcmd(argc, argv);
free(argv[0]);
return r;
}
static int parse_opt(int key, char *arg, struct argp_state *state)
{
switch(key)
{
case ARGP_KEY_ARG:
if(strcmp(arg, "discover") == 0)
exit(call_subcmd(state, "discover", chiaki_cli_cmd_discover));
// fallthrough
case ARGP_KEY_END:
argp_usage(state);
break;
default:
@ -42,11 +70,11 @@ static int parse_opt(int key, char *arg, struct argp_state *state)
return 0;
}
static struct argp argp = { options, parse_opt, "[<cmd> [CMD-OPTION...]]", doc, 0, 0, 0 };
static struct argp argp = { options, parse_opt, "<cmd> [CMD-ARGS...]", doc, 0, 0, 0 };
int main(int argc, char *argv[])
{
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, 0);
argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
return 0;
}