1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006 Carnegie Mellon University |
3 |
|
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, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
#include <utilities/ascConfig.h> |
20 |
|
21 |
ASC_IMPORT(int) AscDriver(int, CONST char **argv); |
22 |
|
23 |
#ifdef __WIN32__ |
24 |
static void setargv(int*, char ***); |
25 |
#endif /* __WIN32__ */ |
26 |
|
27 |
/* |
28 |
* main or WinMain |
29 |
* |
30 |
* The main entry point for a Unix or Windows application. |
31 |
* |
32 |
* Each just calls AscDriver(). |
33 |
* These are based on functions from the Tk 8.0 distribution. |
34 |
* See unix/tkAppInit.c and win/winMain.c in their sources. |
35 |
*/ |
36 |
#ifndef __WIN32__ |
37 |
|
38 |
int main(int argc, CONST char *argv[]) |
39 |
{ |
40 |
AscDriver(argc, argv); |
41 |
return 0; |
42 |
} |
43 |
|
44 |
#else /* __WIN32__ */ |
45 |
|
46 |
#include <windows.h> |
47 |
#include <locale.h> |
48 |
|
49 |
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
50 |
LPSTR lpszCmdLine, int nCmdShow) |
51 |
{ |
52 |
int argc; |
53 |
char **argv; |
54 |
char *p; |
55 |
char buffer[MAX_PATH]; |
56 |
|
57 |
UNUSED_PARAMETER(hInstance); |
58 |
UNUSED_PARAMETER(hPrevInstance); |
59 |
UNUSED_PARAMETER(lpszCmdLine); |
60 |
UNUSED_PARAMETER(nCmdShow); |
61 |
|
62 |
/* |
63 |
* Set up the default locale to be standard "C" locale so parsing |
64 |
* is performed correctly. |
65 |
*/ |
66 |
setlocale(LC_ALL, "C"); |
67 |
|
68 |
/* |
69 |
* Increase the application queue size from default value of 8. |
70 |
* At the default value, cross application SendMessage of WM_KILLFOCUS |
71 |
* will fail because the handler will not be able to do a PostMessage! |
72 |
* This is only needed for Windows 3.x, since NT dynamically expands |
73 |
* the queue. |
74 |
*/ |
75 |
/* |
76 |
RIP WIndows 3.1 |
77 |
SetMessageQueue(64); |
78 |
*/ |
79 |
|
80 |
/* |
81 |
* Create the console channels and install them as the standard |
82 |
* channels. All I/O will be discarded until TkConsoleInit is |
83 |
* called to attach the console to a text widget. |
84 |
*/ |
85 |
|
86 |
/* |
87 |
* Windows expects us to parse our arguments ourselves. |
88 |
*/ |
89 |
setargv(&argc, &argv); |
90 |
|
91 |
/* |
92 |
* Replace argv[0] with full pathname of executable, and forward |
93 |
* slashes substituted for backslashes. |
94 |
*/ |
95 |
GetModuleFileName(NULL, buffer, sizeof(buffer)); |
96 |
argv[0] = buffer; |
97 |
for (p = buffer; *p != '\0'; p++) { |
98 |
if (*p == '\\') { |
99 |
*p = '/'; |
100 |
} |
101 |
} |
102 |
|
103 |
AscDriver(argc, argv); |
104 |
return 1; |
105 |
} |
106 |
#endif /* __WIN32__ */ |
107 |
|
108 |
#ifdef __WIN32__ |
109 |
/* |
110 |
*------------------------------------------------------------------------- |
111 |
* |
112 |
* setargv -- |
113 |
* |
114 |
* Parse the Windows command line string into argc/argv. Done here |
115 |
* because we don't trust the builtin argument parser in crt0. |
116 |
* Windows applications are responsible for breaking their command |
117 |
* line into arguments. |
118 |
* |
119 |
* 2N backslashes + quote -> N backslashes + begin quoted string |
120 |
* 2N + 1 backslashes + quote -> literal |
121 |
* N backslashes + non-quote -> literal |
122 |
* quote + quote in a quoted string -> single quote |
123 |
* quote + quote not in quoted string -> empty string |
124 |
* quote -> begin quoted string |
125 |
* |
126 |
* Results: |
127 |
* Fills argcPtr with the number of arguments and argvPtr with the |
128 |
* array of arguments. |
129 |
* |
130 |
* Side effects: |
131 |
* Memory allocated. |
132 |
* |
133 |
* Parameters: |
134 |
* argcptr Filled with number of argument strings. |
135 |
* argvptr Filled with argument strings (malloc'd). |
136 |
* |
137 |
* This function is from the Tk 8.0 distribution. See win/winMain.c in |
138 |
* their sources. |
139 |
* |
140 |
*-------------------------------------------------------------------------- |
141 |
*/ |
142 |
static void |
143 |
setargv(int *argcPtr, char ***argvPtr) |
144 |
{ |
145 |
char *cmdLine, *p, *arg, *argSpace; |
146 |
char **argv; |
147 |
int argc, size, inquote, copy, slashes; |
148 |
|
149 |
cmdLine = GetCommandLine(); |
150 |
|
151 |
/* |
152 |
* Precompute an overly pessimistic guess at the number of arguments |
153 |
* in the command line by counting non-space spans. |
154 |
*/ |
155 |
|
156 |
size = 2; |
157 |
for (p = cmdLine; *p != '\0'; p++) { |
158 |
if (isspace(*p)) { |
159 |
size++; |
160 |
while (isspace(*p)) { |
161 |
p++; |
162 |
} |
163 |
if (*p == '\0') { |
164 |
break; |
165 |
} |
166 |
} |
167 |
} |
168 |
argSpace = (char *) malloc((unsigned) (size * sizeof(char *) + strlen(cmdLine) + 1)); |
169 |
argv = (char **) argSpace; |
170 |
argSpace += size * sizeof(char *); |
171 |
size--; |
172 |
|
173 |
p = cmdLine; |
174 |
for (argc = 0; argc < size; argc++) { |
175 |
argv[argc] = arg = argSpace; |
176 |
while (isspace(*p)) { |
177 |
p++; |
178 |
} |
179 |
if (*p == '\0') { |
180 |
break; |
181 |
} |
182 |
|
183 |
inquote = 0; |
184 |
slashes = 0; |
185 |
while (1) { |
186 |
copy = 1; |
187 |
while (*p == '\\') { |
188 |
slashes++; |
189 |
p++; |
190 |
} |
191 |
if (*p == '"') { |
192 |
if ((slashes & 1) == 0) { |
193 |
copy = 0; |
194 |
if ((inquote) && (p[1] == '"')) { |
195 |
p++; |
196 |
copy = 1; |
197 |
} else { |
198 |
inquote = !inquote; |
199 |
} |
200 |
} |
201 |
slashes >>= 1; |
202 |
} |
203 |
|
204 |
while (slashes) { |
205 |
*arg = '\\'; |
206 |
arg++; |
207 |
slashes--; |
208 |
} |
209 |
|
210 |
if ((*p == '\0') || (!inquote && isspace(*p))) { |
211 |
break; |
212 |
} |
213 |
if (copy != 0) { |
214 |
*arg = *p; |
215 |
arg++; |
216 |
} |
217 |
p++; |
218 |
} |
219 |
*arg = '\0'; |
220 |
argSpace = arg + 1; |
221 |
} |
222 |
argv[argc] = NULL; |
223 |
|
224 |
*argcPtr = argc; |
225 |
*argvPtr = argv; |
226 |
} |
227 |
|
228 |
#endif /* __WIN32__ */ |