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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [itcl/] [itcl/] [win/] [tclAppInit.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 578 markom
/*
2
 * tclAppInit.c --
3
 *
4
 *      Provides a default version of the main program and Tcl_AppInit
5
 *      procedure for Tcl applications (without Tk).  Note that this
6
 *      program must be built in Win32 console mode to work properly.
7
 *
8
 * Copyright (c) 1996 by Sun Microsystems, Inc.
9
 *
10
 * See the file "license.terms" for information on usage and redistribution
11
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12
 *
13
 * SCCS: @(#) tclAppInit.c 1.12 97/04/30 11:04:50
14
 */
15
 
16
/* include tclInt.h for access to namespace API */
17
#include "tclInt.h"
18
 
19
#include "itcl.h"
20
#include <windows.h>
21
#include <locale.h>
22
 
23
#ifdef TCL_TEST
24
EXTERN int              Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
25
EXTERN int              TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));
26
#endif /* TCL_TEST */
27
 
28
static void             setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));
29
 
30
 
31
/*
32
 *----------------------------------------------------------------------
33
 *
34
 * main --
35
 *
36
 *      This is the main program for the application.
37
 *
38
 * Results:
39
 *      None: Tcl_Main never returns here, so this procedure never
40
 *      returns either.
41
 *
42
 * Side effects:
43
 *      Whatever the application does.
44
 *
45
 *----------------------------------------------------------------------
46
 */
47
 
48
int
49
main(argc, argv)
50
    int argc;                   /* Number of command-line arguments. */
51
    char **argv;                /* Values of command-line arguments. */
52
{
53
    char *p;
54
    char buffer[MAX_PATH];
55
 
56
    /*
57
     * Set up the default locale to be standard "C" locale so parsing
58
     * is performed correctly.
59
     */
60
 
61
    setlocale(LC_ALL, "C");
62
 
63
    setargv(&argc, &argv);
64
 
65
    /*
66
     * Replace argv[0] with full pathname of executable, and forward
67
     * slashes substituted for backslashes.
68
     */
69
 
70
    GetModuleFileName(NULL, buffer, sizeof(buffer));
71
    argv[0] = buffer;
72
    for (p = buffer; *p != '\0'; p++) {
73
        if (*p == '\\') {
74
            *p = '/';
75
        }
76
    }
77
 
78
    Tcl_Main(argc, argv, Tcl_AppInit);
79
    return 0;                    /* Needed only to prevent compiler warning. */
80
}
81
 
82
 
83
/*
84
 *----------------------------------------------------------------------
85
 *
86
 * Tcl_AppInit --
87
 *
88
 *      This procedure performs application-specific initialization.
89
 *      Most applications, especially those that incorporate additional
90
 *      packages, will have their own version of this procedure.
91
 *
92
 * Results:
93
 *      Returns a standard Tcl completion code, and leaves an error
94
 *      message in interp->result if an error occurs.
95
 *
96
 * Side effects:
97
 *      Depends on the startup script.
98
 *
99
 *----------------------------------------------------------------------
100
 */
101
 
102
int
103
Tcl_AppInit(interp)
104
    Tcl_Interp *interp;         /* Interpreter for application. */
105
{
106
    if (Tcl_Init(interp) == TCL_ERROR) {
107
        return TCL_ERROR;
108
    }
109
 
110
#ifdef TCL_TEST
111
    if (Tcltest_Init(interp) == TCL_ERROR) {
112
        return TCL_ERROR;
113
    }
114
    Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
115
            (Tcl_PackageInitProc *) NULL);
116
    if (TclObjTest_Init(interp) == TCL_ERROR) {
117
        return TCL_ERROR;
118
    }
119
#endif /* TCL_TEST */
120
 
121
    /*
122
     * Call the init procedures for included packages.  Each call should
123
     * look like this:
124
     *
125
     * if (Mod_Init(interp) == TCL_ERROR) {
126
     *     return TCL_ERROR;
127
     * }
128
     *
129
     * where "Mod" is the name of the module.
130
     */
131
    if (Itcl_Init(interp) == TCL_ERROR) {
132
        return TCL_ERROR;
133
    }
134
    Tcl_StaticPackage(interp, "Itcl", Itcl_Init, Itcl_SafeInit);
135
 
136
    /*
137
     *  This is itclsh, so import all [incr Tcl] commands by
138
     *  default into the global namespace.  Fix up the autoloader
139
     *  to do the same.
140
     */
141
    if (Tcl_Import(interp, Tcl_GetGlobalNamespace(interp),
142
            "::itcl::*", /* allowOverwrite */ 1) != TCL_OK) {
143
        return TCL_ERROR;
144
    }
145
 
146
    if (Tcl_Eval(interp, "auto_mkindex_parser::slavehook { _%@namespace import -force ::itcl::* }") != TCL_OK) {
147
        return TCL_ERROR;
148
    }
149
 
150
    /*
151
     * Call Tcl_CreateCommand for application-specific commands, if
152
     * they weren't already created by the init procedures called above.
153
     */
154
 
155
    /*
156
     * Specify a user-specific startup file to invoke if the application
157
     * is run interactively.  Typically the startup file is "~/.apprc"
158
     * where "app" is the name of the application.  If this line is deleted
159
     * then no user-specific startup file will be run under any conditions.
160
     */
161
 
162
    Tcl_SetVar(interp, "tcl_rcFileName", "~/itclshrc.tcl", TCL_GLOBAL_ONLY);
163
    return TCL_OK;
164
}
165
 
166
/*
167
 *-------------------------------------------------------------------------
168
 *
169
 * setargv --
170
 *
171
 *      Parse the Windows command line string into argc/argv.  Done here
172
 *      because we don't trust the builtin argument parser in crt0.
173
 *      Windows applications are responsible for breaking their command
174
 *      line into arguments.
175
 *
176
 *      2N backslashes + quote -> N backslashes + begin quoted string
177
 *      2N + 1 backslashes + quote -> literal
178
 *      N backslashes + non-quote -> literal
179
 *      quote + quote in a quoted string -> single quote
180
 *      quote + quote not in quoted string -> empty string
181
 *      quote -> begin quoted string
182
 *
183
 * Results:
184
 *      Fills argcPtr with the number of arguments and argvPtr with the
185
 *      array of arguments.
186
 *
187
 * Side effects:
188
 *      Memory allocated.
189
 *
190
 *--------------------------------------------------------------------------
191
 */
192
 
193
static void
194
setargv(argcPtr, argvPtr)
195
    int *argcPtr;               /* Filled with number of argument strings. */
196
    char ***argvPtr;            /* Filled with argument strings (malloc'd). */
197
{
198
    char *cmdLine, *p, *arg, *argSpace;
199
    char **argv;
200
    int argc, size, inquote, copy, slashes;
201
 
202
    cmdLine = GetCommandLine();
203
 
204
    /*
205
     * Precompute an overly pessimistic guess at the number of arguments
206
     * in the command line by counting non-space spans.
207
     */
208
 
209
    size = 2;
210
    for (p = cmdLine; *p != '\0'; p++) {
211
        if (isspace(*p)) {
212
            size++;
213
            while (isspace(*p)) {
214
                p++;
215
            }
216
            if (*p == '\0') {
217
                break;
218
            }
219
        }
220
    }
221
    argSpace = (char *) ckalloc((unsigned) (size * sizeof(char *)
222
            + strlen(cmdLine) + 1));
223
    argv = (char **) argSpace;
224
    argSpace += size * sizeof(char *);
225
    size--;
226
 
227
    p = cmdLine;
228
    for (argc = 0; argc < size; argc++) {
229
        argv[argc] = arg = argSpace;
230
        while (isspace(*p)) {
231
            p++;
232
        }
233
        if (*p == '\0') {
234
            break;
235
        }
236
 
237
        inquote = 0;
238
        slashes = 0;
239
        while (1) {
240
            copy = 1;
241
            while (*p == '\\') {
242
                slashes++;
243
                p++;
244
            }
245
            if (*p == '"') {
246
                if ((slashes & 1) == 0) {
247
                    copy = 0;
248
                    if ((inquote) && (p[1] == '"')) {
249
                        p++;
250
                        copy = 1;
251
                    } else {
252
                        inquote = !inquote;
253
                    }
254
                }
255
                slashes >>= 1;
256
            }
257
 
258
            while (slashes) {
259
                *arg = '\\';
260
                arg++;
261
                slashes--;
262
            }
263
 
264
            if ((*p == '\0') || (!inquote && isspace(*p))) {
265
                break;
266
            }
267
            if (copy != 0) {
268
                *arg = *p;
269
                arg++;
270
            }
271
            p++;
272
        }
273
        *arg = '\0';
274
        argSpace = arg + 1;
275
    }
276
    argv[argc] = NULL;
277
 
278
    *argcPtr = argc;
279
    *argvPtr = argv;
280
}

powered by: WebSVN 2.1.0

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