int sys_run_scheduler(const char *externalschedlibname, const char *sys_extraflagsstring) { typedef int (*t_externalschedlibmain)(const char *); t_externalschedlibmain externalmainfunc; char filename[MAXPDSTRING]; struct stat statbuf; snprintf(filename, sizeof(filename), "%s%s", externalschedlibname, sys_dllextent); sys_bashfilename(filename, filename); /* if first-choice file extent can't 'stat', go for second */ if (stat(filename, &statbuf) < 0) { snprintf(filename, sizeof(filename), "%s%s", externalschedlibname, sys_dllextent2); sys_bashfilename(filename, filename); } #ifdef _WIN32 { HINSTANCE ntdll = LoadLibrary(filename); if (!ntdll) { fprintf(stderr, "%s: couldn't load external scheduler\n", filename); error("%s: couldn't load external scheduler", filename); return (1); } externalmainfunc = (t_externalschedlibmain)GetProcAddress(ntdll, "pd_extern_sched"); if (!externalmainfunc) externalmainfunc = (t_externalschedlibmain)GetProcAddress(ntdll, "main"); } #elif defined HAVE_LIBDL { void *dlobj; dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); if (!dlobj) { error("%s: %s", filename, dlerror()); fprintf(stderr, "dlopen failed for %s: %s\n", filename, dlerror()); return (1); } externalmainfunc = (t_externalschedlibmain)dlsym(dlobj, "pd_extern_sched"); } #else return (0); #endif if (externalmainfunc) return((*externalmainfunc)(sys_extraflagsstring)); else { fprintf(stderr, "%s: couldn't find pd_extern_sched() or main()\n", filename); return (0); } }
/* save as MAXTEXT */ void binport_write(t_binbuf *bb, char *filename, char *dirname) { int result; FILE *fp; char namebuf[MAXPDSTRING]; namebuf[0] = 0; if (*dirname) strcat(namebuf, dirname), strcat(namebuf, "/"); strcat(namebuf, filename); sys_bashfilename(namebuf, namebuf); if (fp = fopen(namebuf, "w")) { char buf[BINPORT_MAXSTRING]; t_atom *ap = binbuf_getvec(bb); int cnt = 0, ac = binbuf_getnatom(bb); while (ac--) { if (ap->a_type == A_SEMI) { fputs(";\n", fp); cnt = 0; } else if (ap->a_type != A_NULL) { if (cnt++) fputc(' ', fp); lex_atomstring(ap, buf, BINPORT_MAXSTRING, A_INT); fputs(buf, fp); } ap++; } fclose(fp); } }
static void plustot_env_evalfile(t_plustot_env *x, t_symbol *fname) { char buf1[MAXPDSTRING], buf2[MAXPDSTRING], *nameptr, *dir; int fd; dir = canvas_getdir(x->x_glist)->s_name; if ((fd = open_via_path(dir, fname->s_name, "", buf1, &nameptr, MAXPDSTRING, 0)) < 0) { loud_error((t_pd *)x, "file '%s' not found", fname->s_name); } else { Tcl_Interp *interp = plustin_getinterp(x->x_tin); FILE *fp; close(fd); strcpy(buf2, buf1); strcat(buf2, "/"); strcat(buf2, nameptr); sys_bashfilename(buf2, buf2); Tcl_Preserve(interp); if (Tcl_EvalFile(interp, buf2) != TCL_OK) { strcpy(buf1, "evaluation failed ("); strncat(buf1, buf2, MAXPDSTRING - strlen(buf1) - 2); strcat(buf1, ")"); plusloud_tclerror((t_pd *)x, interp, buf1); } Tcl_Release(interp); } }
FILE *fileread_open(char *filename, t_canvas *cv, int textmode) { int fd; char path[MAXPDSTRING+2], *nameptr; t_symbol *dirsym = (cv ? canvas_getdir(cv) : 0); /* path arg is returned unbashed (system-independent) */ if ((fd = open_via_path((dirsym ? dirsym->s_name : ""), filename, "", path, &nameptr, MAXPDSTRING, 1)) < 0) return (0); /* Closing/reopening dance. This is unnecessary under linux, and we could have tried to convert fd to fp, but under windows open_via_path() returns what seems to be an invalid fd. LATER try to understand what is going on here... */ close(fd); if (path != nameptr) { char *slashpos = path + strlen(path); *slashpos++ = '/'; /* try not to be dependent on current open_via_path() implementation */ if (nameptr != slashpos) strcpy(slashpos, nameptr); } sys_bashfilename(path, path); return (fopen(path, (textmode ? "r" : "rb"))); }
static void h_deque_read_at_xml(t_h_deque *x, t_symbol *s, int argc, t_atom *argv) { string symbol; int index=0; switch(argc) { default: post("h_deque read: only two argument are possible!"); break; case 2: symbol = argv[0].a_w.w_symbol->s_name; index = (int)argv[1].a_w.w_float; break; case 1: symbol = argv[0].a_w.w_symbol->s_name; index = 0; break; case 0: post("h_deque read: no filename!"); } // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, (char*)symbol.c_str(), filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); if(!x->hdeque->readFromFile2XML(filename,index)) post("h_deque: couldn't read from file %s",s->s_name); }
static void fwriteln_open (t_fwriteln *x, t_symbol *s, t_symbol*type) { char* filename; string_copy(s->s_name, &filename); sys_bashfilename (filename, filename); fwriteln_close (x); /* if(0==type || type!=gensym("cr")) { pd_error(x, "unknown type '%s'", (type)?type->s_name:""); return; }*/ if (type==gensym("cr")) strcpy(x->linebreak_chr,"\n"); else strcpy(x->linebreak_chr,";\n"); if (!(x->x_file=fopen(filename, "w"))) { pd_error(x, "failed to open %128s",filename); free(filename); return; } string_copy(filename, &x->x_filename); free(filename); x->x_textbuf = (char *) getbytes (MAXPDSTRING + 1); }
static GemDylibHandle*open(const std::string filename) { GemDylibHandle*handle=new GemDylibHandle(); char buf[MAXPDSTRING]; if(filename.empty()) { throw(GemException(std::string("No DyLib name given!"))); } sys_bashfilename(filename.c_str(), buf); #ifdef DL_OPEN handle->dlhandle=dlopen(filename.c_str(), RTLD_NOW); if(handle->dlhandle) { handle->fullname=filename; return handle; } #endif #ifdef _WIN32 UINT errorboxflags=SetErrorMode(SEM_FAILCRITICALERRORS); handle->w32handle=LoadLibrary(buf); errorboxflags=SetErrorMode(errorboxflags); if(handle->w32handle) { handle->fullname=filename; return handle; } #endif delete handle; handle=NULL; std::string errormsg; #ifdef DL_OPEN errormsg=dlerror(); if(!errormsg.empty()) { std::string error="dlerror '"; error+=errormsg; error+="'"; throw(GemException(error)); } #endif #ifdef _WIN32 DWORD errorNumber = GetLastError(); LPVOID lpErrorMessage; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorNumber, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpErrorMessage, 0, NULL ); //std::cerr << "GemDylib: "<<errorNumber<<std::endl; std::string error = "DLLerror: "; error+=(unsigned int)errorNumber; throw(GemException(error)); #endif return NULL; }
static void matrix_write(t_matrix *x, t_symbol *filename) { t_atom *ap=x->atombuffer+2; char filnam[MAXPDSTRING]; int rows = x->row, cols = x->col; FILE *f=0; sys_bashfilename(filename->s_name, filnam); /* open file */ if (!(f = fopen(filnam, "w"))) { pd_error(x, "[matrix]: failed to open %128s", filnam); } else { char *text=(char *)getbytes(sizeof(char)*MAXPDSTRING); int textlen; /* header: * we now write "#matrix" instead of "matrix", * so that these files can easily read by other * applications such as octave */ snprintf(text, MAXPDSTRING, "#matrix %d %d\n", rows, cols); text[MAXPDSTRING-1]=0; textlen = strlen(text); if (fwrite(text, textlen*sizeof(char), 1, f) < 1) { pd_error(x, "[matrix]: failed to write %128s", filnam); goto end; } while(rows--) { int c = cols; while (c--) { t_float val = atom_getfloat(ap++); snprintf(text, MAXPDSTRING, "%.15f ", val); text[MAXPDSTRING-1]=0; textlen=strlen(text); if (fwrite(text, textlen*sizeof(char), 1, f) < 1) { pd_error(x, "[matrix]: failed to write %128s", filnam); goto end; } } if (fwrite("\n", sizeof(char), 1, f) < 1) { pd_error(x, "[matrix]: failed to write %128s", filnam); goto end; } } freebytes(text, sizeof(char)*MAXPDSTRING); } end: /* close file */ if (f) { fclose(f); } }
static void h_list_read_xml(t_h_list *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); if(!x->hlist->readFromFileXML(filename)) post("h_list: couldn't read from file %s",s->s_name); }
static void h_deque_read(t_h_deque *x, t_symbol *s, int argc, t_atom *argv) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); if(!x->hdeque->readFromFile(filename)) post("h_deque: couldn't read from file %s",s->s_name); }
static void lms_tilde_read(t_lms_tilde *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); // read file adaptation_read(filename, &x->N, &x->mu, x->c, x->buf); }
static void lms_tilde_write(t_lms_tilde *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); // save to file adaptation_write(filename, x->N, x->mu, x->c); }
static void h_multimap_read(t_h_multimap *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); if(!x->hmultimap->readFromFile(filename)) post("h_multimap: couldn't read from file %s",s->s_name); }
FILE *sys_fopen(const char *filename, const char *mode) { char namebuf[MAXPDSTRING]; wchar_t ucs2buf[MAXPDSTRING]; wchar_t ucs2mode[MAXPDSTRING]; sys_bashfilename(filename, namebuf); u8_utf8toucs2(ucs2buf, MAXPDSTRING, namebuf, MAXPDSTRING-1); /* mode only uses ASCII, so no need for a full conversion, just copy it */ mbstowcs(ucs2mode, mode, MAXPDSTRING); return (_wfopen(ucs2buf, ucs2mode)); }
int sys_trytoopenone(const char *dir, const char *name, const char* ext, char *dirresult, char **nameresult, unsigned int size, int bin) { int fd; char buf[MAXPDSTRING]; if (strlen(dir) + strlen(name) + strlen(ext) + 4 > size) return (-1); sys_expandpath(dir, buf, MAXPDSTRING); strcpy(dirresult, buf); if (*dirresult && dirresult[strlen(dirresult)-1] != '/') strcat(dirresult, "/"); strcat(dirresult, name); strcat(dirresult, ext); sys_bashfilename(dirresult, dirresult); DEBUG(post("looking for %s",dirresult)); /* see if we can open the file for reading */ if ((fd=open(dirresult,O_RDONLY | MSWOPENFLAG(bin))) >= 0) { /* in unix, further check that it's not a directory */ #ifdef HAVE_UNISTD_H struct stat statbuf; int ok = ((fstat(fd, &statbuf) >= 0) && !S_ISDIR(statbuf.st_mode)); if (!ok) { if (sys_verbose) post("tried %s; stat failed or directory", dirresult); close (fd); fd = -1; } else #endif { char *slash; if (sys_verbose) post("tried %s and succeeded", dirresult); sys_unbashfilename(dirresult, dirresult); slash = strrchr(dirresult, '/'); if (slash) { *slash = 0; *nameresult = slash + 1; } else *nameresult = dirresult; return (fd); } } else { if (sys_verbose) post("tried %s and failed", dirresult); } return (-1); }
FILE *filewrite_open(char *filename, t_canvas *cv, int textmode) { char path[MAXPDSTRING+2]; if (cv) /* path arg is returned unbashed (system-independent) */ canvas_makefilename(cv, filename, path, MAXPDSTRING); else { strncpy(path, filename, MAXPDSTRING); path[MAXPDSTRING-1] = 0; } sys_bashfilename(path, path); return (fopen(path, (textmode ? "w" : "wb"))); }
static void h_list_save_xml(t_h_list *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); if(x->hlist->saveToFileXML(filename)) post("h_list: data of namespace %s written to file %s", x->hlist->getNamespace().c_str(),s->s_name); else post("h_list: couldn't write to file %s",s->s_name); }
int sys_open(const char *path, int oflag, ...) { int i, fd; char pathbuf[MAXPDSTRING]; wchar_t ucs2path[MAXPDSTRING]; sys_bashfilename(path, pathbuf); u8_utf8toucs2(ucs2path, MAXPDSTRING, pathbuf, MAXPDSTRING-1); /* For the create mode, Win32 does not have the same possibilities, * so we ignore the argument and just hard-code read/write. */ if (oflag & O_CREAT) fd = _wopen(ucs2path, oflag | O_BINARY, _S_IREAD | _S_IWRITE); else fd = _wopen(ucs2path, oflag | O_BINARY); return fd; }
static void sdiflists_types(t_sdiflists *x, t_symbol *types) { if(x->markers[0].filepos != -1 ) { t_colout *u; int j; SdifFClose(x->file); for(j=0; j < x->nframes; j++) x->markers[j].timetag = x->markers[j].filepos = 0; x->markers[0].timetag = -1; for ( j=0, u = x->data; j < x->n_outs; u++, j++) freebytes( u->outvec, x->max_vec * sizeof(t_atom)); #if DEBUG post("sdiflists::types: closed previous file"); #endif x->nframes = IFRAMES; } //prepend the full path onto the filename char fullfilename[MAXPDSTRING], namebuf[MAXPDSTRING]; char buf[MAXPDSTRING], *bufptr, *dirname; int fd = 0; dirname=canvas_getdir(x->canvas)->s_name; t_binbuf *bbuf = binbuf_new(); fd = open_via_path(dirname, types->s_name,"", buf, &bufptr, MAXPDSTRING, 0); if(fd > 0) { namebuf[0] = 0; if (*buf) strcat(namebuf, buf), strcat(namebuf, "/"); strcat(namebuf, bufptr); sys_bashfilename(namebuf, fullfilename); SdifGenKill (); // have to close it to read a new .STYP SdifGenInitCond (types->s_name); post("sdiflists: types-declaration file: %s", types->s_name); } else post ("sdiflists: types file %s could not be opened", types->s_name); }
static void nlms2_tilde_read(t_nlms2_tilde *x, t_symbol *s) { // make correct path char filnam[MAXPDSTRING]; char filename[MAXPDSTRING]; int n = x->N; canvas_makefilename(x->x_canvas, s->s_name, filnam, MAXPDSTRING); sys_bashfilename(filnam, filename); // read file adaptation_read(filename, &x->N, &x->mu, x->c, x->buf); // if length changes: if(x->N != n) { if(x->coef) freebytes(x->coef, sizeof(t_atom) * x->N); x->coef = (t_atom *)getbytes(sizeof(t_atom) * x->N); } }
int sys_open(const char *path, int oflag, ...) { int i, fd; char pathbuf[MAXPDSTRING]; sys_bashfilename(path, pathbuf); if (oflag & O_CREAT) { mode_t mode; int imode; va_list ap; va_start(ap, oflag); /* Mac compiler complains if we just set mode = va_arg ... so, even though we all know it's just an int, we explicitly va_arg to an int and then convert. -> http://www.mail-archive.com/[email protected]/msg14212.html -> http://bugs.debian.org/647345 */ imode = va_arg (ap, int); mode = (mode_t)imode; va_end(ap); fd = open(pathbuf, oflag, mode); }
/*-------------------open files, STYP first if given, then SDIF-----------------*/ static void sdiflists_open(t_sdiflists *x, t_symbol *s, int argcount, t_atom *argvec) { int i,j; unsigned int StreamSpecified = 0; for (i = 0; i < argcount; i++) { if (argvec[i].a_type == A_FLOAT) {//TODO: check if this was given after file, cuz that would be useless x->streamid = (int) argvec[i].a_w.w_float; StreamSpecified = 1; post("stream specified:#%d", x->streamid); } if (argvec[i].a_type == A_SYMBOL) { char *sym = argvec[i].a_w.w_symbol->s_name; if(!strcmp( sym + strlen(sym)-5 , ".sdif" ) || !strcmp( sym + strlen(sym)-5 , ".SDIF" )) { t_colout *u; if(x->markers[0].timetag != -1) { post("closing file... "); SdifFClose(x->file); for(j=0; j < x->nframes; j++) x->markers[j].timetag = x->markers[j].filepos = 0; for(j=0, u = x->data; j < x->n_outs; u++, j++) freebytes( u->outvec, x->max_vec * sizeof(t_atom)); x->markers[0].timetag = -1; x->index = -1; x->nframes = IFRAMES; #if DEBUG post("sdiflists::open: closed previous file"); #endif }// end if x->file exists unsigned int rows = 0; unsigned int cols = 0; t_int eof, m, updatepos; t_int firstframe = 1; t_int timepos = 0; size_t bytesread = 0; SdiffPosT currpos; float currtime = 0; /*method for opening file in canvas directory. Based on zexy's [msgfile], which is based on Pd's [textfile]*/ char filnam[MAXPDSTRING], namebuf[MAXPDSTRING]; char buf[MAXPDSTRING], *bufptr, *readbuf; int fd; // used to check if file exists char *dirname; dirname = canvas_getdir(x->canvas)->s_name; t_binbuf *bbuf = binbuf_new(); fd = open_via_path(dirname, sym,"", buf, &bufptr, MAXPDSTRING, 0); if(fd < 0) { error("sdiflists-open: %s cannot be found", sym); return; } namebuf[0] = 0; if (*buf) strcat(namebuf, buf), strcat(namebuf, "/"); strcat(namebuf, bufptr); // open and get length sys_bashfilename(namebuf, filnam); //this is hopefully a readable file #if DEBUG post("(open_via_path) dirname: %s, filename->s_name: %s, buf: %s, bufptr: %s", dirname, sym, buf, bufptr); post("AFTER bashfilename: namebuf: %s, filnam: %s ", namebuf, filnam); #endif x->filename = gensym( namebuf ); /* Check if the file is a good SDIF file, skip function if not */ if (SdifCheckFileFormat (x->filename->s_name)) { post("sdiflists: reading %s", x->filename->s_name); x->file = SdifFOpen ( x->filename->s_name, eReadFile); bytesread += SdifFReadGeneralHeader (x->file); bytesread += SdifFReadAllASCIIChunks (x->file); eof = SdifFCurrSignature(x->file) == eEmptySignature; int err; while (!eof) //frame loop { /*The frame positions must be indexed before the frameheader is read, then check if it is a selected frame. If not, skip the frame and overwrite the marker.*/ err = SdifFGetPos(x->file, &currpos); if(err==-1) error("error SdifFGetPos"); /* Read frame header. Current signature has already been read by SdifFReadAllASCIIChunks or the last loop.) */ bytesread += SdifFReadFrameHeader (x->file); if(!StreamSpecified) { x->streamid = SdifSelectGetFirstInt(x->file->Selection->stream, SdifFCurrID (x->file)); StreamSpecified = 1; post("first stream used: #%d", x->streamid); } //PROBLEM: (maybe not...check)the last frame in the file is always acceptable... fix by using sel spec while (!SdifFCurrFrameIsSelected (x->file) || SdifFCurrID (x->file) != x->streamid ) { // post("frame skipped"); SdifFSkipFrameData (x->file); if ((eof = SdifFGetSignature(x->file, &bytesread) == eEof)) break; SdifFGetPos(x->file, &currpos); bytesread += SdifFReadFrameHeader(x->file); } if(eof) //have to check again...since it might have skipped to the end break; //check if this is a new time so successive frames don't overwrite filepos currtime = SdifFCurrTime (x->file); if( !timepos || x->markers[timepos-1].timetag != currtime ) { x->markers[timepos].filepos = currpos; x->markers[timepos].timetag = currtime; timepos++; if( timepos >= x->nframes ) { x->markers = (t_pos *)resizebytes( x->markers, x->nframes * sizeof(t_pos), (x->nframes + IFRAMES) * sizeof(t_pos) ); x->nframes = x->nframes + IFRAMES; } } /*matrices loop */ for ( m = 0; (unsigned int)m < SdifFCurrNbMatrix (x->file); m++) { bytesread += SdifFReadMatrixHeader (x->file); if( SdifFCurrNbRow (x->file) > rows) { rows = SdifFCurrNbRow (x->file); //get matrix stats cols = SdifFCurrNbCol (x->file);//should stay the same } //skip the actual matrices bytesread += SdifFSkipMatrixData (x->file); }// end for matrices eof = SdifFGetSignature (x->file, &bytesread) == eEof; }// end while no eof x->seconds = currtime; x->nframes= timepos; //last timepos was the eof #if DEBUG post(" rows: %d , cols: %d , frames: %d , seconds: %f", rows, cols, x->nframes, x->seconds); post("sdiflists: %s opened. ", x->filename->s_name); #endif x->max_vec = rows; //needed to free memory //the following lines are a bug work-around... if the eof is reached, you cannot seek //until the fle is closed, opened, and re-initialized... i posted about this on the sdif list. SdifFClose (x->file); x->file = SdifFOpen ( x->filename->s_name, eReadFile); SdifFReadGeneralHeader (x->file); SdifFReadAllASCIIChunks (x->file); eof = SdifFCurrSignature(x->file) == eEmptySignature; //make room for the row lists for ( i=0, u = x->data; i < x->n_outs; u++, i++) u->outvec = (t_atom *)getbytes( x->max_vec * sizeof(t_atom)); } /* end if filetype check */ } } //end if Symbol }//end for arguments }
static int sys_do_load_lib(t_canvas *canvas, char *objectname) { char symname[MAXPDSTRING], filename[MAXPDSTRING], dirbuf[MAXPDSTRING], *classname, *nameptr, altsymname[MAXPDSTRING]; void *dlobj; t_xxx makeout = NULL; int i, hexmunge = 0, fd; #ifdef _WIN32 HINSTANCE ntdll; #endif if (classname = strrchr(objectname, '/')) classname++; else classname = objectname; if (sys_onloadlist(objectname)) { post("%s: already loaded", objectname); return (1); } for (i = 0, nameptr = classname; i < MAXPDSTRING-7 && *nameptr; nameptr++) { char c = *nameptr; if ((c>='0' && c<='9') || (c>='A' && c<='Z')|| (c>='a' && c<='z' )|| c == '_') { symname[i] = c; i++; } /* trailing tilde becomes "_tilde" */ else if (c == '~' && nameptr[1] == 0) { strcpy(symname+i, "_tilde"); i += strlen(symname+i); } else /* anything you can't put in a C symbol is sprintf'ed in hex */ { sprintf(symname+i, "0x%02x", c); i += strlen(symname+i); hexmunge = 1; } } symname[i] = 0; if (hexmunge) { memmove(symname+6, symname, strlen(symname)+1); strncpy(symname, "setup_", 6); } else strcat(symname, "_setup"); #if 0 fprintf(stderr, "lib: %s\n", classname); #endif /* try looking in the path for (objectname).(sys_dllextent) ... */ if ((fd = canvas_open(canvas, objectname, sys_dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; /* same, with the more generic sys_dllextent2 */ if ((fd = canvas_open(canvas, objectname, sys_dllextent2, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; /* next try (objectname)/(classname).(sys_dllextent) ... */ strncpy(filename, objectname, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, classname, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; if ((fd = canvas_open(canvas, filename, sys_dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; if ((fd = canvas_open(canvas, filename, sys_dllextent2, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; return (0); gotone: close(fd); class_set_extern_dir(gensym(dirbuf)); /* rebuild the absolute pathname */ strncpy(filename, dirbuf, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, nameptr, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; #ifdef HAVE_LIBDL dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); if (!dlobj) { post("%s: %s", filename, dlerror()); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)dlsym(dlobj, symname); /* fprintf(stderr, "symbol %s\n", symname); */ #endif #ifdef _WIN32 sys_bashfilename(filename, filename); ntdll = LoadLibrary(filename); if (!ntdll) { post("%s: couldn't load", filename); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)GetProcAddress(ntdll, symname); #endif if (!makeout) { post("load_object: Symbol \"%s\" not found", symname); class_set_extern_dir(&s_); return 0; } (*makeout)(); class_set_extern_dir(&s_); sys_putonloadlist(objectname); return (1); }
static int sys_do_load_lib(t_canvas *canvas, const char *objectname, const char *path) { char symname[MAXPDSTRING], filename[MAXPDSTRING], dirbuf[MAXPDSTRING], *nameptr; const char**dllextent; const char *classname, *cnameptr; void *dlobj; t_xxx makeout = NULL; int i, hexmunge = 0, fd; #ifdef _WIN32 HINSTANCE ntdll; #endif /* NULL-path is only used as a last resort, but we have already tried all paths */ if(!path)return (0); if ((classname = strrchr(objectname, '/'))) classname++; else classname = objectname; for (i = 0, cnameptr = classname; i < MAXPDSTRING-7 && *cnameptr; cnameptr++) { char c = *cnameptr; if ((c>='0' && c<='9') || (c>='A' && c<='Z')|| (c>='a' && c<='z' )|| c == '_') { symname[i] = c; i++; } /* trailing tilde becomes "_tilde" */ else if (c == '~' && cnameptr[1] == 0) { strcpy(symname+i, "_tilde"); i += strlen(symname+i); } else /* anything you can't put in a C symbol is sprintf'ed in hex */ { sprintf(symname+i, "0x%02x", c); i += strlen(symname+i); hexmunge = 1; } } symname[i] = 0; if (hexmunge) { memmove(symname+6, symname, strlen(symname)+1); strncpy(symname, "setup_", 6); } else strcat(symname, "_setup"); #if 0 fprintf(stderr, "lib: %s\n", classname); #endif /* try looking in the path for (objectname).(sys_dllextent) ... */ for(dllextent=sys_dllextent; *dllextent; dllextent++) { if ((fd = sys_trytoopenone(path, objectname, *dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; } /* next try (objectname)/(classname).(sys_dllextent) ... */ strncpy(filename, objectname, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, classname, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; for(dllextent=sys_dllextent; *dllextent; dllextent++) { if ((fd = sys_trytoopenone(path, filename, *dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; } #ifdef ANDROID /* Android libs have a 'lib' prefix, '.so' suffix and don't allow ~ */ char libname[MAXPDSTRING] = "lib"; strncat(libname, objectname, MAXPDSTRING - 4); int len = strlen(libname); if (libname[len-1] == '~' && len < MAXPDSTRING - 6) { strcpy(libname+len-1, "_tilde"); } if ((fd = sys_trytoopenone(path, libname, ".so", dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; #endif return (0); gotone: close(fd); class_set_extern_dir(gensym(dirbuf)); /* rebuild the absolute pathname */ strncpy(filename, dirbuf, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, nameptr, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; #ifdef _WIN32 { char dirname[MAXPDSTRING], *s, *basename; sys_bashfilename(filename, filename); /* set the dirname as DllDirectory, meaning in the path for loading other DLLs so that dependent libraries can be included in the same folder as the external. SetDllDirectory() needs a minimum supported version of Windows XP SP1 for SetDllDirectory, so WINVER must be 0x0502 */ strncpy(dirname, filename, MAXPDSTRING); s = strrchr(dirname, '\\'); basename = s; if (s && *s) *s = '\0'; if (!SetDllDirectory(dirname)) error("Could not set '%s' as DllDirectory(), '%s' might not load.", dirname, basename); /* now load the DLL for the external */ ntdll = LoadLibrary(filename); if (!ntdll) { error("%s: couldn't load", filename); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)GetProcAddress(ntdll, symname); if (!makeout) makeout = (t_xxx)GetProcAddress(ntdll, "setup"); SetDllDirectory(NULL); /* reset DLL dir to nothing */ } #elif defined(HAVE_LIBDL) || defined(__FreeBSD__) dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); if (!dlobj) { error("%s: %s", filename, dlerror()); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)dlsym(dlobj, symname); if(!makeout) makeout = (t_xxx)dlsym(dlobj, "setup"); #else #warning "No dynamic loading mechanism specified, \ libdl or WIN32 required for loading externals!" #endif if (!makeout) { error("load_object: Symbol \"%s\" not found", symname); class_set_extern_dir(&s_); return 0; } (*makeout)(); class_set_extern_dir(&s_); return (1); }
static int sys_do_load_lib(t_canvas *canvas, char *objectname) { char symname[MAXPDSTRING], filename[MAXPDSTRING], dirbuf[MAXPDSTRING], *classname, *nameptr, altsymname[MAXPDSTRING]; void *dlobj; t_xxx makeout = NULL; int i, hexmunge = 0, fd; #ifdef _WIN32 HINSTANCE ntdll; #endif if (classname = strrchr(objectname, '/')) classname++; else classname = objectname; if (sys_onloadlist(objectname)) { post("%s: already loaded", objectname); return (1); } for (i = 0, nameptr = classname; i < MAXPDSTRING-7 && *nameptr; nameptr++) { char c = *nameptr; if ((c>='0' && c<='9') || (c>='A' && c<='Z')|| (c>='a' && c<='z' )|| c == '_') { symname[i] = c; i++; } /* trailing tilde becomes "_tilde" */ else if (c == '~' && nameptr[1] == 0) { strcpy(symname+i, "_tilde"); i += strlen(symname+i); } else /* anything you can't put in a C symbol is sprintf'ed in hex */ { sprintf(symname+i, "0x%02x", c); i += strlen(symname+i); hexmunge = 1; } } symname[i] = 0; if (hexmunge) { memmove(symname+6, symname, strlen(symname)+1); strncpy(symname, "setup_", 6); } else strcat(symname, "_setup"); #if 0 fprintf(stderr, "lib: %s\n", classname); #endif /* try looking in the path for (objectname).(sys_dllextent) ... */ if ((fd = canvas_open(canvas, objectname, sys_dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; /* same, with the more generic sys_dllextent2 */ if ((fd = canvas_open(canvas, objectname, sys_dllextent2, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; /* next try (objectname)/(classname).(sys_dllextent) ... */ strncpy(filename, objectname, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, classname, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; if ((fd = canvas_open(canvas, filename, sys_dllextent, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; if ((fd = canvas_open(canvas, filename, sys_dllextent2, dirbuf, &nameptr, MAXPDSTRING, 1)) >= 0) goto gotone; return (0); gotone: close(fd); class_set_extern_dir(gensym(dirbuf)); /* rebuild the absolute pathname */ strncpy(filename, dirbuf, MAXPDSTRING); filename[MAXPDSTRING-2] = 0; strcat(filename, "/"); strncat(filename, nameptr, MAXPDSTRING-strlen(filename)); filename[MAXPDSTRING-1] = 0; #ifdef _WIN32 { char dirname[MAXPDSTRING], *s, *basename; sys_bashfilename(filename, filename); /* set the dirname as DllDirectory, meaning in the path for loading other DLLs so that dependent libraries can be included in the same folder as the external. SetDllDirectory() needs a minimum supported version of Windows XP SP1 for SetDllDirectory, so WINVER must be 0x0502 */ strncpy(dirname, filename, MAXPDSTRING); s = strrchr(dirname, '\\'); basename = s; if (s && *s) *s = '\0'; if (!SetDllDirectory(dirname)) error("Could not set '%s' as DllDirectory(), '%s' might not load.", dirname, basename); /* now load the DLL for the external */ ntdll = LoadLibrary(filename); if (!ntdll) { post("%s: couldn't load", filename); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)GetProcAddress(ntdll, symname); if (!makeout) makeout = (t_xxx)GetProcAddress(ntdll, "setup"); SetDllDirectory(NULL); /* reset DLL dir to nothing */ } #elif defined HAVE_LIBDL dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL); if (!dlobj) { post("%s: %s", filename, dlerror()); class_set_extern_dir(&s_); return (0); } makeout = (t_xxx)dlsym(dlobj, symname); if(!makeout) makeout = (t_xxx)dlsym(dlobj, "setup"); #else #warning "No dynamic loading mechanism specified, libdl or WIN32 required for loading externals!" #endif if (!makeout) { post("load_object: Symbol \"%s\" not found", symname); class_set_extern_dir(&s_); return 0; } (*makeout)(); class_set_extern_dir(&s_); sys_putonloadlist(objectname); return (1); }
bool open(std::string name, const t_canvas*canvas) { bool loud=false; const char*hookname="plugMain"; if(name.empty()) return false; if(m_plugin) close(); FF_Main_FuncPtr plugmain = NULL; char buf[MAXPDSTRING]; char buf2[MAXPDSTRING]; char *bufptr=NULL; const char *extension= #ifdef _WIN32 ".dll"; #elif defined __APPLE__ ""; #else ".so"; #endif #ifdef __APPLE__ char buf3[MAXPDSTRING]; #ifdef DL_OPEN snprintf(buf3, MAXPDSTRING, "%s.frf/Contents/MacOS/%s", name.c_str(), name.c_str()); #else // this can never work... snprintf(buf3, MAXPDSTRING, "%s.frf/%s", name.c_str(), name.c_str()); #endif buf3[MAXPDSTRING-1]=0; name=buf3; #endif int fd=-1; if ((fd=canvas_open(const_cast<t_canvas*>(canvas), name.c_str(), extension, buf2, &bufptr, MAXPDSTRING, 1))>=0){ ::close(fd); #if defined __APPLE__ && 0 snprintf(buf, MAXPDSTRING, "%s", buf2); #else snprintf(buf, MAXPDSTRING, "%s/%s", buf2, bufptr); #endif buf[MAXPDSTRING-1]=0; } else { if(canvas) { canvas_makefilename(const_cast<t_canvas*>(canvas), const_cast<char*>(name.c_str()), buf, MAXPDSTRING); } else { if(loud)::error("pix_freeframe[%s]: unfindeable", name.c_str()); return false; } } name=buf; std::string libname = name; if(loud)::post("trying to load %s", buf); #ifdef DL_OPEN if(loud)::post("dlopen %s", libname.c_str()); m_dlhandle=dlopen(libname.c_str(), RTLD_NOW); if(!m_dlhandle){ if(loud)::error("pix_freeframe[%s]: %s", libname.c_str(), dlerror()); return NULL; } dlerror(); plugmain = reinterpret_cast<FF_Main_FuncPtr>(dlsym(m_dlhandle, hookname)); #elif defined __APPLE__ CFURLRef bundleURL = NULL; CFBundleRef theBundle = NULL; CFStringRef plugin = CFStringCreateWithCString(NULL, libname.c_str(), kCFStringEncodingMacRoman); bundleURL = CFURLCreateWithFileSystemPath( kCFAllocatorSystemDefault, plugin, kCFURLPOSIXPathStyle, true ); theBundle = CFBundleCreate( kCFAllocatorSystemDefault, bundleURL ); // Get a pointer to the function. if (theBundle){ plugmain = reinterpret_cast<FF_Main_FuncPtr>(CFBundleGetFunctionPointerForName( theBundle, CFSTR("plugMain") ) ); }else{ if(loud)::post("%s: couldn't load", libname.c_str()); return 0; } if(bundleURL != NULL) CFRelease( bundleURL ); if(theBundle != NULL) CFRelease( theBundle ); if(plugin != NULL) CFRelease( plugin ); #elif defined _WIN32 char buffer[MAXPDSTRING]; sys_bashfilename(libname.c_str(), buffer); libname=buffer; m_w32handle = LoadLibrary(libname.c_str()); if (!m_w32handle) { if(loud)::post("%s: couldn't load", libname.c_str()); return false; } plugmain = reinterpret_cast<FF_Main_FuncPtr>(GetProcAddress(m_w32handle, hookname)); #else # error no way to load dynamic linked libraries on this OS #endif m_plugin=plugmain; return (NULL!=m_plugin); }
/* LATER deal with corrupt binary files? */ int binport_read(t_binbuf *bb, char *filename, char *dirname) { int result; FILE *fp; char namebuf[MAXPDSTRING]; namebuf[0] = 0; if (*dirname) strcat(namebuf, dirname), strcat(namebuf, "/"); strcat(namebuf, filename); sys_bashfilename(namebuf, namebuf); if (fp = fopen(namebuf, "rb")) { int ftype; t_binport *bp = binport_new(fp, &ftype); if (bp) { if (ftype == BINPORT_MAXBINARY) result = (binport_tobinbuf(bp, bb) ? BINPORT_MAXBINARY : BINPORT_CORRUPT); else if (ftype == BINPORT_MAXTEXT) { t_atom at; if (bp->b_lex = lex_new(fp, A_INT)) { while (binport_nextatom(bp, &at)) if (at.a_type == A_SEMI) break; binbuf_addv(bb, "ss;", gensym("max"), gensym("v2")); result = (binport_tobinbuf(bp, bb) ? BINPORT_MAXTEXT : BINPORT_CORRUPT); } else result = BINPORT_FAILED; } else if (ftype == BINPORT_MAXOLD) { t_binpold *old = binpold_new(fp); result = BINPORT_FAILED; if (old) { bp->b_old = old; if (binpold_load(old) && binport_tobinbuf(bp, bb)) result = BINPORT_MAXOLD; } else binpold_failure(filename); } else result = BINPORT_FAILED; binport_free(bp); } else if (ftype == BINPORT_PDFILE) result = (binbuf_read(bb, filename, dirname, 0) ? BINPORT_FAILED : BINPORT_PDFILE); else { binport_failure(filename); result = BINPORT_INVALID; } } else { binport_bug("cannot open file"); result = BINPORT_FAILED; } return (result); }