From fab9febdc3a863c157a56cc4de2418cbb9665844 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 9 Dec 2012 15:05:21 +0100 Subject: path: add mp_find_config_file and reorganize some of the code Add `mp_find_config_file` to search different known paths and use that in ass_mp to look for the fontconfig configuration file. Some incidental changes spawned by this feature where: * Buffer allocation for the strings containing the paths is now performed with talloc. All of the allocations are done on a NULL context, but it still improves readability of the code. * Move the OSX function for lookup inside of a bundle: this code path was currently not used by the bundle generated with `make osxbundle`. The plan is to use it again in a future commit to get a fontconfig config file. --- core/input/input.c | 7 +-- core/mplayer.c | 15 ++++--- core/path.c | 130 ++++++++++++++++++++++------------------------------- core/path.h | 12 ++++- 4 files changed, 77 insertions(+), 87 deletions(-) (limited to 'core') diff --git a/core/input/input.c b/core/input/input.c index 6d60211e65..1ee1ff909a 100644 --- a/core/input/input.c +++ b/core/input/input.c @@ -1794,7 +1794,8 @@ struct input_ctx *mp_input_init(struct input_conf *input_conf) char *file; char *config_file = input_conf->config_file; - file = config_file[0] != '/' ? get_path(config_file) : config_file; + file = config_file[0] != '/' ? + mp_find_user_config_file(config_file) : config_file; if (!file) return ictx; @@ -1802,7 +1803,7 @@ struct input_ctx *mp_input_init(struct input_conf *input_conf) // free file if it was allocated by get_path(), // before it gets overwritten if (file != config_file) - free(file); + talloc_free(file); // Try global conf dir file = MPLAYER_CONFDIR "/input.conf"; if (!parse_config_file(ictx, file)) @@ -1811,7 +1812,7 @@ struct input_ctx *mp_input_init(struct input_conf *input_conf) } else { // free file if it was allocated by get_path() if (file != config_file) - free(file); + talloc_free(file); } #ifdef CONFIG_JOYSTICK diff --git a/core/mplayer.c b/core/mplayer.c index c68c4f3dc2..c57062517d 100644 --- a/core/mplayer.c +++ b/core/mplayer.c @@ -702,13 +702,14 @@ static bool parse_cfgfiles(struct MPContext *mpctx, m_config_t *conf) if (!(opts->noconfig & 2) && m_config_parse_config_file(conf, MPLAYER_CONFDIR "/mpv.conf") < 0) return false; - if ((conffile = get_path("")) == NULL) + if ((conffile = mp_find_user_config_file("")) == NULL) mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Cannot find HOME directory.\n"); else { mkdir(conffile, 0777); - free(conffile); - if ((conffile = get_path("config")) == NULL) - mp_tmsg(MSGT_CPLAYER, MSGL_ERR, "get_path(\"config\") problem\n"); + talloc_free(conffile); + if ((conffile = mp_find_user_config_file("config")) == NULL) + mp_tmsg(MSGT_CPLAYER, MSGL_ERR, + "mp_find_user_config_file(\"config\") problem\n"); else { if ((conffile_fd = open(conffile, O_CREAT | O_EXCL | O_WRONLY, 0666)) != -1) { @@ -720,7 +721,7 @@ static bool parse_cfgfiles(struct MPContext *mpctx, m_config_t *conf) if (!(opts->noconfig & 1) && m_config_parse_config_file(conf, conffile) < 0) return false; - free(conffile); + talloc_free(conffile); } } return true; @@ -826,10 +827,10 @@ static void load_per_file_config(m_config_t *conf, const char * const file) return; } - if ((confpath = get_path(name)) != NULL) { + if ((confpath = mp_find_user_config_file(name)) != NULL) { try_load_config(conf, confpath); - free(confpath); + talloc_free(confpath); } } diff --git a/core/path.c b/core/path.c index 3a2d4c09ba..ed53d5d465 100644 --- a/core/path.c +++ b/core/path.c @@ -23,6 +23,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include #include #include #include @@ -33,51 +34,63 @@ #include "config.h" #include "core/mp_msg.h" #include "core/path.h" +#include "talloc.h" +#include "osdep/io.h" -#ifdef CONFIG_MACOSX_BUNDLE -#include -#include -#elif defined(__MINGW32__) +#if defined(__MINGW32__) #include #elif defined(__CYGWIN__) #include #include #endif -#include "talloc.h" +#ifdef CONFIG_MACOSX_BUNDLE +#include "osdep/macosx_bundle.h" +#endif -#include "osdep/io.h" -char *get_path(const char *filename) +typedef char *(*lookup_fun)(const char *); +static const lookup_fun config_lookup_functions[] = { + mp_find_user_config_file, +#ifdef CONFIG_MACOSX_BUNDLE + get_bundled_path, +#endif + mp_find_global_config_file, + NULL +}; + +char *mp_find_config_file(const char *filename) { - char *homedir; - char *buff; + for (int i = 0; config_lookup_functions[i] != NULL; i++) { + char *path = config_lookup_functions[i](filename); + if (!path) continue; + + if (mp_path_exists(path)) + return path; + + talloc_free(path); + } + return NULL; +} + +char *mp_find_user_config_file(const char *filename) +{ + char *homedir = NULL, *buff = NULL; #ifdef __MINGW32__ - static char *config_dir = "/mpv"; + static char *config_dir = "mpv"; #else - static char *config_dir = "/.mpv"; + static char *config_dir = ".mpv"; #endif #if defined(__MINGW32__) || defined(__CYGWIN__) char exedir[260]; #endif - int len; -#ifdef CONFIG_MACOSX_BUNDLE - struct stat dummy; - CFIndex maxlen = 256; - CFURLRef res_url_ref = NULL; - CFURLRef bdl_url_ref = NULL; - char *res_url_path = NULL; - char *bdl_url_path = NULL; -#endif - - if ((homedir = getenv("MPV_HOME")) != NULL) + if ((homedir = getenv("MPV_HOME")) != NULL) { config_dir = ""; - else if ((homedir = getenv("HOME")) == NULL) + } else if ((homedir = getenv("HOME")) == NULL) { #if defined(__MINGW32__) || defined(__CYGWIN__) /* Hack to get fonts etc. loaded outside of Cygwin environment. */ - { int i, imax = 0; - len = (int)GetModuleFileNameA(NULL, exedir, 260); + int len = (int)GetModuleFileNameA(NULL, exedir, 260); for (i = 0; i < len; i++) if (exedir[i] == '\\') { exedir[i] = '/'; @@ -85,67 +98,32 @@ char *get_path(const char *filename) } exedir[imax] = '\0'; homedir = exedir; - } #else return NULL; #endif - len = strlen(homedir) + strlen(config_dir) + 1; - if (filename == NULL) { - if ((buff = malloc(len)) == NULL) - return NULL; - sprintf(buff, "%s%s", homedir, config_dir); - } else { - len += strlen(filename) + 1; - if ((buff = malloc(len)) == NULL) - return NULL; - sprintf(buff, "%s%s/%s", homedir, config_dir, filename); } -#ifdef CONFIG_MACOSX_BUNDLE - if (stat(buff, &dummy)) { - - res_url_ref = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); - bdl_url_ref = CFBundleCopyBundleURL(CFBundleGetMainBundle()); - - if (res_url_ref && bdl_url_ref) { - - res_url_path = malloc(maxlen); - bdl_url_path = malloc(maxlen); - - while (!CFURLGetFileSystemRepresentation(res_url_ref, true, - res_url_path, maxlen)) { - maxlen *= 2; - res_url_path = realloc(res_url_path, maxlen); - } - CFRelease(res_url_ref); - - while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, - bdl_url_path, maxlen)) { - maxlen *= 2; - bdl_url_path = realloc(bdl_url_path, maxlen); - } - CFRelease(bdl_url_ref); - - if (strcmp(res_url_path, bdl_url_path) == 0) - res_url_path = NULL; - } - - if (res_url_path && filename) { - if ((strlen(filename) + strlen(res_url_path) + 2) > maxlen) - maxlen = strlen(filename) + strlen(res_url_path) + 2; - free(buff); - buff = malloc(maxlen); - strcpy(buff, res_url_path); - - strcat(buff, "/"); - strcat(buff, filename); - } + if (filename) { + char * temp = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir)); + buff = mp_path_join(NULL, bstr0(temp), bstr0(filename)); + talloc_free(temp); + } else { + buff = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir)); } -#endif + mp_msg(MSGT_GLOBAL, MSGL_V, "get_path('%s') -> '%s'\n", filename, buff); return buff; } +char *mp_find_global_config_file(const char *filename) +{ + if (filename) { + return mp_path_join(NULL, bstr0(MPLAYER_CONFDIR), bstr0(filename)); + } else { + return talloc_strdup(NULL, MPLAYER_CONFDIR); + } +} + char *mp_basename(const char *path) { char *s; diff --git a/core/path.h b/core/path.h index 0c7dbcca41..3711be44b9 100644 --- a/core/path.h +++ b/core/path.h @@ -24,7 +24,17 @@ #include #include "core/bstr.h" -char *get_path(const char *filename); + +// Search for the input filename in several paths. These include user and global +// config locations by default. Some platforms may implement additional platform +// related lookups (i.e.: OSX inside an application bundle). +char *mp_find_config_file(const char *filename); + +// Search for the input filename in the global configuration location. +char *mp_find_global_config_file(const char *filename); + +// Search for the input filename in the user configuration location. +char *mp_find_user_config_file(const char *filename); // Return pointer to filename part of path -- cgit v1.2.3