How can I, in a c program, perform a glob by using the function provided by the z shell?
I have created a README of my explorations so far. It is for use in an open source library.
https://bitbucket.org/sentimental/zsh_source_experimentation/src/master/README
I copy it here:
Start
Lets get the sources
apt-get source zsh
apt-get source zsh-dev
I’ve discovered by using ldd that zsh does not produce any library files::
#ldd /bin/zsh4
linux-gate.so.1 => (0xb7775000)
libcap.so.2 => /lib/i386-linux-gnu/libcap.so.2 (0xb7751000)
libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb774c000)
libtinfo.so.5 => /lib/i386-linux-gnu/libtinfo.so.5 (0xb772c000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb7700000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb755b000)
I think I will have to use the source files directly.
Lets locate the files containing references to extended globbling
(I’m using zsh as my shell).::
grep -ir EXTENDEDGLOB . | egrep "\.(c|h):" | cut -d: -f1 | sort -u
./README
./zsh-4.3.17/Etc/ChangeLog-3.0
./zsh-4.3.17/Src/glob.c
./zsh-4.3.17/Src/Modules/zutil.c
./zsh-4.3.17/Src/options.c
./zsh-4.3.17/Src/pattern.c
./zsh-4.3.17/Src/utils.c
./zsh-4.3.17/Src/Zle/complist.c
./zsh-4.3.17/Src/Zle/zle_tricky.c
./zsh-4.3.17/Src/zsh.h
Lets consider a couple of those files
zsh.h
In here EXTENDEDGLOB is defined as part of an anonymous enum
There are publications
here
http://publications.gbdirect.co.uk/c_book/chapter6/enums.html
and here
http://bytes.com/topic/c/answers/63891-enum-within-function-standard
detailing the use of enum in c
An example of its use is probably the method arguments for the function
…………….
static int
bin_zregexparse(char *nam, char **args, Options ops, UNUSED(int func))
Found in the file
……………..
./zsh-4.3.17/Src/Modules/zutil.c
Lets see what’s calling that function. Hmm only one call. The only reference to that call is in that file.
grep -r bin_zregexparse . | egrep "\.(c|h):" | cut -d: -f1 | sort -u
./zsh-4.3.17/Src/Modules/zutil.c
Hmm…. How does it work if nothing calls this function?
Ok lets see if there is some conditional configuration that sets up or aliases this code somehow?
grep -i regex ./**/conf*
/zsh-4.3.17/config.h.in :/* Define to 1 if you have the `regexec' function. */
./zsh-4.3.17/config.h.in :#undef HAVE_REGEXEC
./zsh-4.3.17/config.h.in :/* Define to 1 if you have the `regexec' function. */
./zsh-4.3.17/config.h.in :#undef HAVE_REGEXEC
./zsh-4.3.17/configure : regcomp regexecc regerror regfree \
./zsh-4.3.17/configure : regcomp regexec regexecerror regfree \
./zsh-4.3.17/configure.ac : regcomp regexec regerrorror regfree \
Lets investigate these files.
config.h.in
Doesn’t seem to exist, perhaps it is generated?
There seems to be a block in
configure
8148 for ac_func in strftime strptime mktime timelocal \
8149 difftime gettimeofday clock_gettime \
8150 select poll \
8151 readlink faccessx fchdir ftruncate \
etc etc etc ..
8178 htons ntohs \
8179 regcomp regexec regerror regfree \
8180 gdbm_open getxattr \
8181 realpath canonicalize_file_name \
8182 symlink getcwd
8183 do :
8184 as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
8185 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
8186 if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
8187 cat >>confdefs.h <<_ACEOF
8188 #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
8189 _ACEOF
8190
8191 fi
8192 done
}}}
I’ve no idea what that is doing. TODO: Investigate!
In
./zsh-4.3.17/configure.ac
1167 dnl ---------------
1168 dnl CHECK FUNCTIONS
1169 dnl ---------------
1170 | rglobdata.gd_gf_noglobdot
1171 dnl need to integrate this function
1172 dnl AC_FUNC_STRFTIME
1173 | rglobdata.gd_gf_listtypes
1174 AC_CHECK_FUNCS(strftime strptime mktime timelocal \
1175 difftime gettimeofday clock_gettime \
1176 select poll \
etc etc etc
1205 regcomp regexec regerror regfree \
1206 gdbm_open getxattr \
1207 realpath canonicalize_file_name \
1208 symlink getcwd)
1209 AC_FUNC_STRCOLL
Not really sure at this stage how I can do this, consider that I perhaps want to unit-test that function, how might I do so?
The function
bin_zregexparseis the implementation of thezregexparsebuiltin provided by thezsh/zutilmodule. It is used below its definition inzutil.c:zregexparseis intended to be used in the implementation of_regex_arguments. This isn’t the most promising entry point.If you want to implement zsh’s globbing features, you’ll have to pull in almost all of the zsh code into your program, since glob patterns can contain arbitrary embedded code. You can exclude the line editor during build, but that’s about it.
I would recommend using a separate zsh binary and feeding it requests through a pair of pipes.