(expand_function: `shell') [__MSDOS__]: Wholly new implementation.

This commit is contained in:
Roland McGrath 1994-07-25 23:06:00 +00:00
parent 21a1b3b255
commit da2ea1ea7f

View file

@ -22,6 +22,10 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "commands.h"
#include "job.h"
#ifdef __MSDOS__
#include <process.h>
#endif
static char *string_glob ();
/* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
@ -371,6 +375,7 @@ expand_function (o, function, text, end)
else
error_prefix = "";
#ifndef __MSDOS__
if (pipe (pipedes) < 0)
{
perror_with_name (error_prefix, "pipe");
@ -475,6 +480,69 @@ expand_function (o, function, text, end)
free (buffer);
}
#else /* MSDOS. */
{
/* MS-DOS can't do fork, but it can do spawn. However, this
means that we don't have an opportunity to reopen stdout to
trap it. Thus, we save our own stdout onto a new descriptor
and dup a temp file's descriptor onto our stdout temporarily.
After we spawn the shell program, we dup our own stdout back
to the stdout descriptor. The buffer reading is the same as
above, except that we're now reading from a file. */
int save_stdout;
int child_stdout;
char tmp_output[FILENAME_MAX];
FILE *child_stream;
unsigned int maxlen = 200;
int cc;
char *buffer;
strcpy (tmp_output, "shXXXXXX");
mktemp (tmp_output);
child_stdout = open (tmp_output,
O_WRONLY|O_CREAT|O_TRUNC|O_TEXT, 0644);
save_stdout = dup (1);
dup2 (child_stdout, 1);
spawnvp (P_WAIT, argv[0], argv);
dup2 (save_stdout, 1);
close (child_stdout);
close (save_stdout);
child_stdout = open (tmp_output, O_RDONLY|O_TEXT, 0644);
buffer = xmalloc (maxlen);
i = 0;
do
{
if (i == maxlen)
{
maxlen += 512;
buffer = (char *) xrealloc (buffer, maxlen + 1);
}
cc = read (child_stdout, &buffer[i], maxlen - i);
if (cc > 0)
i += cc;
} while (cc > 0);
close (child_stdout);
unlink (tmp_output);
if (i > 0)
{
if (buffer[i - 1] == '\n')
buffer[--i] = '\0';
else
buffer[i] = '\0';
p = buffer;
while ((p = index (p, '\n')) != 0)
*p++ = ' ';
o = variable_buffer_output (o, buffer, i);
}
free (buffer);
}
#endif /* Not MSDOS. */
free (text);
break;