]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/config.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / servers / slapd / back-perl / config.c
1 /* $OpenLDAP$ */
2 /*
3  *       Copyright 1999, John C. Quillan, All rights reserved.
4  *
5  *       Redistribution and use in source and binary forms are permitted only
6  *       as authorized by the OpenLDAP Public License.  A copy of this
7  *       license is available at http://www.OpenLDAP.org/license.html or
8  *       in file LICENSE in the top-level directory of the distribution.
9  */
10
11 #include "portable.h"
12 /* init.c - initialize shell backend */
13         
14 #include <stdio.h>
15 /*      #include <ac/types.h>
16         #include <ac/socket.h>
17 */
18
19 #include <EXTERN.h>
20 #include <perl.h>
21
22 #include "slap.h"
23 #include "perl_back.h"
24
25
26 /**********************************************************
27  *
28  * Config
29  *
30  **********************************************************/
31 int
32 perl_back_db_config(
33          BackendDB *be,
34          const char *fname,
35          int lineno,
36          int argc,
37          char **argv
38 )
39 {
40         SV* loc_sv;
41         PerlBackend *perl_back = (PerlBackend *) be->be_private;
42         char eval_str[EVAL_BUF_SIZE];
43         int count ;
44         int args;
45         int return_code;
46         
47
48         if ( strcasecmp( argv[0], "perlModule" ) == 0 ) {
49                 if ( argc < 2 ) {
50                         Debug( LDAP_DEBUG_ANY,
51                                  "%s.pm: line %d: missing module in \"perlModule <module>\" line\n",
52                                 fname, lineno, 0 );
53                         return( 1 );
54                 }
55
56                 strncpy(eval_str, argv[1], EVAL_BUF_SIZE );
57
58                 perl_require_pv( strcat( eval_str, ".pm" ));
59
60                 if (SvTRUE(GvSV(errgv))) {
61                         fprintf(stderr , "Error %s\n", SvPV(GvSV(errgv), na)) ;
62
63                 } else {
64                         dSP; ENTER; SAVETMPS;
65                         PUSHMARK(sp);
66                         XPUSHs(sv_2mortal(newSVpv(argv[1], 0)));
67                         PUTBACK;
68
69                         count = perl_call_method("new", G_SCALAR);
70                         
71                         SPAGAIN;
72
73                         if (count != 1) {
74                                 croak("Big trouble in config\n") ;
75                         }
76
77                         perl_back->pb_obj_ref = newSVsv(POPs);
78
79                         PUTBACK; FREETMPS; LEAVE ;
80                 }
81
82         } else if ( strcasecmp( argv[0], "perlModulePath" ) == 0 ) {
83                 if ( argc < 2 ) {
84                         fprintf( stderr,
85                                 "%s: line %d: missing module in \"PerlModulePath <module>\" line\n",
86                                 fname, lineno );
87                         return( 1 );
88                 }
89
90                 sprintf( eval_str, "push @INC, '%s';", argv[1] );
91                 loc_sv = perl_eval_pv( eval_str, 0 );
92
93         } else {
94                 /*
95                  * Pass it to Perl module if defined
96                  */
97
98                 {
99                         dSP ;  ENTER ; SAVETMPS;
100
101                         PUSHMARK(sp) ;
102                         XPUSHs( perl_back->pb_obj_ref );
103
104                         /* Put all arguments on the perl stack */
105                         for( args = 0; args < argc; args++ ) {
106                                 XPUSHs(sv_2mortal(newSVpv(argv[args], 0)));
107                         }
108
109                         PUTBACK ;
110
111                         count = perl_call_method("config", G_SCALAR);
112
113                         SPAGAIN ;
114
115                         if (count != 1) {
116                                 croak("Big trouble in config\n") ;
117                         }
118
119                         return_code = POPi;
120
121                         PUTBACK ; FREETMPS ;  LEAVE ;
122
123                 }
124
125                 /* if the module rejected it then we should reject it */
126                 if ( return_code != 0 ) {
127                         fprintf( stderr,
128                                  "Unknown perl backeng config: %s\n", argv[0]);
129                         exit( EXIT_FAILURE );
130                 }
131         }
132
133         return 0;
134 }