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 --)+
Le Samedi 9 Mars 2002 02:10, david casal a écrit :
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?
If i've well understand, your goal is to create one external object for pure-data.
Have a look to the help of pd : doc/6.externs.
Damien.
On Sat, 9 Mar 2002, Damien HENRY wrote:
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?
If i've well understand, your goal is to create one external object for pure-data.
Well, no, the problem is that my programming skills aren't up to the job of converting generated code (from dodgy fuzzy logic apps) into a PD object...so I thought I'd 'simply' use unix named pipes to ask PD to talk to the C prog I have. Hence the bit of C in my message. THe question is: am I doing this the hard way? Couldn't the shell object do this? Or is it easy to just wrap my pipe program within a PD external?
Have a look to the help of pd : doc/6.externs.
Yep read it, and also Zmoelnig's tutorial on IEM. It's not just that I'm a bit lazy, I really just can't follow it. It is a very good tutorial and clear, I'm just not at the point where I can translate that to a working object with lots of wild C from somewhere else...
Ideas?
DC
Damien.
www.ariada.uea.ac.uk/~dcasal --)+