This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
propagate changed error text
[perl5.git] / djgpp / djgpp.c
CommitLineData
39e571d4
LM
1#include <libc/stubs.h>
2#include <io.h>
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8#include <libc/file.h>
9#include <process.h>
10#include <fcntl.h>
11#include <glob.h>
12#include <sys/fsext.h>
13#include <crt0.h>
14#include "EXTERN.h"
15#include "perl.h"
16#include "XSUB.h"
17
9731c6ca 18/* hold file pointer, command, mode, and the status of the command */
39e571d4
LM
19struct pipe_list {
20 FILE *fp;
39e571d4 21 int exit_status;
39e571d4 22 struct pipe_list *next;
9731c6ca 23 char *command, mode;
39e571d4
LM
24};
25
26/* static, global list pointer */
27static struct pipe_list *pl = NULL;
28
29FILE *
30popen (const char *cm, const char *md) /* program name, pipe mode */
31{
32 struct pipe_list *l1;
9731c6ca
LM
33 int fd;
34 char *temp_name=NULL;
39e571d4
LM
35
36 /* make new node */
9731c6ca
LM
37 if ((l1 = (struct pipe_list *) malloc (sizeof (*l1)))
38 && (temp_name = malloc (L_tmpnam)) && tmpnam (temp_name))
39e571d4 39 {
9731c6ca
LM
40 l1->fp = NULL;
41 l1->command = NULL;
42 l1->next = pl;
43 l1->exit_status = -1;
44 l1->mode = md[0];
45
39e571d4 46 /* if caller wants to read */
9731c6ca 47 if (md[0] == 'r' && (fd = dup (fileno (stdout))) >= 0)
39e571d4 48 {
9731c6ca 49 if ((l1->fp = freopen (temp_name, "wb", stdout)))
39e571d4 50 {
9731c6ca
LM
51 l1->exit_status = system (cm);
52 if (dup2 (fd, fileno (stdout)) >= 0)
53 l1->fp = fopen (temp_name, md);
39e571d4 54 }
9731c6ca
LM
55 close (fd);
56 }
57 /* if caller wants to write */
58 else if (md[0] == 'w' && (l1->command = malloc (1 + strlen (cm))))
59 {
60 strcpy (l1->command, cm);
61 l1->fp = fopen (temp_name, md);
62 }
63
64 if (l1->fp)
65 {
66 l1->fp->_flag |= _IORMONCL; /* remove on close */
67 l1->fp->_name_to_remove = temp_name;
68 return (pl = l1)->fp;
39e571d4 69 }
9731c6ca 70 free (l1->command);
39e571d4 71 }
9731c6ca
LM
72 free (temp_name);
73 free (l1);
74 return NULL;
39e571d4
LM
75}
76
77int
78pclose (FILE *pp)
79{
9731c6ca
LM
80 struct pipe_list *l1, **l2; /* list pointers */
81 int retval=-1; /* function return value */
39e571d4 82
9731c6ca
LM
83 for (l2 = &pl; *l2 && (*l2)->fp != pp; l2 = &((*l2)->next))
84 ;
85 if (!(l1 = *l2))
86 return retval;
87 *l2 = l1->next;
39e571d4 88
9731c6ca
LM
89 /* if pipe was opened to write */
90 if (l1->mode == 'w')
39e571d4 91 {
9731c6ca
LM
92 int fd;
93 fflush (l1->fp);
94 close (fileno (l1->fp));
39e571d4 95
9731c6ca
LM
96 if ((fd = dup (fileno (stdin))) >= 0
97 && (freopen (l1->fp->_name_to_remove, "rb", stdin)))
39e571d4 98 {
9731c6ca
LM
99 retval = system (l1->command);
100 dup2 (fd, fileno (stdin));
39e571d4 101 }
9731c6ca
LM
102 close (fd);
103 free (l1->command);
39e571d4 104 }
9731c6ca
LM
105 else
106 /* if pipe was opened to read, return the exit status we saved */
107 retval = l1->exit_status;
39e571d4 108
9731c6ca
LM
109 fclose (l1->fp); /* this removes the temp file */
110 free (l1);
111 return retval; /* retval==0 ? OK : ERROR */
39e571d4
LM
112}
113
39e571d4
LM
114/**/
115
116#define EXECF_SPAWN 0
117#define EXECF_EXEC 1
118
119static int
41cd3736 120convretcode (pTHX_ int rc,char *prog,int fl)
39e571d4 121{
0453d815
PM
122 if (rc < 0 && ckWARN(WARN_EXEC))
123 Perl_warner(aTHX_ WARN_EXEC,"Can't %s \"%s\": %s",
124 fl ? "exec" : "spawn",prog,Strerror (errno));
39e571d4
LM
125 if (rc > 0)
126 return rc <<= 8;
127 if (rc < 0)
128 return 255 << 8;
129 return 0;
130}
131
132int
41cd3736 133do_aspawn (pTHX_ SV *really,SV **mark,SV **sp)
39e571d4
LM
134{
135 dTHR;
136 int rc;
137 char **a,*tmps,**argv;
2d8e6c8d 138 STRLEN n_a;
39e571d4
LM
139
140 if (sp<=mark)
141 return -1;
142 a=argv=(char**) alloca ((sp-mark+3)*sizeof (char*));
143
144 while (++mark <= sp)
145 if (*mark)
2d8e6c8d 146 *a++ = SvPVx(*mark, n_a);
39e571d4
LM
147 else
148 *a++ = "";
149 *a = Nullch;
150
151 if (argv[0][0] != '/' && argv[0][0] != '\\'
152 && !(argv[0][0] && argv[0][1] == ':'
153 && (argv[0][2] == '/' || argv[0][2] != '\\'))
154 ) /* will swawnvp use PATH? */
155 TAINT_ENV(); /* testing IFS here is overkill, probably */
156
2d8e6c8d 157 if (really && *(tmps = SvPV(really, n_a)))
39e571d4
LM
158 rc=spawnvp (P_WAIT,tmps,argv);
159 else
160 rc=spawnvp (P_WAIT,argv[0],argv);
161
162 return convretcode (rc,argv[0],EXECF_SPAWN);
163}
164
165#define EXTRA "\x00\x00\x00\x00\x00\x00"
166
167int
41cd3736 168do_spawn2 (pTHX_ char *cmd,int execf)
39e571d4
LM
169{
170 char **a,*s,*shell,*metachars;
171 int rc,unixysh;
172
173 if ((shell=getenv("SHELL"))==NULL && (shell=getenv("COMSPEC"))==NULL)
174 shell="c:\\command.com" EXTRA;
175
176 unixysh=_is_unixy_shell (shell);
177 metachars=unixysh ? "$&*(){}[]'\";\\?>|<~`\n" EXTRA : "*?[|<>\"\\" EXTRA;
178
179 while (*cmd && isSPACE(*cmd))
180 cmd++;
181
182 if (strnEQ (cmd,"/bin/sh",7) && isSPACE (cmd[7]))
183 cmd+=5;
184
185 /* save an extra exec if possible */
186 /* see if there are shell metacharacters in it */
187 if (strstr (cmd,"..."))
188 goto doshell;
189 if (unixysh)
190 {
191 if (*cmd=='.' && isSPACE (cmd[1]))
192 goto doshell;
193 if (strnEQ (cmd,"exec",4) && isSPACE (cmd[4]))
194 goto doshell;
195 for (s=cmd; *s && isALPHA (*s); s++) ; /* catch VAR=val gizmo */
196 if (*s=='=')
197 goto doshell;
198 }
199 for (s=cmd; *s; s++)
200 if (strchr (metachars,*s))
201 {
202 if (*s=='\n' && s[1]=='\0')
203 {
204 *s='\0';
205 break;
206 }
207doshell:
208 if (execf==EXECF_EXEC)
209 return convretcode (execl (shell,shell,unixysh ? "-c" : "/c",cmd,NULL),cmd,execf);
210 return convretcode (system (cmd),cmd,execf);
211 }
212
6b88bc9c
GS
213 New (1303,PL_Argv,(s-cmd)/2+2,char*);
214 PL_Cmd=savepvn (cmd,s-cmd);
215 a=PL_Argv;
216 for (s=PL_Cmd; *s;) {
39e571d4
LM
217 while (*s && isSPACE (*s)) s++;
218 if (*s)
219 *(a++)=s;
220 while (*s && !isSPACE (*s)) s++;
221 if (*s)
222 *s++='\0';
223 }
224 *a=Nullch;
6b88bc9c 225 if (!PL_Argv[0])
39e571d4
LM
226 return -1;
227
228 if (execf==EXECF_EXEC)
6b88bc9c 229 rc=execvp (PL_Argv[0],PL_Argv);
39e571d4 230 else
6b88bc9c
GS
231 rc=spawnvp (P_WAIT,PL_Argv[0],PL_Argv);
232 return convretcode (rc,PL_Argv[0],execf);
39e571d4
LM
233}
234
235int
41cd3736 236do_spawn (pTHX_ char *cmd)
39e571d4 237{
41cd3736 238 return do_spawn2 (aTHX_ cmd,EXECF_SPAWN);
39e571d4
LM
239}
240
241bool
41cd3736 242Perl_do_exec (pTHX_ char *cmd)
39e571d4 243{
41cd3736 244 do_spawn2 (aTHX_ cmd,EXECF_EXEC);
39e571d4
LM
245 return FALSE;
246}
247
248/**/
249
250struct globinfo
251{
252 int fd;
253 char *matches;
254 size_t size;
255};
256
257#define MAXOPENGLOBS 10
258
259static struct globinfo myglobs[MAXOPENGLOBS];
260
261static struct globinfo *
262searchfd (int fd)
263{
264 int ic;
265 for (ic=0; ic<MAXOPENGLOBS; ic++)
266 if (myglobs[ic].fd==fd)
267 return myglobs+ic;
268 return NULL;
269}
270
271static int
272glob_handler (__FSEXT_Fnumber n,int *rv,va_list args)
273{
274 unsigned ic;
275 struct globinfo *gi;
276 switch (n)
277 {
278 case __FSEXT_open:
279 {
280 char *p1,*pattern,*name=va_arg (args,char*);
281 STRLEN len;
282 glob_t pglob;
283
284 if (strnNE (name,"/dev/dosglob/",13))
285 break;
286 if ((gi=searchfd (-1)) == NULL)
287 break;
288
289 pattern=alloca (strlen (name+=13)+1);
290 strcpy (pattern,name);
291 if (!_USE_LFN)
292 strlwr (pattern);
293 ic=pglob.gl_pathc=0;
294 pglob.gl_pathv=NULL;
295 while (pattern)
296 {
297 if ((p1=strchr (pattern,' '))!=NULL)
298 *p1=0;
299 glob (pattern,ic,0,&pglob);
300 ic=GLOB_APPEND;
301 if ((pattern=p1)!=NULL)
302 pattern++;
303 }
304 for (ic=len=0; ic<pglob.gl_pathc; ic++)
305 len+=1+strlen (pglob.gl_pathv[ic]);
306 if (len)
307 {
308 if ((gi->matches=p1=(char*) malloc (gi->size=len))==NULL)
309 break;
310 for (ic=0; ic<pglob.gl_pathc; ic++)
311 {
312 strcpy (p1,pglob.gl_pathv[ic]);
313 p1+=strlen (p1)+1;
314 }
315 }
316 else
317 {
318 if ((gi->matches=strdup (name))==NULL)
319 break;
320 gi->size=strlen (name)+1;
321 }
322 globfree (&pglob);
323 gi->fd=*rv=__FSEXT_alloc_fd (glob_handler);
324 return 1;
325 }
326 case __FSEXT_read:
327 {
328 int fd=va_arg (args,int);
329 char *buf=va_arg (args,char*);
330 size_t siz=va_arg (args,size_t);
331
332 if ((gi=searchfd (fd))==NULL)
333 break;
334
335 ic=tell (fd);
336 if (siz+ic>=gi->size)
337 siz=gi->size-ic;
338 memcpy (buf,ic+gi->matches,siz);
339 lseek (fd,siz,1);
340 *rv=siz;
341 return 1;
342 }
343 case __FSEXT_close:
344 {
345 int fd=va_arg (args,int);
346
347 if ((gi=searchfd (fd))==NULL)
348 break;
349 free (gi->matches);
350 gi->fd=-1;
351 break;
352 }
353 default:
354 break;
355 }
356 return 0;
357}
358
359static
360XS(dos_GetCwd)
361{
362 dXSARGS;
363
364 if (items)
41cd3736 365 Perl_croak (aTHX_ "Usage: Dos::GetCwd()");
39e571d4
LM
366 {
367 char tmp[PATH_MAX+2];
368 ST(0)=sv_newmortal ();
369 if (getcwd (tmp,PATH_MAX+1)!=NULL)
370 sv_setpv ((SV*)ST(0),tmp);
371 }
372 XSRETURN (1);
373}
374
375static
376XS(dos_UseLFN)
377{
378 dXSARGS;
379 XSRETURN_IV (_USE_LFN);
380}
381
382void
41cd3736 383Perl_init_os_extras(pTHX)
39e571d4
LM
384{
385 char *file = __FILE__;
386
387 dXSUB_SYS;
388
389 newXS ("Dos::GetCwd",dos_GetCwd,file);
390 newXS ("Dos::UseLFN",dos_UseLFN,file);
391
392 /* install my File System Extension for globbing */
393 __FSEXT_add_open_handler (glob_handler);
394 memset (myglobs,-1,sizeof (myglobs));
395}
396
397static char *perlprefix;
398
399#define PERL5 "/perl5"
400
401char *djgpp_pathexp (const char *p)
402{
403 static char expp[PATH_MAX];
404 strcpy (expp,perlprefix);
405 switch (p[0])
406 {
407 case 'B':
408 strcat (expp,"/bin");
409 break;
410 case 'S':
411 strcat (expp,"/lib" PERL5 "/site");
412 break;
413 default:
414 strcat (expp,"/lib" PERL5);
415 break;
416 }
417 return expp;
418}
419
420void
421Perl_DJGPP_init (int *argcp,char ***argvp)
422{
423 char *p;
424
425 perlprefix=strdup (**argvp);
426 strlwr (perlprefix);
427 if ((p=strrchr (perlprefix,'/'))!=NULL)
428 {
429 *p=0;
430 if (strEQ (p-4,"/bin"))
431 p[-4]=0;
432 }
433 else
434 strcpy (perlprefix,"..");
435}
436