/[ascend]/trunk/tcltk98/generic/interface/Driver.c
ViewVC logotype

Contents of /trunk/tcltk98/generic/interface/Driver.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Fri Oct 29 20:54:12 2004 UTC (17 years, 8 months ago) by aw0a
Original Path: trunk/ascend4/interface/Driver.c
File MIME type: text/x-csrc
File size: 40147 byte(s)
Setting up web subdirectory in repository
1 /*
2 * Driver.c
3 * by Kirk Abbott and Ben Allan
4 * Created: 1/94
5 * Version: $Revision: 1.48 $
6 * Version control file: $RCSfile: Driver.c,v $
7 * Date last modified: $Date: 2003/08/23 18:43:06 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the ASCEND Tcl/Tk interface
11 *
12 * Copyright 1997, Carnegie Mellon University
13 *
14 * The ASCEND Tcl/Tk interface is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The ASCEND Tcl/Tk interface is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING. COPYING is found in ../compiler.
28 */
29
30 #include <stdarg.h>
31 #include <ctype.h>
32 #include <signal.h>
33 #include <time.h>
34 #include "tcl.h"
35 #include "tk.h"
36 #include "utilities/ascConfig.h"
37 #ifndef __WIN32__
38 #include <unistd.h>
39 #else
40 #define WIN32_LEAN_AND_MEAN
41 #include <windows.h>
42 #include <locale.h>
43 #undef WIN32_LEAN_AND_MEAN
44 #endif /* __WIN32__ */
45 #include "utilities/ascMalloc.h" /* for ascshutdown */
46 #include "utilities/ascPanic.h" /* for Asc_Panic */
47 #include "utilities/ascEnvVar.h"
48 #include "compiler/compiler.h"
49 #include "compiler/ascCompiler.h"
50 #include "compiler/instance_enum.h"
51 #include "compiler/fractions.h"
52 #include "compiler/dimen.h"
53 #include "compiler/compiler.h" /* for symchar for units.h */
54 #include "compiler/units.h"
55 #include "solver/slv_types.h"
56 #include "solver/var.h"
57 #include "solver/rel.h"
58 #include "solver/logrel.h"
59 #include "solver/discrete.h"
60 #include "solver/mtx.h"
61 #include "solver/slv_stdcalls.h"
62 #include "interface/AscBitmaps.h"
63 #include "interface/HelpProc.h"
64 #include "interface/Commands.h"
65 #include "interface/Driver.h"
66 #include "interface/ScriptProc.h"
67 #include "interface/SolverProc.h"
68 #include "interface/UnitsProc.h"
69
70 #ifndef lint
71 static CONST char DriverID[] = "$Id: Driver.c,v 1.48 2003/08/23 18:43:06 ballan Exp $";
72 #endif
73
74
75 /*
76 * The following are the environment variables ASCEND requires.
77 * If the user does not have the DIST_ENVIRONVAR set in his or her
78 * environment, a default value is set based on the directory where the
79 * ascend binary lives. The other enviornment variables will be set
80 * to default values keyed off of DIST_ENVIRONVAR. See the function
81 * CheckEnvironmentVars later in this file for the details.
82 */
83 #define DIST_ENVIRONVAR "ASCENDDIST"
84 #define ASCTK_ENVIRONVAR "ASCENDTK"
85 #define BITMAP_ENVIRONVAR "ASCENDBITMAPS"
86 #define LIBR_ENVIRONVAR "ASCENDLIBRARY"
87
88 /*
89 * EXPORTED VARIABLES
90 */
91
92 /**
93 * g_compiler_timing
94 *
95 * TRUE if compiler timing is to be printed.
96 * default is false, set to TRUE by passing -t on the command line
97 */
98 int g_compiler_timing = 0;
99
100 /**
101 * g_interp
102 *
103 * Interpreter for this application. We need to make it global
104 * so that our signal/floating-porint traps can access it.
105 */
106 Tcl_Interp *g_interp;
107
108 /*
109 * zz_debug
110 *
111 * Comes from the yacc file if yacc was built with debugging information
112 */
113 #ifdef ZZ_DEBUG
114 extern int zz_debug;
115 #endif
116
117
118 /*
119 * Declarations for procedures defined outside of this file.
120 */
121 extern int Tktable_Init(Tcl_Interp*);
122
123 /*
124 * Forward declarations for procedures defined later in this file.
125 */
126 static int AscDriver(int, CONST84 char * argv[]);
127 static void AscTrap(int);
128 static int AscCheckEnvironVars(Tcl_Interp*);
129 static void AscPrintHelpExit(CONST char *);
130 static int AscProcessCommandLine(Tcl_Interp*, int, CONST84 char **);
131 static void Prompt(Tcl_Interp*, int);
132 static int AscSetStartupFile(Tcl_Interp*);
133 static void StdinProc(ClientData, int);
134 #ifdef DEBUG_MALLOC
135 static void InitDebugMalloc(void);
136 #endif /* DEBUG_MALLOC */
137 #ifdef __WIN32__
138 static void setargv(int*, char ***);
139 #endif /* __WIN32__ */
140
141
142 /*
143 * LOCALLY GLOBAL VARIABLES
144 */
145
146 /*
147 * g_interface_simplify_relations
148 *
149 * TRUE for compiler optimizations
150 * default is TRUE, set to FALSE by passing +s on the command line
151 */
152 static int g_interface_simplify_relations = TRUE;
153
154 /*
155 * g_interfacever
156 *
157 * TRUE if windows to be built; default is TRUE, false is not supported
158 */
159 static int g_interfacever = 1;
160
161 /*
162 * g_command
163 *
164 * Used to assemble lines of terminal input into Tcl commands.
165 */
166 static Tcl_DString g_command;
167
168 /*
169 * g_line
170 *
171 * Used to read the next line from the terminal input.
172 */
173 static Tcl_DString g_line;
174
175 /*
176 * tty
177 *
178 * Non-zero means standard input is a terminal-like device.
179 * Zero means it's a file.
180 */
181 static int tty;
182
183 /*
184 * initScriptTclAdjust
185 * initScriptTkAdjust
186 *
187 * These two variables hold Tcl scripts that will set the TCL_LIBRARY
188 * and TK_LIBRARY environment variables if they are not already
189 * set in the user's environment.
190 * TCL_LIBRARY is set to: dirnameofexecutable/../../Tcl/lib/tcl8.0
191 * TK_LIBRARY is set to $tcl_library/../tk8.0
192 */
193 static char initScriptTclAdjust[] =
194 "proc asc_tclInit {} {\n\
195 global env\n\
196 rename asc_tclInit {}\n\
197 set errors {}\n\
198 set dirs {}\n\
199 if [info exists env(TCL_LIBRARY)] {\n\
200 return\n\
201 } else {\n\
202 set parentDir [file dirname [info nameofexecutable]]\n\
203 set env(TCL_LIBRARY) $parentDir/../../Tcl/lib/tcl8.0\n\
204 }\n\
205 }\n\
206 asc_tclInit";
207 /* for tcl, FOO/ascend4/bin/ascend4.exe is the executable, then
208 * /-- up to FOO/ascend4/bin
209 * set parentDir [file dirname [info nameofexecutable]]\n\
210 * Then if Tcl is next to ascend4, ../../Tcl/lib/tcl8.0 should work.
211 */
212
213 static char initScriptTkAdjust[] =
214 "proc asc_tkInit {} {\n\
215 global env\n\
216 rename asc_tkInit {}\n\
217 set errors {}\n\
218 set dirs {}\n\
219 if [info exists env(TK_LIBRARY)] {\n\
220 return\n\
221 } else {\n\
222 set parentDir [file dirname [info library]]\n\
223 set env(TK_LIBRARY) $parentDir/tk8.0\n\
224 }\n\
225 }\n\
226 asc_tkInit";
227 /*
228 * This assumes tcl_library has been found and that tcl8.0 and tk8.0
229 * are installed in the same lib directory -- the default tcl/tk install.
230 */
231
232 /*
233 * build_name
234 *
235 * who built this binary and when
236 */
237 #ifndef TIMESTAMP
238 static char build_name[]="by anonymous";
239 #else
240 static char build_name[]=TIMESTAMP;
241 #endif /* TIMESTAMP */
242
243
244
245 /*
246 * main or WinMain
247 *
248 * The main entry point for a Unix or Windows application.
249 *
250 * Each just calls AscDriver().
251 * These are based on functions from the Tk 8.0 distribution.
252 * See unix/tkAppInit.c and win/winMain.c in their sources.
253 */
254 #ifndef __WIN32__
255
256 int main(int argc, CONST84 char *argv[])
257 {
258 AscDriver(argc, argv);
259 return 0;
260 }
261
262 #else /* __WIN32__ */
263
264 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
265 LPSTR lpszCmdLine, int nCmdShow)
266 {
267 int argc;
268 char **argv;
269 char *p;
270 char buffer[MAX_PATH];
271
272 /*
273 * Set up the default locale to be standard "C" locale so parsing
274 * is performed correctly.
275 */
276 setlocale(LC_ALL, "C");
277
278 /*
279 * Increase the application queue size from default value of 8.
280 * At the default value, cross application SendMessage of WM_KILLFOCUS
281 * will fail because the handler will not be able to do a PostMessage!
282 * This is only needed for Windows 3.x, since NT dynamically expands
283 * the queue.
284 */
285 SetMessageQueue(64);
286
287 /*
288 * Create the console channels and install them as the standard
289 * channels. All I/O will be discarded until TkConsoleInit is
290 * called to attach the console to a text widget.
291 */
292
293 /*
294 * Windows expects us to parse our arguments ourselves.
295 */
296 setargv(&argc, &argv);
297
298 /*
299 * Replace argv[0] with full pathname of executable, and forward
300 * slashes substituted for backslashes.
301 */
302 GetModuleFileName(NULL, buffer, sizeof(buffer));
303 argv[0] = buffer;
304 for (p = buffer; *p != '\0'; p++) {
305 if (*p == '\\') {
306 *p = '/';
307 }
308 }
309
310 AscDriver(argc, argv);
311 return 1;
312 }
313 #endif /* __WIN32__ */
314
315
316 /*
317 * int AscDriver( argc, argv )
318 * int argc;
319 * char *argv;
320 *
321 * A common entry point for Windows and Unix. The corresponding
322 * WinMain() and main() functions just call this function.
323 *
324 * This function creates a Tcl interpreter, initializes Tcl and Tk,
325 * initializes the Ascend data structures, sets up the user's
326 * environment, sources ASCEND's startup script, and calls Tk_MainLoop
327 * so the user can interact with ASCEND. Cleans up and exits the
328 * program when Tk_MainLoop returns.
329 *
330 * This function is based on the functions Tk_Main and Tcl_AppInit
331 * from the Tk8.0 distribution. See the files tkMain.c and tkAppInit.c
332 * in the Tk sources.
333 *
334 */
335 static int AscDriver(int argc, CONST84 char *argv[])
336 {
337 Tcl_Interp *interp; /* local version of global g_interp */
338 Tcl_Channel inChannel;
339 Tcl_Channel outChannel;
340
341 /*
342 * Create the Tk Console
343 *
344 * Create the console channels and install them as the standard
345 * channels. All I/O will be discarded until TkConsoleInit() is
346 * called to attach the console to a text widget.
347 */
348 #ifdef ASC_USE_TK_CONSOLE
349 TkConsoleCreate();
350 #endif /* ASC_USE_TK_CONSOLE */
351
352 /*
353 * Find the name of the current executable---available in the
354 * Tcl command `info nameofexecutable'
355 */
356 Tcl_FindExecutable(argv[0]);
357
358 /*
359 * Create the interpreter
360 */
361 g_interp = Tcl_CreateInterp();
362 interp = g_interp;
363 if( interp == NULL ) {
364 Asc_Panic(2, "Asc_Driver",
365 "Call to Tcl_CreateInterp returned NULL interpreter");
366 }
367
368 /*
369 * Check the arguments on the command line
370 */
371 AscProcessCommandLine(interp, argc, argv);
372
373 /*
374 * Set the "tcl_interactive" variable.
375 *
376 * ASCEND sets tty to `1', since we assume ASCEND is always interactive.
377 */
378 tty = 1;
379 Tcl_SetVar(interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
380
381 /*
382 * Initialize Tcl and Tk
383 */
384 (void)Tcl_Eval(interp,initScriptTclAdjust);
385 if (Tcl_Init(interp) == TCL_ERROR) {
386 Asc_Panic(2, "Asc_Driver", "Tcl initialization failed:\n%s",
387 Tcl_GetStringResult(interp));
388 }
389 (void)Tcl_Eval(interp,initScriptTkAdjust);
390 if (Tk_Init(interp) == TCL_ERROR) {
391 Asc_Panic(2, "Asc_Driver", "Tk initialization failed:\n%s",
392 Tcl_GetStringResult(interp));
393 }
394 Tcl_StaticPackage( interp, "Tk", Tk_Init, Tk_SafeInit );
395
396 /*
397 * Initialize the console and the ASCEND printf() substitutes.
398 * All output before this point is lost.
399 */
400 #ifdef ASC_USE_TK_CONSOLE
401 if (TkConsoleInit(interp) == TCL_ERROR) {
402 Asc_Panic(2, "Asc_Driver",
403 "Call to TkConsoleInit failed:\n%s",
404 Tcl_GetStringResult(interp));
405 }
406 #endif /* ASC_USE_TK_CONSOLE */
407 #ifdef USE_ASC_PRINTF
408 Asc_PrintInit();
409 #endif /* USE_ASC_PRINTF */
410
411 /*
412 * Now that our console and printing functions are properly
413 * initialized, print our startup banner.
414 */
415 PRINTF("ASCEND VERSION IV\n");
416 PRINTF("Compiler Implemention Version: 2.0\n");
417 PRINTF("Written by Tom Epperly,Kirk Abbott, and Ben Allan\n");
418 PRINTF(" Built: %s %s %s\n",__DATE__,__TIME__,build_name);
419 PRINTF("Copyright(C) 1990, 1993, 1994 Thomas Guthrie Epperly\n");
420 PRINTF("Copyright(C) 1993-1996 Kirk Andre Abbott, Ben Allan\n");
421 PRINTF("Copyright(C) 1997 Carnegie Mellon University\n");
422
423 /*
424 * Call the init procedures for included packages.
425 */
426 #ifdef STATIC_TKTABLE
427 if( Tktable_Init(interp) == TCL_ERROR ) {
428 Asc_Panic(2, "Asc_Driver",
429 "Call to Tktable_Init failed:\n%s",
430 Tcl_GetStringResult(interp));
431 }
432 #endif /* STATIC_TKTABLE */
433
434 /*
435 * Initialize ASCEND C Structures
436 * Create ASCEND Tcl Commands
437 */
438 clock();
439 /* the next line should NOT be Asc_SignalHandlerPush */
440 (void)signal(SIGINT, AscTrap);
441 #ifdef DEBUG_MALLOC
442 InitDebugMalloc();
443 ascstatus("Memory status after calling InitDebugMalloc()");
444 #endif /* DEBUG_MALLOC */
445 if ( Asc_CompilerInit(g_interface_simplify_relations) != 0 ) {
446 Asc_Panic(2, "Asc_CompilerInit",
447 "Insufficient memory to initialize compiler.");
448 }
449 SlvRegisterStandardClients();
450 if( Asc_HelpInit() == TCL_ERROR ) {
451 Asc_Panic(2, "Asc_HelpInit",
452 "Insufficient memory to initialize help system.");
453 }
454 Asc_CreateCommands(interp);
455 Asc_RegisterBitmaps(interp);
456
457 /*
458 * Set the environment, and set find the
459 * location of ASCEND's startup file.
460 */
461 AscCheckEnvironVars(interp);
462 if( AscSetStartupFile(interp) != TCL_OK ) {
463 Asc_Panic(2, "Asc_Driver",
464 "Cannot find ~/.ascendrc nor the default AscendRC\n%s",
465 Tcl_GetStringResult(interp));
466 }
467
468 /*
469 * Evaluate the ~/.ascendrc or $ASCENDTK/AscendRC file
470 */
471 Tcl_SourceRCFile(interp);
472
473 /*
474 * Establish a channel handlers for stdin and stdout
475 */
476 inChannel = Tcl_GetStdChannel(TCL_STDIN);
477 if (inChannel) {
478 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
479 (ClientData) inChannel);
480 }
481 if (tty) {
482 Prompt(interp, 0);
483 }
484 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
485 if (outChannel) {
486 Tcl_Flush(outChannel);
487 }
488
489 /*
490 * Initialize the Dynamic Strings used in StdinProc()
491 */
492 Tcl_DStringInit(&g_command);
493 Tcl_DStringInit(&g_line);
494 Tcl_ResetResult(interp);
495
496 /*
497 * Loop infinitely, waiting for commands to execute. When there
498 * are no windows left, Tk_MainLoop returns and we exit.
499 */
500 #ifdef DEBUG_MALLOC
501 ascstatus("Memory status before calling Tk_MainLoop()");
502 #endif /* DEBUG_MALLOC */
503 if (Asc_ScriptConfigureInterrupt(1,interp)!=0) {
504 Asc_Panic(2, "Asc_ScriptConfigureInterrupt",
505 "Unable to configure script interrupt.");
506 }
507 Tk_MainLoop();
508 Asc_ScriptConfigureInterrupt(0,interp);
509 #ifdef DEBUG_MALLOC
510 ascstatus("Memory status after Tk_MainLoop() exits");
511 #endif /* DEBUG_MALLOC */
512
513 /*
514 * Do ASCEND Cleanup
515 */
516 Asc_HelpDestroy();
517 Asc_UnitValue(NULL);
518 Asc_SolvMemoryCleanup();
519 Asc_CompilerDestroy();
520 Asc_DestroyEnvironment();
521 #ifdef DEBUG_MALLOC
522 ascshutdown("Memory status just before exiting");
523 #endif /* DEBUG_MALLOC */
524
525 /*
526 * Destroy the interpreter and exit
527 */
528 Tcl_DeleteInterp(interp);
529 Tcl_Exit(0);
530 return 0;
531 }
532
533
534 /*
535 *-----------------------------------------------------------------
536 * int AscCheckEnvironVars(interp)
537 * Tcl_Interp *interp;
538 *
539 * This function checks the following environment variables and sets them
540 * to the default value if they are not set. This function will set
541 * DIST_ENVIRONVAR to the parent of the directory where the ASCEND binary
542 * lives if it is not set, e.g., if the ascend binary is
543 * /foo/bar/ascend4
544 * DIST_ENVIRONVAR is set to "/foo".
545 *
546 * We check to see if ASCTK_ENVIRONVAR points to the proper place by
547 * looking for the ASCEND startup script "AscendRC". If we find it, set
548 * the Tcl variable tcl_rcFileName to its location, otherwise, call
549 * Asc_Panic() to exit.
550 *
551 * Returns a standard Tcl return code.
552 *
553 * CPP_MACRO ENVIRONMENT VAR DEFAULT VALUE
554 * ================= =============== =============
555 * DIST_ENVIRONVAR ASCENDDIST parent of binary's directory
556 * ASCTK_ENVIRONVAR ASCENDTK ASCENDDIST/TK
557 * BITMAP_ENVIRONVAR ASCENDBITMAPS ASCENDTK/bitmaps
558 * LIBR_ENVIRONVAR ASCENDLIBRARY
559 * .:ASCENDDIST/models/libraries:ASCENDDIST/models/examples
560 *
561 */
562 static int AscCheckEnvironVars(Tcl_Interp *interp)
563 {
564 char *tmpenv; /* holds values returned by Asc_GetEnv() */
565 Tcl_DString ascenddist; /* holds the value of DIST_ENVIRONVAR */
566 Tcl_DString buffer2; /* used to incrementally build environment
567 * variable values
568 */
569 Tcl_DString buffer1; /* holds the environment variable value in the
570 * native format: result of passing buffer2
571 * into Tcl_TranslateFileName()
572 */
573 Tcl_Channel c; /* used to test if file exists */
574
575 /* initialize */
576 Tcl_DStringInit(&ascenddist);
577 Tcl_DStringInit(&buffer1);
578 Tcl_DStringInit(&buffer2);
579
580 /*
581 * Get the value of the ASCENDDIST environment variable;
582 * if not set, set it to the parent of the directory containing
583 * the ascend binary. For example, if the ascend binary is
584 * /foo/bar/bin/ascend4, set ASCENDDIST to /foo/bar
585 * If Tcl doesn't know where we are---the Tcl command
586 * `info nameofexecutable' returns ""---then ASCENDDIST is set
587 * to "."
588 */
589 if( Asc_ImportPathList(DIST_ENVIRONVAR) == 0 ) {
590 if( (tmpenv = Asc_GetEnv(DIST_ENVIRONVAR)) == NULL ) {
591 /* shouldn't be NULL since we just imported it successfully */
592 Asc_Panic(2, "CheckEnvironmentVars",
593 "Asc_GetEnv(%s) returned NULL value.", DIST_ENVIRONVAR);
594 }
595 Tcl_DStringAppend(&ascenddist, tmpenv, -1);
596 ascfree(tmpenv);
597 } else {
598 char cmd[] =
599 "file nativename [file dirname [file dirname [info nameofexecutable]]]";
600 if( Tcl_Eval(interp, cmd) == TCL_OK ) {
601 Tcl_DStringGetResult(interp, &ascenddist);
602 if(Asc_SetPathList(DIST_ENVIRONVAR,Tcl_DStringValue(&ascenddist)) != 0) {
603 Asc_Panic(2, "AscCheckEnvironVars",
604 "Asc_SetPathList() returned Nonzero: "
605 "Not enough memory to extend the environment");
606 }
607 }
608 }
609 /* Make sure the Tcl side can also see this variable */
610 Tcl_SetVar2(interp, "env", DIST_ENVIRONVAR,
611 Tcl_DStringValue(&ascenddist), TCL_GLOBAL_ONLY);
612
613 /*
614 * If the user's environment does not have ASCENDLIBRARY set, then set
615 * it to a reasonable default.
616 */
617 if( Asc_ImportPathList(LIBR_ENVIRONVAR) == 0 ) {
618 if( (tmpenv = Asc_GetEnv(LIBR_ENVIRONVAR)) == NULL ) {
619 /* shouldn't be NULL since we just imported it successfully */
620 Asc_Panic(2, "CheckEnvironmentVars",
621 "Asc_GetEnv(%s) returned NULL value.", LIBR_ENVIRONVAR);
622 }
623 /* Make sure the Tcl side can also see this variable */
624 Tcl_SetVar2(interp, "env", LIBR_ENVIRONVAR, tmpenv, TCL_GLOBAL_ONLY);
625 ascfree(tmpenv);
626 } else {
627 /* Add ``.'' to the ASCENDLIBRARY envar */
628 if( Asc_SetPathList(LIBR_ENVIRONVAR, ".") != 0 ) {
629 Asc_Panic(2, "AscCheckEnvironVars",
630 "Asc_SetPathList() returned Nonzero: "
631 "Not enough memory to extend the environment");
632 }
633
634 /* Add ``$ASCENDDIST/models'' to the ASCENDLIBRARY envar */
635 Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&ascenddist), -1);
636 Tcl_DStringAppend(&buffer2, "/models", -1);
637 if( NULL != (Tcl_TranslateFileName(interp, Tcl_DStringValue(&buffer2),
638 &buffer1))) {
639 if(Asc_AppendPath(LIBR_ENVIRONVAR, Tcl_DStringValue(&buffer1)) != 0) {
640 Asc_Panic(2, "AscCheckEnvironVars",
641 "Asc_AppendPath() returned Nonzero: "
642 "Not enough memory to extend the environment");
643 }
644 Tcl_DStringFree(&buffer1);
645 }
646 Tcl_DStringFree(&buffer2);
647
648 /* Add ``$ASCENDDIST/models/examples'' to the ASCENDLIBRARY envar */
649 Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&ascenddist), -1);
650 Tcl_DStringAppend(&buffer2, "/models/examples", -1);
651 if( NULL != (Tcl_TranslateFileName(interp, Tcl_DStringValue(&buffer2),
652 &buffer1))) {
653 if(Asc_AppendPath(LIBR_ENVIRONVAR, Tcl_DStringValue(&buffer1)) != 0) {
654 Asc_Panic(2, "AscCheckEnvironVars",
655 "Asc_AppendPath() returned Nonzero: "
656 "Not enough memory to extend the environment");
657 }
658 Tcl_DStringFree(&buffer1);
659 }
660 Tcl_DStringFree(&buffer2);
661
662 /* Add ``$ASCENDDIST/models/libraries'' to the ASCENDLIBRARY envar */
663 Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&ascenddist), -1);
664 Tcl_DStringAppend(&buffer2, "/models/libraries", -1);
665 if( NULL != (Tcl_TranslateFileName(interp, Tcl_DStringValue(&buffer2),
666 &buffer1))) {
667 if(Asc_AppendPath(LIBR_ENVIRONVAR, Tcl_DStringValue(&buffer1)) != 0) {
668 Asc_Panic(2, "AscCheckEnvironVars",
669 "Asc_AppendPath() returned Nonzero: "
670 "Not enough memory to extend the environment");
671 }
672 Tcl_DStringFree(&buffer1);
673 }
674 Tcl_DStringFree(&buffer2);
675
676 /* Get the full value of the environment variable and set
677 * $env(ASCENDLIBRARY) in the Tcl code
678 */
679 if( (tmpenv = Asc_GetEnv(LIBR_ENVIRONVAR)) == NULL ) {
680 /* shouldn't be NULL since we just set it. memory error! */
681 Asc_Panic(2, "CheckEnvironmentVars",
682 "Asc_GetEnv(%s) returned NULL value.", LIBR_ENVIRONVAR);
683 }
684 /* Make sure the Tcl side can also see this variable */
685 Tcl_SetVar2(interp, "env", LIBR_ENVIRONVAR, tmpenv, TCL_GLOBAL_ONLY);
686 ascfree(tmpenv);
687 }
688
689 /*
690 * If the user's environment does not have ASCENDTK set, then set it
691 * by appending `TK' to ASCENDDIST. Later in this function, we check
692 * to make sure it is a valid directory by checking for the existence
693 * of `AscendRC' in that directory.
694 */
695 if( Asc_ImportPathList(ASCTK_ENVIRONVAR) == 0 ) {
696 if( (tmpenv = Asc_GetEnv(ASCTK_ENVIRONVAR)) == NULL ) {
697 /* shouldn't be NULL since we just imported it successfully */
698 Asc_Panic(2, "CheckEnvironmentVars",
699 "Asc_GetEnv(%s) returned NULL value.", ASCTK_ENVIRONVAR);
700 }
701 /* store ASCENDTK in ``buffer1'' so we can check for ASCENDTK/AscendRC
702 * below
703 */
704 Tcl_DStringAppend(&buffer1, tmpenv, -1);
705 ascfree(tmpenv);
706 } else {
707 Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&ascenddist), -1);
708 Tcl_DStringAppend(&buffer2, "/TK", -1);
709 if(NULL != (Tcl_TranslateFileName(interp, Tcl_DStringValue(&buffer2),
710 &buffer1))) {
711 if( Asc_SetPathList(ASCTK_ENVIRONVAR, Tcl_DStringValue(&buffer1)) != 0) {
712 Asc_Panic(2, "Asc_EnvironmentInit",
713 "Not enough memory to initialize the environment");
714 }
715 }
716 Tcl_DStringFree(&buffer2);
717 }
718 /* Make sure the Tcl side can also see this variable */
719 Tcl_SetVar2(interp, "env", ASCTK_ENVIRONVAR,
720 Tcl_DStringValue(&buffer1), TCL_GLOBAL_ONLY);
721
722 /*
723 * Check to see if ASCENDTK looks reasonable by checking
724 * for ASCENDTK/AscendRC We use the Tcl channel
725 * mechanism to see if file exists.
726 */
727 Tcl_DStringAppend(&buffer1, "/AscendRC", -1 );
728 c = Tcl_OpenFileChannel( NULL, Tcl_DStringValue(&buffer1), "r", 0 );
729 if( c != (Tcl_Channel)NULL ) {
730 /*
731 * file exists. close the channel and set tcl_rcfilename to
732 * this location
733 */
734 Tcl_Close( NULL, c );
735 Tcl_SetVar(interp, "tcl_rcFileName", Tcl_DStringValue(&buffer1),
736 TCL_GLOBAL_ONLY);
737 } else {
738 Asc_Panic(2, "AscCheckEnvironVars",
739 "ERROR: Cannot find the file \"%s\" in the subdirectory \"TK\"\n"
740 "under the directory \"%s\"\n"
741 "Please check the value of the environment variables %s and\n"
742 "and %s and start ASCEND again.\n",
743 "AscendRC", Tcl_DStringValue(&ascenddist), DIST_ENVIRONVAR,
744 ASCTK_ENVIRONVAR);
745 }
746 Tcl_DStringFree(&buffer1);
747
748 /*
749 * If the user's environment does not have ASCENDBITMAPS set, then set
750 * it by appending `bitmaps' to ASCENDTK.
751 */
752 if( Asc_ImportPathList(BITMAP_ENVIRONVAR) == 0 ) {
753 if( (tmpenv = Asc_GetEnv(BITMAP_ENVIRONVAR)) == NULL ) {
754 /* shouldn't be NULL since we just imported it successfully */
755 Asc_Panic(2, "CheckEnvironmentVars",
756 "Asc_GetEnv(%s) returned NULL value.", BITMAP_ENVIRONVAR);
757 }
758 /* Make sure the Tcl side can also see this variable */
759 Tcl_SetVar2(interp, "env", BITMAP_ENVIRONVAR, tmpenv, TCL_GLOBAL_ONLY);
760 ascfree(tmpenv);
761 } else {
762 Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&ascenddist), -1);
763 Tcl_DStringAppend(&buffer2, "/TK/bitmaps", -1);
764 if(NULL != (Tcl_TranslateFileName(interp, Tcl_DStringValue(&buffer2),
765 &buffer1))) {
766 if(Asc_SetPathList(BITMAP_ENVIRONVAR, Tcl_DStringValue(&buffer1)) != 0) {
767 Asc_Panic(2, "Asc_EnvironmentInit",
768 "Not enough memory to initialize the environment");
769 }
770 }
771 Tcl_DStringFree(&buffer2);
772 /* Make sure the Tcl side can also see this variable */
773 Tcl_SetVar2(interp, "env", BITMAP_ENVIRONVAR,
774 Tcl_DStringValue(&buffer1), TCL_GLOBAL_ONLY);
775 Tcl_DStringFree(&buffer1);
776 }
777
778 /* Cleanup */
779 Tcl_DStringFree(&ascenddist);
780
781 return TCL_OK;
782 }
783
784
785 /*
786 * int AscSetStartupFile(interp)
787 * Tcl_Interp *interp;
788 *
789 * Look for ~/.ascendrc; if found, set the Tcl variable tcl_rcFileName
790 * to this file's location. This overrides the value set in
791 * AscCheckEnvironVars().
792 * If ~/_ascendrc is available it only gets used if ~/.ascendrc is not.
793 * Returns a standard Tcl return code.
794 */
795 static int AscSetStartupFile(Tcl_Interp *interp)
796 {
797 char *fullname; /* try to find this if first fails */
798 Tcl_DString buffer;
799 Tcl_Channel c; /* used to check for file existance */
800
801 Tcl_ResetResult(interp);
802
803 fullname = Tcl_TranslateFileName( interp, "~/.ascendrc", &buffer );
804 if( fullname != NULL ) {
805 /*
806 * Use the Tcl file channel routines to determine if ~/.ascendrc
807 * exists. We cannot use access() since Windows doesn't use it.
808 */
809 c = Tcl_OpenFileChannel( NULL, fullname, "r", 0 );
810 if( c != (Tcl_Channel)NULL ) {
811 /* file exists. close the channel and set tcl_rcFileName. */
812 Tcl_Close( NULL, c );
813 Tcl_SetVar(interp, "tcl_rcFileName", fullname, TCL_GLOBAL_ONLY);
814 Tcl_DStringFree(&buffer);
815 return TCL_OK;
816 }
817 Tcl_DStringFree(&buffer);
818 }
819 fullname = Tcl_TranslateFileName( interp, "~/_ascendrc", &buffer );
820 if( fullname != NULL ) {
821 /*
822 * Use the Tcl file channel routines to determine if ~/_ascendrc
823 * exists. We cannot use access() since Windows doesn't use it.
824 */
825 c = Tcl_OpenFileChannel( NULL, fullname, "r", 0 );
826 if( c != (Tcl_Channel)NULL ) {
827 /* file exists. close the channel and set tcl_rcFileName */
828 Tcl_Close( NULL, c );
829 Tcl_SetVar(interp, "tcl_rcFileName", fullname, TCL_GLOBAL_ONLY);
830 Tcl_DStringFree(&buffer);
831 return TCL_OK;
832 }
833 Tcl_DStringFree(&buffer);
834 }
835 return TCL_OK; /* probably should be TCL_ERROR */
836 }
837
838
839
840 /*
841 * file = AscProcessCommandLine(argc, argv)
842 * char *file;
843 * int argc;
844 * char *argv[];
845 *
846 * Process the options given on the command line `argv' where `argc' is
847 * the length of argv.
848 *
849 * Strip out ASCEND specific flags and then pass the rest to Tcl so it
850 * can set what it needs.
851 *
852 * This function may call exit() if the user requests help.
853 */
854 static int AscProcessCommandLine(Tcl_Interp *interp, int argc, CONST84 char **argv)
855 {
856 int i;
857 int flag; /* set to 1 for `+arg', -1 for `-arg' */
858 size_t length; /* length of an argv */
859 char *args;
860 char buf[MAXIMUM_NUMERIC_LENGTH]; /* space for integer->string conversion */
861 int new_argc = 0; /* the argc we will pass to Tcl */
862 #ifdef ZZ_DEBUG
863 zz_debug = 0; /* nonzero to print parser debugging info*/
864 #endif
865
866 for( i = 1; i < argc; i++ ) {
867 if( (length = strlen(argv[i])) == 0 ) {
868 /* ignore 0-length arguments */
869 continue;
870 }
871
872 if(( length >= 2 ) && ( strncmp(argv[i],"-h",2) == 0 )) {
873 AscPrintHelpExit(argv[0]);
874 }
875 if(( length >= 2 ) && ( strncmp(argv[i],"-H",2) == 0 )) {
876 AscPrintHelpExit(argv[0]);
877 }
878 if(( length >= 4 ) && ( strncmp(argv[i],"help",4) == 0 )) {
879 AscPrintHelpExit(argv[0]);
880 }
881
882 if( argv[i][0] == '-' ) {
883 flag = -1;
884 } else if( argv[i][0] == '+' ) {
885 flag = 1;
886 } else {
887 flag = 0;
888 }
889
890 if(( length == 2 ) && ( flag != 0 )) {
891 switch( argv[i][1] ) {
892 case 'd':
893 /* '-d' turns on scanner debugging (if ascend was built with it)
894 * '+d' turns off scanner debugging [default]
895 */
896 if( flag == -1 ) {
897 #ifdef ZZ_DEBUG
898 zz_debug = 1;
899 } else {
900 zz_debug = 0;
901 #else
902 FPRINTF(ASCERR, "Sorry, %s wasn't compiled with %s defined.\n",
903 argv[0], "ZZ_DEBUG");
904 #endif /* ZZ_DEBUG */
905 }
906 break;
907 case 's':
908 /* '-s' turns on compiler optimizations [default]
909 * '+s' turns off compiler optimizations
910 */
911 if( flag == -1 ) {
912 g_interface_simplify_relations = 1;
913 } else {
914 g_interface_simplify_relations = 0;
915 }
916 break;
917 case 't':
918 /* '-t' turns on timing of compiler optimizations
919 * '+t' turns off timing of compiler optimizations [default]
920 */
921 if( flag == 0 ) {
922 g_compiler_timing = 1;
923 } else {
924 g_compiler_timing = 0;
925 }
926 break;
927 case 'c':
928 case 'g':
929 fprintf(ASCERR, "WARNING! Obsolete ASCEND option \"%s\"\n", argv[i]);
930 break;
931 default:
932 /* unknown ASCEND option, pass it on to Tcl
933 */
934 argv[++new_argc] = argv[i];
935 break;
936 }
937 } else {
938 /* unknown ASCEND option, pass it on to Tcl
939 */
940 argv[++new_argc] = argv[i];
941 }
942 }
943
944 /*
945 * Make command-line arguments available in the Tcl variables "argc"
946 * and "argv".
947 */
948 args = Tcl_Merge(new_argc, (argv+1));
949 Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
950 ckfree(args);
951 sprintf(buf, "%d", new_argc);
952 Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
953 Tcl_SetVar(interp, "argv0", argv[0], TCL_GLOBAL_ONLY);
954
955 return TCL_OK;
956 }
957
958
959 /*
960 * AscPrintHelpExit(invoke_name)
961 * CONST char *invoke_name;
962 *
963 * Print a help message and exit. Use invoke_name as the name of
964 * the binary
965 */
966 static
967 void AscPrintHelpExit(CONST char *invoke_name)
968 {
969 PRINTF("usage: %s [options]\n"
970 "\n"
971 "where options include [default value]:\n"
972 " -h print this message\n"
973 " -/+d turn on/off yacc debugging [off]\n"
974 " -/+s turn on/off compiler optimizations [on]\n"
975 " -/+t turn on/off timing of compiler operations [off]\n",
976 invoke_name);
977 Tcl_Exit(0); /* Show this help message and leave */
978 }
979
980
981 /*
982 * AscTrap(sig)
983 * int sig;
984 *
985 * Function to call when we receive an interrupt.
986 */
987 static
988 void AscTrap(int sig)
989 {
990 putchar('\n');
991 Asc_Panic(sig, "AscTrap", "Caught Signal: %d", sig);
992 }
993
994
995 /*
996 * See this file's header for documentation.
997 */
998 int Asc_LoadWin(ClientData cdata, Tcl_Interp *interp,
999 int argc, CONST84 char *argv[])
1000 {
1001 (void)cdata; /* stop gcc whine about unused parameter */
1002 (void)argv; /* stop gcc whine about unused parameter */
1003
1004 if ( argc != 1 ) {
1005 FPRINTF(stderr,"call is: ascloadwin <no args> \n");
1006 return TCL_ERROR;
1007 }
1008 if (g_interfacever) {
1009 Tcl_SetResult(interp, "1", TCL_STATIC);
1010 } else {
1011 Tcl_SetResult(interp, "0", TCL_STATIC);
1012 }
1013 return TCL_OK;
1014 }
1015
1016
1017 /*
1018 *----------------------------------------------------------------------
1019 *----------------------------------------------------------------------
1020 * The following StdinProc() and Prompt() are from tkMain.c in
1021 * the Tk4.1 distribution (and did not change in Tk8.0).
1022 *----------------------------------------------------------------------
1023 *----------------------------------------------------------------------
1024 */
1025 /*
1026 *----------------------------------------------------------------------
1027 *
1028 * StdinProc --
1029 *
1030 * This procedure is invoked by the event dispatcher whenever
1031 * standard input becomes readable. It grabs the next line of
1032 * input characters, adds them to a command being assembled, and
1033 * executes the command if it's complete.
1034 *
1035 * Results:
1036 * None.
1037 *
1038 * Side effects:
1039 * Could be almost arbitrary, depending on the command that's
1040 * typed.
1041 *
1042 *----------------------------------------------------------------------
1043 */
1044
1045 /* ARGSUSED */
1046 static void
1047 StdinProc(clientData, mask)
1048 ClientData clientData; /* Not used. */
1049 int mask; /* Not used. */
1050 {
1051 static int gotPartial = 0;
1052 char *cmd;
1053 int code, count;
1054 Tcl_Channel chan = (Tcl_Channel) clientData;
1055
1056 Tcl_Interp *interp = g_interp; /* use a local copy of the
1057 * global tcl interpreter
1058 */
1059
1060 (void)clientData; /* stop gcc whine about unused parameter */
1061 (void)mask; /* stop gcc whine about unused parameter */
1062
1063 count = Tcl_Gets(chan, &g_line);
1064
1065 if (count < 0) {
1066 if (!gotPartial) {
1067 if (tty) {
1068 return;
1069 } else {
1070 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
1071 }
1072 return;
1073 } else {
1074 count = 0;
1075 }
1076 }
1077
1078 (void) Tcl_DStringAppend(&g_command, Tcl_DStringValue(&g_line), -1);
1079 cmd = Tcl_DStringAppend(&g_command, "\n", -1);
1080 Tcl_DStringFree(&g_line);
1081
1082 if (!Tcl_CommandComplete(cmd)) {
1083 gotPartial = 1;
1084 goto prompt;
1085 }
1086 gotPartial = 0;
1087
1088 /*
1089 * Disable the stdin channel handler while evaluating the command;
1090 * otherwise if the command re-enters the event loop we might
1091 * process commands from stdin before the current command is
1092 * finished. Among other things, this will trash the text of the
1093 * command being evaluated.
1094 */
1095
1096 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
1097 code = Tcl_RecordAndEval(interp, cmd, TCL_EVAL_GLOBAL);
1098 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
1099 (ClientData) chan);
1100 Tcl_DStringFree(&g_command);
1101 if (*interp->result != 0) {
1102 if ((code != TCL_OK) || (tty)) {
1103 /*
1104 * The statement below used to call "printf", but that resulted
1105 * in core dumps under Solaris 2.3 if the result was very long.
1106 *
1107 * NOTE: This probably will not work under Windows either.
1108 */
1109
1110 puts(interp->result);
1111 }
1112 }
1113
1114 /*
1115 * Output a prompt.
1116 */
1117
1118 prompt:
1119 if (tty) {
1120 Prompt(interp, gotPartial);
1121 }
1122 Tcl_ResetResult(interp);
1123 }
1124
1125 /*
1126 *----------------------------------------------------------------------
1127 *
1128 * Prompt --
1129 *
1130 * Issue a prompt on standard output, or invoke a script
1131 * to issue the prompt.
1132 *
1133 * Results:
1134 * None.
1135 *
1136 * Side effects:
1137 * A prompt gets output, and a Tcl script may be evaluated
1138 * in interp.
1139 *
1140 *----------------------------------------------------------------------
1141 */
1142
1143 static void
1144 Prompt(interp, partial)
1145 Tcl_Interp *interp; /* Interpreter to use for prompting. */
1146 int partial; /* Non-zero means there already
1147 * exists a partial command, so use
1148 * the secondary prompt. */
1149 {
1150 CONST84 char *promptCmd;
1151 int code;
1152 Tcl_Channel outChannel, errChannel;
1153 CONST84 char *subPrompt;
1154
1155 errChannel = Tcl_GetChannel(interp, "stderr", NULL);
1156
1157 subPrompt = (partial ? "tcl_prompt2" : "tcl_prompt1");
1158 promptCmd = Tcl_GetVar(interp, subPrompt, TCL_GLOBAL_ONLY);
1159 if (promptCmd == NULL) {
1160 defaultPrompt:
1161
1162 /*
1163 * We must check that outChannel is a real channel - it
1164 * is possible that someone has transferred stdout out of
1165 * this interpreter with "interp transfer".
1166 */
1167
1168 outChannel = Tcl_GetChannel(interp, "stdout", NULL);
1169 if (outChannel != (Tcl_Channel) NULL) {
1170 if (!partial) {
1171 Tcl_Write(outChannel, "AscendIV% ", 10);
1172 } else {
1173 Tcl_Write(outChannel, "more? ", 6);
1174 }
1175 }
1176 } else {
1177 code = Tcl_Eval(interp, promptCmd);
1178 if (code != TCL_OK) {
1179 Tcl_AddErrorInfo(interp,
1180 "\n (script that generates prompt)");
1181 /*
1182 * We must check that errChannel is a real channel - it
1183 * is possible that someone has transferred stderr out of
1184 * this interpreter with "interp transfer".
1185 */
1186
1187 errChannel = Tcl_GetChannel(interp, "stderr", NULL);
1188 if (errChannel != (Tcl_Channel) NULL) {
1189 Tcl_Write(errChannel, interp->result, -1);
1190 Tcl_Write(errChannel, "\n", 1);
1191 }
1192 goto defaultPrompt;
1193 }
1194 }
1195 outChannel = Tcl_GetChannel(interp, "stdout", NULL);
1196 if (outChannel != (Tcl_Channel) NULL) {
1197 Tcl_Flush(outChannel);
1198 }
1199 }
1200
1201 /*
1202 *----------------------------------------------------------------------
1203 * Tom Epperly's Malloc Debugger
1204 *----------------------------------------------------------------------
1205 */
1206 #ifdef DEBUG_MALLOC
1207 static void InitDebugMalloc(void)
1208 {
1209 union dbmalloptarg m;
1210 m.str = NULL;
1211 m.i = 0;
1212 dbmallopt(MALLOC_CKDATA,&m);
1213 }
1214
1215 int Asc_DebugMallocCmd(ClientData cdata, Tcl_Interp *interp,
1216 int argc, CONST84 char *argv[])
1217 {
1218 union dbmalloptarg m;
1219
1220 if ( argc != 2 ) {
1221 Tcl_SetResult(interp, "wrong # args : Usage __dbmalloc ?on?off?",
1222 TCL_STATIC);
1223 return TCL_ERROR;
1224 }
1225 m.str = NULL;
1226 if (strcmp(argv[1],"on")==0) {
1227 m.i = 1;
1228 } else if (strcmp(argv[1],"off")==0) {
1229 m.i = 0;
1230 } else {
1231 Tcl_SetResult(interp, "incorrect args : should be \"on\" or \"off\"",
1232 TCL_STATIC);
1233 return TCL_ERROR;
1234 }
1235 dbmallopt(MALLOC_CKDATA,&m); /* turn on str* mem* and b* checking */
1236 return TCL_OK;
1237 }
1238 #endif /* DEBUG_MALLOC */
1239
1240 #ifdef __WIN32__
1241 /*
1242 *-------------------------------------------------------------------------
1243 *
1244 * setargv --
1245 *
1246 * Parse the Windows command line string into argc/argv. Done here
1247 * because we don't trust the builtin argument parser in crt0.
1248 * Windows applications are responsible for breaking their command
1249 * line into arguments.
1250 *
1251 * 2N backslashes + quote -> N backslashes + begin quoted string
1252 * 2N + 1 backslashes + quote -> literal
1253 * N backslashes + non-quote -> literal
1254 * quote + quote in a quoted string -> single quote
1255 * quote + quote not in quoted string -> empty string
1256 * quote -> begin quoted string
1257 *
1258 * Results:
1259 * Fills argcPtr with the number of arguments and argvPtr with the
1260 * array of arguments.
1261 *
1262 * Side effects:
1263 * Memory allocated.
1264 *
1265 * This function is from the Tk 8.0 distribution. See win/winMain.c in
1266 * their sources.
1267 *
1268 *--------------------------------------------------------------------------
1269 */
1270 static void
1271 setargv(argcPtr, argvPtr)
1272 int *argcPtr; /* Filled with number of argument strings. */
1273 char ***argvPtr; /* Filled with argument strings (malloc'd). */
1274 {
1275 char *cmdLine, *p, *arg, *argSpace;
1276 char **argv;
1277 int argc, size, inquote, copy, slashes;
1278
1279 cmdLine = GetCommandLine();
1280
1281 /*
1282 * Precompute an overly pessimistic guess at the number of arguments
1283 * in the command line by counting non-space spans.
1284 */
1285
1286 size = 2;
1287 for (p = cmdLine; *p != '\0'; p++) {
1288 if (isspace(*p)) {
1289 size++;
1290 while (isspace(*p)) {
1291 p++;
1292 }
1293 if (*p == '\0') {
1294 break;
1295 }
1296 }
1297 }
1298 argSpace = (char *) ckalloc((unsigned) (size * sizeof(char *)
1299 + strlen(cmdLine) + 1));
1300 argv = (char **) argSpace;
1301 argSpace += size * sizeof(char *);
1302 size--;
1303
1304 p = cmdLine;
1305 for (argc = 0; argc < size; argc++) {
1306 argv[argc] = arg = argSpace;
1307 while (isspace(*p)) {
1308 p++;
1309 }
1310 if (*p == '\0') {
1311 break;
1312 }
1313
1314 inquote = 0;
1315 slashes = 0;
1316 while (1) {
1317 copy = 1;
1318 while (*p == '\\') {
1319 slashes++;
1320 p++;
1321 }
1322 if (*p == '"') {
1323 if ((slashes & 1) == 0) {
1324 copy = 0;
1325 if ((inquote) && (p[1] == '"')) {
1326 p++;
1327 copy = 1;
1328 } else {
1329 inquote = !inquote;
1330 }
1331 }
1332 slashes >>= 1;
1333 }
1334
1335 while (slashes) {
1336 *arg = '\\';
1337 arg++;
1338 slashes--;
1339 }
1340
1341 if ((*p == '\0') || (!inquote && isspace(*p))) {
1342 break;
1343 }
1344 if (copy != 0) {
1345 *arg = *p;
1346 arg++;
1347 }
1348 p++;
1349 }
1350 *arg = '\0';
1351 argSpace = arg + 1;
1352 }
1353 argv[argc] = NULL;
1354
1355 *argcPtr = argc;
1356 *argvPtr = argv;
1357 }
1358
1359 #endif /* __WIN32__ */

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