/*-
* BSD LICENSE
- *
- * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
+ *
+ * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
* All rights reserved.
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
- *
+ *
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#include <stdarg.h>
#include <inttypes.h>
#include <fcntl.h>
+#include <poll.h>
#include <errno.h>
#include <termios.h>
#include <netinet/in.h>
{
if (!cl || !prompt)
return;
- rte_snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
+ snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
}
struct cmdline *
va_start(ap, fmt);
ret = vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap);
- if (ret < 0)
+ if (ret < 0) {
+ free(buf);
return;
+ }
if (ret >= BUFSIZ)
ret = BUFSIZ - 1;
write(cl->s_out, buf, ret);
rdline_quit(&cl->rdl);
}
+int
+cmdline_poll(struct cmdline *cl)
+{
+ struct pollfd pfd;
+ int status;
+ ssize_t read_status;
+ char c;
+
+ if (!cl)
+ return -EINVAL;
+ else if (cl->rdl.status == RDLINE_EXITED)
+ return RDLINE_EXITED;
+
+ pfd.fd = cl->s_in;
+ pfd.events = POLLIN;
+ pfd.revents = 0;
+
+ status = poll(&pfd, 1, 0);
+ if (status < 0)
+ return status;
+ else if (status > 0) {
+ c = -1;
+ read_status = read(cl->s_in, &c, 1);
+ if (read_status < 0)
+ return read_status;
+
+ status = cmdline_in(cl, &c, 1);
+ if (status < 0 && cl->rdl.status != RDLINE_EXITED)
+ return status;
+ }
+
+ return cl->rdl.status;
+}
+
void
cmdline_interact(struct cmdline *cl)
{
c = -1;
while (1) {
- if (read(cl->s_in, &c, 1) < 0)
+ if (read(cl->s_in, &c, 1) <= 0)
break;
if (cmdline_in(cl, &c, 1) < 0)
break;