diff options
Diffstat (limited to 'tools/mini_epn.c')
| -rw-r--r-- | tools/mini_epn.c | 96 |
1 files changed, 44 insertions, 52 deletions
diff --git a/tools/mini_epn.c b/tools/mini_epn.c index 6f3c5d02..1b09f1c1 100644 --- a/tools/mini_epn.c +++ b/tools/mini_epn.c | |||
| @@ -1,14 +1,14 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * | 2 | * |
| 3 | * MINI_EPN.C - Mini Embedded Perl Nagios | 3 | * MINI_EPN.C - Mini Embedded Perl Nagios |
| 4 | * Contributed by Stanley Hopcroft | 4 | * Contributed by Stanley Hopcroft |
| 5 | * Modified by Douglas Warner | 5 | * Modified by Douglas Warner |
| 6 | * Last Modified: 05/02/2002 | 6 | * Last Modified: 05/02/2002 |
| 7 | * | 7 | * |
| 8 | * This is a sample mini embedded Perl interpreter (hacked out checks.c and | 8 | * This is a sample mini embedded Perl interpreter (hacked out checks.c and |
| 9 | * perlembed) for use in testing Perl plugins. | 9 | * perlembed) for use in testing Perl plugins. |
| 10 | * | 10 | * |
| 11 | * It can be compiled with the following command (see 'man perlembed' for | 11 | * It can be compiled with the following command (see 'man perlembed' for |
| 12 | * more info): | 12 | * more info): |
| 13 | * | 13 | * |
| 14 | * gcc -omini_epn mini_epn.c `perl -MExtUtils::Embed -e ccopts -e ldopts` | 14 | * gcc -omini_epn mini_epn.c `perl -MExtUtils::Embed -e ccopts -e ldopts` |
| @@ -21,7 +21,6 @@ | |||
| 21 | * | 21 | * |
| 22 | */ | 22 | */ |
| 23 | 23 | ||
| 24 | |||
| 25 | #include <EXTERN.h> | 24 | #include <EXTERN.h> |
| 26 | #include <perl.h> | 25 | #include <perl.h> |
| 27 | #include <fcntl.h> | 26 | #include <fcntl.h> |
| @@ -30,7 +29,7 @@ | |||
| 30 | /* include PERL xs_init code for module and C library support */ | 29 | /* include PERL xs_init code for module and C library support */ |
| 31 | 30 | ||
| 32 | #if defined(__cplusplus) | 31 | #if defined(__cplusplus) |
| 33 | #define is_cplusplus | 32 | # define is_cplusplus |
| 34 | #endif | 33 | #endif |
| 35 | 34 | ||
| 36 | #ifdef is_cplusplus | 35 | #ifdef is_cplusplus |
| @@ -42,22 +41,20 @@ extern "C" { | |||
| 42 | 41 | ||
| 43 | #ifdef is_cplusplus | 42 | #ifdef is_cplusplus |
| 44 | } | 43 | } |
| 45 | # ifndef EXTERN_C | 44 | # ifndef EXTERN_C |
| 46 | # define EXTERN_C extern "C" | 45 | # define EXTERN_C extern "C" |
| 47 | # endif | 46 | # endif |
| 48 | #else | 47 | #else |
| 49 | # ifndef EXTERN_C | 48 | # ifndef EXTERN_C |
| 50 | # define EXTERN_C extern | 49 | # define EXTERN_C extern |
| 51 | # endif | 50 | # endif |
| 52 | #endif | 51 | #endif |
| 53 | |||
| 54 | 52 | ||
| 55 | EXTERN_C void xs_init _((void)); | 53 | EXTERN_C void xs_init _((void)); |
| 56 | 54 | ||
| 57 | EXTERN_C void boot_DynaLoader _((CV* cv)); | 55 | EXTERN_C void boot_DynaLoader _((CV * cv)); |
| 58 | 56 | ||
| 59 | EXTERN_C void xs_init(void) | 57 | EXTERN_C void xs_init(void) { |
| 60 | { | ||
| 61 | char *file = __FILE__; | 58 | char *file = __FILE__; |
| 62 | dXSUB_SYS; | 59 | dXSUB_SYS; |
| 63 | 60 | ||
| @@ -65,85 +62,80 @@ EXTERN_C void xs_init(void) | |||
| 65 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); | 62 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
| 66 | } | 63 | } |
| 67 | 64 | ||
| 68 | |||
| 69 | static PerlInterpreter *perl = NULL; | 65 | static PerlInterpreter *perl = NULL; |
| 70 | 66 | ||
| 71 | 67 | int main(int argc, char **argv, char **env) { | |
| 72 | int main(int argc, char **argv, char **env) | 68 | char *embedding[] = {"", "p1.pl"}; |
| 73 | { | ||
| 74 | char *embedding[] = { "", "p1.pl" }; | ||
| 75 | char plugin_output[1024]; | 69 | char plugin_output[1024]; |
| 76 | char buffer[512]; | 70 | char buffer[512]; |
| 77 | char tmpfname[32]; | 71 | char tmpfname[32]; |
| 78 | char fname[32]; | 72 | char fname[32]; |
| 79 | char *args[] = {"","0", "", "", NULL }; | 73 | char *args[] = {"", "0", "", "", NULL}; |
| 80 | FILE *fp; | 74 | FILE *fp; |
| 81 | 75 | ||
| 82 | const int command_line_size = 160; | 76 | const int command_line_size = 160; |
| 83 | char command_line[command_line_size]; | 77 | char command_line[command_line_size]; |
| 84 | char *ap ; | 78 | char *ap; |
| 85 | int exitstatus; | 79 | int exitstatus; |
| 86 | int pclose_result; | 80 | int pclose_result; |
| 87 | #ifdef THREADEDPERL | 81 | #ifdef THREADEDPERL |
| 88 | dTHX; | 82 | dTHX; |
| 89 | #endif | 83 | #endif |
| 90 | dSP; | 84 | dSP; |
| 91 | 85 | ||
| 92 | if ((perl=perl_alloc())==NULL) { | 86 | if ((perl = perl_alloc()) == NULL) { |
| 93 | snprintf(buffer,sizeof(buffer),"Error: Could not allocate memory for embedded Perl interpreter!\n"); | 87 | snprintf(buffer, sizeof(buffer), |
| 94 | buffer[sizeof(buffer)-1]='\x0'; | 88 | "Error: Could not allocate memory for embedded Perl interpreter!\n"); |
| 89 | buffer[sizeof(buffer) - 1] = '\x0'; | ||
| 95 | printf("%s\n", buffer); | 90 | printf("%s\n", buffer); |
| 96 | exit(1); | 91 | exit(1); |
| 97 | } | 92 | } |
| 98 | perl_construct(perl); | 93 | perl_construct(perl); |
| 99 | exitstatus=perl_parse(perl,xs_init,2,embedding,NULL); | 94 | exitstatus = perl_parse(perl, xs_init, 2, embedding, NULL); |
| 100 | if (!exitstatus) { | 95 | if (!exitstatus) { |
| 101 | 96 | ||
| 102 | exitstatus=perl_run(perl); | 97 | exitstatus = perl_run(perl); |
| 103 | 98 | ||
| 104 | while(printf("Enter file name: ") && fgets(command_line, command_line_size, stdin)) { | 99 | while (printf("Enter file name: ") && fgets(command_line, command_line_size, stdin)) { |
| 105 | 100 | ||
| 106 | /* call the subroutine, passing it the filename as an argument */ | 101 | /* call the subroutine, passing it the filename as an argument */ |
| 107 | 102 | ||
| 108 | command_line[strlen(command_line) -1] = '\0'; | 103 | command_line[strlen(command_line) - 1] = '\0'; |
| 109 | 104 | ||
| 110 | strncpy(fname,command_line,strcspn(command_line," ")); | 105 | strncpy(fname, command_line, strcspn(command_line, " ")); |
| 111 | fname[strcspn(command_line," ")] = '\x0'; | 106 | fname[strcspn(command_line, " ")] = '\x0'; |
| 112 | args[0] = fname ; | 107 | args[0] = fname; |
| 113 | args[3] = command_line + strlen(fname) + 1 ; | 108 | args[3] = command_line + strlen(fname) + 1; |
| 114 | 109 | ||
| 115 | /* generate a temporary filename to which stdout can be redirected. */ | 110 | /* generate a temporary filename to which stdout can be redirected. */ |
| 116 | sprintf(tmpfname,"/tmp/embedded%d",getpid()); | 111 | sprintf(tmpfname, "/tmp/embedded%d", getpid()); |
| 117 | args[2] = tmpfname; | 112 | args[2] = tmpfname; |
| 118 | 113 | ||
| 119 | /* call our perl interpreter to compile and optionally cache the command */ | 114 | /* call our perl interpreter to compile and optionally cache the command */ |
| 120 | perl_call_argv("Embed::Persistent::eval_file", G_DISCARD | G_EVAL, args); | 115 | perl_call_argv("Embed::Persistent::eval_file", G_DISCARD | G_EVAL, args); |
| 121 | 116 | ||
| 122 | perl_call_argv("Embed::Persistent::run_package", G_DISCARD | G_EVAL, args); | 117 | perl_call_argv("Embed::Persistent::run_package", G_DISCARD | G_EVAL, args); |
| 123 | 118 | ||
| 124 | /* check return status */ | 119 | /* check return status */ |
| 125 | if(SvTRUE(ERRSV)){ | 120 | if (SvTRUE(ERRSV)) { |
| 126 | pclose_result=-2; | 121 | pclose_result = -2; |
| 127 | printf("embedded perl ran %s with error %s\n",fname,SvPV(ERRSV,PL_na)); | 122 | printf("embedded perl ran %s with error %s\n", fname, SvPV(ERRSV, PL_na)); |
| 128 | } | 123 | } |
| 129 | 124 | ||
| 130 | /* read back stdout from script */ | 125 | /* read back stdout from script */ |
| 131 | fp=fopen(tmpfname, "r"); | 126 | fp = fopen(tmpfname, "r"); |
| 132 | 127 | ||
| 133 | /* default return string in case nothing was returned */ | 128 | /* default return string in case nothing was returned */ |
| 134 | strcpy(plugin_output,"(No output!)"); | 129 | strcpy(plugin_output, "(No output!)"); |
| 135 | |||
| 136 | fgets(plugin_output,sizeof(plugin_output)-1,fp); | ||
| 137 | plugin_output[sizeof(plugin_output)-1]='\x0'; | ||
| 138 | fclose(fp); | ||
| 139 | unlink(tmpfname); | ||
| 140 | printf("embedded perl plugin output was %d,%s\n",pclose_result, plugin_output); | ||
| 141 | 130 | ||
| 131 | fgets(plugin_output, sizeof(plugin_output) - 1, fp); | ||
| 132 | plugin_output[sizeof(plugin_output) - 1] = '\x0'; | ||
| 133 | fclose(fp); | ||
| 134 | unlink(tmpfname); | ||
| 135 | printf("embedded perl plugin output was %d,%s\n", pclose_result, plugin_output); | ||
| 142 | } | 136 | } |
| 143 | |||
| 144 | } | 137 | } |
| 145 | 138 | ||
| 146 | |||
| 147 | PL_perl_destruct_level = 0; | 139 | PL_perl_destruct_level = 0; |
| 148 | perl_destruct(perl); | 140 | perl_destruct(perl); |
| 149 | perl_free(perl); | 141 | perl_free(perl); |
