diff --git a/ChangeLog b/ChangeLog index cafb6494e..0b19f15f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2013-10-13 Vladimir Serbinenko + + Move set_program_name and init_nls to host_init. On windows + fix in this fuction console and argument charset as well. + 2013-10-12 Andrey Borzenkov Fix inconsistent use of GRUB_CRYPTODISK_ENABLE and diff --git a/Makefile.util.def b/Makefile.util.def index a42562627..614644542 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -162,6 +162,7 @@ program = { common = util/grub-mkimage.c; common = util/resolve.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; common = grub-core/kern/arm/dl_helper.c; @@ -182,6 +183,7 @@ program = { common = util/grub-mkrelpath.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -196,6 +198,7 @@ program = { common = util/grub-script-check.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -209,6 +212,7 @@ program = { mansection = 1; common = util/grub-editenv.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -224,6 +228,7 @@ program = { common = util/grub-mkpasswd-pbkdf2.c; common = grub-core/kern/emu/argp_common.c; common = grub-core/osdep/random.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -246,6 +251,7 @@ program = { common = util/grub-fstest.c; common = grub-core/kern/emu/hostfs.c; common = grub-core/disk/host.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -274,18 +280,18 @@ program = { name = grub-mkfont; mansection = 1; common = util/grub-mkfont.c; - common = grub-core/unidata.c; common = grub-core/kern/emu/argp_common.c; - common = grub-core/kern/emu/misc.c; - common = util/misc.c; - common = grub-core/gnulib/progname.c; + common = grub-core/osdep/init.c; cflags = '$(freetype_cflags)'; cppflags = '-DGRUB_MKFONT=1'; + ldadd = libgrubmods.a; + ldadd = libgrubgcry.a; + ldadd = libgrubkern.a; ldadd = grub-core/gnulib/libgnu.a; ldadd = '$(freetype_libs)'; - ldadd = '$(LIBINTL)'; + ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; condition = COND_GRUB_MKFONT; }; @@ -296,6 +302,7 @@ program = { common = util/grub-probe.c; common = grub-core/osdep/ofpath.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -311,6 +318,7 @@ program = { common = util/grub-setup.c; common = grub-core/kern/emu/argp_common.c; common = grub-core/lib/reed_solomon.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubkern.a; @@ -328,6 +336,7 @@ program = { common = grub-core/kern/emu/argp_common.c; common = grub-core/lib/reed_solomon.c; common = grub-core/osdep/ofpath.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubkern.a; @@ -343,6 +352,7 @@ program = { mansection = 8; common = util/ieee1275/grub-ofpathname.c; common = grub-core/osdep/ofpath.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -357,6 +367,7 @@ program = { common = util/grub-mklayout.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -904,6 +915,7 @@ program = { common = util/grub-menulst2cfg.c; common = grub-core/lib/legacy_parse.c; common = grub-core/lib/i386/pc/vesa_modes_table.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -918,6 +930,7 @@ program = { common = util/grub-glue-efi.c; common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; @@ -934,6 +947,7 @@ program = { common = grub-core/kern/emu/argp_common.c; common = grub-core/kern/emu/hostfs.c; common = grub-core/disk/host.c; + common = grub-core/osdep/init.c; ldadd = libgrubmods.a; ldadd = libgrubgcry.a; diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c index b8857fd6b..49e17f15d 100644 --- a/grub-core/kern/emu/main.c +++ b/grub-core/kern/emu/main.c @@ -180,7 +180,7 @@ main (int argc, char *argv[]) }; volatile int hold = 0; - set_program_name (argv[0]); + grub_util_host_init (&argc, &argv); dir = xstrdup (DEFAULT_DIRECTORY); diff --git a/grub-core/osdep/basic/init.c b/grub-core/osdep/basic/init.c new file mode 100644 index 000000000..d01725fc5 --- /dev/null +++ b/grub-core/osdep/basic/init.c @@ -0,0 +1,33 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include + +#include + +#include "progname.h" + +void +grub_util_host_init (int *argc __attribute__ ((unused)), + char ***argv) +{ + set_program_name ((*argv)[0]); + + grub_util_init_nls (); +} diff --git a/grub-core/osdep/init.c b/grub-core/osdep/init.c new file mode 100644 index 000000000..207f67085 --- /dev/null +++ b/grub-core/osdep/init.c @@ -0,0 +1,5 @@ +#if defined (__MINGW32__) +#include "windows/init.c" +#else +#include "basic/init.c" +#endif diff --git a/grub-core/osdep/windows/getroot.c b/grub-core/osdep/windows/getroot.c index 56756555c..d86d0774d 100644 --- a/grub-core/osdep/windows/getroot.c +++ b/grub-core/osdep/windows/getroot.c @@ -49,24 +49,6 @@ #include #include -char * -grub_util_tchar_to_utf8 (LPCTSTR in) -{ -#if SIZEOF_TCHAR == 1 - return xstrdup (path); -#elif SIZEOF_TCHAR == 2 - size_t ssz; - for (ssz = 0; in[ssz]; ssz++); - - size_t tsz = GRUB_MAX_UTF8_PER_UTF16 * ssz + 1; - grub_uint8_t *ret = xmalloc (tsz); - *grub_utf16_to_utf8 (ret, in, ssz) = '\0'; - return (char *) ret; -#else -#error "Unsupported TCHAR size" -#endif -} - #if SIZEOF_TCHAR == 1 #define tcsnicmp strncasecmp #define tclen strlen diff --git a/grub-core/osdep/windows/hostdisk.c b/grub-core/osdep/windows/hostdisk.c index 5f0fc5425..42b58fbab 100644 --- a/grub-core/osdep/windows/hostdisk.c +++ b/grub-core/osdep/windows/hostdisk.c @@ -48,6 +48,51 @@ #include #include +#if SIZEOF_TCHAR == 1 + +LPTSTR +grub_util_utf8_to_tchar (const char *in) +{ + return xstrdup (in); +} + +char * +grub_util_tchar_to_utf8 (LPCTSTR in) +{ + return xstrdup (in); +} + +#elif SIZEOF_TCHAR == 2 + +LPTSTR +grub_util_utf8_to_tchar (const char *in) +{ + LPTSTR ret; + size_t ssz = strlen (in); + size_t tsz = 2 * (GRUB_MAX_UTF16_PER_UTF8 * ssz + 1); + ret = xmalloc (tsz); + tsz = grub_utf8_to_utf16 (ret, tsz, + (const grub_uint8_t *) in, ssz, NULL); + ret[tsz] = 0; + return ret; +} + +char * +grub_util_tchar_to_utf8 (LPCTSTR in) +{ + size_t ssz; + for (ssz = 0; in[ssz]; ssz++); + + size_t tsz = GRUB_MAX_UTF8_PER_UTF16 * ssz + 1; + grub_uint8_t *ret = xmalloc (tsz); + *grub_utf16_to_utf8 (ret, in, ssz) = '\0'; + return (char *) ret; +} + +#else +#error "Unsupported TCHAR size" +#endif + #ifdef __CYGWIN__ LPTSTR grub_util_get_windows_path (const char *path) @@ -64,28 +109,20 @@ grub_util_get_windows_path (const char *path) LPTSTR grub_util_get_windows_path (const char *path) { - char *fpa; - const char *fp; - LPTSTR ret; + LPTSTR fpa; + LPTSTR tpath; - fp = fpa = xmalloc (PATH_MAX); - if (!_fullpath (fpa, path, PATH_MAX)) - fp = path; -#if SIZEOF_TCHAR == 1 - ret = xstrdup (fp); -#elif SIZEOF_TCHAR == 2 - size_t ssz = strlen (fp); - size_t tsz = 2 * (GRUB_MAX_UTF16_PER_UTF8 * ssz + 1); - ret = xmalloc (tsz); - tsz = grub_utf8_to_utf16 (ret, tsz, (const grub_uint8_t *) fp, ssz, NULL); - ret[tsz] = 0; -#else -#error SIZEOF_TCHAR -#error "Unsupported TCHAR size" -#endif + tpath = grub_util_utf8_to_tchar (path); - free (fpa); - return ret; + fpa = xmalloc (PATH_MAX * sizeof (fpa[0])); + if (!_wfullpath (fpa, tpath, PATH_MAX)) + { + free (fpa); + return tpath; + } + + free (tpath); + return fpa; } #endif diff --git a/grub-core/osdep/windows/init.c b/grub-core/osdep/windows/init.c new file mode 100644 index 000000000..e6ad301b8 --- /dev/null +++ b/grub-core/osdep/windows/init.c @@ -0,0 +1,143 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013 Free Software Foundation, Inc. + * + * GRUB 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. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "progname.h" + +struct grub_windows_console_font_infoex { + ULONG cbSize; + DWORD nFont; + COORD dwFontSize; + UINT FontFamily; + UINT FontWeight; + WCHAR FaceName[LF_FACESIZE]; +}; + +static int +check_is_raster (HMODULE kernel32, HANDLE hnd) +{ + CONSOLE_FONT_INFO console_font_info; + BOOL (WINAPI * func_GetCurrentConsoleFont) (HANDLE, BOOL, + PCONSOLE_FONT_INFO); + + func_GetCurrentConsoleFont = (void *) + GetProcAddress (kernel32, "GetCurrentConsoleFont"); + + if (!func_GetCurrentConsoleFont) + return 1; + + if (!func_GetCurrentConsoleFont (hnd, FALSE, &console_font_info)) + return 1; + return console_font_info.nFont < 12; +} + +static void +set_console_unicode_font (void) +{ + BOOL (WINAPI * func_SetCurrentConsoleFontEx) (HANDLE, BOOL, + struct grub_windows_console_font_infoex *); + BOOL (WINAPI * func_SetConsoleFont)(HANDLE, DWORD); + HMODULE kernel32; + HANDLE out_handle = GetStdHandle (STD_OUTPUT_HANDLE); + HANDLE err_handle = GetStdHandle (STD_ERROR_HANDLE); + int out_raster, err_raster; + + kernel32 = GetModuleHandle(TEXT("kernel32.dll")); + if (!kernel32) + return; + + out_raster = check_is_raster (kernel32, out_handle); + err_raster = check_is_raster (kernel32, err_handle); + + if (!out_raster && !err_raster) + return; + + func_SetCurrentConsoleFontEx = (void *) GetProcAddress (kernel32, "SetCurrentConsoleFontEx"); + + /* Newer windows versions. */ + if (func_SetCurrentConsoleFontEx) + { + struct grub_windows_console_font_infoex new_console_font_info; + new_console_font_info.cbSize = sizeof (new_console_font_info); + new_console_font_info.nFont = 12; + new_console_font_info.dwFontSize.X = 7; + new_console_font_info.dwFontSize.Y = 12; + new_console_font_info.FontFamily = FF_DONTCARE; + new_console_font_info.FontWeight = 400; + memcpy (new_console_font_info.FaceName, TEXT("Lucida Console"), + sizeof (TEXT("Lucida Console"))); + if (out_raster) + func_SetCurrentConsoleFontEx (out_handle, FALSE, + &new_console_font_info); + if (err_raster) + func_SetCurrentConsoleFontEx (err_handle, FALSE, + &new_console_font_info); + return; + } + + /* Fallback for older versions. */ + func_SetConsoleFont = (void *) GetProcAddress (kernel32, "SetConsoleFont"); + if (func_SetConsoleFont) + { + if (out_raster) + func_SetConsoleFont (out_handle, 12); + if (err_raster) + func_SetConsoleFont (err_handle, 12); + } +} + +void +grub_util_host_init (int *argc __attribute__ ((unused)), + char ***argv) +{ + SetConsoleOutputCP (CP_UTF8); + SetConsoleCP (CP_UTF8); + + set_console_unicode_font (); + +#if SIZEOF_TCHAR == 1 + +#elif SIZEOF_TCHAR == 2 + LPWSTR tcmdline = GetCommandLineW (); + int i; + LPWSTR *targv; + + targv = CommandLineToArgvW (tcmdline, argc); + *argv = xmalloc ((*argc + 1) * sizeof (argv[0])); + + for (i = 0; i < *argc; i++) + (*argv)[i] = grub_util_tchar_to_utf8 (targv[i]); + (*argv)[i] = NULL; +#else +#error "Unsupported TCHAR size" +#endif + + set_program_name ((*argv)[0]); + + grub_util_init_nls (); +} diff --git a/include/grub/util/misc.h b/include/grub/util/misc.h index 232b92029..6aacf237a 100644 --- a/include/grub/util/misc.h +++ b/include/grub/util/misc.h @@ -45,4 +45,6 @@ char *canonicalize_file_name (const char *path); void grub_util_init_nls (void); +void grub_util_host_init (int *argc, char ***argv); + #endif /* ! GRUB_UTIL_MISC_HEADER */ diff --git a/util/grub-editenv.c b/util/grub-editenv.c index 55206bcc6..7da3fd4a9 100644 --- a/util/grub-editenv.c +++ b/util/grub-editenv.c @@ -274,6 +274,8 @@ main (int argc, char *argv[]) char *command; int curindex, arg_count; + grub_util_host_init (&argc, &argv); + set_program_name (argv[0]); grub_util_init_nls (); diff --git a/util/grub-fstest.c b/util/grub-fstest.c index 5c13a58fc..e6fe3c84f 100644 --- a/util/grub-fstest.c +++ b/util/grub-fstest.c @@ -733,9 +733,7 @@ main (int argc, char *argv[]) const char *default_root; char *alloc_root; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); args = xmalloc (argc * sizeof (args[0])); diff --git a/util/grub-glue-efi.c b/util/grub-glue-efi.c index 47e393a1f..edc4946b8 100644 --- a/util/grub-glue-efi.c +++ b/util/grub-glue-efi.c @@ -168,7 +168,7 @@ main (int argc, char *argv[]) FILE *in32, *in64, *out; struct arguments arguments; - set_program_name (argv[0]); + grub_util_host_init (&argc, &argv); /* Check for options. */ memset (&arguments, 0, sizeof (struct arguments)); diff --git a/util/grub-menulst2cfg.c b/util/grub-menulst2cfg.c index 915135d17..ee9b78112 100644 --- a/util/grub-menulst2cfg.c +++ b/util/grub-menulst2cfg.c @@ -37,6 +37,8 @@ main (int argc, char **argv) int suffixlen = 0; const char *out_fname = 0; + grub_util_host_init (&argc, &argv); + if (argc >= 2 && argv[1][0] == '-') { fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]); diff --git a/util/grub-mkfont.c b/util/grub-mkfont.c index 76ccb26ed..6532a79b7 100644 --- a/util/grub-mkfont.c +++ b/util/grub-mkfont.c @@ -1155,11 +1155,9 @@ main (int argc, char *argv[]) struct arguments arguments; #ifndef GRUB_BUILD - set_program_name (argv[0]); + grub_util_host_init (&argc, &argv); #endif - grub_util_init_nls (); - memset (&arguments, 0, sizeof (struct arguments)); arguments.file_format = PF2; arguments.files_max = argc + 1; diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c index 2999ff2ed..b606bad76 100644 --- a/util/grub-mkimage.c +++ b/util/grub-mkimage.c @@ -2025,9 +2025,7 @@ main (int argc, char *argv[]) FILE *fp = stdout; struct arguments arguments; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); memset (&arguments, 0, sizeof (struct arguments)); arguments.comp = COMPRESSION_AUTO; diff --git a/util/grub-mklayout.c b/util/grub-mklayout.c index e53d7109a..8f8f3a8cb 100644 --- a/util/grub-mklayout.c +++ b/util/grub-mklayout.c @@ -471,7 +471,7 @@ main (int argc, char *argv[]) FILE *in, *out; struct arguments arguments; - set_program_name (argv[0]); + grub_util_host_init (&argc, &argv); /* Check for options. */ memset (&arguments, 0, sizeof (struct arguments)); diff --git a/util/grub-mkpasswd-pbkdf2.c b/util/grub-mkpasswd-pbkdf2.c index 88be4a2e8..5f30c587e 100644 --- a/util/grub-mkpasswd-pbkdf2.c +++ b/util/grub-mkpasswd-pbkdf2.c @@ -119,9 +119,7 @@ main (int argc, char *argv[]) char pass1[GRUB_AUTH_MAX_PASSLEN]; char pass2[GRUB_AUTH_MAX_PASSLEN]; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); /* Check for options. */ if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0) diff --git a/util/grub-mkrelpath.c b/util/grub-mkrelpath.c index 1e0c42f83..6e64accd0 100644 --- a/util/grub-mkrelpath.c +++ b/util/grub-mkrelpath.c @@ -82,9 +82,7 @@ main (int argc, char *argv[]) char *relpath; struct arguments arguments; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); memset (&arguments, 0, sizeof (struct arguments)); diff --git a/util/grub-mount.c b/util/grub-mount.c index 4a2333ac0..f13bc0c0c 100644 --- a/util/grub-mount.c +++ b/util/grub-mount.c @@ -541,9 +541,7 @@ main (int argc, char *argv[]) const char *default_root; char *alloc_root; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0])); fuse_args[fuse_argc] = xstrdup (argv[0]); diff --git a/util/grub-probe.c b/util/grub-probe.c index a7e437399..22d89842d 100644 --- a/util/grub-probe.c +++ b/util/grub-probe.c @@ -910,9 +910,7 @@ main (int argc, char *argv[]) char delim; struct arguments arguments; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); memset (&arguments, 0, sizeof (struct arguments)); arguments.device_max = argc + 1; diff --git a/util/grub-render-label.c b/util/grub-render-label.c index 2af7eff13..c809a981d 100644 --- a/util/grub-render-label.c +++ b/util/grub-render-label.c @@ -208,9 +208,7 @@ main (int argc, char *argv[]) ieee1275_palette[cptr].b = 0; ieee1275_palette[cptr].a = 0xff; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); /* Check for options. */ memset (&arguments, 0, sizeof (struct arguments)); diff --git a/util/grub-script-check.c b/util/grub-script-check.c index d5e878391..9372bdd0f 100644 --- a/util/grub-script-check.c +++ b/util/grub-script-check.c @@ -146,8 +146,7 @@ main (int argc, char *argv[]) int found_input = 0, found_cmd = 0; struct grub_script *script = NULL; - set_program_name (argv[0]); - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); memset (&ctx.arguments, 0, sizeof (struct arguments)); diff --git a/util/grub-setup.c b/util/grub-setup.c index 3cbe81f13..65d4fb899 100644 --- a/util/grub-setup.c +++ b/util/grub-setup.c @@ -1138,9 +1138,7 @@ main (int argc, char *argv[]) char *dest_dev = NULL; struct arguments arguments; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); /* Default option values. */ memset (&arguments, 0, sizeof (struct arguments)); diff --git a/util/ieee1275/grub-ofpathname.c b/util/ieee1275/grub-ofpathname.c index 20a2ae120..8e5d766cb 100644 --- a/util/ieee1275/grub-ofpathname.c +++ b/util/ieee1275/grub-ofpathname.c @@ -32,9 +32,7 @@ int main(int argc, char **argv) { char *of_path; - set_program_name (argv[0]); - - grub_util_init_nls (); + grub_util_host_init (&argc, &argv); if (argc != 2 || strcmp (argv[1], "--help") == 0) {