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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [insight/] [tcl/] [mac/] [tclMacLibrary.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tclMacLibrary.c --
3
 *
4
 *      This file should be included in Tcl extensions that want to
5
 *      automatically oepn their resource forks when the code is linked.
6
 *      These routines should not be exported but should be compiled
7
 *      locally by each fragment.  Many thanks to Jay Lieske
8
 *      <lieske@princeton.edu> who provide an initial version of this
9
 *      file.
10
 *
11
 * Copyright (c) 1996 Sun Microsystems, Inc.
12
 *
13
 * See the file "license.terms" for information on usage and redistribution
14
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
 *
16
 * RCS: @(#) $Id: tclMacLibrary.c,v 1.1.1.1 2002-01-16 10:25:30 markom Exp $
17
 */
18
 
19
/*
20
 * Here is another place that we are using the old routine names...
21
 */
22
 
23
#define OLDROUTINENAMES 1
24
 
25
#include <CodeFragments.h>
26
#include <Errors.h>
27
#include <Resources.h>
28
#include <Strings.h>
29
#include "tclMacInt.h"
30
 
31
/*
32
 * These function are not currently defined in any header file.  The
33
 * only place they should be used is in the Initialization and
34
 * Termination entry points for a code fragment.  The prototypes
35
 * are included here to avoid compile errors.
36
 */
37
 
38
OSErr TclMacInitializeFragment _ANSI_ARGS_((
39
                        struct CFragInitBlock* initBlkPtr));
40
void TclMacTerminateFragment _ANSI_ARGS_((void));
41
 
42
/*
43
 * Static functions in this file.
44
 */
45
 
46
static OSErr OpenLibraryResource _ANSI_ARGS_((
47
                        struct CFragInitBlock* initBlkPtr));
48
static void CloseLibraryResource _ANSI_ARGS_((void));
49
 
50
/*
51
 * The refnum of the opened resource fork.
52
 */
53
static short ourResFile = kResFileNotOpened;
54
 
55
/*
56
 * This is the resource token for the our resource file.
57
 * It stores the name we registered with the resource facility.
58
 * We only need to use this if we are actually registering ourselves.
59
 */
60
 
61
#ifdef TCL_REGISTER_LIBRARY
62
static Tcl_Obj *ourResToken;
63
#endif
64
 
65
/*
66
 *----------------------------------------------------------------------
67
 *
68
 * TclMacInitializeFragment --
69
 *
70
 *      Called by MacOS CFM when the shared library is loaded. All this
71
 *      function really does is give Tcl a chance to open and register
72
 *      the resource fork of the library.
73
 *
74
 * Results:
75
 *      MacOS error code if loading should be canceled.
76
 *
77
 * Side effects:
78
 *      Opens the resource fork of the shared library file.
79
 *
80
 *----------------------------------------------------------------------
81
 */
82
 
83
OSErr
84
TclMacInitializeFragment(
85
    struct CFragInitBlock* initBlkPtr)          /* Pointer to our library. */
86
{
87
    OSErr err = noErr;
88
 
89
#ifdef __MWERKS__
90
    {
91
        extern OSErr __initialize( CFragInitBlock* initBlkPtr);
92
        err = __initialize((CFragInitBlock *) initBlkPtr);
93
    }
94
#endif
95
    if (err == noErr)
96
        err = OpenLibraryResource( initBlkPtr);
97
    return err;
98
}
99
 
100
/*
101
 *----------------------------------------------------------------------
102
 *
103
 * TclMacTerminateFragment --
104
 *
105
 *      Called by MacOS CFM when the shared library is unloaded.
106
 *
107
 * Results:
108
 *      None.
109
 *
110
 * Side effects:
111
 *      The resource fork of the code fragment is closed.
112
 *
113
 *----------------------------------------------------------------------
114
 */
115
 
116
void
117
TclMacTerminateFragment()
118
{
119
    CloseLibraryResource();
120
 
121
#ifdef __MWERKS__
122
    {
123
        extern void __terminate(void);
124
        __terminate();
125
    }
126
#endif
127
}
128
 
129
/*
130
 *----------------------------------------------------------------------
131
 *
132
 * OpenLibraryResource --
133
 *
134
 *      This routine can be called by a MacOS fragment's initialiation
135
 *      function to open the resource fork of the file.
136
 *      Call it with the same data passed to the initialization function.
137
 *      If the fragment loading should fail if the resource fork can't
138
 *      be opened, then the initialization function can pass on this
139
 *      return value.
140
 *
141
 *      If you #define TCL_REGISTER_RESOURCE before compiling this resource,
142
 *      then your library will register its open resource fork with the
143
 *      resource command.
144
 *
145
 * Results:
146
 *      It returns noErr on success and a MacOS error code on failure.
147
 *
148
 * Side effects:
149
 *      The resource fork of the code fragment is opened read-only and
150
 *      is installed at the head of the resource chain.
151
 *
152
 *----------------------------------------------------------------------
153
 */
154
 
155
static OSErr
156
OpenLibraryResource(
157
    struct CFragInitBlock* initBlkPtr)
158
{
159
    /*
160
     * The 3.0 version of the Universal headers changed CFragInitBlock
161
     * to an opaque pointer type.  CFragSystem7InitBlock is now the
162
     * real pointer.
163
     */
164
 
165
#if !defined(UNIVERSAL_INTERFACES_VERSION) || (UNIVERSAL_INTERFACES_VERSION < 0x0300)
166
    struct CFragInitBlock *realInitBlkPtr = initBlkPtr;
167
#else 
168
    CFragSystem7InitBlock *realInitBlkPtr = (CFragSystem7InitBlock *) initBlkPtr;
169
#endif
170
    FSSpec* fileSpec = NULL;
171
    OSErr err = noErr;
172
 
173
 
174
    if (realInitBlkPtr->fragLocator.where == kOnDiskFlat) {
175
        fileSpec = realInitBlkPtr->fragLocator.u.onDisk.fileSpec;
176
    } else if (realInitBlkPtr->fragLocator.where == kOnDiskSegmented) {
177
        fileSpec = realInitBlkPtr->fragLocator.u.inSegs.fileSpec;
178
    } else {
179
        err = resFNotFound;
180
    }
181
 
182
    /*
183
     * Open the resource fork for this library in read-only mode.
184
     * This will make it the current res file, ahead of the
185
     * application's own resources.
186
     */
187
 
188
    if (fileSpec != NULL) {
189
        ourResFile = FSpOpenResFile(fileSpec, fsRdPerm);
190
        if (ourResFile == kResFileNotOpened) {
191
            err = ResError();
192
        } else {
193
#ifdef TCL_REGISTER_LIBRARY
194
            ourResToken = Tcl_NewObj();
195
            Tcl_IncrRefCount(ourResToken);
196
            p2cstr(realInitBlkPtr->libName);
197
            Tcl_SetStringObj(ourResToken, (char *) realInitBlkPtr->libName, -1);
198
            c2pstr((char *) realInitBlkPtr->libName);
199
            TclMacRegisterResourceFork(ourResFile, ourResToken,
200
                    TCL_RESOURCE_DONT_CLOSE);
201
#endif
202
            SetResFileAttrs(ourResFile, mapReadOnly);
203
        }
204
    }
205
 
206
    return err;
207
}
208
 
209
/*
210
 *----------------------------------------------------------------------
211
 *
212
 * CloseLibraryResource --
213
 *
214
 *      This routine should be called by a MacOS fragment's termination
215
 *      function to close the resource fork of the file
216
 *      that was opened with OpenLibraryResource.
217
 *
218
 * Results:
219
 *      None.
220
 *
221
 * Side effects:
222
 *      The resource fork of the code fragment is closed.
223
 *
224
 *----------------------------------------------------------------------
225
 */
226
 
227
static void
228
CloseLibraryResource()
229
{
230
    if (ourResFile != kResFileNotOpened) {
231
#ifdef TCL_REGISTER_LIBRARY
232
        int length;
233
        TclMacUnRegisterResourceFork(
234
                Tcl_GetStringFromObj(ourResToken, &length),
235
                NULL);
236
        Tcl_DecrRefCount(ourResToken);
237
#endif
238
        CloseResFile(ourResFile);
239
        ourResFile = kResFileNotOpened;
240
    }
241
}

powered by: WebSVN 2.1.0

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