Fixed compatibility of output.
[AROS.git] / external / openurl / library / api.c
blobe7adfb50728f10a01d539e787e8f4917c29840de
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/
17 $Id$
19 ***************************************************************************/
21 #include "lib.h"
23 #include <dos/dostags.h>
24 #include <exec/execbase.h>
26 #include "version.h"
27 #include "debug.h"
29 #include <stdio.h>
31 #include <SDI_stdarg.h>
33 #define __NOLIBBASE__
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))
40 struct List portList;
41 TEXT buf[256];
42 STRPTR pubScreenName = NULL;
43 STRPTR fullURL = NULL;
44 ULONG res;
45 BOOL httpPrepend = FALSE;
46 ULONG flags;
48 ENTER();
50 NewList(&portList);
52 ObtainSemaphore(&OpenURLBase->prefsSem);
54 /* parse arguments */
55 pubScreenName = (STRPTR)GetTagData(URL_PubScreenName, (IPTR)"Workbench", attrs);
56 flags = 0;
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 */
67 Forbid();
68 res = copyList(&portList, &((struct ExecBase *)SysBase)->PortList, sizeof(struct Node));
69 Permit();
71 if(res == TRUE)
73 /* prepend "http://" if URL has no method */
74 if(isFlagSet(OpenURLBase->prefs->up_Flags, UPF_PREPENDHTTP))
76 STRPTR colon;
78 colon = strchr((STRPTR)url,':');
79 if(colon == NULL)
80 httpPrepend = TRUE;
81 else
83 STRPTR p;
85 for(p = url; p<colon; p++)
87 if(!isalnum(*p) && *p != '+' && *p != '-')
89 httpPrepend = TRUE;
90 break;
96 if(httpPrepend == TRUE)
98 ULONG len = strlen(url) + 8;
100 if(len > sizeof(buf))
101 fullURL = allocArbitrateVecPooled(strlen(url)+8);
102 else
103 fullURL = buf;
105 if(fullURL != NULL)
106 snprintf(fullURL, len, "http://%s", url);
108 else
109 fullURL = url;
111 if(fullURL != NULL)
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);
118 else
119 res = sendToBrowser(fullURL, &portList, flags, pubScreenName);
123 ReleaseSemaphore(&OpenURLBase->prefsSem);
124 freeList(&portList);
125 if(httpPrepend == TRUE && fullURL != NULL && fullURL != buf)
126 freeArbitrateVecPooled(fullURL);
128 RETURN(res);
129 return res;
132 #if defined(__amigaos4__)
133 LIBPROTOVA(URL_Open, ULONG, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, STRPTR url), ...)
135 ULONG res;
136 VA_LIST args;
138 VA_START(args, url);
139 res = URL_OpenA(url, VA_ARG(args, struct TagItem *));
140 VA_END(args);
142 return res;
144 #endif
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;
151 ULONG mode;
153 ENTER();
155 mode = GetTagData(URL_GetPrefs_Mode, URL_GetPrefs_Mode_Env, attrs);
157 switch(mode)
159 case URL_GetPrefs_Mode_Default:
161 if((p = allocArbitrateVecPooled(sizeof(*p))) != NULL)
162 setDefaultPrefs(p);
164 break;
166 case URL_GetPrefs_Mode_Env:
167 case URL_GetPrefs_Mode_Envarc:
169 BOOL ok = FALSE;
171 mode = (mode == URL_GetPrefs_Mode_Env) ? LOADPREFS_ENV : LOADPREFS_ENVARC;
173 if((p = allocArbitrateVecPooled(sizeof(*p))) != NULL)
175 if(loadPrefs(p, mode) == TRUE)
177 ok = TRUE;
179 else if(GetTagData(URL_GetPrefs_FallBack, TRUE, attrs) != FALSE)
181 if(mode == LOADPREFS_ENV && loadPrefs(p, LOADPREFS_ENVARC) == TRUE)
183 ok = TRUE;
185 else
187 setDefaultPrefs(p);
188 ok = TRUE;
193 if(ok == FALSE)
195 URL_FreePrefsA(p, NULL);
196 p = NULL;
199 break;
201 default:
203 ObtainSemaphoreShared(&OpenURLBase->prefsSem);
204 p = copyPrefs(OpenURLBase->prefs);
205 ReleaseSemaphore(&OpenURLBase->prefsSem);
207 break;
210 RETURN(p);
211 return p;
214 #if defined(__amigaos4__)
215 LIBPROTOVA(URL_GetPrefs, struct URL_Prefs *, REG(a6, UNUSED __BASE_OR_IFACE), ...)
217 struct URL_Prefs *res;
218 VA_LIST args;
220 VA_START(args, IOpenURL);
221 res = URL_GetPrefsA(VA_ARG(args, struct TagItem *));
222 VA_END(args);
224 return res;
226 #endif
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))
239 ENTER();
241 if(up != NULL)
243 freeList((struct List *)&up->up_BrowserList);
244 freeList((struct List *)&up->up_MailerList);
245 freeList((struct List *)&up->up_FTPList);
246 freeArbitrateVecPooled(up);
249 LEAVE();
252 #if defined(__amigaos4__)
253 LIBPROTOVA(URL_FreePrefs, void, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, struct URL_Prefs *up), ...)
255 VA_LIST args;
257 VA_START(args, up);
258 URL_FreePrefsA(up, VA_ARG(args, struct TagItem *));
259 VA_END(args);
261 #endif
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))
274 ULONG res = FALSE;
276 ENTER();
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)
297 res = TRUE;
299 else
300 res = TRUE;
304 ReleaseSemaphore(&OpenURLBase->prefsSem);
307 RETURN(res);
308 return res;
311 #if defined(__amigaos4__)
312 LIBPROTOVA(URL_SetPrefs, ULONG, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, struct URL_Prefs *p), ...)
314 ULONG res;
315 VA_LIST args;
317 VA_START(args, p);
318 res = URL_SetPrefsA(p, VA_ARG(args, struct TagItem *));
319 VA_END(args);
321 return res;
323 #endif
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;
352 BPTR in;
354 ENTER();
356 if((in = Open("NIL:",MODE_OLDFILE)))
358 BPTR out;
360 if((out = Open("NIL:",MODE_OLDFILE)))
362 TEXT name[256];
363 LONG len;
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));
373 else
375 name[0]='\"';
376 strcpy(name+1+len,"/OpenURL\"");
377 name[len+11]='\0';
380 #if defined(__amigaos4__)
381 #define NP_STACKSIZE 48000
382 #else
383 #define NP_STACKSIZE 16000
384 #endif
385 #if !defined(__MORPHOS__)
386 #define NP_PPCStackSize TAG_IGNORE
387 #endif
389 SystemTags(name, SYS_Input, (IPTR)in,
390 SYS_Output, (IPTR)out,
391 SYS_Error, NULL,
392 SYS_Asynch, TRUE,
393 NP_StackSize, NP_STACKSIZE,
394 NP_PPCStackSize, 32000,
395 TAG_END);
397 result = TRUE;
400 if(result == FALSE)
401 Close(in);
404 RETURN(result);
405 return result;
408 #if defined(__amigaos4__)
409 LIBPROTOVA(URL_LaunchPrefsApp, ULONG, REG(a6, UNUSED __BASE_OR_IFACE), ...)
411 ULONG res;
412 VA_LIST args;
414 VA_START(args, IOpenURL);
415 res = URL_LaunchPrefsAppA(VA_ARG(args, struct TagItem *));
416 VA_END(args);
418 return res;
420 #endif
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))
433 switch (attr)
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))
453 ULONG result = 17;
454 STRPTR fun = (STRPTR)msg->rm_Args[0];
455 ULONG na = msg->rm_Action & RXARGMASK;
456 BOOL res = FALSE;
458 ENTER();
460 if(stricmp(fun, "OPENURL") == 0)
462 if(na >= 1)
464 struct TagItem tags[MAXRMARG+1];
465 STRPTR url;
466 int i, j;
468 for(i = na, j = 0, url = NULL; i>0; i--)
470 STRPTR arg = (STRPTR)msg->rm_Args[i];
471 Tag tag;
473 if(arg == NULL || *arg == '\0')
474 continue;
476 if(stricmp(arg, "SHOW") == 0 || stricmp(arg, "NOSHOW") == 0)
477 tag = URL_Show;
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)
481 tag = URL_NewWindow;
482 else if(stricmp(arg, "LAUNCH") == 0 || stricmp(arg,"NOLAUNCH") == 0)
483 tag = URL_Launch;
484 else
486 url = arg;
487 continue;
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));
499 else
501 if (stricmp(fun, "OPENURLPREFS") == 0)
503 if(na == 0)
504 res = URL_LaunchPrefsAppA(NULL);
506 else
507 result = 1;
510 if((*resPtr = (STRPTR)CreateArgstring((STRPTR)(res ? "1" : "0"), 1)) != NULL)
511 result = 0;
512 else
513 result = 3;
515 RETURN(result);
516 return result;
519 /**************************************************************************/