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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [insight/] [expect/] [exp_main_tk.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* exp_main_tk.c - main for expectk
2
 
3
   This file consists of three pieces:
4
   1) AppInit for Expectk.  This has been suitably modified to invoke
5
      a modified version of Tk_Init.
6
   2) Tk_Init for Expectk.  What's wrong with the normal Tk_Init is that
7
      removes the -- in the cmd-line arg list, so Expect cannot know
8
      whether args are flags to Expectk or data for the script.  Sigh.
9
   3) Additions and supporting utilities to Tk's Argv parse table to
10
      support Expectk's flags.
11
 
12
   Author: Don Libes, NIST, 2/20/96
13
 
14
*/
15
 
16
/* Expectk's AppInit */
17
 
18
/*
19
 * tkAppInit.c --
20
 *
21
 *      Provides a default version of the Tcl_AppInit procedure for
22
 *      use in wish and similar Tk-based applications.
23
 *
24
 * Copyright (c) 1993 The Regents of the University of California.
25
 * Copyright (c) 1994 Sun Microsystems, Inc.
26
 *
27
 * See the file "license.terms" for information on usage and redistribution
28
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
29
 */
30
 
31
#ifndef lint
32
static char sccsid[] = "@(#) tkAppInit.c 1.19 95/12/23 17:09:24";
33
#endif /* not lint */
34
 
35
#include <ctype.h>
36
 
37
#include "tk.h"
38
 
39
#include "expect_tcl.h"
40
#include "Dbg.h"
41
 
42
/*
43
 * The following variable is a special hack that is needed in order for
44
 * Sun shared libraries to be used for Tcl.
45
 */
46
 
47
extern int matherr();
48
int *tclDummyMathPtr = (int *) matherr;
49
 
50
#ifdef TK_TEST
51
EXTERN int              Tktest_Init _ANSI_ARGS_((Tcl_Interp *interp));
52
#endif /* TK_TEST */
53
 
54
/*
55
 *----------------------------------------------------------------------
56
 *
57
 * main --
58
 *
59
 *      This is the main program for the application.
60
 *
61
 * Results:
62
 *      None: Tk_Main never returns here, so this procedure never
63
 *      returns either.
64
 *
65
 * Side effects:
66
 *      Whatever the application does.
67
 *
68
 *----------------------------------------------------------------------
69
 */
70
 
71
int
72
main(argc, argv)
73
    int argc;                   /* Number of command-line arguments. */
74
    char **argv;                /* Values of command-line arguments. */
75
{
76
    Tk_Main(argc, argv, Tcl_AppInit);
77
    return 0;                    /* Needed only to prevent compiler warning. */
78
}
79
 
80
/*
81
 *----------------------------------------------------------------------
82
 *
83
 * Tcl_AppInit --
84
 *
85
 *      This procedure performs application-specific initialization.
86
 *      Most applications, especially those that incorporate additional
87
 *      packages, will have their own version of this procedure.
88
 *
89
 * Results:
90
 *      Returns a standard Tcl completion code, and leaves an error
91
 *      message in interp->result if an error occurs.
92
 *
93
 * Side effects:
94
 *      Depends on the startup script.
95
 *
96
 *----------------------------------------------------------------------
97
 */
98
 
99
int
100
Tcl_AppInit(interp)
101
    Tcl_Interp *interp;         /* Interpreter for application. */
102
{
103
    if (Tcl_Init(interp) == TCL_ERROR) {
104
        return TCL_ERROR;
105
    }
106
 
107
    /* do Expect first so we can get access to Expect commands when */
108
    /* Tk_Init does the argument parsing of -c */
109
    if (Expect_Init(interp) == TCL_ERROR) {
110
        return TCL_ERROR;
111
    }
112
    Tcl_StaticPackage(interp, "Expect", Expect_Init, (Tcl_PackageInitProc *)NULL);
113
 
114
    if (Tk_Init2(interp) == TCL_ERROR) {        /* DEL */
115
        return TCL_ERROR;
116
    }
117
    Tcl_StaticPackage(interp, "Tk", Tk_Init, (Tcl_PackageInitProc *) NULL);
118
 
119
    /*
120
     * Call the init procedures for included packages.  Each call should
121
     * look like this:
122
     *
123
     * if (Mod_Init(interp) == TCL_ERROR) {
124
     *     return TCL_ERROR;
125
     * }
126
     *
127
     * where "Mod" is the name of the module.
128
     */
129
 
130
    /*
131
     * Call Tcl_CreateCommand for application-specific commands, if
132
     * they weren't already created by the init procedures called above.
133
     */
134
 
135
    /*
136
     * Specify a user-specific startup file to invoke if the application
137
     * is run interactively.  Typically the startup file is "~/.apprc"
138
     * where "app" is the name of the application.  If this line is deleted
139
     * then no user-specific startup file will be run under any conditions.
140
     */
141
 
142
    Tcl_SetVar(interp, "tcl_rcFileName", "~/.wishrc", TCL_GLOBAL_ONLY);
143
    return TCL_OK;
144
}
145
 
146
 
147
 
148
 
149
/*
150
 * Count of number of main windows currently open in this process.
151
 */
152
 
153
static int numMainWindows;
154
 
155
/*
156
 * The variables and table below are used to parse arguments from
157
 * the "argv" variable in Tk_Init.
158
 */
159
 
160
static int synchronize;
161
static char *name;
162
static char *display;
163
static char *geometry;
164
static char *colormap;
165
static char *visual;
166
static int rest = 0;
167
 
168
/* for Expect */
169
int my_rc = 1;
170
int sys_rc = 1;
171
int optcmd_eval();
172
#ifdef TCL_DEBUGGER
173
int optcmd_debug();
174
#endif
175
int print_version = 0;
176
 
177
static Tk_ArgvInfo argTable[] = {
178
    {"-colormap", TK_ARGV_STRING, (char *) NULL, (char *) &colormap,
179
        "Colormap for main window"},
180
    {"-display", TK_ARGV_STRING, (char *) NULL, (char *) &display,
181
        "Display to use"},
182
    {"-geometry", TK_ARGV_STRING, (char *) NULL, (char *) &geometry,
183
        "Initial geometry for window"},
184
    {"-name", TK_ARGV_STRING, (char *) NULL, (char *) &name,
185
        "Name to use for application"},
186
    {"-sync", TK_ARGV_CONSTANT, (char *) 1, (char *) &synchronize,
187
        "Use synchronous mode for display server"},
188
    {"-visual", TK_ARGV_STRING, (char *) NULL, (char *) &visual,
189
        "Visual for main window"},
190
    {"--", TK_ARGV_REST, (char *) 1, (char *) &rest,
191
        "Pass all remaining arguments through to script"},
192
/* for Expect */
193
    {"-command", TK_ARGV_GENFUNC, (char *) optcmd_eval, (char *)0,
194
        "Command(s) to execute immediately"},
195
    {"-diag", TK_ARGV_CONSTANT, (char *) 1, (char *) &exp_is_debugging,
196
        "Enable diagnostics"},
197
    {"-norc", TK_ARGV_CONSTANT, (char *) 0, (char *) &my_rc,
198
        "Don't read ~/.expect.rc"},
199
    {"-NORC", TK_ARGV_CONSTANT, (char *) 0, (char *) &sys_rc,
200
        "Don't read system-wide expect.rc"},
201
    {"-version", TK_ARGV_CONSTANT, (char *) 1, (char *) &print_version,
202
        "Print version and exit"},
203
#if TCL_DEBUGGER
204
    {"-Debug", TK_ARGV_GENFUNC, (char *) optcmd_debug, (char *)0,
205
        "Enable debugger"},
206
#endif
207
    {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
208
        (char *) NULL}
209
};
210
 
211
/*
212
 *----------------------------------------------------------------------
213
 *
214
 * Tk_Init --
215
 *
216
 *      This procedure is invoked to add Tk to an interpreter.  It
217
 *      incorporates all of Tk's commands into the interpreter and
218
 *      creates the main window for a new Tk application.  If the
219
 *      interpreter contains a variable "argv", this procedure
220
 *      extracts several arguments from that variable, uses them
221
 *      to configure the main window, and modifies argv to exclude
222
 *      the arguments (see the "wish" documentation for a list of
223
 *      the arguments that are extracted).
224
 *
225
 * Results:
226
 *      Returns a standard Tcl completion code and sets interp->result
227
 *      if there is an error.
228
 *
229
 * Side effects:
230
 *      Depends on various initialization scripts that get invoked.
231
 *
232
 *----------------------------------------------------------------------
233
 */
234
 
235
int
236
Tk_Init2(interp)
237
    Tcl_Interp *interp;         /* Interpreter to initialize. */
238
{
239
    char *p;
240
    int argc, code;
241
    char **argv, *args[20];
242
    Tcl_DString class;
243
    char buffer[30];
244
 
245
    /*
246
     * If there is an "argv" variable, get its value, extract out
247
     * relevant arguments from it, and rewrite the variable without
248
     * the arguments that we used.
249
     */
250
 
251
    synchronize = 0;
252
    name = display = geometry = colormap = visual = NULL;
253
    p = Tcl_GetVar2(interp, "argv", (char *) NULL, TCL_GLOBAL_ONLY);
254
    argv = NULL;
255
    if (p != NULL) {
256
        if (Tcl_SplitList(interp, p, &argc, &argv) != TCL_OK) {
257
            argError:
258
            Tcl_AddErrorInfo(interp,
259
                    "\n    (processing arguments in argv variable)");
260
            return TCL_ERROR;
261
        }
262
        if (Tk_ParseArgv(interp, (Tk_Window) NULL, &argc, argv,
263
                argTable, TK_ARGV_DONT_SKIP_FIRST_ARG|TK_ARGV_NO_DEFAULTS)
264
                != TCL_OK) {
265
            ckfree((char *) argv);
266
            goto argError;
267
        }
268
 
269
        if (print_version) {
270
            extern char exp_version[];
271
            printf ("expectk version %s\n", exp_version);
272
            exp_exit (interp, 0);
273
        }
274
 
275
        p = Tcl_Merge(argc, argv);
276
        Tcl_SetVar2(interp, "argv", (char *) NULL, p, TCL_GLOBAL_ONLY);
277
        sprintf(buffer, "%d", argc);
278
        Tcl_SetVar2(interp, "argc", (char *) NULL, buffer, TCL_GLOBAL_ONLY);
279
        ckfree(p);
280
    }
281
 
282
    /*
283
     * Figure out the application's name and class.
284
     */
285
 
286
    if (name == NULL) {
287
        name = Tcl_GetVar(interp, "argv0", TCL_GLOBAL_ONLY);
288
        if ((name == NULL) || (*name == 0)) {
289
            name = "tk";
290
        } else {
291
            p = (char *)strrchr(name, '/');     /* added cast - DEL */
292
            if (p != NULL) {
293
                name = p+1;
294
            }
295
        }
296
    }
297
    Tcl_DStringInit(&class);
298
    Tcl_DStringAppend(&class, name, -1);
299
    p = Tcl_DStringValue(&class);
300
    if (islower(*p)) {
301
        *p = toupper((unsigned char) *p);
302
    }
303
 
304
    /*
305
     * Create an argument list for creating the top-level window,
306
     * using the information parsed from argv, if any.
307
     */
308
 
309
    args[0] = "toplevel";
310
    args[1] = ".";
311
    args[2] = "-class";
312
    args[3] = Tcl_DStringValue(&class);
313
    argc = 4;
314
    if (display != NULL) {
315
        args[argc] = "-screen";
316
        args[argc+1] = display;
317
        argc += 2;
318
 
319
        /*
320
         * If this is the first application for this process, save
321
         * the display name in the DISPLAY environment variable so
322
         * that it will be available to subprocesses created by us.
323
         */
324
 
325
        if (numMainWindows == 0) {
326
            Tcl_SetVar2(interp, "env", "DISPLAY", display, TCL_GLOBAL_ONLY);
327
        }
328
    }
329
    if (colormap != NULL) {
330
        args[argc] = "-colormap";
331
        args[argc+1] = colormap;
332
        argc += 2;
333
    }
334
    if (visual != NULL) {
335
        args[argc] = "-visual";
336
        args[argc+1] = visual;
337
        argc += 2;
338
    }
339
    args[argc] = NULL;
340
    code = TkCreateFrame((ClientData) NULL, interp, argc, args, 1, name);
341
    Tcl_DStringFree(&class);
342
    if (code != TCL_OK) {
343
        goto done;
344
    }
345
    Tcl_ResetResult(interp);
346
    if (synchronize) {
347
        XSynchronize(Tk_Display(Tk_MainWindow(interp)), True);
348
    }
349
 
350
    /*
351
     * Set the geometry of the main window, if requested.  Put the
352
     * requested geometry into the "geometry" variable.
353
     */
354
 
355
    if (geometry != NULL) {
356
        Tcl_SetVar(interp, "geometry", geometry, TCL_GLOBAL_ONLY);
357
        code = Tcl_VarEval(interp, "wm geometry . ", geometry, (char *) NULL);
358
        if (code != TCL_OK) {
359
            goto done;
360
        }
361
    }
362
    if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 1) == NULL) {
363
        code = TCL_ERROR;
364
        goto done;
365
    }
366
    code = Tcl_PkgProvide(interp, "Tk", TK_VERSION);
367
    if (code != TCL_OK) {
368
        goto done;
369
    }
370
 
371
    /*
372
     * Invoke platform-specific initialization.
373
     */
374
 
375
#if TCL_MAJOR_VERSION < 8
376
    code = TkPlatformInit(interp);
377
#else
378
    code = TkpInit(interp, 0);
379
#endif
380
 
381
    done:
382
    if (argv != NULL) {
383
        ckfree((char *) argv);
384
    }
385
    return code;
386
}
387
 
388
/*ARGSUSED*/
389
int
390
optcmd_eval(dst,interp,key,argc,argv)
391
char *dst;
392
Tcl_Interp *interp;
393
char *key;
394
int argc;
395
char **argv;
396
{
397
        int i;
398
        int rc;
399
 
400
        exp_cmdlinecmds = 1;
401
 
402
        rc = Tcl_Eval(interp,argv[0]);
403
        if (rc == TCL_ERROR) return -1;
404
 
405
        argc--;
406
        for (i=0;i<argc;i++) {
407
                argv[i] = argv[i+1];
408
        }
409
 
410
        return argc;
411
}
412
 
413
#ifdef TCL_DEBUGGER
414
/*ARGSUSED*/
415
int
416
optcmd_debug(dst,interp,key,argc,argv)
417
char *dst;
418
Tcl_Interp *interp;
419
char *key;
420
int argc;
421
char **argv;
422
{
423
        int i;
424
 
425
        if (argc == 0) {
426
                strcpy(interp->result,"-Debug flag needs 1 or 0 argument");
427
                return -1;
428
        }
429
 
430
        if (Tcl_GetInt(interp,argv[0],&i) != TCL_OK) {
431
                return -1;
432
        }
433
 
434
        if (i) {
435
                Dbg_On(interp,0);
436
        }
437
 
438
        argc--;
439
        for (i=0;i<argc;i++) {
440
                argv[i] = argv[i+1];
441
        }
442
 
443
        return argc;
444
}
445
#endif /*TCL_DEBUGGER*/

powered by: WebSVN 2.1.0

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