From: Richard Jones Date: Sat, 28 Aug 2010 11:48:49 +0000 (+0100) Subject: fish: Detect UTF-8 output and open termcap/terminfo database. X-Git-Tag: 1.5.7~12 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=3003df6bbc889c3939e6c478462dc4478d5b89f7;p=libguestfs.git 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. --- 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 #include #include +#include #include #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 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);