From f46b90032f7bdfee52b699726c42e2687b699a94 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 11 Jan 2015 22:17:15 -0800 Subject: * glob.c: New file. (glob_wrap, glob_init): New functions. (errfunc_thunk): New static function. * glob.h: New file. * txr.c (main): call glob_init if HAVE_GLOB is defined. * configure (have_glob): New variable. (gen_config_make): Add have_glob to config/config.make. Detect glob function and set have_glob, and add HAVE_GLOB to config/config.h. * Makefile (OBJS): Include glob.h if have_glob is "y". * genvim.txr: Scan glob.c for functions and variables also. * txr.1: Documented glob and glob-related variables. --- glob.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 glob.c (limited to 'glob.c') diff --git a/glob.c b/glob.c new file mode 100644 index 00000000..c11b7f10 --- /dev/null +++ b/glob.c @@ -0,0 +1,104 @@ +/* Copyright 2015 + * Kaz Kylheku + * Vancouver, Canada + * All rights reserved. + * + * Redistribution of this software in source and binary forms, with or without + * modification, is permitted provided that the following two conditions are met. + * + * Use of this software in any manner constitutes agreement with the disclaimer + * which follows the two conditions. + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DAMAGES, HOWEVER CAUSED, + * AND UNDER ANY THEORY OF LIABILITY, ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "lib.h" +#include "hash.h" +#include "gc.h" +#include "signal.h" +#include "unwind.h" +#include "utf8.h" +#include "eval.h" +#include "glob.h" + +static val s_errfunc; + +static int errfunc_thunk(const char *errpath, int errcode) +{ + val result = funcall2(s_errfunc, string_utf8(errpath), num(errcode)); + return result ? 1 : 0; +} + +val glob_wrap(val pattern, val flags, val errfunc) +{ + char *pat_u8 = utf8_dup_to(c_str(pattern)); + glob_t gl; + + s_errfunc = default_bool_arg(errfunc); + + (void) glob(pat_u8, c_num(default_arg(flags, zero)), + s_errfunc ? errfunc_thunk : 0, &gl); + + { + int i; + list_collect_decl (out, ptail); + + for (i = 0; i < gl.gl_pathc; i++) + ptail = list_collect (ptail, string_utf8(gl.gl_pathv[i])); + + globfree(&gl); + return out; + } +} + +void glob_init(void) +{ + prot1(&s_errfunc); + reg_fun(intern(lit("glob"), user_package), func_n3o(glob_wrap, 1)); + reg_var(intern(lit("glob-err"), user_package), num_fast(GLOB_ERR)); + reg_var(intern(lit("glob-mark"), user_package), num_fast(GLOB_MARK)); + reg_var(intern(lit("glob-nosort"), user_package), num_fast(GLOB_NOSORT)); + reg_var(intern(lit("glob-nocheck"), user_package), num_fast(GLOB_NOCHECK)); + reg_var(intern(lit("glob-noescape"), user_package), num_fast(GLOB_NOESCAPE)); +#ifdef GLOB_PERIOD + reg_var(intern(lit("glob-period"), user_package), num_fast(GLOB_PERIOD)); +#endif +#ifdef GLOB_ALTDIRFUNC + reg_var(intern(lit("glob-altdirfunc"), user_package), num_fast(GLOB_ALTDIRFUNC)); +#endif +#ifdef GLOB_BRACE + reg_var(intern(lit("glob-brace"), user_package), num_fast(GLOB_BRACE)); +#endif +#ifdef GLOB_NOMAGIC + reg_var(intern(lit("glob-nomagic"), user_package), num_fast(GLOB_NOMAGIC)); +#endif +#ifdef GLOB_TILDE + reg_var(intern(lit("glob-tilde"), user_package), num_fast(GLOB_TILDE)); +#endif +#ifdef GLOB_TILDE_CHECK + reg_var(intern(lit("glob-tilde-check"), user_package), num_fast(GLOB_TILDE_CHECK)); +#endif +#ifdef GLOB_ONLYDIR + reg_var(intern(lit("glob-onlydir"), user_package), num_fast(GLOB_ONLYDIR)); +#endif +} -- cgit v1.2.3