From 699eb5715ccb595a544df82d8bb8bf2a17c38d32 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 1 Oct 2013 16:00:24 +0800 Subject: [PATCH] Tools/udibuild - Cleanup (can find udibuild.ini if invoked relatively) --- Tools/udibuild/src/build.c | 17 +------------- Tools/udibuild/src/main.c | 45 ++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Tools/udibuild/src/build.c b/Tools/udibuild/src/build.c index ed5d5287..b070c373 100644 --- a/Tools/udibuild/src/build.c +++ b/Tools/udibuild/src/build.c @@ -7,8 +7,8 @@ */ #include #include -#include #include "include/build.h" +#include "include/common.h" #ifndef __GNUC__ # define __attribute__(...) @@ -16,7 +16,6 @@ // === PROTOTYPES === char *get_objfile(tIniFile *opts, const char *srcfile); -char *mkstr(const char *fmt, ...) __attribute__((format(printf,1,2))); // === CODE === int Build_CompileFile(tIniFile *opts, const char *abi, tUdiprops *udiprops, tUdiprops_Srcfile *srcfile) @@ -57,17 +56,3 @@ char *get_objfile(tIniFile *opts, const char *srcfile) { return mkstr("%s.o", srcfile); } - -char *mkstr(const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - size_t len = vsnprintf(NULL, 0, fmt, args); - va_end(args); - va_start(args, fmt); - char *ret = malloc(len+1); - vsnprintf(ret, len+1, fmt, args); - va_end(args); - return ret; -} - diff --git a/Tools/udibuild/src/main.c b/Tools/udibuild/src/main.c index 06ac58d4..51ecbb58 100644 --- a/Tools/udibuild/src/main.c +++ b/Tools/udibuild/src/main.c @@ -9,7 +9,9 @@ #include #include // getopt #include +#include // strrchr #include +#include "include/common.h" #include "include/build.h" #include "include/inifile.h" #include "include/udiprops.h" @@ -41,19 +43,35 @@ int main(int argc, char *argv[]) } // Locate udibuild.ini - if( NULL == gsOpt_ConfigFile ) - { - // 1. Check CWD + // 1. Check CWD + if( !gsOpt_ConfigFile ) { //if( file_exists("./udibuild.ini") ) //{ // gsOpt_ConfigFile = "udibuild.ini"; //} - // 2. Check program dir (if not invoked from PATH) - // 3. Check ~/.config/udi/udibuild.ini - // 4. Check CONFIGNAME - + } + // 2. Check program dir (if not invoked from PATH) + if( !gsOpt_ConfigFile && (argv[0][0] == '.' || argv[0][0] == '/') ) { + char *last_slash = strrchr(argv[0], '/'); + if( last_slash ) { + gsOpt_ConfigFile = mkstr("%.*s/udibuild.ini", + last_slash-argv[0], argv[0]); + } + //if( !file_exists(gsOpt_ConfigFile) ) { + // free(gsOpt_ConfigFile); + // gsOpt_ConfigFile = NULL; + //} + } + // 3. Check ~/.config/udi/udibuild.ini + // 4. Check CONFIGNAME + + // #. Oh well + if( !gsOpt_ConfigFile ) { + fprintf(stderr, "Can't locate udibuild.ini file, please specify using '-c'\n"); exit(2); } + + // Load udibuild.ini gpOptions = IniFile_Load(gsOpt_ConfigFile); assert(gpOptions); @@ -128,3 +146,16 @@ void Usage(const char *progname) "\n"); } +char *mkstr(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + size_t len = vsnprintf(NULL, 0, fmt, args); + va_end(args); + va_start(args, fmt); + char *ret = malloc(len+1); + vsnprintf(ret, len+1, fmt, args); + va_end(args); + return ret; +} + -- 2.20.1