Hi all,
I'm tinkering with some fuzzy logic code generators, and though they produce ANSI-C, my skills as a programmer aren't quite up to the job of making generated code wrap around PD-ways. I thought of maybe doing the pipe thing, so I wrote (with help from local LUG) the bit of C below...any advice on how to wrap this in PD? Also, is this something I should be able to do from within PD, say with 'shell' (and BTW, where -can- I find the shell object)
cheers, dc
/* a bit of c to do pipes, by dc
*/ #include <sys/param.h>
#include <sys/types.h> #include <sys/socket.h>
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <getopt.h> /* needed by some glibc versions, remove if trouble */
enum { DEBUG = 0x1 };
void myerr(int, char *); void usage(void); void do_half(int, char *);
extern char **environ; int flags;
int main(int argc, char *argv[]) { int fd[2], x;
while ((x = getopt(argc, argv, "dh")) != -1)
{
switch (x)
{
case 'd':
flags |= DEBUG;
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd) == -1)
myerr(1, "pipe");
switch (fork())
{
case -1:
myerr(1, "fork");
case 0:
close(fd[1]);
do_half(fd[0], argv[0]);
break;
default:
close(fd[0]);
do_half(fd[1], argv[1]);
break;
}
return 1;
}
void do_half(int fd, char *prog) { char *argv[4]; int x;
if (flags & DEBUG)
x = 1;
else
x = 2;
for (; x >= 0; x--)
if (dup2(fd, x) == -1)
myerr(1, "dup2");
close(fd);
argv[0] = "/bin/sh";
argv[1] = "-c";
argv[2] = prog;
argv[3] = NULL;
execve(argv[0], argv, environ);
myerr(1, "execve");
}
void myerr(int status, char *str) { perror(str); exit(status); }
void usage(void) { fprintf(stderr, "usage: bi-pipe [-d] <cmd one> <cmd two>\n\n"); exit(1); }
www.ariada.uea.ac.uk/~dcasal --)+