From 77c7a463710ee0a5f95630c07da304fd0d296d3f Mon Sep 17 00:00:00 2001 From: carmen rocco Date: Tue, 8 Mar 2005 21:20:35 +0000 Subject: popen() svn path=/trunk/externals/control/popen/; revision=2606 --- popen.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 popen.c (limited to 'popen.c') diff --git a/popen.c b/popen.c new file mode 100755 index 0000000..547fac4 --- /dev/null +++ b/popen.c @@ -0,0 +1,90 @@ +/* :popen: for PD - windows + linux + (probably mac) - carmen rocco */ + +#include +#include +#include +#include +#define INBUFSIZE 1024 + +static t_class *popen_class; + +typedef struct _popen +{ + t_object x_obj; + char buffer[128]; + t_symbol *x_s; + t_outlet* x_done; +} t_popen; + +static void popen_out(t_popen* x) +{ + int i; + int size = 0; + size = strlen(x->buffer); + for (i=0;ibuffer[i] == '\n' || x->buffer[i] == '\r') x->buffer[i] = '\0'; + fprintf(stderr, "%s",x->buffer); + x->x_s = gensym(x->buffer); + outlet_symbol(x->x_obj.ob_outlet, x->x_s); +} + +static void popen_anything(t_popen *x, t_symbol *s, int argc, t_atom *argv) +{ + int i; + char arg[MAXPDSTRING]; + char cmd[20] = ""; + FILE *pPipe; + int size = 0; + for (i=0;is_name); + strcat(cmd, " "); + strcat(cmd, arg); + post("sending %s",cmd); + +#ifdef NT + if( (pPipe = _popen( cmd, "rt" )) == NULL ) +#else + if( (pPipe = popen( cmd, "r" )) == NULL ) +#endif + return; + + while( !feof( pPipe ) ) + { + if( fgets( x->buffer, 128, pPipe ) != NULL ) + popen_out(x); + } +#ifdef NT + _pclose( pPipe ); +#else + pclose( pPipe ); +#endif + + outlet_bang(x->x_done); +} + +void popen_free(t_popen* x) +{ +} + +static void *popen_new(void) +{ + t_popen *x = (t_popen *)pd_new(popen_class); + outlet_new(&x->x_obj, &s_symbol); + x->x_done = outlet_new(&x->x_obj, &s_bang); + return (x); +} + +void popen_setup(void) +{ + popen_class = class_new(gensym("popen"), (t_newmethod)popen_new, + (t_method)popen_free,sizeof(t_popen), 0,0); + class_addbang(popen_class,popen_out); + class_addanything(popen_class, popen_anything); +} -- cgit v1.2.1