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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [insight/] [tcl/] [win/] [tclAppInit.c] - Blame information for rev 1782

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

powered by: WebSVN 2.1.0

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