aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile.in67
-rw-r--r--auto.def325
-rw-r--r--autosetup/LICENSE35
-rw-r--r--autosetup/README.autosetup1
-rwxr-xr-xautosetup/autosetup1696
-rw-r--r--autosetup/cc-lib.tcl36
-rw-r--r--autosetup/cc-shared.tcl48
-rw-r--r--autosetup/cc.tcl572
-rwxr-xr-xautosetup/config.guess (renamed from config.guess)302
-rwxr-xr-xautosetup/config.sub (renamed from config.sub)211
-rwxr-xr-xautosetup/find-tclsh11
-rw-r--r--autosetup/jimsh0.c23539
-rw-r--r--autosetup/system.tcl205
-rw-r--r--autosetup/test-tclsh21
-rwxr-xr-xconfigure5793
-rwxr-xr-xinstall-sh250
-rw-r--r--jim-syslog.c5
-rw-r--r--jim-win32compat.h2
-rw-r--r--jim.c11
-rw-r--r--jim.h8
-rw-r--r--jimregexp.h4
-rw-r--r--linenoise.c2
-rw-r--r--linenoise.h3
24 files changed, 26874 insertions, 6274 deletions
diff --git a/.gitignore b/.gitignore
index d88466e..5a0af81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,6 +13,7 @@ jim-glob.c
jim-stdlib.c
jim-tclcompat.c
jim-tree.c
+jim-oo.c
jimsh
libjim.a
libjim.so
diff --git a/Makefile.in b/Makefile.in
index 1783fd3..cd56e99 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -1,7 +1,8 @@
# Tools
-CC = @CC@
+CC = @CCACHE@ @CC@
RANLIB = @RANLIB@
AR = @AR@
+STRIP = @STRIP@
# Configuration
@@ -16,14 +17,11 @@ LDLIBS += @LIBS@
exec_prefix ?= @exec_prefix@
prefix ?= @prefix@
-# Set an initial, default library and auto_path
-CPPFLAGS += -DTCL_LIBRARY=\"@TCL_LIBRARY@\"
-
-CPPFLAGS += -DTCL_PLATFORM_OS=\"@PLATFORM_OS@\" -DTCL_PLATFORM_PLATFORM=\"@PLATFORM_PLATFORM@\"
-CPPFLAGS += -DJIM_REFERENCES -D_GNU_SOURCE -DNO_COMPLETION
-
-CPPFLAGS += -Wall $(OPTIM) -I@srcdir@ -I. @EXTRA_CFLAGS@
+CPPFLAGS += -D_GNU_SOURCE -Wall -Werror $(OPTIM) -I.
+ifneq (@srcdir@,.)
+CPPFLAGS += -I@srcdir@
VPATH := @srcdir@
+endif
ifeq ($(jim_libtype),static)
LIBJIM := libjim.a
@@ -31,6 +29,7 @@ else
LIBJIM := libjim.so
SH_LIBJIM := $(LIBJIM)
CPPFLAGS += $(SH_CFLAGS)
+ DEF_LD_PATH := @LD_LIBRARY_PATH@=`pwd`
endif
.EXPORT_ALL_VARIABLES:
@@ -47,7 +46,6 @@ EXTENSION_TCL := $(patsubst %,%.tcl,@JIM_TCL_EXTENSIONS@)
# Create C extensions from pure Tcl extensions
jim-%.c: %.tcl
- echo $@ >>.clean
sh @srcdir@/make-c-ext.sh $< >$@
OBJS += jim-load-static-exts.o
@@ -57,23 +55,24 @@ all: $(JIMSH) $(EXTENSION_MODS)
docs: Tcl.html
$(JIMSH): $(LIBJIM) jimsh.o
- $(CC) $(CFLAGS) @LINKFLAGS@ $(LDFLAGS) -o $@ jimsh.o $(LIBJIM) $(LDLIBS) -lm
+ $(CC) $(CFLAGS) @SH_LINKFLAGS@ $(LDFLAGS) -o $@ jimsh.o $(LIBJIM) $(LDLIBS) -lm
install: all docs $(EXTENSION_TCL)
- install -d $(DESTDIR)$(prefix)/bin
- install $(JIMSH) $(DESTDIR)$(prefix)/bin
- install -d $(DESTDIR)$(prefix)/lib/jim
- install $(LIBJIM) $(DESTDIR)$(prefix)/lib
- install @srcdir@/README.extensions $(EXTENSION_MODS) $(EXTENSION_TCL) $(DESTDIR)$(prefix)/lib/jim
- install -d $(DESTDIR)$(prefix)/include
- install @srcdir@/jim.h @srcdir@/jim-eventloop.h @srcdir@/jim-nvp.h @srcdir@/jim-signal.h \
+
+ mkdir -p $(DESTDIR)$(prefix)/bin
+ cp $(JIMSH) $(DESTDIR)$(prefix)/bin
+ mkdir -p $(DESTDIR)$(prefix)/lib/jim
+ cp $(LIBJIM) $(DESTDIR)$(prefix)/lib
+ cp @srcdir@/README.extensions $(EXTENSION_MODS) $(EXTENSION_TCL) $(DESTDIR)$(prefix)/lib/jim
+ mkdir -p $(DESTDIR)$(prefix)/include
+ cp @srcdir@/jim.h @srcdir@/jim-eventloop.h @srcdir@/jim-nvp.h @srcdir@/jim-signal.h \
@srcdir@/jim-subcmd.h @srcdir@/jim-win32compat.h $(DESTDIR)$(prefix)/include
- install jim-config.h $(DESTDIR)$(prefix)/include
- install -d $(DESTDIR)$(prefix)/doc/jim
- install Tcl.html $(DESTDIR)$(prefix)/doc/jim
+ cp jim-config.h $(DESTDIR)$(prefix)/include
+ mkdir -p $(DESTDIR)$(prefix)/doc/jim
+ cp Tcl.html $(DESTDIR)$(prefix)/doc/jim
-test:
- $(MAKE) jimsh=$(shell pwd)/jimsh -C tests
+test: $(JIMSH)
+ $(DEF_LD_PATH) $(MAKE) jimsh=`pwd`/jimsh -C @srcdir@/tests
$(OBJS) $(EXTENSION_OBJS): Makefile
@@ -82,7 +81,7 @@ ifneq (@JIM_UTF8@,)
utf8.o: unicode_mapping.c
unicode_mapping.c: @srcdir@/UnicodeData.txt @srcdir@/parse-unidata.tcl
- tclsh @srcdir@/parse-unidata.tcl @srcdir@/UnicodeData.txt >$@
+ @tclsh@ @srcdir@/parse-unidata.tcl @srcdir@/UnicodeData.txt >$@
endif
ifeq ($(jim_libtype),static)
@@ -99,23 +98,31 @@ endif
$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ $*.o $(SH_LIBJIM) $(LDLIBS)
Tcl.html: jim_tcl.txt
- tclsh @srcdir@/make-index $^ | asciidoc -o $@ -d manpage - || cp @srcdir@/Tcl_shipped.html Tcl.html
+ @tclsh@ @srcdir@/make-index $^ | asciidoc -o $@ -d manpage - || cp @srcdir@/Tcl_shipped.html Tcl.html
clean:
- rm -f *.o *.so lib*.a $(JIMSH) Tcl.html unicode_mapping.c
- if [ -f .clean ]; then rm -f `cat .clean` .clean; fi
+ rm -f *.o *.so lib*.a $(JIMSH) Tcl.html unicode_mapping.c @GENERATED_SRCS@
distclean: clean
- rm -f jimautoconf.h jim-config.h Makefile config.status config.log
- rm -rf autom4te.cache
+ rm -f jimautoconf.h jim-config.h Makefile config.log
ship: Tcl.html
cp Tcl.html Tcl_shipped.html
- autoconf && autoheader
# automake compatibility. do nothing for all these targets
EMPTY_AUTOMAKE_TARGETS := dvi pdf ps info html tags ctags mostlyclean maintainer-clean check installcheck installdirs \
- install-pdf install-ps install-info install-html -install-dvi uninstall install-exec install-data distdir
+ install-pdf install-ps install-info install-html -install-dvi uninstall install-exec install-data
.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
$(EMPTY_AUTOMAKE_TARGETS):
+# automake compatibilty - install sources from $(srcdir) to $(distdir)
+distdir:
+ ( test -z "$(srcdir)" || cd "$(srcdir)"; git ls-files ) | cpio -pdmu $(distdir)
+
+ifeq ($(findstring clean,$(MAKECMDGOALS)),)
+Makefile: @AUTODEPS@ @srcdir@/Makefile.in
+ @CC='@CC@' @AUTOREMAKE@
+endif
+
+reconfig:
+ CC='@CC@' @AUTOREMAKE@
diff --git a/auto.def b/auto.def
new file mode 100644
index 0000000..6904976
--- /dev/null
+++ b/auto.def
@@ -0,0 +1,325 @@
+# vim:se syn=tcl:
+#
+
+# Note: modules which support options *must* be included before 'options'
+use cc cc-shared
+
+options {
+ utf8 => "include support for utf8-encoded strings"
+ lineedit=1 => "disable line editing"
+ references=1 => "disable support for references"
+ math => "include support for math functions"
+ ipv6 => "include ipv6 support in the aio extension"
+ with-jim-shared shared => "build a shared library instead of a static library"
+ jim-regexp => "use the built-in (Tcl-compatible) regexp, even if POSIX regex is available"
+ with-jim-ext: {with-ext:"ext1 ext2 ..."} => {
+ Specify additional jim extensions to include.
+ These are enabled by default:
+
+ aio - ANSI I/O, including open and socket
+ eventloop - after, vwait, update
+ array - Tcl-compatible array command
+ clock - Tcl-compatible clock command
+ exec - Tcl-compatible exec command
+ file - Tcl-compatible file command
+ glob - Tcl-compatible glob command
+ readdir - Required for glob
+ package - Package management with the package command
+ load - Load binary extensions at runtime with load or package
+ posix - Posix APIs including os.fork, os.wait, pid
+ regexp - Tcl-compatible regexp, regsub commands
+ signal - Signal handling
+ stdlib - Built-in commands including lassign, lambda, alias
+ syslog - System logging with syslog
+ tclcompat - Tcl compatible read, gets, puts, parray, case, ...
+
+ These are disabled by default:
+
+ nvp - Name-value pairs C-only API
+ oo - Jim OO extension
+ tree - OO tree structure, similar to tcllib ::struct::tree
+ readline - Interface to libreadline
+ rlprompt - Tcl wrapper around the readline extension
+ sqlite - Interface to sqlite
+ sqlite3 - Interface to sqlite3
+ win32 - Interface to win32
+ }
+ with-out-jim-ext: {without-ext:"default|ext1 ext2 ..."} => {
+ Specify jim extensions to exclude.
+ If 'default' is given, the default extensions will not be added.
+ }
+ with-jim-extmod: {with-mod:"ext1 ext2 ..."} => {
+ Specify jim extensions to build as separate modules (either C or Tcl).
+ Note that not all extensions can be built as loadable modules.
+ }
+}
+
+cc-check-types "long long"
+
+cc-check-includes sys/un.h dlfcn.h unistd.h
+
+cc-check-functions ualarm sysinfo lstat fork vfork
+cc-check-functions backtrace geteuid mkstemp realpath strptime
+cc-check-functions regcomp waitpid sigaction sys_signame sys_siglist
+cc-check-functions syslog opendir readlink sleep usleep pipe inet_ntop getaddrinfo
+
+switch -glob -- [get-define host] {
+ *-*-ming* {
+ # We provide our own implementation of dlopen for mingw32
+ define-feature dlopen-compat
+ }
+}
+
+# Find some tools
+cc-check-tools ar ranlib strip
+define tclsh [info nameofexecutable]
+
+msg-checking "Checking environ declared in unistd.h..."
+if {[cctest -cflags -D_GNU_SOURCE -includes unistd.h -code {char **ep = environ;}]} {
+ define NO_ENVIRON_EXTERN
+ msg-result "yes"
+} else {
+ msg-result "no"
+}
+
+set extra_objs {}
+set jimregexp 0
+
+if {[opt-bool utf8]} {
+ msg-result "Enabling UTF-8"
+ define JIM_UTF8
+ incr jimregexp
+}
+if {[opt-bool math]} {
+ msg-result "Enabling math functions"
+ define JIM_MATH_FUNCTIONS
+}
+if {[opt-bool ipv6]} {
+ msg-result "Enabling IPv6"
+ define JIM_IPV6
+}
+if {[opt-bool lineedit] && [cc-check-includes termios.h]} {
+ msg-result "Enabling line editing"
+ define USE_LINENOISE
+ lappend extra_objs linenoise.o
+}
+if {[opt-bool references]} {
+ msg-result "Enabling references"
+ define JIM_REFERENCES
+}
+if {[opt-bool shared with-jim-shared]} {
+ msg-result "Building shared library"
+ define JIM_LIBTYPE shared
+} else {
+ msg-result "Building static library"
+ define JIM_LIBTYPE static
+}
+
+# Note: Extension handling is mapped directly from the configure.ac
+# implementation
+
+set without [join [opt-val {without-ext with-out-jim-ext}]]
+set withext [join [opt-val {with-ext with-jim-ext}]]
+set withmod [join [opt-val {with-mod with-jim-extmod}]]
+
+# Tcl extensions
+set ext_tcl "stdlib glob tclcompat tree rlprompt oo"
+# C extensions
+set ext_c "load package readdir array clock exec file posix regexp signal aio eventloop syslog nvp readline sqlite sqlite3 win32"
+
+# Tcl extensions which can be modules
+set ext_tcl_mod "glob tree rlprompt oo"
+# C extensions which can be modules
+set ext_c_mod "readdir array clock file posix regexp syslog readline sqlite sqlite3 win32"
+
+# All extensions
+set ext_all [concat $ext_c $ext_tcl]
+
+# Default static extensions
+set ext_default "stdlib load package readdir glob array clock exec file posix regexp signal tclcompat aio eventloop syslog"
+
+if {$without eq "default"} {
+ set ext_default stdlib
+ set without {}
+}
+
+# Check valid extension names
+foreach i [concat $withext $without $withmod] {
+ if {$i ni $ext_all} {
+ user-error "Unknown extension: $i"
+ }
+}
+
+# needs_xxx="expression" means that the expr must eval to 1 to select the extension
+# dep_xxx="yyy zzz" means that if xxx is selected, so is yyy and zzz
+set dep(glob) readdir
+set dep(rlprompt) readline
+set dep(tree) oo
+
+set needs(aio) {expr {[cc-check-function-in-lib socket socket] || 1}}
+set needs(exec) {expr {[have-feature vfork] && [have-feature waitpid]}}
+set needs(load) {expr {[have-feature dlopen-compat] || [cc-check-function-in-lib dlopen dl]}}
+set needs(posix) {have-feature waitpid}
+set needs(readdir) {have-feature opendir}
+set needs(readline) {cc-check-function-in-lib readline readline}
+set needs(signal) {expr {[have-feature sigaction] && [have-feature vfork]}}
+set needs(sqlite) {cc-check-function-in-lib sqlite_open sqlite}
+set needs(sqlite3) {cc-check-function-in-lib sqlite3_open sqlite3}
+set needs(syslog) {have-feature syslog}
+set needs(win32) {have-feature windows}
+
+# First handle dependencies. If an extension is enabled, also enable its dependency
+foreach i [concat $ext_default $withext] {
+ if {$i in $without} {
+ continue
+ }
+ if {[info exists dep($i)]} {
+ lappend withext {*}$dep($i)
+ }
+}
+
+foreach i $withmod {
+ if {[info exists dep($i)]} {
+ # Theoretically, a mod could depend upon something which must be static
+ # If already configured static, don't make it a module
+ foreach d $dep($i) {
+ if {$d ni $withext} {
+ lappend withmod $d
+ }
+ }
+ }
+}
+
+# Now that we know what the platform supports:
+
+# For all known extensions:
+# - If it is disabled, remove it
+# - Otherwise, check to see if it's pre-requisites are met
+# - If yes, add it if it is enabled or is a default
+# - If no, error if it is enabled, or do nothing otherwise
+# - Modules may be either C or Tcl
+
+set extmodtcl {}
+set extmod {}
+set ext {}
+
+foreach i [lsort $ext_all] {
+ # First discard the extension if disabled or not enabled
+ if {$i in $without} {
+ msg-result "Extension $i...disabled"
+ continue
+ }
+ if {$i ni [concat $withext $withmod $ext_default]} {
+ msg-result "Extension $i...not enabled"
+ continue
+ }
+
+ # Check dependencies
+ set met 1
+ if {[info exists needs($i)]} {
+ set met [eval $needs($i)]
+ }
+
+ msg-checking "Extension $i..."
+
+ # Selected as a module?
+ if {$i in $withmod} {
+ if {$i in $ext_tcl_mod} {
+ # Easy, a Tcl module
+ msg-result "tcl"
+ lappend extmodtcl $i
+ continue
+ }
+ if {$i ni $ext_c_mod} {
+ user-error "not a module"
+ }
+ if {!$met} {
+ user-error "dependencies not met"
+ }
+ msg-result "module"
+ lappend extmod $i
+ continue
+ }
+
+ # Selected as a static extension?
+ if {$i in $withext} {
+ if {!$met} {
+ user-error "dependencies not met"
+ }
+ msg-result "enabled"
+ lappend ext $i
+ continue
+ }
+
+ # Enabled by default?
+ if {$i in $ext_default} {
+ if {!$met} {
+ msg-result "disabled (dependencies)"
+ continue
+ }
+ msg-result "enabled (default)"
+ lappend ext $i
+ continue
+ }
+}
+
+if {[have-feature windows]} {
+ if {"aio" in "$ext $extmod"} {
+ define-append LIBS -lwsock32
+ }
+ lappend extra_objs jim-win32compat.o
+
+ if {$extmod ne "" && [get-define JIM_LIBTYPE] eq "static"} {
+ user-error "cygwin/mingw require --shared for dynamic modules"
+ }
+}
+
+if {"regexp" in "$ext $extmod"} {
+ # No regcomp means we need to use the built-in version
+ if {![have-feature regcomp]} {
+ incr jimregexp
+ }
+}
+
+if {$jimregexp || [opt-bool jim-regexp]} {
+ msg-result "Using built-in regexp"
+ define JIM_REGEXP
+
+ # If the built-in regexp overrides the system regcomp, etc.
+ # jim must be built shared so that the correct symbols are found
+ if {"regexp" in $extmod && [get-define JIM_LIBTYPE] eq "static" && [have-feature regcomp]} {
+ user-error "Must use --shared with regexp module and built-in regexp"
+ }
+}
+if {"load" ni $ext} {
+ # If we don't have load, no need to support shared objects
+ define SH_LINKFLAGS ""
+}
+
+msg-result "Jim static extensions: [lsort $ext]"
+if {$extmodtcl ne ""} {
+ msg-result "Jim Tcl extensions: [lsort $extmodtcl]"
+}
+if {$extmod ne ""} {
+ msg-result "Jim dynamic extensions: [lsort $extmod]"
+}
+
+define JIM_EXTENSIONS $ext
+define JIM_TCL_EXTENSIONS $extmodtcl
+define JIM_MOD_EXTENSIONS $extmod
+define EXTRA_OBJS $extra_objs
+
+foreach e $ext {
+ define jim_ext_$e
+ if {$e in $ext_tcl} {
+ define-append GENERATED_SRCS jim-$e.c
+ }
+}
+
+define TCL_LIBRARY [get-define prefix]/lib/jim
+define TCL_PLATFORM_OS [exec uname -s]
+define TCL_PLATFORM_PLATFORM unix
+
+make-autoconf-h jim-config.h {HAVE_LONG_LONG* JIM_UTF8}
+make-autoconf-h jimautoconf.h {HAVE_* jim_ext_* TCL_PLATFORM_* TCL_LIBRARY USE_* JIM_*}
+make-template Makefile.in
diff --git a/autosetup/LICENSE b/autosetup/LICENSE
new file mode 100644
index 0000000..4fe636c
--- /dev/null
+++ b/autosetup/LICENSE
@@ -0,0 +1,35 @@
+Unless explicitly stated, all files which form part of autosetup
+are released under the following license:
+
+---------------------------------------------------------------------
+autosetup - A build environment "autoconfigurator"
+
+Copyright (c) 2010-2011, WorkWare Systems <http://workware.net.au/>
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+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 BY THE WORKWARE SYSTEMS ``AS IS'' AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WORKWARE
+SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+The views and conclusions contained in the software and documentation
+are those of the authors and should not be interpreted as representing
+official policies, either expressed or implied, of WorkWare Systems.
diff --git a/autosetup/README.autosetup b/autosetup/README.autosetup
new file mode 100644
index 0000000..2237234
--- /dev/null
+++ b/autosetup/README.autosetup
@@ -0,0 +1 @@
+This is autosetup v0.6.1. See http://msteveb.github.com/autosetup/
diff --git a/autosetup/autosetup b/autosetup/autosetup
new file mode 100755
index 0000000..76ca036
--- /dev/null
+++ b/autosetup/autosetup
@@ -0,0 +1,1696 @@
+#!/bin/sh
+# Copyright (c) 2006-2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+# \
+exec $($(dirname "$0")/find-tclsh || echo false) "$0" "$@"
+
+set autosetup(version) 0.6.1
+
+# Can be set to 1 to debug early-init problems
+set autosetup(debug) 0
+
+##################################################################
+#
+# Main flow of control, option handling
+#
+proc main {argv} {
+ global autosetup define
+
+ # There are 3 potential directories involved:
+ # 1. The directory containing autosetup (this script)
+ # 2. The directory containing auto.def
+ # 3. The current directory
+
+ # From this we need to determine:
+ # a. The path to this script (and related support files)
+ # b. The path to auto.def
+ # c. The build directory, where output files are created
+
+ # This is also complicated by the fact that autosetup may
+ # have been run via the configure wrapper ([env WRAPPER] is set)
+
+ # Here are the rules.
+ # a. This script is $::argv0
+ # => dir, prog, exe, libdir
+ # b. auto.def is in the directory containing the configure wrapper,
+ # otherwise it is in the current directory.
+ # => srcdir, autodef
+ # c. The build directory is the current directory
+ # => builddir, [pwd]
+
+ # 'misc' is needed before we can do anything, so set a temporary libdir
+ # in case this is the development version
+ set autosetup(libdir) [file dirname $::argv0]/lib
+ use misc
+
+ # (a)
+ set autosetup(dir) [realdir [file dirname [realpath $::argv0]]]
+ set autosetup(prog) [file join $autosetup(dir) [file tail $::argv0]]
+ set autosetup(exe) [env WRAPPER $autosetup(prog)]
+ if {$autosetup(installed)} {
+ set autosetup(libdir) $autosetup(dir)
+ } else {
+ set autosetup(libdir) [file join $autosetup(dir) lib]
+ }
+ autosetup_add_dep $autosetup(prog)
+
+ # (b)
+ if {[env WRAPPER ""] eq ""} {
+ # Invoked directly
+ set autosetup(srcdir) [pwd]
+ } else {
+ # Invoked via the configure wrapper
+ set autosetup(srcdir) [file dirname $autosetup(exe)]
+ }
+ set autosetup(autodef) [relative-path $autosetup(srcdir)/auto.def]
+
+ # (c)
+ set autosetup(builddir) [pwd]
+
+ set autosetup(argv) $argv
+ set autosetup(cmdline) {}
+ set autosetup(options) {}
+ set autosetup(optionhelp) {}
+ set autosetup(showhelp) 0
+
+ # Parse options
+ use getopt
+
+ array set ::useropts [getopt argv]
+
+ #"=Core Options:"
+ options-add {
+ help:=local => "display help and options. Optionally specify a module name, such as --help=system"
+ version => "display the version of autosetup"
+ ref:=text manual:=text
+ reference:=text => "display the autosetup command reference. 'text', 'wiki', 'asciidoc' or 'markdown'"
+ debug => "display debugging output as autosetup runs"
+ install => "install autosetup to the current directory (in the 'autosetup/' subdirectory)"
+ force init => "create an initial 'configure' script if none exists"
+ # Undocumented options
+ option-checking=1
+ nopager
+ quiet
+ timing
+ }
+
+ #parray ::useropts
+ if {[opt-bool version]} {
+ puts $autosetup(version)
+ exit 0
+ }
+
+ # Debugging output (set this early)
+ incr autosetup(debug) [opt-bool debug]
+ incr autosetup(force) [opt-bool force]
+ incr autosetup(msg-quiet) [opt-bool quiet]
+ incr autosetup(msg-timing) [opt-bool timing]
+
+ if {[opt-val help] ne ""} {
+ incr autosetup(showhelp)
+ use help
+ autosetup_help [opt-val help]
+ }
+
+ if {[opt-val {manual ref reference}] ne ""} {
+ use help
+ autosetup_reference [opt-val {manual ref reference}]
+ }
+
+ if {[opt-bool init]} {
+ use init
+ autosetup_init
+ }
+
+ if {[opt-bool install]} {
+ use install
+ autosetup_install
+ }
+
+ if {![file exists $autosetup(autodef)]} {
+ # Check for invalid option first
+ options {}
+ user-error "No auto.def found in $autosetup(srcdir)"
+ }
+
+ # Parse extra arguments into autosetup(cmdline)
+ foreach arg $argv {
+ if {[regexp {([^=]*)=(.*)} $arg -> n v]} {
+ dict set autosetup(cmdline) $n $v
+ define $n $v
+ } else {
+ user-error "Unexpected parameter: $arg"
+ }
+ }
+
+ autosetup_add_dep $autosetup(autodef)
+
+ set cmd [file-normalize $autosetup(exe)]
+ foreach arg $autosetup(argv) {
+ append cmd " [quote-if-needed $arg]"
+ }
+ define AUTOREMAKE $cmd
+
+ # Log how we were invoked
+ configlog "Invoked as: [env WRAPPER $::argv0] [quote-argv $autosetup(argv)]"
+
+ source $autosetup(autodef)
+
+ # Could warn here if options {} was not specified
+
+ show-notices
+
+ if {$autosetup(debug)} {
+ parray define
+ }
+
+ exit 0
+}
+
+# @opt-bool option ...
+#
+# Check each of the named, boolean options and return 1 if any of them have
+# been set by the user.
+#
+proc opt-bool {args} {
+ option-check-names {*}$args
+ opt_bool ::useropts {*}$args
+}
+
+# @opt-val option-list ?default=""?
+#
+# Returns a list containing all the values given for the non-boolean options in 'option-list'.
+# There will be one entry in the list for each option given by the user, including if the
+# same option was used multiple times.
+# If only a single value is required, use something like:
+#
+## lindex [opt-val $names] end
+#
+# If no options were set, $default is returned (exactly, not as a list).
+#
+proc opt-val {names {default ""}} {
+ option-check-names {*}$names
+ join [opt_val ::useropts $names $default]
+}
+
+proc option-check-names {args} {
+ foreach o $args {
+ if {$o ni $::autosetup(options)} {
+ autosetup-error "Request for undeclared option --$o"
+ }
+ }
+}
+
+# Parse the option definition in $opts and update
+# ::useropts() and ::autosetup(optionhelp) appropriately
+#
+proc options-add {opts {header ""}} {
+ global useropts autosetup
+
+ # First weed out comment lines
+ set realopts {}
+ foreach line [split $opts \n] {
+ if {![string match "#*" [string trimleft $line]]} {
+ append realopts $line \n
+ }
+ }
+ set opts $realopts
+
+ for {set i 0} {$i < [llength $opts]} {incr i} {
+ set opt [lindex $opts $i]
+ if {[string match =* $opt]} {
+ # This is a special heading
+ lappend autosetup(optionhelp) $opt ""
+ set header {}
+ continue
+ }
+
+ #puts "i=$i, opt=$opt"
+ regexp {^([^:=]*)(:)?(=)?(.*)$} $opt -> name colon equal value
+ if {$name in $autosetup(options)} {
+ autosetup-error "Option $name already specified"
+ }
+
+ #puts "$opt => $name $colon $equal $value"
+
+ # Find the corresponding value in the user options
+ # and set the default if necessary
+ if {[string match "-*" $opt]} {
+ # This is a documentation-only option, like "-C <dir>"
+ set opthelp $opt
+ } elseif {$colon eq ""} {
+ # Boolean option
+ lappend autosetup(options) $name
+
+ if {![info exists useropts($name)]} {
+ set useropts($name) $value
+ }
+ if {$value eq "1"} {
+ set opthelp "--disable-$name"
+ } else {
+ set opthelp "--$name"
+ }
+ } else {
+ # String option.
+ lappend autosetup(options) $name
+
+ if {$equal eq "="} {
+ if {[info exists useropts($name)]} {
+ # If the user specified the option with no value, the value will be "1"
+ # Replace with the default
+ if {$useropts($name) eq "1"} {
+ set useropts($name) $value
+ }
+ }
+ set opthelp "--$name?=$value?"
+ } else {
+ set opthelp "--$name=$value"
+ }
+ }
+
+ # Now create the help for this option if appropriate
+ if {[lindex $opts $i+1] eq "=>"} {
+ set desc [lindex $opts $i+2]
+ #string match \n* $desc
+ if {$header ne ""} {
+ lappend autosetup(optionhelp) $header ""
+ set header ""
+ }
+ # A multi-line description
+ lappend autosetup(optionhelp) $opthelp $desc
+ incr i 2
+ }
+ }
+}
+
+proc module-options {opts} {
+ set header ""
+ if {$::autosetup(showhelp) > 1 && [llength $opts]} {
+ set header "Module Options:"
+ }
+ options-add $opts $header
+
+ if {$::autosetup(showhelp)} {
+ # Ensure that the module isn't executed on --help
+ # We are running under eval or source, so use break
+ # to prevent further execution
+ #return -code break -level 2
+ return -code break
+ }
+}
+
+proc max {a b} {
+ expr {$a > $b ? $a : $b}
+}
+
+proc options-wrap-desc {text length firstprefix nextprefix initial} {
+ set len $initial
+ set space $firstprefix
+ foreach word [split $text] {
+ set word [string trim $word]
+ if {$word == ""} {
+ continue
+ }
+ if {$len && [string length $space$word] + $len >= $length} {
+ puts ""
+ set len 0
+ set space $nextprefix
+ }
+ incr len [string length $space$word]
+ puts -nonewline $space$word
+ set space " "
+ }
+ if {$len} {
+ puts ""
+ }
+}
+
+proc options-show {} {
+ # Determine the max option width
+ set max 0
+ foreach {opt desc} $::autosetup(optionhelp) {
+ if {[string match =* $opt] || [string match \n* $desc]} {
+ continue
+ }
+ set max [max $max [string length $opt]]
+ }
+ set indent [string repeat " " [expr $max+4]]
+
+ # Now output
+ foreach {opt desc} $::autosetup(optionhelp) {
+ if {[string match =* $opt]} {
+ puts [string range $opt 1 end]
+ continue
+ }
+ puts -nonewline " [format %-${max}s $opt]"
+ if {[string match \n* $desc]} {
+ puts $desc
+ } else {
+ options-wrap-desc [string trim $desc] 90 " " $indent [expr $max + 2]
+ }
+ }
+}
+
+proc options {optlist} {
+ # Allow options as a list or args
+ options-add $optlist "Local Options:"
+
+ if {$::autosetup(showhelp)} {
+ options-show
+ exit 0
+ }
+
+ # Check for invalid options
+ if {[opt-bool option-checking]} {
+ foreach o [array names ::useropts] {
+ if {$o ni $::autosetup(options)} {
+ user-error "Unknown option --$o"
+ }
+ }
+ }
+}
+
+proc config_guess {} {
+ if {[file executable $::autosetup(dir)/config.guess]} {
+ exec $::autosetup(dir)/config.guess
+ } else {
+ configlog "No config.guess, so using uname"
+ string tolower [exec uname -p]-unknown-[exec uname -s][exec uname -r]
+ }
+}
+
+proc config_sub {alias} {
+ if {[file executable $::autosetup(dir)/config.sub]} {
+ exec $::autosetup(dir)/config.sub $alias
+ } else {
+ return $alias
+ }
+}
+
+# @define name ?value=1?
+#
+# Defines the named variable to the given value.
+# These (name, value) pairs represent the results of the configuration check
+# and are available to be checked, modified and substituted.
+#
+proc define {name {value 1}} {
+ set ::define($name) $value
+ #dputs "$name <= $value"
+}
+
+# @define-append name value ...
+#
+# Appends the given value(s) to the given 'defined' variable.
+# If the variable is not defined or empty, it is set to $value.
+# Otherwise the value is appended, separated by a space.
+# Any extra values are similarly appended.
+# If any value is already contained in the variable (as a substring) it is omitted.
+#
+proc define-append {name args} {
+ if {[get-define $name ""] ne ""} {
+ # Make a token attempt to avoid duplicates
+ foreach arg $args {
+ if {[string first $arg $::define($name)] == -1} {
+ append ::define($name) " " $arg
+ }
+ }
+ } else {
+ set ::define($name) [join $args]
+ }
+ #dputs "$name += [join $args] => $::define($name)"
+}
+
+# @get-define name ?default=0?
+#
+# Returns the current value of the 'defined' variable, or $default
+# if not set.
+#
+proc get-define {name {default 0}} {
+ if {[info exists ::define($name)]} {
+ #dputs "$name => $::define($name)"
+ return $::define($name)
+ }
+ #dputs "$name => $default"
+ return $default
+}
+
+# @is-defined name
+#
+# Returns 1 if the given variable is defined.
+#
+proc is-defined {name} {
+ info exists ::define($name)
+}
+
+# @all-defines
+#
+# Returns a dictionary (name value list) of all defined variables.
+#
+# This is suitable for use with 'dict', 'array set' or 'foreach'
+# and allows for arbitrary processing of the defined variables.
+#
+proc all-defines {} {
+ array get ::define
+}
+
+
+# @get-env name default
+#
+# If $name was specified on the command line, return it.
+# If $name was set in the environment, return it.
+# Otherwise return $default.
+#
+proc get-env {name default} {
+ if {[dict exists $::autosetup(cmdline) $name]} {
+ return [dict get $::autosetup(cmdline) $name]
+ }
+ env $name $default
+}
+
+# @env-is-set name
+#
+# Returns 1 if the $name was specified on the command line or in the environment.
+#
+proc env-is-set {name} {
+ if {[dict exists $::autosetup(cmdline) $name]} {
+ return 1
+ }
+ info exists ::env($name)
+}
+
+# @readfile filename ?default=""?
+#
+# Return the contents of the file, without the trailing newline.
+# If the doesn't exist or can't be read, returns $default.
+#
+proc readfile {filename {default_value ""}} {
+ set result $default_value
+ catch {
+ set f [open $filename]
+ set result [read -nonewline $f]
+ close $f
+ }
+ return $result
+}
+
+# @writefile filename value
+#
+# Creates the given file containing $value.
+# Does not add an extra newline.
+#
+proc writefile {filename value} {
+ set f [open $filename w]
+ puts -nonewline $f $value
+ close $f
+}
+
+proc quote-if-needed {str} {
+ if {[string match {*[\" ]*} $str]} {
+ return \"[string map [list \" \\" \\ \\\\] $str]\"
+ }
+ return $str
+}
+
+proc quote-argv {argv} {
+ set args {}
+ foreach arg $argv {
+ lappend args [quote-if-needed $arg]
+ }
+ join $args
+}
+
+# @find-executable name
+#
+# Searches the path for an executable with the given name.
+# Note that the name may include some parameters, e.g. "cc -mbig-endian",
+# in which case the parameters are ignored.
+# Returns 1 if found, or 0 if not.
+#
+proc find-executable {name} {
+ # Ignore any parameters
+ set name [lindex $name 0]
+ if {$name eq ""} {
+ # The empty string is never a valid executable
+ return 0
+ }
+ foreach p [split [env PATH .] :] {
+ set exec [file join $p $name]
+ if {[file isfile $exec] && [file executable $exec]} {
+ dputs "Found $name -> $exec"
+ return 1
+ }
+ }
+ return 0
+}
+
+# @find-an-executable ?-required? name ...
+#
+# Given a list of possible executable names,
+# searches for one of these on the path.
+#
+# Returns the name found, or "" if none found.
+# If the first parameter is '-required', an error is generated
+# if no executable is found.
+#
+proc find-an-executable {args} {
+ set required 0
+ if {[lindex $args 0] eq "-required"} {
+ set args [lrange $args 1 end]
+ incr required
+ }
+ foreach name $args {
+ if {[find-executable $name]} {
+ return $name
+ }
+ }
+ if {$required} {
+ if {[llength $args] == 1} {
+ user-error "failed to find: [join $args]"
+ } else {
+ user-error "failed to find one of: [join $args]"
+ }
+ }
+ return ""
+}
+
+# @configlog msg
+#
+# Writes the given message to the configuration log, config.log
+#
+proc configlog {msg} {
+ if {![info exists ::autosetup(logfh)]} {
+ set ::autosetup(logfh) [open config.log w]
+ }
+ puts $::autosetup(logfh) $msg
+}
+
+# @msg-checking msg
+#
+# Writes the message with no newline to stdout.
+#
+proc msg-checking {msg} {
+ if {$::autosetup(msg-quiet) == 0} {
+ maybe-show-timestamp
+ puts -nonewline $msg
+ set ::autosetup(msg-checking) 1
+ }
+}
+
+# @msg-result msg
+#
+# Writes the message to stdout.
+#
+proc msg-result {msg} {
+ if {$::autosetup(msg-quiet) == 0} {
+ maybe-show-timestamp
+ puts $msg
+ set ::autosetup(msg-checking) 0
+ show-notices
+ }
+}
+
+# @msg-quiet command ...
+#
+# msg-quiet evaluates it's arguments as a command with output
+# from msg-checking and msg-result suppressed.
+#
+# This is useful if a check needs to run a subcheck which isn't
+# of interest to the user.
+proc msg-quiet {args} {
+ incr ::autosetup(msg-quiet)
+ set rc [uplevel 1 $args]
+ incr ::autosetup(msg-quiet) -1
+ return $rc
+}
+
+# Will be overridden by 'use misc'
+proc error-stacktrace {msg} {
+ return $msg
+}
+
+proc error-location {msg} {
+ return $msg
+}
+
+##################################################################
+#
+# Debugging output
+#
+proc dputs {msg} {
+ if {$::autosetup(debug)} {
+ puts $msg
+ }
+}
+
+##################################################################
+#
+# User and system warnings and errors
+#
+# Usage errors such as wrong command line options
+
+# @user-error msg
+#
+# Indicate incorrect usage to the user, including if required components
+# or features are not found.
+# autosetup exits with a non-zero return code.
+#
+proc user-error {msg} {
+ show-notices
+ puts stderr "Error: $msg"
+ puts stderr "Try: autosetup --help for options"
+ exit 1
+}
+
+# @user-notice msg
+#
+# Output the given message to stderr.
+#
+proc user-notice {msg} {
+ lappend ::autosetup(notices) $msg
+}
+
+# Incorrect usage in the auto.def file. Identify the location.
+proc autosetup-error {msg} {
+ show-notices
+ puts stderr [error-location $msg]
+ exit 1
+}
+
+proc show-notices {} {
+ if {$::autosetup(msg-checking)} {
+ puts ""
+ set ::autosetup(msg-checking) 0
+ }
+ flush stdout
+ if {[info exists ::autosetup(notices)]} {
+ puts stderr [join $::autosetup(notices) \n]
+ unset ::autosetup(notices)
+ }
+}
+
+proc maybe-show-timestamp {} {
+ if {$::autosetup(msg-timing) && $::autosetup(msg-checking) == 0} {
+ puts -nonewline [format {[%6.2f] } [expr {([clock millis] - $::autosetup(start)) % 10000 / 1000.0}]]
+ }
+}
+
+proc autosetup_version {} {
+ return "autosetup v$::autosetup(version)"
+}
+
+##################################################################
+#
+# Directory/path handling
+#
+
+proc realdir {dir} {
+ set oldpwd [pwd]
+ cd $dir
+ set pwd [pwd]
+ cd $oldpwd
+ return $pwd
+}
+
+# Follow symlinks until we get to something which is not a symlink
+proc realpath {path} {
+ #puts -nonewline "$path -> "
+ while {1} {
+ if {[catch {
+ set path [file link $path]
+ }]} {
+ # Not a link
+ break
+ }
+ }
+ #puts "$path"
+ return $path
+}
+
+# Convert absolute path, $path into a path relative
+# to the given directory (or the current dir, if not given).
+#
+proc relative-path {path {pwd {}}} {
+ set diff 0
+ set same 0
+ set newf {}
+ set prefix {}
+ set path [file-normalize $path]
+ if {$pwd eq ""} {
+ set pwd [pwd]
+ } else {
+ set pwd [file-normalize $pwd]
+ }
+
+ if {$path eq $pwd} {
+ return .
+ }
+
+ # Try to make the filename relative to the current dir
+ foreach p [split $pwd /] f [split $path /] {
+ if {$p ne $f} {
+ incr diff
+ } elseif {!$diff} {
+ incr same
+ }
+ if {$diff} {
+ if {$p ne ""} {
+ # Add .. for sibling or parent dir
+ lappend prefix ..
+ }
+ if {$f ne ""} {
+ lappend newf $f
+ }
+ }
+ }
+ if {$same == 1 || [llength $prefix] > 3} {
+ return $path
+ }
+
+ file join [join $prefix /] [join $newf /]
+}
+
+# Add filename as a dependency to rerun autosetup
+# The name will be normalised (converted to a full path)
+#
+proc autosetup_add_dep {filename} {
+ lappend ::autosetup(deps) [file-normalize $filename]
+}
+
+##################################################################
+#
+# Library module support
+#
+
+# @use module ...
+#
+# Load the given library modules.
+# e.g. use cc cc-shared
+#
+proc use {args} {
+ foreach m $args {
+ if {[info exists ::libmodule($m)]} {
+ continue
+ }
+ set ::libmodule($m) 1
+ if {[info exists ::modsource($m)]} {
+ uplevel #0 eval $::modsource($m)
+ } else {
+ set source $::autosetup(libdir)/${m}.tcl
+ if {[file exists $source]} {
+ uplevel #0 [list source $source]
+ autosetup_add_dep $source
+ } else {
+ puts "Looking for $source"
+ autosetup-error "use: No such module: $m"
+ }
+ }
+ }
+}
+
+# Initial settings
+set autosetup(exe) $::argv0
+set autosetup(istcl) 1
+set autosetup(start) [clock millis]
+set autosetup(installed) 0
+set autosetup(msg-checking) 0
+set autosetup(msg-quiet) 0
+
+# Embedded modules are inserted below here
+set autosetup(installed) 1
+# ----- module asciidoc-formatting -----
+
+set modsource(asciidoc-formatting) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which provides text formatting
+# asciidoc format
+
+use formatting
+
+proc para {text} {
+ regsub -all "\[ \t\n\]+" [string trim $text] " "
+}
+proc title {text} {
+ underline [para $text] =
+ nl
+}
+proc p {text} {
+ puts [para $text]
+ nl
+}
+proc code {text} {
+ foreach line [parse_code_block $text] {
+ puts " $line"
+ }
+ nl
+}
+proc codelines {lines} {
+ foreach line $lines {
+ puts " $line"
+ }
+ nl
+}
+proc nl {} {
+ puts ""
+}
+proc underline {text char} {
+ regexp "^(\[ \t\]*)(.*)" $text -> indent words
+ puts $text
+ puts $indent[string repeat $char [string length $words]]
+}
+proc section {text} {
+ underline "[para $text]" -
+ nl
+}
+proc subsection {text} {
+ underline "$text" ~
+ nl
+}
+proc bullet {text} {
+ puts "* [para $text]"
+}
+proc indent {text} {
+ puts " :: "
+ puts [para $text]
+}
+proc defn {first args} {
+ set sep ""
+ if {$first ne ""} {
+ puts "${first}::"
+ } else {
+ puts " :: "
+ }
+ set defn [string trim [join $args \n]]
+ regsub -all "\n\n" $defn "\n ::\n" defn
+ puts $defn
+}
+}
+
+# ----- module formatting -----
+
+set modsource(formatting) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which provides common text formatting
+
+# This is designed for documenation which looks like:
+# code {...}
+# or
+# code {
+# ...
+# ...
+# }
+# In the second case, we need to work out the indenting
+# and strip it from all lines but preserve the remaining indenting.
+# Note that all lines need to be indented with the same initial
+# spaces/tabs.
+#
+# Returns a list of lines with the indenting removed.
+#
+proc parse_code_block {text} {
+ # If the text begins with newline, take the following text,
+ # otherwise just return the original
+ if {![regexp "^\n(.*)" $text -> text]} {
+ return [list [string trim $text]]
+ }
+
+ # And trip spaces off the end
+ set text [string trimright $text]
+
+ set min 100
+ # Examine each line to determine the minimum indent
+ foreach line [split $text \n] {
+ if {$line eq ""} {
+ # Ignore empty lines for the indent calculation
+ continue
+ }
+ regexp "^(\[ \t\]*)" $line -> indent
+ set len [string length $indent]
+ if {$len < $min} {
+ set min $len
+ }
+ }
+
+ # Now make a list of lines with this indent removed
+ set lines {}
+ foreach line [split $text \n] {
+ lappend lines [string range $line $min end]
+ }
+
+ # Return the result
+ return $lines
+}
+}
+
+# ----- module getopt -----
+
+set modsource(getopt) {
+# Copyright (c) 2006 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Simple getopt module
+
+# Parse everything out of the argv list which looks like an option
+# Knows about --enable-thing and --disable-thing as alternatives for --thing=0 or --thing=1
+proc getopt {argvname} {
+ upvar $argvname argv
+
+ for {set i 0} {$i < [llength $argv]} {incr i} {
+ set arg [lindex $argv $i]
+
+ #dputs arg=$arg
+
+ if {$arg eq "--"} {
+ # End of options
+ incr i
+ break
+ }
+
+ if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} {
+ lappend opts($name) $value
+ } elseif {[regexp {^--(enable-|disable-)?([^=]*)$} $arg -> prefix name]} {
+ if {$prefix eq "disable-"} {
+ set value 0
+ } else {
+ set value 1
+ }
+ lappend opts($name) $value
+ } else {
+ break
+ }
+ }
+
+ #puts "getopt: argv=[join $argv] => [join [lrange $argv $i end]]"
+ #parray opts
+
+ set argv [lrange $argv $i end]
+
+ return [array get opts]
+}
+
+proc opt_val {optarrayname options {default {}}} {
+ upvar $optarrayname opts
+
+ set result {}
+
+ foreach o $options {
+ if {[info exists opts($o)]} {
+ lappend result {*}$opts($o)
+ }
+ }
+ if {[llength $result] == 0} {
+ return $default
+ }
+ return $result
+}
+
+proc opt_bool {optarrayname args} {
+ upvar $optarrayname opts
+
+ # Support the args being passed as a list
+ if {[llength $args] == 1} {
+ set args [lindex $args 0]
+ }
+
+ foreach o $args {
+ if {[info exists opts($o)]} {
+ if {"1" in $opts($o) || "yes" in $opts($o)} {
+ return 1
+ }
+ }
+ }
+ return 0
+}
+}
+
+# ----- module help -----
+
+set modsource(help) {
+# Copyright (c) 2010 WorkWare Systems http://workware.net.au/
+# All rights reserved
+
+# Module which provides usage, help and the command reference
+
+proc autosetup_help {what} {
+ use_pager
+
+ puts "Usage: [file tail $::autosetup(exe)] \[options\] \[settings\]\n"
+ puts "This is [autosetup_version], a build environment \"autoconfigurator\""
+ puts "See the documentation online at http://msteveb.github.com/autosetup/\n"
+
+ if {$what eq "local"} {
+ if {[file exists $::autosetup(autodef)]} {
+ # This relies on auto.def having a call to 'options'
+ # which will display options and quit
+ source $::autosetup(autodef)
+ } else {
+ options-show
+ }
+ } else {
+ incr ::autosetup(showhelp)
+ if {[catch {use $what}]} {
+ user-error "Unknown module: $what"
+ } else {
+ options-show
+ }
+ }
+ exit 0
+}
+
+# If not already paged and stdout is a tty, pipe the output through the pager
+# This is done by reinvoking autosetup with --nopager added
+proc use_pager {} {
+ if {![opt-bool nopager] && [env PAGER ""] ne "" && ![string match "not a tty" [exec tty]]} {
+ catch {
+ exec [info nameofexecutable] $::argv0 --nopager {*}$::argv | [env PAGER] >@stdout <@stdin 2>/dev/null
+ }
+ exit 0
+ }
+}
+
+# Outputs the autosetup references in one of several formats
+proc autosetup_reference {{type text}} {
+
+ use_pager
+
+ switch -glob -- $type {
+ wiki {use wiki-formatting}
+ ascii* {use asciidoc-formatting}
+ md - markdown {use markdown-formatting}
+ default {use text-formatting}
+ }
+
+ title "[autosetup_version] -- Command Reference"
+
+ section {Introduction}
+
+ p {
+ See http://msteveb.github.com/autosetup/ for the online documentation for 'autosetup'
+ }
+
+ p {
+ 'autosetup' provides a number of built-in commands which
+ are documented below. These may be used from 'auto.def' to test
+ for features, define variables, create files from templates and
+ other similar actions.
+ }
+
+ automf_command_reference
+
+ exit 0
+}
+
+proc autosetup_output_block {type lines} {
+ if {[llength $lines]} {
+ switch $type {
+ code {
+ codelines $lines
+ }
+ p {
+ p [join $lines]
+ }
+ list {
+ foreach line $lines {
+ bullet $line
+ }
+ nl
+ }
+ }
+ }
+}
+
+# Generate a command reference from inline documentation
+proc automf_command_reference {} {
+ lappend files $::autosetup(prog)
+ lappend files {*}[lsort [glob -nocomplain $::autosetup(libdir)/*.tcl]]
+
+ section "Core Commands"
+ set type p
+ set lines {}
+ set cmd {}
+
+ foreach file $files {
+ set f [open $file]
+ while {![eof $f]} {
+ set line [gets $f]
+
+ # Find lines starting with "# @*" and continuing through the remaining comment lines
+ if {![regexp {^# @(.*)} $line -> cmd]} {
+ continue
+ }
+
+ # Synopsis or command?
+ if {$cmd eq "synopsis:"} {
+ section "Module: [file rootname [file tail $file]]"
+ } else {
+ subsection $cmd
+ }
+
+ set lines {}
+ set type p
+
+ # Now the description
+ while {![eof $f]} {
+ set line [gets $f]
+
+ if {![regexp {^#(#)? ?(.*)} $line -> hash cmd]} {
+ break
+ }
+ if {$hash eq "#"} {
+ set t code
+ } elseif {[regexp {^- (.*)} $cmd -> cmd]} {
+ set t list
+ } else {
+ set t p
+ }
+
+ #puts "hash=$hash, oldhash=$oldhash, lines=[llength $lines], cmd=$cmd"
+
+ if {$t ne $type || $cmd eq ""} {
+ # Finish the current block
+ autosetup_output_block $type $lines
+ set lines {}
+ set type $t
+ }
+ if {$cmd ne ""} {
+ lappend lines $cmd
+ }
+ }
+
+ autosetup_output_block $type $lines
+ }
+ close $f
+ }
+}
+}
+
+# ----- module init -----
+
+set modsource(init) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module to help create auto.def and configure
+
+proc autosetup_init {} {
+ set create_configure 1
+ if {[file exists configure]} {
+ if {!$::autosetup(force)} {
+ # Could this be an autosetup configure?
+ if {![string match "*\nWRAPPER=*" [readfile configure]]} {
+ puts "I see configure, but not created by autosetup, so I won't overwrite it."
+ puts "Use autosetup --init --force to overwrite."
+ set create_configure 0
+ }
+ } else {
+ puts "I will overwrite the existing configure because you used --force."
+ }
+ } else {
+ puts "I don't see configure, so I will create it."
+ }
+ if {$create_configure} {
+ if {!$::autosetup(installed)} {
+ user-notice "Warning: Initialising from the development version of autosetup"
+
+ writefile configure "#!/bin/sh\nWRAPPER=\"\$0\" exec $::autosetup(dir)/autosetup \"\$@\"\n"
+ } else {
+ writefile configure \
+{#!/bin/sh
+dir="$(dirname "$0")/autosetup"
+WRAPPER="$0" exec $("$dir/find-tclsh" || echo false) "$dir/autosetup" "$@"
+}
+ }
+ catch {exec chmod 755 configure}
+ }
+ if {![file exists auto.def]} {
+ puts "I don't see auto.def, so I will create a default one."
+ writefile auto.def {# Initial auto.def created by 'autosetup --init'
+
+use cc
+
+# Add any user options here
+options {
+}
+
+make-autoconf-h config.h
+make-template Makefile.in
+}
+ }
+ if {![file exists Makefile.in]} {
+ puts "Note: I don't see Makefile.in. You will probably need to create one."
+ }
+
+ exit 0
+}
+}
+
+# ----- module install -----
+
+set modsource(install) {
+# Copyright (c) 2006-2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which can install autosetup
+
+proc autosetup_install {} {
+ if {[catch {
+ file mkdir autosetup
+
+ set f [open autosetup/autosetup w]
+
+ set publicmodules {}
+
+ # First the main script, but only up until "CUT HERE"
+ set in [open $::autosetup(dir)/autosetup]
+ while {[gets $in buf] >= 0} {
+ if {$buf ne "##-- CUT HERE --##"} {
+ puts $f $buf
+ continue
+ }
+
+ # Insert the static modules here
+ # i.e. those which don't contain @synopsis:
+ puts $f "set autosetup(installed) 1"
+ foreach file [glob $::autosetup(libdir)/*.tcl] {
+ set buf [readfile $file]
+ if {[string match "*\n# @synopsis:*" $buf]} {
+ lappend publicmodules $file
+ continue
+ }
+ set modname [file rootname [file tail $file]]
+ puts $f "# ----- module $modname -----"
+ puts $f "\nset modsource($modname) \{"
+ puts $f $buf
+ puts $f "\}\n"
+ }
+ }
+ close $in
+ close $f
+ exec chmod 755 autosetup/autosetup
+
+ # Install public modules
+ foreach file $publicmodules {
+ autosetup_install_file $file autosetup
+ }
+
+ # Install support files
+ foreach file {config.guess config.sub jimsh0.c find-tclsh test-tclsh LICENSE} {
+ autosetup_install_file $::autosetup(dir)/$file autosetup
+ }
+ exec chmod 755 autosetup/config.sub autosetup/config.guess autosetup/find-tclsh
+
+ writefile autosetup/README.autosetup \
+ "This is [autosetup_version]. See http://msteveb.github.com/autosetup/\n"
+
+ } error]} {
+ user-error "Failed to install autosetup: $error"
+ }
+ puts "Installed [autosetup_version] to autosetup/"
+ catch {exec [info nameofexecutable] autosetup/autosetup --init >@stdout 2>@stderr}
+
+ exit 0
+}
+
+# Append the contents of $file to filehandle $f
+proc autosetup_install_append {f file} {
+ set in [open $file]
+ puts $f [read $in]
+ close $in
+}
+
+proc autosetup_install_file {file dir} {
+ if {![file exists $file]} {
+ error "Missing installation file '$file'"
+ }
+ writefile [file join $dir [file tail $file]] [readfile $file]\n
+}
+
+if {$::autosetup(installed)} {
+ user-error "autosetup can only be installed from development source, not from installed copy"
+}
+}
+
+# ----- module markdown-formatting -----
+
+set modsource(markdown-formatting) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which provides text formatting
+# markdown format (kramdown syntax)
+
+use formatting
+
+proc para {text} {
+ regsub -all "\[ \t\n\]+" [string trim $text] " " text
+ regsub -all {([^a-zA-Z])'([^']*)'} $text {\1**`\2`**} text
+ regsub -all {^'([^']*)'} $text {**`\1`**} text
+ regsub -all {(http[^ \t\n]*)} $text {[\1](\1)} text
+ return $text
+}
+proc title {text} {
+ underline [para $text] =
+ nl
+}
+proc p {text} {
+ puts [para $text]
+ nl
+}
+proc codelines {lines} {
+ puts "~~~~~~~~~~~~"
+ foreach line $lines {
+ puts $line
+ }
+ puts "~~~~~~~~~~~~"
+ nl
+}
+proc code {text} {
+ puts "~~~~~~~~~~~~"
+ foreach line [parse_code_block $text] {
+ puts $line
+ }
+ puts "~~~~~~~~~~~~"
+ nl
+}
+proc nl {} {
+ puts ""
+}
+proc underline {text char} {
+ regexp "^(\[ \t\]*)(.*)" $text -> indent words
+ puts $text
+ puts $indent[string repeat $char [string length $words]]
+}
+proc section {text} {
+ underline "[para $text]" -
+ nl
+}
+proc subsection {text} {
+ puts "### `$text`"
+ nl
+}
+proc bullet {text} {
+ puts "* [para $text]"
+}
+proc defn {first args} {
+ puts "^"
+ set defn [string trim [join $args \n]]
+ if {$first ne ""} {
+ puts "**${first}**"
+ puts -nonewline ": "
+ regsub -all "\n\n" $defn "\n: " defn
+ }
+ puts "$defn"
+}
+}
+
+# ----- module misc -----
+
+set modsource(misc) {
+# Copyright (c) 2007-2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module containing misc procs useful to modules
+
+set ::autosetup(istcl) [info exists ::tcl_library]
+
+# Tcl doesn't have the env command
+if {$::autosetup(istcl)} {
+ proc env {var args} {
+ if {[info exists ::env($var)]} {
+ return $::env($var)
+ }
+ if {[llength $args]} {
+ return [lindex $args 0]
+ }
+ return -code error "environment variable \"$var\" does not exist"
+ }
+}
+
+# In case 'file normalize' doesn't exist
+#
+proc file-normalize {path} {
+ if {[catch {file normalize $path} result]} {
+ if {$path eq ""} {
+ return ""
+ }
+ set oldpwd [pwd]
+ if {[file isdir $path]} {
+ cd $path
+ set result [pwd]
+ } else {
+ cd [file dirname $path]
+ set result [file join [pwd] [file tail $path]]
+ }
+ cd $oldpwd
+ }
+ return $result
+}
+
+# If everything is working properly, the only errors which occur
+# should be generated in user code (e.g. auto.def).
+# By default, we only want to show the error location in user code.
+# We use [info frame] to achieve this, but it works differently on Tcl and Jim.
+#
+# This is designed to be called for incorrect usage in auto.def, via autosetup-error
+#
+proc error-location {msg} {
+ if {$::autosetup(debug)} {
+ return -code error $msg
+ }
+ # Search back through the stack trace for the first error in a .def file
+ for {set i 1} {$i < [info level]} {incr i} {
+ if {$::autosetup(istcl)} {
+ array set info [info frame -$i]
+ } else {
+ lassign [info frame -$i] info(caller) info(file) info(line)
+ }
+ if {[string match *.def $info(file)]} {
+ return "[relative-path $info(file)]:$info(line): Error: $msg"
+ }
+ #puts "Skipping $info(file):$info(line)"
+ }
+ return $msg
+}
+
+# Similar to error-location, but called when user code generates an error
+# In this case we want to show the stack trace in user code, but not in autosetup code
+# (unless --debug is enabled)
+#
+proc error-stacktrace {msg} {
+ if {$::autosetup(istcl)} {
+ if {[regexp {file "([^ ]*)" line ([0-9]*)} $::errorInfo dummy file line]} {
+ return "[relative-path $file]:$line $msg\n$::errorInfo"
+ }
+ return $::errorInfo
+ } else {
+ # Prepend a live stacktrace to the error stacktrace, omitting the current level
+ set stacktrace [concat [info stacktrace] [lrange [stacktrace] 3 end]]
+
+ if {!$::autosetup(debug)} {
+ # Omit any levels from autosetup or with no file
+ set newstacktrace {}
+ foreach {p f l} $stacktrace {
+ if {[string match "*autosetup" $f] || $f eq ""} {
+ #puts "Skipping $p $f:$l"
+ continue
+ }
+ lappend newstacktrace $p $f $l
+ }
+ set stacktrace $newstacktrace
+ }
+
+ # Convert filenames to relative paths
+ set newstacktrace {}
+ foreach {p f l} $stacktrace {
+ lappend newstacktrace $p [relative-path $f] $l
+ }
+ lassign $newstacktrace p f l
+ if {$f ne ""} {
+ set prefix "$f:$l: "
+ } else {
+ set prefix ""
+ }
+
+ return "${prefix}Error: $msg\n[stackdump $newstacktrace]"
+ }
+}
+}
+
+# ----- module text-formatting -----
+
+set modsource(text-formatting) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which provides text formatting
+
+use formatting
+
+proc wordwrap {text length {firstprefix ""} {nextprefix ""}} {
+ set len 0
+ set space $firstprefix
+ foreach word [split $text] {
+ set word [string trim $word]
+ if {$word == ""} {
+ continue
+ }
+ if {$len && [string length $space$word] + $len >= $length} {
+ puts ""
+ set len 0
+ set space $nextprefix
+ }
+ incr len [string length $space$word]
+
+ # Use man-page conventions for highlighting 'quoted' and *quoted*
+ # single words.
+ # Use x^Hx for *bold* and _^Hx for 'underline'.
+ #
+ # less and more will both understand this.
+ # Pipe through 'col -b' to remove them.
+ if {[regexp {^'(.*)'([^a-zA-Z0-9_]*)$} $word -> bareword dot]} {
+ regsub -all . $bareword "_\b&" word
+ append word $dot
+ } elseif {[regexp {^[*](.*)[*]([^a-zA-Z0-9_]*)$} $word -> bareword dot]} {
+ regsub -all . $bareword "&\b&" word
+ append word $dot
+ }
+ puts -nonewline $space$word
+ set space " "
+ }
+ if {$len} {
+ puts ""
+ }
+}
+proc title {text} {
+ underline [string trim $text] =
+ nl
+}
+proc p {text} {
+ wordwrap $text 80
+ nl
+}
+proc codelines {lines} {
+ foreach line $lines {
+ puts " $line"
+ }
+ nl
+}
+proc nl {} {
+ puts ""
+}
+proc underline {text char} {
+ regexp "^(\[ \t\]*)(.*)" $text -> indent words
+ puts $text
+ puts $indent[string repeat $char [string length $words]]
+}
+proc section {text} {
+ underline "[string trim $text]" -
+ nl
+}
+proc subsection {text} {
+ underline "$text" ~
+ nl
+}
+proc bullet {text} {
+ wordwrap $text 76 " * " " "
+}
+proc indent {text} {
+ wordwrap $text 76 " " " "
+}
+proc defn {first args} {
+ if {$first ne ""} {
+ underline " $first" ~
+ }
+ foreach p $args {
+ if {$p ne ""} {
+ indent $p
+ }
+ }
+}
+}
+
+# ----- module wiki-formatting -----
+
+set modsource(wiki-formatting) {
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# Module which provides text formatting
+# wiki.tcl.tk format output
+
+use formatting
+
+proc joinlines {text} {
+ set lines {}
+ foreach l [split [string trim $text] \n] {
+ lappend lines [string trim $l]
+ }
+ join $lines
+}
+proc p {text} {
+ puts [joinlines $text]
+ puts ""
+}
+proc title {text} {
+ puts "*** [joinlines $text] ***"
+ puts ""
+}
+proc codelines {lines} {
+ puts "======"
+ foreach line $lines {
+ puts " $line"
+ }
+ puts "======"
+}
+proc code {text} {
+ puts "======"
+ foreach line [parse_code_block $text] {
+ puts " $line"
+ }
+ puts "======"
+}
+proc nl {} {
+}
+proc section {text} {
+ puts "'''$text'''"
+ puts ""
+}
+proc subsection {text} {
+ puts "''$text''"
+ puts ""
+}
+proc bullet {text} {
+ puts " * [joinlines $text]"
+}
+proc indent {text} {
+ puts " : [joinlines $text]"
+}
+proc defn {first args} {
+ if {$first ne ""} {
+ indent '''$first'''
+ }
+
+ foreach p $args {
+ p $p
+ }
+}
+}
+
+
+##################################################################
+#
+# Entry/Exit
+#
+if {$autosetup(debug)} {
+ main $argv
+}
+if {[catch {main $argv} msg] == 1} {
+ show-notices
+ puts stderr [error-stacktrace $msg]
+ if {!$autosetup(debug) && !$autosetup(istcl)} {
+ puts stderr "Try: '[file tail $autosetup(exe)] --debug' for a full stack trace"
+ }
+ exit 1
+}
diff --git a/autosetup/cc-lib.tcl b/autosetup/cc-lib.tcl
new file mode 100644
index 0000000..e769db9
--- /dev/null
+++ b/autosetup/cc-lib.tcl
@@ -0,0 +1,36 @@
+# Copyright (c) 2011 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# @synopsis:
+#
+# Provides a library of common tests on top of the 'cc' module.
+
+use cc
+
+module-options {}
+
+# @cc-check-lfs
+#
+# The equivalent of the AC_SYS_LARGEFILE macro
+#
+# defines 'HAVE_LFS' if LFS is available,
+# and defines '_FILE_OFFSET_BITS=64' if necessary
+#
+# Returns 1 if 'LFS' is available or 0 otherwise
+#
+proc cc-check-lfs {} {
+ cc-check-includes sys/types.h
+ msg-checking "Checking if -D_FILE_OFFSET_BITS=64 is needed..."
+ set lfs 1
+ if {[msg-quiet cc-with {-includes sys/types.h} {cc-check-sizeof off_t}] == 8} {
+ msg-result no
+ } elseif {[msg-quiet cc-with {-includes sys/types.h -cflags -D_FILE_OFFSET_BITS=64} {cc-check-sizeof off_t}] == 8} {
+ define _FILE_OFFSET_BITS 64
+ msg-result yes
+ } else {
+ set lfs 0
+ msg-result none
+ }
+ define-feature lfs $lfs
+ return $lfs
+}
diff --git a/autosetup/cc-shared.tcl b/autosetup/cc-shared.tcl
new file mode 100644
index 0000000..6de498d
--- /dev/null
+++ b/autosetup/cc-shared.tcl
@@ -0,0 +1,48 @@
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# @synopsis:
+#
+# The 'cc-shared' module provides support for shared libraries and shared objects.
+# It defines the following variables:
+#
+## SH_CFLAGS Flags to use compiling sources destined for a shared library
+## SH_LDFLAGS Flags to use linking a shared library
+## SHOBJ_CFLAGS Flags to use compiling sources destined for a shared object
+## SHOBJ_LDFLAGS Flags to use linking a shared object
+## SH_LINKFLAGS Flags to use linking an executable which will load shared objects
+## LD_LIBRARY_PATH Environment variable which specifies path to shared libraries
+
+module-options {}
+
+foreach i {SH_LINKFLAGS SH_CFLAGS SH_LDFLAGS SHOBJ_CFLAGS SHOBJ_LDFLAGS} {
+ define $i ""
+}
+
+define LD_LIBRARY_PATH LD_LIBRARY_PATH
+
+switch -glob -- [get-define host] {
+ *-*-darwin* {
+ define SH_CFLAGS -dynamic
+ define SH_LDFLAGS "-dynamiclib"
+ define SHOBJ_CFLAGS "-dynamic -fno-common"
+ define SHOBJ_LDFLAGS "-bundle -undefined dynamic_lookup"
+ define LD_LIBRARY_PATH DYLD_LIBRARY_PATH
+ }
+ *-*-ming* {
+ define SH_LDFLAGS -shared
+ define SHOBJ_LDFLAGS -shared
+ }
+ *-*-cygwin {
+ define SH_LDFLAGS -shared
+ define SHOBJ_LDFLAGS -shared
+ }
+ * {
+ # Generic Unix settings
+ define SH_LINKFLAGS -rdynamic
+ define SH_CFLAGS -fPIC
+ define SH_LDFLAGS -shared
+ define SHOBJ_CFLAGS -fPIC
+ define SHOBJ_LDFLAGS "-shared -nostartfiles"
+ }
+}
diff --git a/autosetup/cc.tcl b/autosetup/cc.tcl
new file mode 100644
index 0000000..54c4e8f
--- /dev/null
+++ b/autosetup/cc.tcl
@@ -0,0 +1,572 @@
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# @synopsis:
+#
+# The 'cc' module supports checking various 'features' of the C or C++
+# compiler/linker environment. Common commands are cc-check-includes,
+# cc-check-types, cc-check-functions, cc-with, make-autoconf-h and make-template.
+#
+# The following environment variables are used if set:
+#
+## CC - C compiler
+## CXX - C++ compiler
+## CCACHE - Set to "none" to disable automatic use of ccache
+## CFLAGS - Additional C compiler flags
+## CXXFLAGS - Additional C++ compiler flags
+## LDFLAGS - Additional compiler flags during linking
+## LIBS - Additional libraries to use (for all tests)
+## CROSS - Tool prefix for cross compilation
+#
+# The following variables are defined from the corresponding
+# environment variables if set.
+#
+## CPPFLAGS
+## LINKFLAGS
+## CC_FOR_BUILD
+## LD
+
+use system
+
+module-options {}
+
+# Note that the return code is not meaningful
+proc cc-check-something {name code} {
+ uplevel 1 $code
+}
+
+# Checks for the existence of the given function by linking
+#
+proc cctest_function {function} {
+ cctest -link 1 -declare "extern void $function\(void);" -code "$function\();"
+}
+
+# Checks for the existence of the given type by compiling
+proc cctest_type {type} {
+ cctest -code "$type _x;"
+}
+
+# Checks for the existence of the given type/structure member.
+# e.g. "struct stat.st_mtime"
+proc cctest_member {struct_member} {
+ lassign [split $struct_member .] struct member
+ cctest -code "static $struct _s; return sizeof(_s.$member);"
+}
+
+# Checks for the existence of the given define by compiling
+#
+proc cctest_define {name} {
+ cctest -code "#ifndef $name\n#error not defined\n#endif"
+}
+
+# @cc-check-sizeof type ...
+#
+# Checks the size of the given types (between 1 and 32, inclusive).
+# Defines a variable with the size determined, or "unknown" otherwise.
+# e.g. for type 'long long', defines SIZEOF_LONG_LONG.
+# Returns the size of the last type.
+#
+proc cc-check-sizeof {args} {
+ foreach type $args {
+ msg-checking "Checking for sizeof $type..."
+ set size unknown
+ # Try the most common sizes first
+ foreach i {4 8 1 2 16 32} {
+ if {[cctest -code "static int _x\[sizeof($type) == $i ? 1 : -1\] = { 1 };"]} {
+ set size $i
+ break
+ }
+ }
+ msg-result $size
+ set define [feature-define-name $type SIZEOF_]
+ define $define $size
+ }
+ # Return the last result
+ get-define $define
+}
+
+# Checks for each feature in $list by using the given script.
+#
+# When the script is evaluated, $each is set to the feature
+# being checked, and $extra is set to any additional cctest args.
+#
+# Returns 1 if all features were found, or 0 otherwise.
+proc cc-check-some-feature {list script} {
+ set ret 1
+ foreach each $list {
+ if {![check-feature $each $script]} {
+ set ret 0
+ }
+ }
+ return $ret
+}
+
+# @cc-check-includes includes ...
+#
+# Checks that the given include files can be used
+proc cc-check-includes {args} {
+ cc-check-some-feature $args {
+ cctest -includes $each
+ }
+}
+
+# @cc-check-types type ...
+#
+# Checks that the types exist.
+proc cc-check-types {args} {
+ cc-check-some-feature $args {
+ cctest_type $each
+ }
+}
+
+# @cc-check-defines define ...
+#
+# Checks that the given preprocessor symbol is defined
+proc cc-check-defines {args} {
+ cc-check-some-feature $args {
+ cctest_define $each
+ }
+}
+
+# @cc-check-functions function ...
+#
+# Checks that the given functions exist (can be linked)
+proc cc-check-functions {args} {
+ cc-check-some-feature $args {
+ cctest_function $each
+ }
+}
+
+# @cc-check-members type.member ...
+#
+# Checks that the given type/structure members exist.
+# A structure member is of the form "struct stat.st_mtime"
+proc cc-check-members {args} {
+ cc-check-some-feature $args {
+ cctest_member $each
+ }
+}
+
+# @cc-check-function-in-lib function libs ?otherlibs?
+#
+# Checks that the given given function can be found on one of the libs.
+#
+# First checks for no library required, then checks each of the libraries
+# in turn.
+#
+# If the function is found, the feature is defined and lib_$function is defined
+# to -l$lib where the function was found, or "" if no library required.
+# In addition, -l$lib is added to the LIBS define.
+#
+# If additional libraries may be needed to linked, they should be specified
+# as $extralibs as "-lotherlib1 -lotherlib2".
+# These libraries are not automatically added to LIBS.
+#
+# Returns 1 if found or 0 if not.
+#
+proc cc-check-function-in-lib {function libs {otherlibs {}}} {
+ msg-checking "Checking libs for $function..."
+ set found 0
+ cc-with [list -libs $otherlibs] {
+ if {[cctest_function $function]} {
+ msg-result "none needed"
+ define lib_$function ""
+ incr found
+ } else {
+ foreach lib $libs {
+ cc-with [list -libs -l$lib] {
+ if {[cctest_function $function]} {
+ msg-result -l$lib
+ define lib_$function -l$lib
+ define-append LIBS -l$lib
+ incr found
+ break
+ }
+ }
+ }
+ }
+ }
+ if {$found} {
+ define [feature-define-name $function]
+ } else {
+ msg-result "no"
+ }
+ return $found
+}
+
+# @cc-check-tools tool ...
+#
+# Checks for existence of the given compiler tools, taking
+# into account any cross compilation prefix.
+#
+# For example, when checking for "ar", first AR is checked on the command
+# line and then in the environment. If not found, "${host}-ar" or
+# simply "ar" is assumed depending upon whether cross compiling.
+# The path is searched for this executable, and if found AR is defined
+# to the executable name.
+#
+# It is an error if the executable is not found.
+#
+proc cc-check-tools {args} {
+ foreach tool $args {
+ set TOOL [string toupper $tool]
+ set exe [get-env $TOOL [get-define cross]$tool]
+ if {![find-executable $exe]} {
+ user-error "Failed to find $exe"
+ }
+ define $TOOL $exe
+ }
+}
+
+# @cc-check-progs prog ...
+#
+# Checks for existence of the given executables on the path.
+#
+# For example, when checking for "grep", the path is searched for
+# the executable, 'grep', and if found GREP is defined as "grep".
+#
+# It the executable is not found, the variable is defined as false.
+# Returns 1 if all programs were found, or 0 otherwise.
+#
+proc cc-check-progs {args} {
+ set failed 0
+ foreach prog $args {
+ set PROG [string toupper $prog]
+ msg-checking "Checking for $prog..."
+ if {![find-executable $prog]} {
+ msg-result no
+ define $PROG false
+ incr failed
+ } else {
+ msg-result ok
+ define $PROG $prog
+ }
+ }
+ expr {!$failed}
+}
+
+# Adds the given settings to $::autosetup(ccsettings) and
+# returns the old settings.
+#
+proc cc-add-settings {settings} {
+ if {[llength $settings] % 2} {
+ autosetup-error "settings list is missing a value: $settings"
+ }
+
+ set prev [cc-get-settings]
+ # workaround a bug in some versions of jimsh by forcing
+ # conversion of $prev to a list
+ llength $prev
+
+ array set new $prev
+
+ foreach {name value} $settings {
+ switch -exact -- $name {
+ -cflags - -includes {
+ # These are given as lists
+ lappend new($name) {*}$value
+ }
+ -declare {
+ lappend new($name) $value
+ }
+ -libs {
+ # Note that new libraries are added before previous libraries
+ set new($name) [list {*}$value {*}$new($name)]
+ }
+ -link - -lang {
+ set new($name) $value
+ }
+ -source - -sourcefile - -code {
+ # XXX: These probably are only valid directly from cctest
+ set new($name) $value
+ }
+ default {
+ autosetup-error "unknown cctest setting: $name"
+ }
+ }
+ }
+
+ cc-store-settings [array get new]
+
+ return $prev
+}
+
+proc cc-store-settings {new} {
+ set ::autosetup(ccsettings) $new
+}
+
+proc cc-get-settings {} {
+ return $::autosetup(ccsettings)
+}
+
+# @cc-with settings ?{ script }?
+#
+# Sets the given 'cctest' settings and then runs the tests in 'script'.
+# Note that settings such as -lang replace the current setting, while
+# those such as -includes are appended to the existing setting.
+#
+# If no script is given, the settings become the default for the remainder
+# of the auto.def file.
+#
+## cc-with {-lang c++} {
+## # This will check with the C++ compiler
+## cc-check-types bool
+## cc-with {-includes signal.h} {
+## # This will check with the C++ compiler, signal.h and any existing includes.
+## ...
+## }
+## # back to just the C++ compiler
+## }
+#
+# The -libs setting is special in that newer values are added *before* earlier ones.
+#
+## cc-with {-libs {-lc -lm}} {
+## cc-with {-libs -ldl} {
+## cctest -libs -lsocket ...
+## # libs will be in this order: -lsocket -ldl -lc -lm
+## }
+## }
+proc cc-with {settings args} {
+ if {[llength $args] == 0} {
+ cc-add-settings $settings
+ } elseif {[llength $args] > 1} {
+ autosetup-error "usage: cc-with settings ?script?"
+ } else {
+ set save [cc-add-settings $settings]
+ set rc [catch {uplevel 1 [lindex $args 0]} result info]
+ cc-store-settings $save
+ if {$rc != 0} {
+ return $result -code [dict get $info -code]
+ }
+ return $result
+ }
+}
+
+# @cctest ?settings?
+#
+# Low level C compiler checker. Compiles and or links a small C program
+# according to the arguments and returns 1 if OK, or 0 if not.
+#
+# Supported settings are:
+#
+## -cflags cflags A list of flags to pass to the compiler
+## -includes list A list of includes, e.g. {stdlib.h stdio.h}
+## -declare code Code to declare before main()
+## -link 1 Don't just compile, link too
+## -lang c|c++ Use the C (default) or C++ compiler
+## -libs liblist List of libraries to link, e.g. {-ldl -lm}
+## -code code Code to compile in the body of main()
+## -source code Compile a complete program. Ignore -includes, -declare and -code
+## -sourcefile file Shorthand for -source [readfile [get-define srcdir]/$file]
+#
+# Unless -source or -sourcefile is specified, the C program looks like:
+#
+## #include <firstinclude> /* same for remaining includes in the list */
+##
+## declare-code /* any code in -declare, verbatim */
+##
+## int main(void) {
+## code /* any code in -code, verbatim */
+## return 0;
+## }
+#
+# Any failures are recorded in 'config.log'
+#
+proc cctest {args} {
+ set src conftest__.c
+ set tmp conftest__.o
+
+ # Easiest way to merge in the settings
+ cc-with $args {
+ array set opts [cc-get-settings]
+ }
+
+ if {[info exists opts(-sourcefile)]} {
+ set opts(-source) [readfile [get-define srcdir]/$opts(-sourcefile) "#error can't find $opts(-sourcefile)"]
+ }
+ if {[info exists opts(-source)]} {
+ set lines $opts(-source)
+ } else {
+ foreach i $opts(-includes) {
+ if {$opts(-code) eq "" || [have-feature $i]} {
+ lappend source "#include <$i>"
+ } elseif {![feature-checked $i]} {
+ user-notice "Warning: using #include <$i> which has not been checked -- ignoring"
+ }
+ }
+ lappend source {*}$opts(-declare)
+ lappend source "int main(void) {"
+ lappend source $opts(-code)
+ lappend source "return 0;"
+ lappend source "}"
+
+ set lines [join $source \n]
+ }
+
+ # Build the command line
+ set cmdline {}
+ lappend cmdline {*}[get-define CCACHE]
+ switch -exact -- $opts(-lang) {
+ c++ {
+ lappend cmdline {*}[get-define CXX] {*}[get-define CXXFLAGS]
+ }
+ c {
+ lappend cmdline {*}[get-define CC] {*}[get-define CFLAGS]
+ }
+ default {
+ autosetup-error "cctest called with unknown language: $opts(-lang)"
+ }
+ }
+
+ if {!$opts(-link)} {
+ lappend cmdline -c
+ }
+ lappend cmdline {*}$opts(-cflags)
+
+ switch -glob -- [get-define host] {
+ *-*-darwin* {
+ # Don't generate .dSYM directories
+ lappend cmdline -gstabs
+ }
+ }
+ lappend cmdline $src -o $tmp {*}$opts(-libs)
+
+ # At this point we have the complete command line and the
+ # complete source to be compiled. Get the result from cache if
+ # we can
+ if {[info exists ::cc_cache($cmdline,$lines)]} {
+ set ok $::cc_cache($cmdline,$lines)
+ if {$::autosetup(debug)} {
+ configlog "From cache (ok=$ok): [join $cmdline]"
+ configlog "============"
+ configlog $lines
+ configlog "============"
+ }
+ return $ok
+ }
+
+ writefile $src $lines\n
+
+ set ok 1
+ if {[catch {exec {*}$cmdline 2>@1} result errinfo]} {
+ configlog "Failed: [join $cmdline]"
+ configlog $result
+ configlog "============"
+ configlog "The failed code was:"
+ configlog $lines
+ configlog "============"
+ set ok 0
+ } elseif {$::autosetup(debug)} {
+ configlog "Compiled OK: [join $cmdline]"
+ configlog "============"
+ configlog $lines
+ configlog "============"
+ }
+ file delete $src
+ file delete $tmp
+
+ # cache it
+ set ::cc_cache($cmdline,$lines) $ok
+
+ return $ok
+}
+
+# @make-autoconf-h outfile ?auto-patterns=HAVE_*? ?unquoted-patterns=SIZEOF_*?
+#
+# Examines all defined variables which match the given patterns
+# and writes an include file, $file, which defines each of these.
+# Variables which match 'auto-patterns' are output as follows:
+# - defines which have the value "0" are ignored.
+# - defines which have integer values are defined as the integer value.
+# - any other value is defined as a string, e.g. "value"
+#
+# Variables which match 'unquoted-patterns' are defined unquoted.
+#
+# If the file would be unchanged, it is not written.
+proc make-autoconf-h {file {autopatterns {HAVE_*}} {unquotedpatterns {SIZEOF_*}}} {
+ set guard _[string toupper [regsub -all {[^a-zA-Z0-9]} [file tail $file] _]]
+ file mkdir [file dirname $file]
+ set lines {}
+ lappend lines "#ifndef $guard"
+ lappend lines "#define $guard"
+
+ # Work out the type of each variable
+ array set types {}
+ foreach pattern $autopatterns {
+ foreach n [array names ::define $pattern] {
+ set types($n) auto
+ }
+ }
+ foreach pattern $unquotedpatterns {
+ foreach n [array names ::define $pattern] {
+ set types($n) unquoted
+ }
+ }
+ foreach n [lsort [array names types]] {
+ if {$types($n) eq "auto"} {
+ # Automatically determine the type
+ if {$::define($n) eq "0"} {
+ lappend lines "/* #undef $n */"
+ continue
+ }
+ if {![string is integer -strict $::define($n)]} {
+ lappend lines "#define $n \"$::define($n)\""
+ continue
+ }
+ }
+ # Unquoted
+ lappend lines "#define $n $::define($n)"
+ }
+ lappend lines "#endif"
+ set buf [join $lines \n]
+ write-if-changed $file $buf {
+ msg-result "Created $file"
+ }
+}
+
+# Initialise some values from the environment or commandline or default settings
+foreach i {LDFLAGS LIBS CPPFLAGS LINKFLAGS {CFLAGS "-g -O2"} {CC_FOR_BUILD cc}} {
+ lassign $i var default
+ define $var [get-env $var $default]
+}
+
+if {[env-is-set CC]} {
+ # Set by the user, so don't try anything else
+ set try [list [get-env CC ""]]
+} else {
+ # Try some reasonable options
+ set try [list [get-define cross]cc [get-define cross]gcc]
+}
+define CC [find-an-executable {*}$try]
+if {[get-define CC] eq ""} {
+ user-error "Could not find a C compiler. Tried: [join $try ", "]"
+}
+
+define CPP [get-env CPP "[get-define CC] -E"]
+
+# XXX: Could avoid looking for a C++ compiler until requested
+# Note that if CXX isn't found, we just set it to "false". It might not be needed.
+if {[env-is-set CXX]} {
+ define CXX [find-an-executable -required [get-env CXX ""]]
+} else {
+ define CXX [find-an-executable [get-define cross]c++ [get-define cross]g++ false]
+}
+
+# CXXFLAGS default to CFLAGS if not specified
+define CXXFLAGS [get-env CXXFLAGS [get-define CFLAGS]]
+
+cc-check-tools ld
+
+define CCACHE [find-an-executable [get-env CCACHE ccache]]
+
+# Initial cctest settings
+cc-store-settings {-cflags {} -includes {} -declare {} -link 0 -lang c -libs {} -code {}}
+
+msg-result "C compiler...[get-define CCACHE] [get-define CC] [get-define CFLAGS]"
+if {[get-define CXX] ne "false"} {
+ msg-result "C++ compiler...[get-define CCACHE] [get-define CXX] [get-define CXXFLAGS]"
+}
+
+if {![cc-check-includes stdlib.h]} {
+ user-error "Compiler does not work. See config.log"
+}
diff --git a/config.guess b/autosetup/config.guess
index 396482d..4c8f032 100755
--- a/config.guess
+++ b/autosetup/config.guess
@@ -1,10 +1,10 @@
#! /bin/sh
# Attempt to guess a canonical system name.
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
-# Inc.
+# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+# Free Software Foundation, Inc.
-timestamp='2006-07-02'
+timestamp='2010-09-24'
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
@@ -27,16 +27,16 @@ timestamp='2006-07-02'
# the same distribution terms that you use for the rest of that program.
-# Originally written by Per Bothner <per@bothner.com>.
-# Please send patches to <config-patches@gnu.org>. Submit a context
-# diff and a properly formatted ChangeLog entry.
+# Originally written by Per Bothner. Please send patches (context
+# diff format) to <config-patches@gnu.org> and include a ChangeLog
+# entry.
#
# This script attempts to guess a canonical system name similar to
# config.sub. If it succeeds, it prints the system name on stdout, and
# exits with 0. Otherwise, it exits with 1.
#
-# The plan is that this can be called by configure scripts if you
-# don't specify an explicit build system type.
+# You can get the latest version of this script from:
+# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD
me=`echo "$0" | sed -e 's,.*/,,'`
@@ -56,8 +56,9 @@ version="\
GNU config.guess ($timestamp)
Originally written by Per Bothner.
-Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
-Free Software Foundation, Inc.
+Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
+Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
@@ -91,7 +92,7 @@ if test $# != 0; then
exit 1
fi
-trap 'exit 1' 1 2 15
+trap 'exit 1' HUP INT TERM
# CC_FOR_BUILD -- compiler used by this script. Note that the use of a
# compiler to aid in system detection is discouraged as it requires
@@ -105,7 +106,7 @@ trap 'exit 1' 1 2 15
set_cc_for_build='
trap "exitcode=\$?; (rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null) && exit \$exitcode" 0 ;
-trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" 1 2 13 15 ;
+trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" HUP INT PIPE TERM ;
: ${TMPDIR=/tmp} ;
{ tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } ||
{ test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } ||
@@ -161,6 +162,7 @@ case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
arm*) machine=arm-unknown ;;
sh3el) machine=shl-unknown ;;
sh3eb) machine=sh-unknown ;;
+ sh5el) machine=sh5le-unknown ;;
*) machine=${UNAME_MACHINE_ARCH}-unknown ;;
esac
# The Operating System including object format, if it has switched
@@ -169,7 +171,7 @@ case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
arm*|i386|m68k|ns32k|sh3*|sparc|vax)
eval $set_cc_for_build
if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \
- | grep __ELF__ >/dev/null
+ | grep -q __ELF__
then
# Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout).
# Return netbsd for either. FIX?
@@ -323,14 +325,33 @@ case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
case `/usr/bin/uname -p` in
sparc) echo sparc-icl-nx7; exit ;;
esac ;;
+ s390x:SunOS:*:*)
+ echo ${UNAME_MACHINE}-ibm-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ exit ;;
sun4H:SunOS:5.*:*)
echo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
exit ;;
sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*)
echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
exit ;;
- i86pc:SunOS:5.*:*)
- echo i386-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
+ i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*)
+ echo i386-pc-auroraux${UNAME_RELEASE}
+ exit ;;
+ i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*)
+ eval $set_cc_for_build
+ SUN_ARCH="i386"
+ # If there is a compiler, see if it is configured for 64-bit objects.
+ # Note that the Sun cc does not turn __LP64__ into 1 like gcc does.
+ # This test works for both compilers.
+ if [ "$CC_FOR_BUILD" != 'no_compiler_found' ]; then
+ if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \
+ (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | \
+ grep IS_64BIT_ARCH >/dev/null
+ then
+ SUN_ARCH="x86_64"
+ fi
+ fi
+ echo ${SUN_ARCH}-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`
exit ;;
sun4*:SunOS:6*:*)
# According to config.sub, this is the proper way to canonicalize
@@ -531,7 +552,7 @@ EOF
echo rs6000-ibm-aix3.2
fi
exit ;;
- *:AIX:*:[45])
+ *:AIX:*:[4567])
IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'`
if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then
IBM_ARCH=rs6000
@@ -639,7 +660,7 @@ EOF
# => hppa64-hp-hpux11.23
if echo __LP64__ | (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) |
- grep __LP64__ >/dev/null
+ grep -q __LP64__
then
HP_ARCH="hppa2.0w"
else
@@ -780,7 +801,7 @@ EOF
i*:CYGWIN*:*)
echo ${UNAME_MACHINE}-pc-cygwin
exit ;;
- i*:MINGW*:*)
+ *:MINGW*:*)
echo ${UNAME_MACHINE}-pc-mingw32
exit ;;
i*:windows32*:*)
@@ -790,15 +811,24 @@ EOF
i*:PW*:*)
echo ${UNAME_MACHINE}-pc-pw32
exit ;;
- x86:Interix*:[3456]*)
- echo i586-pc-interix${UNAME_RELEASE}
- exit ;;
- EM64T:Interix*:[3456]*)
- echo x86_64-unknown-interix${UNAME_RELEASE}
- exit ;;
+ *:Interix*:*)
+ case ${UNAME_MACHINE} in
+ x86)
+ echo i586-pc-interix${UNAME_RELEASE}
+ exit ;;
+ authenticamd | genuineintel | EM64T)
+ echo x86_64-unknown-interix${UNAME_RELEASE}
+ exit ;;
+ IA64)
+ echo ia64-unknown-interix${UNAME_RELEASE}
+ exit ;;
+ esac ;;
[345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*)
echo i${UNAME_MACHINE}-pc-mks
exit ;;
+ 8664:Windows_NT:*)
+ echo x86_64-pc-mks
+ exit ;;
i*:Windows_NT*:* | Pentium*:Windows_NT*:*)
# How do we know it's Interix rather than the generic POSIX subsystem?
# It also conflicts with pre-2.0 versions of AT&T UWIN. Should we
@@ -828,8 +858,29 @@ EOF
i*86:Minix:*:*)
echo ${UNAME_MACHINE}-pc-minix
exit ;;
+ alpha:Linux:*:*)
+ case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in
+ EV5) UNAME_MACHINE=alphaev5 ;;
+ EV56) UNAME_MACHINE=alphaev56 ;;
+ PCA56) UNAME_MACHINE=alphapca56 ;;
+ PCA57) UNAME_MACHINE=alphapca56 ;;
+ EV6) UNAME_MACHINE=alphaev6 ;;
+ EV67) UNAME_MACHINE=alphaev67 ;;
+ EV68*) UNAME_MACHINE=alphaev68 ;;
+ esac
+ objdump --private-headers /bin/sh | grep -q ld.so.1
+ if test "$?" = 0 ; then LIBC="libc1" ; else LIBC="" ; fi
+ echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC}
+ exit ;;
arm*:Linux:*:*)
- echo ${UNAME_MACHINE}-unknown-linux-gnu
+ eval $set_cc_for_build
+ if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \
+ | grep -q __ARM_EABI__
+ then
+ echo ${UNAME_MACHINE}-unknown-linux-gnu
+ else
+ echo ${UNAME_MACHINE}-unknown-linux-gnueabi
+ fi
exit ;;
avr32*:Linux:*:*)
echo ${UNAME_MACHINE}-unknown-linux-gnu
@@ -843,6 +894,17 @@ EOF
frv:Linux:*:*)
echo frv-unknown-linux-gnu
exit ;;
+ i*86:Linux:*:*)
+ LIBC=gnu
+ eval $set_cc_for_build
+ sed 's/^ //' << EOF >$dummy.c
+ #ifdef __dietlibc__
+ LIBC=dietlibc
+ #endif
+EOF
+ eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC'`
+ echo "${UNAME_MACHINE}-pc-linux-${LIBC}"
+ exit ;;
ia64:Linux:*:*)
echo ${UNAME_MACHINE}-unknown-linux-gnu
exit ;;
@@ -852,74 +914,33 @@ EOF
m68*:Linux:*:*)
echo ${UNAME_MACHINE}-unknown-linux-gnu
exit ;;
- mips:Linux:*:*)
- eval $set_cc_for_build
- sed 's/^ //' << EOF >$dummy.c
- #undef CPU
- #undef mips
- #undef mipsel
- #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)
- CPU=mipsel
- #else
- #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)
- CPU=mips
- #else
- CPU=
- #endif
- #endif
-EOF
- eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n '
- /^CPU/{
- s: ::g
- p
- }'`"
- test x"${CPU}" != x && { echo "${CPU}-unknown-linux-gnu"; exit; }
- ;;
- mips64:Linux:*:*)
+ mips:Linux:*:* | mips64:Linux:*:*)
eval $set_cc_for_build
sed 's/^ //' << EOF >$dummy.c
#undef CPU
- #undef mips64
- #undef mips64el
+ #undef ${UNAME_MACHINE}
+ #undef ${UNAME_MACHINE}el
#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)
- CPU=mips64el
+ CPU=${UNAME_MACHINE}el
#else
#if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)
- CPU=mips64
+ CPU=${UNAME_MACHINE}
#else
CPU=
#endif
#endif
EOF
- eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n '
- /^CPU/{
- s: ::g
- p
- }'`"
+ eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^CPU'`
test x"${CPU}" != x && { echo "${CPU}-unknown-linux-gnu"; exit; }
;;
or32:Linux:*:*)
echo or32-unknown-linux-gnu
exit ;;
- ppc:Linux:*:*)
- echo powerpc-unknown-linux-gnu
+ padre:Linux:*:*)
+ echo sparc-unknown-linux-gnu
exit ;;
- ppc64:Linux:*:*)
- echo powerpc64-unknown-linux-gnu
- exit ;;
- alpha:Linux:*:*)
- case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in
- EV5) UNAME_MACHINE=alphaev5 ;;
- EV56) UNAME_MACHINE=alphaev56 ;;
- PCA56) UNAME_MACHINE=alphapca56 ;;
- PCA57) UNAME_MACHINE=alphapca56 ;;
- EV6) UNAME_MACHINE=alphaev6 ;;
- EV67) UNAME_MACHINE=alphaev67 ;;
- EV68*) UNAME_MACHINE=alphaev68 ;;
- esac
- objdump --private-headers /bin/sh | grep ld.so.1 >/dev/null
- if test "$?" = 0 ; then LIBC="libc1" ; else LIBC="" ; fi
- echo ${UNAME_MACHINE}-unknown-linux-gnu${LIBC}
+ parisc64:Linux:*:* | hppa64:Linux:*:*)
+ echo hppa64-unknown-linux-gnu
exit ;;
parisc:Linux:*:* | hppa:Linux:*:*)
# Look for CPU level
@@ -929,8 +950,11 @@ EOF
*) echo hppa-unknown-linux-gnu ;;
esac
exit ;;
- parisc64:Linux:*:* | hppa64:Linux:*:*)
- echo hppa64-unknown-linux-gnu
+ ppc64:Linux:*:*)
+ echo powerpc64-unknown-linux-gnu
+ exit ;;
+ ppc:Linux:*:*)
+ echo powerpc-unknown-linux-gnu
exit ;;
s390:Linux:*:* | s390x:Linux:*:*)
echo ${UNAME_MACHINE}-ibm-linux
@@ -944,75 +968,18 @@ EOF
sparc:Linux:*:* | sparc64:Linux:*:*)
echo ${UNAME_MACHINE}-unknown-linux-gnu
exit ;;
+ tile*:Linux:*:*)
+ echo ${UNAME_MACHINE}-tilera-linux-gnu
+ exit ;;
vax:Linux:*:*)
echo ${UNAME_MACHINE}-dec-linux-gnu
exit ;;
x86_64:Linux:*:*)
echo x86_64-unknown-linux-gnu
exit ;;
- i*86:Linux:*:*)
- # The BFD linker knows what the default object file format is, so
- # first see if it will tell us. cd to the root directory to prevent
- # problems with other programs or directories called `ld' in the path.
- # Set LC_ALL=C to ensure ld outputs messages in English.
- ld_supported_targets=`cd /; LC_ALL=C ld --help 2>&1 \
- | sed -ne '/supported targets:/!d
- s/[ ][ ]*/ /g
- s/.*supported targets: *//
- s/ .*//
- p'`
- case "$ld_supported_targets" in
- elf32-i386)
- TENTATIVE="${UNAME_MACHINE}-pc-linux-gnu"
- ;;
- a.out-i386-linux)
- echo "${UNAME_MACHINE}-pc-linux-gnuaout"
- exit ;;
- coff-i386)
- echo "${UNAME_MACHINE}-pc-linux-gnucoff"
- exit ;;
- "")
- # Either a pre-BFD a.out linker (linux-gnuoldld) or
- # one that does not give us useful --help.
- echo "${UNAME_MACHINE}-pc-linux-gnuoldld"
- exit ;;
- esac
- # Determine whether the default compiler is a.out or elf
- eval $set_cc_for_build
- sed 's/^ //' << EOF >$dummy.c
- #include <features.h>
- #ifdef __ELF__
- # ifdef __GLIBC__
- # if __GLIBC__ >= 2
- LIBC=gnu
- # else
- LIBC=gnulibc1
- # endif
- # else
- LIBC=gnulibc1
- # endif
- #else
- #if defined(__INTEL_COMPILER) || defined(__PGI) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)
- LIBC=gnu
- #else
- LIBC=gnuaout
- #endif
- #endif
- #ifdef __dietlibc__
- LIBC=dietlibc
- #endif
-EOF
- eval "`$CC_FOR_BUILD -E $dummy.c 2>/dev/null | sed -n '
- /^LIBC/{
- s: ::g
- p
- }'`"
- test x"${LIBC}" != x && {
- echo "${UNAME_MACHINE}-pc-linux-${LIBC}"
- exit
- }
- test x"${TENTATIVE}" != x && { echo "${TENTATIVE}"; exit; }
- ;;
+ xtensa*:Linux:*:*)
+ echo ${UNAME_MACHINE}-unknown-linux-gnu
+ exit ;;
i*86:DYNIX/ptx:4*:*)
# ptx 4.0 does uname -s correctly, with DYNIX/ptx in there.
# earlier versions are messed up and put the nodename in both
@@ -1041,7 +1008,7 @@ EOF
i*86:syllable:*:*)
echo ${UNAME_MACHINE}-pc-syllable
exit ;;
- i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.0*:*)
+ i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*)
echo i386-unknown-lynxos${UNAME_RELEASE}
exit ;;
i*86:*DOS:*:*)
@@ -1085,8 +1052,11 @@ EOF
pc:*:*:*)
# Left here for compatibility:
# uname -m prints for DJGPP always 'pc', but it prints nothing about
- # the processor, so we play safe by assuming i386.
- echo i386-pc-msdosdjgpp
+ # the processor, so we play safe by assuming i586.
+ # Note: whatever this is, it MUST be the same as what config.sub
+ # prints for the "djgpp" host, or else GDB configury will decide that
+ # this is a cross-build.
+ echo i586-pc-msdosdjgpp
exit ;;
Intel:Mach:3*:*)
echo i386-pc-mach3
@@ -1124,6 +1094,16 @@ EOF
3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*)
/bin/uname -p 2>/dev/null | grep 86 >/dev/null \
&& { echo i486-ncr-sysv4; exit; } ;;
+ NCR*:*:4.2:* | MPRAS*:*:4.2:*)
+ OS_REL='.3'
+ test -r /etc/.relid \
+ && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid`
+ /bin/uname -p 2>/dev/null | grep 86 >/dev/null \
+ && { echo i486-ncr-sysv4.3${OS_REL}; exit; }
+ /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \
+ && { echo i586-ncr-sysv4.3${OS_REL}; exit; }
+ /bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \
+ && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;
m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*)
echo m68k-unknown-lynxos${UNAME_RELEASE}
exit ;;
@@ -1136,7 +1116,7 @@ EOF
rs6000:LynxOS:2.*:*)
echo rs6000-unknown-lynxos${UNAME_RELEASE}
exit ;;
- PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.0*:*)
+ PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*)
echo powerpc-unknown-lynxos${UNAME_RELEASE}
exit ;;
SM[BE]S:UNIX_SV:*:*)
@@ -1199,6 +1179,9 @@ EOF
BePC:BeOS:*:*) # BeOS running on Intel PC compatible.
echo i586-pc-beos
exit ;;
+ BePC:Haiku:*:*) # Haiku running on Intel PC compatible.
+ echo i586-pc-haiku
+ exit ;;
SX-4:SUPER-UX:*:*)
echo sx4-nec-superux${UNAME_RELEASE}
exit ;;
@@ -1208,6 +1191,15 @@ EOF
SX-6:SUPER-UX:*:*)
echo sx6-nec-superux${UNAME_RELEASE}
exit ;;
+ SX-7:SUPER-UX:*:*)
+ echo sx7-nec-superux${UNAME_RELEASE}
+ exit ;;
+ SX-8:SUPER-UX:*:*)
+ echo sx8-nec-superux${UNAME_RELEASE}
+ exit ;;
+ SX-8R:SUPER-UX:*:*)
+ echo sx8r-nec-superux${UNAME_RELEASE}
+ exit ;;
Power*:Rhapsody:*:*)
echo powerpc-apple-rhapsody${UNAME_RELEASE}
exit ;;
@@ -1217,6 +1209,16 @@ EOF
*:Darwin:*:*)
UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown
case $UNAME_PROCESSOR in
+ i386)
+ eval $set_cc_for_build
+ if [ "$CC_FOR_BUILD" != 'no_compiler_found' ]; then
+ if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \
+ (CCOPTS= $CC_FOR_BUILD -E - 2>/dev/null) | \
+ grep IS_64BIT_ARCH >/dev/null
+ then
+ UNAME_PROCESSOR="x86_64"
+ fi
+ fi ;;
unknown) UNAME_PROCESSOR=powerpc ;;
esac
echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}
@@ -1232,6 +1234,9 @@ EOF
*:QNX:*:4*)
echo i386-pc-qnx
exit ;;
+ NEO-?:NONSTOP_KERNEL:*:*)
+ echo neo-tandem-nsk${UNAME_RELEASE}
+ exit ;;
NSE-?:NONSTOP_KERNEL:*:*)
echo nse-tandem-nsk${UNAME_RELEASE}
exit ;;
@@ -1298,6 +1303,9 @@ EOF
i*86:rdos:*:*)
echo ${UNAME_MACHINE}-pc-rdos
exit ;;
+ i*86:AROS:*:*)
+ echo ${UNAME_MACHINE}-pc-aros
+ exit ;;
esac
#echo '(No uname command or uname output not recognized.)' 1>&2
@@ -1458,9 +1466,9 @@ This script, last modified $timestamp, has failed to recognize
the operating system you are using. It is advised that you
download the most up to date version of the config scripts from
- http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.guess
+ http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD
and
- http://savannah.gnu.org/cgi-bin/viewcvs/*checkout*/config/config/config.sub
+ http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD
If the version you run ($0) is already up to date, please
send the following data and any information you think might be
diff --git a/config.sub b/autosetup/config.sub
index fab0aa3..320e303 100755
--- a/config.sub
+++ b/autosetup/config.sub
@@ -1,10 +1,10 @@
#! /bin/sh
# Configuration validation subroutine script.
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-# 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
-# Inc.
+# 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+# Free Software Foundation, Inc.
-timestamp='2006-09-20'
+timestamp='2010-09-11'
# This file is (in principle) common to ALL GNU software.
# The presence of a machine in this file suggests that SOME GNU software
@@ -32,13 +32,16 @@ timestamp='2006-09-20'
# Please send patches to <config-patches@gnu.org>. Submit a context
-# diff and a properly formatted ChangeLog entry.
+# diff and a properly formatted GNU ChangeLog entry.
#
# Configuration subroutine to validate and canonicalize a configuration type.
# Supply the specified configuration type as an argument.
# If it is invalid, we print an error message on stderr and exit with code 1.
# Otherwise, we print the canonical config type on stdout and succeed.
+# You can get the latest version of this script from:
+# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD
+
# This file is supposed to be the same for all GNU packages
# and recognize all the CPU types, system types and aliases
# that are meaningful with *any* GNU software.
@@ -72,8 +75,9 @@ Report bugs and patches to <config-patches@gnu.org>."
version="\
GNU config.sub ($timestamp)
-Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
-Free Software Foundation, Inc.
+Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free
+Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
@@ -120,8 +124,10 @@ esac
# Here we must recognize all the valid KERNEL-OS combinations.
maybe_os=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\2/'`
case $maybe_os in
- nto-qnx* | linux-gnu* | linux-dietlibc | linux-newlib* | linux-uclibc* | \
- uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | knetbsd*-gnu* | netbsd*-gnu* | \
+ nto-qnx* | linux-gnu* | linux-android* | linux-dietlibc | linux-newlib* | \
+ linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \
+ knetbsd*-gnu* | netbsd*-gnu* | \
+ kopensolaris*-gnu* | \
storm-chaos* | os2-emx* | rtmk-nova*)
os=-$maybe_os
basic_machine=`echo $1 | sed 's/^\(.*\)-\([^-]*-[^-]*\)$/\1/'`
@@ -148,10 +154,13 @@ case $os in
-convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\
-c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \
-harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \
- -apple | -axis | -knuth | -cray)
+ -apple | -axis | -knuth | -cray | -microblaze)
os=
basic_machine=$1
;;
+ -bluegene*)
+ os=-cnk
+ ;;
-sim | -cisco | -oki | -wec | -winbond)
os=
basic_machine=$1
@@ -245,17 +254,20 @@ case $basic_machine in
| bfin \
| c4x | clipper \
| d10v | d30v | dlx | dsp16xx \
- | fr30 | frv \
+ | fido | fr30 | frv \
| h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \
| i370 | i860 | i960 | ia64 \
| ip2k | iq2000 \
+ | lm32 \
| m32c | m32r | m32rle | m68000 | m68k | m88k \
- | maxq | mb | microblaze | mcore \
+ | maxq | mb | microblaze | mcore | mep | metag \
| mips | mipsbe | mipseb | mipsel | mipsle \
| mips16 \
| mips64 | mips64el \
- | mips64vr | mips64vrel \
+ | mips64octeon | mips64octeonel \
| mips64orion | mips64orionel \
+ | mips64r5900 | mips64r5900el \
+ | mips64vr | mips64vrel \
| mips64vr4100 | mips64vr4100el \
| mips64vr4300 | mips64vr4300el \
| mips64vr5000 | mips64vr5000el \
@@ -268,28 +280,41 @@ case $basic_machine in
| mipsisa64sr71k | mipsisa64sr71kel \
| mipstx39 | mipstx39el \
| mn10200 | mn10300 \
+ | moxie \
| mt \
| msp430 \
+ | nds32 | nds32le | nds32be \
| nios | nios2 \
| ns16k | ns32k \
| or32 \
| pdp10 | pdp11 | pj | pjl \
| powerpc | powerpc64 | powerpc64le | powerpcle | ppcbe \
| pyramid \
+ | rx \
| score \
- | sh | sh[1234] | sh[24]a | sh[23]e | sh[34]eb | sheb | shbe | shle | sh[1234]le | sh3ele \
+ | sh | sh[1234] | sh[24]a | sh[24]aeb | sh[23]e | sh[34]eb | sheb | shbe | shle | sh[1234]le | sh3ele \
| sh64 | sh64le \
| sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \
| sparcv8 | sparcv9 | sparcv9b | sparcv9v \
| spu | strongarm \
- | tahoe | thumb | tic4x | tic80 | tron \
+ | tahoe | thumb | tic4x | tic54x | tic55x | tic6x | tic80 | tron \
+ | ubicom32 \
| v850 | v850e \
| we32k \
| x86 | xc16x | xscale | xscalee[bl] | xstormy16 | xtensa \
- | z8k)
+ | z8k | z80)
basic_machine=$basic_machine-unknown
;;
- m6811 | m68hc11 | m6812 | m68hc12)
+ c54x)
+ basic_machine=tic54x-unknown
+ ;;
+ c55x)
+ basic_machine=tic55x-unknown
+ ;;
+ c6x)
+ basic_machine=tic6x-unknown
+ ;;
+ m6811 | m68hc11 | m6812 | m68hc12 | picochip)
# Motorola 68HC11/12.
basic_machine=$basic_machine-unknown
os=-none
@@ -320,23 +345,26 @@ case $basic_machine in
| arm-* | armbe-* | armle-* | armeb-* | armv*-* \
| avr-* | avr32-* \
| bfin-* | bs2000-* \
- | c[123]* | c30-* | [cjt]90-* | c4x-* | c54x-* | c55x-* | c6x-* \
+ | c[123]* | c30-* | [cjt]90-* | c4x-* \
| clipper-* | craynv-* | cydra-* \
| d10v-* | d30v-* | dlx-* \
| elxsi-* \
- | f30[01]-* | f700-* | fr30-* | frv-* | fx80-* \
+ | f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \
| h8300-* | h8500-* \
| hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \
| i*86-* | i860-* | i960-* | ia64-* \
| ip2k-* | iq2000-* \
+ | lm32-* \
| m32c-* | m32r-* | m32rle-* \
| m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \
- | m88110-* | m88k-* | maxq-* | mcore-* \
+ | m88110-* | m88k-* | maxq-* | mcore-* | metag-* | microblaze-* \
| mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \
| mips16-* \
| mips64-* | mips64el-* \
- | mips64vr-* | mips64vrel-* \
+ | mips64octeon-* | mips64octeonel-* \
| mips64orion-* | mips64orionel-* \
+ | mips64r5900-* | mips64r5900el-* \
+ | mips64vr-* | mips64vrel-* \
| mips64vr4100-* | mips64vr4100el-* \
| mips64vr4300-* | mips64vr4300el-* \
| mips64vr5000-* | mips64vr5000el-* \
@@ -351,27 +379,34 @@ case $basic_machine in
| mmix-* \
| mt-* \
| msp430-* \
+ | nds32-* | nds32le-* | nds32be-* \
| nios-* | nios2-* \
| none-* | np1-* | ns16k-* | ns32k-* \
| orion-* \
| pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \
| powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* | ppcbe-* \
| pyramid-* \
- | romp-* | rs6000-* \
- | sh-* | sh[1234]-* | sh[24]a-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \
+ | romp-* | rs6000-* | rx-* \
+ | sh-* | sh[1234]-* | sh[24]a-* | sh[24]aeb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \
| shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \
| sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \
| sparclite-* \
| sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | strongarm-* | sv1-* | sx?-* \
| tahoe-* | thumb-* \
| tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \
+ | tile-* | tilegx-* \
| tron-* \
+ | ubicom32-* \
| v850-* | v850e-* | vax-* \
| we32k-* \
| x86-* | x86_64-* | xc16x-* | xps100-* | xscale-* | xscalee[bl]-* \
- | xstormy16-* | xtensa-* \
+ | xstormy16-* | xtensa*-* \
| ymp-* \
- | z8k-*)
+ | z8k-* | z80-*)
+ ;;
+ # Recognize the basic CPU types without company name, with glob match.
+ xtensa*)
+ basic_machine=$basic_machine-unknown
;;
# Recognize the various machine names and aliases which stand
# for a CPU type and a company and sometimes even an OS.
@@ -435,6 +470,10 @@ case $basic_machine in
basic_machine=m68k-apollo
os=-bsd
;;
+ aros)
+ basic_machine=i386-pc
+ os=-aros
+ ;;
aux)
basic_machine=m68k-apple
os=-aux
@@ -443,10 +482,35 @@ case $basic_machine in
basic_machine=ns32k-sequent
os=-dynix
;;
+ blackfin)
+ basic_machine=bfin-unknown
+ os=-linux
+ ;;
+ blackfin-*)
+ basic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'`
+ os=-linux
+ ;;
+ bluegene*)
+ basic_machine=powerpc-ibm
+ os=-cnk
+ ;;
+ c54x-*)
+ basic_machine=tic54x-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ c55x-*)
+ basic_machine=tic55x-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
+ c6x-*)
+ basic_machine=tic6x-`echo $basic_machine | sed 's/^[^-]*-//'`
+ ;;
c90)
basic_machine=c90-cray
os=-unicos
;;
+ cegcc)
+ basic_machine=arm-unknown
+ os=-cegcc
+ ;;
convex-c1)
basic_machine=c1-convex
os=-bsd
@@ -475,8 +539,8 @@ case $basic_machine in
basic_machine=craynv-cray
os=-unicosmp
;;
- cr16c)
- basic_machine=cr16c-unknown
+ cr16)
+ basic_machine=cr16-unknown
os=-elf
;;
crds | unos)
@@ -514,6 +578,10 @@ case $basic_machine in
basic_machine=m88k-motorola
os=-sysv3
;;
+ dicos)
+ basic_machine=i686-pc
+ os=-dicos
+ ;;
djgpp)
basic_machine=i586-pc
os=-msdosdjgpp
@@ -668,6 +736,14 @@ case $basic_machine in
basic_machine=m68k-isi
os=-sysv
;;
+ m68knommu)
+ basic_machine=m68k-unknown
+ os=-linux
+ ;;
+ m68knommu-*)
+ basic_machine=m68k-`echo $basic_machine | sed 's/^[^-]*-//'`
+ os=-linux
+ ;;
m88k-omron*)
basic_machine=m88k-omron
;;
@@ -679,10 +755,17 @@ case $basic_machine in
basic_machine=ns32k-utek
os=-sysv
;;
+ microblaze)
+ basic_machine=microblaze-xilinx
+ ;;
mingw32)
basic_machine=i386-pc
os=-mingw32
;;
+ mingw32ce)
+ basic_machine=arm-unknown
+ os=-mingw32ce
+ ;;
miniframe)
basic_machine=m68000-convergent
;;
@@ -779,6 +862,12 @@ case $basic_machine in
np1)
basic_machine=np1-gould
;;
+ neo-tandem)
+ basic_machine=neo-tandem
+ ;;
+ nse-tandem)
+ basic_machine=nse-tandem
+ ;;
nsr-tandem)
basic_machine=nsr-tandem
;;
@@ -809,6 +898,14 @@ case $basic_machine in
basic_machine=i860-intel
os=-osf
;;
+ parisc)
+ basic_machine=hppa-unknown
+ os=-linux
+ ;;
+ parisc-*)
+ basic_machine=hppa-`echo $basic_machine | sed 's/^[^-]*-//'`
+ os=-linux
+ ;;
pbd)
basic_machine=sparc-tti
;;
@@ -925,6 +1022,9 @@ case $basic_machine in
basic_machine=sh-hitachi
os=-hms
;;
+ sh5el)
+ basic_machine=sh5le-unknown
+ ;;
sh64)
basic_machine=sh64-unknown
;;
@@ -1002,17 +1102,14 @@ case $basic_machine in
basic_machine=t90-cray
os=-unicos
;;
- tic54x | c54x*)
- basic_machine=tic54x-unknown
- os=-coff
+ # This must be matched before tile*.
+ tilegx*)
+ basic_machine=tilegx-unknown
+ os=-linux-gnu
;;
- tic55x | c55x*)
- basic_machine=tic55x-unknown
- os=-coff
- ;;
- tic6x | c6x*)
- basic_machine=tic6x-unknown
- os=-coff
+ tile*)
+ basic_machine=tile-unknown
+ os=-linux-gnu
;;
tx39)
basic_machine=mipstx39-unknown
@@ -1089,6 +1186,10 @@ case $basic_machine in
basic_machine=z8k-unknown
os=-sim
;;
+ z80-*-coff)
+ basic_machine=z80-unknown
+ os=-sim
+ ;;
none)
basic_machine=none-none
os=-none
@@ -1127,7 +1228,7 @@ case $basic_machine in
we32k)
basic_machine=we32k-att
;;
- sh[1234] | sh[24]a | sh[34]eb | sh[1234]le | sh[23]ele)
+ sh[1234] | sh[24]a | sh[24]aeb | sh[34]eb | sh[1234]le | sh[23]ele)
basic_machine=sh-unknown
;;
sparc | sparcv8 | sparcv9 | sparcv9b | sparcv9v)
@@ -1177,6 +1278,9 @@ case $os in
# First match some system type aliases
# that might get confused with valid system types.
# -solaris* is a basic system type, with this one exception.
+ -auroraux)
+ os=-auroraux
+ ;;
-solaris1 | -solaris1.*)
os=`echo $os | sed -e 's|solaris1|sunos4|'`
;;
@@ -1197,10 +1301,11 @@ case $os in
# Each alternative MUST END IN A *, to match a version number.
# -sysv* is not here because it comes later, after sysvr4.
-gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \
- | -*vms* | -sco* | -esix* | -isc* | -aix* | -sunos | -sunos[34]*\
- | -hpux* | -unos* | -osf* | -luna* | -dgux* | -solaris* | -sym* \
+ | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\
+ | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \
+ | -sym* | -kopensolaris* \
| -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \
- | -aos* \
+ | -aos* | -aros* \
| -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \
| -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \
| -hiux* | -386bsd* | -knetbsd* | -mirbsd* | -netbsd* \
@@ -1209,9 +1314,10 @@ case $os in
| -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \
| -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \
| -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \
- | -chorusos* | -chorusrdb* \
+ | -chorusos* | -chorusrdb* | -cegcc* \
| -cygwin* | -pe* | -psos* | -moss* | -proelf* | -rtems* \
- | -mingw32* | -linux-gnu* | -linux-newlib* | -linux-uclibc* \
+ | -mingw32* | -linux-gnu* | -linux-android* \
+ | -linux-newlib* | -linux-uclibc* \
| -uxpv* | -beos* | -mpeix* | -udk* \
| -interix* | -uwin* | -mks* | -rhapsody* | -darwin* | -opened* \
| -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \
@@ -1219,7 +1325,7 @@ case $os in
| -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \
| -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \
| -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \
- | -skyos* | -haiku* | -rdos* | -toppers*)
+ | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es*)
# Remember, each alternative MUST END IN *, to match a version number.
;;
-qnx*)
@@ -1349,6 +1455,11 @@ case $os in
-zvmoe)
os=-zvmoe
;;
+ -dicos*)
+ os=-dicos
+ ;;
+ -nacl*)
+ ;;
-none)
;;
*)
@@ -1389,6 +1500,15 @@ case $basic_machine in
c4x-* | tic4x-*)
os=-coff
;;
+ tic54x-*)
+ os=-coff
+ ;;
+ tic55x-*)
+ os=-coff
+ ;;
+ tic6x-*)
+ os=-coff
+ ;;
# This must come before the *-dec entry.
pdp10-*)
os=-tops20
@@ -1414,6 +1534,9 @@ case $basic_machine in
m68*-cisco)
os=-aout
;;
+ mep-*)
+ os=-elf
+ ;;
mips*-cisco)
os=-elf
;;
@@ -1543,7 +1666,7 @@ case $basic_machine in
-sunos*)
vendor=sun
;;
- -aix*)
+ -cnk*|-aix*)
vendor=ibm
;;
-beos*)
diff --git a/autosetup/find-tclsh b/autosetup/find-tclsh
new file mode 100755
index 0000000..ef056a2
--- /dev/null
+++ b/autosetup/find-tclsh
@@ -0,0 +1,11 @@
+#!/bin/sh
+# Looks for a suitable tclsh or jimsh in the PATH
+# If not found, builds a bootstrap jimsh from source
+PATH="$PATH:$(pwd)"
+d=$(dirname "$0")
+for tclsh in jimsh tclsh8.5 tclsh8.6 jimsh0; do
+ $tclsh "$d/test-tclsh" 2>/dev/null && exit 0
+done
+echo 1>&2 "No installed jimsh or tclsh, building local bootstrap jimsh0"
+${CC_FOR_BUILD:-cc} -o jimsh0 "$d/jimsh0.c" || exit 1
+$(pwd)/jimsh0 "$d/test-tclsh"
diff --git a/autosetup/jimsh0.c b/autosetup/jimsh0.c
new file mode 100644
index 0000000..6cc216c
--- /dev/null
+++ b/autosetup/jimsh0.c
@@ -0,0 +1,23539 @@
+/* This is single source file, bootstrap version of Jim Tcl. See http://jim.berlios.de/ */
+#define _GNU_SOURCE
+#define JIM_TCL_COMPAT
+#define JIM_REFERENCES
+#define JIM_ANSIC
+#define HAVE_VFORK
+#define JIM_REGEXP
+#define HAVE_NO_AUTOCONF
+#define _JIMAUTOCONF_H
+#define TCL_LIBRARY "."
+#define jim_ext_bootstrap
+#define jim_ext_aio
+#define jim_ext_readdir
+#define jim_ext_glob
+#define jim_ext_regexp
+#define jim_ext_file
+#define jim_ext_exec
+#define jim_ext_clock
+#define jim_ext_array
+#define jim_ext_stdlib
+#define jim_ext_tclcompat
+#ifndef UTF8_UTIL_H
+#define UTF8_UTIL_H
+/**
+ * UTF-8 utility functions
+ *
+ * (c) 2010 Steve Bennett <steveb@workware.net.au>
+ *
+ * See LICENCE for licence details.
+ */
+
+/**
+ * Converts the given unicode codepoint (0 - 0xffff) to utf-8
+ * and stores the result at 'p'.
+ *
+ * Returns the number of utf-8 characters (1-3).
+ */
+int utf8_fromunicode(char *p, unsigned short uc);
+
+#ifndef JIM_UTF8
+#include <ctype.h>
+
+/* No utf-8 support. 1 byte = 1 char */
+#define utf8_strlen(S, B) (B) < 0 ? strlen(S) : (B)
+#define utf8_tounicode(S, CP) (*(CP) = *(S), 1)
+#define utf8_upper(C) toupper(C)
+#define utf8_lower(C) tolower(C)
+#define utf8_index(C, I) (I)
+#define utf8_charlen(C) 1
+#define utf8_prev_len(S, L) 1
+
+#else
+/**
+ * Returns the length of the utf-8 sequence starting with 'c'.
+ *
+ * Returns 1-4, or -1 if this is not a valid start byte.
+ *
+ * Note that charlen=4 is not supported by the rest of the API.
+ */
+int utf8_charlen(int c);
+
+/**
+ * Returns the number of characters in the utf-8
+ * string of the given byte length.
+ *
+ * Any bytes which are not part of an valid utf-8
+ * sequence are treated as individual characters.
+ *
+ * The string *must* be null terminated.
+ *
+ * Does not support unicode code points > \uffff
+ */
+int utf8_strlen(const char *str, int bytelen);
+
+/**
+ * Returns the byte index of the given character in the utf-8 string.
+ *
+ * The string *must* be null terminated.
+ *
+ * This will return the byte length of a utf-8 string
+ * if given the char length.
+ */
+int utf8_index(const char *str, int charindex);
+
+/**
+ * Returns the unicode codepoint corresponding to the
+ * utf-8 sequence 'str'.
+ *
+ * Stores the result in *uc and returns the number of bytes
+ * consumed.
+ *
+ * If 'str' is null terminated, then an invalid utf-8 sequence
+ * at the end of the string will be returned as individual bytes.
+ *
+ * If it is not null terminated, the length *must* be checked first.
+ *
+ * Does not support unicode code points > \uffff
+ */
+int utf8_tounicode(const char *str, int *uc);
+
+/**
+ * Returns the number of bytes before 'str' that the previous
+ * utf-8 character sequence starts (which may be the middle of a sequence).
+ *
+ * Looks back at most 'len' bytes backwards, which must be > 0.
+ * If no start char is found, returns -len
+ */
+int utf8_prev_len(const char *str, int len);
+
+/**
+ * Returns the upper-case variant of the given unicode codepoint.
+ *
+ * Does not support unicode code points > \uffff
+ */
+int utf8_upper(int uc);
+
+/**
+ * Returns the lower-case variant of the given unicode codepoint.
+ *
+ * NOTE: Use utf8_upper() in preference for case-insensitive matching.
+ *
+ * Does not support unicode code points > \uffff
+ */
+int utf8_lower(int uc);
+
+#endif
+
+#endif
+/* Jim - A small embeddable Tcl interpreter
+ *
+ * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
+ * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
+ * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
+ * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
+ * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
+ * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
+ * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ *--- Inline Header File Documentation ---
+ * [By Duane Ellis, openocd@duaneellis.com, 8/18/8]
+ *
+ * Belief is "Jim" would greatly benifit if Jim Internals where
+ * documented in some way - form whatever, and perhaps - the package:
+ * 'doxygen' is the correct approach to do that.
+ *
+ * Details, see: http://www.stack.nl/~dimitri/doxygen/
+ *
+ * To that end please follow these guide lines:
+ *
+ * (A) Document the PUBLIC api in the .H file.
+ *
+ * (B) Document JIM Internals, in the .C file.
+ *
+ * (C) Remember JIM is embedded in other packages, to that end do
+ * not assume that your way of documenting is the right way, Jim's
+ * public documentation should be agnostic, such that it is some
+ * what agreeable with the "package" that is embedding JIM inside
+ * of it's own doxygen documentation.
+ *
+ * (D) Use minimal Doxygen tags.
+ *
+ * This will be an "ongoing work in progress" for some time.
+ **/
+
+#ifndef __JIM__H
+#define __JIM__H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <time.h>
+#include <limits.h>
+#include <stdio.h> /* for the FILE typedef definition */
+#include <stdlib.h> /* In order to export the Jim_Free() macro */
+#include <stdarg.h> /* In order to get type va_list */
+
+/* -----------------------------------------------------------------------------
+ * System configuration
+ * autoconf (configure) will set these
+ * ---------------------------------------------------------------------------*/
+
+#ifndef HAVE_NO_AUTOCONF
+#endif
+
+/* -----------------------------------------------------------------------------
+ * Compiler specific fixes.
+ * ---------------------------------------------------------------------------*/
+
+/* Long Long type and related issues */
+#ifndef jim_wide
+# ifdef HAVE_LONG_LONG
+# define jim_wide long long
+# ifndef LLONG_MAX
+# define LLONG_MAX 9223372036854775807LL
+# endif
+# ifndef LLONG_MIN
+# define LLONG_MIN (-LLONG_MAX - 1LL)
+# endif
+# define JIM_WIDE_MIN LLONG_MIN
+# define JIM_WIDE_MAX LLONG_MAX
+# else
+# define jim_wide long
+# define JIM_WIDE_MIN LONG_MIN
+# define JIM_WIDE_MAX LONG_MAX
+# endif
+
+/* -----------------------------------------------------------------------------
+ * LIBC specific fixes
+ * ---------------------------------------------------------------------------*/
+
+# ifdef HAVE_LONG_LONG
+# define JIM_WIDE_MODIFIER "lld"
+# else
+# define JIM_WIDE_MODIFIER "ld"
+# define strtoull strtoul
+# endif
+#endif
+
+#define UCHAR(c) ((unsigned char)(c))
+
+/* -----------------------------------------------------------------------------
+ * Exported defines
+ * ---------------------------------------------------------------------------*/
+
+/* Jim version numbering: every version of jim is marked with a
+ * successive integer number. This is version 0. The first
+ * stable version will be 1, then 2, 3, and so on. */
+#define JIM_VERSION 71
+
+#define JIM_OK 0
+#define JIM_ERR 1
+#define JIM_RETURN 2
+#define JIM_BREAK 3
+#define JIM_CONTINUE 4
+#define JIM_SIGNAL 5
+#define JIM_EXIT 6
+/* The following are internal codes and should never been seen/used */
+#define JIM_EVAL 7
+
+#define JIM_MAX_NESTING_DEPTH 1000 /* default max nesting depth */
+
+/* Some function get an integer argument with flags to change
+ * the behaviour. */
+#define JIM_NONE 0 /* no flags set */
+#define JIM_ERRMSG 1 /* set an error message in the interpreter. */
+
+#define JIM_UNSHARED 4 /* Flag to Jim_GetVariable() */
+
+/* Flags for Jim_SubstObj() */
+#define JIM_SUBST_NOVAR 1 /* don't perform variables substitutions */
+#define JIM_SUBST_NOCMD 2 /* don't perform command substitutions */
+#define JIM_SUBST_NOESC 4 /* don't perform escapes substitutions */
+#define JIM_SUBST_FLAG 128 /* flag to indicate that this is a real substition object */
+
+/* Unused arguments generate annoying warnings... */
+#define JIM_NOTUSED(V) ((void) V)
+
+/* Flags for Jim_GetEnum() */
+#define JIM_ENUM_ABBREV 2 /* Allow unambiguous abbreviation */
+
+/* Flags used by API calls getting a 'nocase' argument. */
+#define JIM_CASESENS 0 /* case sensitive */
+#define JIM_NOCASE 1 /* no case */
+
+/* Filesystem related */
+#define JIM_PATH_LEN 1024
+
+/* Newline, some embedded system may need -DJIM_CRLF */
+#ifdef JIM_CRLF
+#define JIM_NL "\r\n"
+#else
+#define JIM_NL "\n"
+#endif
+
+#define JIM_LIBPATH "auto_path"
+#define JIM_INTERACTIVE "tcl_interactive"
+
+/* -----------------------------------------------------------------------------
+ * Stack
+ * ---------------------------------------------------------------------------*/
+
+typedef struct Jim_Stack {
+ int len;
+ int maxlen;
+ void **vector;
+} Jim_Stack;
+
+/* -----------------------------------------------------------------------------
+ * Hash table
+ * ---------------------------------------------------------------------------*/
+
+typedef struct Jim_HashEntry {
+ const void *key;
+ void *val;
+ struct Jim_HashEntry *next;
+} Jim_HashEntry;
+
+typedef struct Jim_HashTableType {
+ unsigned int (*hashFunction)(const void *key);
+ const void *(*keyDup)(void *privdata, const void *key);
+ void *(*valDup)(void *privdata, const void *obj);
+ int (*keyCompare)(void *privdata, const void *key1, const void *key2);
+ void (*keyDestructor)(void *privdata, const void *key);
+ void (*valDestructor)(void *privdata, void *obj);
+} Jim_HashTableType;
+
+typedef struct Jim_HashTable {
+ Jim_HashEntry **table;
+ const Jim_HashTableType *type;
+ unsigned int size;
+ unsigned int sizemask;
+ unsigned int used;
+ unsigned int collisions;
+ void *privdata;
+} Jim_HashTable;
+
+typedef struct Jim_HashTableIterator {
+ Jim_HashTable *ht;
+ int index;
+ Jim_HashEntry *entry, *nextEntry;
+} Jim_HashTableIterator;
+
+/* This is the initial size of every hash table */
+#define JIM_HT_INITIAL_SIZE 16
+
+/* ------------------------------- Macros ------------------------------------*/
+#define Jim_FreeEntryVal(ht, entry) \
+ if ((ht)->type->valDestructor) \
+ (ht)->type->valDestructor((ht)->privdata, (entry)->val)
+
+#define Jim_SetHashVal(ht, entry, _val_) do { \
+ if ((ht)->type->valDup) \
+ entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
+ else \
+ entry->val = (_val_); \
+} while(0)
+
+#define Jim_FreeEntryKey(ht, entry) \
+ if ((ht)->type->keyDestructor) \
+ (ht)->type->keyDestructor((ht)->privdata, (entry)->key)
+
+#define Jim_SetHashKey(ht, entry, _key_) do { \
+ if ((ht)->type->keyDup) \
+ entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
+ else \
+ entry->key = (_key_); \
+} while(0)
+
+#define Jim_CompareHashKeys(ht, key1, key2) \
+ (((ht)->type->keyCompare) ? \
+ (ht)->type->keyCompare((ht)->privdata, key1, key2) : \
+ (key1) == (key2))
+
+#define Jim_HashKey(ht, key) (ht)->type->hashFunction(key)
+
+#define Jim_GetHashEntryKey(he) ((he)->key)
+#define Jim_GetHashEntryVal(he) ((he)->val)
+#define Jim_GetHashTableCollisions(ht) ((ht)->collisions)
+#define Jim_GetHashTableSize(ht) ((ht)->size)
+#define Jim_GetHashTableUsed(ht) ((ht)->used)
+
+/* -----------------------------------------------------------------------------
+ * Jim_Obj structure
+ * ---------------------------------------------------------------------------*/
+
+/* -----------------------------------------------------------------------------
+ * Jim object. This is mostly the same as Tcl_Obj itself,
+ * with the addition of the 'prev' and 'next' pointers.
+ * In Jim all the objects are stored into a linked list for GC purposes,
+ * so that it's possible to access every object living in a given interpreter
+ * sequentially. When an object is freed, it's moved into a different
+ * linked list, used as object pool.
+ *
+ * The refcount of a freed object is always -1.
+ * ---------------------------------------------------------------------------*/
+typedef struct Jim_Obj {
+ int refCount; /* reference count */
+ char *bytes; /* string representation buffer. NULL = no string repr. */
+ int length; /* number of bytes in 'bytes', not including the numterm. */
+ const struct Jim_ObjType *typePtr; /* object type. */
+ /* Internal representation union */
+ union {
+ /* integer number type */
+ jim_wide wideValue;
+ /* hashed object type value */
+ int hashValue;
+ /* index type */
+ int indexValue;
+ /* return code type */
+ int returnCode;
+ /* double number type */
+ double doubleValue;
+ /* Generic pointer */
+ void *ptr;
+ /* Generic two pointers value */
+ struct {
+ void *ptr1;
+ void *ptr2;
+ } twoPtrValue;
+ /* Variable object */
+ struct {
+ unsigned jim_wide callFrameId;
+ struct Jim_Var *varPtr;
+ } varValue;
+ /* Command object */
+ struct {
+ unsigned jim_wide procEpoch;
+ struct Jim_Cmd *cmdPtr;
+ } cmdValue;
+ /* List object */
+ struct {
+ struct Jim_Obj **ele; /* Elements vector */
+ int len; /* Length */
+ int maxLen; /* Allocated 'ele' length */
+ } listValue;
+ /* String type */
+ struct {
+ int maxLength;
+ int charLength; /* utf-8 char length. -1 if unknown */
+ } strValue;
+ /* Reference type */
+ struct {
+ jim_wide id;
+ struct Jim_Reference *refPtr;
+ } refValue;
+ /* Source type */
+ struct {
+ const char *fileName;
+ int lineNumber;
+ } sourceValue;
+ /* Dict substitution type */
+ struct {
+ struct Jim_Obj *varNameObjPtr;
+ struct Jim_Obj *indexObjPtr;
+ } dictSubstValue;
+ /* tagged binary type */
+ struct {
+ unsigned char *data;
+ size_t len;
+ } binaryValue;
+ /* Regular expression pattern */
+ struct {
+ unsigned flags;
+ void *compre; /* really an allocated (regex_t *) */
+ } regexpValue;
+ struct {
+ int line;
+ int argc;
+ } scriptLineValue;
+ } internalRep;
+ /* This are 8 or 16 bytes more for every object
+ * but this is required for efficient garbage collection
+ * of Jim references. */
+ struct Jim_Obj *prevObjPtr; /* pointer to the prev object. */
+ struct Jim_Obj *nextObjPtr; /* pointer to the next object. */
+} Jim_Obj;
+
+/* Jim_Obj related macros */
+#define Jim_IncrRefCount(objPtr) \
+ ++(objPtr)->refCount
+#define Jim_DecrRefCount(interp, objPtr) \
+ if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr)
+#define Jim_IsShared(objPtr) \
+ ((objPtr)->refCount > 1)
+
+/* This macro is used when we allocate a new object using
+ * Jim_New...Obj(), but for some error we need to destroy it.
+ * Instead to use Jim_IncrRefCount() + Jim_DecrRefCount() we
+ * can just call Jim_FreeNewObj. To call Jim_Free directly
+ * seems too raw, the object handling may change and we want
+ * that Jim_FreeNewObj() can be called only against objects
+ * that are belived to have refcount == 0. */
+#define Jim_FreeNewObj Jim_FreeObj
+
+/* Free the internal representation of the object. */
+#define Jim_FreeIntRep(i,o) \
+ if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \
+ (o)->typePtr->freeIntRepProc(i, o)
+
+/* Get the internal representation pointer */
+#define Jim_GetIntRepPtr(o) (o)->internalRep.ptr
+
+/* Set the internal representation pointer */
+#define Jim_SetIntRepPtr(o, p) \
+ (o)->internalRep.ptr = (p)
+
+/* The object type structure.
+ * There are four methods.
+ *
+ * - FreeIntRep is used to free the internal representation of the object.
+ * Can be NULL if there is nothing to free.
+ * - DupIntRep is used to duplicate the internal representation of the object.
+ * If NULL, when an object is duplicated, the internalRep union is
+ * directly copied from an object to another.
+ * Note that it's up to the caller to free the old internal repr of the
+ * object before to call the Dup method.
+ * - UpdateString is used to create the string from the internal repr.
+ * - setFromAny is used to convert the current object into one of this type.
+ */
+
+struct Jim_Interp;
+
+typedef void (Jim_FreeInternalRepProc)(struct Jim_Interp *interp,
+ struct Jim_Obj *objPtr);
+typedef void (Jim_DupInternalRepProc)(struct Jim_Interp *interp,
+ struct Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+typedef void (Jim_UpdateStringProc)(struct Jim_Obj *objPtr);
+
+typedef struct Jim_ObjType {
+ const char *name; /* The name of the type. */
+ Jim_FreeInternalRepProc *freeIntRepProc;
+ Jim_DupInternalRepProc *dupIntRepProc;
+ Jim_UpdateStringProc *updateStringProc;
+ int flags;
+} Jim_ObjType;
+
+/* Jim_ObjType flags */
+#define JIM_TYPE_NONE 0 /* No flags */
+#define JIM_TYPE_REFERENCES 1 /* The object may contain referneces. */
+
+/* Starting from 1 << 20 flags are reserved for private uses of
+ * different calls. This way the same 'flags' argument may be used
+ * to pass both global flags and private flags. */
+#define JIM_PRIV_FLAG_SHIFT 20
+
+/* -----------------------------------------------------------------------------
+ * Call frame, vars, commands structures
+ * ---------------------------------------------------------------------------*/
+
+/* Call frame */
+typedef struct Jim_CallFrame {
+ unsigned jim_wide id; /* Call Frame ID. Used for caching. */
+ int level; /* Level of this call frame. 0 = global */
+ struct Jim_HashTable vars; /* Where local vars are stored */
+ struct Jim_HashTable *staticVars; /* pointer to procedure static vars */
+ struct Jim_CallFrame *parentCallFrame;
+ Jim_Obj *const *argv; /* object vector of the current procedure call. */
+ int argc; /* number of args of the current procedure call. */
+ Jim_Obj *procArgsObjPtr; /* arglist object of the running procedure */
+ Jim_Obj *procBodyObjPtr; /* body object of the running procedure */
+ struct Jim_CallFrame *nextFramePtr;
+ const char *filename; /* file and line of caller of this proc (if available) */
+ int line;
+} Jim_CallFrame;
+
+/* The var structure. It just holds the pointer of the referenced
+ * object. If linkFramePtr is not NULL the variable is a link
+ * to a variable of name store on objPtr living on the given callframe
+ * (this happens when the [global] or [upvar] command is used).
+ * The interp in order to always know how to free the Jim_Obj associated
+ * with a given variable because In Jim objects memory managment is
+ * bound to interpreters. */
+typedef struct Jim_Var {
+ Jim_Obj *objPtr;
+ struct Jim_CallFrame *linkFramePtr;
+} Jim_Var;
+
+/* The cmd structure. */
+typedef int (*Jim_CmdProc)(struct Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv);
+typedef void (*Jim_DelCmdProc)(struct Jim_Interp *interp, void *privData);
+
+/* A command is implemented in C if funcPtr is != NULL, otherwise
+ * it's a Tcl procedure with the arglist and body represented by the
+ * two objects referenced by arglistObjPtr and bodyoObjPtr. */
+typedef struct Jim_Cmd {
+ int inUse; /* Reference count */
+ int isproc; /* Is this a procedure? */
+ union {
+ struct {
+ /* native (C) command */
+ Jim_CmdProc cmdProc; /* The command implementation */
+ Jim_DelCmdProc delProc; /* Called when the command is deleted if != NULL */
+ void *privData; /* command-private data available via Jim_CmdPrivData() */
+ } native;
+ struct {
+ /* Tcl procedure */
+ Jim_Obj *argListObjPtr;
+ Jim_Obj *bodyObjPtr;
+ Jim_HashTable *staticVars; /* Static vars hash table. NULL if no statics. */
+ int leftArity; /* Required args assigned from the left */
+ int optionalArgs; /* Number of optional args (default values) */
+ int rightArity; /* Required args assigned from the right */
+ int args; /* True if 'args' specified */
+ struct Jim_Cmd *prevCmd; /* Previous command defn if proc created 'local' */
+ int upcall; /* True if proc is currently in upcall */
+ } proc;
+ } u;
+} Jim_Cmd;
+
+/* Pseudo Random Number Generator State structure */
+typedef struct Jim_PrngState {
+ unsigned char sbox[256];
+ unsigned int i, j;
+} Jim_PrngState;
+
+/* -----------------------------------------------------------------------------
+ * Jim interpreter structure.
+ * Fields similar to the real Tcl interpreter structure have the same names.
+ * ---------------------------------------------------------------------------*/
+typedef struct Jim_Interp {
+ Jim_Obj *result; /* object returned by the last command called. */
+ int errorLine; /* Error line where an error occurred. */
+ char *errorFileName; /* Error file where an error occurred. */
+ int addStackTrace; /* > 0 If a level should be added to the stack trace */
+ int maxNestingDepth; /* Used for infinite loop detection. */
+ int returnCode; /* Completion code to return on JIM_RETURN. */
+ int returnLevel; /* Current level of 'return -level' */
+ int exitCode; /* Code to return to the OS on JIM_EXIT. */
+ long id; /* Hold unique id for various purposes */
+ int signal_level; /* A nesting level of catch -signal */
+ jim_wide sigmask; /* Bit mask of caught signals, or 0 if none */
+ int (*signal_set_result)(struct Jim_Interp *interp, jim_wide sigmask); /* Set a result for the sigmask */
+ Jim_CallFrame *framePtr; /* Pointer to the current call frame */
+ Jim_CallFrame *topFramePtr; /* toplevel/global frame pointer. */
+ struct Jim_HashTable commands; /* Commands hash table */
+ unsigned jim_wide procEpoch; /* Incremented every time the result
+ of procedures names lookup caching
+ may no longer be valid. */
+ unsigned jim_wide callFrameEpoch; /* Incremented every time a new
+ callframe is created. This id is used for the
+ 'ID' field contained in the Jim_CallFrame
+ structure. */
+ int local; /* If 'local' is in effect, newly defined procs keep a reference to the old defn */
+ Jim_Obj *liveList; /* Linked list of all the live objects. */
+ Jim_Obj *freeList; /* Linked list of all the unused objects. */
+ Jim_Obj *currentScriptObj; /* Script currently in execution. */
+ Jim_Obj *emptyObj; /* Shared empty string object. */
+ Jim_Obj *trueObj; /* Shared true int object. */
+ Jim_Obj *falseObj; /* Shared false int object. */
+ unsigned jim_wide referenceNextId; /* Next id for reference. */
+ struct Jim_HashTable references; /* References hash table. */
+ jim_wide lastCollectId; /* reference max Id of the last GC
+ execution. It's set to -1 while the collection
+ is running as sentinel to avoid to recursive
+ calls via the [collect] command inside
+ finalizers. */
+ time_t lastCollectTime; /* unix time of the last GC execution */
+ struct Jim_HashTable sharedStrings; /* Shared Strings hash table */
+ Jim_Obj *stackTrace; /* Stack trace object. */
+ Jim_Obj *errorProc; /* Name of last procedure which returned an error */
+ Jim_Obj *unknown; /* Unknown command cache */
+ int unknown_called; /* The unknown command has been invoked */
+ int errorFlag; /* Set if an error occurred during execution. */
+ void *cmdPrivData; /* Used to pass the private data pointer to
+ a command. It is set to what the user specified
+ via Jim_CreateCommand(). */
+
+ struct Jim_CallFrame *freeFramesList; /* list of CallFrame structures. */
+ struct Jim_HashTable assocData; /* per-interp storage for use by packages */
+ Jim_PrngState *prngState; /* per interpreter Random Number Gen. state. */
+ struct Jim_HashTable packages; /* Provided packages hash table */
+ Jim_Stack *localProcs; /* procs to be destroyed on end of evaluation */
+ Jim_Stack *loadHandles; /* handles of loaded modules [load] */
+} Jim_Interp;
+
+/* Currently provided as macro that performs the increment.
+ * At some point may be a real function doing more work.
+ * The proc epoch is used in order to know when a command lookup
+ * cached can no longer considered valid. */
+#define Jim_InterpIncrProcEpoch(i) (i)->procEpoch++
+#define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l))
+#define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval))
+/* Note: Using trueObj and falseObj here makes some things slower...*/
+#define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b)
+#define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj)
+#define Jim_GetResult(i) ((i)->result)
+#define Jim_CmdPrivData(i) ((i)->cmdPrivData)
+#define Jim_String(o) Jim_GetString((o), NULL)
+
+/* Note that 'o' is expanded only one time inside this macro,
+ * so it's safe to use side effects. */
+#define Jim_SetResult(i,o) do { \
+ Jim_Obj *_resultObjPtr_ = (o); \
+ Jim_IncrRefCount(_resultObjPtr_); \
+ Jim_DecrRefCount(i,(i)->result); \
+ (i)->result = _resultObjPtr_; \
+} while(0)
+
+/* Use this for filehandles, etc. which need a unique id */
+#define Jim_GetId(i) (++(i)->id)
+
+/* Reference structure. The interpreter pointer is held within privdata member in HashTable */
+#define JIM_REFERENCE_TAGLEN 7 /* The tag is fixed-length, because the reference
+ string representation must be fixed length. */
+typedef struct Jim_Reference {
+ Jim_Obj *objPtr;
+ Jim_Obj *finalizerCmdNamePtr;
+ char tag[JIM_REFERENCE_TAGLEN+1];
+} Jim_Reference;
+
+/* -----------------------------------------------------------------------------
+ * Exported API prototypes.
+ * ---------------------------------------------------------------------------*/
+
+/* Macros that are common for extensions and core. */
+#define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0)
+
+/* The core includes real prototypes, extensions instead
+ * include a global function pointer for every function exported.
+ * Once the extension calls Jim_InitExtension(), the global
+ * functon pointers are set to the value of the STUB table
+ * contained in the Jim_Interp structure.
+ *
+ * This makes Jim able to load extensions even if it is statically
+ * linked itself, and to load extensions compiled with different
+ * versions of Jim (as long as the API is still compatible.) */
+
+/* Macros are common for core and extensions */
+#define Jim_FreeHashTableIterator(iter) Jim_Free(iter)
+
+#define JIM_EXPORT
+
+/* Memory allocation */
+JIM_EXPORT void *Jim_Alloc (int size);
+JIM_EXPORT void *Jim_Realloc(void *ptr, int size);
+JIM_EXPORT void Jim_Free (void *ptr);
+JIM_EXPORT char * Jim_StrDup (const char *s);
+JIM_EXPORT char *Jim_StrDupLen(const char *s, int l);
+
+/* environment */
+JIM_EXPORT char **Jim_GetEnviron(void);
+JIM_EXPORT void Jim_SetEnviron(char **env);
+
+/* evaluation */
+JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script);
+/* in C code, you can do this and get better error messages */
+/* Jim_Eval_Named( interp, "some tcl commands", __FILE__, __LINE__ ); */
+JIM_EXPORT int Jim_Eval_Named(Jim_Interp *interp, const char *script,const char *filename, int lineno);
+JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script);
+JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename);
+JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename);
+JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr);
+JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc,
+ Jim_Obj *const *objv);
+JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, const char *prefix,
+ int objc, Jim_Obj *const *objv);
+JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr,
+ Jim_Obj **resObjPtrPtr, int flags);
+
+/* stack */
+JIM_EXPORT void Jim_InitStack(Jim_Stack *stack);
+JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack);
+JIM_EXPORT int Jim_StackLen(Jim_Stack *stack);
+JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element);
+JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack);
+JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack);
+JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr));
+
+/* hash table */
+JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht,
+ const Jim_HashTableType *type, void *privdata);
+JIM_EXPORT int Jim_ExpandHashTable (Jim_HashTable *ht,
+ unsigned int size);
+JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key,
+ void *val);
+JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht,
+ const void *key, void *val);
+JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht,
+ const void *key);
+JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht);
+JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht,
+ const void *key);
+JIM_EXPORT int Jim_ResizeHashTable (Jim_HashTable *ht);
+JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator
+ (Jim_HashTable *ht);
+JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry
+ (Jim_HashTableIterator *iter);
+
+/* objects */
+JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp);
+JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr);
+JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr);
+JIM_EXPORT void Jim_InitStringRep (Jim_Obj *objPtr, const char *bytes,
+ int length);
+JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp,
+ Jim_Obj *objPtr);
+JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr,
+ int *lenPtr);
+JIM_EXPORT int Jim_Length(Jim_Obj *objPtr);
+
+/* string object */
+JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp,
+ const char *s, int len);
+JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp,
+ const char *s, int charlen);
+JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp,
+ char *s, int len);
+JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr,
+ const char *str, int len);
+JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr,
+ Jim_Obj *appendObjPtr);
+JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp,
+ Jim_Obj *objPtr, ...);
+JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr);
+JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr,
+ Jim_Obj *objPtr, int nocase);
+JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp,
+ Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr,
+ Jim_Obj *lastObjPtr);
+JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp,
+ Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv);
+JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr,
+ Jim_Obj *fmtObjPtr, int flags);
+JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp,
+ Jim_Obj *objPtr, const char *str);
+JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr,
+ Jim_Obj *secondObjPtr, int nocase);
+JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr);
+
+/* reference object */
+JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp,
+ Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr);
+JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp,
+ Jim_Obj *objPtr);
+JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr);
+JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr);
+
+/* interpreter */
+JIM_EXPORT Jim_Interp * Jim_CreateInterp (void);
+JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i);
+JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp);
+JIM_EXPORT const char *Jim_ReturnCode(int code);
+JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...);
+
+/* commands */
+JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp);
+JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp,
+ const char *cmdName, Jim_CmdProc cmdProc, void *privData,
+ Jim_DelCmdProc delProc);
+JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp,
+ const char *cmdName);
+JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp,
+ const char *oldName, const char *newName);
+JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp,
+ Jim_Obj *objPtr, int flags);
+JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp,
+ Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr);
+JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp,
+ const char *name, Jim_Obj *objPtr);
+JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp,
+ const char *name, Jim_Obj *objPtr);
+JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp,
+ const char *name, const char *val);
+JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp,
+ Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr,
+ Jim_CallFrame *targetCallFrame);
+JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp,
+ Jim_Obj *nameObjPtr, int flags);
+JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp,
+ Jim_Obj *nameObjPtr, int flags);
+JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp,
+ const char *name, int flags);
+JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp,
+ const char *name, int flags);
+JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp,
+ Jim_Obj *nameObjPtr, int flags);
+
+/* call frame */
+JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp,
+ Jim_Obj *levelObjPtr);
+
+/* garbage collection */
+JIM_EXPORT int Jim_Collect (Jim_Interp *interp);
+JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp);
+
+/* index object */
+JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr,
+ int *indexPtr);
+
+/* list object */
+JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp,
+ Jim_Obj *const *elements, int len);
+JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp,
+ Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec);
+JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp,
+ Jim_Obj *listPtr, Jim_Obj *objPtr);
+JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp,
+ Jim_Obj *listPtr, Jim_Obj *appendListPtr);
+JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr);
+JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt,
+ int listindex, Jim_Obj **objPtrPtr, int seterr);
+JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp,
+ Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc,
+ Jim_Obj *newObjPtr);
+JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc,
+ Jim_Obj *const *objv);
+
+/* dict object */
+JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp,
+ Jim_Obj *const *elements, int len);
+JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr,
+ Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags);
+JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp,
+ Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc,
+ Jim_Obj **objPtrPtr, int flags);
+JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp,
+ Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc,
+ Jim_Obj *newObjPtr);
+JIM_EXPORT int Jim_DictPairs(Jim_Interp *interp,
+ Jim_Obj *dictPtr, Jim_Obj ***objPtrPtr, int *len);
+JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
+ Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr);
+JIM_EXPORT int Jim_DictKeys(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj);
+JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr);
+
+/* return code object */
+JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr,
+ int *intPtr);
+
+/* expression object */
+JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp,
+ Jim_Obj *exprObjPtr, Jim_Obj **exprResultPtrPtr);
+JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp,
+ Jim_Obj *exprObjPtr, int *boolPtr);
+
+/* integer object */
+JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr,
+ jim_wide *widePtr);
+JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr,
+ long *longPtr);
+#define Jim_NewWideObj Jim_NewIntObj
+JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp,
+ jim_wide wideValue);
+
+/* double object */
+JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
+ double *doublePtr);
+JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr,
+ double doubleValue);
+JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue);
+
+/* shared strings */
+JIM_EXPORT const char * Jim_GetSharedString (Jim_Interp *interp,
+ const char *str);
+JIM_EXPORT void Jim_ReleaseSharedString (Jim_Interp *interp,
+ const char *str);
+
+/* commands utilities */
+JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv, const char *msg);
+JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr,
+ const char * const *tablePtr, int *indexPtr, const char *name, int flags);
+JIM_EXPORT int Jim_ScriptIsComplete (const char *s, int len,
+ char *stateCharPtr);
+/**
+ * Find a matching name in the array of the given length.
+ *
+ * NULL entries are ignored.
+ *
+ * Returns the matching index if found, or -1 if not.
+ */
+JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len);
+
+/* package utilities */
+typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data);
+JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key);
+JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key,
+ Jim_InterpDeleteProc *delProc, void *data);
+JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key);
+
+/* Packages C API */
+/* jim-package.c */
+JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp,
+ const char *name, const char *ver, int flags);
+JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp,
+ const char *name, int flags);
+
+/* error messages */
+JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp);
+
+/* interactive mode */
+JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp);
+
+/* Misc */
+JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp);
+JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base);
+
+/* jim-load.c */
+JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName);
+JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp);
+
+/* jim-aio.c */
+JIM_EXPORT FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command);
+
+
+/* type inspection - avoid where possible */
+JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr);
+JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __JIM__H */
+
+/*
+ * Local Variables: ***
+ * c-basic-offset: 4 ***
+ * tab-width: 4 ***
+ * End: ***
+ */
+/* Provides a common approach to implementing Tcl commands
+ * which implement subcommands
+ */
+#ifndef JIM_SUBCMD_H
+#define JIM_SUBCMD_H
+
+
+#define JIM_MODFLAG_HIDDEN 0x0001 /* Don't show the subcommand in usage or commands */
+#define JIM_MODFLAG_FULLARGV 0x0002 /* Subcmd proc gets called with full argv */
+
+/* Custom flags start at 0x0100 */
+
+/**
+ * Returns JIM_OK if OK, JIM_ERR (etc.) on error, break, continue, etc.
+ * Returns -1 if invalid args.
+ */
+typedef int tclmod_cmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
+
+typedef struct {
+ const char *cmd; /* Name of the (sub)command */
+ const char *args; /* Textual description of allowed args */
+ tclmod_cmd_function *function; /* Function implementing the subcommand */
+ short minargs; /* Minimum required arguments */
+ short maxargs; /* Maximum allowed arguments or -1 if no limit */
+ unsigned flags; /* JIM_MODFLAG_... plus custom flags */
+ const char *description; /* Description of the subcommand */
+} jim_subcmd_type;
+
+/**
+ * Looks up the appropriate subcommand in the given command table and return
+ * the command function which implements the subcommand.
+ * NULL will be returned and an appropriate error will be set if the subcommand or
+ * arguments are invalid.
+ *
+ * Typical usage is:
+ * {
+ * const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, command_table, argc, argv);
+ *
+ * return Jim_CallSubCmd(interp, ct, argc, argv);
+ * }
+ *
+ */
+const jim_subcmd_type *
+Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv);
+
+/**
+ * Parses the args against the given command table and executes the subcommand if found
+ * or sets an appropriate error if the subcommand or arguments is invalid.
+ *
+ * Can be used directly with Jim_CreateCommand() where the ClientData is the command table.
+ *
+ * e.g. Jim_CreateCommand(interp, "mycmd", Jim_SubCmdProc, command_table, NULL);
+ */
+int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
+
+/**
+ * Invokes the given subcmd with the given args as returned
+ * by Jim_ParseSubCmd()
+ *
+ * If ct is NULL, returns JIM_ERR, leaving any message.
+ * Otherwise invokes ct->function
+ *
+ * If ct->function returns -1, sets an error message and returns JIM_ERR.
+ * Otherwise returns the result of ct->function.
+ */
+int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv);
+
+/**
+ * Standard processing for a command.
+ *
+ * This does the '-help' and '-usage' check and the number of args checks.
+ * for a top level command against a single 'jim_subcmd_type' structure.
+ *
+ * Additionally, if command_table->function is set, it should point to a sub command table
+ * and '-subhelp ?subcmd?', '-subusage' and '-subcommands' are then also recognised.
+ *
+ * Returns 0 if user requested usage, -1 on arg error, 1 if OK to process.
+ */
+int
+Jim_CheckCmdUsage(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv);
+
+#endif
+#ifndef JIMREGEXP_H
+#define JIMREGEXP_H
+
+#ifndef _JIMAUTOCONF_H
+#error Need jimautoconf.h
+#endif
+
+#if defined(HAVE_REGCOMP) && !defined(JIM_REGEXP)
+/* Use POSIX regex */
+#include <regex.h>
+
+#else
+
+#include <stdlib.h>
+
+/*
+ * Definitions etc. for regexp(3) routines.
+ *
+ * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
+ * not the System V one.
+ *
+ * 11/04/02 (seiwald) - const-ing for string literals
+ */
+
+typedef struct {
+ int rm_so;
+ int rm_eo;
+} regmatch_t;
+
+/*
+ * The "internal use only" fields in regexp.h are present to pass info from
+ * compile to execute that permits the execute phase to run lots faster on
+ * simple cases. They are:
+ *
+ * regstart char that must begin a match; '\0' if none obvious
+ * reganch is the match anchored (at beginning-of-line only)?
+ * regmust string (pointer into program) that match must include, or NULL
+ * regmlen length of regmust string
+ *
+ * Regstart and reganch permit very fast decisions on suitable starting points
+ * for a match, cutting down the work a lot. Regmust permits fast rejection
+ * of lines that cannot possibly match. The regmust tests are costly enough
+ * that regcomp() supplies a regmust only if the r.e. contains something
+ * potentially expensive (at present, the only such thing detected is * or +
+ * at the start of the r.e., which can involve a lot of backup). Regmlen is
+ * supplied because the test in regexec() needs it and regcomp() is computing
+ * it anyway.
+ */
+
+typedef struct regexp {
+ /* -- public -- */
+ int re_nsub; /* number of parenthesized subexpressions */
+
+ /* -- private -- */
+ int cflags; /* Flags used when compiling */
+ int err; /* Any error which occurred during compile */
+ int regstart; /* Internal use only. */
+ int reganch; /* Internal use only. */
+ const int *regmust; /* Internal use only. */
+ int regmlen; /* Internal use only. */
+ int *program; /* Allocated */
+
+ /* working state - compile */
+ const char *regparse; /* Input-scan pointer. */
+ int *regcode; /* Code-emit pointer; &regdummy = don't. */
+ long regsize; /* Code size. */
+
+ /* working state - exec */
+ int eflags; /* Flags used when executing */
+ const char *start; /* Initial string pointer. */
+ const char *reginput; /* Current input pointer. */
+ const char *regbol; /* Beginning of input, for ^ check. */
+
+ /* Input to regexec() */
+ regmatch_t *pmatch; /* submatches will be stored here */
+ int nmatch; /* size of pmatch[] */
+} regexp;
+
+typedef regexp regex_t;
+
+#define REG_EXTENDED 0
+#define REG_NEWLINE 1
+#define REG_ICASE 2
+
+#define REG_NOTBOL 16
+
+enum {
+ REG_NOERROR, /* Success. */
+ REG_NOMATCH, /* Didn't find a match (for regexec). */
+ REG_BADPAT, /* >= REG_BADPAT is an error */
+ REG_ERR_NULL_ARGUMENT,
+ REG_ERR_UNKNOWN,
+ REG_ERR_TOO_BIG,
+ REG_ERR_NOMEM,
+ REG_ERR_TOO_MANY_PAREN,
+ REG_ERR_UNMATCHED_PAREN,
+ REG_ERR_UNMATCHED_BRACES,
+ REG_ERR_BAD_COUNT,
+ REG_ERR_JUNK_ON_END,
+ REG_ERR_OPERAND_COULD_BE_EMPTY,
+ REG_ERR_NESTED_COUNT,
+ REG_ERR_INTERNAL,
+ REG_ERR_COUNT_FOLLOWS_NOTHING,
+ REG_ERR_TRAILING_BACKSLASH,
+ REG_ERR_CORRUPTED,
+ REG_ERR_NULL_CHAR,
+ REG_ERR_NUM
+};
+
+int regcomp(regex_t *preg, const char *regex, int cflags);
+int regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
+size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size);
+void regfree(regex_t *preg);
+
+#endif
+
+#endif
+int Jim_bootstrapInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "bootstrap", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ return Jim_Eval_Named(interp,
+"\n"
+"\n"
+"proc package {args} {}\n"
+,"bootstrap.tcl", 1);
+}
+int Jim_globInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "glob", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ return Jim_Eval_Named(interp,
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"package require readdir\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"proc glob {args} {\n"
+"\n"
+"\n"
+"\n"
+"\n"
+" local proc glob.readdir_pattern {dir pattern} {\n"
+" set result {}\n"
+"\n"
+"\n"
+" if {$pattern in {. ..}} {\n"
+" return $pattern\n"
+" }\n"
+"\n"
+"\n"
+" foreach name [readdir -nocomplain $dir] {\n"
+" if {[string match $pattern $name]} {\n"
+"\n"
+" if {[string index $name 0] eq \".\" && [string index $pattern 0] ne \".\"} {\n"
+" continue\n"
+" }\n"
+" lappend result $name\n"
+" }\n"
+" }\n"
+"\n"
+" return $result\n"
+" }\n"
+"\n"
+"\n"
+"\n"
+" local proc glob.do {dir rem} {\n"
+"\n"
+"\n"
+" set i [string first / $rem]\n"
+" if {$i < 0} {\n"
+" set pattern $rem\n"
+" set rempattern \"\"\n"
+" } else {\n"
+" set pattern [string range $rem 0 $i-1]\n"
+" set rempattern [string range $rem $i+1 end]\n"
+" }\n"
+"\n"
+"\n"
+" set sep /\n"
+" set globdir $dir\n"
+" if {[string match \"*/\" $dir]} {\n"
+" set sep \"\"\n"
+" } elseif {$dir eq \"\"} {\n"
+" set globdir .\n"
+" set sep \"\"\n"
+" }\n"
+"\n"
+" set result {}\n"
+"\n"
+"\n"
+"\n"
+"\n"
+" if {[set fb [string first \"\\{\" $pattern]] >= 0} {\n"
+" if {[set nb [string first \"\\}\" $pattern $fb]] >= 0} {\n"
+" set before [string range $pattern 0 $fb-1]\n"
+" set braced [string range $pattern $fb+1 $nb-1]\n"
+" set after [string range $pattern $nb+1 end]\n"
+"\n"
+" foreach part [split $braced ,] {\n"
+" lappend result {*}[glob.do $dir $before$part$after]\n"
+" }\n"
+" return $result\n"
+" }\n"
+" }\n"
+"\n"
+"\n"
+" foreach f [glob.readdir_pattern $globdir $pattern] {\n"
+" if {$rempattern eq \"\"} {\n"
+"\n"
+" lappend result $dir$sep$f\n"
+" } else {\n"
+"\n"
+" lappend result {*}[glob.do $dir$sep$f $rempattern]\n"
+" }\n"
+" }\n"
+" return $result\n"
+" }\n"
+"\n"
+"\n"
+" set nocomplain 0\n"
+"\n"
+" if {[lindex $args 0] eq \"-nocomplain\"} {\n"
+" set nocomplain 1\n"
+" set args [lrange $args 1 end]\n"
+" }\n"
+"\n"
+" set result {}\n"
+" foreach pattern $args {\n"
+" if {$pattern eq \"/\"} {\n"
+" lappend result /\n"
+" } elseif {[string match \"/*\" $pattern]} {\n"
+" lappend result {*}[glob.do / [string range $pattern 1 end]]\n"
+" } else {\n"
+" lappend result {*}[glob.do \"\" $pattern]\n"
+" }\n"
+" }\n"
+"\n"
+" if {$nocomplain == 0 && [llength $result] == 0} {\n"
+" return -code error \"no files matched glob patterns\"\n"
+" }\n"
+"\n"
+" return $result\n"
+"}\n"
+,"glob.tcl", 1);
+}
+int Jim_stdlibInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "stdlib", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ return Jim_Eval_Named(interp,
+"\n"
+"\n"
+"\n"
+"proc alias {name args} {\n"
+" set prefix $args\n"
+" proc $name args prefix {\n"
+" tailcall {*}$prefix {*}$args\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"proc lambda {arglist args} {\n"
+" set name [ref {} function lambda.finalizer]\n"
+" tailcall proc $name $arglist {*}$args\n"
+"}\n"
+"\n"
+"proc lambda.finalizer {name val} {\n"
+" rename $name {}\n"
+"}\n"
+"\n"
+"\n"
+"proc curry {args} {\n"
+" set prefix $args\n"
+" lambda args prefix {\n"
+" tailcall {*}$prefix {*}$args\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"proc function {value} {\n"
+" return $value\n"
+"}\n"
+"\n"
+"\n"
+"proc lassign {list args} {\n"
+"\n"
+" lappend list {}\n"
+" uplevel 1 [list foreach $args $list break]\n"
+" lrange $list [llength $args] end-1\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"proc stacktrace {} {\n"
+" set trace {}\n"
+" foreach level [range 1 [info level]] {\n"
+" lassign [info frame -$level] p f l\n"
+" lappend trace $p $f $l\n"
+" }\n"
+" return $trace\n"
+"}\n"
+"\n"
+"\n"
+"proc stackdump {stacktrace} {\n"
+" set result {}\n"
+" set count 0\n"
+" foreach {l f p} [lreverse $stacktrace] {\n"
+" if {$count} {\n"
+" append result \\n\n"
+" }\n"
+" incr count\n"
+" if {$p ne \"\"} {\n"
+" append result \"in procedure '$p' \"\n"
+" if {$f ne \"\"} {\n"
+" append result \"called \"\n"
+" }\n"
+" }\n"
+" if {$f ne \"\"} {\n"
+" append result \"at file \\\"$f\\\", line $l\"\n"
+" }\n"
+" }\n"
+" return $result\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"proc errorInfo {msg {stacktrace \"\"}} {\n"
+" if {$stacktrace eq \"\"} {\n"
+" set stacktrace [info stacktrace]\n"
+" }\n"
+" lassign $stacktrace p f l\n"
+" if {$f ne \"\"} {\n"
+" set result \"Runtime Error: $f:$l: \"\n"
+" }\n"
+" append result \"$msg\\n\"\n"
+" append result [stackdump $stacktrace]\n"
+"\n"
+"\n"
+" string trim $result\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"proc {info nameofexecutable} {} {\n"
+" if {[info exists ::jim_argv0]} {\n"
+" if {[string first \"/\" $::jim_argv0] >= 0} {\n"
+" return $::jim_argv0\n"
+" }\n"
+" foreach path [split [env PATH \"\"] :] {\n"
+" set exec [file join $path $::jim_argv0]\n"
+" if {[file executable $exec]} {\n"
+" return $exec\n"
+" }\n"
+" }\n"
+" }\n"
+" return \"\"\n"
+"}\n"
+"\n"
+"\n"
+"proc {dict with} {dictVar args script} {\n"
+" upvar $dictVar dict\n"
+" set keys {}\n"
+" foreach {n v} [dict get $dict {*}$args] {\n"
+" upvar $n var_$n\n"
+" set var_$n $v\n"
+" lappend keys $n\n"
+" }\n"
+" catch {uplevel 1 $script} msg opts\n"
+" if {[info exists dict] && [dict exists $dict {*}$args]} {\n"
+" foreach n $keys {\n"
+" if {[info exists var_$n]} {\n"
+" dict set dict {*}$args $n [set var_$n]\n"
+" } else {\n"
+" dict unset dict {*}$args $n\n"
+" }\n"
+" }\n"
+" }\n"
+" return {*}$opts $msg\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"proc {dict merge} {dict args} {\n"
+" foreach d $args {\n"
+"\n"
+" dict size $d\n"
+" foreach {k v} $d {\n"
+" dict set dict $k $v\n"
+" }\n"
+" }\n"
+" return $dict\n"
+"}\n"
+,"stdlib.tcl", 1);
+}
+int Jim_tclcompatInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "tclcompat", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ return Jim_Eval_Named(interp,
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"set env [env]\n"
+"\n"
+"if {[info commands stdout] ne \"\"} {\n"
+"\n"
+" foreach p {gets flush close eof seek tell} {\n"
+" proc $p {chan args} {p} {\n"
+" tailcall $chan $p {*}$args\n"
+" }\n"
+" }\n"
+" unset p\n"
+"\n"
+"\n"
+"\n"
+" proc puts {{-nonewline {}} {chan stdout} msg} {\n"
+" if {${-nonewline} ni {-nonewline {}}} {\n"
+" tailcall ${-nonewline} puts $msg\n"
+" }\n"
+" tailcall $chan puts {*}${-nonewline} $msg\n"
+" }\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+" proc read {{-nonewline {}} chan} {\n"
+" if {${-nonewline} ni {-nonewline {}}} {\n"
+" tailcall ${-nonewline} read {*}${chan}\n"
+" }\n"
+" tailcall $chan read {*}${-nonewline}\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"proc case {var args} {\n"
+"\n"
+" if {[lindex $args 0] eq \"in\"} {\n"
+" set args [lrange $args 1 end]\n"
+" }\n"
+"\n"
+"\n"
+" if {[llength $args] == 1} {\n"
+" set args [lindex $args 0]\n"
+" }\n"
+"\n"
+"\n"
+" if {[llength $args] % 2 != 0} {\n"
+" return -code error \"extra case pattern with no body\"\n"
+" }\n"
+"\n"
+"\n"
+" local proc case.checker {value pattern} {\n"
+" string match $pattern $value\n"
+" }\n"
+"\n"
+" foreach {value action} $args {\n"
+" if {$value eq \"default\"} {\n"
+" set do_action $action\n"
+" continue\n"
+" } elseif {[lsearch -bool -command case.checker $value $var]} {\n"
+" set do_action $action\n"
+" break\n"
+" }\n"
+" }\n"
+"\n"
+" if {[info exists do_action]} {\n"
+" set rc [catch [list uplevel 1 $do_action] result opts]\n"
+" if {$rc} {\n"
+" incr opts(-level)\n"
+" }\n"
+" return {*}$opts $result\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"proc fileevent {args} {\n"
+" tailcall {*}$args\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"proc parray {arrayname {pattern *} {puts puts}} {\n"
+" upvar $arrayname a\n"
+"\n"
+" set max 0\n"
+" foreach name [array names a $pattern]] {\n"
+" if {[string length $name] > $max} {\n"
+" set max [string length $name]\n"
+" }\n"
+" }\n"
+" incr max [string length $arrayname]\n"
+" incr max 2\n"
+" foreach name [lsort [array names a $pattern]] {\n"
+" $puts [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"proc {file copy} {{force {}} source target} {\n"
+" try {\n"
+" if {$force ni {{} -force}} {\n"
+" error \"bad option \\\"$force\\\": should be -force\"\n"
+" }\n"
+"\n"
+" set in [open $source]\n"
+"\n"
+" if {$force eq \"\" && [file exists $target]} {\n"
+" $in close\n"
+" error \"error copying \\\"$source\\\" to \\\"$target\\\": file already exists\"\n"
+" }\n"
+" set out [open $target w]\n"
+" $in copyto $out\n"
+" $out close\n"
+" } on error {msg opts} {\n"
+" incr opts(-level)\n"
+" return {*}$opts $msg\n"
+" } finally {\n"
+" catch {$in close}\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"proc popen {cmd {mode r}} {\n"
+" lassign [socket pipe] r w\n"
+" try {\n"
+" if {[string match \"w*\" $mode]} {\n"
+" lappend cmd <@$r &\n"
+" set pids [exec {*}$cmd]\n"
+" $r close\n"
+" set f $w\n"
+" } else {\n"
+" lappend cmd >@$w &\n"
+" set pids [exec {*}$cmd]\n"
+" $w close\n"
+" set f $r\n"
+" }\n"
+" lambda {cmd args} {f pids} {\n"
+" if {$cmd eq \"pid\"} {\n"
+" return $pids\n"
+" }\n"
+" if {$cmd eq \"close\"} {\n"
+" $f close\n"
+"\n"
+" foreach p $pids { os.wait $p }\n"
+" return\n"
+" }\n"
+" tailcall $f $cmd {*}$args\n"
+" }\n"
+" } on error {error opts} {\n"
+" $r close\n"
+" $w close\n"
+" error $error\n"
+" }\n"
+"}\n"
+"\n"
+"\n"
+"local proc pid {{chan {}}} {\n"
+" if {$chan eq \"\"} {\n"
+" tailcall upcall pid\n"
+" }\n"
+" if {[catch {$chan tell}]} {\n"
+" return -code error \"can not find channel named \\\"$chan\\\"\"\n"
+" }\n"
+" if {[catch {$chan pid} pids]} {\n"
+" return \"\"\n"
+" }\n"
+" return $pids\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"proc try {args} {\n"
+" set catchopts {}\n"
+" while {[string match -* [lindex $args 0]]} {\n"
+" set args [lassign $args opt]\n"
+" if {$opt eq \"--\"} {\n"
+" break\n"
+" }\n"
+" lappend catchopts $opt\n"
+" }\n"
+" if {[llength $args] == 0} {\n"
+" return -code error {wrong # args: should be \"try ?options? script ?argument ...?\"}\n"
+" }\n"
+" set args [lassign $args script]\n"
+" set code [catch -eval {*}$catchopts [list uplevel 1 $script] msg opts]\n"
+"\n"
+" set handled 0\n"
+"\n"
+" foreach {on codes vars script} $args {\n"
+" switch -- $on \\\n"
+" on {\n"
+" if {!$handled && ($codes eq \"*\" || [info returncode $code] in $codes)} {\n"
+" lassign $vars msgvar optsvar\n"
+" if {$msgvar ne \"\"} {\n"
+" upvar $msgvar hmsg\n"
+" set hmsg $msg\n"
+" }\n"
+" if {$optsvar ne \"\"} {\n"
+" upvar $optsvar hopts\n"
+" set hopts $opts\n"
+" }\n"
+"\n"
+" set code [catch [list uplevel 1 $script] msg opts]\n"
+" incr handled\n"
+" }\n"
+" } \\\n"
+" finally {\n"
+" set finalcode [catch [list uplevel 1 $codes] finalmsg finalopts]\n"
+" if {$finalcode} {\n"
+"\n"
+" set code $finalcode\n"
+" set msg $finalmsg\n"
+" set opts $finalopts\n"
+" }\n"
+" break\n"
+" } \\\n"
+" default {\n"
+" return -code error \"try: expected 'on' or 'finally', got '$on'\"\n"
+" }\n"
+" }\n"
+"\n"
+" if {$code} {\n"
+" incr opts(-level)\n"
+" return {*}$opts $msg\n"
+" }\n"
+" return $msg\n"
+"}\n"
+"\n"
+"\n"
+"\n"
+"proc throw {code {msg \"\"}} {\n"
+" return -code $code $msg\n"
+"}\n"
+,"tclcompat.tcl", 1);
+}
+
+/* Jim - A small embeddable Tcl interpreter
+ *
+ * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
+ * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
+ * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
+ * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
+ * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
+ * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
+ * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ **/
+
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+
+
+#if !defined(JIM_ANSIC)
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
+#endif
+
+
+#define AIO_CMD_LEN 32 /* e.g. aio.handleXXXXXX */
+#define AIO_BUF_LEN 256 /* Can keep this small and rely on stdio buffering */
+
+#define AIO_KEEPOPEN 1
+
+#if defined(JIM_IPV6)
+#define IPV6 1
+#else
+#define IPV6 0
+#ifndef PF_INET6
+#define PF_INET6 0
+#endif
+#endif
+
+#ifndef JIM_ANSIC
+union sockaddr_any {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+#if IPV6
+ struct sockaddr_in6 sin6;
+#endif
+};
+
+#ifndef HAVE_INET_NTOP
+const char *inet_ntop(int af, const void *src, char *dst, int size)
+{
+ if (af != PF_INET) {
+ return NULL;
+ }
+ snprintf(dst, size, "%s", inet_ntoa(((struct sockaddr_in *)src)->sin_addr));
+ return dst;
+}
+#endif
+#endif
+
+typedef struct AioFile
+{
+ FILE *fp;
+ Jim_Obj *filename;
+ int type;
+ int OpenFlags; /* AIO_KEEPOPEN? keep FILE* */
+ int fd;
+#ifdef O_NDELAY
+ int flags;
+#endif
+ Jim_Obj *rEvent;
+ Jim_Obj *wEvent;
+ Jim_Obj *eEvent;
+#ifndef JIM_ANSIC
+ int addr_family;
+#endif
+} AioFile;
+
+static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
+
+#ifndef JIM_ANSIC
+static int JimParseIPv6Address(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen)
+{
+#if IPV6
+ /*
+ * An IPv6 addr/port looks like:
+ * [::1]
+ * [::1]:2000
+ * [fe80::223:6cff:fe95:bdc0%en1]:2000
+ * [::]:2000
+ * 2000
+ *
+ * Note that the "any" address is ::, which is the same as when no address is specified.
+ */
+ char *sthost = NULL;
+ const char *stport;
+ int ret = JIM_OK;
+ struct addrinfo req;
+ struct addrinfo *ai;
+
+ stport = strrchr(hostport, ':');
+ if (!stport) {
+ /* No : so, the whole thing is the port */
+ stport = hostport;
+ hostport = "::";
+ sthost = Jim_StrDup(hostport);
+ }
+ else {
+ stport++;
+ }
+
+ if (*hostport == '[') {
+ /* This is a numeric ipv6 address */
+ char *pt = strchr(++hostport, ']');
+ if (pt) {
+ sthost = Jim_StrDupLen(hostport, pt - hostport);
+ }
+ }
+
+ if (!sthost) {
+ sthost = Jim_StrDupLen(hostport, stport - hostport - 1);
+ }
+
+ memset(&req, '\0', sizeof(req));
+ req.ai_family = PF_INET6;
+
+ if (getaddrinfo(sthost, NULL, &req, &ai)) {
+ Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport);
+ ret = JIM_ERR;
+ }
+ else {
+ memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen);
+ *salen = ai->ai_addrlen;
+
+ sa->sin.sin_port = htons(atoi(stport));
+
+ freeaddrinfo(ai);
+ }
+ Jim_Free(sthost);
+
+ return ret;
+#else
+ Jim_SetResultString(interp, "ipv6 not supported", -1);
+ return JIM_ERR;
+#endif
+}
+
+static int JimParseIpAddress(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen)
+{
+ /* An IPv4 addr/port looks like:
+ * 192.168.1.5
+ * 192.168.1.5:2000
+ * 2000
+ *
+ * If the address is missing, INADDR_ANY is used.
+ * If the port is missing, 0 is used (only useful for server sockets).
+ */
+ char *sthost = NULL;
+ const char *stport;
+ int ret = JIM_OK;
+
+ stport = strrchr(hostport, ':');
+ if (!stport) {
+ /* No : so, the whole thing is the port */
+ stport = hostport;
+ sthost = Jim_StrDup("0.0.0.0");
+ }
+ else {
+ sthost = Jim_StrDupLen(hostport, stport - hostport);
+ stport++;
+ }
+
+ {
+#ifdef HAVE_GETADDRINFO
+ struct addrinfo req;
+ struct addrinfo *ai;
+ memset(&req, '\0', sizeof(req));
+ req.ai_family = PF_INET;
+
+ if (getaddrinfo(sthost, NULL, &req, &ai)) {
+ ret = JIM_ERR;
+ }
+ else {
+ memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen);
+ *salen = ai->ai_addrlen;
+ freeaddrinfo(ai);
+ }
+#else
+ struct hostent *he;
+
+ ret = JIM_ERR;
+
+ if ((he = gethostbyname(sthost)) != NULL) {
+ if (he->h_length == sizeof(sa->sin.sin_addr)) {
+ *salen = sizeof(sa->sin);
+ sa->sin.sin_family= he->h_addrtype;
+ memcpy(&sa->sin.sin_addr, he->h_addr, he->h_length); /* set address */
+ ret = JIM_OK;
+ }
+ }
+#endif
+
+ sa->sin.sin_port = htons(atoi(stport));
+ }
+ Jim_Free(sthost);
+
+ if (ret != JIM_OK) {
+ Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport);
+ }
+
+ return ret;
+}
+
+#ifdef HAVE_SYS_UN_H
+static int JimParseDomainAddress(Jim_Interp *interp, const char *path, struct sockaddr_un *sa)
+{
+ sa->sun_family = PF_UNIX;
+ snprintf(sa->sun_path, sizeof(sa->sun_path), "%s", path);
+
+ return JIM_OK;
+}
+#endif
+#endif
+
+static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name)
+{
+ if (name) {
+ Jim_SetResultFormatted(interp, "%#s: %s", name, strerror(errno));
+ }
+ else {
+ Jim_SetResultString(interp, strerror(errno), -1);
+ }
+}
+
+static void JimAioDelProc(Jim_Interp *interp, void *privData)
+{
+ AioFile *af = privData;
+
+ JIM_NOTUSED(interp);
+
+ Jim_DecrRefCount(interp, af->filename);
+
+ if (!(af->OpenFlags & AIO_KEEPOPEN)) {
+ fclose(af->fp);
+ }
+#ifdef jim_ext_eventloop
+ /* remove existing EventHandlers */
+ if (af->rEvent) {
+ Jim_DeleteFileHandler(interp, af->fp);
+ }
+ if (af->wEvent) {
+ Jim_DeleteFileHandler(interp, af->fp);
+ }
+ if (af->eEvent) {
+ Jim_DeleteFileHandler(interp, af->fp);
+ }
+#endif
+ Jim_Free(af);
+}
+
+static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ char buf[AIO_BUF_LEN];
+ Jim_Obj *objPtr;
+ int nonewline = 0;
+ int neededLen = -1; /* -1 is "read as much as possible" */
+
+ if (argc && Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) {
+ nonewline = 1;
+ argv++;
+ argc--;
+ }
+ if (argc == 1) {
+ jim_wide wideValue;
+
+ if (Jim_GetWide(interp, argv[0], &wideValue) != JIM_OK)
+ return JIM_ERR;
+ if (wideValue < 0) {
+ Jim_SetResultString(interp, "invalid parameter: negative len", -1);
+ return JIM_ERR;
+ }
+ neededLen = (int)wideValue;
+ }
+ else if (argc) {
+ return -1;
+ }
+ objPtr = Jim_NewStringObj(interp, NULL, 0);
+ while (neededLen != 0) {
+ int retval;
+ int readlen;
+
+ if (neededLen == -1) {
+ readlen = AIO_BUF_LEN;
+ }
+ else {
+ readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen);
+ }
+ retval = fread(buf, 1, readlen, af->fp);
+ if (retval > 0) {
+ Jim_AppendString(interp, objPtr, buf, retval);
+ if (neededLen != -1) {
+ neededLen -= retval;
+ }
+ }
+ if (retval != readlen)
+ break;
+ }
+ /* Check for error conditions */
+ if (ferror(af->fp) && errno != EAGAIN) {
+ /* I/O error */
+ Jim_FreeNewObj(interp, objPtr);
+ JimAioSetError(interp, af->filename);
+ clearerr(af->fp);
+ return JIM_ERR;
+ }
+ if (nonewline) {
+ int len;
+ const char *s = Jim_GetString(objPtr, &len);
+
+ if (len > 0 && s[len - 1] == '\n') {
+ objPtr->length--;
+ objPtr->bytes[objPtr->length] = '\0';
+ }
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ long count = 0;
+ long maxlen = LONG_MAX;
+ FILE *outfh = Jim_AioFilehandle(interp, argv[0]);
+
+ if (outfh == NULL) {
+ return JIM_ERR;
+ }
+
+ if (argc == 2) {
+ if (Jim_GetLong(interp, argv[1], &maxlen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+
+ while (count < maxlen) {
+ int ch = fgetc(af->fp);
+
+ if (ch == EOF || fputc(ch, outfh) == EOF) {
+ break;
+ }
+ count++;
+ }
+
+ if (ferror(af->fp)) {
+ Jim_SetResultFormatted(interp, "error while reading: %s", strerror(errno));
+ clearerr(af->fp);
+ return JIM_ERR;
+ }
+
+ if (ferror(outfh)) {
+ Jim_SetResultFormatted(interp, "error while writing: %s", strerror(errno));
+ clearerr(outfh);
+ return JIM_ERR;
+ }
+
+ Jim_SetResultInt(interp, count);
+
+ return JIM_OK;
+}
+
+static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ char buf[AIO_BUF_LEN];
+ Jim_Obj *objPtr;
+
+ errno = 0;
+
+ objPtr = Jim_NewStringObj(interp, NULL, 0);
+ while (1) {
+ int more = 0;
+
+ buf[AIO_BUF_LEN - 1] = '_';
+ if (fgets(buf, AIO_BUF_LEN, af->fp) == NULL)
+ break;
+ if (buf[AIO_BUF_LEN - 1] == '\0' && buf[AIO_BUF_LEN - 2] != '\n')
+ more = 1;
+ if (more) {
+ Jim_AppendString(interp, objPtr, buf, AIO_BUF_LEN - 1);
+ }
+ else {
+ int len = strlen(buf);
+
+ if (len) {
+ int hasnl = (buf[len - 1] == '\n');
+
+ /* strip "\n" */
+ Jim_AppendString(interp, objPtr, buf, strlen(buf) - hasnl);
+ }
+ }
+ if (!more)
+ break;
+ }
+ if (ferror(af->fp) && errno != EAGAIN && errno != EINTR) {
+ /* I/O error */
+ Jim_FreeNewObj(interp, objPtr);
+ JimAioSetError(interp, af->filename);
+ clearerr(af->fp);
+ return JIM_ERR;
+ }
+ /* On EOF returns -1 if varName was specified, or the empty string. */
+ if (feof(af->fp) && Jim_Length(objPtr) == 0) {
+ Jim_FreeNewObj(interp, objPtr);
+ if (argc) {
+ Jim_SetResultInt(interp, -1);
+ }
+ return JIM_OK;
+ }
+ if (argc) {
+ int totLen;
+
+ Jim_GetString(objPtr, &totLen);
+ if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) {
+ Jim_FreeNewObj(interp, objPtr);
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, totLen);
+ }
+ else {
+ Jim_SetResult(interp, objPtr);
+ }
+ return JIM_OK;
+}
+
+static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ int wlen;
+ const char *wdata;
+ Jim_Obj *strObj;
+
+ if (argc == 2) {
+ if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) {
+ return -1;
+ }
+ strObj = argv[1];
+ }
+ else {
+ strObj = argv[0];
+ }
+
+ wdata = Jim_GetString(strObj, &wlen);
+ if (fwrite(wdata, 1, wlen, af->fp) == (unsigned)wlen) {
+ if (argc == 2 || putc('\n', af->fp) != EOF) {
+ return JIM_OK;
+ }
+ }
+ JimAioSetError(interp, af->filename);
+ return JIM_ERR;
+}
+
+#ifndef JIM_ANSIC
+static int aio_cmd_recvfrom(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ char *buf;
+ union sockaddr_any sa;
+ long len;
+ socklen_t salen = sizeof(sa);
+ int rlen;
+
+ if (Jim_GetLong(interp, argv[0], &len) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ buf = Jim_Alloc(len + 1);
+
+ rlen = recvfrom(fileno(af->fp), buf, len, 0, &sa.sa, &salen);
+ if (rlen < 0) {
+ Jim_Free(buf);
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ buf[rlen] = 0;
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, rlen));
+
+ if (argc > 1) {
+ /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
+ char addrbuf[60];
+
+#if IPV6
+ if (sa.sa.sa_family == PF_INET6) {
+ addrbuf[0] = '[';
+ /* Allow 9 for []:65535\0 */
+ inet_ntop(sa.sa.sa_family, &sa.sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
+ snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa.sin.sin_port));
+ }
+ else
+#endif
+ {
+ /* Allow 7 for :65535\0 */
+ inet_ntop(sa.sa.sa_family, &sa.sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
+ snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa.sin.sin_port));
+ }
+
+ if (Jim_SetVariable(interp, argv[1], Jim_NewStringObj(interp, addrbuf, -1)) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+
+ return JIM_OK;
+}
+
+
+static int aio_cmd_sendto(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ int wlen;
+ int len;
+ const char *wdata;
+ union sockaddr_any sa;
+ const char *addr = Jim_String(argv[1]);
+ int salen;
+
+ if (IPV6 && af->addr_family == PF_INET6) {
+ if (JimParseIPv6Address(interp, addr, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+ else if (JimParseIpAddress(interp, addr, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ wdata = Jim_GetString(argv[0], &wlen);
+
+ /* Note that we don't validate the socket type. Rely on sendto() failing if appropriate */
+ len = sendto(fileno(af->fp), wdata, wlen, 0, &sa.sa, salen);
+ if (len < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, len);
+ return JIM_OK;
+}
+
+static int aio_cmd_accept(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *serv_af = Jim_CmdPrivData(interp);
+ int sock;
+ union sockaddr_any sa;
+ socklen_t addrlen = sizeof(sa);
+ AioFile *af;
+ char buf[AIO_CMD_LEN];
+
+ sock = accept(serv_af->fd, &sa.sa, &addrlen);
+ if (sock < 0)
+ return JIM_ERR;
+
+ /* Create the file command */
+ af = Jim_Alloc(sizeof(*af));
+ af->fd = sock;
+ fcntl(af->fd, F_SETFD, FD_CLOEXEC);
+ af->filename = Jim_NewStringObj(interp, "accept", -1);
+ Jim_IncrRefCount(af->filename);
+ af->fp = fdopen(sock, "r+");
+
+ af->OpenFlags = 0;
+#ifdef O_NDELAY
+ af->flags = fcntl(af->fd, F_GETFL);
+#endif
+ af->rEvent = NULL;
+ af->wEvent = NULL;
+ af->eEvent = NULL;
+ af->addr_family = serv_af->addr_family;
+ snprintf(buf, sizeof(buf), "aio.sockstream%ld", Jim_GetId(interp));
+ Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc);
+ Jim_SetResultString(interp, buf, -1);
+ return JIM_OK;
+}
+
+#endif
+
+static int aio_cmd_flush(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ if (fflush(af->fp) == EOF) {
+ JimAioSetError(interp, af->filename);
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int aio_cmd_eof(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ Jim_SetResultInt(interp, feof(af->fp));
+ return JIM_OK;
+}
+
+static int aio_cmd_close(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_DeleteCommand(interp, Jim_String(argv[0]));
+ return JIM_OK;
+}
+
+static int aio_cmd_seek(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+ int orig = SEEK_SET;
+ long offset;
+
+ if (argc == 2) {
+ if (Jim_CompareStringImmediate(interp, argv[1], "start"))
+ orig = SEEK_SET;
+ else if (Jim_CompareStringImmediate(interp, argv[1], "current"))
+ orig = SEEK_CUR;
+ else if (Jim_CompareStringImmediate(interp, argv[1], "end"))
+ orig = SEEK_END;
+ else {
+ return -1;
+ }
+ }
+ if (Jim_GetLong(interp, argv[0], &offset) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (fseek(af->fp, offset, orig) == -1) {
+ JimAioSetError(interp, af->filename);
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int aio_cmd_tell(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ Jim_SetResultInt(interp, ftell(af->fp));
+ return JIM_OK;
+}
+
+static int aio_cmd_filename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ Jim_SetResult(interp, af->filename);
+ return JIM_OK;
+}
+
+#ifdef O_NDELAY
+static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ int fmode = af->flags;
+
+ if (argc) {
+ long nb;
+
+ if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (nb) {
+ fmode |= O_NDELAY;
+ }
+ else {
+ fmode &= ~O_NDELAY;
+ }
+ fcntl(af->fd, F_SETFL, fmode);
+ af->flags = fmode;
+ }
+ Jim_SetResultInt(interp, (fmode & O_NONBLOCK) ? 1 : 0);
+ return JIM_OK;
+}
+#endif
+
+#ifdef jim_ext_eventloop
+static void JimAioFileEventFinalizer(Jim_Interp *interp, void *clientData)
+{
+ Jim_Obj *objPtr = clientData;
+
+ Jim_DecrRefCount(interp, objPtr);
+}
+
+static int JimAioFileEventHandler(Jim_Interp *interp, void *clientData, int mask)
+{
+ Jim_Obj *objPtr = clientData;
+
+ return Jim_EvalObjBackground(interp, objPtr);
+}
+
+static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, Jim_Obj **scriptHandlerObj,
+ int argc, Jim_Obj * const *argv)
+{
+ int scriptlen = 0;
+
+ if (argc == 0) {
+ /* Return current script */
+ if (*scriptHandlerObj) {
+ Jim_SetResult(interp, *scriptHandlerObj);
+ }
+ return JIM_OK;
+ }
+
+ if (*scriptHandlerObj) {
+ /* Delete old handler */
+ Jim_DeleteFileHandler(interp, af->fp);
+ *scriptHandlerObj = NULL;
+ }
+
+ /* Now possibly add the new script(s) */
+ Jim_GetString(argv[0], &scriptlen);
+ if (scriptlen == 0) {
+ /* Empty script, so done */
+ return JIM_OK;
+ }
+
+ /* A new script to add */
+ Jim_IncrRefCount(argv[0]);
+ *scriptHandlerObj = argv[0];
+
+ Jim_CreateFileHandler(interp, af->fp, mask,
+ JimAioFileEventHandler, *scriptHandlerObj, JimAioFileEventFinalizer);
+
+ return JIM_OK;
+}
+
+static int aio_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ return aio_eventinfo(interp, af, JIM_EVENT_READABLE, &af->rEvent, argc, argv);
+}
+
+static int aio_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ return aio_eventinfo(interp, af, JIM_EVENT_WRITABLE, &af->wEvent, argc, argv);
+}
+
+static int aio_cmd_onexception(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ AioFile *af = Jim_CmdPrivData(interp);
+
+ return aio_eventinfo(interp, af, JIM_EVENT_EXCEPTION, &af->wEvent, argc, argv);
+}
+#endif
+
+static const jim_subcmd_type aio_command_table[] = {
+ { .cmd = "read",
+ .args = "?-nonewline? ?len?",
+ .function = aio_cmd_read,
+ .minargs = 0,
+ .maxargs = 2,
+ .description = "Read and return bytes from the stream. To eof if no len."
+ },
+ { .cmd = "copyto",
+ .args = "handle ?size?",
+ .function = aio_cmd_copy,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Copy up to 'size' bytes to the given filehandle, or to eof if no size."
+ },
+ { .cmd = "gets",
+ .args = "?var?",
+ .function = aio_cmd_gets,
+ .minargs = 0,
+ .maxargs = 1,
+ .description = "Read one line and return it or store it in the var"
+ },
+ { .cmd = "puts",
+ .args = "?-nonewline? str",
+ .function = aio_cmd_puts,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Write the string, with newline unless -nonewline"
+ },
+#ifndef JIM_ANSIC
+ { .cmd = "recvfrom",
+ .args = "len ?addrvar?",
+ .function = aio_cmd_recvfrom,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Receive up to 'len' bytes on the socket. Sets 'addrvar' with receive address, if set"
+ },
+ { .cmd = "sendto",
+ .args = "str address",
+ .function = aio_cmd_sendto,
+ .minargs = 2,
+ .maxargs = 2,
+ .description = "Send 'str' to the given address (dgram only)"
+ },
+ { .cmd = "accept",
+ .function = aio_cmd_accept,
+ .description = "Server socket only: Accept a connection and return stream"
+ },
+#endif
+ { .cmd = "flush",
+ .function = aio_cmd_flush,
+ .description = "Flush the stream"
+ },
+ { .cmd = "eof",
+ .function = aio_cmd_eof,
+ .description = "Returns 1 if stream is at eof"
+ },
+ { .cmd = "close",
+ .flags = JIM_MODFLAG_FULLARGV,
+ .function = aio_cmd_close,
+ .description = "Closes the stream"
+ },
+ { .cmd = "seek",
+ .args = "offset ?start|current|end",
+ .function = aio_cmd_seek,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Seeks in the stream (default 'current')"
+ },
+ { .cmd = "tell",
+ .function = aio_cmd_tell,
+ .description = "Returns the current seek position"
+ },
+ { .cmd = "filename",
+ .function = aio_cmd_filename,
+ .description = "Returns the original filename"
+ },
+#ifdef O_NDELAY
+ { .cmd = "ndelay",
+ .args = "?0|1?",
+ .function = aio_cmd_ndelay,
+ .minargs = 0,
+ .maxargs = 1,
+ .description = "Set O_NDELAY (if arg). Returns current/new setting."
+ },
+#endif
+#ifdef jim_ext_eventloop
+ { .cmd = "readable",
+ .args = "?readable-script?",
+ .minargs = 0,
+ .maxargs = 1,
+ .function = aio_cmd_readable,
+ .description = "Returns script, or invoke readable-script when readable, {} to remove",
+ },
+ { .cmd = "writable",
+ .args = "?writable-script?",
+ .minargs = 0,
+ .maxargs = 1,
+ .function = aio_cmd_writable,
+ .description = "Returns script, or invoke writable-script when writable, {} to remove",
+ },
+ { .cmd = "onexception",
+ .args = "?exception-script?",
+ .minargs = 0,
+ .maxargs = 1,
+ .function = aio_cmd_onexception,
+ .description = "Returns script, or invoke exception-script when oob data, {} to remove",
+ },
+#endif
+ { 0 }
+};
+
+static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, aio_command_table, argc, argv), argc, argv);
+}
+
+static int JimAioOpenCommand(Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv)
+{
+ FILE *fp;
+ AioFile *af;
+ char buf[AIO_CMD_LEN];
+ int OpenFlags = 0;
+ const char *cmdname;
+
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "filename ?mode?");
+ return JIM_ERR;
+ }
+ cmdname = Jim_String(argv[1]);
+ if (Jim_CompareStringImmediate(interp, argv[1], "stdin")) {
+ OpenFlags |= AIO_KEEPOPEN;
+ fp = stdin;
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[1], "stdout")) {
+ OpenFlags |= AIO_KEEPOPEN;
+ fp = stdout;
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[1], "stderr")) {
+ OpenFlags |= AIO_KEEPOPEN;
+ fp = stderr;
+ }
+ else {
+ const char *mode = (argc == 3) ? Jim_String(argv[2]) : "r";
+ const char *filename = Jim_String(argv[1]);
+
+#ifdef jim_ext_tclcompat
+ /* If the filename starts with '|', use popen instead */
+ if (*filename == '|') {
+ Jim_Obj *evalObj[3];
+
+ evalObj[0] = Jim_NewStringObj(interp, "popen", -1);
+ evalObj[1] = Jim_NewStringObj(interp, filename + 1, -1);
+ evalObj[2] = Jim_NewStringObj(interp, mode, -1);
+
+ return Jim_EvalObjVector(interp, 3, evalObj);
+ }
+#endif
+ fp = fopen(filename, mode);
+ if (fp == NULL) {
+ JimAioSetError(interp, argv[1]);
+ return JIM_ERR;
+ }
+ /* Get the next file id */
+ snprintf(buf, sizeof(buf), "aio.handle%ld", Jim_GetId(interp));
+ cmdname = buf;
+ }
+
+ /* Create the file command */
+ af = Jim_Alloc(sizeof(*af));
+ af->fp = fp;
+ af->fd = fileno(fp);
+ if ((OpenFlags & AIO_KEEPOPEN) == 0) {
+ fcntl(af->fd, F_SETFD, FD_CLOEXEC);
+ }
+#ifdef O_NDELAY
+ af->flags = fcntl(af->fd, F_GETFL);
+#endif
+ af->filename = argv[1];
+ Jim_IncrRefCount(af->filename);
+ af->OpenFlags = OpenFlags;
+ af->rEvent = NULL;
+ af->wEvent = NULL;
+ af->eEvent = NULL;
+ Jim_CreateCommand(interp, cmdname, JimAioSubCmdProc, af, JimAioDelProc);
+ Jim_SetResultString(interp, cmdname, -1);
+ return JIM_OK;
+}
+
+#ifndef JIM_ANSIC
+
+/**
+ * Creates a channel for fd.
+ *
+ * hdlfmt is a sprintf format for the filehandle. Anything with %ld at the end will do.
+ * mode is usual "r+", but may be another fdopen() mode as required.
+ *
+ * Creates the command and lappends the name of the command to the current result.
+ *
+ */
+static int JimMakeChannel(Jim_Interp *interp, Jim_Obj *filename, const char *hdlfmt, int fd, int family,
+ const char *mode)
+{
+ AioFile *af;
+ char buf[AIO_CMD_LEN];
+
+ FILE *fp = fdopen(fd, mode);
+
+ if (fp == NULL) {
+ close(fd);
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+
+ /* Create the file command */
+ af = Jim_Alloc(sizeof(*af));
+ af->fp = fp;
+ af->fd = fd;
+ fcntl(af->fd, F_SETFD, FD_CLOEXEC);
+ af->OpenFlags = 0;
+ af->filename = filename;
+ Jim_IncrRefCount(af->filename);
+#ifdef O_NDELAY
+ af->flags = fcntl(af->fd, F_GETFL);
+#endif
+ af->rEvent = NULL;
+ af->wEvent = NULL;
+ af->eEvent = NULL;
+ af->addr_family = family;
+ snprintf(buf, sizeof(buf), hdlfmt, Jim_GetId(interp));
+ Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc);
+
+ Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp, buf, -1));
+
+ return JIM_OK;
+}
+
+static int JimAioSockCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *hdlfmt = "aio.unknown%ld";
+ const char *socktypes[] = {
+ "unix",
+ "unix.server",
+ "dgram",
+ "dgram.server",
+ "stream",
+ "stream.server",
+ "pipe",
+ NULL
+ };
+ enum
+ {
+ SOCK_UNIX,
+ SOCK_UNIX_SERVER,
+ SOCK_DGRAM_CLIENT,
+ SOCK_DGRAM_SERVER,
+ SOCK_STREAM_CLIENT,
+ SOCK_STREAM_SERVER,
+ SOCK_STREAM_PIPE,
+ SOCK_DGRAM6_CLIENT,
+ SOCK_DGRAM6_SERVER,
+ SOCK_STREAM6_CLIENT,
+ SOCK_STREAM6_SERVER,
+ };
+ int socktype;
+ int sock;
+ const char *hostportarg = NULL;
+ int res;
+ int on = 1;
+ const char *mode = "r+";
+ int family = PF_INET;
+ Jim_Obj *argv0 = argv[0];
+ int ipv6 = 0;
+
+ if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-ipv6")) {
+ if (!IPV6) {
+ Jim_SetResultString(interp, "ipv6 not supported", -1);
+ return JIM_ERR;
+ }
+ ipv6 = 1;
+ family = PF_INET6;
+ }
+ argc -= ipv6;
+ argv += ipv6;
+
+ if (argc < 2) {
+ wrongargs:
+ Jim_WrongNumArgs(interp, 1, &argv0, "?-ipv6? type ?address?");
+ return JIM_ERR;
+ }
+
+ if (Jim_GetEnum(interp, argv[1], socktypes, &socktype, "socket type", JIM_ERRMSG) != JIM_OK)
+ return JIM_ERR;
+
+ Jim_SetResultString(interp, "", 0);
+
+ hdlfmt = "aio.sock%ld";
+
+ if (argc > 2) {
+ hostportarg = Jim_String(argv[2]);
+ }
+
+ switch (socktype) {
+ case SOCK_DGRAM_CLIENT:
+ if (argc == 2) {
+ /* No address, so an unconnected dgram socket */
+ sock = socket(family, SOCK_DGRAM, 0);
+ if (sock < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ break;
+ }
+ /* fall through */
+ case SOCK_STREAM_CLIENT:
+ {
+ union sockaddr_any sa;
+ int salen;
+
+ if (argc != 3) {
+ goto wrongargs;
+ }
+
+ if (ipv6) {
+ if (JimParseIPv6Address(interp, hostportarg, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+ else if (JimParseIpAddress(interp, hostportarg, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ sock = socket(family, (socktype == SOCK_DGRAM_CLIENT) ? SOCK_DGRAM : SOCK_STREAM, 0);
+ if (sock < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ res = connect(sock, &sa.sa, salen);
+ if (res) {
+ JimAioSetError(interp, argv[2]);
+ close(sock);
+ return JIM_ERR;
+ }
+ }
+ break;
+
+ case SOCK_STREAM_SERVER:
+ case SOCK_DGRAM_SERVER:
+ {
+ union sockaddr_any sa;
+ int salen;
+
+ if (argc != 3) {
+ goto wrongargs;
+ }
+
+ if (ipv6) {
+ if (JimParseIPv6Address(interp, hostportarg, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+ else if (JimParseIpAddress(interp, hostportarg, &sa, &salen) != JIM_OK) {
+ return JIM_ERR;
+ }
+ sock = socket(family, (socktype == SOCK_DGRAM_SERVER) ? SOCK_DGRAM : SOCK_STREAM, 0);
+ if (sock < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+
+ /* Enable address reuse */
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
+
+ res = bind(sock, &sa.sa, salen);
+ if (res) {
+ JimAioSetError(interp, argv[2]);
+ close(sock);
+ return JIM_ERR;
+ }
+ if (socktype == SOCK_STREAM_SERVER) {
+ res = listen(sock, 5);
+ if (res) {
+ JimAioSetError(interp, NULL);
+ close(sock);
+ return JIM_ERR;
+ }
+ }
+ hdlfmt = "aio.socksrv%ld";
+ }
+ break;
+
+#ifdef HAVE_SYS_UN_H
+ case SOCK_UNIX:
+ {
+ struct sockaddr_un sa;
+ socklen_t len;
+
+ if (argc != 3 || ipv6) {
+ goto wrongargs;
+ }
+
+ if (JimParseDomainAddress(interp, hostportarg, &sa) != JIM_OK) {
+ JimAioSetError(interp, argv[2]);
+ return JIM_ERR;
+ }
+ family = PF_UNIX;
+ sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ len = strlen(sa.sun_path) + 1 + sizeof(sa.sun_family);
+ res = connect(sock, (struct sockaddr *)&sa, len);
+ if (res) {
+ JimAioSetError(interp, argv[2]);
+ close(sock);
+ return JIM_ERR;
+ }
+ hdlfmt = "aio.sockunix%ld";
+ break;
+ }
+
+ case SOCK_UNIX_SERVER:
+ {
+ struct sockaddr_un sa;
+ socklen_t len;
+
+ if (argc != 3 || ipv6) {
+ goto wrongargs;
+ }
+
+ if (JimParseDomainAddress(interp, hostportarg, &sa) != JIM_OK) {
+ JimAioSetError(interp, argv[2]);
+ return JIM_ERR;
+ }
+ family = PF_UNIX;
+ sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ len = strlen(sa.sun_path) + 1 + sizeof(sa.sun_family);
+ res = bind(sock, (struct sockaddr *)&sa, len);
+ if (res) {
+ JimAioSetError(interp, argv[2]);
+ close(sock);
+ return JIM_ERR;
+ }
+ res = listen(sock, 5);
+ if (res) {
+ JimAioSetError(interp, NULL);
+ close(sock);
+ return JIM_ERR;
+ }
+ hdlfmt = "aio.sockunixsrv%ld";
+ break;
+ }
+#endif
+
+#ifdef HAVE_PIPE
+ case SOCK_STREAM_PIPE:
+ {
+ int p[2];
+
+ if (argc != 2 || ipv6) {
+ goto wrongargs;
+ }
+
+ if (pipe(p) < 0) {
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+
+ hdlfmt = "aio.pipe%ld";
+ if (JimMakeChannel(interp, argv[1], hdlfmt, p[0], family, "r") != JIM_OK) {
+ close(p[0]);
+ close(p[1]);
+ JimAioSetError(interp, NULL);
+ return JIM_ERR;
+ }
+ /* Note, if this fails it will leave p[0] open, but this should never happen */
+ mode = "w";
+ sock = p[1];
+ }
+ break;
+#endif
+ default:
+ Jim_SetResultString(interp, "Unsupported socket type", -1);
+ return JIM_ERR;
+ }
+
+ return JimMakeChannel(interp, argv[1], hdlfmt, sock, family, mode);
+}
+#endif
+
+FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command)
+{
+ Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG);
+
+ if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) {
+ return ((AioFile *) cmdPtr->u.native.privData)->fp;
+ }
+ Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command);
+ return NULL;
+}
+
+int Jim_aioInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "aio", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "open", JimAioOpenCommand, NULL, NULL);
+#ifndef JIM_ANSIC
+ Jim_CreateCommand(interp, "socket", JimAioSockCommand, NULL, NULL);
+#endif
+
+ /* Takeover stdin, stdout and stderr */
+ Jim_EvalGlobal(interp, "open stdin; open stdout; open stderr");
+
+ return JIM_OK;
+}
+
+/*
+ * Tcl readdir command.
+ *
+ * (c) 2008 Steve Bennett <steveb@worware.net.au>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ * Based on original work by:
+ *-----------------------------------------------------------------------------
+ * Copyright 1991-1994 Karl Lehenbauer and Mark Diekhans.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appear in all copies. Karl Lehenbauer and
+ * Mark Diekhans make no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ *-----------------------------------------------------------------------------
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+
+
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * Jim_ReaddirCmd --
+ * Implements the rename TCL command:
+ * readdir ?-nocomplain? dirPath
+ *
+ * Results:
+ * Standard TCL result.
+ *-----------------------------------------------------------------------------
+ */
+int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *dirPath;
+ DIR *dirPtr;
+ struct dirent *entryPtr;
+ int nocomplain = 0;
+
+ if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) {
+ nocomplain = 1;
+ }
+ if (argc != 2 && !nocomplain) {
+ Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath");
+ return JIM_ERR;
+ }
+
+ dirPath = Jim_String(argv[1 + nocomplain]);
+
+ dirPtr = opendir(dirPath);
+ if (dirPtr == NULL) {
+ if (nocomplain) {
+ return JIM_OK;
+ }
+ Jim_SetResultString(interp, strerror(errno), -1);
+ return JIM_ERR;
+ }
+ Jim_SetResultString(interp, strerror(errno), -1);
+
+ Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
+
+ while ((entryPtr = readdir(dirPtr)) != NULL) {
+ if (entryPtr->d_name[0] == '.') {
+ if (entryPtr->d_name[1] == '\0') {
+ continue;
+ }
+ if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0'))
+ continue;
+ }
+ Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp,
+ entryPtr->d_name, -1));
+ }
+ closedir(dirPtr);
+
+ return JIM_OK;
+}
+
+int Jim_readdirInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "readdir", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL);
+ return JIM_OK;
+}
+/*
+ * Implements the regexp and regsub commands for Jim
+ *
+ * (c) 2008 Steve Bennett <steveb@workware.net.au>
+ *
+ * Uses C library regcomp()/regexec() for the matching.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ * Based on code originally from Tcl 6.7:
+ *
+ * Copyright 1987-1991 Regents of the University of California
+ * Permission to use, copy, modify, and distribute this
+ * software and its documentation for any purpose and without
+ * fee is hereby granted, provided that the above copyright
+ * notice appear in all copies. The University of California
+ * makes no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without
+ * express or implied warranty.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+
+static void FreeRegexpInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ regfree(objPtr->internalRep.regexpValue.compre);
+ Jim_Free(objPtr->internalRep.regexpValue.compre);
+}
+
+static const Jim_ObjType regexpObjType = {
+ "regexp",
+ FreeRegexpInternalRep,
+ NULL,
+ NULL,
+ JIM_TYPE_NONE
+};
+
+static regex_t *SetRegexpFromAny(Jim_Interp *interp, Jim_Obj *objPtr, unsigned flags)
+{
+ regex_t *compre;
+ const char *pattern;
+ int ret;
+
+ /* Check if the object is already an uptodate variable */
+ if (objPtr->typePtr == &regexpObjType &&
+ objPtr->internalRep.regexpValue.compre && objPtr->internalRep.regexpValue.flags == flags) {
+ /* nothing to do */
+ return objPtr->internalRep.regexpValue.compre;
+ }
+
+ /* Not a regexp or the flags do not match */
+ if (objPtr->typePtr == &regexpObjType) {
+ FreeRegexpInternalRep(interp, objPtr);
+ objPtr->typePtr = NULL;
+ }
+
+ /* Get the string representation */
+ pattern = Jim_String(objPtr);
+ compre = Jim_Alloc(sizeof(regex_t));
+
+ if ((ret = regcomp(compre, pattern, REG_EXTENDED | flags)) != 0) {
+ char buf[100];
+
+ regerror(ret, compre, buf, sizeof(buf));
+ Jim_SetResultFormatted(interp, "couldn't compile regular expression pattern: %s", buf);
+ regfree(compre);
+ Jim_Free(compre);
+ return NULL;
+ }
+
+ objPtr->typePtr = &regexpObjType;
+ objPtr->internalRep.regexpValue.flags = flags;
+ objPtr->internalRep.regexpValue.compre = compre;
+
+ return compre;
+}
+
+int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int opt_indices = 0;
+ int opt_all = 0;
+ int opt_inline = 0;
+ regex_t *regex;
+ int match, i, j;
+ int offset = 0;
+ regmatch_t *pmatch = NULL;
+ int source_len;
+ int result = JIM_OK;
+ const char *pattern;
+ const char *source_str;
+ int num_matches = 0;
+ int num_vars;
+ Jim_Obj *resultListObj = NULL;
+ int regcomp_flags = 0;
+ int eflags = 0;
+ int option;
+ enum {
+ OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END
+ };
+ static const char * const options[] = {
+ "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL
+ };
+
+ if (argc < 3) {
+ wrongNumArgs:
+ Jim_WrongNumArgs(interp, 1, argv,
+ "?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?");
+ return JIM_ERR;
+ }
+
+ for (i = 1; i < argc; i++) {
+ const char *opt = Jim_String(argv[i]);
+
+ if (*opt != '-') {
+ break;
+ }
+ if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (option == OPT_END) {
+ i++;
+ break;
+ }
+ switch (option) {
+ case OPT_INDICES:
+ opt_indices = 1;
+ break;
+
+ case OPT_NOCASE:
+ regcomp_flags |= REG_ICASE;
+ break;
+
+ case OPT_LINE:
+ regcomp_flags |= REG_NEWLINE;
+ break;
+
+ case OPT_ALL:
+ opt_all = 1;
+ break;
+
+ case OPT_INLINE:
+ opt_inline = 1;
+ break;
+
+ case OPT_START:
+ if (++i == argc) {
+ goto wrongNumArgs;
+ }
+ if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) {
+ return JIM_ERR;
+ }
+ break;
+ }
+ }
+ if (argc - i < 2) {
+ goto wrongNumArgs;
+ }
+
+ regex = SetRegexpFromAny(interp, argv[i], regcomp_flags);
+ if (!regex) {
+ return JIM_ERR;
+ }
+
+ pattern = Jim_String(argv[i]);
+ source_str = Jim_GetString(argv[i + 1], &source_len);
+
+ num_vars = argc - i - 2;
+
+ if (opt_inline) {
+ if (num_vars) {
+ Jim_SetResultString(interp, "regexp match variables not allowed when using -inline",
+ -1);
+ result = JIM_ERR;
+ goto done;
+ }
+ num_vars = regex->re_nsub + 1;
+ }
+
+ pmatch = Jim_Alloc((num_vars + 1) * sizeof(*pmatch));
+
+ /* If an offset has been specified, adjust for that now.
+ * If it points past the end of the string, point to the terminating null
+ */
+ if (offset) {
+ if (offset < 0) {
+ offset += source_len + 1;
+ }
+ if (offset > source_len) {
+ source_str += source_len;
+ }
+ else if (offset > 0) {
+ source_str += offset;
+ }
+ eflags |= REG_NOTBOL;
+ }
+
+ if (opt_inline) {
+ resultListObj = Jim_NewListObj(interp, NULL, 0);
+ }
+
+ next_match:
+ match = regexec(regex, source_str, num_vars + 1, pmatch, eflags);
+ if (match >= REG_BADPAT) {
+ char buf[100];
+
+ regerror(match, regex, buf, sizeof(buf));
+ Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf);
+ result = JIM_ERR;
+ goto done;
+ }
+
+ if (match == REG_NOMATCH) {
+ goto done;
+ }
+
+ num_matches++;
+
+ if (opt_all && !opt_inline) {
+ /* Just count the number of matches, so skip the substitution h */
+ goto try_next_match;
+ }
+
+ /*
+ * If additional variable names have been specified, return
+ * index information in those variables.
+ */
+
+ j = 0;
+ for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) {
+ Jim_Obj *resultObj;
+
+ if (opt_indices) {
+ resultObj = Jim_NewListObj(interp, NULL, 0);
+ }
+ else {
+ resultObj = Jim_NewStringObj(interp, "", 0);
+ }
+
+ if (pmatch[j].rm_so == -1) {
+ if (opt_indices) {
+ Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1));
+ Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, -1));
+ }
+ }
+ else {
+ int len = pmatch[j].rm_eo - pmatch[j].rm_so;
+
+ if (opt_indices) {
+ Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp,
+ offset + pmatch[j].rm_so));
+ Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp,
+ offset + pmatch[j].rm_so + len - 1));
+ }
+ else {
+ Jim_AppendString(interp, resultObj, source_str + pmatch[j].rm_so, len);
+ }
+ }
+
+ if (opt_inline) {
+ Jim_ListAppendElement(interp, resultListObj, resultObj);
+ }
+ else {
+ /* And now set the result variable */
+ result = Jim_SetVariable(interp, argv[i], resultObj);
+
+ if (result != JIM_OK) {
+ Jim_FreeObj(interp, resultObj);
+ break;
+ }
+ }
+ }
+
+ try_next_match:
+ if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) {
+ if (pmatch[0].rm_eo) {
+ offset += pmatch[0].rm_eo;
+ source_str += pmatch[0].rm_eo;
+ }
+ else {
+ source_str++;
+ offset++;
+ }
+ if (*source_str) {
+ eflags = REG_NOTBOL;
+ goto next_match;
+ }
+ }
+
+ done:
+ if (result == JIM_OK) {
+ if (opt_inline) {
+ Jim_SetResult(interp, resultListObj);
+ }
+ else {
+ Jim_SetResultInt(interp, num_matches);
+ }
+ }
+
+ Jim_Free(pmatch);
+ return result;
+}
+
+#define MAX_SUB_MATCHES 50
+
+int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int regcomp_flags = 0;
+ int regexec_flags = 0;
+ int opt_all = 0;
+ int offset = 0;
+ regex_t *regex;
+ const char *p;
+ int result;
+ regmatch_t pmatch[MAX_SUB_MATCHES + 1];
+ int num_matches = 0;
+
+ int i, j, n;
+ Jim_Obj *varname;
+ Jim_Obj *resultObj;
+ const char *source_str;
+ int source_len;
+ const char *replace_str;
+ int replace_len;
+ const char *pattern;
+ int option;
+ enum {
+ OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_END
+ };
+ static const char * const options[] = {
+ "-nocase", "-line", "-all", "-start", "--", NULL
+ };
+
+ if (argc < 4) {
+ wrongNumArgs:
+ Jim_WrongNumArgs(interp, 1, argv,
+ "?switches? exp string subSpec ?varName?");
+ return JIM_ERR;
+ }
+
+ for (i = 1; i < argc; i++) {
+ const char *opt = Jim_String(argv[i]);
+
+ if (*opt != '-') {
+ break;
+ }
+ if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (option == OPT_END) {
+ i++;
+ break;
+ }
+ switch (option) {
+ case OPT_NOCASE:
+ regcomp_flags |= REG_ICASE;
+ break;
+
+ case OPT_LINE:
+ regcomp_flags |= REG_NEWLINE;
+ break;
+
+ case OPT_ALL:
+ opt_all = 1;
+ break;
+
+ case OPT_START:
+ if (++i == argc) {
+ goto wrongNumArgs;
+ }
+ if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) {
+ return JIM_ERR;
+ }
+ break;
+ }
+ }
+ if (argc - i != 3 && argc - i != 4) {
+ goto wrongNumArgs;
+ }
+
+ regex = SetRegexpFromAny(interp, argv[i], regcomp_flags);
+ if (!regex) {
+ return JIM_ERR;
+ }
+ pattern = Jim_String(argv[i]);
+
+ source_str = Jim_GetString(argv[i + 1], &source_len);
+ replace_str = Jim_GetString(argv[i + 2], &replace_len);
+ varname = argv[i + 3];
+
+ /* Create the result string */
+ resultObj = Jim_NewStringObj(interp, "", 0);
+
+ /* If an offset has been specified, adjust for that now.
+ * If it points past the end of the string, point to the terminating null
+ */
+ if (offset) {
+ if (offset < 0) {
+ offset += source_len + 1;
+ }
+ if (offset > source_len) {
+ offset = source_len;
+ }
+ else if (offset < 0) {
+ offset = 0;
+ }
+ }
+
+ /* Copy the part before -start */
+ Jim_AppendString(interp, resultObj, source_str, offset);
+
+ /*
+ * The following loop is to handle multiple matches within the
+ * same source string; each iteration handles one match and its
+ * corresponding substitution. If "-all" hasn't been specified
+ * then the loop body only gets executed once.
+ */
+
+ n = source_len - offset;
+ p = source_str + offset;
+ do {
+ int match = regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags);
+
+ if (match >= REG_BADPAT) {
+ char buf[100];
+
+ regerror(match, regex, buf, sizeof(buf));
+ Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf);
+ return JIM_ERR;
+ }
+ if (match == REG_NOMATCH) {
+ break;
+ }
+
+ num_matches++;
+
+ /*
+ * Copy the portion of the source string before the match to the
+ * result variable.
+ */
+ Jim_AppendString(interp, resultObj, p, pmatch[0].rm_so);
+
+ /*
+ * Append the subSpec (replace_str) argument to the variable, making appropriate
+ * substitutions. This code is a bit hairy because of the backslash
+ * conventions and because the code saves up ranges of characters in
+ * subSpec to reduce the number of calls to Jim_SetVar.
+ */
+
+ for (j = 0; j < replace_len; j++) {
+ int idx;
+ int c = replace_str[j];
+
+ if (c == '&') {
+ idx = 0;
+ }
+ else if (c == '\\' && j < replace_len) {
+ c = replace_str[++j];
+ if ((c >= '0') && (c <= '9')) {
+ idx = c - '0';
+ }
+ else if ((c == '\\') || (c == '&')) {
+ Jim_AppendString(interp, resultObj, replace_str + j, 1);
+ continue;
+ }
+ else {
+ Jim_AppendString(interp, resultObj, replace_str + j - 1, 2);
+ continue;
+ }
+ }
+ else {
+ Jim_AppendString(interp, resultObj, replace_str + j, 1);
+ continue;
+ }
+ if ((idx < MAX_SUB_MATCHES) && pmatch[idx].rm_so != -1 && pmatch[idx].rm_eo != -1) {
+ Jim_AppendString(interp, resultObj, p + pmatch[idx].rm_so,
+ pmatch[idx].rm_eo - pmatch[idx].rm_so);
+ }
+ }
+
+ p += pmatch[0].rm_eo;
+ n -= pmatch[0].rm_eo;
+
+ /* If -all is not specified, or there is no source left, we are done */
+ if (!opt_all || n == 0) {
+ break;
+ }
+
+ /* An anchored pattern without -line must be done */
+ if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') {
+ break;
+ }
+
+ /* If the pattern is empty, need to step forwards */
+ if (pattern[0] == '\0' && n) {
+ /* Need to copy the char we are moving over */
+ Jim_AppendString(interp, resultObj, p, 1);
+ p++;
+ n--;
+ }
+
+ regexec_flags |= REG_NOTBOL;
+ } while (n);
+
+ /*
+ * Copy the portion of the string after the last match to the
+ * result variable.
+ */
+ Jim_AppendString(interp, resultObj, p, -1);
+
+ /* And now set or return the result variable */
+ if (argc - i == 4) {
+ result = Jim_SetVariable(interp, varname, resultObj);
+
+ if (result == JIM_OK) {
+ Jim_SetResultInt(interp, num_matches);
+ }
+ else {
+ Jim_FreeObj(interp, resultObj);
+ }
+ }
+ else {
+ Jim_SetResult(interp, resultObj);
+ result = JIM_OK;
+ }
+
+ return result;
+}
+
+int Jim_regexpInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "regexp", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "regexp", Jim_RegexpCmd, NULL, NULL);
+ Jim_CreateCommand(interp, "regsub", Jim_RegsubCmd, NULL, NULL);
+ return JIM_OK;
+}
+/*
+ * Implements the file command for jim
+ *
+ * (c) 2008 Steve Bennett <steveb@workware.net.au>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ * Based on code originally from Tcl 6.7:
+ *
+ * Copyright 1987-1991 Regents of the University of California
+ * Permission to use, copy, modify, and distribute this
+ * software and its documentation for any purpose and without
+ * fee is hereby granted, provided that the above copyright
+ * notice appear in all copies. The University of California
+ * makes no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without
+ * express or implied warranty.
+ */
+
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+
+
+# ifndef MAXPATHLEN
+# define MAXPATHLEN JIM_PATH_LEN
+# endif
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * JimGetFileType --
+ *
+ * Given a mode word, returns a string identifying the type of a
+ * file.
+ *
+ * Results:
+ * A static text string giving the file type from mode.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static const char *JimGetFileType(int mode)
+{
+ if (S_ISREG(mode)) {
+ return "file";
+ }
+ else if (S_ISDIR(mode)) {
+ return "directory";
+ }
+ else if (S_ISCHR(mode)) {
+ return "characterSpecial";
+ }
+ else if (S_ISBLK(mode)) {
+ return "blockSpecial";
+ }
+ else if (S_ISFIFO(mode)) {
+ return "fifo";
+#ifdef S_ISLNK
+ }
+ else if (S_ISLNK(mode)) {
+ return "link";
+#endif
+#ifdef S_ISSOCK
+ }
+ else if (S_ISSOCK(mode)) {
+ return "socket";
+#endif
+ }
+ return "unknown";
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * StoreStatData --
+ *
+ * This is a utility procedure that breaks out the fields of a
+ * "stat" structure and stores them in textual form into the
+ * elements of an associative array.
+ *
+ * Results:
+ * Returns a standard Tcl return value. If an error occurs then
+ * a message is left in interp->result.
+ *
+ * Side effects:
+ * Elements of the associative array given by "varName" are modified.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int set_array_int_value(Jim_Interp *interp, Jim_Obj *container, const char *key,
+ jim_wide value)
+{
+ Jim_Obj *nameobj = Jim_NewStringObj(interp, key, -1);
+ Jim_Obj *valobj = Jim_NewWideObj(interp, value);
+
+ if (Jim_SetDictKeysVector(interp, container, &nameobj, 1, valobj) != JIM_OK) {
+ Jim_FreeObj(interp, nameobj);
+ Jim_FreeObj(interp, valobj);
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int set_array_string_value(Jim_Interp *interp, Jim_Obj *container, const char *key,
+ const char *value)
+{
+ Jim_Obj *nameobj = Jim_NewStringObj(interp, key, -1);
+ Jim_Obj *valobj = Jim_NewStringObj(interp, value, -1);
+
+ if (Jim_SetDictKeysVector(interp, container, &nameobj, 1, valobj) != JIM_OK) {
+ Jim_FreeObj(interp, nameobj);
+ Jim_FreeObj(interp, valobj);
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int StoreStatData(Jim_Interp *interp, Jim_Obj *varName, const struct stat *sb)
+{
+ if (set_array_int_value(interp, varName, "dev", sb->st_dev) != JIM_OK) {
+ Jim_SetResultFormatted(interp, "can't set \"%#s(dev)\": variables isn't array", varName);
+ return JIM_ERR;
+ }
+ set_array_int_value(interp, varName, "ino", sb->st_ino);
+ set_array_int_value(interp, varName, "mode", sb->st_mode);
+ set_array_int_value(interp, varName, "nlink", sb->st_nlink);
+ set_array_int_value(interp, varName, "uid", sb->st_uid);
+ set_array_int_value(interp, varName, "gid", sb->st_gid);
+ set_array_int_value(interp, varName, "size", sb->st_size);
+ set_array_int_value(interp, varName, "atime", sb->st_atime);
+ set_array_int_value(interp, varName, "mtime", sb->st_mtime);
+ set_array_int_value(interp, varName, "ctime", sb->st_ctime);
+ set_array_string_value(interp, varName, "type", JimGetFileType((int)sb->st_mode));
+
+ /* And also return the value */
+ Jim_SetResult(interp, Jim_GetVariable(interp, varName, 0));
+
+ return JIM_OK;
+}
+
+static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path = Jim_String(argv[0]);
+ const char *p = strrchr(path, '/');
+
+ if (!p) {
+ Jim_SetResultString(interp, ".", -1);
+ }
+ else if (p == path) {
+ Jim_SetResultString(interp, "/", -1);
+ }
+ else {
+ Jim_SetResultString(interp, path, p - path);
+ }
+ return JIM_OK;
+}
+
+static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path = Jim_String(argv[0]);
+ const char *lastSlash = strrchr(path, '/');
+ const char *p = strrchr(path, '.');
+
+ if (p == NULL || (lastSlash != NULL && lastSlash > p)) {
+ Jim_SetResult(interp, argv[0]);
+ }
+ else {
+ Jim_SetResultString(interp, path, p - path);
+ }
+ return JIM_OK;
+}
+
+static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path = Jim_String(argv[0]);
+ const char *lastSlash = strrchr(path, '/');
+ const char *p = strrchr(path, '.');
+
+ if (p == NULL || (lastSlash != NULL && lastSlash >= p)) {
+ p = "";
+ }
+ Jim_SetResultString(interp, p, -1);
+ return JIM_OK;
+}
+
+static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path = Jim_String(argv[0]);
+ const char *lastSlash = strrchr(path, '/');
+
+ if (lastSlash) {
+ Jim_SetResultString(interp, lastSlash + 1, -1);
+ }
+ else {
+ Jim_SetResult(interp, argv[0]);
+ }
+ return JIM_OK;
+}
+
+static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+#ifdef HAVE_REALPATH
+ const char *path = Jim_String(argv[0]);
+ char *newname = Jim_Alloc(MAXPATHLEN + 1);
+
+ if (realpath(path, newname)) {
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1));
+ }
+ else {
+ Jim_Free(newname);
+ Jim_SetResult(interp, argv[0]);
+ }
+ return JIM_OK;
+#else
+ Jim_SetResultString(interp, "Not implemented", -1);
+ return JIM_ERR;
+#endif
+}
+
+static int file_cmd_join(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ char *newname = Jim_Alloc(MAXPATHLEN + 1);
+ char *last = newname;
+
+ *newname = 0;
+
+ /* Simple implementation for now */
+ for (i = 0; i < argc; i++) {
+ int len;
+ const char *part = Jim_GetString(argv[i], &len);
+
+ if (*part == '/') {
+ /* Absolute component, so go back to the start */
+ last = newname;
+ }
+
+ /* Add a slash if needed */
+ if (last != newname) {
+ *last++ = '/';
+ }
+
+ if (len) {
+ if (last + len - newname >= MAXPATHLEN) {
+ Jim_Free(newname);
+ Jim_SetResultString(interp, "Path too long", -1);
+ return JIM_ERR;
+ }
+ memcpy(last, part, len);
+ last += len;
+ }
+
+ /* Remove a slash if needed */
+ if (last != newname && last[-1] == '/') {
+ *--last = 0;
+ }
+ }
+
+ *last = 0;
+
+ /* Probably need to handle some special cases ... */
+
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, last - newname));
+
+ return JIM_OK;
+}
+
+static int file_access(Jim_Interp *interp, Jim_Obj *filename, int mode)
+{
+ const char *path = Jim_String(filename);
+ int rc = access(path, mode);
+
+ Jim_SetResultBool(interp, rc != -1);
+
+ return JIM_OK;
+}
+
+static int file_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return file_access(interp, argv[0], R_OK);
+}
+
+static int file_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return file_access(interp, argv[0], W_OK);
+}
+
+static int file_cmd_executable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return file_access(interp, argv[0], X_OK);
+}
+
+static int file_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return file_access(interp, argv[0], F_OK);
+}
+
+static int file_cmd_delete(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ while (argc--) {
+ const char *path = Jim_String(argv[0]);
+
+ if (unlink(path) == -1 && errno != ENOENT) {
+ if (rmdir(path) == -1) {
+ Jim_SetResultFormatted(interp, "couldn't delete file \"%s\": %s", path,
+ strerror(errno));
+ return JIM_ERR;
+ }
+ }
+ argv++;
+ }
+ return JIM_OK;
+}
+
+#ifdef MKDIR_ONE_ARG
+#define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME)
+#else
+#define MKDIR_DEFAULT(PATHNAME) mkdir(PATHNAME, 0755)
+#endif
+
+/**
+ * Create directory, creating all intermediate paths if necessary.
+ *
+ * Returns 0 if OK or -1 on failure (and sets errno)
+ *
+ * Note: The path may be modified.
+ */
+static int mkdir_all(char *path)
+{
+ int ok = 1;
+
+ /* First time just try to make the dir */
+ goto first;
+
+ while (ok--) {
+ /* Must have failed the first time, so recursively make the parent and try again */
+ char *slash = strrchr(path, '/');
+
+ if (slash && slash != path) {
+ *slash = 0;
+ if (mkdir_all(path) != 0) {
+ return -1;
+ }
+ *slash = '/';
+ }
+ first:
+ if (MKDIR_DEFAULT(path) == 0) {
+ return 0;
+ }
+ if (errno == ENOENT) {
+ /* Create the parent and try again */
+ continue;
+ }
+ /* Maybe it already exists as a directory */
+ if (errno == EEXIST) {
+ struct stat sb;
+
+ if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
+ return 0;
+ }
+ /* Restore errno */
+ errno = EEXIST;
+ }
+ /* Failed */
+ break;
+ }
+ return -1;
+}
+
+static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ while (argc--) {
+ char *path = Jim_StrDup(Jim_String(argv[0]));
+ int rc = mkdir_all(path);
+
+ Jim_Free(path);
+ if (rc != 0) {
+ Jim_SetResultFormatted(interp, "can't create directory \"%#s\": %s", argv[0],
+ strerror(errno));
+ return JIM_ERR;
+ }
+ argv++;
+ }
+ return JIM_OK;
+}
+
+#ifdef HAVE_MKSTEMP
+static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int fd;
+ char *filename;
+ const char *template = "/tmp/tcl.tmp.XXXXXX";
+
+ if (argc >= 1) {
+ template = Jim_String(argv[0]);
+ }
+ filename = Jim_StrDup(template);
+
+ fd = mkstemp(filename);
+ if (fd < 0) {
+ Jim_SetResultString(interp, "Failed to create tempfile", -1);
+ return JIM_ERR;
+ }
+ close(fd);
+
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, filename, -1));
+ return JIM_OK;
+}
+#endif
+
+static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *source;
+ const char *dest;
+ int force = 0;
+
+ if (argc == 3) {
+ if (!Jim_CompareStringImmediate(interp, argv[0], "-force")) {
+ return -1;
+ }
+ force++;
+ argv++;
+ argc--;
+ }
+
+ source = Jim_String(argv[0]);
+ dest = Jim_String(argv[1]);
+
+ if (!force && access(dest, F_OK) == 0) {
+ Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": target exists", argv[0],
+ argv[1]);
+ return JIM_ERR;
+ }
+
+ if (rename(source, dest) != 0) {
+ Jim_SetResultFormatted(interp, "error renaming \"%#s\" to \"%#s\": %s", argv[0], argv[1],
+ strerror(errno));
+ return JIM_ERR;
+ }
+
+ return JIM_OK;
+}
+
+static int file_stat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
+{
+ const char *path = Jim_String(filename);
+
+ if (stat(path, sb) == -1) {
+ Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+#ifndef HAVE_LSTAT
+#define lstat stat
+#endif
+
+static int file_lstat(Jim_Interp *interp, Jim_Obj *filename, struct stat *sb)
+{
+ const char *path = Jim_String(filename);
+
+ if (lstat(path, sb) == -1) {
+ Jim_SetResultFormatted(interp, "could not read \"%#s\": %s", filename, strerror(errno));
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int file_cmd_atime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_stat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, sb.st_atime);
+ return JIM_OK;
+}
+
+static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_stat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, sb.st_mtime);
+ return JIM_OK;
+}
+
+static int file_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return Jim_EvalObjPrefix(interp, "file copy", argc, argv);
+}
+
+static int file_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_stat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, sb.st_size);
+ return JIM_OK;
+}
+
+static int file_cmd_isdirectory(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+ int ret = 0;
+
+ if (file_stat(interp, argv[0], &sb) == JIM_OK) {
+ ret = S_ISDIR(sb.st_mode);
+ }
+ Jim_SetResultInt(interp, ret);
+ return JIM_OK;
+}
+
+static int file_cmd_isfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+ int ret = 0;
+
+ if (file_stat(interp, argv[0], &sb) == JIM_OK) {
+ ret = S_ISREG(sb.st_mode);
+ }
+ Jim_SetResultInt(interp, ret);
+ return JIM_OK;
+}
+
+#ifdef HAVE_GETEUID
+static int file_cmd_owned(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+ int ret = 0;
+
+ if (file_stat(interp, argv[0], &sb) == JIM_OK) {
+ ret = (geteuid() == sb.st_uid);
+ }
+ Jim_SetResultInt(interp, ret);
+ return JIM_OK;
+}
+#endif
+
+#if defined(HAVE_READLINK)
+static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path = Jim_String(argv[0]);
+ char *linkValue = Jim_Alloc(MAXPATHLEN + 1);
+
+ int linkLength = readlink(path, linkValue, MAXPATHLEN);
+
+ if (linkLength == -1) {
+ Jim_Free(linkValue);
+ Jim_SetResultFormatted(interp, "couldn't readlink \"%s\": %s", argv[0], strerror(errno));
+ return JIM_ERR;
+ }
+ linkValue[linkLength] = 0;
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, linkValue, linkLength));
+ return JIM_OK;
+}
+#endif
+
+static int file_cmd_type(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResultString(interp, JimGetFileType((int)sb.st_mode), -1);
+ return JIM_OK;
+}
+
+static int file_cmd_lstat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_lstat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ return StoreStatData(interp, argv[1], &sb);
+}
+
+static int file_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct stat sb;
+
+ if (file_stat(interp, argv[0], &sb) != JIM_OK) {
+ return JIM_ERR;
+ }
+ return StoreStatData(interp, argv[1], &sb);
+}
+
+static const jim_subcmd_type file_command_table[] = {
+ { .cmd = "atime",
+ .args = "name",
+ .function = file_cmd_atime,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Last access time"
+ },
+ { .cmd = "mtime",
+ .args = "name",
+ .function = file_cmd_mtime,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Last modification time"
+ },
+ { .cmd = "copy",
+ .args = "?-force? source dest",
+ .function = file_cmd_copy,
+ .minargs = 2,
+ .maxargs = 3,
+ .description = "Copy source file to destination file"
+ },
+ { .cmd = "dirname",
+ .args = "name",
+ .function = file_cmd_dirname,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Directory part of the name"
+ },
+ { .cmd = "rootname",
+ .args = "name",
+ .function = file_cmd_rootname,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Name without any extension"
+ },
+ { .cmd = "extension",
+ .args = "name",
+ .function = file_cmd_extension,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Last extension including the dot"
+ },
+ { .cmd = "tail",
+ .args = "name",
+ .function = file_cmd_tail,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Last component of the name"
+ },
+ { .cmd = "normalize",
+ .args = "name",
+ .function = file_cmd_normalize,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Normalized path of name"
+ },
+ { .cmd = "join",
+ .args = "name ?name ...?",
+ .function = file_cmd_join,
+ .minargs = 1,
+ .maxargs = -1,
+ .description = "Join multiple path components"
+ },
+ { .cmd = "readable",
+ .args = "name",
+ .function = file_cmd_readable,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Is file readable"
+ },
+ { .cmd = "writable",
+ .args = "name",
+ .function = file_cmd_writable,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Is file writable"
+ },
+ { .cmd = "executable",
+ .args = "name",
+ .function = file_cmd_executable,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Is file executable"
+ },
+ { .cmd = "exists",
+ .args = "name",
+ .function = file_cmd_exists,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Does file exist"
+ },
+ { .cmd = "delete",
+ .args = "name ...",
+ .function = file_cmd_delete,
+ .minargs = 1,
+ .maxargs = -1,
+ .description = "Deletes the files or empty directories"
+ },
+ { .cmd = "mkdir",
+ .args = "dir ...",
+ .function = file_cmd_mkdir,
+ .minargs = 1,
+ .maxargs = -1,
+ .description = "Creates the directories"
+ },
+#ifdef HAVE_MKSTEMP
+ { .cmd = "tempfile",
+ .args = "?template?",
+ .function = file_cmd_tempfile,
+ .minargs = 0,
+ .maxargs = 1,
+ .description = "Creates a temporary filename"
+ },
+#endif
+ { .cmd = "rename",
+ .args = "?-force? source dest",
+ .function = file_cmd_rename,
+ .minargs = 2,
+ .maxargs = 3,
+ .description = "Renames a file"
+ },
+#if defined(HAVE_READLINK)
+ { .cmd = "readlink",
+ .args = "name",
+ .function = file_cmd_readlink,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Value of the symbolic link"
+ },
+#endif
+ { .cmd = "size",
+ .args = "name",
+ .function = file_cmd_size,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Size of file"
+ },
+ { .cmd = "stat",
+ .args = "name var",
+ .function = file_cmd_stat,
+ .minargs = 2,
+ .maxargs = 2,
+ .description = "Stores results of stat in var array"
+ },
+ { .cmd = "lstat",
+ .args = "name var",
+ .function = file_cmd_lstat,
+ .minargs = 2,
+ .maxargs = 2,
+ .description = "Stores results of lstat in var array"
+ },
+ { .cmd = "type",
+ .args = "name",
+ .function = file_cmd_type,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Returns type of the file"
+ },
+#ifdef HAVE_GETEUID
+ { .cmd = "owned",
+ .args = "name",
+ .function = file_cmd_owned,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Returns 1 if owned by the current owner"
+ },
+#endif
+ { .cmd = "isdirectory",
+ .args = "name",
+ .function = file_cmd_isdirectory,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Returns 1 if name is a directory"
+ },
+ { .cmd = "isfile",
+ .args = "name",
+ .function = file_cmd_isfile,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Returns 1 if name is a file"
+ },
+ {
+ .cmd = 0
+ }
+};
+
+static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *path;
+
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "dirname");
+ return JIM_ERR;
+ }
+
+ path = Jim_String(argv[1]);
+
+ if (chdir(path) != 0) {
+ Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path,
+ strerror(errno));
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+static int Jim_PwdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const int cwd_len = 2048;
+ char *cwd = malloc(cwd_len);
+
+ if (getcwd(cwd, cwd_len) == NULL) {
+ Jim_SetResultString(interp, "Failed to get pwd", -1);
+ return JIM_ERR;
+ }
+
+ Jim_SetResultString(interp, cwd, -1);
+
+ free(cwd);
+ return JIM_OK;
+}
+
+int Jim_fileInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "file", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL);
+ Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL);
+ Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL);
+ return JIM_OK;
+}
+
+/*
+ * (c) 2008 Steve Bennett <steveb@workware.net.au>
+ *
+ * Implements the exec command for Jim
+ *
+ * Based on code originally from Tcl 6.7 by John Ousterhout.
+ * From that code:
+ *
+ * The Tcl_Fork and Tcl_WaitPids procedures are based on code
+ * contributed by Karl Lehenbauer, Mark Diekhans and Peter
+ * da Silva.
+ *
+ * Copyright 1987-1991 Regents of the University of California
+ * Permission to use, copy, modify, and distribute this
+ * software and its documentation for any purpose and without
+ * fee is hereby granted, provided that the above copyright
+ * notice appear in all copies. The University of California
+ * makes no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without
+ * express or implied warranty.
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/wait.h>
+
+
+/* These two could be moved into the Tcl core */
+static void Jim_SetResultErrno(Jim_Interp *interp, const char *msg)
+{
+ Jim_SetResultFormatted(interp, "%s: %s", msg, strerror(errno));
+}
+
+static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr)
+{
+ int len;
+ const char *s = Jim_GetString(objPtr, &len);
+
+ if (len > 0 && s[len - 1] == '\n') {
+ objPtr->length--;
+ objPtr->bytes[objPtr->length] = '\0';
+ }
+}
+
+/**
+ * Read from 'fd' and append the data to strObj
+ * Returns JIM_OK if OK, or JIM_ERR on error.
+ */
+static int JimAppendStreamToString(Jim_Interp *interp, int fd, Jim_Obj *strObj)
+{
+ while (1) {
+ char buffer[256];
+ int count;
+
+ count = read(fd, buffer, sizeof(buffer));
+
+ if (count == 0) {
+ Jim_RemoveTrailingNewline(strObj);
+ return JIM_OK;
+ }
+ if (count < 0) {
+ return JIM_ERR;
+ }
+ Jim_AppendString(interp, strObj, buffer, count);
+ }
+}
+
+/*
+ * If the last character of the result is a newline, then remove
+ * the newline character (the newline would just confuse things).
+ *
+ * Note: Ideally we could do this by just reducing the length of stringrep
+ * by 1, but there is no API for this :-(
+ */
+static void JimTrimTrailingNewline(Jim_Interp *interp)
+{
+ int len;
+ const char *p = Jim_GetString(Jim_GetResult(interp), &len);
+
+ if (len > 0 && p[len - 1] == '\n') {
+ Jim_SetResultString(interp, p, len - 1);
+ }
+}
+
+/**
+ * Builds the environment array from $::env
+ *
+ * If $::env is not set, simply returns environ.
+ *
+ * Otherwise allocates the environ array from the contents of $::env
+ *
+ * If the exec fails, memory can be freed via JimFreeEnv()
+ */
+static char **JimBuildEnv(Jim_Interp *interp)
+{
+#ifdef jim_ext_tclcompat
+ int i;
+ int len;
+ int n;
+ char **env;
+
+ Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, "env", JIM_NONE);
+
+ if (!objPtr) {
+ return Jim_GetEnviron();
+ }
+
+ /* Calculate the required size */
+ len = Jim_ListLength(interp, objPtr);
+ if (len % 2) {
+ len--;
+ }
+
+ env = Jim_Alloc(sizeof(*env) * (len / 2 + 1));
+
+ n = 0;
+ for (i = 0; i < len; i += 2) {
+ int l1, l2;
+ const char *s1, *s2;
+ Jim_Obj *elemObj;
+
+ Jim_ListIndex(interp, objPtr, i, &elemObj, JIM_NONE);
+ s1 = Jim_GetString(elemObj, &l1);
+ Jim_ListIndex(interp, objPtr, i + 1, &elemObj, JIM_NONE);
+ s2 = Jim_GetString(elemObj, &l2);
+
+ env[n] = Jim_Alloc(l1 + l2 + 2);
+ sprintf(env[n], "%s=%s", s1, s2);
+ n++;
+ }
+ env[n] = NULL;
+
+ return env;
+#else
+ return Jim_GetEnviron();
+#endif
+}
+
+/**
+ * Frees the environment allocated by JimBuildEnv()
+ *
+ * Must pass original_environ.
+ */
+static void JimFreeEnv(Jim_Interp *interp, char **env, char **original_environ)
+{
+#ifdef jim_ext_tclcompat
+ if (env != original_environ) {
+ int i;
+ for (i = 0; env[i]; i++) {
+ Jim_Free(env[i]);
+ }
+ Jim_Free(env);
+ }
+#endif
+}
+
+/*
+ * Create error messages for unusual process exits. An
+ * extra newline gets appended to each error message, but
+ * it gets removed below (in the same fashion that an
+ * extra newline in the command's output is removed).
+ */
+static int JimCheckWaitStatus(Jim_Interp *interp, int pid, int waitStatus)
+{
+ Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0);
+ int rc = JIM_ERR;
+
+ if (WIFEXITED(waitStatus)) {
+ if (WEXITSTATUS(waitStatus) == 0) {
+ Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1));
+ rc = JIM_OK;
+ }
+ else {
+ Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus)));
+ }
+ }
+ else {
+ const char *type;
+ const char *action;
+
+ if (WIFSIGNALED(waitStatus)) {
+ type = "CHILDKILLED";
+ action = "killed";
+ }
+ else {
+ type = "CHILDSUSP";
+ action = "suspended";
+ }
+
+ Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1));
+
+#ifdef jim_ext_signal
+ Jim_SetResultFormatted(interp, "child %s by signal %s", action, Jim_SignalId(WTERMSIG(waitStatus)));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, Jim_SignalId(WTERMSIG(waitStatus)), -1));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, Jim_SignalName(WTERMSIG(waitStatus)), -1));
+#else
+ Jim_SetResultFormatted(interp, "child %s by signal %d", action, WTERMSIG(waitStatus));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus)));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid));
+ Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus)));
+#endif
+ }
+ Jim_SetGlobalVariableStr(interp, "errorCode", errorCode);
+ return rc;
+}
+
+/*
+ * Data structures of the following type are used by JimFork and
+ * JimWaitPids to keep track of child processes.
+ */
+
+struct WaitInfo
+{
+ int pid; /* Process id of child. */
+ int status; /* Status returned when child exited or suspended. */
+ int flags; /* Various flag bits; see below for definitions. */
+};
+
+struct WaitInfoTable {
+ struct WaitInfo *info;
+ int size;
+ int used;
+};
+
+/*
+ * Flag bits in WaitInfo structures:
+ *
+ * WI_DETACHED - Non-zero means no-one cares about the
+ * process anymore. Ignore it until it
+ * exits, then forget about it.
+ */
+
+#define WI_DETACHED 2
+
+#define WAIT_TABLE_GROW_BY 4
+
+static void JimFreeWaitInfoTable(struct Jim_Interp *interp, void *privData)
+{
+ struct WaitInfoTable *table = privData;
+
+ Jim_Free(table->info);
+ Jim_Free(table);
+}
+
+static struct WaitInfoTable *JimAllocWaitInfoTable(void)
+{
+ struct WaitInfoTable *table = Jim_Alloc(sizeof(*table));
+ table->info = NULL;
+ table->size = table->used = 0;
+
+ return table;
+}
+
+static int Jim_CreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv,
+ int **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr);
+static void JimDetachPids(Jim_Interp *interp, int numPids, const int *pidPtr);
+static int Jim_CleanupChildren(Jim_Interp *interp, int numPids, int *pidPtr, int errorId);
+
+static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int outputId; /* File id for output pipe. -1
+ * means command overrode. */
+ int errorId; /* File id for temporary file
+ * containing error output. */
+ int *pidPtr;
+ int numPids, result;
+
+ /*
+ * See if the command is to be run in background; if so, create
+ * the command, detach it, and return.
+ */
+ if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) {
+ Jim_Obj *listObj;
+ int i;
+
+ argc--;
+ numPids = Jim_CreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL);
+ if (numPids < 0) {
+ return JIM_ERR;
+ }
+ /* The return value is a list of the pids */
+ listObj = Jim_NewListObj(interp, NULL, 0);
+ for (i = 0; i < numPids; i++) {
+ Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, pidPtr[i]));
+ }
+ Jim_SetResult(interp, listObj);
+ JimDetachPids(interp, numPids, pidPtr);
+ Jim_Free(pidPtr);
+ return JIM_OK;
+ }
+
+ /*
+ * Create the command's pipeline.
+ */
+ numPids =
+ Jim_CreatePipeline(interp, argc - 1, argv + 1, &pidPtr, (int *)NULL, &outputId, &errorId);
+ if (numPids < 0) {
+ return JIM_ERR;
+ }
+
+ /*
+ * Read the child's output (if any) and put it into the result.
+ */
+ Jim_SetResultString(interp, "", 0);
+
+ result = JIM_OK;
+ if (outputId != -1) {
+ result = JimAppendStreamToString(interp, outputId, Jim_GetResult(interp));
+ if (result < 0) {
+ Jim_SetResultErrno(interp, "error reading from output pipe");
+ }
+ close(outputId);
+ }
+
+ if (Jim_CleanupChildren(interp, numPids, pidPtr, errorId) != JIM_OK) {
+ result = JIM_ERR;
+ }
+ return result;
+}
+
+void Jim_ReapDetachedPids(struct WaitInfoTable *table)
+{
+ if (!table) {
+ return;
+ }
+
+ struct WaitInfo *waitPtr;
+ int count;
+
+ for (waitPtr = table->info, count = table->used; count > 0; waitPtr++, count--) {
+ if (waitPtr->flags & WI_DETACHED) {
+ int status;
+ int pid = waitpid(waitPtr->pid, &status, WNOHANG);
+ if (pid > 0) {
+ if (waitPtr != &table->info[table->used - 1]) {
+ *waitPtr = table->info[table->used - 1];
+ }
+ table->used--;
+ }
+ }
+ }
+}
+
+/**
+ * Does waitpid() on the given pid, and then removes the
+ * entry from the wait table.
+ *
+ * Returns the pid if OK and updates *statusPtr with the status,
+ * or -1 if the pid was not in the table.
+ */
+static int JimWaitPid(struct WaitInfoTable *table, int pid, int *statusPtr)
+{
+ int i;
+
+ /* Find it in the table */
+ for (i = 0; i < table->used; i++) {
+ if (pid == table->info[i].pid) {
+ /* wait for it */
+ waitpid(pid, statusPtr, 0);
+
+ /* Remove it from the table */
+ if (i != table->used - 1) {
+ table->info[i] = table->info[table->used - 1];
+ }
+ table->used--;
+ return pid;
+ }
+ }
+
+ /* Not found */
+ return -1;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * JimDetachPids --
+ *
+ * This procedure is called to indicate that one or more child
+ * processes have been placed in background and are no longer
+ * cared about. These children can be cleaned up with JimReapDetachedPids().
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void JimDetachPids(Jim_Interp *interp, int numPids, const int *pidPtr)
+{
+ int j;
+ struct WaitInfoTable *table = Jim_CmdPrivData(interp);
+
+ for (j = 0; j < numPids; j++) {
+ /* Find it in the table */
+ int i;
+ for (i = 0; i < table->used; i++) {
+ if (pidPtr[j] == table->info[i].pid) {
+ table->info[i].flags |= WI_DETACHED;
+ break;
+ }
+ }
+ }
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * Jim_CreatePipeline --
+ *
+ * Given an argc/argv array, instantiate a pipeline of processes
+ * as described by the argv.
+ *
+ * Results:
+ * The return value is a count of the number of new processes
+ * created, or -1 if an error occurred while creating the pipeline.
+ * *pidArrayPtr is filled in with the address of a dynamically
+ * allocated array giving the ids of all of the processes. It
+ * is up to the caller to free this array when it isn't needed
+ * anymore. If inPipePtr is non-NULL, *inPipePtr is filled in
+ * with the file id for the input pipe for the pipeline (if any):
+ * the caller must eventually close this file. If outPipePtr
+ * isn't NULL, then *outPipePtr is filled in with the file id
+ * for the output pipe from the pipeline: the caller must close
+ * this file. If errFilePtr isn't NULL, then *errFilePtr is filled
+ * with a file id that may be used to read error output after the
+ * pipeline completes.
+ *
+ * Side effects:
+ * Processes and pipes are created.
+ *
+ *----------------------------------------------------------------------
+ */
+static int
+Jim_CreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int **pidArrayPtr,
+ int *inPipePtr, int *outPipePtr, int *errFilePtr)
+{
+ int *pidPtr = NULL; /* Points to malloc-ed array holding all
+ * the pids of child processes. */
+ int numPids = 0; /* Actual number of processes that exist
+ * at *pidPtr right now. */
+ int cmdCount; /* Count of number of distinct commands
+ * found in argc/argv. */
+ const char *input = NULL; /* Describes input for pipeline, depending
+ * on "inputFile". NULL means take input
+ * from stdin/pipe. */
+
+#define FILE_NAME 0 /* input/output: filename */
+#define FILE_APPEND 1 /* output only: filename, append */
+#define FILE_HANDLE 2 /* input/output: filehandle */
+#define FILE_TEXT 3 /* input only: input is actual text */
+
+ int inputFile = FILE_NAME; /* 1 means input is name of input file.
+ * 2 means input is filehandle name.
+ * 0 means input holds actual
+ * text to be input to command. */
+
+ int outputFile = FILE_NAME; /* 0 means output is the name of output file.
+ * 1 means output is the name of output file, and append.
+ * 2 means output is filehandle name.
+ * All this is ignored if output is NULL
+ */
+ int errorFile = FILE_NAME; /* 0 means error is the name of error file.
+ * 1 means error is the name of error file, and append.
+ * 2 means error is filehandle name.
+ * All this is ignored if error is NULL
+ */
+ const char *output = NULL; /* Holds name of output file to pipe to,
+ * or NULL if output goes to stdout/pipe. */
+ const char *error = NULL; /* Holds name of stderr file to pipe to,
+ * or NULL if stderr goes to stderr/pipe. */
+ int inputId = -1; /* Readable file id input to current command in
+ * pipeline (could be file or pipe). -1
+ * means use stdin. */
+ int outputId = -1; /* Writable file id for output from current
+ * command in pipeline (could be file or pipe).
+ * -1 means use stdout. */
+ int errorId = -1; /* Writable file id for all standard error
+ * output from all commands in pipeline. -1
+ * means use stderr. */
+ int lastOutputId = -1; /* Write file id for output from last command
+ * in pipeline (could be file or pipe).
+ * -1 means use stdout. */
+ int pipeIds[2]; /* File ids for pipe that's being created. */
+ int firstArg, lastArg; /* Indexes of first and last arguments in
+ * current command. */
+ int lastBar;
+ char *execName;
+ int i, pid;
+ char **orig_environ;
+ struct WaitInfoTable *table = Jim_CmdPrivData(interp);
+
+ /* Holds the args which will be used to exec */
+ char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1));
+ int arg_count = 0;
+
+ Jim_ReapDetachedPids(table);
+
+ if (inPipePtr != NULL) {
+ *inPipePtr = -1;
+ }
+ if (outPipePtr != NULL) {
+ *outPipePtr = -1;
+ }
+ if (errFilePtr != NULL) {
+ *errFilePtr = -1;
+ }
+ pipeIds[0] = pipeIds[1] = -1;
+
+ /*
+ * First, scan through all the arguments to figure out the structure
+ * of the pipeline. Count the number of distinct processes (it's the
+ * number of "|" arguments). If there are "<", "<<", or ">" arguments
+ * then make note of input and output redirection and remove these
+ * arguments and the arguments that follow them.
+ */
+ cmdCount = 1;
+ lastBar = -1;
+ for (i = 0; i < argc; i++) {
+ const char *arg = Jim_String(argv[i]);
+
+ if (arg[0] == '<') {
+ inputFile = FILE_NAME;
+ input = arg + 1;
+ if (*input == '<') {
+ inputFile = FILE_TEXT;
+ input++;
+ }
+ else if (*input == '@') {
+ inputFile = FILE_HANDLE;
+ input++;
+ }
+
+ if (!*input && ++i < argc) {
+ input = Jim_String(argv[i]);
+ }
+ }
+ else if (arg[0] == '>') {
+ int dup_error = 0;
+
+ outputFile = FILE_NAME;
+
+ output = arg + 1;
+ if (*output == '>') {
+ outputFile = FILE_APPEND;
+ output++;
+ }
+ if (*output == '&') {
+ /* Redirect stderr too */
+ output++;
+ dup_error = 1;
+ }
+ if (*output == '@') {
+ outputFile = FILE_HANDLE;
+ output++;
+ }
+ if (!*output && ++i < argc) {
+ output = Jim_String(argv[i]);
+ }
+ if (dup_error) {
+ errorFile = outputFile;
+ error = output;
+ }
+ }
+ else if (arg[0] == '2' && arg[1] == '>') {
+ error = arg + 2;
+ errorFile = FILE_NAME;
+
+ if (*error == '@') {
+ errorFile = FILE_HANDLE;
+ error++;
+ }
+ else if (*error == '>') {
+ errorFile = FILE_APPEND;
+ error++;
+ }
+ if (!*error && ++i < argc) {
+ error = Jim_String(argv[i]);
+ }
+ }
+ else {
+ if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) {
+ if (i == lastBar + 1 || i == argc - 1) {
+ Jim_SetResultString(interp, "illegal use of | or |& in command", -1);
+ goto badargs;
+ }
+ lastBar = i;
+ cmdCount++;
+ }
+ /* Either |, |& or a "normal" arg, so store it in the arg array */
+ arg_array[arg_count++] = (char *)arg;
+ continue;
+ }
+
+ if (i >= argc) {
+ Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg);
+ goto badargs;
+ }
+ }
+
+ if (arg_count == 0) {
+ Jim_SetResultString(interp, "didn't specify command to execute", -1);
+badargs:
+ Jim_Free(arg_array);
+ return -1;
+ }
+
+ /* Must do this before vfork(), so do it now */
+ orig_environ = Jim_GetEnviron();
+ Jim_SetEnviron(JimBuildEnv(interp));
+
+ /*
+ * Set up the redirected input source for the pipeline, if
+ * so requested.
+ */
+ if (input != NULL) {
+ if (inputFile == FILE_TEXT) {
+ /*
+ * Immediate data in command. Create temporary file and
+ * put data into file.
+ */
+
+#define TMP_STDIN_NAME "/tmp/tcl.in.XXXXXX"
+ char inName[sizeof(TMP_STDIN_NAME) + 1];
+ int length;
+
+ strcpy(inName, TMP_STDIN_NAME);
+ inputId = mkstemp(inName);
+ if (inputId < 0) {
+ Jim_SetResultErrno(interp, "couldn't create input file for command");
+ goto error;
+ }
+ length = strlen(input);
+ if (write(inputId, input, length) != length) {
+ Jim_SetResultErrno(interp, "couldn't write file input for command");
+ goto error;
+ }
+ if (lseek(inputId, 0L, SEEK_SET) == -1 || unlink(inName) == -1) {
+ Jim_SetResultErrno(interp, "couldn't reset or remove input file for command");
+ goto error;
+ }
+ }
+ else if (inputFile == FILE_HANDLE) {
+ /* Should be a file descriptor */
+ Jim_Obj *fhObj = Jim_NewStringObj(interp, input, -1);
+ FILE *fh = Jim_AioFilehandle(interp, fhObj);
+
+ Jim_FreeNewObj(interp, fhObj);
+ if (fh == NULL) {
+ goto error;
+ }
+ inputId = dup(fileno(fh));
+ }
+ else {
+ /*
+ * File redirection. Just open the file.
+ */
+ inputId = open(input, O_RDONLY, 0);
+ if (inputId < 0) {
+ Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input,
+ strerror(errno));
+ goto error;
+ }
+ }
+ }
+ else if (inPipePtr != NULL) {
+ if (pipe(pipeIds) != 0) {
+ Jim_SetResultErrno(interp, "couldn't create input pipe for command");
+ goto error;
+ }
+ inputId = pipeIds[0];
+ *inPipePtr = pipeIds[1];
+ pipeIds[0] = pipeIds[1] = -1;
+ }
+
+ /*
+ * Set up the redirected output sink for the pipeline from one
+ * of two places, if requested.
+ */
+ if (output != NULL) {
+ if (outputFile == FILE_HANDLE) {
+ Jim_Obj *fhObj = Jim_NewStringObj(interp, output, -1);
+ FILE *fh = Jim_AioFilehandle(interp, fhObj);
+
+ Jim_FreeNewObj(interp, fhObj);
+ if (fh == NULL) {
+ goto error;
+ }
+ fflush(fh);
+ lastOutputId = dup(fileno(fh));
+ }
+ else {
+ /*
+ * Output is to go to a file.
+ */
+ int mode = O_WRONLY | O_CREAT | O_TRUNC;
+
+ if (outputFile == FILE_APPEND) {
+ mode = O_WRONLY | O_CREAT | O_APPEND;
+ }
+
+ lastOutputId = open(output, mode, 0666);
+ if (lastOutputId < 0) {
+ Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output,
+ strerror(errno));
+ goto error;
+ }
+ }
+ }
+ else if (outPipePtr != NULL) {
+ /*
+ * Output is to go to a pipe.
+ */
+ if (pipe(pipeIds) != 0) {
+ Jim_SetResultErrno(interp, "couldn't create output pipe");
+ goto error;
+ }
+ lastOutputId = pipeIds[1];
+ *outPipePtr = pipeIds[0];
+ pipeIds[0] = pipeIds[1] = -1;
+ }
+
+ /* If we are redirecting stderr with 2>filename or 2>@fileId, then we ignore errFilePtr */
+ if (error != NULL) {
+ if (errorFile == FILE_HANDLE) {
+ if (strcmp(error, "1") == 0) {
+ /* Special 2>@1 */
+ if (lastOutputId >= 0) {
+ errorId = dup(lastOutputId);
+ }
+ else {
+ /* No redirection of stdout, so just use 2>@stdout */
+ error = "stdout";
+ }
+ }
+ if (errorId < 0) {
+ Jim_Obj *fhObj = Jim_NewStringObj(interp, error, -1);
+ FILE *fh = Jim_AioFilehandle(interp, fhObj);
+
+ Jim_FreeNewObj(interp, fhObj);
+ if (fh == NULL) {
+ goto error;
+ }
+ fflush(fh);
+ errorId = dup(fileno(fh));
+ }
+ }
+ else {
+ /*
+ * Output is to go to a file.
+ */
+ int mode = O_WRONLY | O_CREAT | O_TRUNC;
+
+ if (errorFile == FILE_APPEND) {
+ mode = O_WRONLY | O_CREAT | O_APPEND;
+ }
+
+ errorId = open(error, mode, 0666);
+ if (errorId < 0) {
+ Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", error,
+ strerror(errno));
+ }
+ }
+ }
+ else if (errFilePtr != NULL) {
+ /*
+ * Set up the standard error output sink for the pipeline, if
+ * requested. Use a temporary file which is opened, then deleted.
+ * Could potentially just use pipe, but if it filled up it could
+ * cause the pipeline to deadlock: we'd be waiting for processes
+ * to complete before reading stderr, and processes couldn't complete
+ * because stderr was backed up.
+ */
+
+#define TMP_STDERR_NAME "/tmp/tcl.err.XXXXXX"
+ char errName[sizeof(TMP_STDERR_NAME) + 1];
+
+ strcpy(errName, TMP_STDERR_NAME);
+ errorId = mkstemp(errName);
+ if (errorId < 0) {
+ errFileError:
+ Jim_SetResultErrno(interp, "couldn't create error file for command");
+ goto error;
+ }
+ *errFilePtr = open(errName, O_RDONLY, 0);
+ if (*errFilePtr < 0) {
+ goto errFileError;
+ }
+ if (unlink(errName) == -1) {
+ Jim_SetResultErrno(interp, "couldn't remove error file for command");
+ goto error;
+ }
+ }
+
+ /*
+ * Scan through the argc array, forking off a process for each
+ * group of arguments between "|" arguments.
+ */
+
+ pidPtr = (int *)Jim_Alloc(cmdCount * sizeof(*pidPtr));
+ for (i = 0; i < numPids; i++) {
+ pidPtr[i] = -1;
+ }
+ for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) {
+ int pipe_dup_err = 0;
+ int origErrorId = errorId;
+ char execerr[64];
+ int execerrlen;
+
+ for (lastArg = firstArg; lastArg < arg_count; lastArg++) {
+ if (arg_array[lastArg][0] == '|') {
+ if (arg_array[lastArg][1] == '&') {
+ pipe_dup_err = 1;
+ }
+ break;
+ }
+ }
+ /* Replace | with NULL for execv() */
+ arg_array[lastArg] = NULL;
+ if (lastArg == arg_count) {
+ outputId = lastOutputId;
+ }
+ else {
+ if (pipe(pipeIds) != 0) {
+ Jim_SetResultErrno(interp, "couldn't create pipe");
+ goto error;
+ }
+ outputId = pipeIds[1];
+ }
+ execName = arg_array[firstArg];
+
+ /* Now fork the child */
+
+ /*
+ * Disable SIGPIPE signals: if they were allowed, this process
+ * might go away unexpectedly if children misbehave. This code
+ * can potentially interfere with other application code that
+ * expects to handle SIGPIPEs; what's really needed is an
+ * arbiter for signals to allow them to be "shared".
+ */
+ if (table->info == NULL) {
+ (void)signal(SIGPIPE, SIG_IGN);
+ }
+
+ /*
+ * Enlarge the wait table if there isn't enough space for a new
+ * entry.
+ */
+ if (table->used == table->size) {
+ table->size += WAIT_TABLE_GROW_BY;
+ table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info));
+ }
+
+ /* Need to do this befor vfork() */
+ if (pipe_dup_err) {
+ errorId = outputId;
+ }
+
+ /* Need to prep an error message before vfork(), just in case */
+ snprintf(execerr, sizeof(execerr), "couldn't exec \"%s\"", execName);
+ execerrlen = strlen(execerr);
+
+ /*
+ * Make a new process and enter it into the table if the fork
+ * is successful.
+ */
+ pid = vfork();
+ if (pid < 0) {
+ Jim_SetResultErrno(interp, "couldn't fork child process");
+ goto error;
+ }
+ if (pid == 0) {
+ /* Child */
+
+ if (inputId != -1) dup2(inputId, 0);
+ if (outputId != -1) dup2(outputId, 1);
+ if (errorId != -1) dup2(errorId, 2);
+
+ for (i = 3; (i <= outputId) || (i <= inputId) || (i <= errorId); i++) {
+ close(i);
+ }
+
+ execvp(execName, &arg_array[firstArg]);
+
+ /* we really can ignore the error here! */
+ write(2, execerr, execerrlen) < 0 ? -1 : 0;
+ _exit(127);
+ }
+
+ /* parent */
+ table->info[table->used].pid = pid;
+ table->info[table->used].flags = 0;
+ table->used++;
+
+ pidPtr[numPids] = pid;
+
+ /* Restore in case of pipe_dup_err */
+ errorId = origErrorId;
+
+ /*
+ * Close off our copies of file descriptors that were set up for
+ * this child, then set up the input for the next child.
+ */
+
+ if (inputId != -1) {
+ close(inputId);
+ }
+ if (outputId != -1) {
+ close(outputId);
+ }
+ inputId = pipeIds[0];
+ pipeIds[0] = pipeIds[1] = -1;
+ }
+ *pidArrayPtr = pidPtr;
+
+ /*
+ * All done. Cleanup open files lying around and then return.
+ */
+
+ cleanup:
+ if (inputId != -1) {
+ close(inputId);
+ }
+ if (lastOutputId != -1) {
+ close(lastOutputId);
+ }
+ if (errorId != -1) {
+ close(errorId);
+ }
+ Jim_Free(arg_array);
+
+ JimFreeEnv(interp, Jim_GetEnviron(), orig_environ);
+ Jim_SetEnviron(orig_environ);
+
+ return numPids;
+
+ /*
+ * An error occurred. There could have been extra files open, such
+ * as pipes between children. Clean them all up. Detach any child
+ * processes that have been created.
+ */
+
+ error:
+ if ((inPipePtr != NULL) && (*inPipePtr != -1)) {
+ close(*inPipePtr);
+ *inPipePtr = -1;
+ }
+ if ((outPipePtr != NULL) && (*outPipePtr != -1)) {
+ close(*outPipePtr);
+ *outPipePtr = -1;
+ }
+ if ((errFilePtr != NULL) && (*errFilePtr != -1)) {
+ close(*errFilePtr);
+ *errFilePtr = -1;
+ }
+ if (pipeIds[0] != -1) {
+ close(pipeIds[0]);
+ }
+ if (pipeIds[1] != -1) {
+ close(pipeIds[1]);
+ }
+ if (pidPtr != NULL) {
+ for (i = 0; i < numPids; i++) {
+ if (pidPtr[i] != -1) {
+ JimDetachPids(interp, 1, &pidPtr[i]);
+ }
+ }
+ Jim_Free(pidPtr);
+ }
+ numPids = -1;
+ goto cleanup;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * CleanupChildren --
+ *
+ * This is a utility procedure used to wait for child processes
+ * to exit, record information about abnormal exits, and then
+ * collect any stderr output generated by them.
+ *
+ * Results:
+ * The return value is a standard Tcl result. If anything at
+ * weird happened with the child processes, JIM_ERROR is returned
+ * and a message is left in interp->result.
+ *
+ * Side effects:
+ * If the last character of interp->result is a newline, then it
+ * is removed. File errorId gets closed, and pidPtr is freed
+ * back to the storage allocator.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int Jim_CleanupChildren(Jim_Interp *interp, int numPids, int *pidPtr, int errorId)
+{
+ struct WaitInfoTable *table = Jim_CmdPrivData(interp);
+ int result = JIM_OK;
+ int i;
+
+ for (i = 0; i < numPids; i++) {
+ int waitStatus = 0;
+ if (JimWaitPid(table, pidPtr[i], &waitStatus) > 0) {
+ if (JimCheckWaitStatus(interp, pidPtr[i], waitStatus) != JIM_OK) {
+ result = JIM_ERR;
+ }
+ }
+ }
+ Jim_Free(pidPtr);
+
+ /*
+ * Read the standard error file. If there's anything there,
+ * then add the file's contents to the result
+ * string.
+ */
+ if (errorId >= 0) {
+ if (JimAppendStreamToString(interp, errorId, Jim_GetResult(interp)) != JIM_OK) {
+ Jim_SetResultErrno(interp, "error reading from stderr output file");
+ result = JIM_ERR;
+ }
+ close(errorId);
+ }
+
+ JimTrimTrailingNewline(interp);
+
+ return result;
+}
+
+int Jim_execInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "exec", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "exec", Jim_ExecCmd, JimAllocWaitInfoTable(), JimFreeWaitInfoTable);
+ return JIM_OK;
+}
+
+/*
+ * tcl_clock.c
+ *
+ * Implements the clock command
+ */
+
+/* For strptime() */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 500
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <sys/time.h>
+
+
+static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ /* How big is big enough? */
+ char buf[100];
+ time_t t;
+ long seconds;
+ struct tm tm;
+
+ const char *format = "%a %b %d %H:%M:%S %Z %Y";
+
+ if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) {
+ return -1;
+ }
+
+ if (argc == 3) {
+ format = Jim_String(argv[2]);
+ }
+
+ if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) {
+ return JIM_ERR;
+ }
+ t = seconds;
+
+ strftime(buf, sizeof(buf), format, localtime_r(&t, &tm));
+
+ Jim_SetResultString(interp, buf, -1);
+
+ return JIM_OK;
+}
+
+#ifdef HAVE_STRPTIME
+static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ char *pt;
+ struct tm tm;
+ time_t now = time(0);
+
+ if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
+ return -1;
+ }
+
+ /* Initialise with the current date/time */
+ localtime_r(&now, &tm);
+
+ pt = strptime(Jim_String(argv[0]), Jim_String(argv[2]), &tm);
+ if (pt == 0 || *pt != 0) {
+ Jim_SetResultString(interp, "Failed to parse time according to format", -1);
+ return JIM_ERR;
+ }
+
+ /* Now convert into a time_t */
+ Jim_SetResultInt(interp, mktime(&tm));
+
+ return JIM_OK;
+}
+#endif
+
+static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_SetResultInt(interp, time(NULL));
+
+ return JIM_OK;
+}
+
+static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+
+ Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec);
+
+ return JIM_OK;
+}
+
+static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+
+ Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000);
+
+ return JIM_OK;
+}
+
+static const jim_subcmd_type clock_command_table[] = {
+ { .cmd = "seconds",
+ .function = clock_cmd_seconds,
+ .minargs = 0,
+ .maxargs = 0,
+ .description = "Returns the current time as seconds since the epoch"
+ },
+ { .cmd = "clicks",
+ .function = clock_cmd_micros,
+ .minargs = 0,
+ .maxargs = 0,
+ .description = "Returns the current time in 'clicks'"
+ },
+ { .cmd = "microseconds",
+ .function = clock_cmd_micros,
+ .minargs = 0,
+ .maxargs = 0,
+ .description = "Returns the current time in microseconds"
+ },
+ { .cmd = "milliseconds",
+ .function = clock_cmd_millis,
+ .minargs = 0,
+ .maxargs = 0,
+ .description = "Returns the current time in milliseconds"
+ },
+ { .cmd = "format",
+ .args = "seconds ?-format format?",
+ .function = clock_cmd_format,
+ .minargs = 1,
+ .maxargs = 3,
+ .description = "Format the given time"
+ },
+#ifdef HAVE_STRPTIME
+ { .cmd = "scan",
+ .args = "str -format format",
+ .function = clock_cmd_scan,
+ .minargs = 3,
+ .maxargs = 3,
+ .description = "Determine the time according to the given format"
+ },
+#endif
+ { 0 }
+};
+
+int Jim_clockInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL);
+ return JIM_OK;
+}
+
+/*
+ * Implements the array command for jim
+ *
+ * (c) 2008 Steve Bennett <steveb@workware.net.au>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ * Based on code originally from Tcl 6.7:
+ *
+ * Copyright 1987-1991 Regents of the University of California
+ * Permission to use, copy, modify, and distribute this
+ * software and its documentation for any purpose and without
+ * fee is hereby granted, provided that the above copyright
+ * notice appear in all copies. The University of California
+ * makes no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without
+ * express or implied warranty.
+ */
+
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+
+
+static int array_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ /* Just a regular [info exists] */
+ Jim_SetResultInt(interp, Jim_GetVariable(interp, argv[0], 0) != 0);
+ return JIM_OK;
+}
+
+static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ int len;
+ int all = 0;
+ Jim_Obj *resultObj;
+ Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
+ Jim_Obj *dictObj;
+ Jim_Obj **dictValuesObj;
+
+ if (!objPtr) {
+ return JIM_OK;
+ }
+
+ if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) {
+ all = 1;
+ }
+
+ /* If it is a dictionary or list with an even number of elements, nothing else to do */
+ if (all) {
+ if (Jim_IsDict(objPtr) || (Jim_IsList(objPtr) && Jim_ListLength(interp, objPtr) % 2 == 0)) {
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+ }
+
+ if (Jim_DictKeysVector(interp, objPtr, NULL, 0, &dictObj, JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ if (Jim_DictPairs(interp, dictObj, &dictValuesObj, &len) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ if (all) {
+ /* Return the whole array */
+ Jim_SetResult(interp, dictObj);
+ }
+ else {
+ /* Only return the matching values */
+ resultObj = Jim_NewListObj(interp, NULL, 0);
+
+ for (i = 0; i < len; i += 2) {
+ if (Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) {
+ Jim_ListAppendElement(interp, resultObj, dictValuesObj[i]);
+ Jim_ListAppendElement(interp, resultObj, dictValuesObj[i + 1]);
+ }
+ }
+
+ Jim_SetResult(interp, resultObj);
+ }
+ Jim_Free(dictValuesObj);
+ return JIM_OK;
+
+}
+
+static int array_cmd_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
+
+ if (!objPtr) {
+ return JIM_OK;
+ }
+
+ return Jim_DictKeys(interp, objPtr, argc == 1 ? NULL : argv[1]);
+}
+
+static int array_cmd_unset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ int len;
+ Jim_Obj *resultObj;
+ Jim_Obj *objPtr;
+ Jim_Obj *dictObj;
+ Jim_Obj **dictValuesObj;
+
+ if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) {
+ /* Unset the whole array */
+ Jim_UnsetVariable(interp, argv[0], JIM_NONE);
+ return JIM_OK;
+ }
+
+ objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
+
+ if (Jim_DictKeysVector(interp, objPtr, NULL, 0, &dictObj, JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ if (Jim_DictPairs(interp, dictObj, &dictValuesObj, &len) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* Create a new object with the values which don't match */
+ resultObj = Jim_NewDictObj(interp, NULL, 0);
+
+ for (i = 0; i < len; i += 2) {
+ if (!Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) {
+ Jim_DictAddElement(interp, resultObj, dictValuesObj[i], dictValuesObj[i + 1]);
+ }
+ }
+ Jim_Free(dictValuesObj);
+
+ Jim_SetVariable(interp, argv[0], resultObj);
+ return JIM_OK;
+}
+
+static int array_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+ int len = 0;
+
+ /* Not found means zero length */
+ objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
+ if (objPtr) {
+ len = Jim_DictSize(interp, objPtr);
+ if (len < 0) {
+ return JIM_ERR;
+ }
+ }
+
+ Jim_SetResultInt(interp, len);
+
+ return JIM_OK;
+}
+
+static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ int len;
+ int rc = JIM_OK;
+ Jim_Obj *listObj = argv[1];
+
+ if (Jim_GetVariable(interp, argv[0], JIM_NONE) == NULL) {
+ /* Doesn't exist, so just set the list directly */
+ return Jim_SetVariable(interp, argv[0], listObj);
+ }
+
+ len = Jim_ListLength(interp, listObj);
+ if (len % 2) {
+ Jim_SetResultString(interp, "list must have an even number of elements", -1);
+ return JIM_ERR;
+ }
+ for (i = 0; i < len && rc == JIM_OK; i += 2) {
+ Jim_Obj *nameObj;
+ Jim_Obj *valueObj;
+
+ Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE);
+ Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE);
+
+ rc = Jim_SetDictKeysVector(interp, argv[0], &nameObj, 1, valueObj);
+ }
+
+ return rc;
+}
+
+static const jim_subcmd_type array_command_table[] = {
+ { .cmd = "exists",
+ .args = "arrayName",
+ .function = array_cmd_exists,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Does array exist?"
+ },
+ { .cmd = "get",
+ .args = "arrayName ?pattern?",
+ .function = array_cmd_get,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Array contents as name value list"
+ },
+ { .cmd = "names",
+ .args = "arrayName ?pattern?",
+ .function = array_cmd_names,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Array keys as a list"
+ },
+ { .cmd = "set",
+ .args = "arrayName list",
+ .function = array_cmd_set,
+ .minargs = 2,
+ .maxargs = 2,
+ .description = "Set array from list"
+ },
+ { .cmd = "size",
+ .args = "arrayName",
+ .function = array_cmd_size,
+ .minargs = 1,
+ .maxargs = 1,
+ .description = "Number of elements in array"
+ },
+ { .cmd = "unset",
+ .args = "arrayName ?pattern?",
+ .function = array_cmd_unset,
+ .minargs = 1,
+ .maxargs = 2,
+ .description = "Unset elements of an array"
+ },
+ { .cmd = 0,
+ }
+};
+
+int Jim_arrayInit(Jim_Interp *interp)
+{
+ if (Jim_PackageProvide(interp, "array", "1.0", JIM_ERRMSG))
+ return JIM_ERR;
+
+ Jim_CreateCommand(interp, "array", Jim_SubCmdProc, (void *)array_command_table, NULL);
+ return JIM_OK;
+}
+int Jim_InitStaticExtensions(Jim_Interp *interp)
+{
+extern int Jim_bootstrapInit(Jim_Interp *);
+Jim_bootstrapInit(interp);
+extern int Jim_aioInit(Jim_Interp *);
+Jim_aioInit(interp);
+extern int Jim_readdirInit(Jim_Interp *);
+Jim_readdirInit(interp);
+extern int Jim_globInit(Jim_Interp *);
+Jim_globInit(interp);
+extern int Jim_regexpInit(Jim_Interp *);
+Jim_regexpInit(interp);
+extern int Jim_fileInit(Jim_Interp *);
+Jim_fileInit(interp);
+extern int Jim_execInit(Jim_Interp *);
+Jim_execInit(interp);
+extern int Jim_clockInit(Jim_Interp *);
+Jim_clockInit(interp);
+extern int Jim_arrayInit(Jim_Interp *);
+Jim_arrayInit(interp);
+extern int Jim_stdlibInit(Jim_Interp *);
+Jim_stdlibInit(interp);
+extern int Jim_tclcompatInit(Jim_Interp *);
+Jim_tclcompatInit(interp);
+return JIM_OK;
+}
+
+/* Jim - A small embeddable Tcl interpreter
+ *
+ * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
+ * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
+ * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
+ * Copyright 2008,2009 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
+ * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
+ * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
+ * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
+ * Copyright 2008 Steve Bennett <steveb@workware.net.au>
+ * Copyright 2009 Nico Coesel <ncoesel@dealogic.nl>
+ * Copyright 2009 Zachary T Welch zw@superlucidity.net
+ * Copyright 2009 David Brownell
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ **/
+#define JIM_OPTIMIZATION /* comment to avoid optimizations and reduce size */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <limits.h>
+#include <assert.h>
+#include <errno.h>
+#include <time.h>
+#include <setjmp.h>
+
+#include <unistd.h>
+#include <sys/time.h>
+
+
+#ifdef HAVE_BACKTRACE
+#include <execinfo.h>
+#endif
+#ifdef HAVE_CRT_EXTERNS_H
+#include <crt_externs.h>
+#endif
+
+/* For INFINITY, even if math functions are not enabled */
+#include <math.h>
+
+/* For the no-autoconf case */
+#ifndef TCL_LIBRARY
+#define TCL_LIBRARY "."
+#endif
+#ifndef TCL_PLATFORM_OS
+#define TCL_PLATFORM_OS "unknown"
+#endif
+#ifndef TCL_PLATFORM_PLATFORM
+#define TCL_PLATFORM_PLATFORM "unknown"
+#endif
+
+/*#define DEBUG_SHOW_SCRIPT*/
+/*#define DEBUG_SHOW_SCRIPT_TOKENS*/
+/*#define DEBUG_SHOW_SUBST*/
+/*#define DEBUG_SHOW_EXPR*/
+/*#define DEBUG_SHOW_EXPR_TOKENS*/
+/*#define JIM_DEBUG_GC*/
+#ifdef JIM_MAINTAINER
+#define JIM_DEBUG_COMMAND
+#define JIM_DEBUG_PANIC
+#endif
+
+const char *jim_tt_name(int type);
+
+#ifdef JIM_DEBUG_PANIC
+static void JimPanicDump(int panic_condition, Jim_Interp *interp, const char *fmt, ...);
+#define JimPanic(X) JimPanicDump X
+#else
+#define JimPanic(X)
+#endif
+
+/* -----------------------------------------------------------------------------
+ * Global variables
+ * ---------------------------------------------------------------------------*/
+
+/* A shared empty string for the objects string representation.
+ * Jim_InvalidateStringRep knows about it and doesn't try to free it. */
+static char JimEmptyStringRep[] = "";
+
+/* -----------------------------------------------------------------------------
+ * Required prototypes of not exported functions
+ * ---------------------------------------------------------------------------*/
+static void JimChangeCallFrameId(Jim_Interp *interp, Jim_CallFrame *cf);
+static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int flags);
+static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int listindex, Jim_Obj *newObjPtr,
+ int flags);
+static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr);
+static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr);
+static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype,
+ const char *prefix, const char *const *tablePtr, const char *name);
+static void JimDeleteLocalProcs(Jim_Interp *interp);
+static int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, const char *filename, int linenr,
+ int argc, Jim_Obj *const *argv);
+static int JimEvalObjVector(Jim_Interp *interp, int objc, Jim_Obj *const *objv,
+ const char *filename, int linenr);
+static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr);
+static int JimSign(jim_wide w);
+static int JimValidName(Jim_Interp *interp, const char *type, Jim_Obj *nameObjPtr);
+
+static const Jim_HashTableType JimVariablesHashTableType;
+
+/* Fast access to the int (wide) value of an object which is known to be of int type */
+#define JimWideValue(objPtr) (objPtr)->internalRep.wideValue
+
+static int utf8_tounicode_case(const char *s, int *uc, int upper)
+{
+ int l = utf8_tounicode(s, uc);
+ if (upper) {
+ *uc = utf8_upper(*uc);
+ }
+ return l;
+}
+
+/* These can be used in addition to JIM_CASESENS/JIM_NOCASE */
+#define JIM_CHARSET_SCAN 2
+#define JIM_CHARSET_GLOB 0
+
+/**
+ * pattern points to a string like "[^a-z\ub5]"
+ *
+ * The pattern may contain trailing chars, which are ignored.
+ *
+ * The pattern is matched against unicode char 'c'.
+ *
+ * If (flags & JIM_NOCASE), case is ignored when matching.
+ * If (flags & JIM_CHARSET_SCAN), the considers ^ and ] special at the start
+ * of the charset, per scan, rather than glob/string match.
+ *
+ * If the unicode char 'c' matches that set, returns a pointer to the ']' character,
+ * or the null character if the ']' is missing.
+ *
+ * Returns NULL on no match.
+ */
+static const char *JimCharsetMatch(const char *pattern, int c, int flags)
+{
+ int not = 0;
+ int pchar;
+ int match = 0;
+ int nocase = 0;
+
+ if (flags & JIM_NOCASE) {
+ nocase++;
+ c = utf8_upper(c);
+ }
+
+ if (flags & JIM_CHARSET_SCAN) {
+ if (*pattern == '^') {
+ not++;
+ pattern++;
+ }
+
+ /* Special case. If the first char is ']', it is part of the set */
+ if (*pattern == ']') {
+ goto first;
+ }
+ }
+
+ while (*pattern && *pattern != ']') {
+ /* Exact match */
+ if (pattern[0] == '\\') {
+first:
+ pattern += utf8_tounicode_case(pattern, &pchar, nocase);
+ }
+ else {
+ /* Is this a range? a-z */
+ int start;
+ int end;
+
+ pattern += utf8_tounicode_case(pattern, &start, nocase);
+ if (pattern[0] == '-' && pattern[1]) {
+ /* skip '-' */
+ pattern += utf8_tounicode(pattern, &pchar);
+ pattern += utf8_tounicode_case(pattern, &end, nocase);
+
+ /* Handle reversed range too */
+ if ((c >= start && c <= end) || (c >= end && c <= start)) {
+ match = 1;
+ }
+ continue;
+ }
+ pchar = start;
+ }
+
+ if (pchar == c) {
+ match = 1;
+ }
+ }
+ if (not) {
+ match = !match;
+ }
+
+ return match ? pattern : NULL;
+}
+
+/* Glob-style pattern matching. */
+
+/* Note: string *must* be valid UTF-8 sequences
+ * slen is a char length, not byte counts.
+ */
+static int GlobMatch(const char *pattern, const char *string, int nocase)
+{
+ int c;
+ int pchar;
+ while (*pattern) {
+ switch (pattern[0]) {
+ case '*':
+ while (pattern[1] == '*') {
+ pattern++;
+ }
+ pattern++;
+ if (!pattern[0]) {
+ return 1; /* match */
+ }
+ while (*string) {
+ /* Recursive call - Does the remaining pattern match anywhere? */
+ if (GlobMatch(pattern, string, nocase))
+ return 1; /* match */
+ string += utf8_tounicode(string, &c);
+ }
+ return 0; /* no match */
+
+ case '?':
+ string += utf8_tounicode(string, &c);
+ break;
+
+ case '[': {
+ string += utf8_tounicode(string, &c);
+ pattern = JimCharsetMatch(pattern + 1, c, nocase ? JIM_NOCASE : 0);
+ if (!pattern) {
+ return 0;
+ }
+ if (!*pattern) {
+ /* Ran out of pattern (no ']') */
+ continue;
+ }
+ break;
+ }
+ case '\\':
+ if (pattern[1]) {
+ pattern++;
+ }
+ /* fall through */
+ default:
+ string += utf8_tounicode_case(string, &c, nocase);
+ utf8_tounicode_case(pattern, &pchar, nocase);
+ if (pchar != c) {
+ return 0;
+ }
+ break;
+ }
+ pattern += utf8_tounicode_case(pattern, &pchar, nocase);
+ if (!*string) {
+ while (*pattern == '*') {
+ pattern++;
+ }
+ break;
+ }
+ }
+ if (!*pattern && !*string) {
+ return 1;
+ }
+ return 0;
+}
+
+static int JimStringMatch(Jim_Interp *interp, Jim_Obj *patternObj, const char *string, int nocase)
+{
+ return GlobMatch(Jim_String(patternObj), string, nocase);
+}
+
+/**
+ * string comparison works on binary data.
+ *
+ * Note that the lengths are byte lengths, not char lengths.
+ */
+static int JimStringCompare(const char *s1, int l1, const char *s2, int l2)
+{
+ if (l1 < l2) {
+ return memcmp(s1, s2, l1) <= 0 ? -1 : 1;
+ }
+ else if (l2 < l1) {
+ return memcmp(s1, s2, l2) >= 0 ? 1 : -1;
+ }
+ else {
+ return JimSign(memcmp(s1, s2, l1));
+ }
+}
+
+/**
+ * No-case version.
+ *
+ * If maxchars is -1, compares to end of string.
+ * Otherwise compares at most 'maxchars' characters.
+ */
+static int JimStringCompareNoCase(const char *s1, const char *s2, int maxchars)
+{
+ while (*s1 && *s2 && maxchars) {
+ int c1, c2;
+ s1 += utf8_tounicode_case(s1, &c1, 1);
+ s2 += utf8_tounicode_case(s2, &c2, 1);
+ if (c1 != c2) {
+ return JimSign(c1 - c2);
+ }
+ maxchars--;
+ }
+ if (!maxchars) {
+ return 0;
+ }
+ /* One string or both terminated */
+ if (*s1) {
+ return 1;
+ }
+ if (*s2) {
+ return -1;
+ }
+ return 0;
+}
+
+/* Search 's1' inside 's2', starting to search from char 'index' of 's2'.
+ * The index of the first occurrence of s1 in s2 is returned.
+ * If s1 is not found inside s2, -1 is returned. */
+static int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int idx)
+{
+ int i;
+ int l1bytelen;
+
+ if (!l1 || !l2 || l1 > l2) {
+ return -1;
+ }
+ if (idx < 0)
+ idx = 0;
+ s2 += utf8_index(s2, idx);
+
+ l1bytelen = utf8_index(s1, l1);
+
+ for (i = idx; i <= l2 - l1; i++) {
+ int c;
+ if (memcmp(s2, s1, l1bytelen) == 0) {
+ return i;
+ }
+ s2 += utf8_tounicode(s2, &c);
+ }
+ return -1;
+}
+
+/**
+ * Note: Lengths and return value are in bytes, not chars.
+ */
+static int JimStringLast(const char *s1, int l1, const char *s2, int l2)
+{
+ const char *p;
+
+ if (!l1 || !l2 || l1 > l2)
+ return -1;
+
+ /* Now search for the needle */
+ for (p = s2 + l2 - 1; p != s2 - 1; p--) {
+ if (*p == *s1 && memcmp(s1, p, l1) == 0) {
+ return p - s2;
+ }
+ }
+ return -1;
+}
+
+#ifdef JIM_UTF8
+/**
+ * Note: Lengths and return value are in chars.
+ */
+static int JimStringLastUtf8(const char *s1, int l1, const char *s2, int l2)
+{
+ int n = JimStringLast(s1, utf8_index(s1, l1), s2, utf8_index(s2, l2));
+ if (n > 0) {
+ n = utf8_strlen(s2, n);
+ }
+ return n;
+}
+#endif
+
+int Jim_WideToString(char *buf, jim_wide wideValue)
+{
+ const char *fmt = "%" JIM_WIDE_MODIFIER;
+
+ return sprintf(buf, fmt, wideValue);
+}
+
+/**
+ * After an strtol()/strtod()-like conversion,
+ * check whether something was converted and that
+ * the only thing left is white space.
+ *
+ * Returns JIM_OK or JIM_ERR.
+ */
+static int JimCheckConversion(const char *str, const char *endptr)
+{
+ if (str[0] == '\0' || str == endptr) {
+ return JIM_ERR;
+ }
+
+ if (endptr[0] != '\0') {
+ while (*endptr) {
+ if (!isspace(UCHAR(*endptr))) {
+ return JIM_ERR;
+ }
+ endptr++;
+ }
+ }
+ return JIM_OK;
+}
+
+int Jim_StringToWide(const char *str, jim_wide * widePtr, int base)
+{
+ char *endptr;
+
+ *widePtr = strtoull(str, &endptr, base);
+
+ return JimCheckConversion(str, endptr);
+}
+
+int Jim_DoubleToString(char *buf, double doubleValue)
+{
+ int len;
+ char *buf0 = buf;
+
+ len = sprintf(buf, "%.12g", doubleValue);
+
+ /* Add a final ".0" if it's a number. But not
+ * for NaN or InF */
+ while (*buf) {
+ if (*buf == '.' || isalpha(UCHAR(*buf))) {
+ /* inf -> Inf, nan -> Nan */
+ if (*buf == 'i' || *buf == 'n') {
+ *buf = toupper(UCHAR(*buf));
+ }
+ if (*buf == 'I') {
+ /* Infinity -> Inf */
+ buf[3] = '\0';
+ len = buf - buf0 + 3;
+ }
+ return len;
+ }
+ buf++;
+ }
+
+ *buf++ = '.';
+ *buf++ = '0';
+ *buf = '\0';
+
+ return len + 2;
+}
+
+int Jim_StringToDouble(const char *str, double *doublePtr)
+{
+ char *endptr;
+
+ /* Callers can check for underflow via ERANGE */
+ errno = 0;
+
+ *doublePtr = strtod(str, &endptr);
+
+ return JimCheckConversion(str, endptr);
+}
+
+static jim_wide JimPowWide(jim_wide b, jim_wide e)
+{
+ jim_wide i, res = 1;
+
+ if ((b == 0 && e != 0) || (e < 0))
+ return 0;
+ for (i = 0; i < e; i++) {
+ res *= b;
+ }
+ return res;
+}
+
+/* -----------------------------------------------------------------------------
+ * Special functions
+ * ---------------------------------------------------------------------------*/
+#ifdef JIM_DEBUG_PANIC
+/* Note that 'interp' may be NULL if not available in the
+ * context of the panic. It's only useful to get the error
+ * file descriptor, it will default to stderr otherwise. */
+void JimPanicDump(int condition, Jim_Interp *interp, const char *fmt, ...)
+{
+ va_list ap;
+
+ if (!condition) {
+ return;
+ }
+
+ va_start(ap, fmt);
+ /*
+ * Send it here first.. Assuming STDIO still works
+ */
+ fprintf(stderr, JIM_NL "JIM INTERPRETER PANIC: ");
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, JIM_NL JIM_NL);
+ va_end(ap);
+
+#ifdef HAVE_BACKTRACE
+ {
+ void *array[40];
+ int size, i;
+ char **strings;
+
+ size = backtrace(array, 40);
+ strings = backtrace_symbols(array, size);
+ for (i = 0; i < size; i++)
+ fprintf(stderr, "[backtrace] %s" JIM_NL, strings[i]);
+ fprintf(stderr, "[backtrace] Include the above lines and the output" JIM_NL);
+ fprintf(stderr, "[backtrace] of 'nm <executable>' in the bug report." JIM_NL);
+ }
+#endif
+
+ abort();
+}
+#endif
+
+/* -----------------------------------------------------------------------------
+ * Memory allocation
+ * ---------------------------------------------------------------------------*/
+
+void *Jim_Alloc(int size)
+{
+ return malloc(size);
+}
+
+void Jim_Free(void *ptr)
+{
+ free(ptr);
+}
+
+void *Jim_Realloc(void *ptr, int size)
+{
+ return realloc(ptr, size);
+}
+
+char *Jim_StrDup(const char *s)
+{
+ return strdup(s);
+}
+
+char *Jim_StrDupLen(const char *s, int l)
+{
+ char *copy = Jim_Alloc(l + 1);
+
+ memcpy(copy, s, l + 1);
+ copy[l] = 0; /* Just to be sure, original could be substring */
+ return copy;
+}
+
+/* -----------------------------------------------------------------------------
+ * Time related functions
+ * ---------------------------------------------------------------------------*/
+
+/* Returns microseconds of CPU used since start. */
+static jim_wide JimClock(void)
+{
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ return (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec;
+}
+
+/* -----------------------------------------------------------------------------
+ * Hash Tables
+ * ---------------------------------------------------------------------------*/
+
+/* -------------------------- private prototypes ---------------------------- */
+static int JimExpandHashTableIfNeeded(Jim_HashTable *ht);
+static unsigned int JimHashTableNextPower(unsigned int size);
+static int JimInsertHashEntry(Jim_HashTable *ht, const void *key);
+
+/* -------------------------- hash functions -------------------------------- */
+
+/* Thomas Wang's 32 bit Mix Function */
+unsigned int Jim_IntHashFunction(unsigned int key)
+{
+ key += ~(key << 15);
+ key ^= (key >> 10);
+ key += (key << 3);
+ key ^= (key >> 6);
+ key += ~(key << 11);
+ key ^= (key >> 16);
+ return key;
+}
+
+/* Generic hash function (we are using to multiply by 9 and add the byte
+ * as Tcl) */
+unsigned int Jim_GenHashFunction(const unsigned char *buf, int len)
+{
+ unsigned int h = 0;
+
+ while (len--)
+ h += (h << 3) + *buf++;
+ return h;
+}
+
+/* ----------------------------- API implementation ------------------------- */
+
+/* reset a hashtable already initialized with ht_init().
+ * NOTE: This function should only called by ht_destroy(). */
+static void JimResetHashTable(Jim_HashTable *ht)
+{
+ ht->table = NULL;
+ ht->size = 0;
+ ht->sizemask = 0;
+ ht->used = 0;
+ ht->collisions = 0;
+}
+
+/* Initialize the hash table */
+int Jim_InitHashTable(Jim_HashTable *ht, const Jim_HashTableType *type, void *privDataPtr)
+{
+ JimResetHashTable(ht);
+ ht->type = type;
+ ht->privdata = privDataPtr;
+ return JIM_OK;
+}
+
+/* Resize the table to the minimal size that contains all the elements,
+ * but with the invariant of a USER/BUCKETS ration near to <= 1 */
+int Jim_ResizeHashTable(Jim_HashTable *ht)
+{
+ int minimal = ht->used;
+
+ if (minimal < JIM_HT_INITIAL_SIZE)
+ minimal = JIM_HT_INITIAL_SIZE;
+ return Jim_ExpandHashTable(ht, minimal);
+}
+
+/* Expand or create the hashtable */
+int Jim_ExpandHashTable(Jim_HashTable *ht, unsigned int size)
+{
+ Jim_HashTable n; /* the new hashtable */
+ unsigned int realsize = JimHashTableNextPower(size), i;
+
+ /* the size is invalid if it is smaller than the number of
+ * elements already inside the hashtable */
+ if (ht->used >= size)
+ return JIM_ERR;
+
+ Jim_InitHashTable(&n, ht->type, ht->privdata);
+ n.size = realsize;
+ n.sizemask = realsize - 1;
+ n.table = Jim_Alloc(realsize * sizeof(Jim_HashEntry *));
+
+ /* Initialize all the pointers to NULL */
+ memset(n.table, 0, realsize * sizeof(Jim_HashEntry *));
+
+ /* Copy all the elements from the old to the new table:
+ * note that if the old hash table is empty ht->size is zero,
+ * so Jim_ExpandHashTable just creates an hash table. */
+ n.used = ht->used;
+ for (i = 0; i < ht->size && ht->used > 0; i++) {
+ Jim_HashEntry *he, *nextHe;
+
+ if (ht->table[i] == NULL)
+ continue;
+
+ /* For each hash entry on this slot... */
+ he = ht->table[i];
+ while (he) {
+ unsigned int h;
+
+ nextHe = he->next;
+ /* Get the new element index */
+ h = Jim_HashKey(ht, he->key) & n.sizemask;
+ he->next = n.table[h];
+ n.table[h] = he;
+ ht->used--;
+ /* Pass to the next element */
+ he = nextHe;
+ }
+ }
+ assert(ht->used == 0);
+ Jim_Free(ht->table);
+
+ /* Remap the new hashtable in the old */
+ *ht = n;
+ return JIM_OK;
+}
+
+/* Add an element to the target hash table */
+int Jim_AddHashEntry(Jim_HashTable *ht, const void *key, void *val)
+{
+ int idx;
+ Jim_HashEntry *entry;
+
+ /* Get the index of the new element, or -1 if
+ * the element already exists. */
+ if ((idx = JimInsertHashEntry(ht, key)) == -1)
+ return JIM_ERR;
+
+ /* Allocates the memory and stores key */
+ entry = Jim_Alloc(sizeof(*entry));
+ entry->next = ht->table[idx];
+ ht->table[idx] = entry;
+
+ /* Set the hash entry fields. */
+ Jim_SetHashKey(ht, entry, key);
+ Jim_SetHashVal(ht, entry, val);
+ ht->used++;
+ return JIM_OK;
+}
+
+/* Add an element, discarding the old if the key already exists */
+int Jim_ReplaceHashEntry(Jim_HashTable *ht, const void *key, void *val)
+{
+ Jim_HashEntry *entry;
+
+ /* Try to add the element. If the key
+ * does not exists Jim_AddHashEntry will suceed. */
+ if (Jim_AddHashEntry(ht, key, val) == JIM_OK)
+ return JIM_OK;
+ /* It already exists, get the entry */
+ entry = Jim_FindHashEntry(ht, key);
+ /* Free the old value and set the new one */
+ Jim_FreeEntryVal(ht, entry);
+ Jim_SetHashVal(ht, entry, val);
+ return JIM_OK;
+}
+
+/* Search and remove an element */
+int Jim_DeleteHashEntry(Jim_HashTable *ht, const void *key)
+{
+ unsigned int h;
+ Jim_HashEntry *he, *prevHe;
+
+ if (ht->size == 0)
+ return JIM_ERR;
+ h = Jim_HashKey(ht, key) & ht->sizemask;
+ he = ht->table[h];
+
+ prevHe = NULL;
+ while (he) {
+ if (Jim_CompareHashKeys(ht, key, he->key)) {
+ /* Unlink the element from the list */
+ if (prevHe)
+ prevHe->next = he->next;
+ else
+ ht->table[h] = he->next;
+ Jim_FreeEntryKey(ht, he);
+ Jim_FreeEntryVal(ht, he);
+ Jim_Free(he);
+ ht->used--;
+ return JIM_OK;
+ }
+ prevHe = he;
+ he = he->next;
+ }
+ return JIM_ERR; /* not found */
+}
+
+/* Destroy an entire hash table */
+int Jim_FreeHashTable(Jim_HashTable *ht)
+{
+ unsigned int i;
+
+ /* Free all the elements */
+ for (i = 0; i < ht->size && ht->used > 0; i++) {
+ Jim_HashEntry *he, *nextHe;
+
+ if ((he = ht->table[i]) == NULL)
+ continue;
+ while (he) {
+ nextHe = he->next;
+ Jim_FreeEntryKey(ht, he);
+ Jim_FreeEntryVal(ht, he);
+ Jim_Free(he);
+ ht->used--;
+ he = nextHe;
+ }
+ }
+ /* Free the table and the allocated cache structure */
+ Jim_Free(ht->table);
+ /* Re-initialize the table */
+ JimResetHashTable(ht);
+ return JIM_OK; /* never fails */
+}
+
+Jim_HashEntry *Jim_FindHashEntry(Jim_HashTable *ht, const void *key)
+{
+ Jim_HashEntry *he;
+ unsigned int h;
+
+ if (ht->size == 0)
+ return NULL;
+ h = Jim_HashKey(ht, key) & ht->sizemask;
+ he = ht->table[h];
+ while (he) {
+ if (Jim_CompareHashKeys(ht, key, he->key))
+ return he;
+ he = he->next;
+ }
+ return NULL;
+}
+
+Jim_HashTableIterator *Jim_GetHashTableIterator(Jim_HashTable *ht)
+{
+ Jim_HashTableIterator *iter = Jim_Alloc(sizeof(*iter));
+
+ iter->ht = ht;
+ iter->index = -1;
+ iter->entry = NULL;
+ iter->nextEntry = NULL;
+ return iter;
+}
+
+Jim_HashEntry *Jim_NextHashEntry(Jim_HashTableIterator *iter)
+{
+ while (1) {
+ if (iter->entry == NULL) {
+ iter->index++;
+ if (iter->index >= (signed)iter->ht->size)
+ break;
+ iter->entry = iter->ht->table[iter->index];
+ }
+ else {
+ iter->entry = iter->nextEntry;
+ }
+ if (iter->entry) {
+ /* We need to save the 'next' here, the iterator user
+ * may delete the entry we are returning. */
+ iter->nextEntry = iter->entry->next;
+ return iter->entry;
+ }
+ }
+ return NULL;
+}
+
+/* ------------------------- private functions ------------------------------ */
+
+/* Expand the hash table if needed */
+static int JimExpandHashTableIfNeeded(Jim_HashTable *ht)
+{
+ /* If the hash table is empty expand it to the intial size,
+ * if the table is "full" dobule its size. */
+ if (ht->size == 0)
+ return Jim_ExpandHashTable(ht, JIM_HT_INITIAL_SIZE);
+ if (ht->size == ht->used)
+ return Jim_ExpandHashTable(ht, ht->size * 2);
+ return JIM_OK;
+}
+
+/* Our hash table capability is a power of two */
+static unsigned int JimHashTableNextPower(unsigned int size)
+{
+ unsigned int i = JIM_HT_INITIAL_SIZE;
+
+ if (size >= 2147483648U)
+ return 2147483648U;
+ while (1) {
+ if (i >= size)
+ return i;
+ i *= 2;
+ }
+}
+
+/* Returns the index of a free slot that can be populated with
+ * an hash entry for the given 'key'.
+ * If the key already exists, -1 is returned. */
+static int JimInsertHashEntry(Jim_HashTable *ht, const void *key)
+{
+ unsigned int h;
+ Jim_HashEntry *he;
+
+ /* Expand the hashtable if needed */
+ if (JimExpandHashTableIfNeeded(ht) == JIM_ERR)
+ return -1;
+ /* Compute the key hash value */
+ h = Jim_HashKey(ht, key) & ht->sizemask;
+ /* Search if this slot does not already contain the given key */
+ he = ht->table[h];
+ while (he) {
+ if (Jim_CompareHashKeys(ht, key, he->key))
+ return -1;
+ he = he->next;
+ }
+ return h;
+}
+
+/* ----------------------- StringCopy Hash Table Type ------------------------*/
+
+static unsigned int JimStringCopyHTHashFunction(const void *key)
+{
+ return Jim_GenHashFunction(key, strlen(key));
+}
+
+static const void *JimStringCopyHTKeyDup(void *privdata, const void *key)
+{
+ int len = strlen(key);
+ char *copy = Jim_Alloc(len + 1);
+
+ JIM_NOTUSED(privdata);
+
+ memcpy(copy, key, len);
+ copy[len] = '\0';
+ return copy;
+}
+
+static void *JimStringKeyValCopyHTValDup(void *privdata, const void *val)
+{
+ int len = strlen(val);
+ char *copy = Jim_Alloc(len + 1);
+
+ JIM_NOTUSED(privdata);
+
+ memcpy(copy, val, len);
+ copy[len] = '\0';
+ return copy;
+}
+
+static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, const void *key2)
+{
+ JIM_NOTUSED(privdata);
+
+ return strcmp(key1, key2) == 0;
+}
+
+static void JimStringCopyHTKeyDestructor(void *privdata, const void *key)
+{
+ JIM_NOTUSED(privdata);
+
+ Jim_Free((void *)key); /* ATTENTION: const cast */
+}
+
+static void JimStringKeyValCopyHTValDestructor(void *privdata, void *val)
+{
+ JIM_NOTUSED(privdata);
+
+ Jim_Free((void *)val); /* ATTENTION: const cast */
+}
+
+#if 0
+static Jim_HashTableType JimStringCopyHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ JimStringCopyHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ NULL /* val destructor */
+};
+#endif
+
+/* This is like StringCopy but does not auto-duplicate the key.
+ * It's used for intepreter's shared strings. */
+static const Jim_HashTableType JimSharedStringsHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ NULL, /* key dup */
+ NULL, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ NULL /* val destructor */
+};
+
+/* This is like StringCopy but also automatically handle dynamic
+ * allocated C strings as values. */
+static const Jim_HashTableType JimStringKeyValCopyHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ JimStringCopyHTKeyDup, /* key dup */
+ JimStringKeyValCopyHTValDup, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ JimStringKeyValCopyHTValDestructor, /* val destructor */
+};
+
+typedef struct AssocDataValue
+{
+ Jim_InterpDeleteProc *delProc;
+ void *data;
+} AssocDataValue;
+
+static void JimAssocDataHashTableValueDestructor(void *privdata, void *data)
+{
+ AssocDataValue *assocPtr = (AssocDataValue *) data;
+
+ if (assocPtr->delProc != NULL)
+ assocPtr->delProc((Jim_Interp *)privdata, assocPtr->data);
+ Jim_Free(data);
+}
+
+static const Jim_HashTableType JimAssocDataHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ JimStringCopyHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ JimAssocDataHashTableValueDestructor /* val destructor */
+};
+
+/* -----------------------------------------------------------------------------
+ * Stack - This is a simple generic stack implementation. It is used for
+ * example in the 'expr' expression compiler.
+ * ---------------------------------------------------------------------------*/
+void Jim_InitStack(Jim_Stack *stack)
+{
+ stack->len = 0;
+ stack->maxlen = 0;
+ stack->vector = NULL;
+}
+
+void Jim_FreeStack(Jim_Stack *stack)
+{
+ Jim_Free(stack->vector);
+}
+
+int Jim_StackLen(Jim_Stack *stack)
+{
+ return stack->len;
+}
+
+void Jim_StackPush(Jim_Stack *stack, void *element)
+{
+ int neededLen = stack->len + 1;
+
+ if (neededLen > stack->maxlen) {
+ stack->maxlen = neededLen < 20 ? 20 : neededLen * 2;
+ stack->vector = Jim_Realloc(stack->vector, sizeof(void *) * stack->maxlen);
+ }
+ stack->vector[stack->len] = element;
+ stack->len++;
+}
+
+void *Jim_StackPop(Jim_Stack *stack)
+{
+ if (stack->len == 0)
+ return NULL;
+ stack->len--;
+ return stack->vector[stack->len];
+}
+
+void *Jim_StackPeek(Jim_Stack *stack)
+{
+ if (stack->len == 0)
+ return NULL;
+ return stack->vector[stack->len - 1];
+}
+
+void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc) (void *ptr))
+{
+ int i;
+
+ for (i = 0; i < stack->len; i++)
+ freeFunc(stack->vector[i]);
+}
+
+/* -----------------------------------------------------------------------------
+ * Parser
+ * ---------------------------------------------------------------------------*/
+
+/* Token types */
+#define JIM_TT_NONE 0 /* No token returned */
+#define JIM_TT_STR 1 /* simple string */
+#define JIM_TT_ESC 2 /* string that needs escape chars conversion */
+#define JIM_TT_VAR 3 /* var substitution */
+#define JIM_TT_DICTSUGAR 4 /* Syntax sugar for [dict get], $foo(bar) */
+#define JIM_TT_CMD 5 /* command substitution */
+/* Note: Keep these three together for TOKEN_IS_SEP() */
+#define JIM_TT_SEP 6 /* word separator. arg is # of tokens. -ve if {*} */
+#define JIM_TT_EOL 7 /* line separator */
+#define JIM_TT_EOF 8 /* end of script */
+
+#define JIM_TT_LINE 9 /* special 'start-of-line' token. arg is # of arguments to the command. -ve if {*} */
+#define JIM_TT_WORD 10 /* special 'start-of-word' token. arg is # of tokens to combine. -ve if {*} */
+
+/* Additional token types needed for expressions */
+#define JIM_TT_SUBEXPR_START 11
+#define JIM_TT_SUBEXPR_END 12
+#define JIM_TT_EXPR_INT 13
+#define JIM_TT_EXPR_DOUBLE 14
+
+#define JIM_TT_EXPRSUGAR 15 /* $(expression) */
+
+/* Operator token types start here */
+#define JIM_TT_EXPR_OP 20
+
+#define TOKEN_IS_SEP(type) (type >= JIM_TT_SEP && type <= JIM_TT_EOF)
+
+/* Parser states */
+#define JIM_PS_DEF 0 /* Default state */
+#define JIM_PS_QUOTE 1 /* Inside "" */
+#define JIM_PS_DICTSUGAR 2 /* Tokenising abc(def) into 4 separate tokens */
+
+/* Parser context structure. The same context is used both to parse
+ * Tcl scripts and lists. */
+struct JimParserCtx
+{
+ const char *prg; /* Program text */
+ const char *p; /* Pointer to the point of the program we are parsing */
+ int len; /* Left length of 'prg' */
+ int linenr; /* Current line number */
+ const char *tstart;
+ const char *tend; /* Returned token is at tstart-tend in 'prg'. */
+ int tline; /* Line number of the returned token */
+ int tt; /* Token type */
+ int eof; /* Non zero if EOF condition is true. */
+ int state; /* Parser state */
+ int comment; /* Non zero if the next chars may be a comment. */
+ char missing; /* At end of parse, ' ' if complete, '{' if braces incomplete, '"' if quotes incomplete */
+};
+
+#define JimParserEof(c) ((c)->eof)
+#define JimParserTstart(c) ((c)->tstart)
+#define JimParserTend(c) ((c)->tend)
+#define JimParserTtype(c) ((c)->tt)
+#define JimParserTline(c) ((c)->tline)
+
+static int JimParseScript(struct JimParserCtx *pc);
+static int JimParseSep(struct JimParserCtx *pc);
+static int JimParseEol(struct JimParserCtx *pc);
+static int JimParseCmd(struct JimParserCtx *pc);
+static int JimParseVar(struct JimParserCtx *pc);
+static int JimParseBrace(struct JimParserCtx *pc);
+static int JimParseStr(struct JimParserCtx *pc);
+static int JimParseComment(struct JimParserCtx *pc);
+static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc);
+
+/* Initialize a parser context.
+ * 'prg' is a pointer to the program text, linenr is the line
+ * number of the first line contained in the program. */
+static void JimParserInit(struct JimParserCtx *pc, const char *prg, int len, int linenr)
+{
+ pc->prg = prg;
+ pc->p = prg;
+ pc->len = len;
+ pc->tstart = NULL;
+ pc->tend = NULL;
+ pc->tline = 0;
+ pc->tt = JIM_TT_NONE;
+ pc->eof = 0;
+ pc->state = JIM_PS_DEF;
+ pc->linenr = linenr;
+ pc->comment = 1;
+ pc->missing = ' ';
+}
+
+static int JimParseScript(struct JimParserCtx *pc)
+{
+ while (1) { /* the while is used to reiterate with continue if needed */
+ if (!pc->len) {
+ pc->tstart = pc->p;
+ pc->tend = pc->p - 1;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_EOL;
+ pc->eof = 1;
+ return JIM_OK;
+ }
+ switch (*(pc->p)) {
+ case '\\':
+ if (*(pc->p + 1) == '\n' && pc->state == JIM_PS_DEF) {
+ return JimParseSep(pc);
+ }
+ else {
+ pc->comment = 0;
+ return JimParseStr(pc);
+ }
+ break;
+ case ' ':
+ case '\t':
+ case '\r':
+ if (pc->state == JIM_PS_DEF)
+ return JimParseSep(pc);
+ else {
+ pc->comment = 0;
+ return JimParseStr(pc);
+ }
+ break;
+ case '\n':
+ case ';':
+ pc->comment = 1;
+ if (pc->state == JIM_PS_DEF)
+ return JimParseEol(pc);
+ else
+ return JimParseStr(pc);
+ break;
+ case '[':
+ pc->comment = 0;
+ return JimParseCmd(pc);
+ break;
+ case '$':
+ pc->comment = 0;
+ if (JimParseVar(pc) == JIM_ERR) {
+ pc->tstart = pc->tend = pc->p++;
+ pc->len--;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_STR;
+ return JIM_OK;
+ }
+ else
+ return JIM_OK;
+ break;
+ case '#':
+ if (pc->comment) {
+ JimParseComment(pc);
+ continue;
+ }
+ else {
+ return JimParseStr(pc);
+ }
+ default:
+ pc->comment = 0;
+ return JimParseStr(pc);
+ break;
+ }
+ return JIM_OK;
+ }
+}
+
+static int JimParseSep(struct JimParserCtx *pc)
+{
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (*pc->p == ' ' || *pc->p == '\t' || *pc->p == '\r' ||
+ (*pc->p == '\\' && *(pc->p + 1) == '\n')) {
+ if (*pc->p == '\\') {
+ pc->p++;
+ pc->len--;
+ pc->linenr++;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_SEP;
+ return JIM_OK;
+}
+
+static int JimParseEol(struct JimParserCtx *pc)
+{
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (*pc->p == ' ' || *pc->p == '\n' || *pc->p == '\t' || *pc->p == '\r' || *pc->p == ';') {
+ if (*pc->p == '\n')
+ pc->linenr++;
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_EOL;
+ return JIM_OK;
+}
+
+static int JimParseCmd(struct JimParserCtx *pc)
+{
+ int level = 1;
+ int quoted = 0;
+
+ pc->tstart = ++pc->p;
+ pc->len--;
+ pc->tline = pc->linenr;
+ while (pc->len) {
+ if (*pc->p == '\\' && pc->len > 1) {
+ if (pc->p[1] == '\n')
+ pc->linenr++;
+
+ pc->p += 2;
+ pc->len -= 2;
+ continue;
+ }
+ else if (*pc->p == '"') {
+ quoted = !quoted;
+ }
+ else if (!quoted) {
+ if (*pc->p == '[') {
+ level++;
+ }
+ else if (*pc->p == ']') {
+ level--;
+ if (!level)
+ break;
+ }
+ else if (*pc->p == '{') {
+ /* Save and restore tstart and tline across JimParseBrace() */
+ const char * tstart = pc->tstart;
+ int tline = pc->tline;
+
+ JimParseBrace(pc);
+
+ pc->tstart = tstart;
+ pc->tline = tline;
+ continue;
+ }
+ }
+ if (*pc->p == '\n') {
+ pc->linenr++;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_CMD;
+ if (*pc->p == ']') {
+ pc->p++;
+ pc->len--;
+ }
+ return JIM_OK;
+}
+
+static int JimParseVar(struct JimParserCtx *pc)
+{
+ int brace = 0, stop = 0;
+ int ttype = JIM_TT_VAR;
+
+ pc->tstart = ++pc->p;
+ pc->len--; /* skip the $ */
+ pc->tline = pc->linenr;
+ if (*pc->p == '{') {
+ pc->tstart = ++pc->p;
+ pc->len--;
+ brace = 1;
+ }
+ if (brace) {
+ while (!stop) {
+ if (*pc->p == '}' || pc->len == 0) {
+ pc->tend = pc->p - 1;
+ stop = 1;
+ if (pc->len == 0)
+ break;
+ }
+ else if (*pc->p == '\n')
+ pc->linenr++;
+ pc->p++;
+ pc->len--;
+ }
+ }
+ else {
+ while (!stop) {
+ /* Skip double colon, but not single colon! */
+ if (pc->p[0] == ':' && pc->len > 1 && pc->p[1] == ':') {
+ pc->p += 2;
+ pc->len -= 2;
+ continue;
+ }
+ if (!((*pc->p >= 'a' && *pc->p <= 'z') ||
+ (*pc->p >= 'A' && *pc->p <= 'Z') ||
+ (*pc->p >= '0' && *pc->p <= '9') || *pc->p == '_'))
+ stop = 1;
+ else {
+ pc->p++;
+ pc->len--;
+ }
+ }
+ /* Parse [dict get] syntax sugar. */
+ if (*pc->p == '(') {
+ int count = 1;
+ const char *paren = NULL;
+
+ while (count && pc->len) {
+ pc->p++;
+ pc->len--;
+ if (*pc->p == '\\' && pc->len >= 1) {
+ pc->p++;
+ pc->len--;
+ }
+ else if (*pc->p == '(') {
+ count++;
+ }
+ else if (*pc->p == ')') {
+ paren = pc->p;
+ count--;
+ }
+ }
+ if (count == 0) {
+ pc->p++;
+ pc->len--;
+ }
+ else if (paren) {
+ /* Did not find a matching paren. Back up */
+ paren++;
+ pc->len += (pc->p - paren);
+ pc->p = paren;
+ }
+ ttype = (*pc->tstart == '(') ? JIM_TT_EXPRSUGAR : JIM_TT_DICTSUGAR;
+ }
+ pc->tend = pc->p - 1;
+ }
+ /* Check if we parsed just the '$' character.
+ * That's not a variable so an error is returned
+ * to tell the state machine to consider this '$' just
+ * a string. */
+ if (pc->tstart == pc->p) {
+ pc->p--;
+ pc->len++;
+ return JIM_ERR;
+ }
+ pc->tt = ttype;
+ return JIM_OK;
+}
+
+static int JimParseBrace(struct JimParserCtx *pc)
+{
+ int level = 1;
+
+ pc->tstart = ++pc->p;
+ pc->len--;
+ pc->tline = pc->linenr;
+ while (1) {
+ if (*pc->p == '\\' && pc->len >= 2) {
+ pc->p++;
+ pc->len--;
+ if (*pc->p == '\n')
+ pc->linenr++;
+ }
+ else if (*pc->p == '{') {
+ level++;
+ }
+ else if (pc->len == 0 || *pc->p == '}') {
+ if (pc->len == 0) {
+ pc->missing = '{';
+ /*printf("Missing brace at line %d, opened on line %d\n", pc->linenr, pc->tline);*/
+ }
+ level--;
+ if (pc->len == 0 || level == 0) {
+ pc->tend = pc->p - 1;
+ if (pc->len != 0) {
+ pc->p++;
+ pc->len--;
+ }
+ pc->tt = JIM_TT_STR;
+ return JIM_OK;
+ }
+ }
+ else if (*pc->p == '\n') {
+ pc->linenr++;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ return JIM_OK; /* unreached */
+}
+
+static int JimParseStr(struct JimParserCtx *pc)
+{
+ int newword = (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL ||
+ pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR);
+ if (newword && *pc->p == '{') {
+ return JimParseBrace(pc);
+ }
+ else if (newword && *pc->p == '"') {
+ pc->state = JIM_PS_QUOTE;
+ pc->p++;
+ pc->len--;
+ }
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (1) {
+ if (pc->len == 0) {
+ if (pc->state == JIM_PS_QUOTE) {
+ pc->missing = '"';
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+ }
+ switch (*pc->p) {
+ case '\\':
+ if (pc->state == JIM_PS_DEF && *(pc->p + 1) == '\n') {
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+ }
+ if (pc->len >= 2) {
+ if (*(pc->p + 1) == '\n') {
+ pc->linenr++;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ break;
+ case '(':
+ /* If the following token is not '$' just keep going */
+ if (pc->len > 1 && pc->p[1] != '$') {
+ break;
+ }
+ case ')':
+ /* Only need a separate ')' token if the previous was a var */
+ if (*pc->p == '(' || pc->tt == JIM_TT_VAR) {
+ if (pc->p == pc->tstart) {
+ /* At the start of the token, so just return this char */
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+ }
+ break;
+
+ case '$':
+ case '[':
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ case ';':
+ if (pc->state == JIM_PS_DEF) {
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+ }
+ else if (*pc->p == '\n') {
+ pc->linenr++;
+ }
+ break;
+ case '"':
+ if (pc->state == JIM_PS_QUOTE) {
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ pc->p++;
+ pc->len--;
+ pc->state = JIM_PS_DEF;
+ return JIM_OK;
+ }
+ break;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ return JIM_OK; /* unreached */
+}
+
+static int JimParseComment(struct JimParserCtx *pc)
+{
+ while (*pc->p) {
+ if (*pc->p == '\n') {
+ pc->linenr++;
+ if (*(pc->p - 1) != '\\') {
+ pc->p++;
+ pc->len--;
+ return JIM_OK;
+ }
+ }
+ pc->p++;
+ pc->len--;
+ }
+ return JIM_OK;
+}
+
+/* xdigitval and odigitval are helper functions for JimEscape() */
+static int xdigitval(int c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return -1;
+}
+
+static int odigitval(int c)
+{
+ if (c >= '0' && c <= '7')
+ return c - '0';
+ return -1;
+}
+
+/* Perform Tcl escape substitution of 's', storing the result
+ * string into 'dest'. The escaped string is guaranteed to
+ * be the same length or shorted than the source string.
+ * Slen is the length of the string at 's', if it's -1 the string
+ * length will be calculated by the function.
+ *
+ * The function returns the length of the resulting string. */
+static int JimEscape(char *dest, const char *s, int slen)
+{
+ char *p = dest;
+ int i, len;
+
+ if (slen == -1)
+ slen = strlen(s);
+
+ for (i = 0; i < slen; i++) {
+ switch (s[i]) {
+ case '\\':
+ switch (s[i + 1]) {
+ case 'a':
+ *p++ = 0x7;
+ i++;
+ break;
+ case 'b':
+ *p++ = 0x8;
+ i++;
+ break;
+ case 'f':
+ *p++ = 0xc;
+ i++;
+ break;
+ case 'n':
+ *p++ = 0xa;
+ i++;
+ break;
+ case 'r':
+ *p++ = 0xd;
+ i++;
+ break;
+ case 't':
+ *p++ = 0x9;
+ i++;
+ break;
+ case 'u':
+ case 'x':
+ /* A unicode or hex sequence.
+ * \u Expect 1-4 hex chars and convert to utf-8.
+ * \x Expect 1-2 hex chars and convert to hex.
+ * An invalid sequence means simply the escaped char.
+ */
+ {
+ int val = 0;
+ int k;
+
+ i++;
+
+ for (k = 0; k < (s[i] == 'u' ? 4 : 2); k++) {
+ int c = xdigitval(s[i + k + 1]);
+ if (c == -1) {
+ break;
+ }
+ val = (val << 4) | c;
+ }
+ if (k) {
+ /* Got a valid sequence, so convert */
+ if (s[i] == 'u') {
+ p += utf8_fromunicode(p, val);
+ }
+ else {
+ *p++ = val;
+ }
+ i += k;
+ break;
+ }
+ /* Not a valid codepoint, just an escaped char */
+ *p++ = s[i];
+ }
+ break;
+ case 'v':
+ *p++ = 0xb;
+ i++;
+ break;
+ case '\0':
+ *p++ = '\\';
+ i++;
+ break;
+ case '\n':
+ /* Replace all spaces and tabs after backslash newline with a single space*/
+ *p++ = ' ';
+ do {
+ i++;
+ } while (s[i + 1] == ' ' || s[i + 1] == '\t');
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ /* octal escape */
+ {
+ int val = 0;
+ int c = odigitval(s[i + 1]);
+
+ val = c;
+ c = odigitval(s[i + 2]);
+ if (c == -1) {
+ *p++ = val;
+ i++;
+ break;
+ }
+ val = (val * 8) + c;
+ c = odigitval(s[i + 3]);
+ if (c == -1) {
+ *p++ = val;
+ i += 2;
+ break;
+ }
+ val = (val * 8) + c;
+ *p++ = val;
+ i += 3;
+ }
+ break;
+ default:
+ *p++ = s[i + 1];
+ i++;
+ break;
+ }
+ break;
+ default:
+ *p++ = s[i];
+ break;
+ }
+ }
+ len = p - dest;
+ *p = '\0';
+ return len;
+}
+
+/* Returns a dynamically allocated copy of the current token in the
+ * parser context. The function performs conversion of escapes if
+ * the token is of type JIM_TT_ESC.
+ *
+ * Note that after the conversion, tokens that are grouped with
+ * braces in the source code, are always recognizable from the
+ * identical string obtained in a different way from the type.
+ *
+ * For example the string:
+ *
+ * {*}$a
+ *
+ * will return as first token "*", of type JIM_TT_STR
+ *
+ * While the string:
+ *
+ * *$a
+ *
+ * will return as first token "*", of type JIM_TT_ESC
+ */
+static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc)
+{
+ const char *start, *end;
+ char *token;
+ int len;
+
+ start = JimParserTstart(pc);
+ end = JimParserTend(pc);
+ if (start > end) {
+ len = 0;
+ token = Jim_Alloc(1);
+ token[0] = '\0';
+ }
+ else {
+ len = (end - start) + 1;
+ token = Jim_Alloc(len + 1);
+ if (JimParserTtype(pc) != JIM_TT_ESC) {
+ /* No escape conversion needed? Just copy it. */
+ memcpy(token, start, len);
+ token[len] = '\0';
+ }
+ else {
+ /* Else convert the escape chars. */
+ len = JimEscape(token, start, len);
+ }
+ }
+
+ return Jim_NewStringObjNoAlloc(interp, token, len);
+}
+
+/* Parses the given string to determine if it represents a complete script.
+ *
+ * This is useful for interactive shells implementation, for [info complete]
+ * and is used by source/Jim_EvalFile().
+ *
+ * If 'stateCharPtr' != NULL, the function stores ' ' on complete script,
+ * '{' on scripts incomplete missing one or more '}' to be balanced.
+ * '"' on scripts incomplete missing a '"' char.
+ *
+ * If the script is complete, 1 is returned, otherwise 0.
+ */
+int Jim_ScriptIsComplete(const char *s, int len, char *stateCharPtr)
+{
+ struct JimParserCtx parser;
+
+ JimParserInit(&parser, s, len, 1);
+ while (!JimParserEof(&parser)) {
+ JimParseScript(&parser);
+ }
+ if (stateCharPtr) {
+ *stateCharPtr = parser.missing;
+ }
+ return parser.missing == ' ';
+}
+
+/* -----------------------------------------------------------------------------
+ * Tcl Lists parsing
+ * ---------------------------------------------------------------------------*/
+static int JimParseListSep(struct JimParserCtx *pc);
+static int JimParseListStr(struct JimParserCtx *pc);
+static int JimParseListQuote(struct JimParserCtx *pc);
+
+static int JimParseList(struct JimParserCtx *pc)
+{
+ if (pc->len == 0) {
+ pc->tstart = pc->tend = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_EOL;
+ pc->eof = 1;
+ return JIM_OK;
+ }
+ switch (*pc->p) {
+ case ' ':
+ case '\n':
+ case '\t':
+ case '\r':
+ return JimParseListSep(pc);
+
+ case '"':
+ return JimParseListQuote(pc);
+
+ case '{':
+ return JimParseBrace(pc);
+
+ default:
+ return JimParseListStr(pc);
+ }
+ return JIM_OK;
+}
+
+static int JimParseListSep(struct JimParserCtx *pc)
+{
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (*pc->p == ' ' || *pc->p == '\t' || *pc->p == '\r' || *pc->p == '\n') {
+ if (*pc->p == '\n') {
+ pc->linenr++;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_SEP;
+ return JIM_OK;
+}
+
+static int JimParseListQuote(struct JimParserCtx *pc)
+{
+ pc->p++;
+ pc->len--;
+
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_STR;
+
+ while (pc->len) {
+ switch (*pc->p) {
+ case '$':
+ case '[':
+ pc->tt = JIM_TT_ESC;
+ break;
+ case '\\':
+ pc->tt = JIM_TT_ESC;
+ if (--pc->len == 0) {
+ /* Trailing backslash */
+ pc->tend = pc->p;
+ return JIM_OK;
+ }
+ pc->p++;
+ break;
+ case '\n':
+ pc->linenr++;
+ break;
+ case '"':
+ pc->tend = pc->p - 1;
+ pc->p++;
+ pc->len--;
+ return JIM_OK;
+ }
+ pc->p++;
+ pc->len--;
+ }
+
+ pc->tend = pc->p - 1;
+ return JIM_OK;
+}
+
+static int JimParseListStr(struct JimParserCtx *pc)
+{
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_STR;
+
+ while (pc->len) {
+ switch (*pc->p) {
+ case '$':
+ case '[':
+ pc->tt = JIM_TT_ESC;
+ break;
+ case '\\':
+ pc->tt = JIM_TT_ESC;
+ if (--pc->len == 0) {
+ /* Trailing backslash */
+ pc->tend = pc->p;
+ return JIM_OK;
+ }
+ pc->p++;
+ break;
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ pc->tend = pc->p - 1;
+ return JIM_OK;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Jim_Obj related functions
+ * ---------------------------------------------------------------------------*/
+
+/* Return a new initialized object. */
+Jim_Obj *Jim_NewObj(Jim_Interp *interp)
+{
+ Jim_Obj *objPtr;
+
+ /* -- Check if there are objects in the free list -- */
+ if (interp->freeList != NULL) {
+ /* -- Unlink the object from the free list -- */
+ objPtr = interp->freeList;
+ interp->freeList = objPtr->nextObjPtr;
+ }
+ else {
+ /* -- No ready to use objects: allocate a new one -- */
+ objPtr = Jim_Alloc(sizeof(*objPtr));
+ }
+
+ /* Object is returned with refCount of 0. Every
+ * kind of GC implemented should take care to don't try
+ * to scan objects with refCount == 0. */
+ objPtr->refCount = 0;
+ /* All the other fields are left not initialized to save time.
+ * The caller will probably want to set them to the right
+ * value anyway. */
+
+ /* -- Put the object into the live list -- */
+ objPtr->prevObjPtr = NULL;
+ objPtr->nextObjPtr = interp->liveList;
+ if (interp->liveList)
+ interp->liveList->prevObjPtr = objPtr;
+ interp->liveList = objPtr;
+
+ return objPtr;
+}
+
+/* Free an object. Actually objects are never freed, but
+ * just moved to the free objects list, where they will be
+ * reused by Jim_NewObj(). */
+void Jim_FreeObj(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ /* Check if the object was already freed, panic. */
+ JimPanic((objPtr->refCount != 0, interp, "!!!Object %p freed with bad refcount %d, type=%s", objPtr,
+ objPtr->refCount, objPtr->typePtr ? objPtr->typePtr->name : "<none>"));
+
+ /* Free the internal representation */
+ Jim_FreeIntRep(interp, objPtr);
+ /* Free the string representation */
+ if (objPtr->bytes != NULL) {
+ if (objPtr->bytes != JimEmptyStringRep)
+ Jim_Free(objPtr->bytes);
+ }
+ /* Unlink the object from the live objects list */
+ if (objPtr->prevObjPtr)
+ objPtr->prevObjPtr->nextObjPtr = objPtr->nextObjPtr;
+ if (objPtr->nextObjPtr)
+ objPtr->nextObjPtr->prevObjPtr = objPtr->prevObjPtr;
+ if (interp->liveList == objPtr)
+ interp->liveList = objPtr->nextObjPtr;
+ /* Link the object into the free objects list */
+ objPtr->prevObjPtr = NULL;
+ objPtr->nextObjPtr = interp->freeList;
+ if (interp->freeList)
+ interp->freeList->prevObjPtr = objPtr;
+ interp->freeList = objPtr;
+ objPtr->refCount = -1;
+}
+
+/* Invalidate the string representation of an object. */
+void Jim_InvalidateStringRep(Jim_Obj *objPtr)
+{
+ if (objPtr->bytes != NULL) {
+ if (objPtr->bytes != JimEmptyStringRep)
+ Jim_Free(objPtr->bytes);
+ }
+ objPtr->bytes = NULL;
+}
+
+#define Jim_SetStringRep(o, b, l) \
+ do { (o)->bytes = b; (o)->length = l; } while (0)
+
+/* Set the initial string representation for an object.
+ * Does not try to free an old one. */
+void Jim_InitStringRep(Jim_Obj *objPtr, const char *bytes, int length)
+{
+ if (length == 0) {
+ objPtr->bytes = JimEmptyStringRep;
+ objPtr->length = 0;
+ }
+ else {
+ objPtr->bytes = Jim_Alloc(length + 1);
+ objPtr->length = length;
+ memcpy(objPtr->bytes, bytes, length);
+ objPtr->bytes[length] = '\0';
+ }
+}
+
+/* Duplicate an object. The returned object has refcount = 0. */
+Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_Obj *dupPtr;
+
+ dupPtr = Jim_NewObj(interp);
+ if (objPtr->bytes == NULL) {
+ /* Object does not have a valid string representation. */
+ dupPtr->bytes = NULL;
+ }
+ else {
+ Jim_InitStringRep(dupPtr, objPtr->bytes, objPtr->length);
+ }
+ if (objPtr->typePtr != NULL) {
+ if (objPtr->typePtr->dupIntRepProc == NULL) {
+ dupPtr->internalRep = objPtr->internalRep;
+ }
+ else {
+ objPtr->typePtr->dupIntRepProc(interp, objPtr, dupPtr);
+ }
+ dupPtr->typePtr = objPtr->typePtr;
+ }
+ else {
+ dupPtr->typePtr = NULL;
+ }
+ return dupPtr;
+}
+
+/* Return the string representation for objPtr. If the object
+ * string representation is invalid, calls the method to create
+ * a new one starting from the internal representation of the object. */
+const char *Jim_GetString(Jim_Obj *objPtr, int *lenPtr)
+{
+ if (objPtr->bytes == NULL) {
+ /* Invalid string repr. Generate it. */
+ JimPanic((objPtr->typePtr->updateStringProc == NULL, NULL, "UpdateStringProc called against '%s' type.", objPtr->typePtr->name));
+ objPtr->typePtr->updateStringProc(objPtr);
+ }
+ if (lenPtr)
+ *lenPtr = objPtr->length;
+ return objPtr->bytes;
+}
+
+/* Just returns the length of the object's string rep */
+int Jim_Length(Jim_Obj *objPtr)
+{
+ int len;
+
+ Jim_GetString(objPtr, &len);
+ return len;
+}
+
+static void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+
+static const Jim_ObjType dictSubstObjType = {
+ "dict-substitution",
+ FreeDictSubstInternalRep,
+ DupDictSubstInternalRep,
+ NULL,
+ JIM_TYPE_NONE,
+};
+
+static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_DecrRefCount(interp, (Jim_Obj *)objPtr->internalRep.twoPtrValue.ptr2);
+}
+
+static const Jim_ObjType interpolatedObjType = {
+ "interpolated",
+ FreeInterpolatedInternalRep,
+ NULL,
+ NULL,
+ JIM_TYPE_NONE,
+};
+
+/* -----------------------------------------------------------------------------
+ * String Object
+ * ---------------------------------------------------------------------------*/
+static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static int SetStringFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType stringObjType = {
+ "string",
+ NULL,
+ DupStringInternalRep,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ JIM_NOTUSED(interp);
+
+ /* This is a bit subtle: the only caller of this function
+ * should be Jim_DuplicateObj(), that will copy the
+ * string representaion. After the copy, the duplicated
+ * object will not have more room in teh buffer than
+ * srcPtr->length bytes. So we just set it to length. */
+ dupPtr->internalRep.strValue.maxLength = srcPtr->length;
+
+ dupPtr->internalRep.strValue.charLength = srcPtr->internalRep.strValue.charLength;
+}
+
+static int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ /* Get a fresh string representation. */
+ (void)Jim_String(objPtr);
+ /* Free any other internal representation. */
+ Jim_FreeIntRep(interp, objPtr);
+ /* Set it as string, i.e. just set the maxLength field. */
+ objPtr->typePtr = &stringObjType;
+ objPtr->internalRep.strValue.maxLength = objPtr->length;
+ /* Don't know the utf-8 length yet */
+ objPtr->internalRep.strValue.charLength = -1;
+ return JIM_OK;
+}
+
+/**
+ * Returns the length of the object string in chars, not bytes.
+ *
+ * These may be different for a utf-8 string.
+ */
+int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+#ifdef JIM_UTF8
+ if (objPtr->typePtr != &stringObjType)
+ SetStringFromAny(interp, objPtr);
+
+ if (objPtr->internalRep.strValue.charLength < 0) {
+ objPtr->internalRep.strValue.charLength = utf8_strlen(objPtr->bytes, objPtr->length);
+ }
+ return objPtr->internalRep.strValue.charLength;
+#else
+ return Jim_Length(objPtr);
+#endif
+}
+
+/* len is in bytes -- see also Jim_NewStringObjUtf8() */
+Jim_Obj *Jim_NewStringObj(Jim_Interp *interp, const char *s, int len)
+{
+ Jim_Obj *objPtr = Jim_NewObj(interp);
+
+ /* Need to find out how many bytes the string requires */
+ if (len == -1)
+ len = strlen(s);
+ /* Alloc/Set the string rep. */
+ if (len == 0) {
+ objPtr->bytes = JimEmptyStringRep;
+ objPtr->length = 0;
+ }
+ else {
+ objPtr->bytes = Jim_Alloc(len + 1);
+ objPtr->length = len;
+ memcpy(objPtr->bytes, s, len);
+ objPtr->bytes[len] = '\0';
+ }
+
+ /* No typePtr field for the vanilla string object. */
+ objPtr->typePtr = NULL;
+ return objPtr;
+}
+
+/* charlen is in characters -- see also Jim_NewStringObj() */
+Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, const char *s, int charlen)
+{
+#ifdef JIM_UTF8
+ /* Need to find out how many bytes the string requires */
+ int bytelen = utf8_index(s, charlen);
+
+ Jim_Obj *objPtr = Jim_NewStringObj(interp, s, bytelen);
+
+ /* Remember the utf8 length, so set the type */
+ objPtr->typePtr = &stringObjType;
+ objPtr->internalRep.strValue.maxLength = bytelen;
+ objPtr->internalRep.strValue.charLength = charlen;
+
+ return objPtr;
+#else
+ return Jim_NewStringObj(interp, s, charlen);
+#endif
+}
+
+/* This version does not try to duplicate the 's' pointer, but
+ * use it directly. */
+Jim_Obj *Jim_NewStringObjNoAlloc(Jim_Interp *interp, char *s, int len)
+{
+ Jim_Obj *objPtr = Jim_NewObj(interp);
+
+ if (len == -1)
+ len = strlen(s);
+ Jim_SetStringRep(objPtr, s, len);
+ objPtr->typePtr = NULL;
+ return objPtr;
+}
+
+/* Low-level string append. Use it only against objects
+ * of type "string". */
+static void StringAppendString(Jim_Obj *objPtr, const char *str, int len)
+{
+ int needlen;
+
+ if (len == -1)
+ len = strlen(str);
+ needlen = objPtr->length + len;
+ if (objPtr->internalRep.strValue.maxLength < needlen ||
+ objPtr->internalRep.strValue.maxLength == 0) {
+ needlen *= 2;
+ /* Inefficient to malloc() for less than 8 bytes */
+ if (needlen < 7) {
+ needlen = 7;
+ }
+ if (objPtr->bytes == JimEmptyStringRep) {
+ objPtr->bytes = Jim_Alloc(needlen + 1);
+ }
+ else {
+ objPtr->bytes = Jim_Realloc(objPtr->bytes, needlen + 1);
+ }
+ objPtr->internalRep.strValue.maxLength = needlen;
+ }
+ memcpy(objPtr->bytes + objPtr->length, str, len);
+ objPtr->bytes[objPtr->length + len] = '\0';
+ if (objPtr->internalRep.strValue.charLength >= 0) {
+ /* Update the utf-8 char length */
+ objPtr->internalRep.strValue.charLength += utf8_strlen(objPtr->bytes + objPtr->length, len);
+ }
+ objPtr->length += len;
+}
+
+/* Higher level API to append strings to objects. */
+void Jim_AppendString(Jim_Interp *interp, Jim_Obj *objPtr, const char *str, int len)
+{
+ JimPanic((Jim_IsShared(objPtr), interp, "Jim_AppendString called with shared object"));
+ if (objPtr->typePtr != &stringObjType)
+ SetStringFromAny(interp, objPtr);
+ StringAppendString(objPtr, str, len);
+}
+
+void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *appendObjPtr)
+{
+ int len;
+ const char *str;
+
+ str = Jim_GetString(appendObjPtr, &len);
+ Jim_AppendString(interp, objPtr, str, len);
+}
+
+void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...)
+{
+ va_list ap;
+
+ if (objPtr->typePtr != &stringObjType)
+ SetStringFromAny(interp, objPtr);
+ va_start(ap, objPtr);
+ while (1) {
+ char *s = va_arg(ap, char *);
+
+ if (s == NULL)
+ break;
+ Jim_AppendString(interp, objPtr, s, -1);
+ }
+ va_end(ap);
+}
+
+int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr)
+{
+ const char *aStr, *bStr;
+ int aLen, bLen;
+
+ if (aObjPtr == bObjPtr)
+ return 1;
+ aStr = Jim_GetString(aObjPtr, &aLen);
+ bStr = Jim_GetString(bObjPtr, &bLen);
+ if (aLen != bLen)
+ return 0;
+ return JimStringCompare(aStr, aLen, bStr, bLen) == 0;
+}
+
+int Jim_StringMatchObj(Jim_Interp *interp, Jim_Obj *patternObjPtr, Jim_Obj *objPtr, int nocase)
+{
+ return JimStringMatch(interp, patternObjPtr, Jim_String(objPtr), nocase);
+}
+
+int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *secondObjPtr, int nocase)
+{
+ const char *s1, *s2;
+ int l1, l2;
+
+ s1 = Jim_GetString(firstObjPtr, &l1);
+ s2 = Jim_GetString(secondObjPtr, &l2);
+
+ if (nocase) {
+ return JimStringCompareNoCase(s1, s2, -1);
+ }
+ return JimStringCompare(s1, l1, s2, l2);
+}
+
+/* Convert a range, as returned by Jim_GetRange(), into
+ * an absolute index into an object of the specified length.
+ * This function may return negative values, or values
+ * bigger or equal to the length of the list if the index
+ * is out of range. */
+static int JimRelToAbsIndex(int len, int idx)
+{
+ if (idx < 0)
+ return len + idx;
+ return idx;
+}
+
+/* Convert a pair of index as normalize by JimRelToAbsIndex(),
+ * into a range stored in *firstPtr, *lastPtr, *rangeLenPtr, suitable
+ * for implementation of commands like [string range] and [lrange].
+ *
+ * The resulting range is guaranteed to address valid elements of
+ * the structure. */
+static void JimRelToAbsRange(int len, int first, int last,
+ int *firstPtr, int *lastPtr, int *rangeLenPtr)
+{
+ int rangeLen;
+
+ if (first > last) {
+ rangeLen = 0;
+ }
+ else {
+ rangeLen = last - first + 1;
+ if (rangeLen) {
+ if (first < 0) {
+ rangeLen += first;
+ first = 0;
+ }
+ if (last >= len) {
+ rangeLen -= (last - (len - 1));
+ last = len - 1;
+ }
+ }
+ }
+ if (rangeLen < 0)
+ rangeLen = 0;
+
+ *firstPtr = first;
+ *lastPtr = last;
+ *rangeLenPtr = rangeLen;
+}
+
+Jim_Obj *Jim_StringByteRangeObj(Jim_Interp *interp,
+ Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr)
+{
+ int first, last;
+ const char *str;
+ int rangeLen;
+ int bytelen;
+
+ if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK ||
+ Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK)
+ return NULL;
+ str = Jim_GetString(strObjPtr, &bytelen);
+ first = JimRelToAbsIndex(bytelen, first);
+ last = JimRelToAbsIndex(bytelen, last);
+ JimRelToAbsRange(bytelen, first, last, &first, &last, &rangeLen);
+ if (first == 0 && rangeLen == bytelen) {
+ return strObjPtr;
+ }
+ return Jim_NewStringObj(interp, str + first, rangeLen);
+}
+
+Jim_Obj *Jim_StringRangeObj(Jim_Interp *interp,
+ Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr)
+{
+#ifdef JIM_UTF8
+ int first, last;
+ const char *str;
+ int len, rangeLen;
+ int bytelen;
+
+ if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK ||
+ Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK)
+ return NULL;
+ str = Jim_GetString(strObjPtr, &bytelen);
+ len = Jim_Utf8Length(interp, strObjPtr);
+ first = JimRelToAbsIndex(len, first);
+ last = JimRelToAbsIndex(len, last);
+ JimRelToAbsRange(len, first, last, &first, &last, &rangeLen);
+ if (first == 0 && rangeLen == len) {
+ return strObjPtr;
+ }
+ if (len == bytelen) {
+ /* ASCII optimisation */
+ return Jim_NewStringObj(interp, str + first, rangeLen);
+ }
+ return Jim_NewStringObjUtf8(interp, str + utf8_index(str, first), rangeLen);
+#else
+ return Jim_StringByteRangeObj(interp, strObjPtr, firstObjPtr, lastObjPtr);
+#endif
+}
+
+static Jim_Obj *JimStringToLower(Jim_Interp *interp, Jim_Obj *strObjPtr)
+{
+ char *buf, *p;
+ int len;
+ const char *str;
+
+ if (strObjPtr->typePtr != &stringObjType) {
+ SetStringFromAny(interp, strObjPtr);
+ }
+
+ str = Jim_GetString(strObjPtr, &len);
+
+ buf = p = Jim_Alloc(len + 1);
+ while (*str) {
+ int c;
+ str += utf8_tounicode(str, &c);
+ p += utf8_fromunicode(p, utf8_lower(c));
+ }
+ *p = 0;
+ return Jim_NewStringObjNoAlloc(interp, buf, len);
+}
+
+static Jim_Obj *JimStringToUpper(Jim_Interp *interp, Jim_Obj *strObjPtr)
+{
+ char *buf, *p;
+ int len;
+ const char *str;
+
+ if (strObjPtr->typePtr != &stringObjType) {
+ SetStringFromAny(interp, strObjPtr);
+ }
+
+ str = Jim_GetString(strObjPtr, &len);
+
+ buf = p = Jim_Alloc(len + 1);
+ while (*str) {
+ int c;
+ str += utf8_tounicode(str, &c);
+ p += utf8_fromunicode(p, utf8_upper(c));
+ }
+ *p = 0;
+ return Jim_NewStringObjNoAlloc(interp, buf, len);
+}
+
+/* Similar to memchr() except searches a UTF-8 string 'str' of byte length 'len'
+ * for unicode character 'c'.
+ * Returns the position if found or NULL if not
+ */
+static const char *utf8_memchr(const char *str, int len, int c)
+{
+#ifdef JIM_UTF8
+ while (len) {
+ int sc;
+ int n = utf8_tounicode(str, &sc);
+ if (sc == c) {
+ return str;
+ }
+ str += n;
+ len -= n;
+ }
+ return NULL;
+#else
+ return memchr(str, c, len);
+#endif
+}
+
+/**
+ * Searches for the first non-trim char in string (str, len)
+ *
+ * If none is found, returns just past the last char.
+ *
+ * Lengths are in bytes.
+ */
+static const char *JimFindTrimLeft(const char *str, int len, const char *trimchars, int trimlen)
+{
+ while (len) {
+ int c;
+ int n = utf8_tounicode(str, &c);
+
+ if (utf8_memchr(trimchars, trimlen, c) == NULL) {
+ /* Not a trim char, so stop */
+ break;
+ }
+ str += n;
+ len -= n;
+ }
+ return str;
+}
+
+/**
+ * Searches backwards for a non-trim char in string (str, len).
+ *
+ * Returns a pointer to just after the non-trim char, or NULL if not found.
+ *
+ * Lengths are in bytes.
+ */
+static const char *JimFindTrimRight(const char *str, int len, const char *trimchars, int trimlen)
+{
+ str += len;
+
+ while (len) {
+ int c;
+ int n = utf8_prev_len(str, len);
+
+ len -= n;
+ str -= n;
+
+ n = utf8_tounicode(str, &c);
+
+ if (utf8_memchr(trimchars, trimlen, c) == NULL) {
+ return str + n;
+ }
+ }
+
+ return NULL;
+}
+
+static const char default_trim_chars[] = " \t\n\r";
+/* sizeof() here includes the null byte */
+static int default_trim_chars_len = sizeof(default_trim_chars);
+
+static Jim_Obj *JimStringTrimLeft(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr)
+{
+ int len;
+ const char *str = Jim_GetString(strObjPtr, &len);
+ const char *trimchars = default_trim_chars;
+ int trimcharslen = default_trim_chars_len;
+ const char *newstr;
+
+ if (trimcharsObjPtr) {
+ trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen);
+ }
+
+ newstr = JimFindTrimLeft(str, len, trimchars, trimcharslen);
+ if (newstr == str) {
+ return strObjPtr;
+ }
+
+ return Jim_NewStringObj(interp, newstr, len - (newstr - str));
+}
+
+static Jim_Obj *JimStringTrimRight(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr)
+{
+ int len;
+ const char *trimchars = default_trim_chars;
+ int trimcharslen = default_trim_chars_len;
+ const char *nontrim;
+
+ if (trimcharsObjPtr) {
+ trimchars = Jim_GetString(trimcharsObjPtr, &trimcharslen);
+ }
+
+ if (strObjPtr->typePtr != &stringObjType) {
+ SetStringFromAny(interp, strObjPtr);
+ }
+ Jim_GetString(strObjPtr, &len);
+ nontrim = JimFindTrimRight(strObjPtr->bytes, len, trimchars, trimcharslen);
+
+ if (nontrim == NULL) {
+ /* All trim, so return a zero-length string */
+ return Jim_NewEmptyStringObj(interp);
+ }
+ if (nontrim == strObjPtr->bytes + len) {
+ return strObjPtr;
+ }
+
+ if (Jim_IsShared(strObjPtr)) {
+ strObjPtr = Jim_NewStringObj(interp, strObjPtr->bytes, (nontrim - strObjPtr->bytes));
+ }
+ else {
+ /* Can modify this string in place */
+ strObjPtr->bytes[nontrim - strObjPtr->bytes] = 0;
+ strObjPtr->length = (nontrim - strObjPtr->bytes);
+ }
+
+ return strObjPtr;
+}
+
+static Jim_Obj *JimStringTrim(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr)
+{
+ /* First trim left. */
+ Jim_Obj *objPtr = JimStringTrimLeft(interp, strObjPtr, trimcharsObjPtr);
+
+ /* Now trim right */
+ strObjPtr = JimStringTrimRight(interp, objPtr, trimcharsObjPtr);
+
+ if (objPtr != strObjPtr) {
+ /* Note that we don't want this object to be leaked */
+ Jim_IncrRefCount(objPtr);
+ Jim_DecrRefCount(interp, objPtr);
+ }
+
+ return strObjPtr;
+}
+
+
+static int JimStringIs(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *strClass, int strict)
+{
+ static const char * const strclassnames[] = {
+ "integer", "alpha", "alnum", "ascii", "digit",
+ "double", "lower", "upper", "space", "xdigit",
+ "control", "print", "graph", "punct",
+ NULL
+ };
+ enum {
+ STR_IS_INTEGER, STR_IS_ALPHA, STR_IS_ALNUM, STR_IS_ASCII, STR_IS_DIGIT,
+ STR_IS_DOUBLE, STR_IS_LOWER, STR_IS_UPPER, STR_IS_SPACE, STR_IS_XDIGIT,
+ STR_IS_CONTROL, STR_IS_PRINT, STR_IS_GRAPH, STR_IS_PUNCT
+ };
+ int strclass;
+ int len;
+ int i;
+ const char *str;
+ int (*isclassfunc)(int c) = NULL;
+
+ if (Jim_GetEnum(interp, strClass, strclassnames, &strclass, "class", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ str = Jim_GetString(strObjPtr, &len);
+ if (len == 0) {
+ Jim_SetResultInt(interp, !strict);
+ return JIM_OK;
+ }
+
+ switch (strclass) {
+ case STR_IS_INTEGER:
+ {
+ jim_wide w;
+ Jim_SetResultInt(interp, JimGetWideNoErr(interp, strObjPtr, &w) == JIM_OK);
+ return JIM_OK;
+ }
+
+ case STR_IS_DOUBLE:
+ {
+ double d;
+ Jim_SetResultInt(interp, Jim_GetDouble(interp, strObjPtr, &d) == JIM_OK && errno != ERANGE);
+ return JIM_OK;
+ }
+
+ case STR_IS_ALPHA: isclassfunc = isalpha; break;
+ case STR_IS_ALNUM: isclassfunc = isalnum; break;
+ case STR_IS_ASCII: isclassfunc = isascii; break;
+ case STR_IS_DIGIT: isclassfunc = isdigit; break;
+ case STR_IS_LOWER: isclassfunc = islower; break;
+ case STR_IS_UPPER: isclassfunc = isupper; break;
+ case STR_IS_SPACE: isclassfunc = isspace; break;
+ case STR_IS_XDIGIT: isclassfunc = isxdigit; break;
+ case STR_IS_CONTROL: isclassfunc = iscntrl; break;
+ case STR_IS_PRINT: isclassfunc = isprint; break;
+ case STR_IS_GRAPH: isclassfunc = isgraph; break;
+ case STR_IS_PUNCT: isclassfunc = ispunct; break;
+ default:
+ return JIM_ERR;
+ }
+
+ for (i = 0; i < len; i++) {
+ if (!isclassfunc(str[i])) {
+ Jim_SetResultInt(interp, 0);
+ return JIM_OK;
+ }
+ }
+ Jim_SetResultInt(interp, 1);
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Compared String Object
+ * ---------------------------------------------------------------------------*/
+
+/* This is strange object that allows to compare a C literal string
+ * with a Jim object in very short time if the same comparison is done
+ * multiple times. For example every time the [if] command is executed,
+ * Jim has to check if a given argument is "else". This comparions if
+ * the code has no errors are true most of the times, so we can cache
+ * inside the object the pointer of the string of the last matching
+ * comparison. Because most C compilers perform literal sharing,
+ * so that: char *x = "foo", char *y = "foo", will lead to x == y,
+ * this works pretty well even if comparisons are at different places
+ * inside the C code. */
+
+static const Jim_ObjType comparedStringObjType = {
+ "compared-string",
+ NULL,
+ NULL,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+/* The only way this object is exposed to the API is via the following
+ * function. Returns true if the string and the object string repr.
+ * are the same, otherwise zero is returned.
+ *
+ * Note: this isn't binary safe, but it hardly needs to be.*/
+int Jim_CompareStringImmediate(Jim_Interp *interp, Jim_Obj *objPtr, const char *str)
+{
+ if (objPtr->typePtr == &comparedStringObjType && objPtr->internalRep.ptr == str)
+ return 1;
+ else {
+ const char *objStr = Jim_String(objPtr);
+
+ if (strcmp(str, objStr) != 0)
+ return 0;
+ if (objPtr->typePtr != &comparedStringObjType) {
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &comparedStringObjType;
+ }
+ objPtr->internalRep.ptr = (char *)str; /*ATTENTION: const cast */
+ return 1;
+ }
+}
+
+static int qsortCompareStringPointers(const void *a, const void *b)
+{
+ char *const *sa = (char *const *)a;
+ char *const *sb = (char *const *)b;
+
+ return strcmp(*sa, *sb);
+}
+
+
+/* -----------------------------------------------------------------------------
+ * Source Object
+ *
+ * This object is just a string from the language point of view, but
+ * in the internal representation it contains the filename and line number
+ * where this given token was read. This information is used by
+ * Jim_EvalObj() if the object passed happens to be of type "source".
+ *
+ * This allows to propagate the information about line numbers and file
+ * names and give error messages with absolute line numbers.
+ *
+ * Note that this object uses shared strings for filenames, and the
+ * pointer to the filename together with the line number is taken into
+ * the space for the "inline" internal representation of the Jim_Object,
+ * so there is almost memory zero-overhead.
+ *
+ * Also the object will be converted to something else if the given
+ * token it represents in the source file is not something to be
+ * evaluated (not a script), and will be specialized in some other way,
+ * so the time overhead is also null.
+ * ---------------------------------------------------------------------------*/
+
+static void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+
+static const Jim_ObjType sourceObjType = {
+ "source",
+ FreeSourceInternalRep,
+ DupSourceInternalRep,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+void FreeSourceInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_ReleaseSharedString(interp, objPtr->internalRep.sourceValue.fileName);
+}
+
+void DupSourceInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ dupPtr->internalRep.sourceValue.fileName =
+ Jim_GetSharedString(interp, srcPtr->internalRep.sourceValue.fileName);
+ dupPtr->internalRep.sourceValue.lineNumber = dupPtr->internalRep.sourceValue.lineNumber;
+ dupPtr->typePtr = &sourceObjType;
+}
+
+static void JimSetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr,
+ const char *fileName, int lineNumber)
+{
+ if (fileName) {
+ JimPanic((Jim_IsShared(objPtr), interp, "JimSetSourceInfo called with shared object"));
+ JimPanic((objPtr->typePtr != NULL, interp, "JimSetSourceInfo called with typePtr != NULL"));
+ objPtr->internalRep.sourceValue.fileName = Jim_GetSharedString(interp, fileName);
+ objPtr->internalRep.sourceValue.lineNumber = lineNumber;
+ objPtr->typePtr = &sourceObjType;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ * Script Object
+ * ---------------------------------------------------------------------------*/
+
+static const Jim_ObjType scriptLineObjType = {
+ "scriptline",
+ NULL,
+ NULL,
+ NULL,
+ 0,
+};
+
+static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line)
+{
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_NewObj(interp);
+ objPtr->typePtr = &scriptLineObjType;
+ objPtr->bytes = JimEmptyStringRep;
+ objPtr->internalRep.scriptLineValue.argc = argc;
+ objPtr->internalRep.scriptLineValue.line = line;
+
+ return objPtr;
+}
+
+#define JIM_CMDSTRUCT_EXPAND -1
+
+static void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static int SetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType scriptObjType = {
+ "script",
+ FreeScriptInternalRep,
+ DupScriptInternalRep,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+/* The ScriptToken structure represents every token into a scriptObj.
+ * Every token contains an associated Jim_Obj that can be specialized
+ * by commands operating on it. */
+typedef struct ScriptToken
+{
+ int type;
+ Jim_Obj *objPtr;
+} ScriptToken;
+
+/* This is the script object internal representation. An array of
+ * ScriptToken structures, including a pre-computed representation of the
+ * command length and arguments.
+ *
+ * For example the script:
+ *
+ * puts hello
+ * set $i $x$y [foo]BAR
+ *
+ * will produce a ScriptObj with the following Tokens:
+ *
+ * LIN 2
+ * ESC puts
+ * ESC hello
+ * LIN 4
+ * ESC set
+ * VAR i
+ * WRD 2
+ * VAR x
+ * VAR y
+ * WRD 2
+ * CMD foo
+ * ESC BAR
+ *
+ * "puts hello" has two args (LIN 2), composed of single tokens.
+ * (Note that the WRD token is omitted for the common case of a single token.)
+ *
+ * "set $i $x$y [foo]BAR" has four (LIN 4) args, the first word
+ * has 1 token (ESC SET), and the last has two tokens (WRD 2 CMD foo ESC BAR)
+ *
+ * The precomputation of the command structure makes Jim_Eval() faster,
+ * and simpler because there aren't dynamic lengths / allocations.
+ *
+ * -- {expand}/{*} handling --
+ *
+ * Expand is handled in a special way.
+ *
+ * If a "word" begins with {*}, the word token count is -ve.
+ *
+ * For example the command:
+ *
+ * list {*}{a b}
+ *
+ * Will produce the following cmdstruct array:
+ *
+ * LIN 2
+ * ESC list
+ * WRD -1
+ * STR a b
+ *
+ * Note that the 'LIN' token also contains the source information for the
+ * first word of the line for error reporting purposes
+ *
+ * -- the substFlags field of the structure --
+ *
+ * The scriptObj structure is used to represent both "script" objects
+ * and "subst" objects. In the second case, the there are no LIN and WRD
+ * tokens. Instead SEP and EOL tokens are added as-is.
+ * In addition, the field 'substFlags' is used to represent the flags used to turn
+ * the string into the internal representation used to perform the
+ * substitution. If this flags are not what the application requires
+ * the scriptObj is created again. For example the script:
+ *
+ * subst -nocommands $string
+ * subst -novariables $string
+ *
+ * Will recreate the internal representation of the $string object
+ * two times.
+ */
+typedef struct ScriptObj
+{
+ int len; /* Length as number of tokens. */
+ ScriptToken *token; /* Tokens array. */
+ int substFlags; /* flags used for the compilation of "subst" objects */
+ int inUse; /* Used to share a ScriptObj. Currently
+ only used by Jim_EvalObj() as protection against
+ shimmering of the currently evaluated object. */
+ const char *fileName;
+ int line; /* Line number of the first line */
+} ScriptObj;
+
+void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ int i;
+ struct ScriptObj *script = (void *)objPtr->internalRep.ptr;
+
+ script->inUse--;
+ if (script->inUse != 0)
+ return;
+ for (i = 0; i < script->len; i++) {
+ Jim_DecrRefCount(interp, script->token[i].objPtr);
+ }
+ Jim_Free(script->token);
+ if (script->fileName) {
+ Jim_ReleaseSharedString(interp, script->fileName);
+ }
+ Jim_Free(script);
+}
+
+void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ JIM_NOTUSED(interp);
+ JIM_NOTUSED(srcPtr);
+
+ /* Just returns an simple string. */
+ dupPtr->typePtr = NULL;
+}
+
+/* A simple parser token.
+ * All the simple tokens for the script point into the same script string rep.
+ */
+typedef struct
+{
+ const char *token; /* Pointer to the start of the token */
+ int len; /* Length of this token */
+ int type; /* Token type */
+ int line; /* Line number */
+} ParseToken;
+
+/* A list of parsed tokens representing a script.
+ * Tokens are added to this list as the script is parsed.
+ * It grows as needed.
+ */
+typedef struct
+{
+ /* Start with a statically allocated list of tokens which will be expanded with realloc if needed */
+ ParseToken *list; /* Array of tokens */
+ int size; /* Current size of the list */
+ int count; /* Number of entries used */
+ ParseToken static_list[20]; /* Small initial token space to avoid allocation */
+} ParseTokenList;
+
+static void ScriptTokenListInit(ParseTokenList *tokenlist)
+{
+ tokenlist->list = tokenlist->static_list;
+ tokenlist->size = sizeof(tokenlist->static_list) / sizeof(ParseToken);
+ tokenlist->count = 0;
+}
+
+static void ScriptTokenListFree(ParseTokenList *tokenlist)
+{
+ if (tokenlist->list != tokenlist->static_list) {
+ Jim_Free(tokenlist->list);
+ }
+}
+
+/**
+ * Adds the new token to the tokenlist.
+ * The token has the given length, type and line number.
+ * The token list is resized as necessary.
+ */
+static void ScriptAddToken(ParseTokenList *tokenlist, const char *token, int len, int type,
+ int line)
+{
+ ParseToken *t;
+
+ if (tokenlist->count == tokenlist->size) {
+ /* Resize the list */
+ tokenlist->size *= 2;
+ if (tokenlist->list != tokenlist->static_list) {
+ tokenlist->list =
+ Jim_Realloc(tokenlist->list, tokenlist->size * sizeof(*tokenlist->list));
+ }
+ else {
+ /* The list needs to become allocated */
+ tokenlist->list = Jim_Alloc(tokenlist->size * sizeof(*tokenlist->list));
+ memcpy(tokenlist->list, tokenlist->static_list,
+ tokenlist->count * sizeof(*tokenlist->list));
+ }
+ }
+ t = &tokenlist->list[tokenlist->count++];
+ t->token = token;
+ t->len = len;
+ t->type = type;
+ t->line = line;
+}
+
+/* Counts the number of adjoining non-separator.
+ *
+ * Returns -ve if the first token is the expansion
+ * operator (in which case the count doesn't include
+ * that token).
+ */
+static int JimCountWordTokens(ParseToken *t)
+{
+ int expand = 1;
+ int count = 0;
+
+ /* Is the first word {*} or {expand}? */
+ if (t->type == JIM_TT_STR && !TOKEN_IS_SEP(t[1].type)) {
+ if ((t->len == 1 && *t->token == '*') || (t->len == 6 && strncmp(t->token, "expand", 6) == 0)) {
+ /* Create an expand token */
+ expand = -1;
+ t++;
+ }
+ }
+
+ /* Now count non-separator words */
+ while (!TOKEN_IS_SEP(t->type)) {
+ t++;
+ count++;
+ }
+
+ return count * expand;
+}
+
+/**
+ * Create a script/subst object from the given token.
+ */
+static Jim_Obj *JimMakeScriptObj(Jim_Interp *interp, const ParseToken *t)
+{
+ Jim_Obj *objPtr;
+
+ if (t->type == JIM_TT_ESC && memchr(t->token, '\\', t->len) != NULL) {
+ /* Convert the backlash escapes . */
+ int len = t->len;
+ char *str = Jim_Alloc(len + 1);
+ len = JimEscape(str, t->token, len);
+ objPtr = Jim_NewStringObjNoAlloc(interp, str, len);
+ }
+ else {
+ /* REVIST: Strictly, JIM_TT_STR should replace <backslash><newline><whitespace>
+ * with a single space. This is currently not done.
+ */
+ objPtr = Jim_NewStringObj(interp, t->token, t->len);
+ }
+ return objPtr;
+}
+
+/**
+ * Takes a tokenlist and creates the allocated list of script tokens
+ * in script->token, of length script->len.
+ *
+ * Unnecessary tokens are discarded, and LINE and WORD tokens are inserted
+ * as required.
+ *
+ * Also sets script->line to the line number of the first token
+ */
+static void ScriptObjAddTokens(Jim_Interp *interp, struct ScriptObj *script,
+ ParseTokenList *tokenlist)
+{
+ int i;
+ struct ScriptToken *token;
+ /* Number of tokens so far for the current command */
+ int lineargs = 0;
+ /* This is the first token for the current command */
+ ScriptToken *linefirst;
+ int count;
+ int linenr;
+
+#ifdef DEBUG_SHOW_SCRIPT_TOKENS
+ printf("==== Tokens ====\n");
+ for (i = 0; i < tokenlist->count; i++) {
+ printf("[%2d]@%d %s '%.*s'\n", i, tokenlist->list[i].line, jim_tt_name(tokenlist->list[i].type),
+ tokenlist->list[i].len, tokenlist->list[i].token);
+ }
+#endif
+
+ /* May need up to one extra script token for each EOL in the worst case */
+ count = tokenlist->count;
+ for (i = 0; i < tokenlist->count; i++) {
+ if (tokenlist->list[i].type == JIM_TT_EOL) {
+ count++;
+ }
+ }
+ linenr = script->line = tokenlist->list[0].line;
+
+ token = script->token = Jim_Alloc(sizeof(ScriptToken) * count);
+
+ /* This is the first token for the current command */
+ linefirst = token++;
+
+ for (i = 0; i < tokenlist->count; ) {
+ /* Look ahead to find out how many tokens make up the next word */
+ int wordtokens;
+
+ /* Skip any leading separators */
+ while (tokenlist->list[i].type == JIM_TT_SEP) {
+ i++;
+ }
+
+ wordtokens = JimCountWordTokens(tokenlist->list + i);
+
+ if (wordtokens == 0) {
+ /* None, so at end of line */
+ if (lineargs) {
+ linefirst->type = JIM_TT_LINE;
+ linefirst->objPtr = JimNewScriptLineObj(interp, lineargs, linenr);
+ Jim_IncrRefCount(linefirst->objPtr);
+
+ /* Reset for new line */
+ lineargs = 0;
+ linefirst = token++;
+ }
+ i++;
+ continue;
+ }
+ else if (wordtokens != 1) {
+ /* More than 1, or {expand}, so insert a WORD token */
+ token->type = JIM_TT_WORD;
+ token->objPtr = Jim_NewIntObj(interp, wordtokens);
+ Jim_IncrRefCount(token->objPtr);
+ token++;
+ if (wordtokens < 0) {
+ /* Skip the expand token */
+ i++;
+ wordtokens = -wordtokens - 1;
+ lineargs--;
+ }
+ }
+
+ lineargs++;
+ linenr = tokenlist->list[i].line;
+
+ /* Add each non-separator word token to the line */
+ while (wordtokens--) {
+ const ParseToken *t = &tokenlist->list[i++];
+
+ token->type = t->type;
+ token->objPtr = JimMakeScriptObj(interp, t);
+ Jim_IncrRefCount(token->objPtr);
+
+ /* Every object is initially a string, but the
+ * internal type may be specialized during execution of the
+ * script. */
+ JimSetSourceInfo(interp, token->objPtr, script->fileName, t->line);
+ token++;
+ }
+ }
+
+ if (lineargs == 0) {
+ token--;
+ }
+
+ script->len = token - script->token;
+
+ assert(script->len < count);
+
+#ifdef DEBUG_SHOW_SCRIPT
+ printf("==== Script ====\n");
+ for (i = 0; i < script->len; i++) {
+ const ScriptToken *t = &script->token[i];
+ printf("[%2d] %s %s\n", i, jim_tt_name(t->type), Jim_String(t->objPtr));
+ }
+#endif
+
+}
+
+/**
+ * Similar to ScriptObjAddTokens(), but for subst objects.
+ */
+static void SubstObjAddTokens(Jim_Interp *interp, struct ScriptObj *script,
+ ParseTokenList *tokenlist)
+{
+ int i;
+ struct ScriptToken *token;
+
+ token = script->token = Jim_Alloc(sizeof(ScriptToken) * tokenlist->count);
+
+ for (i = 0; i < tokenlist->count; i++) {
+ const ParseToken *t = &tokenlist->list[i];
+
+ /* Create a token for 't' */
+ token->type = t->type;
+ token->objPtr = JimMakeScriptObj(interp, t);
+ Jim_IncrRefCount(token->objPtr);
+ token++;
+ }
+
+ script->len = i;
+}
+
+/* This method takes the string representation of an object
+ * as a Tcl script, and generates the pre-parsed internal representation
+ * of the script. */
+int SetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr)
+{
+ int scriptTextLen;
+ const char *scriptText = Jim_GetString(objPtr, &scriptTextLen);
+ struct JimParserCtx parser;
+ struct ScriptObj *script = Jim_Alloc(sizeof(*script));
+ ParseTokenList tokenlist;
+
+ /* Try to get information about filename / line number */
+ if (objPtr->typePtr == &sourceObjType) {
+ script->fileName = Jim_GetSharedString(interp, objPtr->internalRep.sourceValue.fileName);
+ script->line = objPtr->internalRep.sourceValue.lineNumber;
+ }
+ else {
+ script->fileName = NULL;
+ script->line = 1;
+ }
+
+ /* Initially parse the script into tokens (in tokenlist) */
+ ScriptTokenListInit(&tokenlist);
+
+ JimParserInit(&parser, scriptText, scriptTextLen, script->line);
+ while (!JimParserEof(&parser)) {
+ JimParseScript(&parser);
+ ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt,
+ parser.tline);
+ }
+ /* Add a final EOF token */
+ ScriptAddToken(&tokenlist, scriptText + scriptTextLen, 0, JIM_TT_EOF, 0);
+
+ /* Create the "real" script tokens from the initial token list */
+ script->substFlags = 0;
+ script->inUse = 1;
+ ScriptObjAddTokens(interp, script, &tokenlist);
+
+ /* No longer need the token list */
+ ScriptTokenListFree(&tokenlist);
+
+ if (!script->fileName) {
+ script->fileName = Jim_GetSharedString(interp, "");
+ }
+
+ /* Free the old internal rep and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ Jim_SetIntRepPtr(objPtr, script);
+ objPtr->typePtr = &scriptObjType;
+
+ return JIM_OK;
+}
+
+ScriptObj *Jim_GetScript(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ struct ScriptObj *script = Jim_GetIntRepPtr(objPtr);
+
+ if (objPtr->typePtr != &scriptObjType || script->substFlags) {
+ SetScriptFromAny(interp, objPtr);
+ }
+ return (ScriptObj *) Jim_GetIntRepPtr(objPtr);
+}
+
+/* -----------------------------------------------------------------------------
+ * Commands
+ * ---------------------------------------------------------------------------*/
+static void JimIncrCmdRefCount(Jim_Cmd *cmdPtr)
+{
+ cmdPtr->inUse++;
+}
+
+static void JimDecrCmdRefCount(Jim_Interp *interp, Jim_Cmd *cmdPtr)
+{
+ if (--cmdPtr->inUse == 0) {
+ if (cmdPtr->isproc) {
+ Jim_DecrRefCount(interp, cmdPtr->u.proc.argListObjPtr);
+ Jim_DecrRefCount(interp, cmdPtr->u.proc.bodyObjPtr);
+ if (cmdPtr->u.proc.staticVars) {
+ Jim_FreeHashTable(cmdPtr->u.proc.staticVars);
+ Jim_Free(cmdPtr->u.proc.staticVars);
+ }
+ if (cmdPtr->u.proc.prevCmd) {
+ /* Delete any pushed command too */
+ JimDecrCmdRefCount(interp, cmdPtr->u.proc.prevCmd);
+ }
+ }
+ else {
+ /* native (C) */
+ if (cmdPtr->u.native.delProc) {
+ cmdPtr->u.native.delProc(interp, cmdPtr->u.native.privData);
+ }
+ }
+ Jim_Free(cmdPtr);
+ }
+}
+
+/* Commands HashTable Type.
+ *
+ * Keys are dynamic allocated strings, Values are Jim_Cmd structures. */
+static void JimCommandsHT_ValDestructor(void *interp, void *val)
+{
+ JimDecrCmdRefCount(interp, val);
+}
+
+static const Jim_HashTableType JimCommandsHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ JimStringCopyHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ JimCommandsHT_ValDestructor /* val destructor */
+};
+
+/* ------------------------- Commands related functions --------------------- */
+
+int Jim_CreateCommand(Jim_Interp *interp, const char *cmdName,
+ Jim_CmdProc cmdProc, void *privData, Jim_DelCmdProc delProc)
+{
+ Jim_Cmd *cmdPtr;
+
+ if (Jim_DeleteHashEntry(&interp->commands, cmdName) != JIM_ERR) {
+ /* Command existed so incr proc epoch */
+ Jim_InterpIncrProcEpoch(interp);
+ }
+
+ cmdPtr = Jim_Alloc(sizeof(*cmdPtr));
+
+ /* Store the new details for this proc */
+ memset(cmdPtr, 0, sizeof(*cmdPtr));
+ cmdPtr->inUse = 1;
+ cmdPtr->u.native.delProc = delProc;
+ cmdPtr->u.native.cmdProc = cmdProc;
+ cmdPtr->u.native.privData = privData;
+
+ Jim_AddHashEntry(&interp->commands, cmdName, cmdPtr);
+
+ /* There is no need to increment the 'proc epoch' because
+ * creation of a new procedure can never affect existing
+ * cached commands. We don't do negative caching. */
+ return JIM_OK;
+}
+
+static int JimCreateProcedure(Jim_Interp *interp, const char *cmdName,
+ Jim_Obj *argListObjPtr, Jim_Obj *staticsListObjPtr, Jim_Obj *bodyObjPtr,
+ int leftArity, int optionalArgs, int args, int rightArity)
+{
+ Jim_Cmd *cmdPtr;
+ Jim_HashEntry *he;
+
+ cmdPtr = Jim_Alloc(sizeof(*cmdPtr));
+ memset(cmdPtr, 0, sizeof(*cmdPtr));
+ cmdPtr->inUse = 1;
+ cmdPtr->isproc = 1;
+ cmdPtr->u.proc.argListObjPtr = argListObjPtr;
+ cmdPtr->u.proc.bodyObjPtr = bodyObjPtr;
+ Jim_IncrRefCount(argListObjPtr);
+ Jim_IncrRefCount(bodyObjPtr);
+ cmdPtr->u.proc.leftArity = leftArity;
+ cmdPtr->u.proc.optionalArgs = optionalArgs;
+ cmdPtr->u.proc.args = args;
+ cmdPtr->u.proc.rightArity = rightArity;
+ cmdPtr->u.proc.staticVars = NULL;
+ cmdPtr->u.proc.prevCmd = NULL;
+ cmdPtr->inUse = 1;
+
+ /* Create the statics hash table. */
+ if (staticsListObjPtr) {
+ int len, i;
+
+ len = Jim_ListLength(interp, staticsListObjPtr);
+ if (len != 0) {
+ cmdPtr->u.proc.staticVars = Jim_Alloc(sizeof(Jim_HashTable));
+ Jim_InitHashTable(cmdPtr->u.proc.staticVars, &JimVariablesHashTableType, interp);
+ for (i = 0; i < len; i++) {
+ Jim_Obj *objPtr = 0, *initObjPtr = 0, *nameObjPtr = 0;
+ Jim_Var *varPtr;
+ int subLen;
+
+ Jim_ListIndex(interp, staticsListObjPtr, i, &objPtr, JIM_NONE);
+ /* Check if it's composed of two elements. */
+ subLen = Jim_ListLength(interp, objPtr);
+ if (subLen == 1 || subLen == 2) {
+ /* Try to get the variable value from the current
+ * environment. */
+ Jim_ListIndex(interp, objPtr, 0, &nameObjPtr, JIM_NONE);
+ if (subLen == 1) {
+ initObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_NONE);
+ if (initObjPtr == NULL) {
+ Jim_SetResultFormatted(interp,
+ "variable for initialization of static \"%#s\" not found in the local context",
+ nameObjPtr);
+ goto err;
+ }
+ }
+ else {
+ Jim_ListIndex(interp, objPtr, 1, &initObjPtr, JIM_NONE);
+ }
+ if (JimValidName(interp, "static variable", nameObjPtr) != JIM_OK) {
+ goto err;
+ }
+
+ varPtr = Jim_Alloc(sizeof(*varPtr));
+ varPtr->objPtr = initObjPtr;
+ Jim_IncrRefCount(initObjPtr);
+ varPtr->linkFramePtr = NULL;
+ if (Jim_AddHashEntry(cmdPtr->u.proc.staticVars,
+ Jim_String(nameObjPtr), varPtr) != JIM_OK) {
+ Jim_SetResultFormatted(interp,
+ "static variable name \"%#s\" duplicated in statics list", nameObjPtr);
+ Jim_DecrRefCount(interp, initObjPtr);
+ Jim_Free(varPtr);
+ goto err;
+ }
+ }
+ else {
+ Jim_SetResultFormatted(interp, "too many fields in static specifier \"%#s\"",
+ objPtr);
+ goto err;
+ }
+ }
+ }
+ }
+
+ /* Add the new command */
+
+ /* It may already exist, so we try to delete the old one.
+ * Note that reference count means that it won't be deleted yet if
+ * it exists in the call stack.
+ *
+ * BUT, if 'local' is in force, instead of deleting the existing
+ * proc, we stash a reference to the old proc here.
+ */
+ he = Jim_FindHashEntry(&interp->commands, cmdName);
+ if (he) {
+ /* There was an old procedure with the same name, this requires
+ * a 'proc epoch' update. */
+
+ /* If a procedure with the same name didn't existed there is no need
+ * to increment the 'proc epoch' because creation of a new procedure
+ * can never affect existing cached commands. We don't do
+ * negative caching. */
+ Jim_InterpIncrProcEpoch(interp);
+ }
+
+ if (he && interp->local) {
+ /* Just push this proc over the top of the previous one */
+ cmdPtr->u.proc.prevCmd = he->val;
+ he->val = cmdPtr;
+ }
+ else {
+ if (he) {
+ /* Replace the existing proc */
+ Jim_DeleteHashEntry(&interp->commands, cmdName);
+ }
+
+ Jim_AddHashEntry(&interp->commands, cmdName, cmdPtr);
+ }
+
+ /* Unlike Tcl, set the name of the proc as the result */
+ Jim_SetResultString(interp, cmdName, -1);
+ return JIM_OK;
+
+ err:
+ Jim_FreeHashTable(cmdPtr->u.proc.staticVars);
+ Jim_Free(cmdPtr->u.proc.staticVars);
+ Jim_DecrRefCount(interp, argListObjPtr);
+ Jim_DecrRefCount(interp, bodyObjPtr);
+ Jim_Free(cmdPtr);
+ return JIM_ERR;
+}
+
+int Jim_DeleteCommand(Jim_Interp *interp, const char *cmdName)
+{
+ if (Jim_DeleteHashEntry(&interp->commands, cmdName) == JIM_ERR)
+ return JIM_ERR;
+ Jim_InterpIncrProcEpoch(interp);
+ return JIM_OK;
+}
+
+int Jim_RenameCommand(Jim_Interp *interp, const char *oldName, const char *newName)
+{
+ Jim_HashEntry *he;
+
+ /* Does it exist? */
+ he = Jim_FindHashEntry(&interp->commands, oldName);
+ if (he == NULL) {
+ Jim_SetResultFormatted(interp, "can't %s \"%s\": command doesn't exist",
+ newName[0] ? "rename" : "delete", oldName);
+ return JIM_ERR;
+ }
+
+ if (newName[0] == '\0') /* Delete! */
+ return Jim_DeleteCommand(interp, oldName);
+
+ /* rename */
+ if (Jim_FindHashEntry(&interp->commands, newName)) {
+ Jim_SetResultFormatted(interp, "can't rename to \"%s\": command already exists", newName);
+ return JIM_ERR;
+ }
+
+ /* Add the new name first */
+ JimIncrCmdRefCount(he->val);
+ Jim_AddHashEntry(&interp->commands, newName, he->val);
+
+ /* Now remove the old name */
+ Jim_DeleteHashEntry(&interp->commands, oldName);
+
+ /* Increment the epoch */
+ Jim_InterpIncrProcEpoch(interp);
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Command object
+ * ---------------------------------------------------------------------------*/
+
+static int SetCommandFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType commandObjType = {
+ "command",
+ NULL,
+ NULL,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+int SetCommandFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_HashEntry *he;
+ const char *cmdName;
+
+ /* Get the string representation */
+ cmdName = Jim_String(objPtr);
+ /* Lookup this name into the commands hash table */
+ he = Jim_FindHashEntry(&interp->commands, cmdName);
+ if (he == NULL)
+ return JIM_ERR;
+
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &commandObjType;
+ objPtr->internalRep.cmdValue.procEpoch = interp->procEpoch;
+ objPtr->internalRep.cmdValue.cmdPtr = (void *)he->val;
+ return JIM_OK;
+}
+
+/* This function returns the command structure for the command name
+ * stored in objPtr. It tries to specialize the objPtr to contain
+ * a cached info instead to perform the lookup into the hash table
+ * every time. The information cached may not be uptodate, in such
+ * a case the lookup is performed and the cache updated.
+ *
+ * Respects the 'upcall' setting
+ */
+Jim_Cmd *Jim_GetCommand(Jim_Interp *interp, Jim_Obj *objPtr, int flags)
+{
+ Jim_Cmd *cmd;
+
+ if ((objPtr->typePtr != &commandObjType ||
+ objPtr->internalRep.cmdValue.procEpoch != interp->procEpoch) &&
+ SetCommandFromAny(interp, objPtr) == JIM_ERR) {
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultFormatted(interp, "invalid command name \"%#s\"", objPtr);
+ }
+ return NULL;
+ }
+ cmd = objPtr->internalRep.cmdValue.cmdPtr;
+ while (cmd->isproc && cmd->u.proc.upcall) {
+ cmd = cmd->u.proc.prevCmd;
+ }
+ return cmd;
+}
+
+/* -----------------------------------------------------------------------------
+ * Variables
+ * ---------------------------------------------------------------------------*/
+
+/* Variables HashTable Type.
+ *
+ * Keys are dynamic allocated strings, Values are Jim_Var structures. */
+static void JimVariablesHTValDestructor(void *interp, void *val)
+{
+ Jim_Var *varPtr = (void *)val;
+
+ Jim_DecrRefCount(interp, varPtr->objPtr);
+ Jim_Free(val);
+}
+
+static const Jim_HashTableType JimVariablesHashTableType = {
+ JimStringCopyHTHashFunction, /* hash function */
+ JimStringCopyHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimStringCopyHTKeyCompare, /* key compare */
+ JimStringCopyHTKeyDestructor, /* key destructor */
+ JimVariablesHTValDestructor /* val destructor */
+};
+
+/* -----------------------------------------------------------------------------
+ * Variable object
+ * ---------------------------------------------------------------------------*/
+
+#define JIM_DICT_SUGAR 100 /* Only returned by SetVariableFromAny() */
+
+static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType variableObjType = {
+ "variable",
+ NULL,
+ NULL,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+/* Return true if the string "str" looks like syntax sugar for [dict]. I.e.
+ * is in the form "varname(key)". */
+static int JimNameIsDictSugar(const char *str, int len)
+{
+ if (len && str[len - 1] == ')' && strchr(str, '(') != NULL)
+ return 1;
+ return 0;
+}
+
+/**
+ * Check that the name does not contain embedded nulls.
+ *
+ * Variable and procedure names are maniplated as null terminated strings, so
+ * don't allow names with embedded nulls.
+ */
+static int JimValidName(Jim_Interp *interp, const char *type, Jim_Obj *nameObjPtr)
+{
+ /* Variable names and proc names can't contain embedded nulls */
+ if (nameObjPtr->typePtr != &variableObjType) {
+ int len;
+ const char *str = Jim_GetString(nameObjPtr, &len);
+ if (memchr(str, '\0', len)) {
+ Jim_SetResultFormatted(interp, "%s name contains embedded null", type);
+ return JIM_ERR;
+ }
+ }
+ return JIM_OK;
+}
+
+/* This method should be called only by the variable API.
+ * It returns JIM_OK on success (variable already exists),
+ * JIM_ERR if it does not exists, JIM_DICT_SUGAR if it's not
+ * a variable name, but syntax glue for [dict] i.e. the last
+ * character is ')' */
+static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr)
+{
+ Jim_HashEntry *he;
+ const char *varName;
+ int len;
+ Jim_CallFrame *framePtr = interp->framePtr;
+
+ /* Check if the object is already an uptodate variable */
+ if (objPtr->typePtr == &variableObjType &&
+ objPtr->internalRep.varValue.callFrameId == framePtr->id) {
+ return JIM_OK; /* nothing to do */
+ }
+
+ if (objPtr->typePtr == &dictSubstObjType) {
+ return JIM_DICT_SUGAR;
+ }
+
+ if (JimValidName(interp, "variable", objPtr) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* Get the string representation */
+ varName = Jim_GetString(objPtr, &len);
+
+ /* Make sure it's not syntax glue to get/set dict. */
+ if (JimNameIsDictSugar(varName, len)) {
+ return JIM_DICT_SUGAR;
+ }
+
+ if (varName[0] == ':' && varName[1] == ':') {
+ framePtr = interp->topFramePtr;
+ he = Jim_FindHashEntry(&framePtr->vars, varName + 2);
+ if (he == NULL) {
+ return JIM_ERR;
+ }
+ }
+ else {
+ /* Lookup this name into the variables hash table */
+ he = Jim_FindHashEntry(&framePtr->vars, varName);
+ if (he == NULL) {
+ /* Try with static vars. */
+ if (framePtr->staticVars == NULL)
+ return JIM_ERR;
+ if (!(he = Jim_FindHashEntry(framePtr->staticVars, varName)))
+ return JIM_ERR;
+ }
+ }
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &variableObjType;
+ objPtr->internalRep.varValue.callFrameId = framePtr->id;
+ objPtr->internalRep.varValue.varPtr = (void *)he->val;
+ return JIM_OK;
+}
+
+/* -------------------- Variables related functions ------------------------- */
+static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, Jim_Obj *valObjPtr);
+static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *ObjPtr, int flags);
+
+/* For now that's dummy. Variables lookup should be optimized
+ * in many ways, with caching of lookups, and possibly with
+ * a table of pre-allocated vars in every CallFrame for local vars.
+ * All the caching should also have an 'epoch' mechanism similar
+ * to the one used by Tcl for procedures lookup caching. */
+
+int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr)
+{
+ const char *name;
+ Jim_Var *var;
+ int err;
+
+ if ((err = SetVariableFromAny(interp, nameObjPtr)) != JIM_OK) {
+ Jim_CallFrame *framePtr = interp->framePtr;
+
+ /* Check for [dict] syntax sugar. */
+ if (err == JIM_DICT_SUGAR)
+ return JimDictSugarSet(interp, nameObjPtr, valObjPtr);
+
+ if (JimValidName(interp, "variable", nameObjPtr) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* New variable to create */
+ name = Jim_String(nameObjPtr);
+
+ var = Jim_Alloc(sizeof(*var));
+ var->objPtr = valObjPtr;
+ Jim_IncrRefCount(valObjPtr);
+ var->linkFramePtr = NULL;
+ /* Insert the new variable */
+ if (name[0] == ':' && name[1] == ':') {
+ /* Into the top level frame */
+ framePtr = interp->topFramePtr;
+ Jim_AddHashEntry(&framePtr->vars, name + 2, var);
+ }
+ else {
+ Jim_AddHashEntry(&framePtr->vars, name, var);
+ }
+ /* Make the object int rep a variable */
+ Jim_FreeIntRep(interp, nameObjPtr);
+ nameObjPtr->typePtr = &variableObjType;
+ nameObjPtr->internalRep.varValue.callFrameId = framePtr->id;
+ nameObjPtr->internalRep.varValue.varPtr = var;
+ }
+ else {
+ var = nameObjPtr->internalRep.varValue.varPtr;
+ if (var->linkFramePtr == NULL) {
+ Jim_IncrRefCount(valObjPtr);
+ Jim_DecrRefCount(interp, var->objPtr);
+ var->objPtr = valObjPtr;
+ }
+ else { /* Else handle the link */
+ Jim_CallFrame *savedCallFrame;
+
+ savedCallFrame = interp->framePtr;
+ interp->framePtr = var->linkFramePtr;
+ err = Jim_SetVariable(interp, var->objPtr, valObjPtr);
+ interp->framePtr = savedCallFrame;
+ if (err != JIM_OK)
+ return err;
+ }
+ }
+ return JIM_OK;
+}
+
+int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr)
+{
+ Jim_Obj *nameObjPtr;
+ int result;
+
+ nameObjPtr = Jim_NewStringObj(interp, name, -1);
+ Jim_IncrRefCount(nameObjPtr);
+ result = Jim_SetVariable(interp, nameObjPtr, objPtr);
+ Jim_DecrRefCount(interp, nameObjPtr);
+ return result;
+}
+
+int Jim_SetGlobalVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr)
+{
+ Jim_CallFrame *savedFramePtr;
+ int result;
+
+ savedFramePtr = interp->framePtr;
+ interp->framePtr = interp->topFramePtr;
+ result = Jim_SetVariableStr(interp, name, objPtr);
+ interp->framePtr = savedFramePtr;
+ return result;
+}
+
+int Jim_SetVariableStrWithStr(Jim_Interp *interp, const char *name, const char *val)
+{
+ Jim_Obj *nameObjPtr, *valObjPtr;
+ int result;
+
+ nameObjPtr = Jim_NewStringObj(interp, name, -1);
+ valObjPtr = Jim_NewStringObj(interp, val, -1);
+ Jim_IncrRefCount(nameObjPtr);
+ Jim_IncrRefCount(valObjPtr);
+ result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
+ Jim_DecrRefCount(interp, nameObjPtr);
+ Jim_DecrRefCount(interp, valObjPtr);
+ return result;
+}
+
+int Jim_SetVariableLink(Jim_Interp *interp, Jim_Obj *nameObjPtr,
+ Jim_Obj *targetNameObjPtr, Jim_CallFrame *targetCallFrame)
+{
+ const char *varName;
+ int len;
+
+ varName = Jim_GetString(nameObjPtr, &len);
+
+ if (varName[0] == ':' && varName[1] == ':') {
+ /* Linking a global var does nothing */
+ return JIM_OK;
+ }
+
+ if (JimNameIsDictSugar(varName, len)) {
+ Jim_SetResultString(interp, "Dict key syntax invalid as link source", -1);
+ return JIM_ERR;
+ }
+
+ /* Check for an existing variable or link */
+ if (SetVariableFromAny(interp, nameObjPtr) == JIM_OK) {
+ Jim_Var *varPtr = nameObjPtr->internalRep.varValue.varPtr;
+
+ if (varPtr->linkFramePtr == NULL) {
+ Jim_SetResultFormatted(interp, "variable \"%#s\" already exists", nameObjPtr);
+ return JIM_ERR;
+ }
+
+ /* It exists, but is a link, so delete the link */
+ varPtr->linkFramePtr = NULL;
+ }
+
+ /* Check for cycles. */
+ if (interp->framePtr == targetCallFrame) {
+ Jim_Obj *objPtr = targetNameObjPtr;
+ Jim_Var *varPtr;
+
+ /* Cycles are only possible with 'uplevel 0' */
+ while (1) {
+ if (Jim_StringEqObj(objPtr, nameObjPtr)) {
+ Jim_SetResultString(interp, "can't upvar from variable to itself", -1);
+ return JIM_ERR;
+ }
+ if (SetVariableFromAny(interp, objPtr) != JIM_OK)
+ break;
+ varPtr = objPtr->internalRep.varValue.varPtr;
+ if (varPtr->linkFramePtr != targetCallFrame)
+ break;
+ objPtr = varPtr->objPtr;
+ }
+ }
+
+ /* Perform the binding */
+ Jim_SetVariable(interp, nameObjPtr, targetNameObjPtr);
+ /* We are now sure 'nameObjPtr' type is variableObjType */
+ nameObjPtr->internalRep.varValue.varPtr->linkFramePtr = targetCallFrame;
+ return JIM_OK;
+}
+
+/* Return the Jim_Obj pointer associated with a variable name,
+ * or NULL if the variable was not found in the current context.
+ * The same optimization discussed in the comment to the
+ * 'SetVariable' function should apply here.
+ *
+ * If JIM_UNSHARED is set and the variable is an array element (dict sugar)
+ * in a dictionary which is shared, the array variable value is duplicated first.
+ * This allows the array element to be updated (e.g. append, lappend) without
+ * affecting other references to the dictionary.
+ */
+Jim_Obj *Jim_GetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags)
+{
+ switch (SetVariableFromAny(interp, nameObjPtr)) {
+ case JIM_OK:{
+ Jim_Var *varPtr = nameObjPtr->internalRep.varValue.varPtr;
+
+ if (varPtr->linkFramePtr == NULL) {
+ return varPtr->objPtr;
+ }
+ else {
+ Jim_Obj *objPtr;
+
+ /* The variable is a link? Resolve it. */
+ Jim_CallFrame *savedCallFrame = interp->framePtr;
+
+ interp->framePtr = varPtr->linkFramePtr;
+ objPtr = Jim_GetVariable(interp, varPtr->objPtr, flags);
+ interp->framePtr = savedCallFrame;
+ if (objPtr) {
+ return objPtr;
+ }
+ /* Error, so fall through to the error message */
+ }
+ }
+ break;
+
+ case JIM_DICT_SUGAR:
+ /* [dict] syntax sugar. */
+ return JimDictSugarGet(interp, nameObjPtr, flags);
+ }
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultFormatted(interp, "can't read \"%#s\": no such variable", nameObjPtr);
+ }
+ return NULL;
+}
+
+Jim_Obj *Jim_GetGlobalVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags)
+{
+ Jim_CallFrame *savedFramePtr;
+ Jim_Obj *objPtr;
+
+ savedFramePtr = interp->framePtr;
+ interp->framePtr = interp->topFramePtr;
+ objPtr = Jim_GetVariable(interp, nameObjPtr, flags);
+ interp->framePtr = savedFramePtr;
+
+ return objPtr;
+}
+
+Jim_Obj *Jim_GetVariableStr(Jim_Interp *interp, const char *name, int flags)
+{
+ Jim_Obj *nameObjPtr, *varObjPtr;
+
+ nameObjPtr = Jim_NewStringObj(interp, name, -1);
+ Jim_IncrRefCount(nameObjPtr);
+ varObjPtr = Jim_GetVariable(interp, nameObjPtr, flags);
+ Jim_DecrRefCount(interp, nameObjPtr);
+ return varObjPtr;
+}
+
+Jim_Obj *Jim_GetGlobalVariableStr(Jim_Interp *interp, const char *name, int flags)
+{
+ Jim_CallFrame *savedFramePtr;
+ Jim_Obj *objPtr;
+
+ savedFramePtr = interp->framePtr;
+ interp->framePtr = interp->topFramePtr;
+ objPtr = Jim_GetVariableStr(interp, name, flags);
+ interp->framePtr = savedFramePtr;
+
+ return objPtr;
+}
+
+/* Unset a variable.
+ * Note: On success unset invalidates all the variable objects created
+ * in the current call frame incrementing. */
+int Jim_UnsetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags)
+{
+ const char *name;
+ Jim_Var *varPtr;
+ int retval;
+
+ retval = SetVariableFromAny(interp, nameObjPtr);
+ if (retval == JIM_DICT_SUGAR) {
+ /* [dict] syntax sugar. */
+ return JimDictSugarSet(interp, nameObjPtr, NULL);
+ }
+ else if (retval == JIM_OK) {
+ varPtr = nameObjPtr->internalRep.varValue.varPtr;
+
+ /* If it's a link call UnsetVariable recursively */
+ if (varPtr->linkFramePtr) {
+ Jim_CallFrame *savedCallFrame;
+
+ savedCallFrame = interp->framePtr;
+ interp->framePtr = varPtr->linkFramePtr;
+ retval = Jim_UnsetVariable(interp, varPtr->objPtr, JIM_NONE);
+ interp->framePtr = savedCallFrame;
+ }
+ else {
+ Jim_CallFrame *framePtr = interp->framePtr;
+
+ name = Jim_String(nameObjPtr);
+ if (name[0] == ':' && name[1] == ':') {
+ framePtr = interp->topFramePtr;
+ name += 2;
+ }
+ retval = Jim_DeleteHashEntry(&framePtr->vars, name);
+ if (retval == JIM_OK) {
+ /* Change the callframe id, invalidating var lookup caching */
+ JimChangeCallFrameId(interp, framePtr);
+ }
+ }
+ }
+ if (retval != JIM_OK && (flags & JIM_ERRMSG)) {
+ Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such variable", nameObjPtr);
+ }
+ return retval;
+}
+
+/* ---------- Dict syntax sugar (similar to array Tcl syntax) -------------- */
+
+/* Given a variable name for [dict] operation syntax sugar,
+ * this function returns two objects, the first with the name
+ * of the variable to set, and the second with the rispective key.
+ * For example "foo(bar)" will return objects with string repr. of
+ * "foo" and "bar".
+ *
+ * The returned objects have refcount = 1. The function can't fail. */
+static void JimDictSugarParseVarKey(Jim_Interp *interp, Jim_Obj *objPtr,
+ Jim_Obj **varPtrPtr, Jim_Obj **keyPtrPtr)
+{
+ const char *str, *p;
+ int len, keyLen;
+ Jim_Obj *varObjPtr, *keyObjPtr;
+
+ str = Jim_GetString(objPtr, &len);
+
+ p = strchr(str, '(');
+ JimPanic((p == NULL, interp, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str));
+
+ varObjPtr = Jim_NewStringObj(interp, str, p - str);
+
+ p++;
+ keyLen = (str + len) - p;
+ if (str[len - 1] == ')') {
+ keyLen--;
+ }
+
+ /* Create the objects with the variable name and key. */
+ keyObjPtr = Jim_NewStringObj(interp, p, keyLen);
+
+ Jim_IncrRefCount(varObjPtr);
+ Jim_IncrRefCount(keyObjPtr);
+ *varPtrPtr = varObjPtr;
+ *keyPtrPtr = keyObjPtr;
+}
+
+/* Helper of Jim_SetVariable() to deal with dict-syntax variable names.
+ * Also used by Jim_UnsetVariable() with valObjPtr = NULL. */
+static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *valObjPtr)
+{
+ int err;
+
+ SetDictSubstFromAny(interp, objPtr);
+
+ err = Jim_SetDictKeysVector(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr,
+ &objPtr->internalRep.dictSubstValue.indexObjPtr, 1, valObjPtr);
+
+ if (err == JIM_OK) {
+ /* Don't keep an extra ref to the result */
+ Jim_SetEmptyResult(interp);
+ }
+ else {
+ if (!valObjPtr) {
+ /* Better error message for unset a(2) where a exists but a(2) doesn't */
+ if (Jim_GetVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_NONE)) {
+ Jim_SetResultFormatted(interp, "can't unset \"%#s\": no such element in array",
+ objPtr);
+ return err;
+ }
+ }
+ /* Make the error more informative and Tcl-compatible */
+ Jim_SetResultFormatted(interp, "can't %s \"%#s\": variable isn't array",
+ (valObjPtr ? "set" : "unset"), objPtr);
+ }
+ return err;
+}
+
+/**
+ * Expands the array variable (dict sugar) and returns the result, or NULL on error.
+ *
+ * If JIM_UNSHARED is set and the dictionary is shared, it will be duplicated
+ * and stored back to the variable before expansion.
+ */
+static Jim_Obj *JimDictExpandArrayVariable(Jim_Interp *interp, Jim_Obj *varObjPtr,
+ Jim_Obj *keyObjPtr, int flags)
+{
+ Jim_Obj *dictObjPtr;
+ Jim_Obj *resObjPtr = NULL;
+ int ret;
+
+ dictObjPtr = Jim_GetVariable(interp, varObjPtr, JIM_ERRMSG);
+ if (!dictObjPtr) {
+ return NULL;
+ }
+
+ ret = Jim_DictKey(interp, dictObjPtr, keyObjPtr, &resObjPtr, JIM_NONE);
+ if (ret != JIM_OK) {
+ resObjPtr = NULL;
+ if (ret < 0) {
+ Jim_SetResultFormatted(interp,
+ "can't read \"%#s(%#s)\": variable isn't array", varObjPtr, keyObjPtr);
+ }
+ else {
+ Jim_SetResultFormatted(interp,
+ "can't read \"%#s(%#s)\": no such element in array", varObjPtr, keyObjPtr);
+ }
+ }
+ else if ((flags & JIM_UNSHARED) && Jim_IsShared(dictObjPtr)) {
+ dictObjPtr = Jim_DuplicateObj(interp, dictObjPtr);
+ if (Jim_SetVariable(interp, varObjPtr, dictObjPtr) != JIM_OK) {
+ /* This can probably never happen */
+ JimPanic((1, interp, "SetVariable failed for JIM_UNSHARED"));
+ }
+ /* We know that the key exists. Get the result in the now-unshared dictionary */
+ Jim_DictKey(interp, dictObjPtr, keyObjPtr, &resObjPtr, JIM_NONE);
+ }
+
+ return resObjPtr;
+}
+
+/* Helper of Jim_GetVariable() to deal with dict-syntax variable names */
+static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr, int flags)
+{
+ SetDictSubstFromAny(interp, objPtr);
+
+ return JimDictExpandArrayVariable(interp,
+ objPtr->internalRep.dictSubstValue.varNameObjPtr,
+ objPtr->internalRep.dictSubstValue.indexObjPtr, flags);
+}
+
+/* --------- $var(INDEX) substitution, using a specialized object ----------- */
+
+void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr);
+ Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr);
+}
+
+void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ JIM_NOTUSED(interp);
+
+ dupPtr->internalRep.dictSubstValue.varNameObjPtr =
+ srcPtr->internalRep.dictSubstValue.varNameObjPtr;
+ dupPtr->internalRep.dictSubstValue.indexObjPtr = srcPtr->internalRep.dictSubstValue.indexObjPtr;
+ dupPtr->typePtr = &dictSubstObjType;
+}
+
+/* Note: The object *must* be in dict-sugar format */
+static void SetDictSubstFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ if (objPtr->typePtr != &dictSubstObjType) {
+ Jim_Obj *varObjPtr, *keyObjPtr;
+
+ if (objPtr->typePtr == &interpolatedObjType) {
+ /* An interpolated object in dict-sugar form */
+
+ const ScriptToken *token = objPtr->internalRep.twoPtrValue.ptr1;
+
+ varObjPtr = token[0].objPtr;
+ keyObjPtr = objPtr->internalRep.twoPtrValue.ptr2;
+
+ Jim_IncrRefCount(varObjPtr);
+ Jim_IncrRefCount(keyObjPtr);
+ }
+ else {
+ JimDictSugarParseVarKey(interp, objPtr, &varObjPtr, &keyObjPtr);
+ }
+
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &dictSubstObjType;
+ objPtr->internalRep.dictSubstValue.varNameObjPtr = varObjPtr;
+ objPtr->internalRep.dictSubstValue.indexObjPtr = keyObjPtr;
+ }
+}
+
+/* This function is used to expand [dict get] sugar in the form
+ * of $var(INDEX). The function is mainly used by Jim_EvalObj()
+ * to deal with tokens of type JIM_TT_DICTSUGAR. objPtr points to an
+ * object that is *guaranteed* to be in the form VARNAME(INDEX).
+ * The 'index' part is [subst]ituted, and is used to lookup a key inside
+ * the [dict]ionary contained in variable VARNAME. */
+static Jim_Obj *JimExpandDictSugar(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_Obj *resObjPtr = NULL;
+ Jim_Obj *substKeyObjPtr = NULL;
+
+ SetDictSubstFromAny(interp, objPtr);
+
+ if (Jim_SubstObj(interp, objPtr->internalRep.dictSubstValue.indexObjPtr,
+ &substKeyObjPtr, JIM_NONE)
+ != JIM_OK) {
+ return NULL;
+ }
+ Jim_IncrRefCount(substKeyObjPtr);
+ resObjPtr =
+ JimDictExpandArrayVariable(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr,
+ substKeyObjPtr, 0);
+ Jim_DecrRefCount(interp, substKeyObjPtr);
+
+ return resObjPtr;
+}
+
+static Jim_Obj *JimExpandExprSugar(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ Jim_Obj *resultObjPtr;
+
+ if (Jim_EvalExpression(interp, objPtr, &resultObjPtr) == JIM_OK) {
+ /* Note that the result has a ref count of 1, but we need a ref count of 0 */
+ resultObjPtr->refCount--;
+ return resultObjPtr;
+ }
+ return NULL;
+}
+
+/* -----------------------------------------------------------------------------
+ * CallFrame
+ * ---------------------------------------------------------------------------*/
+
+static Jim_CallFrame *JimCreateCallFrame(Jim_Interp *interp, Jim_CallFrame *parent)
+{
+ Jim_CallFrame *cf;
+
+ if (interp->freeFramesList) {
+ cf = interp->freeFramesList;
+ interp->freeFramesList = cf->nextFramePtr;
+ }
+ else {
+ cf = Jim_Alloc(sizeof(*cf));
+ cf->vars.table = NULL;
+ }
+
+ cf->id = interp->callFrameEpoch++;
+ cf->parentCallFrame = parent;
+ cf->level = parent ? parent->level + 1 : 0;
+ cf->argv = NULL;
+ cf->argc = 0;
+ cf->procArgsObjPtr = NULL;
+ cf->procBodyObjPtr = NULL;
+ cf->nextFramePtr = NULL;
+ cf->staticVars = NULL;
+ if (cf->vars.table == NULL)
+ Jim_InitHashTable(&cf->vars, &JimVariablesHashTableType, interp);
+ return cf;
+}
+
+/* Used to invalidate every caching related to callframe stability. */
+static void JimChangeCallFrameId(Jim_Interp *interp, Jim_CallFrame *cf)
+{
+ cf->id = interp->callFrameEpoch++;
+}
+
+#define JIM_FCF_NONE 0 /* no flags */
+#define JIM_FCF_NOHT 1 /* don't free the hash table */
+static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int flags)
+{
+ if (cf->procArgsObjPtr)
+ Jim_DecrRefCount(interp, cf->procArgsObjPtr);
+ if (cf->procBodyObjPtr)
+ Jim_DecrRefCount(interp, cf->procBodyObjPtr);
+ if (!(flags & JIM_FCF_NOHT))
+ Jim_FreeHashTable(&cf->vars);
+ else {
+ int i;
+ Jim_HashEntry **table = cf->vars.table, *he;
+
+ for (i = 0; i < JIM_HT_INITIAL_SIZE; i++) {
+ he = table[i];
+ while (he != NULL) {
+ Jim_HashEntry *nextEntry = he->next;
+ Jim_Var *varPtr = (void *)he->val;
+
+ Jim_DecrRefCount(interp, varPtr->objPtr);
+ Jim_Free(he->val);
+ Jim_Free((void *)he->key); /* ATTENTION: const cast */
+ Jim_Free(he);
+ table[i] = NULL;
+ he = nextEntry;
+ }
+ }
+ cf->vars.used = 0;
+ }
+ cf->nextFramePtr = interp->freeFramesList;
+ interp->freeFramesList = cf;
+}
+
+/* -----------------------------------------------------------------------------
+ * References
+ * ---------------------------------------------------------------------------*/
+#ifdef JIM_REFERENCES
+
+/* References HashTable Type.
+ *
+ * Keys are jim_wide integers, dynamically allocated for now but in the
+ * future it's worth to cache this 8 bytes objects. Values are poitners
+ * to Jim_References. */
+static void JimReferencesHTValDestructor(void *interp, void *val)
+{
+ Jim_Reference *refPtr = (void *)val;
+
+ Jim_DecrRefCount(interp, refPtr->objPtr);
+ if (refPtr->finalizerCmdNamePtr != NULL) {
+ Jim_DecrRefCount(interp, refPtr->finalizerCmdNamePtr);
+ }
+ Jim_Free(val);
+}
+
+static unsigned int JimReferencesHTHashFunction(const void *key)
+{
+ /* Only the least significant bits are used. */
+ const jim_wide *widePtr = key;
+ unsigned int intValue = (unsigned int)*widePtr;
+
+ return Jim_IntHashFunction(intValue);
+}
+
+static const void *JimReferencesHTKeyDup(void *privdata, const void *key)
+{
+ void *copy = Jim_Alloc(sizeof(jim_wide));
+
+ JIM_NOTUSED(privdata);
+
+ memcpy(copy, key, sizeof(jim_wide));
+ return copy;
+}
+
+static int JimReferencesHTKeyCompare(void *privdata, const void *key1, const void *key2)
+{
+ JIM_NOTUSED(privdata);
+
+ return memcmp(key1, key2, sizeof(jim_wide)) == 0;
+}
+
+static void JimReferencesHTKeyDestructor(void *privdata, const void *key)
+{
+ JIM_NOTUSED(privdata);
+
+ Jim_Free((void *)key);
+}
+
+static const Jim_HashTableType JimReferencesHashTableType = {
+ JimReferencesHTHashFunction, /* hash function */
+ JimReferencesHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimReferencesHTKeyCompare, /* key compare */
+ JimReferencesHTKeyDestructor, /* key destructor */
+ JimReferencesHTValDestructor /* val destructor */
+};
+
+/* -----------------------------------------------------------------------------
+ * Reference object type and References API
+ * ---------------------------------------------------------------------------*/
+
+/* The string representation of references has two features in order
+ * to make the GC faster. The first is that every reference starts
+ * with a non common character '<', in order to make the string matching
+ * faster. The second is that the reference string rep is 42 characters
+ * in length, this allows to avoid to check every object with a string
+ * repr < 42, and usually there aren't many of these objects. */
+
+#define JIM_REFERENCE_SPACE (35+JIM_REFERENCE_TAGLEN)
+
+static int JimFormatReference(char *buf, Jim_Reference *refPtr, jim_wide id)
+{
+ const char *fmt = "<reference.<%s>.%020" JIM_WIDE_MODIFIER ">";
+
+ sprintf(buf, fmt, refPtr->tag, id);
+ return JIM_REFERENCE_SPACE;
+}
+
+static void UpdateStringOfReference(struct Jim_Obj *objPtr);
+
+static const Jim_ObjType referenceObjType = {
+ "reference",
+ NULL,
+ NULL,
+ UpdateStringOfReference,
+ JIM_TYPE_REFERENCES,
+};
+
+void UpdateStringOfReference(struct Jim_Obj *objPtr)
+{
+ int len;
+ char buf[JIM_REFERENCE_SPACE + 1];
+ Jim_Reference *refPtr;
+
+ refPtr = objPtr->internalRep.refValue.refPtr;
+ len = JimFormatReference(buf, refPtr, objPtr->internalRep.refValue.id);
+ objPtr->bytes = Jim_Alloc(len + 1);
+ memcpy(objPtr->bytes, buf, len + 1);
+ objPtr->length = len;
+}
+
+/* returns true if 'c' is a valid reference tag character.
+ * i.e. inside the range [_a-zA-Z0-9] */
+static int isrefchar(int c)
+{
+ return (c == '_' || isalnum(c));
+}
+
+static int SetReferenceFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ jim_wide wideValue;
+ int i, len;
+ const char *str, *start, *end;
+ char refId[21];
+ Jim_Reference *refPtr;
+ Jim_HashEntry *he;
+
+ /* Get the string representation */
+ str = Jim_GetString(objPtr, &len);
+ /* Check if it looks like a reference */
+ if (len < JIM_REFERENCE_SPACE)
+ goto badformat;
+ /* Trim spaces */
+ start = str;
+ end = str + len - 1;
+ while (*start == ' ')
+ start++;
+ while (*end == ' ' && end > start)
+ end--;
+ if (end - start + 1 != JIM_REFERENCE_SPACE)
+ goto badformat;
+ /* <reference.<1234567>.%020> */
+ if (memcmp(start, "<reference.<", 12) != 0)
+ goto badformat;
+ if (start[12 + JIM_REFERENCE_TAGLEN] != '>' || end[0] != '>')
+ goto badformat;
+ /* The tag can't contain chars other than a-zA-Z0-9 + '_'. */
+ for (i = 0; i < JIM_REFERENCE_TAGLEN; i++) {
+ if (!isrefchar(start[12 + i]))
+ goto badformat;
+ }
+ /* Extract info from the reference. */
+ memcpy(refId, start + 14 + JIM_REFERENCE_TAGLEN, 20);
+ refId[20] = '\0';
+ /* Try to convert the ID into a jim_wide */
+ if (Jim_StringToWide(refId, &wideValue, 10) != JIM_OK)
+ goto badformat;
+ /* Check if the reference really exists! */
+ he = Jim_FindHashEntry(&interp->references, &wideValue);
+ if (he == NULL) {
+ Jim_SetResultFormatted(interp, "invalid reference id \"%#s\"", objPtr);
+ return JIM_ERR;
+ }
+ refPtr = he->val;
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &referenceObjType;
+ objPtr->internalRep.refValue.id = wideValue;
+ objPtr->internalRep.refValue.refPtr = refPtr;
+ return JIM_OK;
+
+ badformat:
+ Jim_SetResultFormatted(interp, "expected reference but got \"%#s\"", objPtr);
+ return JIM_ERR;
+}
+
+/* Returns a new reference pointing to objPtr, having cmdNamePtr
+ * as finalizer command (or NULL if there is no finalizer).
+ * The returned reference object has refcount = 0. */
+Jim_Obj *Jim_NewReference(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr)
+{
+ struct Jim_Reference *refPtr;
+ jim_wide wideValue = interp->referenceNextId;
+ Jim_Obj *refObjPtr;
+ const char *tag;
+ int tagLen, i;
+
+ /* Perform the Garbage Collection if needed. */
+ Jim_CollectIfNeeded(interp);
+
+ refPtr = Jim_Alloc(sizeof(*refPtr));
+ refPtr->objPtr = objPtr;
+ Jim_IncrRefCount(objPtr);
+ refPtr->finalizerCmdNamePtr = cmdNamePtr;
+ if (cmdNamePtr)
+ Jim_IncrRefCount(cmdNamePtr);
+ Jim_AddHashEntry(&interp->references, &wideValue, refPtr);
+ refObjPtr = Jim_NewObj(interp);
+ refObjPtr->typePtr = &referenceObjType;
+ refObjPtr->bytes = NULL;
+ refObjPtr->internalRep.refValue.id = interp->referenceNextId;
+ refObjPtr->internalRep.refValue.refPtr = refPtr;
+ interp->referenceNextId++;
+ /* Set the tag. Trimmed at JIM_REFERENCE_TAGLEN. Everything
+ * that does not pass the 'isrefchar' test is replaced with '_' */
+ tag = Jim_GetString(tagPtr, &tagLen);
+ if (tagLen > JIM_REFERENCE_TAGLEN)
+ tagLen = JIM_REFERENCE_TAGLEN;
+ for (i = 0; i < JIM_REFERENCE_TAGLEN; i++) {
+ if (i < tagLen && isrefchar(tag[i]))
+ refPtr->tag[i] = tag[i];
+ else
+ refPtr->tag[i] = '_';
+ }
+ refPtr->tag[JIM_REFERENCE_TAGLEN] = '\0';
+ return refObjPtr;
+}
+
+Jim_Reference *Jim_GetReference(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ if (objPtr->typePtr != &referenceObjType && SetReferenceFromAny(interp, objPtr) == JIM_ERR)
+ return NULL;
+ return objPtr->internalRep.refValue.refPtr;
+}
+
+int Jim_SetFinalizer(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr)
+{
+ Jim_Reference *refPtr;
+
+ if ((refPtr = Jim_GetReference(interp, objPtr)) == NULL)
+ return JIM_ERR;
+ Jim_IncrRefCount(cmdNamePtr);
+ if (refPtr->finalizerCmdNamePtr)
+ Jim_DecrRefCount(interp, refPtr->finalizerCmdNamePtr);
+ refPtr->finalizerCmdNamePtr = cmdNamePtr;
+ return JIM_OK;
+}
+
+int Jim_GetFinalizer(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr)
+{
+ Jim_Reference *refPtr;
+
+ if ((refPtr = Jim_GetReference(interp, objPtr)) == NULL)
+ return JIM_ERR;
+ *cmdNamePtrPtr = refPtr->finalizerCmdNamePtr;
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * References Garbage Collection
+ * ---------------------------------------------------------------------------*/
+
+/* This the hash table type for the "MARK" phase of the GC */
+static const Jim_HashTableType JimRefMarkHashTableType = {
+ JimReferencesHTHashFunction, /* hash function */
+ JimReferencesHTKeyDup, /* key dup */
+ NULL, /* val dup */
+ JimReferencesHTKeyCompare, /* key compare */
+ JimReferencesHTKeyDestructor, /* key destructor */
+ NULL /* val destructor */
+};
+
+/* Performs the garbage collection. */
+int Jim_Collect(Jim_Interp *interp)
+{
+ Jim_HashTable marks;
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+ Jim_Obj *objPtr;
+ int collected = 0;
+
+ /* Avoid recursive calls */
+ if (interp->lastCollectId == -1) {
+ /* Jim_Collect() already running. Return just now. */
+ return 0;
+ }
+ interp->lastCollectId = -1;
+
+ /* Mark all the references found into the 'mark' hash table.
+ * The references are searched in every live object that
+ * is of a type that can contain references. */
+ Jim_InitHashTable(&marks, &JimRefMarkHashTableType, NULL);
+ objPtr = interp->liveList;
+ while (objPtr) {
+ if (objPtr->typePtr == NULL || objPtr->typePtr->flags & JIM_TYPE_REFERENCES) {
+ const char *str, *p;
+ int len;
+
+ /* If the object is of type reference, to get the
+ * Id is simple... */
+ if (objPtr->typePtr == &referenceObjType) {
+ Jim_AddHashEntry(&marks, &objPtr->internalRep.refValue.id, NULL);
+#ifdef JIM_DEBUG_GC
+ printf("MARK (reference): %d refcount: %d" JIM_NL,
+ (int)objPtr->internalRep.refValue.id, objPtr->refCount);
+#endif
+ objPtr = objPtr->nextObjPtr;
+ continue;
+ }
+ /* Get the string repr of the object we want
+ * to scan for references. */
+ p = str = Jim_GetString(objPtr, &len);
+ /* Skip objects too little to contain references. */
+ if (len < JIM_REFERENCE_SPACE) {
+ objPtr = objPtr->nextObjPtr;
+ continue;
+ }
+ /* Extract references from the object string repr. */
+ while (1) {
+ int i;
+ jim_wide id;
+ char buf[21];
+
+ if ((p = strstr(p, "<reference.<")) == NULL)
+ break;
+ /* Check if it's a valid reference. */
+ if (len - (p - str) < JIM_REFERENCE_SPACE)
+ break;
+ if (p[41] != '>' || p[19] != '>' || p[20] != '.')
+ break;
+ for (i = 21; i <= 40; i++)
+ if (!isdigit(UCHAR(p[i])))
+ break;
+ /* Get the ID */
+ memcpy(buf, p + 21, 20);
+ buf[20] = '\0';
+ Jim_StringToWide(buf, &id, 10);
+
+ /* Ok, a reference for the given ID
+ * was found. Mark it. */
+ Jim_AddHashEntry(&marks, &id, NULL);
+#ifdef JIM_DEBUG_GC
+ printf("MARK: %d" JIM_NL, (int)id);
+#endif
+ p += JIM_REFERENCE_SPACE;
+ }
+ }
+ objPtr = objPtr->nextObjPtr;
+ }
+
+ /* Run the references hash table to destroy every reference that
+ * is not referenced outside (not present in the mark HT). */
+ htiter = Jim_GetHashTableIterator(&interp->references);
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ const jim_wide *refId;
+ Jim_Reference *refPtr;
+
+ refId = he->key;
+ /* Check if in the mark phase we encountered
+ * this reference. */
+ if (Jim_FindHashEntry(&marks, refId) == NULL) {
+#ifdef JIM_DEBUG_GC
+ printf("COLLECTING %d" JIM_NL, (int)*refId);
+#endif
+ collected++;
+ /* Drop the reference, but call the
+ * finalizer first if registered. */
+ refPtr = he->val;
+ if (refPtr->finalizerCmdNamePtr) {
+ char *refstr = Jim_Alloc(JIM_REFERENCE_SPACE + 1);
+ Jim_Obj *objv[3], *oldResult;
+
+ JimFormatReference(refstr, refPtr, *refId);
+
+ objv[0] = refPtr->finalizerCmdNamePtr;
+ objv[1] = Jim_NewStringObjNoAlloc(interp, refstr, 32);
+ objv[2] = refPtr->objPtr;
+ Jim_IncrRefCount(objv[0]);
+ Jim_IncrRefCount(objv[1]);
+ Jim_IncrRefCount(objv[2]);
+
+ /* Drop the reference itself */
+ Jim_DeleteHashEntry(&interp->references, refId);
+
+ /* Call the finalizer. Errors ignored. */
+ oldResult = interp->result;
+ Jim_IncrRefCount(oldResult);
+ Jim_EvalObjVector(interp, 3, objv);
+ Jim_SetResult(interp, oldResult);
+ Jim_DecrRefCount(interp, oldResult);
+
+ Jim_DecrRefCount(interp, objv[0]);
+ Jim_DecrRefCount(interp, objv[1]);
+ Jim_DecrRefCount(interp, objv[2]);
+ }
+ else {
+ Jim_DeleteHashEntry(&interp->references, refId);
+ }
+ }
+ }
+ Jim_FreeHashTableIterator(htiter);
+ Jim_FreeHashTable(&marks);
+ interp->lastCollectId = interp->referenceNextId;
+ interp->lastCollectTime = time(NULL);
+ return collected;
+}
+
+#define JIM_COLLECT_ID_PERIOD 5000
+#define JIM_COLLECT_TIME_PERIOD 300
+
+void Jim_CollectIfNeeded(Jim_Interp *interp)
+{
+ jim_wide elapsedId;
+ int elapsedTime;
+
+ elapsedId = interp->referenceNextId - interp->lastCollectId;
+ elapsedTime = time(NULL) - interp->lastCollectTime;
+
+
+ if (elapsedId > JIM_COLLECT_ID_PERIOD || elapsedTime > JIM_COLLECT_TIME_PERIOD) {
+ Jim_Collect(interp);
+ }
+}
+#endif
+
+static int JimIsBigEndian(void)
+{
+ union {
+ unsigned short s;
+ unsigned char c[2];
+ } uval = {0x0102};
+
+ return uval.c[0] == 1;
+}
+
+/* -----------------------------------------------------------------------------
+ * Interpreter related functions
+ * ---------------------------------------------------------------------------*/
+
+Jim_Interp *Jim_CreateInterp(void)
+{
+ Jim_Interp *i = Jim_Alloc(sizeof(*i));
+
+ i->errorLine = 0;
+ i->errorFileName = Jim_StrDup("");
+ i->addStackTrace = 0;
+ i->maxNestingDepth = JIM_MAX_NESTING_DEPTH;
+ i->returnCode = JIM_OK;
+ i->returnLevel = 0;
+ i->exitCode = 0;
+ i->procEpoch = 0;
+ i->callFrameEpoch = 0;
+ i->liveList = i->freeList = NULL;
+ i->referenceNextId = 0;
+ i->lastCollectId = 0;
+ i->lastCollectTime = time(NULL);
+ i->freeFramesList = NULL;
+ i->prngState = NULL;
+ i->id = 0;
+ i->sigmask = 0;
+ i->signal_level = 0;
+ i->signal_set_result = NULL;
+ i->localProcs = NULL;
+ i->loadHandles = NULL;
+
+ /* Note that we can create objects only after the
+ * interpreter liveList and freeList pointers are
+ * initialized to NULL. */
+ Jim_InitHashTable(&i->commands, &JimCommandsHashTableType, i);
+ i->local = 0;
+#ifdef JIM_REFERENCES
+ Jim_InitHashTable(&i->references, &JimReferencesHashTableType, i);
+#endif
+ Jim_InitHashTable(&i->sharedStrings, &JimSharedStringsHashTableType, NULL);
+ Jim_InitHashTable(&i->assocData, &JimAssocDataHashTableType, i);
+ Jim_InitHashTable(&i->packages, &JimStringKeyValCopyHashTableType, NULL);
+ i->framePtr = i->topFramePtr = JimCreateCallFrame(i, NULL);
+ i->emptyObj = Jim_NewEmptyStringObj(i);
+ i->trueObj = Jim_NewIntObj(i, 1);
+ i->falseObj = Jim_NewIntObj(i, 0);
+ i->result = i->emptyObj;
+ i->stackTrace = Jim_NewListObj(i, NULL, 0);
+ i->unknown = Jim_NewStringObj(i, "unknown", -1);
+ i->unknown_called = 0;
+ i->errorProc = i->emptyObj;
+ i->currentScriptObj = Jim_NewEmptyStringObj(i);
+ Jim_IncrRefCount(i->emptyObj);
+ Jim_IncrRefCount(i->result);
+ Jim_IncrRefCount(i->stackTrace);
+ Jim_IncrRefCount(i->unknown);
+ Jim_IncrRefCount(i->currentScriptObj);
+ Jim_IncrRefCount(i->errorProc);
+ Jim_IncrRefCount(i->trueObj);
+ Jim_IncrRefCount(i->falseObj);
+
+ /* Initialize key variables every interpreter should contain */
+ Jim_SetVariableStrWithStr(i, JIM_LIBPATH, TCL_LIBRARY);
+ Jim_SetVariableStrWithStr(i, JIM_INTERACTIVE, "0");
+
+ Jim_SetVariableStrWithStr(i, "tcl_platform(os)", TCL_PLATFORM_OS);
+ Jim_SetVariableStrWithStr(i, "tcl_platform(platform)", TCL_PLATFORM_PLATFORM);
+ Jim_SetVariableStrWithStr(i, "tcl_platform(byteOrder)", JimIsBigEndian() ? "bigEndian" : "littleEndian");
+ Jim_SetVariableStrWithStr(i, "tcl_platform(threaded)", "0");
+ Jim_SetVariableStr(i, "tcl_platform(pointerSize)", Jim_NewIntObj(i, sizeof(void *)));
+ Jim_SetVariableStr(i, "tcl_platform(wordSize)", Jim_NewIntObj(i, sizeof(jim_wide)));
+
+ return i;
+}
+
+void Jim_FreeInterp(Jim_Interp *i)
+{
+ Jim_CallFrame *cf = i->framePtr, *prevcf, *nextcf;
+ Jim_Obj *objPtr, *nextObjPtr;
+
+ Jim_DecrRefCount(i, i->emptyObj);
+ Jim_DecrRefCount(i, i->trueObj);
+ Jim_DecrRefCount(i, i->falseObj);
+ Jim_DecrRefCount(i, i->result);
+ Jim_DecrRefCount(i, i->stackTrace);
+ Jim_DecrRefCount(i, i->errorProc);
+ Jim_DecrRefCount(i, i->unknown);
+ Jim_Free((void *)i->errorFileName);
+ Jim_DecrRefCount(i, i->currentScriptObj);
+ Jim_FreeHashTable(&i->commands);
+#ifdef JIM_REFERENCES
+ Jim_FreeHashTable(&i->references);
+#endif
+ Jim_FreeHashTable(&i->packages);
+ Jim_Free(i->prngState);
+ Jim_FreeHashTable(&i->assocData);
+ JimDeleteLocalProcs(i);
+
+ /* Free the call frames list */
+ while (cf) {
+ prevcf = cf->parentCallFrame;
+ JimFreeCallFrame(i, cf, JIM_FCF_NONE);
+ cf = prevcf;
+ }
+ /* Check that the live object list is empty, otherwise
+ * there is a memory leak. */
+ if (i->liveList != NULL) {
+ objPtr = i->liveList;
+
+ printf(JIM_NL "-------------------------------------" JIM_NL);
+ printf("Objects still in the free list:" JIM_NL);
+ while (objPtr) {
+ const char *type = objPtr->typePtr ? objPtr->typePtr->name : "string";
+
+ printf("%p (%d) %-10s: '%.20s'" JIM_NL,
+ (void *)objPtr, objPtr->refCount, type, objPtr->bytes ? objPtr->bytes : "(null)");
+ if (objPtr->typePtr == &sourceObjType) {
+ printf("FILE %s LINE %d" JIM_NL,
+ objPtr->internalRep.sourceValue.fileName,
+ objPtr->internalRep.sourceValue.lineNumber);
+ }
+ objPtr = objPtr->nextObjPtr;
+ }
+ printf("-------------------------------------" JIM_NL JIM_NL);
+ JimPanic((1, i, "Live list non empty freeing the interpreter! Leak?"));
+ }
+ /* Free all the freed objects. */
+ objPtr = i->freeList;
+ while (objPtr) {
+ nextObjPtr = objPtr->nextObjPtr;
+ Jim_Free(objPtr);
+ objPtr = nextObjPtr;
+ }
+ /* Free cached CallFrame structures */
+ cf = i->freeFramesList;
+ while (cf) {
+ nextcf = cf->nextFramePtr;
+ if (cf->vars.table != NULL)
+ Jim_Free(cf->vars.table);
+ Jim_Free(cf);
+ cf = nextcf;
+ }
+#ifdef jim_ext_load
+ Jim_FreeLoadHandles(i);
+#endif
+
+ /* Free the sharedString hash table. Make sure to free it
+ * after every other Jim_Object was freed. */
+ Jim_FreeHashTable(&i->sharedStrings);
+ /* Free the interpreter structure. */
+ Jim_Free(i);
+}
+
+/* Returns the call frame relative to the level represented by
+ * levelObjPtr. If levelObjPtr == NULL, the * level is assumed to be '1'.
+ *
+ * This function accepts the 'level' argument in the form
+ * of the commands [uplevel] and [upvar].
+ *
+ * For a function accepting a relative integer as level suitable
+ * for implementation of [info level ?level?] check the
+ * JimGetCallFrameByInteger() function.
+ *
+ * Returns NULL on error.
+ */
+Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr)
+{
+ long level;
+ const char *str;
+ Jim_CallFrame *framePtr;
+
+ if (levelObjPtr) {
+ str = Jim_String(levelObjPtr);
+ if (str[0] == '#') {
+ char *endptr;
+
+ level = strtol(str + 1, &endptr, 0);
+ if (str[1] == '\0' || endptr[0] != '\0') {
+ level = -1;
+ }
+ }
+ else {
+ if (Jim_GetLong(interp, levelObjPtr, &level) != JIM_OK || level < 0) {
+ level = -1;
+ }
+ else {
+ /* Convert from a relative to an absolute level */
+ level = interp->framePtr->level - level;
+ }
+ }
+ }
+ else {
+ str = "1"; /* Needed to format the error message. */
+ level = interp->framePtr->level - 1;
+ }
+
+ if (level == 0) {
+ return interp->topFramePtr;
+ }
+ if (level > 0) {
+ /* Lookup */
+ for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parentCallFrame) {
+ if (framePtr->level == level) {
+ return framePtr;
+ }
+ }
+ }
+
+ Jim_SetResultFormatted(interp, "bad level \"%s\"", str);
+ return NULL;
+}
+
+/* Similar to Jim_GetCallFrameByLevel() but the level is specified
+ * as a relative integer like in the [info level ?level?] command.
+ **/
+static Jim_CallFrame *JimGetCallFrameByInteger(Jim_Interp *interp, Jim_Obj *levelObjPtr)
+{
+ long level;
+ Jim_CallFrame *framePtr;
+
+ if (Jim_GetLong(interp, levelObjPtr, &level) == JIM_OK) {
+ if (level <= 0) {
+ /* Convert from a relative to an absolute level */
+ level = interp->framePtr->level + level;
+ }
+
+ if (level == 0) {
+ return interp->topFramePtr;
+ }
+
+ /* Lookup */
+ for (framePtr = interp->framePtr; framePtr; framePtr = framePtr->parentCallFrame) {
+ if (framePtr->level == level) {
+ return framePtr;
+ }
+ }
+ }
+
+ Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr);
+ return NULL;
+}
+
+static void JimSetErrorFileName(Jim_Interp *interp, const char *filename)
+{
+ Jim_Free((void *)interp->errorFileName);
+ interp->errorFileName = Jim_StrDup(filename);
+}
+
+static void JimSetErrorLineNumber(Jim_Interp *interp, int linenr)
+{
+ interp->errorLine = linenr;
+}
+
+static void JimResetStackTrace(Jim_Interp *interp)
+{
+ Jim_DecrRefCount(interp, interp->stackTrace);
+ interp->stackTrace = Jim_NewListObj(interp, NULL, 0);
+ Jim_IncrRefCount(interp->stackTrace);
+}
+
+static void JimSetStackTrace(Jim_Interp *interp, Jim_Obj *stackTraceObj)
+{
+ int len;
+
+ /* Increment reference first in case these are the same object */
+ Jim_IncrRefCount(stackTraceObj);
+ Jim_DecrRefCount(interp, interp->stackTrace);
+ interp->stackTrace = stackTraceObj;
+ interp->errorFlag = 1;
+
+ /* This is a bit ugly.
+ * If the filename of the last entry of the stack trace is empty,
+ * the next stack level should be added.
+ */
+ len = Jim_ListLength(interp, interp->stackTrace);
+ if (len >= 3) {
+ Jim_Obj *filenameObj;
+
+ Jim_ListIndex(interp, interp->stackTrace, len - 2, &filenameObj, JIM_NONE);
+
+ Jim_GetString(filenameObj, &len);
+
+ if (len == 0) {
+ interp->addStackTrace = 1;
+ }
+ }
+}
+
+/* Returns 1 if the stack trace information was used or 0 if not */
+static void JimAppendStackTrace(Jim_Interp *interp, const char *procname,
+ const char *filename, int linenr)
+{
+ if (strcmp(procname, "unknown") == 0) {
+ procname = "";
+ }
+ if (!*procname && !*filename) {
+ /* No useful info here */
+ return;
+ }
+
+ if (Jim_IsShared(interp->stackTrace)) {
+ Jim_DecrRefCount(interp, interp->stackTrace);
+ interp->stackTrace = Jim_DuplicateObj(interp, interp->stackTrace);
+ Jim_IncrRefCount(interp->stackTrace);
+ }
+
+ /* If we have no procname but the previous element did, merge with that frame */
+ if (!*procname && *filename) {
+ /* Just a filename. Check the previous entry */
+ int len = Jim_ListLength(interp, interp->stackTrace);
+
+ if (len >= 3) {
+ Jim_Obj *procnameObj;
+ Jim_Obj *filenameObj;
+
+ if (Jim_ListIndex(interp, interp->stackTrace, len - 3, &procnameObj, JIM_NONE) == JIM_OK
+ && Jim_ListIndex(interp, interp->stackTrace, len - 2, &filenameObj,
+ JIM_NONE) == JIM_OK) {
+
+ const char *prev_procname = Jim_String(procnameObj);
+ const char *prev_filename = Jim_String(filenameObj);
+
+ if (*prev_procname && !*prev_filename) {
+ ListSetIndex(interp, interp->stackTrace, len - 2, Jim_NewStringObj(interp,
+ filename, -1), 0);
+ ListSetIndex(interp, interp->stackTrace, len - 1, Jim_NewIntObj(interp, linenr),
+ 0);
+ return;
+ }
+ }
+ }
+ }
+
+ Jim_ListAppendElement(interp, interp->stackTrace, Jim_NewStringObj(interp, procname, -1));
+ Jim_ListAppendElement(interp, interp->stackTrace, Jim_NewStringObj(interp, filename, -1));
+ Jim_ListAppendElement(interp, interp->stackTrace, Jim_NewIntObj(interp, linenr));
+}
+
+int Jim_SetAssocData(Jim_Interp *interp, const char *key, Jim_InterpDeleteProc * delProc,
+ void *data)
+{
+ AssocDataValue *assocEntryPtr = (AssocDataValue *) Jim_Alloc(sizeof(AssocDataValue));
+
+ assocEntryPtr->delProc = delProc;
+ assocEntryPtr->data = data;
+ return Jim_AddHashEntry(&interp->assocData, key, assocEntryPtr);
+}
+
+void *Jim_GetAssocData(Jim_Interp *interp, const char *key)
+{
+ Jim_HashEntry *entryPtr = Jim_FindHashEntry(&interp->assocData, key);
+
+ if (entryPtr != NULL) {
+ AssocDataValue *assocEntryPtr = (AssocDataValue *) entryPtr->val;
+
+ return assocEntryPtr->data;
+ }
+ return NULL;
+}
+
+int Jim_DeleteAssocData(Jim_Interp *interp, const char *key)
+{
+ return Jim_DeleteHashEntry(&interp->assocData, key);
+}
+
+int Jim_GetExitCode(Jim_Interp *interp)
+{
+ return interp->exitCode;
+}
+
+/* -----------------------------------------------------------------------------
+ * Shared strings.
+ * Every interpreter has an hash table where to put shared dynamically
+ * allocate strings that are likely to be used a lot of times.
+ * For example, in the 'source' object type, there is a pointer to
+ * the filename associated with that object. Every script has a lot
+ * of this objects with the identical file name, so it is wise to share
+ * this info.
+ *
+ * The API is trivial: Jim_GetSharedString(interp, "foobar")
+ * returns the pointer to the shared string. Every time a reference
+ * to the string is no longer used, the user should call
+ * Jim_ReleaseSharedString(interp, stringPointer). Once no one is using
+ * a given string, it is removed from the hash table.
+ * ---------------------------------------------------------------------------*/
+const char *Jim_GetSharedString(Jim_Interp *interp, const char *str)
+{
+ Jim_HashEntry *he = Jim_FindHashEntry(&interp->sharedStrings, str);
+
+ if (he == NULL) {
+ char *strCopy = Jim_StrDup(str);
+
+ Jim_AddHashEntry(&interp->sharedStrings, strCopy, (void *)1);
+ return strCopy;
+ }
+ else {
+ long refCount = (long)he->val;
+
+ refCount++;
+ he->val = (void *)refCount;
+ return he->key;
+ }
+}
+
+void Jim_ReleaseSharedString(Jim_Interp *interp, const char *str)
+{
+ long refCount;
+ Jim_HashEntry *he = Jim_FindHashEntry(&interp->sharedStrings, str);
+
+ JimPanic((he == NULL, interp, "Jim_ReleaseSharedString called with " "unknown shared string '%s'", str));
+
+ refCount = (long)he->val;
+ refCount--;
+ if (refCount == 0) {
+ Jim_DeleteHashEntry(&interp->sharedStrings, str);
+ }
+ else {
+ he->val = (void *)refCount;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ * Integer object
+ * ---------------------------------------------------------------------------*/
+#define JIM_INTEGER_SPACE 24
+
+static void UpdateStringOfInt(struct Jim_Obj *objPtr);
+static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags);
+
+static const Jim_ObjType intObjType = {
+ "int",
+ NULL,
+ NULL,
+ UpdateStringOfInt,
+ JIM_TYPE_NONE,
+};
+
+/* A coerced double is closer to an int than a double.
+ * It is an int value temporarily masquerading as a double value.
+ * i.e. it has the same string value as an int and Jim_GetWide()
+ * succeeds, but also Jim_GetDouble() returns the value directly.
+ */
+static const Jim_ObjType coercedDoubleObjType = {
+ "coerced-double",
+ NULL,
+ NULL,
+ UpdateStringOfInt,
+ JIM_TYPE_NONE,
+};
+
+
+void UpdateStringOfInt(struct Jim_Obj *objPtr)
+{
+ int len;
+ char buf[JIM_INTEGER_SPACE + 1];
+
+ len = Jim_WideToString(buf, JimWideValue(objPtr));
+ objPtr->bytes = Jim_Alloc(len + 1);
+ memcpy(objPtr->bytes, buf, len + 1);
+ objPtr->length = len;
+}
+
+int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags)
+{
+ jim_wide wideValue;
+ const char *str;
+
+ if (objPtr->typePtr == &coercedDoubleObjType) {
+ /* Simple switcheroo */
+ objPtr->typePtr = &intObjType;
+ return JIM_OK;
+ }
+
+ /* Get the string representation */
+ str = Jim_String(objPtr);
+ /* Try to convert into a jim_wide */
+ if (Jim_StringToWide(str, &wideValue, 0) != JIM_OK) {
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultFormatted(interp, "expected integer but got \"%#s\"", objPtr);
+ }
+ return JIM_ERR;
+ }
+ if ((wideValue == JIM_WIDE_MIN || wideValue == JIM_WIDE_MAX) && errno == ERANGE) {
+ Jim_SetResultString(interp, "Integer value too big to be represented", -1);
+ return JIM_ERR;
+ }
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &intObjType;
+ objPtr->internalRep.wideValue = wideValue;
+ return JIM_OK;
+}
+
+#ifdef JIM_OPTIMIZATION
+static int JimIsWide(Jim_Obj *objPtr)
+{
+ return objPtr->typePtr == &intObjType;
+}
+#endif
+
+int Jim_GetWide(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr)
+{
+ if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_ERRMSG) == JIM_ERR)
+ return JIM_ERR;
+ *widePtr = JimWideValue(objPtr);
+ return JIM_OK;
+}
+
+/* Get a wide but does not set an error if the format is bad. */
+static int JimGetWideNoErr(Jim_Interp *interp, Jim_Obj *objPtr, jim_wide * widePtr)
+{
+ if (objPtr->typePtr != &intObjType && SetIntFromAny(interp, objPtr, JIM_NONE) == JIM_ERR)
+ return JIM_ERR;
+ *widePtr = JimWideValue(objPtr);
+ return JIM_OK;
+}
+
+int Jim_GetLong(Jim_Interp *interp, Jim_Obj *objPtr, long *longPtr)
+{
+ jim_wide wideValue;
+ int retval;
+
+ retval = Jim_GetWide(interp, objPtr, &wideValue);
+ if (retval == JIM_OK) {
+ *longPtr = (long)wideValue;
+ return JIM_OK;
+ }
+ return JIM_ERR;
+}
+
+Jim_Obj *Jim_NewIntObj(Jim_Interp *interp, jim_wide wideValue)
+{
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_NewObj(interp);
+ objPtr->typePtr = &intObjType;
+ objPtr->bytes = NULL;
+ objPtr->internalRep.wideValue = wideValue;
+ return objPtr;
+}
+
+/* -----------------------------------------------------------------------------
+ * Double object
+ * ---------------------------------------------------------------------------*/
+#define JIM_DOUBLE_SPACE 30
+
+static void UpdateStringOfDouble(struct Jim_Obj *objPtr);
+static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr);
+
+static const Jim_ObjType doubleObjType = {
+ "double",
+ NULL,
+ NULL,
+ UpdateStringOfDouble,
+ JIM_TYPE_NONE,
+};
+
+void UpdateStringOfDouble(struct Jim_Obj *objPtr)
+{
+ int len;
+ char buf[JIM_DOUBLE_SPACE + 1];
+
+ len = Jim_DoubleToString(buf, objPtr->internalRep.doubleValue);
+ objPtr->bytes = Jim_Alloc(len + 1);
+ memcpy(objPtr->bytes, buf, len + 1);
+ objPtr->length = len;
+}
+
+int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ double doubleValue;
+ jim_wide wideValue;
+ const char *str;
+
+ /* Preserve the string representation.
+ * Needed so we can convert back to int without loss
+ */
+ str = Jim_String(objPtr);
+
+#ifdef HAVE_LONG_LONG
+ /* Assume a 53 bit mantissa */
+#define MIN_INT_IN_DOUBLE -(1LL << 53)
+#define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1)
+
+ if (objPtr->typePtr == &intObjType
+ && JimWideValue(objPtr) >= MIN_INT_IN_DOUBLE
+ && JimWideValue(objPtr) <= MAX_INT_IN_DOUBLE) {
+
+ /* Direct conversion to coerced double */
+ objPtr->typePtr = &coercedDoubleObjType;
+ return JIM_OK;
+ }
+ else
+#endif
+ if (Jim_StringToWide(str, &wideValue, 10) == JIM_OK) {
+ /* Managed to convert to an int, so we can use this as a cooerced double */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &coercedDoubleObjType;
+ objPtr->internalRep.wideValue = wideValue;
+ return JIM_OK;
+ }
+ else {
+ /* Try to convert into a double */
+ if (Jim_StringToDouble(str, &doubleValue) != JIM_OK) {
+ Jim_SetResultFormatted(interp, "expected number but got \"%#s\"", objPtr);
+ return JIM_ERR;
+ }
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ }
+ objPtr->typePtr = &doubleObjType;
+ objPtr->internalRep.doubleValue = doubleValue;
+ return JIM_OK;
+}
+
+int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, double *doublePtr)
+{
+ if (objPtr->typePtr == &coercedDoubleObjType) {
+ *doublePtr = JimWideValue(objPtr);
+ return JIM_OK;
+ }
+ if (objPtr->typePtr != &doubleObjType && SetDoubleFromAny(interp, objPtr) == JIM_ERR)
+ return JIM_ERR;
+
+ if (objPtr->typePtr == &coercedDoubleObjType) {
+ *doublePtr = JimWideValue(objPtr);
+ }
+ else {
+ *doublePtr = objPtr->internalRep.doubleValue;
+ }
+ return JIM_OK;
+}
+
+Jim_Obj *Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue)
+{
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_NewObj(interp);
+ objPtr->typePtr = &doubleObjType;
+ objPtr->bytes = NULL;
+ objPtr->internalRep.doubleValue = doubleValue;
+ return objPtr;
+}
+
+/* -----------------------------------------------------------------------------
+ * List object
+ * ---------------------------------------------------------------------------*/
+static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr);
+static void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static void UpdateStringOfList(struct Jim_Obj *objPtr);
+static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+/* Note that while the elements of the list may contain references,
+ * the list object itself can't. This basically means that the
+ * list object string representation as a whole can't contain references
+ * that are not presents in the single elements. */
+static const Jim_ObjType listObjType = {
+ "list",
+ FreeListInternalRep,
+ DupListInternalRep,
+ UpdateStringOfList,
+ JIM_TYPE_NONE,
+};
+
+void FreeListInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ int i;
+
+ for (i = 0; i < objPtr->internalRep.listValue.len; i++) {
+ Jim_DecrRefCount(interp, objPtr->internalRep.listValue.ele[i]);
+ }
+ Jim_Free(objPtr->internalRep.listValue.ele);
+}
+
+void DupListInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ int i;
+
+ JIM_NOTUSED(interp);
+
+ dupPtr->internalRep.listValue.len = srcPtr->internalRep.listValue.len;
+ dupPtr->internalRep.listValue.maxLen = srcPtr->internalRep.listValue.maxLen;
+ dupPtr->internalRep.listValue.ele =
+ Jim_Alloc(sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.maxLen);
+ memcpy(dupPtr->internalRep.listValue.ele, srcPtr->internalRep.listValue.ele,
+ sizeof(Jim_Obj *) * srcPtr->internalRep.listValue.len);
+ for (i = 0; i < dupPtr->internalRep.listValue.len; i++) {
+ Jim_IncrRefCount(dupPtr->internalRep.listValue.ele[i]);
+ }
+ dupPtr->typePtr = &listObjType;
+}
+
+/* The following function checks if a given string can be encoded
+ * into a list element without any kind of quoting, surrounded by braces,
+ * or using escapes to quote. */
+#define JIM_ELESTR_SIMPLE 0
+#define JIM_ELESTR_BRACE 1
+#define JIM_ELESTR_QUOTE 2
+static int ListElementQuotingType(const char *s, int len)
+{
+ int i, level, trySimple = 1;
+
+ /* Try with the SIMPLE case */
+ if (len == 0)
+ return JIM_ELESTR_BRACE;
+ if (s[0] == '#')
+ return JIM_ELESTR_BRACE;
+ if (s[0] == '"' || s[0] == '{') {
+ trySimple = 0;
+ goto testbrace;
+ }
+ for (i = 0; i < len; i++) {
+ switch (s[i]) {
+ case ' ':
+ case '$':
+ case '"':
+ case '[':
+ case ']':
+ case ';':
+ case '\\':
+ case '\r':
+ case '\n':
+ case '\t':
+ case '\f':
+ case '\v':
+ trySimple = 0;
+ case '{':
+ case '}':
+ goto testbrace;
+ }
+ }
+ return JIM_ELESTR_SIMPLE;
+
+ testbrace:
+ /* Test if it's possible to do with braces */
+ if (s[len - 1] == '\\' || s[len - 1] == ']')
+ return JIM_ELESTR_QUOTE;
+ level = 0;
+ for (i = 0; i < len; i++) {
+ switch (s[i]) {
+ case '{':
+ level++;
+ break;
+ case '}':
+ level--;
+ if (level < 0)
+ return JIM_ELESTR_QUOTE;
+ break;
+ case '\\':
+ if (s[i + 1] == '\n')
+ return JIM_ELESTR_QUOTE;
+ else if (s[i + 1] != '\0')
+ i++;
+ break;
+ }
+ }
+ if (level == 0) {
+ if (!trySimple)
+ return JIM_ELESTR_BRACE;
+ for (i = 0; i < len; i++) {
+ switch (s[i]) {
+ case ' ':
+ case '$':
+ case '"':
+ case '[':
+ case ']':
+ case ';':
+ case '\\':
+ case '\r':
+ case '\n':
+ case '\t':
+ case '\f':
+ case '\v':
+ return JIM_ELESTR_BRACE;
+ break;
+ }
+ }
+ return JIM_ELESTR_SIMPLE;
+ }
+ return JIM_ELESTR_QUOTE;
+}
+
+/* Returns the malloc-ed representation of a string
+ * using backslash to quote special chars. */
+static char *BackslashQuoteString(const char *s, int len, int *qlenPtr)
+{
+ char *q = Jim_Alloc(len * 2 + 1), *p;
+
+ p = q;
+ while (*s) {
+ switch (*s) {
+ case ' ':
+ case '$':
+ case '"':
+ case '[':
+ case ']':
+ case '{':
+ case '}':
+ case ';':
+ case '\\':
+ *p++ = '\\';
+ *p++ = *s++;
+ break;
+ case '\n':
+ *p++ = '\\';
+ *p++ = 'n';
+ s++;
+ break;
+ case '\r':
+ *p++ = '\\';
+ *p++ = 'r';
+ s++;
+ break;
+ case '\t':
+ *p++ = '\\';
+ *p++ = 't';
+ s++;
+ break;
+ case '\f':
+ *p++ = '\\';
+ *p++ = 'f';
+ s++;
+ break;
+ case '\v':
+ *p++ = '\\';
+ *p++ = 'v';
+ s++;
+ break;
+ default:
+ *p++ = *s++;
+ break;
+ }
+ }
+ *p = '\0';
+ *qlenPtr = p - q;
+ return q;
+}
+
+void UpdateStringOfList(struct Jim_Obj *objPtr)
+{
+ int i, bufLen, realLength;
+ const char *strRep;
+ char *p;
+ int *quotingType;
+ Jim_Obj **ele = objPtr->internalRep.listValue.ele;
+
+ /* (Over) Estimate the space needed. */
+ quotingType = Jim_Alloc(sizeof(int) * objPtr->internalRep.listValue.len + 1);
+ bufLen = 0;
+ for (i = 0; i < objPtr->internalRep.listValue.len; i++) {
+ int len;
+
+ strRep = Jim_GetString(ele[i], &len);
+ quotingType[i] = ListElementQuotingType(strRep, len);
+ switch (quotingType[i]) {
+ case JIM_ELESTR_SIMPLE:
+ bufLen += len;
+ break;
+ case JIM_ELESTR_BRACE:
+ bufLen += len + 2;
+ break;
+ case JIM_ELESTR_QUOTE:
+ bufLen += len * 2;
+ break;
+ }
+ bufLen++; /* elements separator. */
+ }
+ bufLen++;
+
+ /* Generate the string rep. */
+ p = objPtr->bytes = Jim_Alloc(bufLen + 1);
+ realLength = 0;
+ for (i = 0; i < objPtr->internalRep.listValue.len; i++) {
+ int len, qlen;
+ char *q;
+
+ strRep = Jim_GetString(ele[i], &len);
+
+ switch (quotingType[i]) {
+ case JIM_ELESTR_SIMPLE:
+ memcpy(p, strRep, len);
+ p += len;
+ realLength += len;
+ break;
+ case JIM_ELESTR_BRACE:
+ *p++ = '{';
+ memcpy(p, strRep, len);
+ p += len;
+ *p++ = '}';
+ realLength += len + 2;
+ break;
+ case JIM_ELESTR_QUOTE:
+ q = BackslashQuoteString(strRep, len, &qlen);
+ memcpy(p, q, qlen);
+ Jim_Free(q);
+ p += qlen;
+ realLength += qlen;
+ break;
+ }
+ /* Add a separating space */
+ if (i + 1 != objPtr->internalRep.listValue.len) {
+ *p++ = ' ';
+ realLength++;
+ }
+ }
+ *p = '\0'; /* nul term. */
+ objPtr->length = realLength;
+ Jim_Free(quotingType);
+}
+
+int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr)
+{
+ struct JimParserCtx parser;
+ const char *str;
+ int strLen;
+ const char *filename = NULL;
+ int linenr = 1;
+
+ /* Try to preserve information about filename / line number */
+ if (objPtr->typePtr == &sourceObjType) {
+ filename = Jim_GetSharedString(interp, objPtr->internalRep.sourceValue.fileName);
+ linenr = objPtr->internalRep.sourceValue.lineNumber;
+ }
+
+ /* Get the string representation */
+ str = Jim_GetString(objPtr, &strLen);
+
+ /* Free the old internal repr just now and initialize the
+ * new one just now. The string->list conversion can't fail. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &listObjType;
+ objPtr->internalRep.listValue.len = 0;
+ objPtr->internalRep.listValue.maxLen = 0;
+ objPtr->internalRep.listValue.ele = NULL;
+
+ /* Convert into a list */
+ JimParserInit(&parser, str, strLen, linenr);
+ while (!JimParserEof(&parser)) {
+ Jim_Obj *elementPtr;
+
+ JimParseList(&parser);
+ if (JimParserTtype(&parser) != JIM_TT_STR && JimParserTtype(&parser) != JIM_TT_ESC)
+ continue;
+ elementPtr = JimParserGetTokenObj(interp, &parser);
+ JimSetSourceInfo(interp, elementPtr, filename, JimParserTline(&parser));
+ ListAppendElement(objPtr, elementPtr);
+ }
+ if (filename) {
+ Jim_ReleaseSharedString(interp, filename);
+ }
+ return JIM_OK;
+}
+
+Jim_Obj *Jim_NewListObj(Jim_Interp *interp, Jim_Obj *const *elements, int len)
+{
+ Jim_Obj *objPtr;
+ int i;
+
+ objPtr = Jim_NewObj(interp);
+ objPtr->typePtr = &listObjType;
+ objPtr->bytes = NULL;
+ objPtr->internalRep.listValue.ele = NULL;
+ objPtr->internalRep.listValue.len = 0;
+ objPtr->internalRep.listValue.maxLen = 0;
+ for (i = 0; i < len; i++) {
+ ListAppendElement(objPtr, elements[i]);
+ }
+ return objPtr;
+}
+
+/* Return a vector of Jim_Obj with the elements of a Jim list, and the
+ * length of the vector. Note that the user of this function should make
+ * sure that the list object can't shimmer while the vector returned
+ * is in use, this vector is the one stored inside the internal representation
+ * of the list object. This function is not exported, extensions should
+ * always access to the List object elements using Jim_ListIndex(). */
+static void JimListGetElements(Jim_Interp *interp, Jim_Obj *listObj, int *listLen,
+ Jim_Obj ***listVec)
+{
+ *listLen = Jim_ListLength(interp, listObj);
+ *listVec = listObj->internalRep.listValue.ele;
+}
+
+/* Sorting uses ints, but commands may return wide */
+static int JimSign(jim_wide w)
+{
+ if (w == 0) {
+ return 0;
+ }
+ else if (w < 0) {
+ return -1;
+ }
+ return 1;
+}
+
+/* ListSortElements type values */
+struct lsort_info {
+ jmp_buf jmpbuf;
+ Jim_Obj *command;
+ Jim_Interp *interp;
+ enum {
+ JIM_LSORT_ASCII,
+ JIM_LSORT_NOCASE,
+ JIM_LSORT_INTEGER,
+ JIM_LSORT_COMMAND
+ } type;
+ int order;
+ int index;
+ int indexed;
+ int (*subfn)(Jim_Obj **, Jim_Obj **);
+};
+
+static struct lsort_info *sort_info;
+
+static int ListSortIndexHelper(Jim_Obj **lhsObj, Jim_Obj **rhsObj)
+{
+ Jim_Obj *lObj, *rObj;
+
+ if (Jim_ListIndex(sort_info->interp, *lhsObj, sort_info->index, &lObj, JIM_ERRMSG) != JIM_OK ||
+ Jim_ListIndex(sort_info->interp, *rhsObj, sort_info->index, &rObj, JIM_ERRMSG) != JIM_OK) {
+ longjmp(sort_info->jmpbuf, JIM_ERR);
+ }
+ return sort_info->subfn(&lObj, &rObj);
+}
+
+/* Sort the internal rep of a list. */
+static int ListSortString(Jim_Obj **lhsObj, Jim_Obj **rhsObj)
+{
+ return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order;
+}
+
+static int ListSortStringNoCase(Jim_Obj **lhsObj, Jim_Obj **rhsObj)
+{
+ return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 1) * sort_info->order;
+}
+
+static int ListSortInteger(Jim_Obj **lhsObj, Jim_Obj **rhsObj)
+{
+ jim_wide lhs = 0, rhs = 0;
+
+ if (Jim_GetWide(sort_info->interp, *lhsObj, &lhs) != JIM_OK ||
+ Jim_GetWide(sort_info->interp, *rhsObj, &rhs) != JIM_OK) {
+ longjmp(sort_info->jmpbuf, JIM_ERR);
+ }
+
+ return JimSign(lhs - rhs) * sort_info->order;
+}
+
+static int ListSortCommand(Jim_Obj **lhsObj, Jim_Obj **rhsObj)
+{
+ Jim_Obj *compare_script;
+ int rc;
+
+ jim_wide ret = 0;
+
+ /* This must be a valid list */
+ compare_script = Jim_DuplicateObj(sort_info->interp, sort_info->command);
+ Jim_ListAppendElement(sort_info->interp, compare_script, *lhsObj);
+ Jim_ListAppendElement(sort_info->interp, compare_script, *rhsObj);
+
+ rc = Jim_EvalObj(sort_info->interp, compare_script);
+
+ if (rc != JIM_OK || Jim_GetWide(sort_info->interp, Jim_GetResult(sort_info->interp), &ret) != JIM_OK) {
+ longjmp(sort_info->jmpbuf, rc);
+ }
+
+ return JimSign(ret) * sort_info->order;
+}
+
+/* Sort a list *in place*. MUST be called with non-shared objects. */
+static int ListSortElements(Jim_Interp *interp, Jim_Obj *listObjPtr, struct lsort_info *info)
+{
+ struct lsort_info *prev_info;
+
+ typedef int (qsort_comparator) (const void *, const void *);
+ int (*fn) (Jim_Obj **, Jim_Obj **);
+ Jim_Obj **vector;
+ int len;
+ int rc;
+
+ JimPanic((Jim_IsShared(listObjPtr), interp, "Jim_ListSortElements called with shared object"));
+ if (!Jim_IsList(listObjPtr))
+ SetListFromAny(interp, listObjPtr);
+
+ /* Allow lsort to be called reentrantly */
+ prev_info = sort_info;
+ sort_info = info;
+
+ vector = listObjPtr->internalRep.listValue.ele;
+ len = listObjPtr->internalRep.listValue.len;
+ switch (info->type) {
+ case JIM_LSORT_ASCII:
+ fn = ListSortString;
+ break;
+ case JIM_LSORT_NOCASE:
+ fn = ListSortStringNoCase;
+ break;
+ case JIM_LSORT_INTEGER:
+ fn = ListSortInteger;
+ break;
+ case JIM_LSORT_COMMAND:
+ fn = ListSortCommand;
+ break;
+ default:
+ fn = NULL; /* avoid warning */
+ JimPanic((1, interp, "ListSort called with invalid sort type"));
+ }
+
+ if (info->indexed) {
+ /* Need to interpose a "list index" function */
+ info->subfn = fn;
+ fn = ListSortIndexHelper;
+ }
+
+ if ((rc = setjmp(info->jmpbuf)) == 0) {
+ qsort(vector, len, sizeof(Jim_Obj *), (qsort_comparator *) fn);
+ }
+ Jim_InvalidateStringRep(listObjPtr);
+ sort_info = prev_info;
+
+ return rc;
+}
+
+/* This is the low-level function to insert elements into a list.
+ * The higher-level Jim_ListInsertElements() performs shared object
+ * check and invalidate the string repr. This version is used
+ * in the internals of the List Object and is not exported.
+ *
+ * NOTE: this function can be called only against objects
+ * with internal type of List. */
+static void ListInsertElements(Jim_Obj *listPtr, int idx, int elemc, Jim_Obj *const *elemVec)
+{
+ int currentLen = listPtr->internalRep.listValue.len;
+ int requiredLen = currentLen + elemc;
+ int i;
+ Jim_Obj **point;
+
+ if (requiredLen > listPtr->internalRep.listValue.maxLen) {
+ int maxLen = requiredLen * 2;
+
+ listPtr->internalRep.listValue.ele =
+ Jim_Realloc(listPtr->internalRep.listValue.ele, sizeof(Jim_Obj *) * maxLen);
+ listPtr->internalRep.listValue.maxLen = maxLen;
+ }
+ point = listPtr->internalRep.listValue.ele + idx;
+ memmove(point + elemc, point, (currentLen - idx) * sizeof(Jim_Obj *));
+ for (i = 0; i < elemc; ++i) {
+ point[i] = elemVec[i];
+ Jim_IncrRefCount(point[i]);
+ }
+ listPtr->internalRep.listValue.len += elemc;
+}
+
+/* Convenience call to ListInsertElements() to append a single element.
+ */
+static void ListAppendElement(Jim_Obj *listPtr, Jim_Obj *objPtr)
+{
+ ListInsertElements(listPtr, listPtr->internalRep.listValue.len, 1, &objPtr);
+}
+
+
+/* Appends every element of appendListPtr into listPtr.
+ * Both have to be of the list type.
+ * Convenience call to ListInsertElements()
+ */
+static void ListAppendList(Jim_Obj *listPtr, Jim_Obj *appendListPtr)
+{
+ ListInsertElements(listPtr, listPtr->internalRep.listValue.len,
+ appendListPtr->internalRep.listValue.len, appendListPtr->internalRep.listValue.ele);
+}
+
+void Jim_ListAppendElement(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *objPtr)
+{
+ JimPanic((Jim_IsShared(listPtr), interp, "Jim_ListAppendElement called with shared object"));
+ if (!Jim_IsList(listPtr))
+ SetListFromAny(interp, listPtr);
+ Jim_InvalidateStringRep(listPtr);
+ ListAppendElement(listPtr, objPtr);
+}
+
+void Jim_ListAppendList(Jim_Interp *interp, Jim_Obj *listPtr, Jim_Obj *appendListPtr)
+{
+ JimPanic((Jim_IsShared(listPtr), interp, "Jim_ListAppendList called with shared object"));
+ if (!Jim_IsList(listPtr))
+ SetListFromAny(interp, listPtr);
+ Jim_InvalidateStringRep(listPtr);
+ ListAppendList(listPtr, appendListPtr);
+}
+
+int Jim_ListLength(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ if (!Jim_IsList(objPtr))
+ SetListFromAny(interp, objPtr);
+ return objPtr->internalRep.listValue.len;
+}
+
+void Jim_ListInsertElements(Jim_Interp *interp, Jim_Obj *listPtr, int idx,
+ int objc, Jim_Obj *const *objVec)
+{
+ JimPanic((Jim_IsShared(listPtr), interp, "Jim_ListInsertElement called with shared object"));
+ if (!Jim_IsList(listPtr))
+ SetListFromAny(interp, listPtr);
+ if (idx >= 0 && idx > listPtr->internalRep.listValue.len)
+ idx = listPtr->internalRep.listValue.len;
+ else if (idx < 0)
+ idx = 0;
+ Jim_InvalidateStringRep(listPtr);
+ ListInsertElements(listPtr, idx, objc, objVec);
+}
+
+int Jim_ListIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, Jim_Obj **objPtrPtr, int flags)
+{
+ if (!Jim_IsList(listPtr))
+ SetListFromAny(interp, listPtr);
+ if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) ||
+ (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) {
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultString(interp, "list index out of range", -1);
+ }
+ *objPtrPtr = NULL;
+ return JIM_ERR;
+ }
+ if (idx < 0)
+ idx = listPtr->internalRep.listValue.len + idx;
+ *objPtrPtr = listPtr->internalRep.listValue.ele[idx];
+ return JIM_OK;
+}
+
+static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx,
+ Jim_Obj *newObjPtr, int flags)
+{
+ if (!Jim_IsList(listPtr))
+ SetListFromAny(interp, listPtr);
+ if ((idx >= 0 && idx >= listPtr->internalRep.listValue.len) ||
+ (idx < 0 && (-idx - 1) >= listPtr->internalRep.listValue.len)) {
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultString(interp, "list index out of range", -1);
+ }
+ return JIM_ERR;
+ }
+ if (idx < 0)
+ idx = listPtr->internalRep.listValue.len + idx;
+ Jim_DecrRefCount(interp, listPtr->internalRep.listValue.ele[idx]);
+ listPtr->internalRep.listValue.ele[idx] = newObjPtr;
+ Jim_IncrRefCount(newObjPtr);
+ return JIM_OK;
+}
+
+/* Modify the list stored into the variable named 'varNamePtr'
+ * setting the element specified by the 'indexc' indexes objects in 'indexv',
+ * with the new element 'newObjptr'. */
+int Jim_SetListIndex(Jim_Interp *interp, Jim_Obj *varNamePtr,
+ Jim_Obj *const *indexv, int indexc, Jim_Obj *newObjPtr)
+{
+ Jim_Obj *varObjPtr, *objPtr, *listObjPtr;
+ int shared, i, idx;
+
+ varObjPtr = objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG | JIM_UNSHARED);
+ if (objPtr == NULL)
+ return JIM_ERR;
+ if ((shared = Jim_IsShared(objPtr)))
+ varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr);
+ for (i = 0; i < indexc - 1; i++) {
+ listObjPtr = objPtr;
+ if (Jim_GetIndex(interp, indexv[i], &idx) != JIM_OK)
+ goto err;
+ if (Jim_ListIndex(interp, listObjPtr, idx, &objPtr, JIM_ERRMSG) != JIM_OK) {
+ goto err;
+ }
+ if (Jim_IsShared(objPtr)) {
+ objPtr = Jim_DuplicateObj(interp, objPtr);
+ ListSetIndex(interp, listObjPtr, idx, objPtr, JIM_NONE);
+ }
+ Jim_InvalidateStringRep(listObjPtr);
+ }
+ if (Jim_GetIndex(interp, indexv[indexc - 1], &idx) != JIM_OK)
+ goto err;
+ if (ListSetIndex(interp, objPtr, idx, newObjPtr, JIM_ERRMSG) == JIM_ERR)
+ goto err;
+ Jim_InvalidateStringRep(objPtr);
+ Jim_InvalidateStringRep(varObjPtr);
+ if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK)
+ goto err;
+ Jim_SetResult(interp, varObjPtr);
+ return JIM_OK;
+ err:
+ if (shared) {
+ Jim_FreeNewObj(interp, varObjPtr);
+ }
+ return JIM_ERR;
+}
+
+Jim_Obj *Jim_ConcatObj(Jim_Interp *interp, int objc, Jim_Obj *const *objv)
+{
+ int i;
+
+ /* If all the objects in objv are lists,
+ * it's possible to return a list as result, that's the
+ * concatenation of all the lists. */
+ for (i = 0; i < objc; i++) {
+ if (!Jim_IsList(objv[i]))
+ break;
+ }
+ if (i == objc) {
+ Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
+
+ for (i = 0; i < objc; i++)
+ Jim_ListAppendList(interp, objPtr, objv[i]);
+ return objPtr;
+ }
+ else {
+ /* Else... we have to glue strings together */
+ int len = 0, objLen;
+ char *bytes, *p;
+
+ /* Compute the length */
+ for (i = 0; i < objc; i++) {
+ Jim_GetString(objv[i], &objLen);
+ len += objLen;
+ }
+ if (objc)
+ len += objc - 1;
+ /* Create the string rep, and a string object holding it. */
+ p = bytes = Jim_Alloc(len + 1);
+ for (i = 0; i < objc; i++) {
+ const char *s = Jim_GetString(objv[i], &objLen);
+
+ /* Remove leading space */
+ while (objLen && (*s == ' ' || *s == '\t' || *s == '\n')) {
+ s++;
+ objLen--;
+ len--;
+ }
+ /* And trailing space */
+ while (objLen && (s[objLen - 1] == ' ' ||
+ s[objLen - 1] == '\n' || s[objLen - 1] == '\t')) {
+ /* Handle trailing backslash-space case */
+ if (objLen > 1 && s[objLen - 2] == '\\') {
+ break;
+ }
+ objLen--;
+ len--;
+ }
+ memcpy(p, s, objLen);
+ p += objLen;
+ if (objLen && i + 1 != objc) {
+ *p++ = ' ';
+ }
+ else if (i + 1 != objc) {
+ /* Drop the space calcuated for this
+ * element that is instead null. */
+ len--;
+ }
+ }
+ *p = '\0';
+ return Jim_NewStringObjNoAlloc(interp, bytes, len);
+ }
+}
+
+/* Returns a list composed of the elements in the specified range.
+ * first and start are directly accepted as Jim_Objects and
+ * processed for the end?-index? case. */
+Jim_Obj *Jim_ListRange(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *firstObjPtr,
+ Jim_Obj *lastObjPtr)
+{
+ int first, last;
+ int len, rangeLen;
+
+ if (Jim_GetIndex(interp, firstObjPtr, &first) != JIM_OK ||
+ Jim_GetIndex(interp, lastObjPtr, &last) != JIM_OK)
+ return NULL;
+ len = Jim_ListLength(interp, listObjPtr); /* will convert into list */
+ first = JimRelToAbsIndex(len, first);
+ last = JimRelToAbsIndex(len, last);
+ JimRelToAbsRange(len, first, last, &first, &last, &rangeLen);
+ return Jim_NewListObj(interp, listObjPtr->internalRep.listValue.ele + first, rangeLen);
+}
+
+/* -----------------------------------------------------------------------------
+ * Dict object
+ * ---------------------------------------------------------------------------*/
+static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static void UpdateStringOfDict(struct Jim_Obj *objPtr);
+static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+/* Dict HashTable Type.
+ *
+ * Keys and Values are Jim objects. */
+
+static unsigned int JimObjectHTHashFunction(const void *key)
+{
+ const char *str;
+ Jim_Obj *objPtr = (Jim_Obj *)key;
+ int len, h;
+
+ str = Jim_GetString(objPtr, &len);
+ h = Jim_GenHashFunction((unsigned char *)str, len);
+ return h;
+}
+
+static int JimObjectHTKeyCompare(void *privdata, const void *key1, const void *key2)
+{
+ JIM_NOTUSED(privdata);
+
+ return Jim_StringEqObj((Jim_Obj *)key1, (Jim_Obj *)key2);
+}
+
+static void JimObjectHTKeyValDestructor(void *interp, void *val)
+{
+ Jim_Obj *objPtr = val;
+
+ Jim_DecrRefCount(interp, objPtr);
+}
+
+static const Jim_HashTableType JimDictHashTableType = {
+ JimObjectHTHashFunction, /* hash function */
+ NULL, /* key dup */
+ NULL, /* val dup */
+ JimObjectHTKeyCompare, /* key compare */
+ (void (*)(void *, const void *)) /* ATTENTION: const cast */
+ JimObjectHTKeyValDestructor, /* key destructor */
+ JimObjectHTKeyValDestructor /* val destructor */
+};
+
+/* Note that while the elements of the dict may contain references,
+ * the list object itself can't. This basically means that the
+ * dict object string representation as a whole can't contain references
+ * that are not presents in the single elements. */
+static const Jim_ObjType dictObjType = {
+ "dict",
+ FreeDictInternalRep,
+ DupDictInternalRep,
+ UpdateStringOfDict,
+ JIM_TYPE_NONE,
+};
+
+void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ JIM_NOTUSED(interp);
+
+ Jim_FreeHashTable(objPtr->internalRep.ptr);
+ Jim_Free(objPtr->internalRep.ptr);
+}
+
+void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ Jim_HashTable *ht, *dupHt;
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+
+ /* Create a new hash table */
+ ht = srcPtr->internalRep.ptr;
+ dupHt = Jim_Alloc(sizeof(*dupHt));
+ Jim_InitHashTable(dupHt, &JimDictHashTableType, interp);
+ if (ht->size != 0)
+ Jim_ExpandHashTable(dupHt, ht->size);
+ /* Copy every element from the source to the dup hash table */
+ htiter = Jim_GetHashTableIterator(ht);
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ const Jim_Obj *keyObjPtr = he->key;
+ Jim_Obj *valObjPtr = he->val;
+
+ Jim_IncrRefCount((Jim_Obj *)keyObjPtr); /* ATTENTION: const cast */
+ Jim_IncrRefCount(valObjPtr);
+ Jim_AddHashEntry(dupHt, keyObjPtr, valObjPtr);
+ }
+ Jim_FreeHashTableIterator(htiter);
+
+ dupPtr->internalRep.ptr = dupHt;
+ dupPtr->typePtr = &dictObjType;
+}
+
+void UpdateStringOfDict(struct Jim_Obj *objPtr)
+{
+ int i, bufLen, realLength;
+ const char *strRep;
+ char *p;
+ int *quotingType, objc;
+ Jim_HashTable *ht;
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+ Jim_Obj **objv;
+
+ /* Trun the hash table into a flat vector of Jim_Objects. */
+ ht = objPtr->internalRep.ptr;
+ objc = ht->used * 2;
+ objv = Jim_Alloc(objc * sizeof(Jim_Obj *));
+ htiter = Jim_GetHashTableIterator(ht);
+ i = 0;
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ objv[i++] = (Jim_Obj *)he->key; /* ATTENTION: const cast */
+ objv[i++] = he->val;
+ }
+ Jim_FreeHashTableIterator(htiter);
+ /* (Over) Estimate the space needed. */
+ quotingType = Jim_Alloc(sizeof(int) * objc);
+ bufLen = 0;
+ for (i = 0; i < objc; i++) {
+ int len;
+
+ strRep = Jim_GetString(objv[i], &len);
+ quotingType[i] = ListElementQuotingType(strRep, len);
+ switch (quotingType[i]) {
+ case JIM_ELESTR_SIMPLE:
+ bufLen += len;
+ break;
+ case JIM_ELESTR_BRACE:
+ bufLen += len + 2;
+ break;
+ case JIM_ELESTR_QUOTE:
+ bufLen += len * 2;
+ break;
+ }
+ bufLen++; /* elements separator. */
+ }
+ bufLen++;
+
+ /* Generate the string rep. */
+ p = objPtr->bytes = Jim_Alloc(bufLen + 1);
+ realLength = 0;
+ for (i = 0; i < objc; i++) {
+ int len, qlen;
+ char *q;
+
+ strRep = Jim_GetString(objv[i], &len);
+
+ switch (quotingType[i]) {
+ case JIM_ELESTR_SIMPLE:
+ memcpy(p, strRep, len);
+ p += len;
+ realLength += len;
+ break;
+ case JIM_ELESTR_BRACE:
+ *p++ = '{';
+ memcpy(p, strRep, len);
+ p += len;
+ *p++ = '}';
+ realLength += len + 2;
+ break;
+ case JIM_ELESTR_QUOTE:
+ q = BackslashQuoteString(strRep, len, &qlen);
+ memcpy(p, q, qlen);
+ Jim_Free(q);
+ p += qlen;
+ realLength += qlen;
+ break;
+ }
+ /* Add a separating space */
+ if (i + 1 != objc) {
+ *p++ = ' ';
+ realLength++;
+ }
+ }
+ *p = '\0'; /* nul term. */
+ objPtr->length = realLength;
+ Jim_Free(quotingType);
+ Jim_Free(objv);
+}
+
+static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr)
+{
+ int listlen;
+
+ /* Get the string representation. Do this first so we don't
+ * change order in case of fast conversion to dict.
+ */
+ Jim_String(objPtr);
+
+ /* For simplicity, convert a non-list object to a list and then to a dict */
+ listlen = Jim_ListLength(interp, objPtr);
+ if (listlen % 2) {
+ Jim_SetResultString(interp,
+ "invalid dictionary value: must be a list with an even number of elements", -1);
+ return JIM_ERR;
+ }
+ else {
+ /* Now it is easy to convert to a dict from a list, and it can't fail */
+ Jim_HashTable *ht;
+ int i;
+
+ ht = Jim_Alloc(sizeof(*ht));
+ Jim_InitHashTable(ht, &JimDictHashTableType, interp);
+
+ for (i = 0; i < listlen; i += 2) {
+ Jim_Obj *keyObjPtr;
+ Jim_Obj *valObjPtr;
+
+ Jim_ListIndex(interp, objPtr, i, &keyObjPtr, JIM_NONE);
+ Jim_ListIndex(interp, objPtr, i + 1, &valObjPtr, JIM_NONE);
+
+ Jim_IncrRefCount(keyObjPtr);
+ Jim_IncrRefCount(valObjPtr);
+
+ if (Jim_AddHashEntry(ht, keyObjPtr, valObjPtr) != JIM_OK) {
+ Jim_HashEntry *he;
+
+ he = Jim_FindHashEntry(ht, keyObjPtr);
+ Jim_DecrRefCount(interp, keyObjPtr);
+ /* ATTENTION: const cast */
+ Jim_DecrRefCount(interp, (Jim_Obj *)he->val);
+ he->val = valObjPtr;
+ }
+ }
+
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &dictObjType;
+ objPtr->internalRep.ptr = ht;
+
+ return JIM_OK;
+ }
+}
+
+/* Dict object API */
+
+/* Add an element to a dict. objPtr must be of the "dict" type.
+ * The higer-level exported function is Jim_DictAddElement().
+ * If an element with the specified key already exists, the value
+ * associated is replaced with the new one.
+ *
+ * if valueObjPtr == NULL, the key is instead removed if it exists. */
+static int DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
+ Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr)
+{
+ Jim_HashTable *ht = objPtr->internalRep.ptr;
+
+ if (valueObjPtr == NULL) { /* unset */
+ return Jim_DeleteHashEntry(ht, keyObjPtr);
+ }
+ Jim_IncrRefCount(keyObjPtr);
+ Jim_IncrRefCount(valueObjPtr);
+ if (Jim_AddHashEntry(ht, keyObjPtr, valueObjPtr) != JIM_OK) {
+ Jim_HashEntry *he = Jim_FindHashEntry(ht, keyObjPtr);
+
+ Jim_DecrRefCount(interp, keyObjPtr);
+ /* ATTENTION: const cast */
+ Jim_DecrRefCount(interp, (Jim_Obj *)he->val);
+ he->val = valueObjPtr;
+ }
+ return JIM_OK;
+}
+
+/* Add an element, higher-level interface for DictAddElement().
+ * If valueObjPtr == NULL, the key is removed if it exists. */
+int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr,
+ Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr)
+{
+ int retcode;
+
+ JimPanic((Jim_IsShared(objPtr), interp, "Jim_DictAddElement called with shared object"));
+ if (objPtr->typePtr != &dictObjType) {
+ if (SetDictFromAny(interp, objPtr) != JIM_OK)
+ return JIM_ERR;
+ }
+ retcode = DictAddElement(interp, objPtr, keyObjPtr, valueObjPtr);
+ Jim_InvalidateStringRep(objPtr);
+ return retcode;
+}
+
+Jim_Obj *Jim_NewDictObj(Jim_Interp *interp, Jim_Obj *const *elements, int len)
+{
+ Jim_Obj *objPtr;
+ int i;
+
+ JimPanic((len % 2, interp, "Jim_NewDictObj() 'len' argument must be even"));
+
+ objPtr = Jim_NewObj(interp);
+ objPtr->typePtr = &dictObjType;
+ objPtr->bytes = NULL;
+ objPtr->internalRep.ptr = Jim_Alloc(sizeof(Jim_HashTable));
+ Jim_InitHashTable(objPtr->internalRep.ptr, &JimDictHashTableType, interp);
+ for (i = 0; i < len; i += 2)
+ DictAddElement(interp, objPtr, elements[i], elements[i + 1]);
+ return objPtr;
+}
+
+/* Return the value associated to the specified dict key
+ * Note: Returns JIM_OK if OK, JIM_ERR if entry not found or -1 if can't create dict value
+ */
+int Jim_DictKey(Jim_Interp *interp, Jim_Obj *dictPtr, Jim_Obj *keyPtr,
+ Jim_Obj **objPtrPtr, int flags)
+{
+ Jim_HashEntry *he;
+ Jim_HashTable *ht;
+
+ if (dictPtr->typePtr != &dictObjType) {
+ if (SetDictFromAny(interp, dictPtr) != JIM_OK)
+ return -1;
+ }
+ ht = dictPtr->internalRep.ptr;
+ if ((he = Jim_FindHashEntry(ht, keyPtr)) == NULL) {
+ if (flags & JIM_ERRMSG) {
+ Jim_SetResultFormatted(interp, "key \"%#s\" not found in dictionary", keyPtr);
+ }
+ return JIM_ERR;
+ }
+ *objPtrPtr = he->val;
+ return JIM_OK;
+}
+
+/* Return an allocated array of key/value pairs for the dictionary. Stores the length in *len */
+int Jim_DictPairs(Jim_Interp *interp, Jim_Obj *dictPtr, Jim_Obj ***objPtrPtr, int *len)
+{
+ Jim_HashTable *ht;
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+ Jim_Obj **objv;
+ int i;
+
+ if (dictPtr->typePtr != &dictObjType) {
+ if (SetDictFromAny(interp, dictPtr) != JIM_OK)
+ return JIM_ERR;
+ }
+ ht = dictPtr->internalRep.ptr;
+
+ /* Turn the hash table into a flat vector of Jim_Objects. */
+ objv = Jim_Alloc((ht->used * 2) * sizeof(Jim_Obj *));
+ htiter = Jim_GetHashTableIterator(ht);
+ i = 0;
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ objv[i++] = (Jim_Obj *)he->key; /* ATTENTION: const cast */
+ objv[i++] = he->val;
+ }
+ *len = i;
+ Jim_FreeHashTableIterator(htiter);
+ *objPtrPtr = objv;
+ return JIM_OK;
+}
+
+
+/* Return the value associated to the specified dict keys */
+int Jim_DictKeysVector(Jim_Interp *interp, Jim_Obj *dictPtr,
+ Jim_Obj *const *keyv, int keyc, Jim_Obj **objPtrPtr, int flags)
+{
+ int i;
+
+ if (keyc == 0) {
+ *objPtrPtr = dictPtr;
+ return JIM_OK;
+ }
+
+ for (i = 0; i < keyc; i++) {
+ Jim_Obj *objPtr;
+
+ if (Jim_DictKey(interp, dictPtr, keyv[i], &objPtr, flags)
+ != JIM_OK)
+ return JIM_ERR;
+ dictPtr = objPtr;
+ }
+ *objPtrPtr = dictPtr;
+ return JIM_OK;
+}
+
+/* Modify the dict stored into the variable named 'varNamePtr'
+ * setting the element specified by the 'keyc' keys objects in 'keyv',
+ * with the new value of the element 'newObjPtr'.
+ *
+ * If newObjPtr == NULL the operation is to remove the given key
+ * from the dictionary. */
+int Jim_SetDictKeysVector(Jim_Interp *interp, Jim_Obj *varNamePtr,
+ Jim_Obj *const *keyv, int keyc, Jim_Obj *newObjPtr)
+{
+ Jim_Obj *varObjPtr, *objPtr, *dictObjPtr;
+ int shared, i;
+
+ varObjPtr = objPtr =
+ Jim_GetVariable(interp, varNamePtr, newObjPtr == NULL ? JIM_ERRMSG : JIM_NONE);
+ if (objPtr == NULL) {
+ if (newObjPtr == NULL) /* Cannot remove a key from non existing var */
+ return JIM_ERR;
+ varObjPtr = objPtr = Jim_NewDictObj(interp, NULL, 0);
+ if (Jim_SetVariable(interp, varNamePtr, objPtr) != JIM_OK) {
+ Jim_FreeNewObj(interp, varObjPtr);
+ return JIM_ERR;
+ }
+ }
+ if ((shared = Jim_IsShared(objPtr)))
+ varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr);
+ for (i = 0; i < keyc - 1; i++) {
+ dictObjPtr = objPtr;
+
+ /* Check if it's a valid dictionary */
+ if (dictObjPtr->typePtr != &dictObjType) {
+ if (SetDictFromAny(interp, dictObjPtr) != JIM_OK)
+ goto err;
+ }
+ /* Check if the given key exists. */
+ Jim_InvalidateStringRep(dictObjPtr);
+ if (Jim_DictKey(interp, dictObjPtr, keyv[i], &objPtr,
+ newObjPtr ? JIM_NONE : JIM_ERRMSG) == JIM_OK) {
+ /* This key exists at the current level.
+ * Make sure it's not shared!. */
+ if (Jim_IsShared(objPtr)) {
+ objPtr = Jim_DuplicateObj(interp, objPtr);
+ DictAddElement(interp, dictObjPtr, keyv[i], objPtr);
+ }
+ }
+ else {
+ /* Key not found. If it's an [unset] operation
+ * this is an error. Only the last key may not
+ * exist. */
+ if (newObjPtr == NULL)
+ goto err;
+ /* Otherwise set an empty dictionary
+ * as key's value. */
+ objPtr = Jim_NewDictObj(interp, NULL, 0);
+ DictAddElement(interp, dictObjPtr, keyv[i], objPtr);
+ }
+ }
+ if (Jim_DictAddElement(interp, objPtr, keyv[keyc - 1], newObjPtr) != JIM_OK) {
+ goto err;
+ }
+ Jim_InvalidateStringRep(objPtr);
+ Jim_InvalidateStringRep(varObjPtr);
+ if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK)
+ goto err;
+ Jim_SetResult(interp, varObjPtr);
+ return JIM_OK;
+ err:
+ if (shared) {
+ Jim_FreeNewObj(interp, varObjPtr);
+ }
+ return JIM_ERR;
+}
+
+/* -----------------------------------------------------------------------------
+ * Index object
+ * ---------------------------------------------------------------------------*/
+static void UpdateStringOfIndex(struct Jim_Obj *objPtr);
+static int SetIndexFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType indexObjType = {
+ "index",
+ NULL,
+ NULL,
+ UpdateStringOfIndex,
+ JIM_TYPE_NONE,
+};
+
+void UpdateStringOfIndex(struct Jim_Obj *objPtr)
+{
+ int len;
+ char buf[JIM_INTEGER_SPACE + 1];
+
+ if (objPtr->internalRep.indexValue >= 0)
+ len = sprintf(buf, "%d", objPtr->internalRep.indexValue);
+ else if (objPtr->internalRep.indexValue == -1)
+ len = sprintf(buf, "end");
+ else {
+ len = sprintf(buf, "end%d", objPtr->internalRep.indexValue + 1);
+ }
+ objPtr->bytes = Jim_Alloc(len + 1);
+ memcpy(objPtr->bytes, buf, len + 1);
+ objPtr->length = len;
+}
+
+int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ int idx, end = 0;
+ const char *str;
+ char *endptr;
+
+ /* Get the string representation */
+ str = Jim_String(objPtr);
+
+ /* Try to convert into an index */
+ if (strncmp(str, "end", 3) == 0) {
+ end = 1;
+ str += 3;
+ idx = 0;
+ }
+ else {
+ idx = strtol(str, &endptr, 10);
+
+ if (endptr == str) {
+ goto badindex;
+ }
+ str = endptr;
+ }
+
+ /* Now str may include or +<num> or -<num> */
+ if (*str == '+' || *str == '-') {
+ int sign = (*str == '+' ? 1 : -1);
+
+ idx += sign * strtol(++str, &endptr, 10);
+ if (str == endptr || *endptr) {
+ goto badindex;
+ }
+ str = endptr;
+ }
+ /* The only thing left should be spaces */
+ while (isspace(UCHAR(*str))) {
+ str++;
+ }
+ if (*str) {
+ goto badindex;
+ }
+ if (end) {
+ if (idx > 0) {
+ idx = INT_MAX;
+ }
+ else {
+ /* end-1 is repesented as -2 */
+ idx--;
+ }
+ }
+ else if (idx < 0) {
+ idx = -INT_MAX;
+ }
+
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &indexObjType;
+ objPtr->internalRep.indexValue = idx;
+ return JIM_OK;
+
+ badindex:
+ Jim_SetResultFormatted(interp,
+ "bad index \"%#s\": must be integer?[+-]integer? or end?[+-]integer?", objPtr);
+ return JIM_ERR;
+}
+
+int Jim_GetIndex(Jim_Interp *interp, Jim_Obj *objPtr, int *indexPtr)
+{
+ /* Avoid shimmering if the object is an integer. */
+ if (objPtr->typePtr == &intObjType) {
+ jim_wide val = JimWideValue(objPtr);
+
+ if (!(val < LONG_MIN) && !(val > LONG_MAX)) {
+ *indexPtr = (val < 0) ? -INT_MAX : (long)val;;
+ return JIM_OK;
+ }
+ }
+ if (objPtr->typePtr != &indexObjType && SetIndexFromAny(interp, objPtr) == JIM_ERR)
+ return JIM_ERR;
+ *indexPtr = objPtr->internalRep.indexValue;
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Return Code Object.
+ * ---------------------------------------------------------------------------*/
+
+/* NOTE: These must be kept in the same order as JIM_OK, JIM_ERR, ... */
+static const char * const jimReturnCodes[] = {
+ [JIM_OK] = "ok",
+ [JIM_ERR] = "error",
+ [JIM_RETURN] = "return",
+ [JIM_BREAK] = "break",
+ [JIM_CONTINUE] = "continue",
+ [JIM_SIGNAL] = "signal",
+ [JIM_EXIT] = "exit",
+ [JIM_EVAL] = "eval",
+ NULL
+};
+
+#define jimReturnCodesSize (sizeof(jimReturnCodes)/sizeof(*jimReturnCodes))
+
+static int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr);
+
+static const Jim_ObjType returnCodeObjType = {
+ "return-code",
+ NULL,
+ NULL,
+ NULL,
+ JIM_TYPE_NONE,
+};
+
+/* Converts a (standard) return code to a string. Returns "?" for
+ * non-standard return codes.
+ */
+const char *Jim_ReturnCode(int code)
+{
+ if (code < 0 || code >= (int)jimReturnCodesSize) {
+ return "?";
+ }
+ else {
+ return jimReturnCodes[code];
+ }
+}
+
+int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ int returnCode;
+ jim_wide wideValue;
+
+ /* Try to convert into an integer */
+ if (JimGetWideNoErr(interp, objPtr, &wideValue) != JIM_ERR)
+ returnCode = (int)wideValue;
+ else if (Jim_GetEnum(interp, objPtr, jimReturnCodes, &returnCode, NULL, JIM_NONE) != JIM_OK) {
+ Jim_SetResultFormatted(interp, "expected return code but got \"%#s\"", objPtr);
+ return JIM_ERR;
+ }
+ /* Free the old internal repr and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ objPtr->typePtr = &returnCodeObjType;
+ objPtr->internalRep.returnCode = returnCode;
+ return JIM_OK;
+}
+
+int Jim_GetReturnCode(Jim_Interp *interp, Jim_Obj *objPtr, int *intPtr)
+{
+ if (objPtr->typePtr != &returnCodeObjType && SetReturnCodeFromAny(interp, objPtr) == JIM_ERR)
+ return JIM_ERR;
+ *intPtr = objPtr->internalRep.returnCode;
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Expression Parsing
+ * ---------------------------------------------------------------------------*/
+static int JimParseExprOperator(struct JimParserCtx *pc);
+static int JimParseExprNumber(struct JimParserCtx *pc);
+static int JimParseExprIrrational(struct JimParserCtx *pc);
+
+/* Exrp's Stack machine operators opcodes. */
+
+/* Binary operators (numbers) */
+enum
+{
+ /* Continues on from the JIM_TT_ space */
+ /* Operations */
+ JIM_EXPROP_MUL = JIM_TT_EXPR_OP, /* 15 */
+ JIM_EXPROP_DIV,
+ JIM_EXPROP_MOD,
+ JIM_EXPROP_SUB,
+ JIM_EXPROP_ADD,
+ JIM_EXPROP_LSHIFT,
+ JIM_EXPROP_RSHIFT,
+ JIM_EXPROP_ROTL,
+ JIM_EXPROP_ROTR,
+ JIM_EXPROP_LT,
+ JIM_EXPROP_GT,
+ JIM_EXPROP_LTE,
+ JIM_EXPROP_GTE,
+ JIM_EXPROP_NUMEQ,
+ JIM_EXPROP_NUMNE,
+ JIM_EXPROP_BITAND, /* 30 */
+ JIM_EXPROP_BITXOR,
+ JIM_EXPROP_BITOR,
+
+ /* Note must keep these together */
+ JIM_EXPROP_LOGICAND, /* 33 */
+ JIM_EXPROP_LOGICAND_LEFT,
+ JIM_EXPROP_LOGICAND_RIGHT,
+
+ /* and these */
+ JIM_EXPROP_LOGICOR, /* 36 */
+ JIM_EXPROP_LOGICOR_LEFT,
+ JIM_EXPROP_LOGICOR_RIGHT,
+
+ /* and these */
+ /* Ternary operators */
+ JIM_EXPROP_TERNARY, /* 39 */
+ JIM_EXPROP_TERNARY_LEFT,
+ JIM_EXPROP_TERNARY_RIGHT,
+
+ /* and these */
+ JIM_EXPROP_COLON, /* 42 */
+ JIM_EXPROP_COLON_LEFT,
+ JIM_EXPROP_COLON_RIGHT,
+
+ JIM_EXPROP_POW, /* 45 */
+
+/* Binary operators (strings) */
+ JIM_EXPROP_STREQ,
+ JIM_EXPROP_STRNE,
+ JIM_EXPROP_STRIN,
+ JIM_EXPROP_STRNI,
+
+/* Unary operators (numbers) */
+ JIM_EXPROP_NOT,
+ JIM_EXPROP_BITNOT,
+ JIM_EXPROP_UNARYMINUS,
+ JIM_EXPROP_UNARYPLUS,
+
+ /* Functions */
+ JIM_EXPROP_FUNC_FIRST,
+ JIM_EXPROP_FUNC_INT = JIM_EXPROP_FUNC_FIRST,
+ JIM_EXPROP_FUNC_ABS,
+ JIM_EXPROP_FUNC_DOUBLE,
+ JIM_EXPROP_FUNC_ROUND,
+
+#ifdef JIM_MATH_FUNCTIONS
+ /* math functions from libm */
+ JIM_EXPROP_FUNC_SIN,
+ JIM_EXPROP_FUNC_COS,
+ JIM_EXPROP_FUNC_TAN,
+ JIM_EXPROP_FUNC_ASIN,
+ JIM_EXPROP_FUNC_ACOS,
+ JIM_EXPROP_FUNC_ATAN,
+ JIM_EXPROP_FUNC_SINH,
+ JIM_EXPROP_FUNC_COSH,
+ JIM_EXPROP_FUNC_TANH,
+ JIM_EXPROP_FUNC_CEIL,
+ JIM_EXPROP_FUNC_FLOOR,
+ JIM_EXPROP_FUNC_EXP,
+ JIM_EXPROP_FUNC_LOG,
+ JIM_EXPROP_FUNC_LOG10,
+ JIM_EXPROP_FUNC_SQRT,
+#endif
+};
+
+struct JimExprState
+{
+ Jim_Obj **stack;
+ int stacklen;
+ int opcode;
+ int skip;
+};
+
+/* Operators table */
+typedef struct Jim_ExprOperator
+{
+ const char *name;
+ int precedence;
+ int arity;
+ int (*funcop) (Jim_Interp *interp, struct JimExprState * e);
+ int lazy;
+} Jim_ExprOperator;
+
+static void ExprPush(struct JimExprState *e, Jim_Obj *obj)
+{
+ Jim_IncrRefCount(obj);
+ e->stack[e->stacklen++] = obj;
+}
+
+static Jim_Obj *ExprPop(struct JimExprState *e)
+{
+ return e->stack[--e->stacklen];
+}
+
+static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprState *e)
+{
+ int intresult = 0;
+ int rc = JIM_OK;
+ Jim_Obj *A = ExprPop(e);
+ double dA, dC = 0;
+ jim_wide wA, wC = 0;
+
+ if ((A->typePtr != &doubleObjType || A->bytes) && JimGetWideNoErr(interp, A, &wA) == JIM_OK) {
+ intresult = 1;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_FUNC_INT:
+ wC = wA;
+ break;
+ case JIM_EXPROP_FUNC_ROUND:
+ wC = wA;
+ break;
+ case JIM_EXPROP_FUNC_DOUBLE:
+ dC = wA;
+ intresult = 0;
+ break;
+ case JIM_EXPROP_FUNC_ABS:
+ wC = wA >= 0 ? wA : -wA;
+ break;
+ case JIM_EXPROP_UNARYMINUS:
+ wC = -wA;
+ break;
+ case JIM_EXPROP_UNARYPLUS:
+ wC = wA;
+ break;
+ case JIM_EXPROP_NOT:
+ wC = !wA;
+ break;
+ default:
+ abort();
+ }
+ }
+ else if ((rc = Jim_GetDouble(interp, A, &dA)) == JIM_OK) {
+ switch (e->opcode) {
+ case JIM_EXPROP_FUNC_INT:
+ wC = dA;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_FUNC_ROUND:
+ wC = dA < 0 ? (dA - 0.5) : (dA + 0.5);
+ intresult = 1;
+ break;
+ case JIM_EXPROP_FUNC_DOUBLE:
+ dC = dA;
+ break;
+ case JIM_EXPROP_FUNC_ABS:
+ dC = dA >= 0 ? dA : -dA;
+ break;
+ case JIM_EXPROP_UNARYMINUS:
+ dC = -dA;
+ break;
+ case JIM_EXPROP_UNARYPLUS:
+ dC = dA;
+ break;
+ case JIM_EXPROP_NOT:
+ wC = !dA;
+ intresult = 1;
+ break;
+ default:
+ abort();
+ }
+ }
+
+ if (rc == JIM_OK) {
+ if (intresult) {
+ ExprPush(e, Jim_NewIntObj(interp, wC));
+ }
+ else {
+ ExprPush(e, Jim_NewDoubleObj(interp, dC));
+ }
+ }
+
+ Jim_DecrRefCount(interp, A);
+
+ return rc;
+}
+
+static int JimExprOpIntUnary(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *A = ExprPop(e);
+ jim_wide wA;
+ int rc = JIM_ERR;
+
+
+ if (Jim_GetWide(interp, A, &wA) == JIM_OK) {
+ jim_wide wC;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_BITNOT:
+ wC = ~wA;
+ break;
+ default:
+ abort();
+ }
+ ExprPush(e, Jim_NewIntObj(interp, wC));
+ rc = JIM_OK;
+ }
+
+ Jim_DecrRefCount(interp, A);
+
+ return rc;
+}
+
+#ifdef JIM_MATH_FUNCTIONS
+static int JimExprOpDoubleUnary(Jim_Interp *interp, struct JimExprState *e)
+{
+ int rc;
+ Jim_Obj *A = ExprPop(e);
+ double dA, dC;
+
+ rc = Jim_GetDouble(interp, A, &dA);
+ if (rc == JIM_OK) {
+ switch (e->opcode) {
+ case JIM_EXPROP_FUNC_SIN:
+ dC = sin(dA);
+ break;
+ case JIM_EXPROP_FUNC_COS:
+ dC = cos(dA);
+ break;
+ case JIM_EXPROP_FUNC_TAN:
+ dC = tan(dA);
+ break;
+ case JIM_EXPROP_FUNC_ASIN:
+ dC = asin(dA);
+ break;
+ case JIM_EXPROP_FUNC_ACOS:
+ dC = acos(dA);
+ break;
+ case JIM_EXPROP_FUNC_ATAN:
+ dC = atan(dA);
+ break;
+ case JIM_EXPROP_FUNC_SINH:
+ dC = sinh(dA);
+ break;
+ case JIM_EXPROP_FUNC_COSH:
+ dC = cosh(dA);
+ break;
+ case JIM_EXPROP_FUNC_TANH:
+ dC = tanh(dA);
+ break;
+ case JIM_EXPROP_FUNC_CEIL:
+ dC = ceil(dA);
+ break;
+ case JIM_EXPROP_FUNC_FLOOR:
+ dC = floor(dA);
+ break;
+ case JIM_EXPROP_FUNC_EXP:
+ dC = exp(dA);
+ break;
+ case JIM_EXPROP_FUNC_LOG:
+ dC = log(dA);
+ break;
+ case JIM_EXPROP_FUNC_LOG10:
+ dC = log10(dA);
+ break;
+ case JIM_EXPROP_FUNC_SQRT:
+ dC = sqrt(dA);
+ break;
+ default:
+ abort();
+ }
+ ExprPush(e, Jim_NewDoubleObj(interp, dC));
+ }
+
+ Jim_DecrRefCount(interp, A);
+
+ return rc;
+}
+#endif
+
+/* A binary operation on two ints */
+static int JimExprOpIntBin(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *B = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+ jim_wide wA, wB;
+ int rc = JIM_ERR;
+
+ if (Jim_GetWide(interp, A, &wA) == JIM_OK && Jim_GetWide(interp, B, &wB) == JIM_OK) {
+ jim_wide wC;
+
+ rc = JIM_OK;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_LSHIFT:
+ wC = wA << wB;
+ break;
+ case JIM_EXPROP_RSHIFT:
+ wC = wA >> wB;
+ break;
+ case JIM_EXPROP_BITAND:
+ wC = wA & wB;
+ break;
+ case JIM_EXPROP_BITXOR:
+ wC = wA ^ wB;
+ break;
+ case JIM_EXPROP_BITOR:
+ wC = wA | wB;
+ break;
+ case JIM_EXPROP_MOD:
+ if (wB == 0) {
+ wC = 0;
+ Jim_SetResultString(interp, "Division by zero", -1);
+ rc = JIM_ERR;
+ }
+ else {
+ /*
+ * From Tcl 8.x
+ *
+ * This code is tricky: C doesn't guarantee much
+ * about the quotient or remainder, but Tcl does.
+ * The remainder always has the same sign as the
+ * divisor and a smaller absolute value.
+ */
+ int negative = 0;
+
+ if (wB < 0) {
+ wB = -wB;
+ wA = -wA;
+ negative = 1;
+ }
+ wC = wA % wB;
+ if (wC < 0) {
+ wC += wB;
+ }
+ if (negative) {
+ wC = -wC;
+ }
+ }
+ break;
+ case JIM_EXPROP_ROTL:{
+ /* uint32_t would be better. But not everyone has inttypes.h? */
+ unsigned long uA = (unsigned long)wA;
+ const unsigned int S = sizeof(unsigned long) * 8;
+
+ wC = (unsigned long)((uA << wB) | (uA >> (S - wB)));
+ break;
+ }
+ case JIM_EXPROP_ROTR:{
+ unsigned long uA = (unsigned long)wA;
+ const unsigned int S = sizeof(unsigned long) * 8;
+
+ wC = (unsigned long)((uA >> wB) | (uA << (S - wB)));
+ break;
+ }
+ default:
+ abort();
+ }
+ ExprPush(e, Jim_NewIntObj(interp, wC));
+
+ }
+
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, B);
+
+ return rc;
+}
+
+
+/* A binary operation on two ints or two doubles (or two strings for some ops) */
+static int JimExprOpBin(Jim_Interp *interp, struct JimExprState *e)
+{
+ int intresult = 0;
+ int rc = JIM_OK;
+ double dA, dB, dC = 0;
+ jim_wide wA, wB, wC = 0;
+
+ Jim_Obj *B = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+
+ if ((A->typePtr != &doubleObjType || A->bytes) &&
+ (B->typePtr != &doubleObjType || B->bytes) &&
+ JimGetWideNoErr(interp, A, &wA) == JIM_OK && JimGetWideNoErr(interp, B, &wB) == JIM_OK) {
+
+ /* Both are ints */
+
+ intresult = 1;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_POW:
+ wC = JimPowWide(wA, wB);
+ break;
+ case JIM_EXPROP_ADD:
+ wC = wA + wB;
+ break;
+ case JIM_EXPROP_SUB:
+ wC = wA - wB;
+ break;
+ case JIM_EXPROP_MUL:
+ wC = wA * wB;
+ break;
+ case JIM_EXPROP_DIV:
+ if (wB == 0) {
+ Jim_SetResultString(interp, "Division by zero", -1);
+ rc = JIM_ERR;
+ }
+ else {
+ /*
+ * From Tcl 8.x
+ *
+ * This code is tricky: C doesn't guarantee much
+ * about the quotient or remainder, but Tcl does.
+ * The remainder always has the same sign as the
+ * divisor and a smaller absolute value.
+ */
+ if (wB < 0) {
+ wB = -wB;
+ wA = -wA;
+ }
+ wC = wA / wB;
+ if (wA % wB < 0) {
+ wC--;
+ }
+ }
+ break;
+ case JIM_EXPROP_LT:
+ wC = wA < wB;
+ break;
+ case JIM_EXPROP_GT:
+ wC = wA > wB;
+ break;
+ case JIM_EXPROP_LTE:
+ wC = wA <= wB;
+ break;
+ case JIM_EXPROP_GTE:
+ wC = wA >= wB;
+ break;
+ case JIM_EXPROP_NUMEQ:
+ wC = wA == wB;
+ break;
+ case JIM_EXPROP_NUMNE:
+ wC = wA != wB;
+ break;
+ default:
+ abort();
+ }
+ }
+ else if (Jim_GetDouble(interp, A, &dA) == JIM_OK && Jim_GetDouble(interp, B, &dB) == JIM_OK) {
+ switch (e->opcode) {
+ case JIM_EXPROP_POW:
+#ifdef JIM_MATH_FUNCTIONS
+ dC = pow(dA, dB);
+#else
+ Jim_SetResultString(interp, "unsupported", -1);
+ rc = JIM_ERR;
+#endif
+ break;
+ case JIM_EXPROP_ADD:
+ dC = dA + dB;
+ break;
+ case JIM_EXPROP_SUB:
+ dC = dA - dB;
+ break;
+ case JIM_EXPROP_MUL:
+ dC = dA * dB;
+ break;
+ case JIM_EXPROP_DIV:
+ if (dB == 0) {
+#ifdef INFINITY
+ dC = dA < 0 ? -INFINITY : INFINITY;
+#else
+ dC = (dA < 0 ? -1.0 : 1.0) * strtod("Inf", NULL);
+#endif
+ }
+ else {
+ dC = dA / dB;
+ }
+ break;
+ case JIM_EXPROP_LT:
+ wC = dA < dB;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_GT:
+ wC = dA > dB;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_LTE:
+ wC = dA <= dB;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_GTE:
+ wC = dA >= dB;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_NUMEQ:
+ wC = dA == dB;
+ intresult = 1;
+ break;
+ case JIM_EXPROP_NUMNE:
+ wC = dA != dB;
+ intresult = 1;
+ break;
+ default:
+ abort();
+ }
+ }
+ else {
+ /* Handle the string case */
+
+ /* REVISIT: Could optimise the eq/ne case by checking lengths */
+ int i = Jim_StringCompareObj(interp, A, B, 0);
+
+ intresult = 1;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_LT:
+ wC = i < 0;
+ break;
+ case JIM_EXPROP_GT:
+ wC = i > 0;
+ break;
+ case JIM_EXPROP_LTE:
+ wC = i <= 0;
+ break;
+ case JIM_EXPROP_GTE:
+ wC = i >= 0;
+ break;
+ case JIM_EXPROP_NUMEQ:
+ wC = i == 0;
+ break;
+ case JIM_EXPROP_NUMNE:
+ wC = i != 0;
+ break;
+ default:
+ rc = JIM_ERR;
+ break;
+ }
+ }
+
+ if (rc == JIM_OK) {
+ if (intresult) {
+ ExprPush(e, Jim_NewIntObj(interp, wC));
+ }
+ else {
+ ExprPush(e, Jim_NewDoubleObj(interp, dC));
+ }
+ }
+
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, B);
+
+ return rc;
+}
+
+static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valObj)
+{
+ int listlen;
+ int i;
+
+ listlen = Jim_ListLength(interp, listObjPtr);
+ for (i = 0; i < listlen; i++) {
+ Jim_Obj *objPtr;
+
+ Jim_ListIndex(interp, listObjPtr, i, &objPtr, JIM_NONE);
+
+ if (Jim_StringEqObj(objPtr, valObj)) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *B = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+
+ jim_wide wC;
+
+ switch (e->opcode) {
+ case JIM_EXPROP_STREQ:
+ case JIM_EXPROP_STRNE: {
+ int Alen, Blen;
+ const char *sA = Jim_GetString(A, &Alen);
+ const char *sB = Jim_GetString(B, &Blen);
+
+ if (e->opcode == JIM_EXPROP_STREQ) {
+ wC = (Alen == Blen && memcmp(sA, sB, Alen) == 0);
+ }
+ else {
+ wC = (Alen != Blen || memcmp(sA, sB, Alen) != 0);
+ }
+ break;
+ }
+ case JIM_EXPROP_STRIN:
+ wC = JimSearchList(interp, B, A);
+ break;
+ case JIM_EXPROP_STRNI:
+ wC = !JimSearchList(interp, B, A);
+ break;
+ default:
+ abort();
+ }
+ ExprPush(e, Jim_NewIntObj(interp, wC));
+
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, B);
+
+ return JIM_OK;
+}
+
+static int ExprBool(Jim_Interp *interp, Jim_Obj *obj)
+{
+ long l;
+ double d;
+
+ if (Jim_GetLong(interp, obj, &l) == JIM_OK) {
+ return l != 0;
+ }
+ if (Jim_GetDouble(interp, obj, &d) == JIM_OK) {
+ return d != 0;
+ }
+ return -1;
+}
+
+static int JimExprOpAndLeft(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *skip = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+ int rc = JIM_OK;
+
+ switch (ExprBool(interp, A)) {
+ case 0:
+ /* false, so skip RHS opcodes with a 0 result */
+ e->skip = JimWideValue(skip);
+ ExprPush(e, Jim_NewIntObj(interp, 0));
+ break;
+
+ case 1:
+ /* true so continue */
+ break;
+
+ case -1:
+ /* Invalid */
+ rc = JIM_ERR;
+ }
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, skip);
+
+ return rc;
+}
+
+static int JimExprOpOrLeft(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *skip = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+ int rc = JIM_OK;
+
+ switch (ExprBool(interp, A)) {
+ case 0:
+ /* false, so do nothing */
+ break;
+
+ case 1:
+ /* true so skip RHS opcodes with a 1 result */
+ e->skip = JimWideValue(skip);
+ ExprPush(e, Jim_NewIntObj(interp, 1));
+ break;
+
+ case -1:
+ /* Invalid */
+ rc = JIM_ERR;
+ break;
+ }
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, skip);
+
+ return rc;
+}
+
+static int JimExprOpAndOrRight(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *A = ExprPop(e);
+ int rc = JIM_OK;
+
+ switch (ExprBool(interp, A)) {
+ case 0:
+ ExprPush(e, Jim_NewIntObj(interp, 0));
+ break;
+
+ case 1:
+ ExprPush(e, Jim_NewIntObj(interp, 1));
+ break;
+
+ case -1:
+ /* Invalid */
+ rc = JIM_ERR;
+ break;
+ }
+ Jim_DecrRefCount(interp, A);
+
+ return rc;
+}
+
+static int JimExprOpTernaryLeft(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *skip = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+ int rc = JIM_OK;
+
+ /* Repush A */
+ ExprPush(e, A);
+
+ switch (ExprBool(interp, A)) {
+ case 0:
+ /* false, skip RHS opcodes */
+ e->skip = JimWideValue(skip);
+ /* Push a dummy value */
+ ExprPush(e, Jim_NewIntObj(interp, 0));
+ break;
+
+ case 1:
+ /* true so do nothing */
+ break;
+
+ case -1:
+ /* Invalid */
+ rc = JIM_ERR;
+ break;
+ }
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, skip);
+
+ return rc;
+}
+
+static int JimExprOpColonLeft(Jim_Interp *interp, struct JimExprState *e)
+{
+ Jim_Obj *skip = ExprPop(e);
+ Jim_Obj *B = ExprPop(e);
+ Jim_Obj *A = ExprPop(e);
+
+ /* No need to check for A as non-boolean */
+ if (ExprBool(interp, A)) {
+ /* true, so skip RHS opcodes */
+ e->skip = JimWideValue(skip);
+ /* Repush B as the answer */
+ ExprPush(e, B);
+ }
+
+ Jim_DecrRefCount(interp, skip);
+ Jim_DecrRefCount(interp, A);
+ Jim_DecrRefCount(interp, B);
+ return JIM_OK;
+}
+
+static int JimExprOpNull(Jim_Interp *interp, struct JimExprState *e)
+{
+ return JIM_OK;
+}
+
+enum
+{
+ LAZY_NONE,
+ LAZY_OP,
+ LAZY_LEFT,
+ LAZY_RIGHT
+};
+
+/* name - precedence - arity - opcode */
+static const struct Jim_ExprOperator Jim_ExprOperators[] = {
+ [JIM_EXPROP_FUNC_INT] = {"int", 400, 1, JimExprOpNumUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_DOUBLE] = {"double", 400, 1, JimExprOpNumUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_ABS] = {"abs", 400, 1, JimExprOpNumUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_ROUND] = {"round", 400, 1, JimExprOpNumUnary, LAZY_NONE},
+
+#ifdef JIM_MATH_FUNCTIONS
+ [JIM_EXPROP_FUNC_SIN] = {"sin", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_COS] = {"cos", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_TAN] = {"tan", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_ASIN] = {"asin", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_ACOS] = {"acos", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_ATAN] = {"atan", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_SINH] = {"sinh", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_COSH] = {"cosh", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_TANH] = {"tanh", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_CEIL] = {"ceil", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_FLOOR] = {"floor", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_EXP] = {"exp", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_LOG] = {"log", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_LOG10] = {"log10", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+ [JIM_EXPROP_FUNC_SQRT] = {"sqrt", 400, 1, JimExprOpDoubleUnary, LAZY_NONE},
+#endif
+
+ [JIM_EXPROP_NOT] = {"!", 300, 1, JimExprOpNumUnary, LAZY_NONE},
+ [JIM_EXPROP_BITNOT] = {"~", 300, 1, JimExprOpIntUnary, LAZY_NONE},
+ [JIM_EXPROP_UNARYMINUS] = {NULL, 300, 1, JimExprOpNumUnary, LAZY_NONE},
+ [JIM_EXPROP_UNARYPLUS] = {NULL, 300, 1, JimExprOpNumUnary, LAZY_NONE},
+
+ [JIM_EXPROP_POW] = {"**", 250, 2, JimExprOpBin, LAZY_NONE},
+
+ [JIM_EXPROP_MUL] = {"*", 200, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_DIV] = {"/", 200, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_MOD] = {"%", 200, 2, JimExprOpIntBin, LAZY_NONE},
+
+ [JIM_EXPROP_SUB] = {"-", 100, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_ADD] = {"+", 100, 2, JimExprOpBin, LAZY_NONE},
+
+ [JIM_EXPROP_ROTL] = {"<<<", 90, 2, JimExprOpIntBin, LAZY_NONE},
+ [JIM_EXPROP_ROTR] = {">>>", 90, 2, JimExprOpIntBin, LAZY_NONE},
+ [JIM_EXPROP_LSHIFT] = {"<<", 90, 2, JimExprOpIntBin, LAZY_NONE},
+ [JIM_EXPROP_RSHIFT] = {">>", 90, 2, JimExprOpIntBin, LAZY_NONE},
+
+ [JIM_EXPROP_LT] = {"<", 80, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_GT] = {">", 80, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_LTE] = {"<=", 80, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_GTE] = {">=", 80, 2, JimExprOpBin, LAZY_NONE},
+
+ [JIM_EXPROP_NUMEQ] = {"==", 70, 2, JimExprOpBin, LAZY_NONE},
+ [JIM_EXPROP_NUMNE] = {"!=", 70, 2, JimExprOpBin, LAZY_NONE},
+
+ [JIM_EXPROP_STREQ] = {"eq", 60, 2, JimExprOpStrBin, LAZY_NONE},
+ [JIM_EXPROP_STRNE] = {"ne", 60, 2, JimExprOpStrBin, LAZY_NONE},
+
+ [JIM_EXPROP_STRIN] = {"in", 55, 2, JimExprOpStrBin, LAZY_NONE},
+ [JIM_EXPROP_STRNI] = {"ni", 55, 2, JimExprOpStrBin, LAZY_NONE},
+
+ [JIM_EXPROP_BITAND] = {"&", 50, 2, JimExprOpIntBin, LAZY_NONE},
+ [JIM_EXPROP_BITXOR] = {"^", 49, 2, JimExprOpIntBin, LAZY_NONE},
+ [JIM_EXPROP_BITOR] = {"|", 48, 2, JimExprOpIntBin, LAZY_NONE},
+
+ [JIM_EXPROP_LOGICAND] = {"&&", 10, 2, NULL, LAZY_OP},
+ [JIM_EXPROP_LOGICOR] = {"||", 9, 2, NULL, LAZY_OP},
+
+ [JIM_EXPROP_TERNARY] = {"?", 5, 2, JimExprOpNull, LAZY_OP},
+ [JIM_EXPROP_COLON] = {":", 5, 2, JimExprOpNull, LAZY_OP},
+
+ /* private operators */
+ [JIM_EXPROP_TERNARY_LEFT] = {NULL, 5, 2, JimExprOpTernaryLeft, LAZY_LEFT},
+ [JIM_EXPROP_TERNARY_RIGHT] = {NULL, 5, 2, JimExprOpNull, LAZY_RIGHT},
+ [JIM_EXPROP_COLON_LEFT] = {NULL, 5, 2, JimExprOpColonLeft, LAZY_LEFT},
+ [JIM_EXPROP_COLON_RIGHT] = {NULL, 5, 2, JimExprOpNull, LAZY_RIGHT},
+ [JIM_EXPROP_LOGICAND_LEFT] = {NULL, 10, 2, JimExprOpAndLeft, LAZY_LEFT},
+ [JIM_EXPROP_LOGICAND_RIGHT] = {NULL, 10, 2, JimExprOpAndOrRight, LAZY_RIGHT},
+ [JIM_EXPROP_LOGICOR_LEFT] = {NULL, 9, 2, JimExprOpOrLeft, LAZY_LEFT},
+ [JIM_EXPROP_LOGICOR_RIGHT] = {NULL, 9, 2, JimExprOpAndOrRight, LAZY_RIGHT},
+};
+
+#define JIM_EXPR_OPERATORS_NUM \
+ (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator))
+
+static int JimParseExpression(struct JimParserCtx *pc)
+{
+ /* Discard spaces and quoted newline */
+ while (isspace(UCHAR(*pc->p)) || (*(pc->p) == '\\' && *(pc->p + 1) == '\n')) {
+ pc->p++;
+ pc->len--;
+ }
+
+ if (pc->len == 0) {
+ pc->tstart = pc->tend = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_EOL;
+ pc->eof = 1;
+ return JIM_OK;
+ }
+ switch (*(pc->p)) {
+ case '(':
+ pc->tstart = pc->tend = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_SUBEXPR_START;
+ pc->p++;
+ pc->len--;
+ break;
+ case ')':
+ pc->tstart = pc->tend = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_SUBEXPR_END;
+ pc->p++;
+ pc->len--;
+ break;
+ case '[':
+ return JimParseCmd(pc);
+ break;
+ case '$':
+ if (JimParseVar(pc) == JIM_ERR)
+ return JimParseExprOperator(pc);
+ else {
+ /* Don't allow expr sugar in expressions */
+ if (pc->tt == JIM_TT_EXPRSUGAR) {
+ return JIM_ERR;
+ }
+ return JIM_OK;
+ }
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '.':
+ return JimParseExprNumber(pc);
+ break;
+ case '"':
+ case '{':
+ /* Here it's possible to reuse the List String parsing. */
+ pc->tt = JIM_TT_NONE; /* Make sure it's sensed as a new word. */
+ return JimParseList(pc);
+ break;
+ case 'N':
+ case 'I':
+ case 'n':
+ case 'i':
+ if (JimParseExprIrrational(pc) == JIM_ERR)
+ return JimParseExprOperator(pc);
+ break;
+ default:
+ return JimParseExprOperator(pc);
+ break;
+ }
+ return JIM_OK;
+}
+
+static int JimParseExprNumber(struct JimParserCtx *pc)
+{
+ int allowdot = 1;
+ int allowhex = 0;
+
+ /* Assume an integer for now */
+ pc->tt = JIM_TT_EXPR_INT;
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (isdigit(UCHAR(*pc->p))
+ || (allowhex && isxdigit(UCHAR(*pc->p)))
+ || (allowdot && *pc->p == '.')
+ || (pc->p - pc->tstart == 1 && *pc->tstart == '0' && (*pc->p == 'x' || *pc->p == 'X'))
+ ) {
+ if ((*pc->p == 'x') || (*pc->p == 'X')) {
+ allowhex = 1;
+ allowdot = 0;
+ }
+ if (*pc->p == '.') {
+ allowdot = 0;
+ pc->tt = JIM_TT_EXPR_DOUBLE;
+ }
+ pc->p++;
+ pc->len--;
+ if (!allowhex && (*pc->p == 'e' || *pc->p == 'E') && (pc->p[1] == '-' || pc->p[1] == '+'
+ || isdigit(UCHAR(pc->p[1])))) {
+ pc->p += 2;
+ pc->len -= 2;
+ pc->tt = JIM_TT_EXPR_DOUBLE;
+ }
+ }
+ pc->tend = pc->p - 1;
+ return JIM_OK;
+}
+
+static int JimParseExprIrrational(struct JimParserCtx *pc)
+{
+ const char *Tokens[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL };
+ const char **token;
+
+ for (token = Tokens; *token != NULL; token++) {
+ int len = strlen(*token);
+
+ if (strncmp(*token, pc->p, len) == 0) {
+ pc->tstart = pc->p;
+ pc->tend = pc->p + len - 1;
+ pc->p += len;
+ pc->len -= len;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_EXPR_DOUBLE;
+ return JIM_OK;
+ }
+ }
+ return JIM_ERR;
+}
+
+static int JimParseExprOperator(struct JimParserCtx *pc)
+{
+ int i;
+ int bestIdx = -1, bestLen = 0;
+
+ /* Try to get the longest match. */
+ for (i = JIM_TT_EXPR_OP; i < (signed)JIM_EXPR_OPERATORS_NUM; i++) {
+ const char *opname;
+ int oplen;
+
+ opname = Jim_ExprOperators[i].name;
+ if (opname == NULL) {
+ continue;
+ }
+ oplen = strlen(opname);
+
+ if (strncmp(opname, pc->p, oplen) == 0 && oplen > bestLen) {
+ bestIdx = i;
+ bestLen = oplen;
+ }
+ }
+ if (bestIdx == -1) {
+ return JIM_ERR;
+ }
+
+ /* Validate paretheses around function arguments */
+ if (bestIdx >= JIM_EXPROP_FUNC_FIRST) {
+ const char *p = pc->p + bestLen;
+ int len = pc->len - bestLen;
+
+ while (len && isspace(UCHAR(*p))) {
+ len--;
+ p++;
+ }
+ if (*p != '(') {
+ return JIM_ERR;
+ }
+ }
+ pc->tstart = pc->p;
+ pc->tend = pc->p + bestLen - 1;
+ pc->p += bestLen;
+ pc->len -= bestLen;
+ pc->tline = pc->linenr;
+
+ pc->tt = bestIdx;
+ return JIM_OK;
+}
+
+static const struct Jim_ExprOperator *JimExprOperatorInfoByOpcode(int opcode)
+{
+ return &Jim_ExprOperators[opcode];
+}
+
+const char *jim_tt_name(int type)
+{
+ static const char * const tt_names[JIM_TT_EXPR_OP] =
+ { "NIL", "STR", "ESC", "VAR", "ARY", "CMD", "SEP", "EOL", "EOF", "LIN", "WRD", "(((", ")))", "INT",
+ "DBL", "$()" };
+ if (type < JIM_TT_EXPR_OP) {
+ return tt_names[type];
+ }
+ else {
+ const struct Jim_ExprOperator *op = JimExprOperatorInfoByOpcode(type);
+ static char buf[20];
+
+ if (op && op->name) {
+ return op->name;
+ }
+ sprintf(buf, "(%d)", type);
+ return buf;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ * Expression Object
+ * ---------------------------------------------------------------------------*/
+static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr);
+
+static const Jim_ObjType exprObjType = {
+ "expression",
+ FreeExprInternalRep,
+ DupExprInternalRep,
+ NULL,
+ JIM_TYPE_REFERENCES,
+};
+
+/* Expr bytecode structure */
+typedef struct ExprByteCode
+{
+ int len; /* Length as number of tokens. */
+ ScriptToken *token; /* Tokens array. */
+ int inUse; /* Used for sharing. */
+} ExprByteCode;
+
+static void ExprFreeByteCode(Jim_Interp *interp, ExprByteCode * expr)
+{
+ int i;
+
+ for (i = 0; i < expr->len; i++) {
+ Jim_DecrRefCount(interp, expr->token[i].objPtr);
+ }
+ Jim_Free(expr->token);
+ Jim_Free(expr);
+}
+
+static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ ExprByteCode *expr = (void *)objPtr->internalRep.ptr;
+
+ if (expr) {
+ if (--expr->inUse != 0) {
+ return;
+ }
+
+ ExprFreeByteCode(interp, expr);
+ }
+}
+
+static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ JIM_NOTUSED(interp);
+ JIM_NOTUSED(srcPtr);
+
+ /* Just returns an simple string. */
+ dupPtr->typePtr = NULL;
+}
+
+/* Check if an expr program looks correct. */
+static int ExprCheckCorrectness(ExprByteCode * expr)
+{
+ int i;
+ int stacklen = 0;
+ int ternary = 0;
+
+ /* Try to check if there are stack underflows,
+ * and make sure at the end of the program there is
+ * a single result on the stack. */
+ for (i = 0; i < expr->len; i++) {
+ ScriptToken *t = &expr->token[i];
+ const struct Jim_ExprOperator *op = JimExprOperatorInfoByOpcode(t->type);
+
+ if (op) {
+ stacklen -= op->arity;
+ if (stacklen < 0) {
+ break;
+ }
+ if (t->type == JIM_EXPROP_TERNARY || t->type == JIM_EXPROP_TERNARY_LEFT) {
+ ternary++;
+ }
+ else if (t->type == JIM_EXPROP_COLON || t->type == JIM_EXPROP_COLON_LEFT) {
+ ternary--;
+ }
+ }
+
+ /* All operations and operands add one to the stack */
+ stacklen++;
+ }
+ if (stacklen != 1 || ternary != 0) {
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* This procedure converts every occurrence of || and && opereators
+ * in lazy unary versions.
+ *
+ * a b || is converted into:
+ *
+ * a <offset> |L b |R
+ *
+ * a b && is converted into:
+ *
+ * a <offset> &L b &R
+ *
+ * "|L" checks if 'a' is true:
+ * 1) if it is true pushes 1 and skips <offset> instructions to reach
+ * the opcode just after |R.
+ * 2) if it is false does nothing.
+ * "|R" checks if 'b' is true:
+ * 1) if it is true pushes 1, otherwise pushes 0.
+ *
+ * "&L" checks if 'a' is true:
+ * 1) if it is true does nothing.
+ * 2) If it is false pushes 0 and skips <offset> instructions to reach
+ * the opcode just after &R
+ * "&R" checks if 'a' is true:
+ * if it is true pushes 1, otherwise pushes 0.
+ */
+static int ExprAddLazyOperator(Jim_Interp *interp, ExprByteCode * expr, ParseToken *t)
+{
+ int i;
+
+ int leftindex, arity, offset;
+
+ /* Search for the end of the first operator */
+ leftindex = expr->len - 1;
+
+ arity = 1;
+ while (arity) {
+ ScriptToken *tt = &expr->token[leftindex];
+
+ if (tt->type >= JIM_TT_EXPR_OP) {
+ arity += JimExprOperatorInfoByOpcode(tt->type)->arity;
+ }
+ arity--;
+ if (--leftindex < 0) {
+ return JIM_ERR;
+ }
+ }
+ leftindex++;
+
+ /* Move them up */
+ memmove(&expr->token[leftindex + 2], &expr->token[leftindex],
+ sizeof(*expr->token) * (expr->len - leftindex));
+ expr->len += 2;
+ offset = (expr->len - leftindex) - 1;
+
+ /* Now we rely on the fact the the left and right version have opcodes
+ * 1 and 2 after the main opcode respectively
+ */
+ expr->token[leftindex + 1].type = t->type + 1;
+ expr->token[leftindex + 1].objPtr = interp->emptyObj;
+
+ expr->token[leftindex].type = JIM_TT_EXPR_INT;
+ expr->token[leftindex].objPtr = Jim_NewIntObj(interp, offset);
+
+ /* Now add the 'R' operator */
+ expr->token[expr->len].objPtr = interp->emptyObj;
+ expr->token[expr->len].type = t->type + 2;
+ expr->len++;
+
+ /* Do we need to adjust the skip count for any &L, |L, ?L or :L in the left operand? */
+ for (i = leftindex - 1; i > 0; i--) {
+ if (JimExprOperatorInfoByOpcode(expr->token[i].type)->lazy == LAZY_LEFT) {
+ if (JimWideValue(expr->token[i - 1].objPtr) + i - 1 >= leftindex) {
+ JimWideValue(expr->token[i - 1].objPtr) += 2;
+ }
+ }
+ }
+ return JIM_OK;
+}
+
+static int ExprAddOperator(Jim_Interp *interp, ExprByteCode * expr, ParseToken *t)
+{
+ struct ScriptToken *token = &expr->token[expr->len];
+
+ if (JimExprOperatorInfoByOpcode(t->type)->lazy == LAZY_OP) {
+ return ExprAddLazyOperator(interp, expr, t);
+ }
+ else {
+ token->objPtr = interp->emptyObj;
+ token->type = t->type;
+ expr->len++;
+ return JIM_OK;
+ }
+}
+
+/**
+ * Returns the index of the COLON_LEFT to the left of 'right_index'
+ * taking into account nesting.
+ *
+ * The expression *must* be well formed, thus a COLON_LEFT will always be found.
+ */
+static int ExprTernaryGetColonLeftIndex(ExprByteCode *expr, int right_index)
+{
+ int ternary_count = 1;
+
+ right_index--;
+
+ while (right_index > 1) {
+ if (expr->token[right_index].type == JIM_EXPROP_TERNARY_LEFT) {
+ ternary_count--;
+ }
+ else if (expr->token[right_index].type == JIM_EXPROP_COLON_RIGHT) {
+ ternary_count++;
+ }
+ else if (expr->token[right_index].type == JIM_EXPROP_COLON_LEFT && ternary_count == 1) {
+ return right_index;
+ }
+ right_index--;
+ }
+
+ /*notreached*/
+ return -1;
+}
+
+/**
+ * Find the left/right indices for the ternary expression to the left of 'right_index'.
+ *
+ * Returns 1 if found, and fills in *prev_right_index and *prev_left_index.
+ * Otherwise returns 0.
+ */
+static int ExprTernaryGetMoveIndices(ExprByteCode *expr, int right_index, int *prev_right_index, int *prev_left_index)
+{
+ int i = right_index - 1;
+ int ternary_count = 1;
+
+ while (i > 1) {
+ if (expr->token[i].type == JIM_EXPROP_TERNARY_LEFT) {
+ if (--ternary_count == 0 && expr->token[i - 2].type == JIM_EXPROP_COLON_RIGHT) {
+ *prev_right_index = i - 2;
+ *prev_left_index = ExprTernaryGetColonLeftIndex(expr, *prev_right_index);
+ return 1;
+ }
+ }
+ else if (expr->token[i].type == JIM_EXPROP_COLON_RIGHT) {
+ if (ternary_count == 0) {
+ return 0;
+ }
+ ternary_count++;
+ }
+ i--;
+ }
+ return 0;
+}
+
+/*
+* ExprTernaryReorderExpression description
+* ========================================
+*
+* ?: is right-to-left associative which doesn't work with the stack-based
+* expression engine. The fix is to reorder the bytecode.
+*
+* The expression:
+*
+* expr 1?2:0?3:4
+*
+* Has initial bytecode:
+*
+* '1' '2' (40=TERNARY_LEFT) '2' (41=TERNARY_RIGHT) '2' (43=COLON_LEFT) '0' (44=COLON_RIGHT)
+* '2' (40=TERNARY_LEFT) '3' (41=TERNARY_RIGHT) '2' (43=COLON_LEFT) '4' (44=COLON_RIGHT)
+*
+* The fix involves simulating this expression instead:
+*
+* expr 1?2:(0?3:4)
+*
+* With the following bytecode:
+*
+* '1' '2' (40=TERNARY_LEFT) '2' (41=TERNARY_RIGHT) '10' (43=COLON_LEFT) '0' '2' (40=TERNARY_LEFT)
+* '3' (41=TERNARY_RIGHT) '2' (43=COLON_LEFT) '4' (44=COLON_RIGHT) (44=COLON_RIGHT)
+*
+* i.e. The token COLON_RIGHT at index 8 is moved towards the end of the stack, all tokens above 8
+* are shifted down and the skip count of the token JIM_EXPROP_COLON_LEFT at index 5 is
+* incremented by the amount tokens shifted down. The token JIM_EXPROP_COLON_RIGHT that is moved
+* is identified as immediately preceeding a token JIM_EXPROP_TERNARY_LEFT
+*
+* ExprTernaryReorderExpression works thus as follows :
+* - start from the end of the stack
+* - while walking towards the beginning of the stack
+* if token=JIM_EXPROP_COLON_RIGHT then
+* find the associated token JIM_EXPROP_TERNARY_LEFT, which allows to
+* find the associated token previous(JIM_EXPROP_COLON_RIGHT)
+* find the associated token previous(JIM_EXPROP_LEFT_RIGHT)
+* if all found then
+* perform the rotation
+* update the skip count of the token previous(JIM_EXPROP_LEFT_RIGHT)
+* end if
+* end if
+*
+* Note: care has to be taken for nested ternary constructs!!!
+*/
+static void ExprTernaryReorderExpression(Jim_Interp *interp, ExprByteCode *expr)
+{
+ int i;
+
+ for (i = expr->len - 1; i > 1; i--) {
+ int prev_right_index;
+ int prev_left_index;
+ int j;
+ ScriptToken tmp;
+
+ if (expr->token[i].type != JIM_EXPROP_COLON_RIGHT) {
+ continue;
+ }
+
+ /* COLON_RIGHT found: get the indexes needed to move the tokens in the stack (if any) */
+ if (ExprTernaryGetMoveIndices(expr, i, &prev_right_index, &prev_left_index) == 0) {
+ continue;
+ }
+
+ /*
+ ** rotate tokens down
+ **
+ ** +-> [i] : JIM_EXPROP_COLON_RIGHT
+ ** | | |
+ ** | V V
+ ** | [...] : ...
+ ** | | |
+ ** | V V
+ ** | [...] : ...
+ ** | | |
+ ** | V V
+ ** +- [prev_right_index] : JIM_EXPROP_COLON_RIGHT
+ */
+ tmp = expr->token[prev_right_index];
+ for (j = prev_right_index; j < i; j++) {
+ expr->token[j] = expr->token[j + 1];
+ }
+ expr->token[i] = tmp;
+
+ /* Increment the 'skip' count associated to the previous JIM_EXPROP_COLON_LEFT token
+ *
+ * This is 'colon left increment' = i - prev_right_index
+ *
+ * [prev_left_index] : JIM_EXPROP_LEFT_RIGHT
+ * [prev_left_index-1] : skip_count
+ *
+ */
+ JimWideValue(expr->token[prev_left_index-1].objPtr) += (i - prev_right_index);
+
+ /* Adjust for i-- in the loop */
+ i++;
+ }
+}
+
+static ExprByteCode *ExprCreateByteCode(Jim_Interp *interp, const ParseTokenList *tokenlist)
+{
+ Jim_Stack stack;
+ ExprByteCode *expr;
+ int ok = 1;
+ int i;
+ int prevtt = JIM_TT_NONE;
+ int have_ternary = 0;
+
+ /* -1 for EOL */
+ int count = tokenlist->count - 1;
+
+ expr = Jim_Alloc(sizeof(*expr));
+ expr->inUse = 1;
+ expr->len = 0;
+
+ Jim_InitStack(&stack);
+
+ /* Need extra bytecodes for lazy operators.
+ * Also check for the ternary operator
+ */
+ for (i = 0; i < tokenlist->count; i++) {
+ ParseToken *t = &tokenlist->list[i];
+
+ if (JimExprOperatorInfoByOpcode(t->type)->lazy == LAZY_OP) {
+ count += 2;
+ /* Ternary is a lazy op but also needs reordering */
+ if (t->type == JIM_EXPROP_TERNARY) {
+ have_ternary = 1;
+ }
+ }
+ }
+
+ expr->token = Jim_Alloc(sizeof(ScriptToken) * count);
+
+ for (i = 0; i < tokenlist->count && ok; i++) {
+ ParseToken *t = &tokenlist->list[i];
+
+ /* Next token will be stored here */
+ struct ScriptToken *token = &expr->token[expr->len];
+
+ if (t->type == JIM_TT_EOL) {
+ break;
+ }
+
+ switch (t->type) {
+ case JIM_TT_STR:
+ case JIM_TT_ESC:
+ case JIM_TT_VAR:
+ case JIM_TT_DICTSUGAR:
+ case JIM_TT_EXPRSUGAR:
+ case JIM_TT_CMD:
+ token->objPtr = Jim_NewStringObj(interp, t->token, t->len);
+ token->type = t->type;
+ expr->len++;
+ break;
+
+ case JIM_TT_EXPR_INT:
+ token->objPtr = Jim_NewIntObj(interp, strtoull(t->token, NULL, 0));
+ token->type = t->type;
+ expr->len++;
+ break;
+
+ case JIM_TT_EXPR_DOUBLE:
+ token->objPtr = Jim_NewDoubleObj(interp, strtod(t->token, NULL));
+ token->type = t->type;
+ expr->len++;
+ break;
+
+ case JIM_TT_SUBEXPR_START:
+ Jim_StackPush(&stack, t);
+ prevtt = JIM_TT_NONE;
+ continue;
+
+ case JIM_TT_SUBEXPR_END:
+ ok = 0;
+ while (Jim_StackLen(&stack)) {
+ ParseToken *tt = Jim_StackPop(&stack);
+
+ if (tt->type == JIM_TT_SUBEXPR_START) {
+ ok = 1;
+ break;
+ }
+
+ if (ExprAddOperator(interp, expr, tt) != JIM_OK) {
+ goto err;
+ }
+ }
+ if (!ok) {
+ Jim_SetResultString(interp, "Unexpected close parenthesis", -1);
+ goto err;
+ }
+ break;
+
+
+ default:{
+ /* Must be an operator */
+ const struct Jim_ExprOperator *op;
+ ParseToken *tt;
+
+ /* Convert -/+ to unary minus or unary plus if necessary */
+ if (prevtt == JIM_TT_NONE || prevtt >= JIM_TT_EXPR_OP) {
+ if (t->type == JIM_EXPROP_SUB) {
+ t->type = JIM_EXPROP_UNARYMINUS;
+ }
+ else if (t->type == JIM_EXPROP_ADD) {
+ t->type = JIM_EXPROP_UNARYPLUS;
+ }
+ }
+
+ op = JimExprOperatorInfoByOpcode(t->type);
+
+ /* Now handle precedence */
+ while ((tt = Jim_StackPeek(&stack)) != NULL) {
+ const struct Jim_ExprOperator *tt_op =
+ JimExprOperatorInfoByOpcode(tt->type);
+
+ /* Note that right-to-left associativity of ?: operator is handled later */
+
+ if (op->arity != 1 && tt_op->precedence >= op->precedence) {
+ if (ExprAddOperator(interp, expr, tt) != JIM_OK) {
+ ok = 0;
+ goto err;
+ }
+ Jim_StackPop(&stack);
+ }
+ else {
+ break;
+ }
+ }
+ Jim_StackPush(&stack, t);
+ break;
+ }
+ }
+ prevtt = t->type;
+ }
+
+ /* Reduce any remaining subexpr */
+ while (Jim_StackLen(&stack)) {
+ ParseToken *tt = Jim_StackPop(&stack);
+
+ if (tt->type == JIM_TT_SUBEXPR_START) {
+ ok = 0;
+ Jim_SetResultString(interp, "Missing close parenthesis", -1);
+ goto err;
+ }
+ if (ExprAddOperator(interp, expr, tt) != JIM_OK) {
+ ok = 0;
+ goto err;
+ }
+ }
+
+ if (have_ternary) {
+ ExprTernaryReorderExpression(interp, expr);
+ }
+
+ err:
+ /* Free the stack used for the compilation. */
+ Jim_FreeStack(&stack);
+
+ for (i = 0; i < expr->len; i++) {
+ Jim_IncrRefCount(expr->token[i].objPtr);
+ }
+
+ if (!ok) {
+ ExprFreeByteCode(interp, expr);
+ return NULL;
+ }
+
+ return expr;
+}
+
+
+/* This method takes the string representation of an expression
+ * and generates a program for the Expr's stack-based VM. */
+int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr)
+{
+ int exprTextLen;
+ const char *exprText;
+ struct JimParserCtx parser;
+ struct ExprByteCode *expr;
+ ParseTokenList tokenlist;
+ int rc = JIM_ERR;
+
+ exprText = Jim_GetString(objPtr, &exprTextLen);
+
+ /* Initially tokenise the expression into tokenlist */
+ ScriptTokenListInit(&tokenlist);
+
+ JimParserInit(&parser, exprText, exprTextLen, 0);
+ while (!JimParserEof(&parser)) {
+ if (JimParseExpression(&parser) != JIM_OK) {
+ ScriptTokenListFree(&tokenlist);
+ invalidexpr:
+ Jim_SetResultFormatted(interp, "syntax error in expression: \"%#s\"", objPtr);
+ expr = NULL;
+ goto err;
+ }
+
+ ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt,
+ parser.tline);
+ }
+
+#ifdef DEBUG_SHOW_EXPR_TOKENS
+ {
+ int i;
+ printf("==== Expr Tokens ====\n");
+ for (i = 0; i < tokenlist.count; i++) {
+ printf("[%2d]@%d %s '%.*s'\n", i, tokenlist.list[i].line, jim_tt_name(tokenlist.list[i].type),
+ tokenlist.list[i].len, tokenlist.list[i].token);
+ }
+ }
+#endif
+
+ /* Now create the expression bytecode from the tokenlist */
+ expr = ExprCreateByteCode(interp, &tokenlist);
+
+ /* No longer need the token list */
+ ScriptTokenListFree(&tokenlist);
+
+ if (!expr) {
+ goto err;
+ }
+
+#ifdef DEBUG_SHOW_EXPR
+ {
+ int i;
+
+ printf("==== Expr ====\n");
+ for (i = 0; i < expr->len; i++) {
+ ScriptToken *t = &expr->token[i];
+
+ printf("[%2d] %s '%s'\n", i, jim_tt_name(t->type), Jim_String(t->objPtr));
+ }
+ }
+#endif
+
+ /* Check program correctness. */
+ if (ExprCheckCorrectness(expr) != JIM_OK) {
+ ExprFreeByteCode(interp, expr);
+ goto invalidexpr;
+ }
+
+ rc = JIM_OK;
+
+ err:
+ /* Free the old internal rep and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ Jim_SetIntRepPtr(objPtr, expr);
+ objPtr->typePtr = &exprObjType;
+ return rc;
+}
+
+static ExprByteCode *JimGetExpression(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ if (objPtr->typePtr != &exprObjType) {
+ if (SetExprFromAny(interp, objPtr) != JIM_OK) {
+ return NULL;
+ }
+ }
+ return (ExprByteCode *) Jim_GetIntRepPtr(objPtr);
+}
+
+/* -----------------------------------------------------------------------------
+ * Expressions evaluation.
+ * Jim uses a specialized stack-based virtual machine for expressions,
+ * that takes advantage of the fact that expr's operators
+ * can't be redefined.
+ *
+ * Jim_EvalExpression() uses the bytecode compiled by
+ * SetExprFromAny() method of the "expression" object.
+ *
+ * On success a Tcl Object containing the result of the evaluation
+ * is stored into expResultPtrPtr (having refcount of 1), and JIM_OK is
+ * returned.
+ * On error the function returns a retcode != to JIM_OK and set a suitable
+ * error on the interp.
+ * ---------------------------------------------------------------------------*/
+#define JIM_EE_STATICSTACK_LEN 10
+
+int Jim_EvalExpression(Jim_Interp *interp, Jim_Obj *exprObjPtr, Jim_Obj **exprResultPtrPtr)
+{
+ ExprByteCode *expr;
+ Jim_Obj *staticStack[JIM_EE_STATICSTACK_LEN];
+ int i;
+ int retcode = JIM_OK;
+ struct JimExprState e;
+
+ expr = JimGetExpression(interp, exprObjPtr);
+ if (!expr) {
+ return JIM_ERR; /* error in expression. */
+ }
+
+#ifdef JIM_OPTIMIZATION
+ /* Check for one of the following common expressions used by while/for
+ *
+ * CONST
+ * $a
+ * !$a
+ * $a < CONST, $a < $b
+ * $a <= CONST, $a <= $b
+ * $a > CONST, $a > $b
+ * $a >= CONST, $a >= $b
+ * $a != CONST, $a != $b
+ * $a == CONST, $a == $b
+ */
+ {
+ Jim_Obj *objPtr;
+
+ /* STEP 1 -- Check if there are the conditions to run the specialized
+ * version of while */
+
+ switch (expr->len) {
+ case 1:
+ if (expr->token[0].type == JIM_TT_EXPR_INT) {
+ *exprResultPtrPtr = expr->token[0].objPtr;
+ Jim_IncrRefCount(*exprResultPtrPtr);
+ return JIM_OK;
+ }
+ if (expr->token[0].type == JIM_TT_VAR) {
+ objPtr = Jim_GetVariable(interp, expr->token[0].objPtr, JIM_ERRMSG);
+ if (objPtr) {
+ *exprResultPtrPtr = objPtr;
+ Jim_IncrRefCount(*exprResultPtrPtr);
+ return JIM_OK;
+ }
+ }
+ break;
+
+ case 2:
+ if (expr->token[1].type == JIM_EXPROP_NOT && expr->token[0].type == JIM_TT_VAR) {
+ jim_wide wideValue;
+
+ objPtr = Jim_GetVariable(interp, expr->token[0].objPtr, JIM_NONE);
+ if (objPtr && JimIsWide(objPtr)
+ && Jim_GetWide(interp, objPtr, &wideValue) == JIM_OK) {
+ *exprResultPtrPtr = wideValue ? interp->falseObj : interp->trueObj;
+ Jim_IncrRefCount(*exprResultPtrPtr);
+ return JIM_OK;
+ }
+ }
+ break;
+
+ case 3:
+ if (expr->token[0].type == JIM_TT_VAR && (expr->token[1].type == JIM_TT_EXPR_INT
+ || expr->token[1].type == JIM_TT_VAR)) {
+ switch (expr->token[2].type) {
+ case JIM_EXPROP_LT:
+ case JIM_EXPROP_LTE:
+ case JIM_EXPROP_GT:
+ case JIM_EXPROP_GTE:
+ case JIM_EXPROP_NUMEQ:
+ case JIM_EXPROP_NUMNE:{
+ /* optimise ok */
+ jim_wide wideValueA;
+ jim_wide wideValueB;
+
+ objPtr = Jim_GetVariable(interp, expr->token[0].objPtr, JIM_NONE);
+ if (objPtr && JimIsWide(objPtr)
+ && Jim_GetWide(interp, objPtr, &wideValueA) == JIM_OK) {
+ if (expr->token[1].type == JIM_TT_VAR) {
+ objPtr =
+ Jim_GetVariable(interp, expr->token[1].objPtr,
+ JIM_NONE);
+ }
+ else {
+ objPtr = expr->token[1].objPtr;
+ }
+ if (objPtr && JimIsWide(objPtr)
+ && Jim_GetWide(interp, objPtr, &wideValueB) == JIM_OK) {
+ int cmpRes;
+
+ switch (expr->token[2].type) {
+ case JIM_EXPROP_LT:
+ cmpRes = wideValueA < wideValueB;
+ break;
+ case JIM_EXPROP_LTE:
+ cmpRes = wideValueA <= wideValueB;
+ break;
+ case JIM_EXPROP_GT:
+ cmpRes = wideValueA > wideValueB;
+ break;
+ case JIM_EXPROP_GTE:
+ cmpRes = wideValueA >= wideValueB;
+ break;
+ case JIM_EXPROP_NUMEQ:
+ cmpRes = wideValueA == wideValueB;
+ break;
+ case JIM_EXPROP_NUMNE:
+ cmpRes = wideValueA != wideValueB;
+ break;
+ default: /*notreached */
+ cmpRes = 0;
+ }
+ *exprResultPtrPtr =
+ cmpRes ? interp->trueObj : interp->falseObj;
+ Jim_IncrRefCount(*exprResultPtrPtr);
+ return JIM_OK;
+ }
+ }
+ }
+ }
+ }
+ break;
+ }
+ }
+#endif
+
+ /* In order to avoid that the internal repr gets freed due to
+ * shimmering of the exprObjPtr's object, we make the internal rep
+ * shared. */
+ expr->inUse++;
+
+ /* The stack-based expr VM itself */
+
+ /* Stack allocation. Expr programs have the feature that
+ * a program of length N can't require a stack longer than
+ * N. */
+ if (expr->len > JIM_EE_STATICSTACK_LEN)
+ e.stack = Jim_Alloc(sizeof(Jim_Obj *) * expr->len);
+ else
+ e.stack = staticStack;
+
+ e.stacklen = 0;
+
+ /* Execute every instruction */
+ for (i = 0; i < expr->len && retcode == JIM_OK; i++) {
+ Jim_Obj *objPtr;
+
+ switch (expr->token[i].type) {
+ case JIM_TT_EXPR_INT:
+ case JIM_TT_EXPR_DOUBLE:
+ case JIM_TT_STR:
+ ExprPush(&e, expr->token[i].objPtr);
+ break;
+
+ case JIM_TT_VAR:
+ objPtr = Jim_GetVariable(interp, expr->token[i].objPtr, JIM_ERRMSG);
+ if (objPtr) {
+ ExprPush(&e, objPtr);
+ }
+ else {
+ retcode = JIM_ERR;
+ }
+ break;
+
+ case JIM_TT_DICTSUGAR:
+ objPtr = JimExpandDictSugar(interp, expr->token[i].objPtr);
+ if (objPtr) {
+ ExprPush(&e, objPtr);
+ }
+ else {
+ retcode = JIM_ERR;
+ }
+ break;
+
+ case JIM_TT_ESC:
+ retcode = Jim_SubstObj(interp, expr->token[i].objPtr, &objPtr, JIM_NONE);
+ if (retcode == JIM_OK) {
+ ExprPush(&e, objPtr);
+ }
+ break;
+
+ case JIM_TT_CMD:
+ retcode = Jim_EvalObj(interp, expr->token[i].objPtr);
+ if (retcode == JIM_OK) {
+ ExprPush(&e, Jim_GetResult(interp));
+ }
+ break;
+
+ default:{
+ /* Find and execute the operation */
+ e.skip = 0;
+ e.opcode = expr->token[i].type;
+
+ retcode = JimExprOperatorInfoByOpcode(e.opcode)->funcop(interp, &e);
+ /* Skip some opcodes if necessary */
+ i += e.skip;
+ continue;
+ }
+ }
+ }
+
+ expr->inUse--;
+
+ if (retcode == JIM_OK) {
+ *exprResultPtrPtr = ExprPop(&e);
+ }
+ else {
+ for (i = 0; i < e.stacklen; i++) {
+ Jim_DecrRefCount(interp, e.stack[i]);
+ }
+ }
+ if (e.stack != staticStack) {
+ Jim_Free(e.stack);
+ }
+ return retcode;
+}
+
+int Jim_GetBoolFromExpr(Jim_Interp *interp, Jim_Obj *exprObjPtr, int *boolPtr)
+{
+ int retcode;
+ jim_wide wideValue;
+ double doubleValue;
+ Jim_Obj *exprResultPtr;
+
+ retcode = Jim_EvalExpression(interp, exprObjPtr, &exprResultPtr);
+ if (retcode != JIM_OK)
+ return retcode;
+
+ if (JimGetWideNoErr(interp, exprResultPtr, &wideValue) != JIM_OK) {
+ if (Jim_GetDouble(interp, exprResultPtr, &doubleValue) != JIM_OK) {
+ Jim_DecrRefCount(interp, exprResultPtr);
+ return JIM_ERR;
+ }
+ else {
+ Jim_DecrRefCount(interp, exprResultPtr);
+ *boolPtr = doubleValue != 0;
+ return JIM_OK;
+ }
+ }
+ *boolPtr = wideValue != 0;
+
+ Jim_DecrRefCount(interp, exprResultPtr);
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * ScanFormat String Object
+ * ---------------------------------------------------------------------------*/
+
+/* This Jim_Obj will held a parsed representation of a format string passed to
+ * the Jim_ScanString command. For error diagnostics, the scanformat string has
+ * to be parsed in its entirely first and then, if correct, can be used for
+ * scanning. To avoid endless re-parsing, the parsed representation will be
+ * stored in an internal representation and re-used for performance reason. */
+
+/* A ScanFmtPartDescr will held the information of /one/ part of the whole
+ * scanformat string. This part will later be used to extract information
+ * out from the string to be parsed by Jim_ScanString */
+
+typedef struct ScanFmtPartDescr
+{
+ char type; /* Type of conversion (e.g. c, d, f) */
+ char modifier; /* Modify type (e.g. l - long, h - short */
+ size_t width; /* Maximal width of input to be converted */
+ int pos; /* -1 - no assign, 0 - natural pos, >0 - XPG3 pos */
+ char *arg; /* Specification of a CHARSET conversion */
+ char *prefix; /* Prefix to be scanned literally before conversion */
+} ScanFmtPartDescr;
+
+/* The ScanFmtStringObj will hold the internal representation of a scanformat
+ * string parsed and separated in part descriptions. Furthermore it contains
+ * the original string representation of the scanformat string to allow for
+ * fast update of the Jim_Obj's string representation part.
+ *
+ * As an add-on the internal object representation adds some scratch pad area
+ * for usage by Jim_ScanString to avoid endless allocating and freeing of
+ * memory for purpose of string scanning.
+ *
+ * The error member points to a static allocated string in case of a mal-
+ * formed scanformat string or it contains '0' (NULL) in case of a valid
+ * parse representation.
+ *
+ * The whole memory of the internal representation is allocated as a single
+ * area of memory that will be internally separated. So freeing and duplicating
+ * of such an object is cheap */
+
+typedef struct ScanFmtStringObj
+{
+ jim_wide size; /* Size of internal repr in bytes */
+ char *stringRep; /* Original string representation */
+ size_t count; /* Number of ScanFmtPartDescr contained */
+ size_t convCount; /* Number of conversions that will assign */
+ size_t maxPos; /* Max position index if XPG3 is used */
+ const char *error; /* Ptr to error text (NULL if no error */
+ char *scratch; /* Some scratch pad used by Jim_ScanString */
+ ScanFmtPartDescr descr[1]; /* The vector of partial descriptions */
+} ScanFmtStringObj;
+
+
+static void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr);
+static void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr);
+static void UpdateStringOfScanFmt(Jim_Obj *objPtr);
+
+static const Jim_ObjType scanFmtStringObjType = {
+ "scanformatstring",
+ FreeScanFmtInternalRep,
+ DupScanFmtInternalRep,
+ UpdateStringOfScanFmt,
+ JIM_TYPE_NONE,
+};
+
+void FreeScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ JIM_NOTUSED(interp);
+ Jim_Free((char *)objPtr->internalRep.ptr);
+ objPtr->internalRep.ptr = 0;
+}
+
+void DupScanFmtInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr)
+{
+ size_t size = (size_t) ((ScanFmtStringObj *) srcPtr->internalRep.ptr)->size;
+ ScanFmtStringObj *newVec = (ScanFmtStringObj *) Jim_Alloc(size);
+
+ JIM_NOTUSED(interp);
+ memcpy(newVec, srcPtr->internalRep.ptr, size);
+ dupPtr->internalRep.ptr = newVec;
+ dupPtr->typePtr = &scanFmtStringObjType;
+}
+
+void UpdateStringOfScanFmt(Jim_Obj *objPtr)
+{
+ char *bytes = ((ScanFmtStringObj *) objPtr->internalRep.ptr)->stringRep;
+
+ objPtr->bytes = Jim_StrDup(bytes);
+ objPtr->length = strlen(bytes);
+}
+
+/* SetScanFmtFromAny will parse a given string and create the internal
+ * representation of the format specification. In case of an error
+ * the error data member of the internal representation will be set
+ * to an descriptive error text and the function will be left with
+ * JIM_ERR to indicate unsucessful parsing (aka. malformed scanformat
+ * specification */
+
+static int SetScanFmtFromAny(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ ScanFmtStringObj *fmtObj;
+ char *buffer;
+ int maxCount, i, approxSize, lastPos = -1;
+ const char *fmt = objPtr->bytes;
+ int maxFmtLen = objPtr->length;
+ const char *fmtEnd = fmt + maxFmtLen;
+ int curr;
+
+ Jim_FreeIntRep(interp, objPtr);
+ /* Count how many conversions could take place maximally */
+ for (i = 0, maxCount = 0; i < maxFmtLen; ++i)
+ if (fmt[i] == '%')
+ ++maxCount;
+ /* Calculate an approximation of the memory necessary */
+ approxSize = sizeof(ScanFmtStringObj) /* Size of the container */
+ +(maxCount + 1) * sizeof(ScanFmtPartDescr) /* Size of all partials */
+ +maxFmtLen * sizeof(char) + 3 + 1 /* Scratch + "%n" + '\0' */
+ + maxFmtLen * sizeof(char) + 1 /* Original stringrep */
+ + maxFmtLen * sizeof(char) /* Arg for CHARSETs */
+ +(maxCount + 1) * sizeof(char) /* '\0' for every partial */
+ +1; /* safety byte */
+ fmtObj = (ScanFmtStringObj *) Jim_Alloc(approxSize);
+ memset(fmtObj, 0, approxSize);
+ fmtObj->size = approxSize;
+ fmtObj->maxPos = 0;
+ fmtObj->scratch = (char *)&fmtObj->descr[maxCount + 1];
+ fmtObj->stringRep = fmtObj->scratch + maxFmtLen + 3 + 1;
+ memcpy(fmtObj->stringRep, fmt, maxFmtLen);
+ buffer = fmtObj->stringRep + maxFmtLen + 1;
+ objPtr->internalRep.ptr = fmtObj;
+ objPtr->typePtr = &scanFmtStringObjType;
+ for (i = 0, curr = 0; fmt < fmtEnd; ++fmt) {
+ int width = 0, skip;
+ ScanFmtPartDescr *descr = &fmtObj->descr[curr];
+
+ fmtObj->count++;
+ descr->width = 0; /* Assume width unspecified */
+ /* Overread and store any "literal" prefix */
+ if (*fmt != '%' || fmt[1] == '%') {
+ descr->type = 0;
+ descr->prefix = &buffer[i];
+ for (; fmt < fmtEnd; ++fmt) {
+ if (*fmt == '%') {
+ if (fmt[1] != '%')
+ break;
+ ++fmt;
+ }
+ buffer[i++] = *fmt;
+ }
+ buffer[i++] = 0;
+ }
+ /* Skip the conversion introducing '%' sign */
+ ++fmt;
+ /* End reached due to non-conversion literal only? */
+ if (fmt >= fmtEnd)
+ goto done;
+ descr->pos = 0; /* Assume "natural" positioning */
+ if (*fmt == '*') {
+ descr->pos = -1; /* Okay, conversion will not be assigned */
+ ++fmt;
+ }
+ else
+ fmtObj->convCount++; /* Otherwise count as assign-conversion */
+ /* Check if next token is a number (could be width or pos */
+ if (sscanf(fmt, "%d%n", &width, &skip) == 1) {
+ fmt += skip;
+ /* Was the number a XPG3 position specifier? */
+ if (descr->pos != -1 && *fmt == '$') {
+ int prev;
+
+ ++fmt;
+ descr->pos = width;
+ width = 0;
+ /* Look if "natural" postioning and XPG3 one was mixed */
+ if ((lastPos == 0 && descr->pos > 0)
+ || (lastPos > 0 && descr->pos == 0)) {
+ fmtObj->error = "cannot mix \"%\" and \"%n$\" conversion specifiers";
+ return JIM_ERR;
+ }
+ /* Look if this position was already used */
+ for (prev = 0; prev < curr; ++prev) {
+ if (fmtObj->descr[prev].pos == -1)
+ continue;
+ if (fmtObj->descr[prev].pos == descr->pos) {
+ fmtObj->error =
+ "variable is assigned by multiple \"%n$\" conversion specifiers";
+ return JIM_ERR;
+ }
+ }
+ /* Try to find a width after the XPG3 specifier */
+ if (sscanf(fmt, "%d%n", &width, &skip) == 1) {
+ descr->width = width;
+ fmt += skip;
+ }
+ if (descr->pos > 0 && (size_t) descr->pos > fmtObj->maxPos)
+ fmtObj->maxPos = descr->pos;
+ }
+ else {
+ /* Number was not a XPG3, so it has to be a width */
+ descr->width = width;
+ }
+ }
+ /* If positioning mode was undetermined yet, fix this */
+ if (lastPos == -1)
+ lastPos = descr->pos;
+ /* Handle CHARSET conversion type ... */
+ if (*fmt == '[') {
+ int swapped = 1, beg = i, end, j;
+
+ descr->type = '[';
+ descr->arg = &buffer[i];
+ ++fmt;
+ if (*fmt == '^')
+ buffer[i++] = *fmt++;
+ if (*fmt == ']')
+ buffer[i++] = *fmt++;
+ while (*fmt && *fmt != ']')
+ buffer[i++] = *fmt++;
+ if (*fmt != ']') {
+ fmtObj->error = "unmatched [ in format string";
+ return JIM_ERR;
+ }
+ end = i;
+ buffer[i++] = 0;
+ /* In case a range fence was given "backwards", swap it */
+ while (swapped) {
+ swapped = 0;
+ for (j = beg + 1; j < end - 1; ++j) {
+ if (buffer[j] == '-' && buffer[j - 1] > buffer[j + 1]) {
+ char tmp = buffer[j - 1];
+
+ buffer[j - 1] = buffer[j + 1];
+ buffer[j + 1] = tmp;
+ swapped = 1;
+ }
+ }
+ }
+ }
+ else {
+ /* Remember any valid modifier if given */
+ if (strchr("hlL", *fmt) != 0)
+ descr->modifier = tolower((int)*fmt++);
+
+ descr->type = *fmt;
+ if (strchr("efgcsndoxui", *fmt) == 0) {
+ fmtObj->error = "bad scan conversion character";
+ return JIM_ERR;
+ }
+ else if (*fmt == 'c' && descr->width != 0) {
+ fmtObj->error = "field width may not be specified in %c " "conversion";
+ return JIM_ERR;
+ }
+ else if (*fmt == 'u' && descr->modifier == 'l') {
+ fmtObj->error = "unsigned wide not supported";
+ return JIM_ERR;
+ }
+ }
+ curr++;
+ }
+ done:
+ return JIM_OK;
+}
+
+/* Some accessor macros to allow lowlevel access to fields of internal repr */
+
+#define FormatGetCnvCount(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->convCount
+#define FormatGetMaxPos(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->maxPos
+#define FormatGetError(_fo_) \
+ ((ScanFmtStringObj*)((_fo_)->internalRep.ptr))->error
+
+/* JimScanAString is used to scan an unspecified string that ends with
+ * next WS, or a string that is specified via a charset.
+ *
+ */
+static Jim_Obj *JimScanAString(Jim_Interp *interp, const char *sdescr, const char *str)
+{
+ char *buffer = Jim_StrDup(str);
+ char *p = buffer;
+
+ while (*str) {
+ int c;
+ int n;
+
+ if (!sdescr && isspace(UCHAR(*str)))
+ break; /* EOS via WS if unspecified */
+
+ n = utf8_tounicode(str, &c);
+ if (sdescr && !JimCharsetMatch(sdescr, c, JIM_CHARSET_SCAN))
+ break;
+ while (n--)
+ *p++ = *str++;
+ }
+ *p = 0;
+ return Jim_NewStringObjNoAlloc(interp, buffer, p - buffer);
+}
+
+/* ScanOneEntry will scan one entry out of the string passed as argument.
+ * It use the sscanf() function for this task. After extracting and
+ * converting of the value, the count of scanned characters will be
+ * returned of -1 in case of no conversion tool place and string was
+ * already scanned thru */
+
+static int ScanOneEntry(Jim_Interp *interp, const char *str, int pos, int strLen,
+ ScanFmtStringObj * fmtObj, long idx, Jim_Obj **valObjPtr)
+{
+ const char *tok;
+ const ScanFmtPartDescr *descr = &fmtObj->descr[idx];
+ size_t scanned = 0;
+ size_t anchor = pos;
+ int i;
+ Jim_Obj *tmpObj = NULL;
+
+ /* First pessimistically assume, we will not scan anything :-) */
+ *valObjPtr = 0;
+ if (descr->prefix) {
+ /* There was a prefix given before the conversion, skip it and adjust
+ * the string-to-be-parsed accordingly */
+ /* XXX: Should be checking strLen, not str[pos] */
+ for (i = 0; pos < strLen && descr->prefix[i]; ++i) {
+ /* If prefix require, skip WS */
+ if (isspace(UCHAR(descr->prefix[i])))
+ while (pos < strLen && isspace(UCHAR(str[pos])))
+ ++pos;
+ else if (descr->prefix[i] != str[pos])
+ break; /* Prefix do not match here, leave the loop */
+ else
+ ++pos; /* Prefix matched so far, next round */
+ }
+ if (pos >= strLen) {
+ return -1; /* All of str consumed: EOF condition */
+ }
+ else if (descr->prefix[i] != 0)
+ return 0; /* Not whole prefix consumed, no conversion possible */
+ }
+ /* For all but following conversion, skip leading WS */
+ if (descr->type != 'c' && descr->type != '[' && descr->type != 'n')
+ while (isspace(UCHAR(str[pos])))
+ ++pos;
+ /* Determine how much skipped/scanned so far */
+ scanned = pos - anchor;
+
+ /* %c is a special, simple case. no width */
+ if (descr->type == 'n') {
+ /* Return pseudo conversion means: how much scanned so far? */
+ *valObjPtr = Jim_NewIntObj(interp, anchor + scanned);
+ }
+ else if (pos >= strLen) {
+ /* Cannot scan anything, as str is totally consumed */
+ return -1;
+ }
+ else if (descr->type == 'c') {
+ int c;
+ scanned += utf8_tounicode(&str[pos], &c);
+ *valObjPtr = Jim_NewIntObj(interp, c);
+ return scanned;
+ }
+ else {
+ /* Processing of conversions follows ... */
+ if (descr->width > 0) {
+ /* Do not try to scan as fas as possible but only the given width.
+ * To ensure this, we copy the part that should be scanned. */
+ size_t sLen = utf8_strlen(&str[pos], strLen - pos);
+ size_t tLen = descr->width > sLen ? sLen : descr->width;
+
+ tmpObj = Jim_NewStringObjUtf8(interp, str + pos, tLen);
+ tok = tmpObj->bytes;
+ }
+ else {
+ /* As no width was given, simply refer to the original string */
+ tok = &str[pos];
+ }
+ switch (descr->type) {
+ case 'd':
+ case 'o':
+ case 'x':
+ case 'u':
+ case 'i':{
+ char *endp; /* Position where the number finished */
+ jim_wide w;
+
+ int base = descr->type == 'o' ? 8
+ : descr->type == 'x' ? 16 : descr->type == 'i' ? 0 : 10;
+
+ /* Try to scan a number with the given base */
+ w = strtoull(tok, &endp, base);
+ if (endp == tok && base == 0) {
+ /* If scanning failed, and base was undetermined, simply
+ * put it to 10 and try once more. This should catch the
+ * case where %i begin to parse a number prefix (e.g.
+ * '0x' but no further digits follows. This will be
+ * handled as a ZERO followed by a char 'x' by Tcl */
+ w = strtoull(tok, &endp, 10);
+ }
+
+ if (endp != tok) {
+ /* There was some number sucessfully scanned! */
+ *valObjPtr = Jim_NewIntObj(interp, w);
+
+ /* Adjust the number-of-chars scanned so far */
+ scanned += endp - tok;
+ }
+ else {
+ /* Nothing was scanned. We have to determine if this
+ * happened due to e.g. prefix mismatch or input str
+ * exhausted */
+ scanned = *tok ? 0 : -1;
+ }
+ break;
+ }
+ case 's':
+ case '[':{
+ *valObjPtr = JimScanAString(interp, descr->arg, tok);
+ scanned += Jim_Length(*valObjPtr);
+ break;
+ }
+ case 'e':
+ case 'f':
+ case 'g':{
+ char *endp;
+ double value = strtod(tok, &endp);
+
+ if (endp != tok) {
+ /* There was some number sucessfully scanned! */
+ *valObjPtr = Jim_NewDoubleObj(interp, value);
+ /* Adjust the number-of-chars scanned so far */
+ scanned += endp - tok;
+ }
+ else {
+ /* Nothing was scanned. We have to determine if this
+ * happened due to e.g. prefix mismatch or input str
+ * exhausted */
+ scanned = *tok ? 0 : -1;
+ }
+ break;
+ }
+ }
+ /* If a substring was allocated (due to pre-defined width) do not
+ * forget to free it */
+ if (tmpObj) {
+ Jim_FreeNewObj(interp, tmpObj);
+ }
+ }
+ return scanned;
+}
+
+/* Jim_ScanString is the workhorse of string scanning. It will scan a given
+ * string and returns all converted (and not ignored) values in a list back
+ * to the caller. If an error occured, a NULL pointer will be returned */
+
+Jim_Obj *Jim_ScanString(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *fmtObjPtr, int flags)
+{
+ size_t i, pos;
+ int scanned = 1;
+ const char *str = Jim_String(strObjPtr);
+ int strLen = Jim_Utf8Length(interp, strObjPtr);
+ Jim_Obj *resultList = 0;
+ Jim_Obj **resultVec = 0;
+ int resultc;
+ Jim_Obj *emptyStr = 0;
+ ScanFmtStringObj *fmtObj;
+
+ /* This should never happen. The format object should already be of the correct type */
+ JimPanic((fmtObjPtr->typePtr != &scanFmtStringObjType, interp, "Jim_ScanString() for non-scan format"));
+
+ fmtObj = (ScanFmtStringObj *) fmtObjPtr->internalRep.ptr;
+ /* Check if format specification was valid */
+ if (fmtObj->error != 0) {
+ if (flags & JIM_ERRMSG)
+ Jim_SetResultString(interp, fmtObj->error, -1);
+ return 0;
+ }
+ /* Allocate a new "shared" empty string for all unassigned conversions */
+ emptyStr = Jim_NewEmptyStringObj(interp);
+ Jim_IncrRefCount(emptyStr);
+ /* Create a list and fill it with empty strings up to max specified XPG3 */
+ resultList = Jim_NewListObj(interp, 0, 0);
+ if (fmtObj->maxPos > 0) {
+ for (i = 0; i < fmtObj->maxPos; ++i)
+ Jim_ListAppendElement(interp, resultList, emptyStr);
+ JimListGetElements(interp, resultList, &resultc, &resultVec);
+ }
+ /* Now handle every partial format description */
+ for (i = 0, pos = 0; i < fmtObj->count; ++i) {
+ ScanFmtPartDescr *descr = &(fmtObj->descr[i]);
+ Jim_Obj *value = 0;
+
+ /* Only last type may be "literal" w/o conversion - skip it! */
+ if (descr->type == 0)
+ continue;
+ /* As long as any conversion could be done, we will proceed */
+ if (scanned > 0)
+ scanned = ScanOneEntry(interp, str, pos, strLen, fmtObj, i, &value);
+ /* In case our first try results in EOF, we will leave */
+ if (scanned == -1 && i == 0)
+ goto eof;
+ /* Advance next pos-to-be-scanned for the amount scanned already */
+ pos += scanned;
+
+ /* value == 0 means no conversion took place so take empty string */
+ if (value == 0)
+ value = Jim_NewEmptyStringObj(interp);
+ /* If value is a non-assignable one, skip it */
+ if (descr->pos == -1) {
+ Jim_FreeNewObj(interp, value);
+ }
+ else if (descr->pos == 0)
+ /* Otherwise append it to the result list if no XPG3 was given */
+ Jim_ListAppendElement(interp, resultList, value);
+ else if (resultVec[descr->pos - 1] == emptyStr) {
+ /* But due to given XPG3, put the value into the corr. slot */
+ Jim_DecrRefCount(interp, resultVec[descr->pos - 1]);
+ Jim_IncrRefCount(value);
+ resultVec[descr->pos - 1] = value;
+ }
+ else {
+ /* Otherwise, the slot was already used - free obj and ERROR */
+ Jim_FreeNewObj(interp, value);
+ goto err;
+ }
+ }
+ Jim_DecrRefCount(interp, emptyStr);
+ return resultList;
+ eof:
+ Jim_DecrRefCount(interp, emptyStr);
+ Jim_FreeNewObj(interp, resultList);
+ return (Jim_Obj *)EOF;
+ err:
+ Jim_DecrRefCount(interp, emptyStr);
+ Jim_FreeNewObj(interp, resultList);
+ return 0;
+}
+
+/* -----------------------------------------------------------------------------
+ * Pseudo Random Number Generation
+ * ---------------------------------------------------------------------------*/
+static void JimPrngSeed(Jim_Interp *interp, const unsigned char *seed, int seedLen);
+
+/* Initialize the sbox with the numbers from 0 to 255 */
+static void JimPrngInit(Jim_Interp *interp)
+{
+ int i;
+ /* XXX: Move off stack */
+ unsigned int seed[256];
+ unsigned rseed; /* uninitialized! */
+
+ interp->prngState = Jim_Alloc(sizeof(Jim_PrngState));
+ for (i = 0; i < 256; i++)
+ seed[i] = (rand_r(&rseed) ^ time(NULL) ^ clock());
+ JimPrngSeed(interp, (unsigned char *)seed, sizeof(int) * 256);
+}
+
+/* Generates N bytes of random data */
+static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len)
+{
+ Jim_PrngState *prng;
+ unsigned char *destByte = (unsigned char *)dest;
+ unsigned int si, sj, x;
+
+ /* initialization, only needed the first time */
+ if (interp->prngState == NULL)
+ JimPrngInit(interp);
+ prng = interp->prngState;
+ /* generates 'len' bytes of pseudo-random numbers */
+ for (x = 0; x < len; x++) {
+ prng->i = (prng->i + 1) & 0xff;
+ si = prng->sbox[prng->i];
+ prng->j = (prng->j + si) & 0xff;
+ sj = prng->sbox[prng->j];
+ prng->sbox[prng->i] = sj;
+ prng->sbox[prng->j] = si;
+ *destByte++ = prng->sbox[(si + sj) & 0xff];
+ }
+}
+
+/* Re-seed the generator with user-provided bytes */
+static void JimPrngSeed(Jim_Interp *interp, const unsigned char *seed, int seedLen)
+{
+ int i;
+ /* XXX: Move off stack */
+ unsigned char buf[256];
+ Jim_PrngState *prng;
+
+ /* initialization, only needed the first time */
+ if (interp->prngState == NULL)
+ JimPrngInit(interp);
+ prng = interp->prngState;
+
+ /* Set the sbox[i] with i */
+ for (i = 0; i < 256; i++)
+ prng->sbox[i] = i;
+ /* Now use the seed to perform a random permutation of the sbox */
+ for (i = 0; i < seedLen; i++) {
+ unsigned char t;
+
+ t = prng->sbox[i & 0xFF];
+ prng->sbox[i & 0xFF] = prng->sbox[seed[i]];
+ prng->sbox[seed[i]] = t;
+ }
+ prng->i = prng->j = 0;
+ /* discard the first 256 bytes of stream. */
+ JimRandomBytes(interp, buf, 256);
+}
+
+/* [incr] */
+static int Jim_IncrCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ jim_wide wideValue, increment = 1;
+ Jim_Obj *intObjPtr;
+
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?increment?");
+ return JIM_ERR;
+ }
+ if (argc == 3) {
+ if (Jim_GetWide(interp, argv[2], &increment) != JIM_OK)
+ return JIM_ERR;
+ }
+ intObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
+ if (!intObjPtr) {
+ /* Set missing variable to 0 */
+ wideValue = 0;
+ }
+ else if (Jim_GetWide(interp, intObjPtr, &wideValue) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (!intObjPtr || Jim_IsShared(intObjPtr)) {
+ intObjPtr = Jim_NewIntObj(interp, wideValue + increment);
+ if (Jim_SetVariable(interp, argv[1], intObjPtr) != JIM_OK) {
+ Jim_FreeNewObj(interp, intObjPtr);
+ return JIM_ERR;
+ }
+ }
+ else {
+ /* Can do it the quick way */
+ Jim_InvalidateStringRep(intObjPtr);
+ JimWideValue(intObjPtr) = wideValue + increment;
+
+ /* The following step is required in order to invalidate the
+ * string repr of "FOO" if the var name is on the form of "FOO(IDX)" */
+ if (argv[1]->typePtr != &variableObjType) {
+ /* Note that this can't fail since GetVariable already succeeded */
+ Jim_SetVariable(interp, argv[1], intObjPtr);
+ }
+ }
+ Jim_SetResult(interp, intObjPtr);
+ return JIM_OK;
+}
+
+
+/* -----------------------------------------------------------------------------
+ * Eval
+ * ---------------------------------------------------------------------------*/
+#define JIM_EVAL_SARGV_LEN 8 /* static arguments vector length */
+#define JIM_EVAL_SINTV_LEN 8 /* static interpolation vector length */
+
+/* Handle calls to the [unknown] command */
+static int JimUnknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *filename,
+ int linenr)
+{
+ Jim_Obj **v, *sv[JIM_EVAL_SARGV_LEN];
+ int retCode;
+
+ /* If JimUnknown() is recursively called too many times...
+ * done here
+ */
+ if (interp->unknown_called > 50) {
+ return JIM_ERR;
+ }
+
+ /* If the [unknown] command does not exists returns
+ * just now */
+ if (Jim_GetCommand(interp, interp->unknown, JIM_NONE) == NULL)
+ return JIM_ERR;
+
+ /* The object interp->unknown just contains
+ * the "unknown" string, it is used in order to
+ * avoid to lookup the unknown command every time
+ * but instread to cache the result. */
+ if (argc + 1 <= JIM_EVAL_SARGV_LEN)
+ v = sv;
+ else
+ v = Jim_Alloc(sizeof(Jim_Obj *) * (argc + 1));
+ /* Make a copy of the arguments vector, but shifted on
+ * the right of one position. The command name of the
+ * command will be instead the first argument of the
+ * [unknown] call. */
+ memcpy(v + 1, argv, sizeof(Jim_Obj *) * argc);
+ v[0] = interp->unknown;
+ /* Call it */
+ interp->unknown_called++;
+ retCode = JimEvalObjVector(interp, argc + 1, v, filename, linenr);
+ interp->unknown_called--;
+
+ /* Clean up */
+ if (v != sv)
+ Jim_Free(v);
+ return retCode;
+}
+
+/* Eval the object vector 'objv' composed of 'objc' elements.
+ * Every element is used as single argument.
+ * Jim_EvalObj() will call this function every time its object
+ * argument is of "list" type, with no string representation.
+ *
+ * This is possible because the string representation of a
+ * list object generated by the UpdateStringOfList is made
+ * in a way that ensures that every list element is a different
+ * command argument. */
+static int JimEvalObjVector(Jim_Interp *interp, int objc, Jim_Obj *const *objv,
+ const char *filename, int linenr)
+{
+ int i, retcode;
+ Jim_Cmd *cmdPtr;
+
+ /* Incr refcount of arguments. */
+ for (i = 0; i < objc; i++)
+ Jim_IncrRefCount(objv[i]);
+ /* Command lookup */
+ cmdPtr = Jim_GetCommand(interp, objv[0], JIM_ERRMSG);
+ if (cmdPtr == NULL) {
+ retcode = JimUnknown(interp, objc, objv, filename, linenr);
+ }
+ else {
+ /* Call it -- Make sure result is an empty object. */
+ JimIncrCmdRefCount(cmdPtr);
+ Jim_SetEmptyResult(interp);
+ if (cmdPtr->isproc) {
+ retcode = JimCallProcedure(interp, cmdPtr, filename, linenr, objc, objv);
+ }
+ else {
+ interp->cmdPrivData = cmdPtr->u.native.privData;
+ retcode = cmdPtr->u.native.cmdProc(interp, objc, objv);
+ }
+ JimDecrCmdRefCount(interp, cmdPtr);
+ }
+ /* Decr refcount of arguments and return the retcode */
+ for (i = 0; i < objc; i++)
+ Jim_DecrRefCount(interp, objv[i]);
+
+ return retcode;
+}
+
+int Jim_EvalObjVector(Jim_Interp *interp, int objc, Jim_Obj *const *objv)
+{
+ return JimEvalObjVector(interp, objc, objv, NULL, 0);
+}
+
+/**
+ * Invokes 'prefix' as a command with the objv array as arguments.
+ */
+int Jim_EvalObjPrefix(Jim_Interp *interp, const char *prefix, int objc, Jim_Obj *const *objv)
+{
+ int i;
+ int ret;
+ Jim_Obj **nargv = Jim_Alloc((objc + 1) * sizeof(*nargv));
+
+ nargv[0] = Jim_NewStringObj(interp, prefix, -1);
+ for (i = 0; i < objc; i++) {
+ nargv[i + 1] = objv[i];
+ }
+ ret = Jim_EvalObjVector(interp, objc + 1, nargv);
+ Jim_Free(nargv);
+ return ret;
+}
+
+static void JimAddErrorToStack(Jim_Interp *interp, int retcode, const char *filename, int line)
+{
+ int rc = retcode;
+
+ if (rc == JIM_ERR && !interp->errorFlag) {
+ /* This is the first error, so save the file/line information and reset the stack */
+ interp->errorFlag = 1;
+ JimSetErrorFileName(interp, filename);
+ JimSetErrorLineNumber(interp, line);
+
+ JimResetStackTrace(interp);
+ /* Always add a level where the error first occurs */
+ interp->addStackTrace++;
+ }
+
+ /* Now if this is an "interesting" level, add it to the stack trace */
+ if (rc == JIM_ERR && interp->addStackTrace > 0) {
+ /* Add the stack info for the current level */
+
+ JimAppendStackTrace(interp, Jim_String(interp->errorProc), filename, line);
+
+ /* Note: if we didn't have a filename for this level,
+ * don't clear the addStackTrace flag
+ * so we can pick it up at the next level
+ */
+ if (*filename) {
+ interp->addStackTrace = 0;
+ }
+
+ Jim_DecrRefCount(interp, interp->errorProc);
+ interp->errorProc = interp->emptyObj;
+ Jim_IncrRefCount(interp->errorProc);
+ }
+ else if (rc == JIM_RETURN && interp->returnCode == JIM_ERR) {
+ /* Propagate the addStackTrace value through 'return -code error' */
+ }
+ else {
+ interp->addStackTrace = 0;
+ }
+}
+
+/* And delete any local procs */
+static void JimDeleteLocalProcs(Jim_Interp *interp)
+{
+ if (interp->localProcs) {
+ char *procname;
+
+ while ((procname = Jim_StackPop(interp->localProcs)) != NULL) {
+ /* If there is a pushed command, find it */
+ Jim_Cmd *prevCmd = NULL;
+ Jim_HashEntry *he = Jim_FindHashEntry(&interp->commands, procname);
+ if (he) {
+ Jim_Cmd *cmd = (Jim_Cmd *)he->val;
+ if (cmd->isproc && cmd->u.proc.prevCmd) {
+ prevCmd = cmd->u.proc.prevCmd;
+ cmd->u.proc.prevCmd = NULL;
+ }
+ }
+
+ /* Delete the local proc */
+ Jim_DeleteCommand(interp, procname);
+
+ if (prevCmd) {
+ /* And restore the pushed command */
+ Jim_AddHashEntry(&interp->commands, procname, prevCmd);
+ }
+ Jim_Free(procname);
+ }
+ Jim_FreeStack(interp->localProcs);
+ Jim_Free(interp->localProcs);
+ interp->localProcs = NULL;
+ }
+}
+
+static int JimSubstOneToken(Jim_Interp *interp, const ScriptToken *token, Jim_Obj **objPtrPtr)
+{
+ Jim_Obj *objPtr;
+
+ switch (token->type) {
+ case JIM_TT_STR:
+ case JIM_TT_ESC:
+ objPtr = token->objPtr;
+ break;
+ case JIM_TT_VAR:
+ objPtr = Jim_GetVariable(interp, token->objPtr, JIM_ERRMSG);
+ break;
+ case JIM_TT_DICTSUGAR:
+ objPtr = JimExpandDictSugar(interp, token->objPtr);
+ break;
+ case JIM_TT_EXPRSUGAR:
+ objPtr = JimExpandExprSugar(interp, token->objPtr);
+ break;
+ case JIM_TT_CMD:
+ switch (Jim_EvalObj(interp, token->objPtr)) {
+ case JIM_OK:
+ case JIM_RETURN:
+ objPtr = interp->result;
+ break;
+ case JIM_BREAK:
+ /* Stop substituting */
+ return JIM_BREAK;
+ case JIM_CONTINUE:
+ /* just skip this one */
+ return JIM_CONTINUE;
+ default:
+ return JIM_ERR;
+ }
+ break;
+ default:
+ JimPanic((1, interp,
+ "default token type (%d) reached " "in Jim_SubstObj().", token->type));
+ objPtr = NULL;
+ break;
+ }
+ if (objPtr) {
+ *objPtrPtr = objPtr;
+ return JIM_OK;
+ }
+ return JIM_ERR;
+}
+
+/* Interpolate the given tokens into a unique Jim_Obj returned by reference
+ * via *objPtrPtr. This function is only called by Jim_EvalObj() and Jim_SubstObj()
+ * The returned object has refcount = 0.
+ */
+static Jim_Obj *JimInterpolateTokens(Jim_Interp *interp, const ScriptToken * token, int tokens, int flags)
+{
+ int totlen = 0, i;
+ Jim_Obj **intv;
+ Jim_Obj *sintv[JIM_EVAL_SINTV_LEN];
+ Jim_Obj *objPtr;
+ char *s;
+
+ if (tokens <= JIM_EVAL_SINTV_LEN)
+ intv = sintv;
+ else
+ intv = Jim_Alloc(sizeof(Jim_Obj *) * tokens);
+
+ /* Compute every token forming the argument
+ * in the intv objects vector. */
+ for (i = 0; i < tokens; i++) {
+ switch (JimSubstOneToken(interp, &token[i], &intv[i])) {
+ case JIM_OK:
+ case JIM_RETURN:
+ break;
+ case JIM_BREAK:
+ if (flags & JIM_SUBST_FLAG) {
+ /* Stop here */
+ tokens = i;
+ continue;
+ }
+ /* XXX: Should probably set an error about break outside loop */
+ /* fall through to error */
+ case JIM_CONTINUE:
+ if (flags & JIM_SUBST_FLAG) {
+ intv[i] = NULL;
+ continue;
+ }
+ /* XXX: Ditto continue outside loop */
+ /* fall through to error */
+ default:
+ while (i--) {
+ Jim_DecrRefCount(interp, intv[i]);
+ }
+ if (intv != sintv) {
+ Jim_Free(intv);
+ }
+ return NULL;
+ }
+ Jim_IncrRefCount(intv[i]);
+ Jim_String(intv[i]);
+ totlen += intv[i]->length;
+ }
+
+ /* Fast path return for a single token */
+ if (tokens == 1 && intv[0] && intv == sintv) {
+ Jim_DecrRefCount(interp, intv[0]);
+ return intv[0];
+ }
+
+ /* Concatenate every token in an unique
+ * object. */
+ objPtr = Jim_NewStringObjNoAlloc(interp, NULL, 0);
+
+ if (tokens == 4 && token[0].type == JIM_TT_ESC && token[1].type == JIM_TT_ESC
+ && token[2].type == JIM_TT_VAR) {
+ /* May be able to do fast interpolated object -> dictSubst */
+ objPtr->typePtr = &interpolatedObjType;
+ objPtr->internalRep.twoPtrValue.ptr1 = (void *)token;
+ objPtr->internalRep.twoPtrValue.ptr2 = intv[2];
+ Jim_IncrRefCount(intv[2]);
+ }
+
+ s = objPtr->bytes = Jim_Alloc(totlen + 1);
+ objPtr->length = totlen;
+ for (i = 0; i < tokens; i++) {
+ if (intv[i]) {
+ memcpy(s, intv[i]->bytes, intv[i]->length);
+ s += intv[i]->length;
+ Jim_DecrRefCount(interp, intv[i]);
+ }
+ }
+ objPtr->bytes[totlen] = '\0';
+ /* Free the intv vector if not static. */
+ if (intv != sintv) {
+ Jim_Free(intv);
+ }
+
+ return objPtr;
+}
+
+
+/* If listPtr is a list, call JimEvalObjVector() with the given source info.
+ * Otherwise eval with Jim_EvalObj()
+ */
+int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listPtr, const char *filename, int linenr)
+{
+ if (!Jim_IsList(listPtr)) {
+ return Jim_EvalObj(interp, listPtr);
+ }
+ else {
+ int retcode = JIM_OK;
+
+ if (listPtr->internalRep.listValue.len) {
+ Jim_IncrRefCount(listPtr);
+ retcode = JimEvalObjVector(interp,
+ listPtr->internalRep.listValue.len,
+ listPtr->internalRep.listValue.ele, filename, linenr);
+ Jim_DecrRefCount(interp, listPtr);
+ }
+ return retcode;
+ }
+}
+
+int Jim_EvalObj(Jim_Interp *interp, Jim_Obj *scriptObjPtr)
+{
+ int i;
+ ScriptObj *script;
+ ScriptToken *token;
+ int retcode = JIM_OK;
+ Jim_Obj *sargv[JIM_EVAL_SARGV_LEN], **argv = NULL;
+ int linenr = 0;
+
+ interp->errorFlag = 0;
+
+ /* If the object is of type "list", we can call
+ * a specialized version of Jim_EvalObj() */
+ if (Jim_IsList(scriptObjPtr)) {
+ return Jim_EvalObjList(interp, scriptObjPtr, NULL, 0);
+ }
+
+ Jim_IncrRefCount(scriptObjPtr); /* Make sure it's shared. */
+ script = Jim_GetScript(interp, scriptObjPtr);
+
+ /* Reset the interpreter result. This is useful to
+ * return the empty result in the case of empty program. */
+ Jim_SetEmptyResult(interp);
+
+#ifdef JIM_OPTIMIZATION
+ /* Check for one of the following common scripts used by for, while
+ *
+ * {}
+ * incr a
+ */
+ if (script->len == 0) {
+ Jim_DecrRefCount(interp, scriptObjPtr);
+ return JIM_OK;
+ }
+ if (script->len == 3
+ && script->token[1].objPtr->typePtr == &commandObjType
+ && script->token[1].objPtr->internalRep.cmdValue.cmdPtr->isproc == 0
+ && script->token[1].objPtr->internalRep.cmdValue.cmdPtr->u.native.cmdProc == Jim_IncrCoreCommand
+ && script->token[2].objPtr->typePtr == &variableObjType) {
+
+ Jim_Obj *objPtr = Jim_GetVariable(interp, script->token[2].objPtr, JIM_NONE);
+
+ if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) {
+ JimWideValue(objPtr)++;
+ Jim_InvalidateStringRep(objPtr);
+ Jim_DecrRefCount(interp, scriptObjPtr);
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+ }
+#endif
+
+ /* Now we have to make sure the internal repr will not be
+ * freed on shimmering.
+ *
+ * Think for example to this:
+ *
+ * set x {llength $x; ... some more code ...}; eval $x
+ *
+ * In order to preserve the internal rep, we increment the
+ * inUse field of the script internal rep structure. */
+ script->inUse++;
+
+ token = script->token;
+ argv = sargv;
+
+ /* Execute every command sequentially until the end of the script
+ * or an error occurs.
+ */
+ for (i = 0; i < script->len && retcode == JIM_OK; ) {
+ int argc;
+ int j;
+ Jim_Cmd *cmd;
+
+ /* First token of the line is always JIM_TT_LINE */
+ argc = token[i].objPtr->internalRep.scriptLineValue.argc;
+ linenr = token[i].objPtr->internalRep.scriptLineValue.line;
+
+ /* Allocate the arguments vector if required */
+ if (argc > JIM_EVAL_SARGV_LEN)
+ argv = Jim_Alloc(sizeof(Jim_Obj *) * argc);
+
+ /* Skip the JIM_TT_LINE token */
+ i++;
+
+ /* Populate the arguments objects.
+ * If an error occurs, retcode will be set and
+ * 'j' will be set to the number of args expanded
+ */
+ for (j = 0; j < argc; j++) {
+ long wordtokens = 1;
+ int expand = 0;
+ Jim_Obj *wordObjPtr = NULL;
+
+ if (token[i].type == JIM_TT_WORD) {
+ wordtokens = JimWideValue(token[i++].objPtr);
+ if (wordtokens < 0) {
+ expand = 1;
+ wordtokens = -wordtokens;
+ }
+ }
+
+ if (wordtokens == 1) {
+ /* Fast path if the token does not
+ * need interpolation */
+
+ switch (token[i].type) {
+ case JIM_TT_ESC:
+ case JIM_TT_STR:
+ wordObjPtr = token[i].objPtr;
+ break;
+ case JIM_TT_VAR:
+ wordObjPtr = Jim_GetVariable(interp, token[i].objPtr, JIM_ERRMSG);
+ break;
+ case JIM_TT_EXPRSUGAR:
+ wordObjPtr = JimExpandExprSugar(interp, token[i].objPtr);
+ break;
+ case JIM_TT_DICTSUGAR:
+ wordObjPtr = JimExpandDictSugar(interp, token[i].objPtr);
+ break;
+ case JIM_TT_CMD:
+ retcode = Jim_EvalObj(interp, token[i].objPtr);
+ if (retcode == JIM_OK) {
+ wordObjPtr = Jim_GetResult(interp);
+ }
+ break;
+ default:
+ JimPanic((1, interp, "default token type reached " "in Jim_EvalObj()."));
+ }
+ }
+ else {
+ /* For interpolation we call a helper
+ * function to do the work for us. */
+ wordObjPtr = JimInterpolateTokens(interp, token + i, wordtokens, JIM_NONE);
+ }
+
+ if (!wordObjPtr) {
+ if (retcode == JIM_OK) {
+ retcode = JIM_ERR;
+ }
+ break;
+ }
+
+ Jim_IncrRefCount(wordObjPtr);
+ i += wordtokens;
+
+ if (!expand) {
+ argv[j] = wordObjPtr;
+ }
+ else {
+ /* Need to expand wordObjPtr into multiple args from argv[j] ... */
+ int len = Jim_ListLength(interp, wordObjPtr);
+ int newargc = argc + len - 1;
+ int k;
+
+ if (len > 1) {
+ if (argv == sargv) {
+ if (newargc > JIM_EVAL_SARGV_LEN) {
+ argv = Jim_Alloc(sizeof(*argv) * newargc);
+ memcpy(argv, sargv, sizeof(*argv) * j);
+ }
+ }
+ else {
+ /* Need to realloc to make room for (len - 1) more entries */
+ argv = Jim_Realloc(argv, sizeof(*argv) * newargc);
+ }
+ }
+
+ /* Now copy in the expanded version */
+ for (k = 0; k < len; k++) {
+ argv[j++] = wordObjPtr->internalRep.listValue.ele[k];
+ Jim_IncrRefCount(wordObjPtr->internalRep.listValue.ele[k]);
+ }
+
+ /* The original object reference is no longer needed,
+ * after the expansion it is no longer present on
+ * the argument vector, but the single elements are
+ * in its place. */
+ Jim_DecrRefCount(interp, wordObjPtr);
+
+ /* And update the indexes */
+ j--;
+ argc += len - 1;
+ }
+ }
+
+ if (retcode == JIM_OK && argc) {
+ /* Lookup the command to call */
+ cmd = Jim_GetCommand(interp, argv[0], JIM_ERRMSG);
+ if (cmd != NULL) {
+ /* Call it -- Make sure result is an empty object. */
+ JimIncrCmdRefCount(cmd);
+ Jim_SetEmptyResult(interp);
+ if (cmd->isproc) {
+ retcode =
+ JimCallProcedure(interp, cmd, script->fileName, linenr, argc, argv);
+ } else {
+ interp->cmdPrivData = cmd->u.native.privData;
+ retcode = cmd->u.native.cmdProc(interp, argc, argv);
+ }
+ JimDecrCmdRefCount(interp, cmd);
+ }
+ else {
+ /* Call [unknown] */
+ retcode = JimUnknown(interp, argc, argv, script->fileName, linenr);
+ }
+ if (interp->signal_level && interp->sigmask) {
+ /* Check for a signal after each command */
+ retcode = JIM_SIGNAL;
+ }
+ }
+
+ /* Finished with the command, so decrement ref counts of each argument */
+ while (j-- > 0) {
+ Jim_DecrRefCount(interp, argv[j]);
+ }
+
+ if (argv != sargv) {
+ Jim_Free(argv);
+ argv = sargv;
+ }
+ }
+
+ /* Possibly add to the error stack trace */
+ JimAddErrorToStack(interp, retcode, script->fileName, linenr);
+
+ /* Note that we don't have to decrement inUse, because the
+ * following code transfers our use of the reference again to
+ * the script object. */
+ Jim_FreeIntRep(interp, scriptObjPtr);
+ scriptObjPtr->typePtr = &scriptObjType;
+ Jim_SetIntRepPtr(scriptObjPtr, script);
+ Jim_DecrRefCount(interp, scriptObjPtr);
+
+ return retcode;
+}
+
+static int JimSetProcArg(Jim_Interp *interp, Jim_Obj *argNameObj, Jim_Obj *argValObj)
+{
+ int retcode;
+ /* If argObjPtr begins with '&', do an automatic upvar */
+ const char *varname = Jim_String(argNameObj);
+ if (*varname == '&') {
+ /* First check that the target variable exists */
+ Jim_Obj *objPtr;
+ Jim_CallFrame *savedCallFrame = interp->framePtr;
+
+ interp->framePtr = interp->framePtr->parentCallFrame;
+ objPtr = Jim_GetVariable(interp, argValObj, JIM_ERRMSG);
+ interp->framePtr = savedCallFrame;
+ if (!objPtr) {
+ return JIM_ERR;
+ }
+
+ /* It exists, so perform the binding. */
+ objPtr = Jim_NewStringObj(interp, varname + 1, -1);
+ Jim_IncrRefCount(objPtr);
+ retcode = Jim_SetVariableLink(interp, objPtr, argValObj, interp->framePtr->parentCallFrame);
+ Jim_DecrRefCount(interp, objPtr);
+ }
+ else {
+ retcode = Jim_SetVariable(interp, argNameObj, argValObj);
+ }
+ return retcode;
+}
+
+/* Call a procedure implemented in Tcl.
+ * It's possible to speed-up a lot this function, currently
+ * the callframes are not cached, but allocated and
+ * destroied every time. What is expecially costly is
+ * to create/destroy the local vars hash table every time.
+ *
+ * This can be fixed just implementing callframes caching
+ * in JimCreateCallFrame() and JimFreeCallFrame(). */
+int JimCallProcedure(Jim_Interp *interp, Jim_Cmd *cmd, const char *filename, int linenr, int argc,
+ Jim_Obj *const *argv)
+{
+ int i, d, retcode;
+ Jim_CallFrame *callFramePtr;
+ Jim_Obj *argObjPtr;
+ Jim_Obj *procname = argv[0];
+ Jim_Stack *prevLocalProcs;
+
+ /* Check arity */
+ if (argc - 1 < cmd->u.proc.leftArity + cmd->u.proc.rightArity ||
+ (!cmd->u.proc.args && argc - 1 > cmd->u.proc.leftArity + cmd->u.proc.rightArity + cmd->u.proc.optionalArgs)) {
+ /* Create a nice error message, consistent with Tcl 8.5 */
+ Jim_Obj *argmsg = Jim_NewStringObj(interp, "", 0);
+ int arglen = Jim_ListLength(interp, cmd->u.proc.argListObjPtr);
+
+ for (i = 0; i < arglen; i++) {
+ Jim_Obj *objPtr;
+ Jim_ListIndex(interp, cmd->u.proc.argListObjPtr, i, &argObjPtr, JIM_NONE);
+
+ Jim_AppendString(interp, argmsg, " ", 1);
+
+ if (i < cmd->u.proc.leftArity || i >= arglen - cmd->u.proc.rightArity) {
+ Jim_AppendObj(interp, argmsg, argObjPtr);
+ }
+ else if (i == arglen - cmd->u.proc.rightArity - cmd->u.proc.args) {
+ if (Jim_ListLength(interp, argObjPtr) == 1) {
+ /* We have plain args */
+ Jim_AppendString(interp, argmsg, "?argument ...?", -1);
+ }
+ else {
+ Jim_AppendString(interp, argmsg, "?", 1);
+ Jim_ListIndex(interp, argObjPtr, 1, &objPtr, JIM_NONE);
+ Jim_AppendObj(interp, argmsg, objPtr);
+ Jim_AppendString(interp, argmsg, " ...?", -1);
+ }
+ }
+ else {
+ Jim_AppendString(interp, argmsg, "?", 1);
+ Jim_ListIndex(interp, argObjPtr, 0, &objPtr, JIM_NONE);
+ Jim_AppendObj(interp, argmsg, objPtr);
+ Jim_AppendString(interp, argmsg, "?", 1);
+ }
+ }
+ Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s%#s\"", procname, argmsg);
+ Jim_FreeNewObj(interp, argmsg);
+ return JIM_ERR;
+ }
+
+ /* Check if there are too nested calls */
+ if (interp->framePtr->level == interp->maxNestingDepth) {
+ Jim_SetResultString(interp, "Too many nested calls. Infinite recursion?", -1);
+ return JIM_ERR;
+ }
+
+ /* Create a new callframe */
+ callFramePtr = JimCreateCallFrame(interp, interp->framePtr);
+ callFramePtr->argv = argv;
+ callFramePtr->argc = argc;
+ callFramePtr->procArgsObjPtr = cmd->u.proc.argListObjPtr;
+ callFramePtr->procBodyObjPtr = cmd->u.proc.bodyObjPtr;
+ callFramePtr->staticVars = cmd->u.proc.staticVars;
+ callFramePtr->filename = filename;
+ callFramePtr->line = linenr;
+ Jim_IncrRefCount(cmd->u.proc.argListObjPtr);
+ Jim_IncrRefCount(cmd->u.proc.bodyObjPtr);
+ interp->framePtr = callFramePtr;
+
+ /* Simplify arg counting */
+ argv++;
+ argc--;
+
+ /* Set arguments */
+
+ /* Assign in this order:
+ * leftArity required args.
+ * rightArity required args (but actually do it last for simplicity)
+ * optionalArgs optional args
+ * remaining args into 'args' if 'args'
+ */
+
+ /* Note that 'd' steps along the arg list, whilst argc/argv follow the supplied args */
+
+ /* leftArity required args */
+ for (d = 0; d < cmd->u.proc.leftArity; d++) {
+ Jim_ListIndex(interp, cmd->u.proc.argListObjPtr, d, &argObjPtr, JIM_NONE);
+ retcode = JimSetProcArg(interp, argObjPtr, *argv++);
+ if (retcode != JIM_OK) {
+ goto badargset;
+ }
+ argc--;
+ }
+
+ /* Shorten our idea of the number of supplied args */
+ argc -= cmd->u.proc.rightArity;
+
+ /* optionalArgs optional args */
+ for (i = 0; i < cmd->u.proc.optionalArgs; i++) {
+ Jim_Obj *nameObjPtr;
+ Jim_Obj *valueObjPtr;
+
+ Jim_ListIndex(interp, cmd->u.proc.argListObjPtr, d++, &argObjPtr, JIM_NONE);
+
+ /* The name is the first element of the list */
+ Jim_ListIndex(interp, argObjPtr, 0, &nameObjPtr, JIM_NONE);
+ if (argc) {
+ valueObjPtr = *argv++;
+ argc--;
+ }
+ else {
+ /* No more values, so use default */
+ /* The value is the second element of the list */
+ Jim_ListIndex(interp, argObjPtr, 1, &valueObjPtr, JIM_NONE);
+ }
+ Jim_SetVariable(interp, nameObjPtr, valueObjPtr);
+ }
+
+ /* Any remaining args go to 'args' */
+ if (cmd->u.proc.args) {
+ Jim_Obj *listObjPtr = Jim_NewListObj(interp, argv, argc);
+
+ /* Get the 'args' name from the procedure args */
+ Jim_ListIndex(interp, cmd->u.proc.argListObjPtr, d, &argObjPtr, JIM_NONE);
+
+ /* It is possible to rename args. */
+ i = Jim_ListLength(interp, argObjPtr);
+ if (i == 2) {
+ Jim_ListIndex(interp, argObjPtr, 1, &argObjPtr, JIM_NONE);
+ }
+
+ Jim_SetVariable(interp, argObjPtr, listObjPtr);
+ argv += argc;
+ d++;
+ }
+
+ /* rightArity required args */
+ for (i = 0; i < cmd->u.proc.rightArity; i++) {
+ Jim_ListIndex(interp, cmd->u.proc.argListObjPtr, d++, &argObjPtr, JIM_NONE);
+ retcode = JimSetProcArg(interp, argObjPtr, *argv++);
+ if (retcode != JIM_OK) {
+ goto badargset;
+ }
+ }
+
+ /* Install a new stack for local procs */
+ prevLocalProcs = interp->localProcs;
+ interp->localProcs = NULL;
+
+ /* Eval the body */
+ retcode = Jim_EvalObj(interp, cmd->u.proc.bodyObjPtr);
+
+ /* Delete any local procs */
+ JimDeleteLocalProcs(interp);
+ interp->localProcs = prevLocalProcs;
+
+badargset:
+ /* Destroy the callframe */
+ interp->framePtr = interp->framePtr->parentCallFrame;
+ if (callFramePtr->vars.size != JIM_HT_INITIAL_SIZE) {
+ JimFreeCallFrame(interp, callFramePtr, JIM_FCF_NONE);
+ }
+ else {
+ JimFreeCallFrame(interp, callFramePtr, JIM_FCF_NOHT);
+ }
+ /* Handle the JIM_EVAL return code */
+ while (retcode == JIM_EVAL) {
+ Jim_Obj *resultScriptObjPtr = Jim_GetResult(interp);
+
+ Jim_IncrRefCount(resultScriptObjPtr);
+ /* Should be a list! */
+ retcode = Jim_EvalObjList(interp, resultScriptObjPtr, filename, linenr);
+ Jim_DecrRefCount(interp, resultScriptObjPtr);
+ }
+ /* Handle the JIM_RETURN return code */
+ if (retcode == JIM_RETURN) {
+ if (--interp->returnLevel <= 0) {
+ retcode = interp->returnCode;
+ interp->returnCode = JIM_OK;
+ interp->returnLevel = 0;
+ }
+ }
+ else if (retcode == JIM_ERR) {
+ interp->addStackTrace++;
+ Jim_DecrRefCount(interp, interp->errorProc);
+ interp->errorProc = procname;
+ Jim_IncrRefCount(interp->errorProc);
+ }
+ return retcode;
+}
+
+int Jim_Eval_Named(Jim_Interp *interp, const char *script, const char *filename, int lineno)
+{
+ int retval;
+ Jim_Obj *scriptObjPtr;
+
+ scriptObjPtr = Jim_NewStringObj(interp, script, -1);
+ Jim_IncrRefCount(scriptObjPtr);
+
+
+ if (filename) {
+ Jim_Obj *prevScriptObj;
+
+ JimSetSourceInfo(interp, scriptObjPtr, filename, lineno);
+
+ prevScriptObj = interp->currentScriptObj;
+ interp->currentScriptObj = scriptObjPtr;
+
+ retval = Jim_EvalObj(interp, scriptObjPtr);
+
+ interp->currentScriptObj = prevScriptObj;
+ }
+ else {
+ retval = Jim_EvalObj(interp, scriptObjPtr);
+ }
+ Jim_DecrRefCount(interp, scriptObjPtr);
+ return retval;
+}
+
+int Jim_Eval(Jim_Interp *interp, const char *script)
+{
+ return Jim_Eval_Named(interp, script, NULL, 0);
+}
+
+/* Execute script in the scope of the global level */
+int Jim_EvalGlobal(Jim_Interp *interp, const char *script)
+{
+ int retval;
+ Jim_CallFrame *savedFramePtr = interp->framePtr;
+
+ interp->framePtr = interp->topFramePtr;
+ retval = Jim_Eval(interp, script);
+ interp->framePtr = savedFramePtr;
+
+ return retval;
+}
+
+int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename)
+{
+ int retval;
+ Jim_CallFrame *savedFramePtr = interp->framePtr;
+
+ interp->framePtr = interp->topFramePtr;
+ retval = Jim_EvalFile(interp, filename);
+ interp->framePtr = savedFramePtr;
+
+ return retval;
+}
+
+#include <sys/stat.h>
+
+int Jim_EvalFile(Jim_Interp *interp, const char *filename)
+{
+ FILE *fp;
+ char *buf;
+ Jim_Obj *scriptObjPtr;
+ Jim_Obj *prevScriptObj;
+ Jim_Stack *prevLocalProcs;
+ struct stat sb;
+ int retcode;
+ int readlen;
+ char missing;
+
+ if (stat(filename, &sb) != 0 || (fp = fopen(filename, "rt")) == NULL) {
+ Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno));
+ return JIM_ERR;
+ }
+ if (sb.st_size == 0) {
+ fclose(fp);
+ return JIM_OK;
+ }
+
+ buf = Jim_Alloc(sb.st_size + 1);
+ readlen = fread(buf, 1, sb.st_size, fp);
+ if (ferror(fp)) {
+ fclose(fp);
+ Jim_Free(buf);
+ Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno));
+ return JIM_ERR;
+ }
+ fclose(fp);
+ buf[readlen] = 0;
+
+ if (!Jim_ScriptIsComplete(buf, sb.st_size, &missing)) {
+ Jim_SetResultFormatted(interp, "missing %s in \"%s\"",
+ missing == '{' ? "close-brace" : "\"", filename);
+ Jim_Free(buf);
+ return JIM_ERR;
+ }
+
+ scriptObjPtr = Jim_NewStringObjNoAlloc(interp, buf, readlen);
+ JimSetSourceInfo(interp, scriptObjPtr, filename, 1);
+ Jim_IncrRefCount(scriptObjPtr);
+
+ prevScriptObj = interp->currentScriptObj;
+ interp->currentScriptObj = scriptObjPtr;
+
+ /* Install a new stack for local procs */
+ prevLocalProcs = interp->localProcs;
+ interp->localProcs = NULL;
+
+ retcode = Jim_EvalObj(interp, scriptObjPtr);
+
+ /* Delete any local procs */
+ JimDeleteLocalProcs(interp);
+ interp->localProcs = prevLocalProcs;
+
+ /* Handle the JIM_RETURN return code */
+ if (retcode == JIM_RETURN) {
+ if (--interp->returnLevel <= 0) {
+ retcode = interp->returnCode;
+ interp->returnCode = JIM_OK;
+ interp->returnLevel = 0;
+ }
+ }
+ if (retcode == JIM_ERR) {
+ /* EvalFile changes context, so add a stack frame here */
+ interp->addStackTrace++;
+ }
+
+ interp->currentScriptObj = prevScriptObj;
+
+ Jim_DecrRefCount(interp, scriptObjPtr);
+
+ return retcode;
+}
+
+/* -----------------------------------------------------------------------------
+ * Subst
+ * ---------------------------------------------------------------------------*/
+static int JimParseSubstStr(struct JimParserCtx *pc)
+{
+ pc->tstart = pc->p;
+ pc->tline = pc->linenr;
+ while (pc->len && *pc->p != '$' && *pc->p != '[') {
+ if (*pc->p == '\\' && pc->len > 1) {
+ pc->p++;
+ pc->len--;
+ }
+ pc->p++;
+ pc->len--;
+ }
+ pc->tend = pc->p - 1;
+ pc->tt = JIM_TT_ESC;
+ return JIM_OK;
+}
+
+static int JimParseSubst(struct JimParserCtx *pc, int flags)
+{
+ int retval;
+
+ if (pc->len == 0) {
+ pc->tstart = pc->tend = pc->p;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_EOL;
+ pc->eof = 1;
+ return JIM_OK;
+ }
+ switch (*pc->p) {
+ case '[':
+ retval = JimParseCmd(pc);
+ if (flags & JIM_SUBST_NOCMD) {
+ pc->tstart--;
+ pc->tend++;
+ pc->tt = (flags & JIM_SUBST_NOESC) ? JIM_TT_STR : JIM_TT_ESC;
+ }
+ return retval;
+ break;
+ case '$':
+ if (JimParseVar(pc) == JIM_ERR) {
+ pc->tstart = pc->tend = pc->p++;
+ pc->len--;
+ pc->tline = pc->linenr;
+ pc->tt = JIM_TT_STR;
+ }
+ else {
+ if (flags & JIM_SUBST_NOVAR) {
+ pc->tstart--;
+ if (flags & JIM_SUBST_NOESC)
+ pc->tt = JIM_TT_STR;
+ else
+ pc->tt = JIM_TT_ESC;
+ if (*pc->tstart == '{') {
+ pc->tstart--;
+ if (*(pc->tend + 1))
+ pc->tend++;
+ }
+ }
+ }
+ break;
+ default:
+ retval = JimParseSubstStr(pc);
+ if (flags & JIM_SUBST_NOESC)
+ pc->tt = JIM_TT_STR;
+ return retval;
+ break;
+ }
+ return JIM_OK;
+}
+
+/* The subst object type reuses most of the data structures and functions
+ * of the script object. Script's data structures are a bit more complex
+ * for what is needed for [subst]itution tasks, but the reuse helps to
+ * deal with a single data structure at the cost of some more memory
+ * usage for substitutions. */
+
+/* This method takes the string representation of an object
+ * as a Tcl string where to perform [subst]itution, and generates
+ * the pre-parsed internal representation. */
+static int SetSubstFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr, int flags)
+{
+ int scriptTextLen;
+ const char *scriptText = Jim_GetString(objPtr, &scriptTextLen);
+ struct JimParserCtx parser;
+ struct ScriptObj *script = Jim_Alloc(sizeof(*script));
+ ParseTokenList tokenlist;
+
+ /* Initially parse the subst into tokens (in tokenlist) */
+ ScriptTokenListInit(&tokenlist);
+
+ JimParserInit(&parser, scriptText, scriptTextLen, 1);
+ while (1) {
+ JimParseSubst(&parser, flags);
+ if (JimParserEof(&parser)) {
+ /* Note that subst doesn't need the EOL token */
+ break;
+ }
+ ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt,
+ parser.tline);
+ }
+
+ /* Create the "real" subst/script tokens from the initial token list */
+ script->inUse = 1;
+ script->substFlags = flags;
+ script->fileName = NULL;
+ SubstObjAddTokens(interp, script, &tokenlist);
+
+ /* No longer need the token list */
+ ScriptTokenListFree(&tokenlist);
+
+#ifdef DEBUG_SHOW_SUBST
+ {
+ int i;
+
+ printf("==== Subst ====\n");
+ for (i = 0; i < script->len; i++) {
+ printf("[%2d] %s '%s'\n", i, jim_tt_name(script->token[i].type),
+ Jim_String(script->token[i].objPtr));
+ }
+ }
+#endif
+
+ /* Free the old internal rep and set the new one. */
+ Jim_FreeIntRep(interp, objPtr);
+ Jim_SetIntRepPtr(objPtr, script);
+ objPtr->typePtr = &scriptObjType;
+ return JIM_OK;
+}
+
+static ScriptObj *Jim_GetSubst(Jim_Interp *interp, Jim_Obj *objPtr, int flags)
+{
+ if (objPtr->typePtr != &scriptObjType || ((ScriptObj *)Jim_GetIntRepPtr(objPtr))->substFlags != flags)
+ SetSubstFromAny(interp, objPtr, flags);
+ return (ScriptObj *) Jim_GetIntRepPtr(objPtr);
+}
+
+/* Performs commands,variables,blackslashes substitution,
+ * storing the result object (with refcount 0) into
+ * resObjPtrPtr. */
+int Jim_SubstObj(Jim_Interp *interp, Jim_Obj *substObjPtr, Jim_Obj **resObjPtrPtr, int flags)
+{
+ ScriptObj *script = Jim_GetSubst(interp, substObjPtr, flags);
+
+ Jim_IncrRefCount(substObjPtr); /* Make sure it's shared. */
+ /* In order to preserve the internal rep, we increment the
+ * inUse field of the script internal rep structure. */
+ script->inUse++;
+
+ *resObjPtrPtr = JimInterpolateTokens(interp, script->token, script->len, flags);
+
+ script->inUse--;
+ Jim_DecrRefCount(interp, substObjPtr);
+ if (*resObjPtrPtr == NULL) {
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Core commands utility functions
+ * ---------------------------------------------------------------------------*/
+void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg)
+{
+ int i;
+ Jim_Obj *objPtr = Jim_NewEmptyStringObj(interp);
+
+ Jim_AppendString(interp, objPtr, "wrong # args: should be \"", -1);
+ for (i = 0; i < argc; i++) {
+ Jim_AppendObj(interp, objPtr, argv[i]);
+ if (!(i + 1 == argc && msg[0] == '\0'))
+ Jim_AppendString(interp, objPtr, " ", 1);
+ }
+ Jim_AppendString(interp, objPtr, msg, -1);
+ Jim_AppendString(interp, objPtr, "\"", 1);
+ Jim_SetResult(interp, objPtr);
+}
+
+#define JimTrivialMatch(pattern) (strpbrk((pattern), "*[?\\") == NULL)
+
+/* type is: 0=commands, 1=procs, 2=channels */
+static Jim_Obj *JimCommandsList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int type)
+{
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+ Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ /* Check for the non-pattern case. We can do this much more efficiently. */
+ if (patternObjPtr && JimTrivialMatch(Jim_String(patternObjPtr))) {
+ Jim_Cmd *cmdPtr = Jim_GetCommand(interp, patternObjPtr, JIM_NONE);
+ if (cmdPtr) {
+ if (type == 1 && !cmdPtr->isproc) {
+ /* not a proc */
+ }
+ else if (type == 2 && !Jim_AioFilehandle(interp, patternObjPtr)) {
+ /* not a channel */
+ }
+ else {
+ Jim_ListAppendElement(interp, listObjPtr, patternObjPtr);
+ }
+ }
+ return listObjPtr;
+ }
+
+ htiter = Jim_GetHashTableIterator(&interp->commands);
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ Jim_Cmd *cmdPtr = he->val;
+ Jim_Obj *cmdNameObj;
+
+ if (type == 1 && !cmdPtr->isproc) {
+ /* not a proc */
+ continue;
+ }
+ if (patternObjPtr && !JimStringMatch(interp, patternObjPtr, he->key, 0))
+ continue;
+
+ cmdNameObj = Jim_NewStringObj(interp, he->key, -1);
+
+ /* Is it a channel? */
+ if (type == 2 && !Jim_AioFilehandle(interp, cmdNameObj)) {
+ Jim_FreeNewObj(interp, cmdNameObj);
+ continue;
+ }
+
+ Jim_ListAppendElement(interp, listObjPtr, cmdNameObj);
+ }
+ Jim_FreeHashTableIterator(htiter);
+ return listObjPtr;
+}
+
+/* Keep this in order */
+#define JIM_VARLIST_GLOBALS 0
+#define JIM_VARLIST_LOCALS 1
+#define JIM_VARLIST_VARS 2
+
+static Jim_Obj *JimVariablesList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int mode)
+{
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+ Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ if (mode == JIM_VARLIST_GLOBALS) {
+ htiter = Jim_GetHashTableIterator(&interp->topFramePtr->vars);
+ }
+ else {
+ /* For [info locals], if we are at top level an emtpy list
+ * is returned. I don't agree, but we aim at compatibility (SS) */
+ if (mode == JIM_VARLIST_LOCALS && interp->framePtr == interp->topFramePtr)
+ return listObjPtr;
+ htiter = Jim_GetHashTableIterator(&interp->framePtr->vars);
+ }
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ Jim_Var *varPtr = (Jim_Var *)he->val;
+
+ if (mode == JIM_VARLIST_LOCALS) {
+ if (varPtr->linkFramePtr != NULL)
+ continue;
+ }
+ if (patternObjPtr && !JimStringMatch(interp, patternObjPtr, he->key, 0))
+ continue;
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, he->key, -1));
+ }
+ Jim_FreeHashTableIterator(htiter);
+ return listObjPtr;
+}
+
+static int JimInfoLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr,
+ Jim_Obj **objPtrPtr, int info_level_cmd)
+{
+ Jim_CallFrame *targetCallFrame;
+
+ targetCallFrame = JimGetCallFrameByInteger(interp, levelObjPtr);
+ if (targetCallFrame == NULL) {
+ return JIM_ERR;
+ }
+ /* No proc call at toplevel callframe */
+ if (targetCallFrame == interp->topFramePtr) {
+ Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr);
+ return JIM_ERR;
+ }
+ if (info_level_cmd) {
+ *objPtrPtr = Jim_NewListObj(interp, targetCallFrame->argv, targetCallFrame->argc);
+ }
+ else {
+ Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
+
+ Jim_ListAppendElement(interp, listObj, targetCallFrame->argv[0]);
+ Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp,
+ targetCallFrame->filename ? targetCallFrame->filename : "", -1));
+ Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, targetCallFrame->line));
+ *objPtrPtr = listObj;
+ }
+ return JIM_OK;
+}
+
+/* -----------------------------------------------------------------------------
+ * Core commands
+ * ---------------------------------------------------------------------------*/
+
+/* fake [puts] -- not the real puts, just for debugging. */
+static int Jim_PutsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "?-nonewline? string");
+ return JIM_ERR;
+ }
+ if (argc == 3) {
+ if (!Jim_CompareStringImmediate(interp, argv[1], "-nonewline")) {
+ Jim_SetResultString(interp, "The second argument must " "be -nonewline", -1);
+ return JIM_ERR;
+ }
+ else {
+ fputs(Jim_String(argv[2]), stdout);
+ }
+ }
+ else {
+ puts(Jim_String(argv[1]));
+ }
+ return JIM_OK;
+}
+
+/* Helper for [+] and [*] */
+static int JimAddMulHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op)
+{
+ jim_wide wideValue, res;
+ double doubleValue, doubleRes;
+ int i;
+
+ res = (op == JIM_EXPROP_ADD) ? 0 : 1;
+
+ for (i = 1; i < argc; i++) {
+ if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK)
+ goto trydouble;
+ if (op == JIM_EXPROP_ADD)
+ res += wideValue;
+ else
+ res *= wideValue;
+ }
+ Jim_SetResultInt(interp, res);
+ return JIM_OK;
+ trydouble:
+ doubleRes = (double)res;
+ for (; i < argc; i++) {
+ if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK)
+ return JIM_ERR;
+ if (op == JIM_EXPROP_ADD)
+ doubleRes += doubleValue;
+ else
+ doubleRes *= doubleValue;
+ }
+ Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes));
+ return JIM_OK;
+}
+
+/* Helper for [-] and [/] */
+static int JimSubDivHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int op)
+{
+ jim_wide wideValue, res = 0;
+ double doubleValue, doubleRes = 0;
+ int i = 2;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "number ?number ... number?");
+ return JIM_ERR;
+ }
+ else if (argc == 2) {
+ /* The arity = 2 case is different. For [- x] returns -x,
+ * while [/ x] returns 1/x. */
+ if (Jim_GetWide(interp, argv[1], &wideValue) != JIM_OK) {
+ if (Jim_GetDouble(interp, argv[1], &doubleValue) != JIM_OK) {
+ return JIM_ERR;
+ }
+ else {
+ if (op == JIM_EXPROP_SUB)
+ doubleRes = -doubleValue;
+ else
+ doubleRes = 1.0 / doubleValue;
+ Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes));
+ return JIM_OK;
+ }
+ }
+ if (op == JIM_EXPROP_SUB) {
+ res = -wideValue;
+ Jim_SetResultInt(interp, res);
+ }
+ else {
+ doubleRes = 1.0 / wideValue;
+ Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes));
+ }
+ return JIM_OK;
+ }
+ else {
+ if (Jim_GetWide(interp, argv[1], &res) != JIM_OK) {
+ if (Jim_GetDouble(interp, argv[1], &doubleRes)
+ != JIM_OK) {
+ return JIM_ERR;
+ }
+ else {
+ goto trydouble;
+ }
+ }
+ }
+ for (i = 2; i < argc; i++) {
+ if (Jim_GetWide(interp, argv[i], &wideValue) != JIM_OK) {
+ doubleRes = (double)res;
+ goto trydouble;
+ }
+ if (op == JIM_EXPROP_SUB)
+ res -= wideValue;
+ else
+ res /= wideValue;
+ }
+ Jim_SetResultInt(interp, res);
+ return JIM_OK;
+ trydouble:
+ for (; i < argc; i++) {
+ if (Jim_GetDouble(interp, argv[i], &doubleValue) != JIM_OK)
+ return JIM_ERR;
+ if (op == JIM_EXPROP_SUB)
+ doubleRes -= doubleValue;
+ else
+ doubleRes /= doubleValue;
+ }
+ Jim_SetResult(interp, Jim_NewDoubleObj(interp, doubleRes));
+ return JIM_OK;
+}
+
+
+/* [+] */
+static int Jim_AddCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_ADD);
+}
+
+/* [*] */
+static int Jim_MulCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimAddMulHelper(interp, argc, argv, JIM_EXPROP_MUL);
+}
+
+/* [-] */
+static int Jim_SubCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_SUB);
+}
+
+/* [/] */
+static int Jim_DivCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimSubDivHelper(interp, argc, argv, JIM_EXPROP_DIV);
+}
+
+/* [set] */
+static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?newValue?");
+ return JIM_ERR;
+ }
+ if (argc == 2) {
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG);
+ if (!objPtr)
+ return JIM_ERR;
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+ /* argc == 3 case. */
+ if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK)
+ return JIM_ERR;
+ Jim_SetResult(interp, argv[2]);
+ return JIM_OK;
+}
+
+/* [unset]
+ *
+ * unset ?-nocomplain? ?--? ?varName ...?
+ */
+static int Jim_UnsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i = 1;
+ int complain = 1;
+
+ while (i < argc) {
+ if (Jim_CompareStringImmediate(interp, argv[i], "--")) {
+ i++;
+ break;
+ }
+ if (Jim_CompareStringImmediate(interp, argv[i], "-nocomplain")) {
+ complain = 0;
+ i++;
+ continue;
+ }
+ break;
+ }
+
+ while (i < argc) {
+ if (Jim_UnsetVariable(interp, argv[i], complain ? JIM_ERRMSG : JIM_NONE) != JIM_OK
+ && complain) {
+ return JIM_ERR;
+ }
+ i++;
+ }
+ return JIM_OK;
+}
+
+/* [while] */
+static int Jim_WhileCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "condition body");
+ return JIM_ERR;
+ }
+
+ /* The general purpose implementation of while starts here */
+ while (1) {
+ int boolean, retval;
+
+ if ((retval = Jim_GetBoolFromExpr(interp, argv[1], &boolean)) != JIM_OK)
+ return retval;
+ if (!boolean)
+ break;
+
+ if ((retval = Jim_EvalObj(interp, argv[2])) != JIM_OK) {
+ switch (retval) {
+ case JIM_BREAK:
+ goto out;
+ break;
+ case JIM_CONTINUE:
+ continue;
+ break;
+ default:
+ return retval;
+ }
+ }
+ }
+ out:
+ Jim_SetEmptyResult(interp);
+ return JIM_OK;
+}
+
+/* [for] */
+static int Jim_ForCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int retval;
+ int boolean = 1;
+ Jim_Obj *varNamePtr = NULL;
+ Jim_Obj *stopVarNamePtr = NULL;
+
+ if (argc != 5) {
+ Jim_WrongNumArgs(interp, 1, argv, "start test next body");
+ return JIM_ERR;
+ }
+
+ /* Do the initialisation */
+ if ((retval = Jim_EvalObj(interp, argv[1])) != JIM_OK) {
+ return retval;
+ }
+
+ /* And do the first test now. Better for optimisation
+ * if we can do next/test at the bottom of the loop
+ */
+ retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean);
+
+ /* Ready to do the body as follows:
+ * while (1) {
+ * body // check retcode
+ * next // check retcode
+ * test // check retcode/test bool
+ * }
+ */
+
+#ifdef JIM_OPTIMIZATION
+ /* Check if the for is on the form:
+ * for ... {$i < CONST} {incr i}
+ * for ... {$i < $j} {incr i}
+ */
+ if (retval == JIM_OK && boolean) {
+ ScriptObj *incrScript;
+ ExprByteCode *expr;
+ jim_wide stop, currentVal;
+ unsigned jim_wide procEpoch;
+ Jim_Obj *objPtr;
+ int cmpOffset;
+
+ /* Do it only if there aren't shared arguments */
+ expr = JimGetExpression(interp, argv[2]);
+ incrScript = Jim_GetScript(interp, argv[3]);
+
+ /* Ensure proper lengths to start */
+ if (incrScript->len != 3 || !expr || expr->len != 3) {
+ goto evalstart;
+ }
+ /* Ensure proper token types. */
+ if (incrScript->token[1].type != JIM_TT_ESC ||
+ expr->token[0].type != JIM_TT_VAR ||
+ (expr->token[1].type != JIM_TT_EXPR_INT && expr->token[1].type != JIM_TT_VAR)) {
+ goto evalstart;
+ }
+
+ if (expr->token[2].type == JIM_EXPROP_LT) {
+ cmpOffset = 0;
+ }
+ else if (expr->token[2].type == JIM_EXPROP_LTE) {
+ cmpOffset = 1;
+ }
+ else {
+ goto evalstart;
+ }
+
+ /* Update command must be incr */
+ if (!Jim_CompareStringImmediate(interp, incrScript->token[1].objPtr, "incr")) {
+ goto evalstart;
+ }
+
+ /* incr, expression must be about the same variable */
+ if (!Jim_StringEqObj(incrScript->token[2].objPtr, expr->token[0].objPtr)) {
+ goto evalstart;
+ }
+
+ /* Get the stop condition (must be a variable or integer) */
+ if (expr->token[1].type == JIM_TT_EXPR_INT) {
+ if (Jim_GetWide(interp, expr->token[1].objPtr, &stop) == JIM_ERR) {
+ goto evalstart;
+ }
+ }
+ else {
+ stopVarNamePtr = expr->token[1].objPtr;
+ Jim_IncrRefCount(stopVarNamePtr);
+ /* Keep the compiler happy */
+ stop = 0;
+ }
+
+ /* Initialization */
+ procEpoch = interp->procEpoch;
+ varNamePtr = expr->token[0].objPtr;
+ Jim_IncrRefCount(varNamePtr);
+
+ objPtr = Jim_GetVariable(interp, varNamePtr, JIM_NONE);
+ if (objPtr == NULL || Jim_GetWide(interp, objPtr, &currentVal) != JIM_OK) {
+ goto testcond;
+ }
+
+ /* --- OPTIMIZED FOR --- */
+ while (retval == JIM_OK) {
+ /* === Check condition === */
+ /* Note that currentVal is already set here */
+
+ /* Immediate or Variable? get the 'stop' value if the latter. */
+ if (stopVarNamePtr) {
+ objPtr = Jim_GetVariable(interp, stopVarNamePtr, JIM_NONE);
+ if (objPtr == NULL || Jim_GetWide(interp, objPtr, &stop) != JIM_OK) {
+ goto testcond;
+ }
+ }
+
+ if (currentVal >= stop + cmpOffset) {
+ break;
+ }
+
+ /* Eval body */
+ retval = Jim_EvalObj(interp, argv[4]);
+ if (retval == JIM_OK || retval == JIM_CONTINUE) {
+ retval = JIM_OK;
+ /* If there was a change in procedures/command continue
+ * with the usual [for] command implementation */
+ if (procEpoch != interp->procEpoch) {
+ goto evalnext;
+ }
+
+ objPtr = Jim_GetVariable(interp, varNamePtr, JIM_ERRMSG);
+
+ /* Increment */
+ if (objPtr == NULL) {
+ retval = JIM_ERR;
+ goto out;
+ }
+ if (!Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) {
+ currentVal = ++JimWideValue(objPtr);
+ Jim_InvalidateStringRep(objPtr);
+ }
+ else {
+ if (Jim_GetWide(interp, objPtr, &currentVal) != JIM_OK ||
+ Jim_SetVariable(interp, varNamePtr, Jim_NewIntObj(interp,
+ ++currentVal)) != JIM_OK) {
+ goto evalnext;
+ }
+ }
+ }
+ }
+ goto out;
+ }
+ evalstart:
+#endif
+
+ while (boolean && (retval == JIM_OK || retval == JIM_CONTINUE)) {
+ /* Body */
+ retval = Jim_EvalObj(interp, argv[4]);
+
+ if (retval == JIM_OK || retval == JIM_CONTINUE) {
+ /* increment */
+ evalnext:
+ retval = Jim_EvalObj(interp, argv[3]);
+ if (retval == JIM_OK || retval == JIM_CONTINUE) {
+ /* test */
+ testcond:
+ retval = Jim_GetBoolFromExpr(interp, argv[2], &boolean);
+ }
+ }
+ }
+ out:
+ if (stopVarNamePtr) {
+ Jim_DecrRefCount(interp, stopVarNamePtr);
+ }
+ if (varNamePtr) {
+ Jim_DecrRefCount(interp, varNamePtr);
+ }
+
+ if (retval == JIM_CONTINUE || retval == JIM_BREAK || retval == JIM_OK) {
+ Jim_SetEmptyResult(interp);
+ return JIM_OK;
+ }
+
+ return retval;
+}
+
+/* [loop] */
+static int Jim_LoopCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int retval;
+ jim_wide i;
+ jim_wide limit;
+ jim_wide incr = 1;
+ Jim_Obj *bodyObjPtr;
+
+ if (argc != 5 && argc != 6) {
+ Jim_WrongNumArgs(interp, 1, argv, "var first limit ?incr? body");
+ return JIM_ERR;
+ }
+
+ if (Jim_GetWide(interp, argv[2], &i) != JIM_OK ||
+ Jim_GetWide(interp, argv[3], &limit) != JIM_OK ||
+ (argc == 6 && Jim_GetWide(interp, argv[4], &incr) != JIM_OK)) {
+ return JIM_ERR;
+ }
+ bodyObjPtr = (argc == 5) ? argv[4] : argv[5];
+
+ retval = Jim_SetVariable(interp, argv[1], argv[2]);
+
+ while (((i < limit && incr > 0) || (i > limit && incr < 0)) && retval == JIM_OK) {
+ retval = Jim_EvalObj(interp, bodyObjPtr);
+ if (retval == JIM_OK || retval == JIM_CONTINUE) {
+ Jim_Obj *objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG);
+
+ retval = JIM_OK;
+
+ /* Increment */
+ i += incr;
+
+ if (objPtr && !Jim_IsShared(objPtr) && objPtr->typePtr == &intObjType) {
+ if (argv[1]->typePtr != &variableObjType) {
+ if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+ JimWideValue(objPtr) = i;
+ Jim_InvalidateStringRep(objPtr);
+
+ /* The following step is required in order to invalidate the
+ * string repr of "FOO" if the var name is of the form of "FOO(IDX)" */
+ if (argv[1]->typePtr != &variableObjType) {
+ if (Jim_SetVariable(interp, argv[1], objPtr) != JIM_OK) {
+ retval = JIM_ERR;
+ break;
+ }
+ }
+ }
+ else {
+ objPtr = Jim_NewIntObj(interp, i);
+ retval = Jim_SetVariable(interp, argv[1], objPtr);
+ if (retval != JIM_OK) {
+ Jim_FreeNewObj(interp, objPtr);
+ }
+ }
+ }
+ }
+
+ if (retval == JIM_OK || retval == JIM_CONTINUE || retval == JIM_BREAK) {
+ Jim_SetEmptyResult(interp);
+ return JIM_OK;
+ }
+ return retval;
+}
+
+/* foreach + lmap implementation. */
+static int JimForeachMapHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int doMap)
+{
+ int result = JIM_ERR, i, nbrOfLists, *listsIdx, *listsEnd;
+ int nbrOfLoops = 0;
+ Jim_Obj *emptyStr, *script, *mapRes = NULL;
+
+ if (argc < 4 || argc % 2 != 0) {
+ Jim_WrongNumArgs(interp, 1, argv, "varList list ?varList list ...? script");
+ return JIM_ERR;
+ }
+ if (doMap) {
+ mapRes = Jim_NewListObj(interp, NULL, 0);
+ Jim_IncrRefCount(mapRes);
+ }
+ emptyStr = Jim_NewEmptyStringObj(interp);
+ Jim_IncrRefCount(emptyStr);
+ script = argv[argc - 1]; /* Last argument is a script */
+ nbrOfLists = (argc - 1 - 1) / 2; /* argc - 'foreach' - script */
+ listsIdx = (int *)Jim_Alloc(nbrOfLists * sizeof(int));
+ listsEnd = (int *)Jim_Alloc(nbrOfLists * 2 * sizeof(int));
+ /* Initialize iterators and remember max nbr elements each list */
+ memset(listsIdx, 0, nbrOfLists * sizeof(int));
+ /* Remember lengths of all lists and calculate how much rounds to loop */
+ for (i = 0; i < nbrOfLists * 2; i += 2) {
+ div_t cnt;
+ int count;
+
+ listsEnd[i] = Jim_ListLength(interp, argv[i + 1]);
+ listsEnd[i + 1] = Jim_ListLength(interp, argv[i + 2]);
+ if (listsEnd[i] == 0) {
+ Jim_SetResultString(interp, "foreach varlist is empty", -1);
+ goto err;
+ }
+ cnt = div(listsEnd[i + 1], listsEnd[i]);
+ count = cnt.quot + (cnt.rem ? 1 : 0);
+ if (count > nbrOfLoops)
+ nbrOfLoops = count;
+ }
+ for (; nbrOfLoops-- > 0;) {
+ for (i = 0; i < nbrOfLists; ++i) {
+ int varIdx = 0, var = i * 2;
+
+ while (varIdx < listsEnd[var]) {
+ Jim_Obj *varName, *ele;
+ int lst = i * 2 + 1;
+
+ /* List index operations below can't fail */
+ Jim_ListIndex(interp, argv[var + 1], varIdx, &varName, JIM_NONE);
+ if (listsIdx[i] < listsEnd[lst]) {
+ Jim_ListIndex(interp, argv[lst + 1], listsIdx[i], &ele, JIM_NONE);
+ /* Avoid shimmering */
+ Jim_IncrRefCount(ele);
+ result = Jim_SetVariable(interp, varName, ele);
+ Jim_DecrRefCount(interp, ele);
+ if (result == JIM_OK) {
+ ++listsIdx[i]; /* Remember next iterator of current list */
+ ++varIdx; /* Next variable */
+ continue;
+ }
+ }
+ else if (Jim_SetVariable(interp, varName, emptyStr) == JIM_OK) {
+ ++varIdx; /* Next variable */
+ continue;
+ }
+ goto err;
+ }
+ }
+ switch (result = Jim_EvalObj(interp, script)) {
+ case JIM_OK:
+ if (doMap)
+ Jim_ListAppendElement(interp, mapRes, interp->result);
+ break;
+ case JIM_CONTINUE:
+ break;
+ case JIM_BREAK:
+ goto out;
+ break;
+ default:
+ goto err;
+ }
+ }
+ out:
+ result = JIM_OK;
+ if (doMap)
+ Jim_SetResult(interp, mapRes);
+ else
+ Jim_SetEmptyResult(interp);
+ err:
+ if (doMap)
+ Jim_DecrRefCount(interp, mapRes);
+ Jim_DecrRefCount(interp, emptyStr);
+ Jim_Free(listsIdx);
+ Jim_Free(listsEnd);
+ return result;
+}
+
+/* [foreach] */
+static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimForeachMapHelper(interp, argc, argv, 0);
+}
+
+/* [lmap] */
+static int Jim_LmapCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ return JimForeachMapHelper(interp, argc, argv, 1);
+}
+
+/* [if] */
+static int Jim_IfCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int boolean, retval, current = 1, falsebody = 0;
+
+ if (argc >= 3) {
+ while (1) {
+ /* Far not enough arguments given! */
+ if (current >= argc)
+ goto err;
+ if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean))
+ != JIM_OK)
+ return retval;
+ /* There lacks something, isn't it? */
+ if (current >= argc)
+ goto err;
+ if (Jim_CompareStringImmediate(interp, argv[current], "then"))
+ current++;
+ /* Tsk tsk, no then-clause? */
+ if (current >= argc)
+ goto err;
+ if (boolean)
+ return Jim_EvalObj(interp, argv[current]);
+ /* Ok: no else-clause follows */
+ if (++current >= argc) {
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ return JIM_OK;
+ }
+ falsebody = current++;
+ if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) {
+ /* IIICKS - else-clause isn't last cmd? */
+ if (current != argc - 1)
+ goto err;
+ return Jim_EvalObj(interp, argv[current]);
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif"))
+ /* Ok: elseif follows meaning all the stuff
+ * again (how boring...) */
+ continue;
+ /* OOPS - else-clause is not last cmd? */
+ else if (falsebody != argc - 1)
+ goto err;
+ return Jim_EvalObj(interp, argv[falsebody]);
+ }
+ return JIM_OK;
+ }
+ err:
+ Jim_WrongNumArgs(interp, 1, argv, "condition ?then? trueBody ?elseif ...? ?else? falseBody");
+ return JIM_ERR;
+}
+
+
+/* Returns 1 if match, 0 if no match or -<error> on error (e.g. -JIM_ERR, -JIM_BREAK)*/
+int Jim_CommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj,
+ Jim_Obj *stringObj, int nocase)
+{
+ Jim_Obj *parms[4];
+ int argc = 0;
+ long eq;
+ int rc;
+
+ parms[argc++] = commandObj;
+ if (nocase) {
+ parms[argc++] = Jim_NewStringObj(interp, "-nocase", -1);
+ }
+ parms[argc++] = patternObj;
+ parms[argc++] = stringObj;
+
+ rc = Jim_EvalObjVector(interp, argc, parms);
+
+ if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) {
+ eq = -rc;
+ }
+
+ return eq;
+}
+
+enum
+{ SWITCH_EXACT, SWITCH_GLOB, SWITCH_RE, SWITCH_CMD };
+
+/* [switch] */
+static int Jim_SwitchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int matchOpt = SWITCH_EXACT, opt = 1, patCount, i;
+ Jim_Obj *command = 0, *const *caseList = 0, *strObj;
+ Jim_Obj *script = 0;
+
+ if (argc < 3) {
+ wrongnumargs:
+ Jim_WrongNumArgs(interp, 1, argv, "?options? string "
+ "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}");
+ return JIM_ERR;
+ }
+ for (opt = 1; opt < argc; ++opt) {
+ const char *option = Jim_GetString(argv[opt], 0);
+
+ if (*option != '-')
+ break;
+ else if (strncmp(option, "--", 2) == 0) {
+ ++opt;
+ break;
+ }
+ else if (strncmp(option, "-exact", 2) == 0)
+ matchOpt = SWITCH_EXACT;
+ else if (strncmp(option, "-glob", 2) == 0)
+ matchOpt = SWITCH_GLOB;
+ else if (strncmp(option, "-regexp", 2) == 0)
+ matchOpt = SWITCH_RE;
+ else if (strncmp(option, "-command", 2) == 0) {
+ matchOpt = SWITCH_CMD;
+ if ((argc - opt) < 2)
+ goto wrongnumargs;
+ command = argv[++opt];
+ }
+ else {
+ Jim_SetResultFormatted(interp,
+ "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --",
+ argv[opt]);
+ return JIM_ERR;
+ }
+ if ((argc - opt) < 2)
+ goto wrongnumargs;
+ }
+ strObj = argv[opt++];
+ patCount = argc - opt;
+ if (patCount == 1) {
+ Jim_Obj **vector;
+
+ JimListGetElements(interp, argv[opt], &patCount, &vector);
+ caseList = vector;
+ }
+ else
+ caseList = &argv[opt];
+ if (patCount == 0 || patCount % 2 != 0)
+ goto wrongnumargs;
+ for (i = 0; script == 0 && i < patCount; i += 2) {
+ Jim_Obj *patObj = caseList[i];
+
+ if (!Jim_CompareStringImmediate(interp, patObj, "default")
+ || i < (patCount - 2)) {
+ switch (matchOpt) {
+ case SWITCH_EXACT:
+ if (Jim_StringEqObj(strObj, patObj))
+ script = caseList[i + 1];
+ break;
+ case SWITCH_GLOB:
+ if (Jim_StringMatchObj(interp, patObj, strObj, 0))
+ script = caseList[i + 1];
+ break;
+ case SWITCH_RE:
+ command = Jim_NewStringObj(interp, "regexp", -1);
+ /* Fall thru intentionally */
+ case SWITCH_CMD:{
+ int rc = Jim_CommandMatchObj(interp, command, patObj, strObj, 0);
+
+ /* After the execution of a command we need to
+ * make sure to reconvert the object into a list
+ * again. Only for the single-list style [switch]. */
+ if (argc - opt == 1) {
+ Jim_Obj **vector;
+
+ JimListGetElements(interp, argv[opt], &patCount, &vector);
+ caseList = vector;
+ }
+ /* command is here already decref'd */
+ if (rc < 0) {
+ return -rc;
+ }
+ if (rc)
+ script = caseList[i + 1];
+ break;
+ }
+ }
+ }
+ else {
+ script = caseList[i + 1];
+ }
+ }
+ for (; i < patCount && Jim_CompareStringImmediate(interp, script, "-"); i += 2)
+ script = caseList[i + 1];
+ if (script && Jim_CompareStringImmediate(interp, script, "-")) {
+ Jim_SetResultFormatted(interp, "no body specified for pattern \"%#s\"", caseList[i - 2]);
+ return JIM_ERR;
+ }
+ Jim_SetEmptyResult(interp);
+ if (script) {
+ return Jim_EvalObj(interp, script);
+ }
+ return JIM_OK;
+}
+
+/* [list] */
+static int Jim_ListCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *listObjPtr;
+
+ listObjPtr = Jim_NewListObj(interp, argv + 1, argc - 1);
+ Jim_SetResult(interp, listObjPtr);
+ return JIM_OK;
+}
+
+/* [lindex] */
+static int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr, *listObjPtr;
+ int i;
+ int idx;
+
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "list index ?...?");
+ return JIM_ERR;
+ }
+ objPtr = argv[1];
+ Jim_IncrRefCount(objPtr);
+ for (i = 2; i < argc; i++) {
+ listObjPtr = objPtr;
+ if (Jim_GetIndex(interp, argv[i], &idx) != JIM_OK) {
+ Jim_DecrRefCount(interp, listObjPtr);
+ return JIM_ERR;
+ }
+ if (Jim_ListIndex(interp, listObjPtr, idx, &objPtr, JIM_NONE) != JIM_OK) {
+ /* Returns an empty object if the index
+ * is out of range. */
+ Jim_DecrRefCount(interp, listObjPtr);
+ Jim_SetEmptyResult(interp);
+ return JIM_OK;
+ }
+ Jim_IncrRefCount(objPtr);
+ Jim_DecrRefCount(interp, listObjPtr);
+ }
+ Jim_SetResult(interp, objPtr);
+ Jim_DecrRefCount(interp, objPtr);
+ return JIM_OK;
+}
+
+/* [llength] */
+static int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "list");
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, Jim_ListLength(interp, argv[1]));
+ return JIM_OK;
+}
+
+/* [lsearch] */
+static int Jim_LsearchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ static const char * const options[] = {
+ "-bool", "-not", "-nocase", "-exact", "-glob", "-regexp", "-all", "-inline", "-command",
+ NULL
+ };
+ enum
+ { OPT_BOOL, OPT_NOT, OPT_NOCASE, OPT_EXACT, OPT_GLOB, OPT_REGEXP, OPT_ALL, OPT_INLINE,
+ OPT_COMMAND };
+ int i;
+ int opt_bool = 0;
+ int opt_not = 0;
+ int opt_nocase = 0;
+ int opt_all = 0;
+ int opt_inline = 0;
+ int opt_match = OPT_EXACT;
+ int listlen;
+ int rc = JIM_OK;
+ Jim_Obj *listObjPtr = NULL;
+ Jim_Obj *commandObj = NULL;
+
+ if (argc < 3) {
+ wrongargs:
+ Jim_WrongNumArgs(interp, 1, argv,
+ "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? list value");
+ return JIM_ERR;
+ }
+
+ for (i = 1; i < argc - 2; i++) {
+ int option;
+
+ if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+ switch (option) {
+ case OPT_BOOL:
+ opt_bool = 1;
+ opt_inline = 0;
+ break;
+ case OPT_NOT:
+ opt_not = 1;
+ break;
+ case OPT_NOCASE:
+ opt_nocase = 1;
+ break;
+ case OPT_INLINE:
+ opt_inline = 1;
+ opt_bool = 0;
+ break;
+ case OPT_ALL:
+ opt_all = 1;
+ break;
+ case OPT_COMMAND:
+ if (i >= argc - 2) {
+ goto wrongargs;
+ }
+ commandObj = argv[++i];
+ /* fallthru */
+ case OPT_EXACT:
+ case OPT_GLOB:
+ case OPT_REGEXP:
+ opt_match = option;
+ break;
+ }
+ }
+
+ argv += i;
+
+ if (opt_all) {
+ listObjPtr = Jim_NewListObj(interp, NULL, 0);
+ }
+ if (opt_match == OPT_REGEXP) {
+ commandObj = Jim_NewStringObj(interp, "regexp", -1);
+ }
+ if (commandObj) {
+ Jim_IncrRefCount(commandObj);
+ }
+
+ listlen = Jim_ListLength(interp, argv[0]);
+ for (i = 0; i < listlen; i++) {
+ Jim_Obj *objPtr;
+ int eq = 0;
+
+ Jim_ListIndex(interp, argv[0], i, &objPtr, JIM_NONE);
+ switch (opt_match) {
+ case OPT_EXACT:
+ eq = Jim_StringCompareObj(interp, objPtr, argv[1], opt_nocase) == 0;
+ break;
+
+ case OPT_GLOB:
+ eq = Jim_StringMatchObj(interp, argv[1], objPtr, opt_nocase);
+ break;
+
+ case OPT_REGEXP:
+ case OPT_COMMAND:
+ eq = Jim_CommandMatchObj(interp, commandObj, argv[1], objPtr, opt_nocase);
+ if (eq < 0) {
+ if (listObjPtr) {
+ Jim_FreeNewObj(interp, listObjPtr);
+ }
+ rc = JIM_ERR;
+ goto done;
+ }
+ break;
+ }
+
+ /* If we have a non-match with opt_bool, opt_not, !opt_all, can't exit early */
+ if (!eq && opt_bool && opt_not && !opt_all) {
+ continue;
+ }
+
+ if ((!opt_bool && eq == !opt_not) || (opt_bool && (eq || opt_all))) {
+ /* Got a match (or non-match for opt_not), or (opt_bool && opt_all) */
+ Jim_Obj *resultObj;
+
+ if (opt_bool) {
+ resultObj = Jim_NewIntObj(interp, eq ^ opt_not);
+ }
+ else if (!opt_inline) {
+ resultObj = Jim_NewIntObj(interp, i);
+ }
+ else {
+ resultObj = objPtr;
+ }
+
+ if (opt_all) {
+ Jim_ListAppendElement(interp, listObjPtr, resultObj);
+ }
+ else {
+ Jim_SetResult(interp, resultObj);
+ goto done;
+ }
+ }
+ }
+
+ if (opt_all) {
+ Jim_SetResult(interp, listObjPtr);
+ }
+ else {
+ /* No match */
+ if (opt_bool) {
+ Jim_SetResultBool(interp, opt_not);
+ }
+ else if (!opt_inline) {
+ Jim_SetResultInt(interp, -1);
+ }
+ }
+
+ done:
+ if (commandObj) {
+ Jim_DecrRefCount(interp, commandObj);
+ }
+ return rc;
+}
+
+/* [lappend] */
+static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *listObjPtr;
+ int shared, i;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?");
+ return JIM_ERR;
+ }
+ listObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
+ if (!listObjPtr) {
+ /* Create the list if it does not exists */
+ listObjPtr = Jim_NewListObj(interp, NULL, 0);
+ if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) {
+ Jim_FreeNewObj(interp, listObjPtr);
+ return JIM_ERR;
+ }
+ }
+ shared = Jim_IsShared(listObjPtr);
+ if (shared)
+ listObjPtr = Jim_DuplicateObj(interp, listObjPtr);
+ for (i = 2; i < argc; i++)
+ Jim_ListAppendElement(interp, listObjPtr, argv[i]);
+ if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) {
+ if (shared)
+ Jim_FreeNewObj(interp, listObjPtr);
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, listObjPtr);
+ return JIM_OK;
+}
+
+/* [linsert] */
+static int Jim_LinsertCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int idx, len;
+ Jim_Obj *listPtr;
+
+ if (argc < 4) {
+ Jim_WrongNumArgs(interp, 1, argv, "list index element " "?element ...?");
+ return JIM_ERR;
+ }
+ listPtr = argv[1];
+ if (Jim_IsShared(listPtr))
+ listPtr = Jim_DuplicateObj(interp, listPtr);
+ if (Jim_GetIndex(interp, argv[2], &idx) != JIM_OK)
+ goto err;
+ len = Jim_ListLength(interp, listPtr);
+ if (idx >= len)
+ idx = len;
+ else if (idx < 0)
+ idx = len + idx + 1;
+ Jim_ListInsertElements(interp, listPtr, idx, argc - 3, &argv[3]);
+ Jim_SetResult(interp, listPtr);
+ return JIM_OK;
+ err:
+ if (listPtr != argv[1]) {
+ Jim_FreeNewObj(interp, listPtr);
+ }
+ return JIM_ERR;
+}
+
+/* [lreplace] */
+static int Jim_LreplaceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int first, last, len, rangeLen;
+ Jim_Obj *listObj;
+ Jim_Obj *newListObj;
+ int i;
+ int shared;
+
+ if (argc < 4) {
+ Jim_WrongNumArgs(interp, 1, argv, "list first last ?element element ...?");
+ return JIM_ERR;
+ }
+ if (Jim_GetIndex(interp, argv[2], &first) != JIM_OK ||
+ Jim_GetIndex(interp, argv[3], &last) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ listObj = argv[1];
+ len = Jim_ListLength(interp, listObj);
+
+ first = JimRelToAbsIndex(len, first);
+ last = JimRelToAbsIndex(len, last);
+ JimRelToAbsRange(len, first, last, &first, &last, &rangeLen);
+
+ /* Now construct a new list which consists of:
+ * <elements before first> <supplied elements> <elements after last>
+ */
+
+ /* Check to see if trying to replace past the end of the list */
+ if (first < len) {
+ /* OK. Not past the end */
+ }
+ else if (len == 0) {
+ /* Special for empty list, adjust first to 0 */
+ first = 0;
+ }
+ else {
+ Jim_SetResultString(interp, "list doesn't contain element ", -1);
+ Jim_AppendObj(interp, Jim_GetResult(interp), argv[2]);
+ return JIM_ERR;
+ }
+
+ newListObj = Jim_NewListObj(interp, NULL, 0);
+
+ shared = Jim_IsShared(listObj);
+ if (shared) {
+ listObj = Jim_DuplicateObj(interp, listObj);
+ }
+
+ /* Add the first set of elements */
+ for (i = 0; i < first; i++) {
+ Jim_ListAppendElement(interp, newListObj, listObj->internalRep.listValue.ele[i]);
+ }
+
+ /* Add supplied elements */
+ for (i = 4; i < argc; i++) {
+ Jim_ListAppendElement(interp, newListObj, argv[i]);
+ }
+
+ /* Add the remaining elements */
+ for (i = first + rangeLen; i < len; i++) {
+ Jim_ListAppendElement(interp, newListObj, listObj->internalRep.listValue.ele[i]);
+ }
+ Jim_SetResult(interp, newListObj);
+ if (shared) {
+ Jim_FreeNewObj(interp, listObj);
+ }
+ return JIM_OK;
+}
+
+/* [lset] */
+static int Jim_LsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "listVar ?index...? newVal");
+ return JIM_ERR;
+ }
+ else if (argc == 3) {
+ if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK)
+ return JIM_ERR;
+ Jim_SetResult(interp, argv[2]);
+ return JIM_OK;
+ }
+ if (Jim_SetListIndex(interp, argv[1], argv + 2, argc - 3, argv[argc - 1])
+ == JIM_ERR)
+ return JIM_ERR;
+ return JIM_OK;
+}
+
+/* [lsort] */
+static int Jim_LsortCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const argv[])
+{
+ const char *options[] = {
+ "-ascii", "-nocase", "-increasing", "-decreasing", "-command", "-integer", "-index", NULL
+ };
+ enum
+ { OPT_ASCII, OPT_NOCASE, OPT_INCREASING, OPT_DECREASING, OPT_COMMAND, OPT_INTEGER, OPT_INDEX };
+ Jim_Obj *resObj;
+ int i;
+ int retCode;
+
+ struct lsort_info info;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "?options? list");
+ return JIM_ERR;
+ }
+
+ info.type = JIM_LSORT_ASCII;
+ info.order = 1;
+ info.indexed = 0;
+ info.command = NULL;
+ info.interp = interp;
+
+ for (i = 1; i < (argc - 1); i++) {
+ int option;
+
+ if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG)
+ != JIM_OK)
+ return JIM_ERR;
+ switch (option) {
+ case OPT_ASCII:
+ info.type = JIM_LSORT_ASCII;
+ break;
+ case OPT_NOCASE:
+ info.type = JIM_LSORT_NOCASE;
+ break;
+ case OPT_INTEGER:
+ info.type = JIM_LSORT_INTEGER;
+ break;
+ case OPT_INCREASING:
+ info.order = 1;
+ break;
+ case OPT_DECREASING:
+ info.order = -1;
+ break;
+ case OPT_COMMAND:
+ if (i >= (argc - 2)) {
+ Jim_SetResultString(interp, "\"-command\" option must be followed by comparison command", -1);
+ return JIM_ERR;
+ }
+ info.type = JIM_LSORT_COMMAND;
+ info.command = argv[i + 1];
+ i++;
+ break;
+ case OPT_INDEX:
+ if (i >= (argc - 2)) {
+ Jim_SetResultString(interp, "\"-index\" option must be followed by list index", -1);
+ return JIM_ERR;
+ }
+ if (Jim_GetIndex(interp, argv[i + 1], &info.index) != JIM_OK) {
+ return JIM_ERR;
+ }
+ info.indexed = 1;
+ i++;
+ break;
+ }
+ }
+ resObj = Jim_DuplicateObj(interp, argv[argc - 1]);
+ retCode = ListSortElements(interp, resObj, &info);
+ if (retCode == JIM_OK) {
+ Jim_SetResult(interp, resObj);
+ }
+ else {
+ Jim_FreeNewObj(interp, resObj);
+ }
+ return retCode;
+}
+
+/* [append] */
+static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *stringObjPtr;
+ int i;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?");
+ return JIM_ERR;
+ }
+ if (argc == 2) {
+ stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG);
+ if (!stringObjPtr)
+ return JIM_ERR;
+ }
+ else {
+ int freeobj = 0;
+ stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
+ if (!stringObjPtr) {
+ /* Create the string if it doesn't exist */
+ stringObjPtr = Jim_NewEmptyStringObj(interp);
+ freeobj = 1;
+ }
+ else if (Jim_IsShared(stringObjPtr)) {
+ freeobj = 1;
+ stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr);
+ }
+ for (i = 2; i < argc; i++) {
+ Jim_AppendObj(interp, stringObjPtr, argv[i]);
+ }
+ if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) {
+ if (freeobj) {
+ Jim_FreeNewObj(interp, stringObjPtr);
+ }
+ return JIM_ERR;
+ }
+ }
+ Jim_SetResult(interp, stringObjPtr);
+ return JIM_OK;
+}
+
+/* [debug] */
+static int Jim_DebugCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+#ifdef JIM_DEBUG_COMMAND
+ const char *options[] = {
+ "refcount", "objcount", "objects", "invstr", "scriptlen", "exprlen",
+ "exprbc", "show",
+ NULL
+ };
+ enum
+ {
+ OPT_REFCOUNT, OPT_OBJCOUNT, OPT_OBJECTS, OPT_INVSTR, OPT_SCRIPTLEN,
+ OPT_EXPRLEN, OPT_EXPRBC, OPT_SHOW,
+ };
+ int option;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "subcommand ?...?");
+ return JIM_ERR;
+ }
+ if (Jim_GetEnum(interp, argv[1], options, &option, "subcommand", JIM_ERRMSG) != JIM_OK)
+ return JIM_ERR;
+ if (option == OPT_REFCOUNT) {
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "object");
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, argv[2]->refCount);
+ return JIM_OK;
+ }
+ else if (option == OPT_OBJCOUNT) {
+ int freeobj = 0, liveobj = 0;
+ char buf[256];
+ Jim_Obj *objPtr;
+
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 2, argv, "");
+ return JIM_ERR;
+ }
+ /* Count the number of free objects. */
+ objPtr = interp->freeList;
+ while (objPtr) {
+ freeobj++;
+ objPtr = objPtr->nextObjPtr;
+ }
+ /* Count the number of live objects. */
+ objPtr = interp->liveList;
+ while (objPtr) {
+ liveobj++;
+ objPtr = objPtr->nextObjPtr;
+ }
+ /* Set the result string and return. */
+ sprintf(buf, "free %d used %d", freeobj, liveobj);
+ Jim_SetResultString(interp, buf, -1);
+ return JIM_OK;
+ }
+ else if (option == OPT_OBJECTS) {
+ Jim_Obj *objPtr, *listObjPtr, *subListObjPtr;
+
+ /* Count the number of live objects. */
+ objPtr = interp->liveList;
+ listObjPtr = Jim_NewListObj(interp, NULL, 0);
+ while (objPtr) {
+ char buf[128];
+ const char *type = objPtr->typePtr ? objPtr->typePtr->name : "";
+
+ subListObjPtr = Jim_NewListObj(interp, NULL, 0);
+ sprintf(buf, "%p", objPtr);
+ Jim_ListAppendElement(interp, subListObjPtr, Jim_NewStringObj(interp, buf, -1));
+ Jim_ListAppendElement(interp, subListObjPtr, Jim_NewStringObj(interp, type, -1));
+ Jim_ListAppendElement(interp, subListObjPtr, Jim_NewIntObj(interp, objPtr->refCount));
+ Jim_ListAppendElement(interp, subListObjPtr, objPtr);
+ Jim_ListAppendElement(interp, listObjPtr, subListObjPtr);
+ objPtr = objPtr->nextObjPtr;
+ }
+ Jim_SetResult(interp, listObjPtr);
+ return JIM_OK;
+ }
+ else if (option == OPT_INVSTR) {
+ Jim_Obj *objPtr;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "object");
+ return JIM_ERR;
+ }
+ objPtr = argv[2];
+ if (objPtr->typePtr != NULL)
+ Jim_InvalidateStringRep(objPtr);
+ Jim_SetEmptyResult(interp);
+ return JIM_OK;
+ }
+ else if (option == OPT_SHOW) {
+ const char *s;
+ int len, charlen;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "object");
+ return JIM_ERR;
+ }
+ s = Jim_GetString(argv[2], &len);
+ charlen = Jim_Utf8Length(interp, argv[2]);
+ printf("chars (%d): <<%s>>\n", charlen, s);
+ printf("bytes (%d):", len);
+ while (len--) {
+ printf(" %02x", (unsigned char)*s++);
+ }
+ printf("\n");
+ return JIM_OK;
+ }
+ else if (option == OPT_SCRIPTLEN) {
+ ScriptObj *script;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "script");
+ return JIM_ERR;
+ }
+ script = Jim_GetScript(interp, argv[2]);
+ Jim_SetResultInt(interp, script->len);
+ return JIM_OK;
+ }
+ else if (option == OPT_EXPRLEN) {
+ ExprByteCode *expr;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "expression");
+ return JIM_ERR;
+ }
+ expr = JimGetExpression(interp, argv[2]);
+ if (expr == NULL)
+ return JIM_ERR;
+ Jim_SetResultInt(interp, expr->len);
+ return JIM_OK;
+ }
+ else if (option == OPT_EXPRBC) {
+ Jim_Obj *objPtr;
+ ExprByteCode *expr;
+ int i;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "expression");
+ return JIM_ERR;
+ }
+ expr = JimGetExpression(interp, argv[2]);
+ if (expr == NULL)
+ return JIM_ERR;
+ objPtr = Jim_NewListObj(interp, NULL, 0);
+ for (i = 0; i < expr->len; i++) {
+ const char *type;
+ const Jim_ExprOperator *op;
+ Jim_Obj *obj = expr->token[i].objPtr;
+
+ switch (expr->token[i].type) {
+ case JIM_TT_EXPR_INT:
+ type = "int";
+ break;
+ case JIM_TT_EXPR_DOUBLE:
+ type = "double";
+ break;
+ case JIM_TT_CMD:
+ type = "command";
+ break;
+ case JIM_TT_VAR:
+ type = "variable";
+ break;
+ case JIM_TT_DICTSUGAR:
+ type = "dictsugar";
+ break;
+ case JIM_TT_EXPRSUGAR:
+ type = "exprsugar";
+ break;
+ case JIM_TT_ESC:
+ type = "subst";
+ break;
+ case JIM_TT_STR:
+ type = "string";
+ break;
+ default:
+ op = JimExprOperatorInfoByOpcode(expr->token[i].type);
+ if (op == NULL) {
+ type = "private";
+ }
+ else {
+ type = "operator";
+ }
+ obj = Jim_NewStringObj(interp, op ? op->name : "", -1);
+ break;
+ }
+ Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, type, -1));
+ Jim_ListAppendElement(interp, objPtr, obj);
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+ else {
+ Jim_SetResultString(interp,
+ "bad option. Valid options are refcount, " "objcount, objects, invstr", -1);
+ return JIM_ERR;
+ }
+ /* unreached */
+#else
+ Jim_SetResultString(interp, "unsupported", -1);
+ return JIM_ERR;
+#endif
+}
+
+/* [eval] */
+static int Jim_EvalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int rc;
+ Jim_Stack *prevLocalProcs;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "script ?...?");
+ return JIM_ERR;
+ }
+
+ /* Install a new stack for local procs */
+ prevLocalProcs = interp->localProcs;
+ interp->localProcs = NULL;
+
+ if (argc == 2) {
+ rc = Jim_EvalObj(interp, argv[1]);
+ }
+ else {
+ rc = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1));
+ }
+
+ /* Delete any local procs */
+ JimDeleteLocalProcs(interp);
+ interp->localProcs = prevLocalProcs;
+
+ if (rc == JIM_ERR) {
+ /* eval is "interesting", so add a stack frame here */
+ interp->addStackTrace++;
+ }
+ return rc;
+}
+
+/* [uplevel] */
+static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc >= 2) {
+ int retcode;
+ Jim_CallFrame *savedCallFrame, *targetCallFrame;
+ Jim_Obj *objPtr;
+ const char *str;
+
+ /* Save the old callframe pointer */
+ savedCallFrame = interp->framePtr;
+
+ /* Lookup the target frame pointer */
+ str = Jim_String(argv[1]);
+ if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') {
+ targetCallFrame =Jim_GetCallFrameByLevel(interp, argv[1]);
+ argc--;
+ argv++;
+ }
+ else {
+ targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL);
+ }
+ if (targetCallFrame == NULL) {
+ return JIM_ERR;
+ }
+ if (argc < 2) {
+ argv--;
+ Jim_WrongNumArgs(interp, 1, argv, "?level? command ?arg ...?");
+ return JIM_ERR;
+ }
+ /* Eval the code in the target callframe. */
+ interp->framePtr = targetCallFrame;
+ if (argc == 2) {
+ retcode = Jim_EvalObj(interp, argv[1]);
+ }
+ else {
+ objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1);
+ Jim_IncrRefCount(objPtr);
+ retcode = Jim_EvalObj(interp, objPtr);
+ Jim_DecrRefCount(interp, objPtr);
+ }
+ interp->framePtr = savedCallFrame;
+ return retcode;
+ }
+ else {
+ Jim_WrongNumArgs(interp, 1, argv, "?level? command ?arg ...?");
+ return JIM_ERR;
+ }
+}
+
+/* [expr] */
+static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *exprResultPtr;
+ int retcode;
+
+ if (argc == 2) {
+ retcode = Jim_EvalExpression(interp, argv[1], &exprResultPtr);
+ }
+ else if (argc > 2) {
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1);
+ Jim_IncrRefCount(objPtr);
+ retcode = Jim_EvalExpression(interp, objPtr, &exprResultPtr);
+ Jim_DecrRefCount(interp, objPtr);
+ }
+ else {
+ Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
+ return JIM_ERR;
+ }
+ if (retcode != JIM_OK)
+ return retcode;
+ Jim_SetResult(interp, exprResultPtr);
+ Jim_DecrRefCount(interp, exprResultPtr);
+ return JIM_OK;
+}
+
+/* [break] */
+static int Jim_BreakCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 1) {
+ Jim_WrongNumArgs(interp, 1, argv, "");
+ return JIM_ERR;
+ }
+ return JIM_BREAK;
+}
+
+/* [continue] */
+static int Jim_ContinueCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 1) {
+ Jim_WrongNumArgs(interp, 1, argv, "");
+ return JIM_ERR;
+ }
+ return JIM_CONTINUE;
+}
+
+/* [return] */
+static int Jim_ReturnCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ Jim_Obj *stackTraceObj = NULL;
+ Jim_Obj *errorCodeObj = NULL;
+ int returnCode = JIM_OK;
+ long level = 1;
+
+ for (i = 1; i < argc - 1; i += 2) {
+ if (Jim_CompareStringImmediate(interp, argv[i], "-code")) {
+ if (Jim_GetReturnCode(interp, argv[i + 1], &returnCode) == JIM_ERR) {
+ return JIM_ERR;
+ }
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[i], "-errorinfo")) {
+ stackTraceObj = argv[i + 1];
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[i], "-errorcode")) {
+ errorCodeObj = argv[i + 1];
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[i], "-level")) {
+ if (Jim_GetLong(interp, argv[i + 1], &level) != JIM_OK || level < 0) {
+ Jim_SetResultFormatted(interp, "bad level \"%#s\"", argv[i + 1]);
+ return JIM_ERR;
+ }
+ }
+ else {
+ break;
+ }
+ }
+
+ if (i != argc - 1 && i != argc) {
+ Jim_WrongNumArgs(interp, 1, argv,
+ "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?");
+ }
+
+ /* If a stack trace is supplied and code is error, set the stack trace */
+ if (stackTraceObj && returnCode == JIM_ERR) {
+ JimSetStackTrace(interp, stackTraceObj);
+ }
+ /* If an error code list is supplied, set the global $errorCode */
+ if (errorCodeObj && returnCode == JIM_ERR) {
+ Jim_SetGlobalVariableStr(interp, "errorCode", errorCodeObj);
+ }
+ interp->returnCode = returnCode;
+ interp->returnLevel = level;
+
+ if (i == argc - 1) {
+ Jim_SetResult(interp, argv[i]);
+ }
+ return JIM_RETURN;
+}
+
+/* [tailcall] */
+static int Jim_TailcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+
+ objPtr = Jim_NewListObj(interp, argv + 1, argc - 1);
+ Jim_SetResult(interp, objPtr);
+ return JIM_EVAL;
+}
+
+/* [proc] */
+static int Jim_ProcCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int argListLen;
+ int leftArity, rightArity;
+ int i;
+ int optionalArgs = 0;
+ int args = 0;
+
+ if (argc != 4 && argc != 5) {
+ Jim_WrongNumArgs(interp, 1, argv, "name arglist ?statics? body");
+ return JIM_ERR;
+ }
+
+ if (JimValidName(interp, "procedure", argv[1]) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ argListLen = Jim_ListLength(interp, argv[2]);
+ leftArity = 0;
+ rightArity = 0;
+
+ /* Examine the argument list for default parameters and 'args' */
+ for (i = 0; i < argListLen; i++) {
+ Jim_Obj *argPtr;
+ int len;
+
+ /* Examine a parameter */
+ Jim_ListIndex(interp, argv[2], i, &argPtr, JIM_NONE);
+ len = Jim_ListLength(interp, argPtr);
+ if (len == 0) {
+ Jim_SetResultString(interp, "procedure has argument with no name", -1);
+ return JIM_ERR;
+ }
+ if (len > 2) {
+ Jim_SetResultString(interp, "procedure has argument with too many fields", -1);
+ return JIM_ERR;
+ }
+
+ if (len == 2) {
+ /* May be {args newname} */
+ Jim_ListIndex(interp, argPtr, 0, &argPtr, JIM_NONE);
+ }
+
+ if (Jim_CompareStringImmediate(interp, argPtr, "args")) {
+ if (args) {
+ Jim_SetResultString(interp, "procedure has 'args' specified more than once", -1);
+ return JIM_ERR;
+ }
+ if (rightArity) {
+ Jim_SetResultString(interp, "procedure has 'args' in invalid position", -1);
+ return JIM_ERR;
+ }
+ args = 1;
+ continue;
+ }
+
+ /* Does this parameter have a default? */
+ if (len == 1) {
+ /* A required arg. Is it part of leftArity or rightArity? */
+ if (optionalArgs || args) {
+ rightArity++;
+ }
+ else {
+ leftArity++;
+ }
+ }
+ else {
+ /* Optional arg. Can't be after rightArity */
+ if (rightArity || args) {
+ Jim_SetResultString(interp, "procedure has optional arg in invalid position", -1);
+ return JIM_ERR;
+ }
+ optionalArgs++;
+ }
+ }
+
+ if (argc == 4) {
+ return JimCreateProcedure(interp, Jim_String(argv[1]),
+ argv[2], NULL, argv[3], leftArity, optionalArgs, args, rightArity);
+ }
+ else {
+ return JimCreateProcedure(interp, Jim_String(argv[1]),
+ argv[2], argv[3], argv[4], leftArity, optionalArgs, args, rightArity);
+ }
+}
+
+/* [local] */
+static int Jim_LocalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int retcode;
+
+ /* Evaluate the arguments with 'local' in force */
+ interp->local++;
+ retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1);
+ interp->local--;
+
+
+ /* If OK, and the result is a proc, add it to the list of local procs */
+ if (retcode == 0) {
+ const char *procname = Jim_String(Jim_GetResult(interp));
+
+ if (Jim_FindHashEntry(&interp->commands, procname) == NULL) {
+ Jim_SetResultFormatted(interp, "not a proc: \"%s\"", procname);
+ return JIM_ERR;
+ }
+ if (interp->localProcs == NULL) {
+ interp->localProcs = Jim_Alloc(sizeof(*interp->localProcs));
+ Jim_InitStack(interp->localProcs);
+ }
+ Jim_StackPush(interp->localProcs, Jim_StrDup(procname));
+ }
+
+ return retcode;
+}
+
+/* [upcall] */
+static int Jim_UpcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?");
+ return JIM_ERR;
+ }
+ else {
+ int retcode;
+
+ Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG);
+ if (cmdPtr == NULL || !cmdPtr->isproc || !cmdPtr->u.proc.prevCmd) {
+ Jim_SetResultFormatted(interp, "no previous proc: \"%#s\"", argv[1]);
+ return JIM_ERR;
+ }
+ /* OK. Mark this command as being in an upcall */
+ cmdPtr->u.proc.upcall++;
+ JimIncrCmdRefCount(cmdPtr);
+
+ /* Invoke the command as normal */
+ retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1);
+
+ /* No longer in an upcall */
+ cmdPtr->u.proc.upcall--;
+ JimDecrCmdRefCount(interp, cmdPtr);
+
+ return retcode;
+ }
+}
+
+/* [concat] */
+static int Jim_ConcatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_SetResult(interp, Jim_ConcatObj(interp, argc - 1, argv + 1));
+ return JIM_OK;
+}
+
+/* [upvar] */
+static int Jim_UpvarCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+ Jim_CallFrame *targetCallFrame;
+
+ /* Lookup the target frame pointer */
+ if (argc > 3 && (argc % 2 == 0)) {
+ targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]);
+ argc--;
+ argv++;
+ }
+ else {
+ targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL);
+ }
+ if (targetCallFrame == NULL) {
+ return JIM_ERR;
+ }
+
+ /* Check for arity */
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "?level? otherVar localVar ?otherVar localVar ...?");
+ return JIM_ERR;
+ }
+
+ /* Now... for every other/local couple: */
+ for (i = 1; i < argc; i += 2) {
+ if (Jim_SetVariableLink(interp, argv[i + 1], argv[i], targetCallFrame) != JIM_OK)
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* [global] */
+static int Jim_GlobalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int i;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?varName ...?");
+ return JIM_ERR;
+ }
+ /* Link every var to the toplevel having the same name */
+ if (interp->framePtr->level == 0)
+ return JIM_OK; /* global at toplevel... */
+ for (i = 1; i < argc; i++) {
+ if (Jim_SetVariableLink(interp, argv[i], argv[i], interp->topFramePtr) != JIM_OK)
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* does the [string map] operation. On error NULL is returned,
+ * otherwise a new string object with the result, having refcount = 0,
+ * is returned. */
+static Jim_Obj *JimStringMap(Jim_Interp *interp, Jim_Obj *mapListObjPtr,
+ Jim_Obj *objPtr, int nocase)
+{
+ int numMaps;
+ const char *str, *noMatchStart = NULL;
+ int strLen, i;
+ Jim_Obj *resultObjPtr;
+
+ numMaps = Jim_ListLength(interp, mapListObjPtr);
+ if (numMaps % 2) {
+ Jim_SetResultString(interp, "list must contain an even number of elements", -1);
+ return NULL;
+ }
+
+ str = Jim_String(objPtr);
+ strLen = Jim_Utf8Length(interp, objPtr);
+
+ /* Map it */
+ resultObjPtr = Jim_NewStringObj(interp, "", 0);
+ while (strLen) {
+ for (i = 0; i < numMaps; i += 2) {
+ Jim_Obj *objPtr;
+ const char *k;
+ int kl;
+
+ Jim_ListIndex(interp, mapListObjPtr, i, &objPtr, JIM_NONE);
+ k = Jim_String(objPtr);
+ kl = Jim_Utf8Length(interp, objPtr);
+
+ if (strLen >= kl && kl) {
+ int rc;
+ if (nocase) {
+ rc = JimStringCompareNoCase(str, k, kl);
+ }
+ else {
+ rc = JimStringCompare(str, kl, k, kl);
+ }
+ if (rc == 0) {
+ if (noMatchStart) {
+ Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart);
+ noMatchStart = NULL;
+ }
+ Jim_ListIndex(interp, mapListObjPtr, i + 1, &objPtr, JIM_NONE);
+ Jim_AppendObj(interp, resultObjPtr, objPtr);
+ str += utf8_index(str, kl);
+ strLen -= kl;
+ break;
+ }
+ }
+ }
+ if (i == numMaps) { /* no match */
+ int c;
+ if (noMatchStart == NULL)
+ noMatchStart = str;
+ str += utf8_tounicode(str, &c);
+ strLen--;
+ }
+ }
+ if (noMatchStart) {
+ Jim_AppendString(interp, resultObjPtr, noMatchStart, str - noMatchStart);
+ }
+ return resultObjPtr;
+}
+
+/* [string] */
+static int Jim_StringCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int len;
+ int opt_case = 1;
+ int option;
+ static const char * const options[] = {
+ "bytelength", "length", "compare", "match", "equal", "is", "byterange", "range", "map",
+ "repeat", "reverse", "index", "first", "last",
+ "trim", "trimleft", "trimright", "tolower", "toupper", NULL
+ };
+ enum
+ {
+ OPT_BYTELENGTH, OPT_LENGTH, OPT_COMPARE, OPT_MATCH, OPT_EQUAL, OPT_IS, OPT_BYTERANGE, OPT_RANGE, OPT_MAP,
+ OPT_REPEAT, OPT_REVERSE, OPT_INDEX, OPT_FIRST, OPT_LAST,
+ OPT_TRIM, OPT_TRIMLEFT, OPT_TRIMRIGHT, OPT_TOLOWER, OPT_TOUPPER
+ };
+ static const char * const nocase_options[] = {
+ "-nocase", NULL
+ };
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "option ?arguments ...?");
+ return JIM_ERR;
+ }
+ if (Jim_GetEnum(interp, argv[1], options, &option, NULL,
+ JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
+ return JIM_ERR;
+
+ switch (option) {
+ case OPT_LENGTH:
+ case OPT_BYTELENGTH:
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "string");
+ return JIM_ERR;
+ }
+ if (option == OPT_LENGTH) {
+ len = Jim_Utf8Length(interp, argv[2]);
+ }
+ else {
+ len = Jim_Length(argv[2]);
+ }
+ Jim_SetResultInt(interp, len);
+ return JIM_OK;
+
+ case OPT_COMPARE:
+ case OPT_EQUAL:
+ if (argc != 4 &&
+ (argc != 5 ||
+ Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL,
+ JIM_ENUM_ABBREV) != JIM_OK)) {
+ Jim_WrongNumArgs(interp, 2, argv, "?-nocase? string1 string2");
+ return JIM_ERR;
+ }
+ if (opt_case == 0) {
+ argv++;
+ }
+ if (option == OPT_COMPARE || !opt_case) {
+ Jim_SetResultInt(interp, Jim_StringCompareObj(interp, argv[2], argv[3], !opt_case));
+ }
+ else {
+ Jim_SetResultBool(interp, Jim_StringEqObj(argv[2], argv[3]));
+ }
+ return JIM_OK;
+
+ case OPT_MATCH:
+ if (argc != 4 &&
+ (argc != 5 ||
+ Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL,
+ JIM_ENUM_ABBREV) != JIM_OK)) {
+ Jim_WrongNumArgs(interp, 2, argv, "?-nocase? pattern string");
+ return JIM_ERR;
+ }
+ if (opt_case == 0) {
+ argv++;
+ }
+ Jim_SetResultBool(interp, Jim_StringMatchObj(interp, argv[2], argv[3], !opt_case));
+ return JIM_OK;
+
+ case OPT_MAP:{
+ Jim_Obj *objPtr;
+
+ if (argc != 4 &&
+ (argc != 5 ||
+ Jim_GetEnum(interp, argv[2], nocase_options, &opt_case, NULL,
+ JIM_ENUM_ABBREV) != JIM_OK)) {
+ Jim_WrongNumArgs(interp, 2, argv, "?-nocase? mapList string");
+ return JIM_ERR;
+ }
+
+ if (opt_case == 0) {
+ argv++;
+ }
+ objPtr = JimStringMap(interp, argv[2], argv[3], !opt_case);
+ if (objPtr == NULL) {
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+
+ case OPT_RANGE:
+ case OPT_BYTERANGE:{
+ Jim_Obj *objPtr;
+
+ if (argc != 5) {
+ Jim_WrongNumArgs(interp, 2, argv, "string first last");
+ return JIM_ERR;
+ }
+ if (option == OPT_RANGE) {
+ objPtr = Jim_StringRangeObj(interp, argv[2], argv[3], argv[4]);
+ }
+ else
+ {
+ objPtr = Jim_StringByteRangeObj(interp, argv[2], argv[3], argv[4]);
+ }
+
+ if (objPtr == NULL) {
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+
+ case OPT_REPEAT:{
+ Jim_Obj *objPtr;
+ jim_wide count;
+
+ if (argc != 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "string count");
+ return JIM_ERR;
+ }
+ if (Jim_GetWide(interp, argv[3], &count) != JIM_OK) {
+ return JIM_ERR;
+ }
+ objPtr = Jim_NewStringObj(interp, "", 0);
+ if (count > 0) {
+ while (count--) {
+ Jim_AppendObj(interp, objPtr, argv[2]);
+ }
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+ }
+
+ case OPT_REVERSE:{
+ char *buf, *p;
+ const char *str;
+ int len;
+ int i;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "string");
+ return JIM_ERR;
+ }
+
+ str = Jim_GetString(argv[2], &len);
+ if (!str) {
+ return JIM_ERR;
+ }
+
+ buf = Jim_Alloc(len + 1);
+ p = buf + len;
+ *p = 0;
+ for (i = 0; i < len; ) {
+ int c;
+ int l = utf8_tounicode(str, &c);
+ memcpy(p - l, str, l);
+ p -= l;
+ i += l;
+ str += l;
+ }
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len));
+ return JIM_OK;
+ }
+
+ case OPT_INDEX:{
+ int idx;
+ const char *str;
+
+ if (argc != 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "string index");
+ return JIM_ERR;
+ }
+ if (Jim_GetIndex(interp, argv[3], &idx) != JIM_OK) {
+ return JIM_ERR;
+ }
+ str = Jim_String(argv[2]);
+ len = Jim_Utf8Length(interp, argv[2]);
+ if (idx != INT_MIN && idx != INT_MAX) {
+ idx = JimRelToAbsIndex(len, idx);
+ }
+ if (idx < 0 || idx >= len || str == NULL) {
+ Jim_SetResultString(interp, "", 0);
+ }
+ else if (len == Jim_Length(argv[2])) {
+ /* ASCII optimisation */
+ Jim_SetResultString(interp, str + idx, 1);
+ }
+ else {
+ int c;
+ int i = utf8_index(str, idx);
+ Jim_SetResultString(interp, str + i, utf8_tounicode(str + i, &c));
+ }
+ return JIM_OK;
+ }
+
+ case OPT_FIRST:
+ case OPT_LAST:{
+ int idx = 0, l1, l2;
+ const char *s1, *s2;
+
+ if (argc != 4 && argc != 5) {
+ Jim_WrongNumArgs(interp, 2, argv, "subString string ?index?");
+ return JIM_ERR;
+ }
+ s1 = Jim_String(argv[2]);
+ s2 = Jim_String(argv[3]);
+ l1 = Jim_Utf8Length(interp, argv[2]);
+ l2 = Jim_Utf8Length(interp, argv[3]);
+ if (argc == 5) {
+ if (Jim_GetIndex(interp, argv[4], &idx) != JIM_OK) {
+ return JIM_ERR;
+ }
+ idx = JimRelToAbsIndex(l2, idx);
+ }
+ else if (option == OPT_LAST) {
+ idx = l2;
+ }
+ if (option == OPT_FIRST) {
+ Jim_SetResultInt(interp, JimStringFirst(s1, l1, s2, l2, idx));
+ }
+ else {
+#ifdef JIM_UTF8
+ Jim_SetResultInt(interp, JimStringLastUtf8(s1, l1, s2, idx));
+#else
+ Jim_SetResultInt(interp, JimStringLast(s1, l1, s2, idx));
+#endif
+ }
+ return JIM_OK;
+ }
+
+ case OPT_TRIM:
+ case OPT_TRIMLEFT:
+ case OPT_TRIMRIGHT:{
+ Jim_Obj *trimchars;
+
+ if (argc != 3 && argc != 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "string ?trimchars?");
+ return JIM_ERR;
+ }
+ trimchars = (argc == 4 ? argv[3] : NULL);
+ if (option == OPT_TRIM) {
+ Jim_SetResult(interp, JimStringTrim(interp, argv[2], trimchars));
+ }
+ else if (option == OPT_TRIMLEFT) {
+ Jim_SetResult(interp, JimStringTrimLeft(interp, argv[2], trimchars));
+ }
+ else if (option == OPT_TRIMRIGHT) {
+ Jim_SetResult(interp, JimStringTrimRight(interp, argv[2], trimchars));
+ }
+ return JIM_OK;
+ }
+
+ case OPT_TOLOWER:
+ case OPT_TOUPPER:
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "string");
+ return JIM_ERR;
+ }
+ if (option == OPT_TOLOWER) {
+ Jim_SetResult(interp, JimStringToLower(interp, argv[2]));
+ }
+ else {
+ Jim_SetResult(interp, JimStringToUpper(interp, argv[2]));
+ }
+ return JIM_OK;
+
+ case OPT_IS:
+ if (argc == 4 || (argc == 5 && Jim_CompareStringImmediate(interp, argv[3], "-strict"))) {
+ return JimStringIs(interp, argv[argc - 1], argv[2], argc == 5);
+ }
+ Jim_WrongNumArgs(interp, 2, argv, "class ?-strict? str");
+ return JIM_ERR;
+ }
+ return JIM_OK;
+}
+
+/* [time] */
+static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ long i, count = 1;
+ jim_wide start, elapsed;
+ char buf[60];
+ const char *fmt = "%" JIM_WIDE_MODIFIER " microseconds per iteration";
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "script ?count?");
+ return JIM_ERR;
+ }
+ if (argc == 3) {
+ if (Jim_GetLong(interp, argv[2], &count) != JIM_OK)
+ return JIM_ERR;
+ }
+ if (count < 0)
+ return JIM_OK;
+ i = count;
+ start = JimClock();
+ while (i-- > 0) {
+ int retval;
+
+ retval = Jim_EvalObj(interp, argv[1]);
+ if (retval != JIM_OK) {
+ return retval;
+ }
+ }
+ elapsed = JimClock() - start;
+ sprintf(buf, fmt, count == 0 ? 0 : elapsed / count);
+ Jim_SetResultString(interp, buf, -1);
+ return JIM_OK;
+}
+
+/* [exit] */
+static int Jim_ExitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ long exitCode = 0;
+
+ if (argc > 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "?exitCode?");
+ return JIM_ERR;
+ }
+ if (argc == 2) {
+ if (Jim_GetLong(interp, argv[1], &exitCode) != JIM_OK)
+ return JIM_ERR;
+ }
+ interp->exitCode = exitCode;
+ return JIM_EXIT;
+}
+
+/* [catch] */
+static int Jim_CatchCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int exitCode = 0;
+ int i;
+ int sig = 0;
+
+ /* Which return codes are caught? These are the defaults */
+ jim_wide mask =
+ (1 << JIM_OK | 1 << JIM_ERR | 1 << JIM_BREAK | 1 << JIM_CONTINUE | 1 << JIM_RETURN);
+
+ /* Reset the error code before catch.
+ * Note that this is not strictly correct.
+ */
+ Jim_SetGlobalVariableStr(interp, "errorCode", Jim_NewStringObj(interp, "NONE", -1));
+
+ for (i = 1; i < argc - 1; i++) {
+ const char *arg = Jim_String(argv[i]);
+ jim_wide option;
+ int add;
+
+ /* It's a pity we can't use Jim_GetEnum here :-( */
+ if (strcmp(arg, "--") == 0) {
+ i++;
+ break;
+ }
+ if (*arg != '-') {
+ break;
+ }
+
+ if (strncmp(arg, "-no", 3) == 0) {
+ arg += 3;
+ add = 0;
+ }
+ else {
+ arg++;
+ add = 1;
+ }
+
+ if (Jim_StringToWide(arg, &option, 10) != JIM_OK) {
+ option = -1;
+ }
+ if (option < 0) {
+ option = Jim_FindByName(arg, jimReturnCodes, jimReturnCodesSize);
+ }
+ if (option < 0) {
+ goto wrongargs;
+ }
+
+ if (add) {
+ mask |= (1 << option);
+ }
+ else {
+ mask &= ~(1 << option);
+ }
+ }
+
+ argc -= i;
+ if (argc < 1 || argc > 3) {
+ wrongargs:
+ Jim_WrongNumArgs(interp, 1, argv,
+ "?-?no?code ... --? script ?resultVarName? ?optionVarName?");
+ return JIM_ERR;
+ }
+ argv += i;
+
+ if (mask & (1 << JIM_SIGNAL)) {
+ sig++;
+ }
+
+ interp->signal_level += sig;
+ if (interp->signal_level && interp->sigmask) {
+ /* If a signal is set, don't even try to execute the body */
+ exitCode = JIM_SIGNAL;
+ }
+ else {
+ exitCode = Jim_EvalObj(interp, argv[0]);
+ }
+ interp->signal_level -= sig;
+
+ /* Catch or pass through? Only the first 64 codes can be passed through */
+ if (exitCode >= 0 && exitCode < (int)sizeof(mask) && ((1 << exitCode) & mask) == 0) {
+ /* Not caught, pass it up */
+ return exitCode;
+ }
+
+ if (sig && exitCode == JIM_SIGNAL) {
+ /* Catch the signal at this level */
+ if (interp->signal_set_result) {
+ interp->signal_set_result(interp, interp->sigmask);
+ }
+ else {
+ Jim_SetResultInt(interp, interp->sigmask);
+ }
+ interp->sigmask = 0;
+ }
+
+ if (argc >= 2) {
+ if (Jim_SetVariable(interp, argv[1], Jim_GetResult(interp)) != JIM_OK) {
+ return JIM_ERR;
+ }
+ if (argc == 3) {
+ Jim_Obj *optListObj = Jim_NewListObj(interp, NULL, 0);
+
+ Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-code", -1));
+ Jim_ListAppendElement(interp, optListObj,
+ Jim_NewIntObj(interp, exitCode == JIM_RETURN ? interp->returnCode : exitCode));
+ Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-level", -1));
+ Jim_ListAppendElement(interp, optListObj, Jim_NewIntObj(interp, interp->returnLevel));
+ if (exitCode == JIM_ERR) {
+ Jim_Obj *errorCode;
+ Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorinfo",
+ -1));
+ Jim_ListAppendElement(interp, optListObj, interp->stackTrace);
+
+ errorCode = Jim_GetGlobalVariableStr(interp, "errorCode", JIM_NONE);
+ if (errorCode) {
+ Jim_ListAppendElement(interp, optListObj, Jim_NewStringObj(interp, "-errorcode", -1));
+ Jim_ListAppendElement(interp, optListObj, errorCode);
+ }
+ }
+ if (Jim_SetVariable(interp, argv[2], optListObj) != JIM_OK) {
+ return JIM_ERR;
+ }
+ }
+ }
+ Jim_SetResultInt(interp, exitCode);
+ return JIM_OK;
+}
+
+#ifdef JIM_REFERENCES
+
+/* [ref] */
+static int Jim_RefCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 3 && argc != 4) {
+ Jim_WrongNumArgs(interp, 1, argv, "string tag ?finalizer?");
+ return JIM_ERR;
+ }
+ if (argc == 3) {
+ Jim_SetResult(interp, Jim_NewReference(interp, argv[1], argv[2], NULL));
+ }
+ else {
+ Jim_SetResult(interp, Jim_NewReference(interp, argv[1], argv[2], argv[3]));
+ }
+ return JIM_OK;
+}
+
+/* [getref] */
+static int Jim_GetrefCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Reference *refPtr;
+
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "reference");
+ return JIM_ERR;
+ }
+ if ((refPtr = Jim_GetReference(interp, argv[1])) == NULL)
+ return JIM_ERR;
+ Jim_SetResult(interp, refPtr->objPtr);
+ return JIM_OK;
+}
+
+/* [setref] */
+static int Jim_SetrefCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Reference *refPtr;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "reference newValue");
+ return JIM_ERR;
+ }
+ if ((refPtr = Jim_GetReference(interp, argv[1])) == NULL)
+ return JIM_ERR;
+ Jim_IncrRefCount(argv[2]);
+ Jim_DecrRefCount(interp, refPtr->objPtr);
+ refPtr->objPtr = argv[2];
+ Jim_SetResult(interp, argv[2]);
+ return JIM_OK;
+}
+
+/* [collect] */
+static int Jim_CollectCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 1) {
+ Jim_WrongNumArgs(interp, 1, argv, "");
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, Jim_Collect(interp));
+
+ /* Free all the freed objects. */
+ while (interp->freeList) {
+ Jim_Obj *nextObjPtr = interp->freeList->nextObjPtr;
+ Jim_Free(interp->freeList);
+ interp->freeList = nextObjPtr;
+ }
+
+ return JIM_OK;
+}
+
+/* [finalize] reference ?newValue? */
+static int Jim_FinalizeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "reference ?finalizerProc?");
+ return JIM_ERR;
+ }
+ if (argc == 2) {
+ Jim_Obj *cmdNamePtr;
+
+ if (Jim_GetFinalizer(interp, argv[1], &cmdNamePtr) != JIM_OK)
+ return JIM_ERR;
+ if (cmdNamePtr != NULL) /* otherwise the null string is returned. */
+ Jim_SetResult(interp, cmdNamePtr);
+ }
+ else {
+ if (Jim_SetFinalizer(interp, argv[1], argv[2]) != JIM_OK)
+ return JIM_ERR;
+ Jim_SetResult(interp, argv[2]);
+ }
+ return JIM_OK;
+}
+
+/* [info references] */
+static int JimInfoReferences(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *listObjPtr;
+ Jim_HashTableIterator *htiter;
+ Jim_HashEntry *he;
+
+ listObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ htiter = Jim_GetHashTableIterator(&interp->references);
+ while ((he = Jim_NextHashEntry(htiter)) != NULL) {
+ char buf[JIM_REFERENCE_SPACE];
+ Jim_Reference *refPtr = he->val;
+ const jim_wide *refId = he->key;
+
+ JimFormatReference(buf, refPtr, *refId);
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, buf, -1));
+ }
+ Jim_FreeHashTableIterator(htiter);
+ Jim_SetResult(interp, listObjPtr);
+ return JIM_OK;
+}
+#endif
+
+/* [rename] */
+static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *oldName, *newName;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "oldName newName");
+ return JIM_ERR;
+ }
+
+ if (JimValidName(interp, "new procedure", argv[2])) {
+ return JIM_ERR;
+ }
+
+ oldName = Jim_String(argv[1]);
+ newName = Jim_String(argv[2]);
+ return Jim_RenameCommand(interp, oldName, newName);
+}
+
+int Jim_DictKeys(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj)
+{
+ int i;
+ int len;
+ Jim_Obj *resultObj;
+ Jim_Obj *dictObj;
+ Jim_Obj **dictValuesObj;
+
+ if (Jim_DictKeysVector(interp, objPtr, NULL, 0, &dictObj, JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* XXX: Could make the exact-match case much more efficient here.
+ * See JimCommandsList()
+ */
+ if (Jim_DictPairs(interp, dictObj, &dictValuesObj, &len) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* Only return the matching values */
+ resultObj = Jim_NewListObj(interp, NULL, 0);
+
+ for (i = 0; i < len; i += 2) {
+ if (patternObj == NULL || Jim_StringMatchObj(interp, patternObj, dictValuesObj[i], 0)) {
+ Jim_ListAppendElement(interp, resultObj, dictValuesObj[i]);
+ }
+ }
+ Jim_Free(dictValuesObj);
+
+ Jim_SetResult(interp, resultObj);
+ return JIM_OK;
+}
+
+int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr)
+{
+ if (SetDictFromAny(interp, objPtr) != JIM_OK) {
+ return -1;
+ }
+ return ((Jim_HashTable *)objPtr->internalRep.ptr)->used;
+}
+
+/* [dict] */
+static int Jim_DictCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+ int option;
+ const char *options[] = {
+ "create", "get", "set", "unset", "exists", "keys", "merge", "size", "with", NULL
+ };
+ enum
+ {
+ OPT_CREATE, OPT_GET, OPT_SET, OPT_UNSET, OPT_EXIST, OPT_KEYS, OPT_MERGE, OPT_SIZE, OPT_WITH,
+ };
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "subcommand ?arguments ...?");
+ return JIM_ERR;
+ }
+
+ if (Jim_GetEnum(interp, argv[1], options, &option, "subcommand", JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ switch (option) {
+ case OPT_GET:
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "varName ?key ...?");
+ return JIM_ERR;
+ }
+ if (Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr,
+ JIM_ERRMSG) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+
+ case OPT_SET:
+ if (argc < 5) {
+ Jim_WrongNumArgs(interp, 2, argv, "varName key ?key ...? value");
+ return JIM_ERR;
+ }
+ return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1]);
+
+ case OPT_EXIST:
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "varName ?key ...?");
+ return JIM_ERR;
+ }
+ Jim_SetResultBool(interp, Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3,
+ &objPtr, JIM_ERRMSG) == JIM_OK);
+ return JIM_OK;
+
+ case OPT_UNSET:
+ if (argc < 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "varName key ?key ...?");
+ return JIM_ERR;
+ }
+ return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL);
+
+ case OPT_KEYS:
+ if (argc != 3 && argc != 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "dictVar ?pattern?");
+ return JIM_ERR;
+ }
+ return Jim_DictKeys(interp, argv[2], argc == 4 ? argv[3] : NULL);
+
+ case OPT_SIZE: {
+ int size;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "dictVar");
+ return JIM_ERR;
+ }
+
+ size = Jim_DictSize(interp, argv[2]);
+ if (size < 0) {
+ return JIM_ERR;
+ }
+ Jim_SetResultInt(interp, size);
+ return JIM_OK;
+ }
+
+ case OPT_MERGE:
+ if (argc == 2) {
+ return JIM_OK;
+ }
+ else if (argv[2]->typePtr != &dictObjType && SetDictFromAny(interp, argv[2]) != JIM_OK) {
+ return JIM_ERR;
+ }
+ else {
+ return Jim_EvalObjPrefix(interp, "dict merge", argc - 2, argv + 2);
+ }
+
+ case OPT_WITH:
+ if (argc < 4) {
+ Jim_WrongNumArgs(interp, 2, argv, "dictVar ?key ...? script");
+ return JIM_ERR;
+ }
+ else if (Jim_GetVariable(interp, argv[2], JIM_ERRMSG) == NULL) {
+ return JIM_ERR;
+ }
+ else {
+ return Jim_EvalObjPrefix(interp, "dict with", argc - 2, argv + 2);
+ }
+
+ case OPT_CREATE:
+ if (argc % 2) {
+ Jim_WrongNumArgs(interp, 2, argv, "?key value ...?");
+ return JIM_ERR;
+ }
+ objPtr = Jim_NewDictObj(interp, argv + 2, argc - 2);
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+
+ default:
+ abort();
+ }
+}
+
+/* [subst] */
+static int Jim_SubstCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *options[] = {
+ "-nobackslashes", "-nocommands", "-novariables", NULL
+ };
+ enum
+ { OPT_NOBACKSLASHES, OPT_NOCOMMANDS, OPT_NOVARIABLES };
+ int i;
+ int flags = JIM_SUBST_FLAG;
+ Jim_Obj *objPtr;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "?options? string");
+ return JIM_ERR;
+ }
+ for (i = 1; i < (argc - 1); i++) {
+ int option;
+
+ if (Jim_GetEnum(interp, argv[i], options, &option, NULL,
+ JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+ switch (option) {
+ case OPT_NOBACKSLASHES:
+ flags |= JIM_SUBST_NOESC;
+ break;
+ case OPT_NOCOMMANDS:
+ flags |= JIM_SUBST_NOCMD;
+ break;
+ case OPT_NOVARIABLES:
+ flags |= JIM_SUBST_NOVAR;
+ break;
+ }
+ }
+ if (Jim_SubstObj(interp, argv[argc - 1], &objPtr, flags) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+/* [info] */
+static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int cmd;
+ Jim_Obj *objPtr;
+ int mode = 0;
+
+ static const char * const commands[] = {
+ "body", "commands", "procs", "channels", "exists", "globals", "level", "frame", "locals",
+ "vars", "version", "patchlevel", "complete", "args", "hostname",
+ "script", "source", "stacktrace", "nameofexecutable", "returncodes",
+ "references", NULL
+ };
+ enum
+ { INFO_BODY, INFO_COMMANDS, INFO_PROCS, INFO_CHANNELS, INFO_EXISTS, INFO_GLOBALS, INFO_LEVEL,
+ INFO_FRAME, INFO_LOCALS, INFO_VARS, INFO_VERSION, INFO_PATCHLEVEL, INFO_COMPLETE, INFO_ARGS,
+ INFO_HOSTNAME, INFO_SCRIPT, INFO_SOURCE, INFO_STACKTRACE, INFO_NAMEOFEXECUTABLE,
+ INFO_RETURNCODES, INFO_REFERENCES,
+ };
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "subcommand ?args ...?");
+ return JIM_ERR;
+ }
+ if (Jim_GetEnum(interp, argv[1], commands, &cmd, "subcommand", JIM_ERRMSG | JIM_ENUM_ABBREV)
+ != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ /* Test for the the most common commands first, just in case it makes a difference */
+ switch (cmd) {
+ case INFO_EXISTS:{
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "varName");
+ return JIM_ERR;
+ }
+ Jim_SetResultBool(interp, Jim_GetVariable(interp, argv[2], 0) != NULL);
+ break;
+ }
+
+ case INFO_CHANNELS:
+#ifndef jim_ext_aio
+ Jim_SetResultString(interp, "aio not enabled", -1);
+ return JIM_ERR;
+#endif
+ case INFO_COMMANDS:
+ case INFO_PROCS:
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "?pattern?");
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, JimCommandsList(interp, (argc == 3) ? argv[2] : NULL,
+ (cmd - INFO_COMMANDS)));
+ break;
+
+ case INFO_VARS:
+ mode++; /* JIM_VARLIST_VARS */
+ case INFO_LOCALS:
+ mode++; /* JIM_VARLIST_LOCALS */
+ case INFO_GLOBALS:
+ /* mode 0 => JIM_VARLIST_GLOBALS */
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "?pattern?");
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, JimVariablesList(interp, argc == 3 ? argv[2] : NULL, mode));
+ break;
+
+ case INFO_SCRIPT:
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 2, argv, "");
+ return JIM_ERR;
+ }
+ Jim_SetResultString(interp, Jim_GetScript(interp, interp->currentScriptObj)->fileName,
+ -1);
+ break;
+
+ case INFO_SOURCE:{
+ const char *filename = "";
+ int line = 0;
+ Jim_Obj *resObjPtr;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "source");
+ return JIM_ERR;
+ }
+ if (argv[2]->typePtr == &sourceObjType) {
+ filename = argv[2]->internalRep.sourceValue.fileName;
+ line = argv[2]->internalRep.sourceValue.lineNumber;
+ }
+ else if (argv[2]->typePtr == &scriptObjType) {
+ ScriptObj *script = Jim_GetScript(interp, argv[2]);
+ filename = script->fileName;
+ line = script->line;
+ }
+ resObjPtr = Jim_NewListObj(interp, NULL, 0);
+ Jim_ListAppendElement(interp, resObjPtr, Jim_NewStringObj(interp, filename, -1));
+ Jim_ListAppendElement(interp, resObjPtr, Jim_NewIntObj(interp, line));
+ Jim_SetResult(interp, resObjPtr);
+ break;
+ }
+
+ case INFO_STACKTRACE:
+ Jim_SetResult(interp, interp->stackTrace);
+ break;
+
+ case INFO_LEVEL:
+ case INFO_FRAME:
+ switch (argc) {
+ case 2:
+ Jim_SetResultInt(interp, interp->framePtr->level);
+ break;
+
+ case 3:
+ if (JimInfoLevel(interp, argv[2], &objPtr, cmd == INFO_LEVEL) != JIM_OK) {
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, objPtr);
+ break;
+
+ default:
+ Jim_WrongNumArgs(interp, 2, argv, "?levelNum?");
+ return JIM_ERR;
+ }
+ break;
+
+ case INFO_BODY:
+ case INFO_ARGS:{
+ Jim_Cmd *cmdPtr;
+
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "procname");
+ return JIM_ERR;
+ }
+ if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) {
+ return JIM_ERR;
+ }
+ if (!cmdPtr->isproc) {
+ Jim_SetResultFormatted(interp, "command \"%#s\" is not a procedure", argv[2]);
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp,
+ cmd == INFO_BODY ? cmdPtr->u.proc.bodyObjPtr : cmdPtr->u.proc.argListObjPtr);
+ break;
+ }
+
+ case INFO_VERSION:
+ case INFO_PATCHLEVEL:{
+ char buf[(JIM_INTEGER_SPACE * 2) + 1];
+
+ sprintf(buf, "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100);
+ Jim_SetResultString(interp, buf, -1);
+ break;
+ }
+
+ case INFO_COMPLETE:
+ if (argc != 3) {
+ Jim_WrongNumArgs(interp, 2, argv, "script");
+ return JIM_ERR;
+ }
+ else {
+ int len;
+ const char *s = Jim_GetString(argv[2], &len);
+
+ Jim_SetResultBool(interp, Jim_ScriptIsComplete(s, len, NULL));
+ }
+ break;
+
+ case INFO_HOSTNAME:
+ /* Redirect to os.gethostname if it exists */
+ return Jim_Eval(interp, "os.gethostname");
+
+ case INFO_NAMEOFEXECUTABLE:
+ /* Redirect to Tcl proc */
+ return Jim_Eval(interp, "{info nameofexecutable}");
+
+ case INFO_RETURNCODES:
+ if (argc == 2) {
+ int i;
+ Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ for (i = 0; jimReturnCodes[i]; i++) {
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, i));
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp,
+ jimReturnCodes[i], -1));
+ }
+
+ Jim_SetResult(interp, listObjPtr);
+ }
+ else if (argc == 3) {
+ long code;
+ const char *name;
+
+ if (Jim_GetLong(interp, argv[2], &code) != JIM_OK) {
+ return JIM_ERR;
+ }
+ name = Jim_ReturnCode(code);
+ if (*name == '?') {
+ Jim_SetResultInt(interp, code);
+ }
+ else {
+ Jim_SetResultString(interp, name, -1);
+ }
+ }
+ else {
+ Jim_WrongNumArgs(interp, 2, argv, "?code?");
+ return JIM_ERR;
+ }
+ break;
+ case INFO_REFERENCES:
+#ifdef JIM_REFERENCES
+ return JimInfoReferences(interp, argc, argv);
+#else
+ Jim_SetResultString(interp, "not supported", -1);
+ return JIM_ERR;
+#endif
+ }
+ return JIM_OK;
+}
+
+/* [exists] */
+static int Jim_ExistsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+
+ static const char * const options[] = {
+ "-command", "-proc", "-var", NULL
+ };
+ enum
+ {
+ OPT_COMMAND, OPT_PROC, OPT_VAR
+ };
+ int option;
+
+ if (argc == 2) {
+ option = OPT_VAR;
+ objPtr = argv[1];
+ }
+ else if (argc == 3) {
+ if (Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
+ return JIM_ERR;
+ }
+ objPtr = argv[2];
+ }
+ else {
+ Jim_WrongNumArgs(interp, 1, argv, "?option? name");
+ return JIM_ERR;
+ }
+
+ /* Test for the the most common commands first, just in case it makes a difference */
+ switch (option) {
+ case OPT_VAR:
+ Jim_SetResultBool(interp, Jim_GetVariable(interp, objPtr, 0) != NULL);
+ break;
+
+ case OPT_COMMAND:
+ case OPT_PROC: {
+ Jim_Cmd *cmd = Jim_GetCommand(interp, objPtr, JIM_NONE);
+ Jim_SetResultBool(interp, cmd != NULL && (option == OPT_COMMAND || cmd->isproc));
+ break;
+ }
+ }
+ return JIM_OK;
+}
+
+/* [split] */
+static int Jim_SplitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *str, *splitChars, *noMatchStart;
+ int splitLen, strLen;
+ Jim_Obj *resObjPtr;
+ int c;
+ int len;
+
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "string ?splitChars?");
+ return JIM_ERR;
+ }
+
+ str = Jim_GetString(argv[1], &len);
+ if (len == 0) {
+ return JIM_OK;
+ }
+ strLen = Jim_Utf8Length(interp, argv[1]);
+
+ /* Init */
+ if (argc == 2) {
+ splitChars = " \n\t\r";
+ splitLen = 4;
+ }
+ else {
+ splitChars = Jim_String(argv[2]);
+ splitLen = Jim_Utf8Length(interp, argv[2]);
+ }
+
+ noMatchStart = str;
+ resObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ /* Split */
+ if (splitLen) {
+ Jim_Obj *objPtr;
+ while (strLen--) {
+ const char *sc = splitChars;
+ int scLen = splitLen;
+ int sl = utf8_tounicode(str, &c);
+ while (scLen--) {
+ int pc;
+ sc += utf8_tounicode(sc, &pc);
+ if (c == pc) {
+ objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart));
+ Jim_ListAppendElement(interp, resObjPtr, objPtr);
+ noMatchStart = str + sl;
+ break;
+ }
+ }
+ str += sl;
+ }
+ objPtr = Jim_NewStringObj(interp, noMatchStart, (str - noMatchStart));
+ Jim_ListAppendElement(interp, resObjPtr, objPtr);
+ }
+ else {
+ /* This handles the special case of splitchars eq {}
+ * Optimise by sharing common (ASCII) characters
+ */
+ Jim_Obj **commonObj = NULL;
+#define NUM_COMMON (128 - 32)
+ while (strLen--) {
+ int n = utf8_tounicode(str, &c);
+#ifdef JIM_OPTIMIZATION
+ if (c >= 32 && c < 128) {
+ /* Common ASCII char */
+ c -= 32;
+ if (!commonObj) {
+ commonObj = Jim_Alloc(sizeof(*commonObj) * NUM_COMMON);
+ memset(commonObj, 0, sizeof(*commonObj) * NUM_COMMON);
+ }
+ if (!commonObj[c]) {
+ commonObj[c] = Jim_NewStringObj(interp, str, 1);
+ }
+ Jim_ListAppendElement(interp, resObjPtr, commonObj[c]);
+ str++;
+ continue;
+ }
+#endif
+ Jim_ListAppendElement(interp, resObjPtr, Jim_NewStringObjUtf8(interp, str, 1));
+ str += n;
+ }
+ Jim_Free(commonObj);
+ }
+
+ Jim_SetResult(interp, resObjPtr);
+ return JIM_OK;
+}
+
+/* [join] */
+static int Jim_JoinCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *joinStr;
+ int joinStrLen, i, listLen;
+ Jim_Obj *resObjPtr;
+
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "list ?joinString?");
+ return JIM_ERR;
+ }
+ /* Init */
+ if (argc == 2) {
+ joinStr = " ";
+ joinStrLen = 1;
+ }
+ else {
+ joinStr = Jim_GetString(argv[2], &joinStrLen);
+ }
+ listLen = Jim_ListLength(interp, argv[1]);
+ resObjPtr = Jim_NewStringObj(interp, NULL, 0);
+ /* Split */
+ for (i = 0; i < listLen; i++) {
+ Jim_Obj *objPtr = 0;
+
+ Jim_ListIndex(interp, argv[1], i, &objPtr, JIM_NONE);
+ Jim_AppendObj(interp, resObjPtr, objPtr);
+ if (i + 1 != listLen) {
+ Jim_AppendString(interp, resObjPtr, joinStr, joinStrLen);
+ }
+ }
+ Jim_SetResult(interp, resObjPtr);
+ return JIM_OK;
+}
+
+/* [format] */
+static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "formatString ?arg arg ...?");
+ return JIM_ERR;
+ }
+ objPtr = Jim_FormatString(interp, argv[1], argc - 2, argv + 2);
+ if (objPtr == NULL)
+ return JIM_ERR;
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+/* [scan] */
+static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *listPtr, **outVec;
+ int outc, i;
+
+ if (argc < 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "string format ?varName varName ...?");
+ return JIM_ERR;
+ }
+ if (argv[2]->typePtr != &scanFmtStringObjType)
+ SetScanFmtFromAny(interp, argv[2]);
+ if (FormatGetError(argv[2]) != 0) {
+ Jim_SetResultString(interp, FormatGetError(argv[2]), -1);
+ return JIM_ERR;
+ }
+ if (argc > 3) {
+ int maxPos = FormatGetMaxPos(argv[2]);
+ int count = FormatGetCnvCount(argv[2]);
+
+ if (maxPos > argc - 3) {
+ Jim_SetResultString(interp, "\"%n$\" argument index out of range", -1);
+ return JIM_ERR;
+ }
+ else if (count > argc - 3) {
+ Jim_SetResultString(interp, "different numbers of variable names and "
+ "field specifiers", -1);
+ return JIM_ERR;
+ }
+ else if (count < argc - 3) {
+ Jim_SetResultString(interp, "variable is not assigned by any "
+ "conversion specifiers", -1);
+ return JIM_ERR;
+ }
+ }
+ listPtr = Jim_ScanString(interp, argv[1], argv[2], JIM_ERRMSG);
+ if (listPtr == 0)
+ return JIM_ERR;
+ if (argc > 3) {
+ int rc = JIM_OK;
+ int count = 0;
+
+ if (listPtr != 0 && listPtr != (Jim_Obj *)EOF) {
+ int len = Jim_ListLength(interp, listPtr);
+
+ if (len != 0) {
+ JimListGetElements(interp, listPtr, &outc, &outVec);
+ for (i = 0; i < outc; ++i) {
+ if (Jim_Length(outVec[i]) > 0) {
+ ++count;
+ if (Jim_SetVariable(interp, argv[3 + i], outVec[i]) != JIM_OK) {
+ rc = JIM_ERR;
+ }
+ }
+ }
+ }
+ Jim_FreeNewObj(interp, listPtr);
+ }
+ else {
+ count = -1;
+ }
+ if (rc == JIM_OK) {
+ Jim_SetResultInt(interp, count);
+ }
+ return rc;
+ }
+ else {
+ if (listPtr == (Jim_Obj *)EOF) {
+ Jim_SetResult(interp, Jim_NewListObj(interp, 0, 0));
+ return JIM_OK;
+ }
+ Jim_SetResult(interp, listPtr);
+ }
+ return JIM_OK;
+}
+
+/* [error] */
+static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2 && argc != 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "message ?stacktrace?");
+ return JIM_ERR;
+ }
+ Jim_SetResult(interp, argv[1]);
+ if (argc == 3) {
+ JimSetStackTrace(interp, argv[2]);
+ return JIM_ERR;
+ }
+ interp->addStackTrace++;
+ return JIM_ERR;
+}
+
+/* [lrange] */
+static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+
+ if (argc != 4) {
+ Jim_WrongNumArgs(interp, 1, argv, "list first last");
+ return JIM_ERR;
+ }
+ if ((objPtr = Jim_ListRange(interp, argv[1], argv[2], argv[3])) == NULL)
+ return JIM_ERR;
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+/* [lrepeat] */
+static int Jim_LrepeatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *objPtr;
+ long count;
+
+ if (argc < 2 || Jim_GetLong(interp, argv[1], &count) != JIM_OK || count < 0) {
+ Jim_WrongNumArgs(interp, 1, argv, "count ?value ...?");
+ return JIM_ERR;
+ }
+
+ if (count == 0 || argc == 2) {
+ return JIM_OK;
+ }
+
+ argc -= 2;
+ argv += 2;
+
+ objPtr = Jim_NewListObj(interp, argv, argc);
+ while (--count) {
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ ListAppendElement(objPtr, argv[i]);
+ }
+ }
+
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+char **Jim_GetEnviron(void)
+{
+#if defined(HAVE__NSGETENVIRON)
+ return *_NSGetEnviron();
+#else
+ #if !defined(NO_ENVIRON_EXTERN)
+ extern char **environ;
+ #endif
+
+ return environ;
+#endif
+}
+
+void Jim_SetEnviron(char **env)
+{
+#if defined(HAVE__NSGETENVIRON)
+ *_NSGetEnviron() = env;
+#else
+ #if !defined(NO_ENVIRON_EXTERN)
+ extern char **environ;
+ #endif
+
+ environ = env;
+#endif
+}
+
+/* [env] */
+static int Jim_EnvCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const char *key;
+ const char *val;
+
+ if (argc == 1) {
+ char **environ = Jim_GetEnviron();
+
+ int i;
+ Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0);
+
+ for (i = 0; environ[i]; i++) {
+ const char *equals = strchr(environ[i], '=');
+
+ if (equals) {
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, environ[i],
+ equals - environ[i]));
+ Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, equals + 1, -1));
+ }
+ }
+
+ Jim_SetResult(interp, listObjPtr);
+ return JIM_OK;
+ }
+
+ if (argc < 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "varName ?default?");
+ return JIM_ERR;
+ }
+ key = Jim_String(argv[1]);
+ val = getenv(key);
+ if (val == NULL) {
+ if (argc < 3) {
+ Jim_SetResultFormatted(interp, "environment variable \"%#s\" does not exist", argv[1]);
+ return JIM_ERR;
+ }
+ val = Jim_String(argv[2]);
+ }
+ Jim_SetResult(interp, Jim_NewStringObj(interp, val, -1));
+ return JIM_OK;
+}
+
+/* [source] */
+static int Jim_SourceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int retval;
+
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "fileName");
+ return JIM_ERR;
+ }
+ retval = Jim_EvalFile(interp, Jim_String(argv[1]));
+ if (retval == JIM_RETURN)
+ return JIM_OK;
+ return retval;
+}
+
+/* [lreverse] */
+static int Jim_LreverseCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ Jim_Obj *revObjPtr, **ele;
+ int len;
+
+ if (argc != 2) {
+ Jim_WrongNumArgs(interp, 1, argv, "list");
+ return JIM_ERR;
+ }
+ JimListGetElements(interp, argv[1], &len, &ele);
+ len--;
+ revObjPtr = Jim_NewListObj(interp, NULL, 0);
+ while (len >= 0)
+ ListAppendElement(revObjPtr, ele[len--]);
+ Jim_SetResult(interp, revObjPtr);
+ return JIM_OK;
+}
+
+static int JimRangeLen(jim_wide start, jim_wide end, jim_wide step)
+{
+ jim_wide len;
+
+ if (step == 0)
+ return -1;
+ if (start == end)
+ return 0;
+ else if (step > 0 && start > end)
+ return -1;
+ else if (step < 0 && end > start)
+ return -1;
+ len = end - start;
+ if (len < 0)
+ len = -len; /* abs(len) */
+ if (step < 0)
+ step = -step; /* abs(step) */
+ len = 1 + ((len - 1) / step);
+ /* We can truncate safely to INT_MAX, the range command
+ * will always return an error for a such long range
+ * because Tcl lists can't be so long. */
+ if (len > INT_MAX)
+ len = INT_MAX;
+ return (int)((len < 0) ? -1 : len);
+}
+
+/* [range] */
+static int Jim_RangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ jim_wide start = 0, end, step = 1;
+ int len, i;
+ Jim_Obj *objPtr;
+
+ if (argc < 2 || argc > 4) {
+ Jim_WrongNumArgs(interp, 1, argv, "?start? end ?step?");
+ return JIM_ERR;
+ }
+ if (argc == 2) {
+ if (Jim_GetWide(interp, argv[1], &end) != JIM_OK)
+ return JIM_ERR;
+ }
+ else {
+ if (Jim_GetWide(interp, argv[1], &start) != JIM_OK ||
+ Jim_GetWide(interp, argv[2], &end) != JIM_OK)
+ return JIM_ERR;
+ if (argc == 4 && Jim_GetWide(interp, argv[3], &step) != JIM_OK)
+ return JIM_ERR;
+ }
+ if ((len = JimRangeLen(start, end, step)) == -1) {
+ Jim_SetResultString(interp, "Invalid (infinite?) range specified", -1);
+ return JIM_ERR;
+ }
+ objPtr = Jim_NewListObj(interp, NULL, 0);
+ for (i = 0; i < len; i++)
+ ListAppendElement(objPtr, Jim_NewIntObj(interp, start + i * step));
+ Jim_SetResult(interp, objPtr);
+ return JIM_OK;
+}
+
+/* [rand] */
+static int Jim_RandCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ jim_wide min = 0, max = 0, len, maxMul;
+
+ if (argc < 1 || argc > 3) {
+ Jim_WrongNumArgs(interp, 1, argv, "?min? max");
+ return JIM_ERR;
+ }
+ if (argc == 1) {
+ max = JIM_WIDE_MAX;
+ } else if (argc == 2) {
+ if (Jim_GetWide(interp, argv[1], &max) != JIM_OK)
+ return JIM_ERR;
+ } else if (argc == 3) {
+ if (Jim_GetWide(interp, argv[1], &min) != JIM_OK ||
+ Jim_GetWide(interp, argv[2], &max) != JIM_OK)
+ return JIM_ERR;
+ }
+ len = max-min;
+ if (len < 0) {
+ Jim_SetResultString(interp, "Invalid arguments (max < min)", -1);
+ return JIM_ERR;
+ }
+ maxMul = JIM_WIDE_MAX - (len ? (JIM_WIDE_MAX%len) : 0);
+ while (1) {
+ jim_wide r;
+
+ JimRandomBytes(interp, &r, sizeof(jim_wide));
+ if (r < 0 || r >= maxMul) continue;
+ r = (len == 0) ? 0 : r%len;
+ Jim_SetResultInt(interp, min+r);
+ return JIM_OK;
+ }
+}
+
+static const struct {
+ const char *name;
+ Jim_CmdProc cmdProc;
+} Jim_CoreCommandsTable[] = {
+ {"set", Jim_SetCoreCommand},
+ {"unset", Jim_UnsetCoreCommand},
+ {"puts", Jim_PutsCoreCommand},
+ {"+", Jim_AddCoreCommand},
+ {"*", Jim_MulCoreCommand},
+ {"-", Jim_SubCoreCommand},
+ {"/", Jim_DivCoreCommand},
+ {"incr", Jim_IncrCoreCommand},
+ {"while", Jim_WhileCoreCommand},
+ {"loop", Jim_LoopCoreCommand},
+ {"for", Jim_ForCoreCommand},
+ {"foreach", Jim_ForeachCoreCommand},
+ {"lmap", Jim_LmapCoreCommand},
+ {"if", Jim_IfCoreCommand},
+ {"switch", Jim_SwitchCoreCommand},
+ {"list", Jim_ListCoreCommand},
+ {"lindex", Jim_LindexCoreCommand},
+ {"lset", Jim_LsetCoreCommand},
+ {"lsearch", Jim_LsearchCoreCommand},
+ {"llength", Jim_LlengthCoreCommand},
+ {"lappend", Jim_LappendCoreCommand},
+ {"linsert", Jim_LinsertCoreCommand},
+ {"lreplace", Jim_LreplaceCoreCommand},
+ {"lsort", Jim_LsortCoreCommand},
+ {"append", Jim_AppendCoreCommand},
+ {"debug", Jim_DebugCoreCommand},
+ {"eval", Jim_EvalCoreCommand},
+ {"uplevel", Jim_UplevelCoreCommand},
+ {"expr", Jim_ExprCoreCommand},
+ {"break", Jim_BreakCoreCommand},
+ {"continue", Jim_ContinueCoreCommand},
+ {"proc", Jim_ProcCoreCommand},
+ {"concat", Jim_ConcatCoreCommand},
+ {"return", Jim_ReturnCoreCommand},
+ {"upvar", Jim_UpvarCoreCommand},
+ {"global", Jim_GlobalCoreCommand},
+ {"string", Jim_StringCoreCommand},
+ {"time", Jim_TimeCoreCommand},
+ {"exit", Jim_ExitCoreCommand},
+ {"catch", Jim_CatchCoreCommand},
+#ifdef JIM_REFERENCES
+ {"ref", Jim_RefCoreCommand},
+ {"getref", Jim_GetrefCoreCommand},
+ {"setref", Jim_SetrefCoreCommand},
+ {"finalize", Jim_FinalizeCoreCommand},
+ {"collect", Jim_CollectCoreCommand},
+#endif
+ {"rename", Jim_RenameCoreCommand},
+ {"dict", Jim_DictCoreCommand},
+ {"subst", Jim_SubstCoreCommand},
+ {"info", Jim_InfoCoreCommand},
+ {"exists", Jim_ExistsCoreCommand},
+ {"split", Jim_SplitCoreCommand},
+ {"join", Jim_JoinCoreCommand},
+ {"format", Jim_FormatCoreCommand},
+ {"scan", Jim_ScanCoreCommand},
+ {"error", Jim_ErrorCoreCommand},
+ {"lrange", Jim_LrangeCoreCommand},
+ {"lrepeat", Jim_LrepeatCoreCommand},
+ {"env", Jim_EnvCoreCommand},
+ {"source", Jim_SourceCoreCommand},
+ {"lreverse", Jim_LreverseCoreCommand},
+ {"range", Jim_RangeCoreCommand},
+ {"rand", Jim_RandCoreCommand},
+ {"tailcall", Jim_TailcallCoreCommand},
+ {"local", Jim_LocalCoreCommand},
+ {"upcall", Jim_UpcallCoreCommand},
+ {NULL, NULL},
+};
+
+void Jim_RegisterCoreCommands(Jim_Interp *interp)
+{
+ int i = 0;
+
+ while (Jim_CoreCommandsTable[i].name != NULL) {
+ Jim_CreateCommand(interp,
+ Jim_CoreCommandsTable[i].name, Jim_CoreCommandsTable[i].cmdProc, NULL, NULL);
+ i++;
+ }
+}
+
+/* -----------------------------------------------------------------------------
+ * Interactive prompt
+ * ---------------------------------------------------------------------------*/
+void Jim_MakeErrorMessage(Jim_Interp *interp)
+{
+ Jim_Obj *argv[2];
+
+ argv[0] = Jim_NewStringObj(interp, "errorInfo", -1);
+ argv[1] = interp->result;
+
+ Jim_EvalObjVector(interp, 2, argv);
+}
+
+static void JimSetFailedEnumResult(Jim_Interp *interp, const char *arg, const char *badtype,
+ const char *prefix, const char *const *tablePtr, const char *name)
+{
+ int count;
+ char **tablePtrSorted;
+ int i;
+
+ for (count = 0; tablePtr[count]; count++) {
+ }
+
+ if (name == NULL) {
+ name = "option";
+ }
+
+ Jim_SetResultFormatted(interp, "%s%s \"%s\": must be ", badtype, name, arg);
+ tablePtrSorted = Jim_Alloc(sizeof(char *) * count);
+ memcpy(tablePtrSorted, tablePtr, sizeof(char *) * count);
+ qsort(tablePtrSorted, count, sizeof(char *), qsortCompareStringPointers);
+ for (i = 0; i < count; i++) {
+ if (i + 1 == count && count > 1) {
+ Jim_AppendString(interp, Jim_GetResult(interp), "or ", -1);
+ }
+ Jim_AppendStrings(interp, Jim_GetResult(interp), prefix, tablePtrSorted[i], NULL);
+ if (i + 1 != count) {
+ Jim_AppendString(interp, Jim_GetResult(interp), ", ", -1);
+ }
+ }
+ Jim_Free(tablePtrSorted);
+}
+
+int Jim_GetEnum(Jim_Interp *interp, Jim_Obj *objPtr,
+ const char *const *tablePtr, int *indexPtr, const char *name, int flags)
+{
+ const char *bad = "bad ";
+ const char *const *entryPtr = NULL;
+ int i;
+ int match = -1;
+ int arglen;
+ const char *arg = Jim_GetString(objPtr, &arglen);
+
+ *indexPtr = -1;
+
+ for (entryPtr = tablePtr, i = 0; *entryPtr != NULL; entryPtr++, i++) {
+ if (Jim_CompareStringImmediate(interp, objPtr, *entryPtr)) {
+ /* Found an exact match */
+ *indexPtr = i;
+ return JIM_OK;
+ }
+ if (flags & JIM_ENUM_ABBREV) {
+ /* Accept an unambiguous abbreviation.
+ * Note that '-' doesnt' consitute a valid abbreviation
+ */
+ if (strncmp(arg, *entryPtr, arglen) == 0) {
+ if (*arg == '-' && arglen == 1) {
+ break;
+ }
+ if (match >= 0) {
+ bad = "ambiguous ";
+ goto ambiguous;
+ }
+ match = i;
+ }
+ }
+ }
+
+ /* If we had an unambiguous partial match */
+ if (match >= 0) {
+ *indexPtr = match;
+ return JIM_OK;
+ }
+
+ ambiguous:
+ if (flags & JIM_ERRMSG) {
+ JimSetFailedEnumResult(interp, arg, bad, "", tablePtr, name);
+ }
+ return JIM_ERR;
+}
+
+int Jim_FindByName(const char *name, const char * const array[], size_t len)
+{
+ int i;
+
+ for (i = 0; i < (int)len; i++) {
+ if (array[i] && strcmp(array[i], name) == 0) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int Jim_IsDict(Jim_Obj *objPtr)
+{
+ return objPtr->typePtr == &dictObjType;
+}
+
+int Jim_IsList(Jim_Obj *objPtr)
+{
+ return objPtr->typePtr == &listObjType;
+}
+
+/**
+ * Very simple printf-like formatting, designed for error messages.
+ *
+ * The format may contain up to 5 '%s' or '%#s', corresponding to variable arguments.
+ * The resulting string is created and set as the result.
+ *
+ * Each '%s' should correspond to a regular string parameter.
+ * Each '%#s' should correspond to a (Jim_Obj *) parameter.
+ * Any other printf specifier is not allowed (but %% is allowed for the % character).
+ *
+ * e.g. Jim_SetResultFormatted(interp, "Bad option \"%#s\" in proc \"%#s\"", optionObjPtr, procNamePtr);
+ *
+ * Note: We take advantage of the fact that printf has the same behaviour for both %s and %#s
+ */
+void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...)
+{
+ /* Initial space needed */
+ int len = strlen(format);
+ int extra = 0;
+ int n = 0;
+ const char *params[5];
+ char *buf;
+ va_list args;
+ int i;
+
+ va_start(args, format);
+
+ for (i = 0; i < len && n < 5; i++) {
+ int l;
+
+ if (strncmp(format + i, "%s", 2) == 0) {
+ params[n] = va_arg(args, char *);
+
+ l = strlen(params[n]);
+ }
+ else if (strncmp(format + i, "%#s", 3) == 0) {
+ Jim_Obj *objPtr = va_arg(args, Jim_Obj *);
+
+ params[n] = Jim_GetString(objPtr, &l);
+ }
+ else {
+ if (format[i] == '%') {
+ i++;
+ }
+ continue;
+ }
+ n++;
+ extra += l;
+ }
+
+ len += extra;
+ buf = Jim_Alloc(len + 1);
+ len = snprintf(buf, len + 1, format, params[0], params[1], params[2], params[3], params[4]);
+
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, len));
+}
+
+/* stubs */
+#ifndef jim_ext_package
+int Jim_PackageProvide(Jim_Interp *interp, const char *name, const char *ver, int flags)
+{
+ return JIM_OK;
+}
+#endif
+#ifndef jim_ext_aio
+FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *fhObj)
+{
+ Jim_SetResultString(interp, "aio not enabled", -1);
+ return NULL;
+}
+#endif
+
+
+/*
+ * Local Variables: ***
+ * c-basic-offset: 4 ***
+ * tab-width: 4 ***
+ * End: ***
+ */
+#include <stdio.h>
+#include <string.h>
+
+
+/**
+ * Implements the common 'commands' subcommand
+ */
+static int subcmd_null(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ /* Nothing to do, since the result has already been created */
+ return JIM_OK;
+}
+
+/**
+ * Do-nothing command to support -commands and -usage
+ */
+static const jim_subcmd_type dummy_subcmd = {
+ .cmd = "dummy",
+ .function = subcmd_null,
+ .flags = JIM_MODFLAG_HIDDEN,
+};
+
+static void add_commands(Jim_Interp *interp, const jim_subcmd_type * ct, const char *sep)
+{
+ const char *s = "";
+
+ for (; ct->cmd; ct++) {
+ if (!(ct->flags & JIM_MODFLAG_HIDDEN)) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), s, ct->cmd, NULL);
+ s = sep;
+ }
+ }
+}
+
+static void bad_subcmd(Jim_Interp *interp, const jim_subcmd_type * command_table, const char *type,
+ Jim_Obj *cmd, Jim_Obj *subcmd)
+{
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_String(cmd), ", ", type,
+ " command \"", Jim_String(subcmd), "\": should be ", NULL);
+ add_commands(interp, command_table, ", ");
+}
+
+static void show_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * command_table, int argc,
+ Jim_Obj *const *argv)
+{
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "Usage: \"", Jim_String(argv[0]),
+ " command ... \", where command is one of: ", NULL);
+ add_commands(interp, command_table, ", ");
+}
+
+static void add_cmd_usage(Jim_Interp *interp, const jim_subcmd_type * ct, Jim_Obj *cmd)
+{
+ if (cmd) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_String(cmd), " ", NULL);
+ }
+ Jim_AppendStrings(interp, Jim_GetResult(interp), ct->cmd, NULL);
+ if (ct->args && *ct->args) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), " ", ct->args, NULL);
+ }
+}
+
+static void show_full_usage(Jim_Interp *interp, const jim_subcmd_type * ct, int argc,
+ Jim_Obj *const *argv)
+{
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ for (; ct->cmd; ct++) {
+ if (!(ct->flags & JIM_MODFLAG_HIDDEN)) {
+ /* subcmd */
+ add_cmd_usage(interp, ct, argv[0]);
+ if (ct->description) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\n\n ", ct->description, NULL);
+ }
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\n\n", NULL);
+ }
+ }
+}
+
+static void set_wrong_args(Jim_Interp *interp, const jim_subcmd_type * command_table, Jim_Obj *subcmd)
+{
+ Jim_SetResultString(interp, "wrong # args: must be \"", -1);
+ add_cmd_usage(interp, command_table, subcmd);
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL);
+}
+
+const jim_subcmd_type *Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type * command_table,
+ int argc, Jim_Obj *const *argv)
+{
+ const jim_subcmd_type *ct;
+ const jim_subcmd_type *partial = 0;
+ int cmdlen;
+ Jim_Obj *cmd;
+ const char *cmdstr;
+ const char *cmdname;
+ int help = 0;
+
+ cmdname = Jim_String(argv[0]);
+
+ if (argc < 2) {
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "wrong # args: should be \"", cmdname,
+ " command ...\"\n", NULL);
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "Use \"", cmdname, " -help\" or \"",
+ cmdname, " -help command\" for help", NULL);
+ return 0;
+ }
+
+ cmd = argv[1];
+
+ if (argc == 2 && Jim_CompareStringImmediate(interp, cmd, "-usage")) {
+ /* Show full usage */
+ show_full_usage(interp, command_table, argc, argv);
+ return &dummy_subcmd;
+ }
+
+ /* Check for the help command */
+ if (Jim_CompareStringImmediate(interp, cmd, "-help")) {
+ if (argc == 2) {
+ /* Usage for the command, not the subcommand */
+ show_cmd_usage(interp, command_table, argc, argv);
+ return &dummy_subcmd;
+ }
+ help = 1;
+
+ /* Skip the 'help' command */
+ cmd = argv[2];
+ }
+
+ /* Check for special builtin '-commands' command first */
+ if (Jim_CompareStringImmediate(interp, cmd, "-commands")) {
+ /* Build the result here */
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ add_commands(interp, command_table, " ");
+ return &dummy_subcmd;
+ }
+
+ cmdstr = Jim_GetString(cmd, &cmdlen);
+
+ for (ct = command_table; ct->cmd; ct++) {
+ if (Jim_CompareStringImmediate(interp, cmd, ct->cmd)) {
+ /* Found an exact match */
+ break;
+ }
+ if (strncmp(cmdstr, ct->cmd, cmdlen) == 0) {
+ if (partial) {
+ /* Ambiguous */
+ if (help) {
+ /* Just show the top level help here */
+ show_cmd_usage(interp, command_table, argc, argv);
+ return &dummy_subcmd;
+ }
+ bad_subcmd(interp, command_table, "ambiguous", argv[0], argv[1 + help]);
+ return 0;
+ }
+ partial = ct;
+ }
+ continue;
+ }
+
+ /* If we had an unambiguous partial match */
+ if (partial && !ct->cmd) {
+ ct = partial;
+ }
+
+ if (!ct->cmd) {
+ /* No matching command */
+ if (help) {
+ /* Just show the top level help here */
+ show_cmd_usage(interp, command_table, argc, argv);
+ return &dummy_subcmd;
+ }
+ bad_subcmd(interp, command_table, "unknown", argv[0], argv[1 + help]);
+ return 0;
+ }
+
+ if (help) {
+ Jim_SetResultString(interp, "Usage: ", -1);
+ /* subcmd */
+ add_cmd_usage(interp, ct, argv[0]);
+ if (ct->description) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\n\n", ct->description, NULL);
+ }
+ return &dummy_subcmd;
+ }
+
+ /* Check the number of args */
+ if (argc - 2 < ct->minargs || (ct->maxargs >= 0 && argc - 2 > ct->maxargs)) {
+ Jim_SetResultString(interp, "wrong # args: must be \"", -1);
+ /* subcmd */
+ add_cmd_usage(interp, ct, argv[0]);
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL);
+
+ return 0;
+ }
+
+ /* Good command */
+ return ct;
+}
+
+int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type * ct, int argc, Jim_Obj *const *argv)
+{
+ int ret = JIM_ERR;
+
+ if (ct) {
+ if (ct->flags & JIM_MODFLAG_FULLARGV) {
+ ret = ct->function(interp, argc, argv);
+ }
+ else {
+ ret = ct->function(interp, argc - 2, argv + 2);
+ }
+ if (ret < 0) {
+ set_wrong_args(interp, ct, argv[0]);
+ ret = JIM_ERR;
+ }
+ }
+ return ret;
+}
+
+int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ const jim_subcmd_type *ct =
+ Jim_ParseSubCmd(interp, (const jim_subcmd_type *)Jim_CmdPrivData(interp), argc, argv);
+
+ return Jim_CallSubCmd(interp, ct, argc, argv);
+}
+
+/* The following two functions are for normal commands */
+int
+Jim_CheckCmdUsage(Jim_Interp *interp, const jim_subcmd_type * command_table, int argc,
+ Jim_Obj *const *argv)
+{
+ /* -usage or -help */
+ if (argc == 2) {
+ if (Jim_CompareStringImmediate(interp, argv[1], "-usage")
+ || Jim_CompareStringImmediate(interp, argv[1], "-help")) {
+ Jim_SetResultString(interp, "Usage: ", -1);
+ add_cmd_usage(interp, command_table, NULL);
+ if (command_table->description) {
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\n\n", command_table->description,
+ NULL);
+ }
+ return JIM_OK;
+ }
+ }
+ if (argc >= 2 && command_table->function) {
+ /* This is actually a sub command table */
+
+ Jim_Obj *nargv[4];
+ int nargc = 0;
+ const char *subcmd = NULL;
+
+ if (Jim_CompareStringImmediate(interp, argv[1], "-subcommands")) {
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ add_commands(interp, (jim_subcmd_type *) command_table->function, " ");
+ return JIM_OK;
+ }
+
+ if (Jim_CompareStringImmediate(interp, argv[1], "-subhelp")
+ || Jim_CompareStringImmediate(interp, argv[1], "-help")) {
+ subcmd = "-help";
+ }
+ else if (Jim_CompareStringImmediate(interp, argv[1], "-subusage")) {
+ subcmd = "-usage";
+ }
+
+ if (subcmd) {
+ nargv[nargc++] = Jim_NewStringObj(interp, "$handle", -1);
+ nargv[nargc++] = Jim_NewStringObj(interp, subcmd, -1);
+ if (argc >= 3) {
+ nargv[nargc++] = argv[2];
+ }
+ Jim_ParseSubCmd(interp, (jim_subcmd_type *) command_table->function, nargc, nargv);
+ Jim_FreeNewObj(interp, nargv[0]);
+ Jim_FreeNewObj(interp, nargv[1]);
+ return 0;
+ }
+ }
+
+ /* Check the number of args */
+ if (argc - 1 < command_table->minargs || (command_table->maxargs >= 0
+ && argc - 1 > command_table->maxargs)) {
+ set_wrong_args(interp, command_table, NULL);
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "\nUse \"", Jim_String(argv[0]),
+ " -help\" for help", NULL);
+ return JIM_ERR;
+ }
+
+ /* Not usage, but passed arg checking */
+ return -1;
+}
+/**
+ * UTF-8 utility functions
+ *
+ * (c) 2010 Steve Bennett <steveb@workware.net.au>
+ *
+ * See LICENCE for licence details.
+ */
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+
+/* This one is always implemented */
+int utf8_fromunicode(char *p, unsigned short uc)
+{
+ if (uc <= 0x7f) {
+ *p = uc;
+ return 1;
+ }
+ else if (uc <= 0x7ff) {
+ *p++ = 0xc0 | ((uc & 0x7c0) >> 6);
+ *p = 0x80 | (uc & 0x3f);
+ return 2;
+ }
+ else {
+ *p++ = 0xe0 | ((uc & 0xf000) >> 12);
+ *p++ = 0x80 | ((uc & 0xfc0) >> 6);
+ *p = 0x80 | (uc & 0x3f);
+ return 3;
+ }
+}
+
+#ifdef JIM_UTF8
+int utf8_charlen(int c)
+{
+ if ((c & 0x80) == 0) {
+ return 1;
+ }
+ if ((c & 0xe0) == 0xc0) {
+ return 2;
+ }
+ if ((c & 0xf0) == 0xe0) {
+ return 3;
+ }
+ if ((c & 0xf8) == 0xf0) {
+ return 4;
+ }
+ /* Invalid sequence */
+ return -1;
+}
+
+int utf8_strlen(const char *str, int bytelen)
+{
+ int charlen = 0;
+ if (bytelen < 0) {
+ bytelen = strlen(str);
+ }
+ while (bytelen) {
+ int c;
+ int l = utf8_tounicode(str, &c);
+ charlen++;
+ str += l;
+ bytelen -= l;
+ }
+ return charlen;
+}
+
+int utf8_index(const char *str, int index)
+{
+ const char *s = str;
+ while (index--) {
+ int c;
+ s += utf8_tounicode(s, &c);
+ }
+ return s - str;
+}
+
+int utf8_charequal(const char *s1, const char *s2)
+{
+ int c1, c2;
+
+ utf8_tounicode(s1, &c1);
+ utf8_tounicode(s2, &c2);
+
+ return c1 == c2;
+}
+
+int utf8_prev_len(const char *str, int len)
+{
+ int n = 1;
+
+ assert(len > 0);
+
+ /* Look up to len chars backward for a start-of-char byte */
+ while (--len) {
+ if ((str[-n] & 0x80) == 0) {
+ /* Start of a 1-byte char */
+ break;
+ }
+ if ((str[-n] & 0xc0) == 0xc0) {
+ /* Start of a multi-byte char */
+ break;
+ }
+ n++;
+ }
+ return n;
+}
+
+int utf8_tounicode(const char *str, int *uc)
+{
+ unsigned const char *s = (unsigned const char *)str;
+
+ if (s[0] < 0xc0) {
+ *uc = s[0];
+ return 1;
+ }
+ if (s[0] < 0xe0) {
+ if ((s[1] & 0xc0) == 0x80) {
+ *uc = ((s[0] & ~0xc0) << 6) | (s[1] & ~0x80);
+ return 2;
+ }
+ }
+ else if (s[0] < 0xf0) {
+ if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80)) {
+ *uc = ((s[0] & ~0xe0) << 12) | ((s[1] & ~0x80) << 6) | (s[2] & ~0x80);
+ return 3;
+ }
+ }
+
+ /* Invalid sequence, so just return the byte */
+ *uc = *s;
+ return 1;
+}
+
+struct casemap {
+ unsigned short code; /* code point */
+ signed char lowerdelta; /* add for lowercase, or if -128 use the ext table */
+ signed char upperdelta; /* add for uppercase, or offset into the ext table */
+};
+
+/* Extended table for codepoints where |delta| > 127 */
+struct caseextmap {
+ unsigned short lower;
+ unsigned short upper;
+};
+
+/* Generated mapping tables */
+#include "unicode_mapping.c"
+
+#define NUMCASEMAP sizeof(unicode_case_mapping) / sizeof(*unicode_case_mapping)
+
+static int cmp_casemap(const void *key, const void *cm)
+{
+ return *(int *)key - (int)((const struct casemap *)cm)->code;
+}
+
+static int utf8_map_case(int uc, int upper)
+{
+ const struct casemap *cm = bsearch(&uc, unicode_case_mapping, NUMCASEMAP, sizeof(*unicode_case_mapping), cmp_casemap);
+
+ if (cm) {
+ if (cm->lowerdelta == -128) {
+ uc = upper ? unicode_extmap[cm->upperdelta].upper : unicode_extmap[cm->upperdelta].lower;
+ }
+ else {
+ uc += upper ? cm->upperdelta : cm->lowerdelta;
+ }
+ }
+ return uc;
+}
+
+int utf8_upper(int uc)
+{
+ if (isascii(uc)) {
+ return toupper(uc);
+ }
+ return utf8_map_case(uc, 1);
+}
+
+int utf8_lower(int uc)
+{
+ if (isascii(uc)) {
+ return tolower(uc);
+ }
+
+ return utf8_map_case(uc, 0);
+}
+
+#endif
+#include <errno.h>
+#include <string.h>
+
+#ifdef USE_LINENOISE
+#include "linenoise.h"
+#else
+
+#define MAX_LINE_LEN 512
+
+static char *linenoise(const char *prompt)
+{
+ char *line = malloc(MAX_LINE_LEN);
+
+ fputs(prompt, stdout);
+ fflush(stdout);
+
+ if (fgets(line, MAX_LINE_LEN, stdin) == NULL) {
+ free(line);
+ return NULL;
+ }
+ return line;
+}
+#endif
+
+int Jim_InteractivePrompt(Jim_Interp *interp)
+{
+ int retcode = JIM_OK;
+ char *history_file = NULL;
+#ifdef USE_LINENOISE
+ const char *home;
+
+ home = getenv("HOME");
+ if (home) {
+ int history_len = strlen(home) + sizeof("/.jim_history");
+ history_file = Jim_Alloc(history_len);
+ snprintf(history_file, history_len, "%s/.jim_history", home);
+ linenoiseHistoryLoad(history_file);
+ }
+#endif
+
+ printf("Welcome to Jim version %d.%d" JIM_NL,
+ JIM_VERSION / 100, JIM_VERSION % 100);
+ Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1");
+
+ while (1) {
+ Jim_Obj *scriptObjPtr;
+ const char *result;
+ int reslen;
+ char prompt[20];
+ const char *str;
+
+ if (retcode != 0) {
+ const char *retcodestr = Jim_ReturnCode(retcode);
+
+ if (*retcodestr == '?') {
+ snprintf(prompt, sizeof(prompt) - 3, "[%d] ", retcode);
+ }
+ else {
+ snprintf(prompt, sizeof(prompt) - 3, "[%s] ", retcodestr);
+ }
+ }
+ else {
+ prompt[0] = '\0';
+ }
+ strcat(prompt, ". ");
+
+ scriptObjPtr = Jim_NewStringObj(interp, "", 0);
+ Jim_IncrRefCount(scriptObjPtr);
+ while (1) {
+ char state;
+ int len;
+ char *line;
+
+ line = linenoise(prompt);
+ if (line == NULL) {
+ if (errno == EINTR) {
+ continue;
+ }
+ Jim_DecrRefCount(interp, scriptObjPtr);
+ goto out;
+ }
+ if (Jim_Length(scriptObjPtr) != 0) {
+ Jim_AppendString(interp, scriptObjPtr, "\n", 1);
+ }
+ Jim_AppendString(interp, scriptObjPtr, line, -1);
+ free(line);
+ str = Jim_GetString(scriptObjPtr, &len);
+ if (len == 0) {
+ continue;
+ }
+ if (Jim_ScriptIsComplete(str, len, &state))
+ break;
+
+ snprintf(prompt, sizeof(prompt), "%c> ", state);
+ }
+#ifdef USE_LINENOISE
+ if (strcmp(str, "h") == 0) {
+ /* built-in history command */
+ int i;
+ int len;
+ char **history = linenoiseHistory(&len);
+ for (i = 0; i < len; i++) {
+ printf("%4d %s\n", i + 1, history[i]);
+ }
+ Jim_DecrRefCount(interp, scriptObjPtr);
+ continue;
+ }
+
+ linenoiseHistoryAdd(Jim_String(scriptObjPtr));
+ linenoiseHistorySave(history_file);
+#endif
+ retcode = Jim_EvalObj(interp, scriptObjPtr);
+ Jim_DecrRefCount(interp, scriptObjPtr);
+
+
+
+ if (retcode == JIM_EXIT) {
+ Jim_Free(history_file);
+ return JIM_EXIT;
+ }
+ if (retcode == JIM_ERR) {
+ Jim_MakeErrorMessage(interp);
+ }
+ result = Jim_GetString(Jim_GetResult(interp), &reslen);
+ if (reslen) {
+ printf("%s\n", result);
+ }
+ }
+ out:
+ Jim_Free(history_file);
+ return JIM_OK;
+}
+/*
+ * Implements the internals of the format command for jim
+ *
+ * The FreeBSD license
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 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 BY THE JIM TCL PROJECT ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation
+ * are those of the authors and should not be interpreted as representing
+ * official policies, either expressed or implied, of the Jim Tcl Project.
+ *
+ * Based on code originally from Tcl 8.5:
+ *
+ * Copyright (c) 1995-1997 Sun Microsystems, Inc.
+ * Copyright (c) 1999 by Scriptics Corporation.
+ *
+ * See the file "tcl.license.terms" for information on usage and redistribution of
+ * this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ */
+#include <ctype.h>
+#include <string.h>
+
+
+#define JIM_UTF_MAX 3
+#define JIM_INTEGER_SPACE 24
+#define MAX_FLOAT_WIDTH 320
+
+/**
+ * Apply the printf-like format in fmtObjPtr with the given arguments.
+ *
+ * Returns a new object with zero reference count if OK, or NULL on error.
+ */
+Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv)
+{
+ const char *span, *format, *formatEnd, *msg;
+ int numBytes = 0, objIndex = 0, gotXpg = 0, gotSequential = 0;
+ static const char *mixedXPG =
+ "cannot mix \"%\" and \"%n$\" conversion specifiers";
+ static const char *badIndex[2] = {
+ "not enough arguments for all format specifiers",
+ "\"%n$\" argument index out of range"
+ };
+ int formatLen;
+ Jim_Obj *resultPtr;
+
+ /* A single buffer is used to store numeric fields (with sprintf())
+ * This buffer is allocated/reallocated as necessary
+ */
+ char *num_buffer = NULL;
+ int num_buffer_size = 0;
+
+ span = format = Jim_GetString(fmtObjPtr, &formatLen);
+ formatEnd = format + formatLen;
+ resultPtr = Jim_NewStringObj(interp, "", 0);
+
+ while (format != formatEnd) {
+ char *end;
+ int gotMinus, sawFlag;
+ int gotPrecision, useShort;
+ long width, precision;
+ int newXpg;
+ int ch;
+ int step;
+ int doubleType;
+ char pad = ' ';
+ char spec[2*JIM_INTEGER_SPACE + 12];
+ char *p;
+
+ int formatted_chars;
+ int formatted_bytes;
+ const char *formatted_buf;
+
+ step = utf8_tounicode(format, &ch);
+ format += step;
+ if (ch != '%') {
+ numBytes += step;
+ continue;
+ }
+ if (numBytes) {
+ Jim_AppendString(interp, resultPtr, span, numBytes);
+ numBytes = 0;
+ }
+
+ /*
+ * Saw a % : process the format specifier.
+ *
+ * Step 0. Handle special case of escaped format marker (i.e., %%).
+ */
+
+ step = utf8_tounicode(format, &ch);
+ if (ch == '%') {
+ span = format;
+ numBytes = step;
+ format += step;
+ continue;
+ }
+
+ /*
+ * Step 1. XPG3 position specifier
+ */
+
+ newXpg = 0;
+ if (isdigit(ch)) {
+ int position = strtoul(format, &end, 10);
+ if (*end == '$') {
+ newXpg = 1;
+ objIndex = position - 1;
+ format = end + 1;
+ step = utf8_tounicode(format, &ch);
+ }
+ }
+ if (newXpg) {
+ if (gotSequential) {
+ msg = mixedXPG;
+ goto errorMsg;
+ }
+ gotXpg = 1;
+ } else {
+ if (gotXpg) {
+ msg = mixedXPG;
+ goto errorMsg;
+ }
+ gotSequential = 1;
+ }
+ if ((objIndex < 0) || (objIndex >= objc)) {
+ msg = badIndex[gotXpg];
+ goto errorMsg;
+ }
+
+ /*
+ * Step 2. Set of flags. Also build up the sprintf spec.
+ */
+ p = spec;
+ *p++ = '%';
+
+ gotMinus = 0;
+ sawFlag = 1;
+ do {
+ switch (ch) {
+ case '-':
+ gotMinus = 1;
+ break;
+ case '0':
+ pad = ch;
+ break;
+ case ' ':
+ case '+':
+ case '#':
+ break;
+ default:
+ sawFlag = 0;
+ continue;
+ }
+ *p++ = ch;
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ } while (sawFlag);
+
+ /*
+ * Step 3. Minimum field width.
+ */
+
+ width = 0;
+ if (isdigit(ch)) {
+ width = strtoul(format, &end, 10);
+ format = end;
+ step = utf8_tounicode(format, &ch);
+ } else if (ch == '*') {
+ if (objIndex >= objc - 1) {
+ msg = badIndex[gotXpg];
+ goto errorMsg;
+ }
+ if (Jim_GetLong(interp, objv[objIndex], &width) != JIM_OK) {
+ goto error;
+ }
+ if (width < 0) {
+ width = -width;
+ if (!gotMinus) {
+ *p++ = '-';
+ gotMinus = 1;
+ }
+ }
+ objIndex++;
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ }
+
+ /*
+ * Step 4. Precision.
+ */
+
+ gotPrecision = precision = 0;
+ if (ch == '.') {
+ gotPrecision = 1;
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ }
+ if (isdigit(ch)) {
+ precision = strtoul(format, &end, 10);
+ format = end;
+ step = utf8_tounicode(format, &ch);
+ } else if (ch == '*') {
+ if (objIndex >= objc - 1) {
+ msg = badIndex[gotXpg];
+ goto errorMsg;
+ }
+ if (Jim_GetLong(interp, objv[objIndex], &precision) != JIM_OK) {
+ goto error;
+ }
+
+ /*
+ * TODO: Check this truncation logic.
+ */
+
+ if (precision < 0) {
+ precision = 0;
+ }
+ objIndex++;
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ }
+
+ /*
+ * Step 5. Length modifier.
+ */
+
+ useShort = 0;
+ if (ch == 'h') {
+ useShort = 1;
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ } else if (ch == 'l') {
+ /* Just for compatibility. All non-short integers are wide. */
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ if (ch == 'l') {
+ format += step;
+ step = utf8_tounicode(format, &ch);
+ }
+ }
+
+ format += step;
+ span = format;
+
+ /*
+ * Step 6. The actual conversion character.
+ */
+
+ if (ch == 'i') {
+ ch = 'd';
+ }
+
+ doubleType = 0;
+
+ /* Each valid conversion will set:
+ * formatted_buf - the result to be added
+ * formatted_chars - the length of formatted_buf in characters
+ * formatted_bytes - the length of formatted_buf in bytes
+ */
+ switch (ch) {
+ case '\0':
+ msg = "format string ended in middle of field specifier";
+ goto errorMsg;
+ case 's': {
+ formatted_buf = Jim_GetString(objv[objIndex], &formatted_bytes);
+ formatted_chars = Jim_Utf8Length(interp, objv[objIndex]);
+ if (gotPrecision && (precision < formatted_chars)) {
+ /* Need to build a (null terminated) truncated string */
+ formatted_chars = precision;
+ formatted_bytes = utf8_index(formatted_buf, precision);
+ }
+ break;
+ }
+ case 'c': {
+ jim_wide code;
+
+ if (Jim_GetWide(interp, objv[objIndex], &code) != JIM_OK) {
+ goto error;
+ }
+ /* Just store the value in the 'spec' buffer */
+ formatted_bytes = utf8_fromunicode(spec, code);
+ formatted_buf = spec;
+ formatted_chars = 1;
+ break;
+ }
+
+ case 'e':
+ case 'E':
+ case 'f':
+ case 'g':
+ case 'G':
+ doubleType = 1;
+ /* fall through */
+ case 'd':
+ case 'u':
+ case 'o':
+ case 'x':
+ case 'X': {
+ jim_wide w;
+ double d;
+ int length;
+
+ /* Fill in the width and precision */
+ if (width) {
+ p += sprintf(p, "%ld", width);
+ }
+ if (gotPrecision) {
+ p += sprintf(p, ".%ld", precision);
+ }
+
+ /* Now the modifier, and get the actual value here */
+ if (doubleType) {
+ if (Jim_GetDouble(interp, objv[objIndex], &d) != JIM_OK) {
+ goto error;
+ }
+ length = MAX_FLOAT_WIDTH;
+ }
+ else {
+ if (Jim_GetWide(interp, objv[objIndex], &w) != JIM_OK) {
+ goto error;
+ }
+ length = JIM_INTEGER_SPACE;
+ if (useShort) {
+ *p++ = 'h';
+ if (ch == 'd') {
+ w = (short)w;
+ }
+ else {
+ w = (unsigned short)w;
+ }
+ }
+ else {
+ *p++ = 'l';
+#ifdef HAVE_LONG_LONG
+ if (sizeof(long long) == sizeof(jim_wide)) {
+ *p++ = 'l';
+ }
+#endif
+ }
+ }
+
+ *p++ = (char) ch;
+ *p = '\0';
+
+ /* Adjust length for width and precision */
+ if (width > length) {
+ length = width;
+ }
+ if (gotPrecision) {
+ length += precision;
+ }
+
+ /* Increase the size of the buffer if needed */
+ if (num_buffer_size < length + 1) {
+ num_buffer_size = length + 1;
+ num_buffer = Jim_Realloc(num_buffer, num_buffer_size);
+ }
+
+ if (doubleType) {
+ snprintf(num_buffer, length + 1, spec, d);
+ }
+ else {
+ formatted_bytes = snprintf(num_buffer, length + 1, spec, w);
+ }
+ formatted_chars = formatted_bytes = strlen(num_buffer);
+ formatted_buf = num_buffer;
+ break;
+ }
+
+ default: {
+ /* Just reuse the 'spec' buffer */
+ spec[0] = ch;
+ spec[1] = '\0';
+ Jim_SetResultFormatted(interp, "bad field specifier \"%s\"", spec);
+ goto error;
+ }
+ }
+
+ if (!gotMinus) {
+ while (formatted_chars < width) {
+ Jim_AppendString(interp, resultPtr, &pad, 1);
+ formatted_chars++;
+ }
+ }
+
+ Jim_AppendString(interp, resultPtr, formatted_buf, formatted_bytes);
+
+ while (formatted_chars < width) {
+ Jim_AppendString(interp, resultPtr, &pad, 1);
+ formatted_chars++;
+ }
+
+ objIndex += gotSequential;
+ }
+ if (numBytes) {
+ Jim_AppendString(interp, resultPtr, span, numBytes);
+ }
+
+ Jim_Free(num_buffer);
+ return resultPtr;
+
+ errorMsg:
+ Jim_SetResultString(interp, msg, -1);
+ error:
+ Jim_FreeNewObj(interp, resultPtr);
+ Jim_Free(num_buffer);
+ return NULL;
+}
+/*
+ * regcomp and regexec -- regsub and regerror are elsewhere
+ *
+ * Copyright (c) 1986 by University of Toronto.
+ * Written by Henry Spencer. Not derived from licensed software.
+ *
+ * Permission is granted to anyone to use this software for any
+ * purpose on any computer system, and to redistribute it freely,
+ * subject to the following restrictions:
+ *
+ * 1. The author is not responsible for the consequences of use of
+ * this software, no matter how awful, even if they arise
+ * from defects in it.
+ *
+ * 2. The origin of this software must not be misrepresented, either
+ * by explicit claim or by omission.
+ *
+ * 3. Altered versions must be plainly marked as such, and must not
+ * be misrepresented as being the original software.
+ *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
+ *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
+ *** to assist in implementing egrep.
+ *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
+ *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
+ *** as in BSD grep and ex.
+ *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
+ *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
+ *** THIS IS AN ALTERED VERSION. It was altered by James A. Woods,
+ *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
+ *** THIS IS AN ALTERED VERSION. It was altered by Christopher Seiwald
+ *** seiwald@vix.com, on 28 August 1993, for use in jam. Regmagic.h
+ *** was moved into regexp.h, and the include of regexp.h now uses "'s
+ *** to avoid conflicting with the system regexp.h. Const, bless its
+ *** soul, was removed so it can compile everywhere. The declaration
+ *** of strchr() was in conflict on AIX, so it was removed (as it is
+ *** happily defined in string.h).
+ *** THIS IS AN ALTERED VERSION. It was altered by Christopher Seiwald
+ *** seiwald@perforce.com, on 20 January 2000, to use function prototypes.
+ *** THIS IS AN ALTERED VERSION. It was altered by Christopher Seiwald
+ *** seiwald@perforce.com, on 05 November 2002, to const string literals.
+ *
+ * THIS IS AN ALTERED VERSION. It was altered by Steve Bennett <steveb@workware.net.au>
+ * on 16 October 2010, to remove static state and add better Tcl ARE compatibility.
+ * This includes counted repetitions, UTF-8 support, character classes,
+ * shorthand character classes, increased number of parentheses to 100,
+ * backslash escape sequences. It also removes \n as an alternative to |.
+ *
+ * Beware that some of this code is subtly aware of the way operator
+ * precedence is structured in regular expressions. Serious changes in
+ * regular-expression syntax might require a total rethink.
+ */
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#if !defined(HAVE_REGCOMP) || defined(JIM_REGEXP)
+
+/*
+ * Structure for regexp "program". This is essentially a linear encoding
+ * of a nondeterministic finite-state machine (aka syntax charts or
+ * "railroad normal form" in parsing technology). Each node is an opcode
+ * plus a "next" pointer, possibly plus an operand. "Next" pointers of
+ * all nodes except BRANCH implement concatenation; a "next" pointer with
+ * a BRANCH on both ends of it is connecting two alternatives. (Here we
+ * have one of the subtle syntax dependencies: an individual BRANCH (as
+ * opposed to a collection of them) is never concatenated with anything
+ * because of operator precedence.) The operand of some types of node is
+ * a literal string; for others, it is a node leading into a sub-FSM. In
+ * particular, the operand of a BRANCH node is the first node of the branch.
+ * (NB this is *not* a tree structure: the tail of the branch connects
+ * to the thing following the set of BRANCHes.) The opcodes are:
+ */
+
+/* This *MUST* be less than (255-20)/2=117 */
+#define REG_MAX_PAREN 100
+
+/* definition number opnd? meaning */
+#define END 0 /* no End of program. */
+#define BOL 1 /* no Match "" at beginning of line. */
+#define EOL 2 /* no Match "" at end of line. */
+#define ANY 3 /* no Match any one character. */
+#define ANYOF 4 /* str Match any character in this string. */
+#define ANYBUT 5 /* str Match any character not in this string. */
+#define BRANCH 6 /* node Match this alternative, or the next... */
+#define BACK 7 /* no Match "", "next" ptr points backward. */
+#define EXACTLY 8 /* str Match this string. */
+#define NOTHING 9 /* no Match empty string. */
+#define STAR 10 /* node Match this (simple) thing 0 or more times. */
+#define STARMIN 11 /* node Match this (simple) thing 0 or more times, mininal match. */
+#define PLUS 12 /* node Match this (simple) thing 1 or more times. */
+#define PLUSMIN 13 /* node Match this (simple) thing 1 or more times, mininal match. */
+#define WORDA 14 /* no Match "" at wordchar, where prev is nonword */
+#define WORDZ 15 /* no Match "" at nonwordchar, where prev is word */
+#define OPEN 20 /* no Mark this point in input as start of #n. */
+ /* OPEN+1 is number 1, etc. */
+#define CLOSE (OPEN+REG_MAX_PAREN) /* no Analogous to OPEN. */
+#define CLOSE_END (CLOSE+REG_MAX_PAREN)
+
+/*
+ * The first byte of the regexp internal "program" is actually this magic
+ * number; the start node begins in the second byte.
+ */
+#define REG_MAGIC 0xFADED00D
+
+/*
+ * Opcode notes:
+ *
+ * BRANCH The set of branches constituting a single choice are hooked
+ * together with their "next" pointers, since precedence prevents
+ * anything being concatenated to any individual branch. The
+ * "next" pointer of the last BRANCH in a choice points to the
+ * thing following the whole choice. This is also where the
+ * final "next" pointer of each individual branch points; each
+ * branch starts with the operand node of a BRANCH node.
+ *
+ * BACK Normal "next" pointers all implicitly point forward; BACK
+ * exists to make loop structures possible.
+ *
+ * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
+ * BRANCH structures using BACK. Simple cases (one character
+ * per match) are implemented with STAR and PLUS for speed
+ * and to minimize recursive plunges.
+ *
+ * OPEN,CLOSE ...are numbered at compile time.
+ */
+
+/*
+ * A node is one char of opcode followed by two chars of "next" pointer.
+ * "Next" pointers are stored as two 8-bit pieces, high order first. The
+ * value is a positive offset from the opcode of the node containing it.
+ * An operand, if any, simply follows the node. (Note that much of the
+ * code generation knows about this implicit relationship.)
+ *
+ * Using two bytes for the "next" pointer is vast overkill for most things,
+ * but allows patterns to get big without disasters.
+ */
+#define OP(p) ((p)[0])
+#define NEXT(p) ((p)[1])
+#define OPERAND(p) ((p) + 2)
+
+/*
+ * See regmagic.h for one further detail of program structure.
+ */
+
+
+/*
+ * Utility definitions.
+ */
+
+#define FAIL(R,M) { (R)->err = (M); return (M); }
+#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?' || (c) == '{')
+#define META "^$.[()|?{+*"
+
+/*
+ * Flags to be passed up and down.
+ */
+#define HASWIDTH 01 /* Known never to match null string. */
+#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
+#define SPSTART 04 /* Starts with * or +. */
+#define WORST 0 /* Worst case. */
+
+/*
+ * Forward declarations for regcomp()'s friends.
+ */
+static int *reg(regex_t *preg, int paren /* Parenthesized? */, int *flagp );
+static int *regpiece(regex_t *preg, int *flagp );
+static int *regbranch(regex_t *preg, int *flagp );
+static int *regatom(regex_t *preg, int *flagp );
+static int *regnode(regex_t *preg, int op );
+static const int *regnext(regex_t *preg, const int *p );
+static void regc(regex_t *preg, int b );
+static int *reginsert(regex_t *preg, int op, int *opnd );
+static void regtail(regex_t *preg, int *p, const int *val );
+static void regoptail(regex_t *preg, int *p, const int *val );
+
+static int reg_range_find(const int *string, int c, int nocase);
+static const char *str_find(const char *string, int c, int nocase);
+static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase);
+
+/*#define DEBUG*/
+#ifdef DEBUG
+int regnarrate = 0;
+static void regdump(regex_t *preg);
+static const char *regprop( const int *op );
+#endif
+
+
+static int regdummy;
+
+/**
+ * Returns the length of the null-terminated integer sequence.
+ */
+static int str_int_len(const int *seq)
+{
+ int n = 0;
+ while (*seq++) {
+ n++;
+ }
+ return n;
+}
+
+/*
+ - regcomp - compile a regular expression into internal code
+ *
+ * We can't allocate space until we know how big the compiled form will be,
+ * but we can't compile it (and thus know how big it is) until we've got a
+ * place to put the code. So we cheat: we compile it twice, once with code
+ * generation turned off and size counting turned on, and once "for real".
+ * This also means that we don't allocate space until we are sure that the
+ * thing really will compile successfully, and we never have to move the
+ * code and thus invalidate pointers into it. (Note that it has to be in
+ * one piece because free() must be able to free it all.)
+ *
+ * Beware that the optimization-preparation code in here knows about some
+ * of the structure of the compiled regexp.
+ */
+int regcomp(regex_t *preg, const char *exp, int cflags)
+{
+ const int *scan;
+ const int *longest;
+ unsigned len;
+ int flags;
+
+ memset(preg, 0, sizeof(*preg));
+
+ if (exp == NULL)
+ FAIL(preg, REG_ERR_NULL_ARGUMENT);
+
+ /* First pass: determine size, legality. */
+ preg->cflags = cflags;
+ preg->regparse = exp;
+ preg->re_nsub = 0;
+ preg->regsize = 0L;
+ preg->regcode = &regdummy;
+ regc(preg, REG_MAGIC);
+ if (reg(preg, 0, &flags) == NULL)
+ return preg->err;
+
+ /* Small enough for pointer-storage convention? */
+ if (preg->regsize >= 32767L || preg->re_nsub >= REG_MAX_PAREN) /* Probably could be 65535L. */
+ FAIL(preg,REG_ERR_TOO_BIG);
+
+ /* Allocate space. */
+ preg->program = malloc(preg->regsize * sizeof(*preg->program));
+ if (preg->program == NULL)
+ FAIL(preg, REG_ERR_NOMEM);
+
+ /* Second pass: emit code. */
+ preg->regparse = exp;
+ preg->re_nsub = 0;
+ preg->regsize = 0L;
+ preg->regcode = preg->program;
+ regc(preg, REG_MAGIC);
+ if (reg(preg, 0, &flags) == NULL)
+ return preg->err;
+
+ /* Dig out information for optimizations. */
+ preg->regstart = 0; /* Worst-case defaults. */
+ preg->reganch = 0;
+ preg->regmust = NULL;
+ preg->regmlen = 0;
+ scan = preg->program+1; /* First BRANCH. */
+ if (OP(regnext(preg, scan)) == END) { /* Only one top-level choice. */
+ scan = OPERAND(scan);
+
+ /* Starting-point info. */
+ if (OP(scan) == EXACTLY)
+ preg->regstart = *OPERAND(scan);
+ else if (OP(scan) == BOL)
+ preg->reganch++;
+
+ /*
+ * If there's something expensive in the r.e., find the
+ * longest literal string that must appear and make it the
+ * regmust. Resolve ties in favor of later strings, since
+ * the regstart check works with the beginning of the r.e.
+ * and avoiding duplication strengthens checking. Not a
+ * strong reason, but sufficient in the absence of others.
+ */
+ if (flags&SPSTART) {
+ longest = NULL;
+ len = 0;
+ for (; scan != NULL; scan = regnext(preg, scan)) {
+ if (OP(scan) == EXACTLY) {
+ int plen = str_int_len(OPERAND(scan));
+ if (plen >= len) {
+ longest = OPERAND(scan);
+ len = plen;
+ }
+ }
+ }
+ preg->regmust = longest;
+ preg->regmlen = len;
+ }
+ }
+
+#ifdef DEBUG
+ regdump(preg);
+#endif
+
+ return 0;
+}
+
+/*
+ - reg - regular expression, i.e. main body or parenthesized thing
+ *
+ * Caller must absorb opening parenthesis.
+ *
+ * Combining parenthesis handling with the base level of regular expression
+ * is a trifle forced, but the need to tie the tails of the branches to what
+ * follows makes it hard to avoid.
+ */
+static int *reg(regex_t *preg, int paren /* Parenthesized? */, int *flagp )
+{
+ int *ret;
+ int *br;
+ const int *ender;
+ int parno = 0;
+ int flags;
+
+ *flagp = HASWIDTH; /* Tentatively. */
+
+ /* Make an OPEN node, if parenthesized. */
+ if (paren) {
+ parno = ++preg->re_nsub;
+ ret = regnode(preg, OPEN+parno);
+ } else
+ ret = NULL;
+
+ /* Pick up the branches, linking them together. */
+ br = regbranch(preg, &flags);
+ if (br == NULL)
+ return(NULL);
+ if (ret != NULL)
+ regtail(preg, ret, br); /* OPEN -> first. */
+ else
+ ret = br;
+ if (!(flags&HASWIDTH))
+ *flagp &= ~HASWIDTH;
+ *flagp |= flags&SPSTART;
+ while (*preg->regparse == '|') {
+ preg->regparse++;
+ br = regbranch(preg, &flags);
+ if (br == NULL)
+ return(NULL);
+ regtail(preg, ret, br); /* BRANCH -> BRANCH. */
+ if (!(flags&HASWIDTH))
+ *flagp &= ~HASWIDTH;
+ *flagp |= flags&SPSTART;
+ }
+
+ /* Make a closing node, and hook it on the end. */
+ ender = regnode(preg, (paren) ? CLOSE+parno : END);
+ regtail(preg, ret, ender);
+
+ /* Hook the tails of the branches to the closing node. */
+ for (br = ret; br != NULL; br = (int *)regnext(preg, br))
+ regoptail(preg, br, ender);
+
+ /* Check for proper termination. */
+ if (paren && *preg->regparse++ != ')') {
+ preg->err = REG_ERR_UNMATCHED_PAREN;
+ return NULL;
+ } else if (!paren && *preg->regparse != '\0') {
+ if (*preg->regparse == ')') {
+ preg->err = REG_ERR_UNMATCHED_PAREN;
+ return NULL;
+ } else {
+ preg->err = REG_ERR_JUNK_ON_END;
+ return NULL;
+ }
+ }
+
+ return(ret);
+}
+
+/*
+ - regbranch - one alternative of an | operator
+ *
+ * Implements the concatenation operator.
+ */
+static int *regbranch(regex_t *preg, int *flagp )
+{
+ int *ret;
+ int *chain;
+ int *latest;
+ int flags;
+
+ *flagp = WORST; /* Tentatively. */
+
+ ret = regnode(preg, BRANCH);
+ chain = NULL;
+ while (*preg->regparse != '\0' && *preg->regparse != ')' &&
+ *preg->regparse != '|') {
+ latest = regpiece(preg, &flags);
+ if (latest == NULL)
+ return(NULL);
+ *flagp |= flags&HASWIDTH;
+ if (chain == NULL) {/* First piece. */
+ *flagp |= flags&SPSTART;
+ }
+ else {
+ regtail(preg, chain, latest);
+ }
+ chain = latest;
+ }
+ if (chain == NULL) /* Loop ran zero times. */
+ (void) regnode(preg, NOTHING);
+
+ return(ret);
+}
+
+/**
+ * Duplicates the program at 'pos' of length 'len' at the end of the program.
+ *
+ * If 'maketail' is set, the next point for 'pos' is set to skip to the next
+ * part of the program after 'pos'.
+ */
+static int *regdup(regex_t *preg, int *pos, int len, int maketail)
+{
+ int i;
+
+ preg->regsize += len;
+
+ if (preg->regcode == &regdummy) {
+ return pos;
+ }
+
+ for (i = 0; i < len; i++) {
+ regc(preg, pos[i]);
+ }
+ if (maketail) {
+ regtail(preg, pos, pos + len);
+ }
+ return preg->regcode - len;
+}
+
+/*
+ - regpiece - something followed by possible [*+?]
+ *
+ * Note that the branching code sequences used for ? and the general cases
+ * of * and + are somewhat optimized: they use the same NOTHING node as
+ * both the endmarker for their branch list and the body of the last branch.
+ * It might seem that this node could be dispensed with entirely, but the
+ * endmarker role is not redundant.
+ */
+static int *regpiece(regex_t *preg, int *flagp)
+{
+ int *ret;
+ char op;
+ int *next;
+ int flags;
+ int size = preg->regsize;
+ int *chain = NULL;
+
+ ret = regatom(preg, &flags);
+ if (ret == NULL)
+ return(NULL);
+
+ size = preg->regsize - size;
+
+ op = *preg->regparse;
+ if (!ISMULT(op)) {
+ *flagp = flags;
+ return(ret);
+ }
+
+ if (!(flags&HASWIDTH) && op != '?') {
+ preg->err = REG_ERR_OPERAND_COULD_BE_EMPTY;
+ return NULL;
+ }
+ *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
+
+ /* Handle braces (counted repetition) by expansion */
+ if (op == '{') {
+ int min = 0;
+ int max = 0;
+ char *end;
+
+ min = strtoul(preg->regparse + 1, &end, 10);
+ if (end == preg->regparse + 1) {
+ if (*end == ',') {
+ min = 0;
+ }
+ else {
+ preg->err = REG_ERR_BAD_COUNT;
+ return NULL;
+ }
+ }
+ if (*end == '}') {
+ max = min;
+ }
+ else {
+ preg->regparse = end;
+ max = strtoul(preg->regparse + 1, &end, 10);
+ if (*end != '}') {
+ preg->err = REG_ERR_UNMATCHED_BRACES;
+ return NULL;
+ }
+ }
+ if (end == preg->regparse + 1) {
+ max = -1;
+ }
+ else if (max < min || max >= 100) {
+ preg->err = REG_ERR_BAD_COUNT;
+ return NULL;
+ }
+ if (min >= 100) {
+ preg->err = REG_ERR_BAD_COUNT;
+ return NULL;
+ }
+
+ preg->regparse = strchr(preg->regparse, '}');
+
+ /* By default, chain to the start of the sequence */
+ chain = ret;
+
+ if (max < 0 || max == min) {
+ /* Simple case */
+ if (max == min) {
+ if (min == 0) {
+ /* {0,0} so do nothing at all */
+ reginsert(preg, NOTHING, ret);
+ preg->regparse++;
+ return ret;
+ }
+ /* Output 'min - 1' instances of 'x' */
+ min--;
+ op = 0;
+ }
+ else {
+ /* {n,} is just xxxx* */
+ op = '*';
+ /* No - chain to the tail of the sequence */
+ chain = NULL;
+ }
+
+ /* We need to duplicate the arg 'min' times */
+ while (min--) {
+ ret = regdup(preg, ret, size, 1);
+ }
+ }
+ else {
+ /* Complex case */
+ int i;
+
+ /* Chaining is needed */
+
+ /* Need to emit some min args first */
+ for (i = 0; i < min; i++) {
+ ret = regdup(preg, ret, size, 1);
+ }
+
+ for (i = min; i < max; i++) {
+ /* Emit x */
+ /* There is already one instance of 'reg' at the end */
+ /* Add another 'reg' at the end */
+ int *prog;
+
+ /* Convert to (x|), just like ? */
+ prog = reginsert(preg, BRANCH, ret); /* Either x */
+ regtail(preg, ret, regnode(preg, BRANCH)); /* or */
+ next = regnode(preg, NOTHING); /* null. */
+ regtail(preg, ret, next);
+ regoptail(preg, ret, next);
+
+ /* Now grab a copy ready for the next iteration */
+ if (i != max - 1) {
+ ret = regdup(preg, prog, size, 0);
+ }
+ }
+ op = 0;
+ }
+ }
+
+ if (op == '*' && (flags&SIMPLE)) {
+ if (preg->regparse[1] == '?') {
+ preg->regparse++;
+ reginsert(preg, STARMIN, ret);
+ }
+ else {
+ reginsert(preg, STAR, ret);
+ }
+ }
+ else if (op == '*') {
+ if (preg->regparse[1] == '?') {
+ int *last;
+ int *branch;
+
+ preg->regparse++;
+
+ /* Emit x*? as (|x&), where & means "self". */
+ /* x points to BRANCH */
+
+ /* Note that we need to insert BRANCH NOTHING BRANCH in front.
+ * Carefully keep track of where everything is inserted.
+ */
+ chain = ret;
+ next = ret = reginsert(preg, BRANCH, ret);
+ branch = ret = reginsert(preg, NOTHING, ret);
+ ret = reginsert(preg, BRANCH, ret);
+ regtail(preg, chain, branch);
+ regtail(preg, ret, regnode(preg, BACK));
+ regtail(preg, ret, chain);
+ last = regnode(preg, NOTHING);
+ regtail(preg, chain, last);
+ regtail(preg, next, last);
+ }
+ else {
+ /* Emit x* as (x&|), where & means "self". */
+ reginsert(preg, BRANCH, ret); /* Either x */
+ regoptail(preg, ret, regnode(preg, BACK)); /* and loop */
+ regoptail(preg, ret, ret); /* back */
+ regtail(preg, ret, regnode(preg, BRANCH)); /* or */
+ regtail(preg, ret, regnode(preg, NOTHING)); /* null. */
+ }
+ } else if (op == '+' && (flags&SIMPLE)) {
+ if (preg->regparse[1] == '?') {
+ preg->regparse++;
+ reginsert(preg, PLUSMIN, ret);
+ }
+ else {
+ reginsert(preg, PLUS, ret);
+ }
+ }
+ else if (op == '+') {
+ if (preg->regparse[1] == '?') {
+ int *last;
+ preg->regparse++;
+
+ /* Emit x+? as x(|&), where & means "self". */
+ /* x points to BRANCH */
+ regtail(preg, ret, regnode(preg, BRANCH));
+ next = regnode(preg, NOTHING);
+ regtail(preg, ret, regnode(preg, BRANCH));
+ regtail(preg, regnode(preg, BACK), ret);
+ /* Dummy node that both paths can point to */
+ last = regnode(preg, NOTHING);
+ regtail(preg, next, last);
+ regtail(preg, ret, last);
+ }
+ else {
+ /* Emit x+ as x(&|), where & means "self". */
+ next = regnode(preg, BRANCH); /* Either */
+ regtail(preg, ret, next);
+ regtail(preg, regnode(preg, BACK), ret); /* loop back */
+ regtail(preg, next, regnode(preg, BRANCH)); /* or */
+ regtail(preg, ret, regnode(preg, NOTHING)); /* null. */
+ }
+ } else if (op == '?') {
+ if (preg->regparse[1] == '?') {
+ /* Emit x?? as (|x) */
+ int *last;
+ int *branch;
+
+ preg->regparse++;
+
+ chain = ret;
+ next = ret = reginsert(preg, BRANCH, ret);
+ branch = ret = reginsert(preg, NOTHING, ret);
+ ret = reginsert(preg, BRANCH, ret);
+ regtail(preg, chain, branch);
+ regtail(preg, ret, chain);
+ last = regnode(preg, NOTHING);
+ regtail(preg, chain, last);
+ regtail(preg, next, last);
+ }
+ else {
+ /* Emit x? as (x|) */
+ reginsert(preg, BRANCH, ret); /* Either x */
+ regtail(preg, ret, regnode(preg, BRANCH)); /* or */
+ next = regnode(preg, NOTHING); /* null. */
+ regtail(preg, ret, next);
+ regoptail(preg, ret, next);
+ }
+ }
+ preg->regparse++;
+ if (ISMULT(*preg->regparse)) {
+ preg->err = REG_ERR_NESTED_COUNT;
+ return NULL;
+ }
+
+ return chain ? chain : ret;
+}
+
+/**
+ * Add all characters in the inclusive range between lower and upper.
+ *
+ * Handles a swapped range (upper < lower).
+ */
+static void reg_addrange(regex_t *preg, int lower, int upper)
+{
+ if (lower > upper) {
+ reg_addrange(preg, upper, lower);
+ }
+ /* Add a range as length, start */
+ regc(preg, upper - lower + 1);
+ regc(preg, lower);
+}
+
+/**
+ * Add a null-terminated literal string as a set of ranges.
+ */
+static void reg_addrange_str(regex_t *preg, const char *str)
+{
+ while (*str) {
+ reg_addrange(preg, *str, *str);
+ str++;
+ }
+}
+
+/**
+ * Extracts the next unicode char from utf8.
+ *
+ * If 'upper' is set, converts the char to uppercase.
+ */
+static int reg_utf8_tounicode_case(const char *s, int *uc, int upper)
+{
+ int l = utf8_tounicode(s, uc);
+ if (upper) {
+ *uc = utf8_upper(*uc);
+ }
+ return l;
+}
+
+/**
+ * Converts a hex digit to decimal.
+ *
+ * Returns -1 for an invalid hex digit.
+ */
+static int hexdigitval(int c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return -1;
+}
+
+/**
+ * Parses up to 'n' hex digits at 's' and stores the result in *uc.
+ *
+ * Returns the number of hex digits parsed.
+ * If there are no hex digits, returns 0 and stores nothing.
+ */
+static int parse_hex(const char *s, int n, int *uc)
+{
+ int val = 0;
+ int k;
+
+ for (k = 0; k < n; k++) {
+ int c = hexdigitval(*s++);
+ if (c == -1) {
+ break;
+ }
+ val = (val << 4) | c;
+ }
+ if (k) {
+ *uc = val;
+ }
+ return k;
+}
+
+/**
+ * Call for chars after a backlash to decode the escape sequence.
+ *
+ * Stores the result in *ch.
+ *
+ * Returns the number of bytes consumed.
+ */
+static int reg_decode_escape(const char *s, int *ch)
+{
+ int n;
+ const char *s0 = s;
+
+ *ch = *s++;
+
+ switch (*ch) {
+ case 'b': *ch = '\b'; break;
+ case 'e': *ch = 27; break;
+ case 'f': *ch = '\f'; break;
+ case 'n': *ch = '\n'; break;
+ case 'r': *ch = '\r'; break;
+ case 't': *ch = '\t'; break;
+ case 'v': *ch = '\v'; break;
+ case 'u':
+ if ((n = parse_hex(s, 4, ch)) > 0) {
+ s += n;
+ }
+ break;
+ case 'x':
+ if ((n = parse_hex(s, 2, ch)) > 0) {
+ s += n;
+ }
+ break;
+ case '\0':
+ s--;
+ *ch = '\\';
+ break;
+ }
+ return s - s0;
+}
+
+/*
+ - regatom - the lowest level
+ *
+ * Optimization: gobbles an entire sequence of ordinary characters so that
+ * it can turn them into a single node, which is smaller to store and
+ * faster to run. Backslashed characters are exceptions, each becoming a
+ * separate node; the code is simpler that way and it's not worth fixing.
+ */
+static int *regatom(regex_t *preg, int *flagp)
+{
+ int *ret;
+ int flags;
+ int nocase = (preg->cflags & REG_ICASE);
+
+ int ch;
+ int n = reg_utf8_tounicode_case(preg->regparse, &ch, nocase);
+
+ *flagp = WORST; /* Tentatively. */
+
+ preg->regparse += n;
+ switch (ch) {
+ /* FIXME: these chars only have meaning at beg/end of pat? */
+ case '^':
+ ret = regnode(preg, BOL);
+ break;
+ case '$':
+ ret = regnode(preg, EOL);
+ break;
+ case '.':
+ ret = regnode(preg, ANY);
+ *flagp |= HASWIDTH|SIMPLE;
+ break;
+ case '[': {
+ const char *pattern = preg->regparse;
+
+ if (*pattern == '^') { /* Complement of range. */
+ ret = regnode(preg, ANYBUT);
+ pattern++;
+ } else
+ ret = regnode(preg, ANYOF);
+
+ /* Special case. If the first char is ']' or '-', it is part of the set */
+ if (*pattern == ']' || *pattern == '-') {
+ reg_addrange(preg, *pattern, *pattern);
+ pattern++;
+ }
+
+ while (*pattern && *pattern != ']') {
+ /* Is this a range? a-z */
+ int start;
+ int end;
+
+ pattern += reg_utf8_tounicode_case(pattern, &start, nocase);
+ if (start == '\\') {
+ pattern += reg_decode_escape(pattern, &start);
+ if (start == 0) {
+ preg->err = REG_ERR_NULL_CHAR;
+ return NULL;
+ }
+ }
+ if (pattern[0] == '-' && pattern[1]) {
+ /* skip '-' */
+ pattern += utf8_tounicode(pattern, &end);
+ pattern += reg_utf8_tounicode_case(pattern, &end, nocase);
+ if (end == '\\') {
+ pattern += reg_decode_escape(pattern, &end);
+ if (end == 0) {
+ preg->err = REG_ERR_NULL_CHAR;
+ return NULL;
+ }
+ }
+
+ reg_addrange(preg, start, end);
+ continue;
+ }
+ if (start == '[') {
+ if (strncmp(pattern, ":alpha:]", 8) == 0) {
+ if ((preg->cflags & REG_ICASE) == 0) {
+ reg_addrange(preg, 'a', 'z');
+ }
+ reg_addrange(preg, 'A', 'Z');
+ pattern += 8;
+ continue;
+ }
+ if (strncmp(pattern, ":alnum:]", 8) == 0) {
+ if ((preg->cflags & REG_ICASE) == 0) {
+ reg_addrange(preg, 'a', 'z');
+ }
+ reg_addrange(preg, 'A', 'Z');
+ reg_addrange(preg, '0', '9');
+ pattern += 8;
+ continue;
+ }
+ if (strncmp(pattern, ":space:]", 8) == 0) {
+ reg_addrange_str(preg, " \t\r\n\f\v");
+ pattern += 8;
+ continue;
+ }
+ }
+ /* Not a range, so just add the char */
+ reg_addrange(preg, start, start);
+ }
+ regc(preg, '\0');
+
+ if (*pattern) {
+ pattern++;
+ }
+ preg->regparse = pattern;
+
+ *flagp |= HASWIDTH|SIMPLE;
+ }
+ break;
+ case '(':
+ ret = reg(preg, 1, &flags);
+ if (ret == NULL)
+ return(NULL);
+ *flagp |= flags&(HASWIDTH|SPSTART);
+ break;
+ case '\0':
+ case '|':
+ case ')':
+ preg->err = REG_ERR_INTERNAL;
+ return NULL; /* Supposed to be caught earlier. */
+ case '?':
+ case '+':
+ case '*':
+ case '{':
+ preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING;
+ return NULL;
+ case '\\':
+ switch (*preg->regparse++) {
+ case '\0':
+ preg->err = REG_ERR_TRAILING_BACKSLASH;
+ return NULL;
+ break;
+ case '<':
+ case 'm':
+ ret = regnode(preg, WORDA);
+ break;
+ case '>':
+ case 'M':
+ ret = regnode(preg, WORDZ);
+ break;
+ case 'd':
+ ret = regnode(preg, ANYOF);
+ reg_addrange(preg, '0', '9');
+ regc(preg, '\0');
+ *flagp |= HASWIDTH|SIMPLE;
+ break;
+ case 'w':
+ ret = regnode(preg, ANYOF);
+ if ((preg->cflags & REG_ICASE) == 0) {
+ reg_addrange(preg, 'a', 'z');
+ }
+ reg_addrange(preg, 'A', 'Z');
+ reg_addrange(preg, '0', '9');
+ reg_addrange(preg, '_', '_');
+ regc(preg, '\0');
+ *flagp |= HASWIDTH|SIMPLE;
+ break;
+ case 's':
+ ret = regnode(preg, ANYOF);
+ reg_addrange_str(preg," \t\r\n\f\v");
+ regc(preg, '\0');
+ *flagp |= HASWIDTH|SIMPLE;
+ break;
+ /* FIXME: Someday handle \1, \2, ... */
+ default:
+ /* Handle general quoted chars in exact-match routine */
+ /* Back up to include the backslash */
+ preg->regparse--;
+ goto de_fault;
+ }
+ break;
+ de_fault:
+ default: {
+ /*
+ * Encode a string of characters to be matched exactly.
+ */
+ int added = 0;
+
+ /* Back up to pick up the first char of interest */
+ preg->regparse -= n;
+
+ ret = regnode(preg, EXACTLY);
+
+ /* Note that a META operator such as ? or * consumes the
+ * preceding char.
+ * Thus we must be careful to look ahead by 2 and add the
+ * last char as it's own EXACTLY if necessary
+ */
+
+ /* Until end of string or a META char is reached */
+ while (*preg->regparse && strchr(META, *preg->regparse) == NULL) {
+ n = reg_utf8_tounicode_case(preg->regparse, &ch, (preg->cflags & REG_ICASE));
+ if (ch == '\\' && preg->regparse[n]) {
+ /* Non-trailing backslash.
+ * Is this a special escape, or a regular escape?
+ */
+ if (strchr("<>mMwds", preg->regparse[n])) {
+ /* A special escape. All done with EXACTLY */
+ break;
+ }
+ /* Decode it. Note that we add the length for the escape
+ * sequence to the length for the backlash so we can skip
+ * the entire sequence, or not as required.
+ */
+ n += reg_decode_escape(preg->regparse + n, &ch);
+ if (ch == 0) {
+ preg->err = REG_ERR_NULL_CHAR;
+ return NULL;
+ }
+ }
+
+ /* Now we have one char 'ch' of length 'n'.
+ * Check to see if the following char is a MULT
+ */
+
+ if (ISMULT(preg->regparse[n])) {
+ /* Yes. But do we already have some EXACTLY chars? */
+ if (added) {
+ /* Yes, so return what we have and pick up the current char next time around */
+ break;
+ }
+ /* No, so add this single char and finish */
+ regc(preg, ch);
+ added++;
+ preg->regparse += n;
+ break;
+ }
+
+ /* No, so just add this char normally */
+ regc(preg, ch);
+ added++;
+ preg->regparse += n;
+ }
+ regc(preg, '\0');
+
+ *flagp |= HASWIDTH;
+ if (added == 1)
+ *flagp |= SIMPLE;
+ break;
+ }
+ break;
+ }
+
+ return(ret);
+}
+
+/*
+ - regnode - emit a node
+ */
+/* Location. */
+static int *regnode(regex_t *preg, int op)
+{
+ int *ret;
+ int *ptr;
+
+ preg->regsize += 2;
+ ret = preg->regcode;
+ if (ret == &regdummy) {
+ return(ret);
+ }
+
+ ptr = ret;
+ *ptr++ = op;
+ *ptr++ = 0; /* Null "next" pointer. */
+ preg->regcode = ptr;
+
+ return(ret);
+}
+
+/*
+ - regc - emit (if appropriate) a byte of code
+ */
+static void regc(regex_t *preg, int b )
+{
+ preg->regsize++;
+ if (preg->regcode != &regdummy)
+ *preg->regcode++ = b;
+}
+
+/*
+ - reginsert - insert an operator in front of already-emitted operand
+ *
+ * Means relocating the operand.
+ * Returns the new location of the original operand.
+ */
+static int *reginsert(regex_t *preg, int op, int *opnd )
+{
+ int *src;
+ int *dst;
+ int *place;
+
+ preg->regsize += 2;
+
+ if (preg->regcode == &regdummy) {
+ return opnd;
+ }
+
+ src = preg->regcode;
+ preg->regcode += 2;
+ dst = preg->regcode;
+ while (src > opnd)
+ *--dst = *--src;
+
+ place = opnd; /* Op node, where operand used to be. */
+ *place++ = op;
+ *place++ = 0;
+
+ return place;
+}
+
+/*
+ - regtail - set the next-pointer at the end of a node chain
+ */
+static void regtail(regex_t *preg, int *p, const int *val )
+{
+ int *scan;
+ int *temp;
+ int offset;
+
+ if (p == &regdummy)
+ return;
+
+ /* Find last node. */
+ scan = p;
+ for (;;) {
+ temp = (int *)regnext(preg, scan);
+ if (temp == NULL)
+ break;
+ scan = temp;
+ }
+
+ if (OP(scan) == BACK)
+ offset = scan - val;
+ else
+ offset = val - scan;
+
+ scan[1] = offset;
+}
+
+/*
+ - regoptail - regtail on operand of first argument; nop if operandless
+ */
+
+static void regoptail(regex_t *preg, int *p, const int *val )
+{
+ /* "Operandless" and "op != BRANCH" are synonymous in practice. */
+ if (p == NULL || p == &regdummy || OP(p) != BRANCH)
+ return;
+ regtail(preg, OPERAND(p), val);
+}
+
+/*
+ * regexec and friends
+ */
+
+/*
+ * Forwards.
+ */
+static int regtry(regex_t *preg, const char *string );
+static int regmatch(regex_t *preg, const int *prog);
+static int regrepeat(regex_t *preg, const int *p );
+
+/*
+ - regexec - match a regexp against a string
+ */
+int regexec(regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+ const char *s;
+
+ /* Be paranoid... */
+ if (preg == NULL || preg->program == NULL || string == NULL) {
+ return REG_ERR_NULL_ARGUMENT;
+ }
+
+ /* Check validity of program. */
+ if (*preg->program != REG_MAGIC) {
+ return REG_ERR_CORRUPTED;
+ }
+
+#ifdef DEBUG
+ /*regdump(preg);*/
+#endif
+
+ preg->eflags = eflags;
+ preg->pmatch = pmatch;
+ preg->nmatch = nmatch;
+ preg->start = string; /* All offsets are computed from here */
+
+ /* If there is a "must appear" string, look for it. */
+ if (preg->regmust != NULL) {
+ s = string;
+ while ((s = str_find(s, preg->regmust[0], preg->cflags & REG_ICASE)) != NULL) {
+ if (prefix_cmp(preg->regmust, preg->regmlen, s, preg->cflags & REG_ICASE) >= 0) {
+ break;
+ }
+ s++;
+ }
+ if (s == NULL) /* Not present. */
+ return REG_NOMATCH;
+ }
+
+ /* Mark beginning of line for ^ . */
+ preg->regbol = string;
+
+ /* Simplest case: anchored match need be tried only once (maybe per line). */
+ if (preg->reganch) {
+ if (eflags & REG_NOTBOL) {
+ /* This is an anchored search, but not an BOL, so possibly skip to the next line */
+ goto nextline;
+ }
+ while (1) {
+ int ret = regtry(preg, string);
+ if (ret) {
+ return REG_NOERROR;
+ }
+ if (*string) {
+nextline:
+ if (preg->cflags & REG_NEWLINE) {
+ /* Try the next anchor? */
+ string = strchr(string, '\n');
+ if (string) {
+ preg->regbol = ++string;
+ continue;
+ }
+ }
+ }
+ return REG_NOMATCH;
+ }
+ }
+
+ /* Messy cases: unanchored match. */
+ s = string;
+ if (preg->regstart != '\0') {
+ /* We know what char it must start with. */
+ while ((s = str_find(s, preg->regstart, preg->cflags & REG_ICASE)) != NULL) {
+ if (regtry(preg, s))
+ return REG_NOERROR;
+ s++;
+ }
+ }
+ else
+ /* We don't -- general case. */
+ do {
+ if (regtry(preg, s))
+ return REG_NOERROR;
+ } while (*s++ != '\0');
+
+ /* Failure. */
+ return REG_NOMATCH;
+}
+
+/*
+ - regtry - try match at specific point
+ */
+ /* 0 failure, 1 success */
+static int regtry( regex_t *preg, const char *string )
+{
+ int i;
+
+ preg->reginput = string;
+
+ for (i = 0; i < preg->nmatch; i++) {
+ preg->pmatch[i].rm_so = -1;
+ preg->pmatch[i].rm_eo = -1;
+ }
+ if (regmatch(preg, preg->program + 1)) {
+ preg->pmatch[0].rm_so = string - preg->start;
+ preg->pmatch[0].rm_eo = preg->reginput - preg->start;
+ return(1);
+ } else
+ return(0);
+}
+
+/**
+ * Returns bytes matched if 'pattern' is a prefix of 'string'.
+ *
+ * If 'nocase' is non-zero, does a case-insensitive match.
+ *
+ * Returns -1 on not found.
+ */
+static int prefix_cmp(const int *prog, int proglen, const char *string, int nocase)
+{
+ const char *s = string;
+ while (proglen && *s) {
+ int ch;
+ int n = reg_utf8_tounicode_case(s, &ch, nocase);
+ if (ch != *prog) {
+ return -1;
+ }
+ prog++;
+ s += n;
+ proglen--;
+ }
+ if (proglen == 0) {
+ return s - string;
+ }
+ return -1;
+}
+
+/**
+ * Searchs for 'c' in the range 'range'.
+ *
+ * If 'nocase' is set, the range is assumed to be uppercase
+ * and 'c' is converted to uppercase before matching.
+ *
+ * Returns 1 if found, or 0 if not.
+ */
+static int reg_range_find(const int *range, int c, int nocase)
+{
+ if (nocase) {
+ /* The "string" should already be converted to uppercase */
+ c = utf8_upper(c);
+ }
+ while (*range) {
+ if (c >= range[1] && c <= (range[0] + range[1] - 1)) {
+ return 1;
+ }
+ range += 2;
+ }
+ return 0;
+}
+
+/**
+ * Search for the character 'c' in the utf-8 string 'string'.
+ *
+ * If 'nocase' is set, the 'string' is assumed to be uppercase
+ * and 'c' is converted to uppercase before matching.
+ *
+ * Returns the byte position in the string where the 'c' was found, or
+ * NULL if not found.
+ */
+static const char *str_find(const char *string, int c, int nocase)
+{
+ if (nocase) {
+ /* The "string" should already be converted to uppercase */
+ c = utf8_upper(c);
+ }
+ while (*string) {
+ int ch;
+ int n = reg_utf8_tounicode_case(string, &ch, nocase);
+ if (c == ch) {
+ return string;
+ }
+ string += n;
+ }
+ return NULL;
+}
+
+/**
+ * Returns true if 'ch' is an end-of-line char.
+ *
+ * In REG_NEWLINE mode, \n is considered EOL in
+ * addition to \0
+ */
+static int reg_iseol(regex_t *preg, int ch)
+{
+ if (preg->cflags & REG_NEWLINE) {
+ return ch == '\0' || ch == '\n';
+ }
+ else {
+ return ch == '\0';
+ }
+}
+
+/*
+ - regmatch - main matching routine
+ *
+ * Conceptually the strategy is simple: check to see whether the current
+ * node matches, call self recursively to see whether the rest matches,
+ * and then act accordingly. In practice we make some effort to avoid
+ * recursion, in particular by going through "ordinary" nodes (that don't
+ * need to know whether the rest of the match failed) by a loop instead of
+ * by recursion.
+ */
+/* 0 failure, 1 success */
+static int regmatch(regex_t *preg, const int *prog)
+{
+ const int *scan; /* Current node. */
+ const int *next; /* Next node. */
+
+ scan = prog;
+#ifdef DEBUG
+ if (scan != NULL && regnarrate)
+ fprintf(stderr, "%s(\n", regprop(scan));
+#endif
+ while (scan != NULL) {
+#ifdef DEBUG
+ if (regnarrate) {
+ //fprintf(stderr, "%s...\n", regprop(scan));
+ int op = OP(scan);
+ fprintf(stderr, "%2d{%02x}%s...\n", (int)(scan-preg->program), op, regprop(scan)); /* Where, what. */
+ }
+#endif
+ next = regnext(preg, scan);
+
+ switch (OP(scan)) {
+ case BOL:
+ if (preg->reginput != preg->regbol)
+ return(0);
+ break;
+ case EOL:
+ if (!reg_iseol(preg, *preg->reginput)) {
+ return(0);
+ }
+ break;
+ case WORDA:
+ /* Must be looking at a letter, digit, or _ */
+ if ((!isalnum(UCHAR(*preg->reginput))) && *preg->reginput != '_')
+ return(0);
+ /* Prev must be BOL or nonword */
+ if (preg->reginput > preg->regbol &&
+ (isalnum(UCHAR(preg->reginput[-1])) || preg->reginput[-1] == '_'))
+ return(0);
+ break;
+ case WORDZ:
+ /* Must be looking at non letter, digit, or _ */
+ if (isalnum(UCHAR(*preg->reginput)) || *preg->reginput == '_')
+ return(0);
+ /* We don't care what the previous char was */
+ break;
+ case ANY:
+ if (reg_iseol(preg, *preg->reginput))
+ return 0;
+ preg->reginput++;
+ break;
+ case EXACTLY: {
+ const int *opnd;
+ int len;
+ int slen;
+
+ opnd = OPERAND(scan);
+ len = str_int_len(opnd);
+
+ slen = prefix_cmp(opnd, len, preg->reginput, preg->cflags & REG_ICASE);
+ if (slen < 0) {
+ return(0);
+ }
+ preg->reginput += slen;
+ }
+ break;
+ case ANYOF:
+ if (reg_iseol(preg, *preg->reginput))
+ return 0;
+ if (reg_range_find(OPERAND(scan), *preg->reginput, preg->cflags & REG_ICASE) == 0)
+ return(0);
+ preg->reginput++;
+ break;
+ case ANYBUT:
+ if (reg_iseol(preg, *preg->reginput))
+ return 0;
+ if (reg_range_find(OPERAND(scan), *preg->reginput, preg->cflags & REG_ICASE) != 0)
+ return(0);
+ preg->reginput++;
+ break;
+ case NOTHING:
+ break;
+ case BACK:
+ break;
+ case BRANCH: {
+ const char *save;
+
+ if (OP(next) != BRANCH) /* No choice. */
+ next = OPERAND(scan); /* Avoid recursion. */
+ else {
+ do {
+ save = preg->reginput;
+ if (regmatch(preg, OPERAND(scan))) {
+ return(1);
+ }
+ preg->reginput = save;
+ scan = regnext(preg, scan);
+ } while (scan != NULL && OP(scan) == BRANCH);
+ return(0);
+ /* NOTREACHED */
+ }
+ }
+ break;
+ case STARMIN:
+ case PLUSMIN: {
+ char nextch;
+ const char *save;
+ int min;
+ int max;
+
+ /*
+ * Lookahead to avoid useless match attempts
+ * when we know what character comes next.
+ */
+ nextch = '\0';
+ if (OP(next) == EXACTLY)
+ nextch = *OPERAND(next);
+ min = (OP(scan) == STARMIN) ? 0 : 1;
+ save = preg->reginput;
+ max = regrepeat(preg, OPERAND(scan));
+ while (min < max) {
+ int ch;
+ preg->reginput = save + min;
+ reg_utf8_tounicode_case(preg->reginput, &ch, (preg->cflags & REG_ICASE));
+ /* If it could work, try it. */
+ if (reg_iseol(preg, nextch) || ch == nextch)
+ if (regmatch(preg, next))
+ return(1);
+ /* Couldn't or didn't, add one more */
+ min++;
+ }
+ return(0);
+ }
+ break;
+
+ case STAR:
+ case PLUS: {
+ char nextch;
+ int no;
+ const char *save;
+ int min;
+
+ /*
+ * Lookahead to avoid useless match attempts
+ * when we know what character comes next.
+ */
+ nextch = '\0';
+ if (OP(next) == EXACTLY)
+ nextch = *OPERAND(next);
+ min = (OP(scan) == STAR) ? 0 : 1;
+ save = preg->reginput;
+ no = regrepeat(preg, OPERAND(scan));
+ while (no >= min) {
+ int ch;
+ reg_utf8_tounicode_case(preg->reginput, &ch, (preg->cflags & REG_ICASE));
+ /* If it could work, try it. */
+ if (reg_iseol(preg, nextch) || ch == nextch)
+ if (regmatch(preg, next))
+ return(1);
+ /* Couldn't or didn't -- back up. */
+ no--;
+ preg->reginput = save + no;
+ }
+ return(0);
+ }
+ break;
+ case END:
+ return(1); /* Success! */
+ break;
+ default:
+ if (OP(scan) >= OPEN+1 && OP(scan) < CLOSE_END) {
+ const char *save;
+
+ save = preg->reginput;
+
+ if (regmatch(preg, next)) {
+ int no;
+ /*
+ * Don't set startp if some later
+ * invocation of the same parentheses
+ * already has.
+ */
+ if (OP(scan) < CLOSE) {
+ no = OP(scan) - OPEN;
+ if (no < preg->nmatch && preg->pmatch[no].rm_so == -1) {
+ preg->pmatch[no].rm_so = save - preg->start;
+ }
+ }
+ else {
+ no = OP(scan) - CLOSE;
+ if (no < preg->nmatch && preg->pmatch[no].rm_eo == -1) {
+ preg->pmatch[no].rm_eo = save - preg->start;
+ }
+ }
+ return(1);
+ } else
+ return(0);
+ }
+ return REG_ERR_INTERNAL;
+ }
+
+ scan = next;
+ }
+
+ /*
+ * We get here only if there's trouble -- normally "case END" is
+ * the terminating point.
+ */
+ return REG_ERR_INTERNAL;
+}
+
+/*
+ - regrepeat - repeatedly match something simple, report how many
+ */
+static int regrepeat(regex_t *preg, const int *p )
+{
+ int count = 0;
+ const char *scan;
+ const int *opnd;
+
+ scan = preg->reginput;
+ opnd = OPERAND(p);
+ switch (OP(p)) {
+ case ANY:
+ while (!reg_iseol(preg, *scan)) {
+ count++;
+ scan++;
+ }
+ break;
+ case EXACTLY:
+ if (preg->cflags & REG_ICASE) {
+ while (1) {
+ int ch;
+ int n = reg_utf8_tounicode_case(scan, &ch, 1);
+ if (*opnd != ch) {
+ break;
+ }
+ count++;
+ scan += n;
+ }
+ }
+ else {
+ while (*opnd == *scan) {
+ count++;
+ scan++;
+ }
+ }
+ break;
+ case ANYOF:
+ while (!reg_iseol(preg, *scan) && reg_range_find(opnd, *scan, preg->cflags & REG_ICASE) != 0) {
+ count++;
+ scan++;
+ }
+ break;
+ case ANYBUT:
+ while (!reg_iseol(preg, *scan) && reg_range_find(opnd, *scan, preg->cflags & REG_ICASE) == 0) {
+ count++;
+ scan++;
+ }
+ break;
+ default: /* Oh dear. Called inappropriately. */
+ preg->err = REG_ERR_INTERNAL;
+ count = 0; /* Best compromise. */
+ break;
+ }
+ preg->reginput = scan;
+
+ return(count);
+}
+
+/*
+ - regnext - dig the "next" pointer out of a node
+ */
+static const int *regnext(regex_t *preg, const int *p )
+{
+ int offset;
+
+ if (p == &regdummy)
+ return(NULL);
+
+ offset = NEXT(p);
+ if (offset == 0)
+ return(NULL);
+
+ if (OP(p) == BACK)
+ return(p-offset);
+ else
+ return(p+offset);
+}
+
+#ifdef DEBUG
+
+/*
+ - regdump - dump a regexp onto stdout in vaguely comprehensible form
+ */
+static void regdump(regex_t *preg)
+{
+ const int *s;
+ char op = EXACTLY; /* Arbitrary non-END op. */
+ const int *next;
+ char buf[4];
+
+ if (preg->regcode == &regdummy)
+ return;
+
+ s = preg->program + 1;
+ while (op != END && s < preg->regcode) { /* While that wasn't END last time... */
+ op = OP(s);
+ printf("%2d{%02x}%s", (int)(s-preg->program), op, regprop(s)); /* Where, what. */
+ next = regnext(preg, s);
+ if (next == NULL) /* Next ptr. */
+ printf("(0)");
+ else
+ printf("(%d)", (int)((s-preg->program)+(next-s)));
+ s += 2;
+ if (op == ANYOF || op == ANYBUT) {
+ /* set of ranges */
+
+ while (*s) {
+ int len = *s++;
+ int first = *s++;
+ buf[utf8_fromunicode(buf, first)] = 0;
+ printf("%s", buf);
+ if (len > 1) {
+ buf[utf8_fromunicode(buf, first + len - 1)] = 0;
+ printf("-%s", buf);
+ }
+ }
+ s++;
+ }
+ else if (op == EXACTLY) {
+ /* Literal string, where present. */
+
+ while (*s) {
+ buf[utf8_fromunicode(buf, *s)] = 0;
+ printf("%s", buf);
+ s++;
+ }
+ s++;
+ }
+ putchar('\n');
+ }
+
+ if (op == END) {
+ /* Header fields of interest. */
+ if (preg->regstart != '\0')
+ buf[utf8_fromunicode(buf, preg->regstart)] = 0;
+ printf("start '%s' ", buf);
+ if (preg->reganch)
+ printf("anchored ");
+ if (preg->regmust != NULL) {
+ int i;
+ printf("must have:");
+ for (i = 0; i < preg->regmlen; i++) {
+ putchar(preg->regmust[i]);
+ }
+ putchar('\n');
+ }
+ }
+ printf("\n");
+}
+
+/*
+ - regprop - printable representation of opcode
+ */
+static const char *regprop( const int *op )
+{
+ char *p;
+ static char buf[50];
+
+ (void) strcpy(buf, ":");
+
+ switch (OP(op)) {
+ case BOL:
+ p = "BOL";
+ break;
+ case EOL:
+ p = "EOL";
+ break;
+ case ANY:
+ p = "ANY";
+ break;
+ case ANYOF:
+ p = "ANYOF";
+ break;
+ case ANYBUT:
+ p = "ANYBUT";
+ break;
+ case BRANCH:
+ p = "BRANCH";
+ break;
+ case EXACTLY:
+ p = "EXACTLY";
+ break;
+ case NOTHING:
+ p = "NOTHING";
+ break;
+ case BACK:
+ p = "BACK";
+ break;
+ case END:
+ p = "END";
+ break;
+ case STAR:
+ p = "STAR";
+ break;
+ case PLUS:
+ p = "PLUS";
+ break;
+ case STARMIN:
+ p = "STARMIN";
+ break;
+ case PLUSMIN:
+ p = "PLUSMIN";
+ break;
+ case WORDA:
+ p = "WORDA";
+ break;
+ case WORDZ:
+ p = "WORDZ";
+ break;
+ default:
+ if (OP(op) >= OPEN && OP(op) < CLOSE) {
+ sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
+ }
+ else if (OP(op) >= CLOSE && OP(op) < CLOSE_END) {
+ sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
+ }
+ else {
+ abort();
+ }
+ p = NULL;
+ break;
+ }
+ if (p != NULL)
+ (void) strcat(buf, p);
+ return(buf);
+}
+#endif
+
+size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
+{
+ static const char *error_strings[] = {
+ "success",
+ "no match",
+ "bad pattern",
+ "null argument",
+ "unknown error",
+ "too big",
+ "out of memory",
+ "too many ()",
+ "parentheses () not balanced",
+ "braces {} not balanced",
+ "invalid repetition count(s)",
+ "extra characters",
+ "*+ of empty atom",
+ "nested count",
+ "internal error",
+ "count follows nothing",
+ "trailing backslash",
+ "corrupted program",
+ "contains null char",
+ };
+ const char *err;
+
+ if (errcode < 0 || errcode >= REG_ERR_NUM) {
+ err = "Bad error code";
+ }
+ else {
+ err = error_strings[errcode];
+ }
+
+ return snprintf(errbuf, errbuf_size, "%s", err);
+}
+
+void regfree(regex_t *preg)
+{
+ free(preg->program);
+}
+
+#endif
+
+/* Jimsh - An interactive shell for Jim
+ * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
+ * Copyright 2009 Steve Bennett <steveb@workware.net.au>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * A copy of the license is also included in the source distribution
+ * of Jim, as a TXT file name called LICENSE.
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+/* Script to help initialise jimsh */
+static const char jimsh_init[] = \
+"proc _init {} {\n"
+"\trename _init {}\n"
+"\tlappend p {*}[split [env JIMLIB {}] :]\n"
+"\tlappend p {*}$::auto_path\n"
+"\tlappend p [file dirname [info nameofexecutable]]\n"
+"\tset ::auto_path $p\n"
+"\n"
+"\tif {$::tcl_interactive && [env HOME {}] ne \"\"} {\n"
+"\t\tforeach src {.jimrc jimrc.tcl} {\n"
+"\t\t\tif {[file exists [env HOME]/$src]} {\n"
+"\t\t\t\tuplevel #0 source [env HOME]/$src\n"
+"\t\t\t\tbreak\n"
+"\t\t\t}\n"
+"\t\t}\n"
+"\t}\n"
+"}\n"
+"_init\n";
+
+static void JimSetArgv(Jim_Interp *interp, int argc, char *const argv[])
+{
+ int n;
+ Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
+
+ /* Populate argv global var */
+ for (n = 0; n < argc; n++) {
+ Jim_Obj *obj = Jim_NewStringObj(interp, argv[n], -1);
+
+ Jim_ListAppendElement(interp, listObj, obj);
+ }
+
+ Jim_SetVariableStr(interp, "argv", listObj);
+ Jim_SetVariableStr(interp, "argc", Jim_NewIntObj(interp, argc));
+}
+
+int main(int argc, char *const argv[])
+{
+ int retcode;
+ Jim_Interp *interp;
+
+ if (argc > 1 && strcmp(argv[1], "--version") == 0) {
+ printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
+ return 0;
+ }
+
+ /* Create and initialize the interpreter */
+ interp = Jim_CreateInterp();
+ Jim_RegisterCoreCommands(interp);
+
+ /* Register static extensions */
+ if (Jim_InitStaticExtensions(interp) != JIM_OK) {
+ Jim_MakeErrorMessage(interp);
+ fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
+ }
+
+ Jim_SetVariableStrWithStr(interp, "jim_argv0", argv[0]);
+ Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0");
+ retcode = Jim_Eval(interp, jimsh_init);
+
+ if (argc == 1) {
+ if (retcode == JIM_ERR) {
+ Jim_MakeErrorMessage(interp);
+ fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
+ }
+ if (retcode != JIM_EXIT) {
+ JimSetArgv(interp, 0, NULL);
+ retcode = Jim_InteractivePrompt(interp);
+ }
+ }
+ else {
+ if (argc > 2 && strcmp(argv[1], "-e") == 0) {
+ JimSetArgv(interp, argc - 3, argv + 3);
+ retcode = Jim_Eval(interp, argv[2]);
+ if (retcode != JIM_ERR) {
+ printf("%s\n", Jim_String(Jim_GetResult(interp)));
+ }
+ }
+ else {
+ Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1));
+ JimSetArgv(interp, argc - 2, argv + 2);
+ retcode = Jim_EvalFile(interp, argv[1]);
+ }
+ if (retcode == JIM_ERR) {
+ Jim_MakeErrorMessage(interp);
+ fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
+ }
+ }
+ if (retcode == JIM_EXIT) {
+ retcode = Jim_GetExitCode(interp);
+ }
+ else if (retcode == JIM_ERR) {
+ retcode = 1;
+ }
+ else {
+ retcode = 0;
+ }
+ Jim_FreeInterp(interp);
+ return retcode;
+}
diff --git a/autosetup/system.tcl b/autosetup/system.tcl
new file mode 100644
index 0000000..3a2221a
--- /dev/null
+++ b/autosetup/system.tcl
@@ -0,0 +1,205 @@
+# Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
+# All rights reserved
+
+# @synopsis:
+#
+# This module supports common system interrogation and options
+# such as --host, --build, --prefix, and setting srcdir, builddir, and EXEXT.
+#
+# It also support the 'feature' naming convention, where searching
+# for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
+
+# Note that the hidden options are supported for autoconf compatibility
+
+module-options {
+ host:host-alias => {a complete or partial cpu-vendor-opsys for the system where
+ the application will run (defaults to the same value as --build)}
+ build:build-alias => {a complete or partial cpu-vendor-opsys for the system
+ where the application will be built (defaults to the
+ result of running config.guess)}
+ prefix:dir => {the target directory for the build (defaults to /usr/local)}
+
+ includedir:
+ mandir:
+ infodir:
+ libexecdir:
+ sysconfdir:
+ localstatedir:
+
+ maintainer-mode=0
+ dependency-tracking=0
+}
+
+# Returns 1 if exists, or 0 if not
+#
+proc check-feature {name code} {
+ msg-checking "Checking for $name..."
+ set r [uplevel 1 $code]
+ define-feature $name $r
+ if {$r} {
+ msg-result "ok"
+ } else {
+ msg-result "not found"
+ }
+ return $r
+}
+
+# @have-feature name ?default=0?
+#
+# Returns the value of the feature if defined, or $default if not.
+# See 'feature-define-name' for how the feature name
+# is translated into the define name.
+#
+proc have-feature {name {default 0}} {
+ get-define [feature-define-name $name] $default
+}
+
+# @define-feature name ?value=1?
+#
+# Sets the feature 'define' to the given value.
+# See 'feature-define-name' for how the feature name
+# is translated into the define name.
+#
+proc define-feature {name {value 1}} {
+ define [feature-define-name $name] $value
+}
+
+# @feature-checked name
+#
+# Returns 1 if the feature has been checked, whether true or not
+#
+proc feature-checked {name} {
+ is-defined [feature-define-name $name]
+}
+
+# @feature-define-name name ?prefix=HAVE_?
+#
+# Converts a name to the corresponding define,
+# e.g. sys/stat.h becomes HAVE_SYS_STAT_H.
+#
+# Converts * to P and all non-alphanumeric to underscore.
+#
+proc feature-define-name {name {prefix HAVE_}} {
+ string toupper $prefix[regsub -all {[^a-zA-Z0-9]} [regsub -all {[*]} $name p] _]
+}
+
+# If $file doesn't exist, or it's contents are different than $buf,
+# the file is written and $script is executed.
+# Otherwise a "file is unchanged" message is displayed.
+proc write-if-changed {file buf {script {}}} {
+ set old [readfile $file ""]
+ if {$old eq $buf && [file exists $file]} {
+ msg-result "$file is unchanged"
+ } else {
+ writefile $file $buf\n
+ uplevel 1 $script
+ }
+}
+
+# @make-template template ?outfile?
+#
+# Reads the input file <srcdir>/$template and writes the output file $outfile.
+# If $outfile is blank/omitted, $template should end with ".in" which
+# is removed to create the output file name.
+#
+# Each pattern of the form @define@ is replaced the the corresponding
+# define, if it exists, or left unchanged if not.
+#
+# The special value @srcdir@ is subsituted with the relative
+# path to the source directory from the directory where the output
+# file is created. Use @top_srcdir@ for the absolute path.
+#
+proc make-template {template {out {}}} {
+ set infile [file join $::autosetup(srcdir) $template]
+
+ if {![file exists $infile]} {
+ user-error "Template $template is missing"
+ }
+
+ # Define this as late as possible
+ define AUTODEPS $::autosetup(deps)
+
+ if {$out eq ""} {
+ if {[file ext $template] ne ".in"} {
+ autosetup-error "make_template $template has no target file and can't guess"
+ }
+ set out [file rootname $template]
+ }
+
+ set outdir [file dirname $out]
+
+ # Make sure the directory exists
+ file mkdir $outdir
+
+ # Set up srcdir to be relative to the target dir
+ define srcdir [relative-path [file join $::autosetup(srcdir) $outdir] $outdir]
+
+ set mapping {}
+ foreach {n v} [array get ::define] {
+ lappend mapping @$n@ $v
+ }
+ writefile $out [string map $mapping [readfile $infile]]\n
+
+ msg-result "Created [relative-path $out] from [relative-path $template]"
+}
+
+# build/host tuples and cross-compilation prefix
+set build [opt-val build]
+define build_alias $build
+if {$build eq ""} {
+ define build [config_guess]
+} else {
+ define build [config_sub $build]
+}
+
+set host [opt-val host]
+define host_alias $host
+if {$host eq ""} {
+ define host [get-define build]
+ set cross ""
+} else {
+ define host [config_sub $host]
+ set cross $host-
+}
+define cross [get-env CROSS $cross]
+
+set prefix [opt-val prefix /usr/local]
+
+# These are for compatibility with autoconf
+define target [get-define host]
+define prefix $prefix
+define builddir $autosetup(builddir)
+define srcdir $autosetup(srcdir)
+# Allow this to come from the environment
+define top_srcdir [get-env top_srcdir [get-define srcdir]]
+
+# And less common ones too
+define exec_prefix \${prefix}
+define bindir \${exec_prefix}/bin
+define sbindir \${exec_prefix}/sbin
+define libexecdir [get-env libexecdir \${exec_prefix}/libexec]
+define datadir \${prefix}/share
+define sysconfdir [get-env sysconfdir \${prefix}/etc]
+define sharedstatedir \${prefix}/com
+define localstatedir [get-env localstatedir \${prefix}/var]
+define libdir \${exec_prefix}/lib
+define infodir [get-env infodir \${prefix}/share/info]
+define mandir [get-env mandir \${prefix}/share/man]
+define includedir [get-env includdir \${prefix}/include]
+
+define SHELL [get-env SHELL [find-an-executable sh bash ksh]]
+
+# Windows vs. non-Windows
+switch -glob -- [get-define host] {
+ *-*-ming* - *-*-cygwin {
+ define-feature windows
+ define EXEEXT .exe
+ }
+ default {
+ define EXEEXT ""
+ }
+}
+
+# Display
+msg-result "Host System...[get-define host]"
+msg-result "Build System...[get-define build]"
diff --git a/autosetup/test-tclsh b/autosetup/test-tclsh
new file mode 100644
index 0000000..52b5f7f
--- /dev/null
+++ b/autosetup/test-tclsh
@@ -0,0 +1,21 @@
+# A small Tcl script to verify that the chosen
+# interpreter works. Sometimes we might e.g. pick up
+# an interpreter for a different arch.
+# Outputs the full path to the interpreter
+
+if {[catch {info version} version] == 0} {
+ if {$version >= 0.70} {
+ # Ensure that regexp works
+ regexp a a
+
+ # Unlike Tcl, [info nameofexecutable] can return a relative path
+ puts [file join [pwd] [info nameofexecutable]]
+ exit 0
+ }
+} elseif {[catch {info tclversion} version] == 0} {
+ if {$version >= 8.5} {
+ puts [info nameofexecutable]
+ exit 0
+ }
+}
+exit 1
diff --git a/configure b/configure
index 422750e..b08bc02 100755
--- a/configure
+++ b/configure
@@ -1,5790 +1,3 @@
-#! /bin/sh
-# Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.68 for jim 0.71.
-#
-# Report bugs to <steveb@workware.net.au>.
-#
-#
-# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
-# 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
-# Foundation, Inc.
-#
-#
-# This configure script is free software; the Free Software Foundation
-# gives unlimited permission to copy, distribute and modify it.
-## -------------------- ##
-## M4sh Initialization. ##
-## -------------------- ##
-
-# Be more Bourne compatible
-DUALCASE=1; export DUALCASE # for MKS sh
-if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then :
- emulate sh
- NULLCMD=:
- # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
- # is contrary to our usage. Disable this feature.
- alias -g '${1+"$@"}'='"$@"'
- setopt NO_GLOB_SUBST
-else
- case `(set -o) 2>/dev/null` in #(
- *posix*) :
- set -o posix ;; #(
- *) :
- ;;
-esac
-fi
-
-
-as_nl='
-'
-export as_nl
-# Printing a long string crashes Solaris 7 /usr/bin/printf.
-as_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
-as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo
-as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo
-# Prefer a ksh shell builtin over an external printf program on Solaris,
-# but without wasting forks for bash or zsh.
-if test -z "$BASH_VERSION$ZSH_VERSION" \
- && (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then
- as_echo='print -r --'
- as_echo_n='print -rn --'
-elif (test "X`printf %s $as_echo`" = "X$as_echo") 2>/dev/null; then
- as_echo='printf %s\n'
- as_echo_n='printf %s'
-else
- if test "X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`" = "X-n $as_echo"; then
- as_echo_body='eval /usr/ucb/echo -n "$1$as_nl"'
- as_echo_n='/usr/ucb/echo -n'
- else
- as_echo_body='eval expr "X$1" : "X\\(.*\\)"'
- as_echo_n_body='eval
- arg=$1;
- case $arg in #(
- *"$as_nl"*)
- expr "X$arg" : "X\\(.*\\)$as_nl";
- arg=`expr "X$arg" : ".*$as_nl\\(.*\\)"`;;
- esac;
- expr "X$arg" : "X\\(.*\\)" | tr -d "$as_nl"
- '
- export as_echo_n_body
- as_echo_n='sh -c $as_echo_n_body as_echo'
- fi
- export as_echo_body
- as_echo='sh -c $as_echo_body as_echo'
-fi
-
-# The user is always right.
-if test "${PATH_SEPARATOR+set}" != set; then
- PATH_SEPARATOR=:
- (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {
- (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||
- PATH_SEPARATOR=';'
- }
-fi
-
-
-# IFS
-# We need space, tab and new line, in precisely that order. Quoting is
-# there to prevent editors from complaining about space-tab.
-# (If _AS_PATH_WALK were called with IFS unset, it would disable word
-# splitting by setting IFS to empty value.)
-IFS=" "" $as_nl"
-
-# Find who we are. Look in the path if we contain no directory separator.
-as_myself=
-case $0 in #((
- *[\\/]* ) as_myself=$0 ;;
- *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break
- done
-IFS=$as_save_IFS
-
- ;;
-esac
-# We did not find ourselves, most probably we were run as `sh COMMAND'
-# in which case we are not to be found in the path.
-if test "x$as_myself" = x; then
- as_myself=$0
-fi
-if test ! -f "$as_myself"; then
- $as_echo "$as_myself: error: cannot find myself; rerun with an absolute file name" >&2
- exit 1
-fi
-
-# Unset variables that we do not need and which cause bugs (e.g. in
-# pre-3.0 UWIN ksh). But do not cause bugs in bash 2.01; the "|| exit 1"
-# suppresses any "Segmentation fault" message there. '((' could
-# trigger a bug in pdksh 5.2.14.
-for as_var in BASH_ENV ENV MAIL MAILPATH
-do eval test x\${$as_var+set} = xset \
- && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || :
-done
-PS1='$ '
-PS2='> '
-PS4='+ '
-
-# NLS nuisances.
-LC_ALL=C
-export LC_ALL
-LANGUAGE=C
-export LANGUAGE
-
-# CDPATH.
-(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
-
-if test "x$CONFIG_SHELL" = x; then
- as_bourne_compatible="if test -n \"\${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then :
- emulate sh
- NULLCMD=:
- # Pre-4.2 versions of Zsh do word splitting on \${1+\"\$@\"}, which
- # is contrary to our usage. Disable this feature.
- alias -g '\${1+\"\$@\"}'='\"\$@\"'
- setopt NO_GLOB_SUBST
-else
- case \`(set -o) 2>/dev/null\` in #(
- *posix*) :
- set -o posix ;; #(
- *) :
- ;;
-esac
-fi
-"
- as_required="as_fn_return () { (exit \$1); }
-as_fn_success () { as_fn_return 0; }
-as_fn_failure () { as_fn_return 1; }
-as_fn_ret_success () { return 0; }
-as_fn_ret_failure () { return 1; }
-
-exitcode=0
-as_fn_success || { exitcode=1; echo as_fn_success failed.; }
-as_fn_failure && { exitcode=1; echo as_fn_failure succeeded.; }
-as_fn_ret_success || { exitcode=1; echo as_fn_ret_success failed.; }
-as_fn_ret_failure && { exitcode=1; echo as_fn_ret_failure succeeded.; }
-if ( set x; as_fn_ret_success y && test x = \"\$1\" ); then :
-
-else
- exitcode=1; echo positional parameters were not saved.
-fi
-test x\$exitcode = x0 || exit 1"
- as_suggested=" as_lineno_1=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_1a=\$LINENO
- as_lineno_2=";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested" as_lineno_2a=\$LINENO
- eval 'test \"x\$as_lineno_1'\$as_run'\" != \"x\$as_lineno_2'\$as_run'\" &&
- test \"x\`expr \$as_lineno_1'\$as_run' + 1\`\" = \"x\$as_lineno_2'\$as_run'\"' || exit 1
-test \$(( 1 + 1 )) = 2 || exit 1"
- if (eval "$as_required") 2>/dev/null; then :
- as_have_required=yes
-else
- as_have_required=no
-fi
- if test x$as_have_required = xyes && (eval "$as_suggested") 2>/dev/null; then :
-
-else
- as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-as_found=false
-for as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- as_found=:
- case $as_dir in #(
- /*)
- for as_base in sh bash ksh sh5; do
- # Try only shells that exist, to save several forks.
- as_shell=$as_dir/$as_base
- if { test -f "$as_shell" || test -f "$as_shell.exe"; } &&
- { $as_echo "$as_bourne_compatible""$as_required" | as_run=a "$as_shell"; } 2>/dev/null; then :
- CONFIG_SHELL=$as_shell as_have_required=yes
- if { $as_echo "$as_bourne_compatible""$as_suggested" | as_run=a "$as_shell"; } 2>/dev/null; then :
- break 2
-fi
-fi
- done;;
- esac
- as_found=false
-done
-$as_found || { if { test -f "$SHELL" || test -f "$SHELL.exe"; } &&
- { $as_echo "$as_bourne_compatible""$as_required" | as_run=a "$SHELL"; } 2>/dev/null; then :
- CONFIG_SHELL=$SHELL as_have_required=yes
-fi; }
-IFS=$as_save_IFS
-
-
- if test "x$CONFIG_SHELL" != x; then :
- # We cannot yet assume a decent shell, so we have to provide a
- # neutralization value for shells without unset; and this also
- # works around shells that cannot unset nonexistent variables.
- # Preserve -v and -x to the replacement shell.
- BASH_ENV=/dev/null
- ENV=/dev/null
- (unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV
- export CONFIG_SHELL
- case $- in # ((((
- *v*x* | *x*v* ) as_opts=-vx ;;
- *v* ) as_opts=-v ;;
- *x* ) as_opts=-x ;;
- * ) as_opts= ;;
- esac
- exec "$CONFIG_SHELL" $as_opts "$as_myself" ${1+"$@"}
-fi
-
- if test x$as_have_required = xno; then :
- $as_echo "$0: This script requires a shell more modern than all"
- $as_echo "$0: the shells that I found on your system."
- if test x${ZSH_VERSION+set} = xset ; then
- $as_echo "$0: In particular, zsh $ZSH_VERSION has bugs and should"
- $as_echo "$0: be upgraded to zsh 4.3.4 or later."
- else
- $as_echo "$0: Please tell bug-autoconf@gnu.org and
-$0: steveb@workware.net.au about your system, including any
-$0: error possibly output before this message. Then install
-$0: a modern shell, or manually run the script under such a
-$0: shell if you do have one."
- fi
- exit 1
-fi
-fi
-fi
-SHELL=${CONFIG_SHELL-/bin/sh}
-export SHELL
-# Unset more variables known to interfere with behavior of common tools.
-CLICOLOR_FORCE= GREP_OPTIONS=
-unset CLICOLOR_FORCE GREP_OPTIONS
-
-## --------------------- ##
-## M4sh Shell Functions. ##
-## --------------------- ##
-# as_fn_unset VAR
-# ---------------
-# Portably unset VAR.
-as_fn_unset ()
-{
- { eval $1=; unset $1;}
-}
-as_unset=as_fn_unset
-
-# as_fn_set_status STATUS
-# -----------------------
-# Set $? to STATUS, without forking.
-as_fn_set_status ()
-{
- return $1
-} # as_fn_set_status
-
-# as_fn_exit STATUS
-# -----------------
-# Exit the shell with STATUS, even in a "trap 0" or "set -e" context.
-as_fn_exit ()
-{
- set +e
- as_fn_set_status $1
- exit $1
-} # as_fn_exit
-
-# as_fn_mkdir_p
-# -------------
-# Create "$as_dir" as a directory, including parents if necessary.
-as_fn_mkdir_p ()
-{
-
- case $as_dir in #(
- -*) as_dir=./$as_dir;;
- esac
- test -d "$as_dir" || eval $as_mkdir_p || {
- as_dirs=
- while :; do
- case $as_dir in #(
- *\'*) as_qdir=`$as_echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #'(
- *) as_qdir=$as_dir;;
- esac
- as_dirs="'$as_qdir' $as_dirs"
- as_dir=`$as_dirname -- "$as_dir" ||
-$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
- X"$as_dir" : 'X\(//\)[^/]' \| \
- X"$as_dir" : 'X\(//\)$' \| \
- X"$as_dir" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$as_dir" |
- sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
- s//\1/
- q
- }
- /^X\(\/\/\)[^/].*/{
- s//\1/
- q
- }
- /^X\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
- test -d "$as_dir" && break
- done
- test -z "$as_dirs" || eval "mkdir $as_dirs"
- } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir"
-
-
-} # as_fn_mkdir_p
-# as_fn_append VAR VALUE
-# ----------------------
-# Append the text in VALUE to the end of the definition contained in VAR. Take
-# advantage of any shell optimizations that allow amortized linear growth over
-# repeated appends, instead of the typical quadratic growth present in naive
-# implementations.
-if (eval "as_var=1; as_var+=2; test x\$as_var = x12") 2>/dev/null; then :
- eval 'as_fn_append ()
- {
- eval $1+=\$2
- }'
-else
- as_fn_append ()
- {
- eval $1=\$$1\$2
- }
-fi # as_fn_append
-
-# as_fn_arith ARG...
-# ------------------
-# Perform arithmetic evaluation on the ARGs, and store the result in the
-# global $as_val. Take advantage of shells that can avoid forks. The arguments
-# must be portable across $(()) and expr.
-if (eval "test \$(( 1 + 1 )) = 2") 2>/dev/null; then :
- eval 'as_fn_arith ()
- {
- as_val=$(( $* ))
- }'
-else
- as_fn_arith ()
- {
- as_val=`expr "$@" || test $? -eq 1`
- }
-fi # as_fn_arith
-
-
-# as_fn_error STATUS ERROR [LINENO LOG_FD]
-# ----------------------------------------
-# Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are
-# provided, also output the error to LOG_FD, referencing LINENO. Then exit the
-# script with STATUS, using 1 if that was 0.
-as_fn_error ()
-{
- as_status=$1; test $as_status -eq 0 && as_status=1
- if test "$4"; then
- as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4
- fi
- $as_echo "$as_me: error: $2" >&2
- as_fn_exit $as_status
-} # as_fn_error
-
-if expr a : '\(a\)' >/dev/null 2>&1 &&
- test "X`expr 00001 : '.*\(...\)'`" = X001; then
- as_expr=expr
-else
- as_expr=false
-fi
-
-if (basename -- /) >/dev/null 2>&1 && test "X`basename -- / 2>&1`" = "X/"; then
- as_basename=basename
-else
- as_basename=false
-fi
-
-if (as_dir=`dirname -- /` && test "X$as_dir" = X/) >/dev/null 2>&1; then
- as_dirname=dirname
-else
- as_dirname=false
-fi
-
-as_me=`$as_basename -- "$0" ||
-$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \
- X"$0" : 'X\(//\)$' \| \
- X"$0" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X/"$0" |
- sed '/^.*\/\([^/][^/]*\)\/*$/{
- s//\1/
- q
- }
- /^X\/\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\/\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
-
-# Avoid depending upon Character Ranges.
-as_cr_letters='abcdefghijklmnopqrstuvwxyz'
-as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-as_cr_Letters=$as_cr_letters$as_cr_LETTERS
-as_cr_digits='0123456789'
-as_cr_alnum=$as_cr_Letters$as_cr_digits
-
-
- as_lineno_1=$LINENO as_lineno_1a=$LINENO
- as_lineno_2=$LINENO as_lineno_2a=$LINENO
- eval 'test "x$as_lineno_1'$as_run'" != "x$as_lineno_2'$as_run'" &&
- test "x`expr $as_lineno_1'$as_run' + 1`" = "x$as_lineno_2'$as_run'"' || {
- # Blame Lee E. McMahon (1931-1989) for sed's syntax. :-)
- sed -n '
- p
- /[$]LINENO/=
- ' <$as_myself |
- sed '
- s/[$]LINENO.*/&-/
- t lineno
- b
- :lineno
- N
- :loop
- s/[$]LINENO\([^'$as_cr_alnum'_].*\n\)\(.*\)/\2\1\2/
- t loop
- s/-\n.*//
- ' >$as_me.lineno &&
- chmod +x "$as_me.lineno" ||
- { $as_echo "$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell" >&2; as_fn_exit 1; }
-
- # Don't try to exec as it changes $[0], causing all sort of problems
- # (the dirname of $[0] is not the place where we might find the
- # original and so on. Autoconf is especially sensitive to this).
- . "./$as_me.lineno"
- # Exit status is that of the last command.
- exit
-}
-
-ECHO_C= ECHO_N= ECHO_T=
-case `echo -n x` in #(((((
--n*)
- case `echo 'xy\c'` in
- *c*) ECHO_T=' ';; # ECHO_T is single tab character.
- xy) ECHO_C='\c';;
- *) echo `echo ksh88 bug on AIX 6.1` > /dev/null
- ECHO_T=' ';;
- esac;;
-*)
- ECHO_N='-n';;
-esac
-
-rm -f conf$$ conf$$.exe conf$$.file
-if test -d conf$$.dir; then
- rm -f conf$$.dir/conf$$.file
-else
- rm -f conf$$.dir
- mkdir conf$$.dir 2>/dev/null
-fi
-if (echo >conf$$.file) 2>/dev/null; then
- if ln -s conf$$.file conf$$ 2>/dev/null; then
- as_ln_s='ln -s'
- # ... but there are two gotchas:
- # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.
- # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.
- # In both cases, we have to default to `cp -p'.
- ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||
- as_ln_s='cp -p'
- elif ln conf$$.file conf$$ 2>/dev/null; then
- as_ln_s=ln
- else
- as_ln_s='cp -p'
- fi
-else
- as_ln_s='cp -p'
-fi
-rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file
-rmdir conf$$.dir 2>/dev/null
-
-if mkdir -p . 2>/dev/null; then
- as_mkdir_p='mkdir -p "$as_dir"'
-else
- test -d ./-p && rmdir ./-p
- as_mkdir_p=false
-fi
-
-if test -x / >/dev/null 2>&1; then
- as_test_x='test -x'
-else
- if ls -dL / >/dev/null 2>&1; then
- as_ls_L_option=L
- else
- as_ls_L_option=
- fi
- as_test_x='
- eval sh -c '\''
- if test -d "$1"; then
- test -d "$1/.";
- else
- case $1 in #(
- -*)set "./$1";;
- esac;
- case `ls -ld'$as_ls_L_option' "$1" 2>/dev/null` in #((
- ???[sx]*):;;*)false;;esac;fi
- '\'' sh
- '
-fi
-as_executable_p=$as_test_x
-
-# Sed expression to map a string onto a valid CPP name.
-as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
-
-# Sed expression to map a string onto a valid variable name.
-as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'"
-
-
-test -n "$DJDIR" || exec 7<&0 </dev/null
-exec 6>&1
-
-# Name of the host.
-# hostname on some systems (SVR3.2, old GNU/Linux) returns a bogus exit status,
-# so uname gets run too.
-ac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q`
-
-#
-# Initializations.
-#
-ac_default_prefix=/usr/local
-ac_clean_files=
-ac_config_libobj_dir=.
-LIBOBJS=
-cross_compiling=no
-subdirs=
-MFLAGS=
-MAKEFLAGS=
-
-# Identity of this package.
-PACKAGE_NAME='jim'
-PACKAGE_TARNAME='jim'
-PACKAGE_VERSION='0.71'
-PACKAGE_STRING='jim 0.71'
-PACKAGE_BUGREPORT='steveb@workware.net.au'
-PACKAGE_URL=''
-
-# Factoring default headers for most tests.
-ac_includes_default="\
-#include <stdio.h>
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-#ifdef HAVE_SYS_STAT_H
-# include <sys/stat.h>
-#endif
-#ifdef STDC_HEADERS
-# include <stdlib.h>
-# include <stddef.h>
-#else
-# ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-# endif
-#endif
-#ifdef HAVE_STRING_H
-# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
-# include <memory.h>
-# endif
-# include <string.h>
-#endif
-#ifdef HAVE_STRINGS_H
-# include <strings.h>
-#endif
-#ifdef HAVE_INTTYPES_H
-# include <inttypes.h>
-#endif
-#ifdef HAVE_STDINT_H
-# include <stdint.h>
-#endif
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif"
-
-ac_subst_vars='LTLIBOBJS
-LIBOBJS
-PLATFORM_PLATFORM
-PLATFORM_OS
-TCL_LIBRARY
-EXTRA_OBJS
-EXTRA_CFLAGS
-JIM_TCL_EXTENSIONS
-JIM_MOD_EXTENSIONS
-JIM_EXTENSIONS
-JIM_LIBTYPE
-JIM_UTF8
-LINKFLAGS
-SHOBJ_LDFLAGS
-SHOBJ_CFLAGS
-SH_LDFLAGS
-SH_CFLAGS
-RANLIB
-AR
-host_os
-host_vendor
-host_cpu
-host
-build_os
-build_vendor
-build_cpu
-build
-EGREP
-GREP
-CPP
-DEFINE_HAVE_LONG_LONG
-SET_MAKE
-OBJEXT
-EXEEXT
-ac_ct_CC
-CPPFLAGS
-LDFLAGS
-CFLAGS
-CC
-target_alias
-host_alias
-build_alias
-LIBS
-ECHO_T
-ECHO_N
-ECHO_C
-DEFS
-mandir
-localedir
-libdir
-psdir
-pdfdir
-dvidir
-htmldir
-infodir
-docdir
-oldincludedir
-includedir
-localstatedir
-sharedstatedir
-sysconfdir
-datadir
-datarootdir
-libexecdir
-sbindir
-bindir
-program_transform_name
-prefix
-exec_prefix
-PACKAGE_URL
-PACKAGE_BUGREPORT
-PACKAGE_STRING
-PACKAGE_VERSION
-PACKAGE_TARNAME
-PACKAGE_NAME
-PATH_SEPARATOR
-SHELL'
-ac_subst_files=''
-ac_user_opts='
-enable_option_checking
-enable_math
-enable_ipv6
-enable_utf8
-enable_lineedit
-with_jim_ext
-with_out_jim_ext
-with_jim_extmod
-with_jim_shared
-enable_shared
-enable_static
-with_jim_regexp
-'
- ac_precious_vars='build_alias
-host_alias
-target_alias
-CC
-CFLAGS
-LDFLAGS
-LIBS
-CPPFLAGS
-CPP'
-
-
-# Initialize some variables set by options.
-ac_init_help=
-ac_init_version=false
-ac_unrecognized_opts=
-ac_unrecognized_sep=
-# The variables have the same names as the options, with
-# dashes changed to underlines.
-cache_file=/dev/null
-exec_prefix=NONE
-no_create=
-no_recursion=
-prefix=NONE
-program_prefix=NONE
-program_suffix=NONE
-program_transform_name=s,x,x,
-silent=
-site=
-srcdir=
-verbose=
-x_includes=NONE
-x_libraries=NONE
-
-# Installation directory options.
-# These are left unexpanded so users can "make install exec_prefix=/foo"
-# and all the variables that are supposed to be based on exec_prefix
-# by default will actually change.
-# Use braces instead of parens because sh, perl, etc. also accept them.
-# (The list follows the same order as the GNU Coding Standards.)
-bindir='${exec_prefix}/bin'
-sbindir='${exec_prefix}/sbin'
-libexecdir='${exec_prefix}/libexec'
-datarootdir='${prefix}/share'
-datadir='${datarootdir}'
-sysconfdir='${prefix}/etc'
-sharedstatedir='${prefix}/com'
-localstatedir='${prefix}/var'
-includedir='${prefix}/include'
-oldincludedir='/usr/include'
-docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
-infodir='${datarootdir}/info'
-htmldir='${docdir}'
-dvidir='${docdir}'
-pdfdir='${docdir}'
-psdir='${docdir}'
-libdir='${exec_prefix}/lib'
-localedir='${datarootdir}/locale'
-mandir='${datarootdir}/man'
-
-ac_prev=
-ac_dashdash=
-for ac_option
-do
- # If the previous option needs an argument, assign it.
- if test -n "$ac_prev"; then
- eval $ac_prev=\$ac_option
- ac_prev=
- continue
- fi
-
- case $ac_option in
- *=?*) ac_optarg=`expr "X$ac_option" : '[^=]*=\(.*\)'` ;;
- *=) ac_optarg= ;;
- *) ac_optarg=yes ;;
- esac
-
- # Accept the important Cygnus configure options, so we can diagnose typos.
-
- case $ac_dashdash$ac_option in
- --)
- ac_dashdash=yes ;;
-
- -bindir | --bindir | --bindi | --bind | --bin | --bi)
- ac_prev=bindir ;;
- -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)
- bindir=$ac_optarg ;;
-
- -build | --build | --buil | --bui | --bu)
- ac_prev=build_alias ;;
- -build=* | --build=* | --buil=* | --bui=* | --bu=*)
- build_alias=$ac_optarg ;;
-
- -cache-file | --cache-file | --cache-fil | --cache-fi \
- | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c)
- ac_prev=cache_file ;;
- -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \
- | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*)
- cache_file=$ac_optarg ;;
-
- --config-cache | -C)
- cache_file=config.cache ;;
-
- -datadir | --datadir | --datadi | --datad)
- ac_prev=datadir ;;
- -datadir=* | --datadir=* | --datadi=* | --datad=*)
- datadir=$ac_optarg ;;
-
- -datarootdir | --datarootdir | --datarootdi | --datarootd | --dataroot \
- | --dataroo | --dataro | --datar)
- ac_prev=datarootdir ;;
- -datarootdir=* | --datarootdir=* | --datarootdi=* | --datarootd=* \
- | --dataroot=* | --dataroo=* | --dataro=* | --datar=*)
- datarootdir=$ac_optarg ;;
-
- -disable-* | --disable-*)
- ac_useropt=`expr "x$ac_option" : 'x-*disable-\(.*\)'`
- # Reject names that are not valid shell variable names.
- expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
- as_fn_error $? "invalid feature name: $ac_useropt"
- ac_useropt_orig=$ac_useropt
- ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
- case $ac_user_opts in
- *"
-"enable_$ac_useropt"
-"*) ;;
- *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--disable-$ac_useropt_orig"
- ac_unrecognized_sep=', ';;
- esac
- eval enable_$ac_useropt=no ;;
-
- -docdir | --docdir | --docdi | --doc | --do)
- ac_prev=docdir ;;
- -docdir=* | --docdir=* | --docdi=* | --doc=* | --do=*)
- docdir=$ac_optarg ;;
-
- -dvidir | --dvidir | --dvidi | --dvid | --dvi | --dv)
- ac_prev=dvidir ;;
- -dvidir=* | --dvidir=* | --dvidi=* | --dvid=* | --dvi=* | --dv=*)
- dvidir=$ac_optarg ;;
-
- -enable-* | --enable-*)
- ac_useropt=`expr "x$ac_option" : 'x-*enable-\([^=]*\)'`
- # Reject names that are not valid shell variable names.
- expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
- as_fn_error $? "invalid feature name: $ac_useropt"
- ac_useropt_orig=$ac_useropt
- ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
- case $ac_user_opts in
- *"
-"enable_$ac_useropt"
-"*) ;;
- *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--enable-$ac_useropt_orig"
- ac_unrecognized_sep=', ';;
- esac
- eval enable_$ac_useropt=\$ac_optarg ;;
-
- -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \
- | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \
- | --exec | --exe | --ex)
- ac_prev=exec_prefix ;;
- -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \
- | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \
- | --exec=* | --exe=* | --ex=*)
- exec_prefix=$ac_optarg ;;
-
- -gas | --gas | --ga | --g)
- # Obsolete; use --with-gas.
- with_gas=yes ;;
-
- -help | --help | --hel | --he | -h)
- ac_init_help=long ;;
- -help=r* | --help=r* | --hel=r* | --he=r* | -hr*)
- ac_init_help=recursive ;;
- -help=s* | --help=s* | --hel=s* | --he=s* | -hs*)
- ac_init_help=short ;;
-
- -host | --host | --hos | --ho)
- ac_prev=host_alias ;;
- -host=* | --host=* | --hos=* | --ho=*)
- host_alias=$ac_optarg ;;
-
- -htmldir | --htmldir | --htmldi | --htmld | --html | --htm | --ht)
- ac_prev=htmldir ;;
- -htmldir=* | --htmldir=* | --htmldi=* | --htmld=* | --html=* | --htm=* \
- | --ht=*)
- htmldir=$ac_optarg ;;
-
- -includedir | --includedir | --includedi | --included | --include \
- | --includ | --inclu | --incl | --inc)
- ac_prev=includedir ;;
- -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \
- | --includ=* | --inclu=* | --incl=* | --inc=*)
- includedir=$ac_optarg ;;
-
- -infodir | --infodir | --infodi | --infod | --info | --inf)
- ac_prev=infodir ;;
- -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*)
- infodir=$ac_optarg ;;
-
- -libdir | --libdir | --libdi | --libd)
- ac_prev=libdir ;;
- -libdir=* | --libdir=* | --libdi=* | --libd=*)
- libdir=$ac_optarg ;;
-
- -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \
- | --libexe | --libex | --libe)
- ac_prev=libexecdir ;;
- -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \
- | --libexe=* | --libex=* | --libe=*)
- libexecdir=$ac_optarg ;;
-
- -localedir | --localedir | --localedi | --localed | --locale)
- ac_prev=localedir ;;
- -localedir=* | --localedir=* | --localedi=* | --localed=* | --locale=*)
- localedir=$ac_optarg ;;
-
- -localstatedir | --localstatedir | --localstatedi | --localstated \
- | --localstate | --localstat | --localsta | --localst | --locals)
- ac_prev=localstatedir ;;
- -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \
- | --localstate=* | --localstat=* | --localsta=* | --localst=* | --locals=*)
- localstatedir=$ac_optarg ;;
-
- -mandir | --mandir | --mandi | --mand | --man | --ma | --m)
- ac_prev=mandir ;;
- -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*)
- mandir=$ac_optarg ;;
-
- -nfp | --nfp | --nf)
- # Obsolete; use --without-fp.
- with_fp=no ;;
-
- -no-create | --no-create | --no-creat | --no-crea | --no-cre \
- | --no-cr | --no-c | -n)
- no_create=yes ;;
-
- -no-recursion | --no-recursion | --no-recursio | --no-recursi \
- | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r)
- no_recursion=yes ;;
-
- -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \
- | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \
- | --oldin | --oldi | --old | --ol | --o)
- ac_prev=oldincludedir ;;
- -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \
- | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \
- | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*)
- oldincludedir=$ac_optarg ;;
-
- -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
- ac_prev=prefix ;;
- -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
- prefix=$ac_optarg ;;
-
- -program-prefix | --program-prefix | --program-prefi | --program-pref \
- | --program-pre | --program-pr | --program-p)
- ac_prev=program_prefix ;;
- -program-prefix=* | --program-prefix=* | --program-prefi=* \
- | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*)
- program_prefix=$ac_optarg ;;
-
- -program-suffix | --program-suffix | --program-suffi | --program-suff \
- | --program-suf | --program-su | --program-s)
- ac_prev=program_suffix ;;
- -program-suffix=* | --program-suffix=* | --program-suffi=* \
- | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*)
- program_suffix=$ac_optarg ;;
-
- -program-transform-name | --program-transform-name \
- | --program-transform-nam | --program-transform-na \
- | --program-transform-n | --program-transform- \
- | --program-transform | --program-transfor \
- | --program-transfo | --program-transf \
- | --program-trans | --program-tran \
- | --progr-tra | --program-tr | --program-t)
- ac_prev=program_transform_name ;;
- -program-transform-name=* | --program-transform-name=* \
- | --program-transform-nam=* | --program-transform-na=* \
- | --program-transform-n=* | --program-transform-=* \
- | --program-transform=* | --program-transfor=* \
- | --program-transfo=* | --program-transf=* \
- | --program-trans=* | --program-tran=* \
- | --progr-tra=* | --program-tr=* | --program-t=*)
- program_transform_name=$ac_optarg ;;
-
- -pdfdir | --pdfdir | --pdfdi | --pdfd | --pdf | --pd)
- ac_prev=pdfdir ;;
- -pdfdir=* | --pdfdir=* | --pdfdi=* | --pdfd=* | --pdf=* | --pd=*)
- pdfdir=$ac_optarg ;;
-
- -psdir | --psdir | --psdi | --psd | --ps)
- ac_prev=psdir ;;
- -psdir=* | --psdir=* | --psdi=* | --psd=* | --ps=*)
- psdir=$ac_optarg ;;
-
- -q | -quiet | --quiet | --quie | --qui | --qu | --q \
- | -silent | --silent | --silen | --sile | --sil)
- silent=yes ;;
-
- -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
- ac_prev=sbindir ;;
- -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
- | --sbi=* | --sb=*)
- sbindir=$ac_optarg ;;
-
- -sharedstatedir | --sharedstatedir | --sharedstatedi \
- | --sharedstated | --sharedstate | --sharedstat | --sharedsta \
- | --sharedst | --shareds | --shared | --share | --shar \
- | --sha | --sh)
- ac_prev=sharedstatedir ;;
- -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \
- | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \
- | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \
- | --sha=* | --sh=*)
- sharedstatedir=$ac_optarg ;;
-
- -site | --site | --sit)
- ac_prev=site ;;
- -site=* | --site=* | --sit=*)
- site=$ac_optarg ;;
-
- -srcdir | --srcdir | --srcdi | --srcd | --src | --sr)
- ac_prev=srcdir ;;
- -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*)
- srcdir=$ac_optarg ;;
-
- -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \
- | --syscon | --sysco | --sysc | --sys | --sy)
- ac_prev=sysconfdir ;;
- -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \
- | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*)
- sysconfdir=$ac_optarg ;;
-
- -target | --target | --targe | --targ | --tar | --ta | --t)
- ac_prev=target_alias ;;
- -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
- target_alias=$ac_optarg ;;
-
- -v | -verbose | --verbose | --verbos | --verbo | --verb)
- verbose=yes ;;
-
- -version | --version | --versio | --versi | --vers | -V)
- ac_init_version=: ;;
-
- -with-* | --with-*)
- ac_useropt=`expr "x$ac_option" : 'x-*with-\([^=]*\)'`
- # Reject names that are not valid shell variable names.
- expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
- as_fn_error $? "invalid package name: $ac_useropt"
- ac_useropt_orig=$ac_useropt
- ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
- case $ac_user_opts in
- *"
-"with_$ac_useropt"
-"*) ;;
- *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--with-$ac_useropt_orig"
- ac_unrecognized_sep=', ';;
- esac
- eval with_$ac_useropt=\$ac_optarg ;;
-
- -without-* | --without-*)
- ac_useropt=`expr "x$ac_option" : 'x-*without-\(.*\)'`
- # Reject names that are not valid shell variable names.
- expr "x$ac_useropt" : ".*[^-+._$as_cr_alnum]" >/dev/null &&
- as_fn_error $? "invalid package name: $ac_useropt"
- ac_useropt_orig=$ac_useropt
- ac_useropt=`$as_echo "$ac_useropt" | sed 's/[-+.]/_/g'`
- case $ac_user_opts in
- *"
-"with_$ac_useropt"
-"*) ;;
- *) ac_unrecognized_opts="$ac_unrecognized_opts$ac_unrecognized_sep--without-$ac_useropt_orig"
- ac_unrecognized_sep=', ';;
- esac
- eval with_$ac_useropt=no ;;
-
- --x)
- # Obsolete; use --with-x.
- with_x=yes ;;
-
- -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \
- | --x-incl | --x-inc | --x-in | --x-i)
- ac_prev=x_includes ;;
- -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \
- | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*)
- x_includes=$ac_optarg ;;
-
- -x-libraries | --x-libraries | --x-librarie | --x-librari \
- | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l)
- ac_prev=x_libraries ;;
- -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \
- | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*)
- x_libraries=$ac_optarg ;;
-
- -*) as_fn_error $? "unrecognized option: \`$ac_option'
-Try \`$0 --help' for more information"
- ;;
-
- *=*)
- ac_envvar=`expr "x$ac_option" : 'x\([^=]*\)='`
- # Reject names that are not valid shell variable names.
- case $ac_envvar in #(
- '' | [0-9]* | *[!_$as_cr_alnum]* )
- as_fn_error $? "invalid variable name: \`$ac_envvar'" ;;
- esac
- eval $ac_envvar=\$ac_optarg
- export $ac_envvar ;;
-
- *)
- # FIXME: should be removed in autoconf 3.0.
- $as_echo "$as_me: WARNING: you should use --build, --host, --target" >&2
- expr "x$ac_option" : ".*[^-._$as_cr_alnum]" >/dev/null &&
- $as_echo "$as_me: WARNING: invalid host type: $ac_option" >&2
- : "${build_alias=$ac_option} ${host_alias=$ac_option} ${target_alias=$ac_option}"
- ;;
-
- esac
-done
-
-if test -n "$ac_prev"; then
- ac_option=--`echo $ac_prev | sed 's/_/-/g'`
- as_fn_error $? "missing argument to $ac_option"
-fi
-
-if test -n "$ac_unrecognized_opts"; then
- case $enable_option_checking in
- no) ;;
- fatal) as_fn_error $? "unrecognized options: $ac_unrecognized_opts" ;;
- *) $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2 ;;
- esac
-fi
-
-# Check all directory arguments for consistency.
-for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \
- datadir sysconfdir sharedstatedir localstatedir includedir \
- oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
- libdir localedir mandir
-do
- eval ac_val=\$$ac_var
- # Remove trailing slashes.
- case $ac_val in
- */ )
- ac_val=`expr "X$ac_val" : 'X\(.*[^/]\)' \| "X$ac_val" : 'X\(.*\)'`
- eval $ac_var=\$ac_val;;
- esac
- # Be sure to have absolute directory names.
- case $ac_val in
- [\\/$]* | ?:[\\/]* ) continue;;
- NONE | '' ) case $ac_var in *prefix ) continue;; esac;;
- esac
- as_fn_error $? "expected an absolute directory name for --$ac_var: $ac_val"
-done
-
-# There might be people who depend on the old broken behavior: `$host'
-# used to hold the argument of --host etc.
-# FIXME: To remove some day.
-build=$build_alias
-host=$host_alias
-target=$target_alias
-
-# FIXME: To remove some day.
-if test "x$host_alias" != x; then
- if test "x$build_alias" = x; then
- cross_compiling=maybe
- $as_echo "$as_me: WARNING: if you wanted to set the --build type, don't use --host.
- If a cross compiler is detected then cross compile mode will be used" >&2
- elif test "x$build_alias" != "x$host_alias"; then
- cross_compiling=yes
- fi
-fi
-
-ac_tool_prefix=
-test -n "$host_alias" && ac_tool_prefix=$host_alias-
-
-test "$silent" = yes && exec 6>/dev/null
-
-
-ac_pwd=`pwd` && test -n "$ac_pwd" &&
-ac_ls_di=`ls -di .` &&
-ac_pwd_ls_di=`cd "$ac_pwd" && ls -di .` ||
- as_fn_error $? "working directory cannot be determined"
-test "X$ac_ls_di" = "X$ac_pwd_ls_di" ||
- as_fn_error $? "pwd does not report name of working directory"
-
-
-# Find the source files, if location was not specified.
-if test -z "$srcdir"; then
- ac_srcdir_defaulted=yes
- # Try the directory containing this script, then the parent directory.
- ac_confdir=`$as_dirname -- "$as_myself" ||
-$as_expr X"$as_myself" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
- X"$as_myself" : 'X\(//\)[^/]' \| \
- X"$as_myself" : 'X\(//\)$' \| \
- X"$as_myself" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$as_myself" |
- sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
- s//\1/
- q
- }
- /^X\(\/\/\)[^/].*/{
- s//\1/
- q
- }
- /^X\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
- srcdir=$ac_confdir
- if test ! -r "$srcdir/$ac_unique_file"; then
- srcdir=..
- fi
-else
- ac_srcdir_defaulted=no
-fi
-if test ! -r "$srcdir/$ac_unique_file"; then
- test "$ac_srcdir_defaulted" = yes && srcdir="$ac_confdir or .."
- as_fn_error $? "cannot find sources ($ac_unique_file) in $srcdir"
-fi
-ac_msg="sources are in $srcdir, but \`cd $srcdir' does not work"
-ac_abs_confdir=`(
- cd "$srcdir" && test -r "./$ac_unique_file" || as_fn_error $? "$ac_msg"
- pwd)`
-# When building in place, set srcdir=.
-if test "$ac_abs_confdir" = "$ac_pwd"; then
- srcdir=.
-fi
-# Remove unnecessary trailing slashes from srcdir.
-# Double slashes in file names in object file debugging info
-# mess up M-x gdb in Emacs.
-case $srcdir in
-*/) srcdir=`expr "X$srcdir" : 'X\(.*[^/]\)' \| "X$srcdir" : 'X\(.*\)'`;;
-esac
-for ac_var in $ac_precious_vars; do
- eval ac_env_${ac_var}_set=\${${ac_var}+set}
- eval ac_env_${ac_var}_value=\$${ac_var}
- eval ac_cv_env_${ac_var}_set=\${${ac_var}+set}
- eval ac_cv_env_${ac_var}_value=\$${ac_var}
-done
-
-#
-# Report the --help message.
-#
-if test "$ac_init_help" = "long"; then
- # Omit some internal or obsolete options to make the list less imposing.
- # This message is too long to be a string in the A/UX 3.1 sh.
- cat <<_ACEOF
-\`configure' configures jim 0.71 to adapt to many kinds of systems.
-
-Usage: $0 [OPTION]... [VAR=VALUE]...
-
-To assign environment variables (e.g., CC, CFLAGS...), specify them as
-VAR=VALUE. See below for descriptions of some of the useful variables.
-
-Defaults for the options are specified in brackets.
-
-Configuration:
- -h, --help display this help and exit
- --help=short display options specific to this package
- --help=recursive display the short help of all the included packages
- -V, --version display version information and exit
- -q, --quiet, --silent do not print \`checking ...' messages
- --cache-file=FILE cache test results in FILE [disabled]
- -C, --config-cache alias for \`--cache-file=config.cache'
- -n, --no-create do not create output files
- --srcdir=DIR find the sources in DIR [configure dir or \`..']
-
-Installation directories:
- --prefix=PREFIX install architecture-independent files in PREFIX
- [$ac_default_prefix]
- --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
- [PREFIX]
-
-By default, \`make install' will install all the files in
-\`$ac_default_prefix/bin', \`$ac_default_prefix/lib' etc. You can specify
-an installation prefix other than \`$ac_default_prefix' using \`--prefix',
-for instance \`--prefix=\$HOME'.
-
-For better control, use the options below.
-
-Fine tuning of the installation directories:
- --bindir=DIR user executables [EPREFIX/bin]
- --sbindir=DIR system admin executables [EPREFIX/sbin]
- --libexecdir=DIR program executables [EPREFIX/libexec]
- --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
- --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
- --localstatedir=DIR modifiable single-machine data [PREFIX/var]
- --libdir=DIR object code libraries [EPREFIX/lib]
- --includedir=DIR C header files [PREFIX/include]
- --oldincludedir=DIR C header files for non-gcc [/usr/include]
- --datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
- --datadir=DIR read-only architecture-independent data [DATAROOTDIR]
- --infodir=DIR info documentation [DATAROOTDIR/info]
- --localedir=DIR locale-dependent data [DATAROOTDIR/locale]
- --mandir=DIR man documentation [DATAROOTDIR/man]
- --docdir=DIR documentation root [DATAROOTDIR/doc/jim]
- --htmldir=DIR html documentation [DOCDIR]
- --dvidir=DIR dvi documentation [DOCDIR]
- --pdfdir=DIR pdf documentation [DOCDIR]
- --psdir=DIR ps documentation [DOCDIR]
-_ACEOF
-
- cat <<\_ACEOF
-
-System types:
- --build=BUILD configure for building on BUILD [guessed]
- --host=HOST cross-compile to build programs to run on HOST [BUILD]
-_ACEOF
-fi
-
-if test -n "$ac_init_help"; then
- case $ac_init_help in
- short | recursive ) echo "Configuration of jim 0.71:";;
- esac
- cat <<\_ACEOF
-
-Optional Features:
- --disable-option-checking ignore unrecognized --enable/--with options
- --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
- --enable-FEATURE[=ARG] include FEATURE [ARG=yes]
- --enable-math include support for math functions
- --enable-ipv6 include ipv6 support in the aio extension
- --enable-utf8 include support for utf8-encoded strings
- --disable-lineedit disable line editing
- --enable-shared build a shared library instead of a static library
- --enable-static build a static library instead of a shared library
-
-Optional Packages:
- --with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
- --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
- --with-jim-ext="ext1 ext2 ..."
-
- Specify additional jim extensions to include.
- These are enabled by default:
-
- aio - ANSI I/O, including open and socket
- eventloop - after, vwait, update
- array - Tcl-compatible array command
- clock - Tcl-compatible clock command
- exec - Tcl-compatible exec command
- file - Tcl-compatible file command
- glob - Tcl-compatible glob command
- readdir - Required for glob
- package - Package management with the package command
- load - Load binary extensions at runtime with load or package
- posix - Posix APIs including os.fork, os.wait, pid
- regexp - Tcl-compatible regexp, regsub commands
- signal - Signal handling
- stdlib - Built-in commands including lassign, lambda, alias
- syslog - System logging with syslog
- tclcompat - Tcl compatible read, gets, puts, parray, case, ...
-
- These are disabled by default:
-
- nvp - Name-value pairs C-only API
- oo - Jim OO extension
- tree - OO tree structure, similar to tcllib ::struct::tree
- readline - Interface to libreadline
- rlprompt - Tcl wrapper around the readline extension
- sqlite - Interface to sqlite
- sqlite3 - Interface to sqlite3
- win32 - Interface to win32
-
- --with-out-jim-ext="default|ext1 ext2 ..."
-
- Specify jim extensions to exclude.
- If 'default' is given, the default extensions will not be added.
-
- --with-jim-extmod="ext1 ext2 ..."
-
- Specify jim extensions to build as separate modules (either C or Tcl).
- Note that not all extensions can be built as loadable modules.
-
- --with-jim-shared build a shared library instead of a static library
- --with-jim-regexp use the built-in (Tcl-compatible) regexp, even if POSIX regex is available
-
-Some influential environment variables:
- CC C compiler command
- CFLAGS C compiler flags
- LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
- nonstandard directory <lib dir>
- LIBS libraries to pass to the linker, e.g. -l<library>
- CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
- you have headers in a nonstandard directory <include dir>
- CPP C preprocessor
-
-Use these variables to override the choices made by `configure' or to help
-it to find libraries and programs with nonstandard names/locations.
-
-Report bugs to <steveb@workware.net.au>.
-_ACEOF
-ac_status=$?
-fi
-
-if test "$ac_init_help" = "recursive"; then
- # If there are subdirs, report their specific --help.
- for ac_dir in : $ac_subdirs_all; do test "x$ac_dir" = x: && continue
- test -d "$ac_dir" ||
- { cd "$srcdir" && ac_pwd=`pwd` && srcdir=. && test -d "$ac_dir"; } ||
- continue
- ac_builddir=.
-
-case "$ac_dir" in
-.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;;
-*)
- ac_dir_suffix=/`$as_echo "$ac_dir" | sed 's|^\.[\\/]||'`
- # A ".." for each directory in $ac_dir_suffix.
- ac_top_builddir_sub=`$as_echo "$ac_dir_suffix" | sed 's|/[^\\/]*|/..|g;s|/||'`
- case $ac_top_builddir_sub in
- "") ac_top_builddir_sub=. ac_top_build_prefix= ;;
- *) ac_top_build_prefix=$ac_top_builddir_sub/ ;;
- esac ;;
-esac
-ac_abs_top_builddir=$ac_pwd
-ac_abs_builddir=$ac_pwd$ac_dir_suffix
-# for backward compatibility:
-ac_top_builddir=$ac_top_build_prefix
-
-case $srcdir in
- .) # We are building in place.
- ac_srcdir=.
- ac_top_srcdir=$ac_top_builddir_sub
- ac_abs_top_srcdir=$ac_pwd ;;
- [\\/]* | ?:[\\/]* ) # Absolute name.
- ac_srcdir=$srcdir$ac_dir_suffix;
- ac_top_srcdir=$srcdir
- ac_abs_top_srcdir=$srcdir ;;
- *) # Relative name.
- ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix
- ac_top_srcdir=$ac_top_build_prefix$srcdir
- ac_abs_top_srcdir=$ac_pwd/$srcdir ;;
-esac
-ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix
-
- cd "$ac_dir" || { ac_status=$?; continue; }
- # Check for guested configure.
- if test -f "$ac_srcdir/configure.gnu"; then
- echo &&
- $SHELL "$ac_srcdir/configure.gnu" --help=recursive
- elif test -f "$ac_srcdir/configure"; then
- echo &&
- $SHELL "$ac_srcdir/configure" --help=recursive
- else
- $as_echo "$as_me: WARNING: no configuration information is in $ac_dir" >&2
- fi || ac_status=$?
- cd "$ac_pwd" || { ac_status=$?; break; }
- done
-fi
-
-test -n "$ac_init_help" && exit $ac_status
-if $ac_init_version; then
- cat <<\_ACEOF
-jim configure 0.71
-generated by GNU Autoconf 2.68
-
-Copyright (C) 2010 Free Software Foundation, Inc.
-This configure script is free software; the Free Software Foundation
-gives unlimited permission to copy, distribute and modify it.
-_ACEOF
- exit
-fi
-
-## ------------------------ ##
-## Autoconf initialization. ##
-## ------------------------ ##
-
-# ac_fn_c_try_compile LINENO
-# --------------------------
-# Try to compile conftest.$ac_ext, and return whether this succeeded.
-ac_fn_c_try_compile ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- rm -f conftest.$ac_objext
- if { { ac_try="$ac_compile"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_compile") 2>conftest.err
- ac_status=$?
- if test -s conftest.err; then
- grep -v '^ *+' conftest.err >conftest.er1
- cat conftest.er1 >&5
- mv -f conftest.er1 conftest.err
- fi
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; } && {
- test -z "$ac_c_werror_flag" ||
- test ! -s conftest.err
- } && test -s conftest.$ac_objext; then :
- ac_retval=0
-else
- $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
- ac_retval=1
-fi
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
- as_fn_set_status $ac_retval
-
-} # ac_fn_c_try_compile
-
-# ac_fn_c_check_type LINENO TYPE VAR INCLUDES
-# -------------------------------------------
-# Tests whether TYPE exists after having included INCLUDES, setting cache
-# variable VAR accordingly.
-ac_fn_c_check_type ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
-$as_echo_n "checking for $2... " >&6; }
-if eval \${$3+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- eval "$3=no"
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-$4
-int
-main ()
-{
-if (sizeof ($2))
- return 0;
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-$4
-int
-main ()
-{
-if (sizeof (($2)))
- return 0;
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-
-else
- eval "$3=yes"
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-eval ac_res=\$$3
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-
-} # ac_fn_c_check_type
-
-# ac_fn_c_try_cpp LINENO
-# ----------------------
-# Try to preprocess conftest.$ac_ext, and return whether this succeeded.
-ac_fn_c_try_cpp ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- if { { ac_try="$ac_cpp conftest.$ac_ext"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_cpp conftest.$ac_ext") 2>conftest.err
- ac_status=$?
- if test -s conftest.err; then
- grep -v '^ *+' conftest.err >conftest.er1
- cat conftest.er1 >&5
- mv -f conftest.er1 conftest.err
- fi
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; } > conftest.i && {
- test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" ||
- test ! -s conftest.err
- }; then :
- ac_retval=0
-else
- $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
- ac_retval=1
-fi
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
- as_fn_set_status $ac_retval
-
-} # ac_fn_c_try_cpp
-
-# ac_fn_c_try_run LINENO
-# ----------------------
-# Try to link conftest.$ac_ext, and return whether this succeeded. Assumes
-# that executables *can* be run.
-ac_fn_c_try_run ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- if { { ac_try="$ac_link"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_link") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; } && { ac_try='./conftest$ac_exeext'
- { { case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_try") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }; }; then :
- ac_retval=0
-else
- $as_echo "$as_me: program exited with status $ac_status" >&5
- $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
- ac_retval=$ac_status
-fi
- rm -rf conftest.dSYM conftest_ipa8_conftest.oo
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
- as_fn_set_status $ac_retval
-
-} # ac_fn_c_try_run
-
-# ac_fn_c_check_header_compile LINENO HEADER VAR INCLUDES
-# -------------------------------------------------------
-# Tests whether HEADER exists and can be compiled using the include files in
-# INCLUDES, setting the cache variable VAR accordingly.
-ac_fn_c_check_header_compile ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
-$as_echo_n "checking for $2... " >&6; }
-if eval \${$3+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-$4
-#include <$2>
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- eval "$3=yes"
-else
- eval "$3=no"
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-eval ac_res=\$$3
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-
-} # ac_fn_c_check_header_compile
-
-# ac_fn_c_check_header_mongrel LINENO HEADER VAR INCLUDES
-# -------------------------------------------------------
-# Tests whether HEADER exists, giving a warning if it cannot be compiled using
-# the include files in INCLUDES and setting the cache variable VAR
-# accordingly.
-ac_fn_c_check_header_mongrel ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- if eval \${$3+:} false; then :
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
-$as_echo_n "checking for $2... " >&6; }
-if eval \${$3+:} false; then :
- $as_echo_n "(cached) " >&6
-fi
-eval ac_res=\$$3
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
-else
- # Is the header compilable?
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 usability" >&5
-$as_echo_n "checking $2 usability... " >&6; }
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-$4
-#include <$2>
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- ac_header_compiler=yes
-else
- ac_header_compiler=no
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_compiler" >&5
-$as_echo "$ac_header_compiler" >&6; }
-
-# Is the header present?
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking $2 presence" >&5
-$as_echo_n "checking $2 presence... " >&6; }
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <$2>
-_ACEOF
-if ac_fn_c_try_cpp "$LINENO"; then :
- ac_header_preproc=yes
-else
- ac_header_preproc=no
-fi
-rm -f conftest.err conftest.i conftest.$ac_ext
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_header_preproc" >&5
-$as_echo "$ac_header_preproc" >&6; }
-
-# So? What about this header?
-case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in #((
- yes:no: )
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&5
-$as_echo "$as_me: WARNING: $2: accepted by the compiler, rejected by the preprocessor!" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5
-$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;}
- ;;
- no:yes:* )
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: present but cannot be compiled" >&5
-$as_echo "$as_me: WARNING: $2: present but cannot be compiled" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: check for missing prerequisite headers?" >&5
-$as_echo "$as_me: WARNING: $2: check for missing prerequisite headers?" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: see the Autoconf documentation" >&5
-$as_echo "$as_me: WARNING: $2: see the Autoconf documentation" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&5
-$as_echo "$as_me: WARNING: $2: section \"Present But Cannot Be Compiled\"" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result" >&5
-$as_echo "$as_me: WARNING: $2: proceeding with the compiler's result" >&2;}
-( $as_echo "## ------------------------------------- ##
-## Report this to steveb@workware.net.au ##
-## ------------------------------------- ##"
- ) | sed "s/^/$as_me: WARNING: /" >&2
- ;;
-esac
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
-$as_echo_n "checking for $2... " >&6; }
-if eval \${$3+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- eval "$3=\$ac_header_compiler"
-fi
-eval ac_res=\$$3
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
-fi
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-
-} # ac_fn_c_check_header_mongrel
-
-# ac_fn_c_try_link LINENO
-# -----------------------
-# Try to link conftest.$ac_ext, and return whether this succeeded.
-ac_fn_c_try_link ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- rm -f conftest.$ac_objext conftest$ac_exeext
- if { { ac_try="$ac_link"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_link") 2>conftest.err
- ac_status=$?
- if test -s conftest.err; then
- grep -v '^ *+' conftest.err >conftest.er1
- cat conftest.er1 >&5
- mv -f conftest.er1 conftest.err
- fi
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; } && {
- test -z "$ac_c_werror_flag" ||
- test ! -s conftest.err
- } && test -s conftest$ac_exeext && {
- test "$cross_compiling" = yes ||
- $as_test_x conftest$ac_exeext
- }; then :
- ac_retval=0
-else
- $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
- ac_retval=1
-fi
- # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information
- # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would
- # interfere with the next link command; also delete a directory that is
- # left behind by Apple's compiler. We do this before executing the actions.
- rm -rf conftest.dSYM conftest_ipa8_conftest.oo
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
- as_fn_set_status $ac_retval
-
-} # ac_fn_c_try_link
-
-# ac_fn_c_check_func LINENO FUNC VAR
-# ----------------------------------
-# Tests whether FUNC exists, setting the cache variable VAR accordingly
-ac_fn_c_check_func ()
-{
- as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
-$as_echo_n "checking for $2... " >&6; }
-if eval \${$3+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-/* Define $2 to an innocuous variant, in case <limits.h> declares $2.
- For example, HP-UX 11i <limits.h> declares gettimeofday. */
-#define $2 innocuous_$2
-
-/* System header to define __stub macros and hopefully few prototypes,
- which can conflict with char $2 (); below.
- Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
- <limits.h> exists even on freestanding compilers. */
-
-#ifdef __STDC__
-# include <limits.h>
-#else
-# include <assert.h>
-#endif
-
-#undef $2
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char $2 ();
-/* The GNU C library defines this for functions which it implements
- to always fail with ENOSYS. Some functions are actually named
- something starting with __ and the normal name is an alias. */
-#if defined __stub_$2 || defined __stub___$2
-choke me
-#endif
-
-int
-main ()
-{
-return $2 ();
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
- eval "$3=yes"
-else
- eval "$3=no"
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext conftest.$ac_ext
-fi
-eval ac_res=\$$3
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
-$as_echo "$ac_res" >&6; }
- eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-
-} # ac_fn_c_check_func
-cat >config.log <<_ACEOF
-This file contains any messages produced by compilers while
-running configure, to aid debugging if configure makes a mistake.
-
-It was created by jim $as_me 0.71, which was
-generated by GNU Autoconf 2.68. Invocation command line was
-
- $ $0 $@
-
-_ACEOF
-exec 5>>config.log
-{
-cat <<_ASUNAME
-## --------- ##
-## Platform. ##
-## --------- ##
-
-hostname = `(hostname || uname -n) 2>/dev/null | sed 1q`
-uname -m = `(uname -m) 2>/dev/null || echo unknown`
-uname -r = `(uname -r) 2>/dev/null || echo unknown`
-uname -s = `(uname -s) 2>/dev/null || echo unknown`
-uname -v = `(uname -v) 2>/dev/null || echo unknown`
-
-/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null || echo unknown`
-/bin/uname -X = `(/bin/uname -X) 2>/dev/null || echo unknown`
-
-/bin/arch = `(/bin/arch) 2>/dev/null || echo unknown`
-/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null || echo unknown`
-/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null || echo unknown`
-/usr/bin/hostinfo = `(/usr/bin/hostinfo) 2>/dev/null || echo unknown`
-/bin/machine = `(/bin/machine) 2>/dev/null || echo unknown`
-/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null || echo unknown`
-/bin/universe = `(/bin/universe) 2>/dev/null || echo unknown`
-
-_ASUNAME
-
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- $as_echo "PATH: $as_dir"
- done
-IFS=$as_save_IFS
-
-} >&5
-
-cat >&5 <<_ACEOF
-
-
-## ----------- ##
-## Core tests. ##
-## ----------- ##
-
-_ACEOF
-
-
-# Keep a trace of the command line.
-# Strip out --no-create and --no-recursion so they do not pile up.
-# Strip out --silent because we don't want to record it for future runs.
-# Also quote any args containing shell meta-characters.
-# Make two passes to allow for proper duplicate-argument suppression.
-ac_configure_args=
-ac_configure_args0=
-ac_configure_args1=
-ac_must_keep_next=false
-for ac_pass in 1 2
-do
- for ac_arg
- do
- case $ac_arg in
- -no-create | --no-c* | -n | -no-recursion | --no-r*) continue ;;
- -q | -quiet | --quiet | --quie | --qui | --qu | --q \
- | -silent | --silent | --silen | --sile | --sil)
- continue ;;
- *\'*)
- ac_arg=`$as_echo "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;;
- esac
- case $ac_pass in
- 1) as_fn_append ac_configure_args0 " '$ac_arg'" ;;
- 2)
- as_fn_append ac_configure_args1 " '$ac_arg'"
- if test $ac_must_keep_next = true; then
- ac_must_keep_next=false # Got value, back to normal.
- else
- case $ac_arg in
- *=* | --config-cache | -C | -disable-* | --disable-* \
- | -enable-* | --enable-* | -gas | --g* | -nfp | --nf* \
- | -q | -quiet | --q* | -silent | --sil* | -v | -verb* \
- | -with-* | --with-* | -without-* | --without-* | --x)
- case "$ac_configure_args0 " in
- "$ac_configure_args1"*" '$ac_arg' "* ) continue ;;
- esac
- ;;
- -* ) ac_must_keep_next=true ;;
- esac
- fi
- as_fn_append ac_configure_args " '$ac_arg'"
- ;;
- esac
- done
-done
-{ ac_configure_args0=; unset ac_configure_args0;}
-{ ac_configure_args1=; unset ac_configure_args1;}
-
-# When interrupted or exit'd, cleanup temporary files, and complete
-# config.log. We remove comments because anyway the quotes in there
-# would cause problems or look ugly.
-# WARNING: Use '\'' to represent an apostrophe within the trap.
-# WARNING: Do not start the trap code with a newline, due to a FreeBSD 4.0 bug.
-trap 'exit_status=$?
- # Save into config.log some information that might help in debugging.
- {
- echo
-
- $as_echo "## ---------------- ##
-## Cache variables. ##
-## ---------------- ##"
- echo
- # The following way of writing the cache mishandles newlines in values,
-(
- for ac_var in `(set) 2>&1 | sed -n '\''s/^\([a-zA-Z_][a-zA-Z0-9_]*\)=.*/\1/p'\''`; do
- eval ac_val=\$$ac_var
- case $ac_val in #(
- *${as_nl}*)
- case $ac_var in #(
- *_cv_*) { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline" >&5
-$as_echo "$as_me: WARNING: cache variable $ac_var contains a newline" >&2;} ;;
- esac
- case $ac_var in #(
- _ | IFS | as_nl) ;; #(
- BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #(
- *) { eval $ac_var=; unset $ac_var;} ;;
- esac ;;
- esac
- done
- (set) 2>&1 |
- case $as_nl`(ac_space='\'' '\''; set) 2>&1` in #(
- *${as_nl}ac_space=\ *)
- sed -n \
- "s/'\''/'\''\\\\'\'''\''/g;
- s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\''\\2'\''/p"
- ;; #(
- *)
- sed -n "/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p"
- ;;
- esac |
- sort
-)
- echo
-
- $as_echo "## ----------------- ##
-## Output variables. ##
-## ----------------- ##"
- echo
- for ac_var in $ac_subst_vars
- do
- eval ac_val=\$$ac_var
- case $ac_val in
- *\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;;
- esac
- $as_echo "$ac_var='\''$ac_val'\''"
- done | sort
- echo
-
- if test -n "$ac_subst_files"; then
- $as_echo "## ------------------- ##
-## File substitutions. ##
-## ------------------- ##"
- echo
- for ac_var in $ac_subst_files
- do
- eval ac_val=\$$ac_var
- case $ac_val in
- *\'\''*) ac_val=`$as_echo "$ac_val" | sed "s/'\''/'\''\\\\\\\\'\'''\''/g"`;;
- esac
- $as_echo "$ac_var='\''$ac_val'\''"
- done | sort
- echo
- fi
-
- if test -s confdefs.h; then
- $as_echo "## ----------- ##
-## confdefs.h. ##
-## ----------- ##"
- echo
- cat confdefs.h
- echo
- fi
- test "$ac_signal" != 0 &&
- $as_echo "$as_me: caught signal $ac_signal"
- $as_echo "$as_me: exit $exit_status"
- } >&5
- rm -f core *.core core.conftest.* &&
- rm -f -r conftest* confdefs* conf$$* $ac_clean_files &&
- exit $exit_status
-' 0
-for ac_signal in 1 2 13 15; do
- trap 'ac_signal='$ac_signal'; as_fn_exit 1' $ac_signal
-done
-ac_signal=0
-
-# confdefs.h avoids OS command line length limits that DEFS can exceed.
-rm -f -r conftest* confdefs.h
-
-$as_echo "/* confdefs.h */" > confdefs.h
-
-# Predefined preprocessor variables.
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_NAME "$PACKAGE_NAME"
-_ACEOF
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_TARNAME "$PACKAGE_TARNAME"
-_ACEOF
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_VERSION "$PACKAGE_VERSION"
-_ACEOF
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_STRING "$PACKAGE_STRING"
-_ACEOF
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_BUGREPORT "$PACKAGE_BUGREPORT"
-_ACEOF
-
-cat >>confdefs.h <<_ACEOF
-#define PACKAGE_URL "$PACKAGE_URL"
-_ACEOF
-
-
-# Let the site file select an alternate cache file if it wants to.
-# Prefer an explicitly selected file to automatically selected ones.
-ac_site_file1=NONE
-ac_site_file2=NONE
-if test -n "$CONFIG_SITE"; then
- # We do not want a PATH search for config.site.
- case $CONFIG_SITE in #((
- -*) ac_site_file1=./$CONFIG_SITE;;
- */*) ac_site_file1=$CONFIG_SITE;;
- *) ac_site_file1=./$CONFIG_SITE;;
- esac
-elif test "x$prefix" != xNONE; then
- ac_site_file1=$prefix/share/config.site
- ac_site_file2=$prefix/etc/config.site
-else
- ac_site_file1=$ac_default_prefix/share/config.site
- ac_site_file2=$ac_default_prefix/etc/config.site
-fi
-for ac_site_file in "$ac_site_file1" "$ac_site_file2"
-do
- test "x$ac_site_file" = xNONE && continue
- if test /dev/null != "$ac_site_file" && test -r "$ac_site_file"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: loading site script $ac_site_file" >&5
-$as_echo "$as_me: loading site script $ac_site_file" >&6;}
- sed 's/^/| /' "$ac_site_file" >&5
- . "$ac_site_file" \
- || { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "failed to load site script $ac_site_file
-See \`config.log' for more details" "$LINENO" 5; }
- fi
-done
-
-if test -r "$cache_file"; then
- # Some versions of bash will fail to source /dev/null (special files
- # actually), so we avoid doing that. DJGPP emulates it as a regular file.
- if test /dev/null != "$cache_file" && test -f "$cache_file"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: loading cache $cache_file" >&5
-$as_echo "$as_me: loading cache $cache_file" >&6;}
- case $cache_file in
- [\\/]* | ?:[\\/]* ) . "$cache_file";;
- *) . "./$cache_file";;
- esac
- fi
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: creating cache $cache_file" >&5
-$as_echo "$as_me: creating cache $cache_file" >&6;}
- >$cache_file
-fi
-
-# Check that the precious variables saved in the cache have kept the same
-# value.
-ac_cache_corrupted=false
-for ac_var in $ac_precious_vars; do
- eval ac_old_set=\$ac_cv_env_${ac_var}_set
- eval ac_new_set=\$ac_env_${ac_var}_set
- eval ac_old_val=\$ac_cv_env_${ac_var}_value
- eval ac_new_val=\$ac_env_${ac_var}_value
- case $ac_old_set,$ac_new_set in
- set,)
- { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&5
-$as_echo "$as_me: error: \`$ac_var' was set to \`$ac_old_val' in the previous run" >&2;}
- ac_cache_corrupted=: ;;
- ,set)
- { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' was not set in the previous run" >&5
-$as_echo "$as_me: error: \`$ac_var' was not set in the previous run" >&2;}
- ac_cache_corrupted=: ;;
- ,);;
- *)
- if test "x$ac_old_val" != "x$ac_new_val"; then
- # differences in whitespace do not lead to failure.
- ac_old_val_w=`echo x $ac_old_val`
- ac_new_val_w=`echo x $ac_new_val`
- if test "$ac_old_val_w" != "$ac_new_val_w"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: error: \`$ac_var' has changed since the previous run:" >&5
-$as_echo "$as_me: error: \`$ac_var' has changed since the previous run:" >&2;}
- ac_cache_corrupted=:
- else
- { $as_echo "$as_me:${as_lineno-$LINENO}: warning: ignoring whitespace changes in \`$ac_var' since the previous run:" >&5
-$as_echo "$as_me: warning: ignoring whitespace changes in \`$ac_var' since the previous run:" >&2;}
- eval $ac_var=\$ac_old_val
- fi
- { $as_echo "$as_me:${as_lineno-$LINENO}: former value: \`$ac_old_val'" >&5
-$as_echo "$as_me: former value: \`$ac_old_val'" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: current value: \`$ac_new_val'" >&5
-$as_echo "$as_me: current value: \`$ac_new_val'" >&2;}
- fi;;
- esac
- # Pass precious variables to config.status.
- if test "$ac_new_set" = set; then
- case $ac_new_val in
- *\'*) ac_arg=$ac_var=`$as_echo "$ac_new_val" | sed "s/'/'\\\\\\\\''/g"` ;;
- *) ac_arg=$ac_var=$ac_new_val ;;
- esac
- case " $ac_configure_args " in
- *" '$ac_arg' "*) ;; # Avoid dups. Use of quotes ensures accuracy.
- *) as_fn_append ac_configure_args " '$ac_arg'" ;;
- esac
- fi
-done
-if $ac_cache_corrupted; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
- { $as_echo "$as_me:${as_lineno-$LINENO}: error: changes in the environment can compromise the build" >&5
-$as_echo "$as_me: error: changes in the environment can compromise the build" >&2;}
- as_fn_error $? "run \`make distclean' and/or \`rm $cache_file' and start over" "$LINENO" 5
-fi
-## -------------------- ##
-## Main body of script. ##
-## -------------------- ##
-
-ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
-ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
-ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
-ac_compiler_gnu=$ac_cv_c_compiler_gnu
-
-
-ac_config_headers="$ac_config_headers jimautoconf.h"
-
-
-# Checks for programs.
-ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
-ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
-ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
-ac_compiler_gnu=$ac_cv_c_compiler_gnu
-if test -n "$ac_tool_prefix"; then
- # Extract the first word of "${ac_tool_prefix}gcc", so it can be a program name with args.
-set dummy ${ac_tool_prefix}gcc; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$CC"; then
- ac_cv_prog_CC="$CC" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_CC="${ac_tool_prefix}gcc"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-CC=$ac_cv_prog_CC
-if test -n "$CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5
-$as_echo "$CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
-fi
-if test -z "$ac_cv_prog_CC"; then
- ac_ct_CC=$CC
- # Extract the first word of "gcc", so it can be a program name with args.
-set dummy gcc; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_ac_ct_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$ac_ct_CC"; then
- ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_ac_ct_CC="gcc"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-ac_ct_CC=$ac_cv_prog_ac_ct_CC
-if test -n "$ac_ct_CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC" >&5
-$as_echo "$ac_ct_CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
- if test "x$ac_ct_CC" = x; then
- CC=""
- else
- case $cross_compiling:$ac_tool_warned in
-yes:)
-{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5
-$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;}
-ac_tool_warned=yes ;;
-esac
- CC=$ac_ct_CC
- fi
-else
- CC="$ac_cv_prog_CC"
-fi
-
-if test -z "$CC"; then
- if test -n "$ac_tool_prefix"; then
- # Extract the first word of "${ac_tool_prefix}cc", so it can be a program name with args.
-set dummy ${ac_tool_prefix}cc; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$CC"; then
- ac_cv_prog_CC="$CC" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_CC="${ac_tool_prefix}cc"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-CC=$ac_cv_prog_CC
-if test -n "$CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5
-$as_echo "$CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
- fi
-fi
-if test -z "$CC"; then
- # Extract the first word of "cc", so it can be a program name with args.
-set dummy cc; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$CC"; then
- ac_cv_prog_CC="$CC" # Let the user override the test.
-else
- ac_prog_rejected=no
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- if test "$as_dir/$ac_word$ac_exec_ext" = "/usr/ucb/cc"; then
- ac_prog_rejected=yes
- continue
- fi
- ac_cv_prog_CC="cc"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-if test $ac_prog_rejected = yes; then
- # We found a bogon in the path, so make sure we never use it.
- set dummy $ac_cv_prog_CC
- shift
- if test $# != 0; then
- # We chose a different compiler from the bogus one.
- # However, it has the same basename, so the bogon will be chosen
- # first if we set CC to just the basename; use the full file name.
- shift
- ac_cv_prog_CC="$as_dir/$ac_word${1+' '}$@"
- fi
-fi
-fi
-fi
-CC=$ac_cv_prog_CC
-if test -n "$CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5
-$as_echo "$CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
-fi
-if test -z "$CC"; then
- if test -n "$ac_tool_prefix"; then
- for ac_prog in cl.exe
- do
- # Extract the first word of "$ac_tool_prefix$ac_prog", so it can be a program name with args.
-set dummy $ac_tool_prefix$ac_prog; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$CC"; then
- ac_cv_prog_CC="$CC" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_CC="$ac_tool_prefix$ac_prog"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-CC=$ac_cv_prog_CC
-if test -n "$CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $CC" >&5
-$as_echo "$CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
- test -n "$CC" && break
- done
-fi
-if test -z "$CC"; then
- ac_ct_CC=$CC
- for ac_prog in cl.exe
-do
- # Extract the first word of "$ac_prog", so it can be a program name with args.
-set dummy $ac_prog; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_ac_ct_CC+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$ac_ct_CC"; then
- ac_cv_prog_ac_ct_CC="$ac_ct_CC" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_ac_ct_CC="$ac_prog"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-ac_ct_CC=$ac_cv_prog_ac_ct_CC
-if test -n "$ac_ct_CC"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC" >&5
-$as_echo "$ac_ct_CC" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
- test -n "$ac_ct_CC" && break
-done
-
- if test "x$ac_ct_CC" = x; then
- CC=""
- else
- case $cross_compiling:$ac_tool_warned in
-yes:)
-{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5
-$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;}
-ac_tool_warned=yes ;;
-esac
- CC=$ac_ct_CC
- fi
-fi
-
-fi
-
-
-test -z "$CC" && { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "no acceptable C compiler found in \$PATH
-See \`config.log' for more details" "$LINENO" 5; }
-
-# Provide some information about the compiler.
-$as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler version" >&5
-set X $ac_compile
-ac_compiler=$2
-for ac_option in --version -v -V -qversion; do
- { { ac_try="$ac_compiler $ac_option >&5"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_compiler $ac_option >&5") 2>conftest.err
- ac_status=$?
- if test -s conftest.err; then
- sed '10a\
-... rest of stderr output deleted ...
- 10q' conftest.err >conftest.er1
- cat conftest.er1 >&5
- fi
- rm -f conftest.er1 conftest.err
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }
-done
-
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-ac_clean_files_save=$ac_clean_files
-ac_clean_files="$ac_clean_files a.out a.out.dSYM a.exe b.out"
-# Try to create an executable without -o first, disregard a.out.
-# It will help us diagnose broken compilers, and finding out an intuition
-# of exeext.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the C compiler works" >&5
-$as_echo_n "checking whether the C compiler works... " >&6; }
-ac_link_default=`$as_echo "$ac_link" | sed 's/ -o *conftest[^ ]*//'`
-
-# The possible output files:
-ac_files="a.out conftest.exe conftest a.exe a_out.exe b.out conftest.*"
-
-ac_rmfiles=
-for ac_file in $ac_files
-do
- case $ac_file in
- *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;;
- * ) ac_rmfiles="$ac_rmfiles $ac_file";;
- esac
-done
-rm -f $ac_rmfiles
-
-if { { ac_try="$ac_link_default"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_link_default") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }; then :
- # Autoconf-2.13 could set the ac_cv_exeext variable to `no'.
-# So ignore a value of `no', otherwise this would lead to `EXEEXT = no'
-# in a Makefile. We should not override ac_cv_exeext if it was cached,
-# so that the user can short-circuit this test for compilers unknown to
-# Autoconf.
-for ac_file in $ac_files ''
-do
- test -f "$ac_file" || continue
- case $ac_file in
- *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj )
- ;;
- [ab].out )
- # We found the default executable, but exeext='' is most
- # certainly right.
- break;;
- *.* )
- if test "${ac_cv_exeext+set}" = set && test "$ac_cv_exeext" != no;
- then :; else
- ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'`
- fi
- # We set ac_cv_exeext here because the later test for it is not
- # safe: cross compilers may not add the suffix if given an `-o'
- # argument, so we may need to know it at that point already.
- # Even if this section looks crufty: it has the advantage of
- # actually working.
- break;;
- * )
- break;;
- esac
-done
-test "$ac_cv_exeext" = no && ac_cv_exeext=
-
-else
- ac_file=''
-fi
-if test -z "$ac_file"; then :
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-$as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error 77 "C compiler cannot create executables
-See \`config.log' for more details" "$LINENO" 5; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
-$as_echo "yes" >&6; }
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C compiler default output file name" >&5
-$as_echo_n "checking for C compiler default output file name... " >&6; }
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_file" >&5
-$as_echo "$ac_file" >&6; }
-ac_exeext=$ac_cv_exeext
-
-rm -f -r a.out a.out.dSYM a.exe conftest$ac_cv_exeext b.out
-ac_clean_files=$ac_clean_files_save
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for suffix of executables" >&5
-$as_echo_n "checking for suffix of executables... " >&6; }
-if { { ac_try="$ac_link"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_link") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }; then :
- # If both `conftest.exe' and `conftest' are `present' (well, observable)
-# catch `conftest.exe'. For instance with Cygwin, `ls conftest' will
-# work properly (i.e., refer to `conftest.exe'), while it won't with
-# `rm'.
-for ac_file in conftest.exe conftest conftest.*; do
- test -f "$ac_file" || continue
- case $ac_file in
- *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;;
- *.* ) ac_cv_exeext=`expr "$ac_file" : '[^.]*\(\..*\)'`
- break;;
- * ) break;;
- esac
-done
-else
- { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "cannot compute suffix of executables: cannot compile and link
-See \`config.log' for more details" "$LINENO" 5; }
-fi
-rm -f conftest conftest$ac_cv_exeext
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_exeext" >&5
-$as_echo "$ac_cv_exeext" >&6; }
-
-rm -f conftest.$ac_ext
-EXEEXT=$ac_cv_exeext
-ac_exeext=$EXEEXT
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <stdio.h>
-int
-main ()
-{
-FILE *f = fopen ("conftest.out", "w");
- return ferror (f) || fclose (f) != 0;
-
- ;
- return 0;
-}
-_ACEOF
-ac_clean_files="$ac_clean_files conftest.out"
-# Check that the compiler produces executables we can run. If not, either
-# the compiler is broken, or we cross compile.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether we are cross compiling" >&5
-$as_echo_n "checking whether we are cross compiling... " >&6; }
-if test "$cross_compiling" != yes; then
- { { ac_try="$ac_link"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_link") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }
- if { ac_try='./conftest$ac_cv_exeext'
- { { case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_try") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }; }; then
- cross_compiling=no
- else
- if test "$cross_compiling" = maybe; then
- cross_compiling=yes
- else
- { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "cannot run C compiled programs.
-If you meant to cross compile, use \`--host'.
-See \`config.log' for more details" "$LINENO" 5; }
- fi
- fi
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $cross_compiling" >&5
-$as_echo "$cross_compiling" >&6; }
-
-rm -f conftest.$ac_ext conftest$ac_cv_exeext conftest.out
-ac_clean_files=$ac_clean_files_save
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for suffix of object files" >&5
-$as_echo_n "checking for suffix of object files... " >&6; }
-if ${ac_cv_objext+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-rm -f conftest.o conftest.obj
-if { { ac_try="$ac_compile"
-case "(($ac_try" in
- *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
- *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
- (eval "$ac_compile") 2>&5
- ac_status=$?
- $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
- test $ac_status = 0; }; then :
- for ac_file in conftest.o conftest.obj conftest.*; do
- test -f "$ac_file" || continue;
- case $ac_file in
- *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM ) ;;
- *) ac_cv_objext=`expr "$ac_file" : '.*\.\(.*\)'`
- break;;
- esac
-done
-else
- $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-{ { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "cannot compute suffix of object files: cannot compile
-See \`config.log' for more details" "$LINENO" 5; }
-fi
-rm -f conftest.$ac_cv_objext conftest.$ac_ext
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_objext" >&5
-$as_echo "$ac_cv_objext" >&6; }
-OBJEXT=$ac_cv_objext
-ac_objext=$OBJEXT
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether we are using the GNU C compiler" >&5
-$as_echo_n "checking whether we are using the GNU C compiler... " >&6; }
-if ${ac_cv_c_compiler_gnu+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-#ifndef __GNUC__
- choke me
-#endif
-
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- ac_compiler_gnu=yes
-else
- ac_compiler_gnu=no
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-ac_cv_c_compiler_gnu=$ac_compiler_gnu
-
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_compiler_gnu" >&5
-$as_echo "$ac_cv_c_compiler_gnu" >&6; }
-if test $ac_compiler_gnu = yes; then
- GCC=yes
-else
- GCC=
-fi
-ac_test_CFLAGS=${CFLAGS+set}
-ac_save_CFLAGS=$CFLAGS
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC accepts -g" >&5
-$as_echo_n "checking whether $CC accepts -g... " >&6; }
-if ${ac_cv_prog_cc_g+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_save_c_werror_flag=$ac_c_werror_flag
- ac_c_werror_flag=yes
- ac_cv_prog_cc_g=no
- CFLAGS="-g"
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- ac_cv_prog_cc_g=yes
-else
- CFLAGS=""
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-
-else
- ac_c_werror_flag=$ac_save_c_werror_flag
- CFLAGS="-g"
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- ac_cv_prog_cc_g=yes
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
- ac_c_werror_flag=$ac_save_c_werror_flag
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_g" >&5
-$as_echo "$ac_cv_prog_cc_g" >&6; }
-if test "$ac_test_CFLAGS" = set; then
- CFLAGS=$ac_save_CFLAGS
-elif test $ac_cv_prog_cc_g = yes; then
- if test "$GCC" = yes; then
- CFLAGS="-g -O2"
- else
- CFLAGS="-g"
- fi
-else
- if test "$GCC" = yes; then
- CFLAGS="-O2"
- else
- CFLAGS=
- fi
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $CC option to accept ISO C89" >&5
-$as_echo_n "checking for $CC option to accept ISO C89... " >&6; }
-if ${ac_cv_prog_cc_c89+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_cv_prog_cc_c89=no
-ac_save_CC=$CC
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <stdarg.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-/* Most of the following tests are stolen from RCS 5.7's src/conf.sh. */
-struct buf { int x; };
-FILE * (*rcsopen) (struct buf *, struct stat *, int);
-static char *e (p, i)
- char **p;
- int i;
-{
- return p[i];
-}
-static char *f (char * (*g) (char **, int), char **p, ...)
-{
- char *s;
- va_list v;
- va_start (v,p);
- s = g (p, va_arg (v,int));
- va_end (v);
- return s;
-}
-
-/* OSF 4.0 Compaq cc is some sort of almost-ANSI by default. It has
- function prototypes and stuff, but not '\xHH' hex character constants.
- These don't provoke an error unfortunately, instead are silently treated
- as 'x'. The following induces an error, until -std is added to get
- proper ANSI mode. Curiously '\x00'!='x' always comes out true, for an
- array size at least. It's necessary to write '\x00'==0 to get something
- that's true only with -std. */
-int osf4_cc_array ['\x00' == 0 ? 1 : -1];
-
-/* IBM C 6 for AIX is almost-ANSI by default, but it replaces macro parameters
- inside strings and character constants. */
-#define FOO(x) 'x'
-int xlc6_cc_array[FOO(a) == 'x' ? 1 : -1];
-
-int test (int i, double x);
-struct s1 {int (*f) (int a);};
-struct s2 {int (*f) (double a);};
-int pairnames (int, char **, FILE *(*)(struct buf *, struct stat *, int), int, int);
-int argc;
-char **argv;
-int
-main ()
-{
-return f (e, argv, 0) != argv[0] || f (e, argv, 1) != argv[1];
- ;
- return 0;
-}
-_ACEOF
-for ac_arg in '' -qlanglvl=extc89 -qlanglvl=ansi -std \
- -Ae "-Aa -D_HPUX_SOURCE" "-Xc -D__EXTENSIONS__"
-do
- CC="$ac_save_CC $ac_arg"
- if ac_fn_c_try_compile "$LINENO"; then :
- ac_cv_prog_cc_c89=$ac_arg
-fi
-rm -f core conftest.err conftest.$ac_objext
- test "x$ac_cv_prog_cc_c89" != "xno" && break
-done
-rm -f conftest.$ac_ext
-CC=$ac_save_CC
-
-fi
-# AC_CACHE_VAL
-case "x$ac_cv_prog_cc_c89" in
- x)
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: none needed" >&5
-$as_echo "none needed" >&6; } ;;
- xno)
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: unsupported" >&5
-$as_echo "unsupported" >&6; } ;;
- *)
- CC="$CC $ac_cv_prog_cc_c89"
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_c89" >&5
-$as_echo "$ac_cv_prog_cc_c89" >&6; } ;;
-esac
-if test "x$ac_cv_prog_cc_c89" != xno; then :
-
-fi
-
-ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
-ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
-ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
-ac_compiler_gnu=$ac_cv_c_compiler_gnu
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${MAKE-make} sets \$(MAKE)" >&5
-$as_echo_n "checking whether ${MAKE-make} sets \$(MAKE)... " >&6; }
-set x ${MAKE-make}
-ac_make=`$as_echo "$2" | sed 's/+/p/g; s/[^a-zA-Z0-9_]/_/g'`
-if eval \${ac_cv_prog_make_${ac_make}_set+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat >conftest.make <<\_ACEOF
-SHELL = /bin/sh
-all:
- @echo '@@@%%%=$(MAKE)=@@@%%%'
-_ACEOF
-# GNU make sometimes prints "make[1]: Entering ...", which would confuse us.
-case `${MAKE-make} -f conftest.make 2>/dev/null` in
- *@@@%%%=?*=@@@%%%*)
- eval ac_cv_prog_make_${ac_make}_set=yes;;
- *)
- eval ac_cv_prog_make_${ac_make}_set=no;;
-esac
-rm -f conftest.make
-fi
-if eval test \$ac_cv_prog_make_${ac_make}_set = yes; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
-$as_echo "yes" >&6; }
- SET_MAKE=
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
- SET_MAKE="MAKE=${MAKE-make}"
-fi
-
-
-# Checks for types
-
-ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
-ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
-ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
-ac_compiler_gnu=$ac_cv_c_compiler_gnu
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to run the C preprocessor" >&5
-$as_echo_n "checking how to run the C preprocessor... " >&6; }
-# On Suns, sometimes $CPP names a directory.
-if test -n "$CPP" && test -d "$CPP"; then
- CPP=
-fi
-if test -z "$CPP"; then
- if ${ac_cv_prog_CPP+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- # Double quotes because CPP needs to be expanded
- for CPP in "$CC -E" "$CC -E -traditional-cpp" "/lib/cpp"
- do
- ac_preproc_ok=false
-for ac_c_preproc_warn_flag in '' yes
-do
- # Use a header file that comes with gcc, so configuring glibc
- # with a fresh cross-compiler works.
- # Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
- # <limits.h> exists even on freestanding compilers.
- # On the NeXT, cc -E runs the code through the compiler's parser,
- # not just through cpp. "Syntax error" is here to catch this case.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#ifdef __STDC__
-# include <limits.h>
-#else
-# include <assert.h>
-#endif
- Syntax error
-_ACEOF
-if ac_fn_c_try_cpp "$LINENO"; then :
-
-else
- # Broken: fails on valid input.
-continue
-fi
-rm -f conftest.err conftest.i conftest.$ac_ext
-
- # OK, works on sane cases. Now check whether nonexistent headers
- # can be detected and how.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <ac_nonexistent.h>
-_ACEOF
-if ac_fn_c_try_cpp "$LINENO"; then :
- # Broken: success on invalid input.
-continue
-else
- # Passes both tests.
-ac_preproc_ok=:
-break
-fi
-rm -f conftest.err conftest.i conftest.$ac_ext
-
-done
-# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.
-rm -f conftest.i conftest.err conftest.$ac_ext
-if $ac_preproc_ok; then :
- break
-fi
-
- done
- ac_cv_prog_CPP=$CPP
-
-fi
- CPP=$ac_cv_prog_CPP
-else
- ac_cv_prog_CPP=$CPP
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $CPP" >&5
-$as_echo "$CPP" >&6; }
-ac_preproc_ok=false
-for ac_c_preproc_warn_flag in '' yes
-do
- # Use a header file that comes with gcc, so configuring glibc
- # with a fresh cross-compiler works.
- # Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
- # <limits.h> exists even on freestanding compilers.
- # On the NeXT, cc -E runs the code through the compiler's parser,
- # not just through cpp. "Syntax error" is here to catch this case.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#ifdef __STDC__
-# include <limits.h>
-#else
-# include <assert.h>
-#endif
- Syntax error
-_ACEOF
-if ac_fn_c_try_cpp "$LINENO"; then :
-
-else
- # Broken: fails on valid input.
-continue
-fi
-rm -f conftest.err conftest.i conftest.$ac_ext
-
- # OK, works on sane cases. Now check whether nonexistent headers
- # can be detected and how.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <ac_nonexistent.h>
-_ACEOF
-if ac_fn_c_try_cpp "$LINENO"; then :
- # Broken: success on invalid input.
-continue
-else
- # Passes both tests.
-ac_preproc_ok=:
-break
-fi
-rm -f conftest.err conftest.i conftest.$ac_ext
-
-done
-# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.
-rm -f conftest.i conftest.err conftest.$ac_ext
-if $ac_preproc_ok; then :
-
-else
- { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "C preprocessor \"$CPP\" fails sanity check
-See \`config.log' for more details" "$LINENO" 5; }
-fi
-
-ac_ext=c
-ac_cpp='$CPP $CPPFLAGS'
-ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
-ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
-ac_compiler_gnu=$ac_cv_c_compiler_gnu
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for grep that handles long lines and -e" >&5
-$as_echo_n "checking for grep that handles long lines and -e... " >&6; }
-if ${ac_cv_path_GREP+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -z "$GREP"; then
- ac_path_GREP_found=false
- # Loop through the user's path and test for each of PROGNAME-LIST
- as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_prog in grep ggrep; do
- for ac_exec_ext in '' $ac_executable_extensions; do
- ac_path_GREP="$as_dir/$ac_prog$ac_exec_ext"
- { test -f "$ac_path_GREP" && $as_test_x "$ac_path_GREP"; } || continue
-# Check for GNU ac_path_GREP and select it if it is found.
- # Check for GNU $ac_path_GREP
-case `"$ac_path_GREP" --version 2>&1` in
-*GNU*)
- ac_cv_path_GREP="$ac_path_GREP" ac_path_GREP_found=:;;
-*)
- ac_count=0
- $as_echo_n 0123456789 >"conftest.in"
- while :
- do
- cat "conftest.in" "conftest.in" >"conftest.tmp"
- mv "conftest.tmp" "conftest.in"
- cp "conftest.in" "conftest.nl"
- $as_echo 'GREP' >> "conftest.nl"
- "$ac_path_GREP" -e 'GREP$' -e '-(cannot match)-' < "conftest.nl" >"conftest.out" 2>/dev/null || break
- diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break
- as_fn_arith $ac_count + 1 && ac_count=$as_val
- if test $ac_count -gt ${ac_path_GREP_max-0}; then
- # Best one so far, save it but keep looking for a better one
- ac_cv_path_GREP="$ac_path_GREP"
- ac_path_GREP_max=$ac_count
- fi
- # 10*(2^10) chars as input seems more than enough
- test $ac_count -gt 10 && break
- done
- rm -f conftest.in conftest.tmp conftest.nl conftest.out;;
-esac
-
- $ac_path_GREP_found && break 3
- done
- done
- done
-IFS=$as_save_IFS
- if test -z "$ac_cv_path_GREP"; then
- as_fn_error $? "no acceptable grep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
- fi
-else
- ac_cv_path_GREP=$GREP
-fi
-
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_GREP" >&5
-$as_echo "$ac_cv_path_GREP" >&6; }
- GREP="$ac_cv_path_GREP"
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for egrep" >&5
-$as_echo_n "checking for egrep... " >&6; }
-if ${ac_cv_path_EGREP+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if echo a | $GREP -E '(a|b)' >/dev/null 2>&1
- then ac_cv_path_EGREP="$GREP -E"
- else
- if test -z "$EGREP"; then
- ac_path_EGREP_found=false
- # Loop through the user's path and test for each of PROGNAME-LIST
- as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_prog in egrep; do
- for ac_exec_ext in '' $ac_executable_extensions; do
- ac_path_EGREP="$as_dir/$ac_prog$ac_exec_ext"
- { test -f "$ac_path_EGREP" && $as_test_x "$ac_path_EGREP"; } || continue
-# Check for GNU ac_path_EGREP and select it if it is found.
- # Check for GNU $ac_path_EGREP
-case `"$ac_path_EGREP" --version 2>&1` in
-*GNU*)
- ac_cv_path_EGREP="$ac_path_EGREP" ac_path_EGREP_found=:;;
-*)
- ac_count=0
- $as_echo_n 0123456789 >"conftest.in"
- while :
- do
- cat "conftest.in" "conftest.in" >"conftest.tmp"
- mv "conftest.tmp" "conftest.in"
- cp "conftest.in" "conftest.nl"
- $as_echo 'EGREP' >> "conftest.nl"
- "$ac_path_EGREP" 'EGREP$' < "conftest.nl" >"conftest.out" 2>/dev/null || break
- diff "conftest.out" "conftest.nl" >/dev/null 2>&1 || break
- as_fn_arith $ac_count + 1 && ac_count=$as_val
- if test $ac_count -gt ${ac_path_EGREP_max-0}; then
- # Best one so far, save it but keep looking for a better one
- ac_cv_path_EGREP="$ac_path_EGREP"
- ac_path_EGREP_max=$ac_count
- fi
- # 10*(2^10) chars as input seems more than enough
- test $ac_count -gt 10 && break
- done
- rm -f conftest.in conftest.tmp conftest.nl conftest.out;;
-esac
-
- $ac_path_EGREP_found && break 3
- done
- done
- done
-IFS=$as_save_IFS
- if test -z "$ac_cv_path_EGREP"; then
- as_fn_error $? "no acceptable egrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin" "$LINENO" 5
- fi
-else
- ac_cv_path_EGREP=$EGREP
-fi
-
- fi
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_EGREP" >&5
-$as_echo "$ac_cv_path_EGREP" >&6; }
- EGREP="$ac_cv_path_EGREP"
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ANSI C header files" >&5
-$as_echo_n "checking for ANSI C header files... " >&6; }
-if ${ac_cv_header_stdc+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <float.h>
-
-int
-main ()
-{
-
- ;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
- ac_cv_header_stdc=yes
-else
- ac_cv_header_stdc=no
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-
-if test $ac_cv_header_stdc = yes; then
- # SunOS 4.x string.h does not declare mem*, contrary to ANSI.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <string.h>
-
-_ACEOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
- $EGREP "memchr" >/dev/null 2>&1; then :
-
-else
- ac_cv_header_stdc=no
-fi
-rm -f conftest*
-
-fi
-
-if test $ac_cv_header_stdc = yes; then
- # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI.
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <stdlib.h>
-
-_ACEOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
- $EGREP "free" >/dev/null 2>&1; then :
-
-else
- ac_cv_header_stdc=no
-fi
-rm -f conftest*
-
-fi
-
-if test $ac_cv_header_stdc = yes; then
- # /bin/cc in Irix-4.0.5 gets non-ANSI ctype macros unless using -ansi.
- if test "$cross_compiling" = yes; then :
- :
-else
- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-#include <ctype.h>
-#include <stdlib.h>
-#if ((' ' & 0x0FF) == 0x020)
-# define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
-# define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))
-#else
-# define ISLOWER(c) \
- (('a' <= (c) && (c) <= 'i') \
- || ('j' <= (c) && (c) <= 'r') \
- || ('s' <= (c) && (c) <= 'z'))
-# define TOUPPER(c) (ISLOWER(c) ? ((c) | 0x40) : (c))
-#endif
-
-#define XOR(e, f) (((e) && !(f)) || (!(e) && (f)))
-int
-main ()
-{
- int i;
- for (i = 0; i < 256; i++)
- if (XOR (islower (i), ISLOWER (i))
- || toupper (i) != TOUPPER (i))
- return 2;
- return 0;
-}
-_ACEOF
-if ac_fn_c_try_run "$LINENO"; then :
-
-else
- ac_cv_header_stdc=no
-fi
-rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
- conftest.$ac_objext conftest.beam conftest.$ac_ext
-fi
-
-fi
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_header_stdc" >&5
-$as_echo "$ac_cv_header_stdc" >&6; }
-if test $ac_cv_header_stdc = yes; then
-
-$as_echo "#define STDC_HEADERS 1" >>confdefs.h
-
-fi
-
-# On IRIX 5.3, sys/types and inttypes.h are conflicting.
-for ac_header in sys/types.h sys/stat.h stdlib.h string.h memory.h strings.h \
- inttypes.h stdint.h unistd.h
-do :
- as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
-ac_fn_c_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default
-"
-if eval test \"x\$"$as_ac_Header"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-
-done
-
-
-ac_fn_c_check_type "$LINENO" "long long" "ac_cv_type_long_long" "$ac_includes_default"
-if test "x$ac_cv_type_long_long" = xyes; then :
-
-cat >>confdefs.h <<_ACEOF
-#define HAVE_LONG_LONG 1
-_ACEOF
-
-DEFINE_HAVE_LONG_LONG="#define HAVE_LONG_LONG 1"
-
-else
- DEFINE_HAVE_LONG_LONG="#undef HAVE_LONG_LONG"
-
-fi
-
-
-ac_aux_dir=
-for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
- if test -f "$ac_dir/install-sh"; then
- ac_aux_dir=$ac_dir
- ac_install_sh="$ac_aux_dir/install-sh -c"
- break
- elif test -f "$ac_dir/install.sh"; then
- ac_aux_dir=$ac_dir
- ac_install_sh="$ac_aux_dir/install.sh -c"
- break
- elif test -f "$ac_dir/shtool"; then
- ac_aux_dir=$ac_dir
- ac_install_sh="$ac_aux_dir/shtool install -c"
- break
- fi
-done
-if test -z "$ac_aux_dir"; then
- as_fn_error $? "cannot find install-sh, install.sh, or shtool in \"$srcdir\" \"$srcdir/..\" \"$srcdir/../..\"" "$LINENO" 5
-fi
-
-# These three variables are undocumented and unsupported,
-# and are intended to be withdrawn in a future Autoconf release.
-# They can cause serious problems if a builder's source tree is in a directory
-# whose full name contains unusual characters.
-ac_config_guess="$SHELL $ac_aux_dir/config.guess" # Please don't use this var.
-ac_config_sub="$SHELL $ac_aux_dir/config.sub" # Please don't use this var.
-ac_configure="$SHELL $ac_aux_dir/configure" # Please don't use this var.
-
-
-# Make sure we can run config.sub.
-$SHELL "$ac_aux_dir/config.sub" sun4 >/dev/null 2>&1 ||
- as_fn_error $? "cannot run $SHELL $ac_aux_dir/config.sub" "$LINENO" 5
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking build system type" >&5
-$as_echo_n "checking build system type... " >&6; }
-if ${ac_cv_build+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_build_alias=$build_alias
-test "x$ac_build_alias" = x &&
- ac_build_alias=`$SHELL "$ac_aux_dir/config.guess"`
-test "x$ac_build_alias" = x &&
- as_fn_error $? "cannot guess build type; you must specify one" "$LINENO" 5
-ac_cv_build=`$SHELL "$ac_aux_dir/config.sub" $ac_build_alias` ||
- as_fn_error $? "$SHELL $ac_aux_dir/config.sub $ac_build_alias failed" "$LINENO" 5
-
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_build" >&5
-$as_echo "$ac_cv_build" >&6; }
-case $ac_cv_build in
-*-*-*) ;;
-*) as_fn_error $? "invalid value of canonical build" "$LINENO" 5;;
-esac
-build=$ac_cv_build
-ac_save_IFS=$IFS; IFS='-'
-set x $ac_cv_build
-shift
-build_cpu=$1
-build_vendor=$2
-shift; shift
-# Remember, the first character of IFS is used to create $*,
-# except with old shells:
-build_os=$*
-IFS=$ac_save_IFS
-case $build_os in *\ *) build_os=`echo "$build_os" | sed 's/ /-/g'`;; esac
-
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking host system type" >&5
-$as_echo_n "checking host system type... " >&6; }
-if ${ac_cv_host+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test "x$host_alias" = x; then
- ac_cv_host=$ac_cv_build
-else
- ac_cv_host=`$SHELL "$ac_aux_dir/config.sub" $host_alias` ||
- as_fn_error $? "$SHELL $ac_aux_dir/config.sub $host_alias failed" "$LINENO" 5
-fi
-
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_host" >&5
-$as_echo "$ac_cv_host" >&6; }
-case $ac_cv_host in
-*-*-*) ;;
-*) as_fn_error $? "invalid value of canonical host" "$LINENO" 5;;
-esac
-host=$ac_cv_host
-ac_save_IFS=$IFS; IFS='-'
-set x $ac_cv_host
-shift
-host_cpu=$1
-host_vendor=$2
-shift; shift
-# Remember, the first character of IFS is used to create $*,
-# except with old shells:
-host_os=$*
-IFS=$ac_save_IFS
-case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac
-
-
-
-if test -n "$ac_tool_prefix"; then
- # Extract the first word of "${ac_tool_prefix}ar", so it can be a program name with args.
-set dummy ${ac_tool_prefix}ar; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_AR+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$AR"; then
- ac_cv_prog_AR="$AR" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_AR="${ac_tool_prefix}ar"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-AR=$ac_cv_prog_AR
-if test -n "$AR"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $AR" >&5
-$as_echo "$AR" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
-fi
-if test -z "$ac_cv_prog_AR"; then
- ac_ct_AR=$AR
- # Extract the first word of "ar", so it can be a program name with args.
-set dummy ar; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_ac_ct_AR+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$ac_ct_AR"; then
- ac_cv_prog_ac_ct_AR="$ac_ct_AR" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_ac_ct_AR="ar"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-ac_ct_AR=$ac_cv_prog_ac_ct_AR
-if test -n "$ac_ct_AR"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_AR" >&5
-$as_echo "$ac_ct_AR" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
- if test "x$ac_ct_AR" = x; then
- AR=""
- else
- case $cross_compiling:$ac_tool_warned in
-yes:)
-{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5
-$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;}
-ac_tool_warned=yes ;;
-esac
- AR=$ac_ct_AR
- fi
-else
- AR="$ac_cv_prog_AR"
-fi
-
-if test -n "$ac_tool_prefix"; then
- # Extract the first word of "${ac_tool_prefix}ranlib", so it can be a program name with args.
-set dummy ${ac_tool_prefix}ranlib; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_RANLIB+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$RANLIB"; then
- ac_cv_prog_RANLIB="$RANLIB" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_RANLIB="${ac_tool_prefix}ranlib"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-RANLIB=$ac_cv_prog_RANLIB
-if test -n "$RANLIB"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $RANLIB" >&5
-$as_echo "$RANLIB" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
-
-fi
-if test -z "$ac_cv_prog_RANLIB"; then
- ac_ct_RANLIB=$RANLIB
- # Extract the first word of "ranlib", so it can be a program name with args.
-set dummy ranlib; ac_word=$2
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
-$as_echo_n "checking for $ac_word... " >&6; }
-if ${ac_cv_prog_ac_ct_RANLIB+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- if test -n "$ac_ct_RANLIB"; then
- ac_cv_prog_ac_ct_RANLIB="$ac_ct_RANLIB" # Let the user override the test.
-else
-as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- for ac_exec_ext in '' $ac_executable_extensions; do
- if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
- ac_cv_prog_ac_ct_RANLIB="ranlib"
- $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
- break 2
- fi
-done
- done
-IFS=$as_save_IFS
-
-fi
-fi
-ac_ct_RANLIB=$ac_cv_prog_ac_ct_RANLIB
-if test -n "$ac_ct_RANLIB"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_ct_RANLIB" >&5
-$as_echo "$ac_ct_RANLIB" >&6; }
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-fi
-
- if test "x$ac_ct_RANLIB" = x; then
- RANLIB="true"
- else
- case $cross_compiling:$ac_tool_warned in
-yes:)
-{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet" >&5
-$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" >&2;}
-ac_tool_warned=yes ;;
-esac
- RANLIB=$ac_ct_RANLIB
- fi
-else
- RANLIB="$ac_cv_prog_RANLIB"
-fi
-
-
-iswin=no
-# Shared library support. Because we don't believe in automake!
-case $host in
-*-*-darwin*)
- SH_CFLAGS=-dynamic
-
- SH_LDFLAGS="-dynamiclib -undefined suppress -flat_namespace"
-
- SHOBJ_CFLAGS="-dynamic -fno-common"
-
- SHOBJ_LDFLAGS="-bundle -undefined dynamic_lookup -flat_namespace"
-
- ;;
-*-*-ming*)
- # Use the built-in dlopen wrapper
- have_dlopen=yes
- iswin=yes
- SHOBJ_LDFLAGS=-shared
-
- SH_LDFLAGS=-shared
-
- ;;
-*-*-cygwin)
- iswin=yes
- SHOBJ_LDFLAGS=-shared
-
- SH_LDFLAGS=-shared
-
- ;;
-*)
- # Generic Unix settings
- LINKFLAGS=-rdynamic
-
- SH_CFLAGS=-fPIC
-
- SH_LDFLAGS=-shared
-
- SHOBJ_CFLAGS=-fPIC
-
- SHOBJ_LDFLAGS="-shared -nostartfiles"
-
- ;;
-esac
-
-# Check whether --enable-math was given.
-if test "${enable_math+set}" = set; then :
- enableval=$enable_math;
- if test "x$enableval" = "xyes" ; then
- EXTRA_CFLAGS="$EXTRA_CFLAGS -DJIM_MATH_FUNCTIONS"
- fi
-
-
-fi
-
-# Check whether --enable-ipv6 was given.
-if test "${enable_ipv6+set}" = set; then :
- enableval=$enable_ipv6;
- if test "x$enableval" = "xyes" ; then
- EXTRA_CFLAGS="$EXTRA_CFLAGS -DJIM_IPV6"
- fi
-
-
-fi
-
-# Check whether --enable-utf8 was given.
-if test "${enable_utf8+set}" = set; then :
- enableval=$enable_utf8;
- if test "x$enableval" = "xyes" ; then
- EXTRA_CFLAGS="$EXTRA_CFLAGS -DJIM_UTF8 -DJIM_REGEXP"
- JIM_UTF8=1
-
- fi
-
-
-fi
-
-
-lineedit=1
-# Check whether --enable-lineedit was given.
-if test "${enable_lineedit+set}" = set; then :
- enableval=$enable_lineedit;
- if test "x$enableval" = "xno" ; then
- lineedit=0
- fi
-
-
-fi
-
-
-# Is $1 in list $2?
-in_list()
-{
- echo "$2" | tr ' ' '\n' | grep "^$1\$" >/dev/null
-}
-
-# Tcl extensions
-ext_tcl="stdlib glob tclcompat tree rlprompt oo"
-# C extensions
-ext_c="load package readdir array clock exec file posix regexp signal aio eventloop syslog nvp readline sqlite sqlite3 win32"
-
-# Tcl extensions which can be modules
-ext_tcl_mod="glob tree rlprompt oo"
-# C extensions which can be modules
-ext_c_mod="readdir array clock file posix regexp syslog readline sqlite sqlite3 win32"
-
-# All extensions
-ext_all="$ext_c $ext_tcl"
-
-# Default static extensions
-ext_default="stdlib load package readdir glob array clock exec file posix regexp signal tclcompat aio eventloop syslog"
-
-
-# Check whether --with-jim-ext was given.
-if test "${with_jim_ext+set}" = set; then :
- withval=$with_jim_ext;
-fi
-
-
-# Check whether --with-out-jim-ext was given.
-if test "${with_out_jim_ext+set}" = set; then :
- withval=$with_out_jim_ext;
-fi
-
-
-# Check whether --with-jim-extmod was given.
-if test "${with_jim_extmod+set}" = set; then :
- withval=$with_jim_extmod;
-fi
-
-
-if test "$with_out_jim_ext" = "default"; then
- # but we always include stdlib
- ext_default=stdlib
- with_out_jim_ext=
-fi
-
-# Check valid extension names
-for i in $with_jim_ext $with_out_jim_ext $with_jim_extmod; do
- in_list "$i" "$ext_all" || as_fn_error $? "Unknown extension: $i" "$LINENO" 5
-done
-
-JIM_LIBTYPE=static
-
-# Check whether --with-jim-shared was given.
-if test "${with_jim_shared+set}" = set; then :
- withval=$with_jim_shared;
- if test "x$withval" = "xyes" ; then
- JIM_LIBTYPE=shared
- fi
-
-
-fi
-
-# Check whether --enable-shared was given.
-if test "${enable_shared+set}" = set; then :
- enableval=$enable_shared;
- if test "x$enableval" = "xyes" ; then
- JIM_LIBTYPE=shared
- fi
-
-
-fi
-
-# Check whether --enable-static was given.
-if test "${enable_static+set}" = set; then :
- enableval=$enable_static;
- if test "x$enableval" = "xyes" ; then
- JIM_LIBTYPE=static
- fi
-
-
-fi
-
-JIM_LIBTYPE=$JIM_LIBTYPE
-
-
-
-# Check whether --with-jim-regexp was given.
-if test "${with_jim_regexp+set}" = set; then :
- withval=$with_jim_regexp;
- if test "x$withval" = "xyes" ; then
- EXTRA_CFLAGS="$EXTRA_CFLAGS -DJIM_REGEXP"
- fi
-
-
-fi
-
-
-for ac_header in sys/un.h dlfcn.h termios.h
-do :
- as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
-ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
-if eval test \"x\$"$as_ac_Header"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_header" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-
-done
-
-ac_fn_c_check_header_mongrel "$LINENO" "termios.h" "ac_cv_header_termios_h" "$ac_includes_default"
-if test "x$ac_cv_header_termios_h" = xyes; then :
-
-else
- lineedit=0
-fi
-
-
-
-for ac_func in ualarm sysinfo lstat fork vfork
-do :
- as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
-ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
-if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-done
-
-for ac_func in backtrace geteuid mkstemp realpath strptime
-do :
- as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
-ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
-if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-done
-
-for ac_func in regcomp waitpid sigaction sys_signame sys_siglist
-do :
- as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
-ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
-if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-done
-
-for ac_func in syslog opendir readlink sleep usleep pipe inet_ntop getaddrinfo
-do :
- as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
-ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
-if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
- cat >>confdefs.h <<_ACEOF
-#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-done
-
-
-# needs_xxx="yyy zzz" means that extension xxx needs environment variables $yyy and $zzz
-# to not be "no"
-# dep_xxx="yyy zzz" means that if xxx is selected, so is yyy and zzz
-# libs_xxx="yyy zzz" means that if xxx is selected, libs yyy and zzz may be required
-libs_aio="socket"
-needs_regexp="ac_cv_func_regcomp"
-needs_syslog="ac_cv_func_syslog"
-needs_exec="ac_cv_func_vfork ac_cv_func_waitpid"
-needs_readdir="ac_cv_func_opendir"
-dep_glob="readdir"
-needs_posix="ac_cv_func_waitpid"
-needs_load="have_dlopen"
-libs_load="dl"
-needs_signal="ac_cv_func_sigaction ac_cv_func_vfork"
-needs_readline="ac_cv_search_readline"
-dep_rlprompt="readline"
-libs_readline="readline"
-needs_sqlite="ac_cv_search_sqlite_open"
-libs_sqlite="sqlite"
-needs_sqlite3="ac_cv_search_sqlite3_open"
-libs_sqlite3="sqlite3"
-needs_win32="iswin"
-dep_tree="oo"
-
-for i in $ext_default $with_jim_ext; do
- in_list "$i" "$with_out_jim_ext" && continue
- eval "dep=\$dep_$i"
- test -z "$dep" && continue
- with_jim_ext="$with_jim_ext $dep"
-done
-for i in $with_jim_extmod; do
- eval "dep=\$dep_$i"
- test -z "$dep" && continue
- for d in $dep; do
- in_list "$d" "$with_jim_ext" || with_jim_extmod="$with_jim_extmod $d"
- done
-done
-
-for i in $ext_default $with_jim_ext $with_jim_extmod; do
- in_list "$i" "$with_out_jim_ext" && continue
- eval "libs=\$libs_$i"
- needed_libs="$needed_libs $libs"
-done
-
-if in_list dl "$needed_libs"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing dlopen" >&5
-$as_echo_n "checking for library containing dlopen... " >&6; }
-if ${ac_cv_search_dlopen+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char dlopen ();
-int
-main ()
-{
-return dlopen ();
- ;
- return 0;
-}
-_ACEOF
-for ac_lib in '' dl; do
- if test -z "$ac_lib"; then
- ac_res="none required"
- else
- ac_res=-l$ac_lib
- LIBS="-l$ac_lib $ac_func_search_save_LIBS"
- fi
- if ac_fn_c_try_link "$LINENO"; then :
- ac_cv_search_dlopen=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext
- if ${ac_cv_search_dlopen+:} false; then :
- break
-fi
-done
-if ${ac_cv_search_dlopen+:} false; then :
-
-else
- ac_cv_search_dlopen=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_dlopen" >&5
-$as_echo "$ac_cv_search_dlopen" >&6; }
-ac_res=$ac_cv_search_dlopen
-if test "$ac_res" != no; then :
- test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
- have_dlopen=yes;
-$as_echo "#define HAVE_DLOPEN 1" >>confdefs.h
-
-fi
-
-fi
-if in_list readline "$needed_libs"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing readline" >&5
-$as_echo_n "checking for library containing readline... " >&6; }
-if ${ac_cv_search_readline+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char readline ();
-int
-main ()
-{
-return readline ();
- ;
- return 0;
-}
-_ACEOF
-for ac_lib in '' readline; do
- if test -z "$ac_lib"; then
- ac_res="none required"
- else
- ac_res=-l$ac_lib
- LIBS="-l$ac_lib $ac_func_search_save_LIBS"
- fi
- if ac_fn_c_try_link "$LINENO"; then :
- ac_cv_search_readline=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext
- if ${ac_cv_search_readline+:} false; then :
- break
-fi
-done
-if ${ac_cv_search_readline+:} false; then :
-
-else
- ac_cv_search_readline=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_readline" >&5
-$as_echo "$ac_cv_search_readline" >&6; }
-ac_res=$ac_cv_search_readline
-if test "$ac_res" != no; then :
- test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
-$as_echo "#define HAVE_READLINE 1" >>confdefs.h
-
-fi
-
-fi
-if in_list sqlite "$needed_libs"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing sqlite_open" >&5
-$as_echo_n "checking for library containing sqlite_open... " >&6; }
-if ${ac_cv_search_sqlite_open+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char sqlite_open ();
-int
-main ()
-{
-return sqlite_open ();
- ;
- return 0;
-}
-_ACEOF
-for ac_lib in '' sqlite; do
- if test -z "$ac_lib"; then
- ac_res="none required"
- else
- ac_res=-l$ac_lib
- LIBS="-l$ac_lib $ac_func_search_save_LIBS"
- fi
- if ac_fn_c_try_link "$LINENO"; then :
- ac_cv_search_sqlite_open=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext
- if ${ac_cv_search_sqlite_open+:} false; then :
- break
-fi
-done
-if ${ac_cv_search_sqlite_open+:} false; then :
-
-else
- ac_cv_search_sqlite_open=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_sqlite_open" >&5
-$as_echo "$ac_cv_search_sqlite_open" >&6; }
-ac_res=$ac_cv_search_sqlite_open
-if test "$ac_res" != no; then :
- test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
-$as_echo "#define HAVE_SQLITE 1" >>confdefs.h
-
-fi
-
-fi
-if in_list sqlite3 "$needed_libs"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing sqlite3_open" >&5
-$as_echo_n "checking for library containing sqlite3_open... " >&6; }
-if ${ac_cv_search_sqlite3_open+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char sqlite3_open ();
-int
-main ()
-{
-return sqlite3_open ();
- ;
- return 0;
-}
-_ACEOF
-for ac_lib in '' sqlite3; do
- if test -z "$ac_lib"; then
- ac_res="none required"
- else
- ac_res=-l$ac_lib
- LIBS="-l$ac_lib $ac_func_search_save_LIBS"
- fi
- if ac_fn_c_try_link "$LINENO"; then :
- ac_cv_search_sqlite3_open=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext
- if ${ac_cv_search_sqlite3_open+:} false; then :
- break
-fi
-done
-if ${ac_cv_search_sqlite3_open+:} false; then :
-
-else
- ac_cv_search_sqlite3_open=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_sqlite3_open" >&5
-$as_echo "$ac_cv_search_sqlite3_open" >&6; }
-ac_res=$ac_cv_search_sqlite3_open
-if test "$ac_res" != no; then :
- test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
-$as_echo "#define HAVE_SQLITE3 1" >>confdefs.h
-
-fi
-
-fi
-if in_list socket "$needed_libs"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing socket" >&5
-$as_echo_n "checking for library containing socket... " >&6; }
-if ${ac_cv_search_socket+:} false; then :
- $as_echo_n "(cached) " >&6
-else
- ac_func_search_save_LIBS=$LIBS
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-/* Override any GCC internal prototype to avoid an error.
- Use char because int might match the return type of a GCC
- builtin and then its argument prototype would still apply. */
-#ifdef __cplusplus
-extern "C"
-#endif
-char socket ();
-int
-main ()
-{
-return socket ();
- ;
- return 0;
-}
-_ACEOF
-for ac_lib in '' socket; do
- if test -z "$ac_lib"; then
- ac_res="none required"
- else
- ac_res=-l$ac_lib
- LIBS="-l$ac_lib $ac_func_search_save_LIBS"
- fi
- if ac_fn_c_try_link "$LINENO"; then :
- ac_cv_search_socket=$ac_res
-fi
-rm -f core conftest.err conftest.$ac_objext \
- conftest$ac_exeext
- if ${ac_cv_search_socket+:} false; then :
- break
-fi
-done
-if ${ac_cv_search_socket+:} false; then :
-
-else
- ac_cv_search_socket=no
-fi
-rm conftest.$ac_ext
-LIBS=$ac_func_search_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_socket" >&5
-$as_echo "$ac_cv_search_socket" >&6; }
-ac_res=$ac_cv_search_socket
-if test "$ac_res" != no; then :
- test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
-fi
-
-fi
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking environ declared in unistd.h?" >&5
-$as_echo_n "checking environ declared in unistd.h?... " >&6; }
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h. */
-
-#define _GNU_SOURCE
-#include <unistd.h>
-int main(int argc, char **argv) { char **ep = environ; }
-
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
-$as_echo "yes" >&6; }
-
-$as_echo "#define NO_ENVIRON_EXTERN 1" >>confdefs.h
-
-
-else
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
-
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-
-# Now that we know what the platform supports:
-
-# For all known extensions:
-# - If it is disabled, remove it
-# - Otherwise, check to see if it's pre-requisites are met
-# - If yes, add it if it is enabled or is a default
-# - If no, error if it is enabled, or do nothing otherwise
-# - Modules may be either C or Tcl
-
-for i in $ext_all; do
- { $as_echo "$as_me:${as_lineno-$LINENO}: checking extension $i" >&5
-$as_echo_n "checking extension $i... " >&6; }
- # Disabled?
- in_list "$i" "$with_out_jim_ext"
- if test $? -eq 0; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: disabled" >&5
-$as_echo "disabled" >&6; }
- continue
- fi
- # Check dependencies
- eval "dep=\$needs_$i"
- met=1
- for d in $dep; do
- eval "check=\$$d"
- if test "$check" == "no" -o -z "$check" ; then
- met=0
- break
- fi
- done
- # Selected as a module?
- in_list "$i" "$with_jim_extmod"
- if test $? -eq 0; then
- in_list "$i" "$ext_tcl_mod"
- if test $? -eq 0; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: tcl" >&5
-$as_echo "tcl" >&6; }
- extmodtcl="$exmodtcl $i"
- continue
- fi
- in_list "$i" "$ext_c_mod"
- if test $? -ne 0; then
- as_fn_error $? "not a module" "$LINENO" 5
- fi
- if test $met -eq 0; then
- as_fn_error $? "dependencies not met" "$LINENO" 5
- fi
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: module" >&5
-$as_echo "module" >&6; }
- extmod="$extmod $i"
- continue
- fi
- # Selected as a static extension?
- in_list "$i" "$with_jim_ext"
- if test $? -eq 0; then
- if test $met -eq 0; then
- as_fn_error $? "dependencies not met" "$LINENO" 5
- fi
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: enabled" >&5
-$as_echo "enabled" >&6; }
- ext="$ext $i"
- continue
- fi
- # Enabled by default?
- in_list "$i" "$ext_default"
- if test $? -eq 0; then
- if test $met -eq 0; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: disabled (dependencies)" >&5
-$as_echo "disabled (dependencies)" >&6; }
- continue
- fi
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: enabled (default)" >&5
-$as_echo "enabled (default)" >&6; }
- ext="$ext $i"
- continue
- fi
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: not enabled" >&5
-$as_echo "not enabled" >&6; }
-done
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: Jim static extensions:$ext" >&5
-$as_echo "Jim static extensions:$ext" >&6; }
-JIM_EXTENSIONS=$ext
-
-if test -n "$extmod"; then
-case "$iswin,$JIM_LIBTYPE" in
-yes,static)
- { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error $? "cygwin/mingw require --with-jim-shared for dynamic modules
-See \`config.log' for more details" "$LINENO" 5; }
- ;;
-esac
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: Jim dynamic extensions:$extmod" >&5
-$as_echo "Jim dynamic extensions:$extmod" >&6; }
- JIM_MOD_EXTENSIONS=$extmod
-
-fi
-if test -n "$extmodtcl"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: result: Jim Tcl extensions:$extmodtcl" >&5
-$as_echo "Jim Tcl extensions:$extmodtcl" >&6; }
- JIM_TCL_EXTENSIONS=$extmodtcl
-
-fi
-for i in $ext; do
- EXTRA_CFLAGS="$EXTRA_CFLAGS -Djim_ext_$i"
-done
-
-if test $iswin = yes; then
- in_list "aio" "$ext $extmod" && LIBS="$LIBS -lwsock32"
- EXTRA_OBJS="$EXTRA_OBJS jim-win32compat.o"
-fi
-if test $lineedit -eq 1; then
- EXTRA_CFLAGS="$EXTRA_CFLAGS -DUSE_LINENOISE"
- EXTRA_OBJS="$EXTRA_OBJS linenoise.o"
-fi
-
-
-EXTRA_CFLAGS=$EXTRA_CFLAGS
-
-EXTRA_OBJS=$EXTRA_OBJS
-
-TCL_LIBRARY=$libdir/jim
-
-PLATFORM_OS=`uname -s`
-
-PLATFORM_PLATFORM=unix
-
-
-ac_config_files="$ac_config_files Makefile jim-config.h"
-
-cat >confcache <<\_ACEOF
-# This file is a shell script that caches the results of configure
-# tests run on this system so they can be shared between configure
-# scripts and configure runs, see configure's option --config-cache.
-# It is not useful on other systems. If it contains results you don't
-# want to keep, you may remove or edit it.
-#
-# config.status only pays attention to the cache file if you give it
-# the --recheck option to rerun configure.
-#
-# `ac_cv_env_foo' variables (set or unset) will be overridden when
-# loading this file, other *unset* `ac_cv_foo' will be assigned the
-# following values.
-
-_ACEOF
-
-# The following way of writing the cache mishandles newlines in values,
-# but we know of no workaround that is simple, portable, and efficient.
-# So, we kill variables containing newlines.
-# Ultrix sh set writes to stderr and can't be redirected directly,
-# and sets the high bit in the cache file unless we assign to the vars.
-(
- for ac_var in `(set) 2>&1 | sed -n 's/^\([a-zA-Z_][a-zA-Z0-9_]*\)=.*/\1/p'`; do
- eval ac_val=\$$ac_var
- case $ac_val in #(
- *${as_nl}*)
- case $ac_var in #(
- *_cv_*) { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline" >&5
-$as_echo "$as_me: WARNING: cache variable $ac_var contains a newline" >&2;} ;;
- esac
- case $ac_var in #(
- _ | IFS | as_nl) ;; #(
- BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #(
- *) { eval $ac_var=; unset $ac_var;} ;;
- esac ;;
- esac
- done
-
- (set) 2>&1 |
- case $as_nl`(ac_space=' '; set) 2>&1` in #(
- *${as_nl}ac_space=\ *)
- # `set' does not quote correctly, so add quotes: double-quote
- # substitution turns \\\\ into \\, and sed turns \\ into \.
- sed -n \
- "s/'/'\\\\''/g;
- s/^\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\)=\\(.*\\)/\\1='\\2'/p"
- ;; #(
- *)
- # `set' quotes correctly as required by POSIX, so do not add quotes.
- sed -n "/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p"
- ;;
- esac |
- sort
-) |
- sed '
- /^ac_cv_env_/b end
- t clear
- :clear
- s/^\([^=]*\)=\(.*[{}].*\)$/test "${\1+set}" = set || &/
- t end
- s/^\([^=]*\)=\(.*\)$/\1=${\1=\2}/
- :end' >>confcache
-if diff "$cache_file" confcache >/dev/null 2>&1; then :; else
- if test -w "$cache_file"; then
- if test "x$cache_file" != "x/dev/null"; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: updating cache $cache_file" >&5
-$as_echo "$as_me: updating cache $cache_file" >&6;}
- if test ! -f "$cache_file" || test -h "$cache_file"; then
- cat confcache >"$cache_file"
- else
- case $cache_file in #(
- */* | ?:*)
- mv -f confcache "$cache_file"$$ &&
- mv -f "$cache_file"$$ "$cache_file" ;; #(
- *)
- mv -f confcache "$cache_file" ;;
- esac
- fi
- fi
- else
- { $as_echo "$as_me:${as_lineno-$LINENO}: not updating unwritable cache $cache_file" >&5
-$as_echo "$as_me: not updating unwritable cache $cache_file" >&6;}
- fi
-fi
-rm -f confcache
-
-test "x$prefix" = xNONE && prefix=$ac_default_prefix
-# Let make expand exec_prefix.
-test "x$exec_prefix" = xNONE && exec_prefix='${prefix}'
-
-DEFS=-DHAVE_CONFIG_H
-
-ac_libobjs=
-ac_ltlibobjs=
-U=
-for ac_i in : $LIBOBJS; do test "x$ac_i" = x: && continue
- # 1. Remove the extension, and $U if already installed.
- ac_script='s/\$U\././;s/\.o$//;s/\.obj$//'
- ac_i=`$as_echo "$ac_i" | sed "$ac_script"`
- # 2. Prepend LIBOBJDIR. When used with automake>=1.10 LIBOBJDIR
- # will be set to the directory where LIBOBJS objects are built.
- as_fn_append ac_libobjs " \${LIBOBJDIR}$ac_i\$U.$ac_objext"
- as_fn_append ac_ltlibobjs " \${LIBOBJDIR}$ac_i"'$U.lo'
-done
-LIBOBJS=$ac_libobjs
-
-LTLIBOBJS=$ac_ltlibobjs
-
-
-
-: "${CONFIG_STATUS=./config.status}"
-ac_write_fail=0
-ac_clean_files_save=$ac_clean_files
-ac_clean_files="$ac_clean_files $CONFIG_STATUS"
-{ $as_echo "$as_me:${as_lineno-$LINENO}: creating $CONFIG_STATUS" >&5
-$as_echo "$as_me: creating $CONFIG_STATUS" >&6;}
-as_write_fail=0
-cat >$CONFIG_STATUS <<_ASEOF || as_write_fail=1
-#! $SHELL
-# Generated by $as_me.
-# Run this file to recreate the current configuration.
-# Compiler output produced by configure, useful for debugging
-# configure, is in config.log if it exists.
-
-debug=false
-ac_cs_recheck=false
-ac_cs_silent=false
-
-SHELL=\${CONFIG_SHELL-$SHELL}
-export SHELL
-_ASEOF
-cat >>$CONFIG_STATUS <<\_ASEOF || as_write_fail=1
-## -------------------- ##
-## M4sh Initialization. ##
-## -------------------- ##
-
-# Be more Bourne compatible
-DUALCASE=1; export DUALCASE # for MKS sh
-if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then :
- emulate sh
- NULLCMD=:
- # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
- # is contrary to our usage. Disable this feature.
- alias -g '${1+"$@"}'='"$@"'
- setopt NO_GLOB_SUBST
-else
- case `(set -o) 2>/dev/null` in #(
- *posix*) :
- set -o posix ;; #(
- *) :
- ;;
-esac
-fi
-
-
-as_nl='
-'
-export as_nl
-# Printing a long string crashes Solaris 7 /usr/bin/printf.
-as_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
-as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo
-as_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo
-# Prefer a ksh shell builtin over an external printf program on Solaris,
-# but without wasting forks for bash or zsh.
-if test -z "$BASH_VERSION$ZSH_VERSION" \
- && (test "X`print -r -- $as_echo`" = "X$as_echo") 2>/dev/null; then
- as_echo='print -r --'
- as_echo_n='print -rn --'
-elif (test "X`printf %s $as_echo`" = "X$as_echo") 2>/dev/null; then
- as_echo='printf %s\n'
- as_echo_n='printf %s'
-else
- if test "X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`" = "X-n $as_echo"; then
- as_echo_body='eval /usr/ucb/echo -n "$1$as_nl"'
- as_echo_n='/usr/ucb/echo -n'
- else
- as_echo_body='eval expr "X$1" : "X\\(.*\\)"'
- as_echo_n_body='eval
- arg=$1;
- case $arg in #(
- *"$as_nl"*)
- expr "X$arg" : "X\\(.*\\)$as_nl";
- arg=`expr "X$arg" : ".*$as_nl\\(.*\\)"`;;
- esac;
- expr "X$arg" : "X\\(.*\\)" | tr -d "$as_nl"
- '
- export as_echo_n_body
- as_echo_n='sh -c $as_echo_n_body as_echo'
- fi
- export as_echo_body
- as_echo='sh -c $as_echo_body as_echo'
-fi
-
-# The user is always right.
-if test "${PATH_SEPARATOR+set}" != set; then
- PATH_SEPARATOR=:
- (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {
- (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||
- PATH_SEPARATOR=';'
- }
-fi
-
-
-# IFS
-# We need space, tab and new line, in precisely that order. Quoting is
-# there to prevent editors from complaining about space-tab.
-# (If _AS_PATH_WALK were called with IFS unset, it would disable word
-# splitting by setting IFS to empty value.)
-IFS=" "" $as_nl"
-
-# Find who we are. Look in the path if we contain no directory separator.
-as_myself=
-case $0 in #((
- *[\\/]* ) as_myself=$0 ;;
- *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
-for as_dir in $PATH
-do
- IFS=$as_save_IFS
- test -z "$as_dir" && as_dir=.
- test -r "$as_dir/$0" && as_myself=$as_dir/$0 && break
- done
-IFS=$as_save_IFS
-
- ;;
-esac
-# We did not find ourselves, most probably we were run as `sh COMMAND'
-# in which case we are not to be found in the path.
-if test "x$as_myself" = x; then
- as_myself=$0
-fi
-if test ! -f "$as_myself"; then
- $as_echo "$as_myself: error: cannot find myself; rerun with an absolute file name" >&2
- exit 1
-fi
-
-# Unset variables that we do not need and which cause bugs (e.g. in
-# pre-3.0 UWIN ksh). But do not cause bugs in bash 2.01; the "|| exit 1"
-# suppresses any "Segmentation fault" message there. '((' could
-# trigger a bug in pdksh 5.2.14.
-for as_var in BASH_ENV ENV MAIL MAILPATH
-do eval test x\${$as_var+set} = xset \
- && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || :
-done
-PS1='$ '
-PS2='> '
-PS4='+ '
-
-# NLS nuisances.
-LC_ALL=C
-export LC_ALL
-LANGUAGE=C
-export LANGUAGE
-
-# CDPATH.
-(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
-
-
-# as_fn_error STATUS ERROR [LINENO LOG_FD]
-# ----------------------------------------
-# Output "`basename $0`: error: ERROR" to stderr. If LINENO and LOG_FD are
-# provided, also output the error to LOG_FD, referencing LINENO. Then exit the
-# script with STATUS, using 1 if that was 0.
-as_fn_error ()
-{
- as_status=$1; test $as_status -eq 0 && as_status=1
- if test "$4"; then
- as_lineno=${as_lineno-"$3"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
- $as_echo "$as_me:${as_lineno-$LINENO}: error: $2" >&$4
- fi
- $as_echo "$as_me: error: $2" >&2
- as_fn_exit $as_status
-} # as_fn_error
-
-
-# as_fn_set_status STATUS
-# -----------------------
-# Set $? to STATUS, without forking.
-as_fn_set_status ()
-{
- return $1
-} # as_fn_set_status
-
-# as_fn_exit STATUS
-# -----------------
-# Exit the shell with STATUS, even in a "trap 0" or "set -e" context.
-as_fn_exit ()
-{
- set +e
- as_fn_set_status $1
- exit $1
-} # as_fn_exit
-
-# as_fn_unset VAR
-# ---------------
-# Portably unset VAR.
-as_fn_unset ()
-{
- { eval $1=; unset $1;}
-}
-as_unset=as_fn_unset
-# as_fn_append VAR VALUE
-# ----------------------
-# Append the text in VALUE to the end of the definition contained in VAR. Take
-# advantage of any shell optimizations that allow amortized linear growth over
-# repeated appends, instead of the typical quadratic growth present in naive
-# implementations.
-if (eval "as_var=1; as_var+=2; test x\$as_var = x12") 2>/dev/null; then :
- eval 'as_fn_append ()
- {
- eval $1+=\$2
- }'
-else
- as_fn_append ()
- {
- eval $1=\$$1\$2
- }
-fi # as_fn_append
-
-# as_fn_arith ARG...
-# ------------------
-# Perform arithmetic evaluation on the ARGs, and store the result in the
-# global $as_val. Take advantage of shells that can avoid forks. The arguments
-# must be portable across $(()) and expr.
-if (eval "test \$(( 1 + 1 )) = 2") 2>/dev/null; then :
- eval 'as_fn_arith ()
- {
- as_val=$(( $* ))
- }'
-else
- as_fn_arith ()
- {
- as_val=`expr "$@" || test $? -eq 1`
- }
-fi # as_fn_arith
-
-
-if expr a : '\(a\)' >/dev/null 2>&1 &&
- test "X`expr 00001 : '.*\(...\)'`" = X001; then
- as_expr=expr
-else
- as_expr=false
-fi
-
-if (basename -- /) >/dev/null 2>&1 && test "X`basename -- / 2>&1`" = "X/"; then
- as_basename=basename
-else
- as_basename=false
-fi
-
-if (as_dir=`dirname -- /` && test "X$as_dir" = X/) >/dev/null 2>&1; then
- as_dirname=dirname
-else
- as_dirname=false
-fi
-
-as_me=`$as_basename -- "$0" ||
-$as_expr X/"$0" : '.*/\([^/][^/]*\)/*$' \| \
- X"$0" : 'X\(//\)$' \| \
- X"$0" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X/"$0" |
- sed '/^.*\/\([^/][^/]*\)\/*$/{
- s//\1/
- q
- }
- /^X\/\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\/\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
-
-# Avoid depending upon Character Ranges.
-as_cr_letters='abcdefghijklmnopqrstuvwxyz'
-as_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-as_cr_Letters=$as_cr_letters$as_cr_LETTERS
-as_cr_digits='0123456789'
-as_cr_alnum=$as_cr_Letters$as_cr_digits
-
-ECHO_C= ECHO_N= ECHO_T=
-case `echo -n x` in #(((((
--n*)
- case `echo 'xy\c'` in
- *c*) ECHO_T=' ';; # ECHO_T is single tab character.
- xy) ECHO_C='\c';;
- *) echo `echo ksh88 bug on AIX 6.1` > /dev/null
- ECHO_T=' ';;
- esac;;
-*)
- ECHO_N='-n';;
-esac
-
-rm -f conf$$ conf$$.exe conf$$.file
-if test -d conf$$.dir; then
- rm -f conf$$.dir/conf$$.file
-else
- rm -f conf$$.dir
- mkdir conf$$.dir 2>/dev/null
-fi
-if (echo >conf$$.file) 2>/dev/null; then
- if ln -s conf$$.file conf$$ 2>/dev/null; then
- as_ln_s='ln -s'
- # ... but there are two gotchas:
- # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.
- # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.
- # In both cases, we have to default to `cp -p'.
- ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||
- as_ln_s='cp -p'
- elif ln conf$$.file conf$$ 2>/dev/null; then
- as_ln_s=ln
- else
- as_ln_s='cp -p'
- fi
-else
- as_ln_s='cp -p'
-fi
-rm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file
-rmdir conf$$.dir 2>/dev/null
-
-
-# as_fn_mkdir_p
-# -------------
-# Create "$as_dir" as a directory, including parents if necessary.
-as_fn_mkdir_p ()
-{
-
- case $as_dir in #(
- -*) as_dir=./$as_dir;;
- esac
- test -d "$as_dir" || eval $as_mkdir_p || {
- as_dirs=
- while :; do
- case $as_dir in #(
- *\'*) as_qdir=`$as_echo "$as_dir" | sed "s/'/'\\\\\\\\''/g"`;; #'(
- *) as_qdir=$as_dir;;
- esac
- as_dirs="'$as_qdir' $as_dirs"
- as_dir=`$as_dirname -- "$as_dir" ||
-$as_expr X"$as_dir" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
- X"$as_dir" : 'X\(//\)[^/]' \| \
- X"$as_dir" : 'X\(//\)$' \| \
- X"$as_dir" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$as_dir" |
- sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
- s//\1/
- q
- }
- /^X\(\/\/\)[^/].*/{
- s//\1/
- q
- }
- /^X\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
- test -d "$as_dir" && break
- done
- test -z "$as_dirs" || eval "mkdir $as_dirs"
- } || test -d "$as_dir" || as_fn_error $? "cannot create directory $as_dir"
-
-
-} # as_fn_mkdir_p
-if mkdir -p . 2>/dev/null; then
- as_mkdir_p='mkdir -p "$as_dir"'
-else
- test -d ./-p && rmdir ./-p
- as_mkdir_p=false
-fi
-
-if test -x / >/dev/null 2>&1; then
- as_test_x='test -x'
-else
- if ls -dL / >/dev/null 2>&1; then
- as_ls_L_option=L
- else
- as_ls_L_option=
- fi
- as_test_x='
- eval sh -c '\''
- if test -d "$1"; then
- test -d "$1/.";
- else
- case $1 in #(
- -*)set "./$1";;
- esac;
- case `ls -ld'$as_ls_L_option' "$1" 2>/dev/null` in #((
- ???[sx]*):;;*)false;;esac;fi
- '\'' sh
- '
-fi
-as_executable_p=$as_test_x
-
-# Sed expression to map a string onto a valid CPP name.
-as_tr_cpp="eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'"
-
-# Sed expression to map a string onto a valid variable name.
-as_tr_sh="eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'"
-
-
-exec 6>&1
-## ----------------------------------- ##
-## Main body of $CONFIG_STATUS script. ##
-## ----------------------------------- ##
-_ASEOF
-test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-# Save the log message, to keep $0 and so on meaningful, and to
-# report actual input values of CONFIG_FILES etc. instead of their
-# values after options handling.
-ac_log="
-This file was extended by jim $as_me 0.71, which was
-generated by GNU Autoconf 2.68. Invocation command line was
-
- CONFIG_FILES = $CONFIG_FILES
- CONFIG_HEADERS = $CONFIG_HEADERS
- CONFIG_LINKS = $CONFIG_LINKS
- CONFIG_COMMANDS = $CONFIG_COMMANDS
- $ $0 $@
-
-on `(hostname || uname -n) 2>/dev/null | sed 1q`
-"
-
-_ACEOF
-
-case $ac_config_files in *"
-"*) set x $ac_config_files; shift; ac_config_files=$*;;
-esac
-
-case $ac_config_headers in *"
-"*) set x $ac_config_headers; shift; ac_config_headers=$*;;
-esac
-
-
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-# Files that config.status was made for.
-config_files="$ac_config_files"
-config_headers="$ac_config_headers"
-
-_ACEOF
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-ac_cs_usage="\
-\`$as_me' instantiates files and other configuration actions
-from templates according to the current configuration. Unless the files
-and actions are specified as TAGs, all are instantiated by default.
-
-Usage: $0 [OPTION]... [TAG]...
-
- -h, --help print this help, then exit
- -V, --version print version number and configuration settings, then exit
- --config print configuration, then exit
- -q, --quiet, --silent
- do not print progress messages
- -d, --debug don't remove temporary files
- --recheck update $as_me by reconfiguring in the same conditions
- --file=FILE[:TEMPLATE]
- instantiate the configuration file FILE
- --header=FILE[:TEMPLATE]
- instantiate the configuration header FILE
-
-Configuration files:
-$config_files
-
-Configuration headers:
-$config_headers
-
-Report bugs to <steveb@workware.net.au>."
-
-_ACEOF
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
-ac_cs_version="\\
-jim config.status 0.71
-configured by $0, generated by GNU Autoconf 2.68,
- with options \\"\$ac_cs_config\\"
-
-Copyright (C) 2010 Free Software Foundation, Inc.
-This config.status script is free software; the Free Software Foundation
-gives unlimited permission to copy, distribute and modify it."
-
-ac_pwd='$ac_pwd'
-srcdir='$srcdir'
-test -n "\$AWK" || AWK=awk
-_ACEOF
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-# The default lists apply if the user does not specify any file.
-ac_need_defaults=:
-while test $# != 0
-do
- case $1 in
- --*=?*)
- ac_option=`expr "X$1" : 'X\([^=]*\)='`
- ac_optarg=`expr "X$1" : 'X[^=]*=\(.*\)'`
- ac_shift=:
- ;;
- --*=)
- ac_option=`expr "X$1" : 'X\([^=]*\)='`
- ac_optarg=
- ac_shift=:
- ;;
- *)
- ac_option=$1
- ac_optarg=$2
- ac_shift=shift
- ;;
- esac
-
- case $ac_option in
- # Handling of the options.
- -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
- ac_cs_recheck=: ;;
- --version | --versio | --versi | --vers | --ver | --ve | --v | -V )
- $as_echo "$ac_cs_version"; exit ;;
- --config | --confi | --conf | --con | --co | --c )
- $as_echo "$ac_cs_config"; exit ;;
- --debug | --debu | --deb | --de | --d | -d )
- debug=: ;;
- --file | --fil | --fi | --f )
- $ac_shift
- case $ac_optarg in
- *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;;
- '') as_fn_error $? "missing file argument" ;;
- esac
- as_fn_append CONFIG_FILES " '$ac_optarg'"
- ac_need_defaults=false;;
- --header | --heade | --head | --hea )
- $ac_shift
- case $ac_optarg in
- *\'*) ac_optarg=`$as_echo "$ac_optarg" | sed "s/'/'\\\\\\\\''/g"` ;;
- esac
- as_fn_append CONFIG_HEADERS " '$ac_optarg'"
- ac_need_defaults=false;;
- --he | --h)
- # Conflict between --help and --header
- as_fn_error $? "ambiguous option: \`$1'
-Try \`$0 --help' for more information.";;
- --help | --hel | -h )
- $as_echo "$ac_cs_usage"; exit ;;
- -q | -quiet | --quiet | --quie | --qui | --qu | --q \
- | -silent | --silent | --silen | --sile | --sil | --si | --s)
- ac_cs_silent=: ;;
-
- # This is an error.
- -*) as_fn_error $? "unrecognized option: \`$1'
-Try \`$0 --help' for more information." ;;
-
- *) as_fn_append ac_config_targets " $1"
- ac_need_defaults=false ;;
-
- esac
- shift
-done
-
-ac_configure_extra_args=
-
-if $ac_cs_silent; then
- exec 6>/dev/null
- ac_configure_extra_args="$ac_configure_extra_args --silent"
-fi
-
-_ACEOF
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-if \$ac_cs_recheck; then
- set X '$SHELL' '$0' $ac_configure_args \$ac_configure_extra_args --no-create --no-recursion
- shift
- \$as_echo "running CONFIG_SHELL=$SHELL \$*" >&6
- CONFIG_SHELL='$SHELL'
- export CONFIG_SHELL
- exec "\$@"
-fi
-
-_ACEOF
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-exec 5>>config.log
-{
- echo
- sed 'h;s/./-/g;s/^.../## /;s/...$/ ##/;p;x;p;x' <<_ASBOX
-## Running $as_me. ##
-_ASBOX
- $as_echo "$ac_log"
-} >&5
-
-_ACEOF
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-_ACEOF
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-
-# Handling of arguments.
-for ac_config_target in $ac_config_targets
-do
- case $ac_config_target in
- "jimautoconf.h") CONFIG_HEADERS="$CONFIG_HEADERS jimautoconf.h" ;;
- "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
- "jim-config.h") CONFIG_FILES="$CONFIG_FILES jim-config.h" ;;
-
- *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
- esac
-done
-
-
-# If the user did not use the arguments to specify the items to instantiate,
-# then the envvar interface is used. Set only those that are not.
-# We use the long form for the default assignment because of an extremely
-# bizarre bug on SunOS 4.1.3.
-if $ac_need_defaults; then
- test "${CONFIG_FILES+set}" = set || CONFIG_FILES=$config_files
- test "${CONFIG_HEADERS+set}" = set || CONFIG_HEADERS=$config_headers
-fi
-
-# Have a temporary directory for convenience. Make it in the build tree
-# simply because there is no reason against having it here, and in addition,
-# creating and moving files from /tmp can sometimes cause problems.
-# Hook for its removal unless debugging.
-# Note that there is a small window in which the directory will not be cleaned:
-# after its creation but before its name has been assigned to `$tmp'.
-$debug ||
-{
- tmp= ac_tmp=
- trap 'exit_status=$?
- : "${ac_tmp:=$tmp}"
- { test ! -d "$ac_tmp" || rm -fr "$ac_tmp"; } && exit $exit_status
-' 0
- trap 'as_fn_exit 1' 1 2 13 15
-}
-# Create a (secure) tmp directory for tmp files.
-
-{
- tmp=`(umask 077 && mktemp -d "./confXXXXXX") 2>/dev/null` &&
- test -d "$tmp"
-} ||
-{
- tmp=./conf$$-$RANDOM
- (umask 077 && mkdir "$tmp")
-} || as_fn_error $? "cannot create a temporary directory in ." "$LINENO" 5
-ac_tmp=$tmp
-
-# Set up the scripts for CONFIG_FILES section.
-# No need to generate them if there are no CONFIG_FILES.
-# This happens for instance with `./config.status config.h'.
-if test -n "$CONFIG_FILES"; then
-
-
-ac_cr=`echo X | tr X '\015'`
-# On cygwin, bash can eat \r inside `` if the user requested igncr.
-# But we know of no other shell where ac_cr would be empty at this
-# point, so we can use a bashism as a fallback.
-if test "x$ac_cr" = x; then
- eval ac_cr=\$\'\\r\'
-fi
-ac_cs_awk_cr=`$AWK 'BEGIN { print "a\rb" }' </dev/null 2>/dev/null`
-if test "$ac_cs_awk_cr" = "a${ac_cr}b"; then
- ac_cs_awk_cr='\\r'
-else
- ac_cs_awk_cr=$ac_cr
-fi
-
-echo 'BEGIN {' >"$ac_tmp/subs1.awk" &&
-_ACEOF
-
-
-{
- echo "cat >conf$$subs.awk <<_ACEOF" &&
- echo "$ac_subst_vars" | sed 's/.*/&!$&$ac_delim/' &&
- echo "_ACEOF"
-} >conf$$subs.sh ||
- as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
-ac_delim_num=`echo "$ac_subst_vars" | grep -c '^'`
-ac_delim='%!_!# '
-for ac_last_try in false false false false false :; do
- . ./conf$$subs.sh ||
- as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
-
- ac_delim_n=`sed -n "s/.*$ac_delim\$/X/p" conf$$subs.awk | grep -c X`
- if test $ac_delim_n = $ac_delim_num; then
- break
- elif $ac_last_try; then
- as_fn_error $? "could not make $CONFIG_STATUS" "$LINENO" 5
- else
- ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
- fi
-done
-rm -f conf$$subs.sh
-
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-cat >>"\$ac_tmp/subs1.awk" <<\\_ACAWK &&
-_ACEOF
-sed -n '
-h
-s/^/S["/; s/!.*/"]=/
-p
-g
-s/^[^!]*!//
-:repl
-t repl
-s/'"$ac_delim"'$//
-t delim
-:nl
-h
-s/\(.\{148\}\)..*/\1/
-t more1
-s/["\\]/\\&/g; s/^/"/; s/$/\\n"\\/
-p
-n
-b repl
-:more1
-s/["\\]/\\&/g; s/^/"/; s/$/"\\/
-p
-g
-s/.\{148\}//
-t nl
-:delim
-h
-s/\(.\{148\}\)..*/\1/
-t more2
-s/["\\]/\\&/g; s/^/"/; s/$/"/
-p
-b
-:more2
-s/["\\]/\\&/g; s/^/"/; s/$/"\\/
-p
-g
-s/.\{148\}//
-t delim
-' <conf$$subs.awk | sed '
-/^[^""]/{
- N
- s/\n//
-}
-' >>$CONFIG_STATUS || ac_write_fail=1
-rm -f conf$$subs.awk
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-_ACAWK
-cat >>"\$ac_tmp/subs1.awk" <<_ACAWK &&
- for (key in S) S_is_set[key] = 1
- FS = ""
-
-}
-{
- line = $ 0
- nfields = split(line, field, "@")
- substed = 0
- len = length(field[1])
- for (i = 2; i < nfields; i++) {
- key = field[i]
- keylen = length(key)
- if (S_is_set[key]) {
- value = S[key]
- line = substr(line, 1, len) "" value "" substr(line, len + keylen + 3)
- len += length(value) + length(field[++i])
- substed = 1
- } else
- len += 1 + keylen
- }
-
- print line
-}
-
-_ACAWK
-_ACEOF
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-if sed "s/$ac_cr//" < /dev/null > /dev/null 2>&1; then
- sed "s/$ac_cr\$//; s/$ac_cr/$ac_cs_awk_cr/g"
-else
- cat
-fi < "$ac_tmp/subs1.awk" > "$ac_tmp/subs.awk" \
- || as_fn_error $? "could not setup config files machinery" "$LINENO" 5
-_ACEOF
-
-# VPATH may cause trouble with some makes, so we remove sole $(srcdir),
-# ${srcdir} and @srcdir@ entries from VPATH if srcdir is ".", strip leading and
-# trailing colons and then remove the whole line if VPATH becomes empty
-# (actually we leave an empty line to preserve line numbers).
-if test "x$srcdir" = x.; then
- ac_vpsub='/^[ ]*VPATH[ ]*=[ ]*/{
-h
-s///
-s/^/:/
-s/[ ]*$/:/
-s/:\$(srcdir):/:/g
-s/:\${srcdir}:/:/g
-s/:@srcdir@:/:/g
-s/^:*//
-s/:*$//
-x
-s/\(=[ ]*\).*/\1/
-G
-s/\n//
-s/^[^=]*=[ ]*$//
-}'
-fi
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-fi # test -n "$CONFIG_FILES"
-
-# Set up the scripts for CONFIG_HEADERS section.
-# No need to generate them if there are no CONFIG_HEADERS.
-# This happens for instance with `./config.status Makefile'.
-if test -n "$CONFIG_HEADERS"; then
-cat >"$ac_tmp/defines.awk" <<\_ACAWK ||
-BEGIN {
-_ACEOF
-
-# Transform confdefs.h into an awk script `defines.awk', embedded as
-# here-document in config.status, that substitutes the proper values into
-# config.h.in to produce config.h.
-
-# Create a delimiter string that does not exist in confdefs.h, to ease
-# handling of long lines.
-ac_delim='%!_!# '
-for ac_last_try in false false :; do
- ac_tt=`sed -n "/$ac_delim/p" confdefs.h`
- if test -z "$ac_tt"; then
- break
- elif $ac_last_try; then
- as_fn_error $? "could not make $CONFIG_HEADERS" "$LINENO" 5
- else
- ac_delim="$ac_delim!$ac_delim _$ac_delim!! "
- fi
-done
-
-# For the awk script, D is an array of macro values keyed by name,
-# likewise P contains macro parameters if any. Preserve backslash
-# newline sequences.
-
-ac_word_re=[_$as_cr_Letters][_$as_cr_alnum]*
-sed -n '
-s/.\{148\}/&'"$ac_delim"'/g
-t rset
-:rset
-s/^[ ]*#[ ]*define[ ][ ]*/ /
-t def
-d
-:def
-s/\\$//
-t bsnl
-s/["\\]/\\&/g
-s/^ \('"$ac_word_re"'\)\(([^()]*)\)[ ]*\(.*\)/P["\1"]="\2"\
-D["\1"]=" \3"/p
-s/^ \('"$ac_word_re"'\)[ ]*\(.*\)/D["\1"]=" \2"/p
-d
-:bsnl
-s/["\\]/\\&/g
-s/^ \('"$ac_word_re"'\)\(([^()]*)\)[ ]*\(.*\)/P["\1"]="\2"\
-D["\1"]=" \3\\\\\\n"\\/p
-t cont
-s/^ \('"$ac_word_re"'\)[ ]*\(.*\)/D["\1"]=" \2\\\\\\n"\\/p
-t cont
-d
-:cont
-n
-s/.\{148\}/&'"$ac_delim"'/g
-t clear
-:clear
-s/\\$//
-t bsnlc
-s/["\\]/\\&/g; s/^/"/; s/$/"/p
-d
-:bsnlc
-s/["\\]/\\&/g; s/^/"/; s/$/\\\\\\n"\\/p
-b cont
-' <confdefs.h | sed '
-s/'"$ac_delim"'/"\\\
-"/g' >>$CONFIG_STATUS || ac_write_fail=1
-
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
- for (key in D) D_is_set[key] = 1
- FS = ""
-}
-/^[\t ]*#[\t ]*(define|undef)[\t ]+$ac_word_re([\t (]|\$)/ {
- line = \$ 0
- split(line, arg, " ")
- if (arg[1] == "#") {
- defundef = arg[2]
- mac1 = arg[3]
- } else {
- defundef = substr(arg[1], 2)
- mac1 = arg[2]
- }
- split(mac1, mac2, "(") #)
- macro = mac2[1]
- prefix = substr(line, 1, index(line, defundef) - 1)
- if (D_is_set[macro]) {
- # Preserve the white space surrounding the "#".
- print prefix "define", macro P[macro] D[macro]
- next
- } else {
- # Replace #undef with comments. This is necessary, for example,
- # in the case of _POSIX_SOURCE, which is predefined and required
- # on some systems where configure will not decide to define it.
- if (defundef == "undef") {
- print "/*", prefix defundef, macro, "*/"
- next
- }
- }
-}
-{ print }
-_ACAWK
-_ACEOF
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
- as_fn_error $? "could not setup config headers machinery" "$LINENO" 5
-fi # test -n "$CONFIG_HEADERS"
-
-
-eval set X " :F $CONFIG_FILES :H $CONFIG_HEADERS "
-shift
-for ac_tag
-do
- case $ac_tag in
- :[FHLC]) ac_mode=$ac_tag; continue;;
- esac
- case $ac_mode$ac_tag in
- :[FHL]*:*);;
- :L* | :C*:*) as_fn_error $? "invalid tag \`$ac_tag'" "$LINENO" 5;;
- :[FH]-) ac_tag=-:-;;
- :[FH]*) ac_tag=$ac_tag:$ac_tag.in;;
- esac
- ac_save_IFS=$IFS
- IFS=:
- set x $ac_tag
- IFS=$ac_save_IFS
- shift
- ac_file=$1
- shift
-
- case $ac_mode in
- :L) ac_source=$1;;
- :[FH])
- ac_file_inputs=
- for ac_f
- do
- case $ac_f in
- -) ac_f="$ac_tmp/stdin";;
- *) # Look for the file first in the build tree, then in the source tree
- # (if the path is not absolute). The absolute path cannot be DOS-style,
- # because $ac_f cannot contain `:'.
- test -f "$ac_f" ||
- case $ac_f in
- [\\/$]*) false;;
- *) test -f "$srcdir/$ac_f" && ac_f="$srcdir/$ac_f";;
- esac ||
- as_fn_error 1 "cannot find input file: \`$ac_f'" "$LINENO" 5;;
- esac
- case $ac_f in *\'*) ac_f=`$as_echo "$ac_f" | sed "s/'/'\\\\\\\\''/g"`;; esac
- as_fn_append ac_file_inputs " '$ac_f'"
- done
-
- # Let's still pretend it is `configure' which instantiates (i.e., don't
- # use $as_me), people would be surprised to read:
- # /* config.h. Generated by config.status. */
- configure_input='Generated from '`
- $as_echo "$*" | sed 's|^[^:]*/||;s|:[^:]*/|, |g'
- `' by configure.'
- if test x"$ac_file" != x-; then
- configure_input="$ac_file. $configure_input"
- { $as_echo "$as_me:${as_lineno-$LINENO}: creating $ac_file" >&5
-$as_echo "$as_me: creating $ac_file" >&6;}
- fi
- # Neutralize special characters interpreted by sed in replacement strings.
- case $configure_input in #(
- *\&* | *\|* | *\\* )
- ac_sed_conf_input=`$as_echo "$configure_input" |
- sed 's/[\\\\&|]/\\\\&/g'`;; #(
- *) ac_sed_conf_input=$configure_input;;
- esac
-
- case $ac_tag in
- *:-:* | *:-) cat >"$ac_tmp/stdin" \
- || as_fn_error $? "could not create $ac_file" "$LINENO" 5 ;;
- esac
- ;;
- esac
-
- ac_dir=`$as_dirname -- "$ac_file" ||
-$as_expr X"$ac_file" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
- X"$ac_file" : 'X\(//\)[^/]' \| \
- X"$ac_file" : 'X\(//\)$' \| \
- X"$ac_file" : 'X\(/\)' \| . 2>/dev/null ||
-$as_echo X"$ac_file" |
- sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
- s//\1/
- q
- }
- /^X\(\/\/\)[^/].*/{
- s//\1/
- q
- }
- /^X\(\/\/\)$/{
- s//\1/
- q
- }
- /^X\(\/\).*/{
- s//\1/
- q
- }
- s/.*/./; q'`
- as_dir="$ac_dir"; as_fn_mkdir_p
- ac_builddir=.
-
-case "$ac_dir" in
-.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;;
-*)
- ac_dir_suffix=/`$as_echo "$ac_dir" | sed 's|^\.[\\/]||'`
- # A ".." for each directory in $ac_dir_suffix.
- ac_top_builddir_sub=`$as_echo "$ac_dir_suffix" | sed 's|/[^\\/]*|/..|g;s|/||'`
- case $ac_top_builddir_sub in
- "") ac_top_builddir_sub=. ac_top_build_prefix= ;;
- *) ac_top_build_prefix=$ac_top_builddir_sub/ ;;
- esac ;;
-esac
-ac_abs_top_builddir=$ac_pwd
-ac_abs_builddir=$ac_pwd$ac_dir_suffix
-# for backward compatibility:
-ac_top_builddir=$ac_top_build_prefix
-
-case $srcdir in
- .) # We are building in place.
- ac_srcdir=.
- ac_top_srcdir=$ac_top_builddir_sub
- ac_abs_top_srcdir=$ac_pwd ;;
- [\\/]* | ?:[\\/]* ) # Absolute name.
- ac_srcdir=$srcdir$ac_dir_suffix;
- ac_top_srcdir=$srcdir
- ac_abs_top_srcdir=$srcdir ;;
- *) # Relative name.
- ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix
- ac_top_srcdir=$ac_top_build_prefix$srcdir
- ac_abs_top_srcdir=$ac_pwd/$srcdir ;;
-esac
-ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix
-
-
- case $ac_mode in
- :F)
- #
- # CONFIG_FILE
- #
-
-_ACEOF
-
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-# If the template does not know about datarootdir, expand it.
-# FIXME: This hack should be removed a few years after 2.60.
-ac_datarootdir_hack=; ac_datarootdir_seen=
-ac_sed_dataroot='
-/datarootdir/ {
- p
- q
-}
-/@datadir@/p
-/@docdir@/p
-/@infodir@/p
-/@localedir@/p
-/@mandir@/p'
-case `eval "sed -n \"\$ac_sed_dataroot\" $ac_file_inputs"` in
-*datarootdir*) ac_datarootdir_seen=yes;;
-*@datadir@*|*@docdir@*|*@infodir@*|*@localedir@*|*@mandir@*)
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting" >&5
-$as_echo "$as_me: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting" >&2;}
-_ACEOF
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
- ac_datarootdir_hack='
- s&@datadir@&$datadir&g
- s&@docdir@&$docdir&g
- s&@infodir@&$infodir&g
- s&@localedir@&$localedir&g
- s&@mandir@&$mandir&g
- s&\\\${datarootdir}&$datarootdir&g' ;;
-esac
-_ACEOF
-
-# Neutralize VPATH when `$srcdir' = `.'.
-# Shell code in configure.ac might set extrasub.
-# FIXME: do we really want to maintain this feature?
-cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
-ac_sed_extra="$ac_vpsub
-$extrasub
-_ACEOF
-cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
-:t
-/@[a-zA-Z_][a-zA-Z_0-9]*@/!b
-s|@configure_input@|$ac_sed_conf_input|;t t
-s&@top_builddir@&$ac_top_builddir_sub&;t t
-s&@top_build_prefix@&$ac_top_build_prefix&;t t
-s&@srcdir@&$ac_srcdir&;t t
-s&@abs_srcdir@&$ac_abs_srcdir&;t t
-s&@top_srcdir@&$ac_top_srcdir&;t t
-s&@abs_top_srcdir@&$ac_abs_top_srcdir&;t t
-s&@builddir@&$ac_builddir&;t t
-s&@abs_builddir@&$ac_abs_builddir&;t t
-s&@abs_top_builddir@&$ac_abs_top_builddir&;t t
-$ac_datarootdir_hack
-"
-eval sed \"\$ac_sed_extra\" "$ac_file_inputs" | $AWK -f "$ac_tmp/subs.awk" \
- >$ac_tmp/out || as_fn_error $? "could not create $ac_file" "$LINENO" 5
-
-test -z "$ac_datarootdir_hack$ac_datarootdir_seen" &&
- { ac_out=`sed -n '/\${datarootdir}/p' "$ac_tmp/out"`; test -n "$ac_out"; } &&
- { ac_out=`sed -n '/^[ ]*datarootdir[ ]*:*=/p' \
- "$ac_tmp/out"`; test -z "$ac_out"; } &&
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: $ac_file contains a reference to the variable \`datarootdir'
-which seems to be undefined. Please make sure it is defined" >&5
-$as_echo "$as_me: WARNING: $ac_file contains a reference to the variable \`datarootdir'
-which seems to be undefined. Please make sure it is defined" >&2;}
-
- rm -f "$ac_tmp/stdin"
- case $ac_file in
- -) cat "$ac_tmp/out" && rm -f "$ac_tmp/out";;
- *) rm -f "$ac_file" && mv "$ac_tmp/out" "$ac_file";;
- esac \
- || as_fn_error $? "could not create $ac_file" "$LINENO" 5
- ;;
- :H)
- #
- # CONFIG_HEADER
- #
- if test x"$ac_file" != x-; then
- {
- $as_echo "/* $configure_input */" \
- && eval '$AWK -f "$ac_tmp/defines.awk"' "$ac_file_inputs"
- } >"$ac_tmp/config.h" \
- || as_fn_error $? "could not create $ac_file" "$LINENO" 5
- if diff "$ac_file" "$ac_tmp/config.h" >/dev/null 2>&1; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: $ac_file is unchanged" >&5
-$as_echo "$as_me: $ac_file is unchanged" >&6;}
- else
- rm -f "$ac_file"
- mv "$ac_tmp/config.h" "$ac_file" \
- || as_fn_error $? "could not create $ac_file" "$LINENO" 5
- fi
- else
- $as_echo "/* $configure_input */" \
- && eval '$AWK -f "$ac_tmp/defines.awk"' "$ac_file_inputs" \
- || as_fn_error $? "could not create -" "$LINENO" 5
- fi
- ;;
-
-
- esac
-
-done # for ac_tag
-
-
-as_fn_exit 0
-_ACEOF
-ac_clean_files=$ac_clean_files_save
-
-test $ac_write_fail = 0 ||
- as_fn_error $? "write failure creating $CONFIG_STATUS" "$LINENO" 5
-
-
-# configure is writing to config.log, and then calls config.status.
-# config.status does its own redirection, appending to config.log.
-# Unfortunately, on DOS this fails, as config.log is still kept open
-# by configure, so config.status won't be able to write to it; its
-# output is simply discarded. So we exec the FD to /dev/null,
-# effectively closing config.log, so it can be properly (re)opened and
-# appended to by config.status. When coming back to configure, we
-# need to make the FD available again.
-if test "$no_create" != yes; then
- ac_cs_success=:
- ac_config_status_args=
- test "$silent" = yes &&
- ac_config_status_args="$ac_config_status_args --quiet"
- exec 5>/dev/null
- $SHELL $CONFIG_STATUS $ac_config_status_args || ac_cs_success=false
- exec 5>>config.log
- # Use ||, not &&, to avoid exiting from the if with $? = 1, which
- # would make configure fail if this is the last instruction.
- $ac_cs_success || as_fn_exit 1
-fi
-if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then
- { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5
-$as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
-fi
-
+#!/bin/sh
+dir="$(dirname "$0")/autosetup"
+WRAPPER="$0" exec $("$dir/find-tclsh" || echo false) "$dir/autosetup" "$@"
diff --git a/install-sh b/install-sh
deleted file mode 100755
index e843669..0000000
--- a/install-sh
+++ /dev/null
@@ -1,250 +0,0 @@
-#!/bin/sh
-#
-# install - install a program, script, or datafile
-# This comes from X11R5 (mit/util/scripts/install.sh).
-#
-# Copyright 1991 by the Massachusetts Institute of Technology
-#
-# Permission to use, copy, modify, distribute, and sell this software and its
-# documentation for any purpose is hereby granted without fee, provided that
-# the above copyright notice appear in all copies and that both that
-# copyright notice and this permission notice appear in supporting
-# documentation, and that the name of M.I.T. not be used in advertising or
-# publicity pertaining to distribution of the software without specific,
-# written prior permission. M.I.T. makes no representations about the
-# suitability of this software for any purpose. It is provided "as is"
-# without express or implied warranty.
-#
-# Calling this script install-sh is preferred over install.sh, to prevent
-# `make' implicit rules from creating a file called install from it
-# when there is no Makefile.
-#
-# This script is compatible with the BSD install script, but was written
-# from scratch. It can only install one file at a time, a restriction
-# shared with many OS's install programs.
-
-
-# set DOITPROG to echo to test this script
-
-# Don't use :- since 4.3BSD and earlier shells don't like it.
-doit="${DOITPROG-}"
-
-
-# put in absolute paths if you don't have them in your path; or use env. vars.
-
-mvprog="${MVPROG-mv}"
-cpprog="${CPPROG-cp}"
-chmodprog="${CHMODPROG-chmod}"
-chownprog="${CHOWNPROG-chown}"
-chgrpprog="${CHGRPPROG-chgrp}"
-stripprog="${STRIPPROG-strip}"
-rmprog="${RMPROG-rm}"
-mkdirprog="${MKDIRPROG-mkdir}"
-
-transformbasename=""
-transform_arg=""
-instcmd="$mvprog"
-chmodcmd="$chmodprog 0755"
-chowncmd=""
-chgrpcmd=""
-stripcmd=""
-rmcmd="$rmprog -f"
-mvcmd="$mvprog"
-src=""
-dst=""
-dir_arg=""
-
-while [ x"$1" != x ]; do
- case $1 in
- -c) instcmd="$cpprog"
- shift
- continue;;
-
- -d) dir_arg=true
- shift
- continue;;
-
- -m) chmodcmd="$chmodprog $2"
- shift
- shift
- continue;;
-
- -o) chowncmd="$chownprog $2"
- shift
- shift
- continue;;
-
- -g) chgrpcmd="$chgrpprog $2"
- shift
- shift
- continue;;
-
- -s) stripcmd="$stripprog"
- shift
- continue;;
-
- -t=*) transformarg=`echo $1 | sed 's/-t=//'`
- shift
- continue;;
-
- -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
- shift
- continue;;
-
- *) if [ x"$src" = x ]
- then
- src=$1
- else
- # this colon is to work around a 386BSD /bin/sh bug
- :
- dst=$1
- fi
- shift
- continue;;
- esac
-done
-
-if [ x"$src" = x ]
-then
- echo "install: no input file specified"
- exit 1
-else
- true
-fi
-
-if [ x"$dir_arg" != x ]; then
- dst=$src
- src=""
-
- if [ -d $dst ]; then
- instcmd=:
- else
- instcmd=mkdir
- fi
-else
-
-# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
-# might cause directories to be created, which would be especially bad
-# if $src (and thus $dsttmp) contains '*'.
-
- if [ -f $src -o -d $src ]
- then
- true
- else
- echo "install: $src does not exist"
- exit 1
- fi
-
- if [ x"$dst" = x ]
- then
- echo "install: no destination specified"
- exit 1
- else
- true
- fi
-
-# If destination is a directory, append the input filename; if your system
-# does not like double slashes in filenames, you may need to add some logic
-
- if [ -d $dst ]
- then
- dst="$dst"/`basename $src`
- else
- true
- fi
-fi
-
-## this sed command emulates the dirname command
-dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
-
-# Make sure that the destination directory exists.
-# this part is taken from Noah Friedman's mkinstalldirs script
-
-# Skip lots of stat calls in the usual case.
-if [ ! -d "$dstdir" ]; then
-defaultIFS='
-'
-IFS="${IFS-${defaultIFS}}"
-
-oIFS="${IFS}"
-# Some sh's can't handle IFS=/ for some reason.
-IFS='%'
-set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
-IFS="${oIFS}"
-
-pathcomp=''
-
-while [ $# -ne 0 ] ; do
- pathcomp="${pathcomp}${1}"
- shift
-
- if [ ! -d "${pathcomp}" ] ;
- then
- $mkdirprog "${pathcomp}"
- else
- true
- fi
-
- pathcomp="${pathcomp}/"
-done
-fi
-
-if [ x"$dir_arg" != x ]
-then
- $doit $instcmd $dst &&
-
- if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
- if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
- if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
- if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
-else
-
-# If we're going to rename the final executable, determine the name now.
-
- if [ x"$transformarg" = x ]
- then
- dstfile=`basename $dst`
- else
- dstfile=`basename $dst $transformbasename |
- sed $transformarg`$transformbasename
- fi
-
-# don't allow the sed command to completely eliminate the filename
-
- if [ x"$dstfile" = x ]
- then
- dstfile=`basename $dst`
- else
- true
- fi
-
-# Make a temp file name in the proper directory.
-
- dsttmp=$dstdir/#inst.$$#
-
-# Move or copy the file name to the temp name
-
- $doit $instcmd $src $dsttmp &&
-
- trap "rm -f ${dsttmp}" 0 &&
-
-# and set any options; do chmod last to preserve setuid bits
-
-# If any of these fail, we abort the whole thing. If we want to
-# ignore errors from any of these, just make sure not to ignore
-# errors from the above "$doit $instcmd $src $dsttmp" command.
-
- if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
- if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
- if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
- if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
-
-# Now rename the file to the real destination.
-
- $doit $rmcmd -f $dstdir/$dstfile &&
- $doit $mvcmd $dsttmp $dstdir/$dstfile
-
-fi &&
-
-
-exit 0
diff --git a/jim-syslog.c b/jim-syslog.c
index 66b3e5e..189309a 100644
--- a/jim-syslog.c
+++ b/jim-syslog.c
@@ -6,11 +6,12 @@
* Slightly modified by Steve Bennett <steveb@snapgear.com>
* Ported to Jim by Steve Bennett <steveb@workware.net.au>
*/
-#include "jim.h"
-#include "jimautoconf.h"
#include <syslog.h>
#include <string.h>
+#include "jim.h"
+#include "jimautoconf.h"
+
typedef struct
{
int logOpened;
diff --git a/jim-win32compat.h b/jim-win32compat.h
index 0db8439..22a1502 100644
--- a/jim-win32compat.h
+++ b/jim-win32compat.h
@@ -17,8 +17,6 @@
#define rand_r(S) ((void)(S), rand())
#define localtime_r(T,TM) ((void)(TM), localtime(T))
-#define HAVE_DLOPEN_COMPAT
-
#define RTLD_LAZY 0
void *dlopen(const char *path, int mode);
int dlclose(void *handle);
diff --git a/jim.c b/jim.c
index 8ddc7a4..526522c 100644
--- a/jim.c
+++ b/jim.c
@@ -69,6 +69,17 @@
/* For INFINITY, even if math functions are not enabled */
#include <math.h>
+/* For the no-autoconf case */
+#ifndef TCL_LIBRARY
+#define TCL_LIBRARY "."
+#endif
+#ifndef TCL_PLATFORM_OS
+#define TCL_PLATFORM_OS "unknown"
+#endif
+#ifndef TCL_PLATFORM_PLATFORM
+#define TCL_PLATFORM_PLATFORM "unknown"
+#endif
+
/*#define DEBUG_SHOW_SCRIPT*/
/*#define DEBUG_SHOW_SCRIPT_TOKENS*/
/*#define DEBUG_SHOW_SUBST*/
diff --git a/jim.h b/jim.h
index f19bf1a..6411be0 100644
--- a/jim.h
+++ b/jim.h
@@ -178,14 +178,6 @@ extern "C" {
#define JIM_LIBPATH "auto_path"
#define JIM_INTERACTIVE "tcl_interactive"
-#ifndef TCL_PLATFORM_OS
-#define TCL_PLATFORM_OS "unknown"
-#endif
-#ifndef TCL_PLATFORM_PLATFORM
-#define TCL_PLATFORM_PLATFORM "unknown"
-#endif
-
-
/* -----------------------------------------------------------------------------
* Stack
* ---------------------------------------------------------------------------*/
diff --git a/jimregexp.h b/jimregexp.h
index 5d7dc94..336e20e 100644
--- a/jimregexp.h
+++ b/jimregexp.h
@@ -1,6 +1,10 @@
#ifndef JIMREGEXP_H
#define JIMREGEXP_H
+#ifndef _JIMAUTOCONF_H
+#error Need jimautoconf.h
+#endif
+
#if defined(HAVE_REGCOMP) && !defined(JIM_REGEXP)
/* Use POSIX regex */
#include <regex.h>
diff --git a/linenoise.c b/linenoise.c
index 7cb964e..e0cc7a8 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -110,7 +110,7 @@
#include <unistd.h>
#include "linenoise.h"
-#include "linenoise.h"
+#include "jim-config.h"
#ifdef JIM_UTF8
#define USE_UTF8
#endif
diff --git a/linenoise.h b/linenoise.h
index 1efa12f..4ac7f18 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -37,6 +37,9 @@
#ifndef __LINENOISE_H
#define __LINENOISE_H
+/* Currently never enable completion */
+#define NO_COMPLETION
+
#ifndef NO_COMPLETION
typedef struct linenoiseCompletions {
size_t len;