home *** CD-ROM | disk | FTP | other *** search
/ Netscape Plug-Ins Developer's Kit / Netscape_Plug-Ins_Developers_Kit.iso / CGIPERL / MACPERL / MSRCE418.HQX / Perl Source ƒ / Perl / usersub.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  4.4 KB  |  210 lines

  1. /* $RCSfile: usersub.c,v $$Revision: 4.0.1.2 $$Date: 92/06/08 16:04:24 $
  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.2  92/06/08  16:04:24  lwall
  9.  * patch20: removed implicit int declarations on functions
  10.  * 
  11.  * Revision 4.0.1.1  91/11/11  16:47:17  lwall
  12.  * patch19: deleted some unused functions from usersub.c
  13.  * 
  14.  * Revision 4.0  91/03/20  01:55:56  lwall
  15.  * 4.0 baseline.
  16.  * 
  17.  */
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21.  
  22. static int usersub();
  23.  
  24. int
  25. userinit()
  26. {
  27.     return 0;
  28. }
  29.  
  30. /* Be sure to refetch the stack pointer after calling these routines. */
  31. #ifdef macintosh
  32. static ARG myarg[3];    /* fake syntax tree node */
  33. #endif
  34.  
  35. int
  36. callback(subname, sp, gimme, hasargs, numargs)
  37. char *subname;
  38. int sp;            /* stack pointer after args are pushed */
  39. int gimme;        /* called in array or scalar context */
  40. int hasargs;        /* whether to create a @_ array for routine */
  41. int numargs;        /* how many args are pushed on the stack */
  42. {
  43. #ifndef macintosh
  44.     static ARG myarg[3];    /* fake syntax tree node */
  45. #endif
  46.     int arglast[3];
  47.  
  48.     arglast[2] = sp;
  49.     sp -= numargs;
  50.     arglast[1] = sp--;
  51.     arglast[0] = sp;
  52.  
  53.     if (!myarg[0].arg_ptr.arg_str)
  54.     myarg[0].arg_ptr.arg_str = str_make("",0);
  55.  
  56.     myarg[1].arg_type = A_WORD;
  57.     myarg[1].arg_ptr.arg_stab = stabent(subname, FALSE);
  58.  
  59.     myarg[2].arg_type = hasargs ? A_EXPR : A_NULL;
  60.  
  61.     return do_subr(myarg, gimme, arglast);
  62. }
  63.  
  64. int
  65. callv(subname, sp, gimme, argv)
  66. char *subname;
  67. register int sp;    /* current stack pointer */
  68. int gimme;        /* called in array or scalar context */
  69. register char **argv;    /* null terminated arg list, NULL for no arglist */
  70. {
  71.     register int items = 0;
  72.     int hasargs = (argv != 0);
  73.  
  74.     astore(stack, ++sp, Nullstr);    /* reserve spot for 1st return arg */
  75.     if (hasargs) {
  76.     while (*argv) {
  77.         astore(stack, ++sp, str_2mortal(str_make(*argv,0)));
  78.         items++;
  79.         argv++;
  80.     }
  81.     }
  82.     return callback(subname, sp, gimme, hasargs, items);
  83. }
  84.  
  85. /*
  86.  * The following is supplied by John Macdonald as a means of decrypting
  87.  * and executing (presumably proprietary) scripts that have been encrypted
  88.  * by a (presumably secret) method.  The idea is that you supply your own
  89.  * routine in place of cryptfilter (which is purposefully a very weak
  90.  * encryption).  If an encrypted script is detected, a process is forked
  91.  * off to run the cryptfilter routine as input to perl.
  92.  */
  93.  
  94. #ifdef CRYPTSCRIPT
  95.  
  96. #include <signal.h>
  97. #ifdef I_VFORK
  98. #include <vfork.h>
  99. #endif
  100.  
  101. #ifdef CRYPTLOCAL
  102.  
  103. #include "cryptlocal.h"
  104.  
  105. #else    /* ndef CRYPTLOCAL */
  106.  
  107. #define    CRYPT_MAGIC_1    0xfb
  108. #define    CRYPT_MAGIC_2    0xf1
  109.  
  110. void
  111. cryptfilter( fil )
  112. FILE *    fil;
  113. {
  114.     int    ch;
  115.  
  116.     while( (ch = getc( fil )) != EOF ) {
  117.     putchar( (ch ^ 0x80) );
  118.     }
  119. }
  120.  
  121. #endif    /* CRYPTLOCAL */
  122.  
  123. #ifndef MSDOS
  124. static FILE    *lastpipefile;
  125. static int    pipepid;
  126.  
  127. #ifdef VOIDSIG
  128. #  define    VOID    void
  129. #else
  130. #  define    VOID    int
  131. #endif
  132.  
  133. FILE *
  134. mypfiopen(fil,func)        /* open a pipe to function call for input */
  135. FILE    *fil;
  136. VOID    (*func)();
  137. {
  138.     int p[2];
  139.     STR *str;
  140.  
  141.     if (pipe(p) < 0) {
  142.     fclose( fil );
  143.     fatal("Can't get pipe for decrypt");
  144.     }
  145.  
  146.     /* make sure that the child doesn't get anything extra */
  147.     fflush(stdout);
  148.     fflush(stderr);
  149.  
  150.     while ((pipepid = fork()) < 0) {
  151.     if (errno != EAGAIN) {
  152.         close(p[0]);
  153.         close(p[1]);
  154.         fclose( fil );
  155.         fatal("Can't fork for decrypt");
  156.     }
  157.     sleep(5);
  158.     }
  159.     if (pipepid == 0) {
  160.     close(p[0]);
  161.     if (p[1] != 1) {
  162.         dup2(p[1], 1);
  163.         close(p[1]);
  164.     }
  165.     (*func)(fil);
  166.     fflush(stdout);
  167.     fflush(stderr);
  168.     _exit(0);
  169.     }
  170.     close(p[1]);
  171.     close(fileno(fil));
  172.     fclose(fil);
  173.     str = afetch(fdpid,p[0],TRUE);
  174.     str->str_u.str_useful = pipepid;
  175.     return fdopen(p[0], "r");
  176. }
  177.  
  178. void
  179. cryptswitch()
  180. {
  181.     int ch;
  182. #ifdef STDSTDIO
  183.     /* cheat on stdio if possible */
  184.     if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
  185.     return;
  186. #endif
  187.     ch = getc(rsfp);
  188.     if (ch == CRYPT_MAGIC_1) {
  189.     if (getc(rsfp) == CRYPT_MAGIC_2) {
  190.         if( perldb ) fatal("can't debug an encrypted script");
  191.         rsfp = mypfiopen( rsfp, cryptfilter );
  192.         preprocess = 1;    /* force call to pclose when done */
  193.     }
  194.     else
  195.         fatal( "bad encryption format" );
  196.     }
  197.     else
  198.     ungetc(ch,rsfp);
  199. }
  200. #endif /* !MSDOS */
  201.  
  202. #endif /* CRYPTSCRIPT */
  203.  
  204. #ifdef macintosh
  205. void reinit_usersub()
  206. {
  207.    memset(myarg, 0, 3*sizeof(ARG));
  208. }
  209. #endif
  210.