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