1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Copyright © 2015 Keith Packard <keithp@keithp.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
/* A windows stub program to launch a java program with suitable parameters
*
* Given that the name of this exe is altusmetrum-foo.exe living in directory bar, and
* that it was run with 'args' extra command line parameters, run:
*
* javaw.exe -Djava.library.path="bar" -jar "bar/foo-fat.jar" args
*/
#define _UNICODE
#define UNICODE
#include <stdlib.h>
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <stdarg.h>
#include <shlwapi.h>
/* Concatenate a list of strings together
*/
static LPTSTR
wcsbuild(LPTSTR first, ...)
{
va_list args;
int len;
LPTSTR buf;
LPTSTR arg;
buf = wcsdup(first);
va_start(args, first);
while ((arg = va_arg(args, LPTSTR)) != NULL) {
len = wcslen(buf) + wcslen(arg) + 1;
buf = realloc(buf, len * sizeof (wchar_t));
wcscat(buf, arg);
}
va_end(args);
return buf;
}
/* Quote a single string, taking care to escape embedded quote and
* backslashes within
*/
static LPTSTR
quote_arg(LPTSTR arg)
{
LPTSTR result;
LPTSTR in, out;
int out_len = 3; /* quotes and terminating null */
/* Find quote and backslashes */
for (in = arg; *in; in++) {
switch (*in) {
case '"':
case '\\':
out_len += 2;
break;
default:
out_len++;
break;
}
}
result = malloc ((out_len + 1) * sizeof (wchar_t));
out = result;
*out++ = '"';
for (in = arg; *in; in++) {
switch (*in) {
case '"':
case '\\':
*out++ = '\\';
break;
}
*out++ = *in;
}
*out++ = '"';
*out++ = '\0';
return result;
}
/* Construct a single string from a list of arguments
*/
static LPTSTR
quote_args(LPTSTR *argv, int argc)
{
LPTSTR result = NULL, arg;
int i;
result = malloc(1 * sizeof (wchar_t));
result[0] = '\0';
for (i = 0; i < argc; i++) {
arg = quote_arg(argv[i]);
result = realloc(result, (wcslen(result) + 1 + wcslen(arg) + 1) * sizeof (wchar_t));
wcscat(result, L" ");
wcscat(result, arg);
free(arg);
}
return result;
}
/* Return the directory portion of the provided file
*/
static LPTSTR
get_dir(LPTSTR file)
{
DWORD len = GetFullPathName(file, 0, NULL, NULL);
LPTSTR full = malloc (len * sizeof (wchar_t));
GetFullPathName(file, len, full, NULL);
PathRemoveFileSpec(full);
return full;
}
/* Convert a .exe name into a -fat.jar name, starting
* by computing the complete path name of the source filename
*/
static LPTSTR
make_jar(LPTSTR file)
{
DWORD len = GetFullPathName(file, 0, NULL, NULL);
LPTSTR full = malloc (len * sizeof (wchar_t));
LPTSTR base_part;
LPTSTR jar;
LPTSTR dot;
GetFullPathName(file, len, full, &base_part);
static const wchar_t head[] = L"altusmetrum-";
if (wcsncmp(base_part, head, wcslen(head)) == 0)
base_part += wcslen(head);
dot = wcsrchr(base_part, '.');
if (dot)
*dot = '\0';
jar = wcsdup(base_part);
PathRemoveFileSpec(full);
return wcsbuild(full, L"\\", jar, L"-fat.jar", NULL);
}
/* Build the complete command line from the pieces
*/
static LPTSTR
make_cmd(LPTSTR dir, LPTSTR jar, LPTSTR quote_args)
{
LPTSTR quote_dir = quote_arg(dir);
LPTSTR quote_jar = quote_arg(jar);
LPTSTR cmd;
cmd = wcsbuild(L"javaw.exe -Djava.library.path=", quote_dir, L" -jar ", quote_jar, quote_args, NULL);
free(quote_jar);
free(jar);
free(quote_dir);
return cmd;
}
int WINAPI
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmd_line_a, int cmd_show)
{
STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
BOOL result;
wchar_t *command_line;
int argc;
LPTSTR *argv = CommandLineToArgvW(GetCommandLine(), &argc);
LPTSTR my_dir;
LPTSTR my_jar;
LPTSTR args = quote_args(argv + 1, argc - 1);
my_dir = get_dir(argv[0]);
my_jar = make_jar(argv[0]);
command_line = make_cmd(my_dir, my_jar, args);
memset(&startup_info, '\0', sizeof startup_info);
startup_info.cb = sizeof startup_info;
result = CreateProcess(NULL,
command_line,
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
NULL,
&startup_info,
&process_information);
if (result) {
CloseHandle(process_information.hProcess);
CloseHandle(process_information.hThread);
}
exit(0);
}
|