obmc-console: Fix readability-braces-around-statements
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I88d2bdcb15106298d83a1cf7176e069092820f1d
diff --git a/config.c b/config.c
index 6fc840c..ce2ed47 100644
--- a/config.c
+++ b/config.c
@@ -47,9 +47,11 @@
{
struct config_item *item;
- for (item = config->items; item; item = item->next)
- if (!strcasecmp(item->name, name))
+ for (item = config->items; item; item = item->next) {
+ if (!strcasecmp(item->name, name)) {
return item->value;
+ }
+ }
return NULL;
}
@@ -65,12 +67,14 @@
int rc;
/* trim leading space */
- for (; *line == ' ' || *line == '\t'; line++)
+ for (; *line == ' ' || *line == '\t'; line++) {
;
+ }
/* skip comments */
- if (*line == '#')
+ if (*line == '#') {
continue;
+ }
name = malloc(strlen(line));
value = malloc(strlen(line));
@@ -139,8 +143,9 @@
struct config *config;
int fd;
- if (!filename)
+ if (!filename) {
filename = config_default_filename;
+ }
fd = open(filename, O_RDONLY);
if (fd < 0) {
@@ -271,16 +276,19 @@
char *suffix;
size_t i;
- if (!size_str)
+ if (!size_str) {
return -1;
+ }
logsize = strtoul(size_str, &suffix, 0);
- if (logsize == 0 || logsize >= UINT32_MAX || suffix == size_str)
+ if (logsize == 0 || logsize >= UINT32_MAX || suffix == size_str) {
return -1;
+ }
/* Ignore spaces between number and suffix */
- while (*suffix && isspace(*suffix))
+ while (*suffix && isspace(*suffix)) {
suffix++;
+ }
for (i = 0; i < num_suffixes; i++) {
if (*suffix == suffixes[i].unit) {
@@ -288,8 +296,9 @@
* If logsize overflows, probably something was wrong.
* Return instead of clamping to an arbitrary value.
*/
- if (logsize > (UINT32_MAX >> suffixes[i].shiftwidth))
+ if (logsize > (UINT32_MAX >> suffixes[i].shiftwidth)) {
return -1;
+ }
logsize <<= suffixes[i].shiftwidth;
suffix++;
@@ -298,8 +307,9 @@
}
/* Allow suffix like 'kB' */
- while (*suffix && (tolower(*suffix) == 'b' || isspace(*suffix)))
+ while (*suffix && (tolower(*suffix) == 'b' || isspace(*suffix))) {
suffix++;
+ }
if (*suffix) {
warn("Invalid suffix!");
diff --git a/console-client.c b/console-client.c
index c63677a..f05a6f8 100644
--- a/console-client.c
+++ b/console-client.c
@@ -90,8 +90,9 @@
/* We need to print everything to skip the tilde */
rc = write_buf_to_fd(client->console_sd, out_buf,
i - (out_buf - buf));
- if (rc < 0)
+ if (rc < 0) {
return PROCESS_ERR;
+ }
out_buf = &buf[i + 1];
break;
case '\r':
@@ -115,10 +116,11 @@
size_t i;
for (i = 0; i < len; ++i) {
- if (buf[i] == esc_state->str[esc_state->pos])
+ if (buf[i] == esc_state->str[esc_state->pos]) {
esc_state->pos++;
- else
+ } else {
esc_state->pos = 0;
+ }
if (esc_state->str[esc_state->pos] == '\0') {
prc = PROCESS_ESC;
@@ -127,8 +129,9 @@
}
}
- if (write_buf_to_fd(client->console_sd, buf, i) < 0)
+ if (write_buf_to_fd(client->console_sd, buf, i) < 0) {
return PROCESS_ERR;
+ }
return prc;
}
@@ -138,10 +141,12 @@
ssize_t len;
len = read(client->fd_in, buf, sizeof(buf));
- if (len < 0)
+ if (len < 0) {
return PROCESS_ERR;
- if (len == 0)
+ }
+ if (len == 0) {
return PROCESS_EXIT;
+ }
switch (client->esc_type) {
case ESC_TYPE_SSH:
@@ -186,8 +191,9 @@
client->fd_out = STDOUT_FILENO;
client->is_tty = isatty(client->fd_in);
- if (!client->is_tty)
+ if (!client->is_tty) {
return 0;
+ }
rc = tcgetattr(client->fd_in, &termios);
if (rc) {
@@ -223,17 +229,19 @@
addr.sun_family = AF_UNIX;
len = console_socket_path(&addr, socket_id);
if (len < 0) {
- if (errno)
+ if (errno) {
warn("Failed to configure socket: %s", strerror(errno));
- else
+ } else {
warn("Socket name length exceeds buffer limits");
+ }
goto cleanup;
}
rc = connect(client->console_sd, (struct sockaddr *)&addr,
sizeof(addr) - sizeof(addr.sun_path) + len);
- if (!rc)
+ if (!rc) {
return 0;
+ }
console_socket_path_readable(&addr, len, path);
warn("Can't connect to console server '@%s'", path);
@@ -244,8 +252,9 @@
static void client_fini(struct console_client *client)
{
- if (client->is_tty)
+ if (client->is_tty) {
tcsetattr(client->fd_in, TCSANOW, &client->orig_termios);
+ }
close(client->console_sd);
}
@@ -266,8 +275,9 @@
for (;;) {
rc = getopt(argc, argv, "c:e:i:");
- if (rc == -1)
+ if (rc == -1) {
break;
+ }
switch (rc) {
case 'c':
@@ -310,12 +320,14 @@
return EXIT_FAILURE;
}
- if (!esc)
+ if (!esc) {
esc = (const uint8_t *)config_get_value(
config, "escape-sequence");
+ }
- if (!socket_id)
+ if (!socket_id) {
socket_id = config_get_value(config, "socket-id");
+ }
}
if (esc) {
@@ -324,12 +336,14 @@
}
rc = client_init(client, socket_id);
- if (rc)
+ if (rc) {
goto out_config_fini;
+ }
rc = client_tty_init(client);
- if (rc)
+ if (rc) {
goto out_client_fini;
+ }
for (;;) {
pollfds[0].fd = client->fd_in;
@@ -343,25 +357,30 @@
break;
}
- if (pollfds[0].revents)
+ if (pollfds[0].revents) {
prc = process_tty(client);
+ }
- if (prc == PROCESS_OK && pollfds[1].revents)
+ if (prc == PROCESS_OK && pollfds[1].revents) {
prc = process_console(client);
+ }
rc = (prc == PROCESS_ERR) ? -1 : 0;
- if (prc != PROCESS_OK)
+ if (prc != PROCESS_OK) {
break;
+ }
}
out_client_fini:
client_fini(client);
out_config_fini:
- if (config_path)
+ if (config_path) {
config_fini(config);
+ }
- if (prc == PROCESS_ESC)
+ if (prc == PROCESS_ESC) {
return EXIT_ESCAPE;
+ }
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
diff --git a/console-server.c b/console-server.c
index 8e24da6..1a1c4be 100644
--- a/console-server.c
+++ b/console-server.c
@@ -116,8 +116,9 @@
/* udev may rename the tty name with a symbol link, try to resolve */
rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
- if (rc < 0)
+ if (rc < 0) {
return -1;
+ }
tty_path_input_real = realpath(tty_path_input, NULL);
if (!tty_path_input_real) {
@@ -135,8 +136,9 @@
rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
tty_kname_real);
- if (rc < 0)
+ if (rc < 0) {
goto out_free;
+ }
tty_device_tty_dir = realpath(tty_class_device_link, NULL);
if (!tty_device_tty_dir) {
@@ -146,16 +148,19 @@
}
rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
- if (rc < 0)
+ if (rc < 0) {
goto out_free;
+ }
console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
- if (!console->tty_sysfs_devnode)
+ if (!console->tty_sysfs_devnode) {
warn("Can't find parent device for %s", tty_kname_real);
+ }
rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
- if (rc < 0)
+ if (rc < 0) {
goto out_free;
+ }
rc = 0;
@@ -176,8 +181,9 @@
int rc;
rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
- if (rc < 0)
+ if (rc < 0) {
return -1;
+ }
fp = fopen(path, "w");
if (!fp) {
@@ -189,9 +195,10 @@
setvbuf(fp, NULL, _IONBF, 0);
rc = fprintf(fp, "0x%x", value);
- if (rc < 0)
+ if (rc < 0) {
warn("Error writing to %s attribute of device %s", name,
console->tty_kname);
+ }
fclose(fp);
out_free:
@@ -214,8 +221,9 @@
}
if (console->tty_baud) {
- if (cfsetspeed(&termios, console->tty_baud) < 0)
+ if (cfsetspeed(&termios, console->tty_baud) < 0) {
warn("Couldn't set speeds for %s", console->tty_kname);
+ }
}
/* Set console to raw mode: we don't want any processing to occur on
@@ -224,8 +232,9 @@
cfmakeraw(&termios);
rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
- if (rc)
+ if (rc) {
warn("Can't set terminal options for %s", console->tty_kname);
+ }
}
static void tty_change_baudrate(struct console *console)
@@ -237,13 +246,15 @@
for (i = 0; i < console->n_handlers; i++) {
handler = console->handlers[i];
- if (!handler->baudrate)
+ if (!handler->baudrate) {
continue;
+ }
rc = handler->baudrate(handler, console->tty_baud);
- if (rc)
+ if (rc) {
warnx("Can't set terminal baudrate for handler %s",
handler->name);
+ }
}
}
@@ -252,11 +263,13 @@
*/
static int tty_init_io(struct console *console)
{
- if (console->tty_sirq)
+ if (console->tty_sirq) {
tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
- if (console->tty_lpc_addr)
+ }
+ if (console->tty_lpc_addr) {
tty_set_sysfs_attr(console, "lpc_address",
console->tty_lpc_addr);
+ }
console->tty_fd = open(console->tty_dev, O_RDWR);
if (console->tty_fd <= 0) {
@@ -315,18 +328,21 @@
val);
}
- if (parsed > 16)
+ if (parsed > 16) {
warn("Invalid LPC SERIRQ: '%s'", val);
+ }
console->tty_sirq = (int)parsed;
- if (endp == optarg)
+ if (endp == optarg) {
warn("Invalid sirq: '%s'", val);
+ }
}
val = config_get_value(config, "baud");
if (val) {
- if (config_parse_baud(&console->tty_baud, val))
+ if (config_parse_baud(&console->tty_baud, val)) {
warnx("Invalid baud rate: '%s'", val);
+ }
}
if (!console->tty_kname) {
@@ -335,8 +351,9 @@
}
rc = tty_find_device(console);
- if (rc)
+ if (rc) {
return rc;
+ }
rc = tty_init_io(console);
return rc;
@@ -390,8 +407,9 @@
int r;
baudrate = parse_baud_to_int(console->tty_baud);
- if (!baudrate)
+ if (!baudrate) {
warnx("Invalid baud rate: '%d'", console->tty_baud);
+ }
r = sd_bus_message_append(reply, "u", baudrate);
@@ -467,8 +485,9 @@
handler = console->handlers[i];
rc = 0;
- if (handler->init)
+ if (handler->init) {
rc = handler->init(handler, console, config);
+ }
handler->active = rc == 0;
@@ -484,8 +503,9 @@
for (i = 0; i < console->n_handlers; i++) {
handler = console->handlers[i];
- if (handler->fini && handler->active)
+ if (handler->fini && handler->active) {
handler->fini(handler);
+ }
}
}
@@ -500,8 +520,9 @@
* convenient for calculations, so convert to that.
*/
rc = clock_gettime(CLOCK_MONOTONIC, &t);
- if (rc)
+ if (rc) {
return rc;
+ }
tv->tv_sec = t.tv_sec;
tv->tv_usec = t.tv_nsec / 1000;
@@ -566,9 +587,11 @@
int i;
/* find the entry in our pollers array */
- for (i = 0; i < console->n_pollers; i++)
- if (console->pollers[i] == poller)
+ for (i = 0; i < console->n_pollers; i++) {
+ if (console->pollers[i] == poller) {
break;
+ }
+ }
assert(i < console->n_pollers);
@@ -607,9 +630,11 @@
int i;
/* find the entry in our pollers array */
- for (i = 0; i < console->n_pollers; i++)
- if (console->pollers[i] == poller)
+ for (i = 0; i < console->n_pollers; i++) {
+ if (console->pollers[i] == poller) {
break;
+ }
+ }
console->pollfds[i].events = (short)(events & 0x7fff);
}
@@ -621,8 +646,9 @@
int rc;
rc = get_current_time(&now);
- if (rc)
+ if (rc) {
return;
+ }
timeradd(&now, tv, &poller->timeout);
}
@@ -686,10 +712,11 @@
if (pollfd->revents) {
prc = poller->event_fn(poller->handler, pollfd->revents,
poller->data);
- if (prc == POLLER_EXIT)
+ if (prc == POLLER_EXIT) {
rc = -1;
- else if (prc == POLLER_REMOVE)
+ } else if (prc == POLLER_REMOVE) {
poller->remove = true;
+ }
}
if ((prc == POLLER_OK) && poller->timeout_fn &&
@@ -724,8 +751,9 @@
break;
}
}
- if (!removed)
+ if (!removed) {
break;
+ }
}
return rc;
@@ -733,8 +761,9 @@
static void sighandler(int signal)
{
- if (signal == SIGINT)
+ if (signal == SIGINT) {
sigint = true;
+ }
}
int run_console(struct console *console)
@@ -786,8 +815,9 @@
break;
}
rc = ringbuffer_queue(console->rb, buf, rc);
- if (rc)
+ if (rc) {
break;
+ }
}
if (console->pollfds[console->n_pollers + 1].revents) {
@@ -796,8 +826,9 @@
/* ... and then the pollers */
rc = call_pollers(console, &tv);
- if (rc)
+ if (rc) {
break;
+ }
}
signal(SIGINT, sighandler_save);
@@ -824,8 +855,9 @@
int c, idx;
c = getopt_long(argc, argv, "c:", options, &idx);
- if (c == -1)
+ if (c == -1) {
break;
+ }
switch (c) {
case 'c':
@@ -838,8 +870,9 @@
}
}
- if (optind < argc)
+ if (optind < argc) {
config_tty_kname = argv[optind];
+ }
console = malloc(sizeof(struct console));
memset(console, 0, sizeof(*console));
@@ -853,8 +886,9 @@
goto out_free;
}
- if (!config_tty_kname)
+ if (!config_tty_kname) {
config_tty_kname = config_get_value(config, "upstream-tty");
+ }
if (!config_tty_kname) {
warnx("No TTY device specified");
@@ -865,8 +899,9 @@
console->tty_kname = config_tty_kname;
rc = tty_init(console, config);
- if (rc)
+ if (rc) {
goto out_config_fini;
+ }
dbus_init(console, config);
diff --git a/console-socket.c b/console-socket.c
index 7785688..792ca62 100644
--- a/console-socket.c
+++ b/console-socket.c
@@ -42,8 +42,9 @@
CONSOLE_SOCKET_PREFIX);
}
- if (rc < 0)
+ if (rc < 0) {
return rc;
+ }
if ((size_t)rc > (sizeof(addr->sun_path) - 1)) {
errno = 0;
@@ -61,8 +62,9 @@
const char *src = (const char *)addr;
size_t len;
- if (addrlen > SSIZE_MAX)
+ if (addrlen > SSIZE_MAX) {
return -EINVAL;
+ }
len = addrlen - sizeof(addr->sun_family) - 1;
memcpy(path, src + sizeof(addr->sun_family) + 1, len);
diff --git a/log-handler.c b/log-handler.c
index 1212b52..5b17c94 100644
--- a/log-handler.c
+++ b/log-handler.c
@@ -84,13 +84,15 @@
if (lh->size + len > lh->maxsize) {
rc = log_trim(lh);
- if (rc)
+ if (rc) {
return rc;
+ }
}
rc = write_buf_to_fd(lh->fd, buf, len);
- if (rc)
+ if (rc) {
return rc;
+ }
lh->size += len;
@@ -109,12 +111,14 @@
* commit straight away. */
for (;;) {
len = ringbuffer_dequeue_peek(lh->rbc, 0, &buf);
- if (!len)
+ if (!len) {
break;
+ }
rc = log_data(lh, buf, len);
- if (rc)
+ if (rc) {
return RINGBUFFER_POLL_REMOVE;
+ }
ringbuffer_dequeue_commit(lh->rbc, len);
}
@@ -146,8 +150,9 @@
lh->maxsize = logsize <= lh->pagesize ? lh->pagesize + 1 : logsize;
filename = config_get_value(config, "logfile");
- if (!filename)
+ if (!filename) {
filename = default_filename;
+ }
lh->fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (lh->fd < 0) {
diff --git a/ringbuffer.c b/ringbuffer.c
index efa5204..856e077 100644
--- a/ringbuffer.c
+++ b/ringbuffer.c
@@ -46,8 +46,9 @@
struct ringbuffer *rb;
rb = malloc(sizeof(*rb) + size);
- if (!rb)
+ if (!rb) {
return NULL;
+ }
memset(rb, 0, sizeof(*rb));
rb->size = size;
@@ -58,8 +59,9 @@
void ringbuffer_fini(struct ringbuffer *rb)
{
- while (rb->n_consumers)
+ while (rb->n_consumers) {
ringbuffer_consumer_unregister(rb->consumers[0]);
+ }
free(rb);
}
@@ -95,9 +97,11 @@
struct ringbuffer *rb = rbc->rb;
int i;
- for (i = 0; i < rb->n_consumers; i++)
- if (rb->consumers[i] == rbc)
+ for (i = 0; i < rb->n_consumers; i++) {
+ if (rb->consumers[i] == rbc) {
break;
+ }
+ }
assert(i < rb->n_consumers);
@@ -120,10 +124,11 @@
size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
{
- if (rbc->pos <= rbc->rb->tail)
+ if (rbc->pos <= rbc->rb->tail) {
return rbc->rb->tail - rbc->pos;
- else
+ } else {
return rbc->rb->tail + rbc->rb->size - rbc->pos;
+ }
}
static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
@@ -137,14 +142,16 @@
enum ringbuffer_poll_ret prc;
size_t force_len;
- if (ringbuffer_space(rbc) >= len)
+ if (ringbuffer_space(rbc) >= len) {
return 0;
+ }
force_len = len - ringbuffer_space(rbc);
prc = rbc->poll_fn(rbc->poll_data, force_len);
- if (prc != RINGBUFFER_POLL_OK)
+ if (prc != RINGBUFFER_POLL_OK) {
return -1;
+ }
return 0;
}
@@ -155,11 +162,13 @@
size_t wlen;
int i, rc;
- if (len >= rb->size)
+ if (len >= rb->size) {
return -1;
+ }
- if (len == 0)
+ if (len == 0) {
return 0;
+ }
/* Ensure there is at least len bytes of space available.
*
@@ -212,14 +221,16 @@
size_t pos;
size_t len;
- if (offset >= ringbuffer_len(rbc))
+ if (offset >= ringbuffer_len(rbc)) {
return 0;
+ }
pos = (rbc->pos + offset) % rb->size;
- if (pos <= rb->tail)
+ if (pos <= rb->tail) {
len = rb->tail - pos;
- else
+ } else {
len = rb->size - pos;
+ }
*data = rb->buf + pos;
return len;
diff --git a/socket-handler.c b/socket-handler.c
index 954c326..d05fec3 100644
--- a/socket-handler.c
+++ b/socket-handler.c
@@ -70,15 +70,19 @@
int idx;
close(client->fd);
- if (client->poller)
+ if (client->poller) {
console_poller_unregister(sh->console, client->poller);
+ }
- if (client->rbc)
+ if (client->rbc) {
ringbuffer_consumer_unregister(client->rbc);
+ }
- for (idx = 0; idx < sh->n_clients; idx++)
- if (sh->clients[idx] == client)
+ for (idx = 0; idx < sh->n_clients; idx++) {
+ if (sh->clients[idx] == client) {
break;
+ }
+ }
assert(idx < sh->n_clients);
@@ -102,14 +106,16 @@
{
int events;
- if (client->blocked == blocked)
+ if (client->blocked == blocked) {
return;
+ }
client->blocked = blocked;
events = POLLIN;
- if (client->blocked)
+ if (client->blocked) {
events |= POLLOUT;
+ }
console_poller_set_events(client->sh->console, client->poller, events);
}
@@ -121,14 +127,16 @@
ssize_t rc;
size_t pos;
- if (len > SSIZE_MAX)
+ if (len > SSIZE_MAX) {
return -EINVAL;
+ }
fd = client->fd;
flags = MSG_NOSIGNAL;
- if (!block)
+ if (!block) {
flags |= MSG_DONTWAIT;
+ }
for (pos = 0; pos < len; pos += rc) {
rc = send(fd, (char *)buf + pos, len - pos, flags);
@@ -139,13 +147,15 @@
break;
}
- if (errno == EINTR)
+ if (errno == EINTR) {
continue;
+ }
return -1;
}
- if (rc == 0)
+ if (rc == 0) {
return -1;
+ }
}
return (ssize_t)pos;
@@ -166,29 +176,35 @@
block = !!force_len;
/* if we're already blocked, no need for the write */
- if (!block && client->blocked)
+ if (!block && client->blocked) {
return 0;
+ }
for (;;) {
len = ringbuffer_dequeue_peek(client->rbc, total_len, &buf);
- if (!len)
+ if (!len) {
break;
+ }
wlen = send_all(client, buf, len, block);
- if (wlen <= 0)
+ if (wlen <= 0) {
break;
+ }
total_len += wlen;
- if (force_len && total_len >= force_len)
+ if (force_len && total_len >= force_len) {
break;
+ }
}
- if (wlen < 0)
+ if (wlen < 0) {
return -1;
+ }
- if (force_len && total_len < force_len)
+ if (force_len && total_len < force_len) {
return -1;
+ }
ringbuffer_dequeue_commit(client->rbc, total_len);
return 0;
@@ -253,13 +269,15 @@
if (events & POLLIN) {
rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT);
if (rc < 0) {
- if (errno == EAGAIN || errno == EWOULDBLOCK)
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
return POLLER_OK;
- else
+ } else {
goto err_close;
+ }
}
- if (rc == 0)
+ if (rc == 0) {
goto err_close;
+ }
console_data_out(sh->console, buf, rc);
}
@@ -267,8 +285,9 @@
if (events & POLLOUT) {
client_set_blocked(client, false);
rc = client_drain_queue(client, 0);
- if (rc)
+ if (rc) {
goto err_close;
+ }
}
return POLLER_OK;
@@ -286,12 +305,14 @@
struct client *client;
int fd, n;
- if (!(events & POLLIN))
+ if (!(events & POLLIN)) {
return POLLER_OK;
+ }
fd = accept(sh->sd, NULL, NULL);
- if (fd < 0)
+ if (fd < 0) {
return POLLER_OK;
+ }
client = malloc(sizeof(*client));
memset(client, 0, sizeof(*client));
@@ -335,10 +356,11 @@
addr.sun_family = AF_UNIX;
len = console_socket_path(&addr, config_get_value(config, "socket-id"));
if (len < 0) {
- if (errno)
+ if (errno) {
warn("Failed to configure socket: %s", strerror(errno));
- else
+ } else {
warn("Socket name length exceeds buffer limits");
+ }
return -1;
}
@@ -385,11 +407,13 @@
{
struct socket_handler *sh = to_socket_handler(handler);
- while (sh->n_clients)
+ while (sh->n_clients) {
client_close(sh->clients[0]);
+ }
- if (sh->poller)
+ if (sh->poller) {
console_poller_unregister(sh->console, sh->poller);
+ }
close(sh->sd);
}
diff --git a/test/ringbuffer-test-utils.c b/test/ringbuffer-test-utils.c
index 56e37ee..fff4325 100644
--- a/test/ringbuffer-test-utils.c
+++ b/test/ringbuffer-test-utils.c
@@ -36,30 +36,35 @@
size_t len, total_len;
uint8_t *buf;
- if (ctx->ignore_poll)
+ if (ctx->ignore_poll) {
return RINGBUFFER_POLL_OK;
+ }
- if (ctx->force_only && !force_len)
+ if (ctx->force_only && !force_len) {
return RINGBUFFER_POLL_OK;
+ }
ctx->count++;
total_len = 0;
for (;;) {
len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf);
- if (!len)
+ if (!len) {
break;
+ }
- if (ctx->force_only && total_len + len > force_len)
+ if (ctx->force_only && total_len + len > force_len) {
len = force_len - total_len;
+ }
ctx->data = realloc(ctx->data, ctx->len + len);
memcpy(ctx->data + ctx->len, buf, len);
ctx->len += len;
total_len += len;
- if (ctx->force_only && total_len >= force_len)
+ if (ctx->force_only && total_len >= force_len) {
break;
+ }
}
ringbuffer_dequeue_commit(ctx->rbc, total_len);
@@ -79,16 +84,19 @@
bool has_consumer = false;
const char *prefix = "";
- if (rb->tail == i)
+ if (rb->tail == i) {
prefix = "tail=>";
+ }
printf("%6s %02x", prefix, rb->buf[i]);
for (j = 0; j < rb->n_consumers; j++) {
rbc = rb->consumers[j];
- if (rbc->pos != i)
+ if (rbc->pos != i) {
continue;
- if (!has_consumer)
+ }
+ if (!has_consumer) {
printf(" <=");
+ }
printf("c[%d],len=%zd ", j, ringbuffer_len(rbc));
has_consumer = true;
}
diff --git a/test/test-client-escape.c b/test/test-client-escape.c
index b818889..b3083b7 100644
--- a/test/test-client-escape.c
+++ b/test/test-client-escape.c
@@ -180,8 +180,9 @@
return -1;
}
- if (ctx->cur_in >= ctx->test->n_in)
+ if (ctx->cur_in >= ctx->test->n_in) {
return 0;
+ }
inbuf = ctx->test->in[ctx->cur_in];
inlen = strlen(inbuf);
@@ -209,8 +210,9 @@
for (;;) {
rc = process_tty(&ctx->client);
- if (rc != PROCESS_OK)
+ if (rc != PROCESS_OK) {
break;
+ }
}
exp_out_len = strlen(test->exp_out);
@@ -229,8 +231,9 @@
{
size_t i;
- for (i = 0; i < ARRAY_SIZE(tests); i++)
+ for (i = 0; i < ARRAY_SIZE(tests); i++) {
run_one_test(i, &tests[i], &ctxs[i]);
+ }
return EXIT_SUCCESS;
}
diff --git a/test/test-config-parse-logsize.c b/test/test-config-parse-logsize.c
index 9bd4f79..4946cf0 100644
--- a/test/test-config-parse-logsize.c
+++ b/test/test-config-parse-logsize.c
@@ -58,8 +58,9 @@
test_data[i].expected_size, size);
}
assert(rc == test_data[i].expected_rc);
- if (rc == 0)
+ if (rc == 0) {
assert(size == test_data[i].expected_size);
+ }
}
}
diff --git a/test/test-config-parse.c b/test/test-config-parse.c
index 7d2d687..df90e91 100644
--- a/test/test-config-parse.c
+++ b/test/test-config-parse.c
@@ -23,8 +23,9 @@
config_parse(ctx, buf);
free(buf);
found = config_get_value(ctx, key);
- if (!expected)
+ if (!expected) {
assert(!found);
+ }
if (expected) {
assert(found);
assert(!strcmp(expected, found));
diff --git a/test/test-ringbuffer-boundary-read.c b/test/test-ringbuffer-boundary-read.c
index 642c345..f62f66f 100644
--- a/test/test-ringbuffer-boundary-read.c
+++ b/test/test-ringbuffer-boundary-read.c
@@ -34,8 +34,9 @@
pos = 0;
for (;;) {
len = ringbuffer_dequeue_peek(rbc, pos, &out_buf);
- if (len == 0)
+ if (len == 0) {
break;
+ }
assert(!memcmp(in_buf + pos, out_buf, len));
pos += len;
}
diff --git a/test/test-ringbuffer-contained-offset-read.c b/test/test-ringbuffer-contained-offset-read.c
index 7490f01..1fbe1cb 100644
--- a/test/test-ringbuffer-contained-offset-read.c
+++ b/test/test-ringbuffer-contained-offset-read.c
@@ -25,8 +25,9 @@
for (i = 0; i <= sizeof(in_buf); i++) {
len = ringbuffer_dequeue_peek(rbc, i, &out_buf);
assert(len == sizeof(in_buf) - i);
- if (len)
+ if (len) {
assert(!memcmp(in_buf + i, out_buf, len));
+ }
}
ringbuffer_fini(rb);
diff --git a/tty-handler.c b/tty-handler.c
index dd348b6..a02d899 100644
--- a/tty-handler.c
+++ b/tty-handler.c
@@ -46,8 +46,9 @@
int flags;
flags = th->fd_flags & ~O_NONBLOCK;
- if (!fd_blocking)
+ if (!fd_blocking) {
flags |= O_NONBLOCK;
+ }
if (flags != th->fd_flags) {
fcntl(th->fd, F_SETFL, flags);
@@ -68,14 +69,16 @@
{
int events;
- if (blocked == th->blocked)
+ if (blocked == th->blocked) {
return;
+ }
th->blocked = blocked;
events = POLLIN;
- if (th->blocked)
+ if (th->blocked) {
events |= POLLOUT;
+ }
console_poller_set_events(th->console, th->poller, events);
}
@@ -87,28 +90,32 @@
uint8_t *buf;
/* if we're forcing data, we need to clear non-blocking mode */
- if (force_len)
+ if (force_len) {
tty_set_fd_blocking(th, true);
- /* no point writing, we'll just see -EAGAIN */
- else if (th->blocked)
+ /* no point writing, we'll just see -EAGAIN */
+ } else if (th->blocked) {
return 0;
+ }
total_len = 0;
for (;;) {
len = ringbuffer_dequeue_peek(th->rbc, total_len, &buf);
- if (!len)
+ if (!len) {
break;
+ }
/* write as little as possible while blocking */
- if (force_len && force_len < total_len + len)
+ if (force_len && force_len < total_len + len) {
len = force_len - total_len;
+ }
wlen = write(th->fd, buf, len);
if (wlen < 0) {
- if (errno == EINTR)
+ if (errno == EINTR) {
continue;
+ }
if ((errno == EAGAIN || errno == EWOULDBLOCK) &&
!force_len) {
tty_set_blocked(th, true);
@@ -120,14 +127,16 @@
total_len += wlen;
- if (force_len && total_len >= force_len)
+ if (force_len && total_len >= force_len) {
break;
+ }
}
ringbuffer_dequeue_commit(th->rbc, total_len);
- if (force_len)
+ if (force_len) {
tty_set_fd_blocking(th, false);
+ }
return 0;
}
@@ -156,8 +165,9 @@
if (events & POLLIN) {
len = read(th->fd, buf, sizeof(buf));
- if (len <= 0)
+ if (len <= 0) {
goto err;
+ }
console_data_out(th->console, buf, len);
}
@@ -165,8 +175,9 @@
if (events & POLLOUT) {
tty_set_blocked(th, false);
rc = tty_drain_queue(th, 0);
- if (rc)
+ if (rc) {
goto err;
+ }
}
return POLLER_OK;
@@ -235,12 +246,14 @@
int rc;
tty_name = config_get_value(config, "local-tty");
- if (!tty_name)
+ if (!tty_name) {
return -1;
+ }
rc = asprintf(&tty_path, "/dev/%s", tty_name);
- if (!rc)
+ if (!rc) {
return -1;
+ }
th->fd = open(tty_path, O_RDWR | O_NONBLOCK);
if (th->fd < 0) {
@@ -260,15 +273,17 @@
tty_baud);
} else {
rc = set_terminal_baud(th, tty_name, desired_speed);
- if (rc)
+ if (rc) {
fprintf(stderr,
"Couldn't set baud rate for %s to %s\n",
tty_name, tty_baud);
+ }
}
}
- if (make_terminal_raw(th, tty_name) != 0)
+ if (make_terminal_raw(th, tty_name) != 0) {
fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name);
+ }
th->poller = console_poller_register(console, handler, tty_poll, NULL,
th->fd, POLLIN, NULL);
@@ -282,8 +297,9 @@
static void tty_fini(struct handler *handler)
{
struct tty_handler *th = to_tty_handler(handler);
- if (th->poller)
+ if (th->poller) {
console_poller_unregister(th->console, th->poller);
+ }
close(th->fd);
}