summaryrefslogtreecommitdiff
path: root/TSRM/tsrm_nw.c
blob: c0f69adb5dcdd081ed2d8938dc948869a07c2b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2002 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Venkat Raghavan S <rvenkat@novell.com>                      |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

#include "TSRM.h"

#ifdef NETWARE

#ifdef USE_MKFIFO
#include <sys/stat.h>
#elif !defined(USE_PIPE_OPEN)   /* NXFifoOpen */
#include <nks/fsio.h>
#endif

#include <nks/vm.h>
#include <nks/memory.h>

#include <string.h>

#include "mktemp.h"

/* strtok() call in LibC is abending when used in a different address space -- hence using
   PHP's version itself for now : Venkat (30/4/02) */
#include "tsrm_strtok_r.h"
#define tsrm_strtok_r(a,b,c) strtok((a),(b))

#define WHITESPACE  " \t"
#define MAX_ARGS    10


TSRM_API FILE* popen(const char *commandline, const char *type)
{
    char *command = NULL, *argv[MAX_ARGS] = {'\0'}, **env = NULL;
	char *tempName = "sys:/php/temp/phpXXXXXX.tmp";
    char *filePath = NULL;
    char *ptr = NULL;
    int ptrLen = 0, argc = 0, i = 0, envCount = 0, err = 0;
	FILE *stream = NULL;
#if defined(USE_PIPE_OPEN) || defined(USE_MKFIFO)
    int pipe_handle;
    int mode = O_RDONLY;
#else
    NXHandle_t pipe_handle;
    NXMode_t mode = NX_O_RDONLY;
#endif
    NXExecEnvSpec_t envSpec;
    NXNameSpec_t nameSpec;
    NXVmId_t newVM = 0;

    /* Check for validity of input parameters */
    if (!commandline || !type)
        return NULL;

    /* Get temporary file name */
    filePath = mktemp(tempName);
/*consoleprintf ("PHP | popen: file path = %s, mode = %s\n", filePath, type);*/
	if (!filePath)
		return NULL;

    /* Set pipe mode according to type -- for now allow only "r" or "w" */
    if (strcmp(type, "r") == 0)
#if defined(USE_PIPE_OPEN) || defined(USE_MKFIFO)
        mode = O_RDONLY;
#else
        mode = NX_O_RDONLY;
#endif
    else if (strcmp(type, "w") == 0)
#if defined(USE_PIPE_OPEN) || defined(USE_MKFIFO)
        mode = O_WRONLY;
#else
        mode = NX_O_WRONLY;
#endif
    else
        return NULL;

#ifdef USE_PIPE_OPEN
    pipe_handle = pipe_open(filePath, mode);
/*consoleprintf ("PHP | popen: pipe_open() returned %d\n", pipe_handle);*/
    if (pipe_handle == -1)
        return NULL;
#elif defined(USE_MKFIFO)
    pipe_handle = mkfifo(filePath, mode);
consoleprintf ("PHP | popen: mkfifo() returned %d\n", pipe_handle);
    if (pipe_handle == -1)
        return NULL;
#else
    /*
        - NetWare doesn't require first parameter
        - Allowing LibC to choose the buffer size for now
    */
    err = NXFifoOpen(0, filePath, mode, 0, &pipe_handle);
/*consoleprintf ("PHP | popen: NXFifoOpen() returned %d\n", err);*/
    if (err)
        return NULL;
#endif

    /* Copy the environment variables in preparation for the spawn call */

    envCount = NXGetEnvCount() + 1;  /* add one for NULL */
    env = (char**)NXMemAlloc(sizeof(char*) * envCount, 0);
    if (!env)
        return NULL;

    err = NXCopyEnv(env, envCount);
consoleprintf ("PHP | popen: NXCopyEnv() returned %d\n", err);
    if (err)
    {
        NXMemFree (env);
        return NULL;
    }

    /* Separate commandline string into words */
consoleprintf ("PHP | popen: commandline = %s\n", commandline);
    ptr = tsrm_strtok_r((char*)commandline, WHITESPACE, NULL);
    ptrLen = strlen(ptr);

    command = (char*)malloc(ptrLen + 1);
    if (!command)
    {
        NXMemFree (env);
        return NULL;
    }

    strcpy (command, ptr);

    ptr = tsrm_strtok_r(NULL, WHITESPACE, NULL);
    while (ptr && (argc < MAX_ARGS))
    {
        ptrLen = strlen(ptr);

        argv[argc] = (char*)malloc(ptrLen + 1);
        if (!argv[argc])
        {
            NXMemFree (env);

            if (command)
                free (command);

            for (i = 0; i < argc; i++)
            {
                if (argv[i])
                free (argv[i]);
            }

            return NULL;
        }

        strcpy (argv[argc], ptr);

        argc++;

        ptr = tsrm_strtok_r(NULL, WHITESPACE, NULL);
    }
consoleprintf ("PHP | popen: commandline string parsed into tokens\n");
    /* Setup the execution environment and spawn new process */

    envSpec.esFlags = 0;    /* Not used */
    envSpec.esArgc = argc;
    envSpec.esArgv = (void**)argv;
    envSpec.esEnv = (void**)env;

    envSpec.esStdin.ssType =
    envSpec.esStdout.ssType = NX_OBJ_FIFO;
    envSpec.esStderr.ssType = NX_OBJ_FILE;
/*
    envSpec.esStdin.ssHandle =
    envSpec.esStdout.ssHandle =
    envSpec.esStderr.ssHandle = -1;
*/
    envSpec.esStdin.ssPathCtx =
    envSpec.esStdout.ssPathCtx =
    envSpec.esStderr.ssPathCtx = NULL;

#if defined(USE_PIPE_OPEN) || defined(USE_MKFIFO)
    if (mode == O_RDONLY)
#else
    if (mode == NX_O_RDONLY)
#endif
    {
        envSpec.esStdin.ssPath = filePath;
        envSpec.esStdout.ssPath = stdout;
    }
    else /* Write Only */
    {
        envSpec.esStdin.ssPath = stdin;
        envSpec.esStdout.ssPath = filePath;
    }

    envSpec.esStderr.ssPath = stdout;

    nameSpec.ssType = NX_OBJ_FIFO;
/*    nameSpec.ssHandle = 0; */ /* Not used */
    nameSpec.ssPathCtx = NULL;  /* Not used */
    nameSpec.ssPath = argv[0];
consoleprintf ("PHP | popen: environment setup\n");
    err = NXVmSpawn(&nameSpec, &envSpec, 0, &newVM);
consoleprintf ("PHP | popen: NXVmSpawn() returned %d\n", err);
    if (!err)
        /* Get file pointer corresponding to the pipe (file) opened */
        stream = fdopen(pipe_handle, type);

    /* Clean-up */

    if (env)
        NXMemFree (env);

    if (pipe_handle)
#if defined(USE_PIPE_OPEN) || defined(USE_MKFIFO)
        close(pipe_handle);
#else
        NXClose(pipe_handle);
#endif

    if (command)
        free (command);

    for (i = 0; i < argc; i++)
    {
        if (argv[i])
            free (argv[i]);
    }
consoleprintf ("PHP | popen: all clean-up done, returning...\n");
    return stream;
}

TSRM_API int pclose(FILE* stream)
{
    int err = 0;
    NXHandle_t fd = 0;

    /* Get the process associated with this pipe (file) handle and terminate it */
    fd = fileno(stream);
    NXClose (fd);

    err = fclose(stream);

    return err;
}

#endif