Update of /cvsroot/pure-data/pd/src In directory sc8-pr-cvs1:/tmp/cvs-serv24977
Modified Files: Tag: devel_0_37 m_pd.h s_print.c Log Message: Added console print hook functionality. This is especially useful when PD is used as a shared library without its console-oriented main program and enables GUI systems to capture the output. See m_pd.h for details (functions sys_addprinthook, sys_rmprinthook)
Index: m_pd.h =================================================================== RCS file: /cvsroot/pure-data/pd/src/m_pd.h,v retrieving revision 1.1.1.4.2.7 retrieving revision 1.1.1.4.2.8 diff -C2 -d -r1.1.1.4.2.7 -r1.1.1.4.2.8 *** m_pd.h 23 Sep 2003 00:47:42 -0000 1.1.1.4.2.7 --- m_pd.h 14 Oct 2003 12:32:16 -0000 1.1.1.4.2.8 *************** *** 53,56 **** --- 53,59 ---- #endif
+ /* T.Grill - for print hooking */ + #include <stdarg.h> /* for va_list */ + #define MAXPDSTRING 1000 /* use this for anything you want */ #define MAXPDARG 5 /* max number of args we can typecheck today */ *************** *** 452,460 **** EXTERN void sys_ouch(void);
#ifdef __linux__ EXTERN char* sys_get_path( void); #endif EXTERN void sys_addpath(const char* p); -
/* ------------ system interface routines ------------------- */ --- 455,472 ---- EXTERN void sys_ouch(void);
+ /* T.Grill - hooking functionality for console printouts + This is especially useful when pd is used as a shared library without a main program. + See s_print.c for details. + Don't call PD print functions from a hook function! + */ + typedef void (*t_printhookfn)(char *fmt,va_list ap); + EXTERN int sys_addprinthook(t_printhookfn fn); + EXTERN int sys_rmprinthook(t_printhookfn fn); + + #ifdef __linux__ EXTERN char* sys_get_path( void); #endif EXTERN void sys_addpath(const char* p);
/* ------------ system interface routines ------------------- */
Index: s_print.c =================================================================== RCS file: /cvsroot/pure-data/pd/src/s_print.c,v retrieving revision 1.1.1.1 retrieving revision 1.1.1.1.16.1 diff -C2 -d -r1.1.1.1 -r1.1.1.1.16.1 *** s_print.c 29 Jul 2002 17:06:00 -0000 1.1.1.1 --- s_print.c 14 Oct 2003 12:32:16 -0000 1.1.1.1.16.1 *************** *** 10,40 **** #include <errno.h>
void post(char *fmt, ...) { va_list ap; - t_int arg[8]; - int i; va_start(ap, fmt); ! vfprintf(stderr, fmt, ap); va_end(ap); ! putc('\n', stderr); }
void startpost(char *fmt, ...) { va_list ap; - t_int arg[8]; - int i; va_start(ap, fmt); ! ! for (i = 0 ; i < 8; i++) arg[i] = va_arg(ap, t_int); va_end(ap); - fprintf(stderr, fmt, arg[0], arg[1], arg[2], arg[3], - arg[4], arg[5], arg[6], arg[7]); }
void poststring(char *s) { ! fprintf(stderr, " %s", s); }
--- 10,103 ---- #include <errno.h>
+ /* T.Grill - linked list structure */ + typedef struct t_printhook { + t_printhookfn hook; + struct t_printhook *nxt; + }; + + static struct t_printhook *printlist = NULL; + + /* T.Grill - this is the central printing function + Strings are passed to registered hook functions and to the console + */ + static void vprintcon(char *fmt,va_list ap) + { + /* eventually buffer stuff and pass strings?! */ + + /* call print hook functions */ + struct t_printhook *hk = printlist; + while(hk) { + hk->hook(fmt,ap); + hk = hk->nxt; + } + + /* print to console */ + vfprintf(stderr,fmt,ap); + } + + static void printcon(char *fmt,...) + { + va_list ap; + va_start(ap,fmt); + vprintcon(fmt,ap); + va_end(ap); + } + + /* T.Grill - add print hook function */ + int sys_addprinthook(t_printhookfn fn) + { + struct t_printhook *hkel = getbytes(sizeof(struct t_printhook)); + hkel->hook = fn; + /* insert as first element */ + hkel->nxt = printlist; + printlist = hkel; + + return 0; + } + + /* T.Grill - remove print hook function */ + int sys_rmprinthook(t_printhookfn fn) + { + struct t_printhook *hk = printlist,*hkprv = NULL; + + /* iterate through list */ + for(; hk && hk->hook != fn; hkprv = hk,hk = hk->nxt); + + if(hk) { + /* function was found -> detach from list */ + if(hkprv) + hkprv->nxt = hk->nxt; + else + printlist = hk->nxt; + + freebytes(hk,sizeof(struct t_printhook)); + return 0; + } + else + return -1; + } + + void post(char *fmt, ...) { va_list ap; va_start(ap, fmt); ! vprintcon(fmt, ap); va_end(ap); ! printcon("\n"); }
+ /* T.Grill: This is identical to printcon .... */ void startpost(char *fmt, ...) { va_list ap; va_start(ap, fmt); ! vprintcon(fmt, ap); va_end(ap); }
void poststring(char *s) { ! printcon(" %s",s); }
*************** *** 60,64 **** void endpost(void) { ! fprintf(stderr, "\n"); }
--- 123,127 ---- void endpost(void) { ! printcon("\n"); }
*************** *** 66,76 **** { va_list ap; - t_int arg[8]; - int i; va_start(ap, fmt); ! fprintf(stderr, "error: "); ! vfprintf(stderr, fmt, ap); va_end(ap); ! putc('\n', stderr); }
--- 129,137 ---- { va_list ap; va_start(ap, fmt); ! printcon("error: "); ! vprintcon(fmt, ap); va_end(ap); ! printcon("\n"); }
*************** *** 86,96 **** { va_list ap; - t_int arg[8]; - int i; static int saidit = 0; va_start(ap, fmt); vsprintf(error_string, fmt, ap); va_end(ap); ! fprintf(stderr, "error: %s\n", error_string); error_object = object; if (!saidit) --- 147,155 ---- { va_list ap; static int saidit = 0; va_start(ap, fmt); vsprintf(error_string, fmt, ap); va_end(ap); ! printcon("error: %s\n", error_string); error_object = object; if (!saidit) *************** *** 116,129 **** { va_list ap; ! t_int arg[8]; ! int i; va_start(ap, fmt); ! ! for (i = 0 ; i < 8; i++) arg[i] = va_arg(ap, t_int); va_end(ap); ! fprintf(stderr, "Consistency check failed: "); ! fprintf(stderr, fmt, arg[0], arg[1], arg[2], arg[3], ! arg[4], arg[5], arg[6], arg[7]); ! putc('\n', stderr); }
--- 175,183 ---- { va_list ap; ! printcon("Consistency check failed: "); va_start(ap, fmt); ! vprintcon(fmt,ap); va_end(ap); ! printcon("\n"); }