Skip to content

Commit 4d59493

Browse files
committed
[windows] msys (or something) is mucking with the command-line, need to work around
1 parent e4f891f commit 4d59493

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

src/base/lnav.console.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ namespace lnav::console {
4040

4141
bool only_process_attached_to_win32_console();
4242

43+
void get_command_line_args(int* argc, char*** argv);
44+
4345
void println(FILE* file, const attr_line_t& al);
4446

4547
struct snippet {

src/base/lnav.console.win.cc

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,57 @@
3232
#if __has_include("windows.h")
3333
#define WIN32_LEAN_AND_MEAN
3434
#include <windows.h>
35+
#include <shellapi.h>
3536
#define HAVE_WINDOWS_H
3637
#endif
3738

3839
namespace lnav::console {
39-
40-
bool only_process_attached_to_win32_console() {
40+
bool
41+
only_process_attached_to_win32_console()
42+
{
4143
#if defined(HAVE_WINDOWS_H)
4244
DWORD procIDs[2];
43-
DWORD count = GetConsoleProcessList((LPDWORD)procIDs, 2);
45+
DWORD count = GetConsoleProcessList((LPDWORD) procIDs, 2);
4446
return count == 1;
4547
#else
4648
return false;
4749
#endif
4850
}
4951

52+
void
53+
get_command_line_args(int* argc, char*** argv)
54+
{
55+
#if defined(HAVE_WINDOWS_H)
56+
// Get the command line arguments as wchar_t strings
57+
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), argc);
58+
if (!wargv) {
59+
*argc = 0;
60+
*argv = NULL;
61+
return;
62+
}
63+
64+
// Count the number of bytes necessary to store the UTF-8 versions of those strings
65+
int n = 0;
66+
for (int i = 0; i < *argc; i++)
67+
n += WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL)
68+
+ 1;
69+
70+
// Allocate the argv[] array + all the UTF-8 strings
71+
*argv = (char**) malloc((*argc + 1) * sizeof(char*) + n);
72+
if (!*argv) {
73+
*argc = 0;
74+
return;
75+
}
76+
77+
// Convert all wargv[] --> argv[]
78+
char* arg = (char*) &((*argv)[*argc + 1]);
79+
for (int i = 0; i < *argc; i++) {
80+
(*argv)[i] = arg;
81+
arg += WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, arg, n, NULL, NULL)
82+
+ 1;
83+
}
84+
(*argv)[*argc] = NULL;
85+
#endif
86+
}
87+
5088
}

src/lnav.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,6 +3017,9 @@ SELECT tbl_name FROM sqlite_master WHERE sql LIKE 'CREATE VIRTUAL TABLE%'
30173017

30183018
auto is_mmode = argc >= 2 && strcmp(argv[1], "-m") == 0;
30193019
try {
3020+
#if defined(__MSYS__)
3021+
lnav::console::get_command_line_args(&argc, &argv);
3022+
#endif
30203023
if (is_mmode) {
30213024
mmode_ops = lnav::management::describe_cli(app, argc, argv);
30223025
} else {

0 commit comments

Comments
 (0)