epio.c

filter: Statementstmt 0.0% 0 / 365 Branchbran 0.0% 0 / 248 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 613 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« epinit.c ↑ Summary epmain.c »
1 /*###################################################################################
2 #
3 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
4 # Embperl - Copyright (c) 2008-2015 Gerald Richter
5 # Embperl - Copyright (c) 2015-2023 actevy.io
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9 # For use with Apache httpd and mod_perl, see also Apache copyright.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 ###################################################################################*/
16  
17  
18 #include "ep.h"
19 #include "epmacro.h"
20 #include "crypto/epcrypto.h"
21  
22  
23  
24 #ifndef PerlIO
25  
26  
27 #define FILEIOTYPE "StdIO"
28 /* define same helper macros to let it run with plain perl 5.003 */
29 /* where no PerlIO is present */
30  
31 #define PerlIO_stdinF stdin
32 #define PerlIO_stdoutF stdout
33 #define PerlIO_stderrF stderr
34 #define PerlIO_close fclose
35 #define PerlIO_open fopen
36 #define PerlIO_flush fflush
37 #define PerlIO_vprintf vfprintf
38 #define PerlIO_fileno fileno
39 #define PerlIO_tell ftell
40 #define PerlIO_seek fseek
41  
42 #define PerlIO_read(f,buf,cnt) fread(buf,1,cnt,f)
43 #define PerlIO_write(f,buf,cnt) fwrite(buf,1,cnt,f)
44  
45 #define PerlIO_putc(f,c) fputc(c,f)
46  
47 #else
48  
49 #define FILEIOTYPE "PerlIO"
50  
51 #define PerlIO_stdinF PerlIO_stdin ()
52 #define PerlIO_stdoutF PerlIO_stdout ()
53 #define PerlIO_stderrF PerlIO_stderr ()
54  
55  
56 #endif
57  
58  
59 /* Some helper macros for tied handles, taken from mod_perl 2.0 :-) */
60 /*
61 * bleedperl change #11639 switch tied handle magic
62 * from living in the gv to the GvIOp(gv), so we have to deal
63 * with both to support 5.6.x
64 */
65 #if ((PERL_REVISION == 5) && (PERL_VERSION >= 7))
66 # define TIEHANDLE_SV(handle) (SV*)GvIOp((SV*)handle)
67 #else
68 # define TIEHANDLE_SV(handle) (SV*)handle
69 #endif
70  
71 #define HANDLE_GV(name) gv_fetchpv(name, TRUE, SVt_PVIO)
72 //#define HANDLE_GV(name) gv_fetchpv(name, GV_ADD, SVt_PVIO)
73  
74  
75  
76 #ifdef APACHE
77 #define DefaultLog "/tmp/embperl.log"
78  
79  
80 static request_rec * pAllocReq = NULL ;
81 #endif
82  
83  
84  
85 /* -------------------------------------------------------------------------------------
86 *
87 * begin output transaction
88 *
89 -------------------------------------------------------------------------------------- */
90  
91 0 struct tBuf * oBegin (/*i/o*/ register req * r)
92  
93 {
94 EPENTRY1N (oBegin, r -> Component.pOutput -> nMarker) ;
95
96 0 r -> Component.pOutput -> nMarker++ ;
97
98 0 return r -> Component.pOutput -> pLastBuf ;
99 }
100  
101 /* -------------------------------------------------------------------------------------
102 *
103 * rollback output transaction (throw away all the output since corresponding begin)
104 *
105 -------------------------------------------------------------------------------------- */
106  
107 0 void oRollbackOutput (/*i/o*/ register req * r,
108 struct tBuf * pBuf)
109  
110 {
111 EPENTRY1N (oRollback, r -> Component.pOutput -> nMarker) ;
112  
113 0 ▶ if (pBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pBuf == NULL) 0 0 0
114 {
115 0 ▶ if (r -> Component.pOutput -> pLastFreeBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastFreeBuf) 0 0 0
116 0 r -> Component.pOutput -> pLastFreeBuf -> pNext = r -> Component.pOutput -> pFirstBuf ;
117 else
118 0 r -> Component.pOutput -> pFreeBuf = r -> Component.pOutput -> pFirstBuf ;
119
120 0 r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
121
122 0 r -> Component.pOutput -> pFirstBuf = NULL ;
123 0 r -> Component.pOutput -> nMarker = 0 ;
124 }
125 else
126 {
127 0 ▶ if (r -> Component.pOutput -> pLastBuf == pBuf || pBuf -> pNext == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastBuf == pBuf || pBuf -> pNext == NULL) 0 0 0
if (r -> Component.pOutput -> pLastBuf == pBuf || pBuf -> pNext == NULL) 0 0 0
128 0 r -> Component.pOutput -> nMarker-- ;
129 else
130 {
131 0 r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
132 0 ▶ if (r -> Component.pOutput -> pLastFreeBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastFreeBuf) 0 0 0
133 0 r -> Component.pOutput -> pLastFreeBuf -> pNext = pBuf -> pNext ;
134 else
135 0 r -> Component.pOutput -> pFreeBuf = pBuf -> pNext ;
136 0 r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
137 }
138 0 pBuf -> pNext = NULL ;
139 }
140
141 0 r -> Component.pOutput -> pLastBuf = pBuf ;
142  
143 0 }
144  
145 /* -------------------------------------------------------------------------------------
146 *
147 * rollback output transaction and errors(throw away all the output since corresponding
148 * begin)
149 *
150 -------------------------------------------------------------------------------------- */
151  
152 0 void oRollback (/*i/o*/ register req * r,
153 struct tBuf * pBuf)
154  
155 {
156 0 oRollbackOutput (r, pBuf) ;
157
158 /* RollbackError (r) ; */
159 0 }
160  
161 /* ---------------------------------------------------------------------------- */
162 /* */
163 /* commit output transaction (all the output since corresponding begin is vaild)*/
164 /* */
165 /* ---------------------------------------------------------------------------- */
166  
167 0 void oCommitToMem (/*i/o*/ register req * r,
168 struct tBuf * pBuf,
169 char * pOut)
170  
171 {
172 EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
173  
174
175 0 ▶ if (pBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pBuf == NULL) 0 0 0
176 0 r -> Component.pOutput -> nMarker = 0 ;
177 else
178 0 ▶ if (r -> Component.pOutput -> pLastBuf == pBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastBuf == pBuf) 0 0 0
179 0 r -> Component.pOutput -> nMarker-- ;
180 else
181 0 r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
182
183 0 ▶ if (r -> Component.pOutput -> nMarker == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> nMarker == 0) 0 0 0
184 {
185 0 ▶ if (pBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pBuf == NULL) 0 0 0
186 0 pBuf = r -> Component.pOutput -> pFirstBuf ;
187 else
188 0 pBuf = pBuf -> pNext ;
189
190 0 ▶ if (pOut)
Branch: 1 branch
BranchTrueFalseTotal
if (pOut) 0 0 0
191 {
192 0 ▶ while (pBuf)
Branch: 1 branch
BranchTrueFalseTotal
while (pBuf) 0 0 0
193 {
194 0 memmove (pOut, pBuf + 1, pBuf -> nSize) ;
195 0 pOut += pBuf -> nSize ;
196 0 pBuf = pBuf -> pNext ;
197 }
198 0 *pOut = '\0' ;
199 }
200 else
201 {
202 0 ▶ while (pBuf)
Branch: 1 branch
BranchTrueFalseTotal
while (pBuf) 0 0 0
203 {
204 0 owrite (r, pBuf + 1, pBuf -> nSize) ;
205 0 pBuf = pBuf -> pNext ;
206 }
207 }
208 }
209  
210 /* CommitError (r) ; */
211 0 }
212  
213 /* ---------------------------------------------------------------------------- */
214 /* */
215 /* commit output transaction (all the output since corresponding begin is vaild)*/
216 /* */
217 /* ---------------------------------------------------------------------------- */
218  
219 0 void oCommit (/*i/o*/ register req * r,
220 struct tBuf * pBuf)
221  
222 {
223 EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
224  
225 0 oCommitToMem (r, pBuf, NULL) ;
226 0 }
227  
228 /* ---------------------------------------------------------------------------- */
229 /* */
230 /* write to a buffer */
231 /* */
232 /* we will alloc a new buffer for every write */
233 /* this is fast with ep_palloc */
234 /* */
235 /* ---------------------------------------------------------------------------- */
236  
237  
238 0 static int bufwrite (/*i/o*/ register req * r,
239 /*in*/ const void * ptr, size_t size)
240  
241  
242 {
243 struct tBuf * pBuf ;
244  
245 EPENTRY1N (bufwrite, r -> Component.pOutput -> nMarker) ;
246  
247 0 pBuf = (struct tBuf *)ep_palloc (r -> Component.pOutput -> pPool, size + sizeof (struct tBuf)) ;
248  
249 0 ▶ if (pBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pBuf == NULL) 0 0 0
250 0 return 0 ;
251  
252 0 memcpy (pBuf + 1, ptr, size) ;
253 0 pBuf -> pNext = NULL ;
254 0 pBuf -> nSize = size ;
255 0 pBuf -> nMarker = r -> Component.pOutput -> nMarker ;
256  
257 0 ▶ if (r -> Component.pOutput -> pLastBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastBuf) 0 0 0
258 {
259 0 r -> Component.pOutput -> pLastBuf -> pNext = pBuf ;
260 0 pBuf -> nCount = r -> Component.pOutput -> pLastBuf -> nCount + size ;
261 }
262 else
263 0 pBuf -> nCount = size ;
264
265 0 ▶ if (r -> Component.pOutput -> pFirstBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pFirstBuf == NULL) 0 0 0
266 0 r -> Component.pOutput -> pFirstBuf = pBuf ;
267 0 r -> Component.pOutput -> pLastBuf = pBuf ;
268  
269  
270 0 return size ;
271 }
272  
273 #if 0
274 /* ---------------------------------------------------------------------------- */
275 /* */
276 /* free buffers */
277 /* */
278 /* free all buffers */
279 /* note: this is not nessecary for apache palloc, because all buffers are freed */
280 /* at the end of the request */
281 /* */
282 /* ---------------------------------------------------------------------------- */
283  
284  
285 static void buffree (/*i/o*/ register req * r)
286  
287 {
288 struct tBuf * pNext = NULL ;
289 struct tBuf * pBuf ;
290  
291 #ifdef APACHE
292 if ((r -> Component.Config.bDebug & dbgMem) == 0 && pAllocReq != NULL)
293 {
294 r -> Component.pOutput -> pFirstBuf = NULL ;
295 r -> Component.pOutput -> pLastBuf = NULL ;
296 r -> Component.pOutput -> pFreeBuf = NULL ;
297 r -> Component.pOutput -> pLastFreeBuf = NULL ;
298 return ; /* no need for apache to free memory */
299 }
300 #endif
301
302 /* first walk thru the used buffers */
303  
304 pBuf = r -> Component.pOutput -> pFirstBuf ;
305 while (pBuf)
306 {
307 pNext = pBuf -> pNext ;
308 _free (r, pBuf) ;
309 pBuf = pNext ;
310 }
311  
312 r -> Component.pOutput -> pFirstBuf = NULL ;
313 r -> Component.pOutput -> pLastBuf = NULL ;
314  
315  
316 /* now walk thru the unused buffers */
317
318 pBuf = r -> Component.pOutput -> pFreeBuf ;
319 while (pBuf)
320 {
321 pNext = pBuf -> pNext ;
322 _free (r, pBuf) ;
323 pBuf = pNext ;
324 }
325  
326 r -> Component.pOutput -> pFreeBuf = NULL ;
327 r -> Component.pOutput -> pLastFreeBuf = NULL ;
328 }
329 #endif
330  
331 /* ---------------------------------------------------------------------------- */
332 /* */
333 /* get the length outputed to buffers so far */
334 /* */
335 /* ---------------------------------------------------------------------------- */
336  
337 0 int GetContentLength (/*i/o*/ register req * r)
338 {
339 0 ▶ if (r -> Component.pOutput -> pLastBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pLastBuf) 0 0 0
340 0 return r -> Component.pOutput -> pLastBuf -> nCount ;
341 else
342 0 return 0 ;
343
344 }
345  
346 /* ---------------------------------------------------------------------------- */
347 /* */
348 /* set the name of the input file and open it */
349 /* */
350 /* ---------------------------------------------------------------------------- */
351  
352  
353 0 int OpenInput (/*i/o*/ register req * r,
354 /*in*/ const char * sFilename)
355  
356 {
357 MAGIC *mg;
358 GV *handle ;
359 epTHX ;
360  
361 #ifdef APACHE
362 if (r -> pApacheReq)
363 return ok ;
364 #endif
365
366 0 handle = HANDLE_GV("STDIN") ;
367 0 ▶ if (handle)
Branch: 1 branch
BranchTrueFalseTotal
if (handle) 0 0 0
368 {
369 0 SV *iohandle = TIEHANDLE_SV(handle) ;
370  
371 0 ▶ if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj)
Branch: 4 branches
BranchTrueFalseTotal
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
372 {
373 0 r -> Component.ifdobj = mg->mg_obj ;
374 0 ▶ if (r -> Component.Config.bDebug)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug) 0 0 0
375 {
376 0 ▶ char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
Branch: 6 branches
BranchTrueFalseTotal
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
377 0 lprintf (r -> pApp, "[%d]Open TIED STDIN %s...\n", r -> pThread -> nPid, package) ;
378 }
379 0 return ok ;
380 }
381 }
382  
383 0 ▶ if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
Branch: 2 branches
BranchTrueFalseTotal
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF) 0 0 0
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF) 0 0 0
384 0 PerlIO_close (r -> Component.ifd) ;
385  
386 0 r -> Component.ifd = NULL ;
387  
388 0 ▶ if (sFilename == NULL || *sFilename == '\0')
Branch: 2 branches
BranchTrueFalseTotal
if (sFilename == NULL || *sFilename == '\0') 0 0 0
if (sFilename == NULL || *sFilename == '\0') 0 0 0
389 {
390 /*
391 GV * io = gv_fetchpv("STDIN", TRUE, SVt_PVIO) ;
392 if (io == NULL || (r -> Component.ifd = IoIFP(io)) == NULL)
393 {
394 if (r -> Component.Config.bDebug)
395 lprintf (r -> pApp, "[%d]Cannot get Perl STDIN, open os stdin\n", r -> pThread -> nPid) ;
396 r -> Component.ifd = PerlIO_stdinF ;
397 }
398 */
399
400 0 r -> Component.ifd = PerlIO_stdinF ;
401  
402 0 return ok ;
403 }
404  
405 0 ▶ if ((r -> Component.ifd = PerlIO_open (sFilename, "r")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((r -> Component.ifd = PerlIO_open (sFilename, "r")) == NULL) 0 0 0
406 {
407 0 strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
408 0 strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
409 0 return rcFileOpenErr ;
410 }
411  
412 0 return ok ;
413 }
414  
415  
416 /* ---------------------------------------------------------------------------- */
417 /* */
418 /* close input file */
419 /* */
420 /* ---------------------------------------------------------------------------- */
421  
422  
423 0 int CloseInput (/*i/o*/ register req * r)
424  
425 {
426 epTHX ;
427  
428 #if 0
429 if (0) /* r -> Component.ifdobj) */
430 {
431 dSP;
432 ENTER;
433 SAVETMPS;
434 PUSHMARK(sp);
435 XPUSHs(r -> Component.ifdobj);
436 PUTBACK;
437 perl_call_method ("CLOSE", G_VOID | G_EVAL) ;
438 SPAGAIN ;
439 FREETMPS;
440 LEAVE;
441 r -> Component.ifdobj = NULL ;
442 }
443 #endif
444  
445 #ifdef APACHE
446 if (r -> pApacheReq)
447 return ok ;
448 #endif
449  
450 0 ▶ if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
Branch: 2 branches
BranchTrueFalseTotal
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF) 0 0 0
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF) 0 0 0
451 0 PerlIO_close (r -> Component.ifd) ;
452  
453 0 r -> Component.ifd = NULL ;
454  
455 0 return ok ;
456 }
457  
458  
459  
460 /* ---------------------------------------------------------------------------- */
461 /* */
462 /* read block of data from input (web client) */
463 /* */
464 /* ---------------------------------------------------------------------------- */
465  
466  
467 0 int iread (/*i/o*/ register req * r,
468 /*in*/ void * ptr, size_t size)
469  
470 {
471 0 char * p = (char *)ptr ; /* workaround for aix c complier */
472 epTHX ;
473
474 0 ▶ if (size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) 0 0 0
475 0 return 0 ;
476  
477 0 ▶ if (r -> Component.ifdobj)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.ifdobj) 0 0 0
478 {
479 int num ;
480 int n ;
481 SV * pBufSV ;
482  
483 0 dSP;
484 0 ENTER;
485 0 SAVETMPS;
486 0 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 0 0
487 0 ▶ XPUSHs(r -> Component.ifdobj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(r -> Component.ifdobj); 0 0 0
488 0 ▶ XPUSHs(sv_2mortal(pBufSV = NEWSV(0, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(pBufSV = NEWSV(0, 0))); 0 0 0
489 0 ▶ XPUSHs(sv_2mortal(newSViv (size)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv (size))); 0 0 0
490 0 PUTBACK;
491 0 num = perl_call_method ("READ", G_SCALAR) ;
492 0 SPAGAIN;
493 0 n = 0 ;
494 0 ▶ if (num > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (num > 0) 0 0 0
495 {
496 0 STRLEN n = POPu ;
497 char * p ;
498 STRLEN l ;
499 if (n >= 0)
500 {
501 0 p = SvPV (pBufSV, l) ;
502 0 ▶ if (l > size)
Branch: 1 branch
BranchTrueFalseTotal
if (l > size) 0 0 0
503 0 l = size ;
504 0 ▶ if (l > n)
Branch: 1 branch
BranchTrueFalseTotal
if (l > n) 0 0 0
505 0 l = n ;
506 0 memcpy (ptr, p, l) ;
507 }
508 }
509 0 PUTBACK;
510 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
511 0 LEAVE;
512 0 return n ;
513 }
514  
515 #if defined (APACHE)
516 if (r -> pApacheReq)
517 {
518 ap_setup_client_block(r -> pApacheReq, REQUEST_CHUNKED_ERROR);
519 if(ap_should_client_block(r -> pApacheReq))
520 {
521 int c ;
522 int n = 0 ;
523 while (1)
524 {
525 c = ap_get_client_block(r -> pApacheReq, p, size);
526 if (c < 0 || c == 0)
527 return n ;
528 n += c ;
529 p += c ;
530 size -= c ;
531 }
532 }
533 else
534 return 0 ;
535 }
536 #endif
537  
538 0 return PerlIO_read (r -> Component.ifd, p, size) ;
539 }
540  
541  
542 /* ---------------------------------------------------------------------------- */
543 /* */
544 /* read line of data from input (web client) */
545 /* */
546 /* ---------------------------------------------------------------------------- */
547  
548  
549 0 char * igets (/*i/o*/ register req * r,
550 /*in*/ char * s, int size)
551  
552 {
553 #if defined (APACHE)
554 if (r -> pApacheReq)
555 return NULL ;
556 #endif
557  
558 #ifdef PerlIO
559 {
560 /*
561 FILE * f = PerlIO_exportFILE (r -> Component.ifd, 0) ;
562 char * p = fgets (s, size, f) ;
563 PerlIO_releaseFILE (r -> Component.ifd, f) ;
564 return p ;
565 */
566 0 return NULL ;
567 }
568 #else
569 return fgets (s, size, r -> Component.ifd) ;
570 #endif
571 }
572  
573 /* ---------------------------------------------------------------------------- */
574 /* */
575 /* read HTML File into pBuf */
576 /* */
577 /* ---------------------------------------------------------------------------- */
578  
579 0 int ReadHTML (/*i/o*/ register req * r,
580 /*in*/ char * sInputfile,
581 /*in*/ size_t * nFileSize,
582 /*out*/ SV * * ppBuf)
583  
584 {
585 SV * pBufSV ;
586 char * pData ;
587 #ifdef PerlIO
588 PerlIO * ifd ;
589 #else
590 FILE * ifd ;
591 #endif
592 epTHX ;
593
594 0 ▶ if (r -> Component.Config.bDebug)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug) 0 0 0
595 0 lprintf (r -> pApp, "[%d]Reading %s as input using %s (%d Bytes)...\n", r -> pThread -> nPid, sInputfile, FILEIOTYPE, *nFileSize) ;
596  
597 #ifdef WIN32
598 if ((ifd = PerlIO_open (sInputfile, "rb")) == NULL)
599 #else
600 0 ▶ if ((ifd = PerlIO_open (sInputfile, "r")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((ifd = PerlIO_open (sInputfile, "r")) == NULL) 0 0 0
601 #endif
602 {
603 0 strncpy (r -> errdat1, sInputfile, sizeof (r -> errdat1) - 1) ;
604 0 strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
605 0 ▶ if (errno == EACCES)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EACCES) 0 0 0
606 0 return rcForbidden ;
607 0 ▶ else if (errno == ENOENT)
Branch: 1 branch
BranchTrueFalseTotal
else if (errno == ENOENT) 0 0 0
608 0 return rcNotFound ;
609 0 return rcFileOpenErr ;
610 }
611  
612 0 ▶ if ((long)*nFileSize < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((long)*nFileSize < 0) 0 0 0
613 0 return rcFileOpenErr ;
614  
615  
616 0 pBufSV = sv_2mortal (newSV(*nFileSize + 1)) ;
617 0 pData = SvPVX(pBufSV) ;
618  
619 #if EPC_ENABLE
620
621 if (*nFileSize)
622 {
623 int rc ;
624 char * syntax ;
625 int fileno = PerlIO_fileno (ifd) ;
626 FILE * ifile = fdopen(fileno, "r") ;
627 if (!ifile)
628 {
629 strncpy (r -> errdat1, sInputfile, sizeof (r -> errdat1) - 1) ;
630 strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
631 return rcFileOpenErr ;
632 }
633  
634 #ifndef EP2
635 syntax = (r -> Component.pTokenTable && strcmp ((char *)r -> Component.pTokenTable, "Text") == 0)?"Text":"Embperl" ;
636 #else
637 syntax = r -> Component.Config.sSyntax ;
638 #endif
639  
640 if ((rc = do_crypt_file (ifile, NULL, pData, *nFileSize, 0, syntax, EPC_HEADER)) <= 0)
641 {
642 if (rc < -1 || !EPC_UNENCYRPTED)
643 {
644 sprintf (r -> errdat1, "%d", rc) ;
645 return rcCryptoWrongHeader + -rc - 1;
646 }
647  
648 PerlIO_seek (ifd, 0, SEEK_SET) ;
649 *nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
650 }
651 else
652 *nFileSize = rc ;
653 fclose (ifile) ;
654 }
655  
656 #else
657
658 0 ▶ if (*nFileSize)
Branch: 1 branch
BranchTrueFalseTotal
if (*nFileSize) 0 0 0
659 0 *nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
660  
661 #endif
662  
663 0 PerlIO_close (ifd) ;
664
665 0 pData [*nFileSize] = '\0' ;
666 0 SvCUR_set (pBufSV, *nFileSize) ;
667 0 SvTEMP_off (pBufSV) ;
668 0 SvPOK_on (pBufSV) ;
669  
670 0 *ppBuf = pBufSV ;
671  
672 0 return ok ;
673 }
674  
675  
676 /* ---------------------------------------------------------------------------- */
677 /* */
678 /* set the name of the output file and open it */
679 /* */
680 /* ---------------------------------------------------------------------------- */
681  
682  
683  
684 0 int OpenOutput (/*i/o*/ register req * r,
685 /*in*/ const char * sFilename)
686  
687 {
688 MAGIC *mg;
689 GV *handle ;
690 epTHX ;
691
692 0 r -> Component.pOutput -> pFirstBuf = NULL ;
693 0 r -> Component.pOutput -> pLastBuf = NULL ;
694 0 r -> Component.pOutput -> nMarker = 0 ;
695 0 r -> Component.pOutput -> pMemBuf = NULL ;
696 0 r -> Component.pOutput -> nMemBufSize = 0 ;
697 0 r -> Component.pOutput -> pFreeBuf = NULL ;
698 0 r -> Component.pOutput -> pLastFreeBuf = NULL ;
699  
700  
701
702 0 ▶ if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF && !r -> Component.pOutput -> no_ofd_close)
Branch: 3 branches
BranchTrueFalseTotal
if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF && !r -> Component.pOutput -> no_ofd_close) 0 0 0
if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF && !r -> Component.pOutput -> no_ofd_close) 0 0 0
if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF && !r -> Component.pOutput -> no_ofd_close) 0 0 0
703 0 PerlIO_close (r -> Component.pOutput -> ofd) ;
704  
705 0 r -> Component.pOutput -> ofd = NULL ;
706 0 r -> Component.pOutput -> no_ofd_close = 0 ;
707  
708 0 ▶ if (sFilename == NULL || *sFilename == '\0')
Branch: 2 branches
BranchTrueFalseTotal
if (sFilename == NULL || *sFilename == '\0') 0 0 0
if (sFilename == NULL || *sFilename == '\0') 0 0 0
709 {
710 #if defined (APACHE)
711 if (r -> pApacheReq)
712 {
713 if (r -> Component.Config.bDebug)
714 lprintf (r -> pApp, "[%d]Using APACHE for output...\n", r -> pThread -> nPid) ;
715 return ok ;
716 }
717 #endif
718  
719 0 handle = HANDLE_GV("STDOUT") ;
720 0 ▶ if (handle)
Branch: 1 branch
BranchTrueFalseTotal
if (handle) 0 0 0
721 {
722 0 SV *iohandle = TIEHANDLE_SV(handle) ;
723  
724 0 ▶ if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj)
Branch: 4 branches
BranchTrueFalseTotal
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj) 0 0 0
725 {
726 0 r -> Component.pOutput -> ofdobj = mg->mg_obj ;
727 0 ▶ if (r -> Component.Config.bDebug)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug) 0 0 0
728 {
729 0 ▶ char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
Branch: 6 branches
BranchTrueFalseTotal
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj))); 0 0 0
730 0 lprintf (r -> pApp, "[%d]Open TIED STDOUT %s for output...\n", r -> pThread -> nPid, package) ;
731 }
732 0 return ok ;
733 }
734  
735 0 ▶ r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ;
Branch: 5 branches
BranchTrueFalseTotal
r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ; 0 0 0
r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ; 0 0 0
r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ; 0 0 0
r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ; 0 0 0
r -> Component.pOutput -> ofd = IoOFP(GvIOn(handle)) ; 0 0 0
736 0 ▶ if (r -> Component.pOutput -> ofd)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> ofd) 0 0 0
737 {
738 0 r -> Component.pOutput -> no_ofd_close = 1 ;
739 0 return ok ;
740 }
741 }
742
743 0 r -> Component.pOutput -> ofd = PerlIO_stdoutF ;
744
745 0 ▶ if (r -> Component.Config.bDebug)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug) 0 0 0
746 {
747 #ifdef APACHE
748 if (r -> pApacheReq)
749 lprintf (r -> pApp, "[%d]Open STDOUT to Apache for output...\n", r -> pThread -> nPid) ;
750 else
751 #endif
752 0 lprintf (r -> pApp, "[%d]Open STDOUT for output...\n", r -> pThread -> nPid) ;
753 }
754 0 return ok ;
755 }
756  
757 0 ▶ if (r -> Component.Config.bDebug)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug) 0 0 0
758 0 lprintf (r -> pApp, "[%d]Open %s for output...\n", r -> pThread -> nPid, sFilename) ;
759  
760 #ifdef WIN32
761 if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "wb")) == NULL)
762 #else
763 0 ▶ if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "w")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "w")) == NULL) 0 0 0
764 #endif
765 {
766 0 strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
767 0 strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
768 0 return rcFileOpenErr ;
769 }
770  
771 0 return ok ;
772 }
773  
774  
775 /* ---------------------------------------------------------------------------- */
776 /* */
777 /* close the output file */
778 /* */
779 /* ---------------------------------------------------------------------------- */
780  
781  
782 0 int CloseOutput (/*in*/ tReq * r,
783 tComponentOutput * pOutput)
784  
785 {
786 epTHX ;
787
788 0 ▶ if (!pOutput)
Branch: 1 branch
BranchTrueFalseTotal
if (!pOutput) 0 0 0
789 0 return ok ;
790  
791 #if 0
792 if (0) /* r -> Component.pOutput -> ofdobj) */
793 {
794 dSP;
795 ENTER;
796 SAVETMPS;
797 PUSHMARK(sp);
798 XPUSHs(pOutput -> ofdobj);
799 PUTBACK;
800 perl_call_method ("CLOSE", G_VOID | G_EVAL) ;
801 SPAGAIN ;
802 FREETMPS;
803 LEAVE;
804 pOutput -> ofdobj = NULL ;
805 }
806 #endif
807  
808 0 ▶ if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF && !pOutput -> no_ofd_close)
Branch: 3 branches
BranchTrueFalseTotal
if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF && !pOutput -> no_ofd_close) 0 0 0
if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF && !pOutput -> no_ofd_close) 0 0 0
if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF && !pOutput -> no_ofd_close) 0 0 0
809 0 PerlIO_close (pOutput -> ofd) ;
810  
811 0 pOutput -> ofd = NULL ;
812  
813 0 return ok ;
814 }
815  
816  
817 /* ---------------------------------------------------------------------------- */
818 /* */
819 /* set output to memory buffer */
820 /* */
821 /* ---------------------------------------------------------------------------- */
822  
823  
824  
825 0 void OutputToMemBuf (/*i/o*/ register req * r,
826 /*in*/ char * pBuf,
827 /*in*/ size_t nBufSize)
828  
829 {
830 0 ▶ if (pBuf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pBuf == NULL) 0 0 0
831 0 pBuf = ep_palloc (r -> Component.pOutput -> pPool, nBufSize) ;
832  
833 0 *pBuf = '\0' ;
834 0 r -> Component.pOutput -> pMemBuf = pBuf ;
835 0 r -> Component.pOutput -> pMemBufPtr = pBuf ;
836 0 r -> Component.pOutput -> nMemBufSize = nBufSize ;
837 0 r -> Component.pOutput -> nMemBufSizeFree = nBufSize ;
838 0 }
839  
840  
841 /* ---------------------------------------------------------------------------- */
842 /* */
843 /* set output to standard */
844 /* */
845 /* ---------------------------------------------------------------------------- */
846  
847  
848 0 char * OutputToStd (/*i/o*/ register req * r)
849  
850 {
851 0 char * p = r -> Component.pOutput -> pMemBuf ;
852 0 r -> Component.pOutput -> pMemBuf = NULL ;
853 0 r -> Component.pOutput -> nMemBufSize = 0 ;
854 0 r -> Component.pOutput -> nMemBufSizeFree = 0 ;
855 0 return p ;
856 }
857  
858  
859  
860 /* ---------------------------------------------------------------------------- */
861 /* */
862 /* puts to output (web client) */
863 /* */
864 /* ---------------------------------------------------------------------------- */
865  
866 0 int oputs (/*i/o*/ register req * r,
867 /*in*/ const char * str)
868  
869 {
870 0 return owrite (r, str, strlen (str)) ;
871 }
872  
873  
874 /* ---------------------------------------------------------------------------- */
875 /* */
876 /* write block of data to output (web client) */
877 /* */
878 /* ---------------------------------------------------------------------------- */
879  
880 0 int owrite (/*i/o*/ register req * r,
881 /*in*/ const void * ptr, size_t size)
882  
883 {
884 0 size_t n = size ;
885 epTHX ;
886  
887 0 ▶ if (n == 0 || r -> Component.pOutput -> bDisableOutput)
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || r -> Component.pOutput -> bDisableOutput) 0 0 0
if (n == 0 || r -> Component.pOutput -> bDisableOutput) 0 0 0
888 0 return 0 ;
889  
890 0 ▶ if (r -> Component.pOutput -> pMemBuf)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> pMemBuf) 0 0 0
891 {
892 char * p ;
893 0 size_t s = r -> Component.pOutput -> nMemBufSize ;
894 0 ▶ if (n >= r -> Component.pOutput -> nMemBufSizeFree)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= r -> Component.pOutput -> nMemBufSizeFree) 0 0 0
895 {
896 0 size_t oldsize = s ;
897 0 ▶ if (s < n)
Branch: 1 branch
BranchTrueFalseTotal
if (s < n) 0 0 0
898 0 s = n + r -> Component.pOutput -> nMemBufSize ;
899
900 0 r -> Component.pOutput -> nMemBufSize += s ;
901 0 r -> Component.pOutput -> nMemBufSizeFree += s ;
902 /*lprintf (r -> pApp, "[%d]MEM: Realloc pMemBuf, nMemSize = %d\n", nPid, nMemBufSize) ; */
903
904 0 p = ep_palloc (r -> Component.pOutput -> pPool, r -> Component.pOutput -> nMemBufSize) ;
905 0 ▶ if (p == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) 0 0 0
906 {
907 0 r -> Component.pOutput -> nMemBufSize -= s ;
908 0 r -> Component.pOutput -> nMemBufSizeFree -= s ;
909 0 return 0 ;
910 }
911 0 memcpy (p, r -> Component.pOutput -> pMemBuf, oldsize) ;
912 0 r -> Component.pOutput -> pMemBufPtr = p + (r -> Component.pOutput -> pMemBufPtr - r -> Component.pOutput -> pMemBuf) ;
913 0 r -> Component.pOutput -> pMemBuf = p ;
914 }
915
916 0 memcpy (r -> Component.pOutput -> pMemBufPtr, ptr, n) ;
917 0 r -> Component.pOutput -> pMemBufPtr += n ;
918 0 *(r -> Component.pOutput -> pMemBufPtr) = '\0' ;
919 0 r -> Component.pOutput -> nMemBufSizeFree -= n ;
920 0 return n ;
921 }
922  
923
924 0 ▶ if (r -> Component.pOutput -> nMarker)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> nMarker) 0 0 0
925 0 return bufwrite (r, ptr, n) ;
926  
927 0 ▶ if (r -> Component.pOutput -> ofdobj)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> ofdobj) 0 0 0
928 {
929 0 dSP;
930 0 ENTER;
931 0 SAVETMPS;
932 0 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 0 0
933 0 ▶ XPUSHs(r -> Component.pOutput -> ofdobj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(r -> Component.pOutput -> ofdobj); 0 0 0
934 0 ▶ XPUSHs(sv_2mortal(newSVpv((char *)ptr,size)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv((char *)ptr,size))); 0 0 0
935 0 PUTBACK;
936 0 perl_call_method ("PRINT", G_SCALAR) ;
937 0 SPAGAIN ;
938 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
939 0 LEAVE;
940 0 return size ;
941 }
942  
943 #if defined (APACHE)
944 if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
945 {
946 if (n > 0)
947 {
948 n = ap_rwrite (ptr, n, r -> pApacheReq) ;
949 if (r -> Component.Config.bDebug & dbgFlushOutput)
950 ap_rflush (r -> pApacheReq) ;
951 return n ;
952 }
953 else
954 return 0 ;
955 }
956 #endif
957 0 ▶ if (n > 0 && r -> Component.pOutput -> ofd)
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0 && r -> Component.pOutput -> ofd) 0 0 0
if (n > 0 && r -> Component.pOutput -> ofd) 0 0 0
958 {
959 0 n = PerlIO_write (r -> Component.pOutput -> ofd, (void *)ptr, size) ;
960  
961 0 ▶ if (r -> Component.Config.bDebug & dbgFlushOutput)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug & dbgFlushOutput) 0 0 0
962 0 PerlIO_flush (r -> Component.pOutput -> ofd) ;
963 }
964  
965 0 return n ;
966 }
967  
968  
969 /* ---------------------------------------------------------------------------- */
970 /* */
971 /* flush output */
972 /* */
973 /* ---------------------------------------------------------------------------- */
974  
975 0 void oflush (/*i/o*/ register req * r)
976  
977 {
978 epTHX_
979  
980 #if defined (APACHE)
981 if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
982 {
983 ap_rflush (r -> pApacheReq) ;
984 return ;
985 }
986 #endif
987 0 ▶ if (r -> Component.pOutput -> ofd)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.pOutput -> ofd) 0 0 0
988 {
989 0 PerlIO_flush (r -> Component.pOutput -> ofd) ;
990 }
991  
992 0 return ;
993 }
994  
995  
996  
997 /* ---------------------------------------------------------------------------- */
998 /* */
999 /* write one char to output (web client) */
1000 /* */
1001 /* ---------------------------------------------------------------------------- */
1002  
1003  
1004 0 void oputc (/*i/o*/ register req * r,
1005 /*in*/ char c)
1006  
1007 {
1008 epTHX ;
1009
1010 0 ▶ if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj)
Branch: 3 branches
BranchTrueFalseTotal
if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj) 0 0 0
if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj) 0 0 0
if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj) 0 0 0
1011 {
1012 0 owrite (r, &c, 1) ;
1013 0 return ;
1014 }
1015  
1016 #if defined (APACHE)
1017 if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
1018 {
1019 ap_rputc (c, r -> pApacheReq) ;
1020 if (r -> Component.Config.bDebug & dbgFlushOutput)
1021 ap_rflush (r -> pApacheReq) ;
1022 return ;
1023 }
1024 #endif
1025 0 PerlIO_putc (r -> Component.pOutput -> ofd, c) ;
1026  
1027 0 ▶ if (r -> Component.Config.bDebug & dbgFlushOutput)
Branch: 1 branch
BranchTrueFalseTotal
if (r -> Component.Config.bDebug & dbgFlushOutput) 0 0 0
1028 0 PerlIO_flush (r -> Component.pOutput -> ofd) ;
1029 }
1030  
1031  
1032  
1033 /* ---------------------------------------------------------------------------- */
1034 /* */
1035 /* log file open */
1036 /* */
1037 /* ---------------------------------------------------------------------------- */
1038  
1039  
1040 0 int OpenLog (/*i/o*/ tApp * a)
1041  
1042 {
1043 epaTHX ;
1044
1045 0 ▶ if (a -> lfd)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd) 0 0 0
1046 0 return ok ; /*already open */
1047  
1048 0 ▶ if (a -> lfd && a -> lfd != PerlIO_stdoutF)
Branch: 2 branches
BranchTrueFalseTotal
if (a -> lfd && a -> lfd != PerlIO_stdoutF) 0 0 0
if (a -> lfd && a -> lfd != PerlIO_stdoutF) 0 0 0
1049 0 PerlIO_close (a -> lfd) ; /* close old logfile */
1050
1051 0 a -> lfd = NULL ;
1052  
1053
1054 0 ▶ if (a -> Config.bDebug == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> Config.bDebug == 0) 0 0 0
1055 0 return ok ; /* never write to logfile if debugging is disabled */
1056
1057 0 ▶ if (!a -> Config.sLog && a -> Config.sLog[0] == '\0')
Branch: 2 branches
BranchTrueFalseTotal
if (!a -> Config.sLog && a -> Config.sLog[0] == '\0') 0 0 0
if (!a -> Config.sLog && a -> Config.sLog[0] == '\0') 0 0 0
1058 {
1059 0 a -> lfd = PerlIO_stdoutF ;
1060 0 return ok ;
1061 }
1062  
1063 0 ▶ if ((a -> lfd = PerlIO_open (a -> Config.sLog, "a")) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((a -> lfd = PerlIO_open (a -> Config.sLog, "a")) == NULL) 0 0 0
1064 {
1065 0 tReq * r = a -> pThread -> pCurrReq ;
1066 0 ▶ if (r)
Branch: 1 branch
BranchTrueFalseTotal
if (r) 0 0 0
1067 {
1068 0 strncpy (r -> errdat1, a -> Config.sLog, sizeof (r -> errdat1) - 1) ;
1069 0 strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
1070 }
1071 0 return rcLogFileOpenErr ;
1072 }
1073  
1074 0 return ok ;
1075 }
1076  
1077 /* ---------------------------------------------------------------------------- */
1078 /* */
1079 /* return the handle of the log file */
1080 /* */
1081 /* ---------------------------------------------------------------------------- */
1082  
1083  
1084 0 int GetLogHandle (/*i/o*/ tApp * a)
1085  
1086 {
1087 epaTHX ;
1088  
1089 0 ▶ if (a -> lfd)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd) 0 0 0
1090 0 return PerlIO_fileno (a -> lfd) ;
1091  
1092 0 return 0 ;
1093 }
1094  
1095  
1096 /* ---------------------------------------------------------------------------- */
1097 /* */
1098 /* return the current posittion of the log file */
1099 /* */
1100 /* ---------------------------------------------------------------------------- */
1101  
1102  
1103 0 long GetLogFilePos (/*i/o*/ tApp * a)
1104  
1105 {
1106 epaTHX ;
1107  
1108 0 ▶ if (a -> lfd)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd) 0 0 0
1109 0 return PerlIO_tell (a -> lfd) ;
1110  
1111 0 return 0 ;
1112 }
1113  
1114  
1115 /* ---------------------------------------------------------------------------- */
1116 /* */
1117 /* close the log file */
1118 /* */
1119 /* ---------------------------------------------------------------------------- */
1120  
1121  
1122 0 int CloseLog (/*i/o*/ tApp * a)
1123  
1124 {
1125 epaTHX ;
1126  
1127 0 ▶ if (a -> lfd && a -> lfd != PerlIO_stdoutF)
Branch: 2 branches
BranchTrueFalseTotal
if (a -> lfd && a -> lfd != PerlIO_stdoutF) 0 0 0
if (a -> lfd && a -> lfd != PerlIO_stdoutF) 0 0 0
1128 0 PerlIO_close (a -> lfd) ;
1129  
1130 0 a -> lfd = NULL ;
1131  
1132 0 return ok ;
1133 }
1134  
1135  
1136  
1137 /* ---------------------------------------------------------------------------- */
1138 /* */
1139 /* flush the log file */
1140 /* */
1141 /* ---------------------------------------------------------------------------- */
1142  
1143  
1144 0 int FlushLog (/*i/o*/ tApp * a)
1145  
1146 {
1147 epaTHX ;
1148  
1149 0 ▶ if (a -> lfd != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd != NULL) 0 0 0
1150 0 PerlIO_flush (a -> lfd) ;
1151  
1152 0 return ok ;
1153 }
1154  
1155  
1156  
1157 /* ---------------------------------------------------------------------------- */
1158 /* */
1159 /* printf to log file */
1160 /* */
1161 /* ---------------------------------------------------------------------------- */
1162  
1163 0 int lprintf (/*i/o*/ tApp * a,
1164 /*in*/ const char * sFormat,
1165 /*in*/ ...)
1166  
1167 {
1168 va_list ap ;
1169 int n ;
1170 epaTHX ;
1171  
1172 0 ▶ if (a -> lfd == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd == NULL) 0 0 0
1173 0 return 0 ;
1174
1175 0 va_start (ap, sFormat) ;
1176
1177 {
1178 0 n = PerlIO_vprintf (a -> lfd, sFormat, ap) ;
1179 0 ▶ if (a -> Config.bDebug & dbgFlushLog)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> Config.bDebug & dbgFlushLog) 0 0 0
1180 0 PerlIO_flush (a -> lfd) ;
1181 }
1182  
1183  
1184 0 va_end (ap) ;
1185  
1186 0 return n ;
1187 }
1188  
1189  
1190 /* ---------------------------------------------------------------------------- */
1191 /* */
1192 /* write block of data to log file */
1193 /* */
1194 /* ---------------------------------------------------------------------------- */
1195  
1196  
1197 0 int lwrite (/*i/o*/ tApp * a,
1198 /*in*/ const void * ptr,
1199 /*in*/ size_t size)
1200  
1201 {
1202 int n ;
1203 epaTHX ;
1204  
1205 0 ▶ if (a -> lfd == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> lfd == NULL) 0 0 0
1206 0 return 0 ;
1207
1208 0 n = PerlIO_write (a -> lfd, (void *)ptr, size) ;
1209  
1210 0 ▶ if (a -> Config.bDebug & dbgFlushLog)
Branch: 1 branch
BranchTrueFalseTotal
if (a -> Config.bDebug & dbgFlushLog) 0 0 0
1211 0 PerlIO_flush (a -> lfd) ;
1212  
1213 0 return n ;
1214 }
1215  
1216  
1217  
1218  
1219 /* ---------------------------------------------------------------------------- */
1220 /* */
1221 /* Memory Allocation */
1222 /* */
1223 /* ---------------------------------------------------------------------------- */
1224  
1225  
1226 0 void _free (/*i/o*/ register req * r,
1227 void * p)
1228  
1229 {
1230 #ifdef APACHE
1231 if (pAllocReq && !(r -> Component.Config.bDebug & dbgMem))
1232 return ;
1233 #endif
1234  
1235  
1236 #ifdef ALLOCSIZE
1237 if (r -> Component.Config.bDebug & dbgMem)
1238 {
1239 size_t size ;
1240 size_t * ps ;
1241  
1242 /* we do it a bit complicted so it compiles also on aix */
1243 ps = (size_t *)p ;
1244 ps-- ;
1245 size = *ps ;
1246 p = ps ;
1247 r -> nAllocSize -= size ;
1248 lprintf (r -> pApp, "[%d]MEM: Free %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1249 }
1250 #endif
1251  
1252 #ifdef APACHE
1253 if (r -> pApacheReq == NULL)
1254 #endif
1255 {
1256 epTHX ;
1257 0 free (p) ;
1258 }
1259 0 }
1260  
1261 0 void * _malloc (/*i/o*/ register req * r,
1262 size_t size)
1263  
1264 {
1265 void * p ;
1266  
1267 #ifdef APACHE
1268 pAllocReq = r -> pApacheReq ;
1269  
1270 if (r -> pApacheReq)
1271 {
1272 p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
1273 }
1274 else
1275 #endif
1276 {
1277 epTHX ;
1278
1279 0 p = malloc (size + sizeof (size)) ;
1280 }
1281  
1282 #ifdef ALLOCSIZE
1283 if (r -> Component.Config.bDebug & dbgMem)
1284 {
1285 size_t * ps ;
1286 /* we do it a bit complicted so it compiles also on aix */
1287 ps = (size_t *)p ;
1288 *ps = size ;
1289 p = ps + 1 ;
1290  
1291 r -> nAllocSize += size ;
1292 lprintf (r -> pApp, "[%d]MEM: Alloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1293 }
1294 #endif
1295  
1296 0 return p ;
1297 }
1298  
1299 0 void * _realloc (/*i/o*/ register req * r, void * ptr, size_t oldsize, size_t size)
1300  
1301 {
1302 void * p ;
1303
1304 #ifdef APACHE
1305 if (r -> pApacheReq)
1306 {
1307 p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
1308 if (p == NULL)
1309 return NULL ;
1310
1311 #ifdef ALLOCSIZE
1312 if (r -> Component.Config.bDebug & dbgMem)
1313 {
1314 size_t * ps ;
1315 size_t sizeold ;
1316 /* we do it a bit complicted so it compiles also on aix */
1317 ps = (size_t *)p ;
1318 *ps = size ;
1319 p = ps + 1;
1320
1321 ps = (size_t *)ptr ;
1322 ps-- ;
1323 sizeold = *ps ;
1324 r -> nAllocSize += size - sizeold ;
1325  
1326 lprintf (r -> pApp, "[%d]MEM: ReAlloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1327 }
1328 #endif
1329  
1330 memcpy (p, ptr, oldsize) ;
1331 }
1332 else
1333 #endif
1334 #ifdef ALLOCSIZE
1335 if (r -> Component.Config.bDebug & dbgMem)
1336 {
1337 size_t * ps ;
1338 ps = (size_t *)ptr ;
1339 ps-- ;
1340 r -> nAllocSize -= *ps ;
1341
1342 p = realloc (ps, size + sizeof (size)) ;
1343 if (p == NULL)
1344 return NULL ;
1345  
1346 /* we do it a bit complicted so it compiles also on aix */
1347 ps = (size_t *)p ;
1348 *ps = size ;
1349 p = ps + 1;
1350 r -> nAllocSize += size ;
1351 lprintf (r -> pApp, "[%d]MEM: ReAlloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1352 }
1353 else
1354 #endif
1355 {
1356 epTHX ;
1357 0 p = realloc (ptr, size + sizeof (size)) ;
1358 }
1359  
1360 0 return p ;
1361 }
1362  
1363  
1364 0 char * _memstrcat (/*i/o*/ register req * r,
1365 const char *s, ...)
1366  
1367 {
1368 va_list ap ;
1369 char * p ;
1370 char * str ;
1371 char * sp ;
1372 int l ;
1373 int sum ;
1374  
1375 EPENTRY(_memstrcat) ;
1376  
1377 0 va_start(ap, s) ;
1378  
1379 0 p = (char *)s ;
1380 0 sum = 0 ;
1381 0 ▶ while (p)
Branch: 1 branch
BranchTrueFalseTotal
while (p) 0 0 0
1382 {
1383 0 sum += strlen (p) ;
1384 0 lprintf (r -> pApp, "sum = %d p = %s\n", sum, p) ;
1385 0 p = va_arg (ap, char *) ;
1386 }
1387 0 sum++ ;
1388  
1389 0 va_end (ap) ;
1390  
1391 0 sp = str = _malloc (r, sum+1) ;
1392  
1393 0 va_start(ap, s) ;
1394  
1395 0 p = (char *)s ;
1396 0 ▶ while (p)
Branch: 1 branch
BranchTrueFalseTotal
while (p) 0 0 0
1397 {
1398 0 l = strlen (p) ;
1399 0 lprintf (r -> pApp, "l = %d p = %s\n", l, p) ;
1400 0 memcpy (str, p, l) ;
1401 0 str += l ;
1402 0 p = va_arg (ap, char *) ;
1403 }
1404 0 *str = '\0' ;
1405  
1406 0 va_end (ap) ;
1407  
1408  
1409 0 return sp ;
1410 }
1411  
1412  
1413  
1414 0 char * _ep_strdup (/*i/o*/ register req * r,
1415 /*in*/ const char * str)
1416  
1417 {
1418 char * p ;
1419 0 int len = strlen (str) ;
1420  
1421 0 p = (char *)_malloc (r, len + 1) ;
1422  
1423 0 ▶ if (p)
Branch: 1 branch
BranchTrueFalseTotal
if (p) 0 0 0
1424 0 strcpy (p, str) ;
1425  
1426 0 return p ;
1427 }
1428
1429  
1430  
1431 0 char * _ep_strndup (/*i/o*/ register req * r,
1432 /*in*/ const char * str,
1433 /*in*/ int len)
1434  
1435 {
1436 char * p ;
1437  
1438 0 p = (char *)_malloc (r, len + 1) ;
1439  
1440 0 ▶ if (p)
Branch: 1 branch
BranchTrueFalseTotal
if (p) 0 0 0
1441 {
1442 0 strncpy (p, str, len) ;
1443  
1444 0 p[len] = '\0' ;
1445 }
1446  
1447 0 return p ;
1448 }
1449
1450 0 char * _ep_memdup (/*i/o*/ register req * r,
1451 /*in*/ const char * str,
1452 /*in*/ int len)
1453  
1454 {
1455 char * p ;
1456  
1457 0 p = (char *)_malloc (r, len + 1) ;
1458  
1459 0 ▶ if (p)
Branch: 1 branch
BranchTrueFalseTotal
if (p) 0 0 0
1460 {
1461 0 memcpy (p, str, len) ;
1462  
1463 0 p[len] = '\0' ;
1464 }
1465  
1466 0 return p ;
1467 }
1468
« epinit.c ↑ Summary epmain.c »