]> git.proxmox.com Git - qemu.git/blobdiff - cmd.c
slirp: Replace m_freem with m_free
[qemu.git] / cmd.c
diff --git a/cmd.c b/cmd.c
index e2f4486a10f2f27d98c0e2045b9511bce056ee4a..ecca167399bc817358430ad4d730c0d309a9d1a0 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -12,8 +12,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
+#include <sys/time.h>
+#include <getopt.h>
 
 #include "cmd.h"
+#include "qemu-aio.h"
 
 #define _(x)   x       /* not gettext support yet */
 
-extern int optind;
-
 /* from libxcmd/command.c */
 
 cmdinfo_t      *cmdtab;
@@ -150,10 +150,20 @@ add_args_command(
        args_func = af;
 }
 
+static void prep_fetchline(void *opaque)
+{
+    int *fetchable = opaque;
+
+    qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
+    *fetchable= 1;
+}
+
+static char *get_prompt(void);
+
 void
 command_loop(void)
 {
-       int             c, i, j = 0, done = 0;
+       int             c, i, j = 0, done = 0, fetchable = 0, prompted = 0;
        char            *input;
        char            **v;
        const cmdinfo_t *ct;
@@ -187,7 +197,21 @@ command_loop(void)
                free(cmdline);
                return;
        }
+
        while (!done) {
+        if (!prompted) {
+            printf("%s", get_prompt());
+            fflush(stdout);
+            qemu_aio_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, NULL,
+                                    NULL, &fetchable);
+            prompted = 1;
+        }
+
+        qemu_aio_wait();
+
+        if (!fetchable) {
+            continue;
+        }
                if ((input = fetchline()) == NULL)
                        break;
                v = breakline(input, &c);
@@ -200,7 +224,11 @@ command_loop(void)
                                        v[0]);
                }
                doneline(input, v);
+
+        prompted = 0;
+        fetchable = 0;
        }
+    qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
 }
 
 /* from libxcmd/input.c */
@@ -212,8 +240,6 @@ command_loop(void)
 # include <histedit.h>
 #endif
 
-extern char *progname;
-
 static char *
 get_prompt(void)
 {
@@ -273,8 +299,6 @@ fetchline(void)
 
        if (!line)
                return NULL;
-       printf("%s", get_prompt());
-       fflush(stdout);
        if (!fgets(line, MAXREADLINESZ, stdin)) {
                free(line);
                return NULL;
@@ -286,6 +310,27 @@ fetchline(void)
 }
 #endif
 
+static char *qemu_strsep(char **input, const char *delim)
+{
+    char *result = *input;
+    if (result != NULL) {
+        char *p;
+
+        for (p = result; *p != '\0'; p++) {
+            if (strchr(delim, *p)) {
+                break;
+            }
+        }
+        if (*p == '\0') {
+            *input = NULL;
+        } else {
+            *p = '\0';
+            *input = p + 1;
+        }
+    }
+    return result;
+}
+
 char **
 breakline(
        char    *input,
@@ -295,7 +340,7 @@ breakline(
        char    *p;
        char    **rval = calloc(sizeof(char *), 1);
 
-       while (rval && (p = strsep(&input, " ")) != NULL) {
+       while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
                if (!*p)
                        continue;
                c++;
@@ -441,7 +486,7 @@ timestr(
                        snprintf(ts, size, "%u:%02u.%02u",
                                (unsigned int) MINUTES(tv->tv_sec),
                                (unsigned int) SECONDS(tv->tv_sec),
-                               (unsigned int) usec * 100);
+                               (unsigned int) (usec * 100));
                        return;
                }
                format |= VERBOSE_FIXED_TIME;   /* fallback if hours needed */
@@ -452,9 +497,9 @@ timestr(
                        (unsigned int) HOURS(tv->tv_sec),
                        (unsigned int) MINUTES(tv->tv_sec),
                        (unsigned int) SECONDS(tv->tv_sec),
-                       (unsigned int) usec * 100);
+                       (unsigned int) (usec * 100));
        } else {
-               snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
+               snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
        }
 }