diff --git a/pd/src/g_canvas.c b/pd/src/g_canvas.c index 7d77993b95958a9cdd9514e0ed2bb7128ddecfad..c12643d5ee9759f688041bca5ff0e5410d75f9f4 100644 --- a/pd/src/g_canvas.c +++ b/pd/src/g_canvas.c @@ -211,23 +211,28 @@ t_symbol *canvas_getdir(t_canvas *x) void canvas_makefilename(t_canvas *x, char *file, char *result, int resultsize) { + char interim[FILENAME_MAX]; + sys_expandpathelems(file, interim); char *dir = canvas_getenv(x)->ce_dir->s_name; - if (file[0] == '/' || (file[0] && file[1] == ':') || !*dir) + if (interim[0] == '/' || (interim[0] && interim[1] == ':') || !*dir) { - strncpy(result, file, resultsize); + fprintf(stderr,"root file\n"); + strncpy(result, interim, resultsize); result[resultsize-1] = 0; } else { + fprintf(stderr,"relative file\n"); int nleft; strncpy(result, dir, resultsize); result[resultsize-1] = 0; nleft = resultsize - strlen(result) - 1; if (nleft <= 0) return; strcat(result, "/"); - strncat(result, file, nleft); + strncat(result, interim, nleft); result[resultsize-1] = 0; - } + } + fprintf(stderr,"resulting file = <%s>\n", result); } void canvas_rename(t_canvas *x, t_symbol *s, t_symbol *dir) @@ -1693,72 +1698,6 @@ static void canvas_declare(t_canvas *x, t_symbol *s, int argc, t_atom *argv) } } -// utility function to replace @pd_extra and other sys-recognizable paths - -char * canvas_path_replace( - char const * const original, - char const * const pattern, - char const * const replacement -) { - size_t const replen = strlen(replacement); - size_t const patlen = strlen(pattern); - size_t const orilen = strlen(original); - - size_t patcnt = 0; - const char * oriptr; - const char * patloc; - - // find how many times the pattern occurs in the original string - for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen) - { - patcnt++; - } - - { - // allocate memory for the new string - size_t const retlen = orilen + patcnt * (replen - patlen); - char * returned = (char *) malloc( sizeof(char) * (retlen + 1) ); - - if (returned != NULL) - { - // copy the original string, - // replacing all the instances of the pattern - char * retptr = returned; - for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen) - { - size_t const skplen = patloc - oriptr; - // copy the section until the occurence of the pattern - strncpy(retptr, oriptr, skplen); - retptr += skplen; - // copy the replacement - strncpy(retptr, replacement, replen); - retptr += replen; - } - // copy the rest of the string. - strcpy(retptr, oriptr); - } - return returned; - } -} - - -char * canvas_parse_sys_filename_args(const char *name) -{ - //check for @sys_extra path and replace - char *final_name = NULL; - if (strstr(name, "@pd_extra") != NULL) { - t_namelist *path = pd_extrapath; - while (path->nl_next) - path = path->nl_next; - final_name = canvas_path_replace(name, "@pd_extra", path->nl_string); - } - else { - final_name = (char *) malloc( sizeof(char) * (strlen(name) + 1) ); - strcpy(final_name, name); - } - return(final_name); -} - /* utility function to read a file, looking first down the canvas's search path (set with "declare" objects in the patch and recursively in calling patches), then down the system one. The filename is the concatenation of @@ -1779,10 +1718,10 @@ int canvas_open(t_canvas *x, const char *name, const char *ext, int fd = -1; int result = 0; t_canvas *y; - char *final_name; + char final_name[FILENAME_MAX]; - //check for sys path and replace - final_name = canvas_parse_sys_filename_args(name); + /* first check for @pd_extra (and later possibly others) and ~/ and replace */ + sys_expandpathelems(name, final_name); /* first check if "name" is absolute (and if so, try to open) */ if (sys_open_absolute(final_name, ext, dirresult, nameresult, size, bin, &fd)) @@ -1820,7 +1759,6 @@ int canvas_open(t_canvas *x, const char *name, const char *ext, } } result = open_via_path((x ? canvas_getdir(x)->s_name : "."), final_name, ext, dirresult, nameresult, size, bin); - freebytes((void *)final_name, strlen(final_name)); return(result); } diff --git a/pd/src/g_canvas.h b/pd/src/g_canvas.h index b4e01a937a79b1471058febbb390a16eb050076e..47781baac80bc3ff0ae97c99b92cbdaa3c76ba96 100644 --- a/pd/src/g_canvas.h +++ b/pd/src/g_canvas.h @@ -550,7 +550,7 @@ EXTERN int canvas_setdeleting(t_canvas *x, int flag); /* ---- for parsing @pd_extra and other sys paths in filenames --------------------- */ -EXTERN char * canvas_parse_sys_filename_args(const char *name); +EXTERN void sys_expandpathelems(const char *name, const char *result); typedef void (*t_undofn)(t_canvas *canvas, void *buf, int action); /* a function that does UNDO/REDO */ diff --git a/pd/src/g_editor.c b/pd/src/g_editor.c index 2b5cf7bd6801b785b0aba15155d938522c49577b..b22e06a74dd7974b9deeeb603bedf8b3e22708a1 100644 --- a/pd/src/g_editor.c +++ b/pd/src/g_editor.c @@ -215,8 +215,8 @@ int gobj_shouldvis(t_gobj *x, struct _glist *glist) (glist->gl_goprect && (ob->te_type == T_TEXT))); } else { - return (1); //fprintf(stderr,"else return 1\n"); + return (1); } } diff --git a/pd/src/s_path.c b/pd/src/s_path.c index 131c4ab70500cf6d92655afab5d29bd3be85843f..bf7163c8243d49cc2fc970b8aebd0a35f97dda8d 100644 --- a/pd/src/s_path.c +++ b/pd/src/s_path.c @@ -70,6 +70,55 @@ void sys_unbashfilename(const char *from, char *to) *to = 0; } +/* expand special tags inside path that start with @ */ + +// utility function to expand paths (see sys_expandpathelems call below for more info) + +static void sys_path_replace( + char const * const original, + char * returned, + char const * const pattern, + char const * const replacement +) { + size_t const replen = strlen(replacement); + size_t const patlen = strlen(pattern); + size_t const orilen = strlen(original); + + size_t patcnt = 0; + const char * oriptr; + const char * patloc; + + // find how many times the pattern occurs in the original string + for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen) + { + patcnt++; + } + + { + // allocate memory for the new string + size_t const retlen = orilen + patcnt * (replen - patlen); + + if (returned != NULL) + { + // copy the original string, + // replacing all the instances of the pattern + char *retptr = returned; + for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen) + { + size_t const skplen = patloc - oriptr; + // copy the section until the occurence of the pattern + strncpy(retptr, oriptr, skplen); + retptr += skplen; + // copy the replacement + strncpy(retptr, replacement, replen); + retptr += replen; + } + // copy the rest of the string. + strcpy(retptr, oriptr); + } + } +} + /* expand env vars and ~ at the beginning of a path and make a copy to return */ static void sys_expandpath(const char *from, char *to) { @@ -95,6 +144,23 @@ static void sys_expandpath(const char *from, char *to) #endif } + +/* used for expanding paths for various objects */ +void sys_expandpathelems(const char *name, char *result) +{ + //check for expandable elements in path (e.g. @pd_extra, ~/) and replace + char interim[FILENAME_MAX]; + if (strstr(name, "@pd_extra") != NULL) { + t_namelist *path = pd_extrapath; + while (path->nl_next) + path = path->nl_next; + sys_path_replace(name, interim, "@pd_extra", path->nl_string); + } else { + strcpy(interim, name); + } + sys_expandpath(interim, result); +} + /* test if path is absolute or relative, based on leading /, env vars, ~, etc */ int sys_isabsolutepath(const char *dir) { @@ -344,17 +410,16 @@ there is no search and instead we just try to open the file literally. */ /* see also canvas_open() which, in addition, searches down the canvas-specific path. */ -EXTERN const char * canvas_parse_sys_filename_args(const char *name); - static int do_open_via_path(const char *dir, const char *name, const char *ext, char *dirresult, char **nameresult, unsigned int size, int bin, t_namelist *searchpath) { t_namelist *nl; int fd = -1; + char final_name[FILENAME_MAX]; - //check for sys path and replace - const char *final_name = canvas_parse_sys_filename_args(name); + /* first check for @ and ~ (and later others) and replace */ + sys_expandpathelems(name, final_name); /* first check if "name" is absolute (and if so, try to open) */ if (sys_open_absolute(final_name, ext, dirresult, nameresult, size, bin, &fd)) @@ -380,10 +445,8 @@ static int do_open_via_path(const char *dir, const char *name, *dirresult = 0; *nameresult = dirresult; - freebytes((void *)final_name, strlen(final_name)); return (-1); do_open_via_path_end: - freebytes((void *)final_name, strlen(final_name)); return (fd); }