sl@0: /* sl@0: * tclMacBOAAppInit.c -- sl@0: * sl@0: * Provides a version of the Tcl_AppInit procedure for a sl@0: * Macintosh Background Only Application. sl@0: * sl@0: * Copyright (c) 1997 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclMacBOAAppInit.c,v 1.5 2001/06/14 00:48:51 dgp Exp $ sl@0: */ sl@0: sl@0: #include "tcl.h" sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: #include "tclMac.h" sl@0: #include "tclMacInt.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if defined(THINK_C) sl@0: # include sl@0: #elif defined(__MWERKS__) sl@0: # include sl@0: short InstallConsole _ANSI_ARGS_((short fd)); sl@0: #endif sl@0: sl@0: void TkMacInitAppleEvents(Tcl_Interp *interp); sl@0: int HandleHighLevelEvents(EventRecord *eventPtr); sl@0: sl@0: #ifdef TCL_TEST sl@0: EXTERN int TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp)); sl@0: EXTERN int Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp)); sl@0: #endif /* TCL_TEST */ sl@0: sl@0: /* sl@0: * Forward declarations for procedures defined later in this file: sl@0: */ sl@0: sl@0: static int MacintoshInit _ANSI_ARGS_((void)); sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * main -- sl@0: * sl@0: * Main program for tclsh. This file can be used as a prototype sl@0: * for other applications using the Tcl library. sl@0: * sl@0: * Results: sl@0: * None. This procedure never returns (it exits the process when sl@0: * it's done. sl@0: * sl@0: * Side effects: sl@0: * This procedure initializes the Macintosh world and then sl@0: * calls Tcl_Main. Tcl_Main will never return except to exit. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: void sl@0: main( sl@0: int argc, /* Number of arguments. */ sl@0: char **argv) /* Array of argument strings. */ sl@0: { sl@0: char *newArgv[3]; sl@0: sl@0: if (MacintoshInit() != TCL_OK) { sl@0: Tcl_Exit(1); sl@0: } sl@0: sl@0: argc = 2; sl@0: newArgv[0] = "tclsh"; sl@0: newArgv[1] = "bgScript.tcl"; sl@0: newArgv[2] = NULL; sl@0: Tcl_Main(argc, newArgv, Tcl_AppInit); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_AppInit -- sl@0: * sl@0: * This procedure performs application-specific initialization. sl@0: * Most applications, especially those that incorporate additional sl@0: * packages, will have their own version of this procedure. sl@0: * sl@0: * Results: sl@0: * Returns a standard Tcl completion code, and leaves an error sl@0: * message in the interp's result if an error occurs. sl@0: * sl@0: * Side effects: sl@0: * Depends on the startup script. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: Tcl_AppInit( sl@0: Tcl_Interp *interp) /* Interpreter for application. */ sl@0: { sl@0: Tcl_Channel tempChan; sl@0: sl@0: if (Tcl_Init(interp) == TCL_ERROR) { sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: #ifdef TCL_TEST sl@0: if (Tcltest_Init(interp) == TCL_ERROR) { sl@0: return TCL_ERROR; sl@0: } sl@0: Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init, sl@0: (Tcl_PackageInitProc *) NULL); sl@0: if (TclObjTest_Init(interp) == TCL_ERROR) { sl@0: return TCL_ERROR; sl@0: } sl@0: #endif /* TCL_TEST */ sl@0: sl@0: /* sl@0: * Call the init procedures for included packages. Each call should sl@0: * look like this: sl@0: * sl@0: * if (Mod_Init(interp) == TCL_ERROR) { sl@0: * return TCL_ERROR; sl@0: * } sl@0: * sl@0: * where "Mod" is the name of the module. sl@0: */ sl@0: sl@0: /* sl@0: * Call Tcl_CreateCommand for application-specific commands, if sl@0: * they weren't already created by the init procedures called above. sl@0: * Each call would loo like this: sl@0: * sl@0: * Tcl_CreateCommand(interp, "tclName", CFuncCmd, NULL, NULL); sl@0: */ sl@0: sl@0: /* sl@0: * Specify a user-specific startup script to invoke if the application sl@0: * is run interactively. On the Mac we can specifiy either a TEXT resource sl@0: * which contains the script or the more UNIX like file location sl@0: * may also used. (I highly recommend using the resource method.) sl@0: */ sl@0: sl@0: Tcl_SetVar(interp, "tcl_rcRsrcName", "tclshrc", TCL_GLOBAL_ONLY); sl@0: sl@0: /* Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY); */ sl@0: sl@0: /* sl@0: * We have to support at least the quit Apple Event. sl@0: */ sl@0: sl@0: TkMacInitAppleEvents(interp); sl@0: sl@0: /* sl@0: * Open a file channel to put stderr, stdin, stdout... sl@0: */ sl@0: sl@0: tempChan = Tcl_OpenFileChannel(interp, ":temp.in", "a+", 0); sl@0: Tcl_SetStdChannel(tempChan,TCL_STDIN); sl@0: Tcl_RegisterChannel(interp, tempChan); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr"); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-buffering", "line"); sl@0: sl@0: tempChan = Tcl_OpenFileChannel(interp, ":temp.out", "a+", 0); sl@0: Tcl_SetStdChannel(tempChan,TCL_STDOUT); sl@0: Tcl_RegisterChannel(interp, tempChan); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr"); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-buffering", "line"); sl@0: sl@0: tempChan = Tcl_OpenFileChannel(interp, ":temp.err", "a+", 0); sl@0: Tcl_SetStdChannel(tempChan,TCL_STDERR); sl@0: Tcl_RegisterChannel(interp, tempChan); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr"); sl@0: Tcl_SetChannelOption(NULL, tempChan, "-buffering", "none"); sl@0: sl@0: sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * MacintoshInit -- sl@0: * sl@0: * This procedure calls initalization routines to set up a simple sl@0: * console on a Macintosh. This is necessary as the Mac doesn't sl@0: * have a stdout & stderr by default. sl@0: * sl@0: * Results: sl@0: * Returns TCL_OK if everything went fine. If it didn't the sl@0: * application should probably fail. sl@0: * sl@0: * Side effects: sl@0: * Inits the appropiate console package. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static int sl@0: MacintoshInit() sl@0: { sl@0: THz theZone = GetZone(); sl@0: SysEnvRec sys; sl@0: sl@0: sl@0: /* sl@0: * There is a bug in systems earlier that 7.5.5, where a second BOA will sl@0: * get a corrupted heap. This is the fix from TechNote 1070 sl@0: */ sl@0: sl@0: SysEnvirons(1, &sys); sl@0: sl@0: if (sys.systemVersion < 0x0755) sl@0: { sl@0: if ( LMGetHeapEnd() != theZone->bkLim) { sl@0: LMSetHeapEnd(theZone->bkLim); sl@0: } sl@0: } sl@0: sl@0: #if GENERATING68K && !GENERATINGCFM sl@0: SetApplLimit(GetApplLimit() - (TCL_MAC_68K_STACK_GROWTH)); sl@0: #endif sl@0: MaxApplZone(); sl@0: sl@0: InitGraf((Ptr)&qd.thePort); sl@0: sl@0: /* No problems with initialization */ sl@0: Tcl_MacSetEventProc(HandleHighLevelEvents); sl@0: sl@0: return TCL_OK; sl@0: } sl@0: sl@0: int sl@0: HandleHighLevelEvents( sl@0: EventRecord *eventPtr) sl@0: { sl@0: int eventFound = false; sl@0: sl@0: if (eventPtr->what == kHighLevelEvent) { sl@0: AEProcessAppleEvent(eventPtr); sl@0: eventFound = true; sl@0: } else if (eventPtr->what == nullEvent) { sl@0: eventFound = true; sl@0: } sl@0: return eventFound; sl@0: }