1 /***************************************************************************
3 openurl.library - universal URL display and browser launcher library
4 Copyright (C) 1998-2005 by Troels Walsted Hansen, et al.
5 Copyright (C) 2005-2009 by openurl.library Open Source Team
7 This library is free software; it has been placed in the public domain
8 and you can freely redistribute it and/or modify it. Please note, however,
9 that some components may be under the LGPL or GPL license.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 openurl.library project: http://sourceforge.net/projects/openurllib/
19 ***************************************************************************/
24 #include <exec/resident.h>
25 #include <proto/exec.h>
30 /****************************************************************************/
32 #if !defined(__MORPHOS__)
34 #define MIN_STACKSIZE 65536
37 // stack cookie for shell v45+
38 static const char USED_VAR stack_size
[] = "$STACK:" STR(MIN_STACKSIZE
) "\n";
41 /****************************************************************************/
43 #if defined(__amigaos4__)
44 struct Library
*SysBase
= NULL
;
45 struct ExecIFace
* IExec
= NULL
;
46 #if defined(__NEWLIB__)
47 struct Library
*NewlibBase
= NULL
;
48 struct NewlibIFace
* INewlib
= NULL
;
51 struct ExecBase
*SysBase
= NULL
;
54 struct LibraryHeader
*OpenURLBase
= NULL
;
56 static const char UserLibName
[] = "openurl.library";
57 static const char UserLibID
[] = "$VER: openurl.library " LIB_REV_STRING
" [" SYSTEMSHORT
"/" CPU
"] (" LIB_DATE
") " LIB_COPYRIGHT
;
59 /****************************************************************************/
61 #define libvector LFUNC_FAS(URL_OpenA) \
63 LFUNC_FA_(URL_OldGetPrefs) \
64 LFUNC_FA_(URL_OldFreePrefs) \
65 LFUNC_FA_(URL_OldSetPrefs) \
66 LFUNC_FA_(URL_OldGetDefaultPrefs) \
67 LFUNC_FA_(URL_OldLaunchPrefsApp) \
69 LFUNC_FA_(URL_GetPrefsA) \
70 LFUNC_VA_(URL_GetPrefs) \
71 LFUNC_FA_(URL_FreePrefsA) \
72 LFUNC_VA_(URL_FreePrefs) \
73 LFUNC_FA_(URL_SetPrefsA) \
74 LFUNC_VA_(URL_SetPrefs) \
75 LFUNC_FA_(URL_LaunchPrefsAppA) \
76 LFUNC_VA_(URL_LaunchPrefsApp) \
77 LFUNC_FA_(URL_GetAttr)
79 /****************************************************************************/
81 #if defined(__amigaos4__)
83 static struct LibraryHeader
* LIBFUNC
LibInit (struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecIFace
*pIExec
);
84 static BPTR LIBFUNC
LibExpunge (struct LibraryManagerInterface
*Self
);
85 static struct LibraryHeader
* LIBFUNC
LibOpen (struct LibraryManagerInterface
*Self
, ULONG version
);
86 static BPTR LIBFUNC
LibClose (struct LibraryManagerInterface
*Self
);
87 //static LONG LIBFUNC LibNull (void);
89 #elif defined(__MORPHOS__)
91 static struct LibraryHeader
* LIBFUNC
LibInit (struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecBase
*sb
);
92 static BPTR LIBFUNC
LibExpunge(void);
93 static struct LibraryHeader
* LIBFUNC
LibOpen (void);
94 static BPTR LIBFUNC
LibClose (void);
95 static LONG LIBFUNC
LibNull (void);
99 static struct LibraryHeader
* LIBFUNC
LibInit (REG(d0
, struct LibraryHeader
*lh
), REG(a0
, BPTR Segment
), REG(a6
, struct ExecBase
*sb
));
100 static BPTR LIBFUNC
LibExpunge (REG(a6
, struct LibraryHeader
*base
));
101 static struct LibraryHeader
* LIBFUNC
LibOpen (REG(d0
, ULONG version
), REG(a6
, struct LibraryHeader
*base
));
102 static BPTR LIBFUNC
LibClose (REG(a6
, struct LibraryHeader
*base
));
103 static LONG LIBFUNC
LibNull (void);
107 /****************************************************************************/
110 * The system (and compiler) rely on a symbol named _start which marks
111 * the beginning of execution of an ELF file. To prevent others from
112 * executing this library, and to keep the compiler/linker happy, we
113 * define an empty _start symbol here.
115 * On the classic system (pre-AmigaOS4) this was usually done by
121 #if defined(__amigaos4__)
130 #if !defined(__amigaos4__)
131 static LONG LIBFUNC
LibNull(VOID
)
137 /****************************************************************************/
139 #if defined(__amigaos4__)
140 /* ------------------- OS4 Manager Interface ------------------------ */
141 STATIC uint32
_manager_Obtain(struct LibraryManagerInterface
*Self
)
144 __asm__
__volatile__(
150 : "r" (&Self
->Data
.RefCount
)
156 STATIC uint32
_manager_Release(struct LibraryManagerInterface
*Self
)
159 __asm__
__volatile__(
165 : "r" (&Self
->Data
.RefCount
)
171 STATIC CONST CONST_APTR lib_manager_vectors
[] =
173 (CONST_APTR
)_manager_Obtain
,
174 (CONST_APTR
)_manager_Release
,
178 (CONST_APTR
)LibClose
,
179 (CONST_APTR
)LibExpunge
,
184 STATIC CONST
struct TagItem lib_managerTags
[] =
186 { MIT_Name
, (Tag
)"__library" },
187 { MIT_VectorTable
, (Tag
)lib_manager_vectors
},
192 /* ------------------- Library Interface(s) ------------------------ */
194 ULONG
LibObtain(UNUSED
struct Interface
*Self
)
199 ULONG
LibRelease(UNUSED
struct Interface
*Self
)
204 STATIC CONST CONST_APTR main_vectors
[] =
206 (CONST_APTR
)LibObtain
,
207 (CONST_APTR
)LibRelease
,
210 (CONST_APTR
)libvector
,
214 STATIC CONST
struct TagItem mainTags
[] =
216 { MIT_Name
, (Tag
)"main" },
217 { MIT_VectorTable
, (Tag
)main_vectors
},
222 STATIC CONST CONST_APTR libInterfaces
[] =
224 (CONST_APTR
)lib_managerTags
,
225 (CONST_APTR
)mainTags
,
229 // Our libraries always have to carry a 68k jump table with it, so
230 // lets define it here as extern, as we are going to link it to
232 #ifndef NO_VECTABLE68K
233 extern CONST APTR VecTable68K
[];
236 STATIC CONST
struct TagItem libCreateTags
[] =
238 { CLT_DataSize
, sizeof(struct LibraryHeader
) },
239 { CLT_InitFunc
, (Tag
)LibInit
},
240 { CLT_Interfaces
, (Tag
)libInterfaces
},
241 #ifndef NO_VECTABLE68K
242 { CLT_Vector68K
, (Tag
)VecTable68K
},
249 STATIC CONST CONST_APTR LibVectors
[] =
252 (CONST_APTR
)FUNCARRAY_32BIT_NATIVE
,
255 (CONST_APTR
)LibClose
,
256 (CONST_APTR
)LibExpunge
,
258 (CONST_APTR
)libvector
,
262 STATIC CONST IPTR LibInitTab
[] =
264 sizeof(struct LibraryHeader
),
272 /****************************************************************************/
274 static const USED_VAR
struct Resident ROMTag
=
277 (struct Resident
*)&ROMTag
,
278 (struct Resident
*)(&ROMTag
+ 1),
279 #if defined(__amigaos4__)
280 RTF_AUTOINIT
|RTF_NATIVE
, // The Library should be set up according to the given table.
281 #elif defined(__MORPHOS__)
282 RTF_AUTOINIT
|RTF_EXTENDED
|RTF_PPC
,
283 #elif defined(__AROS__)
284 RTF_AUTOINIT
|RTF_EXTENDED
,
292 (char *)UserLibID
+6, // +6 to skip '$VER: '
293 #if defined(__amigaos4__)
294 (APTR
)libCreateTags
// This table is for initializing the Library.
298 #if defined(__MORPHOS__) || defined(__AROS__)
304 #if defined(__MORPHOS__)
306 * To tell the loader that this is a new emulppc elf and not
307 * one for the ppc.library.
310 const USED_VAR ULONG __abox__
= 1;
312 #endif /* __MORPHOS */
314 /****************************************************************************/
316 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
318 /* generic StackSwap() function which calls function() surrounded by
321 #if defined(__mc68000__)
322 ULONG
stackswap_call(struct StackSwapStruct
*stack
,
323 ULONG (*function
)(struct LibraryHeader
*),
324 struct LibraryHeader
*arg
);
328 .globl _stackswap_call \n\
330 moveml #0x3022,sp@- \n\
334 movel _SysBase,a6 \n\
341 movel _SysBase,a6 \n\
345 moveml sp@+,#0x440c \n\
347 #elif defined(__MORPHOS__)
348 ULONG
stackswap_call(struct StackSwapStruct
*stack
,
349 ULONG (*function
)(struct LibraryHeader
*),
350 struct LibraryHeader
*arg
)
352 struct PPCStackSwapArgs swapargs
;
354 swapargs
.Args
[0] = (ULONG
)arg
;
356 return NewPPCStackSwap(stack
, function
, &swapargs
);
359 /* FIXME: This does not work because it can't work. 'arg' variable is also on stack.
360 On AmigaOS v4 it ocassionally works because function's parameters are placed
361 in registers on PPC */
362 ULONG REGARGS
stackswap_call(struct StackSwapStruct
*stack
,
363 ULONG (*function
)(struct LibraryHeader
*),
364 struct LibraryHeader
*arg
)
366 register ULONG result
;
369 result
= function(arg
);
376 static BOOL
callLibFunction(ULONG (*function
)(struct LibraryHeader
*), struct LibraryHeader
*arg
)
378 BOOL success
= FALSE
;
382 // retrieve the task structure for the
386 #if defined(__MORPHOS__)
387 // In MorphOS we have two stacks. One for PPC code and another for 68k code.
388 // We are only interested in the PPC stack.
389 NewGetTaskAttrsA(tc
, &stacksize
, sizeof(ULONG
), TASKINFOTYPE_STACKSIZE
, NULL
);
391 // on all other systems we query via SPUpper-SPLower calculation
392 stacksize
= (ULONG
)tc
->tc_SPUpper
- (ULONG
)tc
->tc_SPLower
;
395 // Swap stacks only if current stack is insufficient
396 if(stacksize
< MIN_STACKSIZE
)
398 struct StackSwapStruct
*stack
;
400 if((stack
= AllocVec(sizeof(*stack
), MEMF_PUBLIC
)) != NULL
)
402 if((stack
->stk_Lower
= AllocVec(MIN_STACKSIZE
, MEMF_PUBLIC
)) != NULL
)
404 // perform the StackSwap
405 stack
->stk_Upper
= (ULONG
)stack
->stk_Lower
+ MIN_STACKSIZE
;
406 stack
->stk_Pointer
= (APTR
)stack
->stk_Upper
;
408 // call routine but with embedding it into a [NewPPC]StackSwap()
409 success
= stackswap_call(stack
, function
, arg
);
411 FreeVec(stack
->stk_Lower
);
417 success
= function(arg
);
422 #endif // !__amigaos4__
424 /****************************************************************************/
426 #if defined(__amigaos4__)
427 static struct LibraryHeader
* LibInit(struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecIFace
*pIExec
)
429 struct ExecBase
*sb
= (struct ExecBase
*)pIExec
->Data
.LibBase
;
431 #elif defined(__MORPHOS__)
432 static struct LibraryHeader
* LibInit(struct LibraryHeader
*base
, BPTR librarySegment
, struct ExecBase
*sb
)
435 static struct LibraryHeader
* LIBFUNC
LibInit(REG(d0
, struct LibraryHeader
*base
), REG(a0
, BPTR librarySegment
), REG(a6
, struct ExecBase
*sb
))
441 // make sure that this is really a 68020+ machine if optimized for 020+
442 #if _M68060 || _M68040 || _M68030 || _M68020 || __mc68020 || __mc68030 || __mc68040 || __mc68060
443 if(isFlagClear(SysBase
->AttnFlags
, AFF_68020
))
447 #if defined(__amigaos4__) && defined(__NEWLIB__)
448 if((NewlibBase
= OpenLibrary("newlib.library", 3)) &&
449 GETINTERFACE(INewlib
, NewlibBase
))
452 BOOL success
= FALSE
;
454 D(DBF_STARTUP
, "LibInit()");
456 // cleanup the library header structure beginning with the
458 base
->libBase
.lib_Node
.ln_Type
= NT_LIBRARY
;
459 base
->libBase
.lib_Node
.ln_Pri
= 0;
460 base
->libBase
.lib_Node
.ln_Name
= (char *)UserLibName
;
461 base
->libBase
.lib_Flags
= LIBF_CHANGED
| LIBF_SUMUSED
;
462 base
->libBase
.lib_Version
= LIB_VERSION
;
463 base
->libBase
.lib_Revision
= LIB_REVISION
;
464 base
->libBase
.lib_IdString
= (char *)(UserLibID
+6);
466 InitSemaphore(&base
->libSem
);
467 InitSemaphore(&base
->poolSem
);
468 InitSemaphore(&base
->prefsSem
);
470 base
->sysBase
= (APTR
)SysBase
;
476 // protect access to initBase()
477 ObtainSemaphore(&base
->libSem
);
479 // set the OpenURLBase
482 // If we are not running on AmigaOS4 (no stackswap required) we go and
483 // do an explicit StackSwap() in case the user wants to make sure we
484 // have enough stack for his user functions
485 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
486 success
= callLibFunction(initBase
, base
);
488 success
= initBase(base
);
491 // unprotect initBase()
492 ReleaseSemaphore(&base
->libSem
);
494 // check if everything worked out fine
497 // everything was successfully so lets
498 // set the initialized value and contiue
499 // with the class open phase
500 base
->segList
= librarySegment
;
502 // return the library base as success
508 #if defined(__amigaos4__) && defined(__NEWLIB__)
511 DROPINTERFACE(INewlib
);
512 CloseLibrary(NewlibBase
);
521 /****************************************************************************/
524 #define DeleteLibrary(LIB) \
525 FreeMem((STRPTR)(LIB)-(LIB)->lib_NegSize, (ULONG)((LIB)->lib_NegSize+(LIB)->lib_PosSize))
528 #if defined(__amigaos4__)
529 static BPTR
LibExpunge(struct LibraryManagerInterface
*Self
)
531 struct ExecIFace
*IExec
= (struct ExecIFace
*)(*(struct ExecBase
**)4)->MainInterface
;
532 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
533 #elif defined(__MORPHOS__)
534 static BPTR
LibExpunge(void)
536 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
538 static BPTR LIBFUNC
LibExpunge(REG(a6
, struct LibraryHeader
*base
))
543 D(DBF_STARTUP
, "LibExpunge(): %ld", base
->libBase
.lib_OpenCnt
);
545 // in case our open counter is still > 0, we have
546 // to set the late expunge flag and return immediately
547 if(base
->libBase
.lib_OpenCnt
> 0)
549 SET_FLAG(base
->libBase
.lib_Flags
, LIBF_DELEXP
);
554 // make sure to restore the SysBase
555 SysBase
= (APTR
)base
->sysBase
;
557 // remove the library base from exec's lib list in advance
558 Remove((struct Node
*)base
);
560 // free all our private data and stuff.
561 ObtainSemaphore(&base
->libSem
);
563 // make sure we have enough stack here
564 #if defined(MIN_STACKSIZE) && !defined(__amigaos4__)
565 callLibFunction(freeBase
, base
);
571 ReleaseSemaphore(&base
->libSem
);
573 #if defined(__amigaos4__) && defined(__NEWLIB__)
576 DROPINTERFACE(INewlib
);
577 CloseLibrary(NewlibBase
);
582 // make sure the system deletes the library as well.
584 DeleteLibrary(&base
->libBase
);
590 /****************************************************************************/
592 #if defined(__amigaos4__)
593 static struct LibraryHeader
*LibOpen(struct LibraryManagerInterface
*Self
, ULONG version UNUSED
)
595 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
596 #elif defined(__MORPHOS__)
597 static struct LibraryHeader
*LibOpen(void)
599 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
601 static struct LibraryHeader
* LIBFUNC
LibOpen(REG(d0
, UNUSED ULONG version
), REG(a6
, struct LibraryHeader
*base
))
604 struct LibraryHeader
*res
= base
;
606 D(DBF_STARTUP
, "LibOpen(): %ld", base
->libBase
.lib_OpenCnt
);
608 // LibOpen(), LibClose() and LibExpunge() are called while the system is in
609 // Forbid() state. That means that these functions should be quick and should
610 // not break this Forbid()!! Therefore the open counter should be increased
611 // as the very first instruction during LibOpen(), because a ClassOpen()
612 // which breaks a Forbid() and another task calling LibExpunge() will cause
613 // to expunge this library while it is not yet fully initialized. A crash
614 // is unavoidable then. Even the semaphore does not guarantee 100% protection
615 // against such a race condition, because waiting for the semaphore to be
616 // obtained will effectively break the Forbid()!
618 // increase the open counter ahead of anything else
619 base
->libBase
.lib_OpenCnt
++;
621 // delete the late expunge flag
622 CLEAR_FLAG(base
->libBase
.lib_Flags
, LIBF_DELEXP
);
627 /****************************************************************************/
629 #if defined(__amigaos4__)
630 static BPTR
LibClose(struct LibraryManagerInterface
*Self
)
632 struct LibraryHeader
*base
= (struct LibraryHeader
*)Self
->Data
.LibBase
;
633 #elif defined(__MORPHOS__)
634 static BPTR
LibClose(void)
636 struct LibraryHeader
*base
= (struct LibraryHeader
*)REG_A6
;
638 static BPTR LIBFUNC
LibClose(REG(a6
, struct LibraryHeader
*base
))
643 D(DBF_STARTUP
, "LibClose(): %ld", base
->libBase
.lib_OpenCnt
);
645 // decrease the open counter
646 base
->libBase
.lib_OpenCnt
--;
648 // in case the opern counter is <= 0 we can
649 // make sure that we free everything
650 if(base
->libBase
.lib_OpenCnt
<= 0)
652 // in case the late expunge flag is set we go and
653 // expunge the library base right now
654 if(isFlagSet(base
->libBase
.lib_Flags
, LIBF_DELEXP
))
656 #if defined(__amigaos4__)
657 rc
= LibExpunge(Self
);
658 #elif defined(__MORPHOS__)
661 rc
= LibExpunge(base
);
669 /****************************************************************************/