#include "less.h"
#include <signal.h>
#include "position.h"
#if MSDOS_COMPILER
#include <dos.h>
#ifdef _MSC_VER
#include <direct.h>
#define setdisk(n) _chdrive((n)+1)
#else
#include <dir.h>
#endif
#endif
extern int screen_trashed;
extern IFILE curr_ifile;
#if HAVE_SYSTEM
public void
lsystem(cmd, donemsg)
char *cmd;
char *donemsg;
{
register int inp;
#if HAVE_SHELL
register char *shell;
register char *p;
#endif
IFILE save_ifile;
#if MSDOS_COMPILER
char cwd[FILENAME_MAX+1];
#endif
if (cmd[0] == '-')
cmd++;
else
{
clear_bot();
putstr("!");
putstr(cmd);
putstr("\n");
}
#if MSDOS_COMPILER
getcwd(cwd, FILENAME_MAX);
#endif
save_ifile = save_curr_ifile();
(void) edit_ifile(NULL_IFILE);
deinit();
flush();
raw_mode(0);
#if MSDOS_COMPILER==WIN32C
close_getchr();
#endif
init_signals(0);
#if HAVE_DUP
inp = dup(0);
close(0);
#if OS2
if (__open("/dev/tty", OPEN_READ) < 0)
#else
if (open("/dev/tty", OPEN_READ) < 0)
#endif
dup(inp);
#endif
#if HAVE_SHELL
p = NULL;
if ((shell = lgetenv("SHELL")) != NULL && *shell != '\0')
{
if (*cmd == '\0')
p = save(shell);
else
{
char *esccmd = shell_quote(cmd);
if (esccmd != NULL)
{
p = (char *) ecalloc(strlen(shell) +
strlen(esccmd) + 5, sizeof(char));
sprintf(p, "%s %s %s", shell, shell_coption(), esccmd);
free(esccmd);
}
}
}
if (p == NULL)
{
if (*cmd == '\0')
p = save("sh");
else
p = save(cmd);
}
system(p);
free(p);
#else
#if MSDOS_COMPILER==DJGPPC
setmode(0, O_TEXT);
__djgpp_exception_toggle();
system(cmd);
__djgpp_exception_toggle();
#else
system(cmd);
#endif
#endif
#if HAVE_DUP
close(0);
dup(inp);
close(inp);
#endif
#if MSDOS_COMPILER==WIN32C
open_getchr();
#endif
init_signals(1);
raw_mode(1);
if (donemsg != NULL)
{
putstr(donemsg);
putstr(" (press RETURN)");
get_return();
putchr('\n');
flush();
}
init();
screen_trashed = 1;
#if MSDOS_COMPILER
chdir(cwd);
#if MSDOS_COMPILER != DJGPPC
if (cwd[1] == ':')
{
if (cwd[0] >= 'a' && cwd[0] <= 'z')
setdisk(cwd[0] - 'a');
else if (cwd[0] >= 'A' && cwd[0] <= 'Z')
setdisk(cwd[0] - 'A');
}
#endif
#endif
reedit_ifile(save_ifile);
#if defined(SIGWINCH) || defined(SIGWIND)
winch(0);
#endif
}
#endif
#if PIPEC
public int
pipe_mark(c, cmd)
int c;
char *cmd;
{
POSITION mpos, tpos, bpos;
mpos = markpos(c);
if (mpos == NULL_POSITION)
return (-1);
tpos = position(TOP);
if (tpos == NULL_POSITION)
tpos = ch_zero();
bpos = position(BOTTOM);
if (c == '.')
return (pipe_data(cmd, tpos, bpos));
else if (mpos <= tpos)
return (pipe_data(cmd, mpos, bpos));
else if (bpos == NULL_POSITION)
return (pipe_data(cmd, tpos, bpos));
else
return (pipe_data(cmd, tpos, mpos));
}
public int
pipe_data(cmd, spos, epos)
char *cmd;
POSITION spos;
POSITION epos;
{
register FILE *f;
register int c;
extern FILE *popen();
if (ch_seek(spos) != 0)
{
error("Cannot seek to start position", NULL_PARG);
return (-1);
}
if ((f = popen(cmd, "w")) == NULL)
{
error("Cannot create pipe", NULL_PARG);
return (-1);
}
clear_bot();
putstr("!");
putstr(cmd);
putstr("\n");
deinit();
flush();
raw_mode(0);
init_signals(0);
#if MSDOS_COMPILER==WIN32C
close_getchr();
#endif
#ifdef SIGPIPE
LSIGNAL(SIGPIPE, SIG_IGN);
#endif
c = EOI;
while (epos == NULL_POSITION || spos++ <= epos)
{
c = ch_forw_get();
if (c == EOI)
break;
if (putc(c, f) == EOF)
break;
}
while (c != '\n' && c != EOI )
{
c = ch_forw_get();
if (c == EOI)
break;
if (putc(c, f) == EOF)
break;
}
pclose(f);
#ifdef SIGPIPE
LSIGNAL(SIGPIPE, SIG_DFL);
#endif
#if MSDOS_COMPILER==WIN32C
open_getchr();
#endif
init_signals(1);
raw_mode(1);
init();
screen_trashed = 1;
#if defined(SIGWINCH) || defined(SIGWIND)
winch(0);
#endif
return (0);
}
#endif
#ifdef _OSK
#include <strings.h>
#include <errno.h>
extern char **environ;
extern char *getenv();
extern int os9forkc();
static int pids[_NFILE] = { 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 };
FILE *popen(name, mode)
char *name;
char *mode;
{
int fd, fd2, fdsav, pid;
static char *argv[] = {NULL, NULL, NULL };
static char cmd[200];
static char cmd_path[200];
char *cp;
char *shell;
FILE *r;
if ((shell = getenv("SHELL")) == NULL)
return(NULL);
cp = name;
while (*cp == ' ')
cp++;
strcpy(cmd_path, cp);
if (cp = index(cmd_path, ' '))
*cp++ = '\0';
strcpy(cmd, "ex ");
strcat(cmd, cmd_path);
if (cp)
{
strcat(cmd, " ");
strcat(cmd, cp);
}
argv[0] = shell;
argv[1] = cmd;
switch(mode[0])
{
case 'w': fd = 0;
break;
case 'r': fd = 1;
break;
default: return(NULL);
}
if (fd == 1)
fflush(stdout);
fdsav = dup(fd);
close(fd);
creat("/pipe", S_IWRITE+S_IREAD);
pid = os9exec(os9forkc, argv[0], argv, environ, 0, 0, 3);
fd2 = dup(fd);
close(fd);
dup(fdsav);
close(fdsav);
if (pid > 0)
{
pids[fd2] = pid;
r = fdopen(fd2, mode);
}
else
{
close(fd2);
r = NULL;
}
return(r);
}
int pclose(fp)
FILE *fp;
{
unsigned int status;
int pid;
int fd,
i;
fd = fileno(fp);
if (pids[fd] == 0)
return(-1);
fflush(fp);
fclose(fp);
while ((pid = wait(&status)) != -1)
if (pid == pids[fd])
break;
else
for (i = 0; i < _NFILE; i++)
if (pids[i] == pid)
{
pids[i] = 0;
break;
}
if (pid == -1)
status = -1;
pids[fd] = 0;
return(status);
}
#endif