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-2013 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 ***************************************************************************/
23 #include <dos/dostags.h>
24 #include <exec/execbase.h>
31 #include <SDI_stdarg.h>
34 #include <proto/openurl.h>
36 /**************************************************************************/
38 LIBPROTO(URL_OpenA
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, STRPTR url
), REG(a1
, struct TagItem
*attrs
))
42 STRPTR pubScreenName
= NULL
;
43 STRPTR fullURL
= NULL
;
45 BOOL httpPrepend
= FALSE
;
52 ObtainSemaphore(&OpenURLBase
->prefsSem
);
55 pubScreenName
= (STRPTR
)GetTagData(URL_PubScreenName
, (IPTR
)"Workbench", attrs
);
57 if(GetTagData(URL_Show
, OpenURLBase
->prefs
->up_DefShow
, attrs
))
58 SET_FLAG(flags
, SENDTOF_SHOW
);
59 if(GetTagData(URL_BringToFront
, OpenURLBase
->prefs
->up_DefBringToFront
, attrs
))
60 SET_FLAG(flags
, SENDTOF_TOFRONT
);
61 if(GetTagData(URL_NewWindow
, OpenURLBase
->prefs
->up_DefNewWindow
, attrs
))
62 SET_FLAG(flags
, SENDTOF_NEWWINDOW
);
63 if(GetTagData(URL_Launch
, OpenURLBase
->prefs
->up_DefLaunch
, attrs
))
64 SET_FLAG(flags
, SENDTOF_LAUNCH
);
66 /* make a copy of the global list of named ports */
68 res
= copyList(&portList
, &((struct ExecBase
*)SysBase
)->PortList
, sizeof(struct Node
));
73 /* prepend "http://" if URL has no method */
74 if(isFlagSet(OpenURLBase
->prefs
->up_Flags
, UPF_PREPENDHTTP
))
78 colon
= strchr((STRPTR
)url
,':');
85 for(p
= url
; p
<colon
; p
++)
87 if(!isalnum(*p
) && *p
!= '+' && *p
!= '-')
96 if(httpPrepend
== TRUE
)
98 ULONG len
= strlen(url
) + 8;
100 if(len
> sizeof(buf
))
101 fullURL
= allocArbitrateVecPooled(strlen(url
)+8);
106 snprintf(fullURL
, len
, "http://%s", url
);
113 /* Be case insensitive - Piru */
114 if(isFlagSet(OpenURLBase
->prefs
->up_Flags
, UPF_DOMAILTO
) && Strnicmp((STRPTR
)url
,"mailto:", 7) == 0)
115 res
= sendToMailer(fullURL
, &portList
, flags
, pubScreenName
);
116 else if(isFlagSet(OpenURLBase
->prefs
->up_Flags
, UPF_DOFTP
) && Strnicmp((STRPTR
)url
,"ftp://", 6) == 0)
117 res
= sendToFTP(fullURL
, &portList
, flags
, pubScreenName
);
119 res
= sendToBrowser(fullURL
, &portList
, flags
, pubScreenName
);
123 ReleaseSemaphore(&OpenURLBase
->prefsSem
);
125 if(httpPrepend
== TRUE
&& fullURL
!= NULL
&& fullURL
!= buf
)
126 freeArbitrateVecPooled(fullURL
);
132 #if defined(__amigaos4__)
133 LIBPROTOVA(URL_Open
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, STRPTR url
), ...)
139 res
= URL_OpenA(url
, VA_ARG(args
, struct TagItem
*));
146 /**************************************************************************/
148 LIBPROTO(URL_GetPrefsA
, struct URL_Prefs
*, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct TagItem
*attrs
))
150 struct URL_Prefs
*p
= NULL
;
155 mode
= GetTagData(URL_GetPrefs_Mode
, URL_GetPrefs_Mode_Env
, attrs
);
159 case URL_GetPrefs_Mode_Default
:
161 if((p
= allocArbitrateVecPooled(sizeof(*p
))) != NULL
)
166 case URL_GetPrefs_Mode_Env
:
167 case URL_GetPrefs_Mode_Envarc
:
171 mode
= (mode
== URL_GetPrefs_Mode_Env
) ? LOADPREFS_ENV
: LOADPREFS_ENVARC
;
173 if((p
= allocArbitrateVecPooled(sizeof(*p
))) != NULL
)
175 if(loadPrefs(p
, mode
) == TRUE
)
179 else if(GetTagData(URL_GetPrefs_FallBack
, TRUE
, attrs
) != FALSE
)
181 if(mode
== LOADPREFS_ENV
&& loadPrefs(p
, LOADPREFS_ENVARC
) == TRUE
)
195 URL_FreePrefsA(p
, NULL
);
203 ObtainSemaphoreShared(&OpenURLBase
->prefsSem
);
204 p
= copyPrefs(OpenURLBase
->prefs
);
205 ReleaseSemaphore(&OpenURLBase
->prefsSem
);
214 #if defined(__amigaos4__)
215 LIBPROTOVA(URL_GetPrefs
, struct URL_Prefs
*, REG(a6
, UNUSED __BASE_OR_IFACE
), ...)
217 struct URL_Prefs
*res
;
220 VA_START(args
, IOpenURL
);
221 res
= URL_GetPrefsA(VA_ARG(args
, struct TagItem
*));
228 /**************************************************************************/
230 LIBPROTO(URL_OldGetPrefs
, struct URL_Prefs
*, REG(a6
, UNUSED __BASE_OR_IFACE
))
232 return URL_GetPrefsA(NULL
);
235 /**************************************************************************/
237 LIBPROTO(URL_FreePrefsA
, void, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*up
), REG(a1
, UNUSED
struct TagItem
*attrs
))
243 freeList((struct List
*)&up
->up_BrowserList
);
244 freeList((struct List
*)&up
->up_MailerList
);
245 freeList((struct List
*)&up
->up_FTPList
);
246 freeArbitrateVecPooled(up
);
252 #if defined(__amigaos4__)
253 LIBPROTOVA(URL_FreePrefs
, void, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*up
), ...)
258 URL_FreePrefsA(up
, VA_ARG(args
, struct TagItem
*));
263 /**************************************************************************/
265 LIBPROTO(URL_OldFreePrefs
, void, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*up
))
267 URL_FreePrefsA(up
,NULL
);
270 /**************************************************************************/
272 LIBPROTO(URL_SetPrefsA
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*p
), REG(a1
, struct TagItem
*attrs
))
278 if(p
->up_Version
==PREFS_VERSION
)
280 struct URL_Prefs
*newp
;
282 ObtainSemaphore(&OpenURLBase
->prefsSem
);
284 if((newp
= copyPrefs(p
)) != NULL
)
286 newp
->up_Version
= PREFS_VERSION
;
287 CLEAR_FLAG(newp
->up_Flags
, UPF_ISDEFAULTS
);
289 URL_FreePrefsA(OpenURLBase
->prefs
,NULL
);
290 OpenURLBase
->prefs
= newp
;
292 if(savePrefs((STRPTR
)DEF_ENV
, OpenURLBase
->prefs
) == TRUE
)
294 if(GetTagData(URL_SetPrefs_Save
, FALSE
, attrs
) != FALSE
)
296 if(savePrefs((STRPTR
)DEF_ENVARC
, OpenURLBase
->prefs
) == TRUE
)
304 ReleaseSemaphore(&OpenURLBase
->prefsSem
);
311 #if defined(__amigaos4__)
312 LIBPROTOVA(URL_SetPrefs
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*p
), ...)
318 res
= URL_SetPrefsA(p
, VA_ARG(args
, struct TagItem
*));
325 /**************************************************************************/
327 LIBPROTO(URL_OldSetPrefs
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct URL_Prefs
*p
), REG(d0
, ULONG permanent
))
329 struct TagItem stags
[] = { { URL_SetPrefs_Save
, 0 },
330 { TAG_DONE
, TAG_DONE
} };
332 stags
[0].ti_Data
= permanent
;
334 return URL_SetPrefsA(p
,stags
);
337 /**************************************************************************/
339 LIBPROTO(URL_OldGetDefaultPrefs
, struct URL_Prefs
*, REG(a6
, UNUSED __BASE_OR_IFACE
))
341 struct TagItem gtags
[] = { { URL_GetPrefs_Mode
, URL_GetPrefs_Mode_Default
},
342 { TAG_DONE
, TAG_DONE
} };
344 return URL_GetPrefsA(gtags
);
347 /**************************************************************************/
349 LIBPROTO(URL_LaunchPrefsAppA
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, UNUSED
struct TagItem
*attrs
))
351 ULONG result
= FALSE
;
356 if((in
= Open("NIL:",MODE_OLDFILE
)))
360 if((out
= Open("NIL:",MODE_OLDFILE
)))
365 if((len
= GetVar("AppPaths/OpenURL", name
+1, sizeof(name
)-1, GVF_GLOBAL_ONLY
)) <= 0)
367 // Ok let's try to be backward compatible
368 if(GetVar("OpenURL_Prefs_Path", name
, sizeof(name
), GVF_GLOBAL_ONLY
) <= 0)
370 strlcpy(name
, "SYS:Prefs/OpenURL", sizeof(name
));
376 strcpy(name
+1+len
,"/OpenURL\"");
380 #if defined(__amigaos4__)
381 #define NP_STACKSIZE 48000
383 #define NP_STACKSIZE 16000
385 #if !defined(__MORPHOS__)
386 #define NP_PPCStackSize TAG_IGNORE
389 SystemTags(name
, SYS_Input
, (IPTR
)in
,
390 SYS_Output
, (IPTR
)out
,
393 NP_StackSize
, NP_STACKSIZE
,
394 NP_PPCStackSize
, 32000,
408 #if defined(__amigaos4__)
409 LIBPROTOVA(URL_LaunchPrefsApp
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), ...)
414 VA_START(args
, IOpenURL
);
415 res
= URL_LaunchPrefsAppA(VA_ARG(args
, struct TagItem
*));
422 /**************************************************************************/
424 LIBPROTO(URL_OldLaunchPrefsApp
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
))
426 return URL_LaunchPrefsAppA(NULL
);
429 /**************************************************************************/
431 LIBPROTO(URL_GetAttr
, ULONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(d0
, ULONG attr
), REG(a0
, IPTR
*storage
))
435 case URL_GetAttr_Version
: *storage
= LIB_VERSION
; return TRUE
;
436 case URL_GetAttr_Revision
: *storage
= LIB_REVISION
; return TRUE
;
437 case URL_GetAttr_VerString
: *storage
= (IPTR
)"$VER: openurl.library " LIB_REV_STRING
" [" SYSTEMSHORT
"/" CPU
"] (" LIB_DATE
") " LIB_COPYRIGHT
;; return TRUE
;
439 case URL_GetAttr_PrefsVer
: *storage
= PREFS_VERSION
; return TRUE
;
441 case URL_GetAttr_HandlerVersion
: *storage
= 0; return TRUE
;
442 case URL_GetAttr_HandlerRevision
: *storage
= 0; return TRUE
;
443 case URL_GetAttr_HandlerVerString
: *storage
= (IPTR
)""; return TRUE
;
445 default: return FALSE
;
449 /**************************************************************************/
451 LIBPROTO(dispatch
, LONG
, REG(a6
, UNUSED __BASE_OR_IFACE
), REG(a0
, struct RexxMsg
*msg
), REG(a1
, STRPTR
*resPtr
))
454 STRPTR fun
= (STRPTR
)msg
->rm_Args
[0];
455 ULONG na
= msg
->rm_Action
& RXARGMASK
;
460 if(stricmp(fun
, "OPENURL") == 0)
464 struct TagItem tags
[MAXRMARG
+1];
468 for(i
= na
, j
= 0, url
= NULL
; i
>0; i
--)
470 STRPTR arg
= (STRPTR
)msg
->rm_Args
[i
];
473 if(arg
== NULL
|| *arg
== '\0')
476 if(stricmp(arg
, "SHOW") == 0 || stricmp(arg
, "NOSHOW") == 0)
478 else if(stricmp(arg
, "TOFRONT") == 0 || stricmp(arg
, "NOTOFRONT") == 0)
479 tag
= URL_BringToFront
;
480 else if(stricmp(arg
, "NEWWIN") == 0 || stricmp(arg
, "NONEWWIN") == 0)
482 else if(stricmp(arg
, "LAUNCH") == 0 || stricmp(arg
,"NOLAUNCH") == 0)
490 tags
[j
].ti_Tag
= tag
;
491 tags
[j
++].ti_Data
= strnicmp(arg
, "NO" , 2);
494 tags
[j
].ti_Tag
= TAG_END
;
496 res
= (url
!= NULL
&& URL_OpenA(url
, tags
));
501 if (stricmp(fun
, "OPENURLPREFS") == 0)
504 res
= URL_LaunchPrefsAppA(NULL
);
510 if((*resPtr
= (STRPTR
)CreateArgstring((STRPTR
)(res
? "1" : "0"), 1)) != NULL
)
519 /**************************************************************************/