perl 4.0 patch 31: patch #20, continued
[p5sagit/p5-mst-13.2.git] / usersub.c
1 /* $RCSfile: usersub.c,v $$Revision: 4.0.1.1 $$Date: 91/11/11 16:47:17 $
2  *
3  *  This file contains stubs for routines that the user may define to
4  *  set up glue routines for C libraries or to decrypt encrypted scripts
5  *  for execution.
6  *
7  * $Log:        usersub.c,v $
8  * Revision 4.0.1.1  91/11/11  16:47:17  lwall
9  * patch19: deleted some unused functions from usersub.c
10  * 
11  * Revision 4.0  91/03/20  01:55:56  lwall
12  * 4.0 baseline.
13  * 
14  */
15
16 #include "EXTERN.h"
17 #include "perl.h"
18
19 userinit()
20 {
21     return 0;
22 }
23
24 /*
25  * The following is supplied by John Macdonald as a means of decrypting
26  * and executing (presumably proprietary) scripts that have been encrypted
27  * by a (presumably secret) method.  The idea is that you supply your own
28  * routine in place of cryptfilter (which is purposefully a very weak
29  * encryption).  If an encrypted script is detected, a process is forked
30  * off to run the cryptfilter routine as input to perl.
31  */
32
33 #ifdef CRYPTSCRIPT
34
35 #include <signal.h>
36 #ifdef I_VFORK
37 #include <vfork.h>
38 #endif
39
40 #ifdef CRYPTLOCAL
41
42 #include "cryptlocal.h"
43
44 #else   /* ndef CRYPTLOCAL */
45
46 #define CRYPT_MAGIC_1   0xfb
47 #define CRYPT_MAGIC_2   0xf1
48
49 cryptfilter( fil )
50 FILE *  fil;
51 {
52     int    ch;
53
54     while( (ch = getc( fil )) != EOF ) {
55         putchar( (ch ^ 0x80) );
56     }
57 }
58
59 #endif  /* CRYPTLOCAL */
60
61 #ifndef MSDOS
62 static FILE     *lastpipefile;
63 static int      pipepid;
64
65 #ifdef VOIDSIG
66 #  define       VOID    void
67 #else
68 #  define       VOID    int
69 #endif
70
71 FILE *
72 mypfiopen(fil,func)             /* open a pipe to function call for input */
73 FILE    *fil;
74 VOID    (*func)();
75 {
76     int p[2];
77     STR *str;
78
79     if (pipe(p) < 0) {
80         fclose( fil );
81         fatal("Can't get pipe for decrypt");
82     }
83
84     /* make sure that the child doesn't get anything extra */
85     fflush(stdout);
86     fflush(stderr);
87
88     while ((pipepid = fork()) < 0) {
89         if (errno != EAGAIN) {
90             close(p[0]);
91             close(p[1]);
92             fclose( fil );
93             fatal("Can't fork for decrypt");
94         }
95         sleep(5);
96     }
97     if (pipepid == 0) {
98         close(p[0]);
99         if (p[1] != 1) {
100             dup2(p[1], 1);
101             close(p[1]);
102         }
103         (*func)(fil);
104         fflush(stdout);
105         fflush(stderr);
106         _exit(0);
107     }
108     close(p[1]);
109     close(fileno(fil));
110     fclose(fil);
111     str = afetch(fdpid,p[0],TRUE);
112     str->str_u.str_useful = pipepid;
113     return fdopen(p[0], "r");
114 }
115
116 cryptswitch()
117 {
118     int ch;
119 #ifdef STDSTDIO
120     /* cheat on stdio if possible */
121     if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
122         return;
123 #endif
124     ch = getc(rsfp);
125     if (ch == CRYPT_MAGIC_1) {
126         if (getc(rsfp) == CRYPT_MAGIC_2) {
127             if( perldb ) fatal("can't debug an encrypted script");
128             rsfp = mypfiopen( rsfp, cryptfilter );
129             preprocess = 1;     /* force call to pclose when done */
130         }
131         else
132             fatal( "bad encryption format" );
133     }
134     else
135         ungetc(ch,rsfp);
136 }
137 #endif /* !MSDOS */
138
139 #endif /* CRYPTSCRIPT */