OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [tags/] [start/] [insight/] [itcl/] [itk/] [win/] [winMain.c] - Blame information for rev 579

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * winMain.c --
3
 *
4
 *      Main entry point for wish and other Tk-based applications.
5
 *
6
 * Copyright (c) 1995 Sun Microsystems, Inc.
7
 *
8
 * See the file "license.terms" for information on usage and redistribution
9
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
 *
11
 * SCCS: @(#) winMain.c 1.33 96/12/17 12:56:14
12
 */
13
 
14
#include <tk.h>
15
#define WIN32_LEAN_AND_MEAN
16
#include <windows.h>
17
#undef WIN32_LEAN_AND_MEAN
18
#include <malloc.h>
19
#include <locale.h>
20
 
21
#include "itk.h"
22
 
23
/* include tclInt.h for access to namespace API */
24
#include "tclInt.h"
25
 
26
/*
27
 * The following declarations refer to internal Tk routines.  These
28
 * interfaces are available for use, but are not supported.
29
 */
30
 
31
EXTERN void             TkConsoleCreate(void);
32
EXTERN int              TkConsoleInit(Tcl_Interp *interp);
33
 
34
/*
35
 * Forward declarations for procedures defined later in this file:
36
 */
37
 
38
static void             setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));
39
static void             WishPanic _ANSI_ARGS_(TCL_VARARGS(char *,format));
40
 
41
#ifdef TK_TEST
42
EXTERN int              Tktest_Init(Tcl_Interp *interp);
43
#endif /* TK_TEST */
44
 
45
 
46
/*
47
 *----------------------------------------------------------------------
48
 *
49
 * WinMain --
50
 *
51
 *      Main entry point from Windows.
52
 *
53
 * Results:
54
 *      Returns false if initialization fails, otherwise it never
55
 *      returns.
56
 *
57
 * Side effects:
58
 *      Just about anything, since from here we call arbitrary Tcl code.
59
 *
60
 *----------------------------------------------------------------------
61
 */
62
 
63
int APIENTRY
64
WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
65
    HINSTANCE hInstance;
66
    HINSTANCE hPrevInstance;
67
    LPSTR lpszCmdLine;
68
    int nCmdShow;
69
{
70
    char **argv, *p;
71
    int argc;
72
    char buffer[MAX_PATH];
73
 
74
    Tcl_SetPanicProc(WishPanic);
75
 
76
    /*
77
     * Set up the default locale to be standard "C" locale so parsing
78
     * is performed correctly.
79
     */
80
 
81
    setlocale(LC_ALL, "C");
82
 
83
 
84
    /*
85
     * Increase the application queue size from default value of 8.
86
     * At the default value, cross application SendMessage of WM_KILLFOCUS
87
     * will fail because the handler will not be able to do a PostMessage!
88
     * This is only needed for Windows 3.x, since NT dynamically expands
89
     * the queue.
90
     */
91
    SetMessageQueue(64);
92
 
93
    /*
94
     * Create the console channels and install them as the standard
95
     * channels.  All I/O will be discarded until TkConsoleInit is
96
     * called to attach the console to a text widget.
97
     */
98
 
99
    TkConsoleCreate();
100
 
101
    setargv(&argc, &argv);
102
 
103
    /*
104
     * Replace argv[0] with full pathname of executable, and forward
105
     * slashes substituted for backslashes.
106
     */
107
 
108
    GetModuleFileName(NULL, buffer, sizeof(buffer));
109
    argv[0] = buffer;
110
    for (p = buffer; *p != '\0'; p++) {
111
        if (*p == '\\') {
112
            *p = '/';
113
        }
114
    }
115
 
116
    Tk_Main(argc, argv, Tcl_AppInit);
117
    return 1;
118
}
119
 
120
 
121
/*
122
 *----------------------------------------------------------------------
123
 *
124
 * Tcl_AppInit --
125
 *
126
 *      This procedure performs application-specific initialization.
127
 *      Most applications, especially those that incorporate additional
128
 *      packages, will have their own version of this procedure.
129
 *
130
 * Results:
131
 *      Returns a standard Tcl completion code, and leaves an error
132
 *      message in interp->result if an error occurs.
133
 *
134
 * Side effects:
135
 *      Depends on the startup script.
136
 *
137
 *----------------------------------------------------------------------
138
 */
139
 
140
int
141
Tcl_AppInit(interp)
142
    Tcl_Interp *interp;         /* Interpreter for application. */
143
{
144
    if (Tcl_Init(interp) == TCL_ERROR) {
145
        goto error;
146
    }
147
    if (Tk_Init(interp) == TCL_ERROR) {
148
        goto error;
149
    }
150
    Tcl_StaticPackage(interp, "Tk", Tk_Init, Tk_SafeInit);
151
 
152
    if (Itcl_Init(interp) == TCL_ERROR) {
153
        return TCL_ERROR;
154
    }
155
    if (Itk_Init(interp) == TCL_ERROR) {
156
        return TCL_ERROR;
157
    }
158
    Tcl_StaticPackage(interp, "Itcl", Itcl_Init, Itcl_SafeInit);
159
    Tcl_StaticPackage(interp, "Itk", Itk_Init, (Tcl_PackageInitProc *) NULL);
160
 
161
    /*
162
     *  This is itkwish, so import all [incr Tcl] commands by
163
     *  default into the global namespace.  Fix up the autoloader
164
     *  to do the same.
165
     */
166
    if (Tcl_Import(interp, Tcl_GetGlobalNamespace(interp),
167
            "::itk::*", /* allowOverwrite */ 1) != TCL_OK) {
168
        return TCL_ERROR;
169
    }
170
 
171
    if (Tcl_Import(interp, Tcl_GetGlobalNamespace(interp),
172
            "::itcl::*", /* allowOverwrite */ 1) != TCL_OK) {
173
        return TCL_ERROR;
174
    }
175
 
176
    if (Tcl_Eval(interp, "auto_mkindex_parser::slavehook { _%@namespace import -force ::itcl::* ::itk::* }") != TCL_OK) {
177
        return TCL_ERROR;
178
    }
179
 
180
    /*
181
     * Initialize the console only if we are running as an interactive
182
     * application.
183
     */
184
 
185
    if (TkConsoleInit(interp) == TCL_ERROR) {
186
        goto error;
187
    }
188
 
189
#ifdef TK_TEST
190
    if (Tktest_Init(interp) == TCL_ERROR) {
191
        goto error;
192
    }
193
    Tcl_StaticPackage(interp, "Tktest", Tktest_Init,
194
            (Tcl_PackageInitProc *) NULL);
195
#endif /* TK_TEST */
196
 
197
    Tcl_SetVar(interp, "tcl_rcFileName", "~/itkwishrc.tcl", TCL_GLOBAL_ONLY);
198
    return TCL_OK;
199
 
200
error:
201
    WishPanic(interp->result);
202
    return TCL_ERROR;
203
}
204
 
205
/*
206
 *----------------------------------------------------------------------
207
 *
208
 * WishPanic --
209
 *
210
 *      Display a message and exit.
211
 *
212
 * Results:
213
 *      None.
214
 *
215
 * Side effects:
216
 *      Exits the program.
217
 *
218
 *----------------------------------------------------------------------
219
 */
220
 
221
void
222
WishPanic TCL_VARARGS_DEF(char *,arg1)
223
{
224
    va_list argList;
225
    char buf[1024];
226
    char *format;
227
 
228
    format = TCL_VARARGS_START(char *,arg1,argList);
229
    vsprintf(buf, format, argList);
230
 
231
    MessageBeep(MB_ICONEXCLAMATION);
232
    MessageBox(NULL, buf, "Fatal Error in Wish",
233
            MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
234
#ifdef _MSC_VER
235
    DebugBreak();
236
#endif
237
    ExitProcess(1);
238
}
239
/*
240
 *-------------------------------------------------------------------------
241
 *
242
 * setargv --
243
 *
244
 *      Parse the Windows command line string into argc/argv.  Done here
245
 *      because we don't trust the builtin argument parser in crt0.
246
 *      Windows applications are responsible for breaking their command
247
 *      line into arguments.
248
 *
249
 *      2N backslashes + quote -> N backslashes + begin quoted string
250
 *      2N + 1 backslashes + quote -> literal
251
 *      N backslashes + non-quote -> literal
252
 *      quote + quote in a quoted string -> single quote
253
 *      quote + quote not in quoted string -> empty string
254
 *      quote -> begin quoted string
255
 *
256
 * Results:
257
 *      Fills argcPtr with the number of arguments and argvPtr with the
258
 *      array of arguments.
259
 *
260
 * Side effects:
261
 *      Memory allocated.
262
 *
263
 *--------------------------------------------------------------------------
264
 */
265
 
266
static void
267
setargv(argcPtr, argvPtr)
268
    int *argcPtr;               /* Filled with number of argument strings. */
269
    char ***argvPtr;            /* Filled with argument strings (malloc'd). */
270
{
271
    char *cmdLine, *p, *arg, *argSpace;
272
    char **argv;
273
    int argc, size, inquote, copy, slashes;
274
 
275
    cmdLine = GetCommandLine();
276
 
277
    /*
278
     * Precompute an overly pessimistic guess at the number of arguments
279
     * in the command line by counting non-space spans.
280
     */
281
 
282
    size = 2;
283
    for (p = cmdLine; *p != '\0'; p++) {
284
        if (isspace(*p)) {
285
            size++;
286
            while (isspace(*p)) {
287
                p++;
288
            }
289
            if (*p == '\0') {
290
                break;
291
            }
292
        }
293
    }
294
    argSpace = (char *) ckalloc((unsigned) (size * sizeof(char *)
295
            + strlen(cmdLine) + 1));
296
    argv = (char **) argSpace;
297
    argSpace += size * sizeof(char *);
298
    size--;
299
 
300
    p = cmdLine;
301
    for (argc = 0; argc < size; argc++) {
302
        argv[argc] = arg = argSpace;
303
        while (isspace(*p)) {
304
            p++;
305
        }
306
        if (*p == '\0') {
307
            break;
308
        }
309
 
310
        inquote = 0;
311
        slashes = 0;
312
        while (1) {
313
            copy = 1;
314
            while (*p == '\\') {
315
                slashes++;
316
                p++;
317
            }
318
            if (*p == '"') {
319
                if ((slashes & 1) == 0) {
320
                    copy = 0;
321
                    if ((inquote) && (p[1] == '"')) {
322
                        p++;
323
                        copy = 1;
324
                    } else {
325
                        inquote = !inquote;
326
                    }
327
                }
328
                slashes >>= 1;
329
            }
330
 
331
            while (slashes) {
332
                *arg = '\\';
333
                arg++;
334
                slashes--;
335
            }
336
 
337
            if ((*p == '\0') || (!inquote && isspace(*p))) {
338
                break;
339
            }
340
            if (copy != 0) {
341
                *arg = *p;
342
                arg++;
343
            }
344
            p++;
345
        }
346
        *arg = '\0';
347
        argSpace = arg + 1;
348
    }
349
    argv[argc] = NULL;
350
 
351
    *argcPtr = argc;
352
    *argvPtr = argv;
353
}
354
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.