Moving all our URL and URL pattern parsing code to urlmatch.c.
[privoxy.git] / amiga.c
1 const char amiga_rcs[] = "$Id: amiga.c,v 1.3 2001/09/12 22:54:51 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  *                IJBSWA team.  http://ijbswa.sourceforge.net
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.3  2001/09/12 22:54:51  joergs
32  *    Stacksize of main thread increased.
33  *
34  *    Revision 1.2  2001/05/23 00:13:58  joergs
35  *    AmigaOS support fixed.
36  *
37  *    Revision 1.1.1.1  2001/05/15 13:58:46  oes
38  *    Initial import of version 2.9.3 source tree
39  *
40  *
41  *********************************************************************/
42 \f
43
44 #include "config.h"
45
46 #ifdef AMIGA
47
48 #include <stdio.h>
49 #include <signal.h>
50
51 #include "project.h"
52
53 const char amiga_h_rcs[] = AMIGA_H_VERSION;
54
55 unsigned long __stack = 100*1024;
56 static char ver[] = "$VER: junkbuster " __AMIGAVERSION__ " (" __AMIGADATE__ ")";
57 struct Task *main_task = NULL;
58 int childs = 0;
59
60 void serve(struct client_state *csp);
61
62 __saveds ULONG server_thread(void)
63 {
64    struct client_state *local_csp;
65    struct UserData UserData;
66    struct Task *me=FindTask(NULL);
67
68    Wait(SIGF_SINGLE);
69    local_csp=(struct client_state *)(me->tc_UserData);
70    me->tc_UserData=&UserData;
71    SocketBase=(APTR)OpenLibrary("bsdsocket.library",3);
72    if(SocketBase)
73    {
74       SetErrnoPtr(&(UserData.eno),sizeof(int));
75       local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0);
76       if(-1!=local_csp->cfd)
77       {
78          Signal(main_task,SIGF_SINGLE);
79          serve((struct client_state *) local_csp);
80       } else {
81          local_csp->flags &= ~CSP_FLAG_ACTIVE;
82          Signal(main_task,SIGF_SINGLE);
83       }
84       CloseLibrary(SocketBase);
85    } else {
86       local_csp->flags &= ~CSP_FLAG_ACTIVE;
87       Signal(main_task,SIGF_SINGLE);
88    }
89    childs--;
90    return 0;
91 }
92
93 void amiga_exit(void)
94 {
95    if(SocketBase)
96    {
97       CloseLibrary(SocketBase);
98    }
99 }
100
101 static struct SignalSemaphore memsem;
102 static struct SignalSemaphore *memsemptr = NULL;
103 static struct UserData GlobalUserData;
104
105 void InitAmiga(void)
106 {
107    main_task = FindTask(NULL);
108    main_task->tc_UserData = &GlobalUserData;
109
110    if (((struct Library *)SysBase)->lib_Version < 39)
111    {
112       exit(RETURN_FAIL);
113    }
114
115    signal(SIGINT,SIG_IGN);
116    SocketBase = (APTR)OpenLibrary("bsdsocket.library",3);
117    if (!SocketBase)
118    {
119       fprintf(stderr, "Can't open bsdsocket.library V3+\n");
120       exit(RETURN_ERROR);
121    }
122    SetErrnoPtr(&(GlobalUserData.eno),sizeof(int));
123    InitSemaphore(&memsem);
124    memsemptr = &memsem;
125
126    atexit(amiga_exit);
127 }
128
129 #ifdef __GNUC__
130 #ifdef libnix
131 /* multitaskingsafe libnix replacements */
132 static void *memPool=NULL;
133
134 void *malloc (size_t s)
135 {
136    ULONG *mem;
137    LONG size = s;
138
139    if (size<=0)
140    {
141       return NULL;
142    }
143    if (!memPool)
144    {
145       if (!(memPool=CreatePool(MEMF_ANY,32*1024,8*1024)))
146       {
147          return NULL;
148       }
149    }
150    size += sizeof(ULONG) + MEM_BLOCKMASK;
151    size &= ~MEM_BLOCKMASK;
152    if (memsemptr)
153    {
154       ObtainSemaphore(memsemptr);
155    }
156    if ((mem=AllocPooled(memPool,size)))
157    {
158       *mem++=size;
159    }
160    if (memsemptr)
161    {
162       ReleaseSemaphore(memsemptr);
163    }
164    return mem;
165 }
166
167 void free (void *m)
168 {
169    ULONG *mem = m;
170
171    if(mem && memPool)
172    {
173       ULONG size=*--mem;
174
175       if (memsemptr)
176       {
177          ObtainSemaphore(memsemptr);
178       }
179       FreePooled(memPool,mem,size);
180       if (memsemptr)
181       {
182          ReleaseSemaphore(memsemptr);
183       }
184    }
185 }
186
187 void *realloc (void *old, size_t ns)
188 {
189    void *new;
190    LONG osize, *o = old;
191    LONG nsize = ns;
192
193    if (!old)
194    {
195       return malloc(nsize);
196    }
197    osize = (*(o-1)) - sizeof(ULONG);
198    if (nsize <= osize)
199    {
200       return old;
201    }
202    if ((new = malloc(nsize)))
203    {
204       ULONG *n = new;
205
206       osize >>= 2;
207       while(osize--)
208       {
209          *n++ = *o++;
210       }
211       free(old);
212    }
213    return new;
214 }
215
216 void __memCleanUp (void)
217 {
218    if (memsemptr)
219    {
220       ObtainSemaphore(memsemptr);
221    }
222    if (memPool)
223    {
224       DeletePool(memPool);
225    }
226    if (memsemptr)
227    {
228       ReleaseSemaphore(memsemptr);
229    }
230 }
231
232 #define ADD2LIST(a,b,c) asm(".stabs \"_" #b "\"," #c ",0,0,_" #a )
233 #define ADD2EXIT(a,pri) ADD2LIST(a,__EXIT_LIST__,22); \
234                         asm(".stabs \"___EXIT_LIST__\",20,0,0," #pri "+128")
235 ADD2EXIT(__memCleanUp,-50);
236 #elif !defined(ixemul)
237 #error No libnix and no ixemul!?
238 #endif /* libnix */
239 #else
240 #error Only GCC is supported, multitasking safe malloc/free required.
241 #endif /* __GNUC__ */
242
243 #endif /* def AMIGA */