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