1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "apr_pools.h"
#include "apr_tables.h"
#include "apr.h"
#include "apr_hooks.h"
#include "apr_hash.h"
#include "apr_optional_hooks.h"
#include "apr_optional.h"
#define APR_WANT_MEMFUNC
#define APR_WANT_STRFUNC
#include "apr_want.h"
#if 0
#define apr_palloc(pool,size) malloc(size)
#endif
APU_DECLARE_DATA apr_pool_t *apr_hook_global_pool = NULL;
APU_DECLARE_DATA int apr_hook_debug_enabled = 0;
APU_DECLARE_DATA const char *apr_hook_debug_current = NULL;
/** @deprecated @see apr_hook_global_pool */
APU_DECLARE_DATA apr_pool_t *apr_global_hook_pool = NULL;
/** @deprecated @see apr_hook_debug_enabled */
APU_DECLARE_DATA int apr_debug_module_hooks = 0;
/** @deprecated @see apr_hook_debug_current */
APU_DECLARE_DATA const char *apr_current_hooking_module = NULL;
/* NB: This must echo the LINK_##name structure */
typedef struct
{
void (*dummy)(void *);
const char *szName;
const char * const *aszPredecessors;
const char * const *aszSuccessors;
int nOrder;
} TSortData;
typedef struct tsort_
{
void *pData;
int nPredecessors;
struct tsort_ **ppPredecessors;
struct tsort_ *pNext;
} TSort;
#ifdef NETWARE
#include "apr_private.h"
#define get_apd APP_DATA* apd = (APP_DATA*)get_app_data(gLibId);
#define s_aHooksToSort ((apr_array_header_t *)(apd->gs_aHooksToSort))
#define s_phOptionalHooks ((apr_hash_t *)(apd->gs_phOptionalHooks))
#define s_phOptionalFunctions ((apr_hash_t *)(apd->gs_phOptionalFunctions))
#endif
static int crude_order(const void *a_,const void *b_)
{
const TSortData *a=a_;
const TSortData *b=b_;
return a->nOrder-b->nOrder;
}
static TSort *prepare(apr_pool_t *p,TSortData *pItems,int nItems)
{
TSort *pData=apr_palloc(p,nItems*sizeof *pData);
int n;
qsort(pItems,nItems,sizeof *pItems,crude_order);
for(n=0 ; n < nItems ; ++n) {
pData[n].nPredecessors=0;
pData[n].ppPredecessors=apr_pcalloc(p,nItems*sizeof *pData[n].ppPredecessors);
pData[n].pNext=NULL;
pData[n].pData=&pItems[n];
}
for(n=0 ; n < nItems ; ++n) {
int i,k;
for(i=0 ; pItems[n].aszPredecessors && pItems[n].aszPredecessors[i] ; ++i)
for(k=0 ; k < nItems ; ++k)
if(!strcmp(pItems[k].szName,pItems[n].aszPredecessors[i])) {
int l;
for(l=0 ; l < pData[n].nPredecessors ; ++l)
if(pData[n].ppPredecessors[l] == &pData[k])
goto got_it;
pData[n].ppPredecessors[pData[n].nPredecessors]=&pData[k];
++pData[n].nPredecessors;
got_it:
break;
}
for(i=0 ; pItems[n].aszSuccessors && pItems[n].aszSuccessors[i] ; ++i)
for(k=0 ; k < nItems ; ++k)
if(!strcmp(pItems[k].szName,pItems[n].aszSuccessors[i])) {
int l;
for(l=0 ; l < pData[k].nPredecessors ; ++l)
if(pData[k].ppPredecessors[l] == &pData[n])
goto got_it2;
pData[k].ppPredecessors[pData[k].nPredecessors]=&pData[n];
++pData[k].nPredecessors;
got_it2:
break;
}
}
return pData;
}
/* Topologically sort, dragging out-of-order items to the front. Note that
this tends to preserve things that want to be near the front better, and
changing that behaviour might compromise some of Apache's behaviour (in
particular, mod_log_forensic might otherwise get pushed to the end, and
core.c's log open function used to end up at the end when pushing items
to the back was the methedology). Also note that the algorithm could
go back to its original simplicity by sorting from the back instead of
the front.
*/
static TSort *tsort(TSort *pData,int nItems)
{
int nTotal;
TSort *pHead=NULL;
TSort *pTail=NULL;
for(nTotal=0 ; nTotal < nItems ; ++nTotal) {
int n,i,k;
for(n=0 ; ; ++n) {
if(n == nItems)
assert(0); /* we have a loop... */
if(!pData[n].pNext) {
if(pData[n].nPredecessors) {
for(k=0 ; ; ++k) {
assert(k < nItems);
if(pData[n].ppPredecessors[k])
break;
}
for(i=0 ; ; ++i) {
assert(i < nItems);
if(&pData[i] == pData[n].ppPredecessors[k]) {
n=i-1;
break;
}
}
} else
break;
}
}
if(pTail)
pTail->pNext=&pData[n];
else
pHead=&pData[n];
pTail=&pData[n];
pTail->pNext=pTail; /* fudge it so it looks linked */
for(i=0 ; i < nItems ; ++i)
for(k=0 ; k < nItems ; ++k)
if(pData[i].ppPredecessors[k] == &pData[n]) {
--pData[i].nPredecessors;
pData[i].ppPredecessors[k]=NULL;
break;
}
}
pTail->pNext=NULL; /* unfudge the tail */
return pHead;
}
static apr_array_header_t *sort_hook(apr_array_header_t *pHooks,
const char *szName)
{
apr_pool_t *p;
TSort *pSort;
apr_array_header_t *pNew;
int n;
apr_pool_create(&p, apr_hook_global_pool);
pSort=prepare(p,(TSortData *)pHooks->elts,pHooks->nelts);
pSort=tsort(pSort,pHooks->nelts);
pNew=apr_array_make(apr_hook_global_pool,pHooks->nelts,sizeof(TSortData));
if(apr_hook_debug_enabled)
printf("Sorting %s:",szName);
for(n=0 ; pSort ; pSort=pSort->pNext,++n) {
TSortData *pHook;
assert(n < pHooks->nelts);
pHook=apr_array_push(pNew);
memcpy(pHook,pSort->pData,sizeof *pHook);
if(apr_hook_debug_enabled)
printf(" %s",pHook->szName);
}
if(apr_hook_debug_enabled)
fputc('\n',stdout);
/* destroy the pool - the sorted hooks were already copied */
apr_pool_destroy(p);
return pNew;
}
#ifndef NETWARE
static apr_array_header_t *s_aHooksToSort;
#endif
typedef struct
{
const char *szHookName;
apr_array_header_t **paHooks;
} HookSortEntry;
APU_DECLARE(void) apr_hook_sort_register(const char *szHookName,
apr_array_header_t **paHooks)
{
#ifdef NETWARE
get_apd
#endif
HookSortEntry *pEntry;
if(!s_aHooksToSort)
s_aHooksToSort=apr_array_make(apr_hook_global_pool,1,sizeof(HookSortEntry));
pEntry=apr_array_push(s_aHooksToSort);
pEntry->szHookName=szHookName;
pEntry->paHooks=paHooks;
}
APU_DECLARE(void) apr_hook_sort_all(void)
{
#ifdef NETWARE
get_apd
#endif
int n;
if (!s_aHooksToSort) {
s_aHooksToSort = apr_array_make(apr_hook_global_pool, 1, sizeof(HookSortEntry));
}
for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
*pEntry->paHooks=sort_hook(*pEntry->paHooks,pEntry->szHookName);
}
}
#ifndef NETWARE
static apr_hash_t *s_phOptionalHooks;
static apr_hash_t *s_phOptionalFunctions;
#endif
APU_DECLARE(void) apr_hook_deregister_all(void)
{
#ifdef NETWARE
get_apd
#endif
int n;
if (!s_aHooksToSort) {
return;
}
for(n=0 ; n < s_aHooksToSort->nelts ; ++n) {
HookSortEntry *pEntry=&((HookSortEntry *)s_aHooksToSort->elts)[n];
*pEntry->paHooks=NULL;
}
s_aHooksToSort=NULL;
s_phOptionalHooks=NULL;
s_phOptionalFunctions=NULL;
}
APU_DECLARE(void) apr_hook_debug_show(const char *szName,
const char * const *aszPre,
const char * const *aszSucc)
{
int nFirst;
printf(" Hooked %s",szName);
if(aszPre) {
fputs(" pre(",stdout);
nFirst=1;
while(*aszPre) {
if(!nFirst)
fputc(',',stdout);
nFirst=0;
fputs(*aszPre,stdout);
++aszPre;
}
fputc(')',stdout);
}
if(aszSucc) {
fputs(" succ(",stdout);
nFirst=1;
while(*aszSucc) {
if(!nFirst)
fputc(',',stdout);
nFirst=0;
fputs(*aszSucc,stdout);
++aszSucc;
}
fputc(')',stdout);
}
fputc('\n',stdout);
}
/* Optional hook support */
APR_DECLARE_EXTERNAL_HOOK(apr,APU,void,_optional,(void))
APU_DECLARE(apr_array_header_t *) apr_optional_hook_get(const char *szName)
{
#ifdef NETWARE
get_apd
#endif
apr_array_header_t **ppArray;
if(!s_phOptionalHooks)
return NULL;
ppArray=apr_hash_get(s_phOptionalHooks,szName,strlen(szName));
if(!ppArray)
return NULL;
return *ppArray;
}
APU_DECLARE(void) apr_optional_hook_add(const char *szName,void (*pfn)(void),
const char * const *aszPre,
const char * const *aszSucc,int nOrder)
{
#ifdef NETWARE
get_apd
#endif
apr_array_header_t *pArray=apr_optional_hook_get(szName);
apr_LINK__optional_t *pHook;
if(!pArray) {
apr_array_header_t **ppArray;
pArray=apr_array_make(apr_hook_global_pool,1,
sizeof(apr_LINK__optional_t));
if(!s_phOptionalHooks)
s_phOptionalHooks=apr_hash_make(apr_hook_global_pool);
ppArray=apr_palloc(apr_hook_global_pool,sizeof *ppArray);
*ppArray=pArray;
apr_hash_set(s_phOptionalHooks,szName,strlen(szName),ppArray);
apr_hook_sort_register(szName,ppArray);
}
pHook=apr_array_push(pArray);
pHook->pFunc=pfn;
pHook->aszPredecessors=aszPre;
pHook->aszSuccessors=aszSucc;
pHook->nOrder=nOrder;
pHook->szName=apr_hook_debug_current;
if(apr_hook_debug_enabled)
apr_hook_debug_show(szName,aszPre,aszSucc);
}
/* optional function support */
APU_DECLARE(apr_opt_fn_t *) apr_dynamic_fn_retrieve(const char *szName)
{
#ifdef NETWARE
get_apd
#endif
if(!s_phOptionalFunctions)
return NULL;
return (void(*)(void))apr_hash_get(s_phOptionalFunctions,szName,strlen(szName));
}
/* Deprecated */
APU_DECLARE_NONSTD(void) apr_dynamic_fn_register(const char *szName,
apr_opt_fn_t *pfn)
{
#ifdef NETWARE
get_apd
#endif
if(!s_phOptionalFunctions)
s_phOptionalFunctions=apr_hash_make(apr_hook_global_pool);
apr_hash_set(s_phOptionalFunctions,szName,strlen(szName),(void *)pfn);
}
#if 0
void main()
{
const char *aszAPre[]={"b","c",NULL};
const char *aszBPost[]={"a",NULL};
const char *aszCPost[]={"b",NULL};
TSortData t1[]=
{
{ "a",aszAPre,NULL },
{ "b",NULL,aszBPost },
{ "c",NULL,aszCPost }
};
TSort *pResult;
pResult=prepare(t1,3);
pResult=tsort(pResult,3);
for( ; pResult ; pResult=pResult->pNext)
printf("%s\n",pResult->pData->szName);
}
#endif
|