tty.c: Refactor parsing of tty .conf stanza in tty_register()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-07-08 15:30:51 +02:00
1 parent eec32e299b
commit 60f73dc952
1 file changed
+21 -26
+21 -26
View File
@@ -22,6 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include <ctype.h> /* isdigit() */
#include <signal.h> #include <signal.h>
#include <termios.h> #include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@@ -48,7 +49,7 @@ int tty_register(char *line)
{ {
tty_node_t *entry; tty_node_t *entry;
int insert = 0; int insert = 0;
char *cmd, *dev, *baud = NULL; char *tok, *dev = NULL, *baud = NULL;
char *runlevels = NULL, *term = NULL; char *runlevels = NULL, *term = NULL;
if (!line) { if (!line) {
@@ -56,45 +57,39 @@ int tty_register(char *line)
return errno = EINVAL; return errno = EINVAL;
} }
cmd = strtok(line, " "); tok = strtok(line, " ");
if (!cmd) { while (tok) {
incomplete: if (tok[0] == '[')
runlevels = &tok[0];
else if (tok[0] == '/')
dev = tok;
else if (isdigit(tok[0]))
baud = tok;
else
term = tok;
tok = strtok(NULL, " ");
}
if (!dev) {
_e("Incomplete tty, cannot register"); _e("Incomplete tty, cannot register");
return errno = EINVAL; return errno = EINVAL;
} }
if (cmd[0] == '[') { /* [runlevels] */
runlevels = &cmd[0];
dev = strtok(NULL, " ");
if (!dev)
goto incomplete;
} else {
dev = cmd;
}
cmd = strtok(NULL, " ");
if (cmd) {
baud = strdup(cmd);
term = strtok(NULL, " ");
}
entry = tty_find(dev); entry = tty_find(dev);
if (!entry) { if (!entry) {
insert = 1; insert = 1;
entry = calloc(1, sizeof(*entry)); entry = calloc(1, sizeof(*entry));
if (!entry) { if (!entry)
if (baud)
free(baud);
return errno = ENOMEM; return errno = ENOMEM;
}
} }
entry->data.name = strdup(dev); entry->data.name = strdup(dev);
entry->data.baud = baud; entry->data.baud = baud ? strdup(baud) : NULL;
entry->data.term = term ? strdup(term) : NULL; entry->data.term = term ? strdup(term) : NULL;
entry->data.runlevels = conf_parse_runlevels(runlevels); entry->data.runlevels = conf_parse_runlevels(runlevels);
_d("Registering tty %s at %s baud with term=%s on runlevels %s", dev, baud ?: "NULL", term ?: "N/A", runlevels); _d("Registering tty %s at %s baud with term=%s on runlevels %s",
dev, baud ?: "NULL", term ?: "N/A", runlevels ?: "[2-5]");
if (insert) if (insert)
LIST_INSERT_HEAD(&tty_list, entry, link); LIST_INSERT_HEAD(&tty_list, entry, link);