Killed REDIRECT_URL, added USER_MANUAL_URL and HELP_LINK_PREFIX
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.8 2002/03/25 19:32:15 joergs Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/amiga.c,v $
5  *
6  * Purpose     :  Amiga-specific declarations.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                Privoxy team. http://www.privoxy.org/
10  *
11  *                This program is free software; you can redistribute it 
12  *                and/or modify it under the terms of the GNU General
13  *                Public License as published by the Free Software
14  *                Foundation; either version 2 of the License, or (at
15  *                your option) any later version.
16  *
17  *                This program is distributed in the hope that it will
18  *                be useful, but WITHOUT ANY WARRANTY; without even the
19  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
20  *                PARTICULAR PURPOSE.  See the GNU General Public
21  *                License for more details.
22  *
23  *                The GNU General Public License should be included with
24  *                this file.  If not, you can view it at
25  *                http://www.gnu.org/copyleft/gpl.html
26  *                or write to the Free Software Foundation, Inc., 59
27  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  *
29  * Revisions   :
30  *    $Log: amiga.c,v $
31  *    Revision 1.8  2002/03/25 19:32:15  joergs
32  *    Name in version string changed from junkbuster to Privoxy.
33  *
34  *    Revision 1.7  2002/03/24 13:25:43  swa
35  *    name change related issues
36  *
37  *    Revision 1.6  2002/03/09 20:03:52  jongfoster
38  *    - Making various functions return int rather than size_t.
39  *      (Undoing a recent change).  Since size_t is unsigned on
40  *      Windows, functions like read_socket that return -1 on
41  *      error cannot return a size_t.
42  *
43  *      THIS WAS A MAJOR BUG - it caused frequent, unpredictable
44  *      crashes, and also frequently caused JB to jump to 100%
45  *      CPU and stay there.  (Because it thought it had just
46  *      read ((unsigned)-1) == 4Gb of data...)
47  *
48  *    - The signature of write_socket has changed, it now simply
49  *      returns success=0/failure=nonzero.
50  *
51  *    - Trying to get rid of a few warnings --with-debug on
52  *      Windows, I've introduced a new type "jb_socket".  This is
53  *      used for the socket file descriptors.  On Windows, this
54  *      is SOCKET (a typedef for unsigned).  Everywhere else, it's
55  *      an int.  The error value can't be -1 any more, so it's
56  *      now JB_INVALID_SOCKET (which is -1 on UNIX, and in
57  *      Windows it maps to the #define INVALID_SOCKET.)
58  *
59  *    - The signature of bind_port has changed.
60  *
61  *    Revision 1.5  2002/03/03 09:18:03  joergs
62  *    Made jumbjuster work on AmigaOS again.
63  *
64  *    Revision 1.4  2001/10/07 15:35:13  oes
65  *    Replaced 6 boolean members of csp with one bitmap (csp->flags)
66  *
67  *    Revision 1.3  2001/09/12 22:54:51  joergs
68  *    Stacksize of main thread increased.
69  *
70  *    Revision 1.2  2001/05/23 00:13:58  joergs
71  *    AmigaOS support fixed.
72  *
73  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
74  *    Initial import of version 2.9.3 source tree
75  *
76  *
77  *********************************************************************/
78 \f
79
80 #include "config.h"
81
82 #ifdef AMIGA
83
84 #include <stdio.h>
85 #include <signal.h>
86
87 #include "project.h"
88
89 const char amiga_h_rcs[] = AMIGA_H_VERSION;
90
91 unsigned long __stack = 100*1024;
92 static char ver[] = "$VER: Privoxy " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
93 struct Task *main_task = NULL;
94 int childs = 0;
95
96 void serve(struct client_state *csp);
97
98 __saveds ULONG server_thread(void)
99 {
100    struct client_state *local_csp;
101    struct UserData UserData;
102    struct Task *me=FindTask(NULL);
103
104    Wait(SIGF_SINGLE);
105    local_csp=(struct client_state *)(me->tc_UserData);
106    me->tc_UserData=&UserData;
107    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
108    if(SocketBase)
109    {
110       SetErrnoPtr(&(UserData.eno),sizeof(int));
111       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
112       if(JB_INVALID_SOCKET!=local_csp->cfd)
113       {
114          Signal(main_task,SIGF_SINGLE);
115          serve((struct client_state *) local_csp);
116       } else {
117          local_csp->flags &= ~CSP_FLAG_ACTIVE;
118          Signal(main_task,SIGF_SINGLE);
119       }
120       CloseLibrary(SocketBase);
121    } else {
122       local_csp->flags &= ~CSP_FLAG_ACTIVE;
123       Signal(main_task,SIGF_SINGLE);
124    }
125    childs--;
126    return 0;
127 }
128
129 static BPTR olddir;
130
131 void amiga_exit(void)
132 {
133    if(SocketBase)
134    {
135       CloseLibrary(SocketBase);
136    }
137    CurrentDir(olddir);
138 }
139
140 static struct SignalSemaphore memsem;
141 static struct SignalSemaphore *memsemptr = NULL;
142 static struct UserData GlobalUserData;
143
144 void InitAmiga(void)
145 {
146    main_task = FindTask(NULL);
147    main_task->tc_UserData = &GlobalUserData;
148
149    if (((struct Library *)SysBase)->lib_Version < 39)
150    {
151       exit(RETURN_FAIL);
152    }
153
154    signal(SIGINT,SIG_IGN);
155    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
156    if (!SocketBase)
157    {
158       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
159       exit(RETURN_ERROR);
160    }
161    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
162    InitSemaphore(&memsem);
163    memsemptr = &memsem;
164
165    olddir=CurrentDir(GetProgramDir());
166    atexit(amiga_exit);
167 }
168
169 #ifdef __GNUC__
170 #ifdef libnix
171 /* multithreadingsafe libnix replacements */
172 static void *memPool=NULL;
173
174 void *malloc (size_t s)
175 {
176    ULONG *mem;
177    LONG size = s;
178
179    if (size<=0)
180    {
181       return NULL;
182    }
183    if (!memPool)
184    {
185       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
186       {
187          return NULL;
188       }
189    }
190    size += sizeof(ULONG) + MEM_BLOCKMASK;
191    size &= ~MEM_BLOCKMASK;
192    if (memsemptr)
193    {
194       ObtainSemaphore(memsemptr);
195    }
196    if ((mem=AllocPooled(memPool,size)))
197    {
198       *mem++=size;
199    }
200    if (memsemptr)
201    {
202       ReleaseSemaphore(memsemptr);
203    }
204    return mem;
205 }
206
207 void free (void *m)
208 {
209    ULONG *mem = m;
210
211    if(mem && memPool)
212    {
213       ULONG size=*--mem;
214
215       if (memsemptr)
216       {
217          ObtainSemaphore(memsemptr);
218       }
219       FreePooled(memPool,mem,size);
220       if (memsemptr)
221       {
222          ReleaseSemaphore(memsemptr);
223       }
224    }
225 }
226
227 void *realloc (void *old, size_t ns)
228 {
229    void *new;
230    LONG osize, *o = old;
231    LONG nsize = ns;
232
233    if (!old)
234    {
235       return malloc(nsize);
236    }
237    osize = (*(o-1)) - sizeof(ULONG);
238    if (nsize <= osize)
239    {
240       return old;
241    }
242    if ((new = malloc(nsize)))
243    {
244       ULONG *n = new;
245
246       osize >>= 2;
247       while(osize--)
248       {
249          *n++ = *o++;
250       }
251       free(old);
252    }
253    return new;
254 }
255
256 void __memCleanUp (void)
257 {
258    if (memsemptr)
259    {
260       ObtainSemaphore(memsemptr);
261    }
262    if (memPool)
263    {
264       DeletePool(memPool);
265    }
266    if (memsemptr)
267    {
268       ReleaseSemaphore(memsemptr);
269    }
270 }
271
272 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
273 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
274                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
275 ADD2EXIT(__memCleanUp,-50);
276 #elif !defined(ixemul)
277 #error No libnix and no ixemul!?
278 #endif /* libnix */
279 #else
280 #error Only GCC is supported, multithreading safe malloc/free required.
281 #endif /* __GNUC__ */
282
283 #endif /* def AMIGA */