os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacBOAMain.c
First public contribution.
4 * Main program for Macintosh Background Only Application shells.
6 * Copyright (c) 1997 Sun Microsystems, Inc.
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * RCS: @(#) $Id: tclMacBOAMain.c,v 1.4 2001/12/28 23:36:31 dgp Exp $
16 #include "tclMacInt.h"
17 #include <Resources.h>
18 #include <Notification.h>
22 * This variable is used to get out of the modal loop of the
23 * notification manager.
26 int NotificationIsDone = 0;
29 * The following code ensures that tclLink.c is linked whenever
30 * Tcl is linked. Without this code there's no reference to the
31 * code in that file from anywhere in Tcl, so it may not be
32 * linked into the application.
35 EXTERN int Tcl_LinkVar();
36 int (*tclDummyLinkVarPtr)() = Tcl_LinkVar;
39 * Declarations for various library procedures and variables (don't want
40 * to include tclPort.h here, because people might copy this file out of
41 * the Tcl source directory to make their own modified versions).
42 * Note: "exit" should really be declared here, but there's no way to
43 * declare it without causing conflicts with other definitions elsewher
44 * on some systems, so it's better just to leave it out.
47 extern int isatty _ANSI_ARGS_((int fd));
48 extern char * strcpy _ANSI_ARGS_((char *dst, CONST char *src));
50 static Tcl_Interp *interp; /* Interpreter for application. */
53 * Forward references for procedures defined later in this file:
56 void TclMacDoNotification(char *mssg);
57 void TclMacNotificationResponse(NMRecPtr nmRec);
58 int Tcl_MacBGNotifyObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const *objv);
62 *----------------------------------------------------------------------
66 * Main program for tclsh and most other Tcl-based applications.
69 * None. This procedure never returns (it exits the process when
73 * This procedure initializes the Tk world and then starts
74 * interpreting commands; almost anything could happen, depending
75 * on the script being interpreted.
77 *----------------------------------------------------------------------
81 Tcl_Main(argc, argv, appInitProc)
82 int argc; /* Number of arguments. */
83 char **argv; /* Array of argument strings. */
84 Tcl_AppInitProc *appInitProc;
85 /* Application-specific initialization
86 * procedure to call after most
87 * initialization but before starting to
88 * execute commands. */
90 Tcl_Obj *prompt1NamePtr = NULL;
91 Tcl_Obj *prompt2NamePtr = NULL;
92 Tcl_Obj *commandPtr = NULL;
93 char buffer[1000], *args, *fileName;
97 Tcl_FindExecutable(argv[0]);
98 interp = Tcl_CreateInterp();
99 Tcl_InitMemory(interp);
102 * Make command-line arguments available in the Tcl variables "argc"
103 * and "argv". If the first argument doesn't start with a "-" then
104 * strip it off and use it as the name of a script file to process.
108 if ((argc > 1) && (argv[1][0] != '-')) {
113 args = Tcl_Merge(argc-1, argv+1);
114 Tcl_SetVar(interp, "argv", args, TCL_GLOBAL_ONLY);
116 TclFormatInt(buffer, argc-1);
117 Tcl_SetVar(interp, "argc", buffer, TCL_GLOBAL_ONLY);
118 Tcl_SetVar(interp, "argv0", (fileName != NULL) ? fileName : argv[0],
122 * Set the "tcl_interactive" variable.
126 Tcl_SetVar(interp, "tcl_interactive",
127 ((fileName == NULL) && tty) ? "1" : "0", TCL_GLOBAL_ONLY);
130 * Invoke application-specific initialization.
133 if ((*appInitProc)(interp) != TCL_OK) {
136 Tcl_DStringInit(&errStr);
137 Tcl_DStringAppend(&errStr,
138 "application-specific initialization failed: \n", -1);
139 Tcl_DStringAppend(&errStr, Tcl_GetStringResult(interp), -1);
140 Tcl_DStringAppend(&errStr, "\n", 1);
141 TclMacDoNotification(Tcl_DStringValue(&errStr));
142 Tcl_DStringFree(&errStr);
147 * Install the BGNotify command:
150 if ( Tcl_CreateObjCommand(interp, "bgnotify", Tcl_MacBGNotifyObjCmd, NULL,
151 (Tcl_CmdDeleteProc *) NULL) == NULL) {
156 * If a script file was specified then just source that file
157 * and quit. In this Mac BG Application version, we will try the
158 * resource fork first, then the file system second...
161 if (fileName != NULL) {
165 strcpy((char *) resName + 1, fileName);
166 resName[0] = strlen(fileName);
167 resource = GetNamedResource('TEXT',resName);
168 if (resource != NULL) {
169 code = Tcl_MacEvalResource(interp, fileName, -1, NULL);
171 code = Tcl_EvalFile(interp, fileName);
174 if (code != TCL_OK) {
177 Tcl_DStringInit(&errStr);
178 Tcl_DStringAppend(&errStr, " Error sourcing resource or file: ", -1);
179 Tcl_DStringAppend(&errStr, fileName, -1);
180 Tcl_DStringAppend(&errStr, "\n\nError was: ", -1);
181 Tcl_DStringAppend(&errStr, Tcl_GetStringResult(interp), -1);
182 TclMacDoNotification(Tcl_DStringValue(&errStr));
183 Tcl_DStringFree(&errStr);
190 * Rather than calling exit, invoke the "exit" command so that
191 * users can replace "exit" with some other command to do additional
192 * cleanup on exit. The Tcl_Eval call should never return.
196 if (commandPtr != NULL) {
197 Tcl_DecrRefCount(commandPtr);
199 if (prompt1NamePtr != NULL) {
200 Tcl_DecrRefCount(prompt1NamePtr);
202 if (prompt2NamePtr != NULL) {
203 Tcl_DecrRefCount(prompt2NamePtr);
205 sprintf(buffer, "exit %d", exitCode);
206 Tcl_Eval(interp, buffer);
209 /*----------------------------------------------------------------------
211 * TclMacDoNotification --
213 * This posts an error message using the Notification manager.
216 * Post a Notification Manager dialog.
221 *----------------------------------------------------------------------
224 TclMacDoNotification(mssg)
228 EventRecord *theEvent = NULL;
232 errorNot.qType = nmType;
235 errorNot.nmSound = (Handle) -1;
237 for ( ptr = mssg; *ptr != '\0'; ptr++) {
244 errorNot.nmStr = (StringPtr) mssg;
246 errorNot.nmResp = NewNMProc(TclMacNotificationResponse);
247 errorNot.nmRefCon = SetCurrentA5();
249 NotificationIsDone = 0;
252 * Cycle while waiting for the user to click on the
253 * notification box. Don't take any events off the event queue,
254 * since we want Tcl to do this but we want to block till the notification
255 * has been handled...
258 err = NMInstall(&errorNot);
260 while (!NotificationIsDone) {
261 WaitNextEvent(0, theEvent, 20, NULL);
266 p2cstr((unsigned char *) mssg);
270 TclMacNotificationResponse(nmRec)
275 curA5 = SetCurrentA5();
276 SetA5(nmRec->nmRefCon);
278 NotificationIsDone = 1;
285 Tcl_MacBGNotifyObjCmd(clientData, interp, objc, objv)
286 ClientData clientData;
293 resultPtr = Tcl_GetObjResult(interp);
296 Tcl_WrongNumArgs(interp, 1, objv, "message");
300 TclMacDoNotification(Tcl_GetString(objv[1]));