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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 /* A windows stub program to launch a java program with suitable parameters
20 * Given that the name of this exe is altusmetrum-foo.exe living in directory bar, and
21 * that it was run with 'args' extra command line parameters, run:
23 * javaw.exe -Djava.library.path="bar" -jar "bar/foo-fat.jar" args
35 /* Concatenate a list of strings together
38 wcsbuild(LPTSTR first, ...)
46 va_start(args, first);
47 while ((arg = va_arg(args, LPTSTR)) != NULL) {
48 len = wcslen(buf) + wcslen(arg) + 1;
49 buf = realloc(buf, len * sizeof (wchar_t));
56 /* Quote a single string, taking care to escape embedded quote and
64 int out_len = 3; /* quotes and terminating null */
66 /* Find quote and backslashes */
67 for (in = arg; *in; in++) {
79 result = malloc ((out_len + 1) * sizeof (wchar_t));
82 for (in = arg; *in; in++) {
96 /* Construct a single string from a list of arguments
99 quote_args(LPTSTR *argv, int argc)
101 LPTSTR result = NULL, arg;
104 result = malloc(1 * sizeof (wchar_t));
106 for (i = 0; i < argc; i++) {
107 arg = quote_arg(argv[i]);
108 result = realloc(result, (wcslen(result) + 1 + wcslen(arg) + 1) * sizeof (wchar_t));
109 wcscat(result, L" ");
116 /* Return the directory portion of the provided file
121 DWORD len = GetFullPathName(file, 0, NULL, NULL);
122 LPTSTR full = malloc (len * sizeof (wchar_t));
123 GetFullPathName(file, len, full, NULL);
124 PathRemoveFileSpec(full);
128 /* Convert a .exe name into a -fat.jar name, starting
129 * by computing the complete path name of the source filename
132 make_jar(LPTSTR file)
134 DWORD len = GetFullPathName(file, 0, NULL, NULL);
135 LPTSTR full = malloc (len * sizeof (wchar_t));
139 GetFullPathName(file, len, full, &base_part);
140 static const wchar_t head[] = L"altusmetrum-";
142 if (wcsncmp(base_part, head, wcslen(head)) == 0)
143 base_part += wcslen(head);
144 dot = wcsrchr(base_part, '.');
147 jar = wcsdup(base_part);
148 PathRemoveFileSpec(full);
149 return wcsbuild(full, L"\\", jar, L"-fat.jar", NULL);
152 /* Build the complete command line from the pieces
155 make_cmd(LPTSTR dir, LPTSTR jar, LPTSTR quote_args)
157 LPTSTR quote_dir = quote_arg(dir);
158 LPTSTR quote_jar = quote_arg(jar);
161 cmd = wcsbuild(L"javaw.exe -Djava.library.path=", quote_dir, L" -jar ", quote_jar, quote_args, NULL);
169 WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line_a, int cmd_show)
171 STARTUPINFO startup_info;
172 PROCESS_INFORMATION process_information;
174 wchar_t *command_line;
176 LPTSTR *argv = CommandLineToArgvW(GetCommandLine(), &argc);
179 LPTSTR args = quote_args(argv + 1, argc - 1);
181 my_dir = get_dir(argv[0]);
182 my_jar = make_jar(argv[0]);
183 command_line = make_cmd(my_dir, my_jar, args);
184 memset(&startup_info, '\0', sizeof startup_info);
185 startup_info.cb = sizeof startup_info;
186 result = CreateProcess(NULL,
195 &process_information);
197 CloseHandle(process_information.hProcess);
198 CloseHandle(process_information.hThread);