Skip to content

Commit

Permalink
main.c: move win32 main() quirks to main-win32.h
Browse files Browse the repository at this point in the history
Also drop unnecessary includes.
  • Loading branch information
emanuele6 committed Dec 11, 2023
1 parent ff83706 commit 8f458fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LIBJQ_INCS = src/builtin.h src/bytecode.h src/compile.h \
src/linker.h src/locfile.h src/opcode_list.h src/parser.y \
src/util.h src/decNumber/decContext.h src/decNumber/decNumber.h \
src/decNumber/decNumberLocal.h src/jv_dtoa_tsd.h src/jv_thread.h \
src/jv_private.h src/math-macos.h
src/jv_private.h src/math-macos.h src/main-win32.h

LIBJQ_SRC = src/builtin.c src/bytecode.c src/compile.c src/execute.c \
src/jq_test.c src/jv.c src/jv_alloc.c src/jv_aux.c \
Expand Down
28 changes: 28 additions & 0 deletions src/main-win32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef MAIN_WIN32_H
#define MAIN_WIN32_H

#ifdef WIN32
#include <stringapiset.h>
#include <wchar.h>

#define DEFINE_MAIN(ARGC, ARGV) \
int umain(ARGC, ARGV); \
int wmain(int argc, wchar_t* wargv[]) { \
size_t arg_sz; \
char **argv = alloca(argc * sizeof(wchar_t*)); \
for (int i = 0; i < argc; i++) { \
argv[i] = alloca((arg_sz = WideCharToMultiByte(CP_UTF8, \
0, \
wargv[i], \
-1, 0, 0, 0, 0))); \
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], arg_sz, 0, 0); \
} \
return umain(argc, argv); \
} \
int umain(ARGC, ARGV)
#else
#define DEFINE_MAIN(ARGC, ARGV) \
int main(ARGC, ARGV)
#endif

#endif
27 changes: 2 additions & 25 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
#include <unistd.h>

#ifdef WIN32
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <processenv.h>
#include <shellapi.h>
#include <wchar.h>
#include <wtypes.h>
extern void jv_tsd_dtoa_ctx_init();
#endif

Expand All @@ -37,6 +32,7 @@ extern void jv_tsd_dtoa_ctx_init();
#include "util.h"
#include "src/version.h"
#include "src/config_opts.inc"
#include "main-win32.h"

int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]);

Expand Down Expand Up @@ -274,26 +270,7 @@ static void stderr_cb(void *data, jv input) {
jv_free(input);
}

#ifdef WIN32
int umain(int argc, char* argv[]);

int wmain(int argc, wchar_t* wargv[]) {
size_t arg_sz;
char **argv = alloca(argc * sizeof(wchar_t*));
for (int i = 0; i < argc; i++) {
argv[i] = alloca((arg_sz = WideCharToMultiByte(CP_UTF8,
0,
wargv[i],
-1, 0, 0, 0, 0)));
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], arg_sz, 0, 0);
}
return umain(argc, argv);
}

int umain(int argc, char* argv[]) {
#else /*}*/
int main(int argc, char* argv[]) {
#endif
DEFINE_MAIN(int argc, char *argv[]) {
jq_state *jq = NULL;
jq_util_input_state *input_state = NULL;
int ret = JQ_OK_NO_OUTPUT;
Expand Down

0 comments on commit 8f458fd

Please sign in to comment.