/* system.c -- UNIX version */
/* This file contains a new version of the system() function and related stuff.
* system(cmd) - run a single shell command
* wildcard(names) - expand wildcard characters in filanames
* filter(m,n,cmd,back) - run text lines through a filter program
* This is probably the single least portable file in the program. The code
* shown here should work correctly if it links at all; it will work on UNIX
* and any O.S./Compiler combination which adheres to UNIX forking conventions.
/* This is a new version of the system() function. The only difference
* between this one and the library one is: this one uses the o_shell option.
char *cmd
; /* a command to run */
int pid
; /* process ID of child */
int status
; /* exit status of the command */
/* for the child, close all files except stdin/out/err */
for (status
= 3; status
< 60 && (close(status
), errno
!= EINVAL
); status
++)
execle(o_shell
, o_shell
, (char *)0, environ
);
execle(o_shell
, o_shell
, "-c", cmd
, (char *)0, environ
);
msg("execle(\"%s\", ...) failed", o_shell
);
exit(1); /* if we get here, the exec failed */
} while (died
>= 0 && died
!= pid
);
signal(SIGINT
, (void (*)()) trapint
);
/* This private function opens a pipe from a filter. It is similar to the
* system() function above, and to popen(cmd, "r").
char *cmd
; /* the filter command to use */
int in
; /* the fd to use for stdin */
int r0w1
[2];/* the pipe fd's */
return -1; /* pipe failed */
/* The parent process (elvis) ignores signals while the filter runs.
* The child (the filter program) will reset this, so that it can
/* close the "read" end of the pipe */
/* redirect stdout to go to the "write" end of the pipe */
/* the filter should accept SIGINT signals */
/* exec the shell to run the command */
execle(o_shell
, o_shell
, "-c", cmd
, (char *)0, environ
);
exit(1); /* if we get here, exec failed */
/* close the "write" end of the pipe */
/* This private function opens a pipe from a filter. It is similar to the
* system() function above, and to popen(cmd, "r").
char *cmd
; /* the filter command to use */
int in
; /* the fd to use for stdin */
return osk_popen(cmd
, "r", in
, 0);
/* This function closes the pipe opened by rpipe(), and returns 0 for success */
signal(SIGINT
, (void (*)()) trapint
);
/* This function expands wildcards in a filename or filenames. It does this
* by running the "echo" command on the filenames via the shell; it is assumed
* that the shell will expand the names for you. If for any reason it can't
* run echo, then it returns the names unmodified.
We could use expand() [vmswild.c], but what's the point on VMS?
Anyway, echo is the wrong thing to do, it takes too long to build
a subprocess on VMS and any "echo" program would have to be supplied
by elvis. More importantly, many VMS utilities expand names
themselves (the shell doesn't do any expansion) so the concept is
/* build the echo command */
/* the names aren't in tmpblk.c, so we can do it the easy way */
/* the names are already in tmpblk.c, so shift them to make
* room for the word "echo "
for (s
= names
+ strlen(names
) + 1, d
= s
+ PROGLEN
; s
> names
; )
strncpy(names
, PROG
, PROGLEN
);
/* run the command & read the resulting names */
if (fd
< 0) return names
;
j
= tread(fd
, tmpblk
.c
+ i
, BLKSIZE
- i
);
if (rpclose(fd
) == 0 && j
== 0 && i
< BLKSIZE
&& i
> 0)
tmpblk
.c
[i
-1] = '\0'; /* "i-1" so we clip off the newline */
/* This function runs a range of lines through a filter program, and replaces
* the original text with the filtered version. As a special case, if "to"
* is MARK_UNSET, then it runs the filter program with stdin coming from
* /dev/null, and inserts any output lines.
int filter(from
, to
, cmd
, back
)
MARK from
, to
; /* the range of lines to filter */
char *cmd
; /* the filter command */
int back
; /* boolean: will we read lines back? */
int scratch
; /* fd of the scratch file */
int fd
; /* fd of the pipe from the filter */
char scrout
[50]; /* name of the scratch out file */
MARK
new; /* place where new text should go */
long sent
, rcvd
; /* number of lines sent/received */
/* write the lines (if specified) to a temp file */
strcpy(scrout
, o_directory
);
if ((i
=strlen(scrout
)) && !strchr("\\/:", scrout
[i
-1]))
strcpy(scrout
+i
, SCRATCHOUT
+3);
sprintf(scrout
, SCRATCHOUT
, o_directory
);
cmd_write(from
, to
, CMD_BANG
, FALSE
, scrout
);
sent
= markline(to
) - markline(from
) + 1L;
/* use those lines as stdin */
scratch
= open(scrout
, O_RDONLY
);
/* start the filter program */
VMS doesn't know a thing about file descriptor 0. The rpipe
concept is non-portable. Hence we need a file name argument.
fd
= rpipe(cmd
, scratch
, scrout
);
fd
= rpipe(cmd
, scratch
);
/* adjust MARKs for whole lines, and set "new" */
/* Reading from a VMS mailbox (pipe) is record oriented... */
/* repeatedly read in new text and add it */
while ((i
= tread(fd
, tmpblk
.c
, BLKSIZE
- 1)) > 0)
/* What! An advantage to record oriented reads? */
new = (new & ~(BLKSIZE
- 1)) + BLKSIZE
;
for (i
= 0; tmpblk
.c
[i
]; i
++)
new = (new & ~(BLKSIZE
- 1)) + BLKSIZE
;
/* delete old text, if any */
/* read the command's output, and copy it to the screen */
while ((i
= tread(fd
, tmpblk
.c
, BLKSIZE
- 1)) > 0)
if (sent
>= *o_report
|| rcvd
>= *o_report
)
if (sent
> 0L && rcvd
> 0L)
msg("%ld lines out, %ld lines back", sent
, rcvd
);
msg("%ld lines written to filter", sent
);
msg("%ld lines read from filter", rcvd
);