From: Jérémie Astor Date: Wed, 1 Apr 2020 14:48:29 +0000 (+0200) Subject: :art: Check if file is regular before compilation X-Git-Tag: nightly~1726^2~13 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=bfe11879e4858648bc9814b3b33918de16181746;p=gwion.git :art: Check if file is regular before compilation Was needed for window not to hang on ```sh ./gwion.exe some_directory ``` --- diff --git a/src/compile.c b/src/compile.c index 52b0b5c0..a4c85682 100644 --- a/src/compile.c +++ b/src/compile.c @@ -24,7 +24,7 @@ struct Compiler { enum compile_type type; }; -static void compiler_name(MemPool p, struct Compiler* c) { +ANN static void compiler_name(MemPool p, struct Compiler* c) { m_str d = strdup(c->base); c->name = strsep(&d, ":"); if(d) @@ -34,7 +34,7 @@ static void compiler_name(MemPool p, struct Compiler* c) { free(d); } -static inline void compiler_error(MemPool p, const struct Compiler* c) { +ANN static inline void compiler_error(MemPool p, const struct Compiler* c) { if(c->args) { for(m_uint i = 0; i < vector_size(c->args); ++i) { const m_str str = (m_str)vector_at(c->args, i); @@ -45,7 +45,7 @@ static inline void compiler_error(MemPool p, const struct Compiler* c) { } } -static void compiler_clean(MemPool p, const struct Compiler* c) { +ANN static void compiler_clean(MemPool p, const struct Compiler* c) { if(c->name) xfree(c->name); /* test c->type because COMPILE_FILE does not own file */ @@ -55,7 +55,7 @@ static void compiler_clean(MemPool p, const struct Compiler* c) { free_ast(p, c->ast); } -static m_bool _compiler_open(struct Compiler* c) { +ANN static m_bool _compiler_open(struct Compiler* c) { if(c->type == COMPILE_NAME) { m_str name = c->name; c->name = realpath(name, NULL); @@ -68,18 +68,37 @@ static m_bool _compiler_open(struct Compiler* c) { return GW_OK; } -static inline m_bool compiler_open(MemPool p, struct Compiler* c) { +#ifndef BUILD_ON_WINDOWS +#include +ANN static int is_reg(const m_str path) { + struct stat s; + stat(path, &s); + return S_ISREG(s.st_mode); +} +#else +ANN static bool is_reg(const m_str path) { + cionst DWORD dw = GetFileAttributes(path); + return !(dw == INVALID_FILE_ATTRIBUTES || + dw & FILE_ATTRIBUTE_DIRECTORY); +} +#endif + +ANN static inline m_bool compiler_open(MemPool p, struct Compiler* c) { char name[strlen(c->name) + 1]; strcpy(name, c->name); + if(!is_reg(name)) { + gw_err(_("'%s': is a not a regular file\n"), name); + return GW_ERROR; + } if(_compiler_open(c) < 0) { compiler_error(p, c); - gw_err(_("'%s': no such file\n"), name); + gw_err(_("can't open '%s'\n"), name); return GW_ERROR; } return GW_OK; } -static inline m_bool _check(struct Gwion_* gwion, struct Compiler* c) { +ANN static inline m_bool _check(struct Gwion_* gwion, struct Compiler* c) { struct AstGetter_ arg = { c->name, c->file, gwion->st, .ppa=gwion->ppa }; CHECK_OB((c->ast = parse(&arg))) gwion->env->name = c->name; @@ -90,7 +109,7 @@ static inline m_bool _check(struct Gwion_* gwion, struct Compiler* c) { return GW_OK; } -static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { +ANN static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { CHECK_BB(compiler_open(gwion->mp, c)) if(_check(gwion, c) < 0) { gw_err(_("while compiling file '%s'\n"), c->base); @@ -106,7 +125,7 @@ static m_uint _compile(struct Gwion_* gwion, struct Compiler* c) { return GW_OK; } -static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { +ANN static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { compiler_name(gwion->mp, c); MUTEX_LOCK(gwion->data->mutex); const m_uint ret = _compile(gwion, c); @@ -115,17 +134,17 @@ static m_uint compile(struct Gwion_* gwion, struct Compiler* c) { return ret; } -m_uint compile_filename(struct Gwion_* gwion, const m_str filename) { +ANN m_uint compile_filename(struct Gwion_* gwion, const m_str filename) { struct Compiler c = { .base=filename, .type=COMPILE_NAME }; return compile(gwion, &c); } -m_uint compile_string(struct Gwion_* vm, const m_str filename, const m_str data) { +ANN m_uint compile_string(struct Gwion_* vm, const m_str filename, const m_str data) { struct Compiler c = { .base=filename, .type=COMPILE_MSTR, .data=data }; return compile(vm, &c); } -m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file) { +ANN m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file) { struct Compiler c = { .base=filename, .type=COMPILE_FILE, .file=file }; return compile(vm, &c); } diff --git a/tests/sh/opt.sh b/tests/sh/opt.sh index f752f8a9..55fe68e8 100644 --- a/tests/sh/opt.sh +++ b/tests/sh/opt.sh @@ -44,6 +44,10 @@ run "$n" "wrong file" "non_existant_file:with_args" "file" n=$((n+1)) run "$n" "plugin directory" "-p non_existant_dir" "file" +# plug_dir +n=$((n+1)) +run "$n" "arg is a directory" "-p src" "file" + # config n=$((n+1)) RC=tmp_gwionrc