]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Rename vshMakeStdinRaw to vshTTYMakeRaw and move it to virsh.c
authorPeter Krempa <pkrempa@redhat.com>
Thu, 29 Aug 2013 16:15:07 +0000 (18:15 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 3 Sep 2013 12:06:11 +0000 (14:06 +0200)
Move the function to virsh.c to the rest of the TTY managing functions
and change the code so that it mirrors the rest.

tools/virsh-console.c
tools/virsh-console.h
tools/virsh-domain.c
tools/virsh.c
tools/virsh.h

index 9e5d66489f1e1496d45f1c7296b0887f692697a4..7e6b979c028625fb7bd931310a715ac1bf618a4d 100644 (file)
@@ -38,6 +38,7 @@
 # include <c-ctype.h>
 
 # include "internal.h"
+# include "virsh.h"
 # include "virsh-console.h"
 # include "virlog.h"
 # include "virfile.h"
@@ -86,20 +87,6 @@ virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED)
 }
 
 
-# ifndef HAVE_CFMAKERAW
-static void
-cfmakeraw(struct termios *attr)
-{
-    attr->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
-                         | INLCR | IGNCR | ICRNL | IXON);
-    attr->c_oflag &= ~OPOST;
-    attr->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
-    attr->c_cflag &= ~(CSIZE | PARENB);
-    attr->c_cflag |= CS8;
-}
-# endif /* !HAVE_CFMAKERAW */
-
-
 static void
 virConsoleShutdown(virConsolePtr con)
 {
@@ -320,40 +307,12 @@ vshGetEscapeChar(const char *s)
 
 
 int
-vshMakeStdinRaw(struct termios *ttyattr, bool report_errors)
-{
-    struct termios rawattr;
-    char ebuf[1024];
-
-    if (tcgetattr(STDIN_FILENO, ttyattr) < 0) {
-        if (report_errors)
-            VIR_ERROR(_("unable to get tty attributes: %s"),
-                      virStrerror(errno, ebuf, sizeof(ebuf)));
-        return -1;
-    }
-
-    rawattr = *ttyattr;
-    cfmakeraw(&rawattr);
-
-    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) {
-        if (report_errors)
-            VIR_ERROR(_("unable to set tty attributes: %s"),
-                      virStrerror(errno, ebuf, sizeof(ebuf)));
-        return -1;
-    }
-
-    return 0;
-}
-
-
-int
-vshRunConsole(virDomainPtr dom,
+vshRunConsole(vshControl *ctl,
+              virDomainPtr dom,
               const char *dev_name,
-              const char *escape_seq,
               unsigned int flags)
 {
     int ret = -1;
-    struct termios ttyattr;
     void (*old_sigquit)(int);
     void (*old_sigterm)(int);
     void (*old_sigint)(int);
@@ -366,7 +325,7 @@ vshRunConsole(virDomainPtr dom,
        result in it being echoed back already), and
        also ensure Ctrl-C, etc is blocked, and misc
        other bits */
-    if (vshMakeStdinRaw(&ttyattr, true) < 0)
+    if (vshTTYMakeRaw(ctl, true) < 0)
         goto resettty;
 
     /* Trap all common signals so that we can safely restore
@@ -383,7 +342,7 @@ vshRunConsole(virDomainPtr dom,
     if (VIR_ALLOC(con) < 0)
         goto cleanup;
 
-    con->escapeChar = vshGetEscapeChar(escape_seq);
+    con->escapeChar = vshGetEscapeChar(ctl->escapeChar);
     con->st = virStreamNew(virDomainGetConnect(dom),
                            VIR_STREAM_NONBLOCK);
     if (!con->st)
@@ -434,7 +393,7 @@ cleanup:
 resettty:
     /* Put STDIN back into the (sane?) state we found
        it in before starting */
-    tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);
+    vshTTYRestore(ctl);
 
     return ret;
 }
index 1e3160f0feb0f13b3a3b9769161ec0c3d8f7c07b..5b82e286d5fcc5eaf826d9e1e0d5b74cf12c5924 100644 (file)
 
 # ifndef WIN32
 
-#  include <termios.h>
+#  include <virsh.h>
 
-int vshRunConsole(virDomainPtr dom,
+int vshRunConsole(vshControl *ctl,
+                  virDomainPtr dom,
                   const char *dev_name,
-                  const char *escape_seq,
                   unsigned int flags);
 
-int vshMakeStdinRaw(struct termios *ttyattr, bool report_errors);
-
 # endif /* !WIN32 */
 
 #endif /* __VIR_CONSOLE_H__ */
index 9932ce8125b3c0318709e6fffc1921544840e31d..4f85f5f7e5d7ada3a17c46e2d6911159f6a3066d 100644 (file)
@@ -2263,7 +2263,7 @@ cmdRunConsole(vshControl *ctl, virDomainPtr dom,
     vshPrintExtra(ctl, _("Connected to domain %s\n"), virDomainGetName(dom));
     vshPrintExtra(ctl, _("Escape character is %s\n"), ctl->escapeChar);
     fflush(stdout);
-    if (vshRunConsole(dom, name, ctl->escapeChar, flags) == 0)
+    if (vshRunConsole(ctl, dom, name, flags) == 0)
         ret = true;
 
  cleanup:
index 0cc9bdd3b8ba3637d7ad5e1d16284ff8dff07ba9..37e971659fab2c5195b4994dd0899d1b9651e81c 100644 (file)
@@ -458,14 +458,13 @@ int
 vshAskReedit(vshControl *ctl, const char *msg)
 {
     int c = -1;
-    struct termios ttyattr;
 
     if (!isatty(STDIN_FILENO))
         return -1;
 
     vshReportError(ctl);
 
-    if (vshMakeStdinRaw(&ttyattr, false) < 0)
+    if (vshTTYMakeRaw(ctl, false) < 0)
         return -1;
 
     while (true) {
@@ -488,7 +487,7 @@ vshAskReedit(vshControl *ctl, const char *msg)
         }
     }
 
-    tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);
+    vshTTYRestore(ctl);
 
     vshPrint(ctl, "\r\n");
     return c;
@@ -2260,6 +2259,49 @@ vshTTYRestore(vshControl *ctl)
 }
 
 
+#ifndef HAVE_CFMAKERAW
+/* provide fallback in case cfmakeraw isn't available */
+static void
+cfmakeraw(struct termios *attr)
+{
+    attr->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
+                         | INLCR | IGNCR | ICRNL | IXON);
+    attr->c_oflag &= ~OPOST;
+    attr->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
+    attr->c_cflag &= ~(CSIZE | PARENB);
+    attr->c_cflag |= CS8;
+}
+#endif /* !HAVE_CFMAKERAW */
+
+
+int
+vshTTYMakeRaw(vshControl *ctl, bool report_errors)
+{
+    struct termios rawattr = ctl->termattr;
+    char ebuf[1024];
+
+    if (!ctl->istty) {
+        if (report_errors) {
+            vshError(ctl, "%s",
+                     _("unable to make terminal raw: console isn't a tty"));
+        }
+
+        return -1;
+    }
+
+    cfmakeraw(&rawattr);
+
+    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) {
+        if (report_errors)
+            vshError(ctl, _("unable to set tty attributes: %s"),
+                     virStrerror(errno, ebuf, sizeof(ebuf)));
+        return -1;
+    }
+
+    return 0;
+}
+
+
 void
 vshError(vshControl *ctl, const char *format, ...)
 {
index db5934f99abbc991d39b34413f7a88b2fc1c1ff6..8afe13f60d2e062ca133914edc864b74fd492a5f 100644 (file)
@@ -358,6 +358,7 @@ void vshSaveLibvirtError(void);
 bool vshTTYIsInterruptCharacter(vshControl *ctl, const char chr);
 int vshTTYDisableInterrupt(vshControl *ctl);
 int vshTTYRestore(vshControl *ctl);
+int vshTTYMakeRaw(vshControl *ctl, bool report_errors);
 
 /* allocation wrappers */
 void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);