os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacBOAAppInit.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /* 
     2  * tclMacBOAAppInit.c --
     3  *
     4  *	Provides a version of the Tcl_AppInit procedure for a 
     5  *      Macintosh Background Only Application.
     6  *
     7  * Copyright (c) 1997 Sun Microsystems, Inc.
     8  *
     9  * See the file "license.terms" for information on usage and redistribution
    10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  *
    12  * RCS: @(#) $Id: tclMacBOAAppInit.c,v 1.5 2001/06/14 00:48:51 dgp Exp $
    13  */
    14 
    15 #include "tcl.h"
    16 #include "tclInt.h"
    17 #include "tclPort.h"
    18 #include "tclMac.h"
    19 #include "tclMacInt.h"
    20 #include <Fonts.h>
    21 #include <Windows.h>
    22 #include <Dialogs.h>
    23 #include <Menus.h>
    24 #include <Aliases.h>
    25 #include <LowMem.h>
    26 
    27 #include <AppleEvents.h>
    28 #include <SegLoad.h>
    29 #include <ToolUtils.h>
    30 
    31 #if defined(THINK_C)
    32 #   include <console.h>
    33 #elif defined(__MWERKS__)
    34 #   include <SIOUX.h>
    35 short InstallConsole _ANSI_ARGS_((short fd));
    36 #endif
    37 
    38 void TkMacInitAppleEvents(Tcl_Interp *interp);
    39 int HandleHighLevelEvents(EventRecord *eventPtr);	
    40 
    41 #ifdef TCL_TEST
    42 EXTERN int		TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));
    43 EXTERN int		Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
    44 #endif /* TCL_TEST */
    45 
    46 /*
    47  * Forward declarations for procedures defined later in this file:
    48  */
    49 
    50 static int		MacintoshInit _ANSI_ARGS_((void));
    51 
    52 /*
    53  *----------------------------------------------------------------------
    54  *
    55  * main --
    56  *
    57  *	Main program for tclsh.  This file can be used as a prototype
    58  *	for other applications using the Tcl library.
    59  *
    60  * Results:
    61  *	None. This procedure never returns (it exits the process when
    62  *	it's done.
    63  *
    64  * Side effects:
    65  *	This procedure initializes the Macintosh world and then 
    66  *	calls Tcl_Main.  Tcl_Main will never return except to exit.
    67  *
    68  *----------------------------------------------------------------------
    69  */
    70 
    71 void
    72 main(
    73     int argc,				/* Number of arguments. */
    74     char **argv)			/* Array of argument strings. */
    75 {
    76     char *newArgv[3];
    77     
    78     if (MacintoshInit()  != TCL_OK) {
    79 	Tcl_Exit(1);
    80     }
    81 
    82     argc = 2;
    83     newArgv[0] = "tclsh";
    84     newArgv[1] = "bgScript.tcl";
    85     newArgv[2] = NULL;
    86     Tcl_Main(argc, newArgv, Tcl_AppInit);
    87 }
    88 
    89 /*
    90  *----------------------------------------------------------------------
    91  *
    92  * Tcl_AppInit --
    93  *
    94  *	This procedure performs application-specific initialization.
    95  *	Most applications, especially those that incorporate additional
    96  *	packages, will have their own version of this procedure.
    97  *
    98  * Results:
    99  *	Returns a standard Tcl completion code, and leaves an error
   100  *	message in the interp's result if an error occurs.
   101  *
   102  * Side effects:
   103  *	Depends on the startup script.
   104  *
   105  *----------------------------------------------------------------------
   106  */
   107 
   108 int
   109 Tcl_AppInit(
   110     Tcl_Interp *interp)		/* Interpreter for application. */
   111 {
   112     Tcl_Channel tempChan;
   113     
   114     if (Tcl_Init(interp) == TCL_ERROR) {
   115 	return TCL_ERROR;
   116     }
   117 
   118 #ifdef TCL_TEST
   119     if (Tcltest_Init(interp) == TCL_ERROR) {
   120 	return TCL_ERROR;
   121     }
   122     Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
   123             (Tcl_PackageInitProc *) NULL);
   124     if (TclObjTest_Init(interp) == TCL_ERROR) {
   125 	return TCL_ERROR;
   126     }
   127 #endif /* TCL_TEST */
   128 
   129     /*
   130      * Call the init procedures for included packages.  Each call should
   131      * look like this:
   132      *
   133      * if (Mod_Init(interp) == TCL_ERROR) {
   134      *     return TCL_ERROR;
   135      * }
   136      *
   137      * where "Mod" is the name of the module.
   138      */
   139 
   140     /*
   141      * Call Tcl_CreateCommand for application-specific commands, if
   142      * they weren't already created by the init procedures called above.
   143      * Each call would loo like this:
   144      *
   145      * Tcl_CreateCommand(interp, "tclName", CFuncCmd, NULL, NULL);
   146      */
   147 
   148     /*
   149      * Specify a user-specific startup script to invoke if the application
   150      * is run interactively.  On the Mac we can specifiy either a TEXT resource
   151      * which contains the script or the more UNIX like file location
   152      * may also used.  (I highly recommend using the resource method.)
   153      */
   154 
   155     Tcl_SetVar(interp, "tcl_rcRsrcName", "tclshrc", TCL_GLOBAL_ONLY);
   156 
   157     /* Tcl_SetVar(interp, "tcl_rcFileName", "~/.tclshrc", TCL_GLOBAL_ONLY); */
   158 
   159     /*
   160      * We have to support at least the quit Apple Event. 
   161      */
   162     
   163     TkMacInitAppleEvents(interp);
   164     
   165     /* 
   166      * Open a file channel to put stderr, stdin, stdout... 
   167      */
   168     
   169     tempChan = Tcl_OpenFileChannel(interp, ":temp.in", "a+", 0);
   170     Tcl_SetStdChannel(tempChan,TCL_STDIN);
   171     Tcl_RegisterChannel(interp, tempChan);
   172     Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr");
   173     Tcl_SetChannelOption(NULL, tempChan, "-buffering", "line");
   174 
   175     tempChan = Tcl_OpenFileChannel(interp, ":temp.out", "a+", 0);
   176     Tcl_SetStdChannel(tempChan,TCL_STDOUT);
   177     Tcl_RegisterChannel(interp, tempChan);
   178     Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr");
   179     Tcl_SetChannelOption(NULL, tempChan, "-buffering", "line");
   180 
   181     tempChan = Tcl_OpenFileChannel(interp, ":temp.err", "a+", 0);
   182     Tcl_SetStdChannel(tempChan,TCL_STDERR);
   183     Tcl_RegisterChannel(interp, tempChan);
   184     Tcl_SetChannelOption(NULL, tempChan, "-translation", "cr");
   185     Tcl_SetChannelOption(NULL, tempChan, "-buffering", "none");
   186    
   187     
   188     return TCL_OK;
   189 }
   190 
   191 /*
   192  *----------------------------------------------------------------------
   193  *
   194  * MacintoshInit --
   195  *
   196  *	This procedure calls initalization routines to set up a simple
   197  *	console on a Macintosh.  This is necessary as the Mac doesn't
   198  *	have a stdout & stderr by default.
   199  *
   200  * Results:
   201  *	Returns TCL_OK if everything went fine.  If it didn't the 
   202  *	application should probably fail.
   203  *
   204  * Side effects:
   205  *	Inits the appropiate console package.
   206  *
   207  *----------------------------------------------------------------------
   208  */
   209 
   210 static int
   211 MacintoshInit()
   212 {
   213     THz theZone = GetZone();
   214     SysEnvRec   sys;
   215 
   216     
   217     /*
   218      * There is a bug in systems earlier that 7.5.5, where a second BOA will
   219      * get a corrupted heap.  This is the fix from TechNote 1070
   220      */
   221      
   222     SysEnvirons(1, &sys);
   223 
   224     if (sys.systemVersion < 0x0755)
   225     {
   226         if ( LMGetHeapEnd() != theZone->bkLim) {
   227             LMSetHeapEnd(theZone->bkLim);
   228         }
   229     }
   230     
   231 #if GENERATING68K && !GENERATINGCFM
   232     SetApplLimit(GetApplLimit() - (TCL_MAC_68K_STACK_GROWTH));
   233 #endif
   234     MaxApplZone();
   235 
   236     InitGraf((Ptr)&qd.thePort);
   237     		    
   238     /* No problems with initialization */
   239     Tcl_MacSetEventProc(HandleHighLevelEvents);
   240 
   241     return TCL_OK;
   242 }
   243 
   244 int
   245 HandleHighLevelEvents(
   246     EventRecord *eventPtr)
   247 {
   248     int eventFound = false;
   249     
   250     if (eventPtr->what == kHighLevelEvent) {
   251 	AEProcessAppleEvent(eventPtr);
   252         eventFound = true;
   253     } else if (eventPtr->what == nullEvent) {
   254         eventFound = true;
   255     }
   256     return eventFound;    
   257 }