[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 3/4] fish: Detect UTF-8 output and open termcap/terminfo database.



-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming blog: http://rwmj.wordpress.com
Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
>From 3bee9619367383bb3b6e3294289a36e176099c3d Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones redhat com>
Date: Sat, 28 Aug 2010 12:48:49 +0100
Subject: [PATCH 3/4] fish: Detect UTF-8 output and open termcap/terminfo database.

Provide a generic mechanism within guestfish to detect if
output if UTF-8 and to open the termcap (or terminfo) database
for the current terminal type.
---
 fish/fish.c |   35 +++++++++++++++++++++++++++++++++++
 fish/fish.h |    2 ++
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/fish/fish.c b/fish/fish.c
index c535e06..c4ade8c 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <locale.h>
+#include <langinfo.h>
 #include <termios.h>
 
 #ifdef HAVE_LIBREADLINE
@@ -69,6 +70,7 @@ struct mp {
   char *mountpoint;
 };
 
+static void set_up_terminal (void);
 static char add_drives (struct drv *drv, char next_drive);
 static void prepare_drives (struct drv *drv);
 static void mount_mps (struct mp *mp);
@@ -96,6 +98,8 @@ int command_num = 0;
 int keys_from_stdin = 0;
 const char *libvirt_uri = NULL;
 int inspector = 0;
+int utf8_mode = 0;
+int have_terminfo = 0;
 
 static void __attribute__((noreturn))
 usage (int status)
@@ -159,6 +163,8 @@ main (int argc, char *argv[])
   bindtextdomain (PACKAGE, LOCALEBASEDIR);
   textdomain (PACKAGE);
 
+  set_up_terminal ();
+
   enum { HELP_OPTION = CHAR_MAX + 1 };
 
   static const char *options = "a:c:d:Df:h::im:nN:rv?Vx";
@@ -509,6 +515,35 @@ main (int argc, char *argv[])
   exit (EXIT_SUCCESS);
 }
 
+/* The <term.h> header file which defines this has "issues". */
+extern int tgetent (char *, const char *);
+
+static void
+set_up_terminal (void)
+{
+  /* http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate */
+  utf8_mode = STREQ (nl_langinfo (CODESET), "UTF-8");
+
+  char *term = getenv ("TERM");
+  if (term == NULL) {
+    //fprintf (stderr, _("guestfish: TERM (terminal type) not defined.\n"));
+    return;
+  }
+
+  int r = tgetent (NULL, term);
+  if (r == -1) {
+    fprintf (stderr, _("guestfish: could not access termcap or terminfo database.\n"));
+    return;
+  }
+  if (r == 0) {
+    fprintf (stderr, _("guestfish: terminal type \"%s\" not defined.\n"),
+             term);
+    return;
+  }
+
+  have_terminfo = 1;
+}
+
 void
 pod2text (const char *name, const char *shortdesc, const char *str)
 {
diff --git a/fish/fish.h b/fish/fish.h
index 660b8ee..8106610 100644
--- a/fish/fish.h
+++ b/fish/fish.h
@@ -53,6 +53,8 @@ extern int read_only;
 extern int quit;
 extern int verbose;
 extern int command_num;
+extern int utf8_mode;
+extern int have_terminfo;
 extern const char *libvirt_uri;
 extern int issue_command (const char *cmd, char *argv[], const char *pipe);
 extern void pod2text (const char *name, const char *shortdesc, const char *body);
-- 
1.7.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]