/[ascend]/trunk/tcltk/generic/interface/main.c
ViewVC logotype

Contents of /trunk/tcltk/generic/interface/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (show annotations) (download) (as text)
Tue May 9 01:07:41 2006 UTC (16 years, 9 months ago) by johnpye
File MIME type: text/x-csrc
File size: 5371 byte(s)
Commenting changes, GPL header.
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 SetMessageQueue(64);
76
77 /*
78 * Create the console channels and install them as the standard
79 * channels. All I/O will be discarded until TkConsoleInit is
80 * called to attach the console to a text widget.
81 */
82
83 /*
84 * Windows expects us to parse our arguments ourselves.
85 */
86 setargv(&argc, &argv);
87
88 /*
89 * Replace argv[0] with full pathname of executable, and forward
90 * slashes substituted for backslashes.
91 */
92 GetModuleFileName(NULL, buffer, sizeof(buffer));
93 argv[0] = buffer;
94 for (p = buffer; *p != '\0'; p++) {
95 if (*p == '\\') {
96 *p = '/';
97 }
98 }
99
100 AscDriver(argc, argv);
101 return 1;
102 }
103 #endif /* __WIN32__ */
104
105 #ifdef __WIN32__
106 /*
107 *-------------------------------------------------------------------------
108 *
109 * setargv --
110 *
111 * Parse the Windows command line string into argc/argv. Done here
112 * because we don't trust the builtin argument parser in crt0.
113 * Windows applications are responsible for breaking their command
114 * line into arguments.
115 *
116 * 2N backslashes + quote -> N backslashes + begin quoted string
117 * 2N + 1 backslashes + quote -> literal
118 * N backslashes + non-quote -> literal
119 * quote + quote in a quoted string -> single quote
120 * quote + quote not in quoted string -> empty string
121 * quote -> begin quoted string
122 *
123 * Results:
124 * Fills argcPtr with the number of arguments and argvPtr with the
125 * array of arguments.
126 *
127 * Side effects:
128 * Memory allocated.
129 *
130 * Parameters:
131 * argcptr Filled with number of argument strings.
132 * argvptr Filled with argument strings (malloc'd).
133 *
134 * This function is from the Tk 8.0 distribution. See win/winMain.c in
135 * their sources.
136 *
137 *--------------------------------------------------------------------------
138 */
139 static void
140 setargv(int *argcPtr, char ***argvPtr)
141 {
142 char *cmdLine, *p, *arg, *argSpace;
143 char **argv;
144 int argc, size, inquote, copy, slashes;
145
146 cmdLine = GetCommandLine();
147
148 /*
149 * Precompute an overly pessimistic guess at the number of arguments
150 * in the command line by counting non-space spans.
151 */
152
153 size = 2;
154 for (p = cmdLine; *p != '\0'; p++) {
155 if (isspace(*p)) {
156 size++;
157 while (isspace(*p)) {
158 p++;
159 }
160 if (*p == '\0') {
161 break;
162 }
163 }
164 }
165 argSpace = (char *) malloc((unsigned) (size * sizeof(char *) + strlen(cmdLine) + 1));
166 argv = (char **) argSpace;
167 argSpace += size * sizeof(char *);
168 size--;
169
170 p = cmdLine;
171 for (argc = 0; argc < size; argc++) {
172 argv[argc] = arg = argSpace;
173 while (isspace(*p)) {
174 p++;
175 }
176 if (*p == '\0') {
177 break;
178 }
179
180 inquote = 0;
181 slashes = 0;
182 while (1) {
183 copy = 1;
184 while (*p == '\\') {
185 slashes++;
186 p++;
187 }
188 if (*p == '"') {
189 if ((slashes & 1) == 0) {
190 copy = 0;
191 if ((inquote) && (p[1] == '"')) {
192 p++;
193 copy = 1;
194 } else {
195 inquote = !inquote;
196 }
197 }
198 slashes >>= 1;
199 }
200
201 while (slashes) {
202 *arg = '\\';
203 arg++;
204 slashes--;
205 }
206
207 if ((*p == '\0') || (!inquote && isspace(*p))) {
208 break;
209 }
210 if (copy != 0) {
211 *arg = *p;
212 arg++;
213 }
214 p++;
215 }
216 *arg = '\0';
217 argSpace = arg + 1;
218 }
219 argv[argc] = NULL;
220
221 *argcPtr = argc;
222 *argvPtr = argv;
223 }
224
225 #endif /* __WIN32__ */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22