command.c - declare variables at top

This commit is contained in:
twinaphex 2014-08-03 01:38:49 +02:00
parent 6e905cba50
commit ed25984a3c

View File

@ -64,12 +64,15 @@ static bool socket_nonblock(int fd)
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
{ {
char port_buf[16];
struct addrinfo hints, *res = NULL;
int yes = 1;
if (!netplay_init_network()) if (!netplay_init_network())
return false; return false;
RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port); RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port);
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) #if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
@ -79,8 +82,6 @@ static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port)
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
char port_buf[16];
int yes = 1;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo(NULL, port_buf, &hints, &res) < 0) if (getaddrinfo(NULL, port_buf, &hints, &res) < 0)
@ -210,11 +211,14 @@ static const struct cmd_map map[] = {
static bool cmd_set_shader(const char *arg) static bool cmd_set_shader(const char *arg)
{ {
char msg[PATH_MAX];
const char *ext;
enum rarch_shader_type type = RARCH_SHADER_NONE;
if (!driver.video->set_shader) if (!driver.video->set_shader)
return false; return false;
enum rarch_shader_type type = RARCH_SHADER_NONE; ext = path_get_extension(arg);
const char *ext = path_get_extension(arg);
if (strcmp(ext, "glsl") == 0 || strcmp(ext, "glslp") == 0) if (strcmp(ext, "glsl") == 0 || strcmp(ext, "glslp") == 0)
type = RARCH_SHADER_GLSL; type = RARCH_SHADER_GLSL;
@ -226,7 +230,6 @@ static bool cmd_set_shader(const char *arg)
msg_queue_clear(g_extern.msg_queue); msg_queue_clear(g_extern.msg_queue);
char msg[PATH_MAX];
snprintf(msg, sizeof(msg), "Shader: \"%s\"", arg); snprintf(msg, sizeof(msg), "Shader: \"%s\"", arg);
msg_queue_push(g_extern.msg_queue, msg, 1, 120); msg_queue_push(g_extern.msg_queue, msg, 1, 120);
RARCH_LOG("Applying shader \"%s\".\n", arg); RARCH_LOG("Applying shader \"%s\".\n", arg);
@ -241,6 +244,7 @@ static const struct cmd_action_map action_map[] = {
static bool command_get_arg(const char *tok, const char **arg, unsigned *index) static bool command_get_arg(const char *tok, const char **arg, unsigned *index)
{ {
unsigned i; unsigned i;
for (i = 0; i < ARRAY_SIZE(map); i++) for (i = 0; i < ARRAY_SIZE(map); i++)
{ {
if (strcmp(tok, map[i].str) == 0) if (strcmp(tok, map[i].str) == 0)
@ -300,6 +304,7 @@ static void parse_msg(rarch_cmd_t *handle, char *buf)
{ {
char *save = NULL; char *save = NULL;
const char *tok = strtok_r(buf, "\n", &save); const char *tok = strtok_r(buf, "\n", &save);
while (tok) while (tok)
{ {
parse_sub_msg(handle, tok); parse_sub_msg(handle, tok);
@ -321,14 +326,15 @@ bool rarch_cmd_get(rarch_cmd_t *handle, unsigned id)
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static void network_cmd_poll(rarch_cmd_t *handle) static void network_cmd_poll(rarch_cmd_t *handle)
{ {
fd_set fds;
struct timeval tmp_tv = {0};
if (handle->net_fd < 0) if (handle->net_fd < 0)
return; return;
fd_set fds;
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(handle->net_fd, &fds); FD_SET(handle->net_fd, &fds);
struct timeval tmp_tv = {0};
if (select(handle->net_fd + 1, &fds, NULL, NULL, &tmp_tv) <= 0) if (select(handle->net_fd + 1, &fds, NULL, NULL, &tmp_tv) <= 0)
return; return;
@ -339,6 +345,7 @@ static void network_cmd_poll(rarch_cmd_t *handle)
{ {
char buf[1024]; char buf[1024];
ssize_t ret = recvfrom(handle->net_fd, buf, sizeof(buf) - 1, 0, NULL, NULL); ssize_t ret = recvfrom(handle->net_fd, buf, sizeof(buf) - 1, 0, NULL, NULL);
if (ret <= 0) if (ret <= 0)
break; break;
@ -452,17 +459,22 @@ static size_t read_stdin(char *buf, size_t size)
static void stdin_cmd_poll(rarch_cmd_t *handle) static void stdin_cmd_poll(rarch_cmd_t *handle)
{ {
char *last_newline;
ssize_t ret;
ptrdiff_t msg_len;
if (!handle->stdin_enable) if (!handle->stdin_enable)
return; return;
size_t ret = read_stdin(handle->stdin_buf + handle->stdin_buf_ptr, STDIN_BUF_SIZE - handle->stdin_buf_ptr - 1); ret = read_stdin(handle->stdin_buf + handle->stdin_buf_ptr, STDIN_BUF_SIZE - handle->stdin_buf_ptr - 1);
if (ret == 0) if (ret == 0)
return; return;
handle->stdin_buf_ptr += ret; handle->stdin_buf_ptr += ret;
handle->stdin_buf[handle->stdin_buf_ptr] = '\0'; handle->stdin_buf[handle->stdin_buf_ptr] = '\0';
char *last_newline = strrchr(handle->stdin_buf, '\n'); last_newline = strrchr(handle->stdin_buf, '\n');
if (!last_newline) if (!last_newline)
{ {
// We're receiving bogus data in pipe (no terminating newline), // We're receiving bogus data in pipe (no terminating newline),
@ -477,7 +489,7 @@ static void stdin_cmd_poll(rarch_cmd_t *handle)
} }
*last_newline++ = '\0'; *last_newline++ = '\0';
ptrdiff_t msg_len = last_newline - handle->stdin_buf; msg_len = last_newline - handle->stdin_buf;
parse_msg(handle, handle->stdin_buf); parse_msg(handle, handle->stdin_buf);
@ -502,7 +514,12 @@ void rarch_cmd_poll(rarch_cmd_t *handle)
#if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY)
static bool send_udp_packet(const char *host, uint16_t port, const char *msg) static bool send_udp_packet(const char *host, uint16_t port, const char *msg)
{ {
char port_buf[16];
struct addrinfo hints, *res = NULL; struct addrinfo hints, *res = NULL;
const struct addrinfo *tmp = NULL;
int fd = -1;
bool ret = true;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) #if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
@ -511,19 +528,17 @@ static bool send_udp_packet(const char *host, uint16_t port, const char *msg)
#endif #endif
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
int fd = -1;
bool ret = true;
char port_buf[16];
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
if (getaddrinfo(host, port_buf, &hints, &res) < 0) if (getaddrinfo(host, port_buf, &hints, &res) < 0)
return false; return false;
// Send to all possible targets. // Send to all possible targets.
// "localhost" might resolve to several different IPs. // "localhost" might resolve to several different IPs.
const struct addrinfo *tmp = res; tmp = (const struct addrinfo*)res;
while (tmp) while (tmp)
{ {
ssize_t len, ret_len;
fd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); fd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol);
if (fd < 0) if (fd < 0)
{ {
@ -531,8 +546,9 @@ static bool send_udp_packet(const char *host, uint16_t port, const char *msg)
goto end; goto end;
} }
ssize_t len = strlen(msg); len = strlen(msg);
ssize_t ret_len = sendto(fd, msg, len, 0, tmp->ai_addr, tmp->ai_addrlen); ret_len = sendto(fd, msg, len, 0, tmp->ai_addr, tmp->ai_addrlen);
if (ret_len < len) if (ret_len < len)
{ {
ret = false; ret = false;
@ -554,6 +570,7 @@ end:
static bool verify_command(const char *cmd) static bool verify_command(const char *cmd)
{ {
unsigned i; unsigned i;
if (command_get_arg(cmd, NULL, NULL)) if (command_get_arg(cmd, NULL, NULL))
return true; return true;
@ -570,22 +587,22 @@ static bool verify_command(const char *cmd)
bool network_cmd_send(const char *cmd_) bool network_cmd_send(const char *cmd_)
{ {
if (!netplay_init_network()) char *command, *save;
return false; bool ret;
char *command = strdup(cmd_);
if (!command)
return false;
bool old_verbose = g_extern.verbosity;
g_extern.verbosity = true;
const char *cmd = NULL; const char *cmd = NULL;
const char *host = NULL; const char *host = NULL;
const char *port_ = NULL; const char *port_ = NULL;
bool old_verbose = g_extern.verbosity;
uint16_t port = DEFAULT_NETWORK_CMD_PORT; uint16_t port = DEFAULT_NETWORK_CMD_PORT;
char *save; if (!netplay_init_network())
return false;
if (!(command = strdup(cmd_)))
return false;
g_extern.verbosity = true;
cmd = strtok_r(command, ";", &save); cmd = strtok_r(command, ";", &save);
if (cmd) if (cmd)
host = strtok_r(NULL, ";", &save); host = strtok_r(NULL, ";", &save);
@ -606,7 +623,7 @@ bool network_cmd_send(const char *cmd_)
RARCH_LOG("Sending command: \"%s\" to %s:%hu\n", cmd, host, (unsigned short)port); RARCH_LOG("Sending command: \"%s\" to %s:%hu\n", cmd, host, (unsigned short)port);
bool ret = verify_command(cmd) && send_udp_packet(host, port, cmd); ret = verify_command(cmd) && send_udp_packet(host, port, cmd);
free(command); free(command);
g_extern.verbosity = old_verbose; g_extern.verbosity = old_verbose;