home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
aztec-c
/
azpipe11.ark
/
AZPIPE11.C
next >
Wrap
Text File
|
1989-11-12
|
4KB
|
168 lines
#include <stdio.h>
#include <io.h>
#include <sgtty.h>
#include <ctype.h>
#define NUM_MSG 20
#define MSG_SIZ 20
#define TRUE 1
#define FALSE 0
char msgarray[NUM_MSG][MSG_SIZ]; /* local array of args to pass on */
int pipeflag; /* local flag for pipe_set and pipe_exec: TRUE == piping */
char pipein[] = "pipe.in";
char pipeout[] = "pipe.out";
/*
Version 1.1 12 Nov 1989 Bob Presswood
*/
pipe_set(argc,argv)
int *argc; char *argv[];
{
/* Structures channel and fcbtab are defined in io.h */
struct channel *chanptr;
struct fcbtab *fcbptr;
char drive[2], name[9], type[4], user[3];
char *p, *t;
int i, j, n;
pipeflag = FALSE;
for (i = 1; i < *argc; i++){
/* If pipein exists in arg list, redirect stdin. */
if (!strcmp(argv[i],pipein)){
freopen(pipein,"r",stdin);
for (j = i; j < (*argc - 1); j++)
argv[j] = argv[j+1];
(*argc)--;
break;
}
}
/* If < 2 args, there can't be a pipe. */
if (*argc < 2){
return();
}
for (i = 1; i < *argc; i++){
if (!strcmp(argv[i],"|")){
for (j = 0; j < NUM_MSG; j++)
msgarray[j][0] = 0; /* Null out msgarray */
if ((p = index(argv[i+1],':')) && !index(argv[i+1],'/') && (p > (argv[i+1] + 1))){
drive[0] = *argv[i+1];
drive[1] = user[0] = user[1] = user[2] = '\0';
for (j = 1; j < 3; j++){
t = argv[i+1] + j;
if (isdigit(*t))
user[j-1] = *t;
}
strcpy(msgarray[0],user);
strcat(msgarray[0],"/");
strcat(msgarray[0],drive);
strcat(msgarray[0],p);
} else
strcpy(msgarray[0], argv[i+1]); /* Prog to run next */
strcpy(msgarray[1], "?"); /* not passed by execv */
strcpy(msgarray[2], pipein); /* stdin from pipein */
for (j = i + 2, n = 3; j < *argc; j++, n++)
strcpy(msgarray[n], argv[j]); /* Any other args */
/* Is stdout redirected to a file? */
if (!isatty(1)){
drive[1] = name[8] = type[3] = user[2] = '\0';
/* chantab from io.h, 1 is stdout */
chanptr = &chantab[1];
/* c_arg of chantab[1] is a pointer to stdout fcbtab */
fcbptr = chanptr->c_arg;
/* recover file, etc. for later stdout redirection */
if (fcbptr->fcb.f_driv)
drive[0] = fcbptr->fcb.f_driv + '@';
else
drive[0] = '\0';
user[0] = (fcbptr->user > 9 ? '1' : '0');
user[1] = (fcbptr->user > 9 ? fcbptr->user - 10 + '0' : fcbptr->user + '0');
strncpy(name,fcbptr->fcb.f_name,8);
if (p = index(name,' '))
*p = '\0';
strncpy(type,fcbptr->fcb.f_type,3);
/* add redirected stdout file to msgarray */
if (fcbptr->fcb.f_driv)
sprintf(msgarray[n],"^%s/%s:%s.%s",user,drive,name,type);
else
sprintf(msgarray[n],"^%s/%s.%s",user,name,type);
}
freopen(pipeout,"w",stdout);
*argc = i; /* Extra args not needed for this prog. */
pipeflag = TRUE;
break;
} else if (*argv[i] == '^'){
/* '^' used to flag for output redirection at end of piping */
freopen(argv[i]+1,"w",stdout);
(*argc)--;
break;
}
}
}
#define NUMPATH 5
char path[NUMPATH][6] = {
"00/A:",
"00/B:",
"00/C:",
"00/D:",
"00/E:"
};
pipe_exec()
{
char *msgv[NUM_MSG-1];
char user, current[6], *p, fname[13];
int i;
/* Erase pipein, ignoring error if it does not exist. */
unlink(pipein);
if (pipeflag == TRUE){
/* Close stdin and stdout, whether redirected or not. */
fclose(stdin);
fclose(stdout);
/* If pipeout was created, it must be changed to pipein now. */
rename(pipeout,pipein);
/* Set array of pointers to char. */
for (i = 0; i < NUM_MSG - 1; i++)
if (strlen(msgarray[i+1]))
msgv[i] = &msgarray[i+1];
else
msgv[i] = NULL;
/* Chain and pass args. */
execv(msgarray[0],msgv);
strcpy(current," / :");
user = bdos(0x20,0xff);
current[3] = bdos(0x19,0) + 'A' ;
current[0] = (user > 9 ? '1' : '0');
current[1] = (user > 9 ? user - 10 + '0' : user + '0');
if (p = index(msgarray[0],':'))
strcpy(fname,p+1);
else if (p = index(msgarray[0],'/'))
strcpy(fname,p+1);
else
strcpy(fname,msgarray[0]);
for (i = 0; i < NUMPATH; i++){
if (strcmp(current,path[i])){
strcpy(msgarray[0],path[i]);
strcat(msgarray[0],fname);
execv(msgarray[0],msgv);
}
}
fprintf(stderr,"\nFile %s not found for piping.\n", fname);
exit(1);
}
}