2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 /* A windows stub program to launch a java program with suitable parameters
21 * Given that the name of this exe is altusmetrum-foo.exe living in directory bar, and
22 * that it was run with 'args' extra command line parameters, run:
24 * javaw.exe -Djava.library.path="bar" -jar "bar/foo-fat.jar" args
36 /* Concatenate a list of strings together
39 wcsbuild(LPTSTR first, ...)
47 va_start(args, first);
48 while ((arg = va_arg(args, LPTSTR)) != NULL) {
49 len = wcslen(buf) + wcslen(arg) + 1;
50 buf = realloc(buf, len * sizeof (wchar_t));
57 /* Quote a single string, taking care to escape embedded quote and
65 int out_len = 3; /* quotes and terminating null */
67 /* Find quote and backslashes */
68 for (in = arg; *in; in++) {
80 result = malloc ((out_len + 1) * sizeof (wchar_t));
83 for (in = arg; *in; in++) {
97 /* Construct a single string from a list of arguments
100 quote_args(LPTSTR *argv, int argc)
102 LPTSTR result = NULL, arg;
105 result = malloc(1 * sizeof (wchar_t));
107 for (i = 0; i < argc; i++) {
108 arg = quote_arg(argv[i]);
109 result = realloc(result, (wcslen(result) + 1 + wcslen(arg) + 1) * sizeof (wchar_t));
110 wcscat(result, L" ");
117 /* Return the directory portion of the provided file
122 DWORD len = GetFullPathName(file, 0, NULL, NULL);
123 LPTSTR full = malloc (len * sizeof (wchar_t));
124 GetFullPathName(file, len, full, NULL);
125 PathRemoveFileSpec(full);
129 /* Convert a .exe name into a -fat.jar name, starting
130 * by computing the complete path name of the source filename
133 make_jar(LPTSTR file)
135 DWORD len = GetFullPathName(file, 0, NULL, NULL);
136 LPTSTR full = malloc (len * sizeof (wchar_t));
140 GetFullPathName(file, len, full, &base_part);
141 static const wchar_t head[] = L"altusmetrum-";
143 if (wcsncmp(base_part, head, wcslen(head)) == 0)
144 base_part += wcslen(head);
145 dot = wcsrchr(base_part, '.');
148 jar = wcsdup(base_part);
149 PathRemoveFileSpec(full);
150 return wcsbuild(full, L"\\", jar, L"-fat.jar", NULL);
153 /* Build the complete command line from the pieces
156 make_cmd(LPTSTR dir, LPTSTR jar, LPTSTR quote_args)
158 LPTSTR quote_dir = quote_arg(dir);
159 LPTSTR quote_jar = quote_arg(jar);
162 cmd = wcsbuild(L"javaw.exe -Djava.library.path=", quote_dir, L" -jar ", quote_jar, quote_args, NULL);
170 WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line_a, int cmd_show)
172 STARTUPINFO startup_info;
173 PROCESS_INFORMATION process_information;
175 wchar_t *command_line;
177 LPTSTR *argv = CommandLineToArgvW(GetCommandLine(), &argc);
180 LPTSTR args = quote_args(argv + 1, argc - 1);
182 my_dir = get_dir(argv[0]);
183 my_jar = make_jar(argv[0]);
184 command_line = make_cmd(my_dir, my_jar, args);
185 memset(&startup_info, '\0', sizeof startup_info);
186 startup_info.cb = sizeof startup_info;
187 result = CreateProcess(NULL,
196 &process_information);
198 CloseHandle(process_information.hProcess);
199 CloseHandle(process_information.hThread);