From 7ed5174aa7ca223bf5502b936e4a160d60a7a9a4 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 14 Mar 2012 07:10:54 -0400 Subject: [PATCH] Move some locale handling into one place --- src/examples/Makefile.am | 2 +- src/examples/benchmark.cc | 14 +++-------- src/examples/parse.cc | 13 +++-------- src/examples/termemu.cc | 13 +++-------- src/frontend/mosh-client.cc | 6 ++--- src/frontend/mosh-server.cc | 14 +++-------- src/frontend/stmclient.cc | 8 ++----- src/terminal/parser.cc | 2 +- src/util/Makefile.am | 2 +- src/util/locale_utils.cc | 46 +++++++++++++++++++++++++++++++++++++ src/util/locale_utils.h | 25 ++++++++++++++++++++ 11 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 src/util/locale_utils.cc create mode 100644 src/util/locale_utils.h diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index 47b4bbd..a9fa363 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am @@ -34,7 +34,7 @@ ntester_LDADD = ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a .. benchmark_SOURCES = benchmark.cc benchmark_CPPFLAGS = -I$(srcdir)/../util -I$(srcdir)/../statesync -I$(srcdir)/../terminal -I../protobufs -I$(srcdir)/../frontend -I$(srcdir)/../crypto $(BOOST_CPPFLAGS) -I$(srcdir)/../network -benchmark_LDADD = ../frontend/terminaloverlay.o ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../protobufs/libmoshprotos.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS) +benchmark_LDADD = ../frontend/terminaloverlay.o ../statesync/libmoshstatesync.a ../terminal/libmoshterminal.a ../protobufs/libmoshprotos.a ../network/libmoshnetwork.a ../crypto/libmoshcrypto.a ../util/libmoshutil.a -lutil -lm $(BOOST_LDFLAGS) $(protobuf_LIBS) if COND_THIRD_LIBSTDDJB benchmark_CPPFLAGS += -I$(top_srcdir)/third/libstddjb benchmark_LDADD += $(top_builddir)/third/libstddjb/libstddjb.a diff --git a/src/examples/benchmark.cc b/src/examples/benchmark.cc index ef97f2e..7bd993d 100644 --- a/src/examples/benchmark.cc +++ b/src/examples/benchmark.cc @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -47,6 +46,7 @@ extern "C" { #include "completeterminal.h" #include "user.h" #include "terminaloverlay.h" +#include "locale_utils.h" const int ITERATIONS = 100000; @@ -63,16 +63,8 @@ int main( void ) Complete local_terminal( 80, 24 ); /* Adopt native locale */ - if ( NULL == setlocale( LC_ALL, "" ) ) { - perror( "setlocale" ); - exit( 1 ); - } - - /* Verify locale calls for UTF-8 */ - if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { - fprintf( stderr, "mosh requires a UTF-8 locale.\n" ); - exit( 1 ); - } + set_native_locale(); + assert_utf8_locale(); for ( int i = 0; i < ITERATIONS; i++ ) { /* type a character */ diff --git a/src/examples/parse.cc b/src/examples/parse.cc index c98f392..9725e81 100644 --- a/src/examples/parse.cc +++ b/src/examples/parse.cc @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -41,6 +40,7 @@ #include "parser.h" #include "swrite.h" +#include "locale_utils.h" const size_t buf_size = 1024; @@ -55,15 +55,8 @@ int main( int argc __attribute__((unused)), int master; struct termios saved_termios, raw_termios, child_termios; - if ( NULL == setlocale( LC_ALL, "" ) ) { - perror( "setlocale" ); - exit( 1 ); - } - - if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { - fprintf( stderr, "stm requires a UTF-8 locale.\n" ); - exit( 1 ); - } + set_native_locale(); + assert_utf8_locale(); if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) { perror( "tcgetattr" ); diff --git a/src/examples/termemu.cc b/src/examples/termemu.cc index 502a0d2..a40b584 100644 --- a/src/examples/termemu.cc +++ b/src/examples/termemu.cc @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -50,6 +49,7 @@ #include "completeterminal.h" #include "swrite.h" #include "fatal_assert.h" +#include "locale_utils.h" extern "C" { #include "selfpipe.h" @@ -64,15 +64,8 @@ int main( void ) int master; struct termios saved_termios, raw_termios, child_termios; - if ( NULL == setlocale( LC_ALL, "" ) ) { - perror( "setlocale" ); - exit( 1 ); - } - - if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { - fprintf( stderr, "stm requires a UTF-8 locale.\n" ); - exit( 1 ); - } + set_native_locale(); + assert_utf8_locale(); if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) { perror( "tcgetattr" ); diff --git a/src/frontend/mosh-client.cc b/src/frontend/mosh-client.cc index 9cd2613..2c1709c 100644 --- a/src/frontend/mosh-client.cc +++ b/src/frontend/mosh-client.cc @@ -24,6 +24,7 @@ #include "stmclient.h" #include "crypto.h" +#include "locale_utils.h" /* these need to be included last because of conflicting defines */ #include @@ -106,10 +107,7 @@ int main( int argc, char *argv[] ) } /* Adopt native locale */ - if ( NULL == setlocale( LC_ALL, "" ) ) { - perror( "setlocale" ); - exit( 1 ); - } + set_native_locale(); try { STMClient client( ip, port, key, predict_mode ); diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index a61f196..46379ca 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -49,6 +48,7 @@ extern "C" { #include "swrite.h" #include "user.h" #include "fatal_assert.h" +#include "locale_utils.h" #if HAVE_PTY_H #include @@ -196,16 +196,8 @@ int main( int argc, char *argv[] ) } /* Adopt implementation locale */ - if ( NULL == setlocale( LC_ALL, "" ) ) { - perror( "setlocale" ); - exit( 1 ); - } - - /* Verify locale calls for UTF-8 */ - if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { - fprintf( stderr, "mosh requires a UTF-8 locale.\n" ); - exit( 1 ); - } + set_native_locale(); + assert_utf8_locale(); try { return run_server( desired_ip, desired_port, command, colors ); diff --git a/src/frontend/stmclient.cc b/src/frontend/stmclient.cc index 6f3e452..b0e5ca6 100644 --- a/src/frontend/stmclient.cc +++ b/src/frontend/stmclient.cc @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -47,16 +46,13 @@ extern "C" { #include "completeterminal.h" #include "user.h" #include "fatal_assert.h" +#include "locale_utils.h" #include "networktransport.cc" void STMClient::init( void ) { - /* Verify locale calls for UTF-8 */ - if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { - fprintf( stderr, "mosh requires a UTF-8 locale.\n" ); - exit( 1 ); - } + assert_utf8_locale(); /* Verify terminal configuration */ if ( tcgetattr( STDIN_FILENO, &saved_termios ) < 0 ) { diff --git a/src/terminal/parser.cc b/src/terminal/parser.cc index d2c5594..db0b0b7 100644 --- a/src/terminal/parser.cc +++ b/src/terminal/parser.cc @@ -18,8 +18,8 @@ #include #include -#include #include +#include #include "parser.h" diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 828b181..0b4b6db 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -2,4 +2,4 @@ AM_CXXFLAGS = $(WARNING_CXXFLAGS) $(PICKY_CXXFLAGS) -fno-default-inline -pipe noinst_LIBRARIES = libmoshutil.a -libmoshutil_a_SOURCES = swrite.cc swrite.h dos_assert.h fatal_assert.h +libmoshutil_a_SOURCES = locale_utils.cc locale_utils.h swrite.cc swrite.h dos_assert.h fatal_assert.h diff --git a/src/util/locale_utils.cc b/src/util/locale_utils.cc new file mode 100644 index 0000000..5c758dc --- /dev/null +++ b/src/util/locale_utils.cc @@ -0,0 +1,46 @@ +/* + Mosh: the mobile shell + Copyright 2012 Keith Winstein + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + 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, see . +*/ + +#include "config.h" + +#include +#include +#include +#include + +#if HAVE_LANGINFO_H +#include +#endif + +#include "locale_utils.h" + +void assert_utf8_locale( void ) { + /* Verify locale calls for UTF-8 */ + if ( strcmp( nl_langinfo( CODESET ), "UTF-8" ) != 0 ) { + fprintf( stderr, "mosh requires a UTF-8 locale.\n" ); + exit( 1 ); + } +} + +void set_native_locale( void ) { + /* Adopt native locale */ + if ( NULL == setlocale( LC_ALL, "" ) ) { + perror( "setlocale" ); + exit( 1 ); + } +} diff --git a/src/util/locale_utils.h b/src/util/locale_utils.h new file mode 100644 index 0000000..d8ea64c --- /dev/null +++ b/src/util/locale_utils.h @@ -0,0 +1,25 @@ +/* + Mosh: the mobile shell + Copyright 2012 Keith Winstein + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + 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, see . +*/ + +#ifndef LOCALE_UTILS_HPP +#define LOCALE_UTILS_HPP + +void assert_utf8_locale( void ); +void set_native_locale( void ); + +#endif