wmi-1.3.16 from opsview.com
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
The lib/appweb directory is a partial import of the appweb ejs and esp
|
||||
code from http://www.appwebserver.org/
|
||||
|
||||
Many thanks to the mbedthis people, and especially Michael O'Brien for
|
||||
his assistance in getting this code integrated into Samba4.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
AC_CHECK_HEADERS(math.h)
|
||||
@@ -0,0 +1,31 @@
|
||||
#######################
|
||||
# Start SUBSYSTEM MPR
|
||||
[SUBSYSTEM::MPR]
|
||||
OBJ_FILES = \
|
||||
mpr/miniMpr.o \
|
||||
mpr/var.o
|
||||
# End SUBSYSTEM MPR
|
||||
#######################
|
||||
|
||||
|
||||
#######################
|
||||
# Start SUBSYSTEM EJS
|
||||
[SUBSYSTEM::EJS]
|
||||
OBJ_FILES = \
|
||||
ejs/ejsLib.o \
|
||||
ejs/ejsLex.o \
|
||||
ejs/ejsParser.o \
|
||||
ejs/ejsProcs.o
|
||||
PUBLIC_DEPENDENCIES = MPR
|
||||
# End SUBSYSTEM EJS
|
||||
#######################
|
||||
|
||||
#######################
|
||||
# Start SUBSYSTEM ESP
|
||||
[SUBSYSTEM::ESP]
|
||||
OBJ_FILES = \
|
||||
esp/esp.o \
|
||||
esp/espProcs.o
|
||||
PUBLIC_DEPENDENCIES = EJS
|
||||
# End SUBSYSTEM ESP
|
||||
#######################
|
||||
@@ -0,0 +1,153 @@
|
||||
#
|
||||
# .bashrc -- Login shell startup script for windows using Mbedthis winTools
|
||||
#
|
||||
# Copyright (c) Mbedthis Software, 2003-2005. All Rights Reserved.
|
||||
#
|
||||
|
||||
TERM=ansi
|
||||
#
|
||||
# Set the desired .NET Framework
|
||||
#
|
||||
# FRAMEWORK=v1.0.3705
|
||||
FRAMEWORK=v1.1.4322
|
||||
# FRAMEWORK=v2.0.40607
|
||||
|
||||
#
|
||||
# Set the desired Microsoft C Compiler version
|
||||
#
|
||||
# PREFERRED_CC=VS2005
|
||||
# PREFERRED_CC=VS2003
|
||||
# PREFERRED_CC=VS.NET
|
||||
PREFERRED_CC=VS6
|
||||
|
||||
#
|
||||
# Set to 1 if VXWORKS support is required
|
||||
#
|
||||
# VXWORKS=1
|
||||
|
||||
HOME=`pwd`
|
||||
if [ ! -x winTools -o ! -x winTools/cygpath.exe ]
|
||||
then
|
||||
echo "Can't find build tools. Install build tools in $HOME/winTools"
|
||||
fi
|
||||
|
||||
ROOT=`winTools/cygpath -u $HOMEDRIVE`
|
||||
: ${ROOT:=C:/}
|
||||
APPWEB_PATH="${HOME}/bin/DEBUG:${HOME}/bin/RELEASE:${HOME}/bin:${HOME}/winTools"
|
||||
CDPATH=".:${HOME}:${HOME}/http:${HOME}/http/modules:${HOME}/packages"
|
||||
PS1="$ "
|
||||
|
||||
export CDPATH INCLUDE LIB LIBPATH PATH PS1 TERM
|
||||
|
||||
echo -e "\n\n###################################################"
|
||||
echo "Mbedthis AppWeb, Cygwin build tools."
|
||||
echo "Using compiler: $PREFERRED_CC, .NET framework: $FRAMEWORK"
|
||||
echo -e "###################################################"
|
||||
|
||||
################################################################################
|
||||
|
||||
#
|
||||
# Setup for Visual Studio and SDK
|
||||
#
|
||||
if [ $PREFERRED_CC == "VS2005" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET 2005 defines.
|
||||
#
|
||||
CYNET="${ROOT}/Program Files/Microsoft Visual Studio 8"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio 8"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC/BIN:$CYNET/VC/VCPackages:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin:$CYNET/SDK/v2.0/bin:`cygpath -W`/Microsoft.NET/Framework/v2.0.40607:$CYNET/SDK/v2.0/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC/ATLMFC/INCLUDE;$DOSNET/VC/INCLUDE;$DOSNET/VC/PlatformSDK/include;$DOSNET/SDK/v2.0/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC/ATLMFC/LIB;$DOSNET/VC/LIB;$DOSNET/VC/PlatformSDK/lib;$DOSNET/SDK/v2.0/lib;$LIB"
|
||||
LIBPATH=c:/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK
|
||||
fi
|
||||
|
||||
if [ $PREFERRED_CC == "VS2003" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET 2003 defines.
|
||||
#
|
||||
CYNET="${ROOT}/Program Files/Microsoft Visual Studio .NET 2003"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio .NET 2003"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC7/BIN:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin/prerelease:$CYNET/Common7/Tools/bin:$CYNET/FrameworkSDK/bin:${ROOT}/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$CYNET/SDK/v1.1/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC7/ATLMFC/INCLUDE;$DOSNET/VC7/INCLUDE;$DOSNET/VC7/PlatformSDK/include/prerelease;$DOSNET/VC7/PlatformSDK/include;$DOSNET/FrameworkSDK/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC7/ATLMFC/LIB;$DOSNET/VC7/LIB;$DOSNET/VC7/PlatformSDK/lib/prerelease;$DOSNET/VC7/PlatformSDK/lib;$DOSNET/FrameworkSDK/lib;$LIB"
|
||||
fi
|
||||
|
||||
|
||||
if [ $PREFERRED_CC == "VS.NET" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET defines.
|
||||
#
|
||||
CYNET="${ROOT}/Program Files/Microsoft Visual Studio .NET"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio .NET"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC7/BIN:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin/prerelease:$CYNET/Common7/Tools/bin:$CYNET/FrameworkSDK/bin:${ROOT}/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$CYNET/SDK/v1.0/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC7/ATLMFC/INCLUDE;$DOSNET/VC7/INCLUDE;$DOSNET/VC7/PlatformSDK/include/prerelease;$DOSNET/VC7/PlatformSDK/include;$DOSNET/FrameworkSDK/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC7/ATLMFC/LIB;$DOSNET/VC7/LIB;$DOSNET/VC7/PlatformSDK/lib/prerelease;$DOSNET/VC7/PlatformSDK/lib;$DOSNET/FrameworkSDK/lib;$LIB"
|
||||
fi
|
||||
|
||||
|
||||
if [ $PREFERRED_CC == "VS6" ]
|
||||
then
|
||||
# Visual Studio 6 defines.
|
||||
#
|
||||
CYNET="${ROOT}/Program Files/Microsoft Visual Studio"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common/MSDev98/bin:$CYNET/VC98/BIN:$CYNET/Common/IDE:$CYNET/Common/Tools/WinNT:$CYNET/Common/Tools:$PATH"
|
||||
INCLUDE="$DOSNET/VC98/ATLMFC/INCLUDE;$DOSNET/VC98/INCLUDE;$DOSNET/VC98/MFC/INCLUDE;$INCLUDE"
|
||||
LIB="$DOSNET/VC98/LIB;$DOSNET/VC98/MFC/LIB;$LIB"
|
||||
fi
|
||||
|
||||
if [ $VXWORKS ]
|
||||
then
|
||||
#
|
||||
# Required by VxWorks
|
||||
#
|
||||
WIND_BASE=C:/tornado
|
||||
WIND_HOST_TYPE=x86-win32
|
||||
WIND_REGISTRY=coalsack
|
||||
WIND_LMHOST=coalsack
|
||||
BLD_VX_HOST=i386-wrs-vxworks
|
||||
VX_TOOLS=`cygpath $WIND_BASE`/host/$WIND_HOST_TYPE
|
||||
export WIND_BASE WIND_HOST_TYPE WIND_REGISTRY WIND_LMHOST BLD_VX_HOST
|
||||
|
||||
#
|
||||
# Use cygwin make and tools by preference
|
||||
#
|
||||
PATH="$APPWEB_PATH:$VX_TOOLS/bin:$PATH"
|
||||
fi
|
||||
|
||||
#
|
||||
# Make required directories for CYGWIN
|
||||
#
|
||||
if [ ! -x /bin/bash.exe ]
|
||||
then
|
||||
DIR=`cygpath -w "$HOME/winTools"`
|
||||
echo -e "\nCreating /bin"
|
||||
echo Mounting \"${DIR}\" as /bin
|
||||
mount -f -b "$DIR" /bin
|
||||
fi
|
||||
|
||||
if [ ! -x /tmp ]
|
||||
then
|
||||
mkdir -p tmp
|
||||
DIR=`cygpath -w "$HOME/tmp"`
|
||||
echo -e "\nCreating /tmp"
|
||||
echo Mounting \"${DIR}\" as /tmp
|
||||
mount -f -b "$DIR" /tmp
|
||||
fi
|
||||
echo
|
||||
|
||||
################################################################################
|
||||
#
|
||||
# Do a bit of validation
|
||||
#
|
||||
type cl 2>/dev/null >/dev/null
|
||||
if [ $? -ne 0 ]
|
||||
then
|
||||
echo "Can't find compiler: cl. Check WIN/bashrc settings for PATH"
|
||||
fi
|
||||
set -o vi
|
||||
|
||||
################################################################################
|
||||
@@ -0,0 +1 @@
|
||||
set ts=4 sw=4
|
||||
@@ -0,0 +1,57 @@
|
||||
*.class
|
||||
*.dll
|
||||
*.exe
|
||||
*.exp
|
||||
*.jar
|
||||
*.lib
|
||||
*.lnk
|
||||
*.map
|
||||
*.new
|
||||
*.old
|
||||
*.pdb
|
||||
*.res
|
||||
*.sln
|
||||
*.suo
|
||||
*.sym
|
||||
*.tmp
|
||||
*.sav
|
||||
.ICEauthority
|
||||
.bash_history
|
||||
.changes
|
||||
.cvsignore
|
||||
.esd_auth
|
||||
.fonts.cache-1
|
||||
.viminfo
|
||||
make.dep
|
||||
thumbs.db
|
||||
vc70.pdb
|
||||
*.ncb
|
||||
*.opt
|
||||
*.idb
|
||||
*.pch
|
||||
*.dep
|
||||
*.aps
|
||||
all
|
||||
tmp
|
||||
make.vars
|
||||
sh.vars
|
||||
config.make
|
||||
config.h
|
||||
config.sh
|
||||
configure
|
||||
make.os
|
||||
.firstBuild
|
||||
.viminfo
|
||||
.ssh
|
||||
_viminfo
|
||||
buildConfig.h
|
||||
buildConfig.make
|
||||
buildConfig.sh
|
||||
.ccache
|
||||
appWeb.kdev*
|
||||
subversion
|
||||
.vimrc
|
||||
.subversion
|
||||
.ignore
|
||||
njs
|
||||
msvc
|
||||
@@ -0,0 +1,218 @@
|
||||
#
|
||||
# .loginrc -- Michael's login shell startup script (used only for Windows)
|
||||
#
|
||||
# NOTE: this should not be distributed with releases.
|
||||
#
|
||||
# Copyright (c) Mbedthis Software, 2003-2005. All Rights Reserved.
|
||||
#
|
||||
HOME=`pwd`
|
||||
|
||||
TERM=ansi
|
||||
CYROOT=/cygdrive/c
|
||||
JDK="/cygdrive/c/program files/java/jdk1.5.0_07/bin"
|
||||
H=$CYROOT
|
||||
R=usr/svn/appWeb/main
|
||||
CDPATH=".:${H}/usr/svn/appWeb:${H}/${R}/http:${H}/${R}/http/modules:${H}/${R}:${H}/usr/svn/appWeb/releases:${H}/usr/svn/packages:${H}/usr/svn/bling/player/trunk:${H}/usr/svn/bling/player/trunk/src:${H}/usr/svn/bling/player/trunk/appdir:${H}/usr/svn:${H}:${H}/usr/svn:${H}/usr"
|
||||
APPWEB_PATH="${H}/${R}/bin/DEBUG:${H}/${R}/bin/RELEASE:${H}/${R}/bin:${H}/usr/bin"
|
||||
PATH="${H}/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:$PATH:${H}/tcl/bin:${JDK}"
|
||||
|
||||
CLASSPATH="c:/usr/svn/j/ggperf"
|
||||
PS1='`cygpath -m $PWD`> '
|
||||
SVN_EDITOR=C:/cygwin/bin/vim.exe
|
||||
|
||||
umask 022
|
||||
|
||||
export TERM CDPATH INCLUDE LIB LIBPATH PATH PS1 SVN_EDITOR MSCL CLASSPATH
|
||||
|
||||
################################################################################
|
||||
#
|
||||
# Set the dev environment PATH and other critical environment variables
|
||||
#
|
||||
|
||||
# export BLD_ALTERNATE_CONFIG=WIN/buildConfig
|
||||
|
||||
#
|
||||
# Desired .NET Framework
|
||||
#
|
||||
# FRAMEWORK=v1.0.3705
|
||||
# FRAMEWORK=v1.1.4322
|
||||
FRAMEWORK=v2.0.50727
|
||||
|
||||
#
|
||||
# Desired C Compiler
|
||||
#
|
||||
# MSCL=VS2005
|
||||
# MSCL=VS2003
|
||||
# MSCL=VS.NET
|
||||
MSCL=VS6
|
||||
VXWORKS=1
|
||||
|
||||
echo "Using compiler: $MSCL, .NET framework: $FRAMEWORK"
|
||||
|
||||
#
|
||||
# Setup for Visual Studio and SDK
|
||||
#
|
||||
if [ $MSCL == "VS2005" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET 2005 defines.
|
||||
#
|
||||
CYNET="$H/Program Files/Microsoft Visual Studio 8"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio 8"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC/BIN:$CYNET/VC/VCPackages:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin:$CYNET/SDK/v2.0/bin:$CYROOT/WINDOWS/Microsoft.NET/Framework/v2.0.40607:$CYNET/SDK/v2.0/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC/ATLMFC/INCLUDE;$DOSNET/VC/INCLUDE;$DOSNET/VC/PlatformSDK/include;$DOSNET/SDK/v2.0/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC/ATLMFC/LIB;$DOSNET/VC/LIB;$DOSNET/VC/PlatformSDK/lib;$DOSNET/SDK/v2.0/lib;$LIB"
|
||||
LIBPATH=c:/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK
|
||||
|
||||
# MOB -- old
|
||||
# PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC/BIN:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin/prerelease:$CYNET/Common7/Tools/bin:$CYNET/FrameworkSDK/bin:$H/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$PATH"
|
||||
# INCLUDE="$DOSNET/VC/ATLMFC/INCLUDE;$DOSNET/VC/INCLUDE;$DOSNET/VC/PlatformSDK/include/prerelease;$DOSNET/VC/PlatformSDK/include;$DOSNET/FrameworkSDK/include;$INCLUDE"
|
||||
# LIB="$DOSNET/VC/ATLMFC/LIB;$DOSNET/VC/LIB;$DOSNET/VC/PlatformSDK/lib/prerelease;$DOSNET/VC/PlatformSDK/lib;$DOSNET/FrameworkSDK/lib;$LIB"
|
||||
fi
|
||||
|
||||
if [ $MSCL == "VS2003" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET 2003 defines.
|
||||
#
|
||||
CYNET="$H/Program Files/Microsoft Visual Studio .NET 2003"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio .NET 2003"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC7/BIN:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin/prerelease:$CYNET/Common7/Tools/bin:$CYNET/FrameworkSDK/bin:$H/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$CYNET/SDK/v1.1/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC7/ATLMFC/INCLUDE;$DOSNET/VC7/INCLUDE;$DOSNET/VC7/PlatformSDK/include/prerelease;$DOSNET/VC7/PlatformSDK/include;$DOSNET/FrameworkSDK/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC7/ATLMFC/LIB;$DOSNET/VC7/LIB;$DOSNET/VC7/PlatformSDK/lib/prerelease;$DOSNET/VC7/PlatformSDK/lib;$DOSNET/FrameworkSDK/lib;$LIB"
|
||||
fi
|
||||
|
||||
|
||||
if [ $MSCL == "VS.NET" ]
|
||||
then
|
||||
#
|
||||
# Visual Studio .NET defines.
|
||||
#
|
||||
CYNET="$H/Program Files/Microsoft Visual Studio .NET"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio .NET"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common7/IDE:$CYNET/VC7/BIN:$CYNET/Common7/Tools:$CYNET/Common7/Tools/bin/prerelease:$CYNET/Common7/Tools/bin:$CYNET/FrameworkSDK/bin:$H/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$CYNET/SDK/v1.0/bin:$PATH"
|
||||
INCLUDE="$DOSNET/VC7/ATLMFC/INCLUDE;$DOSNET/VC7/INCLUDE;$DOSNET/VC7/PlatformSDK/include/prerelease;$DOSNET/VC7/PlatformSDK/include;$DOSNET/FrameworkSDK/include;$INCLUDE"
|
||||
LIB="$DOSNET/VC7/ATLMFC/LIB;$DOSNET/VC7/LIB;$DOSNET/VC7/PlatformSDK/lib/prerelease;$DOSNET/VC7/PlatformSDK/lib;$DOSNET/FrameworkSDK/lib;$LIB"
|
||||
fi
|
||||
|
||||
|
||||
if [ $MSCL == "VS6" ]
|
||||
then
|
||||
# Visual Studio 6 defines.
|
||||
#
|
||||
CYNET="$H/Program Files/Microsoft Visual Studio"
|
||||
DOSNET="C:/Program Files/Microsoft Visual Studio"
|
||||
PATH="$APPWEB_PATH:$CYNET/Common/MSDev98/bin:$CYNET/VC98/BIN:$CYNET/Common/IDE:$CYNET/Common/Tools/WinNT:$CYNET/Common/Tools:$PATH"
|
||||
# OLD PATH="$APPWEB_PATH:$CYNET/Common/IDE:$CYNET/VC98/BIN:$CYNET/Common/MSDev98/bin:$CYNET/Common/Tools:$CYNET/Common/Tools/bin/prerelease:$CYNET/Common/Tools/bin:$CYNET/FrameworkSDK/bin:$H/WINDOWS/Microsoft.NET/Framework/$FRAMEWORK:$PATH"
|
||||
INCLUDE="$DOSNET/VC98/ATLMFC/INCLUDE;$DOSNET/VC98/INCLUDE;$DOSNET/VC98/MFC/INCLUDE;$INCLUDE"
|
||||
LIB="$DOSNET/VC98/LIB;$DOSNET/VC98/MFC/LIB;$LIB"
|
||||
fi
|
||||
|
||||
if [ $VXWORKS ]
|
||||
then
|
||||
#
|
||||
# Required by VxWorks
|
||||
#
|
||||
WIND_BASE=C:/tornado
|
||||
WIND_HOST_TYPE=x86-win32
|
||||
WIND_REGISTRY=coalsack
|
||||
WIND_LMHOST=coalsack
|
||||
BLD_VX_HOST=i386-wrs-vxworks
|
||||
export WIND_BASE WIND_HOST_TYPE WIND_REGISTRY WIND_LMHOST BLD_VX_HOST
|
||||
|
||||
VX_TOOLS=`cygpath $WIND_BASE`/host/$WIND_HOST_TYPE
|
||||
#
|
||||
# Use cygwin make and tools by preference
|
||||
#
|
||||
PATH="$APPWEB_PATH:$PATH:$VX_TOOLS/bin"
|
||||
fi
|
||||
|
||||
#
|
||||
# Make required directories for CYGWIN
|
||||
#
|
||||
if [ ! -x /bin/bash.exe ]
|
||||
then
|
||||
DIR=`cygpath -w "$HOME/winTools"`
|
||||
echo -e "\nCreating /bin"
|
||||
echo Mounting \"${DIR}\" as /bin
|
||||
mount -f -b "$DIR" /bin
|
||||
fi
|
||||
|
||||
if [ ! -x /tmp ]
|
||||
then
|
||||
mkdir -p tmp
|
||||
DIR=`cygpath -w "$HOME/tmp"`
|
||||
echo -e "\nCreating /tmp"
|
||||
echo Mounting \"${DIR}\" as /tmp
|
||||
mount -f -b "$DIR" /tmp
|
||||
fi
|
||||
echo
|
||||
|
||||
|
||||
################################################################################
|
||||
#
|
||||
# Do a bit of validation (MOB -- extend)
|
||||
#
|
||||
type cl 2>/dev/null >/dev/null
|
||||
if [ $? -ne 0 ]
|
||||
then
|
||||
echo "Can't find compiler: cl. Check WIN/bashrc settings for PATH"
|
||||
fi
|
||||
|
||||
################################################################################
|
||||
#
|
||||
# Some convenient functions
|
||||
#
|
||||
pvi () {
|
||||
|
||||
pattern=$1
|
||||
shift
|
||||
files=$*
|
||||
if [ -z "${files}" ]
|
||||
then
|
||||
files='*.c *.cpp *.h Makefile *.html *.aspx *.cs'
|
||||
fi
|
||||
vi -c "/${pattern}" $(grep -l "${pattern}" ${files})
|
||||
}
|
||||
|
||||
################################################################################
|
||||
|
||||
g() {
|
||||
pattern=$1
|
||||
shift
|
||||
files=$*
|
||||
if [ -z "${files}" ]
|
||||
then
|
||||
files=`echo *.c *.cpp *.h Makefile *.html *.aspx *.cs`
|
||||
fi
|
||||
eval grep "${pattern}" ${files}
|
||||
}
|
||||
|
||||
################################################################################
|
||||
|
||||
usedvi() {
|
||||
pvi $1 $HOME/mr/*.c $HOME/mr/*.h $HOME/mr/WIN/*.c $HOME/lib/*/*.c
|
||||
}
|
||||
|
||||
################################################################################
|
||||
|
||||
alias ls='ls -CF $*'
|
||||
alias lc='ls -CF $*'
|
||||
alias lr='ls -R $*'
|
||||
alias xwin='startxwin.sh'
|
||||
alias htmlview='"C:/Program Files/Internet Explorer/iexplore.exe" $*'
|
||||
set -o vi
|
||||
|
||||
if [ `uname -o` = "Cygwin" ]
|
||||
then
|
||||
alias vim='"C:/Program Files/vim/vim64/vim.exe" $*'
|
||||
alias gvim='"C:/Program Files/vim/vim64/gvim.exe" $*'
|
||||
fi
|
||||
|
||||
brew() {
|
||||
"C:/Program Files/BREW SDK v2.1.3/Bin/BREW_Emulator.exe"
|
||||
}
|
||||
|
||||
js() {
|
||||
cscript /nologo $*
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
ejs
|
||||
future
|
||||
@@ -0,0 +1,61 @@
|
||||
#
|
||||
# Makefile for Embedded Javascript (EJS)
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
#
|
||||
# Ejs may be linked into shared handlers so we must build the objects both
|
||||
# shared and static if --shared was specified to configure.
|
||||
#
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
PRE_DIRS := classes system db
|
||||
MAKE_IFLAGS := -I../mpr -I../exml
|
||||
|
||||
include make.dep
|
||||
|
||||
ifeq ($(BLD_PRODUCT),ejs)
|
||||
POST_DIRS := package
|
||||
endif
|
||||
|
||||
ifeq ($(BLD_FEATURE_TEST),1)
|
||||
POST_DIRS += test
|
||||
endif
|
||||
|
||||
ifeq ($(BLD_FEATURE_EJS_DB),1)
|
||||
LIBS += sqlite
|
||||
endif
|
||||
|
||||
TARGETS += $(BLD_BIN_DIR)/libejs$(BLD_LIB)
|
||||
TARGETS += $(BLD_BIN_DIR)/ejs$(BLD_EXE)
|
||||
|
||||
ifeq ($(BLD_FEATURE_EJS),1)
|
||||
compileExtra: $(TARGETS)
|
||||
endif
|
||||
|
||||
$(BLD_BIN_DIR)/libejs$(BLD_LIB): files \
|
||||
$(shell BLD_OBJ=$(BLD_OBJ) \; BLD_OBJ_DIR=$(BLD_OBJ_DIR) \; \
|
||||
eval echo `cat files`)
|
||||
@bld --library $(BLD_BIN_DIR)/libejs \
|
||||
--objectsDir $(BLD_OBJ_DIR) --objectList files \
|
||||
--libs "exml mpr $(LIBS)"
|
||||
|
||||
$(BLD_BIN_DIR)/ejs$(BLD_EXE): $(BLD_BIN_DIR)/libejs$(BLD_LIB) \
|
||||
$(BLD_BIN_DIR)/libmpr$(BLD_LIB) \
|
||||
$(BLD_BIN_DIR)/libejs$(BLD_LIB) $(FILES)
|
||||
@bld --executable $(BLD_BIN_DIR)/ejs$(BLD_EXE) \
|
||||
--rpath "$(BLD_PREFIX)/bin" \
|
||||
--preferStatic --smartLibs "ejs exml mpr $(LIBS)" \
|
||||
--objectsDir $(BLD_OBJ_DIR) \
|
||||
--objects "$(BLD_OBJ_DIR)/ejsCmd$(BLD_OBJ)"
|
||||
|
||||
cleanExtra:
|
||||
@echo "rm -f $(TARGETS)" | $(BLDOUT)
|
||||
@rm -f $(TARGETS)
|
||||
@rm -f $(BLD_BIN_DIR)/libejs.*
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1 @@
|
||||
.updated
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# Makefile to build the EJS Classes
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I.. -I../../mpr -I../../exml
|
||||
|
||||
include make.dep
|
||||
|
||||
compileExtra: .updated
|
||||
|
||||
.updated: $(FILES)
|
||||
@touch .updated
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* @file ejsArray.c
|
||||
* @brief Array class
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int ejsDefineArrayClass(Ejs *ep)
|
||||
{
|
||||
if (ejsDefineClass(ep, "Array", "Object", ejsArrayConstructor) == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Routine to create the base array type
|
||||
*/
|
||||
|
||||
EjsVar *ejsCreateArrayInternal(EJS_LOC_DEC(ep, loc), int size)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
EjsVar *obj, *vp;
|
||||
|
||||
/* MOB -- need to supply hash size -- max(size, 503)); */
|
||||
|
||||
obj = ejsCreateSimpleObjInternal(EJS_LOC_PASS(ep, loc), "Array");
|
||||
if (obj == 0) {
|
||||
mprAssert(0);
|
||||
return obj;
|
||||
}
|
||||
obj->isArray = 1;
|
||||
|
||||
/* MOB -- call constructor here and replace this code */
|
||||
|
||||
pp = ejsSetPropertyToInteger(ep, obj, "length", size);
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
|
||||
vp = ejsGetVarPtr(pp);
|
||||
vp->isArrayLength = 1;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
EjsVar *ejsAddArrayElt(Ejs *ep, EjsVar *op, EjsVar *element,
|
||||
EjsCopyDepth copyDepth)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
EjsVar *vp;
|
||||
char idx[16];
|
||||
int length;
|
||||
|
||||
mprAssert(op->isArray);
|
||||
|
||||
length = ejsGetPropertyAsInteger(ep, op, "length");
|
||||
|
||||
mprItoa(idx, sizeof(idx), length);
|
||||
pp = ejsCreateProperty(ep, op, idx);
|
||||
vp = ejsGetVarPtr(pp);
|
||||
|
||||
ejsWriteVar(ep, vp, element, copyDepth);
|
||||
|
||||
ejsSetPropertyToInteger(ep, op, "length", length + 1);
|
||||
|
||||
return vp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
int ejsArrayConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
EjsVar *vp;
|
||||
char idx[16];
|
||||
int i, max;
|
||||
|
||||
thisObj->isArray = 1;
|
||||
max = 0;
|
||||
|
||||
if (argc > 0) {
|
||||
if (argc == 1 && ejsVarIsNumber(argv[0])) {
|
||||
/*
|
||||
* x = new Array(size);
|
||||
*/
|
||||
max = (int) ejsVarToInteger(argv[0]);
|
||||
|
||||
} else {
|
||||
/*
|
||||
* x = new Array(element0, element1, ..., elementN):
|
||||
*/
|
||||
max = argc;
|
||||
for (i = 0; i < max; i++) {
|
||||
mprItoa(idx, sizeof(idx), i);
|
||||
pp = ejsCreateSimpleProperty(ep, thisObj, idx);
|
||||
vp = ejsGetVarPtr(pp);
|
||||
ejsWriteVar(ep, vp, argv[i], EJS_SHALLOW_COPY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pp = ejsCreateSimpleProperty(ep, thisObj, "length");
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
vp = ejsGetVarPtr(pp);
|
||||
ejsWriteVarAsInteger(ep, vp, max);
|
||||
vp->isArrayLength = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsArrayDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* @file ejsStndClasses.c
|
||||
* @brief EJS support methods
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS && 0
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Date constructor
|
||||
|
||||
*
|
||||
* Date();
|
||||
* Date(milliseconds);
|
||||
* Date(dateString);
|
||||
* Date(year, month, date);
|
||||
* Date(year, month, date, hour, minute, second);
|
||||
*/
|
||||
|
||||
int ejsDateConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int load(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
const char *fileName;
|
||||
XmlState *parser;
|
||||
Exml *xp;
|
||||
MprFile *file;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Bad args. Usage: load(fileName);");
|
||||
return -1;
|
||||
}
|
||||
fileName = argv[0]->string;
|
||||
|
||||
/* FUTURE -- not romable
|
||||
Need rom code in MPR not MprServices
|
||||
*/
|
||||
file = mprOpen(ep, fileName, O_RDONLY, 0664);
|
||||
if (file == 0) {
|
||||
ejsError(ep, EJS_IO_ERROR, "Can't open: %s", fileName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
xp = initParser(ep, thisObj, fileName);
|
||||
parser = exmlGetParseArg(xp);
|
||||
|
||||
exmlSetInputStream(xp, readFileData, (void*) file);
|
||||
|
||||
if (exmlParse(xp) < 0) {
|
||||
if (! ejsGotException(ep)) {
|
||||
ejsError(ep, EJS_IO_ERROR, "Can't parse XML file: %s\nDetails %s",
|
||||
fileName, exmlGetErrorMsg(xp));
|
||||
}
|
||||
termParser(xp);
|
||||
mprClose(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetReturnValue(ep, parser->nodeStack[0].obj);
|
||||
|
||||
termParser(xp);
|
||||
mprClose(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineDateClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *dateClass;
|
||||
|
||||
dateClass = ejsDefineClass(ep, "Date", "Object", ejsDateConstructor);
|
||||
if (dateClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
ejsDefineCMethod(ep, dateClass, "getDate", xxxProc, EJS_NO_LOCAL);
|
||||
|
||||
/* Returns "Friday" or 4 ? */
|
||||
ejsDefineCMethod(ep, dateClass, "getDay", xxxProc, EJS_NO_LOCAL);
|
||||
|
||||
ejsDefineCMethod(ep, dateClass, "getMonth", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getFullYear", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getYear", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getHours", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getMinutes", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getSeconds", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getMilliseconds", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getTime", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "getTimeZoneOffset", xxxProc, EJS_NO_LOCAL);
|
||||
|
||||
ejsDefineCMethod(ep, dateClass, "parse", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setDate", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setMonth", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setFullYear", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setYear", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setMinutes", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setSeconds", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setMilliseconds", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "setTime", xxxProc, EJS_NO_LOCAL);
|
||||
|
||||
ejsDefineCMethod(ep, dateClass, "toString", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "toGMTString", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "toUTCString", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "toLocaleString", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "UTC", xxxProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, dateClass, "valueOf", xxxProc, EJS_NO_LOCAL);
|
||||
/*
|
||||
UTC: getUTCDate, getUTCDay, getUTCMonth, getUTCFullYear, getUTCHours,
|
||||
getUTCMinutes, getUTCSeconds, getUTCMilliseconds
|
||||
setUTCDate, setUTCDay, setUTCMonth, setUTCFullYear, setUTCHours,
|
||||
setUTCMinutes, setUTCSeconds, setUTCMilliseconds
|
||||
*/
|
||||
|
||||
return ejsObjHasErrors(dateClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
Time is since 1970/01/01 GMT
|
||||
|
||||
Normal: Fri Feb 10 2006 05:06:44 GMT-0800 (Pacific Standard Time)
|
||||
UTC: Sat, 11 Feb 2006 05:06:44 GMT
|
||||
|
||||
// Using without New
|
||||
|
||||
println(Date());
|
||||
|
||||
var myDate = new Date();
|
||||
myDate.setFullYear(2010, 0, 14);
|
||||
|
||||
var today = new Date();
|
||||
|
||||
if (myDate > today) {
|
||||
} else {
|
||||
}
|
||||
|
||||
|
||||
X=Date() should be equivalent to X=(new Date()).toString()
|
||||
|
||||
*/
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsStndClassesDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* @file ejsError.c
|
||||
* @brief Error class
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/************************************ Code ************************************/
|
||||
/*
|
||||
* Parse the args and return the message. Convert non-string args using
|
||||
* .toString.
|
||||
*/
|
||||
|
||||
static char *getMessage(Ejs *ep, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc == 0) {
|
||||
return "";
|
||||
|
||||
} else if (argc == 1) {
|
||||
if (! ejsVarIsString(argv[0])) {
|
||||
if (ejsRunMethod(ep, argv[0], "toString", 0) < 0) {
|
||||
return 0;
|
||||
}
|
||||
return ep->result->string;
|
||||
|
||||
} else {
|
||||
return argv[0]->string;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Don't call ejsError here or it will go recursive. */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Error Constructor and also used for constructor for sub classes.
|
||||
*
|
||||
* Usage: new Error([message])
|
||||
*/
|
||||
|
||||
int ejsErrorCons(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *msg, *stack;
|
||||
|
||||
msg = getMessage(ep, argc, argv);
|
||||
if (msg == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetPropertyToString(ep, thisObj, "name", ejsGetBaseClassName(thisObj));
|
||||
ejsSetPropertyToString(ep, thisObj, "message", msg);
|
||||
|
||||
ejsSetPropertyToUndefined(ep, thisObj, "stack");
|
||||
|
||||
stack = ejsFormatStack(ep);
|
||||
if (stack) {
|
||||
ejsSetPropertyToString(ep, thisObj, "stack", stack);
|
||||
mprFree(stack);
|
||||
}
|
||||
|
||||
if (ejsObjHasErrors(thisObj)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineErrorClasses(Ejs *ep)
|
||||
{
|
||||
if (ejsDefineClass(ep, "Error", "Object", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "AssertError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "EvalError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "InternalError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "IOError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "MemoryError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "RangeError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "ReferenceError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "SyntaxError", "Error", ejsErrorCons) == 0 ||
|
||||
ejsDefineClass(ep, "TypeError", "Error", ejsErrorCons) == 0) {
|
||||
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsErrorDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,588 @@
|
||||
/*
|
||||
* @file ejsObject.c
|
||||
* @brief Object class
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
/*
|
||||
* Support routines
|
||||
*/
|
||||
|
||||
static void formatVar(Ejs *ep, MprBuf *bp, EjsVar *vp);
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Routine to create an object of the desired class. Class name may
|
||||
* contain "."
|
||||
*
|
||||
* The created object will be a stand-alone class NOT entered into the
|
||||
* properties of any other object. Callers must do this if required. ClassName
|
||||
* may contain "." and is interpreted relative to "obj" if supplied.
|
||||
*
|
||||
* Note: this does not call the constructors for the various objects and base
|
||||
* classes.
|
||||
*/
|
||||
|
||||
EjsVar *ejsCreateSimpleObjInternal(EJS_LOC_DEC(ep, loc), const char *className)
|
||||
{
|
||||
EjsVar *baseClass;
|
||||
|
||||
if (className && *className) {
|
||||
baseClass = ejsGetClass(ep, 0, className);
|
||||
if (baseClass == 0) {
|
||||
mprError(ep, MPR_LOC, "Can't find base class %s", className);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
baseClass = 0;
|
||||
}
|
||||
|
||||
return ejsCreateSimpleObjUsingClassInt(EJS_LOC_PASS(ep, loc),
|
||||
baseClass);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Create an object based upon the specified base class object. It will be a
|
||||
* stand-alone class not entered into the properties of any other object.
|
||||
* Callers must do this if required.
|
||||
*
|
||||
* Note: this does not call the constructors for the various objects and base
|
||||
* classes.
|
||||
*/
|
||||
|
||||
EjsVar *ejsCreateSimpleObjUsingClassInt(EJS_LOC_DEC(ep, loc),
|
||||
EjsVar *baseClass)
|
||||
{
|
||||
EjsVar *vp;
|
||||
|
||||
mprAssert(baseClass);
|
||||
|
||||
if (baseClass == 0) {
|
||||
mprError(ep, MPR_LOC, "Missing base class\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
vp = ejsCreateObjVarInternal(EJS_LOC_PASS(ep, loc));
|
||||
if (vp == 0) {
|
||||
return vp;
|
||||
}
|
||||
|
||||
ejsSetBaseClass(vp, baseClass);
|
||||
|
||||
/*
|
||||
* This makes all internal method accesses faster
|
||||
* NOTE: this code is duplicated in ejsCreateSimpleClass
|
||||
*/
|
||||
mprAssert(vp->objectState);
|
||||
vp->objectState->methods = baseClass->objectState->methods;
|
||||
|
||||
return vp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void ejsSetMethods(Ejs *ep, EjsVar *op)
|
||||
{
|
||||
op->objectState->methods = ep->global->objectState->methods;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Internal Methods ****************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static EjsVar *createObjProperty(Ejs *ep, EjsVar *obj, const char *property)
|
||||
{
|
||||
return ejsGetVarPtr(ejsCreateSimpleProperty(ep, obj, property));
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int deleteObjProperty(Ejs *ep, EjsVar *obj, const char *property)
|
||||
{
|
||||
return ejsDeleteProperty(ep, obj, property);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static EjsVar *getObjProperty(Ejs *ep, EjsVar *obj, const char *property)
|
||||
{
|
||||
return ejsGetVarPtr(ejsGetSimpleProperty(ep, obj, property));
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Set the value of a property. Create if it does not exist
|
||||
*/
|
||||
|
||||
static EjsVar *setObjProperty(Ejs *ep, EjsVar *obj, const char *property,
|
||||
const EjsVar *value)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
EjsVar *vp;
|
||||
|
||||
pp = ejsCreateSimpleProperty(ep, obj, property);
|
||||
if (pp == 0) {
|
||||
mprAssert(pp);
|
||||
return 0;
|
||||
}
|
||||
vp = ejsGetVarPtr(pp);
|
||||
if (ejsWriteVar(ep, vp, value, EJS_SHALLOW_COPY) < 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
return ejsGetVarPtr(pp);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*********************************** Constructors *****************************/
|
||||
/******************************************************************************/
|
||||
#if UNUSED
|
||||
/*
|
||||
* Object constructor. We don't use this for speed. Think very carefully if
|
||||
* you add an object constructor.
|
||||
*/
|
||||
|
||||
int ejsObjectConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/******************************** Visible Methods *****************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static int cloneMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int copyDepth;
|
||||
|
||||
copyDepth = EJS_DEEP_COPY;
|
||||
|
||||
if (argc == 1 && ejsVarToBoolean(argv[0])) {
|
||||
copyDepth = EJS_RECURSIVE_DEEP_COPY;
|
||||
}
|
||||
|
||||
ejsWriteVar(ep, ep->result, thisObj, copyDepth);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int toStringMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprBuf *bp;
|
||||
int saveMaxDepth, saveDepth, saveFlags;
|
||||
|
||||
saveMaxDepth = ep->maxDepth;
|
||||
|
||||
if (argc >= 1) {
|
||||
ep->maxDepth = ejsVarToInteger(argv[0]);
|
||||
} else if (ep->maxDepth == 0) {
|
||||
ep->maxDepth = MAXINT;
|
||||
}
|
||||
|
||||
saveFlags = ep->flags;
|
||||
if (argc >= 2) {
|
||||
if (ejsVarToBoolean(argv[1])) {
|
||||
ep->flags |= EJS_FLAGS_ENUM_HIDDEN;
|
||||
}
|
||||
}
|
||||
if (argc == 3) {
|
||||
if (ejsVarToBoolean(argv[2])) {
|
||||
ep->flags |= EJS_FLAGS_ENUM_BASE;
|
||||
}
|
||||
}
|
||||
|
||||
bp = mprCreateBuf(ep, 0, 0);
|
||||
|
||||
saveDepth = ep->depth;
|
||||
|
||||
formatVar(ep, bp, thisObj);
|
||||
|
||||
ep->depth = saveDepth;
|
||||
ep->maxDepth = saveMaxDepth;
|
||||
|
||||
mprAddNullToBuf(bp);
|
||||
|
||||
ejsWriteVarAsString(ep, ep->result, mprGetBufStart(bp));
|
||||
mprFree(bp);
|
||||
|
||||
ep->flags = saveFlags;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int valueOfMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 0) {
|
||||
mprAssert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (thisObj->type) {
|
||||
default:
|
||||
case EJS_TYPE_UNDEFINED:
|
||||
case EJS_TYPE_NULL:
|
||||
case EJS_TYPE_CMETHOD:
|
||||
case EJS_TYPE_OBJECT:
|
||||
case EJS_TYPE_METHOD:
|
||||
case EJS_TYPE_STRING_CMETHOD:
|
||||
ejsWriteVar(ep, ep->result, thisObj, EJS_SHALLOW_COPY);
|
||||
break;
|
||||
|
||||
case EJS_TYPE_STRING:
|
||||
ejsWriteVarAsInteger(ep, ep->result, atoi(thisObj->string));
|
||||
break;
|
||||
|
||||
case EJS_TYPE_BOOL:
|
||||
case EJS_TYPE_INT:
|
||||
#if BLD_FEATURE_INT64
|
||||
case EJS_TYPE_INT64:
|
||||
#endif
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
case EJS_TYPE_FLOAT:
|
||||
#endif
|
||||
ejsWriteVar(ep, ep->result, thisObj, EJS_SHALLOW_COPY);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int hashGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ejs, (int) thisObj->objectState);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int classGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (thisObj->objectState == 0 || thisObj->objectState->baseClass == 0) {
|
||||
ejsSetReturnValueToString(ejs, "object");
|
||||
} else {
|
||||
ejsSetReturnValueToString(ejs,
|
||||
thisObj->objectState->baseClass->objectState->className);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Format an object. Called recursively to format properties and contained
|
||||
* objects.
|
||||
*/
|
||||
|
||||
static void formatVar(Ejs *ep, MprBuf *bp, EjsVar *vp)
|
||||
{
|
||||
EjsProperty *pp, *first;
|
||||
EjsVar *propVar, *baseClass;
|
||||
char *buf, *value;
|
||||
int i;
|
||||
|
||||
if (vp->type == EJS_TYPE_OBJECT) {
|
||||
if (!vp->objectState->visited) {
|
||||
|
||||
mprPutStringToBuf(bp, vp->isArray ? "[\n" : "{\n");
|
||||
|
||||
ep->depth++;
|
||||
vp->objectState->visited = 1;
|
||||
|
||||
if (ep->depth <= ep->maxDepth) {
|
||||
first = ejsGetFirstProperty(vp, EJS_ENUM_ALL);
|
||||
|
||||
if (ep->flags & EJS_FLAGS_ENUM_BASE) {
|
||||
baseClass = vp->objectState->baseClass;
|
||||
if (baseClass) {
|
||||
for (i = 0; i < ep->depth; i++) {
|
||||
mprPutStringToBuf(bp, " ");
|
||||
}
|
||||
mprPutStringToBuf(bp, baseClass->objectState->objName);
|
||||
mprPutStringToBuf(bp, ": /* Base Class */ ");
|
||||
if (baseClass->objectState == vp->objectState) {
|
||||
value = "this";
|
||||
} else if (ejsRunMethodCmd(ep, baseClass, "toString",
|
||||
"%d", ep->maxDepth) < 0) {
|
||||
value = "[object Object]";
|
||||
} else {
|
||||
mprAssert(ejsVarIsString(ep->result));
|
||||
value = ep->result->string;
|
||||
}
|
||||
mprPutStringToBuf(bp, value);
|
||||
if (first) {
|
||||
mprPutStringToBuf(bp, ",\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pp = first;
|
||||
while (pp) {
|
||||
if (! pp->dontEnumerate ||
|
||||
ep->flags & EJS_FLAGS_ENUM_HIDDEN) {
|
||||
for (i = 0; i < ep->depth; i++) {
|
||||
mprPutStringToBuf(bp, " ");
|
||||
}
|
||||
|
||||
if (! vp->isArray) {
|
||||
mprPutStringToBuf(bp, pp->name);
|
||||
mprPutStringToBuf(bp, ": ");
|
||||
}
|
||||
|
||||
propVar = ejsGetVarPtr(pp);
|
||||
if (propVar->type == EJS_TYPE_OBJECT) {
|
||||
if (pp->var.objectState == vp->objectState) {
|
||||
value = "this";
|
||||
} else if (ejsRunMethodCmd(ep, propVar,
|
||||
"toString", "%d", ep->maxDepth) < 0) {
|
||||
value = "[object Object]";
|
||||
} else {
|
||||
mprAssert(ejsVarIsString(ep->result));
|
||||
value = ep->result->string;
|
||||
}
|
||||
mprPutStringToBuf(bp, value);
|
||||
|
||||
} else {
|
||||
formatVar(ep, bp, &pp->var);
|
||||
}
|
||||
|
||||
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
|
||||
if (pp) {
|
||||
mprPutStringToBuf(bp, ",\n");
|
||||
}
|
||||
} else {
|
||||
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
|
||||
}
|
||||
}
|
||||
}
|
||||
vp->objectState->visited = 0;
|
||||
|
||||
mprPutCharToBuf(bp, '\n');
|
||||
|
||||
ep->depth--;
|
||||
for (i = 0; i < ep->depth; i++) {
|
||||
mprPutStringToBuf(bp, " ");
|
||||
}
|
||||
mprPutCharToBuf(bp, vp->isArray ? ']' : '}');
|
||||
}
|
||||
|
||||
} else if (vp->type == EJS_TYPE_METHOD) {
|
||||
|
||||
mprPutStringToBuf(bp, "function (");
|
||||
for (i = 0; i < vp->method.args->length; i++) {
|
||||
mprPutStringToBuf(bp, vp->method.args->items[i]);
|
||||
if ((i + 1) < vp->method.args->length) {
|
||||
mprPutStringToBuf(bp, ", ");
|
||||
}
|
||||
}
|
||||
mprPutStringToBuf(bp, ") {");
|
||||
mprPutStringToBuf(bp, vp->method.body);
|
||||
for (i = 0; i < ep->depth; i++) {
|
||||
mprPutStringToBuf(bp, " ");
|
||||
}
|
||||
mprPutStringToBuf(bp, "}");
|
||||
|
||||
} else {
|
||||
|
||||
if (vp->type == EJS_TYPE_STRING) {
|
||||
mprPutCharToBuf(bp, '\"');
|
||||
}
|
||||
|
||||
/*
|
||||
* We don't use ejsVarToString for arrays, objects and strings.
|
||||
* This is because ejsVarToString does not call "obj.toString"
|
||||
* and it is not required for strings.
|
||||
* MOB - rc
|
||||
*/
|
||||
buf = ejsVarToString(ep, vp);
|
||||
mprPutStringToBuf(bp, buf);
|
||||
|
||||
if (vp->type == EJS_TYPE_STRING) {
|
||||
mprPutCharToBuf(bp, '\"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* mixin code. Blends code at the "thisObj" level.
|
||||
*/
|
||||
|
||||
static int mixinMethod(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
char *buf;
|
||||
int fid, i, rc;
|
||||
|
||||
mprAssert(argv);
|
||||
|
||||
/*
|
||||
* Create a variable scope block set to the current object
|
||||
*/
|
||||
rc = 0;
|
||||
fid = ejsSetBlock(ep, thisObj);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
|
||||
if (ejsVarIsString(argv[i])) {
|
||||
rc = ejsEvalScript(ep, argv[i]->string, 0);
|
||||
|
||||
} else if (ejsVarIsObject(argv[i])) {
|
||||
|
||||
/* MOB -- OPT. When we have proper scope chains, we should just
|
||||
refer to the module and not copy */
|
||||
pp = ejsGetFirstProperty(argv[i], EJS_ENUM_ALL);
|
||||
while (pp) {
|
||||
ejsSetProperty(ep, thisObj, pp->name, ejsGetVarPtr(pp));
|
||||
pp = ejsGetNextProperty(pp, EJS_ENUM_ALL);
|
||||
}
|
||||
|
||||
} else {
|
||||
/* MOB - rc */
|
||||
buf = ejsVarToString(ep, argv[i]);
|
||||
rc = ejsEvalScript(ep, buf, 0);
|
||||
|
||||
}
|
||||
if (rc < 0) {
|
||||
ejsCloseBlock(ep, fid);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ejsCloseBlock(ep, fid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Create the object class
|
||||
*/
|
||||
|
||||
int ejsDefineObjectClass(Ejs *ep)
|
||||
{
|
||||
EjsMethods *methods;
|
||||
EjsProperty *objectProp, *protoProp;
|
||||
EjsVar *op, *globalClass;
|
||||
|
||||
/*
|
||||
* Must specially hand-craft the object class as it is the base class
|
||||
* of all objects.
|
||||
*/
|
||||
op = ejsCreateObjVar(ep);
|
||||
if (op == 0) {
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
ejsSetClassName(ep, op, "Object");
|
||||
|
||||
/*
|
||||
* Don't use a constructor for objects for speed
|
||||
*/
|
||||
ejsMakeClassNoConstructor(op);
|
||||
|
||||
/*
|
||||
* MOB -- should mark properties as public / private and class or instance.
|
||||
*/
|
||||
ejsDefineCMethod(ep, op, "clone", cloneMethod, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, op, "toString", toStringMethod, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, op, "valueOf", valueOfMethod, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, op, "mixin", mixinMethod, EJS_NO_LOCAL);
|
||||
|
||||
ejsDefineCAccessors(ep, op, "hash", hashGetAccessor, 0, EJS_NO_LOCAL);
|
||||
ejsDefineCAccessors(ep, op, "baseClass", classGetAccessor, 0, EJS_NO_LOCAL);
|
||||
|
||||
/*
|
||||
* MOB -- make this an accessor
|
||||
*/
|
||||
protoProp = ejsSetProperty(ep, op, "prototype", op);
|
||||
if (protoProp == 0) {
|
||||
ejsFreeVar(ep, op);
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup the internal methods. Most classes will never override these.
|
||||
* The XML class will. We rely on talloc to free internal. Use "ep" as
|
||||
* the parent as we need "methods" to live while the interpreter lives.
|
||||
*/
|
||||
methods = mprAllocTypeZeroed(ep, EjsMethods);
|
||||
op->objectState->methods = methods;
|
||||
|
||||
methods->createProperty = createObjProperty;
|
||||
methods->deleteProperty = deleteObjProperty;
|
||||
methods->getProperty = getObjProperty;
|
||||
methods->setProperty = setObjProperty;
|
||||
|
||||
objectProp = ejsSetPropertyAndFree(ep, ep->global, "Object", op);
|
||||
|
||||
/*
|
||||
* Change the global class to use Object's methods
|
||||
*/
|
||||
globalClass = ep->service->globalClass;
|
||||
globalClass->objectState->methods = methods;
|
||||
globalClass->objectState->baseClass = ejsGetVarPtr(protoProp);
|
||||
|
||||
ep->objectClass = ejsGetVarPtr(objectProp);
|
||||
|
||||
if (ejsObjHasErrors(ejsGetVarPtr(objectProp))) {
|
||||
ejsFreeVar(ep, op);
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsObjectDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* @file ejsStndClasses.c
|
||||
* @brief EJS support methods
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* Function Class *******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineFunctionClass(Ejs *ep)
|
||||
{
|
||||
if (ejsDefineClass(ep, "Function", "Object", ejsFunctionConstructor) == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Function constructor
|
||||
*/
|
||||
|
||||
int ejsFunctionConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsArgError(ep, "Usage: Function(\"function (arg) { script };\");");
|
||||
}
|
||||
|
||||
rc = ejsEvalScript(ep, argv[0]->string, 0);
|
||||
|
||||
/*
|
||||
* Note: this will convert the object into a method. It will cease to be
|
||||
* an object.
|
||||
*/
|
||||
if (rc == 0 && ejsVarIsMethod(ep->result)) {
|
||||
/*
|
||||
* Must make thisObj collectable.
|
||||
*/
|
||||
ejsMakeObjPermanent(thisObj, 0);
|
||||
ejsMakeObjLive(thisObj, 1);
|
||||
mprAssert(ejsObjIsCollectable(thisObj));
|
||||
ejsWriteVar(ep, thisObj, ep->result, EJS_SHALLOW_COPY);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* Boolean Class ********************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineBooleanClass(Ejs *ep)
|
||||
{
|
||||
if (ejsDefineClass(ep, "Boolean", "Object", ejsBooleanConstructor) == 0){
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Boolean constructor
|
||||
*/
|
||||
|
||||
int ejsBooleanConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Number Class ********************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineNumberClass(Ejs *ep)
|
||||
{
|
||||
if (ejsDefineClass(ep, "Number", "Object", ejsNumberConstructor) == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Number constructor
|
||||
*/
|
||||
|
||||
int ejsNumberConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsStndClassesDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
* @file ejsString.c
|
||||
* @brief EJScript string class
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
/******************************************************************************/
|
||||
/*********************************** Constructors *****************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* String constructor.
|
||||
*/
|
||||
|
||||
int ejsStringConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (argc == 0) {
|
||||
ejsSetReturnValueToString(ejs, "");
|
||||
|
||||
} else if (argc == 1) {
|
||||
/* MOB -- rc */
|
||||
str = ejsVarToString(ejs, argv[0]);
|
||||
ejsSetReturnValueToString(ejs, str);
|
||||
|
||||
} else {
|
||||
ejsArgError(ejs, "usage: String([var])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Visible Methods *****************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return a string containing the character at a given index
|
||||
*
|
||||
* String string.charAt(Number)
|
||||
*/
|
||||
|
||||
static int charAt(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsNum num;
|
||||
char buf[2];
|
||||
|
||||
if (argc != 1) {
|
||||
ejsArgError(ejs, "usage: charAt(integer)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
num = ejsVarToNumber(argv[0]);
|
||||
if (num < 0 || num >= thisObj->length) {
|
||||
ejsError(ejs, EJS_RANGE_ERROR, "Bad index");
|
||||
return -1;
|
||||
}
|
||||
|
||||
mprAssert(ejsVarIsString(thisObj));
|
||||
|
||||
buf[0] = argv[0]->string[num];
|
||||
buf[1] = '\0';
|
||||
ejsSetReturnValueToString(ejs, buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return an integer containing the character at a given index
|
||||
*
|
||||
* Number string.charCodeAt(Number)
|
||||
*/
|
||||
|
||||
static EjsNum charCodeAt(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsNum num;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsArgError(ejs, "usage: charCodeAt(integer)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
num = ejsVarToNumber(argv[0]);
|
||||
if (num < 0 || num >= thisObj->length) {
|
||||
ejsError(ejs, EJS_RANGE_ERROR, "Bad index");
|
||||
return -1;
|
||||
}
|
||||
ejsSetReturnValueToNumber(ejs, (EjsNum) argv[0]->string[num]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Catenate
|
||||
*
|
||||
* String string.catenate(var, ...)
|
||||
*/
|
||||
|
||||
static int concat(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (argc == 0) {
|
||||
ejsArgError(ejs, "usage: concat(String, ...)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
mprAssert(ejsVarIsString(thisObj));
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (ejsStrcat(ejs, thisObj, argv[i]) < 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ejsSetReturnValue(ejs, thisObj);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the position of the first occurance of a substring
|
||||
*
|
||||
* Number string.indexOf(String subString [, Number start])
|
||||
*/
|
||||
|
||||
static int indexOf(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *pat, *s1, *s2, *origin;
|
||||
int start, i;
|
||||
|
||||
if (argc == 0 || argc > 2) {
|
||||
ejsArgError(ejs, "usage: indexOf(String [, Number])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pat = ejsVarToString(ejs, argv[0]);
|
||||
|
||||
if (argc == 2) {
|
||||
start = ejsVarToNumber(argv[1]);
|
||||
if (start > thisObj->length) {
|
||||
start = thisObj->length;
|
||||
}
|
||||
} else {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
i = start;
|
||||
for (origin = &thisObj->string[i]; i < thisObj->length; i++, origin++) {
|
||||
s1 = origin;
|
||||
for (s2 = pat; *s1 && *s2; s1++, s2++) {
|
||||
if (*s1 != *s2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*s2 == '\0') {
|
||||
ejsSetReturnValueToNumber(ejs, (EjsNum) (origin - thisObj->string));
|
||||
}
|
||||
}
|
||||
|
||||
ejsSetReturnValueToNumber(ejs, (EjsNum) -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the position of the last occurance of a substring
|
||||
*
|
||||
* Number string.lastIndexOf(String subString [, Number start])
|
||||
*/
|
||||
|
||||
static int lastIndexOf(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *pat, *s1, *s2, *origin;
|
||||
int start;
|
||||
|
||||
if (argc == 0 || argc > 2) {
|
||||
ejsArgError(ejs, "usage: indexOf(String [, Number])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pat = ejsVarToString(ejs, argv[0]);
|
||||
|
||||
if (argc == 2) {
|
||||
start = ejsVarToNumber(argv[1]);
|
||||
if (start > thisObj->length) {
|
||||
start = thisObj->length;
|
||||
}
|
||||
} else {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
origin = &thisObj->string[thisObj->length - 1];
|
||||
for (; origin >= &thisObj->string[start]; origin--) {
|
||||
|
||||
s1 = origin;
|
||||
for (s2 = pat; *s1 && *s2; s1++, s2++) {
|
||||
if (*s1 != *s2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*s2 == '\0') {
|
||||
ejsSetReturnValueToNumber(ejs, (EjsNum) (origin - thisObj->string));
|
||||
}
|
||||
}
|
||||
|
||||
ejsSetReturnValueToNumber(ejs, (EjsNum) -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return a substring
|
||||
*
|
||||
* Number string.slice(Number start, Number end)
|
||||
*/
|
||||
|
||||
static int slice(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsNum start, end;
|
||||
|
||||
if (argc != 2) {
|
||||
ejsArgError(ejs, "usage: slice(Number, Number)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
start = ejsVarToNumber(argv[0]);
|
||||
end = ejsVarToNumber(argv[1]);
|
||||
if (start < 0 || start >= thisObj->length) {
|
||||
ejsError(ejs, EJS_RANGE_ERROR, "Bad start index");
|
||||
return-1;
|
||||
}
|
||||
if (end < 0 || end >= thisObj->length) {
|
||||
ejsError(ejs, EJS_RANGE_ERROR, "Bad end index");
|
||||
return -1;
|
||||
}
|
||||
|
||||
mprAssert(ejsVarIsString(thisObj));
|
||||
|
||||
ejsSetReturnValueToBinaryString(ejs, (uchar*) &thisObj->string[start],
|
||||
end - start);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Split a string
|
||||
*
|
||||
* Number string.split(String delimiter [, Number limit])
|
||||
*/
|
||||
|
||||
static int split(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsVar *array, *vp;
|
||||
char *delim, *last, *cp;
|
||||
int len, limit, alloc;
|
||||
|
||||
if (argc == 0 || argc > 2) {
|
||||
ejsArgError(ejs, "usage: split(String [, Number])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
delim = ejsVarToStringEx(ejs, argv[0], &alloc);
|
||||
|
||||
limit = ejsVarToNumber(argv[1]);
|
||||
|
||||
array = ejsCreateArray(ejs, 0);
|
||||
|
||||
len = strlen(delim);
|
||||
|
||||
last = thisObj->string;
|
||||
for (cp = last; *cp; cp++) {
|
||||
if (*cp == *delim && strncmp(cp, delim, len) == 0) {
|
||||
if (cp > last) {
|
||||
vp = ejsCreateBinaryStringVar(ejs, (uchar*) last, (cp - last));
|
||||
ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY);
|
||||
ejsFreeVar(ejs, vp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ejsSetReturnValue(ejs, array);
|
||||
ejsFreeVar(ejs, array);
|
||||
|
||||
if (alloc) {
|
||||
mprFree(delim);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Create the object class
|
||||
*/
|
||||
|
||||
int ejsDefineStringClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *sc;
|
||||
|
||||
sc = ejsDefineClass(ejs, "String", "Object", ejsStringConstructor);
|
||||
if (sc == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
ejsDefineCMethod(ejs, sc, "charAt", charAt, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "charCodeAt", charCodeAt, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "concat", concat, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "indexOf", indexOf, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "lastIndexOf", lastIndexOf, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "slice", slice, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "split", split, EJS_NO_LOCAL);
|
||||
#if UNUSED
|
||||
ejsDefineCMethod(ejs, sc, "match", match, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "replace", replace, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "search", search, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "substring", substring, EJS_NO_LOCAL);
|
||||
// MOB bad name
|
||||
ejsDefineCMethod(ejs, sc, "substr", substr, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "toLowerCase", toLowerCase, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, sc, "toUpperCase", toUpperCase, EJS_NO_LOCAL);
|
||||
|
||||
// Static method
|
||||
ejsDefineCMethod(ejs, sc, "fromCharCode", fromCharCode, 0, EJS_NO_LOCAL);
|
||||
#endif
|
||||
|
||||
if (ejsObjHasErrors(sc)) {
|
||||
ejsFreeVar(ejs, sc);
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,849 @@
|
||||
/*
|
||||
* ejs.h - EJScript Language (ECMAScript) header.
|
||||
*/
|
||||
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default.g
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
* Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#ifndef _h_EJS
|
||||
#define _h_EJS 1
|
||||
|
||||
#include "mpr.h"
|
||||
#include "ejsVar.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define EJS_GC_WORK_QUOTA 160 /* Allocations required before
|
||||
garbage colllection */
|
||||
|
||||
#define EJS_PARSE_INCR 256 /* Growth factor */
|
||||
#define EJS_MAX_RECURSE 25 /* Sanity for maximum recursion */
|
||||
#define EJS_SMALL_OBJ_HASH_SIZE 11 /* Small object hash size */
|
||||
#define EJS_LIST_INCR 8 /* Growth increment for lists */
|
||||
#define EJS_MAX_BACKTRACE 10 /* Recursion limit for assert */
|
||||
|
||||
#else
|
||||
#define EJS_GC_WORK_QUOTA 500
|
||||
|
||||
#define EJS_PARSE_INCR 1024
|
||||
#define EJS_MAX_RECURSE 100
|
||||
#define EJS_SMALL_OBJ_HASH_SIZE 11
|
||||
#define EJS_LIST_INCR 16
|
||||
#define EJS_MAX_BACKTRACE 25
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Allocation increments for the default interpreter
|
||||
*/
|
||||
#define EJS_DEFAULT_VAR_INC 8 /* Var allocation increment */
|
||||
#define EJS_DEFAULT_PROP_INC 96 /* Property allocation increment */
|
||||
#define EJS_DEFAULT_OBJ_INC 24 /* Object allocation increment */
|
||||
#define EJS_DEFAULT_STR_INC 64 /* Object allocation increment */
|
||||
|
||||
#define EJS_MIN_TIME_FOR_GC 300 /**< Need 1/3 sec for GC */
|
||||
#define EJS_GC_MIN_WORK_QUOTA 50 /**< Min to stop thrashing */
|
||||
|
||||
/*
|
||||
* Allocation increments for all non-default interpreters
|
||||
*/
|
||||
#define EJS_VAR_INC 32
|
||||
#define EJS_PROP_INC 64
|
||||
#define EJS_OBJ_INC 64
|
||||
#define EJS_STR_INC 64
|
||||
|
||||
#define EJS_INC_FRAMES 8 /* Frame stack increment */
|
||||
#define EJS_MAX_FRAMES 64 /* Max frame stack */
|
||||
|
||||
/*
|
||||
* Lexical analyser tokens
|
||||
*/
|
||||
#define EJS_TOK_ERR -1 /* Any error */
|
||||
#define EJS_TOK_LPAREN 1 /* ( */
|
||||
#define EJS_TOK_RPAREN 2 /* ) */
|
||||
#define EJS_TOK_IF 3 /* if */
|
||||
#define EJS_TOK_ELSE 4 /* else */
|
||||
#define EJS_TOK_LBRACE 5 /* { */
|
||||
#define EJS_TOK_RBRACE 6 /* } */
|
||||
#define EJS_TOK_LOGICAL 7 /* ||, &&, ! */
|
||||
#define EJS_TOK_EXPR 8 /* +, -, /, % */
|
||||
#define EJS_TOK_SEMI 9 /* ; */
|
||||
#define EJS_TOK_LITERAL 10 /* literal string */
|
||||
#define EJS_TOK_METHOD_NAME 11 /* methodName( */
|
||||
#define EJS_TOK_NEWLINE 12 /* newline white space */
|
||||
#define EJS_TOK_ID 13 /* Identifier */
|
||||
#define EJS_TOK_EOF 14 /* End of script */
|
||||
#define EJS_TOK_COMMA 15 /* Comma */
|
||||
#define EJS_TOK_VAR 16 /* var */
|
||||
#define EJS_TOK_ASSIGNMENT 17 /* = */
|
||||
#define EJS_TOK_FOR 18 /* for */
|
||||
#define EJS_TOK_INC_DEC 19 /* ++, -- */
|
||||
#define EJS_TOK_RETURN 20 /* return */
|
||||
#define EJS_TOK_PERIOD 21 /* . */
|
||||
#define EJS_TOK_LBRACKET 22 /* [ */
|
||||
#define EJS_TOK_RBRACKET 23 /* ] */
|
||||
#define EJS_TOK_NEW 24 /* new */
|
||||
#define EJS_TOK_DELETE 25 /* delete */
|
||||
#define EJS_TOK_IN 26 /* in */
|
||||
#define EJS_TOK_FUNCTION 27 /* function */
|
||||
#define EJS_TOK_NUMBER 28 /* Number */
|
||||
#define EJS_TOK_CLASS 29 /* class */
|
||||
#define EJS_TOK_EXTENDS 30 /* extends */
|
||||
#define EJS_TOK_PUBLIC 31 /* public */
|
||||
#define EJS_TOK_PRIVATE 32 /* private */
|
||||
#define EJS_TOK_PROTECTED 33 /* private */
|
||||
#define EJS_TOK_TRY 34 /* try */
|
||||
#define EJS_TOK_CATCH 35 /* catch */
|
||||
#define EJS_TOK_FINALLY 36 /* finally */
|
||||
#define EJS_TOK_THROW 37 /* throw */
|
||||
#define EJS_TOK_COLON 38 /* : */
|
||||
#define EJS_TOK_GET 39 /* get */
|
||||
#define EJS_TOK_SET 40 /* set */
|
||||
#define EJS_TOK_MODULE 41 /* module */
|
||||
#define EJS_TOK_EACH 42 /* each */
|
||||
|
||||
/*
|
||||
* Expression operators
|
||||
*/
|
||||
#define EJS_EXPR_LESS 1 /* < */
|
||||
#define EJS_EXPR_LESSEQ 2 /* <= */
|
||||
#define EJS_EXPR_GREATER 3 /* > */
|
||||
#define EJS_EXPR_GREATEREQ 4 /* >= */
|
||||
#define EJS_EXPR_EQ 5 /* == */
|
||||
#define EJS_EXPR_NOTEQ 6 /* != */
|
||||
#define EJS_EXPR_PLUS 7 /* + */
|
||||
#define EJS_EXPR_MINUS 8 /* - */
|
||||
#define EJS_EXPR_DIV 9 /* / */
|
||||
#define EJS_EXPR_MOD 10 /* % */
|
||||
#define EJS_EXPR_LSHIFT 11 /* << */
|
||||
#define EJS_EXPR_RSHIFT 12 /* >> */
|
||||
#define EJS_EXPR_MUL 13 /* * */
|
||||
#define EJS_EXPR_ASSIGNMENT 14 /* = */
|
||||
#define EJS_EXPR_INC 15 /* ++ */
|
||||
#define EJS_EXPR_DEC 16 /* -- */
|
||||
#define EJS_EXPR_BOOL_COMP 17 /* ! */
|
||||
|
||||
/*
|
||||
* Conditional operators
|
||||
*/
|
||||
#define EJS_COND_AND 1 /* && */
|
||||
#define EJS_COND_OR 2 /* || */
|
||||
#define EJS_COND_NOT 3 /* ! */
|
||||
|
||||
/**
|
||||
* EJ Parsing States. Error and Return are be negative.
|
||||
*/
|
||||
#define EJS_STATE_ERR -1 /**< Error state */
|
||||
#define EJS_STATE_RET -2 /**< Return statement */
|
||||
#define EJS_STATE_EOF -3 /**< End of file */
|
||||
#define EJS_STATE_COND 2 /* Parsing a conditional stmt */
|
||||
#define EJS_STATE_COND_DONE 3
|
||||
#define EJS_STATE_RELEXP 4 /* Parsing a relational expr */
|
||||
#define EJS_STATE_RELEXP_DONE 5
|
||||
#define EJS_STATE_EXPR 6 /* Parsing an expression */
|
||||
#define EJS_STATE_EXPR_DONE 7
|
||||
#define EJS_STATE_STMT 8 /* Parsing General statement */
|
||||
#define EJS_STATE_STMT_DONE 9
|
||||
#define EJS_STATE_STMT_BLOCK_DONE 10 /* End of block "}" */
|
||||
#define EJS_STATE_ARG_LIST 11 /* Method arg list */
|
||||
#define EJS_STATE_ARG_LIST_DONE 12
|
||||
#define EJS_STATE_DEC_LIST 16 /* Declaration list */
|
||||
#define EJS_STATE_DEC_LIST_DONE 17
|
||||
#define EJS_STATE_DEC 18 /* Declaration statement */
|
||||
#define EJS_STATE_DEC_DONE 19
|
||||
|
||||
#define EJS_STATE_BEGIN EJS_STATE_STMT
|
||||
|
||||
/*
|
||||
* General parsing flags.
|
||||
*/
|
||||
#define EJS_FLAGS_EXE 0x1 /* Execute statements */
|
||||
#define EJS_FLAGS_LOCAL 0x2 /* Get local vars only */
|
||||
#define EJS_FLAGS_GLOBAL 0x4 /* Get global vars only */
|
||||
#define EJS_FLAGS_CREATE 0x8 /* Create var */
|
||||
#define EJS_FLAGS_ASSIGNMENT 0x10 /* In assignment stmt */
|
||||
#define EJS_FLAGS_DELETE 0x20 /* Deleting a variable */
|
||||
#define EJS_FLAGS_NEW 0x80 /* In a new stmt() */
|
||||
#define EJS_FLAGS_EXIT 0x100 /* Must exit */
|
||||
#define EJS_FLAGS_LHS 0x200 /* Left-hand-side of assignment */
|
||||
#define EJS_FLAGS_FORIN 0x400 /* In "for (v in ..." */
|
||||
#define EJS_FLAGS_CLASS_DEC 0x800 /* "class name [extends] name " */
|
||||
#define EJS_FLAGS_TRY 0x2000 /* In a try {} block */
|
||||
#define EJS_FLAGS_CATCH 0x4000 /* "catch (variable)" */
|
||||
#define EJS_FLAGS_DONT_GC 0x8000 /* Don't garbage collect */
|
||||
#define EJS_FLAGS_NO_ARGS 0x10000 /* Accessors don't use args */
|
||||
#define EJS_FLAGS_ENUM_HIDDEN 0x20000 /* Enumerate hidden fields */
|
||||
#define EJS_FLAGS_ENUM_BASE 0x40000 /* Enumerate base classes */
|
||||
#define EJS_FLAGS_TRACE_ARGS 0x80000 /* Support for printv */
|
||||
#define EJS_FLAGS_SHARED_SLAB 0x100000/* Using a shared slab */
|
||||
|
||||
/*
|
||||
* Exceptions
|
||||
*/
|
||||
#define EJS_ARG_ERROR "ArgError" /**< Method argument error */
|
||||
#define EJS_ASSERT_ERROR "AssertError" /**< Assertion error */
|
||||
#define EJS_EVAL_ERROR "EvalError" /**< General evalation error */
|
||||
#define EJS_INTERNAL_ERROR "InternalError" /**< Internal error */
|
||||
#define EJS_IO_ERROR "IOError" /**< IO or data error */
|
||||
#define EJS_MEMORY_ERROR "MemoryError" /**< Memory allocation error */
|
||||
#define EJS_RANGE_ERROR "RangeError" /**< Data out of range (div by 0) */
|
||||
#define EJS_REFERENCE_ERROR "ReferenceError"/**< Object or property reference */
|
||||
#define EJS_SYNTAX_ERROR "SyntaxError" /**< Javascript syntax error */
|
||||
#define EJS_TYPE_ERROR "TypeError" /**< Wrong type supplied */
|
||||
|
||||
/*
|
||||
* E4X
|
||||
*/
|
||||
#if BLD_FEATURE_EJS_E4X
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define E4X_BUF_SIZE 512 /* Initial buffer size for tokens */
|
||||
#define E4X_BUF_MAX (32 * 1024) /* Max size for tokens */
|
||||
#define E4X_MAX_NODE_DEPTH 24 /* Max nesting of tags */
|
||||
#else
|
||||
#define E4X_BUF_SIZE 4096
|
||||
#define E4X_BUF_MAX (128 * 1024)
|
||||
#define E4X_MAX_NODE_DEPTH 128
|
||||
#endif
|
||||
|
||||
#define E4X_MAX_ELT_SIZE (E4X_BUF_MAX-1)
|
||||
#define E4X_TEXT_PROPERTY "-txt"
|
||||
#define E4X_TAG_NAME_PROPERTY "-tag"
|
||||
#define E4X_COMMENT_PROPERTY "-com"
|
||||
#define E4X_ATTRIBUTES_PROPERTY "-att"
|
||||
#define E4X_PI_PROPERTY "-pi"
|
||||
#define E4X_PARENT_PROPERTY "-parent"
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
/**
|
||||
* Multithreaded lock function
|
||||
*/
|
||||
typedef void (*EjsLockFn)(void *lockData);
|
||||
/**
|
||||
* Multithreaded unlock function
|
||||
*/
|
||||
typedef void (*EjsUnlockFn)(void *lockData);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Token limits
|
||||
*/
|
||||
#define EJS_MAX_LINE 128 /* Maximum input line buffer */
|
||||
#define EJS_MAX_TOKEN 640 /* Max input parse token */
|
||||
#define EJS_TOKEN_STACK 3 /* Put back token stack */
|
||||
|
||||
/*
|
||||
* Putback token
|
||||
*/
|
||||
|
||||
typedef struct EjsToken {
|
||||
char tokbuf[EJS_MAX_TOKEN];
|
||||
int tid; /* Token ID */
|
||||
} EjsToken;
|
||||
|
||||
/*
|
||||
* EJ evaluation block structure
|
||||
*/
|
||||
typedef struct EjsInput {
|
||||
EjsToken putBack[EJS_TOKEN_STACK]; /* Put back token stack */
|
||||
int putBackIndex; /* Top of stack index */
|
||||
char line[EJS_MAX_LINE]; /* Current line */
|
||||
char *fileName; /* File or script name */
|
||||
int lineLength; /* Current line length */
|
||||
int lineNumber; /* Parse line number */
|
||||
int lineColumn; /* Column in line */
|
||||
struct EjsInput *next; /* Used for backtraces */
|
||||
const char *procName; /* Gives name in backtrace */
|
||||
const char *script; /* Input script for parsing */
|
||||
char *scriptServp; /* Next token in the script */
|
||||
int scriptSize; /* Length of script */
|
||||
char tokbuf[EJS_MAX_TOKEN]; /* Current token */
|
||||
int tid; /* Token ID */
|
||||
char *tokEndp; /* Pointer past end of token */
|
||||
char *tokServp; /* Pointer to next token char */
|
||||
struct EjsInput *nextInput; /* Free list of input structs */
|
||||
} EjsInput;
|
||||
|
||||
/*
|
||||
* Method call structure
|
||||
*/
|
||||
typedef struct EjsProc {
|
||||
MprArray *args; /* Args for method */
|
||||
EjsVar *fn; /* Method definition */
|
||||
char *procName; /* Method name */
|
||||
} EjsProc;
|
||||
|
||||
|
||||
/**
|
||||
* @overview EJScript Service structure
|
||||
* @description The EJScript service manages the overall language runtime. It
|
||||
* is the factory that creates interpreter instances via ejsCreateInterp.
|
||||
* The EJScript service creates a master interpreter that holds the
|
||||
* standard language classes and properties. When user interpreters are
|
||||
* created, they reference (without copying) the master interpreter to
|
||||
* gain access to the standard classes and types.
|
||||
* @stability Prototype.
|
||||
* @library libejs.
|
||||
* @see ejsOpenService, ejsCloseService, ejsCreateInterp, ejsDestoryInterp
|
||||
*/
|
||||
typedef struct EjsService {
|
||||
EjsVar *globalClass; /* Global class */
|
||||
struct Ejs *master; /* Master Interp inherited by all */
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
EjsLockFn lock;
|
||||
EjsUnlockFn unlock;
|
||||
void *lockData;
|
||||
#endif
|
||||
} EjsService;
|
||||
|
||||
|
||||
/*
|
||||
* Memory statistics
|
||||
*/
|
||||
typedef struct EjsMemStats {
|
||||
uint maxMem;
|
||||
uint usedMem;
|
||||
} EjsMemStats;
|
||||
|
||||
|
||||
/*
|
||||
* Garbage collection block alignment
|
||||
*/
|
||||
#define EJS_ALLOC_ALIGN(ptr) \
|
||||
(((ptr) + sizeof(void*) - 1) & ~(sizeof(void*) - 1))
|
||||
|
||||
/*
|
||||
* Default GC tune factors
|
||||
*/
|
||||
#define EJS_GC_START_THRESHOLD (32 * 1024)
|
||||
|
||||
/*
|
||||
* The Garbage collector is a generational collector. It ages blocks and
|
||||
* optimizes the mark / sweep algorithm to focus on new and recent blocks
|
||||
*/
|
||||
typedef enum EjsGeneration {
|
||||
EJS_GEN_NEW = 0,
|
||||
EJS_GEN_RECENT_1 = 1,
|
||||
EJS_GEN_RECENT_2 = 2,
|
||||
EJS_GEN_OLD = 3,
|
||||
EJS_GEN_PERMANENT = 4,
|
||||
EJS_GEN_MAX = 5,
|
||||
} EjsGeneration;
|
||||
|
||||
/*
|
||||
* Garbage collector control
|
||||
*/
|
||||
typedef struct EjsGC {
|
||||
bool enable;
|
||||
bool enableDemandCollect;
|
||||
bool enableIdleCollect;
|
||||
/*
|
||||
* maxMemory should be set to be 95% of the real max memory limit
|
||||
*/
|
||||
uint maxMemory; /* Above this, Throw Memory exception. */
|
||||
int workQuota; /* Quota of work before GC */
|
||||
int workDone; /* Count of allocations */
|
||||
int degraded; /* Have exceeded maxMemory */
|
||||
|
||||
/*
|
||||
* Debug Levels 0-N (increases verbosity)
|
||||
* 1 -- Sweep and collection count
|
||||
* 2 -- Trace objects deleted
|
||||
* 3 -- Trace objects marked
|
||||
* 4 -- Print alloc report when needing a demand allocation
|
||||
*
|
||||
*/
|
||||
int debugLevel; /* In debug mode */
|
||||
int collecting; /* Running garbage collection */
|
||||
uint collectionCount; /* Number of times GC ran */
|
||||
#if BLD_DEBUG
|
||||
int gcIndent; /* Indent formatting */
|
||||
int objectsInUse; /* Objects currently reachable */
|
||||
int propertiesInUse; /* Properties currently reachable */
|
||||
#endif
|
||||
} EjsGC;
|
||||
|
||||
/*
|
||||
* Slab memory allocation
|
||||
*/
|
||||
typedef struct EjsSlab {
|
||||
uint allocIncrement; /* Growth increment in slab */
|
||||
uint size; /* Size of allocations */
|
||||
EjsGCLink freeList; /* Free list (only next ptr is used) */
|
||||
EjsObj *lastRecentBlock; /* Saved for GC age generations phase */
|
||||
EjsGCLink allocList[EJS_GEN_MAX]; /* Allocated block list */
|
||||
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
uint totalAlloc; /* Total count of allocation calls */
|
||||
uint freeCount; /* Number of blocks on the slab freelist */
|
||||
uint allocCount; /* Number of allocated blocks */
|
||||
uint peakAllocated; /* Peak allocated */
|
||||
uint peakFree; /* Peak on the free list */
|
||||
uint totalReclaimed; /* Total blocks reclaimed on sweeps */
|
||||
uint totalSweeps; /* Total sweeps */
|
||||
#endif
|
||||
} EjsSlab;
|
||||
|
||||
|
||||
/**
|
||||
* @overview EJ interpreter control structure.
|
||||
* @description EJ allocates one control structure per active interpreter.
|
||||
* The \ref ejsCreateInterp routine creates the Ejs structure and returns
|
||||
* a reference to be used in subsequent EJ API calls.
|
||||
* @stability Prototype.
|
||||
* @library libejs.
|
||||
* @see ejsCreateInterp, ejsDestroyInterp, ejsOpenService
|
||||
*/
|
||||
struct Ejs {
|
||||
void *altHandle; /* Alternate callback handle */
|
||||
bool castAlloc; /* True if castTemp is allocated */
|
||||
char *castTemp; /* Temporary string for casting */
|
||||
char *currentClass; /* Current class name */
|
||||
EjsVar *currentObj; /* Ptr to current object */
|
||||
EjsVar *thisObject; /* Ptr to current "this" */
|
||||
EjsProperty *currentProperty; /* Ptr to current property */
|
||||
EjsGC gc; /* Garbage collector control */
|
||||
char *errorMsg; /* Error message */
|
||||
char *fileName; /* File or script name */
|
||||
int lineNumber; /* File line number */
|
||||
int scriptStatus; /* Status to exit() */
|
||||
int flags; /* Flags */
|
||||
MprArray *frames; /* List of variable frames */
|
||||
EjsVar *global; /* Global object */
|
||||
EjsVar *objectClass; /* Object class */
|
||||
int gotException; /* Exception thrown */
|
||||
EjsInput *input; /* Input evaluation block */
|
||||
int depth; /* Recursion depth */
|
||||
EjsVar *local; /* Local object */
|
||||
int maxDepth; /* Maximum depth for formatting */
|
||||
void *primaryHandle; /* primary callback handle */
|
||||
EjsProc *proc; /* Current method */
|
||||
int recurseCount; /* Recursion counter */
|
||||
EjsVar *result; /* Variable result */
|
||||
int tid; /* Current token id */
|
||||
char *token; /* Pointer to token string */
|
||||
EjsVar tokenNumber; /* Parsed number */
|
||||
EjsService *service; /* Service object */
|
||||
void *userData; /* Method user data */
|
||||
|
||||
EjsSlab *slabs; /* Memory allocation slabs */
|
||||
MprCtx slabAllocContext; /* Allocation context */
|
||||
EjsInput *inputList; /* Free list of input structs */
|
||||
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
EjsLockFn lock; /* Lock method */
|
||||
EjsUnlockFn unlock; /* Unlock method */
|
||||
void *lockData; /* Lock data argument */
|
||||
#endif
|
||||
#define EJS_MAX_STACK (10 * 1024)
|
||||
char stack[EJS_MAX_STACK]; /* Local variable stack */
|
||||
char *stkPtr; /* Local variable stack ptr */
|
||||
void *inputMarker; /* Recurse protection */
|
||||
};
|
||||
|
||||
|
||||
typedef struct EjsModule
|
||||
{
|
||||
int dummy;
|
||||
} EjsModule;
|
||||
|
||||
|
||||
/*
|
||||
* Method callback when using Alternate handles. GaCompat uses these and
|
||||
* passes the web server request structure via the altHandle.
|
||||
*/
|
||||
typedef void *EjsHandle;
|
||||
typedef int (*EjsAltCMethod)(Ejs *ejs, EjsHandle altHandle,
|
||||
EjsVar *thisObj, int argc, EjsVar **argv);
|
||||
typedef int (*EjsAltStringCMethod)(Ejs *ejs, EjsHandle altHandle,
|
||||
EjsVar *thisObj, int argc, char **argv);
|
||||
|
||||
|
||||
/*
|
||||
* API Constants
|
||||
*/
|
||||
#define EJS_USE_OWN_SLAB 1
|
||||
|
||||
/******************************** Internal API ********************************/
|
||||
/*
|
||||
* Ejs Lex
|
||||
*/
|
||||
extern int ejsLexOpenScript(Ejs *ejs, const char *script);
|
||||
extern void ejsLexCloseScript(Ejs *ejs);
|
||||
extern int ejsInitInputState(EjsInput *ip);
|
||||
extern void ejsLexSaveInputState(Ejs *ejs, EjsInput* state);
|
||||
extern void ejsLexFreeInputState(Ejs *ejs, EjsInput* state);
|
||||
extern void ejsLexRestoreInputState(Ejs *ejs, EjsInput* state);
|
||||
extern int ejsLexGetToken(Ejs *ejs, int state);
|
||||
extern void ejsLexPutbackToken(Ejs *ejs, int tid, char *string);
|
||||
|
||||
/*
|
||||
* Parsing
|
||||
*/
|
||||
extern int ejsParse(Ejs *ejs, int state, int flags);
|
||||
extern int ejsGetFlags(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Create variable scope blocks
|
||||
*/
|
||||
extern int ejsOpenBlock(Ejs *ejs);
|
||||
extern int ejsSetBlock(Ejs *ejs, EjsVar *local);
|
||||
extern int ejsCloseBlock(Ejs *ejs, int vid);
|
||||
extern int ejsEvalBlock(Ejs *ejs, char *script, EjsVar *vp);
|
||||
extern void ejsSetFileName(Ejs *ejs, const char *fileName);
|
||||
|
||||
/*
|
||||
* Class definitions
|
||||
*/
|
||||
extern EjsVar *ejsCreateSimpleClass(Ejs *ejs, EjsVar *baseClass,
|
||||
const char *className);
|
||||
extern int ejsDefineObjectClass(Ejs *ejs);
|
||||
extern int ejsDefineArrayClass(Ejs *ejs);
|
||||
extern int ejsDefineBooleanClass(Ejs *ejs);
|
||||
extern int ejsDefineErrorClasses(Ejs *ejs);
|
||||
extern int ejsDefineFileClass(Ejs *ejs);
|
||||
extern int ejsDefineFileSystemClass(Ejs *ejs);
|
||||
extern int ejsDefineHTTPClass(Ejs *ejs);
|
||||
extern int ejsDefineFunctionClass(Ejs *ejs);
|
||||
extern int ejsDefineNumberClass(Ejs *ejs);
|
||||
extern int ejsDefineStringClass(Ejs *ejs);
|
||||
extern int ejsDefineDateClass(Ejs *ejs);
|
||||
extern int ejsDefineStandardClasses(Ejs *ejs);
|
||||
|
||||
#if BLD_FEATURE_EJS_E4X
|
||||
extern int ejsDefineXmlClasses(Ejs *ejs);
|
||||
extern EjsVar *ejsCreateXml(Ejs *ejs);
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_EJS_DB
|
||||
extern int ejsDefineDbClasses(Ejs *ejs);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* System class definitions
|
||||
*/
|
||||
extern int ejsDefineSystemClasses(Ejs *ejs);
|
||||
extern int ejsDefineSystemClass(Ejs *ejs);
|
||||
extern int ejsDefineAppClass(Ejs *ejs);
|
||||
extern int ejsDefineDebugClass(Ejs *ejs);
|
||||
extern int ejsDefineLogClass(Ejs *ejs);
|
||||
extern int ejsDefineMemoryClass(Ejs *ejs);
|
||||
extern int ejsDefineGCClass(Ejs *ejs);
|
||||
extern int ejsDefineGlobalProperties(Ejs *ejs);
|
||||
|
||||
extern int ejsTermSystemClasses(Ejs *ejs);
|
||||
extern void ejsTermHTTPClass(Ejs *ejs);
|
||||
|
||||
extern int ejsCreateObjectModel(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Class constructors
|
||||
*/
|
||||
extern int ejsArrayConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv);
|
||||
extern int ejsXmlConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv);
|
||||
extern int ejsXmlListConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv);
|
||||
extern int ejsBooleanConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **agv);
|
||||
extern int ejsFunctionConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **agv);
|
||||
extern int ejsNumberConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv);
|
||||
extern int ejsStringConstructor(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv);
|
||||
extern int ejsDateConstructor(Ejs *ejs, EjsVar *thisObj,
|
||||
int argc, EjsVar **argv);
|
||||
|
||||
/*
|
||||
* Garbage collection
|
||||
*/
|
||||
extern void ejsGCInit(Ejs *ejs, int objInc, int propInc, int varInc,
|
||||
int strInc);
|
||||
extern int ejsIsTimeForGC(Ejs *ep, int timeTillNextEvent);
|
||||
|
||||
extern bool ejsSetGCDebugLevel(Ejs *ep, int debugLevel);
|
||||
extern void ejsSweepAll(Ejs *ep);
|
||||
|
||||
extern EjsObj *ejsAllocObj(EJS_LOC_DEC(ejs, loc));
|
||||
extern EjsProperty *ejsAllocProperty(EJS_LOC_DEC(ejs, loc));
|
||||
extern EjsVar *ejsAllocVar(EJS_LOC_DEC(ejs, loc));
|
||||
extern void ejsFree(Ejs *ejs, void *ptr, int slabIndex);
|
||||
|
||||
extern int ejsCollectGarbage(Ejs *ejs, int slabIndex);
|
||||
extern int ejsIncrementalCollectGarbage(Ejs *ejs);
|
||||
|
||||
#if BLD_DEBUG
|
||||
extern void ejsDumpObjects(Ejs *ejs);
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
extern void ejsPrintAllocReport(Ejs *ejs, bool printLeakReport);
|
||||
#endif
|
||||
|
||||
extern void ejsCleanInterp(Ejs *ejs, bool doStats);
|
||||
extern void ejsSetInternalMethods(Ejs *ejs, EjsVar *op);
|
||||
extern void ejsSetPrimaryHandle(Ejs *ep, void *primaryHandle);
|
||||
extern void ejsSetAlternateHandle(Ejs *ep, void *alternateHandle);
|
||||
extern void *ejsGetUserData(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Could possibly make these routines public
|
||||
*/
|
||||
|
||||
extern int ejsSetGCMaxMemory(Ejs *ep, uint maxMemory);
|
||||
extern uint ejsGetUsedMemory(Ejs *ejs);
|
||||
extern uint ejsGetAllocatedMemory(Ejs *ejs);
|
||||
extern uint ejsGetAvailableMemory(Ejs *ejs);
|
||||
extern char *ejsFormatStack(Ejs* ep);;
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
extern int ejsSetServiceLocks(EjsService *sp, EjsLockFn lock,
|
||||
EjsUnlockFn unlock, void *data);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Ejs service and interpreter management
|
||||
*/
|
||||
extern EjsService *ejsOpenService(MprCtx ctx);
|
||||
extern void ejsCloseService(EjsService *sp, bool doStats);
|
||||
|
||||
extern Ejs *ejsCreateInterp(EjsService *sp, void *primaryHandle,
|
||||
void *altHandle, EjsVar *global, bool useOwnSlab);
|
||||
extern void ejsDestroyInterp(Ejs *ejs, bool doStats);
|
||||
|
||||
extern Ejs *ejsGetMasterInterp(EjsService *sp);
|
||||
extern EjsVar *ejsGetGlobalClass(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Module support
|
||||
*/
|
||||
extern EjsModule *ejsCreateModule(const char *name, const char *version,
|
||||
int (*start)(EjsModule*), int (*stop)(EjsModule*));
|
||||
|
||||
/*
|
||||
* Native Objects
|
||||
*/
|
||||
|
||||
void ejsSetNativeData(EjsVar *obj, void *data);
|
||||
void ejsSetNativeHelpers(Ejs *ejs, EjsVar *nativeClass,
|
||||
int (*createInstance)(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv),
|
||||
void (*disposeInstance)(Ejs *ejs, EjsVar *thisObj),
|
||||
bool (*hasProperty)(Ejs *ejs, EjsVar *thisObj, const char *prop),
|
||||
int (*deleteProperty)(Ejs *ejs, EjsVar *thisObj, const char *prop),
|
||||
int (*getProperty)(Ejs *ejs, EjsVar *thisObj, const char *prop,
|
||||
EjsVar *dest),
|
||||
int (*setProperty)(Ejs *ejs, EjsVar *thisObj, const char *prop,
|
||||
EjsVar *value),
|
||||
int (*doOperator)(Ejs *ejs, EjsVar *thisObj, EjsOp *op, EjsVar
|
||||
*result, EjsVar *lhs, EjsVar *rhs, int *code)
|
||||
);
|
||||
|
||||
/*
|
||||
* Evaluation methods
|
||||
*/
|
||||
extern int ejsEvalFile(Ejs *ejs, const char *path, EjsVar *result);
|
||||
extern int ejsEvalScript(Ejs *ejs, const char *script, EjsVar *result);
|
||||
extern int ejsRunMethod(Ejs *ejs, EjsVar *obj,
|
||||
const char *methodName, MprArray *args);
|
||||
extern int ejsRunMethodCmd(Ejs *ejs, EjsVar *obj,
|
||||
const char *methodName, const char *cmdFmt, ...);
|
||||
extern EjsVar *ejsGetReturnValue(Ejs *ejs);
|
||||
|
||||
extern EjsVar *ejsGetLocalObj(Ejs *ejs);
|
||||
extern EjsVar *ejsGetGlobalObj(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Define a class in the specified interpreter. If used with the default
|
||||
* interpeter, then the class is defined for all interpreters.
|
||||
*/
|
||||
extern EjsVar *ejsDefineClass(Ejs *ejs, const char *className,
|
||||
const char *extends, EjsCMethod constructor);
|
||||
extern EjsVar *ejsGetClass(Ejs *ejs, EjsVar *parentClass,
|
||||
const char *className);
|
||||
|
||||
extern const char *ejsGetClassName(EjsVar *obj);
|
||||
extern const char *ejsGetBaseClassName(EjsVar *obj);
|
||||
|
||||
extern bool ejsIsSubClass(EjsVar *target, EjsVar *baseClass);
|
||||
extern EjsVar *ejsGetBaseClass(EjsVar *obj);
|
||||
extern void ejsSetBaseClass(EjsVar *obj, EjsVar *baseClass);
|
||||
|
||||
|
||||
#define ejsCreateSimpleObj(ejs, className) \
|
||||
ejsCreateSimpleObjInternal(EJS_LOC_ARGS(ejs), className)
|
||||
extern EjsVar *ejsCreateSimpleObjInternal(EJS_LOC_DEC(ejs, loc),
|
||||
const char *className);
|
||||
|
||||
#define ejsCreateSimpleObjUsingClass(ejs, baseClass) \
|
||||
ejsCreateSimpleObjUsingClassInt(EJS_LOC_ARGS(ejs), \
|
||||
baseClass)
|
||||
extern EjsVar *ejsCreateSimpleObjUsingClassInt(EJS_LOC_DEC(ejs, loc),
|
||||
EjsVar *baseClass);
|
||||
|
||||
/*
|
||||
* This will create an object and call all required constructors
|
||||
*/
|
||||
extern EjsVar *ejsCreateObj(Ejs *ejs, EjsVar *obj,
|
||||
const char *className, const char *constructorArgs);
|
||||
|
||||
#define ejsCreateObjUsingArgv(ejs, obj, className, args) \
|
||||
ejsCreateObjUsingArgvInternal(EJS_LOC_ARGS(ejs), obj, \
|
||||
className, args)
|
||||
extern EjsVar *ejsCreateObjUsingArgvInternal(EJS_LOC_DEC(ejs, loc),
|
||||
EjsVar *obj, const char *className, MprArray *args);
|
||||
|
||||
#define ejsCreateArray(ejs, size) \
|
||||
ejsCreateArrayInternal(EJS_LOC_ARGS(ejs), size)
|
||||
extern EjsVar *ejsCreateArrayInternal(EJS_LOC_DEC(ejs, loc),
|
||||
int size);
|
||||
|
||||
/*
|
||||
* Array methods. MOB -- need other array methods
|
||||
*/
|
||||
/* MOB -- spell out element */
|
||||
extern EjsVar *ejsAddArrayElt(Ejs *ejs, EjsVar *op, EjsVar *element,
|
||||
EjsCopyDepth copyDepth);
|
||||
/*
|
||||
* Required: Array methods
|
||||
*
|
||||
array = obj.getMethods();
|
||||
array = obj.getProperties();
|
||||
|
||||
array.property.isPublic();
|
||||
array.property.isPrivate();
|
||||
array.property.isMethod();
|
||||
array.property.isEnumerable();
|
||||
array.property.isReadOnly();
|
||||
array.property.allowsNonUnique();
|
||||
array.property.getParent();
|
||||
*/
|
||||
|
||||
/* MOB -- should we have an API that takes a EjsCopyDepth */
|
||||
extern void ejsSetReturnValue(Ejs *ejs, EjsVar *vp);
|
||||
extern void ejsSetReturnValueAndFree(Ejs *ejs, EjsVar *vp);
|
||||
extern void ejsSetReturnValueToBoolean(Ejs *ejs, bool value);
|
||||
extern void ejsSetReturnValueToBinaryString(Ejs *ejs,
|
||||
const uchar *value, int len);
|
||||
extern void ejsSetReturnValueToInteger(Ejs *ejs, int value);
|
||||
extern void ejsSetReturnValueToNumber(Ejs *ejs, EjsNum value);
|
||||
extern void ejsSetReturnValueToString(Ejs *ejs, const char *value);
|
||||
extern void ejsSetReturnValueToUndefined(Ejs *ejs);
|
||||
|
||||
/*
|
||||
* Variable access and control. The fullName arg can contain "[]" and "."
|
||||
*/
|
||||
extern bool ejsGetBool(Ejs *ejs, const char *fullName, bool defaultValue);
|
||||
extern int ejsGetInt(Ejs *ejs, const char *fullName, int defaultValue);
|
||||
extern const char *ejsGetStr(Ejs *ejs, const char *fullName,
|
||||
const char *defaultValue);
|
||||
extern EjsVar *ejsGetVar(Ejs *ejs, const char *fullName);
|
||||
|
||||
extern int ejsSetBool(Ejs *ejs, const char *fullName, bool value);
|
||||
extern int ejsSetInt(Ejs *ejs, const char *fullName, int value);
|
||||
extern int ejsSetStr(Ejs *ejs, const char *fullName, const char *value);
|
||||
extern int ejsSetVar(Ejs *ejs, const char *fullName, const EjsVar *value);
|
||||
extern int ejsSetVarAndFree(Ejs *ejs, const char *fullName, EjsVar *value);
|
||||
|
||||
extern int ejsDeleteVar(Ejs *ejs, const char *fullName);
|
||||
|
||||
/*
|
||||
* Error handling
|
||||
*/
|
||||
extern void ejsError(Ejs *ejs, const char *errorType, const char *fmt,
|
||||
...) PRINTF_ATTRIBUTE(3,4);
|
||||
/* MOB -- this should take no arguments */
|
||||
extern void ejsArgError(Ejs *ejs, const char *msg);
|
||||
extern void ejsInternalError(Ejs *ejs, const char *msg);
|
||||
extern void ejsMemoryError(Ejs *ejs);
|
||||
extern void ejsSyntaxError(Ejs *ejs, const char *msg);
|
||||
|
||||
/*
|
||||
* Utility methods
|
||||
*/
|
||||
extern int ejsParseArgs(int argc, char **argv, const char *fmt, ...);
|
||||
|
||||
extern void ejsExit(Ejs *ejs, int status);
|
||||
extern bool ejsIsExiting(Ejs *ejs);
|
||||
extern void ejsClearExiting(Ejs *ejs);
|
||||
|
||||
extern bool ejsGotException(Ejs *ejs);
|
||||
|
||||
/* MOB -- rename Method to Function */
|
||||
extern void ejsFreeMethodArgs(Ejs *ep, MprArray *args);
|
||||
extern int ejsStrcat(Ejs *ep, EjsVar *dest, EjsVar *src);
|
||||
|
||||
/*
|
||||
* Debugging routines
|
||||
*/
|
||||
extern char *ejsGetErrorMsg(Ejs *ejs);
|
||||
extern int ejsGetLineNumber(Ejs *ejs);
|
||||
extern void ejsTrace(Ejs *ejs, const char *fmt, ...);
|
||||
|
||||
/*
|
||||
* Multithreaded lock routines
|
||||
*/
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
#define ejsLock(sp) if (sp->lock) { (sp->lock)(sp->lockData); } else
|
||||
#define ejsUnlock(sp) if (sp->unlock) { (sp->unlock)(sp->lockData); } else
|
||||
#else
|
||||
#define ejsLock(sp)
|
||||
#define ejsUnlock(sp)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _h_EJS */
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* @file ejsClass.c
|
||||
* @brief EJS class support
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/************************************ Code ************************************/
|
||||
/*
|
||||
* Internal API
|
||||
*
|
||||
* Routine to create a simple class object. This routine will create a
|
||||
* stand-alone class object. Callers must insert this into the relevant
|
||||
* "global" object for name resolution. From these class objects, instance
|
||||
* objects may be created via the javascript "new" command.
|
||||
*
|
||||
* Users should use ejsDefineClass
|
||||
*/
|
||||
|
||||
EjsVar *ejsCreateSimpleClass(Ejs *ep, EjsVar *baseClass, const char *className)
|
||||
{
|
||||
EjsProperty *pp;
|
||||
EjsVar *classObj;
|
||||
|
||||
/*
|
||||
* Create an instance of an Object to act as the static class object
|
||||
*/
|
||||
classObj = ejsCreateSimpleObjUsingClass(ep, baseClass);
|
||||
if (classObj == 0) {
|
||||
mprAssert(classObj);
|
||||
return 0;
|
||||
}
|
||||
ejsSetClassName(ep, classObj, className);
|
||||
|
||||
/*
|
||||
* Set the propotype property to point to this class.
|
||||
* Note: this is a self reference so the alive bit will not be turned on.
|
||||
*/
|
||||
pp = ejsSetProperty(ep, classObj, "prototype", classObj);
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
|
||||
return classObj;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Define a class in the given interpreter. If parentClass is specified, the
|
||||
* class is defined in the parent. Otherwise, the class will be defined
|
||||
* locally/globally. ClassName and extends are full variable specs
|
||||
* (may contain ".")
|
||||
*/
|
||||
|
||||
EjsVar *ejsDefineClass(Ejs *ep, const char *className, const char *extends,
|
||||
EjsCMethod constructor)
|
||||
{
|
||||
EjsVar *parentClass, *classObj, *baseClass, *vp;
|
||||
char *name;
|
||||
char *cp;
|
||||
|
||||
/*
|
||||
* If the className is a qualified name (with "."), then get the
|
||||
* parent class name.
|
||||
*/
|
||||
name = mprStrdup(ep, className);
|
||||
cp = strrchr(name, '.');
|
||||
if (cp != 0) {
|
||||
*cp++ = '\0';
|
||||
className = cp;
|
||||
parentClass = ejsFindProperty(ep, 0, 0, ep->global, ep->local, name, 0);
|
||||
if (parentClass == 0 || parentClass->type != EJS_TYPE_OBJECT) {
|
||||
mprError(ep, MPR_LOC, "Can't find class's parent class %s", name);
|
||||
mprFree(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Simple class name without a "." so create the class locally
|
||||
* if a local scope exists, otherwise globally.
|
||||
*/
|
||||
parentClass = (ep->local) ? ep->local : ep->global;
|
||||
}
|
||||
|
||||
if (parentClass == 0) {
|
||||
mprError(ep, MPR_LOC, "Can't find parent class");
|
||||
mprFree(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* OPT should use function that doesn't parse [] . */
|
||||
baseClass = ejsGetClass(ep, 0, extends);
|
||||
if (baseClass == 0) {
|
||||
mprAssert(baseClass);
|
||||
mprFree(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
classObj = ejsCreateSimpleClass(ep, baseClass, className);
|
||||
if (classObj == 0) {
|
||||
mprAssert(classObj);
|
||||
mprFree(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (constructor) {
|
||||
ejsDefineCMethod(ep, classObj, className, constructor, 0);
|
||||
}
|
||||
|
||||
ejsSetPropertyAndFree(ep, parentClass, className, classObj);
|
||||
|
||||
vp = ejsGetPropertyAsVar(ep, parentClass, className);
|
||||
mprFree(name);
|
||||
|
||||
return vp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Find a class and return the property defining the class. ClassName may
|
||||
* contain "." and is interpreted relative to obj. Obj is typically some
|
||||
* parent object, ep->local or ep->global. If obj is null, then the global
|
||||
* space is used.
|
||||
*/
|
||||
|
||||
EjsVar *ejsGetClass(Ejs *ep, EjsVar *obj, const char *className)
|
||||
{
|
||||
EjsVar *vp;
|
||||
|
||||
mprAssert(ep);
|
||||
|
||||
/*
|
||||
* Search first for a constructor of the name of class
|
||||
* global may not be defined yet.
|
||||
*/
|
||||
if (obj) {
|
||||
vp = ejsFindProperty(ep, 0, 0, obj, 0, className, 0);
|
||||
|
||||
} else {
|
||||
mprAssert(ep->global);
|
||||
vp = ejsFindProperty(ep, 0, 0, ep->global, ep->local, className, 0);
|
||||
}
|
||||
if (vp == 0 || vp->type != EJS_TYPE_OBJECT) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Return a reference to the prototype (self) reference. This
|
||||
* ensures that even if "obj" is deleted, this reference will remain
|
||||
* usable.
|
||||
*/
|
||||
return ejsGetPropertyAsVar(ep, vp, "prototype");
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the class name of a class or object
|
||||
*/
|
||||
|
||||
const char *ejsGetClassName(EjsVar *vp)
|
||||
{
|
||||
EjsObj *obj;
|
||||
|
||||
mprAssert(vp);
|
||||
mprAssert(vp->type == EJS_TYPE_OBJECT);
|
||||
mprAssert(vp->objectState->baseClass);
|
||||
|
||||
if (vp == 0 || !ejsVarIsObject(vp)) {
|
||||
return 0;
|
||||
}
|
||||
obj = vp->objectState;
|
||||
|
||||
return obj->className;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the class name of an objects underlying class
|
||||
* If called on an object, it returns the base class.
|
||||
* If called on a class, it returns the base class for the class.
|
||||
*/
|
||||
|
||||
const char *ejsGetBaseClassName(EjsVar *vp)
|
||||
{
|
||||
EjsObj *obj;
|
||||
|
||||
mprAssert(vp);
|
||||
mprAssert(vp->type == EJS_TYPE_OBJECT);
|
||||
mprAssert(vp->objectState->baseClass);
|
||||
|
||||
if (vp == 0 || !ejsVarIsObject(vp)) {
|
||||
return 0;
|
||||
}
|
||||
obj = vp->objectState;
|
||||
if (obj->baseClass == 0) {
|
||||
return 0;
|
||||
}
|
||||
mprAssert(obj->baseClass->objectState);
|
||||
|
||||
return obj->baseClass->objectState->className;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
EjsVar *ejsGetBaseClass(EjsVar *vp)
|
||||
{
|
||||
if (vp == 0 || !ejsVarIsObject(vp) || vp->objectState == 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
return vp->objectState->baseClass;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void ejsSetBaseClass(EjsVar *vp, EjsVar *baseClass)
|
||||
{
|
||||
if (vp == 0 || !ejsVarIsObject(vp) || vp->objectState == 0) {
|
||||
mprAssert(0);
|
||||
return;
|
||||
}
|
||||
vp->objectState->baseClass = baseClass;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsProcsDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,468 @@
|
||||
/*
|
||||
* @file ejsCmd.c
|
||||
* @brief Embedded JavaScript (EJS) command line program.
|
||||
* @overview
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS && !BREW
|
||||
|
||||
/************************************ Defines *********************************/
|
||||
|
||||
#define EJS_MAX_CMD_LINE (16 * 1024)
|
||||
#define EJS_MAX_SCRIPT (4 * 1024 * 1024)
|
||||
#define EJS_MAX_RESULT_SIZE (4 * 1024 * 1024)
|
||||
#define EJS_PROMPT "ejs> "
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
|
||||
static int parseFile(EjsService *ejsService, Ejs *ejs, const char *fileName,
|
||||
const char *testName, MprFile *testLogFile);
|
||||
static int ifConsole();
|
||||
|
||||
static int interactiveUse(MprApp *app, Ejs *ejs, FILE *input,
|
||||
char *fileName);
|
||||
static char *readCmd(MprApp *app, FILE *input);
|
||||
|
||||
static int memoryFailure(MprApp *app, uint size, uint total, bool granted);
|
||||
|
||||
static int isConsole = 0;
|
||||
static int traceCmds = 0;
|
||||
static int stats = 0;
|
||||
static int verbose = 0;
|
||||
|
||||
/************************************ Main ************************************/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
MprApp *app;
|
||||
const char *programName;
|
||||
MprFile *testLogFile;
|
||||
EjsService *ejsService;
|
||||
Ejs *ejs;
|
||||
char *commandLine;
|
||||
const char *testName;
|
||||
char *argp, *cmd, *testLog;
|
||||
int i, rc, nextArg, err, len, firstArg, iterations, debugLevel;
|
||||
|
||||
app = mprInit(memoryFailure);
|
||||
|
||||
isConsole = ifConsole();
|
||||
programName = mprGetBaseName(argv[0]);
|
||||
debugLevel = 0;
|
||||
|
||||
ejsService = ejsOpenService(app);
|
||||
if (ejsService == 0) {
|
||||
mprError(app, MPR_LOC, "Can't initialize the EJS service.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
err = 0;
|
||||
iterations = 1;
|
||||
stats = 0;
|
||||
testLog = getenv("TEST_LOG");
|
||||
testLogFile = 0;
|
||||
testName = 0;
|
||||
|
||||
for (nextArg = 1; nextArg < argc; nextArg++) {
|
||||
argp = argv[nextArg];
|
||||
if (*argp != '-') {
|
||||
break;
|
||||
}
|
||||
if (strcmp(argp, "--debug") == 0) {
|
||||
if (nextArg >= argc) {
|
||||
err++;
|
||||
} else {
|
||||
debugLevel = atoi(argv[++nextArg]);
|
||||
}
|
||||
|
||||
} else if (strcmp(argp, "--stats") == 0) {
|
||||
stats++;
|
||||
|
||||
} else if (strcmp(argp, "--trace") == 0) {
|
||||
traceCmds++;
|
||||
|
||||
} else if (strcmp(argp, "--iterations") == 0) {
|
||||
if (nextArg >= argc) {
|
||||
err++;
|
||||
} else {
|
||||
iterations = atoi(argv[++nextArg]);
|
||||
}
|
||||
|
||||
} else if (strcmp(argp, "--log") == 0) {
|
||||
/* Get file to log test results to when using ejs as a test shell */
|
||||
if (nextArg >= argc) {
|
||||
err++;
|
||||
} else {
|
||||
testLog = argv[++nextArg];
|
||||
}
|
||||
|
||||
} else if (strcmp(argp, "--testName") == 0) {
|
||||
if (nextArg >= argc) {
|
||||
err++;
|
||||
} else {
|
||||
testName = argv[++nextArg];
|
||||
}
|
||||
|
||||
} else if (strcmp(argp, "-v") == 0) {
|
||||
verbose++;
|
||||
|
||||
} else if (strcmp(argp, "-vv") == 0) {
|
||||
verbose += 2;
|
||||
|
||||
} else if (strcmp(argp, "--verbose") == 0) {
|
||||
verbose += 2;
|
||||
|
||||
} else {
|
||||
err++;
|
||||
break;
|
||||
}
|
||||
if (err) {
|
||||
mprErrorPrintf(app,
|
||||
"Usage: %s [options] files... or\n"
|
||||
" %s < file or\n"
|
||||
" %s or\n"
|
||||
" Switches:\n"
|
||||
" --iterations num # Number of iterations to eval file\n"
|
||||
" --stats # Output stats on exit\n"
|
||||
" --testName name # Set the test name",
|
||||
programName, programName, programName);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (testName) {
|
||||
i = 0;
|
||||
commandLine = 0;
|
||||
len = mprAllocStrcat(MPR_LOC_ARGS(app), &commandLine, 0, " ",
|
||||
mprGetBaseName(argv[i++]), 0);
|
||||
for (; i < argc; i++) {
|
||||
len = mprReallocStrcat(MPR_LOC_ARGS(app), &commandLine, 0, len,
|
||||
" ", argv[i], 0);
|
||||
}
|
||||
mprPrintf(app, " %s\n", commandLine);
|
||||
}
|
||||
if (testLog) {
|
||||
testLogFile = mprOpen(app, testLog,
|
||||
O_CREAT | O_APPEND | O_WRONLY | O_TEXT, 0664);
|
||||
if (testLogFile == 0) {
|
||||
mprError(app, MPR_LOC, "Can't open %s", testLog);
|
||||
return MPR_ERR_CANT_OPEN;
|
||||
}
|
||||
mprFprintf(testLogFile, "\n %s\n", commandLine);
|
||||
}
|
||||
|
||||
ejs = ejsCreateInterp(ejsService, 0, 0, 0, 0);
|
||||
if (ejs == 0) {
|
||||
mprError(app, MPR_LOC, "Can't create EJS interpreter");
|
||||
ejsCloseService(ejsService, stats);
|
||||
if (testLogFile) {
|
||||
mprClose(testLogFile);
|
||||
}
|
||||
mprTerm(app, stats);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (debugLevel > 0) {
|
||||
ejsSetGCDebugLevel(ejs, debugLevel);
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
|
||||
if (nextArg < argc) {
|
||||
/*
|
||||
* Process files supplied on the command line
|
||||
*/
|
||||
firstArg = nextArg;
|
||||
for (i = 0; i < iterations; i++) {
|
||||
for (nextArg = firstArg; nextArg < argc; nextArg++) {
|
||||
rc = parseFile(ejsService, ejs, argv[nextArg], testName,
|
||||
testLogFile);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (testName) {
|
||||
if (verbose == 1) {
|
||||
mprPrintf(app, "\n");
|
||||
}
|
||||
if (verbose <= 1) {
|
||||
mprPrintf(app, " # PASSED all tests for \"%s\"\n", testName);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (! isConsole) {
|
||||
/*
|
||||
* Read a script from stdin
|
||||
*/
|
||||
cmd = readCmd(app, stdin);
|
||||
|
||||
ejsSetFileName(ejs, "stdin");
|
||||
|
||||
rc = ejsEvalScript(ejs, cmd, 0);
|
||||
if (rc < 0) {
|
||||
mprPrintf(app, "ejs: Error: %s\n", ejsGetErrorMsg(ejs));
|
||||
}
|
||||
mprFree(cmd);
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Interactive use. Read commands from the command line.
|
||||
*/
|
||||
rc = interactiveUse(app, ejs, stdin, "stdin");
|
||||
}
|
||||
|
||||
/*
|
||||
* Cleanup. Do stats if required.
|
||||
*/
|
||||
if (ejs) {
|
||||
ejsCleanInterp(ejs, 0);
|
||||
ejsCleanInterp(ejs->service->master, 0);
|
||||
ejsDestroyInterp(ejs, 0);
|
||||
}
|
||||
|
||||
ejsCloseService(ejsService, stats);
|
||||
|
||||
if (testLogFile) {
|
||||
mprClose(testLogFile);
|
||||
}
|
||||
|
||||
mprTerm(app, stats);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int parseFile(EjsService *ejsService, Ejs *ejs, const char *fileName,
|
||||
const char *testName, MprFile *testLogFile)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (testName && verbose == 1) {
|
||||
mprPrintf(ejs, ".");
|
||||
}
|
||||
if (verbose > 1) {
|
||||
mprPrintf(ejs, "File: %s\n", fileName);
|
||||
}
|
||||
|
||||
rc = ejsEvalFile(ejs, fileName, 0);
|
||||
|
||||
if (testName) {
|
||||
char fileBuf[MPR_MAX_FNAME], *cp;
|
||||
mprStrcpy(fileBuf, sizeof(fileBuf), fileName);
|
||||
if ((cp = strstr(fileBuf, ".ejs")) != 0) {
|
||||
*cp = '\0';
|
||||
}
|
||||
if (rc == 0) {
|
||||
if (verbose > 1) {
|
||||
mprPrintf(ejs, " # PASSED test \"%s.%s\"\n", testName,
|
||||
fileBuf);
|
||||
}
|
||||
if (testLogFile) {
|
||||
mprFprintf(testLogFile, " # PASSED test \"%s.%s\"\n",
|
||||
testName, fileBuf);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
mprPrintf(ejs, "FAILED test \"%s.%s\"\nDetails: %s\n",
|
||||
testName, fileBuf, ejsGetErrorMsg(ejs));
|
||||
|
||||
if (testLogFile) {
|
||||
mprFprintf(testLogFile,
|
||||
"FAILED test \"%s.%s\"\nDetails: %s\n",
|
||||
testName, fileBuf, ejsGetErrorMsg(ejs));
|
||||
}
|
||||
}
|
||||
} else if (rc < 0) {
|
||||
mprPrintf(ejs, "ejs: %sIn file \"%s\"\n",
|
||||
ejsGetErrorMsg(ejs), fileName);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static char *readCmd(MprApp *app, FILE *input)
|
||||
{
|
||||
char line[EJS_MAX_CMD_LINE];
|
||||
char *cmd;
|
||||
int len, cmdLen;
|
||||
|
||||
cmd = 0;
|
||||
cmdLen = 0;
|
||||
|
||||
line[sizeof(line) - 1] = '\0';
|
||||
|
||||
while (1) {
|
||||
|
||||
if (fgets(line, sizeof(line) - 1, input) == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
len = strlen(line);
|
||||
|
||||
if (line[len - 1] == '\\') {
|
||||
line[len - 1] = '\0';
|
||||
}
|
||||
cmdLen = mprReallocStrcat(MPR_LOC_ARGS(app), &cmd, EJS_MAX_SCRIPT,
|
||||
cmdLen, 0, line, 0);
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int interactiveUse(MprApp *app, Ejs *ejs, FILE *input, char *fileName)
|
||||
{
|
||||
EjsVar result;
|
||||
char line[EJS_MAX_CMD_LINE];
|
||||
char *cmd, *buf;
|
||||
int len, cmdLen, rc;
|
||||
|
||||
cmd = 0;
|
||||
cmdLen = 0;
|
||||
|
||||
line[sizeof(line) - 1] = '\0';
|
||||
|
||||
ejsSetFileName(ejs, "console");
|
||||
|
||||
while (! ejsIsExiting(ejs)) {
|
||||
|
||||
if (isConsole) {
|
||||
write(1, EJS_PROMPT, strlen(EJS_PROMPT));
|
||||
}
|
||||
|
||||
if (fgets(line, sizeof(line) - 1, input) == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
len = strlen(line);
|
||||
while (len > 0 &&
|
||||
(line[len - 1] == '\n' || line[len - 1] == '\r')) {
|
||||
len--;
|
||||
line[len] = '\0';
|
||||
}
|
||||
|
||||
if (line[len - 1] == '\\') {
|
||||
line[len - 1] = '\0';
|
||||
cmdLen = mprReallocStrcat(MPR_LOC_ARGS(app), &cmd, EJS_MAX_SCRIPT,
|
||||
cmdLen, 0, line, 0);
|
||||
|
||||
} else {
|
||||
|
||||
cmdLen = mprReallocStrcat(MPR_LOC_ARGS(app), &cmd, EJS_MAX_SCRIPT,
|
||||
cmdLen, 0, line, 0);
|
||||
|
||||
|
||||
if (traceCmds) {
|
||||
mprPrintf(ejs, "# %s\n", cmd);
|
||||
}
|
||||
|
||||
if (cmd[0] == 0x4 || cmd[0] == 0x26 || strcmp(cmd, "quit") == 0) {
|
||||
ejsExit(ejs, 0);
|
||||
|
||||
} else if ((rc = ejsEvalScript(ejs, cmd, &result)) < 0) {
|
||||
|
||||
mprPrintf(app, "ejs: Error: %s\n", ejsGetErrorMsg(ejs));
|
||||
|
||||
if (! isConsole) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (isConsole || traceCmds) {
|
||||
buf = ejsVarToString(ejs, &result);
|
||||
mprPrintf(ejs, "%s\n", buf);
|
||||
}
|
||||
}
|
||||
mprFree(cmd);
|
||||
cmd = 0;
|
||||
cmdLen = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int ifConsole()
|
||||
{
|
||||
#if WIN
|
||||
INPUT_RECORD irec[1];
|
||||
int records = 0;
|
||||
|
||||
if (PeekConsoleInput(GetStdHandle(STD_INPUT_HANDLE), irec, 1,
|
||||
&records) != 0) {
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
return isatty(0);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int memoryFailure(MprApp *app, uint size, uint total, bool granted)
|
||||
{
|
||||
if (!granted) {
|
||||
mprPrintf(app, "Can't allocate memory block of size %d\n", size);
|
||||
mprPrintf(app, "Total memory used %d\n", total);
|
||||
exit(255);
|
||||
}
|
||||
mprPrintf(app, "Memory request for %d bytes exceeds memory red-line\n",
|
||||
size);
|
||||
mprPrintf(app, "Total memory used %d\n", total);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsCmdLineDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+1214
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* @file event.js
|
||||
* @brief Event class
|
||||
* @copy Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*
|
||||
* Usage:
|
||||
* listener = new System.Listener();
|
||||
* listener.onClick = function() {
|
||||
* // Any code here
|
||||
* }
|
||||
* eventTarget.addListener(eventName, listener);
|
||||
* or
|
||||
* listener = new System.Listener(obj, method);
|
||||
* eventTarget.addListener(eventName, listener);
|
||||
*
|
||||
* To fire events:
|
||||
* eventTarget.fire(eventName, new System.Event("My Event"));
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Base event class
|
||||
*/
|
||||
class System.Event
|
||||
{
|
||||
var type; // keyboard
|
||||
var timeStamp;
|
||||
var arg;
|
||||
|
||||
/* MOB -- constructor should take a type */
|
||||
function Event(arg)
|
||||
{
|
||||
timeStamp = time();
|
||||
type = "default";
|
||||
this.arg = arg;
|
||||
}
|
||||
}
|
||||
|
||||
/* MOB -- should not be needed */
|
||||
Event = System.Event;
|
||||
|
||||
class System.Listener
|
||||
{
|
||||
var obj;
|
||||
var method;
|
||||
|
||||
function Listener(obj, method)
|
||||
{
|
||||
if (arguments.length >= 1) {
|
||||
this.obj = obj;
|
||||
} else {
|
||||
this.obj = this;
|
||||
}
|
||||
if (arguments.length == 2) {
|
||||
this.method = method;
|
||||
} else {
|
||||
this.method = "onEvent";
|
||||
}
|
||||
}
|
||||
}
|
||||
/* MOB -- should not be needed */
|
||||
Listener = System.Listener;
|
||||
|
||||
|
||||
/*
|
||||
* The Event target class
|
||||
*/
|
||||
class System.EventTarget
|
||||
{
|
||||
// Private
|
||||
var events; /* Hash of a event names */
|
||||
|
||||
function EventTarget()
|
||||
{
|
||||
events = new Object();
|
||||
}
|
||||
|
||||
// Public
|
||||
function addListener(eventName, listener)
|
||||
{
|
||||
var listeners = events[eventName];
|
||||
if (listeners == undefined) {
|
||||
listeners = events[eventName] = new Array();
|
||||
}
|
||||
if (arguments.length == 2) {
|
||||
var method = eventName;
|
||||
}
|
||||
/* MOB OPT */
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
var l = listeners[i];
|
||||
if (l == listener) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
listeners[listeners.length] = listener;
|
||||
}
|
||||
|
||||
function removeListener(eventName, listener)
|
||||
{
|
||||
var listeners = events[eventName];
|
||||
|
||||
if (listeners == undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < listeners.length; i++) {
|
||||
var l = listeners[i];
|
||||
if (l == listener) {
|
||||
// MOB -- want listeners.splice here
|
||||
// listeners.splice(i, 1);
|
||||
for (var j = i; j < (listeners.length - 1); j++) {
|
||||
listeners[j] = listeners[j + 1];
|
||||
}
|
||||
delete listeners[listeners.length - 1];
|
||||
i = listeners.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function fire(eventName, event)
|
||||
{
|
||||
var listeners = events[eventName];
|
||||
|
||||
if (listeners == undefined) {
|
||||
// println("Event.fire(): unknown eventName " + eventName);
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = listeners.length - 1; i >= 0; i--) {
|
||||
var listener = listeners[i];
|
||||
var method = listener.obj[listener.method];
|
||||
if (method == undefined) {
|
||||
throw new EvalError("Undefined method: " + listener.method);
|
||||
}
|
||||
listener.obj[listener.method](listener, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* MOB -- should not be needed */
|
||||
EventTarget = System.EventTarget;
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* @file global.js
|
||||
* @brief Misc global functions
|
||||
* @copy Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
function min(a, b)
|
||||
{
|
||||
if (a < b) {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function max(a, b)
|
||||
{
|
||||
if (a > b) {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
function abs(a)
|
||||
{
|
||||
if (a < 0) {
|
||||
return -a;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* @file startup.js
|
||||
* @brief Embedded JavaScript Startup Code
|
||||
* @copy Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*
|
||||
* Invoked automatically on startup.
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// println("Loading startup.js ...");
|
||||
|
||||
include("lib/event.js");
|
||||
include("lib/global.js");
|
||||
include("lib/timer.js");
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* @file timer.js
|
||||
* @brief Timer class
|
||||
* @copy Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*
|
||||
* Usage:
|
||||
* timer = new System.Timer("name", period);
|
||||
* timer.onTick = function(arg) {
|
||||
* // Anything here
|
||||
* }
|
||||
* timer.start();
|
||||
* or
|
||||
*
|
||||
* timer = new System.Timer("name", period, obj, method);
|
||||
* timer.start();
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
class System.Timer
|
||||
{
|
||||
var id;
|
||||
|
||||
/* MOB -- really need accessor on period. If user updates period,
|
||||
then due must be updated. */
|
||||
var period;
|
||||
var due;
|
||||
var runOnce; // Run timer just once
|
||||
var method; // Callback method
|
||||
var obj; // Callback object
|
||||
|
||||
function Timer(id, period, obj, method)
|
||||
{
|
||||
this.id = id;
|
||||
this.period = period;
|
||||
due = time() + period;
|
||||
|
||||
if (arguments.length >= 3) {
|
||||
this.obj = obj;
|
||||
} else {
|
||||
this.obj = this;
|
||||
}
|
||||
if (arguments.length >= 4) {
|
||||
this.method = method;
|
||||
} else {
|
||||
this.method = "onTick";
|
||||
}
|
||||
}
|
||||
|
||||
/* MOB this should be deprecated */
|
||||
function reschedule(period)
|
||||
{
|
||||
/* MOB -- should update the timer service somehow */
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
function run(now)
|
||||
{
|
||||
if (obj[method] == undefined) {
|
||||
trace("Timer cant find timer method " + method);
|
||||
due = now + this.period;
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the timer
|
||||
*/
|
||||
try {
|
||||
obj[method](this);
|
||||
}
|
||||
catch (error) {
|
||||
trace("Timer exception: " + error);
|
||||
}
|
||||
|
||||
if (runOnce) {
|
||||
timerService.removeTimer(this);
|
||||
|
||||
} else {
|
||||
due = now + this.period;
|
||||
}
|
||||
}
|
||||
|
||||
function start()
|
||||
{
|
||||
if (obj[method] == undefined) {
|
||||
throw new Error("Callback method is undefined");
|
||||
} else {
|
||||
timerService.addTimer(this);
|
||||
}
|
||||
}
|
||||
|
||||
function stop()
|
||||
{
|
||||
timerService.removeTimer(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* MOB -- should not need this */
|
||||
Timer = System.Timer;
|
||||
|
||||
|
||||
/*
|
||||
* Timer service
|
||||
*/
|
||||
class System.TimerService
|
||||
{
|
||||
var timers;
|
||||
var nextDue;
|
||||
|
||||
function TimerService()
|
||||
{
|
||||
timers = new Object();
|
||||
nextDue = 0;
|
||||
global.timerService = this;
|
||||
}
|
||||
|
||||
function addTimer(timer)
|
||||
{
|
||||
timers[timer.id] = timer;
|
||||
}
|
||||
|
||||
function removeTimer(timer)
|
||||
{
|
||||
try {
|
||||
delete timers[timer.id];
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
function getIdleTime()
|
||||
{
|
||||
return nextDue - time();
|
||||
}
|
||||
|
||||
function runTimers()
|
||||
{
|
||||
var now = time();
|
||||
|
||||
nextDue = 2147483647; /* MOB -- MATH.MAX_INT; */
|
||||
|
||||
for each (var timer in timers)
|
||||
{
|
||||
if (timer.due < now) {
|
||||
timer.run(now);
|
||||
}
|
||||
}
|
||||
for each (var timer in timers)
|
||||
{
|
||||
if (timer.due < nextDue) {
|
||||
nextDue = timer.due;
|
||||
}
|
||||
}
|
||||
// println("runTimers leaving with " + (nextDue - now));
|
||||
return nextDue - time();
|
||||
}
|
||||
}
|
||||
TimerService = System.TimerService;
|
||||
+1
@@ -0,0 +1 @@
|
||||
.updated
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
#
|
||||
# Makefile to build the EJS Object Model
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I.. -I../../mpr -I../../exml
|
||||
|
||||
include make.dep
|
||||
|
||||
ifeq ($(BLD_HOST_UNIX),1)
|
||||
PRE_DIRS = UNIX
|
||||
else
|
||||
PRE_DIRS = $(BLD_HOST_OS)
|
||||
endif
|
||||
|
||||
compileExtra: .updated
|
||||
|
||||
.updated: $(FILES)
|
||||
@touch .updated
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,63 @@
|
||||
Embedded JavaScript System Model
|
||||
|
||||
|
||||
- Need args, arg types and exceptions thrown
|
||||
- Error classes
|
||||
|
||||
class Global
|
||||
class System
|
||||
class environment
|
||||
var
|
||||
class GC
|
||||
void function run()
|
||||
function tune()
|
||||
function getUsedMemory() // Should be properties
|
||||
function getAllocatedMemory() // Should be properties
|
||||
|
||||
var javascript
|
||||
var null
|
||||
var undefined
|
||||
var true
|
||||
var false
|
||||
var Nan
|
||||
var Infinity
|
||||
|
||||
function random // Not implemented
|
||||
function sleep // Not implemented
|
||||
function exit
|
||||
function yield // Not implemented
|
||||
|
||||
Debug
|
||||
isDebugMode
|
||||
|
||||
Limits
|
||||
isLimitsMode // Not implemented
|
||||
stack // Not implemented
|
||||
heap // Not implemented
|
||||
flash // Not implemented
|
||||
|
||||
Memory
|
||||
getUsedMemory() // Should be properties
|
||||
getAvailableMemory() // Should be properties
|
||||
used
|
||||
flash // Not implemented
|
||||
|
||||
assert()
|
||||
breakpoint()
|
||||
dirname()
|
||||
basename()
|
||||
eval()
|
||||
exit()
|
||||
print()
|
||||
println()
|
||||
printVars()
|
||||
sleep()
|
||||
sort()
|
||||
time()
|
||||
typeof()
|
||||
include()
|
||||
trace()
|
||||
printf() // Not implemented
|
||||
sprintf()
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
.updated
|
||||
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# Makefile to build the EJS Object Model for WIN
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I../.. -I../../../mpr
|
||||
|
||||
include make.dep
|
||||
|
||||
compileExtra: .updated
|
||||
|
||||
.updated: $(FILES)
|
||||
@touch .updated
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* @file ejsFile.c
|
||||
* @brief File class for the EJ System Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Default Constructor
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function open();
|
||||
*/
|
||||
|
||||
static int openProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.open()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function close();
|
||||
*/
|
||||
|
||||
static int closeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.close()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function read();
|
||||
*/
|
||||
|
||||
static int readProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.read()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function write();
|
||||
*/
|
||||
|
||||
static int writeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.write()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineFileClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *fileClass;
|
||||
|
||||
fileClass = ejsDefineClass(ep, "File", "Object", 0);
|
||||
if (fileClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ep, fileClass, "open", openProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "close", closeProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "read", readProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "write", writeProc, 0);
|
||||
|
||||
return ejsObjHasErrors(fileClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,454 @@
|
||||
/*
|
||||
* @file ejsFileSystem.c
|
||||
* @brief FileSystem class for the EJ System Object Model
|
||||
* MOB -- this is almost the same as for Windows. Should common up.
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
#include <dirent.h>
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void access(string path);
|
||||
* MOB - API insufficient. Access for read or write?
|
||||
*/
|
||||
|
||||
static int accessProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: access(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = access(argv[0]->string, 04);
|
||||
|
||||
ejsSetReturnValueToBoolean(ejs, (rc == 0) ? 1 : 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void mkdir(string path);
|
||||
*/
|
||||
|
||||
static int mkdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mprMakeDirPath(ejs, argv[0]->string) < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create directory");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void rmdir(string path);
|
||||
*/
|
||||
|
||||
static int rmdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = mprDeleteDir(ejs, argv[0]->string);
|
||||
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant remove directory");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void dirList(string path, [bool enumDirs]);
|
||||
* MOB -- need pattern to match (what about "." and ".." and ".*"
|
||||
*/
|
||||
|
||||
static int dirListProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *dirent;
|
||||
char path[MPR_MAX_FNAME];
|
||||
EjsVar *array, *vp;
|
||||
uchar enumDirs;
|
||||
|
||||
if (argc < 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: dirList(path)");
|
||||
return -1;
|
||||
}
|
||||
if (argc == 2) {
|
||||
enumDirs = ejsVarToBoolean(argv[1]);
|
||||
} else {
|
||||
enumDirs = 0;
|
||||
}
|
||||
array = ejsCreateArray(ejs, 0);
|
||||
ejsMakeObjPermanent(array, 1);
|
||||
|
||||
/*
|
||||
* First collect the files
|
||||
*/
|
||||
mprSprintf(path, sizeof(path), "%s/*.*", argv[0]->string);
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir == 0) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Can't enumerate dirList(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((dirent = readdir(dir)) != 0) {
|
||||
if (dirent->d_name[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
if (!enumDirs || (dirent->d_type & DT_DIR)) {
|
||||
mprSprintf(path, sizeof(path), "%s/%s", argv[0]->string,
|
||||
dirent->d_name);
|
||||
vp = ejsCreateStringVar(ejs, path);
|
||||
ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY);
|
||||
ejsFreeVar(ejs, vp);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
ejsSetReturnValue(ejs, array);
|
||||
ejsMakeObjPermanent(array, 0);
|
||||
|
||||
/*
|
||||
* Can free now as the return value holds the reference
|
||||
*/
|
||||
ejsFreeVar(ejs, array);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void getFreeSpace();
|
||||
*/
|
||||
|
||||
static int getFreeSpaceProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
#if UNUSED
|
||||
MprApp *app;
|
||||
uint space;
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
space = IFILEMGR_GetFreeSpace(app->fileMgr, 0);
|
||||
ejsSetReturnValueToInteger(ejs, space);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void writeFile(string path, var data);
|
||||
*/
|
||||
|
||||
static int writeFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFile *file;
|
||||
char *data, *buf;
|
||||
int bytes, length, rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: writeFile(path, var)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ejsVarIsString(argv[1])) {
|
||||
data = argv[1]->string;
|
||||
length = argv[1]->length;
|
||||
buf = 0;
|
||||
} else {
|
||||
buf = data = ejsVarToString(ejs, argv[1]);
|
||||
length = strlen(data);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create fails if already present
|
||||
*/
|
||||
rc = mprDelete(ejs, argv[0]->string);
|
||||
file = mprOpen(ejs, argv[0]->string, O_CREAT | O_WRONLY | O_BINARY, 0664);
|
||||
if (file == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
bytes = mprWrite(file, data, length);
|
||||
if (bytes != length) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
}
|
||||
|
||||
mprClose(file);
|
||||
|
||||
mprFree(buf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function string readFile(string path);
|
||||
*/
|
||||
|
||||
static int readFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprApp *app;
|
||||
MprFile *file;
|
||||
MprBuf *buf;
|
||||
char *data;
|
||||
int bytes, rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: readFile(path)");
|
||||
return -1;
|
||||
}
|
||||
buf = mprCreateBuf(ejs, MPR_BUF_INCR, MPR_MAX_BUF);
|
||||
if (buf == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = mprAlloc(ejs, MPR_BUFSIZE);
|
||||
if (buf == 0) {
|
||||
mprFree(buf);
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
file = mprOpen(ejs, argv[0]->string, O_RDONLY, 0664);
|
||||
if (file == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
while ((bytes = mprRead(file, data, MPR_BUFSIZE)) > 0) {
|
||||
if (mprPutBlockToBuf(buf, data, bytes) != bytes) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ejsSetReturnValueToBinaryString(ejs, (uchar*) mprGetBufStart(buf),
|
||||
mprGetBufLength(buf));
|
||||
|
||||
mprClose(file);
|
||||
mprFree(data);
|
||||
mprFree(buf);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void remove(string path);
|
||||
*/
|
||||
|
||||
static int removeProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: remove(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = unlink(argv[0]->string);
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant remove file");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void rename(string from, string to);
|
||||
*/
|
||||
|
||||
static int renameProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: rename(old, new)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unlink(argv[1]->string);
|
||||
rc = rename(argv[0]->string, argv[1]->string);
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant rename file");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void copy(string old, string new);
|
||||
*/
|
||||
|
||||
static int copyProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFile *from, *to;
|
||||
char *buf;
|
||||
uint bytes;
|
||||
int rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: copy(old, new)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = mprAlloc(ejs, MPR_BUFSIZE);
|
||||
if (buf == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
from = mprOpen(ejs, argv[0]->string, O_RDONLY | O_BINARY, 0664);
|
||||
if (from == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
to = mprOpen(ejs, argv[1]->string, O_CREAT | O_BINARY, 0664);
|
||||
if (to == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[1]->string);
|
||||
mprClose(from);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
while ((bytes = mprRead(from, buf, MPR_BUFSIZE)) > 0) {
|
||||
if (mprWrite(to, buf, bytes) != bytes) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mprClose(from);
|
||||
mprClose(to);
|
||||
mprFree(buf);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function FileInfo getFileInfo(string path);
|
||||
*
|
||||
* MOB -- should create a real class FileInfo
|
||||
*/
|
||||
|
||||
static int getFileInfoProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFileInfo info;
|
||||
EjsVar *fileInfo;
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: getFileInfo(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fileInfo = ejsCreateObjVar(ejs);
|
||||
if (fileInfo == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
ejsMakeObjPermanent(fileInfo, 1);
|
||||
|
||||
rc = mprGetFileInfo(ejs, argv[0]->string, &info);
|
||||
if (rc < 0) {
|
||||
ejsMakeObjPermanent(fileInfo, 0);
|
||||
ejsFreeVar(ejs, fileInfo);
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant get file info for %s",
|
||||
argv[0]->string);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetPropertyToInteger(ejs, fileInfo, "created", info.ctime);
|
||||
ejsSetPropertyToInteger(ejs, fileInfo, "length", info.size);
|
||||
ejsSetPropertyToBoolean(ejs, fileInfo, "isDir", info.isDir);
|
||||
|
||||
ejsSetReturnValue(ejs, fileInfo);
|
||||
ejsMakeObjPermanent(fileInfo, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineFileSystemClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *fileSystemClass;
|
||||
|
||||
fileSystemClass = ejsDefineClass(ejs, "FileSystem", "Object", 0);
|
||||
if (fileSystemClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "access", accessProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "mkdir", mkdirProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "rmdir", rmdirProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "dirList", dirListProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "writeFile", writeFileProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "readFile", readFileProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "remove", removeProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "rename", renameProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "copy", copyProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "getFileInfo", getFileInfoProc, 0);
|
||||
|
||||
// MOB -- should be a property with accessor
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "getFreeSpace", getFreeSpaceProc, 0);
|
||||
|
||||
return ejsObjHasErrors(fileSystemClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+488
@@ -0,0 +1,488 @@
|
||||
/*
|
||||
* @file ejsHTTP.c
|
||||
* @brief HTTP class for the EJ System Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if UNUSED
|
||||
/*********************************** Defines **********************************/
|
||||
|
||||
#define EJS_WEB_PROPERTY "-web"
|
||||
#define EJS_HTTP_PROPERTY "-http"
|
||||
|
||||
#define EJS_HTTP_DISPOSED 550
|
||||
|
||||
/*
|
||||
* Control structure for one HTTP request structure
|
||||
*/
|
||||
typedef struct HTTPControl {
|
||||
Ejs *ejs;
|
||||
IWebResp *webResp;
|
||||
AEECallback *callback;
|
||||
MprBuf *buf;
|
||||
EjsVar *thisObj;
|
||||
char *url;
|
||||
MprTime requestStarted;
|
||||
uint timeout;
|
||||
} HTTPControl;
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
|
||||
static void cleanup(HTTPControl *hp);
|
||||
static int createWeb(Ejs *ejs, EjsVar *thisObj);
|
||||
static void brewCallback(HTTPControl *hp);
|
||||
static int httpDestructor(Ejs *ejs, EjsVar *vp);
|
||||
static void httpCallback(HTTPControl *hp, int responseCode);
|
||||
static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv);
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
int ejsHTTPConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 0 && argc != 2) {
|
||||
ejsError(ejs, EJS_ARG_ERROR,
|
||||
"Bad usage: HTTP([obj = this, method = onComplete]);");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (createWeb(ejs, thisObj) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
setCallback(ejs, thisObj, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int createWeb(Ejs *ejs, EjsVar *thisObj)
|
||||
{
|
||||
MprApp *app;
|
||||
void *web;
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
|
||||
/*
|
||||
* Create one instance of IWeb for the entire application. Do it here
|
||||
* so only widgets that require HTTP incurr the overhead.
|
||||
*/
|
||||
web = mprGetKeyValue(ejs, "bpWeb");
|
||||
if (web == 0) {
|
||||
if (ISHELL_CreateInstance(app->shell, AEECLSID_WEB, &web) != SUCCESS) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Can't create IWEB");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
mprSetKeyValue(ejs, "bpWeb", web);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function setCallback(obj, methodString);
|
||||
*/
|
||||
|
||||
static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc >= 1) {
|
||||
ejsSetProperty(ejs, thisObj, "obj", argv[0]);
|
||||
} else {
|
||||
ejsSetProperty(ejs, thisObj, "obj", thisObj);
|
||||
}
|
||||
|
||||
if (argc >= 2) {
|
||||
ejsSetProperty(ejs, thisObj, "method", argv[1]);
|
||||
} else {
|
||||
ejsSetPropertyToString(ejs, thisObj, "method", "onComplete");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function fetch();
|
||||
*/
|
||||
|
||||
static int fetchProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
EjsProperty *pp;
|
||||
MprApp *app;
|
||||
IWeb *web;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: fetch(url)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");
|
||||
|
||||
/*
|
||||
* Web options
|
||||
*
|
||||
* WEBOPT_USERAGENT (char*) sets user agent
|
||||
* WEBOPT_HANDLERDATA (void*)
|
||||
* WEBOPT_CONNECTTIMEOUT (uint) msec
|
||||
* WEBOPT_CONTENTLENGTH (long)
|
||||
* WEBOPT_IDLECONNTIMEOUT (int)
|
||||
* WEBOPT_ACTIVEXACTIONST (uint) Number of active requests
|
||||
*
|
||||
* WEBREQUEST_REDIRECT redirect transparently
|
||||
*
|
||||
*/
|
||||
|
||||
hp = mprAllocType(ejs, HTTPControl);
|
||||
if (hp == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hp->ejs = ejs;
|
||||
hp->buf = mprCreateBuf(hp, MPR_BUF_INCR, MPR_MAX_BUF);
|
||||
if (hp->buf == 0) {
|
||||
mprFree(hp);
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* We copy thisObj because we need to preserve both the var and the object.
|
||||
* We pass the var to brewCallback and so it must persist. The call to
|
||||
* ejsMakeObjPermanent will stop the GC from collecting the object.
|
||||
*/
|
||||
hp->thisObj = ejsDupVar(ejs, thisObj, EJS_SHALLOW_COPY);
|
||||
ejsSetVarName(ejs, hp->thisObj, "internalHttp");
|
||||
|
||||
/*
|
||||
* Must keep a reference to the http object
|
||||
*/
|
||||
ejsMakeObjPermanent(hp->thisObj, 1);
|
||||
|
||||
/*
|
||||
* Make a property so we can access the HTTPControl structure from other
|
||||
* methods.
|
||||
*/
|
||||
pp = ejsSetPropertyToPtr(ejs, thisObj, EJS_HTTP_PROPERTY, hp, 0);
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
ejsSetObjDestructor(ejs, hp->thisObj, httpDestructor);
|
||||
|
||||
hp->url = mprStrdup(hp, argv[0]->string);
|
||||
|
||||
hp->timeout = ejsGetPropertyAsInteger(ejs, thisObj, "timeout");
|
||||
mprGetTime(hp, &hp->requestStarted);
|
||||
|
||||
hp->callback = mprAllocTypeZeroed(hp, AEECallback);
|
||||
CALLBACK_Init(hp->callback, brewCallback, hp);
|
||||
|
||||
hp->webResp = 0;
|
||||
IWEB_GetResponse(web,
|
||||
(web, &hp->webResp, hp->callback, hp->url,
|
||||
WEBOPT_HANDLERDATA, hp,
|
||||
WEBOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)",
|
||||
WEBOPT_CONNECTTIMEOUT, hp->timeout,
|
||||
WEBOPT_COPYOPTS, TRUE,
|
||||
WEBOPT_CONTENTLENGTH, 0,
|
||||
WEBOPT_END));
|
||||
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "active");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Called whenver the http object is deleted.
|
||||
*/
|
||||
|
||||
static int httpDestructor(Ejs *ejs, EjsVar *thisObj)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
|
||||
/*
|
||||
* If the httpCallback has run, then this property will not exist
|
||||
*/
|
||||
hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
if (hp) {
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Stop the request immediately without calling the callback
|
||||
*/
|
||||
|
||||
static int stopProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
|
||||
hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
if (hp) {
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Brew HTTP callback. Invoked for any return data.
|
||||
*/
|
||||
|
||||
static void brewCallback(HTTPControl *hp)
|
||||
{
|
||||
Ejs *ejs;
|
||||
EjsVar *thisObj;
|
||||
ISource *source;
|
||||
WebRespInfo *info;
|
||||
char data[MPR_BUF_INCR];
|
||||
int bytes;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
info = IWEBRESP_GetInfo(hp->webResp);
|
||||
|
||||
if (info == 0) {
|
||||
mprAssert(info);
|
||||
/* should not happen */
|
||||
return;
|
||||
}
|
||||
|
||||
ejs = hp->ejs;
|
||||
thisObj = hp->thisObj;
|
||||
|
||||
if (! WEB_ERROR_SUCCEEDED(info->nCode)) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "error");
|
||||
httpCallback(hp, info->nCode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (hp->timeout) {
|
||||
if (mprGetTimeRemaining(hp, hp->requestStarted, hp->timeout) <= 0) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "timeout");
|
||||
httpCallback(hp, 504);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Normal success
|
||||
*/
|
||||
source = info->pisMessage;
|
||||
mprAssert(source);
|
||||
|
||||
bytes = ISOURCE_Read(source, data, sizeof(data));
|
||||
|
||||
switch (bytes) {
|
||||
case ISOURCE_WAIT: // No data yet
|
||||
ISOURCE_Readable(source, hp->callback);
|
||||
break;
|
||||
|
||||
case ISOURCE_ERROR:
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "error");
|
||||
httpCallback(hp, info->nCode);
|
||||
break;
|
||||
|
||||
case ISOURCE_END:
|
||||
mprAddNullToBuf(hp->buf);
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "complete");
|
||||
httpCallback(hp, info->nCode);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (bytes > 0) {
|
||||
if (mprPutBlockToBuf(hp->buf, data, bytes) != bytes) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "partialData");
|
||||
httpCallback(hp, 500);
|
||||
}
|
||||
}
|
||||
ISOURCE_Readable(source, hp->callback);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Invoke the HTTP completion method
|
||||
*/
|
||||
|
||||
static void httpCallback(HTTPControl *hp, int responseCode)
|
||||
{
|
||||
Ejs *ejs;
|
||||
EjsVar *thisObj, *callbackObj;
|
||||
MprArray *args;
|
||||
char *msg;
|
||||
const char *callbackMethod;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
thisObj = hp->thisObj;
|
||||
ejs = hp->ejs;
|
||||
|
||||
ejsSetPropertyToInteger(ejs, thisObj, "responseCode", responseCode);
|
||||
if (mprGetBufLength(hp->buf) > 0) {
|
||||
ejsSetPropertyToBinaryString(ejs, thisObj, "responseData",
|
||||
mprGetBufStart(hp->buf), mprGetBufLength(hp->buf));
|
||||
}
|
||||
|
||||
callbackObj = ejsGetPropertyAsVar(ejs, thisObj, "obj");
|
||||
callbackMethod = ejsGetPropertyAsString(ejs, thisObj, "method");
|
||||
|
||||
if (callbackObj != 0 && callbackMethod != 0) {
|
||||
|
||||
args = mprCreateItemArray(ejs, EJS_INC_ARGS, EJS_MAX_ARGS);
|
||||
mprAddItem(args, ejsDupVar(ejs, hp->thisObj, EJS_SHALLOW_COPY));
|
||||
|
||||
if (ejsRunMethod(ejs, callbackObj, callbackMethod, args) < 0) {
|
||||
msg = ejsGetErrorMsg(ejs);
|
||||
mprError(ejs, MPR_LOC, "HTTP callback failed. Details: %s", msg);
|
||||
}
|
||||
ejsFreeMethodArgs(ejs, args);
|
||||
|
||||
} else if (ejsRunMethod(ejs, thisObj, "onComplete", 0) < 0) {
|
||||
msg = ejsGetErrorMsg(ejs);
|
||||
mprError(ejs, MPR_LOC, "HTTP onComplete failed. Details: %s", msg);
|
||||
}
|
||||
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Cleanup
|
||||
*/
|
||||
|
||||
static void cleanup(HTTPControl *hp)
|
||||
{
|
||||
Ejs *ejs;
|
||||
MprApp *app;
|
||||
int rc;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
ejs = hp->ejs;
|
||||
|
||||
if (hp->webResp) {
|
||||
rc = IWEBRESP_Release(hp->webResp);
|
||||
// mprAssert(rc == 0);
|
||||
hp->webResp = 0;
|
||||
}
|
||||
|
||||
if (hp->callback) {
|
||||
CALLBACK_Cancel(hp->callback);
|
||||
mprFree(hp->callback);
|
||||
hp->callback = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Once the property is deleted, then if the destructor runs, it will
|
||||
* notice that the EJS_HTTP_PROPERTY is undefined.
|
||||
*/
|
||||
ejsDeleteProperty(ejs, hp->thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
/*
|
||||
* Allow garbage collection to work on thisObj
|
||||
*/
|
||||
ejsMakeObjPermanent(hp->thisObj, 0);
|
||||
ejsFreeVar(ejs, hp->thisObj);
|
||||
|
||||
mprFree(hp->buf);
|
||||
mprFree(hp->url);
|
||||
|
||||
mprFree(hp);
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
|
||||
|
||||
ISHELL_SendEvent(app->shell, (AEECLSID) app->classId, EVT_USER, 0, 0);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineHTTPClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *httpClass;
|
||||
|
||||
httpClass =
|
||||
ejsDefineClass(ejs, "HTTP", "Object", ejsHTTPConstructor);
|
||||
if (httpClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ejs, httpClass, "fetch", fetchProc, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "stop", stopProc, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "setCallback", setCallback, 0);
|
||||
|
||||
#if FUTURE
|
||||
ejsDefineCMethod(ejs, httpClass, "put", put, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "upload", upload, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addUploadFile", addUploadFile, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addPostData", addPostData, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "setUserPassword", setUserPassword, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addCookie", addCookie, 0);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define properties
|
||||
*/
|
||||
ejsSetPropertyToString(ejs, httpClass, "status", "inactive");
|
||||
|
||||
/* This default should come from player.xml */
|
||||
|
||||
ejsSetPropertyToInteger(ejs, httpClass, "timeout", 30 * 1000);
|
||||
ejsSetPropertyToInteger(ejs, httpClass, "responseCode", 0);
|
||||
|
||||
return ejsObjHasErrors(httpClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void ejsTermHTTPClass(Ejs *ejs)
|
||||
{
|
||||
IWeb *web;
|
||||
int rc;
|
||||
|
||||
web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");
|
||||
if (web) {
|
||||
rc = IWEB_Release(web);
|
||||
mprAssert(rc == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1 @@
|
||||
.updated
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# Makefile to build the EJS Object Model for WIN
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I../.. -I../../../mpr
|
||||
|
||||
include make.dep
|
||||
|
||||
compileExtra: .updated
|
||||
|
||||
.updated: $(FILES)
|
||||
@touch .updated
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* @file ejsFile.c
|
||||
* @brief File class for the EJScript System Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Default Constructor
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function open();
|
||||
*/
|
||||
|
||||
static int openProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.open()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function close();
|
||||
*/
|
||||
|
||||
static int closeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.close()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function read();
|
||||
*/
|
||||
|
||||
static int readProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.read()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function write();
|
||||
*/
|
||||
|
||||
static int writeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "File.write()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineFileClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *fileClass;
|
||||
|
||||
fileClass = ejsDefineClass(ep, "File", "Object", 0);
|
||||
if (fileClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ep, fileClass, "open", openProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "close", closeProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "read", readProc, 0);
|
||||
ejsDefineCMethod(ep, fileClass, "write", writeProc, 0);
|
||||
|
||||
return ejsObjHasErrors(fileClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,456 @@
|
||||
/*
|
||||
* @file ejsFileSystem.c
|
||||
* @brief FileSystem class for the EJ System Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Default Constructor
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void access(string path);
|
||||
* MOB - API insufficient. Access for read or write?
|
||||
*/
|
||||
|
||||
static int accessProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: access(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = access(argv[0]->string, 04);
|
||||
|
||||
ejsSetReturnValueToBoolean(ejs, (rc == 0) ? 1 : 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void mkdir(string path);
|
||||
*/
|
||||
|
||||
static int mkdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mprMakeDirPath(ejs, argv[0]->string) < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create directory");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void rmdir(string path);
|
||||
*/
|
||||
|
||||
static int rmdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = mprDeleteDir(ejs, argv[0]->string);
|
||||
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant remove directory");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void dirList(string path, [bool enumDirs]);
|
||||
* MOB -- need pattern to match (what about "." and ".." and ".*"
|
||||
*/
|
||||
|
||||
static int dirListProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
WIN32_FIND_DATA findData;
|
||||
HANDLE h;
|
||||
char path[MPR_MAX_FNAME];
|
||||
EjsVar *array, *vp;
|
||||
uchar enumDirs;
|
||||
|
||||
if (argc < 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: dirList(path)");
|
||||
return -1;
|
||||
}
|
||||
if (argc == 2) {
|
||||
enumDirs = ejsVarToBoolean(argv[1]);
|
||||
} else {
|
||||
enumDirs = 0;
|
||||
}
|
||||
array = ejsCreateArray(ejs, 0);
|
||||
ejsMakeObjPermanent(array, 1);
|
||||
|
||||
/*
|
||||
* First collect the files
|
||||
*/
|
||||
mprSprintf(path, sizeof(path), "%s/*.*", argv[0]->string);
|
||||
h = FindFirstFile(path, &findData);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Can't enumerate dirList(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
if (findData.cFileName[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
if (!enumDirs ||
|
||||
(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
||||
mprSprintf(path, sizeof(path), "%s/%s", argv[0]->string,
|
||||
findData.cFileName);
|
||||
vp = ejsCreateStringVar(ejs, path);
|
||||
ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY);
|
||||
ejsFreeVar(ejs, vp);
|
||||
}
|
||||
} while (FindNextFile(h, &findData) != 0);
|
||||
|
||||
FindClose(h);
|
||||
|
||||
ejsSetReturnValue(ejs, array);
|
||||
ejsMakeObjPermanent(array, 0);
|
||||
|
||||
/*
|
||||
* Can free now as the return value holds the reference
|
||||
*/
|
||||
ejsFreeVar(ejs, array);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void getFreeSpace();
|
||||
*/
|
||||
|
||||
static int getFreeSpaceProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
#if UNUSED
|
||||
MprApp *app;
|
||||
uint space;
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
space = IFILEMGR_GetFreeSpace(app->fileMgr, 0);
|
||||
ejsSetReturnValueToInteger(ejs, space);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void writeFile(string path, var data);
|
||||
*/
|
||||
|
||||
static int writeFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFile *file;
|
||||
char *data, *buf;
|
||||
int bytes, length, rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: writeFile(path, var)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ejsVarIsString(argv[1])) {
|
||||
data = argv[1]->string;
|
||||
length = argv[1]->length;
|
||||
buf = 0;
|
||||
} else {
|
||||
buf = data = ejsVarToString(ejs, argv[1]);
|
||||
length = strlen(data);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create fails if already present
|
||||
*/
|
||||
rc = mprDelete(ejs, argv[0]->string);
|
||||
file = mprOpen(ejs, argv[0]->string, O_CREAT | O_WRONLY | O_BINARY, 0664);
|
||||
if (file == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
bytes = mprWrite(file, data, length);
|
||||
if (bytes != length) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
}
|
||||
|
||||
mprClose(file);
|
||||
|
||||
mprFree(buf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function string readFile(string path);
|
||||
*/
|
||||
|
||||
static int readFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprApp *app;
|
||||
MprFile *file;
|
||||
MprBuf *buf;
|
||||
char *data;
|
||||
int bytes, rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: readFile(path)");
|
||||
return -1;
|
||||
}
|
||||
buf = mprCreateBuf(ejs, MPR_BUF_INCR, MPR_MAX_BUF);
|
||||
if (buf == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
data = mprAlloc(ejs, MPR_BUFSIZE);
|
||||
if (buf == 0) {
|
||||
mprFree(buf);
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
file = mprOpen(ejs, argv[0]->string, O_RDONLY, 0664);
|
||||
if (file == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
while ((bytes = mprRead(file, data, MPR_BUFSIZE)) > 0) {
|
||||
if (mprPutBlockToBuf(buf, data, bytes) != bytes) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ejsSetReturnValueToBinaryString(ejs, mprGetBufStart(buf),
|
||||
mprGetBufLength(buf));
|
||||
|
||||
mprClose(file);
|
||||
mprFree(data);
|
||||
mprFree(buf);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void remove(string path);
|
||||
*/
|
||||
|
||||
static int removeProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: remove(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = unlink(argv[0]->string);
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant remove file");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void rename(string from, string to);
|
||||
*/
|
||||
|
||||
static int renameProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: rename(old, new)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unlink(argv[1]->string);
|
||||
rc = rename(argv[0]->string, argv[1]->string);
|
||||
if (rc < 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant rename file");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void copy(string old, string new);
|
||||
*/
|
||||
|
||||
static int copyProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFile *from, *to;
|
||||
char *buf;
|
||||
int bytes, rc;
|
||||
|
||||
if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: copy(old, new)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = mprAlloc(ejs, MPR_BUFSIZE);
|
||||
if (buf == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
from = mprOpen(ejs, argv[0]->string, O_RDONLY | O_BINARY, 0664);
|
||||
if (from == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
to = mprOpen(ejs, argv[1]->string, O_CREAT | O_BINARY, 0664);
|
||||
if (to == 0) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[1]->string);
|
||||
mprClose(from);
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = 0;
|
||||
while ((bytes = mprRead(from, buf, MPR_BUFSIZE)) > 0) {
|
||||
if (mprWrite(to, buf, bytes) != bytes) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string);
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mprClose(from);
|
||||
mprClose(to);
|
||||
mprFree(buf);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function FileInfo getFileInfo(string path);
|
||||
*
|
||||
* MOB -- should create a real class FileInfo
|
||||
*/
|
||||
|
||||
static int getFileInfoProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprFileInfo info;
|
||||
EjsVar *fileInfo;
|
||||
int rc;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: getFileInfo(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fileInfo = ejsCreateObjVar(ejs);
|
||||
if (fileInfo == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
ejsMakeObjPermanent(fileInfo, 1);
|
||||
|
||||
rc = mprGetFileInfo(ejs, argv[0]->string, &info);
|
||||
if (rc < 0) {
|
||||
ejsMakeObjPermanent(fileInfo, 0);
|
||||
ejsFreeVar(ejs, fileInfo);
|
||||
ejsError(ejs, EJS_IO_ERROR, "Cant get file info for %s",
|
||||
argv[0]->string);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetPropertyToInteger(ejs, fileInfo, "created", info.ctime);
|
||||
ejsSetPropertyToInteger(ejs, fileInfo, "length", info.size);
|
||||
ejsSetPropertyToBoolean(ejs, fileInfo, "isDir", info.isDir);
|
||||
|
||||
ejsSetReturnValue(ejs, fileInfo);
|
||||
ejsMakeObjPermanent(fileInfo, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineFileSystemClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *fileSystemClass;
|
||||
|
||||
fileSystemClass = ejsDefineClass(ejs, "FileSystem", "Object", 0);
|
||||
if (fileSystemClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "access", accessProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "mkdir", mkdirProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "rmdir", rmdirProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "dirList", dirListProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "writeFile", writeFileProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "readFile", readFileProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "remove", removeProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "rename", renameProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "copy", copyProc, 0);
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "getFileInfo", getFileInfoProc, 0);
|
||||
|
||||
// MOB -- should be a property with accessor
|
||||
ejsDefineCMethod(ejs, fileSystemClass, "getFreeSpace", getFreeSpaceProc, 0);
|
||||
|
||||
return ejsObjHasErrors(fileSystemClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+488
@@ -0,0 +1,488 @@
|
||||
/*
|
||||
* @file ejsHTTP.c
|
||||
* @brief HTTP class for the EJ System Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if UNUSED
|
||||
/*********************************** Defines **********************************/
|
||||
|
||||
#define EJS_WEB_PROPERTY "-web"
|
||||
#define EJS_HTTP_PROPERTY "-http"
|
||||
|
||||
#define EJS_HTTP_DISPOSED 550
|
||||
|
||||
/*
|
||||
* Control structure for one HTTP request structure
|
||||
*/
|
||||
typedef struct HTTPControl {
|
||||
Ejs *ejs;
|
||||
IWebResp *webResp;
|
||||
AEECallback *callback;
|
||||
MprBuf *buf;
|
||||
EjsVar *thisObj;
|
||||
char *url;
|
||||
MprTime requestStarted;
|
||||
uint timeout;
|
||||
} HTTPControl;
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
|
||||
static void cleanup(HTTPControl *hp);
|
||||
static int createWeb(Ejs *ejs, EjsVar *thisObj);
|
||||
static void brewCallback(HTTPControl *hp);
|
||||
static int httpDestructor(Ejs *ejs, EjsVar *vp);
|
||||
static void httpCallback(HTTPControl *hp, int responseCode);
|
||||
static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv);
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
int ejsHTTPConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 0 && argc != 2) {
|
||||
ejsError(ejs, EJS_ARG_ERROR,
|
||||
"Bad usage: HTTP([obj = this, method = onComplete]);");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (createWeb(ejs, thisObj) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
setCallback(ejs, thisObj, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int createWeb(Ejs *ejs, EjsVar *thisObj)
|
||||
{
|
||||
MprApp *app;
|
||||
void *web;
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
|
||||
/*
|
||||
* Create one instance of IWeb for the entire application. Do it here
|
||||
* so only widgets that require HTTP incurr the overhead.
|
||||
*/
|
||||
web = mprGetKeyValue(ejs, "bpWeb");
|
||||
if (web == 0) {
|
||||
if (ISHELL_CreateInstance(app->shell, AEECLSID_WEB, &web) != SUCCESS) {
|
||||
ejsError(ejs, EJS_IO_ERROR, "Can't create IWEB");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
mprSetKeyValue(ejs, "bpWeb", web);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function setCallback(obj, methodString);
|
||||
*/
|
||||
|
||||
static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc >= 1) {
|
||||
ejsSetProperty(ejs, thisObj, "obj", argv[0]);
|
||||
} else {
|
||||
ejsSetProperty(ejs, thisObj, "obj", thisObj);
|
||||
}
|
||||
|
||||
if (argc >= 2) {
|
||||
ejsSetProperty(ejs, thisObj, "method", argv[1]);
|
||||
} else {
|
||||
ejsSetPropertyToString(ejs, thisObj, "method", "onComplete");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function fetch();
|
||||
*/
|
||||
|
||||
static int fetchProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
EjsProperty *pp;
|
||||
MprApp *app;
|
||||
IWeb *web;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsError(ejs, EJS_ARG_ERROR, "Bad usage: fetch(url)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");
|
||||
|
||||
/*
|
||||
* Web options
|
||||
*
|
||||
* WEBOPT_USERAGENT (char*) sets user agent
|
||||
* WEBOPT_HANDLERDATA (void*)
|
||||
* WEBOPT_CONNECTTIMEOUT (uint) msec
|
||||
* WEBOPT_CONTENTLENGTH (long)
|
||||
* WEBOPT_IDLECONNTIMEOUT (int)
|
||||
* WEBOPT_ACTIVEXACTIONST (uint) Number of active requests
|
||||
*
|
||||
* WEBREQUEST_REDIRECT redirect transparently
|
||||
*
|
||||
*/
|
||||
|
||||
hp = mprAllocType(ejs, HTTPControl);
|
||||
if (hp == 0) {
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hp->ejs = ejs;
|
||||
hp->buf = mprCreateBuf(hp, MPR_BUF_INCR, MPR_MAX_BUF);
|
||||
if (hp->buf == 0) {
|
||||
mprFree(hp);
|
||||
ejsMemoryError(ejs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* We copy thisObj because we need to preserve both the var and the object.
|
||||
* We pass the var to brewCallback and so it must persist. The call to
|
||||
* ejsMakeObjPermanent will stop the GC from collecting the object.
|
||||
*/
|
||||
hp->thisObj = ejsDupVar(ejs, thisObj, EJS_SHALLOW_COPY);
|
||||
ejsSetVarName(ejs, hp->thisObj, "internalHttp");
|
||||
|
||||
/*
|
||||
* Must keep a reference to the http object
|
||||
*/
|
||||
ejsMakeObjPermanent(hp->thisObj, 1);
|
||||
|
||||
/*
|
||||
* Make a property so we can access the HTTPControl structure from other
|
||||
* methods.
|
||||
*/
|
||||
pp = ejsSetPropertyToPtr(ejs, thisObj, EJS_HTTP_PROPERTY, hp, 0);
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
ejsSetObjDestructor(ejs, hp->thisObj, httpDestructor);
|
||||
|
||||
hp->url = mprStrdup(hp, argv[0]->string);
|
||||
|
||||
hp->timeout = ejsGetPropertyAsInteger(ejs, thisObj, "timeout");
|
||||
mprGetTime(hp, &hp->requestStarted);
|
||||
|
||||
hp->callback = mprAllocTypeZeroed(hp, AEECallback);
|
||||
CALLBACK_Init(hp->callback, brewCallback, hp);
|
||||
|
||||
hp->webResp = 0;
|
||||
IWEB_GetResponse(web,
|
||||
(web, &hp->webResp, hp->callback, hp->url,
|
||||
WEBOPT_HANDLERDATA, hp,
|
||||
WEBOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)",
|
||||
WEBOPT_CONNECTTIMEOUT, hp->timeout,
|
||||
WEBOPT_COPYOPTS, TRUE,
|
||||
WEBOPT_CONTENTLENGTH, 0,
|
||||
WEBOPT_END));
|
||||
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "active");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Called whenver the http object is deleted.
|
||||
*/
|
||||
|
||||
static int httpDestructor(Ejs *ejs, EjsVar *thisObj)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
|
||||
/*
|
||||
* If the httpCallback has run, then this property will not exist
|
||||
*/
|
||||
hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
if (hp) {
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Stop the request immediately without calling the callback
|
||||
*/
|
||||
|
||||
static int stopProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
HTTPControl *hp;
|
||||
|
||||
hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
if (hp) {
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Brew HTTP callback. Invoked for any return data.
|
||||
*/
|
||||
|
||||
static void brewCallback(HTTPControl *hp)
|
||||
{
|
||||
Ejs *ejs;
|
||||
EjsVar *thisObj;
|
||||
ISource *source;
|
||||
WebRespInfo *info;
|
||||
char data[MPR_BUF_INCR];
|
||||
int bytes;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
info = IWEBRESP_GetInfo(hp->webResp);
|
||||
|
||||
if (info == 0) {
|
||||
mprAssert(info);
|
||||
/* should not happen */
|
||||
return;
|
||||
}
|
||||
|
||||
ejs = hp->ejs;
|
||||
thisObj = hp->thisObj;
|
||||
|
||||
if (! WEB_ERROR_SUCCEEDED(info->nCode)) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "error");
|
||||
httpCallback(hp, info->nCode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (hp->timeout) {
|
||||
if (mprGetTimeRemaining(hp, hp->requestStarted, hp->timeout) <= 0) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "timeout");
|
||||
httpCallback(hp, 504);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Normal success
|
||||
*/
|
||||
source = info->pisMessage;
|
||||
mprAssert(source);
|
||||
|
||||
bytes = ISOURCE_Read(source, data, sizeof(data));
|
||||
|
||||
switch (bytes) {
|
||||
case ISOURCE_WAIT: // No data yet
|
||||
ISOURCE_Readable(source, hp->callback);
|
||||
break;
|
||||
|
||||
case ISOURCE_ERROR:
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "error");
|
||||
httpCallback(hp, info->nCode);
|
||||
break;
|
||||
|
||||
case ISOURCE_END:
|
||||
mprAddNullToBuf(hp->buf);
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "complete");
|
||||
httpCallback(hp, info->nCode);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (bytes > 0) {
|
||||
if (mprPutBlockToBuf(hp->buf, data, bytes) != bytes) {
|
||||
ejsSetPropertyToString(ejs, thisObj, "status", "partialData");
|
||||
httpCallback(hp, 500);
|
||||
}
|
||||
}
|
||||
ISOURCE_Readable(source, hp->callback);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Invoke the HTTP completion method
|
||||
*/
|
||||
|
||||
static void httpCallback(HTTPControl *hp, int responseCode)
|
||||
{
|
||||
Ejs *ejs;
|
||||
EjsVar *thisObj, *callbackObj;
|
||||
MprArray *args;
|
||||
char *msg;
|
||||
const char *callbackMethod;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
thisObj = hp->thisObj;
|
||||
ejs = hp->ejs;
|
||||
|
||||
ejsSetPropertyToInteger(ejs, thisObj, "responseCode", responseCode);
|
||||
if (mprGetBufLength(hp->buf) > 0) {
|
||||
ejsSetPropertyToBinaryString(ejs, thisObj, "responseData",
|
||||
mprGetBufStart(hp->buf), mprGetBufLength(hp->buf));
|
||||
}
|
||||
|
||||
callbackObj = ejsGetPropertyAsVar(ejs, thisObj, "obj");
|
||||
callbackMethod = ejsGetPropertyAsString(ejs, thisObj, "method");
|
||||
|
||||
if (callbackObj != 0 && callbackMethod != 0) {
|
||||
|
||||
args = mprCreateItemArray(ejs, EJS_INC_ARGS, EJS_MAX_ARGS);
|
||||
mprAddItem(args, ejsDupVar(ejs, hp->thisObj, EJS_SHALLOW_COPY));
|
||||
|
||||
if (ejsRunMethod(ejs, callbackObj, callbackMethod, args) < 0) {
|
||||
msg = ejsGetErrorMsg(ejs);
|
||||
mprError(ejs, MPR_LOC, "HTTP callback failed. Details: %s", msg);
|
||||
}
|
||||
ejsFreeMethodArgs(ejs, args);
|
||||
|
||||
} else if (ejsRunMethod(ejs, thisObj, "onComplete", 0) < 0) {
|
||||
msg = ejsGetErrorMsg(ejs);
|
||||
mprError(ejs, MPR_LOC, "HTTP onComplete failed. Details: %s", msg);
|
||||
}
|
||||
|
||||
cleanup(hp);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Cleanup
|
||||
*/
|
||||
|
||||
static void cleanup(HTTPControl *hp)
|
||||
{
|
||||
Ejs *ejs;
|
||||
MprApp *app;
|
||||
int rc;
|
||||
|
||||
mprAssert(hp);
|
||||
mprAssert(hp->webResp);
|
||||
|
||||
ejs = hp->ejs;
|
||||
|
||||
if (hp->webResp) {
|
||||
rc = IWEBRESP_Release(hp->webResp);
|
||||
// mprAssert(rc == 0);
|
||||
hp->webResp = 0;
|
||||
}
|
||||
|
||||
if (hp->callback) {
|
||||
CALLBACK_Cancel(hp->callback);
|
||||
mprFree(hp->callback);
|
||||
hp->callback = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Once the property is deleted, then if the destructor runs, it will
|
||||
* notice that the EJS_HTTP_PROPERTY is undefined.
|
||||
*/
|
||||
ejsDeleteProperty(ejs, hp->thisObj, EJS_HTTP_PROPERTY);
|
||||
|
||||
/*
|
||||
* Allow garbage collection to work on thisObj
|
||||
*/
|
||||
ejsMakeObjPermanent(hp->thisObj, 0);
|
||||
ejsFreeVar(ejs, hp->thisObj);
|
||||
|
||||
mprFree(hp->buf);
|
||||
mprFree(hp->url);
|
||||
|
||||
mprFree(hp);
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
|
||||
|
||||
ISHELL_SendEvent(app->shell, (AEECLSID) app->classId, EVT_USER, 0, 0);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineHTTPClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *httpClass;
|
||||
|
||||
httpClass =
|
||||
ejsDefineClass(ejs, "HTTP", "Object", ejsHTTPConstructor);
|
||||
if (httpClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the methods
|
||||
*/
|
||||
ejsDefineCMethod(ejs, httpClass, "fetch", fetchProc, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "stop", stopProc, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "setCallback", setCallback, 0);
|
||||
|
||||
#if FUTURE
|
||||
ejsDefineCMethod(ejs, httpClass, "put", put, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "upload", upload, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addUploadFile", addUploadFile, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addPostData", addPostData, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "setUserPassword", setUserPassword, 0);
|
||||
ejsDefineCMethod(ejs, httpClass, "addCookie", addCookie, 0);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define properties
|
||||
*/
|
||||
ejsSetPropertyToString(ejs, httpClass, "status", "inactive");
|
||||
|
||||
/* This default should come from player.xml */
|
||||
|
||||
ejsSetPropertyToInteger(ejs, httpClass, "timeout", 30 * 1000);
|
||||
ejsSetPropertyToInteger(ejs, httpClass, "responseCode", 0);
|
||||
|
||||
return ejsObjHasErrors(httpClass) ? MPR_ERR_CANT_INITIALIZE: 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void ejsTermHTTPClass(Ejs *ejs)
|
||||
{
|
||||
IWeb *web;
|
||||
int rc;
|
||||
|
||||
web = (IWeb*) mprGetKeyValue(ejs, "bpWeb");
|
||||
if (web) {
|
||||
rc = IWEB_Release(web);
|
||||
mprAssert(rc == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
* @file ejsGC.c
|
||||
* @brief Garbage collector class for the EJS Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
#if (WIN || BREW_SIMULATOR) && BLD_DEBUG
|
||||
|
||||
static int checkProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
_CrtCheckMemory();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
static int debugProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Bad args: debug(debugLevel)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetGCDebugLevel(ep, ejsVarToInteger(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Print stats and dump objects
|
||||
*/
|
||||
|
||||
static int printStatsProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
bool leakStats;
|
||||
|
||||
if (argc > 1) {
|
||||
leakStats = ejsVarToInteger(argv[0]);
|
||||
} else {
|
||||
leakStats = 0;
|
||||
}
|
||||
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
ejsPrintAllocReport(ep, 0);
|
||||
|
||||
mprPrintAllocReport(mprGetApp(ep), leakStats, 0);
|
||||
#endif
|
||||
|
||||
#if BLD_DEBUG
|
||||
ejsDumpObjects(ep);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int runProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc > 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Bad args: run([quick])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (argc == 1) {
|
||||
ejsIncrementalCollectGarbage(ep);
|
||||
} else {
|
||||
ejsCollectGarbage(ep, -1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int usedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ep, ejsGetUsedMemory(ep));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int allocatedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv)
|
||||
{
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
ejsSetReturnValueToInteger(ep, ejsGetAllocatedMemory(ep));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int mprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
ejsSetReturnValueToInteger(ep, mprGetAllocatedMemory(ep));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int peakMprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
ejsSetReturnValueToInteger(ep, mprGetPeakAllocatedMemory(ep));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ep, ep->gc.debugLevel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
ep->gc.debugLevel= ejsVarToInteger(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToBoolean(ep, ep->gc.enable);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
ep->gc.enable= ejsVarToBoolean(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToBoolean(ep, ep->gc.enableDemandCollect);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
ep->gc.enableDemandCollect = ejsVarToBoolean(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToBoolean(ep, ep->gc.enableIdleCollect);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
ep->gc.enableIdleCollect = ejsVarToBoolean(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ep, ep->gc.workQuota);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int quota;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
quota = ejsVarToInteger(argv[0]);
|
||||
if (quota < EJS_GC_MIN_WORK_QUOTA && quota != 0) {
|
||||
ejsArgError(ep, "Bad work quota");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ep->gc.workQuota = quota;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ep, ep->gc.maxMemory);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int setMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int maxMemory;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsArgError(ep, "Bad arguments");
|
||||
return -1;
|
||||
}
|
||||
maxMemory = ejsVarToInteger(argv[0]);
|
||||
if (maxMemory < 0) {
|
||||
ejsArgError(ep, "Bad maxMemory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ep->gc.maxMemory = maxMemory;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineGCClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *gcClass;
|
||||
int flags;
|
||||
|
||||
flags = EJS_NO_LOCAL;
|
||||
|
||||
/*
|
||||
* NOTE: We create the GC class and define static methods on it. There
|
||||
* is no object instance
|
||||
*/
|
||||
gcClass = ejsDefineClass(ep, "System.GC", "Object", 0);
|
||||
if (gcClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* MOB -- convert these to properties with accessors when available
|
||||
*/
|
||||
ejsDefineCMethod(ep, gcClass, "printStats", printStatsProc, flags);
|
||||
ejsDefineCMethod(ep, gcClass, "run", runProc, flags);
|
||||
|
||||
ejsDefineCMethod(ep, gcClass, "getUsedMemory", usedMemoryProc, flags);
|
||||
ejsDefineCMethod(ep, gcClass, "getAllocatedMemory", allocatedMemoryProc,
|
||||
flags);
|
||||
ejsDefineCMethod(ep, gcClass, "getMprMemory", mprMemoryProc, flags);
|
||||
ejsDefineCMethod(ep, gcClass, "getPeakMprMemory", peakMprMemoryProc, flags);
|
||||
ejsDefineCMethod(ep, gcClass, "debug", debugProc, flags);
|
||||
|
||||
#if (WIN || BREW_SIMULATOR) && BLD_DEBUG
|
||||
ejsDefineCMethod(ep, gcClass, "check", checkProc, flags);
|
||||
#endif
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "debugLevel",
|
||||
getDebugLevel, setDebugLevel, flags);
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "enable",
|
||||
getEnable, setEnable, flags);
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "demandCollect",
|
||||
getDemandCollect, setDemandCollect, flags);
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "idleCollect",
|
||||
getIdleCollect, setIdleCollect, flags);
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "workQuota",
|
||||
getWorkQuota, setWorkQuota, flags);
|
||||
|
||||
ejsDefineCAccessors(ep, gcClass, "maxMemory",
|
||||
getMaxMemory, setMaxMemory, flags);
|
||||
|
||||
return ejsObjHasErrors(gcClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+785
@@ -0,0 +1,785 @@
|
||||
/*
|
||||
* @file ejsGlobal.c
|
||||
* @brief EJS support methods
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************* Code ***********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* assert(condition)
|
||||
*/
|
||||
|
||||
static int assertProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int b;
|
||||
|
||||
if (argc < 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: assert(condition)");
|
||||
return -1;
|
||||
}
|
||||
b = ejsVarToBoolean(argv[0]);
|
||||
if (b == 0) {
|
||||
ejsError(ep, EJS_ASSERT_ERROR, "Assertion failure at line %d",
|
||||
ejsGetLineNumber(ep));
|
||||
return -1;
|
||||
}
|
||||
ejsWriteVarAsBoolean(ep, ep->result, b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* breakpoint(msg)
|
||||
*/
|
||||
|
||||
static int breakpointProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
if (argc < 1) {
|
||||
return 0;
|
||||
}
|
||||
buf = ejsVarToString(ep, argv[0]);
|
||||
if (buf) {
|
||||
mprBreakpoint(0, buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* basename(path)
|
||||
*/
|
||||
|
||||
static int basenameProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *path;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: basename(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
path = ejsVarToString(ep, argv[0]);
|
||||
if (path == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
ejsSetReturnValueToString(ep, mprGetBaseName(path));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* stripext(path)
|
||||
*/
|
||||
|
||||
static int stripextProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *cp, *path, *stripPath;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: stripext(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
path = ejsVarToString(ep, argv[0]);
|
||||
if (path == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
stripPath = mprStrdup(ep, path);
|
||||
|
||||
if ((cp = strrchr(stripPath, '.')) != 0) {
|
||||
*cp = '\0';
|
||||
}
|
||||
|
||||
ejsSetReturnValueToString(ep, stripPath);
|
||||
|
||||
mprFree(stripPath);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* dirname(path)
|
||||
*/
|
||||
|
||||
static int dirnameProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *path;
|
||||
char dirname[MPR_MAX_FNAME];
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: dirname(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
path = ejsVarToString(ep, argv[0]);
|
||||
if (path == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
|
||||
ejsSetReturnValueToString(ep,
|
||||
mprGetDirName(dirname, sizeof(dirname), path));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* trim(string) -- trim white space
|
||||
*/
|
||||
|
||||
static int trimProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *str, *buf, *cp;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: trim(string)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
str = ejsVarToString(ep, argv[0]);
|
||||
if (str == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
str = buf = mprStrdup(ep, str);
|
||||
|
||||
while (isspace(*str)) {
|
||||
str++;
|
||||
}
|
||||
cp = &str[strlen(str) - 1];
|
||||
while (cp >= str) {
|
||||
if (isspace(*cp)) {
|
||||
*cp = '\0';
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
cp--;
|
||||
}
|
||||
|
||||
ejsSetReturnValueToString(ep, str);
|
||||
|
||||
mprFree(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Terminate the script
|
||||
*/
|
||||
|
||||
static int exitScript(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "usage: exit(status)");
|
||||
return -1;
|
||||
}
|
||||
status = (int) ejsVarToInteger(argv[0]);
|
||||
ejsExit(ep, status);
|
||||
|
||||
ejsWriteVarAsString(ep, ep->result, "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* include javascript libraries.
|
||||
*/
|
||||
|
||||
static int includeProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
mprAssert(argv);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (ejsEvalFile(ep, argv[i], 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* include javascript libraries at the global level
|
||||
*/
|
||||
|
||||
static int includeGlobalProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv)
|
||||
{
|
||||
int fid, i;
|
||||
|
||||
mprAssert(argv);
|
||||
|
||||
/*
|
||||
* Create a new block and set the context to be the global scope
|
||||
*/
|
||||
fid = ejsSetBlock(ep, ep->global);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (ejsEvalFile(ep, argv[i], 0) < 0) {
|
||||
ejsCloseBlock(ep, fid);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ejsCloseBlock(ep, fid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if BLD_DEBUG
|
||||
/*
|
||||
* Print variables to stdout
|
||||
*/
|
||||
|
||||
static int printvProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsVar *vp;
|
||||
char *buf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < argc; ) {
|
||||
vp = argv[i++];
|
||||
|
||||
/* mprPrintf(ep, "arg[%d] = ", i); */
|
||||
|
||||
buf = ejsVarToString(ep, vp);
|
||||
|
||||
if (vp->propertyName == 0 || *vp->propertyName == '\0') {
|
||||
mprPrintf(ep, "%s: ", buf);
|
||||
|
||||
} else if (i < argc) {
|
||||
mprPrintf(ep, "%s = %s, ", vp->propertyName, buf);
|
||||
} else {
|
||||
mprPrintf(ep, "%s = %s\n", vp->propertyName, buf);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Print the args to stdout
|
||||
*/
|
||||
|
||||
static int printProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *buf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
buf = ejsVarToString(ep, argv[i]);
|
||||
mprPrintf(ep, "%s", buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* println
|
||||
*/
|
||||
|
||||
static int printlnProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
printProc(ep, thisObj, argc, argv);
|
||||
mprPrintf(ep, "\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if FUTURE
|
||||
/*
|
||||
* sprintf
|
||||
*/
|
||||
|
||||
static int sprintfProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
void **args;
|
||||
int result;
|
||||
|
||||
if (argc <= 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: sprintf(fmt, [args ...])");
|
||||
return -1;
|
||||
}
|
||||
|
||||
args = mprAlloc(ep, sizeof(void*) * (argc - 1));
|
||||
if (args == 0) {
|
||||
mprAssert(args);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
args[i - 1] = argv[i]);
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
*buf = 0;
|
||||
result = inner(0, &buf, MPR_MAX_STRING, fmt, args);
|
||||
va_end(ap);
|
||||
|
||||
ejsSetReturnValueToString(ep, buf);
|
||||
|
||||
mprFree(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
inner(const char *fmt, void **args)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
*buf = 0;
|
||||
mprSprintfCore(ctx, &buf, maxSize, fmt, ap, MPR_PRINTF_ARGV);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* sleep
|
||||
*/
|
||||
|
||||
static int sleepProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: sleep(milliseconds)");
|
||||
return -1;
|
||||
}
|
||||
mprSleep(ep, ejsVarToInteger(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* sort properties
|
||||
* FUTURE -- should have option to sort object based on a given property value
|
||||
* ascending or descending
|
||||
* Usage: sort(object, order = ascending, property = 0);
|
||||
*/
|
||||
|
||||
static int sortProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
const char *property;
|
||||
int error, order;
|
||||
|
||||
error = 0;
|
||||
property = 0;
|
||||
|
||||
/*
|
||||
* Default order is increasing
|
||||
*/
|
||||
order = 1;
|
||||
|
||||
if (argc < 1 || argc > 3 || !ejsVarIsObject(argv[0])) {
|
||||
error++;
|
||||
}
|
||||
|
||||
if (argc >= 2) {
|
||||
order = ejsVarToInteger(argv[1]);
|
||||
}
|
||||
|
||||
/*
|
||||
* If property is not defined, it sorts the properties in the object
|
||||
*/
|
||||
if (argc == 3) {
|
||||
if (! ejsVarIsString(argv[2])) {
|
||||
error++;
|
||||
} else {
|
||||
property = argv[2]->string;
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: sort(object, [order], [property])");
|
||||
return -1;
|
||||
}
|
||||
ejsSortProperties(ep, argv[0], 0, property, order);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get a time mark
|
||||
* MOB -- WARNING: this can overflow. OK on BREW, but other O/Ss it may have
|
||||
* overflowed on the first call. It should be renamed.
|
||||
* MOB -- replace with proper Date.
|
||||
*/
|
||||
|
||||
static int timeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
MprTime now;
|
||||
|
||||
mprGetTime(ep, &now);
|
||||
#if WIN || LINUX || SOLARIS
|
||||
{
|
||||
/* MOB -- poor hack */
|
||||
static MprTime initial;
|
||||
if (initial.sec == 0) {
|
||||
initial = now;
|
||||
}
|
||||
now.sec -= initial.sec;
|
||||
|
||||
if (initial.msec > now.msec) {
|
||||
now.msec = now.msec + 1000 - initial.msec;
|
||||
now.sec--;
|
||||
} else {
|
||||
now.msec -= initial.msec;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* MOB -- this can overflow */
|
||||
ejsSetReturnValueToInteger(ep, now.sec * 1000 + now.msec);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* MOB -- Temporary Get the date (time since Jan 6, 1980 GMT
|
||||
*/
|
||||
|
||||
static int dateProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
#if BREW
|
||||
uint now;
|
||||
|
||||
now = GETTIMESECONDS();
|
||||
ejsSetReturnValueToInteger(ep, now);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* strlen(string)
|
||||
*/
|
||||
|
||||
static int strlenProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: strlen(var)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = 0;
|
||||
if (! ejsVarIsString(argv[0])) {
|
||||
buf = ejsVarToString(ep, argv[0]);
|
||||
if (buf) {
|
||||
len = strlen(buf);
|
||||
}
|
||||
|
||||
} else {
|
||||
len = argv[0]->length;
|
||||
}
|
||||
|
||||
ejsSetReturnValueToInteger(ep, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* toint(num)
|
||||
*/
|
||||
|
||||
static int tointProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: toint(number)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
i = ejsVarToInteger(argv[0]);
|
||||
|
||||
ejsSetReturnValueToInteger(ep, i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* string strstr(string, pat)
|
||||
*/
|
||||
|
||||
static int strstrProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
char *str, *pat;
|
||||
char *s;
|
||||
int strAlloc;
|
||||
|
||||
if (argc != 2) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: strstr(string, pat)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
str = ejsVarToString(ep, argv[0]);
|
||||
|
||||
strAlloc = ep->castAlloc;
|
||||
ep->castTemp = 0;
|
||||
|
||||
pat = ejsVarToString(ep, argv[1]);
|
||||
|
||||
s = strstr(str, pat);
|
||||
|
||||
if (s == 0) {
|
||||
ejsSetReturnValueToUndefined(ep);
|
||||
} else {
|
||||
ejsSetReturnValueToString(ep, s);
|
||||
}
|
||||
|
||||
if (strAlloc) {
|
||||
mprFree(str);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Trace
|
||||
*/
|
||||
|
||||
static int traceProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
mprLog(ep, 0, "%s", argv[0]);
|
||||
|
||||
} else if (argc == 2) {
|
||||
mprLog(ep, atoi(argv[0]), "%s", argv[1]);
|
||||
|
||||
} else {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Usage: trace([level], message)");
|
||||
return -1;
|
||||
}
|
||||
ejsWriteVarAsString(ep, ep->result, "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Evaluate a sub-script. It is evaluated in the same variable scope as
|
||||
* the calling script / method.
|
||||
*/
|
||||
|
||||
static int evalScriptProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
EjsVar *arg;
|
||||
int i;
|
||||
|
||||
ejsWriteVarAsUndefined(ep, ep->result);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
arg = argv[i];
|
||||
if (arg->type != EJS_TYPE_STRING) {
|
||||
continue;
|
||||
}
|
||||
if (ejsEvalScript(ep, arg->string, 0) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Return with the value of the last expression
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/* MOB -- need a real datatype returning int, int64, etc */
|
||||
|
||||
static int typeofProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
const struct {
|
||||
EjsType type;
|
||||
const char *name;
|
||||
} types[] = {
|
||||
{ EJS_TYPE_UNDEFINED, "undefined" },
|
||||
#if EJS_ECMA_STND
|
||||
{ EJS_TYPE_NULL, "object" },
|
||||
#else
|
||||
{ EJS_TYPE_NULL, "null" },
|
||||
#endif
|
||||
{ EJS_TYPE_BOOL, "boolean" },
|
||||
{ EJS_TYPE_CMETHOD, "function" },
|
||||
{ EJS_TYPE_FLOAT, "number" },
|
||||
{ EJS_TYPE_INT, "number" },
|
||||
{ EJS_TYPE_INT64, "number" },
|
||||
{ EJS_TYPE_OBJECT, "object" },
|
||||
{ EJS_TYPE_METHOD, "function" },
|
||||
{ EJS_TYPE_STRING, "string" },
|
||||
{ EJS_TYPE_STRING_CMETHOD, "function" },
|
||||
{ EJS_TYPE_PTR, "pointer" }
|
||||
};
|
||||
const char *type;
|
||||
int i;
|
||||
|
||||
type = NULL;
|
||||
if (argc != 1) {
|
||||
ejsError(ep, EJS_ARG_ERROR, "Bad args: typeof(var)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < MPR_ARRAY_SIZE(types); i++) {
|
||||
if (argv[0]->type == types[i].type) {
|
||||
type = types[i].name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (type == NULL) {
|
||||
mprAssert(type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ejsSetReturnValueToString(ep, type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Define the standard properties and methods inherited by all interpreters
|
||||
* Obj is set to the global class in the default interpreter. When an
|
||||
* interpreter attempts to write to any property, a copy will be written
|
||||
* into the interpeters own global space. This is like a "copy-on-write".
|
||||
*/
|
||||
|
||||
int ejsDefineGlobalProperties(Ejs *ep)
|
||||
{
|
||||
EjsVar *obj;
|
||||
|
||||
obj = ep->service->globalClass;
|
||||
mprAssert(obj);
|
||||
|
||||
ejsSetPropertyToNull(ep, obj, "null");
|
||||
ejsSetPropertyToUndefined(ep, obj, "undefined");
|
||||
ejsSetPropertyToBoolean(ep, obj, "true", 1);
|
||||
ejsSetPropertyToBoolean(ep, obj, "false", 0);
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
{
|
||||
/* MOB. Fix. This generates warnings on some systems.
|
||||
This is intended. */
|
||||
double d = 0.0;
|
||||
double e = 0.0;
|
||||
ejsSetPropertyToFloat(ep, obj, "NaN", e / d);
|
||||
|
||||
d = MAX_FLOAT;
|
||||
ejsSetPropertyToFloat(ep, obj, "Infinity", d * d);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_LEGACY_API
|
||||
/*
|
||||
* DEPRECATED: 2.0.
|
||||
* So that ESP/ASP can ignore "language=javascript" statements
|
||||
*/
|
||||
ejsSetPropertyToInteger(ep, obj, "javascript", 0);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Extension methods. We go directly to the mpr property APIs for speed.
|
||||
* Flags will cause the callbacks to be supplied the Ejs handle.
|
||||
*/
|
||||
ejsDefineCMethod(ep, obj, "assert", assertProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "breakpoint", breakpointProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "basename", basenameProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "dirname", dirnameProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "stripext", stripextProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "trim", trimProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "eval", evalScriptProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "exit", exitScript, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "print", printProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "println", printlnProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "sleep", sleepProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "sort", sortProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "time", timeProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "date", dateProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "strlen", strlenProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "strstr", strstrProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "typeof", typeofProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "toint", tointProc, EJS_NO_LOCAL);
|
||||
|
||||
ejsDefineStringCMethod(ep, obj, "include", includeProc, EJS_NO_LOCAL);
|
||||
ejsDefineStringCMethod(ep, obj, "includeGlobal", includeGlobalProc,
|
||||
EJS_NO_LOCAL);
|
||||
ejsDefineStringCMethod(ep, obj, "trace", traceProc, EJS_NO_LOCAL);
|
||||
|
||||
#if BLD_DEBUG
|
||||
ejsDefineCMethod(ep, obj, "printv", printvProc, EJS_NO_LOCAL);
|
||||
#endif
|
||||
|
||||
#if FUTURE
|
||||
ejsDefineCMethod(ep, obj, "printf", printfProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, obj, "sprintf", sprintfProc, EJS_NO_LOCAL);
|
||||
#endif
|
||||
|
||||
if (ejsObjHasErrors(obj)) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsProcsDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* @file ejsSystem.c
|
||||
* @brief System class for the EJS Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
#if UNUSED
|
||||
/*
|
||||
* function int random()
|
||||
*/
|
||||
|
||||
static int randomProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "random()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void yield()
|
||||
*/
|
||||
|
||||
static int yieldProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "yield()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void sleep(int milliSeconds)
|
||||
*/
|
||||
|
||||
static int sleepProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ep, "sleep()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function void exit(int status)
|
||||
*
|
||||
* Exit the widget with the given status. All JavaScript processing ceases.
|
||||
*/
|
||||
|
||||
static int exitProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = 0;
|
||||
if ((argc == 1) && ejsVarIsInteger(argv[0])) {
|
||||
status = argv[0]->integer;
|
||||
}
|
||||
ejsExit(ep, status);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineSystemClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *systemClass;
|
||||
|
||||
/*
|
||||
* We create the system class and define static methods on it.
|
||||
* NOTE: There is no object instance
|
||||
*/
|
||||
systemClass = ejsDefineClass(ep, "System", "Object", 0);
|
||||
if (systemClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
ejsDefineCMethod(ep, systemClass, "exit", exitProc, EJS_NO_LOCAL);
|
||||
|
||||
#if UNUSED
|
||||
ejsDefineCMethod(ep, systemClass, "random", randomProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, systemClass, "yield", yieldProc, EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ep, systemClass, "sleep", sleepProc, EJS_NO_LOCAL);
|
||||
|
||||
/*
|
||||
* Define properties
|
||||
*/
|
||||
ejsSetPropertyToString(systemClass, "name", "");
|
||||
#endif
|
||||
|
||||
return ejsObjHasErrors(systemClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* @file ejsSystemApp.c
|
||||
* @brief App class
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software Inc, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int ejsDefineAppClass(Ejs *ep)
|
||||
{
|
||||
EjsVar *appClass;
|
||||
|
||||
appClass = ejsDefineClass(ep, "System.App", "Object", 0);
|
||||
if (appClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define properties
|
||||
*/
|
||||
ejsSetPropertyToString(ep, appClass, "name", BLD_PRODUCT);
|
||||
ejsSetPropertyToString(ep, appClass, "title", BLD_NAME);
|
||||
ejsSetPropertyToString(ep, appClass, "version", BLD_VERSION);
|
||||
|
||||
/*
|
||||
* Command line arguments
|
||||
*/
|
||||
ejsSetPropertyToNull(ep, appClass, "args");
|
||||
|
||||
return ejsObjHasErrors(appClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* @file ejsSystemDebug.c
|
||||
* @brief System.Debug class
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function bool isDebugMode()
|
||||
* MOB -- convert to accessor
|
||||
*/
|
||||
|
||||
static int isDebugMode(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsTrace(ejs, "isDebugMode()\n");
|
||||
ejsSetReturnValueToInteger(ejs, mprGetDebugMode(ejs));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineDebugClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *systemDebugClass;
|
||||
|
||||
systemDebugClass = ejsDefineClass(ejs, "System.Debug", "Object", 0);
|
||||
if (systemDebugClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the class methods
|
||||
*/
|
||||
ejsDefineCMethod(ejs, systemDebugClass, "isDebugMode", isDebugMode,
|
||||
EJS_NO_LOCAL);
|
||||
|
||||
return ejsObjHasErrors(systemDebugClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* @file ejsSystemLog.c
|
||||
* @brief System.Log class for the EJS Object Model
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/*********************************** Usage ************************************/
|
||||
/*
|
||||
* System.Log.setLog(path);
|
||||
* System.Log.enable;
|
||||
*/
|
||||
/******************************************************************************/
|
||||
|
||||
static void logHandler(MPR_LOC_DEC(ctx, loc), int flags, int level,
|
||||
const char *msg)
|
||||
{
|
||||
MprApp *app;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
if (app->logFile == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (flags & MPR_LOG_SRC) {
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"Log %d: %s\n", level, msg);
|
||||
|
||||
} else if (flags & MPR_ERROR_SRC) {
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"Error: %s\n", msg);
|
||||
|
||||
} else if (flags & MPR_FATAL_SRC) {
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"Fatal: %s\n", msg);
|
||||
|
||||
} else if (flags & MPR_ASSERT_SRC) {
|
||||
#if BLD_FEATURE_ALLOC_LEAK_TRACK
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"Assertion %s, failed at %s\n",
|
||||
msg, loc);
|
||||
#else
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"Assertion %s, failed\n", msg);
|
||||
#endif
|
||||
|
||||
} else if (flags & MPR_RAW) {
|
||||
/* OPT */
|
||||
len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0,
|
||||
"%s", msg);
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
mprPuts(app->logFile, buf, len);
|
||||
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/************************************ Methods *********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* function int setLog(string path)
|
||||
*/
|
||||
|
||||
static int setLog(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
const char *path;
|
||||
MprFile *file;
|
||||
MprApp *app;
|
||||
|
||||
if (argc != 1 || !ejsVarIsString(argv[0])) {
|
||||
ejsArgError(ejs, "Usage: setLog(path)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
|
||||
/*
|
||||
* Ignore errors if we can't create the log file.
|
||||
* Use the app context so this will live longer than the interpreter
|
||||
* MOB -- this leaks files.
|
||||
*/
|
||||
path = argv[0]->string;
|
||||
file = mprOpen(app, path, O_CREAT | O_TRUNC | O_WRONLY, 0664);
|
||||
if (file) {
|
||||
app->logFile = file;
|
||||
mprSetLogHandler(ejs, logHandler);
|
||||
}
|
||||
mprLog(ejs, 0, "Test log");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if UNUSED
|
||||
|
||||
static int enableSetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
ejsArgError(ejs, "Usage: set(value)");
|
||||
return -1;
|
||||
}
|
||||
ejsSetProperty(ejs, thisObj, "_enabled", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int enableGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValue(ejs, ejsGetPropertyAsVar(ejs, thisObj, "_enabled"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineLogClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *logClass;
|
||||
|
||||
logClass = ejsDefineClass(ejs, "System.Log", "Object", 0);
|
||||
if (logClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
ejsDefineCMethod(ejs, logClass, "setLog", setLog, EJS_NO_LOCAL);
|
||||
|
||||
#if UNUSED
|
||||
EjsProperty *pp;
|
||||
ejsDefineCAccessors(ejs, logClass, "enable", enableSetAccessor,
|
||||
enableGetAccessor, EJS_NO_LOCAL);
|
||||
|
||||
pp = ejsSetPropertyToBoolean(ejs, logClass, "_enabled", 0);
|
||||
ejsMakePropertyEnumerable(pp, 0);
|
||||
#endif
|
||||
|
||||
return ejsObjHasErrors(logClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* @file ejsSystemMemory.c
|
||||
* @brief System.Memory class
|
||||
*/
|
||||
/********************************** Copyright *********************************/
|
||||
/*
|
||||
* Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/****************************** Forward Declarations***************************/
|
||||
|
||||
static uint getUsedMemory(Ejs *ejs);
|
||||
|
||||
/******************************************************************************/
|
||||
/*********************************** Methods *********************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static int getUsedMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ejs, getUsedMemory(ejs));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getUsedStackProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
|
||||
{
|
||||
ejsSetReturnValueToInteger(ejs, mprStackSize(ejs));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Public function
|
||||
*/
|
||||
|
||||
uint ejsGetAvailableMemory(Ejs *ejs)
|
||||
{
|
||||
EjsVar *memoryClass;
|
||||
uint ram;
|
||||
|
||||
memoryClass = ejsGetClass(ejs, 0, "System.Memory");
|
||||
|
||||
ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram");
|
||||
return ram - getUsedMemory(ejs);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getAvailableMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc,
|
||||
EjsVar **argv)
|
||||
{
|
||||
EjsVar *memoryClass;
|
||||
uint ram;
|
||||
|
||||
memoryClass = ejsGetClass(ejs, 0, "System.Memory");
|
||||
|
||||
ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram");
|
||||
#if BREW
|
||||
ejsSetReturnValueToInteger(ejs, ram - getUsedMemory(ejs));
|
||||
#else
|
||||
ejsSetReturnValueToInteger(ejs, 0);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static uint getUsedMemory(Ejs *ejs)
|
||||
{
|
||||
#if BREW
|
||||
MprApp *app;
|
||||
IHeap *heap;
|
||||
uint memInUse;
|
||||
void *ptr;
|
||||
|
||||
app = mprGetApp(ejs);
|
||||
ptr = (void*) &heap;
|
||||
if (ISHELL_CreateInstance(app->shell, AEECLSID_HEAP, (void**) ptr)
|
||||
== SUCCESS) {
|
||||
memInUse = IHEAP_GetMemStats(heap);
|
||||
IHEAP_Release(heap);
|
||||
} else {
|
||||
memInUse = 0;
|
||||
}
|
||||
|
||||
return memInUse;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Initialization ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int ejsDefineMemoryClass(Ejs *ejs)
|
||||
{
|
||||
EjsVar *memoryClass;
|
||||
uint used;
|
||||
|
||||
#if BREW
|
||||
MprApp *app;
|
||||
AEEDeviceInfo *info;
|
||||
|
||||
/*
|
||||
* Get needed information for class properties.
|
||||
*/
|
||||
info = mprAllocType(ejs, AEEDeviceInfo);
|
||||
if (info == 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
info->wStructSize = sizeof(AEEDeviceInfo);
|
||||
app = mprGetApp(ejs);
|
||||
ISHELL_GetDeviceInfo(app->shell, info);
|
||||
used = getUsedMemory(ejs);
|
||||
#else
|
||||
used = 0;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Create the class
|
||||
*/
|
||||
memoryClass = ejsDefineClass(ejs, "System.Memory", "Object", 0);
|
||||
if (memoryClass == 0) {
|
||||
return MPR_ERR_CANT_INITIALIZE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Define the class methods
|
||||
* MOB -- change to accessors
|
||||
*/
|
||||
ejsDefineCMethod(ejs, memoryClass, "getUsedStack", getUsedStackProc,
|
||||
EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, memoryClass, "getUsedMemory", getUsedMemoryProc,
|
||||
EJS_NO_LOCAL);
|
||||
ejsDefineCMethod(ejs, memoryClass, "getAvailableMemory",
|
||||
getAvailableMemoryProc, EJS_NO_LOCAL);
|
||||
|
||||
/*
|
||||
* Define properties
|
||||
*/
|
||||
#if BREW
|
||||
ejsSetPropertyToInteger(ejs, memoryClass, "ram", info->dwRAM);
|
||||
|
||||
#if UNUSED
|
||||
/* MOB -- delete this */
|
||||
ejsSetPropertyToInteger(ejs, memoryClass, "available",
|
||||
info->dwRAM - used);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if UNUSED
|
||||
ejsSetPropertyToInteger(ejs, memoryClass, "used", used);
|
||||
ejsSetPropertyToInteger(ejs, memoryClass, "flash", 0);
|
||||
#endif
|
||||
|
||||
return ejsObjHasErrors(memoryClass) ? MPR_ERR_CANT_INITIALIZE : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,42 @@
|
||||
#
|
||||
# Makefile for Embedded XML (EXML)
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
#
|
||||
# EXML may be linked into shared handlers so we must build the objects both
|
||||
# shared and static.
|
||||
#
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I../mpr
|
||||
|
||||
include make.dep
|
||||
|
||||
ifeq ($(BLD_FEATURE_TEST),1)
|
||||
POST_DIRS := test
|
||||
endif
|
||||
|
||||
TARGETS += $(BLD_BIN_DIR)/libexml$(BLD_LIB)
|
||||
|
||||
ifeq ($(BLD_FEATURE_XML),1)
|
||||
compileExtra: $(TARGETS)
|
||||
endif
|
||||
|
||||
# MOB -- remove when FEATURE_XML is defined
|
||||
compileExtra: $(TARGETS)
|
||||
|
||||
$(BLD_BIN_DIR)/libexml$(BLD_LIB): $(FILES)
|
||||
@bld --library $(BLD_BIN_DIR)/libexml \
|
||||
--objectsDir $(BLD_OBJ_DIR) --objectList files --libs mpr
|
||||
|
||||
cleanExtra:
|
||||
@echo "rm -f $(TARGETS)" | $(BLDOUT)
|
||||
@rm -f $(TARGETS)
|
||||
@rm -f $(BLD_BIN_DIR)/libexml.*
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* exml.h -- Embedded Xml Parser header
|
||||
*
|
||||
* Copyright (c) Mbedthis Software, LLC, 2003-2003. All Rights Reserved. -- MOB
|
||||
*/
|
||||
|
||||
#ifndef _h_EXML
|
||||
#define _h_EXML 1
|
||||
|
||||
/******************************** Description *********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/********************************** Defines ***********************************/
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define EXML_BUFSIZE 512 /* Read buffer size */
|
||||
#else
|
||||
#define EXML_BUFSIZE 1024 /* Read buffer size */
|
||||
#endif
|
||||
|
||||
/*
|
||||
* XML parser states. The states that are passed to the user handler have
|
||||
* "U" appended to the comment. The error states (ERR and EOF) must be
|
||||
* negative.
|
||||
*/
|
||||
#define EXML_ERR -1 /* Error */
|
||||
#define EXML_EOF -2 /* End of input */
|
||||
#define EXML_BEGIN 1 /* Before next tag */
|
||||
#define EXML_AFTER_LS 2 /* Seen "<" */
|
||||
#define EXML_COMMENT 3 /* Seen "<!--" (usr) U */
|
||||
#define EXML_NEW_ELT 4 /* Seen "<tag" (usr) U */
|
||||
#define EXML_ATT_NAME 5 /* Seen "<tag att" */
|
||||
#define EXML_ATT_EQ 6 /* Seen "<tag att" = */
|
||||
#define EXML_NEW_ATT 7 /* Seen "<tag att = "val" U */
|
||||
#define EXML_SOLO_ELT_DEFINED 8 /* Seen "<tag../>" U */
|
||||
#define EXML_ELT_DEFINED 9 /* Seen "<tag...>" U */
|
||||
#define EXML_ELT_DATA 10 /* Seen "<tag>....<" U */
|
||||
#define EXML_END_ELT 11 /* Seen "<tag>....</tag>" U */
|
||||
#define EXML_PI 12 /* Seen "<?processingInst" U */
|
||||
#define EXML_CDATA 13 /* Seen "<![CDATA[" U */
|
||||
|
||||
/*
|
||||
* Lex tokens
|
||||
*/
|
||||
typedef enum ExmlToken {
|
||||
TOKEN_ERR,
|
||||
TOKEN_TOO_BIG, /* Token is too big */
|
||||
TOKEN_CDATA,
|
||||
TOKEN_COMMENT,
|
||||
TOKEN_INSTRUCTIONS,
|
||||
TOKEN_LS, /* "<" -- Opening a tag */
|
||||
TOKEN_LS_SLASH, /* "</" -- Closing a tag */
|
||||
TOKEN_GR, /* ">" -- End of an open tag */
|
||||
TOKEN_SLASH_GR, /* "/>" -- End of a solo tag */
|
||||
TOKEN_TEXT,
|
||||
TOKEN_EQ,
|
||||
TOKEN_EOF,
|
||||
TOKEN_SPACE,
|
||||
} ExmlToken;
|
||||
|
||||
struct Exml;
|
||||
typedef int (*ExmlHandler)(struct Exml *xp, int state,
|
||||
const char *tagName, const char* attName, const char* value);
|
||||
typedef int (*ExmlInputStream)(struct Exml *xp, void *arg, char *buf, int size);
|
||||
|
||||
/*
|
||||
* Per XML session structure
|
||||
*/
|
||||
typedef struct Exml {
|
||||
ExmlHandler handler; /* Callback function */
|
||||
ExmlInputStream readFn; /* Read data function */
|
||||
MprBuf *inBuf; /* Input data queue */
|
||||
MprBuf *tokBuf; /* Parsed token buffer */
|
||||
int quoteChar; /* XdbAtt quote char */
|
||||
int lineNumber; /* Current line no for debug */
|
||||
void *parseArg; /* Arg passed to exmlParse() */
|
||||
void *inputArg; /* Arg passed to exmlSetInputStream() */
|
||||
char *errMsg; /* Error message text */
|
||||
} Exml;
|
||||
|
||||
extern Exml *exmlOpen(MprCtx ctx, int initialSize, int maxSize);
|
||||
extern void exmlClose(Exml *xp);
|
||||
extern void exmlSetParserHandler(Exml *xp, ExmlHandler h);
|
||||
extern void exmlSetInputStream(Exml *xp, ExmlInputStream s, void *arg);
|
||||
extern int exmlParse(Exml *xp);
|
||||
extern void exmlSetParseArg(Exml *xp, void *parseArg);
|
||||
extern void *exmlGetParseArg(Exml *xp);
|
||||
extern const char *exmlGetErrorMsg(Exml *xp);
|
||||
extern int exmlGetLineNumber(Exml *xp);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#endif /* _h_EXML */
|
||||
@@ -0,0 +1,752 @@
|
||||
/*
|
||||
* exml.c -- A simple SAX style XML parser
|
||||
*/
|
||||
|
||||
/********************************* Description ********************************/
|
||||
/*
|
||||
* This is a recursive descent parser for XML text files. It is a one-pass
|
||||
* simple parser that invokes a user supplied callback for key tokens in the
|
||||
* XML file. The user supplies a read function so that XML files can be parsed
|
||||
* from disk or in-memory.
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "exml.h"
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
/* MOB -- FIX */
|
||||
#if BLD_FEATURE_EXML || 1
|
||||
|
||||
static int parseNext(Exml *xp, int state);
|
||||
static ExmlToken getToken(Exml *xp, int state);
|
||||
static int getNextChar(Exml *xp);
|
||||
static int scanFor(Exml *xp, char *str);
|
||||
static int putLastChar(Exml *xp, int c);
|
||||
static void error(Exml *xp, char *fmt, ...);
|
||||
static void trimToken(Exml *xp);
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
Exml *exmlOpen(MprCtx ctx, int initialSize, int maxSize)
|
||||
{
|
||||
Exml *xp;
|
||||
|
||||
xp = mprAllocTypeZeroed(ctx, Exml);
|
||||
|
||||
xp->inBuf = mprCreateBuf(xp, EXML_BUFSIZE, EXML_BUFSIZE);
|
||||
xp->tokBuf = mprCreateBuf(xp, initialSize, maxSize);
|
||||
|
||||
return xp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void exmlClose(Exml *xp)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
mprFree(xp);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void exmlSetParserHandler(Exml *xp, ExmlHandler h)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
xp->handler = h;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void exmlSetInputStream(Exml *xp, ExmlInputStream s, void *arg)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
xp->readFn = s;
|
||||
xp->inputArg = arg;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Set the parse arg
|
||||
*/
|
||||
|
||||
void exmlSetParseArg(Exml *xp, void *parseArg)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
xp->parseArg = parseArg;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Set the parse arg
|
||||
*/
|
||||
|
||||
void *exmlGetParseArg(Exml *xp)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
return xp->parseArg;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Parse an XML file. Return 0 for success, -1 for error.
|
||||
*/
|
||||
|
||||
int exmlParse(Exml *xp)
|
||||
{
|
||||
mprAssert(xp);
|
||||
|
||||
return parseNext(xp, EXML_BEGIN);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* XML parser. This is a recursive descent parser. Return -1 for errors, 0 for
|
||||
* EOF and 1 if there is still more data to parse.
|
||||
*/
|
||||
|
||||
static int parseNext(Exml *xp, int state)
|
||||
{
|
||||
ExmlHandler handler;
|
||||
ExmlToken token;
|
||||
MprBuf *tokBuf;
|
||||
char *tname, *aname;
|
||||
int rc;
|
||||
|
||||
mprAssert(state >= 0);
|
||||
|
||||
tokBuf = xp->tokBuf;
|
||||
handler = xp->handler;
|
||||
tname = aname = 0;
|
||||
rc = 0;
|
||||
|
||||
/*
|
||||
* In this parse loop, the state is never assigned EOF or ERR. In
|
||||
* such cases we always return EOF or ERR.
|
||||
*/
|
||||
while (1) {
|
||||
|
||||
token = getToken(xp, state);
|
||||
|
||||
if (token == TOKEN_TOO_BIG) {
|
||||
error(xp, "XML token is too big");
|
||||
goto err;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case EXML_BEGIN: /* ------------------------------------------ */
|
||||
/*
|
||||
* Expect to get an element, comment or processing instruction
|
||||
*/
|
||||
switch (token) {
|
||||
case TOKEN_EOF:
|
||||
goto exit;
|
||||
|
||||
case TOKEN_LS:
|
||||
/*
|
||||
* Recurse to handle the new element, comment etc.
|
||||
*/
|
||||
rc = parseNext(xp, EXML_AFTER_LS);
|
||||
if (rc < 0) {
|
||||
goto exit;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
error(xp, "Syntax error");
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
|
||||
case EXML_AFTER_LS: /* ------------------------------------------ */
|
||||
switch (token) {
|
||||
case TOKEN_COMMENT:
|
||||
state = EXML_COMMENT;
|
||||
rc = (*handler)(xp, state, "!--", 0, mprGetBufStart(tokBuf));
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
rc = 1;
|
||||
goto exit;
|
||||
|
||||
case TOKEN_CDATA:
|
||||
state = EXML_CDATA;
|
||||
rc = (*handler)(xp, state, "!--", 0, mprGetBufStart(tokBuf));
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
rc = 1;
|
||||
goto exit;
|
||||
|
||||
case TOKEN_INSTRUCTIONS:
|
||||
/* Just ignore processing instructions */
|
||||
rc = 1;
|
||||
goto exit;
|
||||
|
||||
case TOKEN_TEXT:
|
||||
state = EXML_NEW_ELT;
|
||||
tname = mprStrdup(xp, mprGetBufStart(tokBuf));
|
||||
if (tname == 0) {
|
||||
rc = MPR_ERR_MEMORY;
|
||||
goto exit;
|
||||
}
|
||||
rc = (*handler)(xp, state, tname, 0, 0);
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
error(xp, "Syntax error");
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
|
||||
case EXML_NEW_ELT: /* ------------------------------------------ */
|
||||
/*
|
||||
* We have seen the opening "<element" for a new element and have
|
||||
* not yet seen the terminating ">" of the opening element.
|
||||
*/
|
||||
switch (token) {
|
||||
case TOKEN_TEXT:
|
||||
/*
|
||||
* Must be an attribute name
|
||||
*/
|
||||
aname = mprStrdup(xp, mprGetBufStart(tokBuf));
|
||||
token = getToken(xp, state);
|
||||
if (token != TOKEN_EQ) {
|
||||
error(xp, "Missing assignment for attribute \"%s\"", aname);
|
||||
goto err;
|
||||
}
|
||||
|
||||
token = getToken(xp, state);
|
||||
if (token != TOKEN_TEXT) {
|
||||
error(xp, "Missing value for attribute \"%s\"", aname);
|
||||
goto err;
|
||||
}
|
||||
state = EXML_NEW_ATT;
|
||||
rc = (*handler)(xp, state, tname, aname,
|
||||
mprGetBufStart(tokBuf));
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
state = EXML_NEW_ELT;
|
||||
break;
|
||||
|
||||
case TOKEN_GR:
|
||||
/*
|
||||
* This is ">" the termination of the opening element
|
||||
*/
|
||||
if (*tname == '\0') {
|
||||
error(xp, "Missing element name");
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Tell the user that the opening element is now complete
|
||||
*/
|
||||
state = EXML_ELT_DEFINED;
|
||||
rc = (*handler)(xp, state, tname, 0, 0);
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
state = EXML_ELT_DATA;
|
||||
break;
|
||||
|
||||
case TOKEN_SLASH_GR:
|
||||
/*
|
||||
* If we see a "/>" then this is a solo element
|
||||
*/
|
||||
if (*tname == '\0') {
|
||||
error(xp, "Missing element name");
|
||||
goto err;
|
||||
}
|
||||
state = EXML_SOLO_ELT_DEFINED;
|
||||
rc = (*handler)(xp, state, tname, 0, 0);
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
rc = 1;
|
||||
goto exit;
|
||||
|
||||
default:
|
||||
error(xp, "Syntax error");
|
||||
goto err;
|
||||
}
|
||||
break;
|
||||
|
||||
case EXML_ELT_DATA: /* -------------------------------------- */
|
||||
/*
|
||||
* We have seen the full opening element "<name ...>" and now
|
||||
* await data or another element.
|
||||
*/
|
||||
if (token == TOKEN_LS) {
|
||||
/*
|
||||
* Recurse to handle the new element, comment etc.
|
||||
*/
|
||||
rc = parseNext(xp, EXML_AFTER_LS);
|
||||
if (rc < 0) {
|
||||
goto exit;
|
||||
}
|
||||
break;
|
||||
|
||||
} else if (token == TOKEN_LS_SLASH) {
|
||||
state = EXML_END_ELT;
|
||||
break;
|
||||
|
||||
} else if (token != TOKEN_TEXT) {
|
||||
goto err;
|
||||
}
|
||||
if (mprGetBufLength(tokBuf) > 0) {
|
||||
/*
|
||||
* Pass the data between the element to the user
|
||||
*/
|
||||
rc = (*handler)(xp, state, tname, 0, mprGetBufStart(tokBuf));
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case EXML_END_ELT: /* -------------------------------------- */
|
||||
if (token != TOKEN_TEXT) {
|
||||
error(xp, "Missing closing element name for \"%s\"", tname);
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* The closing element name must match the opening element name
|
||||
*/
|
||||
if (strcmp(tname, mprGetBufStart(tokBuf)) != 0) {
|
||||
error(xp,
|
||||
"Closing element name \"%s\" does not match on line %d"
|
||||
"opening name \"%s\"",
|
||||
mprGetBufStart(tokBuf), xp->lineNumber, tname);
|
||||
goto err;
|
||||
}
|
||||
rc = (*handler)(xp, state, tname, 0, 0);
|
||||
if (rc < 0) {
|
||||
goto err;
|
||||
}
|
||||
if (getToken(xp, state) != TOKEN_GR) {
|
||||
error(xp, "Syntax error");
|
||||
goto err;
|
||||
}
|
||||
return 1;
|
||||
|
||||
case EXML_EOF: /* ---------------------------------------------- */
|
||||
goto exit;
|
||||
|
||||
case EXML_ERR: /* ---------------------------------------------- */
|
||||
default:
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
mprAssert(0);
|
||||
|
||||
err:
|
||||
rc = -1;
|
||||
|
||||
exit:
|
||||
mprFree(tname);
|
||||
mprFree(aname);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Lexical analyser for XML. Return the next token reading input as required.
|
||||
* It uses a one token look ahead and push back mechanism (LAR1 parser).
|
||||
* Text token identifiers are left in the tokBuf parser buffer on exit.
|
||||
* This Lex has special cases for the states EXML_ELT_DATA where we
|
||||
* have an optimized read of element data, and EXML_AFTER_LS where we
|
||||
* distinguish between element names, processing instructions and comments.
|
||||
*/
|
||||
|
||||
static ExmlToken getToken(Exml *xp, int state)
|
||||
{
|
||||
MprBuf *tokBuf, *inBuf;
|
||||
uchar *cp;
|
||||
int c, rc;
|
||||
|
||||
tokBuf = xp->tokBuf;
|
||||
inBuf = xp->inBuf;
|
||||
|
||||
mprAssert(state >= 0);
|
||||
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
mprFlushBuf(tokBuf);
|
||||
|
||||
/*
|
||||
* Special case parsing for names and for element data. We do this for
|
||||
* performance so we can return to the caller the largest token possible
|
||||
*/
|
||||
if (state == EXML_ELT_DATA) {
|
||||
/*
|
||||
* Read all the data up to the start of the closing element "<" or the
|
||||
* start of a sub-element.
|
||||
*/
|
||||
#if UNUSED
|
||||
while (isspace(c)) {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (c == '<') {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
if (c == '/') {
|
||||
return TOKEN_LS_SLASH;
|
||||
}
|
||||
putLastChar(xp, c);
|
||||
return TOKEN_LS;
|
||||
}
|
||||
do {
|
||||
if (mprPutCharToBuf(tokBuf, c) < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
}
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
} while (c != '<');
|
||||
|
||||
/*
|
||||
* Put back the last look-ahead character
|
||||
*/
|
||||
putLastChar(xp, c);
|
||||
|
||||
/*
|
||||
* If all white space, then zero the token buffer
|
||||
*/
|
||||
for (cp = tokBuf->start; *cp; cp++) {
|
||||
if (!isspace(*cp)) {
|
||||
return TOKEN_TEXT;
|
||||
}
|
||||
}
|
||||
mprFlushBuf(tokBuf);
|
||||
return TOKEN_TEXT;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\t':
|
||||
case '\r':
|
||||
break;
|
||||
|
||||
case '<':
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
if (c == '/') {
|
||||
return TOKEN_LS_SLASH;
|
||||
}
|
||||
putLastChar(xp, c);
|
||||
return TOKEN_LS;
|
||||
|
||||
case '=':
|
||||
return TOKEN_EQ;
|
||||
|
||||
case '>':
|
||||
return TOKEN_GR;
|
||||
|
||||
case '/':
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
if (c == '>') {
|
||||
return TOKEN_SLASH_GR;
|
||||
}
|
||||
return TOKEN_ERR;
|
||||
|
||||
case '\"':
|
||||
case '\'':
|
||||
xp->quoteChar = c;
|
||||
/* Fall through */
|
||||
|
||||
default:
|
||||
/*
|
||||
* We handle element names, attribute names and attribute values
|
||||
* here. We do NOT handle data between elements here. Read the
|
||||
* token. Stop on white space or a closing element ">"
|
||||
*/
|
||||
if (xp->quoteChar) {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
while (c != xp->quoteChar) {
|
||||
if (mprPutCharToBuf(tokBuf, c) < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
}
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
}
|
||||
xp->quoteChar = 0;
|
||||
|
||||
} else {
|
||||
while (!isspace(c) && c != '>' && c != '/' && c != '=') {
|
||||
if (mprPutCharToBuf(tokBuf, c) < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
}
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
}
|
||||
putLastChar(xp, c);
|
||||
}
|
||||
if (mprGetBufLength(tokBuf) <= 0) {
|
||||
return TOKEN_ERR;
|
||||
}
|
||||
mprAddNullToBuf(tokBuf);
|
||||
|
||||
if (state == EXML_AFTER_LS) {
|
||||
/*
|
||||
* If we are just inside an element "<", then analyze what we
|
||||
* have to see if we have an element name, instruction or
|
||||
* comment. Tokbuf will hold "?" for instructions or "!--"
|
||||
* for comments.
|
||||
*/
|
||||
if (mprLookAtNextCharInBuf(tokBuf) == '?') {
|
||||
/* Just ignore processing instructions */
|
||||
rc = scanFor(xp, "?>");
|
||||
if (rc < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
} else if (rc == 0) {
|
||||
return TOKEN_ERR;
|
||||
}
|
||||
return TOKEN_INSTRUCTIONS;
|
||||
|
||||
} else if (mprLookAtNextCharInBuf(tokBuf) == '!') {
|
||||
/*
|
||||
* First discard the comment leadin "!--" and eat leading
|
||||
* white space.
|
||||
*/
|
||||
if (strcmp((char*) tokBuf->start, "![CDATA[") == 0) {
|
||||
mprFlushBuf(tokBuf);
|
||||
#if UNUSED
|
||||
c = mprLookAtNextCharInBuf(inBuf);
|
||||
while (isspace(c)) {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
c = mprLookAtNextCharInBuf(inBuf);
|
||||
}
|
||||
#endif
|
||||
rc = scanFor(xp, "]]>");
|
||||
if (rc < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
} else if (rc == 0) {
|
||||
return TOKEN_ERR;
|
||||
}
|
||||
return TOKEN_CDATA;
|
||||
|
||||
} else {
|
||||
mprFlushBuf(tokBuf);
|
||||
#if UNUSED
|
||||
c = mprLookAtNextCharInBuf(inBuf);
|
||||
while (isspace(c)) {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
c = mprLookAtNextCharInBuf(inBuf);
|
||||
}
|
||||
#endif
|
||||
rc = scanFor(xp, "-->");
|
||||
if (rc < 0) {
|
||||
return TOKEN_TOO_BIG;
|
||||
} else if (rc == 0) {
|
||||
return TOKEN_ERR;
|
||||
}
|
||||
return TOKEN_COMMENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
trimToken(xp);
|
||||
return TOKEN_TEXT;
|
||||
}
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return TOKEN_EOF;
|
||||
}
|
||||
}
|
||||
|
||||
/* Should never get here */
|
||||
mprAssert(0);
|
||||
return TOKEN_ERR;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Scan for a pattern. Eat and discard input up to the pattern. Return 1 if
|
||||
* the pattern was found, return 0 if not found. Return < 0 on errors.
|
||||
*/
|
||||
|
||||
static int scanFor(Exml *xp, char *str)
|
||||
{
|
||||
MprBuf *tokBuf;
|
||||
char *cp;
|
||||
int c;
|
||||
|
||||
mprAssert(str);
|
||||
|
||||
tokBuf = xp->tokBuf;
|
||||
|
||||
while (1) {
|
||||
for (cp = str; *cp; cp++) {
|
||||
if ((c = getNextChar(xp)) < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (tokBuf) {
|
||||
if (mprPutCharToBuf(tokBuf, c) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (c != *cp) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*cp == '\0') {
|
||||
/*
|
||||
* Remove the pattern from the tokBuf
|
||||
*/
|
||||
if (tokBuf) {
|
||||
mprAdjustBufEnd(tokBuf, -(int) strlen(str));
|
||||
trimToken(xp);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get another character. We read and buffer blocks of data if we need more
|
||||
* data to parse.
|
||||
*/
|
||||
|
||||
static int getNextChar(Exml *xp)
|
||||
{
|
||||
MprBuf *inBuf;
|
||||
char c;
|
||||
int l;
|
||||
|
||||
inBuf = xp->inBuf;
|
||||
if (mprGetBufLength(inBuf) <= 0) {
|
||||
/*
|
||||
* Flush to reset the servp/endp pointers to the start of the buffer
|
||||
* so we can do a maximal read
|
||||
*/
|
||||
mprFlushBuf(inBuf);
|
||||
l = (xp->readFn)(xp, xp->inputArg, mprGetBufStart(inBuf),
|
||||
mprGetBufLinearSpace(inBuf));
|
||||
if (l <= 0) {
|
||||
return -1;
|
||||
}
|
||||
mprAdjustBufEnd(inBuf, l);
|
||||
}
|
||||
c = mprGetCharFromBuf(inBuf);
|
||||
|
||||
if (c == '\n') {
|
||||
xp->lineNumber++;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Put back a character in the input buffer
|
||||
*/
|
||||
|
||||
static int putLastChar(Exml *xp, int c)
|
||||
{
|
||||
if (mprInsertCharToBuf(xp->inBuf, (char) c) < 0) {
|
||||
mprAssert(0);
|
||||
return -1;
|
||||
}
|
||||
if (c == '\n') {
|
||||
xp->lineNumber--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Output a parse message
|
||||
*/
|
||||
|
||||
static void error(Exml *xp, char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
|
||||
mprAssert(fmt);
|
||||
|
||||
va_start(args, fmt);
|
||||
mprAllocVsprintf(MPR_LOC_ARGS(xp), &buf, MPR_MAX_STRING, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
/*
|
||||
* MOB need to add the failing line text and a pointer to which column
|
||||
*/
|
||||
mprFree(xp->errMsg);
|
||||
mprAllocSprintf(MPR_LOC_ARGS(xp), &xp->errMsg, MPR_MAX_STRING,
|
||||
"XML error: %s\nAt line %d\n", buf, xp->lineNumber);
|
||||
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Remove trailing whitespace in a token and ensure it is terminated with
|
||||
* a NULL for easy parsing
|
||||
*/
|
||||
|
||||
static void trimToken(Exml *xp)
|
||||
{
|
||||
while (isspace(mprLookAtLastCharInBuf(xp->tokBuf))) {
|
||||
mprAdjustBufEnd(xp->tokBuf, -1);
|
||||
}
|
||||
mprAddNullToBuf(xp->tokBuf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
const char *exmlGetErrorMsg(Exml *xp)
|
||||
{
|
||||
if (xp->errMsg == 0) {
|
||||
return "";
|
||||
}
|
||||
return xp->errMsg;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int exmlGetLineNumber(Exml *xp)
|
||||
{
|
||||
return xp->lineNumber;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#else
|
||||
|
||||
void exmlParserDummy() {}
|
||||
#endif /* BLD_FEATURE_EXML */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1 @@
|
||||
${BLD_OBJ_DIR}/exmlParser${BLD_OBJ}
|
||||
@@ -0,0 +1,41 @@
|
||||
#
|
||||
# Makefile for the Mbedthis Portable Runtime (MPR) library
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
|
||||
include make.dep
|
||||
|
||||
ifeq ($(BLD_HOST_UNIX),1)
|
||||
PRE_DIRS = UNIX
|
||||
else
|
||||
PRE_DIRS = $(BLD_HOST_OS)
|
||||
endif
|
||||
|
||||
POST_DIRS = package
|
||||
|
||||
TARGETS += $(BLD_BIN_DIR)/libmpr$(BLD_LIB)
|
||||
|
||||
compileExtra: $(TARGETS)
|
||||
|
||||
#
|
||||
# Build the mpr libraries
|
||||
#
|
||||
$(BLD_BIN_DIR)/libmpr$(BLD_LIB): files \
|
||||
$(shell BLD_OBJ=$(BLD_OBJ) \; BLD_OBJ_DIR=$(BLD_OBJ_DIR) \; \
|
||||
eval echo `cat files`)
|
||||
@bld --library $(BLD_BIN_DIR)/libmpr \
|
||||
--objectsDir $(BLD_OBJ_DIR) --objectList files
|
||||
|
||||
cleanExtra:
|
||||
@echo "rm -f $(TARGETS)" | $(BLDOUT)
|
||||
@rm -f $(TARGETS)
|
||||
@rm -f $(BLD_BIN_DIR)/libmpr.*
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# Makefile for the Mbedthis Portable Runtime (MPR) library for UNIX
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I..
|
||||
|
||||
include make.dep
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @file mprFile.c
|
||||
* @brief File services for Unix
|
||||
* @overview
|
||||
* @remarks
|
||||
* See mprGenFile.c for other file services.
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int mprGetFileInfo(MprCtx ctx, const char *path, MprFileInfo *info)
|
||||
{
|
||||
struct stat s;
|
||||
|
||||
mprAssert(path);
|
||||
mprAssert(info);
|
||||
|
||||
if (stat(path, &s) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
info->size = s.st_size;
|
||||
info->ctime = s.st_ctime;
|
||||
info->mtime = s.st_mtime;
|
||||
info->inode = s.st_ino;
|
||||
info->isDir = (s.st_mode & S_IFDIR) != 0;
|
||||
info->isReg = (s.st_mode & S_IFREG) != 0;
|
||||
|
||||
if (strcmp(path, "/dev/null") == 0) {
|
||||
info->isReg = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprMakeDir(MprCtx ctx, const char *path, int perms)
|
||||
{
|
||||
return mkdir(path, perms);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* @file mprPlatform.c
|
||||
* @brief Cross platform routines
|
||||
* @overview This module provides low level cross platform routines.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
char *mprInetToStr(char *buffer, int bufsize, const struct in_addr in)
|
||||
{
|
||||
#if HAVE_NTOA_R
|
||||
inet_ntoa_r(in, buffer, bufsize);
|
||||
#else
|
||||
uchar *cp;
|
||||
/* FUTURE -- this is not portable */
|
||||
cp = (uchar*) ∈
|
||||
mprSprintf(buffer, bufsize, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
|
||||
#endif
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetShell(MprCtx ctx, void *shell)
|
||||
{
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetShell(MprCtx ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Sleep. Period given in milliseconds.
|
||||
*/
|
||||
|
||||
void mprSleep(MprCtx ctx, int milliseconds)
|
||||
{
|
||||
struct timespec timeout;
|
||||
int rc;
|
||||
|
||||
mprAssert(milliseconds >= 0);
|
||||
timeout.tv_sec = milliseconds / 1000;
|
||||
timeout.tv_nsec = (milliseconds % 1000) * 1000000;
|
||||
do {
|
||||
rc = nanosleep(&timeout, 0);
|
||||
} while (rc < 0 && errno == EINTR);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Make intervening directories
|
||||
*/
|
||||
|
||||
int mprMakeDirPath(MprCtx ctx, const char *path)
|
||||
{
|
||||
char dir[MPR_MAX_PATH], buf[MPR_MAX_PATH];
|
||||
char *dirSep;
|
||||
char *next, *tok;
|
||||
|
||||
dir[0] = '\0';
|
||||
dirSep = "/\\";
|
||||
|
||||
if (path == 0 || *path == '\0') {
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
|
||||
mprStrcpy(buf, sizeof(buf), path);
|
||||
next = mprStrTok(buf, dirSep, &tok);
|
||||
if (*buf == '/') {
|
||||
dir[0] = '/';
|
||||
}
|
||||
while (next != NULL) {
|
||||
if (strcmp(next, ".") == 0 ) {
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
continue;
|
||||
}
|
||||
strcat(dir, next);
|
||||
if (access(dir, R_OK) != 0) {
|
||||
if (mkdir(dir, 0666) < 0) {
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
}
|
||||
strcat(dir, "/");
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get a fully qualified file name for the given path. Return with forward
|
||||
* slashes always
|
||||
*/
|
||||
|
||||
char *mprGetFullPathName(char *buf, int buflen, const char *path)
|
||||
{
|
||||
if (mprStrcpy(buf, buflen, path) < 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Replacement for gethostbyname that is multi-thread safe
|
||||
*/
|
||||
|
||||
struct hostent *mprGetHostByName(MprCtx ctx, const char *name)
|
||||
{
|
||||
MprApp *app;
|
||||
struct hostent *hp;
|
||||
struct hostent *ip;
|
||||
int count, i;
|
||||
|
||||
hp = (struct hostent*) mprAlloc(ctx, sizeof(struct hostent));
|
||||
memset(hp, 0, sizeof(struct hostent));
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
#undef gethostbyname
|
||||
|
||||
mprGlobalLock(app);
|
||||
ip = gethostbyname(name);
|
||||
mprGlobalUnlock(app);
|
||||
|
||||
if (ip == 0) {
|
||||
return 0;
|
||||
}
|
||||
hp->h_addrtype = ip->h_addrtype;
|
||||
hp->h_length = ip->h_length;
|
||||
hp->h_name = mprStrdup(hp, ip->h_name);
|
||||
hp->h_addr_list = 0;
|
||||
hp->h_aliases = 0;
|
||||
|
||||
for (count = 0; ip->h_addr_list[count] != 0; ) {
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
count++;
|
||||
hp->h_addr_list = mprAlloc(hp, count * sizeof(char*));
|
||||
for (i = 0; ip->h_addr_list[i] != 0; i++) {
|
||||
memcpy(&hp->h_addr_list[i], &ip->h_addr_list[i], ip->h_length);
|
||||
}
|
||||
hp->h_addr_list[i] = 0;
|
||||
}
|
||||
|
||||
for (count = 0; ip->h_aliases[count] != 0; ) {
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
count++;
|
||||
hp->h_aliases = mprAlloc(hp, count * sizeof(char*));
|
||||
for (i = 0; ip->h_aliases[i] != 0; i++) {
|
||||
hp->h_aliases[i] = mprStrdup(hp, ip->h_aliases[i]);
|
||||
}
|
||||
hp->h_aliases[i] = 0;
|
||||
}
|
||||
return hp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* @file mprTime.c
|
||||
* @brief Time handling for Unix
|
||||
* @overview
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#undef localtime
|
||||
#undef localtime_r
|
||||
#undef gmtime
|
||||
#undef gmtime_r
|
||||
#undef ctime
|
||||
#undef ctime_r
|
||||
#undef asctime
|
||||
#undef asctime_r
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Returns time in seconds and milliseconds. This is NOT time-of-day.
|
||||
*/
|
||||
|
||||
MprTime *mprGetTime(MprCtx ctx, MprTime *tp)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
if (gettimeofday(&tv, 0) < 0) {
|
||||
mprAssert(0);
|
||||
tp->sec = 0;
|
||||
tp->msec = 0;
|
||||
return tp;
|
||||
}
|
||||
tp->sec = tv.tv_sec;
|
||||
tp->msec = tv.tv_usec / 1000;
|
||||
return tp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of localtime
|
||||
*/
|
||||
|
||||
struct tm *mprLocaltime(MprCtx ctx, struct tm *timep, time_t *now)
|
||||
{
|
||||
localtime_r(now, timep);
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of gmtime
|
||||
*/
|
||||
|
||||
struct tm *mprGmtime(MprCtx ctx, time_t *now, struct tm *timep)
|
||||
{
|
||||
gmtime_r(now, timep);
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of ctime
|
||||
*/
|
||||
|
||||
int mprCtime(MprCtx ctx, char *buf, int bufsize, const time_t *timer)
|
||||
{
|
||||
char localBuf[80];
|
||||
char *cp;
|
||||
int len;
|
||||
|
||||
mprAssert(buf);
|
||||
|
||||
mprGlobalLock(ctx);
|
||||
|
||||
cp = ctime_r(timer, localBuf);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprStrcpy(buf, bufsize, "WONT FIT");
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
len = mprStrcpy(buf, bufsize, cp);
|
||||
|
||||
if (buf[len - 1] == '\n') {
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
|
||||
mprGlobalUnlock(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of asctime
|
||||
*/
|
||||
|
||||
int mprAsctime(MprCtx ctx, char *buf, int bufsize, const struct tm *timeptr)
|
||||
{
|
||||
char *cp;
|
||||
char localBuf[80];
|
||||
|
||||
cp = asctime_r(timeptr, localBuf);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
mprStrcpy(buf, bufsize, cp);
|
||||
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# Makefile for the Mbedthis Portable Runtime (MPR) library for VXWORKS
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I..
|
||||
|
||||
include make.dep
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* @file mprUnixFile.c
|
||||
* @brief File services for Unix
|
||||
* @overview
|
||||
* @remarks
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int mprGetFileInfo(MprCtx ctx, const char *path, MprFileInfo *info)
|
||||
{
|
||||
struct stat s;
|
||||
|
||||
mprAssert(path);
|
||||
mprAssert(info);
|
||||
|
||||
if (stat(path, &s) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
info->size = s.st_size;
|
||||
info->ctime = s.st_ctime;
|
||||
info->mtime = s.st_mtime;
|
||||
info->inode = s.st_ino;
|
||||
info->isDir = (s.st_mode & S_IFDIR) != 0;
|
||||
info->isReg = (s.st_mode & S_IFREG) != 0;
|
||||
|
||||
if (strcmp(path, "/dev/null") == 0) {
|
||||
info->isReg = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprMakeDir(MprCtx ctx, const char *path, int perms)
|
||||
{
|
||||
return mkdir(path, perms);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* @file mprPlatform.c
|
||||
* @brief Cross platform routines
|
||||
* @overview This module provides low level cross platform routines.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
char *mprInetToStr(char *buffer, int bufsize, const struct in_addr in)
|
||||
{
|
||||
#if HAVE_NTOA_R
|
||||
inet_ntoa_r(in, buffer, bufsize);
|
||||
#else
|
||||
uchar *cp;
|
||||
/* FUTURE -- this is not portable */
|
||||
cp = (uchar*) ∈
|
||||
mprSprintf(buffer, bufsize, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
|
||||
#endif
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetShell(MprCtx ctx, void *shell)
|
||||
{
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetShell(MprCtx ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Sleep. Period given in milliseconds.
|
||||
*/
|
||||
|
||||
void mprSleep(MprCtx ctx, int milliseconds)
|
||||
{
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
|
||||
timeout.tv_sec = milliseconds / 1000;
|
||||
timeout.tv_usec = (milliseconds % 1000) * 1000;
|
||||
do {
|
||||
rc = select(1, 0, 0, 0, &timeout);
|
||||
} while (rc < 0 && errno == EINTR);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Make intervening directories
|
||||
*/
|
||||
|
||||
int mprMakeDirPath(MprCtx ctx, const char *path)
|
||||
{
|
||||
char dir[MPR_MAX_PATH], buf[MPR_MAX_PATH];
|
||||
char *dirSep;
|
||||
char *next, *tok;
|
||||
|
||||
dir[0] = '\0';
|
||||
dirSep = "/\\";
|
||||
|
||||
if (path == 0 || *path == '\0') {
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
|
||||
mprStrcpy(buf, sizeof(buf), path);
|
||||
next = mprStrTok(buf, dirSep, &tok);
|
||||
if (*buf == '/') {
|
||||
dir[0] = '/';
|
||||
}
|
||||
while (next != NULL) {
|
||||
if (strcmp(next, ".") == 0 ) {
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
continue;
|
||||
}
|
||||
strcat(dir, next);
|
||||
if (access(dir, R_OK) != 0) {
|
||||
if (mkdir(dir) < 0) {
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
}
|
||||
strcat(dir, "/");
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get a fully qualified file name for the given path. Return with forward
|
||||
* slashes always
|
||||
*/
|
||||
|
||||
char *mprGetFullPathName(char *buf, int buflen, const char *path)
|
||||
{
|
||||
if (mprStrcpy(buf, buflen, path) < 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Replacement for gethostbyname that is multi-thread safe
|
||||
*/
|
||||
|
||||
struct hostent *mprGetHostByName(MprCtx ctx, const char *name)
|
||||
{
|
||||
struct hostent *hp;
|
||||
|
||||
hp = (struct hostent*) mprAlloc(ctx, sizeof(struct hostent));
|
||||
memset(hp, 0, sizeof(struct hostent));
|
||||
|
||||
struct in_addr inaddr;
|
||||
inaddr.s_addr = (ulong) hostGetByName(name);
|
||||
if (inaddr.s_addr < 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
hp->h_addrtype = AF_INET;
|
||||
hp->h_length = sizeof(int);
|
||||
hp->h_name = mprStrdup(name);
|
||||
hp->h_addr_list = 0;
|
||||
hp->h_aliases = 0;
|
||||
|
||||
hp->h_addr_list = new char*[2];
|
||||
hp->h_addr_list[0] = (char *) mprAlloc(hp, sizeof(struct in_addr));
|
||||
memcpy(&hp->h_addr_list[0], &inaddr, hp->h_length);
|
||||
hp->h_addr_list[1] = 0;
|
||||
|
||||
return hp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* @file mprTime.c
|
||||
* @brief Time handling for VxWorks
|
||||
* @overview
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#undef localtime
|
||||
#undef localtime_r
|
||||
#undef gmtime
|
||||
#undef gmtime_r
|
||||
#undef ctime
|
||||
#undef ctime_r
|
||||
#undef asctime
|
||||
#undef asctime_r
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Returns time in seconds and milliseconds. This is NOT time-of-day.
|
||||
*/
|
||||
|
||||
MprTime *mprGetTime(MprCtx ctx, MprTime *tp)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
if (gettimeofday(&tv, 0) < 0) {
|
||||
mprAssert(0);
|
||||
tp->sec = 0;
|
||||
tp->msec = 0;
|
||||
return tp;
|
||||
}
|
||||
tp->sec = tv.tv_sec;
|
||||
tp->msec = tv.tv_usec / 1000;
|
||||
return tp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of localtime
|
||||
*/
|
||||
|
||||
struct tm *mprLocaltime(MprCtx ctx, struct tm *timep, time_t *now)
|
||||
{
|
||||
localtime_r(now, timep);
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of gmtime
|
||||
*/
|
||||
|
||||
struct tm *mprGmtime(MprCtx ctx, time_t *now, struct tm *timep)
|
||||
{
|
||||
gmtime_r(now, timep);
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of ctime
|
||||
*/
|
||||
|
||||
int mprCtime(MprCtx ctx, char *buf, int bufsize, const time_t *timer)
|
||||
{
|
||||
char localBuf[80];
|
||||
char *cp;
|
||||
int len;
|
||||
|
||||
mprAssert(buf);
|
||||
|
||||
mprGlobalLock(ctx);
|
||||
|
||||
cp = ctime_r(timer, localBuf);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprStrcpy(buf, bufsize, "WONT FIT");
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
len = mprStrcpy(buf, bufsize, cp);
|
||||
|
||||
if (buf[len - 1] == '\n') {
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
|
||||
mprGlobalUnlock(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of asctime
|
||||
*/
|
||||
|
||||
int mprAsctime(MprCtx ctx, char *buf, int bufsize, const struct tm *timeptr)
|
||||
{
|
||||
char *cp;
|
||||
char localBuf[80];
|
||||
|
||||
cp = asctime_r(timeptr, localBuf);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
mprStrcpy(buf, bufsize, cp);
|
||||
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# Makefile for the Mbedthis Portable Runtime (MPR) library for Windows
|
||||
#
|
||||
# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
#
|
||||
|
||||
COMPILE := *.c
|
||||
EXPORT_OBJECTS := yes
|
||||
MAKE_IFLAGS := -I..
|
||||
|
||||
include make.dep
|
||||
|
||||
## Local variables:
|
||||
## tab-width: 4
|
||||
## End:
|
||||
## vim: tw=78 sw=4 ts=4
|
||||
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @file mprWinFile.c
|
||||
* @brief File services for Windows
|
||||
* @overview
|
||||
* @remarks
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int mprGetFileInfo(MprCtx ctx, const char *path, MprFileInfo *info)
|
||||
{
|
||||
struct stat s;
|
||||
|
||||
mprAssert(path);
|
||||
mprAssert(info);
|
||||
|
||||
if (stat(path, &s) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
info->size = s.st_size;
|
||||
/*
|
||||
* MOB -- these are time64_t. Loss of precision
|
||||
*/
|
||||
info->ctime = (uint) s.st_ctime;
|
||||
info->mtime = (uint) s.st_mtime;
|
||||
info->inode = s.st_ino;
|
||||
info->isDir = (s.st_mode & S_IFDIR) != 0;
|
||||
info->isReg = (s.st_mode & S_IFREG) != 0;
|
||||
|
||||
/*
|
||||
* Work hard on windows to determine if the file is a regular file.
|
||||
* FUTURE -- OPT. Eliminate this CreateFile.
|
||||
*/
|
||||
if (info->isReg) {
|
||||
long fileType, att;
|
||||
|
||||
if ((att = GetFileAttributes(path)) == -1) {
|
||||
return -1;
|
||||
}
|
||||
if (att & (FILE_ATTRIBUTE_REPARSE_POINT |
|
||||
FILE_ATTRIBUTE_DIRECTORY |
|
||||
FILE_ATTRIBUTE_ENCRYPTED |
|
||||
FILE_ATTRIBUTE_SYSTEM |
|
||||
FILE_ATTRIBUTE_OFFLINE)) {
|
||||
/*
|
||||
* Catch accesses to devices like CON, AUX, NUL, LPT etc
|
||||
* att will be set to ENCRYPTED on Win9X and NT.
|
||||
*/
|
||||
info->isReg = 0;
|
||||
}
|
||||
if (info->isReg) {
|
||||
HANDLE handle;
|
||||
handle = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0, OPEN_EXISTING, 0, 0);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
info->isReg = 0;
|
||||
} else {
|
||||
fileType = GetFileType(handle);
|
||||
if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE) {
|
||||
info->isReg = 0;
|
||||
}
|
||||
CloseHandle(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strcmp(path, "nul") == 0) {
|
||||
info->isReg = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprMakeDir(MprCtx ctx, const char *path, int perms)
|
||||
{
|
||||
return mkdir(path, perms);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,378 @@
|
||||
/**
|
||||
* @file mprPlatform.c
|
||||
* @brief Cross platform routines
|
||||
* @overview This module provides low level cross platform routines.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/**************************** Forward Declarations ****************************/
|
||||
|
||||
static const char *getHive(const char *keyPath, HKEY *hive);
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
char *mprInetToStr(char *buffer, int bufsize, const struct in_addr in)
|
||||
{
|
||||
#if HAVE_NTOA_R
|
||||
inet_ntoa_r(in, buffer, bufsize);
|
||||
#else
|
||||
uchar *cp;
|
||||
cp = (uchar*) ∈
|
||||
mprSprintf(buffer, bufsize, "%d.%d.%d.%d", cp[0], cp[1], cp[2], cp[3]);
|
||||
#endif
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetShell(MprCtx ctx, void *shell)
|
||||
{
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetShell(MprCtx ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Sleep. Period given in milliseconds.
|
||||
*/
|
||||
|
||||
void mprSleep(MprCtx ctx, int milliseconds)
|
||||
{
|
||||
Sleep(milliseconds);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Make intervening directories
|
||||
*/
|
||||
|
||||
int mprMakeDirPath(MprCtx ctx, const char *path)
|
||||
{
|
||||
char dir[MPR_MAX_PATH], buf[MPR_MAX_PATH];
|
||||
char *dirSep;
|
||||
char *next, *tok;
|
||||
|
||||
dir[0] = '\0';
|
||||
dirSep = "/\\";
|
||||
|
||||
if (path == 0 || *path == '\0') {
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
|
||||
mprStrcpy(buf, sizeof(buf), path);
|
||||
next = mprStrTok(buf, dirSep, &tok);
|
||||
if (*buf == '/') {
|
||||
dir[0] = '/';
|
||||
}
|
||||
while (next != NULL) {
|
||||
if (strcmp(next, ".") == 0 ) {
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
continue;
|
||||
}
|
||||
strcat(dir, next);
|
||||
if (access(dir, R_OK) != 0) {
|
||||
if (_mkdir(dir) < 0) {
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
}
|
||||
strcat(dir, "/");
|
||||
next = mprStrTok(NULL, dirSep, &tok);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get a fully qualified file name for the given path. Return with forward
|
||||
* slashes always
|
||||
*/
|
||||
|
||||
char *mprGetFullPathName(char *buf, int buflen, const char *path)
|
||||
{
|
||||
#if (WIN || NW || OS2) && !BLD_FEATURE_ROMFS
|
||||
char *junk, *cp;
|
||||
int rc;
|
||||
|
||||
--buflen;
|
||||
rc = GetFullPathName(path, buflen, buf, &junk);
|
||||
for (cp = buf; *cp; cp++) {
|
||||
if (*cp == '\\') {
|
||||
*cp = '/';
|
||||
}
|
||||
}
|
||||
buf[buflen] = '\0';
|
||||
#else
|
||||
if (mprStrcpy(buf, buflen, path) < 0) {
|
||||
mprAssert(0);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Replacement for gethostbyname that is multi-thread safe
|
||||
*/
|
||||
|
||||
struct hostent *mprGetHostByName(MprCtx ctx, const char *name)
|
||||
{
|
||||
MprApp *app;
|
||||
struct hostent *hp;
|
||||
struct hostent *ip;
|
||||
int count, i;
|
||||
|
||||
hp = (struct hostent*) mprAlloc(ctx, sizeof(struct hostent));
|
||||
memset(hp, 0, sizeof(struct hostent));
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
#undef gethostbyname
|
||||
|
||||
mprGlobalLock(app);
|
||||
ip = gethostbyname(name);
|
||||
mprGlobalUnlock(app);
|
||||
|
||||
if (ip == 0) {
|
||||
return 0;
|
||||
}
|
||||
hp->h_addrtype = ip->h_addrtype;
|
||||
hp->h_length = ip->h_length;
|
||||
hp->h_name = mprStrdup(hp, ip->h_name);
|
||||
hp->h_addr_list = 0;
|
||||
hp->h_aliases = 0;
|
||||
|
||||
for (count = 0; ip->h_addr_list[count] != 0; ) {
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
count++;
|
||||
hp->h_addr_list = mprAlloc(hp, count * sizeof(char*));
|
||||
for (i = 0; ip->h_addr_list[i] != 0; i++) {
|
||||
memcpy(&hp->h_addr_list[i], &ip->h_addr_list[i], ip->h_length);
|
||||
}
|
||||
hp->h_addr_list[i] = 0;
|
||||
}
|
||||
|
||||
for (count = 0; ip->h_aliases[count] != 0; ) {
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
count++;
|
||||
hp->h_aliases = mprAlloc(hp, count * sizeof(char*));
|
||||
for (i = 0; ip->h_aliases[i] != 0; i++) {
|
||||
hp->h_aliases[i] = mprStrdup(hp, ip->h_aliases[i]);
|
||||
}
|
||||
hp->h_aliases[i] = 0;
|
||||
}
|
||||
return hp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Read a registry value. Returns allocated memory in buf.
|
||||
*/
|
||||
|
||||
int mprReadRegistry(MprCtx ctx, char **buf, int max, const char *key,
|
||||
const char *name)
|
||||
{
|
||||
HKEY top, h;
|
||||
char *value;
|
||||
ulong type, size;
|
||||
|
||||
mprAssert(key && *key);
|
||||
mprAssert(buf);
|
||||
|
||||
/*
|
||||
* Get the registry hive
|
||||
*/
|
||||
if ((key = getHive(key, &top)) == 0) {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
|
||||
if (RegOpenKeyEx(top, key, 0, KEY_READ, &h) != ERROR_SUCCESS) {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the type
|
||||
*/
|
||||
if (RegQueryValueEx(h, name, 0, &type, 0, &size) != ERROR_SUCCESS) {
|
||||
RegCloseKey(h);
|
||||
return MPR_ERR_CANT_READ;
|
||||
}
|
||||
if (type != REG_SZ && type != REG_EXPAND_SZ) {
|
||||
RegCloseKey(h);
|
||||
return MPR_ERR_BAD_TYPE;
|
||||
}
|
||||
|
||||
value = (char*) mprAlloc(ctx, size);
|
||||
if ((int) size > max) {
|
||||
RegCloseKey(h);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (RegQueryValueEx(h, name, 0, &type, (uchar*) value, &size) !=
|
||||
ERROR_SUCCESS) {
|
||||
mprFree(value);
|
||||
RegCloseKey(h);
|
||||
return MPR_ERR_CANT_READ;
|
||||
}
|
||||
|
||||
RegCloseKey(h);
|
||||
*buf = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Write a string registry value. Returns allocated memory in buf.
|
||||
*/
|
||||
|
||||
int mprWriteRegistry(MprCtx ctx, const char *key, const char *name,
|
||||
const char *value)
|
||||
{
|
||||
HKEY top, h, subHandle;
|
||||
ulong disposition;
|
||||
|
||||
mprAssert(key && *key);
|
||||
mprAssert(name && *name);
|
||||
mprAssert(value && *value);
|
||||
|
||||
/*
|
||||
* Get the registry hive
|
||||
*/
|
||||
if ((key = getHive(key, &top)) == 0) {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
|
||||
if (name) {
|
||||
/*
|
||||
* Write a registry string value
|
||||
*/
|
||||
if (RegOpenKeyEx(top, key, 0, KEY_ALL_ACCESS, &h) != ERROR_SUCCESS) {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
if (RegSetValueEx(h, name, 0, REG_SZ, value, strlen(value) + 1)
|
||||
!= ERROR_SUCCESS) {
|
||||
RegCloseKey(h);
|
||||
return MPR_ERR_CANT_READ;
|
||||
}
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Create a new sub key
|
||||
*/
|
||||
if (RegOpenKeyEx(top, key, 0, KEY_CREATE_SUB_KEY, &h) != ERROR_SUCCESS){
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
if (RegCreateKeyEx(h, name, 0, NULL, REG_OPTION_NON_VOLATILE,
|
||||
KEY_ALL_ACCESS, NULL, &subHandle, &disposition) != ERROR_SUCCESS) {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
RegCloseKey(subHandle);
|
||||
}
|
||||
RegCloseKey(h);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Determine the registry hive by the first portion of the path. Return
|
||||
* a pointer to the rest of key path after the hive portion.
|
||||
*/
|
||||
|
||||
static const char *getHive(const char *keyPath, HKEY *hive)
|
||||
{
|
||||
char key[MPR_MAX_STRING], *cp;
|
||||
int len;
|
||||
|
||||
mprAssert(keyPath && *keyPath);
|
||||
|
||||
*hive = 0;
|
||||
|
||||
mprStrcpy(key, sizeof(key), keyPath);
|
||||
key[sizeof(key) - 1] = '\0';
|
||||
|
||||
if (cp = strchr(key, '\\')) {
|
||||
*cp++ = '\0';
|
||||
}
|
||||
if (cp == 0 || *cp == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!mprStrcmpAnyCase(key, "HKEY_LOCAL_MACHINE")) {
|
||||
*hive = HKEY_LOCAL_MACHINE;
|
||||
} else if (!mprStrcmpAnyCase(key, "HKEY_CURRENT_USER")) {
|
||||
*hive = HKEY_CURRENT_USER;
|
||||
} else if (!mprStrcmpAnyCase(key, "HKEY_USERS")) {
|
||||
*hive = HKEY_USERS;
|
||||
} else if (!mprStrcmpAnyCase(key, "HKEY_CLASSES_ROOT")) {
|
||||
*hive = HKEY_CLASSES_ROOT;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (*hive == 0) {
|
||||
return 0;
|
||||
}
|
||||
len = strlen(key) + 1;
|
||||
return keyPath + len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* @file mprTime.c
|
||||
* @brief Time handling for Windows
|
||||
* @overview
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/************************************ Code ************************************/
|
||||
/*
|
||||
* Returns time in seconds and milliseconds. This is NOT time-of-day.
|
||||
*/
|
||||
|
||||
MprTime *mprGetTime(MprCtx ctx, MprTime *tp)
|
||||
{
|
||||
FILETIME fileTime;
|
||||
int64 now, base;
|
||||
|
||||
GetSystemTimeAsFileTime(&fileTime);
|
||||
|
||||
now = ((((int64) fileTime.dwHighDateTime) << BITS(uint)) +
|
||||
((int64) fileTime.dwLowDateTime));
|
||||
|
||||
/*
|
||||
* Convert from 100-nanosec units to milliseconds
|
||||
*/
|
||||
now = (now / 10000);
|
||||
|
||||
/*
|
||||
* Adjust to be seconds since Jan 1 1970. Do this to be consistent with
|
||||
* UNIX but not really required by the API definition.
|
||||
*/
|
||||
base = ((UINT64(365) * 86400 * (1970 - 1601)) * 1000);
|
||||
now -= base;
|
||||
tp->sec = (uint) (now / 1000);
|
||||
tp->msec = (uint) (now % 1000);
|
||||
|
||||
#if UNUSED
|
||||
{
|
||||
static int64 start;
|
||||
|
||||
if (start == 0) {
|
||||
start = now;
|
||||
}
|
||||
if (now < start) {
|
||||
mprLog(ctx, 0, "TIME WENT BACKWARDS");
|
||||
mprLog(ctx, 0, "start %Ld", start);
|
||||
mprLog(ctx, 0, "now %Ld", now);
|
||||
}
|
||||
mprLog(ctx, 0, "getTime %Ld", now);
|
||||
start = now;
|
||||
}
|
||||
#endif
|
||||
|
||||
return tp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of localtime
|
||||
*/
|
||||
|
||||
struct tm *mprLocaltime(MprCtx ctx, struct tm *timep, time_t *now)
|
||||
{
|
||||
struct tm *tbuf;
|
||||
mprGlobalLock(ctx);
|
||||
tbuf = localtime(now);
|
||||
*timep = *tbuf;
|
||||
mprGlobalUnlock(ctx);
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of gmtime
|
||||
*/
|
||||
|
||||
struct tm *mprGmtime(MprCtx ctx, time_t *now, struct tm *timep)
|
||||
{
|
||||
struct tm *tbuf;
|
||||
tbuf = gmtime(now);
|
||||
*timep = *tbuf;
|
||||
|
||||
return timep;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of ctime
|
||||
*/
|
||||
|
||||
int mprCtime(MprCtx ctx, char *buf, int bufsize, const time_t *timer)
|
||||
{
|
||||
char *cp;
|
||||
int len;
|
||||
|
||||
mprAssert(buf);
|
||||
|
||||
mprGlobalLock(ctx);
|
||||
|
||||
cp = ctime(timer);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprStrcpy(buf, bufsize, "WONT FIT");
|
||||
mprAssert(0);
|
||||
mprGlobalUnlock(ctx);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
|
||||
len = mprStrcpy(buf, bufsize, cp);
|
||||
if (buf[len - 1] == '\n') {
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
|
||||
mprGlobalUnlock(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of asctime
|
||||
*/
|
||||
|
||||
int mprAsctime(MprCtx ctx, char *buf, int bufsize, const struct tm *timeptr)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
mprAssert(buf);
|
||||
mprGlobalLock(ctx);
|
||||
cp = asctime(timeptr);
|
||||
if ((int) strlen(cp) >= bufsize) {
|
||||
mprAssert(0);
|
||||
mprGlobalUnlock(ctx);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
mprStrcpy(buf, bufsize, cp);
|
||||
mprGlobalUnlock(ctx);
|
||||
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,14 @@
|
||||
${BLD_OBJ_DIR}/mpr${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprAlloc${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprArray${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprBuf${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprFile${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprGenFile${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprGenTime${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprLock${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprLog${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprPlatform${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprPrintf${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprString${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprSymbol${BLD_OBJ}
|
||||
${BLD_OBJ_DIR}/mprTime${BLD_OBJ}
|
||||
@@ -0,0 +1,340 @@
|
||||
/**
|
||||
* @file mpr.c
|
||||
* @brief Mpr initialization
|
||||
* @overview
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Initialize the MPR. Create the top level memory context. This routine is
|
||||
* the first call an MPR application must do. If using MprServices, the
|
||||
* creation of an Mpr object will call this routine.
|
||||
*/
|
||||
|
||||
MprApp *mprInit(MprAllocCback cback)
|
||||
{
|
||||
return mprInitEx(cback, 0);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Add a shell parameter then do the regular init
|
||||
*/
|
||||
|
||||
MprApp *mprInitEx(MprAllocCback cback, void *shell)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = (MprApp*) mprAllocInit(cback);
|
||||
|
||||
mprAssert(app);
|
||||
if (app == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
app->name = mprStrdup(app, BLD_PRODUCT);
|
||||
app->title = mprStrdup(app, BLD_NAME);
|
||||
app->version = mprStrdup(app, BLD_VERSION);
|
||||
|
||||
mprSetShell(app, shell);
|
||||
|
||||
app->table = mprCreateSymbolTable(app, 0);
|
||||
|
||||
if (mprStartFileServices(app) < 0) {
|
||||
mprAllocTerm(app);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
mprInitThreads(app);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* See if any of the preceeding allocations failed
|
||||
*/
|
||||
if (mprGetAllocErrors(app) > 0) {
|
||||
mprAllocTerm(app);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Mark all blocks allocated so far as required. They will then be
|
||||
* omitted from leak reports.
|
||||
*/
|
||||
mprSetRequiredAlloc(app, 1);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Terminate the MPR. If doStats is true, then output a memory allocation
|
||||
* report.
|
||||
*/
|
||||
|
||||
void mprTerm(MprApp *app, bool doStats)
|
||||
{
|
||||
#if BLD_FEATURE_ALLOC_STATS
|
||||
if (doStats) {
|
||||
mprPrintAllocReport(app, 1, "MPR Memory Allocation Report");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
mprTermThreads(app);
|
||||
#endif
|
||||
|
||||
mprStopFileServices(app);
|
||||
|
||||
#if BLD_DEBUG
|
||||
mprValidateAllocTree(app);
|
||||
#endif
|
||||
mprAllocTerm(app);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
bool mprIsExiting(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
if (app == 0) {
|
||||
return 1;
|
||||
}
|
||||
return app->flags & MPR_APP_EXITING;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprHasAllocError(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
if (app == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return app->flags & MPR_APP_ALLOC_ERROR;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSignalExit(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
app->flags |= MPR_APP_EXITING;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSignalAllocError(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
app->flags |= MPR_APP_ALLOC_ERROR;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprSetAppName(MprCtx ctx, const char *name, const char *title,
|
||||
const char *version)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
if (name) {
|
||||
mprFree(app->name);
|
||||
if ((app->name = mprStrdup(ctx, name)) == 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
}
|
||||
|
||||
if (title) {
|
||||
mprFree(app->title);
|
||||
if ((app->title = mprStrdup(ctx, title)) == 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
}
|
||||
|
||||
if (version) {
|
||||
mprFree(app->version);
|
||||
if ((app->version = mprStrdup(ctx, version)) == 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
const char *mprGetAppName(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
return app->name;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
const char *mprGetAppTitle(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
return app->title;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
const char *mprGetAppVersion(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
return app->version;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprSetKeyValue(MprCtx ctx, const char *key, void *ptr)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
if (mprInsertSymbol(app->table, key, ptr) == 0) {
|
||||
return MPR_ERR_CANT_WRITE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprRemoveKeyValue(MprCtx ctx, const char *key)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
return mprRemoveSymbol(app->table, key);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetKeyValue(MprCtx ctx, const char *key)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
return mprLookupSymbol(app->table, key);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
bool mprGetDebugMode(MprCtx ctx)
|
||||
{
|
||||
return mprGetApp(ctx)->debugMode;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetDebugMode(MprCtx ctx, bool on)
|
||||
{
|
||||
mprGetApp(ctx)->debugMode = on;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetLogHandler(MprCtx ctx, MprLogHandler handler)
|
||||
{
|
||||
mprGetApp(ctx)->logHandler = handler;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
MprLogHandler mprGetLogHandler(MprCtx ctx)
|
||||
{
|
||||
return mprGetApp(ctx)->logHandler;
|
||||
}
|
||||
|
||||
#if UNUSED
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetMprInstance(MprCtx ctx, void *mprInstance)
|
||||
{
|
||||
mprGetApp(ctx)->mprInstance = mprInstance;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetMprInstance(MprCtx ctx)
|
||||
{
|
||||
return mprGetApp(ctx)->mprInstance;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
const char *mprCopyright()
|
||||
{
|
||||
return "Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.";
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,385 @@
|
||||
/**
|
||||
* @file mprArray.c
|
||||
* @brief Growable array structure
|
||||
* @overview Simple growable array structure.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Create a general growable array structure. Use mprFree to destroy.
|
||||
*/
|
||||
|
||||
MprArray *mprCreateItemArrayInternal(MPR_LOC_DEC(ctx, loc), int initialSize,
|
||||
int maxSize)
|
||||
{
|
||||
MprArray *array;
|
||||
int size;
|
||||
|
||||
mprAssert(initialSize <= maxSize);
|
||||
|
||||
array = (MprArray*) mprSlabAllocZeroedBlock(MPR_LOC_PASS(ctx, loc),
|
||||
sizeof(MprArray), 0);
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (initialSize == 0) {
|
||||
initialSize = MPR_ARRAY_INCR;
|
||||
}
|
||||
if (maxSize == 0) {
|
||||
maxSize = MAXINT;
|
||||
}
|
||||
size = initialSize * sizeof(void*);
|
||||
|
||||
array->items = (void**) mprSlabAllocBlock(MPR_LOC_PASS(array, loc),
|
||||
size, 0);
|
||||
|
||||
if (array->items == 0) {
|
||||
mprFree(array);
|
||||
return 0;
|
||||
}
|
||||
|
||||
array->capacity = initialSize;
|
||||
array->maxSize = maxSize;
|
||||
array->incr = min(initialSize * 2, (array->maxSize - array->length));
|
||||
array->length = 0;
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Add an item to the array
|
||||
*/
|
||||
|
||||
int mprAddItem(MprArray *array, void *item)
|
||||
{
|
||||
int memsize, index, len;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(array->capacity >= 0);
|
||||
mprAssert(array->length >= 0);
|
||||
|
||||
if (array->length < array->capacity) {
|
||||
/*
|
||||
* Room to fit in the current capacity
|
||||
*/
|
||||
index = array->length++;
|
||||
array->items[index] = item;
|
||||
return index;
|
||||
}
|
||||
mprAssert(array->length == array->capacity);
|
||||
|
||||
/*
|
||||
* Need to grow the array
|
||||
*/
|
||||
if (array->capacity >= array->maxSize) {
|
||||
mprAssert(array->capacity < array->maxSize);
|
||||
return MPR_ERR_TOO_MANY;
|
||||
}
|
||||
|
||||
len = array->capacity + array->incr;
|
||||
memsize = len * sizeof(void*);
|
||||
|
||||
/*
|
||||
* Grow the array of items
|
||||
*/
|
||||
|
||||
array->items = (void**) mprRealloc(array, array->items, memsize);
|
||||
|
||||
/*
|
||||
* Zero the new portion
|
||||
*/
|
||||
memset(&array->items[array->capacity], 0, sizeof(void*) * array->incr);
|
||||
array->capacity = len;
|
||||
|
||||
array->incr = min(array->incr * 2, (array->maxSize - array->length));
|
||||
|
||||
index = array->length++;
|
||||
array->items[index] = item;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Remove an item from the array
|
||||
*/
|
||||
|
||||
int mprRemoveItem(MprArray *array, void *item)
|
||||
{
|
||||
int index;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(array->capacity > 0);
|
||||
mprAssert(array->length > 0);
|
||||
|
||||
index = mprFindItem(array, item);
|
||||
if (index < 0) {
|
||||
return index;
|
||||
}
|
||||
|
||||
return mprRemoveItemByIndex(array, index);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Remove an index from the array
|
||||
*/
|
||||
|
||||
int mprRemoveItemByIndex(MprArray *array, int index)
|
||||
{
|
||||
void **items;
|
||||
int i;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(array->capacity > 0);
|
||||
mprAssert(index >= 0 && index < array->capacity);
|
||||
mprAssert(array->items[index] != 0);
|
||||
mprAssert(array->length > 0);
|
||||
|
||||
if (index < 0 || index >= array->length) {
|
||||
return MPR_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy down to compress
|
||||
*/
|
||||
items = array->items;
|
||||
for (i = index; i < (array->length - 1); i++) {
|
||||
items[i] = items[i + 1];
|
||||
}
|
||||
array->length--;
|
||||
|
||||
#if BLD_DEBUG
|
||||
if (array->length < array->capacity) {
|
||||
items[array->length] = 0;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprRemoveRangeOfItems(MprArray *array, int start, int end)
|
||||
{
|
||||
void **items;
|
||||
int i, count;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(array->capacity > 0);
|
||||
mprAssert(array->length > 0);
|
||||
mprAssert(start > end);
|
||||
|
||||
if (start < 0 || start >= array->length) {
|
||||
return MPR_ERR_NOT_FOUND;
|
||||
}
|
||||
if (end < 0 || end >= array->length) {
|
||||
return MPR_ERR_NOT_FOUND;
|
||||
}
|
||||
if (start > end) {
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy down to compress
|
||||
*/
|
||||
items = array->items;
|
||||
count = end - start;
|
||||
for (i = start; i < (array->length - count); i++) {
|
||||
items[i] = items[i + count];
|
||||
}
|
||||
array->length -= count;
|
||||
|
||||
#if BLD_DEBUG
|
||||
if (array->length < array->capacity) {
|
||||
for (i = array->length; i < array->capacity; i++) {
|
||||
items[i] = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetItem(MprArray *array, int index)
|
||||
{
|
||||
mprAssert(array);
|
||||
|
||||
if (index < 0 || index >= array->length) {
|
||||
return 0;
|
||||
}
|
||||
return array->items[index];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetFirstItem(MprArray *array, int *last)
|
||||
{
|
||||
mprAssert(array);
|
||||
mprAssert(last);
|
||||
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*last = 0;
|
||||
|
||||
if (array->length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return array->items[0];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetNextItem(MprArray *array, int *last)
|
||||
{
|
||||
int index;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(last);
|
||||
mprAssert(*last >= 0);
|
||||
|
||||
index = *last;
|
||||
|
||||
if (++index < array->length) {
|
||||
*last = index;
|
||||
return array->items[index];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprGetPrevItem(MprArray *array, int *last)
|
||||
{
|
||||
int index;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(last);
|
||||
mprAssert(*last >= 0);
|
||||
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
index = *last;
|
||||
|
||||
if (--index < array->length && index >= 0) {
|
||||
*last = index;
|
||||
return array->items[index];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetItemCount(MprArray *array)
|
||||
{
|
||||
mprAssert(array);
|
||||
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return array->length;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetItemCapacity(MprArray *array)
|
||||
{
|
||||
mprAssert(array);
|
||||
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return array->capacity;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprClearAndFreeItems(MprArray *array)
|
||||
{
|
||||
int i;
|
||||
|
||||
mprAssert(array);
|
||||
|
||||
for (i = 0; i < array->length; i++) {
|
||||
mprFree(array->items[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprClearItems(MprArray *array)
|
||||
{
|
||||
mprAssert(array);
|
||||
|
||||
array->length = 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprFindItem(MprArray *array, void *item)
|
||||
{
|
||||
int i;
|
||||
|
||||
mprAssert(array);
|
||||
|
||||
for (i = 0; i < array->length; i++) {
|
||||
if (array->items[i] == item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return MPR_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,535 @@
|
||||
/**
|
||||
* @file mprBuf.c
|
||||
* @brief Dynamic buffer module
|
||||
* @overview
|
||||
* @remarks
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/**************************** Forward Declarations ****************************/
|
||||
|
||||
static int grow(MprBuf *bp);
|
||||
|
||||
/*********************************** Code *************************************/
|
||||
/*
|
||||
* Create a new buffer. "maxsize" is the limit to which the buffer can
|
||||
* ever grow. -1 means no limit. The buffer can ever only fix maxsize-1 bytes.
|
||||
* "initialSize" is used to define the amount to increase the size of the
|
||||
* buffer each time if it becomes full. (Note: grow() will exponentially
|
||||
* increase this number for performance.)
|
||||
*/
|
||||
|
||||
MprBuf *mprCreateBuf(MprCtx ctx, int initialSize, int maxSize)
|
||||
{
|
||||
MprBuf *bp;
|
||||
|
||||
if (initialSize <= 0) {
|
||||
initialSize = MPR_DEFAULT_ALLOC;
|
||||
}
|
||||
bp = mprAllocTypeZeroed(ctx, MprBuf);
|
||||
bp->growBy = MPR_BUFSIZE;
|
||||
bp->maxsize = 0;
|
||||
mprSetBufSize(bp, initialSize, maxSize);
|
||||
return bp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Set the initial buffer parameters and create the first buffer
|
||||
*/
|
||||
|
||||
void mprSetBufSize(MprBuf *bp, int initialSize, int max)
|
||||
{
|
||||
mprAssert(initialSize > 0);
|
||||
|
||||
if (max > 0 && initialSize > max) {
|
||||
initialSize = max;
|
||||
}
|
||||
|
||||
if (bp->buf && bp->growBy > 0) {
|
||||
mprFree(bp->buf);
|
||||
}
|
||||
|
||||
bp->buf = (uchar*) mprAlloc(bp, initialSize);
|
||||
bp->growBy = initialSize;
|
||||
bp->maxsize = max;
|
||||
bp->buflen = initialSize;
|
||||
bp->endbuf = &bp->buf[bp->buflen];
|
||||
bp->start = bp->buf;
|
||||
bp->end = bp->buf;
|
||||
*bp->start = '\0';
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprStealBuf(MprCtx ctx, MprBuf *bp)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = (char*) bp->start;
|
||||
|
||||
mprStealAllocBlock(MPR_LOC_ARGS(ctx), bp->start);
|
||||
|
||||
bp->start = bp->end = bp->buf = bp->endbuf = 0;
|
||||
bp->buflen = 0;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprAddNullToBuf(MprBuf *bp)
|
||||
{
|
||||
*((char*) bp->end) = (char) '\0';
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprAdjustBufEnd(MprBuf *bp, int size)
|
||||
{
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
mprAssert(size < bp->buflen);
|
||||
|
||||
bp->end += size;
|
||||
if (bp->end >= bp->endbuf) {
|
||||
bp->end -= bp->buflen;
|
||||
}
|
||||
if (bp->end < bp->buf) {
|
||||
bp->end += bp->buflen;
|
||||
}
|
||||
|
||||
if (bp->end >= bp->endbuf) {
|
||||
mprAssert(bp->end < bp->endbuf);
|
||||
mprFlushBuf(bp);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Adjust the start pointer after a user copy
|
||||
*/
|
||||
|
||||
void mprAdjustBufStart(MprBuf *bp, int size)
|
||||
{
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
mprAssert(size < bp->buflen);
|
||||
|
||||
bp->start += size;
|
||||
while (bp->start >= bp->endbuf) {
|
||||
bp->start -= bp->buflen;
|
||||
}
|
||||
while (bp->start < bp->buf) {
|
||||
bp->start += bp->buflen;
|
||||
}
|
||||
|
||||
/*
|
||||
* Flush the buffer if the start pointer is corrupted via a bad size
|
||||
*/
|
||||
if (bp->start >= bp->endbuf) {
|
||||
mprAssert(bp->start < bp->endbuf);
|
||||
mprFlushBuf(bp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprFlushBuf(MprBuf *bp)
|
||||
{
|
||||
bp->start = bp->buf;
|
||||
bp->end = bp->buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetCharFromBuf(MprBuf *bp)
|
||||
{
|
||||
int c;
|
||||
|
||||
if (bp->start == bp->end) {
|
||||
return -1;
|
||||
}
|
||||
c = (uchar) *bp->start++;
|
||||
if (bp->start >= bp->endbuf) {
|
||||
bp->start = bp->buf;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBlockFromBuf(MprBuf *bp, uchar *buf, int size)
|
||||
{
|
||||
int thisLen, bytesRead;
|
||||
|
||||
mprAssert(buf);
|
||||
mprAssert(size > 0);
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
|
||||
/*
|
||||
* Get the max bytes in a straight copy
|
||||
*/
|
||||
bytesRead = 0;
|
||||
while (size > 0) {
|
||||
thisLen = mprGetBufLinearData(bp);
|
||||
thisLen = min(thisLen, size);
|
||||
if (thisLen <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(buf, bp->start, thisLen);
|
||||
buf += thisLen;
|
||||
bp->start += thisLen;
|
||||
size -= thisLen;
|
||||
bytesRead += thisLen;
|
||||
|
||||
if (bp->start >= bp->endbuf) {
|
||||
bp->start = bp->buf;
|
||||
}
|
||||
}
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBufLength(MprBuf *bp)
|
||||
{
|
||||
if (bp->start > bp->end) {
|
||||
return (bp->buflen + (bp->end - bp->start));
|
||||
} else {
|
||||
return (bp->end - bp->start);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBufLinearData(MprBuf *bp)
|
||||
{
|
||||
return min(mprGetBufLength(bp), (bp->endbuf - bp->start));
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBufLinearSpace(MprBuf *bp)
|
||||
{
|
||||
int len = mprGetBufLength(bp);
|
||||
int space = bp->buflen - len - 1;
|
||||
return min((bp->endbuf - bp->end), space);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBufSize(MprBuf *bp)
|
||||
{
|
||||
return bp->buflen;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetBufSpace(MprBuf *bp)
|
||||
{
|
||||
return bp->buflen - mprGetBufLength(bp) - 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprGetBufOrigin(MprBuf *bp)
|
||||
{
|
||||
return (char*) bp->buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprGetBufStart(MprBuf *bp)
|
||||
{
|
||||
return (char*) bp->start;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprGetBufEnd(MprBuf *bp)
|
||||
{
|
||||
return (char*) bp->end;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprInsertCharToBuf(MprBuf *bp, int c)
|
||||
{
|
||||
char *cp;
|
||||
int space;
|
||||
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
|
||||
space = bp->buflen - mprGetBufLength(bp) - 1;
|
||||
if (space < (int) sizeof(char)) {
|
||||
if (!grow(bp)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (bp->start <= bp->buf) {
|
||||
bp->start = bp->endbuf;
|
||||
}
|
||||
cp = (char*) bp->start;
|
||||
*--cp = (char) c;
|
||||
bp->start = (uchar *) cp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprLookAtNextCharInBuf(MprBuf *bp)
|
||||
{
|
||||
if (bp->start == bp->end) {
|
||||
return -1;
|
||||
}
|
||||
return *bp->start;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprLookAtLastCharInBuf(MprBuf *bp)
|
||||
{
|
||||
if (bp->start == bp->end) {
|
||||
return -1;
|
||||
}
|
||||
return (bp->end == bp->buf) ? bp->endbuf[-1] : bp->end[-1];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPutCharToBuf(MprBuf *bp, int c)
|
||||
{
|
||||
char *cp;
|
||||
int space;
|
||||
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
|
||||
space = bp->buflen - mprGetBufLength(bp) - 1;
|
||||
if (space < (int) sizeof(char)) {
|
||||
if (! grow(bp)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
cp = (char*) bp->end;
|
||||
*cp++ = (char) c;
|
||||
bp->end = (uchar *) cp;
|
||||
if (bp->end >= bp->endbuf) {
|
||||
bp->end = bp->buf;
|
||||
}
|
||||
*((char*) bp->end) = (char) '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPutBlockToBuf(MprBuf *bp, const char *str, int size)
|
||||
{
|
||||
int thisLen, bytes, space;
|
||||
|
||||
mprAssert(str);
|
||||
mprAssert(size >= 0);
|
||||
mprAssert(bp->buflen == (bp->endbuf - bp->buf));
|
||||
|
||||
/*
|
||||
* Add the max we can in one copy
|
||||
*/
|
||||
bytes = 0;
|
||||
while (size > 0) {
|
||||
space = mprGetBufLinearSpace(bp);
|
||||
thisLen = min(space, size);
|
||||
if (thisLen <= 0) {
|
||||
if (! grow(bp)) {
|
||||
break;
|
||||
}
|
||||
space = mprGetBufLinearSpace(bp);
|
||||
thisLen = min(space, size);
|
||||
}
|
||||
|
||||
memcpy(bp->end, str, thisLen);
|
||||
str += thisLen;
|
||||
bp->end += thisLen;
|
||||
size -= thisLen;
|
||||
bytes += thisLen;
|
||||
|
||||
if (bp->end >= bp->endbuf) {
|
||||
bp->end = bp->buf;
|
||||
}
|
||||
}
|
||||
*((char*) bp->end) = (char) '\0';
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPutStringToBuf(MprBuf *bp, const char *str)
|
||||
{
|
||||
return mprPutBlockToBuf(bp, str, strlen(str));
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPutFmtStringToBuf(MprBuf *bp, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
int rc, len, space;
|
||||
|
||||
va_start(ap, fmt);
|
||||
space = mprGetBufLinearSpace(bp);
|
||||
|
||||
/*
|
||||
* Add max that the buffer can grow
|
||||
*/
|
||||
space += (bp->maxsize - bp->buflen - 1);
|
||||
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(bp), &buf, space, fmt, ap);
|
||||
rc = mprPutBlockToBuf(bp, buf, len);
|
||||
|
||||
mprFree(buf);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Grow the buffer to fit new data. Return 1 if the buffer can grow.
|
||||
* Grow using the growBy size specified when creating the buffer.
|
||||
*/
|
||||
|
||||
static int grow(MprBuf *bp)
|
||||
{
|
||||
uchar *newbuf;
|
||||
|
||||
if (bp->maxsize > 0 && bp->buflen >= bp->maxsize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
newbuf = (uchar*) mprAlloc(bp, bp->buflen + bp->growBy);
|
||||
if (bp->buf) {
|
||||
memcpy(newbuf, bp->buf, bp->buflen);
|
||||
mprFree(bp->buf);
|
||||
}
|
||||
|
||||
bp->buflen += bp->growBy;
|
||||
bp->end = newbuf + (bp->end - bp->buf);
|
||||
bp->start = newbuf + (bp->start - bp->buf);
|
||||
bp->buf = newbuf;
|
||||
bp->endbuf = &bp->buf[bp->buflen];
|
||||
|
||||
/*
|
||||
* Increase growBy to reduce overhead
|
||||
*/
|
||||
bp->growBy *= 2;
|
||||
if (bp->maxsize > 0 && (bp->buflen + bp->growBy) > bp->maxsize) {
|
||||
bp->growBy = bp->maxsize - bp->buflen;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Add a number to the buffer (always null terminated).
|
||||
*/
|
||||
|
||||
int mprPutIntToBuf(MprBuf *bp, int i)
|
||||
{
|
||||
char numBuf[16];
|
||||
int rc;
|
||||
|
||||
mprItoa(numBuf, sizeof(numBuf), i);
|
||||
rc = mprPutStringToBuf(bp, numBuf);
|
||||
*((char*) bp->end) = (char) '\0';
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprCopyBufDown(MprBuf *bp)
|
||||
{
|
||||
if (mprGetBufLength(bp) == 0) {
|
||||
mprFlushBuf(bp);
|
||||
return;
|
||||
}
|
||||
memmove(bp->buf, bp->start, (bp->end - bp->start));
|
||||
bp->end -= (bp->start - bp->buf);
|
||||
bp->start = bp->buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
MprBufProc mprGetBufRefillProc(MprBuf *bp)
|
||||
{
|
||||
return bp->refillProc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetBufRefillProc(MprBuf *bp, MprBufProc fn, void *arg)
|
||||
{
|
||||
bp->refillProc = fn;
|
||||
bp->refillArg = arg;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprRefillBuf(MprBuf *bp)
|
||||
{
|
||||
return (bp->refillProc) ? (bp->refillProc)(bp, bp->refillArg) : 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprResetBufIfEmpty(MprBuf *bp)
|
||||
{
|
||||
if (mprGetBufLength(bp) == 0) {
|
||||
mprFlushBuf(bp);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,336 @@
|
||||
/**
|
||||
* @file mprGenFile.c
|
||||
* @brief Generic File services
|
||||
* @overview
|
||||
* @remarks
|
||||
* See OS/mprFile.c for the per O/S portions
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
#if !BREW
|
||||
|
||||
static int closeDestructor(void *data);
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
int mprStartFileServices(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
app->console = mprAllocTypeZeroed(ctx, MprFile);
|
||||
app->error = mprAllocTypeZeroed(ctx, MprFile);
|
||||
|
||||
/*
|
||||
* We assume that STDOUT is 1 and STDERR is 2
|
||||
*/
|
||||
app->console->fd = 1;
|
||||
app->error->fd = 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprStopFileServices(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
mprFree(app->console);
|
||||
app->console = 0;
|
||||
mprFree(app->error);
|
||||
app->error = 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
MprFile *mprOpen(MprCtx ctx, const char *path, int omode, int perms)
|
||||
{
|
||||
MprFile *file;
|
||||
|
||||
mprAssert(path && *path);
|
||||
|
||||
file = mprAllocTypeZeroed(ctx, MprFile);
|
||||
|
||||
file->fd = open(path, omode, perms);
|
||||
if (file->fd < 0) {
|
||||
mprFree(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
mprSetDestructor(file, closeDestructor);
|
||||
return file;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int closeDestructor(void *data)
|
||||
{
|
||||
MprFile *file = (MprFile*) data;
|
||||
|
||||
mprAssert(file);
|
||||
|
||||
mprClose(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprClose(MprFile *file)
|
||||
{
|
||||
mprAssert(file);
|
||||
|
||||
if (file < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
mprAssert(file->fd >= 0);
|
||||
close(file->fd);
|
||||
|
||||
mprSetDestructor(file, 0);
|
||||
mprFree(file);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprRead(MprFile *file, void *buf, uint size)
|
||||
{
|
||||
mprAssert(file);
|
||||
|
||||
if (file == 0) {
|
||||
return MPR_ERR_BAD_HANDLE;
|
||||
}
|
||||
|
||||
return read(file->fd, buf, size);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprWrite(MprFile *file, const void *buf, uint count)
|
||||
{
|
||||
mprAssert(file);
|
||||
|
||||
if (file == 0) {
|
||||
return MPR_ERR_BAD_HANDLE;
|
||||
}
|
||||
|
||||
return write(file->fd, buf, count);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprSeek(MprFile *file, int seekType, long distance)
|
||||
{
|
||||
mprAssert(file);
|
||||
|
||||
if (file == 0) {
|
||||
return MPR_ERR_BAD_HANDLE;
|
||||
}
|
||||
|
||||
return lseek(file->fd, seekType, distance);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprDelete(MprCtx ctx, const char *path)
|
||||
{
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprDeleteDir(MprCtx ctx, const char *path)
|
||||
{
|
||||
return rmdir(path);
|
||||
}
|
||||
|
||||
#endif /* !BREW */
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprGets(MprFile *file, char *buf, uint size)
|
||||
{
|
||||
MprBuf *bp;
|
||||
int count, len, c;
|
||||
|
||||
mprAssert(file);
|
||||
|
||||
if (file == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file->buf == 0) {
|
||||
file->buf = mprCreateBuf(file, MPR_DEFAULT_ALLOC, MPR_MAX_STRING);
|
||||
}
|
||||
bp = file->buf;
|
||||
|
||||
/*
|
||||
* Must leave room for null
|
||||
*/
|
||||
count = 0;
|
||||
while (--size > 0) {
|
||||
if (mprGetBufLength(bp) == 0) {
|
||||
mprFlushBuf(bp);
|
||||
len = mprRead(file, mprGetBufEnd(bp),
|
||||
mprGetBufLinearSpace(bp));
|
||||
if (len <= 0) {
|
||||
return 0;
|
||||
}
|
||||
mprAdjustBufEnd(bp, len);
|
||||
mprAddNullToBuf(bp);
|
||||
}
|
||||
if ((c = mprGetCharFromBuf(bp)) == '\n') {
|
||||
buf[count] = '\0';
|
||||
return buf;
|
||||
}
|
||||
buf[count++] = c;
|
||||
}
|
||||
buf[count] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPuts(MprFile *file, const char *writeBuf, uint count)
|
||||
{
|
||||
MprBuf *bp;
|
||||
char *buf;
|
||||
int total, bytes, len;
|
||||
|
||||
mprAssert(file);
|
||||
|
||||
/*
|
||||
* Buffer output and flush when full.
|
||||
*/
|
||||
if (file->buf == 0) {
|
||||
file->buf = mprCreateBuf(file, MPR_BUFSIZE, 0);
|
||||
if (file->buf == 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
}
|
||||
bp = file->buf;
|
||||
|
||||
if (mprGetBufLength(bp) > 0 && mprGetBufSpace(bp) < (int) count) {
|
||||
len = mprGetBufLength(bp);
|
||||
if (mprWrite(file, mprGetBufStart(bp), len) != len) {
|
||||
return MPR_ERR_CANT_WRITE;
|
||||
}
|
||||
mprFlushBuf(bp);
|
||||
}
|
||||
|
||||
total = 0;
|
||||
buf = (char*) writeBuf;
|
||||
|
||||
while (count > 0) {
|
||||
bytes = mprPutBlockToBuf(bp, buf, count);
|
||||
if (bytes <= 0) {
|
||||
return MPR_ERR_CANT_ALLOCATE;
|
||||
}
|
||||
count -= bytes;
|
||||
buf += bytes;
|
||||
total += bytes;
|
||||
mprAddNullToBuf(bp);
|
||||
|
||||
if (count > 0) {
|
||||
len = mprGetBufLength(bp);
|
||||
if (mprWrite(file, mprGetBufStart(bp), len) != len) {
|
||||
return MPR_ERR_CANT_WRITE;
|
||||
}
|
||||
mprFlushBuf(bp);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprMakeTempFileName(MprCtx ctx, char *buf, int bufsize, const char *tempDir)
|
||||
{
|
||||
MprFile *file;
|
||||
MprTime now;
|
||||
char *dir;
|
||||
int seed, i;
|
||||
|
||||
if (tempDir == 0) {
|
||||
#if WIN
|
||||
char *cp;
|
||||
dir = mprStrdup(ctx, getenv("TEMP"));
|
||||
for (cp = dir; *cp; cp++) {
|
||||
if (*cp == '\\') {
|
||||
*cp = '/';
|
||||
}
|
||||
}
|
||||
#else
|
||||
dir = mprStrdup(ctx, "/tmp");
|
||||
#endif
|
||||
} else {
|
||||
dir = mprStrdup(ctx, tempDir);
|
||||
}
|
||||
|
||||
mprGetTime(ctx, &now);
|
||||
seed = now.msec % 64000;
|
||||
file = 0;
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
mprSprintf(buf, bufsize, "%s/MPR_%d_%d.tmp", dir, getpid(), seed++);
|
||||
file = mprOpen(ctx, buf, O_CREAT | O_EXCL | O_BINARY, 0664);
|
||||
if (file) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (file == 0) {
|
||||
return MPR_ERR_CANT_CREATE;
|
||||
}
|
||||
|
||||
mprClose(file);
|
||||
mprFree(dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* @file mprGenTime.c
|
||||
* @brief Generic Time handling
|
||||
* @overview
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the number of milliseconds until the given timeout has expired.
|
||||
*/
|
||||
|
||||
int mprGetTimeRemaining(MprCtx ctx, MprTime mark, uint timeout)
|
||||
{
|
||||
MprTime now;
|
||||
uint diff;
|
||||
|
||||
mprGetTime(ctx, &now);
|
||||
diff = ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
|
||||
|
||||
if (diff < 0) {
|
||||
/*
|
||||
* Detect time going backwards
|
||||
*/
|
||||
mprAssert(diff >= 0);
|
||||
diff = 0;
|
||||
}
|
||||
return (int) (timeout - diff);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the number of milliseconds until the given timeout has expired.
|
||||
*/
|
||||
|
||||
int mprGetElapsedTime(MprCtx ctx, MprTime mark)
|
||||
{
|
||||
MprTime now;
|
||||
|
||||
mprGetTime(ctx, &now);
|
||||
return ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprAddElapsedToTime(MprTime *time, uint elapsed)
|
||||
{
|
||||
time->sec += elapsed / 1000;
|
||||
time->msec += elapsed % 1000;
|
||||
if (time->msec > 1000) {
|
||||
time->msec -= 1000;
|
||||
time->sec++;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprCompareTime(MprTime *t1, MprTime *t2)
|
||||
{
|
||||
if (t1->sec < t2->sec) {
|
||||
return -1;
|
||||
} else if (t1->sec == t2->sec) {
|
||||
if (t1->msec < t2->msec) {
|
||||
return -1;
|
||||
} else if (t1->msec == t2->msec) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
uint mprSubtractTime(MprTime *t1, MprTime *t2)
|
||||
{
|
||||
return ((t1->sec - t2->sec) * 1000) + (t1->msec - t2->msec);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if !BREW
|
||||
/*
|
||||
* Thread-safe RFC822 dates (Eg: "Fri, 07 Jan 2003 12:12:21 GMT")
|
||||
*/
|
||||
|
||||
int mprRfcTime(MprCtx ctx, char *buf, int bufsize, const struct tm *timep)
|
||||
{
|
||||
char months[12][4] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
|
||||
"Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
char days[7][4] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
|
||||
char *dayp, *monthp;
|
||||
int year;
|
||||
|
||||
if (bufsize < 30) {
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
dayp = &days[timep->tm_wday][0];
|
||||
*buf++ = *dayp++;
|
||||
*buf++ = *dayp++;
|
||||
*buf++ = *dayp++;
|
||||
*buf++ = ',';
|
||||
*buf++ = ' ';
|
||||
|
||||
*buf++ = timep->tm_mday / 10 + '0';
|
||||
*buf++ = timep->tm_mday % 10 + '0';
|
||||
*buf++ = ' ';
|
||||
|
||||
monthp = &months[timep->tm_mon][0];
|
||||
*buf++ = *monthp++;
|
||||
*buf++ = *monthp++;
|
||||
*buf++ = *monthp++;
|
||||
*buf++ = ' ';
|
||||
|
||||
year = 1900 + timep->tm_year;
|
||||
/* This routine isn't y10k ready. */
|
||||
*buf++ = year / 1000 + '0';
|
||||
*buf++ = year % 1000 / 100 + '0';
|
||||
*buf++ = year % 100 / 10 + '0';
|
||||
*buf++ = year % 10 + '0';
|
||||
*buf++ = ' ';
|
||||
|
||||
*buf++ = timep->tm_hour / 10 + '0';
|
||||
*buf++ = timep->tm_hour % 10 + '0';
|
||||
*buf++ = ':';
|
||||
*buf++ = timep->tm_min / 10 + '0';
|
||||
*buf++ = timep->tm_min % 10 + '0';
|
||||
*buf++ = ':';
|
||||
*buf++ = timep->tm_sec / 10 + '0';
|
||||
*buf++ = timep->tm_sec % 10 + '0';
|
||||
*buf++ = ' ';
|
||||
|
||||
*buf++ = 'G';
|
||||
*buf++ = 'M';
|
||||
*buf++ = 'T';
|
||||
*buf++ = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,266 @@
|
||||
/**
|
||||
* @file mprThread.c
|
||||
* @brief Mbedthis Portable Runtime Base Thread Locking Support
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
/************************************ Code ************************************/
|
||||
|
||||
void mprInitThreads(MprApp *app)
|
||||
{
|
||||
mprAssert(app);
|
||||
|
||||
if (app->globalLock == 0) {
|
||||
app->globalLock = mprCreateLock(app);
|
||||
app->allocLock = mprCreateLock(app);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprTermThreads(MprApp *app)
|
||||
{
|
||||
mprAssert(app);
|
||||
|
||||
if (app->globalLock) {
|
||||
mprDestroyLock(app->globalLock);
|
||||
app->globalLock = 0;
|
||||
}
|
||||
if (app->allocLock) {
|
||||
MprLock *lock = app->allocLock;
|
||||
app->allocLock = 0;
|
||||
mprDestroyLock(lock);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
MprLock *mprCreateLock(MprCtx ctx)
|
||||
{
|
||||
MprLock *lock;
|
||||
|
||||
mprAssert(ctx);
|
||||
|
||||
lock = mprAllocType(ctx, MprLock);
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
pthread_mutexattr_t attr;
|
||||
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
||||
pthread_mutex_init(&lock->cs, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
#elif WIN
|
||||
InitializeCriticalSectionAndSpinCount(&lock->cs, 5000);
|
||||
#elif VXWORKS
|
||||
lock->cs = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE |
|
||||
SEM_INVERSION_SAFE);
|
||||
if (lock->cs == 0) {
|
||||
mprAssert(0);
|
||||
mprFree(lock);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return lock;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Destroy a lock. Must be locked on entrance.
|
||||
*/
|
||||
|
||||
void mprDestroyLock(MprLock *lock)
|
||||
{
|
||||
mprAssert(lock);
|
||||
if (lock == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
pthread_mutex_unlock(&lock->cs);
|
||||
pthread_mutex_destroy(&lock->cs);
|
||||
#elif WIN
|
||||
DeleteCriticalSection(&lock->cs);
|
||||
#elif VXWORKS
|
||||
semDelete(lock->cs);
|
||||
#endif
|
||||
mprFree(lock);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Lock a mutex
|
||||
*/
|
||||
|
||||
void mprLock(MprLock *lock)
|
||||
{
|
||||
/*
|
||||
* OPT -- Do this just so we can allocate MprApp before we have created its
|
||||
* lock. Should remove this test here and in mprUnlock.
|
||||
*/
|
||||
if (lock == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
pthread_mutex_lock(&lock->cs);
|
||||
#elif WIN
|
||||
EnterCriticalSection(&lock->cs);
|
||||
#elif VXWORKS
|
||||
semTake(lock->cs, WAIT_FOREVER);
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Try to attain a lock. Do not block!
|
||||
*/
|
||||
|
||||
int mprTryLock(MprLock *lock)
|
||||
{
|
||||
mprAssert(lock);
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
{
|
||||
int err;
|
||||
|
||||
if ((err = pthread_mutex_trylock(&lock->cs)) != 0) {
|
||||
if (err == EBUSY) {
|
||||
return MPR_ERR_BUSY;
|
||||
} else {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#elif WIN
|
||||
if (TryEnterCriticalSection(&lock->cs) == 0) {
|
||||
return MPR_ERR_BUSY;
|
||||
}
|
||||
#elif VXWORKS
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = semTake(cs, NO_WAIT);
|
||||
if (rc == -1) {
|
||||
mprAssert(0);
|
||||
}
|
||||
if (rc == S_objLib_OBJ_UNAVAILABLE) {
|
||||
return MPR_ERR_BUSY;
|
||||
} else {
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
/* Success */
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Unlock.
|
||||
*/
|
||||
|
||||
void mprUnlock(MprLock *lock)
|
||||
{
|
||||
if (lock == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
pthread_mutex_unlock(&lock->cs);
|
||||
#elif WIN
|
||||
LeaveCriticalSection(&lock->cs);
|
||||
#elif VXWORKS
|
||||
semGive(lock->cs);
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Big global lock. Avoid using this.
|
||||
*/
|
||||
|
||||
void mprGlobalLock(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
mprAssert(app);
|
||||
|
||||
if (app && app->globalLock) {
|
||||
mprLock(app->globalLock);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprGlobalUnlock(MprCtx ctx)
|
||||
{
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
mprAssert(app);
|
||||
|
||||
if (app && app->globalLock) {
|
||||
mprUnlock(app->globalLock);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetCurrentThreadID()
|
||||
{
|
||||
#if BLD_HOST_UNIX
|
||||
return (int) pthread_self();
|
||||
#elif WIN
|
||||
return GetCurrentThreadId();
|
||||
#elif VXWORKS
|
||||
return (int) pthread_self();
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_MULTITHREAD */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,602 @@
|
||||
/**
|
||||
* @file mprLog.c
|
||||
* @brief Mbedthis Portable Runtime (MPR) Logging and error reporting.
|
||||
* @remarks We always provide these routines.
|
||||
*/
|
||||
|
||||
/*********************************** License **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
|
||||
static void defaultLogHandler(MPR_LOC_DEC(ctx, loc), int flags,
|
||||
int level, const char *msg);
|
||||
static void logOutput(MPR_LOC_DEC(ctx, loc), int flags, int level,
|
||||
const char *msg);
|
||||
|
||||
/************************************ Code ************************************/
|
||||
|
||||
void mprLog(MprCtx ctx, int level, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
|
||||
if (level > mprGetLogLevel(ctx)) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_start(args, fmt);
|
||||
mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_ARGS(ctx), MPR_LOG_SRC, level, buf);
|
||||
|
||||
va_end(args);
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Do raw output
|
||||
*/
|
||||
|
||||
void mprRawLog(MprCtx ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
va_start(args, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_ARGS(ctx), MPR_RAW, 0, buf);
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Handle an error
|
||||
*/
|
||||
|
||||
void mprError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
va_start(args, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_PASS(ctx, loc), MPR_ERROR_MSG | MPR_ERROR_SRC, 0, buf);
|
||||
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Handle an error that should be displayed to the user
|
||||
*/
|
||||
|
||||
void mprUserError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
va_start(args, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_PASS(ctx, loc), MPR_USER_MSG | MPR_ERROR_SRC, 0, buf);
|
||||
|
||||
mprFree(buf);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Handle a fatal error. Forcibly shutdown the application.
|
||||
*/
|
||||
|
||||
void mprFatalError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
va_start(args, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_PASS(ctx, loc), MPR_USER_MSG | MPR_FATAL_SRC, 0, buf);
|
||||
|
||||
mprFree(buf);
|
||||
|
||||
#if BREW
|
||||
mprSignalExit(ctx);
|
||||
#else
|
||||
exit(2);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Handle a program assertion
|
||||
*/
|
||||
|
||||
void mprAssertError(MPR_LOC_DEC(ctx, loc), const char *msg)
|
||||
{
|
||||
logOutput(MPR_LOC_PASS(ctx, loc), MPR_ASSERT_MSG | MPR_ASSERT_SRC, 0, msg);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Handle an error
|
||||
*/
|
||||
|
||||
void mprStaticError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int len;
|
||||
char buf[MPR_MAX_STRING];
|
||||
|
||||
va_start(args, fmt);
|
||||
len = mprVsprintf(buf, sizeof(buf), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
logOutput(MPR_LOC_PASS(ctx, loc), MPR_ERROR_MSG | MPR_ERROR_SRC, 0, buf);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Direct output to the standard output. Does not hook into the logging
|
||||
* system and does not allocate memory.
|
||||
*/
|
||||
|
||||
void mprStaticAssert(const char *loc, const char *msg)
|
||||
{
|
||||
#if BLD_DEBUG
|
||||
char buf[MPR_MAX_STRING];
|
||||
int len;
|
||||
|
||||
len = mprSprintf(buf, sizeof(buf), "Assertion %s, failed at %s\n",
|
||||
msg, loc);
|
||||
mprBreakpoint(loc, buf);
|
||||
|
||||
#if BLD_HOST_UNIX
|
||||
/*
|
||||
* MOB -- but is stdout always okay to use
|
||||
*/
|
||||
write(1, buf, len);
|
||||
#elif BREW || WIN
|
||||
/*
|
||||
* Only time we use printf. We can't get an alloc context so we have
|
||||
* to use real print
|
||||
*/
|
||||
#if BREW && !BREW_SIMULATOR
|
||||
printf(" MP: %s\n", buf);
|
||||
#else
|
||||
printf("%s\n", buf);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprGetLogLevel(MprCtx ctx)
|
||||
{
|
||||
return mprGetApp(ctx)->logLevel;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void mprSetLogLevel(MprCtx ctx, int level)
|
||||
{
|
||||
mprGetApp(ctx)->logLevel = level;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Output a log message to the log handler
|
||||
*/
|
||||
|
||||
static void logOutput(MPR_LOC_DEC(ctx, loc), int flags, int level,
|
||||
const char *msg)
|
||||
{
|
||||
MprLogHandler handler;
|
||||
|
||||
if (flags & (MPR_ERROR_SRC | MPR_FATAL_SRC | MPR_ASSERT_SRC)) {
|
||||
mprBreakpoint(MPR_LOC, 0);
|
||||
}
|
||||
|
||||
mprAssert(ctx != 0);
|
||||
handler = mprGetApp(ctx)->logHandler;
|
||||
if (handler != 0) {
|
||||
(handler)(MPR_LOC_PASS(ctx, loc), flags, level, msg);
|
||||
return;
|
||||
}
|
||||
defaultLogHandler(MPR_LOC_PASS(ctx, loc), flags, level, msg);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Default log output is just to the console
|
||||
*/
|
||||
|
||||
static void defaultLogHandler(MPR_LOC_DEC(ctx, loc), int flags,
|
||||
int level, const char *msg)
|
||||
{
|
||||
MprApp *app;
|
||||
char *prefix;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
prefix = app->name;
|
||||
|
||||
while (*msg == '\n') {
|
||||
mprPrintf(ctx, "\n");
|
||||
msg++;
|
||||
}
|
||||
|
||||
if (flags & MPR_LOG_SRC) {
|
||||
#if BREW && !BREW_SIMULATOR
|
||||
mprPrintf(ctx, "%s\n", msg);
|
||||
#else
|
||||
mprPrintf(ctx, "%s: %d: %s\n", prefix, level, msg);
|
||||
#endif
|
||||
|
||||
} else if (flags & MPR_ERROR_SRC) {
|
||||
/*
|
||||
* Use static printing to avoid malloc when the messages are small.
|
||||
* This is important for memory allocation errors.
|
||||
*/
|
||||
if (strlen(msg) < (MPR_MAX_STRING - 32)) {
|
||||
mprStaticPrintf(ctx, "%s: Error: %s\n", prefix, msg);
|
||||
} else {
|
||||
mprPrintf(ctx, "%s: Error: %s\n", prefix, msg);
|
||||
}
|
||||
|
||||
} else if (flags & MPR_FATAL_SRC) {
|
||||
mprPrintf(ctx, "%s: Fatal: %s\n", prefix, msg);
|
||||
|
||||
} else if (flags & MPR_ASSERT_SRC) {
|
||||
#if BLD_FEATURE_ALLOC_LEAK_TRACK
|
||||
mprPrintf(ctx, "%s: Assertion %s, failed at %s\n", prefix, msg, loc);
|
||||
#else
|
||||
mprPrintf(ctx, "%s: Assertion %s, failed\n", prefix, msg);
|
||||
#endif
|
||||
|
||||
} else if (flags & MPR_RAW) {
|
||||
mprPrintf(ctx, "%s", msg);
|
||||
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Map the O/S error code to portable error codes.
|
||||
*/
|
||||
|
||||
int mprGetOsError()
|
||||
{
|
||||
#if WIN
|
||||
int rc;
|
||||
rc = GetLastError();
|
||||
|
||||
/*
|
||||
* Client has closed the pipe
|
||||
*/
|
||||
if (rc == ERROR_NO_DATA) {
|
||||
return EPIPE;
|
||||
}
|
||||
return rc;
|
||||
#endif
|
||||
#if LINUX || VXWORKS || SOLARIS
|
||||
return errno;
|
||||
#endif
|
||||
#if BREW
|
||||
/*
|
||||
* No such thing on Brew. Errors are per class
|
||||
*/
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if UNUSED
|
||||
|
||||
const char *mprGetErrorMsg(int err)
|
||||
{
|
||||
/*
|
||||
* MPR error messages. Declare here so we don't have any globals.
|
||||
*/
|
||||
char *mprErrMessages[] = {
|
||||
/* 0 MPR_ERR_OK */ "Success",
|
||||
/* -201 MPR_ERR_GENERAL */ "General error",
|
||||
/* -202 MPR_ERR_ABORTED */ "Aborted",
|
||||
/* -203 MPR_ERR_ALREADY_EXISTS */ "Already exists",
|
||||
/* -204 MPR_ERR_BAD_ARGS */ "Bad args",
|
||||
/* -205 MPR_ERR_BAD_FORMAT */ "Bad format",
|
||||
/* -206 MPR_ERR_BAD_HANDLE */ "Bad handle",
|
||||
/* -207 MPR_ERR_BAD_STATE */ "Bad state",
|
||||
/* -208 MPR_ERR_BAD_SYNTAX */ "Bad syntax",
|
||||
/* -209 MPR_ERR_BAD_TYPE */ "Bad type",
|
||||
/* -210 MPR_ERR_BAD_VALUE */ "Bad value",
|
||||
/* -211 MPR_ERR_BUSY */ "Busy",
|
||||
/* -212 MPR_ERR_CANT_ACCESS */ "Can't access",
|
||||
/* -213 MPR_ERR_CANT_COMPLETE */ "Can't complete",
|
||||
/* -214 MPR_ERR_CANT_CREATE */ "Can't create",
|
||||
/* -215 MPR_ERR_CANT_INITIALIZE */ "Can't initialize",
|
||||
/* -216 MPR_ERR_CANT_OPEN */ "Can't open",
|
||||
/* -217 MPR_ERR_CANT_READ */ "Can't read",
|
||||
/* -218 MPR_ERR_CANT_WRITE */ "Can't write",
|
||||
/* -219 MPR_ERR_DELETED */ "Already deleted",
|
||||
/* -220 MPR_ERR_NETWORK */ "Network error",
|
||||
/* -221 MPR_ERR_NOT_FOUND */ "Not found",
|
||||
/* -222 MPR_ERR_NOT_INITIALIZED */ "Not initialized",
|
||||
/* -223 MPR_ERR_NOT_READY */ "Not ready",
|
||||
/* -224 MPR_ERR_READ_ONLY */ "Read only",
|
||||
/* -225 MPR_ERR_TIMEOUT */ "Timeout",
|
||||
/* -226 MPR_ERR_TOO_MANY */ "Too many",
|
||||
/* -227 MPR_ERR_WONT_FIT */ "Won't fit",
|
||||
/* -228 MPR_ERR_WOULD_BLOCK */ "Would block",
|
||||
/* -229 MPR_ERR_CANT_ALLOCATE */ "Can't allocate",
|
||||
};
|
||||
int mprNumErr = sizeof(mprErrMessages) / sizeof(char*);
|
||||
|
||||
/*
|
||||
* Operating system error messages
|
||||
*/
|
||||
#if WIN
|
||||
char *osErrMessages[] =
|
||||
{
|
||||
/* 0 */ "No error",
|
||||
/* 1 EPERM */ "Operation not permitted",
|
||||
/* 2 ENOENT */ "No such file or directory",
|
||||
/* 3 ESRCH */ "No such process",
|
||||
/* 4 EINTR */ "Interrupted function call",
|
||||
/* 5 EIO */ "I/O error",
|
||||
/* 6 ENXIO */ "No such device or address",
|
||||
/* 7 E2BIG */ "Arg list too long",
|
||||
/* 8 ENOEXEC */ "Exec format error",
|
||||
/* 9 EBADF */ "Bad file number",
|
||||
/* 10 ECHILD */ "No child processes",
|
||||
/* 11 EAGAIN */ "Try again",
|
||||
/* 12 ENOMEM */ "Out of memory",
|
||||
/* 13 EACCES */ "Permission denied",
|
||||
/* 14 EFAULT */ "Bad address",
|
||||
/* 15 ENOTBLK */ "Unknown error",
|
||||
/* 16 EBUSY */ "Resource busy",
|
||||
/* 17 EEXIST */ "File exists",
|
||||
/* 18 EXDEV */ "Improper link",
|
||||
/* 19 ENODEV */ "No such device",
|
||||
/* 20 ENOTDIR */ "Not a directory",
|
||||
/* 21 EISDIR */ "Is a directory",
|
||||
/* 22 EINVAL */ "Invalid argument",
|
||||
/* 23 ENFILE */ "Too many open files in system",
|
||||
/* 24 EMFILE */ "Too many open files",
|
||||
/* 25 ENOTTY */ "Inappropriate I/O control operation",
|
||||
/* 26 ETXTBSY */ "Unknown error",
|
||||
/* 27 EFBIG */ "File too large",
|
||||
/* 28 ENOSPC */ "No space left on device",
|
||||
/* 29 ESPIPE */ "Invalid seek",
|
||||
/* 30 EROFS */ "Read-only file system",
|
||||
/* 31 EMLINK */ "Too many links",
|
||||
/* 32 EPIPE */ "Broken pipe",
|
||||
/* 33 EDOM */ "Domain error",
|
||||
/* 34 ERANGE */ "Result too large",
|
||||
/* 35 EUCLEAN */ "Unknown error",
|
||||
/* 36 EDEADLK */ "Resource deadlock would occur",
|
||||
/* 37 UNKNOWN */ "Unknown error",
|
||||
/* 38 ENAMETOOLONG */ "Filename too long",
|
||||
/* 39 ENOLCK */ "No locks available",
|
||||
/* 40 ENOSYS */ "Function not implemented",
|
||||
/* 41 ENOTEMPTY */ "Directory not empty",
|
||||
/* 42 EILSEQ */ "Illegal byte sequence",
|
||||
/* 43 ENETDOWN */ "Network is down",
|
||||
/* 44 ECONNRESET */ "Connection reset",
|
||||
/* 45 ECONNREFUSED */ "Connection refused",
|
||||
/* 46 EADDRINUSE */ "Address already in use"
|
||||
|
||||
};
|
||||
|
||||
#else /* WIN */
|
||||
|
||||
char *osErrMessages[] =
|
||||
{
|
||||
/* 0 */ "Success"
|
||||
/* 1 EPERM */ "Operation not permitted"
|
||||
/* 2 ENOENT */ "No such file or directory"
|
||||
/* 3 ESRCH */ "No such process"
|
||||
/* 4 EINTR */ "Interrupted system call"
|
||||
/* 5 EIO */ "I/O error"
|
||||
/* 6 ENXIO */ "No such device or address"
|
||||
/* 7 E2BIG */ "Arg list too long"
|
||||
/* 8 ENOEXEC */ "Exec format error"
|
||||
/* 9 EBADF */ "Bad file number"
|
||||
/* 10 ECHILD */ "No child processes"
|
||||
/* 11 EAGAIN */ "Try again"
|
||||
/* 12 ENOMEM */ "Out of memory"
|
||||
/* 13 EACCES */ "Permission denied"
|
||||
/* 14 EFAULT */ "Bad address"
|
||||
/* 15 ENOTBLK */ "Block device required"
|
||||
/* 16 EBUSY */ "Device or resource busy"
|
||||
/* 17 EEXIST */ "File exists"
|
||||
/* 18 EXDEV */ "Cross-device link"
|
||||
/* 19 ENODEV */ "No such device"
|
||||
/* 20 ENOTDIR */ "Not a directory"
|
||||
/* 21 EISDIR */ "Is a directory"
|
||||
/* 22 EINVAL */ "Invalid argument"
|
||||
/* 23 ENFILE */ "File table overflow"
|
||||
/* 24 EMFILE */ "Too many open files"
|
||||
/* 25 ENOTTY */ "Not a typewriter"
|
||||
/* 26 ETXTBSY */ "Text file busy"
|
||||
/* 27 EFBIG */ "File too large"
|
||||
/* 28 ENOSPC */ "No space left on device"
|
||||
/* 29 ESPIPE */ "Illegal seek"
|
||||
/* 30 EROFS */ "Read-only file system"
|
||||
/* 31 EMLINK */ "Too many links"
|
||||
/* 32 EPIPE */ "Broken pipe"
|
||||
/* 33 EDOM */ "Math argument out of domain of func"
|
||||
/* 34 ERANGE */ "Math result not representable"
|
||||
/* 35 EDEADLK */ "Resource deadlock would occur"
|
||||
/* 36 ENAMETOOLONG */ "File name too long"
|
||||
/* 37 ENOLCK */ "No record locks available"
|
||||
/* 38 ENOSYS */ "Function not implemented"
|
||||
/* 39 ENOTEMPTY */ "Directory not empty"
|
||||
/* 40 ELOOP */ "Too many symbolic links encountered"
|
||||
/* 41 EWOULDBLOCK EAGAIN */"Operation would block"
|
||||
/* 42 ENOMSG */ "No message of desired type"
|
||||
/* 43 EIDRM */ "Identifier removed"
|
||||
|
||||
#if !BLD_FEATURE_SQUEEZE
|
||||
/* 44 ECHRNG */ "Channel number out of range"
|
||||
/* 45 EL2NSYNC */ "Level 2 not synchronized"
|
||||
/* 46 EL3HLT */ "Level 3 halted"
|
||||
/* 47 EL3RST */ "Level 3 reset"
|
||||
/* 48 ELNRNG */ "Link number out of range"
|
||||
/* 49 EUNATCH */ "Protocol driver not attached"
|
||||
/* 50 ENOCSI */ "No CSI structure available"
|
||||
/* 51 EL2HLT */ "Level 2 halted"
|
||||
/* 52 EBADE */ "Invalid exchange"
|
||||
/* 53 EBADR */ "Invalid request descriptor"
|
||||
/* 54 EXFULL */ "Exchange full"
|
||||
/* 55 ENOANO */ "No anode"
|
||||
/* 56 EBADRQC */ "Invalid request code"
|
||||
/* 57 EBADSLT */ "Invalid slot"
|
||||
/* 59 EBFONT */ "Bad font file format"
|
||||
/* 60 ENOSTR */ "Device not a stream"
|
||||
/* 61 ENODATA */ "No data available"
|
||||
/* 62 ETIME */ "Timer expired"
|
||||
/* 63 ENOSR */ "Out of streams resources"
|
||||
/* 64 ENONET */ "Machine is not on the network"
|
||||
/* 65 ENOPKG */ "Package not installed"
|
||||
/* 66 EREMOTE */ "Object is remote"
|
||||
/* 67 ENOLINK */ "Link has been severed"
|
||||
/* 68 EADV */ "Advertise error"
|
||||
/* 69 ESRMNT */ "Srmount error"
|
||||
/* 70 ECOMM */ "Communication error on send"
|
||||
/* 71 EPROTO */ "Protocol error"
|
||||
/* 72 EMULTIHOP */ "Multihop attempted"
|
||||
/* 73 EDOTDOT */ "RFS specific error"
|
||||
/* 74 EBADMSG */ "Not a data message"
|
||||
/* 75 EOVERFLOW */ "Value too large for defined data type"
|
||||
/* 76 ENOTUNIQ */ "Name not unique on network"
|
||||
/* 77 EBADFD */ "File descriptor in bad state"
|
||||
/* 78 EREMCHG */ "Remote address changed"
|
||||
/* 79 ELIBACC */ "Can not access a needed shared library"
|
||||
/* 80 ELIBBAD */ "Accessing a corrupted shared library"
|
||||
/* 81 ELIBSCN */ ".lib section in a.out corrupted"
|
||||
/* 82 ELIBMAX */ "Linking in too many shared libraries"
|
||||
/* 83 ELIBEXEC */ "Cannot exec a shared library directly"
|
||||
/* 84 EILSEQ */ "Illegal byte sequence"
|
||||
/* 85 ERESTART */ "Interrupted system call should be restarted"
|
||||
/* 86 ESTRPIPE */ "Streams pipe error"
|
||||
/* 87 EUSERS */ "Too many users"
|
||||
/* 88 ENOTSOCK */ "Socket operation on non-socket"
|
||||
/* 89 EDESTADDRREQ */ "Destination address required"
|
||||
/* 90 EMSGSIZE */ "Message too long"
|
||||
/* 91 EPROTOTYPE */ "Protocol wrong type for socket"
|
||||
/* 92 ENOPROTOOPT */ "Protocol not available"
|
||||
/* 93 EPROTONOSUPPORT */ "Protocol not supported"
|
||||
/* 94 ESOCKTNOSUPPORT */ "Socket type not supported"
|
||||
/* 95 EOPNOTSUPP */ "Operation not supported on transport endpoint"
|
||||
/* 96 EPFNOSUPPORT */ "Protocol family not supported"
|
||||
/* 97 EAFNOSUPPORT */ "Address family not supported by protocol"
|
||||
/* 98 EADDRINUSE */ "Address already in use"
|
||||
/* 99 EADDRNOTAVAIL */ "Cannot assign requested address"
|
||||
/* 100 ENETDOWN */ "Network is down"
|
||||
/* 101 ENETUNREACH */ "Network is unreachable"
|
||||
/* 102 ENETRESET */ "Network dropped connection because of reset"
|
||||
/* 103 ECONNABORTED */ "Software caused connection abort"
|
||||
/* 104 ECONNRESET */ "Connection reset by peer"
|
||||
/* 105 ENOBUFS */ "No buffer space available"
|
||||
/* 106 EISCONN */ "Transport endpoint is already connected"
|
||||
/* 107 ENOTCONN */ "Transport endpoint is not connected"
|
||||
/* 108 ESHUTDOWN */ "Cannot send after transport endpoint shutdown"
|
||||
/* 109 ETOOMANYREFS */ "Too many references: cannot splice"
|
||||
/* 110 ETIMEDOUT */ "Connection timed out"
|
||||
/* 111 ECONNREFUSED */ "Connection refused"
|
||||
/* 112 EHOSTDOWN */ "Host is down"
|
||||
/* 113 EHOSTUNREACH */ "No route to host"
|
||||
/* 114 EALREADY */ "Operation already in progress"
|
||||
/* 115 EINPROGRESS */ "Operation now in progress"
|
||||
/* 116 ESTALE */ "Stale NFS file handle"
|
||||
/* 117 EUCLEAN */ "Structure needs cleaning"
|
||||
/* 118 ENOTNAM */ "Not a XENIX named type file"
|
||||
/* 119 ENAVAIL */ "No XENIX semaphores available"
|
||||
/* 120 EISNAM */ "Is a named type file"
|
||||
/* 121 EREMOTEIO */ "Remote I/O error"
|
||||
/* 122 EDQUOT */ "Quota exceeded"
|
||||
/* 123 ENOMEDIUM */ "No medium found"
|
||||
/* 124 EMEDIUMTYPE */ "Wrong medium type"
|
||||
};
|
||||
#endif /* BLD_FEATURE_SQUEEZE */
|
||||
#endif /* WIN */
|
||||
|
||||
int osNumErr = sizeof(osErrMessages) / sizeof(char*);
|
||||
|
||||
if (err < MPR_ERR_BASE) {
|
||||
err = MPR_ERR_BASE - err;
|
||||
if (err < 0 || err >= mprNumErr) {
|
||||
return "Bad error code";
|
||||
}
|
||||
return mprErrMessages[err];
|
||||
|
||||
} else {
|
||||
/*
|
||||
* Negative O/S error code. Map to a positive standard Posix error.
|
||||
*/
|
||||
err = -err;
|
||||
if (err < 0 || err >= osNumErr) {
|
||||
return "Bad O/S error code";
|
||||
}
|
||||
return osErrMessages[err];
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/*****************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,707 @@
|
||||
/*
|
||||
* @file mprOs.h
|
||||
* @brief Include O/S headers and smooth out per-O/S differences
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/******************************* Documentation ********************************/
|
||||
|
||||
/*
|
||||
* This header is part of the Mbedthis Portable Runtime and aims to include
|
||||
* all necessary O/S headers and to unify the constants and declarations
|
||||
* required by Mbedthis products. It can be included by C or C++ programs.
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifndef _h_MPR_OS_HDRS
|
||||
#define _h_MPR_OS_HDRS 1
|
||||
|
||||
#include "buildConfig.h"
|
||||
|
||||
/********************************* CPU Families *******************************/
|
||||
/*
|
||||
* Porters, add your CPU families here and update configure code.
|
||||
*/
|
||||
#define MPR_CPU_UNKNOWN 0
|
||||
#define MPR_CPU_IX86 1
|
||||
#define MPR_CPU_PPC 2
|
||||
#define MPR_CPU_SPARC 3
|
||||
#define MPR_CPU_XSCALE 4
|
||||
#define MPR_CPU_ARM 5
|
||||
#define MPR_CPU_MIPS 6
|
||||
#define MPR_CPU_68K 7
|
||||
#define MPR_CPU_SIMNT 8 /* VxWorks NT simulator */
|
||||
#define MPR_CPU_SIMSPARC 9 /* VxWorks sparc simulator */
|
||||
|
||||
/********************************* O/S Includes *******************************/
|
||||
|
||||
#if LINUX || SOLARIS
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <limits.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <pthread.h>
|
||||
#include <pwd.h>
|
||||
#include <resolv.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if LINUX
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#if SOLARIS
|
||||
#include <netinet/in_systm.h>
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define __USE_ISOC99 1
|
||||
#include <math.h>
|
||||
#include <values.h>
|
||||
#endif
|
||||
|
||||
#endif /* LINUX || SOLARIS */
|
||||
|
||||
#if VXWORKS
|
||||
#include <vxWorks.h>
|
||||
#include <envLib.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <loadLib.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sysSymTbl.h>
|
||||
#include <sys/fcntlcom.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/times.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <unldLib.h>
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#include <float.h>
|
||||
#define __USE_ISOC99 1
|
||||
#include <math.h>
|
||||
#endif
|
||||
|
||||
#include <sockLib.h>
|
||||
#include <inetLib.h>
|
||||
#include <ioLib.h>
|
||||
#include <pipeDrv.h>
|
||||
#include <hostLib.h>
|
||||
#include <netdb.h>
|
||||
#include <tickLib.h>
|
||||
#include <taskHookLib.h>
|
||||
|
||||
#endif /* VXWORKS */
|
||||
|
||||
#if MACOSX
|
||||
#include <time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <errno.h>
|
||||
#include <libgen.h>
|
||||
#include <limits.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#include <netinet/in_systm.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <pthread.h>
|
||||
#include <pwd.h>
|
||||
#include <resolv.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/times.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif /* MACOSX */
|
||||
|
||||
#if WIN
|
||||
/*
|
||||
* We replace insecure functions with Mbedthis replacements
|
||||
*/
|
||||
#define _CRT_SECURE_NO_DEPRECATE 1
|
||||
|
||||
#include <ctype.h>
|
||||
#include <conio.h>
|
||||
#include <direct.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#include <limits.h>
|
||||
#include <malloc.h>
|
||||
#include <process.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <winbase.h>
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#include <float.h>
|
||||
#endif
|
||||
#include <shlobj.h>
|
||||
#include <shellapi.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
#if BLD_DEBUG
|
||||
#include <crtdbg.h>
|
||||
#endif
|
||||
#include "mprUnix.h"
|
||||
#endif /* WIN */
|
||||
|
||||
#if BREW
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#warning "Floating point is not supported on Brew"
|
||||
#endif
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
#warning "Multithreading is not supported on Brew"
|
||||
#endif
|
||||
|
||||
#include "AEEModGen.h"
|
||||
#include "AEEAppGen.h"
|
||||
#include "BREWVersion.h"
|
||||
|
||||
#if BREW_MAJ_VER == 2
|
||||
/*
|
||||
* Fix for BREW 2.X
|
||||
*/
|
||||
#ifdef __GNUC__
|
||||
#define __inline extern __inline__
|
||||
#endif
|
||||
#include "AEENet.h"
|
||||
#undef __inline
|
||||
#endif
|
||||
|
||||
#include "AEE.h"
|
||||
#include "AEEBitmap.h"
|
||||
#include "AEEDisp.h"
|
||||
#include "AEEFile.h"
|
||||
#include "AEEHeap.h"
|
||||
#include "AEEImageCtl.h"
|
||||
#include "AEEMedia.h"
|
||||
#include "AEEMediaUtil.h"
|
||||
#include "AEEMimeTypes.h"
|
||||
#include "AEEStdLib.h"
|
||||
#include "AEEShell.h"
|
||||
#include "AEESoundPlayer.h"
|
||||
#include "AEEText.h"
|
||||
#include "AEETransform.h"
|
||||
#include "AEEWeb.h"
|
||||
#if BREW_MAJ_VER >= 3
|
||||
#include "AEESMS.h"
|
||||
#endif
|
||||
#include "AEETAPI.h"
|
||||
|
||||
#if 0
|
||||
#include "AEESound.h"
|
||||
#include "AEEDb.h"
|
||||
#include "AEEMenu.h"
|
||||
#endif
|
||||
|
||||
#endif /* BREW */
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* General Defines ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#ifndef MAXINT
|
||||
#if INT_MAX
|
||||
#define MAXINT INT_MAX
|
||||
#else
|
||||
#define MAXINT 0x7fffffff
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef BITSPERBYTE
|
||||
#define BITSPERBYTE (8 * sizeof(char))
|
||||
#endif
|
||||
|
||||
#define BITS(type) (BITSPERBYTE * (int) sizeof(type))
|
||||
|
||||
#ifndef max
|
||||
#define max(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef min
|
||||
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#define MPR_ARRAY_SIZE(type) (sizeof(type) / sizeof(type[0]))
|
||||
|
||||
#ifndef PRINTF_ATTRIBUTE
|
||||
#if (__GNUC__ >= 3) && !DOXYGEN && BLD_FEATURE_ALLOC_LEAK_TRACK
|
||||
/** Use gcc attribute to check printf fns. a1 is the 1-based index of
|
||||
* the parameter containing the format, and a2 the index of the first
|
||||
* argument. Note that some gcc 2.x versions don't handle this
|
||||
* properly **/
|
||||
#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
|
||||
#else
|
||||
#define PRINTF_ATTRIBUTE(a1, a2)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef char *MprStr; /* Used for dynamic strings */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#else
|
||||
typedef int bool;
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Linux Defines *******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#if LINUX
|
||||
typedef unsigned char uchar;
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
__extension__ typedef long long int int64;
|
||||
__extension__ typedef unsigned long long int uint64;
|
||||
#define INT64(x) (x##LL)
|
||||
#define UINT64(x) (x##ULL)
|
||||
#endif
|
||||
|
||||
#define closesocket(x) close(x)
|
||||
#define MPR_BINARY ""
|
||||
#define MPR_TEXT ""
|
||||
#define O_BINARY 0
|
||||
#define O_TEXT 0
|
||||
#define SOCKET_ERROR -1
|
||||
#define MPR_DLL_EXT ".so"
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define MAX_FLOAT MAXFLOAT
|
||||
#endif
|
||||
|
||||
/*
|
||||
* For some reason it is removed from fedora pthreads.h and only
|
||||
* comes in for UNIX96
|
||||
*/
|
||||
extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
|
||||
__attr, int *__restrict __kind) __THROW;
|
||||
/* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
|
||||
PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
|
||||
PTHREAD_MUTEX_DEFAULT). */
|
||||
extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
|
||||
__THROW;
|
||||
|
||||
#endif /* LINUX */
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* VxWorks Defines ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#if VXWORKS
|
||||
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
|
||||
#define HAVE_SOCKLEN_T
|
||||
typedef int socklen_t;
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
typedef long long int int64;
|
||||
typedef unsigned long long int uint64;
|
||||
#define INT64(x) (x##LL)
|
||||
#define UINT64(x) (x##ULL)
|
||||
#endif
|
||||
|
||||
#define closesocket(x) close(x)
|
||||
#define getpid() taskIdSelf()
|
||||
#define MPR_BINARY ""
|
||||
#define MPR_TEXT ""
|
||||
#define O_BINARY 0
|
||||
#define O_TEXT 0
|
||||
#define SOCKET_ERROR -1
|
||||
#define MPR_DLL_EXT ".so"
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define MAX_FLOAT FLT_MAX
|
||||
#endif
|
||||
|
||||
#undef R_OK
|
||||
#define R_OK 4
|
||||
#undef W_OK
|
||||
#define W_OK 2
|
||||
#undef X_OK
|
||||
#define X_OK 1
|
||||
#undef F_OK
|
||||
#define F_OK 0
|
||||
|
||||
#define MSG_NOSIGNAL 0
|
||||
|
||||
extern int access(char *path, int mode);
|
||||
extern int sysClkRateGet();
|
||||
|
||||
#endif /* VXWORKS */
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** MacOsx Defines ******************************/
|
||||
/******************************************************************************/
|
||||
#if MACOSX
|
||||
typedef unsigned long ulong;
|
||||
typedef unsigned char uchar;
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
__extension__ typedef long long int int64;
|
||||
__extension__ typedef unsigned long long int uint64;
|
||||
#define INT64(x) (x##LL)
|
||||
#define UINT64(x) (x##ULL)
|
||||
#endif
|
||||
|
||||
#define closesocket(x) close(x)
|
||||
#define MPR_BINARY ""
|
||||
#define MPR_TEXT ""
|
||||
#define O_BINARY 0
|
||||
#define O_TEXT 0
|
||||
#define SOCKET_ERROR -1
|
||||
#define MPR_DLL_EXT ".dylib"
|
||||
#define MSG_NOSIGNAL 0
|
||||
#define __WALL 0x40000000
|
||||
#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define MAX_FLOAT MAXFLOAT
|
||||
#endif
|
||||
|
||||
#endif /* MACOSX */
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************* Windows Defines ******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#if WIN
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
typedef unsigned short ushort;
|
||||
|
||||
/*
|
||||
* We always define INT64 types on windows
|
||||
*/
|
||||
#if BLD_FEATURE_INT64 || 1
|
||||
typedef __int64 int64;
|
||||
typedef unsigned __int64 uint64;
|
||||
#define INT64(x) (x##i64)
|
||||
#define UINT64(x) (x##Ui64)
|
||||
#endif
|
||||
|
||||
typedef int uid_t;
|
||||
typedef void *handle;
|
||||
typedef char *caddr_t;
|
||||
typedef long pid_t;
|
||||
typedef int gid_t;
|
||||
typedef ushort mode_t;
|
||||
typedef void *siginfo_t;
|
||||
|
||||
#define HAVE_SOCKLEN_T
|
||||
typedef int socklen_t;
|
||||
|
||||
#undef R_OK
|
||||
#define R_OK 4
|
||||
#undef W_OK
|
||||
#define W_OK 2
|
||||
|
||||
/*
|
||||
* On windows map X_OK to R_OK
|
||||
*/
|
||||
#undef X_OK
|
||||
#define X_OK 4
|
||||
#undef F_OK
|
||||
#define F_OK 0
|
||||
|
||||
#ifndef EADDRINUSE
|
||||
#define EADDRINUSE 46
|
||||
#endif
|
||||
#ifndef EWOULDBLOCK
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
#endif
|
||||
#ifndef ENETDOWN
|
||||
#define ENETDOWN 43
|
||||
#endif
|
||||
#ifndef ECONNRESET
|
||||
#define ECONNRESET 44
|
||||
#endif
|
||||
#ifndef ECONNREFUSED
|
||||
#define ECONNREFUSED 45
|
||||
#endif
|
||||
|
||||
#define MSG_NOSIGNAL 0
|
||||
#define MPR_BINARY "b"
|
||||
#define MPR_TEXT "t"
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define MAX_FLOAT DBL_MAX
|
||||
#endif
|
||||
|
||||
#ifndef FILE_FLAG_FIRST_PIPE_INSTANCE
|
||||
#define FILE_FLAG_FIRST_PIPE_INSTANCE 0x00080000
|
||||
#endif
|
||||
|
||||
#define MPR_DLL_EXT ".dll"
|
||||
#endif /* WIN */
|
||||
|
||||
/******************************************************************************/
|
||||
/****************************** Solaris Defines *******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#if SOLARIS
|
||||
typedef unsigned char uchar;
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
typedef long long int int64;
|
||||
typedef unsigned long long int uint64;
|
||||
#define INT64(x) (x##LL)
|
||||
#define UINT64(x) (x##ULL)
|
||||
#endif
|
||||
|
||||
#define closesocket(x) close(x)
|
||||
#define MPR_BINARY ""
|
||||
#define MPR_TEXT ""
|
||||
#define O_BINARY 0
|
||||
#define O_TEXT 0
|
||||
#define SOCKET_ERROR -1
|
||||
#define MPR_DLL_EXT ".so"
|
||||
#define MSG_NOSIGNAL 0
|
||||
#define INADDR_NONE ((in_addr_t) 0xffffffff)
|
||||
#define __WALL 0
|
||||
#define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
#define MAX_FLOAT MAXFLOAT
|
||||
#endif
|
||||
|
||||
#endif /* SOLARIS */
|
||||
|
||||
/******************************************************************************/
|
||||
/********************************* BREW Defines *******************************/
|
||||
/******************************************************************************/
|
||||
|
||||
#if BREW
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned long ulong;
|
||||
typedef unsigned short ushort;
|
||||
|
||||
typedef uint off_t;
|
||||
typedef long pid_t;
|
||||
|
||||
#if UNUSED
|
||||
typedef int uid_t;
|
||||
typedef void *handle;
|
||||
typedef char *caddr_t;
|
||||
typedef int gid_t;
|
||||
typedef ushort mode_t;
|
||||
typedef void *siginfo_t;
|
||||
|
||||
#define HAVE_SOCKLEN_T
|
||||
typedef int socklen_t;
|
||||
|
||||
#ifndef EADDRINUSE
|
||||
#define EADDRINUSE 46
|
||||
#endif
|
||||
#ifndef EWOULDBLOCK
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
#endif
|
||||
#ifndef ENETDOWN
|
||||
#define ENETDOWN 43
|
||||
#endif
|
||||
#ifndef ECONNRESET
|
||||
#define ECONNRESET 44
|
||||
#endif
|
||||
#ifndef ECONNREFUSED
|
||||
#define ECONNREFUSED 45
|
||||
#endif
|
||||
|
||||
#define MSG_NOSIGNAL 0
|
||||
#define MPR_BINARY "b"
|
||||
#define MPR_TEXT "t"
|
||||
|
||||
#define MPR_DLL_EXT ".dll"
|
||||
#endif
|
||||
|
||||
#define O_RDONLY 0
|
||||
#define O_WRONLY 1
|
||||
#define O_RDWR 2
|
||||
#define O_CREAT 0x200
|
||||
#define O_TRUNC 0x400
|
||||
#define O_BINARY 0
|
||||
#define O_TEXT 0x20000
|
||||
#define O_EXCL 0x40000
|
||||
#define O_APPEND 0x80000
|
||||
|
||||
#define R_OK 4
|
||||
#define W_OK 2
|
||||
#define X_OK 1
|
||||
#define F_OK 0
|
||||
|
||||
#define SEEK_SET 0
|
||||
#define SEEK_CUR 1
|
||||
#define SEEK_END 2
|
||||
|
||||
#if UNUSED
|
||||
struct stat {
|
||||
uint st_size;
|
||||
};
|
||||
#endif
|
||||
|
||||
extern int getpid();
|
||||
extern int isalnum(int c);
|
||||
extern int isalpha(int c);
|
||||
extern int isdigit(int c);
|
||||
extern int islower(int c);
|
||||
extern int isupper(int c);
|
||||
extern int isspace(int c);
|
||||
extern int isxdigit(int c);
|
||||
|
||||
extern uint strlen(const char *str);
|
||||
extern char *strstr(const char *string, const char *strSet);
|
||||
extern void *memset(const void *dest, int c, uint count);
|
||||
extern void exit(int status);
|
||||
extern char *strpbrk(const char *str, const char *set);
|
||||
extern uint strspn(const char *str, const char *set);
|
||||
extern int tolower(int c);
|
||||
extern int toupper(int c);
|
||||
extern void *memcpy(void *dest, const void *src, uint count);
|
||||
extern void *memmove(void *dest, const void *src, uint count);
|
||||
|
||||
extern int atoi(const char *str);
|
||||
extern void free(void *ptr);
|
||||
extern void *malloc(uint size);
|
||||
extern void *realloc(void *ptr, uint size);
|
||||
extern char *strcat(char *dest, const char *src);
|
||||
extern char *strchr(const char *str, int c);
|
||||
extern int strcmp(const char *s1, const char *s2);
|
||||
extern int strncmp(const char *s1, const char *s2, uint count);
|
||||
extern char *strcpy(char *dest, const char *src);
|
||||
extern char *strncpy(char *dest, const char *src, uint count);
|
||||
extern char *strrchr(const char *str, int c);
|
||||
|
||||
#undef printf
|
||||
#define printf DBGPRINTF
|
||||
|
||||
#if BREW_SIMULATOR && BLD_DEBUG
|
||||
extern _CRTIMP int __cdecl _CrtCheckMemory(void);
|
||||
extern _CRTIMP int __cdecl _CrtSetReportHook();
|
||||
#endif
|
||||
|
||||
#endif /* BREW */
|
||||
|
||||
/******************************************************************************/
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _h_MPR_OS_HDRS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,924 @@
|
||||
/**
|
||||
* @file mprPrintf.c
|
||||
* @brief Printf routines safe for embedded programming
|
||||
* @overview This module provides safe replacements for the standard
|
||||
* printf formatting routines.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/*********************************** Defines **********************************/
|
||||
/*
|
||||
* Class definitions
|
||||
*/
|
||||
#define CLASS_NORMAL 0 /* [All other] Normal characters */
|
||||
#define CLASS_PERCENT 1 /* [%] Begin format */
|
||||
#define CLASS_MODIFIER 2 /* [-+ #,] Modifiers */
|
||||
#define CLASS_ZERO 3 /* [0] Special modifier */
|
||||
#define CLASS_STAR 4 /* [*] Width supplied by arg */
|
||||
#define CLASS_DIGIT 5 /* [1-9] Field widths */
|
||||
#define CLASS_DOT 6 /* [.] Introduce precision */
|
||||
#define CLASS_BITS 7 /* [hlL] Length bits */
|
||||
#define CLASS_TYPE 8 /* [cdfinopsSuxX] Type specifiers */
|
||||
|
||||
#define STATE_NORMAL 0 /* Normal chars in format string */
|
||||
#define STATE_PERCENT 1 /* "%" */
|
||||
#define STATE_MODIFIER 2 /* Read flag */
|
||||
#define STATE_WIDTH 3 /* Width spec */
|
||||
#define STATE_DOT 4 /* "." */
|
||||
#define STATE_PRECISION 5 /* Precision spec */
|
||||
#define STATE_BITS 6 /* Size spec */
|
||||
#define STATE_TYPE 7 /* Data type */
|
||||
#define STATE_COUNT 8
|
||||
|
||||
/*
|
||||
* Format: %[modifier][width][precision][bits][type]
|
||||
*
|
||||
* #define CLASS_MODIFIER 2 [-+ #,] Modifiers
|
||||
* #define CLASS_BITS 7 [hlL] Length bits
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Flags
|
||||
*/
|
||||
#define SPRINTF_LEFT 0x1 /* Left align */
|
||||
#define SPRINTF_SIGN 0x2 /* Always sign the result */
|
||||
#define SPRINTF_LEAD_SPACE 0x4 /* put leading space for +ve numbers */
|
||||
#define SPRINTF_ALTERNATE 0x8 /* Alternate format */
|
||||
#define SPRINTF_LEAD_ZERO 0x10 /* Zero pad */
|
||||
#define SPRINTF_SHORT 0x20 /* 16-bit */
|
||||
#define SPRINTF_LONG 0x40 /* 32-bit */
|
||||
#if BLD_FEATURE_INT64
|
||||
#define SPRINTF_LONGLONG 0x80 /* 64-bit */
|
||||
#endif
|
||||
#define SPRINTF_COMMA 0x100 /* Thousand comma separators */
|
||||
#define SPRINTF_UPPER_CASE 0x200 /* As the name says for numbers */
|
||||
|
||||
typedef struct Format {
|
||||
uchar *buf;
|
||||
uchar *endbuf;
|
||||
uchar *start;
|
||||
uchar *end;
|
||||
int growBy;
|
||||
int maxsize;
|
||||
|
||||
int precision;
|
||||
int radix;
|
||||
int width;
|
||||
int flags;
|
||||
int len;
|
||||
} Format;
|
||||
|
||||
static int growBuf(MPR_LOC_DEC(ctx, loc), Format *fmt);
|
||||
|
||||
#define BPUT(ctx, loc, fmt, c) \
|
||||
if (1) { \
|
||||
/* Less one to allow room for the null */ \
|
||||
if ((fmt)->end >= ((fmt)->endbuf - sizeof(char))) { \
|
||||
if (growBuf(MPR_LOC_PASS(ctx, loc), fmt)) { \
|
||||
*(fmt)->end++ = (c); \
|
||||
} \
|
||||
} else { \
|
||||
*(fmt)->end++ = (c); \
|
||||
} \
|
||||
} else
|
||||
|
||||
#define BPUTNULL(ctx, loc, fmt) \
|
||||
if (1) { \
|
||||
if ((fmt)->end > (fmt)->endbuf) { \
|
||||
if (growBuf(MPR_LOC_PASS(ctx, loc), fmt)) { \
|
||||
*(fmt)->end = '\0'; \
|
||||
} \
|
||||
} else { \
|
||||
*(fmt)->end = '\0'; \
|
||||
} \
|
||||
} else
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
#define unum uint64
|
||||
#define num int64
|
||||
#else
|
||||
#define unum uint
|
||||
#define num int
|
||||
#endif
|
||||
|
||||
/***************************** Forward Declarations ***************************/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
static int getState(char c, int state);
|
||||
static int mprSprintfCore(MPR_LOC_DEC(ctx, loc), char **s,
|
||||
int maxsize, const char *fmt, va_list arg);
|
||||
static void outNum(MPR_LOC_DEC(ctx, loc), Format *fmt, const char *prefix,
|
||||
unum val);
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
static void outFloat(MPR_LOC_DEC(ctx, loc), Format *fmt, char specChar,
|
||||
double value);
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprPrintf(MprCtx ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
int len;
|
||||
MprApp *app;
|
||||
|
||||
/* No asserts here as this is used as part of assert reporting */
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
if (len >= 0 && app->console) {
|
||||
len = mprWrite(app->console, buf, len);
|
||||
}
|
||||
mprFree(buf);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprErrorPrintf(MprCtx ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
int len;
|
||||
MprApp *app;
|
||||
|
||||
/* No asserts here as this is used as part of assert reporting */
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
if (len >= 0 && app->error) {
|
||||
len = mprWrite(app->error, buf, len);
|
||||
}
|
||||
mprFree(buf);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprFprintf(MprFile *file, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char *buf;
|
||||
int len;
|
||||
|
||||
if (file == 0) {
|
||||
return MPR_ERR_BAD_HANDLE;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = mprAllocVsprintf(MPR_LOC_ARGS(file), &buf, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (len >= 0) {
|
||||
len = mprWrite(file, buf, len);
|
||||
}
|
||||
mprFree(buf);
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Printf with a static buffer. Used internally only. WILL NOT MALLOC.
|
||||
*/
|
||||
|
||||
int mprStaticPrintf(MprCtx ctx, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char buf[MPR_MAX_STRING];
|
||||
char *bufp;
|
||||
int len;
|
||||
MprApp *app;
|
||||
|
||||
app = mprGetApp(ctx);
|
||||
|
||||
va_start(ap, fmt);
|
||||
bufp = buf;
|
||||
len = mprSprintfCore(MPR_LOC_ARGS(0), &bufp, MPR_MAX_STRING, fmt, ap);
|
||||
va_end(ap);
|
||||
if (len >= 0) {
|
||||
len = mprWrite(app->console, buf, len);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprSprintf(char *buf, int n, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
mprAssert(buf);
|
||||
mprAssert(fmt);
|
||||
mprAssert(n > 0);
|
||||
|
||||
va_start(ap, fmt);
|
||||
result = mprSprintfCore(MPR_LOC_ARGS(0), &buf, n, fmt, ap);
|
||||
va_end(ap);
|
||||
return result;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprVsprintf(char *buf, int n, const char *fmt, va_list arg)
|
||||
{
|
||||
mprAssert(buf);
|
||||
mprAssert(fmt);
|
||||
mprAssert(n > 0);
|
||||
|
||||
return mprSprintfCore(MPR_LOC_ARGS(0), &buf, n, fmt, arg);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprAllocSprintf(MPR_LOC_DEC(ctx, loc), char **buf, int maxSize,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
mprAssert(buf);
|
||||
mprAssert(fmt);
|
||||
|
||||
*buf = 0;
|
||||
va_start(ap, fmt);
|
||||
result = mprSprintfCore(MPR_LOC_PASS(ctx, loc), buf, maxSize, fmt, ap);
|
||||
va_end(ap);
|
||||
return result;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprAllocVsprintf(MPR_LOC_DEC(ctx, loc), char **buf, int maxSize,
|
||||
const char *fmt, va_list arg)
|
||||
{
|
||||
mprAssert(buf);
|
||||
mprAssert(fmt);
|
||||
|
||||
*buf = 0;
|
||||
return mprSprintfCore(MPR_LOC_PASS(ctx, loc), buf, maxSize, fmt, arg);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int getState(char c, int state)
|
||||
{
|
||||
/*
|
||||
* Declared here to remove all static / globals
|
||||
* FUTURE OPT -- need to measure this. Could be slow on BREW.
|
||||
*/
|
||||
|
||||
char stateMap[] = {
|
||||
/* STATES: Normal Percent Modifier Width Dot Prec Bits Type */
|
||||
/* CLASS 0 1 2 3 4 5 6 7 */
|
||||
/* Normal 0 */ 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* Percent 1 */ 1, 0, 1, 1, 1, 1, 1, 1,
|
||||
/* Modifier 2 */ 0, 2, 2, 0, 0, 0, 0, 0,
|
||||
/* Zero 3 */ 0, 2, 2, 3, 0, 5, 0, 0,
|
||||
/* Star 4 */ 0, 3, 3, 0, 5, 0, 0, 0,
|
||||
/* Digit 5 */ 0, 3, 3, 3, 5, 5, 0, 0,
|
||||
/* Dot 6 */ 0, 4, 4, 4, 0, 0, 0, 0,
|
||||
/* Bits 7 */ 0, 6, 6, 6, 6, 6, 6, 0,
|
||||
/* Types 8 */ 0, 7, 7, 7, 7, 7, 7, 0,
|
||||
};
|
||||
|
||||
/*
|
||||
* Format: %[modifier][width][precision][bits][type]
|
||||
*/
|
||||
char classMap[] = {
|
||||
/* 0 ' ' ! " # $ % & ' */
|
||||
2, 0, 0, 2, 0, 1, 0, 0,
|
||||
/* 07 ( ) * + , - . / */
|
||||
0, 0, 4, 2, 2, 2, 6, 0,
|
||||
/* 10 0 1 2 3 4 5 6 7 */
|
||||
3, 5, 5, 5, 5, 5, 5, 5,
|
||||
/* 17 8 9 : ; < = > ? */
|
||||
5, 5, 0, 0, 0, 0, 0, 0,
|
||||
/* 20 @ A B C D E F G */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 27 H I J K L M N O */
|
||||
0, 0, 0, 0, 7, 0, 0, 0,
|
||||
/* 30 P Q R S T U V W */
|
||||
0, 0, 0, 8, 0, 0, 0, 0,
|
||||
/* 37 X Y Z [ \ ] ^ _ */
|
||||
8, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 40 ' a b c d e f g */
|
||||
0, 0, 0, 8, 8, 0, 8, 0,
|
||||
/* 47 h i j k l m n o */
|
||||
7, 8, 0, 0, 7, 0, 8, 8,
|
||||
/* 50 p q r s t u v w */
|
||||
8, 0, 0, 8, 0, 8, 0, 0,
|
||||
/* 57 x y z */
|
||||
8, 0, 0,
|
||||
};
|
||||
|
||||
int chrClass;
|
||||
|
||||
if (c < ' ' || c > 'z') {
|
||||
chrClass = CLASS_NORMAL;
|
||||
} else {
|
||||
mprAssert((c - ' ') < (int) sizeof(classMap));
|
||||
chrClass = classMap[(c - ' ')];
|
||||
}
|
||||
mprAssert((chrClass * STATE_COUNT + state) < (int) sizeof(stateMap));
|
||||
state = stateMap[chrClass * STATE_COUNT + state];
|
||||
return state;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int mprSprintfCore(MPR_LOC_DEC(ctx, loc), char **bufPtr,
|
||||
int maxsize, const char *spec, va_list arg)
|
||||
{
|
||||
Format fmt;
|
||||
char *cp;
|
||||
char c;
|
||||
char *sValue;
|
||||
num iValue;
|
||||
unum uValue;
|
||||
int count, i, len, state;
|
||||
|
||||
mprAssert(bufPtr);
|
||||
mprAssert(spec);
|
||||
|
||||
if (*bufPtr != 0) {
|
||||
mprAssert(maxsize > 0);
|
||||
fmt.buf = (uchar*) *bufPtr;
|
||||
fmt.endbuf = &fmt.buf[maxsize];
|
||||
fmt.growBy = 0;
|
||||
} else {
|
||||
if (maxsize <= 0) {
|
||||
maxsize = MAXINT;
|
||||
}
|
||||
|
||||
len = min(MPR_DEFAULT_ALLOC, maxsize);
|
||||
fmt.buf = (uchar*) mprAllocBlock(MPR_LOC_PASS(ctx, loc), len);
|
||||
fmt.endbuf = &fmt.buf[len];
|
||||
fmt.growBy = MPR_DEFAULT_ALLOC * 2;
|
||||
}
|
||||
|
||||
fmt.maxsize = maxsize;
|
||||
fmt.start = fmt.buf;
|
||||
fmt.end = fmt.buf;
|
||||
fmt.len = 0;
|
||||
*fmt.start = '\0';
|
||||
|
||||
state = STATE_NORMAL;
|
||||
|
||||
while ((c = *spec++) != '\0') {
|
||||
state = getState(c, state);
|
||||
|
||||
switch (state) {
|
||||
case STATE_NORMAL:
|
||||
BPUT(ctx, loc, &fmt, c);
|
||||
break;
|
||||
|
||||
case STATE_PERCENT:
|
||||
fmt.precision = -1;
|
||||
fmt.width = 0;
|
||||
fmt.flags = 0;
|
||||
break;
|
||||
|
||||
case STATE_MODIFIER:
|
||||
switch (c) {
|
||||
case '+':
|
||||
fmt.flags |= SPRINTF_SIGN;
|
||||
break;
|
||||
case '-':
|
||||
fmt.flags |= SPRINTF_LEFT;
|
||||
break;
|
||||
case '#':
|
||||
fmt.flags |= SPRINTF_ALTERNATE;
|
||||
break;
|
||||
case '0':
|
||||
fmt.flags |= SPRINTF_LEAD_ZERO;
|
||||
break;
|
||||
case ' ':
|
||||
fmt.flags |= SPRINTF_LEAD_SPACE;
|
||||
break;
|
||||
case ',':
|
||||
fmt.flags |= SPRINTF_COMMA;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_WIDTH:
|
||||
if (c == '*') {
|
||||
fmt.width = va_arg(arg, int);
|
||||
if (fmt.width < 0) {
|
||||
fmt.width = -fmt.width;
|
||||
fmt.flags |= SPRINTF_LEFT;
|
||||
}
|
||||
} else {
|
||||
while (isdigit((int)c)) {
|
||||
fmt.width = fmt.width * 10 + (c - '0');
|
||||
c = *spec++;
|
||||
}
|
||||
spec--;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_DOT:
|
||||
fmt.precision = 0;
|
||||
fmt.flags &= ~SPRINTF_LEAD_ZERO;
|
||||
break;
|
||||
|
||||
case STATE_PRECISION:
|
||||
if (c == '*') {
|
||||
fmt.precision = va_arg(arg, int);
|
||||
} else {
|
||||
while (isdigit((int) c)) {
|
||||
fmt.precision = fmt.precision * 10 + (c - '0');
|
||||
c = *spec++;
|
||||
}
|
||||
spec--;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_BITS:
|
||||
switch (c) {
|
||||
#if BLD_FEATURE_INT64
|
||||
case 'L':
|
||||
fmt.flags |= SPRINTF_LONGLONG; /* 64 bit */
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 'l':
|
||||
fmt.flags |= SPRINTF_LONG;
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
fmt.flags |= SPRINTF_SHORT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_TYPE:
|
||||
switch (c) {
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
case 'e':
|
||||
case 'g':
|
||||
case 'f':
|
||||
fmt.radix = 10;
|
||||
outFloat(MPR_LOC_PASS(ctx, loc), &fmt, c,
|
||||
(double) va_arg(arg, double));
|
||||
break;
|
||||
#endif
|
||||
case 'c':
|
||||
BPUT(ctx, loc, &fmt, (char) va_arg(arg, int));
|
||||
break;
|
||||
|
||||
case 's':
|
||||
case 'S':
|
||||
sValue = va_arg(arg, char*);
|
||||
if (sValue == 0) {
|
||||
sValue = "null";
|
||||
len = strlen(sValue);
|
||||
} else if (fmt.flags & SPRINTF_ALTERNATE) {
|
||||
sValue++;
|
||||
len = (int) *sValue;
|
||||
} else if (fmt.precision >= 0) {
|
||||
/*
|
||||
* Can't use strlen(), the string may not have a null
|
||||
*/
|
||||
cp = sValue;
|
||||
for (len = 0; len < fmt.precision; len++) {
|
||||
if (*cp++ == '\0') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
len = strlen(sValue);
|
||||
}
|
||||
if (!(fmt.flags & SPRINTF_LEFT)) {
|
||||
for (i = len; i < fmt.width; i++) {
|
||||
BPUT(ctx, loc, &fmt, (char) ' ');
|
||||
}
|
||||
}
|
||||
for (i = 0; i < len && *sValue; i++) {
|
||||
BPUT(ctx, loc, &fmt, *sValue++);
|
||||
}
|
||||
if (fmt.flags & SPRINTF_LEFT) {
|
||||
for (i = len; i < fmt.width; i++) {
|
||||
BPUT(ctx, loc, &fmt, (char) ' ');
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
;
|
||||
case 'd':
|
||||
fmt.radix = 10;
|
||||
if (fmt.flags & SPRINTF_SHORT) {
|
||||
iValue = (short) va_arg(arg, int);
|
||||
} else if (fmt.flags & SPRINTF_LONG) {
|
||||
iValue = va_arg(arg, long);
|
||||
#if BLD_FEATURE_INT64
|
||||
} else if (fmt.flags & SPRINTF_LONGLONG) {
|
||||
iValue = va_arg(arg, num);
|
||||
#endif
|
||||
} else {
|
||||
iValue = va_arg(arg, int);
|
||||
}
|
||||
if (iValue >= 0) {
|
||||
if (fmt.flags & SPRINTF_LEAD_SPACE) {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, " ", iValue);
|
||||
} else if (fmt.flags & SPRINTF_SIGN) {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "+", iValue);
|
||||
} else {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, iValue);
|
||||
}
|
||||
} else {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "-", -iValue);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'X':
|
||||
fmt.flags |= SPRINTF_UPPER_CASE;
|
||||
/* Fall through */
|
||||
case 'o':
|
||||
case 'x':
|
||||
case 'u':
|
||||
if (fmt.flags & SPRINTF_SHORT) {
|
||||
uValue = (ushort) va_arg(arg, uint);
|
||||
} else if (fmt.flags & SPRINTF_LONG) {
|
||||
uValue = va_arg(arg, ulong);
|
||||
#if BLD_FEATURE_INT64
|
||||
} else if (fmt.flags & SPRINTF_LONGLONG) {
|
||||
uValue = va_arg(arg, unum);
|
||||
#endif
|
||||
} else {
|
||||
uValue = va_arg(arg, uint);
|
||||
}
|
||||
if (c == 'u') {
|
||||
fmt.radix = 10;
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue);
|
||||
} else if (c == 'o') {
|
||||
fmt.radix = 8;
|
||||
if (fmt.flags & SPRINTF_ALTERNATE && uValue != 0) {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0", uValue);
|
||||
} else {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue);
|
||||
}
|
||||
} else {
|
||||
fmt.radix = 16;
|
||||
if (fmt.flags & SPRINTF_ALTERNATE && uValue != 0) {
|
||||
if (c == 'X') {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0X", uValue);
|
||||
} else {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0x", uValue);
|
||||
}
|
||||
} else {
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, 0, uValue);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'n': /* Count of chars seen thus far */
|
||||
if (fmt.flags & SPRINTF_SHORT) {
|
||||
short *count = va_arg(arg, short*);
|
||||
*count = fmt.end - fmt.start;
|
||||
} else if (fmt.flags & SPRINTF_LONG) {
|
||||
long *count = va_arg(arg, long*);
|
||||
*count = fmt.end - fmt.start;
|
||||
} else {
|
||||
int *count = va_arg(arg, int *);
|
||||
*count = fmt.end - fmt.start;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'p': /* Pointer */
|
||||
#if __WORDSIZE == 64 && BLD_FEATURE_INT64
|
||||
uValue = (unum) va_arg(arg, void*);
|
||||
#else
|
||||
uValue = (uint) (int) va_arg(arg, void*);
|
||||
#endif
|
||||
fmt.radix = 16;
|
||||
outNum(MPR_LOC_PASS(ctx, loc), &fmt, "0x", uValue);
|
||||
break;
|
||||
|
||||
default:
|
||||
BPUT(ctx, loc, &fmt, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
BPUTNULL(ctx, loc, &fmt);
|
||||
|
||||
count = fmt.end - fmt.start;
|
||||
if (*bufPtr == 0) {
|
||||
*bufPtr = (char*) fmt.buf;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Output a number according to the given format. If BLD_FEATURE_INT64 is
|
||||
* defined, then uses 64 bits universally. Slower but smaller code.
|
||||
*/
|
||||
|
||||
static void outNum(MPR_LOC_DEC(ctx, loc), Format *fmt, const char *prefix,
|
||||
unum value)
|
||||
{
|
||||
char numBuf[64];
|
||||
char *cp;
|
||||
char *endp;
|
||||
char c;
|
||||
int letter, len, leadingZeros, i, fill;
|
||||
|
||||
endp = &numBuf[sizeof(numBuf) - 1];
|
||||
*endp = '\0';
|
||||
cp = endp;
|
||||
|
||||
/*
|
||||
* Convert to ascii
|
||||
*/
|
||||
if (fmt->radix == 16) {
|
||||
do {
|
||||
letter = (int) (value % fmt->radix);
|
||||
if (letter > 9) {
|
||||
if (fmt->flags & SPRINTF_UPPER_CASE) {
|
||||
letter = 'A' + letter - 10;
|
||||
} else {
|
||||
letter = 'a' + letter - 10;
|
||||
}
|
||||
} else {
|
||||
letter += '0';
|
||||
}
|
||||
*--cp = letter;
|
||||
value /= fmt->radix;
|
||||
} while (value > 0);
|
||||
|
||||
} else if (fmt->flags & SPRINTF_COMMA) {
|
||||
i = 1;
|
||||
do {
|
||||
*--cp = '0' + (int) (value % fmt->radix);
|
||||
value /= fmt->radix;
|
||||
if ((i++ % 3) == 0 && value > 0) {
|
||||
*--cp = ',';
|
||||
}
|
||||
} while (value > 0);
|
||||
} else {
|
||||
do {
|
||||
*--cp = '0' + (int) (value % fmt->radix);
|
||||
value /= fmt->radix;
|
||||
} while (value > 0);
|
||||
}
|
||||
|
||||
len = endp - cp;
|
||||
fill = fmt->width - len;
|
||||
|
||||
if (prefix != 0) {
|
||||
fill -= strlen(prefix);
|
||||
}
|
||||
leadingZeros = (fmt->precision > len) ? fmt->precision - len : 0;
|
||||
fill -= leadingZeros;
|
||||
|
||||
if (!(fmt->flags & SPRINTF_LEFT)) {
|
||||
c = (fmt->flags & SPRINTF_LEAD_ZERO) ? '0': ' ';
|
||||
for (i = 0; i < fill; i++) {
|
||||
BPUT(ctx, loc, fmt, c);
|
||||
}
|
||||
}
|
||||
if (prefix != 0) {
|
||||
while (*prefix) {
|
||||
BPUT(ctx, loc, fmt, *prefix++);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < leadingZeros; i++) {
|
||||
BPUT(ctx, loc, fmt, '0');
|
||||
}
|
||||
while (*cp) {
|
||||
BPUT(ctx, loc, fmt, *cp);
|
||||
cp++;
|
||||
}
|
||||
if (fmt->flags & SPRINTF_LEFT) {
|
||||
for (i = 0; i < fill; i++) {
|
||||
BPUT(ctx, loc, fmt, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
/*
|
||||
* Output a floating point number
|
||||
*/
|
||||
|
||||
static void outFloat(MPR_LOC_DEC(ctx, loc), Format *fmt, char specChar,
|
||||
double value)
|
||||
{
|
||||
char *cp;
|
||||
#if FUTURE
|
||||
char numBuf[64];
|
||||
char *endp;
|
||||
char c;
|
||||
int letter, len, leadingZeros, i, fill, width, precision;
|
||||
|
||||
endp = &numBuf[sizeof(numBuf) - 1];
|
||||
*endp = '\0';
|
||||
|
||||
precision = fmt->precision;
|
||||
if (precision < 0) {
|
||||
precision = 6;
|
||||
} else if (precision > (sizeof(numBuf) - 1)) {
|
||||
precision = (sizeof(numBuf) - 1);
|
||||
}
|
||||
width = min(fmt->width, sizeof(numBuf) - 1);
|
||||
|
||||
if (__isnanl(value)) {
|
||||
"nan"
|
||||
} else if (__isinfl(value)) {
|
||||
"infinity"
|
||||
} else if (value < 0) {
|
||||
prefix = "-";
|
||||
} else if (fmt.flags & SPRINTF_LEAD_SPACE) {
|
||||
prefix = " ";
|
||||
} else if (fmt.flags & SPRINTF_SIGN) {
|
||||
prefix = "+";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Do the exponent part
|
||||
*/
|
||||
cp = &numBuf[sizeof(numBuf) - precision];
|
||||
for (i = 0; i < precision; i++) {
|
||||
*cp++ = '0' + (int) (value % fmt->radix);
|
||||
value /= fmt->radix;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do the decimal part
|
||||
*/
|
||||
if (fmt->flags & SPRINTF_COMMA) {
|
||||
i = 1;
|
||||
do {
|
||||
*--cp = '0' + (int) (value % fmt->radix);
|
||||
value /= fmt->radix;
|
||||
if ((i++ % 3) == 0 && value > 0) {
|
||||
*--cp = ',';
|
||||
}
|
||||
} while (value >= 1.0);
|
||||
|
||||
} else {
|
||||
do {
|
||||
*--cp = '0' + (int) (value % fmt->radix);
|
||||
value /= fmt->radix;
|
||||
} while (value > 1.0);
|
||||
}
|
||||
|
||||
len = endp - cp;
|
||||
fill = fmt->width - len;
|
||||
|
||||
if (prefix != 0) {
|
||||
fill -= strlen(prefix);
|
||||
}
|
||||
|
||||
leadingZeros = (fmt->precision > len) ? fmt->precision - len : 0;
|
||||
fill -= leadingZeros;
|
||||
|
||||
if (!(fmt->flags & SPRINTF_LEFT)) {
|
||||
c = (fmt->flags & SPRINTF_LEAD_ZERO) ? '0': ' ';
|
||||
for (i = 0; i < fill; i++) {
|
||||
BPUT(ctx, loc, fmt, c);
|
||||
}
|
||||
}
|
||||
if (prefix != 0) {
|
||||
BPUT(ctx, loc, fmt, prefix);
|
||||
}
|
||||
for (i = 0; i < leadingZeros; i++) {
|
||||
BPUT(ctx, loc, fmt, '0');
|
||||
}
|
||||
BPUT(ctx, loc, fmt, cp);
|
||||
if (fmt->flags & SPRINTF_LEFT) {
|
||||
for (i = 0; i < fill; i++) {
|
||||
BPUT(ctx, loc, fmt, ' ');
|
||||
}
|
||||
}
|
||||
#else
|
||||
char numBuf[64];
|
||||
if (specChar == 'f') {
|
||||
sprintf(numBuf, "%*.*f", fmt->width, fmt->precision, value);
|
||||
} else if (specChar == 'g') {
|
||||
sprintf(numBuf, "%*.*g", fmt->width, fmt->precision, value);
|
||||
} else if (specChar == 'e') {
|
||||
sprintf(numBuf, "%*.*e", fmt->width, fmt->precision, value);
|
||||
}
|
||||
for (cp = numBuf; *cp; cp++) {
|
||||
BPUT(ctx, loc, fmt, *cp);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* BLD_FEATURE_FLOATING_POINT */
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Grow the buffer to fit new data. Return 1 if the buffer can grow.
|
||||
* Grow using the growBy size specified when creating the buffer.
|
||||
*/
|
||||
|
||||
static int growBuf(MPR_LOC_DEC(ctx, loc), Format *fmt)
|
||||
{
|
||||
uchar *newbuf;
|
||||
int buflen;
|
||||
|
||||
buflen = fmt->endbuf - fmt->buf;
|
||||
if (fmt->maxsize >= 0 && buflen >= fmt->maxsize) {
|
||||
return 0;
|
||||
}
|
||||
if (fmt->growBy < 0) {
|
||||
/*
|
||||
* User supplied buffer
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
newbuf = (uchar*) mprAlloc(ctx, buflen + fmt->growBy);
|
||||
if (fmt->buf) {
|
||||
memcpy(newbuf, fmt->buf, buflen);
|
||||
mprFree(fmt->buf);
|
||||
}
|
||||
|
||||
buflen += fmt->growBy;
|
||||
fmt->end = newbuf + (fmt->end - fmt->buf);
|
||||
fmt->start = newbuf + (fmt->start - fmt->buf);
|
||||
fmt->buf = newbuf;
|
||||
fmt->endbuf = &fmt->buf[buflen];
|
||||
|
||||
/*
|
||||
* Increase growBy to reduce overhead
|
||||
*/
|
||||
if ((buflen + (fmt->growBy * 2)) < fmt->maxsize) {
|
||||
fmt->growBy *= 2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,727 @@
|
||||
/**
|
||||
* @file mprString.c
|
||||
* @brief String routines safe for embedded programming
|
||||
* @overview This module provides safe replacements for the standard
|
||||
* string library.
|
||||
* @remarks Most routines in this file are not thread-safe. It is the callers
|
||||
* responsibility to perform all thread synchronization.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* We need to use the underlying str(cpy) routines to implement our safe
|
||||
* alternatives
|
||||
*/
|
||||
#if !DOXYGEN
|
||||
#define UNSAFE_FUNCTIONS_OK 1
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
/**************************** Safe String Handling ****************************/
|
||||
/******************************************************************************/
|
||||
|
||||
int mprStrcpy(char *dest, int destMax, const char *src)
|
||||
{
|
||||
int len;
|
||||
|
||||
mprAssert(dest);
|
||||
mprAssert(destMax >= 0);
|
||||
mprAssert(src);
|
||||
|
||||
len = strlen(src);
|
||||
if (destMax > 0 && len >= destMax && len > 0) {
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (len > 0) {
|
||||
memcpy(dest, src, len);
|
||||
dest[len] = '\0';
|
||||
} else {
|
||||
*dest = '\0';
|
||||
len = 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprAllocStrcpy(MPR_LOC_DEC(ctx, loc), char **dest, int destMax,
|
||||
const char *src)
|
||||
{
|
||||
int len;
|
||||
|
||||
mprAssert(dest);
|
||||
mprAssert(destMax >= 0);
|
||||
mprAssert(src);
|
||||
|
||||
len = strlen(src);
|
||||
if (destMax > 0 && len >= destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (len > 0) {
|
||||
*dest = (char*) mprAllocBlock(MPR_LOC_PASS(ctx, loc), len);
|
||||
memcpy(*dest, src, len);
|
||||
(*dest)[len] = '\0';
|
||||
} else {
|
||||
*dest = (char*) mprAlloc(ctx, 1);
|
||||
*dest = '\0';
|
||||
len = 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprMemcpy(char *dest, int destMax, const char *src, int nbytes)
|
||||
{
|
||||
mprAssert(dest);
|
||||
mprAssert(destMax <= 0 || destMax >= nbytes);
|
||||
mprAssert(src);
|
||||
mprAssert(nbytes >= 0);
|
||||
|
||||
if (destMax > 0 && nbytes > destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (nbytes > 0) {
|
||||
memcpy(dest, src, nbytes);
|
||||
return nbytes;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprAllocMemcpy(MPR_LOC_DEC(ctx, loc), char **dest, int destMax,
|
||||
const void *src, int nbytes)
|
||||
{
|
||||
mprAssert(dest);
|
||||
mprAssert(src);
|
||||
mprAssert(nbytes > 0);
|
||||
mprAssert(destMax <= 0 || destMax >= nbytes);
|
||||
|
||||
if (destMax > 0 && nbytes > destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (nbytes > 0) {
|
||||
*dest = (char*) mprAllocBlock(MPR_LOC_PASS(ctx,loc), nbytes);
|
||||
if (*dest == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
memcpy(*dest, src, nbytes);
|
||||
} else {
|
||||
*dest = (char*) mprAlloc(ctx, 1);
|
||||
}
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int mprCoreStrcat(MPR_LOC_DEC(ctx, loc), char **destp, int destMax,
|
||||
int existingLen, const char *delim, const char *src, va_list args)
|
||||
{
|
||||
va_list ap;
|
||||
char *dest, *str, *dp;
|
||||
int sepLen, addBytes, required;
|
||||
|
||||
mprAssert(destp);
|
||||
mprAssert(destMax >= 0);
|
||||
mprAssert(src);
|
||||
|
||||
dest = *destp;
|
||||
sepLen = (delim) ? strlen(delim) : 0;
|
||||
|
||||
#ifdef __va_copy
|
||||
__va_copy(ap, args);
|
||||
#else
|
||||
ap = args;
|
||||
#endif
|
||||
addBytes = 0;
|
||||
if (existingLen > 0) {
|
||||
addBytes += sepLen;
|
||||
}
|
||||
str = (char*) src;
|
||||
|
||||
while (str) {
|
||||
addBytes += strlen(str);
|
||||
str = va_arg(ap, char*);
|
||||
if (str) {
|
||||
addBytes += sepLen;
|
||||
}
|
||||
}
|
||||
|
||||
required = existingLen + addBytes + 1;
|
||||
if (destMax > 0 && required >= destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
|
||||
if (ctx != 0) {
|
||||
if (dest == 0) {
|
||||
dest = (char*) mprAllocBlock(MPR_LOC_PASS(ctx, loc), required);
|
||||
} else {
|
||||
dest = (char*) mprReallocBlock(MPR_LOC_PASS(ctx, loc), dest,
|
||||
required);
|
||||
}
|
||||
} else {
|
||||
dest = (char*) *destp;
|
||||
}
|
||||
|
||||
dp = &dest[existingLen];
|
||||
if (delim && existingLen > 0) {
|
||||
strcpy(dp, delim);
|
||||
dp += sepLen;
|
||||
}
|
||||
|
||||
if (addBytes > 0) {
|
||||
#ifdef __va_copy
|
||||
__va_copy(ap, args);
|
||||
#else
|
||||
ap = args;
|
||||
#endif
|
||||
str = (char*) src;
|
||||
while (str) {
|
||||
strcpy(dp, str);
|
||||
dp += strlen(str);
|
||||
str = va_arg(ap, char*);
|
||||
if (delim && str) {
|
||||
strcpy(dp, delim);
|
||||
dp += sepLen;
|
||||
}
|
||||
}
|
||||
} else if (dest == 0) {
|
||||
dest = (char*) mprAlloc(ctx, 1);
|
||||
}
|
||||
*dp = '\0';
|
||||
|
||||
*destp = dest;
|
||||
mprAssert(dp < &dest[required]);
|
||||
return required - 1;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprStrcat(char *dest, int destMax, const char *delim, const char *src, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
mprAssert(dest);
|
||||
mprAssert(src);
|
||||
|
||||
va_start(ap, src);
|
||||
rc = mprCoreStrcat(MPR_LOC_ARGS(0), &dest, destMax, strlen(dest),
|
||||
delim, src, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprAllocStrcat(MPR_LOC_DEC(ctx, loc), char **destp, int destMax,
|
||||
const char *delim, const char *src, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
mprAssert(destp);
|
||||
mprAssert(src);
|
||||
|
||||
*destp = 0;
|
||||
va_start(ap, src);
|
||||
rc = mprCoreStrcat(MPR_LOC_PASS(ctx, loc), destp, destMax, 0, delim,
|
||||
src, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprReallocStrcat(MPR_LOC_DEC(ctx, loc), char **destp, int destMax,
|
||||
int existingLen, const char *delim, const char *src,...)
|
||||
{
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
va_start(ap, src);
|
||||
rc = mprCoreStrcat(MPR_LOC_PASS(ctx, loc), destp, destMax, existingLen,
|
||||
delim, src, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprStrlen(const char *src, int max)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = strlen(src);
|
||||
if (len >= max) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprStrTrim(char *str, const char *set)
|
||||
{
|
||||
int len, i;
|
||||
|
||||
if (str == 0 || set == 0) {
|
||||
return str;
|
||||
}
|
||||
|
||||
i = strspn(str, set);
|
||||
str += i;
|
||||
|
||||
len = strlen(str);
|
||||
while (strspn(&str[len - 1], set) > 0) {
|
||||
str[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Map a string to lower case (overwrites original string)
|
||||
*/
|
||||
|
||||
char *mprStrLower(char *str)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
mprAssert(str);
|
||||
|
||||
if (str == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (cp = str; *cp; cp++) {
|
||||
if (isupper(*cp)) {
|
||||
*cp = (char) tolower(*cp);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Map a string to upper case (overwrites buffer)
|
||||
*/
|
||||
|
||||
char *mprStrUpper(char *str)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
mprAssert(str);
|
||||
if (str == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (cp = str; *cp; cp++) {
|
||||
if (islower(*cp)) {
|
||||
*cp = (char) toupper(*cp);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Case insensitive string comparison. Stop at the end of str1.
|
||||
*/
|
||||
|
||||
int mprStrcmpAnyCase(const char *str1, const char *str2)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (str1 == 0 || str2 == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (str1 == str2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (rc = 0; *str1 && rc == 0; str1++, str2++) {
|
||||
rc = tolower(*str1) - tolower(*str2);
|
||||
}
|
||||
if (*str2) {
|
||||
return -1;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Case insensitive string comparison. Limited by length
|
||||
*/
|
||||
|
||||
int mprStrcmpAnyCaseCount(const char *str1, const char *str2, int len)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (str1 == 0 || str2 == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (str1 == str2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (rc = 0; len-- > 0 && *str1 && rc == 0; str1++, str2++) {
|
||||
rc = tolower(*str1) - tolower(*str2);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the last portion of a pathname
|
||||
*/
|
||||
|
||||
const char *mprGetBaseName(const char *name)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
cp = strrchr(name, '/');
|
||||
|
||||
if (cp == 0) {
|
||||
cp = strrchr(name, '\\');
|
||||
if (cp == 0) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
if (cp == name) {
|
||||
if (cp[1] == '\0') {
|
||||
return name;
|
||||
}
|
||||
} else {
|
||||
if (cp[1] == '\0') {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return &cp[1];
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the directory portion of a pathname into the users buffer.
|
||||
*/
|
||||
|
||||
char *mprGetDirName(char *buf, int bufsize, const char *path)
|
||||
{
|
||||
char *cp;
|
||||
int dlen;
|
||||
|
||||
mprAssert(path);
|
||||
mprAssert(buf);
|
||||
mprAssert(bufsize > 0);
|
||||
|
||||
cp = strrchr(path, '/');
|
||||
if (cp == 0) {
|
||||
#if WIN
|
||||
cp = strrchr(path, '\\');
|
||||
if (cp == 0)
|
||||
#endif
|
||||
{
|
||||
buf[0] = '\0';
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
if (cp == path && cp[1] == '\0') {
|
||||
strcpy(buf, ".");
|
||||
return buf;
|
||||
}
|
||||
|
||||
dlen = cp - path;
|
||||
if (dlen < bufsize) {
|
||||
if (dlen == 0) {
|
||||
dlen++;
|
||||
}
|
||||
mprMemcpy(buf, bufsize, path, dlen);
|
||||
buf[dlen] = '\0';
|
||||
return buf;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of strtok. Note "str" is modifed as per strtok()
|
||||
*/
|
||||
|
||||
char *mprStrTok(char *str, const char *delim, char **last)
|
||||
{
|
||||
char *start, *end;
|
||||
int i;
|
||||
|
||||
start = str ? str : *last;
|
||||
|
||||
if (start == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
i = strspn(start, delim);
|
||||
start += i;
|
||||
if (*start == '\0') {
|
||||
*last = 0;
|
||||
return 0;
|
||||
}
|
||||
end = strpbrk(start, delim);
|
||||
if (end) {
|
||||
*end++ = '\0';
|
||||
i = strspn(end, delim);
|
||||
end += i;
|
||||
}
|
||||
*last = end;
|
||||
return start;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Split the buffer into word tokens
|
||||
*/
|
||||
|
||||
char *mprGetWordTok(char *buf, int bufsize, const char *str, const char *delim,
|
||||
const char **tok)
|
||||
{
|
||||
const char *start, *end;
|
||||
int i, len;
|
||||
|
||||
start = str ? str : *tok;
|
||||
|
||||
if (start == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
i = strspn(start, delim);
|
||||
start += i;
|
||||
if (*start =='\0') {
|
||||
*tok = 0;
|
||||
return 0;
|
||||
}
|
||||
end = strpbrk(start, delim);
|
||||
if (end) {
|
||||
len = min(end - start, bufsize - 1);
|
||||
mprMemcpy(buf, bufsize, start, len);
|
||||
buf[len] = '\0';
|
||||
} else {
|
||||
if (mprStrcpy(buf, bufsize, start) < 0) {
|
||||
buf[bufsize - 1] = '\0';
|
||||
return 0;
|
||||
}
|
||||
buf[bufsize - 1] = '\0';
|
||||
}
|
||||
*tok = end;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Format a number as a string.
|
||||
*/
|
||||
|
||||
char *mprItoa(char *buf, int size, int value)
|
||||
{
|
||||
char numBuf[16];
|
||||
char *cp, *dp, *endp;
|
||||
int negative;
|
||||
|
||||
cp = &numBuf[sizeof(numBuf)];
|
||||
*--cp = '\0';
|
||||
|
||||
if (value < 0) {
|
||||
negative = 1;
|
||||
value = -value;
|
||||
size--;
|
||||
} else {
|
||||
negative = 0;
|
||||
}
|
||||
|
||||
do {
|
||||
*--cp = '0' + (value % 10);
|
||||
value /= 10;
|
||||
} while (value > 0);
|
||||
|
||||
if (negative) {
|
||||
*--cp = '-';
|
||||
}
|
||||
|
||||
dp = buf;
|
||||
endp = &buf[size];
|
||||
while (dp < endp && *cp) {
|
||||
*dp++ = *cp++;
|
||||
}
|
||||
*dp++ = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Parse an ascii number. Supports radix 10 or 16.
|
||||
*/
|
||||
|
||||
int mprAtoi(const char *str, int radix)
|
||||
{
|
||||
int c, val, negative;
|
||||
|
||||
mprAssert(radix == 10 || radix == 16);
|
||||
|
||||
if (str == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
val = 0;
|
||||
if (radix == 10 && *str == '-') {
|
||||
negative = 1;
|
||||
str++;
|
||||
} else {
|
||||
negative = 0;
|
||||
}
|
||||
|
||||
if (radix == 10) {
|
||||
while (*str && isdigit(*str)) {
|
||||
val = (val * radix) + *str - '0';
|
||||
str++;
|
||||
}
|
||||
} else if (radix == 16) {
|
||||
if (*str == '0' && tolower(str[1]) == 'x') {
|
||||
str += 2;
|
||||
}
|
||||
while (*str) {
|
||||
c = tolower(*str);
|
||||
if (isdigit(c)) {
|
||||
val = (val * radix) + c - '0';
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val = (val * radix) + c - 'a' + 10;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
return (negative) ? -val: val;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Make an argv array. Caller must free by calling mprFree(argv) to free
|
||||
* everything.
|
||||
*/
|
||||
|
||||
int mprMakeArgv(MprCtx ctx, const char *program, const char *cmd,
|
||||
char ***argvp, int *argcp)
|
||||
{
|
||||
char *cp, **argv, *buf, *args;
|
||||
int size, argc;
|
||||
|
||||
/*
|
||||
* Allocate one buffer for argv and the actual args themselves
|
||||
*/
|
||||
size = strlen(cmd) + 1;
|
||||
|
||||
buf = (char*) mprAlloc(ctx, (MPR_MAX_ARGC * sizeof(char*)) + size);
|
||||
if (buf == 0) {
|
||||
return MPR_ERR_MEMORY;
|
||||
}
|
||||
|
||||
args = &buf[MPR_MAX_ARGC * sizeof(char*)];
|
||||
strcpy(args, cmd);
|
||||
argv = (char**) buf;
|
||||
|
||||
argc = 0;
|
||||
if (program) {
|
||||
argv[argc++] = (char*) mprStrdup(ctx, program);
|
||||
}
|
||||
|
||||
for (cp = args; cp && *cp != '\0'; argc++) {
|
||||
if (argc >= MPR_MAX_ARGC) {
|
||||
mprAssert(argc < MPR_MAX_ARGC);
|
||||
mprFree(buf);
|
||||
*argvp = 0;
|
||||
if (argcp) {
|
||||
*argcp = 0;
|
||||
}
|
||||
return MPR_ERR_TOO_MANY;
|
||||
}
|
||||
while (isspace(*cp)) {
|
||||
cp++;
|
||||
}
|
||||
if (*cp == '\0') {
|
||||
break;
|
||||
}
|
||||
if (*cp == '"') {
|
||||
cp++;
|
||||
argv[argc] = cp;
|
||||
while ((*cp != '\0') && (*cp != '"')) {
|
||||
cp++;
|
||||
}
|
||||
} else {
|
||||
argv[argc] = cp;
|
||||
while (*cp != '\0' && !isspace(*cp)) {
|
||||
cp++;
|
||||
}
|
||||
}
|
||||
if (*cp != '\0') {
|
||||
*cp++ = '\0';
|
||||
}
|
||||
}
|
||||
argv[argc] = 0;
|
||||
|
||||
if (argcp) {
|
||||
*argcp = argc;
|
||||
}
|
||||
*argvp = argv;
|
||||
|
||||
return argc;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
* @file mprSym.cpp
|
||||
* @brief Fast hashing symbol table lookup module
|
||||
* @overview This symbol table uses a fast key lookup mechanism. Keys are
|
||||
* strings and the value entries are arbitrary pointers. The keys are
|
||||
* hashed into a series of buckets which then have a chain of hash
|
||||
* entries using the standard doubly linked list classes (List/Link).
|
||||
* The chain in in collating sequence so search time through the chain
|
||||
* is on average (N/hashSize)/2.
|
||||
* @remarks This module is not thread-safe. It is the callers responsibility
|
||||
* to perform all thread synchronization.
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http: *www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http: *www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "mpr.h"
|
||||
|
||||
/**************************** Forward Declarations ****************************/
|
||||
|
||||
static int hashIndex(const char *key, int size);
|
||||
static MprSymbol *lookupInner(int *bucketIndex, MprSymbol **prevSp,
|
||||
MprSymbolTable *table, const char *key);
|
||||
|
||||
/*********************************** Code *************************************/
|
||||
/*
|
||||
* Create a new symbol table of a given size. Caller should provide a size
|
||||
* that is a prime number for the greatest efficiency. Caller should use
|
||||
* mprFree to free the symbol table.
|
||||
*/
|
||||
|
||||
MprSymbolTable *mprCreateSymbolTable(MprCtx ctx, int hashSize)
|
||||
{
|
||||
MprSymbolTable *table;
|
||||
|
||||
table = mprAllocTypeZeroed(ctx, MprSymbolTable);
|
||||
if (table == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (hashSize < MPR_DEFAULT_HASH_SIZE) {
|
||||
hashSize = MPR_DEFAULT_HASH_SIZE;
|
||||
}
|
||||
table->hashSize = hashSize;
|
||||
|
||||
table->count = 0;
|
||||
table->hashSize = hashSize;
|
||||
table->buckets = mprAllocZeroedBlock(MPR_LOC_ARGS(table),
|
||||
sizeof(MprSymbol*) * hashSize);
|
||||
|
||||
if (table->buckets == 0) {
|
||||
mprFree(table);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Insert an entry into the symbol table. If the entry already exists, update
|
||||
* its value. Order of insertion is not preserved.
|
||||
*/
|
||||
|
||||
MprSymbol *mprInsertSymbol(MprSymbolTable *table, const char *key, void *ptr)
|
||||
{
|
||||
MprSymbol *sp, *prevSp;
|
||||
int index;
|
||||
|
||||
sp = lookupInner(&index, &prevSp, table, key);
|
||||
|
||||
if (sp != 0) {
|
||||
/*
|
||||
* Already exists. Just update the data.
|
||||
*/
|
||||
sp->data = ptr;
|
||||
return sp;
|
||||
}
|
||||
|
||||
/*
|
||||
* New entry
|
||||
*/
|
||||
sp = mprAllocTypeZeroed(table, MprSymbol);
|
||||
if (sp == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
sp->data = ptr;
|
||||
sp->key = mprStrdup(sp, key);
|
||||
sp->bucket = index;
|
||||
|
||||
sp->next = table->buckets[index];
|
||||
table->buckets[index] = sp;
|
||||
|
||||
table->count++;
|
||||
return sp;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Remove an entry from the table
|
||||
*/
|
||||
|
||||
int mprRemoveSymbol(MprSymbolTable *table, const char *key)
|
||||
{
|
||||
MprSymbol *sp, *prevSp;
|
||||
int index;
|
||||
|
||||
if ((sp = lookupInner(&index, &prevSp, table, key)) == 0) {
|
||||
return MPR_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (prevSp) {
|
||||
prevSp->next = sp->next;
|
||||
} else {
|
||||
table->buckets[index] = sp->next;
|
||||
}
|
||||
table->count--;
|
||||
|
||||
mprFree(sp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Lookup a key and return the hash entry
|
||||
*/
|
||||
|
||||
void *mprLookupSymbol(MprSymbolTable *table, const char *key)
|
||||
{
|
||||
MprSymbol *sp;
|
||||
|
||||
mprAssert(key);
|
||||
|
||||
sp = lookupInner(0, 0, table, key);
|
||||
if (sp == 0) {
|
||||
return 0;
|
||||
}
|
||||
return sp->data;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static MprSymbol *lookupInner(int *bucketIndex, MprSymbol **prevSp,
|
||||
MprSymbolTable *table, const char *key)
|
||||
{
|
||||
MprSymbol *sp, *prev;
|
||||
int index, rc;
|
||||
|
||||
mprAssert(key);
|
||||
|
||||
index = hashIndex(key, table->hashSize);
|
||||
if (bucketIndex) {
|
||||
*bucketIndex = index;
|
||||
}
|
||||
|
||||
sp = table->buckets[index];
|
||||
prev = 0;
|
||||
|
||||
while (sp) {
|
||||
rc = strcmp(sp->key, key);
|
||||
if (rc == 0) {
|
||||
if (prevSp) {
|
||||
*prevSp = prev;
|
||||
}
|
||||
return sp;
|
||||
}
|
||||
prev = sp;
|
||||
mprAssert(sp != sp->next);
|
||||
sp = sp->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
int mprGetSymbolCount(MprSymbolTable *table)
|
||||
{
|
||||
return table->count;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the first entry in the table.
|
||||
*/
|
||||
|
||||
MprSymbol *mprGetFirstSymTab(MprSymbolTable *table)
|
||||
{
|
||||
MprSymbol *sp;
|
||||
int i;
|
||||
|
||||
mprAssert(table);
|
||||
|
||||
for (i = 0; i < table->hashSize; i++) {
|
||||
if ((sp = (MprSymbol*) table->buckets[i]) != 0) {
|
||||
return sp;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the next entry in the table
|
||||
*/
|
||||
|
||||
MprSymbol *mprGetNextSymTab(MprSymbolTable *table, MprSymbol *last)
|
||||
{
|
||||
MprSymbol *sp;
|
||||
int i;
|
||||
|
||||
mprAssert(table);
|
||||
|
||||
if (last->next) {
|
||||
return last->next;
|
||||
}
|
||||
|
||||
for (i = last->bucket + 1; i < table->hashSize; i++) {
|
||||
if ((sp = (MprSymbol*) table->buckets[i]) != 0) {
|
||||
return sp;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Hash the key to produce a hash index.
|
||||
*/
|
||||
|
||||
static int hashIndex(const char *key, int size)
|
||||
{
|
||||
uint sum;
|
||||
|
||||
sum = 0;
|
||||
while (*key) {
|
||||
sum += (sum * 33) + *key++;
|
||||
}
|
||||
|
||||
return sum % size;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* @file mprUnix.h
|
||||
* @brief Make windows a bit more unix like
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/******************************* Documentation ********************************/
|
||||
|
||||
/*
|
||||
* This header is part of the Mbedthis Portable Runtime and aims to include
|
||||
* all necessary O/S headers and to unify the constants and declarations
|
||||
* required by Mbedthis products. It can be included by C or C++ programs.
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifndef _h_MPR_UNIX
|
||||
#define _h_MPR_UNIX 1
|
||||
|
||||
/******************************************************************************/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define BLD_NO_POSIX_REMAP if these defines mess with your app
|
||||
*/
|
||||
#if WIN && !BLD_NO_POSIX_REMAP
|
||||
/*
|
||||
* MOB -- clashes with ATL
|
||||
*/
|
||||
#define access _access
|
||||
#define close _close
|
||||
#define fileno _fileno
|
||||
#define fstat _fstat
|
||||
#define getpid _getpid
|
||||
#define open _open
|
||||
#define putenv _putenv
|
||||
#define read _read
|
||||
#define stat _stat
|
||||
#define umask _umask
|
||||
#define unlink _unlink
|
||||
#define write _write
|
||||
#define strdup _strdup
|
||||
#define lseek _lseek
|
||||
#define getcwd _getcwd
|
||||
#define chdir _chdir
|
||||
|
||||
#define mkdir(a,b) _mkdir(a)
|
||||
#define rmdir(a) _rmdir(a)
|
||||
|
||||
#define R_OK 4
|
||||
#define W_OK 2
|
||||
#define MPR_TEXT "t"
|
||||
|
||||
extern void srand48(long);
|
||||
extern long lrand48(void);
|
||||
extern long ulimit(int, ...);
|
||||
extern long nap(long);
|
||||
extern int getuid(void);
|
||||
extern int geteuid(void);
|
||||
#endif
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _h_MPR_UNIX */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,141 @@
|
||||
#define BLD_PRODUCT "Samba4"
|
||||
#define BLD_NAME "Samba4 SWAT"
|
||||
#define BLD_VERSION "4"
|
||||
#define BLD_NUMBER "1"
|
||||
#define BLD_TYPE "DEBUG"
|
||||
#define BLD_DEFAULTS "normal"
|
||||
#define BLD_PACKAGES ""
|
||||
#define BLD_APPWEB_CONFIG "normal.conf"
|
||||
#define BLD_APPWEB 0
|
||||
#define BLD_COMPANY "Mbedthis"
|
||||
#define BLD_DEBUG 0
|
||||
#define BLD_DIRS "bootstrap include obj bin mpr ejs esp http doc appWeb appWebSamples images"
|
||||
#define BLD_HTTP_PORT 7777
|
||||
#define BLD_LIB_VERSION "1.0.0"
|
||||
#define BLD_SSL_PORT 4443
|
||||
#define BLD_CLEAN_INSTALL "0"
|
||||
#define BLD_LICENSE "gpl"
|
||||
#define BLD_HOST_SYSTEM "i686-pc-linux-gnu"
|
||||
#define BLD_BUILD_SYSTEM "i686-pc-linux-gnu"
|
||||
#define BLD_HOST_OS "LINUX"
|
||||
#define BLD_HOST_CPU_ARCH MPR_CPU_IX86
|
||||
#define BLD_HOST_CPU "i686"
|
||||
#define BLD_HOST_UNIX 1
|
||||
#define BLD_BUILD_OS "LINUX"
|
||||
#define BLD_BUILD_CPU_ARCH MPR_CPU_IX86
|
||||
#define BLD_BUILD_CPU i686
|
||||
#define BLD_BUILD_UNIX 1
|
||||
#define BLD_ROOT_PREFIX "/"
|
||||
#define BLD_FEATURE_ACCESS_LOG 0
|
||||
#define BLD_FEATURE_ADMIN_MODULE 0
|
||||
#define BLD_FEATURE_ASPNET_MODULE 0
|
||||
#define BLD_FEATURE_ASSERT 1
|
||||
#define BLD_FEATURE_AUTH_MODULE 0
|
||||
#define BLD_FEATURE_C_API_MODULE 1
|
||||
#define BLD_FEATURE_C_API_CLIENT 0
|
||||
#define BLD_FEATURE_CGI_MODULE 0
|
||||
#define BLD_FEATURE_COMPAT_MODULE 0
|
||||
#define BLD_FEATURE_CONFIG_PARSE 0
|
||||
#define BLD_FEATURE_CONFIG_SAVE 0
|
||||
#define BLD_FEATURE_COOKIE 0
|
||||
#define BLD_FEATURE_COPY_MODULE 0
|
||||
#define BLD_FEATURE_DIGEST 0
|
||||
#define BLD_FEATURE_DLL 0
|
||||
#define BLD_FEATURE_EGI_MODULE 0
|
||||
#define BLD_FEATURE_EJS 1
|
||||
#define BLD_FEATURE_ESP_MODULE 1
|
||||
#define BLD_FEATURE_EVAL_PERIOD 30
|
||||
#define BLD_FEATURE_FLOATING_POINT 1
|
||||
#define BLD_FEATURE_IF_MODIFIED 0
|
||||
#define BLD_FEATURE_INT64 1
|
||||
#define BLD_FEATURE_KEEP_ALIVE 0
|
||||
#define BLD_FEATURE_LEGACY_API 0
|
||||
#define BLD_FEATURE_LIB_STDCPP 0
|
||||
#define BLD_FEATURE_LICENSE 0
|
||||
#define BLD_FEATURE_LOG 0
|
||||
#define BLD_FEATURE_MULTITHREAD 0
|
||||
#define BLD_FEATURE_MALLOC 0
|
||||
#define BLD_FEATURE_MALLOC_STATS 0
|
||||
#define BLD_FEATURE_MALLOC_LEAK 0
|
||||
#define BLD_FEATURE_MALLOC_HOOK 0
|
||||
#define BLD_FEATURE_NUM_TYPE int64_t
|
||||
#define BLD_FEATURE_NUM_TYPE_ID MPR_TYPE_INT64
|
||||
#define BLD_FEATURE_ROMFS 0
|
||||
#define BLD_FEATURE_RUN_AS_SERVICE 0
|
||||
#define BLD_FEATURE_SAFE_STRINGS 0
|
||||
#define BLD_FEATURE_SAMPLES 0
|
||||
#define BLD_FEATURE_SESSION 1
|
||||
#define BLD_FEATURE_SHARED 0
|
||||
#define BLD_FEATURE_SQUEEZE 0
|
||||
#define BLD_FEATURE_SSL_MODULE 0
|
||||
#define BLD_FEATURE_STATIC 1
|
||||
#define BLD_FEATURE_STATIC_LINK_LIBC 0
|
||||
#define BLD_FEATURE_TEST 0
|
||||
#define BLD_FEATURE_UPLOAD_MODULE 0
|
||||
#define BLD_FEATURE_XDB_MODULE 0
|
||||
#define BLD_FEATURE_ADMIN_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_ASPNET_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_AUTH_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_C_API_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_CGI_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_COMPAT_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_COPY_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_EGI_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_ESP_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_SSL_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_UPLOAD_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_XDB_MODULE_BUILTIN 0
|
||||
#define BLD_FEATURE_ADMIN_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_ASPNET_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_AUTH_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_C_API_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_CGI_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_COMPAT_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_COPY_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_EGI_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_ESP_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_SSL_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_UPLOAD_MODULE_LOADABLE 0
|
||||
#define BLD_FEATURE_XDB_MODULE_LOADABLE 0
|
||||
#define BLD_AR_FOR_BUILD "ar"
|
||||
#define BLD_CC_FOR_BUILD "cc"
|
||||
#define BLD_CSC_FOR_BUILD ""
|
||||
#define BLD_JAVAC_FOR_BUILD ""
|
||||
#define BLD_LD_FOR_BUILD "ld"
|
||||
#define BLD_RANLIB_FOR_BUILD ""
|
||||
#define BLD_NM_FOR_BUILD "nm"
|
||||
#define BLD_CFLAGS_FOR_BUILD ""
|
||||
#define BLD_IFLAGS_FOR_BUILD ""
|
||||
#define BLD_LDFLAGS_FOR_BUILD ""
|
||||
#define BLD_ARCHIVE_FOR_BUILD ".a"
|
||||
#define BLD_EXE_FOR_BUILD ""
|
||||
#define BLD_OBJ_FOR_BUILD ".o"
|
||||
#define BLD_PIOBJ_FOR_BUILD ".lo"
|
||||
#define BLD_CLASS_FOR_BUILD ".class"
|
||||
#define BLD_SHLIB_FOR_BUILD ""
|
||||
#define BLD_SHOBJ_FOR_BUILD ".so"
|
||||
#define BLD_AR_FOR_HOST "ar"
|
||||
#define BLD_CC_FOR_HOST "cc"
|
||||
#define BLD_CSC_FOR_HOST "csc"
|
||||
#define BLD_JAVAC_FOR_HOST "javac"
|
||||
#define BLD_LD_FOR_HOST "ld"
|
||||
#define BLD_RANLIB_FOR_HOST "true"
|
||||
#define BLD_NM_FOR_HOST "nm"
|
||||
#define BLD_CFLAGS_FOR_HOST ""
|
||||
#define BLD_IFLAGS_FOR_HOST ""
|
||||
#define BLD_LDFLAGS_FOR_HOST ""
|
||||
#define BLD_ARCHIVE_FOR_HOST ".a"
|
||||
#define BLD_EXE_FOR_HOST ""
|
||||
#define BLD_OBJ_FOR_HOST ".o"
|
||||
#define BLD_PIOBJ_FOR_HOST ".lo"
|
||||
#define BLD_CLASS_FOR_HOST ".class"
|
||||
#define BLD_SHLIB_FOR_HOST ""
|
||||
#define BLD_SHOBJ_FOR_HOST ".so"
|
||||
#define BLD_TOOLS_DIR "${BLD_TOP}/bin"
|
||||
#define BLD_BIN_DIR "${BLD_TOP}/bin"
|
||||
#define BLD_INC_DIR "/usr/include/${BLD_PRODUCT}"
|
||||
#define BLD_EXP_OBJ_DIR "${BLD_TOP}/obj"
|
||||
|
||||
#ifndef MAX_FLOAT
|
||||
#define MAX_FLOAT 3.40282347e+38F
|
||||
#endif
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* @file ejs.h
|
||||
* @brief Primary Embedded Javascript (ECMAScript) header.
|
||||
* @overview This Embedded Javascript (EJS) header defines the
|
||||
* public API. This API should only be used by those directly
|
||||
* using EJS without using Embedded Server Pages (ESP). ESP
|
||||
* wraps all relevant APIs to expose a single consistent API.
|
||||
* \n\n
|
||||
* This API requires the mpr/var.h facilities to create and
|
||||
* manage objects and properties.
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default.g
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
* Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#ifndef _h_EJS
|
||||
#define _h_EJS 1
|
||||
|
||||
#include "lib/appweb/mpr/miniMpr.h"
|
||||
#include "lib/appweb/mpr/var.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
|
||||
typedef MprVarHandle EjsId;
|
||||
typedef MprVarHandle EjsHandle;
|
||||
|
||||
/*
|
||||
* Multithreaded lock routines
|
||||
*/
|
||||
typedef void (*EjsLock)(void *lockData);
|
||||
typedef void (*EjsUnlock)(void *lockData);
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
/*
|
||||
* Module management
|
||||
*/
|
||||
extern int ejsOpen(EjsLock lock, EjsUnlock unlock, void *lockData);
|
||||
extern void ejsClose(void);
|
||||
extern EjsId ejsOpenEngine(EjsHandle primaryHandle, EjsHandle altHandle);
|
||||
extern void ejsCloseEngine(EjsId eid);
|
||||
|
||||
void *ejs_save_state(void);
|
||||
void ejs_restore_state(void *ptr);
|
||||
|
||||
/*
|
||||
* Evaluation functions
|
||||
*/
|
||||
extern int ejsEvalFile(EjsId eid, char *path, MprVar *result, char **emsg);
|
||||
extern int ejsEvalScript(EjsId eid, char *script, MprVar *result,
|
||||
char **emsg);
|
||||
extern int ejsRunFunction(int eid, MprVar *obj, const char *functionName,
|
||||
MprArray *args);
|
||||
|
||||
/*
|
||||
* Composite variable get / set routines. Can also use the MPR property
|
||||
* routines on an object variable.
|
||||
*/
|
||||
extern MprVar ejsCreateObj(const char *name, int hashSize);
|
||||
extern MprVar ejsCreateArray(const char *name, int hashSize);
|
||||
extern bool ejsDestroyVar(MprVar *obj);
|
||||
extern int ejsCopyVar(EjsId eid, const char *var, MprVar *value,
|
||||
bool copyRef);
|
||||
extern int ejsReadVar(EjsId eid, const char *var, MprVar *value);
|
||||
extern int ejsWriteVar(EjsId eid, const char *var, MprVar *value);
|
||||
extern int ejsWriteVarValue(EjsId eid, const char *var, MprVar value);
|
||||
extern int ejsDeleteVar(EjsId eid, const char *var);
|
||||
|
||||
extern MprVar *ejsGetLocalObject(EjsId eid);
|
||||
extern MprVar *ejsGetGlobalObject(EjsId eid);
|
||||
|
||||
/*
|
||||
* Function routines
|
||||
*/
|
||||
extern void ejsDefineFunction(EjsId eid, const char *functionName,
|
||||
char *args, char *body);
|
||||
extern void ejsDefineCFunction(EjsId eid, const char *functionName,
|
||||
MprCFunction fn, void *thisPtr, int flags);
|
||||
extern void ejsDefineStringCFunction(EjsId eid, const char *functionName,
|
||||
MprStringCFunction fn, void *thisPtr, int flags);
|
||||
extern void *ejsGetThisPtr(EjsId eid);
|
||||
extern MprVar *ejsGetReturnValue(EjsId eid);
|
||||
extern int ejsGetLineNumber(EjsId eid);
|
||||
extern int ejsParseArgs(int argc, char **argv, char *fmt, ...);
|
||||
extern void ejsSetErrorMsg(EjsId eid, const char* fmt, ...)
|
||||
PRINTF_ATTRIBUTE(2,3);
|
||||
extern void ejsSetReturnValue(EjsId eid, MprVar value);
|
||||
extern void ejsSetReturnString(EjsId eid, const char *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _h_EJS */
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* @file ejsInternal.h
|
||||
* @brief Private header for Embedded Javascript (ECMAScript)
|
||||
* @overview This Embedded Javascript header defines the private Embedded
|
||||
* Javascript internal structures.
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default.g
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
* Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#ifndef _h_EJS_INTERNAL
|
||||
#define _h_EJS_INTERNAL 1
|
||||
|
||||
#include "ejs.h"
|
||||
|
||||
/********************************** Defines ***********************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define EJS_PARSE_INCR 256 /* Growth factor */
|
||||
#define EJS_MAX_RECURSE 25 /* Sanity for maximum recursion */
|
||||
#define EJS_MAX_ID 128 /* Maximum ID length */
|
||||
#define EJS_OBJ_HASH_SIZE 13 /* Object hash table size */
|
||||
#define EJS_SMALL_OBJ_HASH_SIZE 11 /* Small object hash size */
|
||||
#define EJS_LIST_INCR 8 /* Growth increment for lists */
|
||||
#else
|
||||
#define EJS_PARSE_INCR 1024 /* Growth factor */
|
||||
#define EJS_MAX_RECURSE 100 /* Sanity for maximum recursion */
|
||||
#define EJS_MAX_ID 256 /* Maximum ID length */
|
||||
#define EJS_OBJ_HASH_SIZE 29 /* Object hash table size */
|
||||
#define EJS_SMALL_OBJ_HASH_SIZE 11 /* Small object hash size */
|
||||
#define EJS_LIST_INCR 16 /* Growth increment for lists */
|
||||
#endif
|
||||
#define EJS_TOKEN_STACK 4 /* Put back token stack */
|
||||
|
||||
/*
|
||||
* Lexical analyser tokens
|
||||
*/
|
||||
#define EJS_TOK_ERR -1 /* Any error */
|
||||
#define EJS_TOK_LPAREN 1 /* ( */
|
||||
#define EJS_TOK_RPAREN 2 /* ) */
|
||||
#define EJS_TOK_IF 3 /* if */
|
||||
#define EJS_TOK_ELSE 4 /* else */
|
||||
#define EJS_TOK_LBRACE 5 /* { */
|
||||
#define EJS_TOK_RBRACE 6 /* } */
|
||||
#define EJS_TOK_LOGICAL 7 /* ||, &&, ! */
|
||||
#define EJS_TOK_EXPR 8 /* +, -, /, % */
|
||||
#define EJS_TOK_SEMI 9 /* ; */
|
||||
#define EJS_TOK_LITERAL 10 /* literal string */
|
||||
#define EJS_TOK_FUNCTION_NAME 11 /* functionName */
|
||||
#define EJS_TOK_NEWLINE 12 /* newline white space */
|
||||
#define EJS_TOK_ID 13 /* Identifier */
|
||||
#define EJS_TOK_EOF 14 /* End of script */
|
||||
#define EJS_TOK_COMMA 15 /* Comma */
|
||||
#define EJS_TOK_VAR 16 /* var */
|
||||
#define EJS_TOK_ASSIGNMENT 17 /* = */
|
||||
#define EJS_TOK_FOR 18 /* for */
|
||||
#define EJS_TOK_INC_DEC 19 /* ++, -- */
|
||||
#define EJS_TOK_RETURN 20 /* return */
|
||||
#define EJS_TOK_PERIOD 21 /* . */
|
||||
#define EJS_TOK_LBRACKET 22 /* [ */
|
||||
#define EJS_TOK_RBRACKET 23 /* ] */
|
||||
#define EJS_TOK_NEW 24 /* new */
|
||||
#define EJS_TOK_DELETE 25 /* delete */
|
||||
#define EJS_TOK_IN 26 /* in */
|
||||
#define EJS_TOK_FUNCTION 27 /* function */
|
||||
#define EJS_TOK_NUMBER 28 /* Number */
|
||||
|
||||
/*
|
||||
* Expression operators
|
||||
*/
|
||||
#define EJS_EXPR_LESS 1 /* < */
|
||||
#define EJS_EXPR_LESSEQ 2 /* <= */
|
||||
#define EJS_EXPR_GREATER 3 /* > */
|
||||
#define EJS_EXPR_GREATEREQ 4 /* >= */
|
||||
#define EJS_EXPR_EQ 5 /* == */
|
||||
#define EJS_EXPR_NOTEQ 6 /* != */
|
||||
#define EJS_EXPR_PLUS 7 /* + */
|
||||
#define EJS_EXPR_MINUS 8 /* - */
|
||||
#define EJS_EXPR_DIV 9 /* / */
|
||||
#define EJS_EXPR_MOD 10 /* % */
|
||||
#define EJS_EXPR_LSHIFT 11 /* << */
|
||||
#define EJS_EXPR_RSHIFT 12 /* >> */
|
||||
#define EJS_EXPR_MUL 13 /* * */
|
||||
#define EJS_EXPR_ASSIGNMENT 14 /* = */
|
||||
#define EJS_EXPR_INC 15 /* ++ */
|
||||
#define EJS_EXPR_DEC 16 /* -- */
|
||||
#define EJS_EXPR_BOOL_COMP 17 /* ! */
|
||||
|
||||
/*
|
||||
* Conditional operators
|
||||
*/
|
||||
#define EJS_COND_AND 1 /* && */
|
||||
#define EJS_COND_OR 2 /* || */
|
||||
#define EJS_COND_NOT 3 /* ! */
|
||||
|
||||
/*
|
||||
* States
|
||||
*/
|
||||
#define EJS_STATE_ERR -1 /* Error state */
|
||||
#define EJS_STATE_EOF 1 /* End of file */
|
||||
#define EJS_STATE_COND 2 /* Parsing a "(conditional)" stmt */
|
||||
#define EJS_STATE_COND_DONE 3
|
||||
#define EJS_STATE_RELEXP 4 /* Parsing a relational expr */
|
||||
#define EJS_STATE_RELEXP_DONE 5
|
||||
#define EJS_STATE_EXPR 6 /* Parsing an expression */
|
||||
#define EJS_STATE_EXPR_DONE 7
|
||||
#define EJS_STATE_STMT 8 /* Parsing General statement */
|
||||
#define EJS_STATE_STMT_DONE 9
|
||||
#define EJS_STATE_STMT_BLOCK_DONE 10 /* End of block "}" */
|
||||
#define EJS_STATE_ARG_LIST 11 /* Function arg list */
|
||||
#define EJS_STATE_ARG_LIST_DONE 12
|
||||
#define EJS_STATE_DEC_LIST 16 /* Declaration list */
|
||||
#define EJS_STATE_DEC_LIST_DONE 17
|
||||
#define EJS_STATE_DEC 18 /* Declaration statement */
|
||||
#define EJS_STATE_DEC_DONE 19
|
||||
#define EJS_STATE_RET 20 /* Return statement */
|
||||
|
||||
#define EJS_STATE_BEGIN EJS_STATE_STMT
|
||||
|
||||
/*
|
||||
* General parsing flags.
|
||||
*/
|
||||
#define EJS_FLAGS_EXE 0x1 /* Execute statements */
|
||||
#define EJS_FLAGS_LOCAL 0x2 /* Get local vars only */
|
||||
#define EJS_FLAGS_GLOBAL 0x4 /* Get global vars only */
|
||||
#define EJS_FLAGS_CREATE 0x8 /* Create var */
|
||||
#define EJS_FLAGS_ASSIGNMENT 0x10 /* In assignment stmt */
|
||||
#define EJS_FLAGS_DELETE 0x20 /* Deleting a variable */
|
||||
#define EJS_FLAGS_FOREACH 0x40 /* In foreach */
|
||||
#define EJS_FLAGS_NEW 0x80 /* In a new stmt() */
|
||||
#define EJS_FLAGS_EXIT 0x100 /* Must exit */
|
||||
|
||||
/*
|
||||
* Putback token
|
||||
*/
|
||||
|
||||
typedef struct EjsToken {
|
||||
char *token; /* Token string */
|
||||
int id; /* Token ID */
|
||||
} EjsToken;
|
||||
|
||||
/*
|
||||
* EJ evaluation block structure
|
||||
*/
|
||||
typedef struct ejEval {
|
||||
EjsToken putBack[EJS_TOKEN_STACK]; /* Put back token stack */
|
||||
int putBackIndex; /* Top of stack index */
|
||||
MprStr line; /* Current line */
|
||||
int lineLength; /* Current line length */
|
||||
int lineNumber; /* Parse line number */
|
||||
int lineColumn; /* Column in line */
|
||||
MprStr script; /* Input script for parsing */
|
||||
char *scriptServp; /* Next token in the script */
|
||||
int scriptSize; /* Length of script */
|
||||
MprStr tokbuf; /* Current token */
|
||||
char *tokEndp; /* Pointer past end of token */
|
||||
char *tokServp; /* Pointer to next token char */
|
||||
int tokSize; /* Size of token buffer */
|
||||
struct ejEval *next; /* used for backtraces */
|
||||
const char *procName; /* gives name in backtrace */
|
||||
} EjsInput;
|
||||
|
||||
/*
|
||||
* Function call structure
|
||||
*/
|
||||
typedef struct {
|
||||
MprArray *args; /* Args for function */
|
||||
MprVar *fn; /* Function definition */
|
||||
char *procName; /* Function name */
|
||||
} EjsProc;
|
||||
|
||||
/*
|
||||
* Per EJS structure
|
||||
*/
|
||||
typedef struct ej {
|
||||
EjsHandle altHandle; /* alternate callback handle */
|
||||
MprVar *currentObj; /* Ptr to current object */
|
||||
MprVar *currentProperty; /* Ptr to current property */
|
||||
EjsId eid; /* Halloc handle */
|
||||
char *error; /* Error message */
|
||||
int exitStatus; /* Status to exit() */
|
||||
int flags; /* Flags */
|
||||
MprArray *frames; /* List of variable frames */
|
||||
MprVar *global; /* Global object */
|
||||
EjsInput *input; /* Input evaluation block */
|
||||
MprVar *local; /* Local object */
|
||||
EjsHandle primaryHandle; /* primary callback handle */
|
||||
EjsProc *proc; /* Current function */
|
||||
MprVar result; /* Variable result */
|
||||
void *thisPtr; /* C++ ptr for functions */
|
||||
int tid; /* Current token id */
|
||||
char *token; /* Pointer to token string */
|
||||
MprVar tokenNumber; /* Parsed number */
|
||||
} Ejs;
|
||||
|
||||
typedef int EjsBlock; /* Scope block id */
|
||||
|
||||
/*
|
||||
* Function callback when using Alternate handles.
|
||||
*/
|
||||
typedef int (*EjsAltStringCFunction)(EjsHandle userHandle, EjsHandle altHandle,
|
||||
int argc, char **argv);
|
||||
typedef int (*EjsAltCFunction)(EjsHandle userHandle, EjsHandle altHandle,
|
||||
int argc, MprVar **argv);
|
||||
|
||||
/******************************** Prototypes **********************************/
|
||||
/*
|
||||
* Ejs Lex
|
||||
*/
|
||||
extern int ejsLexOpenScript(Ejs* ep, char *script);
|
||||
extern void ejsLexCloseScript(Ejs* ep);
|
||||
extern int ejsInitInputState(EjsInput *ip);
|
||||
extern void ejsLexSaveInputState(Ejs* ep, EjsInput* state);
|
||||
extern void ejsLexFreeInputState(Ejs* ep, EjsInput* state);
|
||||
extern void ejsLexRestoreInputState(Ejs* ep, EjsInput* state);
|
||||
extern int ejsLexGetToken(Ejs* ep, int state);
|
||||
extern void ejsLexPutbackToken(Ejs* ep, int tid, char *string);
|
||||
|
||||
/*
|
||||
* Parsing
|
||||
*/
|
||||
extern MprVar *ejsFindObj(Ejs *ep, int state, const char *property,
|
||||
int flags);
|
||||
extern MprVar *ejsFindProperty(Ejs *ep, int state, MprVar *obj,
|
||||
char *property, int flags);
|
||||
extern int ejsGetVarCore(Ejs *ep, const char *var, MprVar **obj,
|
||||
MprVar **varValue, int flags);
|
||||
extern int ejsParse(Ejs *ep, int state, int flags);
|
||||
extern Ejs *ejsPtr(EjsId eid);
|
||||
extern void ejsSetExitStatus(int eid, int status);
|
||||
extern void ejsSetFlags(int orFlags, int andFlags);
|
||||
|
||||
/*
|
||||
* Create variable scope blocks
|
||||
*/
|
||||
extern EjsBlock ejsOpenBlock(EjsId eid);
|
||||
extern int ejsCloseBlock(EjsId eid, EjsBlock vid);
|
||||
extern int ejsEvalBlock(EjsId eid, char *script, MprVar *v, char **err);
|
||||
extern int ejsDefineStandardProperties(MprVar *objVar);
|
||||
|
||||
/*
|
||||
* Error handling
|
||||
*/
|
||||
extern void ejsError(Ejs *ep, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _h_EJS_INTERNAL */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,923 @@
|
||||
/*
|
||||
* @file ejsLex.c
|
||||
* @brief EJS Lexical Analyser
|
||||
* @overview EJS lexical analyser. This implementes a lexical analyser
|
||||
* for a subset of the JavaScript language.
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default.g
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
* Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejsInternal.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
|
||||
static int getLexicalToken(Ejs *ep, int state);
|
||||
static int tokenAddChar(Ejs *ep, int c);
|
||||
static int inputGetc(Ejs *ep);
|
||||
static void inputPutback(Ejs *ep, int c);
|
||||
static int charConvert(Ejs *ep, int base, int maxDig);
|
||||
|
||||
/************************************* Code ***********************************/
|
||||
/*
|
||||
* Open a new input script
|
||||
*/
|
||||
|
||||
int ejsLexOpenScript(Ejs *ep, char *script)
|
||||
{
|
||||
EjsInput *ip;
|
||||
|
||||
mprAssert(ep);
|
||||
mprAssert(script);
|
||||
|
||||
if ((ip = mprMalloc(sizeof(EjsInput))) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(ip, 0, sizeof(*ip));
|
||||
ip->next = ep->input;
|
||||
ep->input = ip;
|
||||
ip->procName = ep->proc?ep->proc->procName:NULL;
|
||||
|
||||
/*
|
||||
* Create the parse token buffer and script buffer
|
||||
*/
|
||||
ip->tokbuf = mprMalloc(EJS_PARSE_INCR);
|
||||
ip->tokSize = EJS_PARSE_INCR;
|
||||
ip->tokServp = ip->tokbuf;
|
||||
ip->tokEndp = ip->tokbuf;
|
||||
|
||||
ip->script = mprStrdup(script);
|
||||
ip->scriptSize = strlen(script);
|
||||
ip->scriptServp = ip->script;
|
||||
|
||||
ip->lineNumber = 1;
|
||||
ip->lineLength = 0;
|
||||
ip->lineColumn = 0;
|
||||
ip->line = NULL;
|
||||
|
||||
ip->putBackIndex = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Close the input script
|
||||
*/
|
||||
|
||||
void ejsLexCloseScript(Ejs *ep)
|
||||
{
|
||||
EjsInput *ip;
|
||||
int i;
|
||||
|
||||
mprAssert(ep);
|
||||
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
ep->input = ip->next;
|
||||
|
||||
for (i = 0; i < EJS_TOKEN_STACK; i++) {
|
||||
mprFree(ip->putBack[i].token);
|
||||
ip->putBack[i].token = 0;
|
||||
}
|
||||
|
||||
mprFree(ip->line);
|
||||
mprFree(ip->tokbuf);
|
||||
mprFree(ip->script);
|
||||
|
||||
mprFree(ip);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Initialize an input state structure
|
||||
*/
|
||||
|
||||
int ejsInitInputState(EjsInput *ip)
|
||||
{
|
||||
mprAssert(ip);
|
||||
|
||||
memset(ip, 0, sizeof(*ip));
|
||||
ip->putBackIndex = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Save the input state
|
||||
*/
|
||||
|
||||
void ejsLexSaveInputState(Ejs *ep, EjsInput *state)
|
||||
{
|
||||
EjsInput *ip;
|
||||
int i;
|
||||
|
||||
mprAssert(ep);
|
||||
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
|
||||
*state = *ip;
|
||||
|
||||
for (i = 0; i < ip->putBackIndex; i++) {
|
||||
state->putBack[i].token = mprStrdup(ip->putBack[i].token);
|
||||
state->putBack[i].id = ip->putBack[i].id;
|
||||
}
|
||||
for (; i < EJS_TOKEN_STACK; i++) {
|
||||
state->putBack[i].token = 0;
|
||||
}
|
||||
|
||||
state->line = mprMalloc(ip->lineLength);
|
||||
mprStrcpy(state->line, ip->lineLength, ip->line);
|
||||
|
||||
state->lineColumn = ip->lineColumn;
|
||||
state->lineNumber = ip->lineNumber;
|
||||
state->lineLength = ip->lineLength;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Restore the input state
|
||||
*/
|
||||
|
||||
void ejsLexRestoreInputState(Ejs *ep, EjsInput *state)
|
||||
{
|
||||
EjsInput *ip;
|
||||
int i;
|
||||
|
||||
mprAssert(ep);
|
||||
mprAssert(state);
|
||||
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
|
||||
ip->tokbuf = state->tokbuf;
|
||||
ip->tokServp = state->tokServp;
|
||||
ip->tokEndp = state->tokEndp;
|
||||
ip->tokSize = state->tokSize;
|
||||
|
||||
ip->script = state->script;
|
||||
ip->scriptServp = state->scriptServp;
|
||||
ip->scriptSize = state->scriptSize;
|
||||
|
||||
ip->putBackIndex = state->putBackIndex;
|
||||
for (i = 0; i < ip->putBackIndex; i++) {
|
||||
mprFree(ip->putBack[i].token);
|
||||
ip->putBack[i].id = state->putBack[i].id;
|
||||
ip->putBack[i].token = mprStrdup(state->putBack[i].token);
|
||||
}
|
||||
|
||||
mprFree(ip->line);
|
||||
ip->line = mprMalloc(state->lineLength);
|
||||
mprStrcpy(ip->line, state->lineLength, state->line);
|
||||
|
||||
ip->lineColumn = state->lineColumn;
|
||||
ip->lineNumber = state->lineNumber;
|
||||
ip->lineLength = state->lineLength;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Free a saved input state
|
||||
*/
|
||||
|
||||
void ejsLexFreeInputState(Ejs *ep, EjsInput *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
mprAssert(ep);
|
||||
mprAssert(state);
|
||||
|
||||
for (i = 0; i < EJS_TOKEN_STACK; i++) {
|
||||
mprFree(state->putBack[i].token);
|
||||
}
|
||||
state->putBackIndex = -1;
|
||||
mprFree(state->line);
|
||||
state->lineLength = 0;
|
||||
state->lineColumn = 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get the next EJS token
|
||||
*/
|
||||
|
||||
int ejsLexGetToken(Ejs *ep, int state)
|
||||
{
|
||||
mprAssert(ep);
|
||||
|
||||
ep->tid = getLexicalToken(ep, state);
|
||||
return ep->tid;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
/*
|
||||
* Check for reserved words "if", "else", "var", "for", "foreach",
|
||||
* "delete", "function", and "return". "new", "in" and "function"
|
||||
* done below. "true", "false", "null", "undefined" are handled
|
||||
* as global objects.
|
||||
*
|
||||
* Other reserved words not supported:
|
||||
* "break", "case", "catch", "continue", "default", "do",
|
||||
* "finally", "instanceof", "switch", "this", "throw", "try",
|
||||
* "typeof", "while", "with"
|
||||
*
|
||||
* ECMA extensions reserved words (not supported):
|
||||
* "abstract", "boolean", "byte", "char", "class", "const",
|
||||
* "debugger", "double", "enum", "export", "extends",
|
||||
* "final", "float", "goto", "implements", "import", "int",
|
||||
* "interface", "long", "native", "package", "private",
|
||||
* "protected", "public", "short", "static", "super",
|
||||
* "synchronized", "throws", "transient", "volatile"
|
||||
*/
|
||||
|
||||
static int checkReservedWord(Ejs *ep, int state, int c, int tid)
|
||||
{
|
||||
if (state == EJS_STATE_STMT) {
|
||||
if (strcmp(ep->token, "if") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_IF;
|
||||
} else if (strcmp(ep->token, "else") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_ELSE;
|
||||
} else if (strcmp(ep->token, "var") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_VAR;
|
||||
} else if (strcmp(ep->token, "for") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_FOR;
|
||||
} else if (strcmp(ep->token, "delete") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_DELETE;
|
||||
} else if (strcmp(ep->token, "function") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_FUNCTION;
|
||||
} else if (strcmp(ep->token, "return") == 0) {
|
||||
if ((c == ';') || (c == '(')) {
|
||||
inputPutback(ep, c);
|
||||
}
|
||||
return EJS_TOK_RETURN;
|
||||
}
|
||||
} else if (state == EJS_STATE_EXPR) {
|
||||
if (strcmp(ep->token, "new") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_NEW;
|
||||
} else if (strcmp(ep->token, "in") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_IN;
|
||||
} else if (strcmp(ep->token, "function") == 0) {
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_FUNCTION;
|
||||
}
|
||||
}
|
||||
return tid;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get the next EJS token
|
||||
*/
|
||||
|
||||
static int getLexicalToken(Ejs *ep, int state)
|
||||
{
|
||||
MprType type;
|
||||
EjsInput *ip;
|
||||
int done, tid, c, quote, style, idx;
|
||||
|
||||
mprAssert(ep);
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
|
||||
ep->tid = -1;
|
||||
tid = -1;
|
||||
type = BLD_FEATURE_NUM_TYPE_ID;
|
||||
|
||||
/*
|
||||
* Use a putback tokens first. Don't free strings as caller needs access.
|
||||
*/
|
||||
if (ip->putBackIndex >= 0) {
|
||||
idx = ip->putBackIndex;
|
||||
tid = ip->putBack[idx].id;
|
||||
ep->token = (char*) ip->putBack[idx].token;
|
||||
tid = checkReservedWord(ep, state, 0, tid);
|
||||
ip->putBackIndex--;
|
||||
return tid;
|
||||
}
|
||||
ep->token = ip->tokServp = ip->tokEndp = ip->tokbuf;
|
||||
*ip->tokServp = '\0';
|
||||
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
return EJS_TOK_EOF;
|
||||
}
|
||||
|
||||
/*
|
||||
* Main lexical analyser
|
||||
*/
|
||||
for (done = 0; !done; ) {
|
||||
switch (c) {
|
||||
case -1:
|
||||
return EJS_TOK_EOF;
|
||||
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
do {
|
||||
if ((c = inputGetc(ep)) < 0)
|
||||
break;
|
||||
} while (c == ' ' || c == '\t' || c == '\r');
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
return EJS_TOK_NEWLINE;
|
||||
|
||||
case '(':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_LPAREN;
|
||||
|
||||
case ')':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_RPAREN;
|
||||
|
||||
case '[':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_LBRACKET;
|
||||
|
||||
case ']':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_RBRACKET;
|
||||
|
||||
case '.':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_PERIOD;
|
||||
|
||||
case '{':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_LBRACE;
|
||||
|
||||
case '}':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_RBRACE;
|
||||
|
||||
case '+':
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c != '+' ) {
|
||||
inputPutback(ep, c);
|
||||
tokenAddChar(ep, EJS_EXPR_PLUS);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_EXPR_INC);
|
||||
return EJS_TOK_INC_DEC;
|
||||
|
||||
case '-':
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c != '-' ) {
|
||||
inputPutback(ep, c);
|
||||
tokenAddChar(ep, EJS_EXPR_MINUS);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_EXPR_DEC);
|
||||
return EJS_TOK_INC_DEC;
|
||||
|
||||
case '*':
|
||||
tokenAddChar(ep, EJS_EXPR_MUL);
|
||||
return EJS_TOK_EXPR;
|
||||
|
||||
case '%':
|
||||
tokenAddChar(ep, EJS_EXPR_MOD);
|
||||
return EJS_TOK_EXPR;
|
||||
|
||||
case '/':
|
||||
/*
|
||||
* Handle the division operator and comments
|
||||
*/
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c != '*' && c != '/') {
|
||||
inputPutback(ep, c);
|
||||
tokenAddChar(ep, EJS_EXPR_DIV);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
style = c;
|
||||
/*
|
||||
* Eat comments. Both C and C++ comment styles are supported.
|
||||
*/
|
||||
while (1) {
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
if (style == '/') {
|
||||
return EJS_TOK_EOF;
|
||||
}
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c == '\n' && style == '/') {
|
||||
break;
|
||||
} else if (c == '*') {
|
||||
c = inputGetc(ep);
|
||||
if (style == '/') {
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (c == '/') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Continue looking for a token, so get the next character
|
||||
*/
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
return EJS_TOK_EOF;
|
||||
}
|
||||
break;
|
||||
|
||||
case '<': /* < and <= */
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c == '<') {
|
||||
tokenAddChar(ep, EJS_EXPR_LSHIFT);
|
||||
return EJS_TOK_EXPR;
|
||||
} else if (c == '=') {
|
||||
tokenAddChar(ep, EJS_EXPR_LESSEQ);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_EXPR_LESS);
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_EXPR;
|
||||
|
||||
case '>': /* > and >= */
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c == '>') {
|
||||
tokenAddChar(ep, EJS_EXPR_RSHIFT);
|
||||
return EJS_TOK_EXPR;
|
||||
} else if (c == '=') {
|
||||
tokenAddChar(ep, EJS_EXPR_GREATEREQ);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_EXPR_GREATER);
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_EXPR;
|
||||
|
||||
case '=': /* "==" */
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c == '=') {
|
||||
tokenAddChar(ep, EJS_EXPR_EQ);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_ASSIGNMENT;
|
||||
|
||||
case '!': /* "!=" or "!"*/
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if (c == '=') {
|
||||
tokenAddChar(ep, EJS_EXPR_NOTEQ);
|
||||
return EJS_TOK_EXPR;
|
||||
}
|
||||
inputPutback(ep, c);
|
||||
tokenAddChar(ep, EJS_EXPR_BOOL_COMP);
|
||||
return EJS_TOK_EXPR;
|
||||
|
||||
case ';':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_SEMI;
|
||||
|
||||
case ',':
|
||||
tokenAddChar(ep, c);
|
||||
return EJS_TOK_COMMA;
|
||||
|
||||
case '|': /* "||" */
|
||||
if ((c = inputGetc(ep)) < 0 || c != '|') {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_COND_OR);
|
||||
return EJS_TOK_LOGICAL;
|
||||
|
||||
case '&': /* "&&" */
|
||||
if ((c = inputGetc(ep)) < 0 || c != '&') {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
tokenAddChar(ep, EJS_COND_AND);
|
||||
return EJS_TOK_LOGICAL;
|
||||
|
||||
case '\"': /* String quote */
|
||||
case '\'':
|
||||
quote = c;
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Syntax Error");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
|
||||
while (c != quote) {
|
||||
/*
|
||||
* Check for escape sequence characters
|
||||
*/
|
||||
if (c == '\\') {
|
||||
c = inputGetc(ep);
|
||||
|
||||
if (isdigit(c)) {
|
||||
/*
|
||||
* Octal support, \101 maps to 65 = 'A'. Put first
|
||||
* char back so converter will work properly.
|
||||
*/
|
||||
inputPutback(ep, c);
|
||||
c = charConvert(ep, 8, 3);
|
||||
|
||||
} else {
|
||||
switch (c) {
|
||||
case 'n':
|
||||
c = '\n'; break;
|
||||
case 'b':
|
||||
c = '\b'; break;
|
||||
case 'f':
|
||||
c = '\f'; break;
|
||||
case 'r':
|
||||
c = '\r'; break;
|
||||
case 't':
|
||||
c = '\t'; break;
|
||||
case 'x':
|
||||
/*
|
||||
* Hex support, \x41 maps to 65 = 'A'
|
||||
*/
|
||||
c = charConvert(ep, 16, 2);
|
||||
break;
|
||||
case 'u':
|
||||
/*
|
||||
* Unicode support, \x0401 maps to 65 = 'A'
|
||||
*/
|
||||
c = charConvert(ep, 16, 2);
|
||||
c = c*16 + charConvert(ep, 16, 2);
|
||||
|
||||
break;
|
||||
case '\'':
|
||||
case '\"':
|
||||
case '\\':
|
||||
break;
|
||||
default:
|
||||
ejsError(ep, "Invalid Escape Sequence");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
}
|
||||
if (tokenAddChar(ep, c) < 0) {
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
} else {
|
||||
if (tokenAddChar(ep, c) < 0) {
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
}
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
ejsError(ep, "Unmatched Quote");
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
}
|
||||
return EJS_TOK_LITERAL;
|
||||
|
||||
case '0':
|
||||
if (tokenAddChar(ep, c) < 0) {
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
if (tolower(c) == 'x') {
|
||||
do {
|
||||
if (tokenAddChar(ep, c) < 0) {
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
} while (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f'));
|
||||
|
||||
mprDestroyVar(&ep->tokenNumber);
|
||||
ep->tokenNumber = mprParseVar(ep->token, type);
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_NUMBER;
|
||||
}
|
||||
if (! isdigit(c)) {
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
if (c == '.' || tolower(c) == 'e' || c == '+' || c == '-') {
|
||||
/* Fall through */
|
||||
type = MPR_TYPE_FLOAT;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
mprDestroyVar(&ep->tokenNumber);
|
||||
ep->tokenNumber = mprParseVar(ep->token, type);
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_NUMBER;
|
||||
}
|
||||
}
|
||||
/* Fall through to get more digits */
|
||||
|
||||
case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
do {
|
||||
if (tokenAddChar(ep, c) < 0) {
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
if (c == '.' || tolower(c) == 'e' || tolower(c) == 'f') {
|
||||
type = MPR_TYPE_FLOAT;
|
||||
}
|
||||
} while (isdigit(c) || c == '.' || tolower(c) == 'e' || tolower(c) == 'f' ||
|
||||
((type == MPR_TYPE_FLOAT) && (c == '+' || c == '-')));
|
||||
#else
|
||||
} while (isdigit(c));
|
||||
#endif
|
||||
|
||||
mprDestroyVar(&ep->tokenNumber);
|
||||
ep->tokenNumber = mprParseVar(ep->token, type);
|
||||
inputPutback(ep, c);
|
||||
return EJS_TOK_NUMBER;
|
||||
|
||||
default:
|
||||
/*
|
||||
* Identifiers or a function names
|
||||
*/
|
||||
while (1) {
|
||||
if (c == '\\') {
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
if (c == '\n' || c == '\r') {
|
||||
break;
|
||||
}
|
||||
} else if (tokenAddChar(ep, c) < 0) {
|
||||
break;
|
||||
}
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
if (!isalnum(c) && c != '$' && c != '_' && c != '\\') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (*ep->token == '\0') {
|
||||
c = inputGetc(ep);
|
||||
break;
|
||||
}
|
||||
if (! isalpha((int) *ep->token) && *ep->token != '$' &&
|
||||
*ep->token != '_') {
|
||||
ejsError(ep, "Invalid identifier %s", ep->token);
|
||||
return EJS_TOK_ERR;
|
||||
}
|
||||
|
||||
tid = checkReservedWord(ep, state, c, EJS_TOK_ID);
|
||||
if (tid != EJS_TOK_ID) {
|
||||
return tid;
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip white space after token to find out whether this is
|
||||
* a function or not.
|
||||
*/
|
||||
while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
|
||||
if ((c = inputGetc(ep)) < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
tid = EJS_TOK_ID;
|
||||
done++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Putback the last extra character for next time
|
||||
*/
|
||||
inputPutback(ep, c);
|
||||
return tid;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Convert a hex or octal character back to binary, return original char if
|
||||
* not a hex digit
|
||||
*/
|
||||
|
||||
static int charConvert(Ejs *ep, int base, int maxDig)
|
||||
{
|
||||
int i, c, lval, convChar;
|
||||
|
||||
lval = 0;
|
||||
for (i = 0; i < maxDig; i++) {
|
||||
if ((c = inputGetc(ep)) < 0) {
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Initialize to out of range value
|
||||
*/
|
||||
convChar = base;
|
||||
if (isdigit(c)) {
|
||||
convChar = c - '0';
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
convChar = c - 'a' + 10;
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
convChar = c - 'A' + 10;
|
||||
}
|
||||
/*
|
||||
* If unexpected character then return it to buffer.
|
||||
*/
|
||||
if (convChar >= base) {
|
||||
inputPutback(ep, c);
|
||||
break;
|
||||
}
|
||||
lval = (lval * base) + convChar;
|
||||
}
|
||||
return lval;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Putback the last token read. Accept at most one push back token.
|
||||
*/
|
||||
|
||||
void ejsLexPutbackToken(Ejs *ep, int tid, char *string)
|
||||
{
|
||||
EjsInput *ip;
|
||||
int idx;
|
||||
|
||||
mprAssert(ep);
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
|
||||
ip->putBackIndex += 1;
|
||||
idx = ip->putBackIndex;
|
||||
ip->putBack[idx].id = tid;
|
||||
|
||||
if (ip->putBack[idx].token) {
|
||||
if (ip->putBack[idx].token == string) {
|
||||
return;
|
||||
}
|
||||
mprFree(ip->putBack[idx].token);
|
||||
}
|
||||
ip->putBack[idx].token = mprStrdup(string);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Add a character to the token buffer
|
||||
*/
|
||||
|
||||
static int tokenAddChar(Ejs *ep, int c)
|
||||
{
|
||||
EjsInput *ip;
|
||||
uchar *oldbuf;
|
||||
|
||||
mprAssert(ep);
|
||||
ip = ep->input;
|
||||
mprAssert(ip);
|
||||
|
||||
if (ip->tokEndp >= &ip->tokbuf[ip->tokSize - 1]) {
|
||||
ip->tokSize += EJS_PARSE_INCR;
|
||||
oldbuf = ip->tokbuf;
|
||||
ip->tokbuf = mprRealloc(ip->tokbuf, ip->tokSize);
|
||||
if (ip->tokbuf == 0) {
|
||||
ejsError(ep, "Token too big");
|
||||
return -1;
|
||||
}
|
||||
ip->tokEndp += (int) ((uchar*) ip->tokbuf - oldbuf);
|
||||
ip->tokServp += (int) ((uchar*) ip->tokbuf - oldbuf);
|
||||
ep->token += (int) ((uchar*) ip->tokbuf - oldbuf);
|
||||
}
|
||||
*ip->tokEndp++ = c;
|
||||
*ip->tokEndp = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Get another input character
|
||||
*/
|
||||
|
||||
static int inputGetc(Ejs *ep)
|
||||
{
|
||||
EjsInput *ip;
|
||||
int c;
|
||||
|
||||
mprAssert(ep);
|
||||
ip = ep->input;
|
||||
|
||||
if (ip->scriptSize <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
c = (uchar) (*ip->scriptServp++);
|
||||
ip->scriptSize--;
|
||||
|
||||
/*
|
||||
* For debugging, accumulate the line number and the currenly parsed line
|
||||
*/
|
||||
if (c == '\n') {
|
||||
#if BLD_DEBUG && 0
|
||||
if (ip->lineColumn > 0) {
|
||||
printf("PARSED: %s\n", ip->line);
|
||||
}
|
||||
#endif
|
||||
ip->lineNumber++;
|
||||
ip->lineColumn = 0;
|
||||
} else {
|
||||
if ((ip->lineColumn + 2) >= ip->lineLength) {
|
||||
ip->lineLength += 80;
|
||||
ip->line = mprRealloc(ip->line, ip->lineLength * sizeof(char));
|
||||
}
|
||||
ip->line[ip->lineColumn++] = c;
|
||||
ip->line[ip->lineColumn] = '\0';
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Putback a character onto the input queue
|
||||
*/
|
||||
|
||||
static void inputPutback(Ejs *ep, int c)
|
||||
{
|
||||
EjsInput *ip;
|
||||
|
||||
mprAssert(ep);
|
||||
|
||||
if (c != 0) {
|
||||
ip = ep->input;
|
||||
*--ip->scriptServp = c;
|
||||
ip->scriptSize++;
|
||||
ip->lineColumn--;
|
||||
ip->line[ip->lineColumn] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsLexDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,704 @@
|
||||
/*
|
||||
* @file ejsProc.c
|
||||
* @brief EJS support functions
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default.g
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
* Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "ejsInternal.h"
|
||||
|
||||
#if BLD_FEATURE_EJS
|
||||
|
||||
/****************************** Forward Declarations **************************/
|
||||
/*
|
||||
* Object constructors
|
||||
*/
|
||||
static int objectConsProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
static int arrayConsProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
static int booleanConsProc(EjsHandle eid, int argc, MprVar **agv);
|
||||
static int numberConsProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
static int stringConsProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
|
||||
/*
|
||||
* Core functions
|
||||
*/
|
||||
static int toStringProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
static int valueOfProc(EjsHandle eid, int argc, MprVar **argv);
|
||||
|
||||
/*
|
||||
* Triggers
|
||||
*/
|
||||
static MprVarTriggerStatus lengthTrigger(MprVarTriggerOp op,
|
||||
MprProperties *parentProperties, MprVar *prop, MprVar *newValue,
|
||||
int copyRef);
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Routine to create the base common to all object types
|
||||
*/
|
||||
|
||||
MprVar ejsCreateObj(const char *name, int hashSize)
|
||||
{
|
||||
MprVar o;
|
||||
|
||||
o = mprCreateObjVar(name, hashSize);
|
||||
if (o.type == MPR_TYPE_UNDEFINED) {
|
||||
mprAssert(0);
|
||||
return o;
|
||||
}
|
||||
|
||||
mprCreatePropertyValue(&o, "toString",
|
||||
mprCreateCFunctionVar(toStringProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(&o, "valueOf",
|
||||
mprCreateCFunctionVar(valueOfProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
return o;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Routine to destroy a variable
|
||||
*/
|
||||
|
||||
bool ejsDestroyVar(MprVar *obj)
|
||||
{
|
||||
return mprDestroyVar(obj);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Routine to create the base array type
|
||||
*/
|
||||
|
||||
MprVar ejsCreateArray(const char *name, int size)
|
||||
{
|
||||
MprVar obj, *lp, undef;
|
||||
char idx[16];
|
||||
int i;
|
||||
|
||||
/* Sanity limit for size of hash table */
|
||||
|
||||
obj = ejsCreateObj(name, max(size, 503));
|
||||
if (obj.type == MPR_TYPE_UNDEFINED) {
|
||||
mprAssert(0);
|
||||
return obj;
|
||||
}
|
||||
|
||||
undef = mprCreateUndefinedVar();
|
||||
for (i = 0; i < size; i++) {
|
||||
mprItoa(i, idx, sizeof(idx));
|
||||
mprCreateProperty(&obj, idx, &undef);
|
||||
}
|
||||
|
||||
lp = mprCreatePropertyValue(&obj, "length", mprCreateIntegerVar(size));
|
||||
mprAssert(lp);
|
||||
|
||||
mprSetVarReadonly(lp, 1);
|
||||
mprAddVarTrigger(lp, lengthTrigger);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************** Constructors ********************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Object constructor. Nothing really done here. For future expansion.
|
||||
*/
|
||||
|
||||
static int objectConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
#if XX_UNUSED_XX
|
||||
MprVar *obj;
|
||||
Ejs *ep;
|
||||
|
||||
if((ep = ejsPtr(eid)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj = mprGetProperty(ep->local, "this", 0);
|
||||
mprAssert(obj);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Array constructor
|
||||
*/
|
||||
|
||||
static int arrayConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *obj, *lp, undef;
|
||||
Ejs *ep;
|
||||
char idx[16];
|
||||
int i, max;
|
||||
|
||||
objectConsProc(eid, argc, argv);
|
||||
|
||||
if((ep = ejsPtr(eid)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
obj = mprGetProperty(ep->local, "this", 0);
|
||||
mprAssert(obj);
|
||||
|
||||
|
||||
if (argc == 1 && mprVarIsNumber(argv[0]->type)) {
|
||||
/*
|
||||
* x = new Array(size);
|
||||
*/
|
||||
undef = mprCreateUndefinedVar();
|
||||
max = (int) mprVarToInteger(argv[0]);
|
||||
for (i = 0; i < max; i++) {
|
||||
mprItoa(i, idx, sizeof(idx));
|
||||
mprCreateProperty(obj, idx, &undef);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* x = new Array(element0, element1, ..., elementN):
|
||||
*/
|
||||
max = argc;
|
||||
for (i = 0; i < max; i++) {
|
||||
mprItoa(i, idx, sizeof(idx));
|
||||
mprCreateProperty(obj, idx, argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
lp = mprCreatePropertyValue(obj, "length", mprCreateIntegerVar(max));
|
||||
mprAssert(lp);
|
||||
|
||||
mprSetVarReadonly(lp, 1);
|
||||
mprAddVarTrigger(lp, lengthTrigger);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Boolean constructor
|
||||
*/
|
||||
|
||||
static int booleanConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
objectConsProc(eid, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if FUTURE
|
||||
/*
|
||||
* Date constructor
|
||||
*/
|
||||
|
||||
static int dateConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
objectConsProc(eid, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Number constructor
|
||||
*/
|
||||
|
||||
static int numberConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
objectConsProc(eid, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* String constructor
|
||||
*/
|
||||
|
||||
static int stringConsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
objectConsProc(eid, argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/********************************** Functions *********************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static int toStringProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *obj;
|
||||
Ejs *ep;
|
||||
char *buf;
|
||||
int radix;
|
||||
|
||||
if (argc == 0) {
|
||||
radix = 10;
|
||||
|
||||
} else if (argc == 1) {
|
||||
radix = (int) mprVarToInteger(argv[0]);
|
||||
|
||||
} else {
|
||||
mprAssert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((ep = ejsPtr(eid)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj = mprGetProperty(ep->local, "this", 0);
|
||||
mprAssert(obj);
|
||||
|
||||
mprVarToString(&buf, MPR_MAX_STRING, 0, obj);
|
||||
mprCopyVarValue(&ep->result, mprCreateStringVar(buf, 0), MPR_SHALLOW_COPY);
|
||||
mprFree(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static int valueOfProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *obj;
|
||||
Ejs *ep;
|
||||
|
||||
if (argc != 0) {
|
||||
mprAssert(0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if((ep = ejsPtr(eid)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
obj = mprGetProperty(ep->local, "this", 0);
|
||||
mprAssert(obj);
|
||||
|
||||
switch (obj->type) {
|
||||
default:
|
||||
case MPR_TYPE_UNDEFINED:
|
||||
case MPR_TYPE_NULL:
|
||||
case MPR_TYPE_CFUNCTION:
|
||||
case MPR_TYPE_OBJECT:
|
||||
case MPR_TYPE_FUNCTION:
|
||||
case MPR_TYPE_STRING_CFUNCTION:
|
||||
case MPR_TYPE_PTR:
|
||||
mprCopyVar(&ep->result, obj, MPR_SHALLOW_COPY);
|
||||
break;
|
||||
|
||||
case MPR_TYPE_STRING:
|
||||
mprCopyVarValue(&ep->result, mprCreateIntegerVar(atoi(obj->string)), 0);
|
||||
break;
|
||||
|
||||
case MPR_TYPE_BOOL:
|
||||
case MPR_TYPE_INT:
|
||||
#if BLD_FEATURE_INT64
|
||||
case MPR_TYPE_INT64:
|
||||
#endif
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
case MPR_TYPE_FLOAT:
|
||||
#endif
|
||||
mprCopyVar(&ep->result, obj, 0);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Var access trigger on the Array.length property. Return the count of
|
||||
* enumerable properties (don't count functions).
|
||||
*/
|
||||
|
||||
static MprVarTriggerStatus lengthTrigger(MprVarTriggerOp op,
|
||||
MprProperties *parentProperties, MprVar *prop, MprVar *newValue,
|
||||
int copyRef)
|
||||
{
|
||||
switch (op) {
|
||||
case MPR_VAR_READ:
|
||||
/*
|
||||
* Subtract one for the length property
|
||||
* FUTURE -- need an API to access parentProperties
|
||||
* FUTURE -- contradiction to be read-only yet allow USE_NEW_VALUE.
|
||||
* API needs finer control.
|
||||
*/
|
||||
*newValue = mprCreateIntegerVar(parentProperties->numDataItems - 1);
|
||||
return MPR_TRIGGER_USE_NEW_VALUE;
|
||||
|
||||
case MPR_VAR_WRITE:
|
||||
return MPR_TRIGGER_ABORT;
|
||||
|
||||
case MPR_VAR_CREATE_PROPERTY:
|
||||
case MPR_VAR_DELETE_PROPERTY:
|
||||
case MPR_VAR_DELETE:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return MPR_TRIGGER_PROCEED;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/**************************** Extension Functions *****************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Assert
|
||||
*/
|
||||
|
||||
static int assertProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
bool b;
|
||||
|
||||
if (argc < 1) {
|
||||
ejsSetErrorMsg(eid, "usage: assert(condition)\n");
|
||||
return -1;
|
||||
}
|
||||
b = mprVarToBool(argv[0]);
|
||||
if (b == 0) {
|
||||
ejsSetErrorMsg(eid, "Assertion failure\n");
|
||||
return -1;
|
||||
}
|
||||
ejsSetReturnValue(eid, mprCreateBoolVar(b));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Exit
|
||||
*/
|
||||
|
||||
static int exitProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (argc < 1) {
|
||||
ejsSetErrorMsg(eid, "usage: exit(status)\n");
|
||||
return -1;
|
||||
}
|
||||
status = (int) mprVarToInteger(argv[0]);
|
||||
ejsSetExitStatus(eid, status);
|
||||
|
||||
ejsSetReturnValue(eid, mprCreateStringVar("", 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static void printVar(MprVar *vp, int recurseCount, int indent)
|
||||
{
|
||||
MprVar *np;
|
||||
char *buf;
|
||||
int i;
|
||||
|
||||
if (recurseCount > 5) {
|
||||
write(1, "Skipping - recursion too deep\n", 29);
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < indent; i++) {
|
||||
write(1, " ", 2);
|
||||
}
|
||||
|
||||
if (vp->type == MPR_TYPE_OBJECT) {
|
||||
if (vp->name) {
|
||||
write(1, vp->name, strlen(vp->name));
|
||||
} else {
|
||||
write(1, "unknown", 7);
|
||||
}
|
||||
write(1, ": {\n", 4);
|
||||
np = mprGetFirstProperty(vp, MPR_ENUM_DATA);
|
||||
while (np) {
|
||||
if (strcmp(np->name, "local") == 0 ||
|
||||
strcmp(np->name, "global") == 0 ||
|
||||
strcmp(np->name, "this") == 0) {
|
||||
np = mprGetNextProperty(vp, np, MPR_ENUM_DATA);
|
||||
continue;
|
||||
}
|
||||
printVar(np, recurseCount + 1, indent + 1);
|
||||
np = mprGetNextProperty(vp, np, MPR_ENUM_DATA);
|
||||
if (np) {
|
||||
write(1, ",\n", 2);
|
||||
}
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
for (i = 0; i < indent; i++) {
|
||||
write(1, " ", 2);
|
||||
}
|
||||
write(1, "}", 1);
|
||||
|
||||
} else {
|
||||
if (vp->name) {
|
||||
write(1, vp->name, strlen(vp->name));
|
||||
} else {
|
||||
write(1, "unknown", 7);
|
||||
}
|
||||
write(1, ": ", 2);
|
||||
|
||||
/* FUTURE -- other types ? */
|
||||
mprVarToString(&buf, MPR_MAX_STRING, 0, vp);
|
||||
if (vp->type == MPR_TYPE_STRING) {
|
||||
write(1, "\"", 1);
|
||||
}
|
||||
write(1, buf, strlen(buf));
|
||||
if (vp->type == MPR_TYPE_STRING) {
|
||||
write(1, "\"", 1);
|
||||
}
|
||||
mprFree(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Print the args to stdout
|
||||
*/
|
||||
|
||||
static int printVarsProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *vp;
|
||||
char *buf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
vp = argv[i];
|
||||
switch (vp->type) {
|
||||
case MPR_TYPE_OBJECT:
|
||||
printVar(vp, 0, 0);
|
||||
break;
|
||||
default:
|
||||
mprVarToString(&buf, MPR_MAX_STRING, 0, vp);
|
||||
write(1, buf, strlen(buf));
|
||||
mprFree(buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
|
||||
ejsSetReturnValue(eid, mprCreateStringVar("", 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Print the args to stdout
|
||||
*/
|
||||
|
||||
static int printProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
char *buf;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
mprVarToString(&buf, MPR_MAX_STRING, 0, argv[i]);
|
||||
write(1, buf, strlen(buf));
|
||||
mprFree(buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* println
|
||||
*/
|
||||
|
||||
static int printlnProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
printProc(eid, argc, argv);
|
||||
write(1, "\n", 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Trace
|
||||
*/
|
||||
|
||||
static int traceProc(EjsHandle eid, int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
mprLog(0, "%s", argv[0]);
|
||||
|
||||
} else if (argc == 2) {
|
||||
mprLog(atoi(argv[0]), "%s", argv[1]);
|
||||
|
||||
} else {
|
||||
ejsSetErrorMsg(eid, "Usage: trace([level], message)");
|
||||
return -1;
|
||||
}
|
||||
ejsSetReturnString(eid, "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Return the object reference count
|
||||
*/
|
||||
|
||||
static int refCountProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *vp;
|
||||
int count;
|
||||
|
||||
vp = argv[0];
|
||||
if (vp->type == MPR_TYPE_OBJECT) {
|
||||
count = mprGetVarRefCount(vp);
|
||||
ejsSetReturnValue(eid, mprCreateIntegerVar(count));
|
||||
} else {
|
||||
ejsSetReturnValue(eid, mprCreateIntegerVar(0));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Evaluate a sub-script. It is evaluated in the same variable scope as
|
||||
* the calling script / function.
|
||||
*/
|
||||
|
||||
static int evalScriptProc(EjsHandle eid, int argc, MprVar **argv)
|
||||
{
|
||||
MprVar *arg;
|
||||
char *emsg;
|
||||
int i;
|
||||
|
||||
ejsSetReturnValue(eid, mprCreateUndefinedVar());
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
arg = argv[i];
|
||||
if (arg->type != MPR_TYPE_STRING) {
|
||||
continue;
|
||||
}
|
||||
if (ejsEvalScript(eid, arg->string, 0, &emsg) < 0) {
|
||||
ejsSetErrorMsg(eid, "%s", emsg);
|
||||
mprFree(emsg);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Return with the value of the last expression
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* Define the standard properties and functions inherited by all script engines.
|
||||
*/
|
||||
|
||||
int ejsDefineStandardProperties(MprVar *obj)
|
||||
{
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
double d = 0.0;
|
||||
|
||||
/* FUTURE - this generates warnings on some systems. This is okay. */
|
||||
|
||||
mprCreatePropertyValue(obj, "NaN", mprCreateFloatVar(0.0 / d));
|
||||
d = MAX_FLOAT;
|
||||
mprCreatePropertyValue(obj, "Infinity", mprCreateFloatVar(d * d));
|
||||
#endif
|
||||
mprCreatePropertyValue(obj, "null", mprCreateNullVar());
|
||||
mprCreatePropertyValue(obj, "undefined", mprCreateUndefinedVar());
|
||||
mprCreatePropertyValue(obj, "true", mprCreateBoolVar(1));
|
||||
mprCreatePropertyValue(obj, "false", mprCreateBoolVar(0));
|
||||
mprCreatePropertyValue(obj, "NULL", mprCreatePtrVar(NULL));
|
||||
|
||||
#if BLD_FEATURE_LEGACY_API
|
||||
/*
|
||||
* DEPRECATED: 2.0.
|
||||
* So that ESP/ASP can ignore "language=javascript" statements
|
||||
*/
|
||||
mprCreatePropertyValue(obj, "javascript", mprCreateIntegerVar(0));
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Extension functions
|
||||
*/
|
||||
mprCreatePropertyValue(obj, "assert",
|
||||
mprCreateCFunctionVar(assertProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "eval",
|
||||
mprCreateCFunctionVar(evalScriptProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "exit",
|
||||
mprCreateCFunctionVar(exitProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "refCount",
|
||||
mprCreateCFunctionVar(refCountProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "print",
|
||||
mprCreateCFunctionVar(printProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "println",
|
||||
mprCreateCFunctionVar(printlnProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "printVars",
|
||||
mprCreateCFunctionVar(printVarsProc,0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "trace",
|
||||
mprCreateStringCFunctionVar(traceProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
|
||||
/*
|
||||
* Constructors
|
||||
*/
|
||||
mprCreatePropertyValue(obj, "Array",
|
||||
mprCreateCFunctionVar(arrayConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "Boolean",
|
||||
mprCreateCFunctionVar(booleanConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "Object",
|
||||
mprCreateCFunctionVar(objectConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "Number",
|
||||
mprCreateCFunctionVar(numberConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
mprCreatePropertyValue(obj, "String",
|
||||
mprCreateCFunctionVar(stringConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
|
||||
/* mprCreatePropertyValue(obj, "Date",
|
||||
* mprCreateCFunctionVar(dateConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
* mprCreatePropertyValue(obj, "Regexp",
|
||||
* mprCreateCFunctionVar(regexpConsProc, 0, MPR_VAR_SCRIPT_HANDLE));
|
||||
*/
|
||||
|
||||
/*
|
||||
* Can we use on var x = "string text";
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void ejsProcsDummy() {}
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* BLD_FEATURE_EJS */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,277 @@
|
||||
/**
|
||||
* @file esp.h
|
||||
* @brief Header for Embedded Server Pages (ESP)
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#ifndef _h_ESP_h
|
||||
#define _h_ESP_h 1
|
||||
|
||||
#include "lib/appweb/ejs/ejs.h"
|
||||
#include "lib/appweb/esp/espEnv.h"
|
||||
#include "lib/appweb/mpr/var.h"
|
||||
#include "lib/appweb/mpr/miniMpr.h"
|
||||
|
||||
/*********************************** Defines **********************************/
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define ESP_TOK_INCR 1024
|
||||
#define ESP_MAX_HEADER 1024
|
||||
#else
|
||||
#define ESP_TOK_INCR 4096
|
||||
#define ESP_MAX_HEADER 4096
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ESP lexical analyser tokens
|
||||
*/
|
||||
#define ESP_TOK_ERR -1 /* Any input error */
|
||||
#define ESP_TOK_EOF 0 /* End of file */
|
||||
#define ESP_TOK_START_ESP 1 /* <% */
|
||||
#define ESP_TOK_END_ESP 2 /* %> */
|
||||
#define ESP_TOK_ATAT 3 /* @@var */
|
||||
#define ESP_TOK_LITERAL 4 /* literal HTML */
|
||||
#define ESP_TOK_INCLUDE 5 /* include file.esp */
|
||||
#define ESP_TOK_EQUALS 6 /* = var */
|
||||
|
||||
/*
|
||||
* ESP parser states
|
||||
*/
|
||||
#define ESP_STATE_BEGIN 1 /* Starting state */
|
||||
#define ESP_STATE_IN_ESP_TAG 2 /* Inside a <% %> group */
|
||||
|
||||
/*********************************** Types ************************************/
|
||||
|
||||
typedef void* EspHandle; /* Opaque Web server handle type */
|
||||
|
||||
/*
|
||||
* Per request control block
|
||||
*/
|
||||
typedef struct EspRequest {
|
||||
MprStr docPath; /* Physical path for ESP page */
|
||||
EjsId eid; /* EJS instance handle */
|
||||
const struct Esp *esp; /* Pointer to ESP control block */
|
||||
EspHandle requestHandle; /* Per request web server handle */
|
||||
MprStr uri; /* Request URI */
|
||||
MprVar *variables; /* Pointer to variables */
|
||||
} EspRequest;
|
||||
|
||||
/*
|
||||
* Master ESP control block. This defines the function callbacks for a
|
||||
* web server handler to implement. ESP will call these functions as
|
||||
* required.
|
||||
*/
|
||||
typedef struct Esp {
|
||||
int maxScriptSize;
|
||||
void (*createSession)(EspHandle handle, int timeout);
|
||||
void (*destroySession)(EspHandle handle);
|
||||
const char *(*getSessionId)(EspHandle handle);
|
||||
int (*mapToStorage)(EspHandle handle, char *path, int len, const char *uri,
|
||||
int flags);
|
||||
int (*readFile)(EspHandle handle, char **buf, int *len, const char *path);
|
||||
void (*redirect)(EspHandle handle, int code, char *url);
|
||||
void (*setCookie)(EspHandle handle, const char *name, const char *value,
|
||||
int lifetime, const char *path, bool secure);
|
||||
void (*setHeader)(EspHandle handle, const char *value, bool allowMultiple);
|
||||
void (*setResponseCode)(EspHandle handle, int code);
|
||||
int (*writeBlock)(EspHandle handle, const char *buf, int size);
|
||||
int (*writeFmt)(EspHandle handle, char *fmt, ...);
|
||||
#if BLD_FEATURE_MULTITHREAD
|
||||
void (*lock)(void *lockData);
|
||||
void (*unlock)(void *lockData);
|
||||
void *lockData;
|
||||
#endif
|
||||
} Esp;
|
||||
|
||||
|
||||
/*
|
||||
* ESP parse context
|
||||
*/
|
||||
typedef struct {
|
||||
char *inBuf; /* Input data to parse */
|
||||
char *inp; /* Next character for input */
|
||||
char *endp; /* End of storage (allow for null) */
|
||||
char *tokp; /* Pointer to current parsed token */
|
||||
char *token; /* Storage buffer for token */
|
||||
int tokLen; /* Length of buffer */
|
||||
} EspParse;
|
||||
|
||||
|
||||
/******************************** Private APIs ********************************/
|
||||
|
||||
extern void espRegisterProcs(void);
|
||||
|
||||
/******************************** Published API *******************************/
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Function callback signatures
|
||||
*/
|
||||
typedef int (*EspCFunction)(EspRequest *ep, int argc,
|
||||
struct MprVar **argv);
|
||||
typedef int (*EspStringCFunction)(EspRequest *ep, int argc,
|
||||
char **argv);
|
||||
|
||||
/*
|
||||
* APIs for those hosting the ESP module
|
||||
*/
|
||||
extern int espOpen(const Esp *control);
|
||||
extern void espClose(void);
|
||||
extern EspRequest *espCreateRequest(EspHandle webServerRequestHandle,
|
||||
char *uri, MprVar *envObj);
|
||||
extern void espDestroyRequest(EspRequest *ep);
|
||||
extern int espProcessRequest(EspRequest *ep, const char *docPath,
|
||||
char *docBuf, char **errMsg);
|
||||
|
||||
/*
|
||||
* Method invocation
|
||||
*/
|
||||
extern void espDefineCFunction(EspRequest *ep, const char *functionName,
|
||||
EspCFunction fn, void *thisPtr);
|
||||
extern void espDefineFunction(EspRequest *ep, const char *functionName,
|
||||
char *args, char *body);
|
||||
extern void espDefineStringCFunction(EspRequest *ep,
|
||||
const char *functionName, EspStringCFunction fn,
|
||||
void *thisPtr);
|
||||
extern int espRunFunction(EspRequest *ep, MprVar *obj,
|
||||
char *functionName, MprArray *args);
|
||||
extern void espSetResponseCode(EspRequest *ep, int code);
|
||||
extern void espSetReturn(EspRequest *ep, MprVar value);
|
||||
extern void *espGetThisPtr(EspRequest *ep);
|
||||
|
||||
/*
|
||||
* Utility routines to use in C methods
|
||||
*/
|
||||
extern void espError(EspRequest *ep, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
||||
extern int espEvalFile(EspRequest *ep, char *path, MprVar *result,
|
||||
char **emsg);
|
||||
extern int espEvalScript(EspRequest *ep, char *script, MprVar *result,
|
||||
char **emsg);
|
||||
extern MprVar *espGetLocalObject(EspRequest *ep);
|
||||
extern MprVar *espGetGlobalObject(EspRequest *ep);
|
||||
extern EspHandle espGetRequestHandle(EspRequest *ep);
|
||||
extern MprVar *espGetResult(EspRequest *ep);
|
||||
extern EjsId espGetScriptHandle(EspRequest *ep);
|
||||
extern void espRedirect(EspRequest *ep, int code, char *url);
|
||||
extern void espSetHeader(EspRequest *ep, char *header,
|
||||
bool allowMultiple);
|
||||
extern void espSetReturnString(EspRequest *ep, const char *str);
|
||||
extern int espWrite(EspRequest *ep, char *buf, int size);
|
||||
extern int espWriteString(EspRequest *ep, char *buf);
|
||||
extern int espWriteFmt(EspRequest *ep, char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
||||
|
||||
/*
|
||||
* ESP array[] variable access (set will update/create)
|
||||
*/
|
||||
extern int espGetVar(EspRequest *ep, EspEnvType oType, char *var,
|
||||
MprVar *value);
|
||||
extern char *espGetStringVar(EspRequest *ep, EspEnvType oType,
|
||||
char *var, char *defaultValue);
|
||||
extern void espSetVar(EspRequest *ep, EspEnvType oType, char *var,
|
||||
MprVar value);
|
||||
extern void espSetStringVar(EspRequest *ep, EspEnvType oType,
|
||||
const char *var, const char *value);
|
||||
extern int espUnsetVar(EspRequest *ep, EspEnvType oType, char *var);
|
||||
|
||||
/*
|
||||
* Object creation and management
|
||||
*/
|
||||
extern MprVar espCreateObjVar(char *name, int hashSize);
|
||||
extern MprVar espCreateArrayVar(char *name, int size);
|
||||
extern bool espDestroyVar(MprVar *var);
|
||||
extern MprVar *espCreateProperty(MprVar *obj, char *property,
|
||||
MprVar *newValue);
|
||||
extern MprVar *espCreatePropertyValue(MprVar *obj, char *property,
|
||||
MprVar newValue);
|
||||
extern int espDeleteProperty(MprVar *obj, char *property);
|
||||
|
||||
/*
|
||||
* JavaScript variable management. Set will create/update a property.
|
||||
* All return a property reference. GetProperty will optionally return the
|
||||
* property in value.
|
||||
*/
|
||||
extern MprVar *espGetProperty(MprVar *obj, char *property,
|
||||
MprVar *value);
|
||||
extern MprVar *espSetProperty(MprVar *obj, char *property,
|
||||
MprVar *newValue);
|
||||
extern MprVar *espSetPropertyValue(MprVar *obj, char *property,
|
||||
MprVar newValue);
|
||||
|
||||
#if 0
|
||||
/*
|
||||
* Low-level direct read and write of properties.
|
||||
* FUTURE: -- Read is not (dest, src). MUST WARN IN DOC ABOUT COPY/READ
|
||||
* Will still cause triggers to run.
|
||||
*/
|
||||
extern int espReadProperty(MprVar *dest, MprVar *prop);
|
||||
extern int espWriteProperty(MprVar *prop, MprVar *newValue);
|
||||
extern int espWritePropertyValue(MprVar *prop, MprVar newValue);
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Access JavaScript variables by their full name. Can use "." or "[]". For
|
||||
* example: "global.request['REQUEST_URI']"
|
||||
* For Read/write, the variables must exist.
|
||||
*/
|
||||
extern int espCopyVar(EspRequest *ep, char *var, MprVar *value,
|
||||
int copyDepth);
|
||||
extern int espDeleteVar(EspRequest *ep, char *var);
|
||||
extern int espReadVar(EspRequest *ep, char *var, MprVar *value);
|
||||
extern int espWriteVar(EspRequest *ep, char *var, MprVar *value);
|
||||
extern int espWriteVarValue(EspRequest *ep, char *var, MprVar value);
|
||||
|
||||
/*
|
||||
* Object property enumeration
|
||||
*/
|
||||
extern MprVar *espGetFirstProperty(MprVar *obj, int includeFlags);
|
||||
extern MprVar *espGetNextProperty(MprVar *obj, MprVar *currentProperty,
|
||||
int includeFlags);
|
||||
extern int espGetPropertyCount(MprVar *obj, int includeFlags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/******************************************************************************/
|
||||
#endif /* _h_ESP_h */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* @file espEnv.h
|
||||
* @brief ESP Environment Variables
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#ifndef _h_ESP_ENV_h
|
||||
#define _h_ESP_ENV_h 1
|
||||
|
||||
/*
|
||||
* @brief Scripting environment variable array types
|
||||
*/
|
||||
typedef enum EspEnvType {
|
||||
ESP_UNDEFINED_OBJ = -1,
|
||||
|
||||
/**
|
||||
* Elements for server[]:
|
||||
* DOCUMENT_ROOT GATEWAY_INTERFACE SERVER_ADDR SERVER_PORT SERVER_NAME
|
||||
* SERVER_PROTOCOL SERVER_SOFTWARE SERVER_URL UPLOAD_DIR
|
||||
* FUTURE: SERVER_ADMIN
|
||||
* FUTURE: this could be shared across all hosts and be made read-only.
|
||||
*/
|
||||
ESP_SERVER_OBJ = 0, /*! server[] data */
|
||||
|
||||
/**
|
||||
* Elements for session[]: are user defined
|
||||
*/
|
||||
ESP_SESSION_OBJ = 1, /*! session[] data */
|
||||
|
||||
/**
|
||||
* Elements for request[]:
|
||||
* AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE QUERY_STRING PATH_INFO
|
||||
* PATH_TRANSLATED REMOTE_ADDR REMOTE_HOST REMOTE_USER REQUEST_METHOD
|
||||
* REQUEST_URI SCRIPT_FILENAME SCRIPT_NAME
|
||||
* FUTURE: FILEPATH_INFO REDIRECT_URL SELF REMOTE_PORT AUTH_USER
|
||||
* AUTH_GROUP AUTH_ACL
|
||||
*/
|
||||
ESP_REQUEST_OBJ = 2, /*! request[] data */
|
||||
|
||||
/**
|
||||
* Elements for headers[]:
|
||||
* HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_CONNECTION HTTP_HOST
|
||||
* HTTP_REFERER HTTP_USER_AGENT and any other custom headers
|
||||
*/
|
||||
ESP_HEADERS_OBJ = 3, /*! header [] data */
|
||||
|
||||
/**
|
||||
* Elements for cookies[]: are defined by the HTTP request
|
||||
*/
|
||||
ESP_COOKIES_OBJ = 4, /*! cookies[] data */
|
||||
|
||||
/**
|
||||
* Elements for files[]: are defined by the HTTP request
|
||||
* CLIENT_FILENAME CONTENT_TYPE FILENAME SIZE
|
||||
*/
|
||||
ESP_FILES_OBJ = 5, /*! files[] data */
|
||||
|
||||
/**
|
||||
* Elements for form[]: are defined by the HTTP request
|
||||
*/
|
||||
ESP_FORM_OBJ = 6, /*! form[] data */
|
||||
|
||||
/**
|
||||
* Elements for application[]: are user defined
|
||||
*/
|
||||
ESP_APPLICATION_OBJ = 7, /*! application[] data */
|
||||
|
||||
/**
|
||||
* Elements for global[]: are defined by ESP/EJS
|
||||
*/
|
||||
ESP_GLOBAL_OBJ = 8, /*! global [] data */
|
||||
|
||||
/*
|
||||
* Elements for local[]: are defined by ESP/EJS
|
||||
*/
|
||||
ESP_LOCAL_OBJ = 9, /*! local [] data */
|
||||
} EspEnvType;
|
||||
|
||||
#define ESP_OBJ_MAX 10 /* Total objects */
|
||||
|
||||
#if BLD_SQUEEZE
|
||||
#define ESP_HASH_SIZE 19 /* Size of hash tables */
|
||||
#else
|
||||
#define ESP_HASH_SIZE 37
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
#endif /* _h_ESP_ENV_h */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* @file espProcs.c
|
||||
* @brief Embedded Server Pages (ESP) Procedures.
|
||||
* @overview These ESP procedures can be used in ESP pages for common tasks.
|
||||
*/
|
||||
/********************************* Copyright **********************************/
|
||||
/*
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
/********************************** Includes **********************************/
|
||||
|
||||
#include "esp.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
#if BLD_FEATURE_ESP_MODULE
|
||||
#if BLD_FEATURE_SESSION
|
||||
/*
|
||||
* destroySession
|
||||
*/
|
||||
|
||||
static int destroySessionProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
ep->esp->destroySession(ep->requestHandle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* BLD_FEATURE_SESSION */
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* include
|
||||
*
|
||||
* This includes javascript libraries. For example:
|
||||
*
|
||||
* <% include("file", ...); %>
|
||||
*
|
||||
* Don't confuse with ESP includes:
|
||||
*
|
||||
* <% include file.esp %>
|
||||
*
|
||||
* Filenames are relative to the base document including the file.
|
||||
* FUTURE -- move back to EJS. Only here now because we need ep->readFile.
|
||||
*/
|
||||
|
||||
static int includeProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
const Esp *esp;
|
||||
char path[MPR_MAX_FNAME], dir[MPR_MAX_FNAME];
|
||||
char *emsg=NULL, *buf;
|
||||
int size, i;
|
||||
|
||||
esp = ep->esp;
|
||||
mprAssert(argv);
|
||||
for (i = 0; i < argc; i++) {
|
||||
const char *extension;
|
||||
|
||||
if (argv[i][0] != '/') {
|
||||
mprGetDirName(dir, sizeof(dir), ep->docPath);
|
||||
mprSprintf(path, sizeof(path), "%s/%s", dir, argv[i]);
|
||||
} else {
|
||||
mprSprintf(path, sizeof(path), "%s", argv[i]);
|
||||
}
|
||||
|
||||
if (esp->readFile(ep->requestHandle, &buf, &size, path) < 0) {
|
||||
espError(ep, "Can't read include file: %s", path);
|
||||
return MPR_ERR_CANT_ACCESS;
|
||||
}
|
||||
buf[size] = '\0';
|
||||
|
||||
extension = strrchr(argv[i], '.');
|
||||
|
||||
/*
|
||||
* Allow nested inclusion of ESP requests
|
||||
*/
|
||||
if (extension && mprStrCmpAnyCase(extension, ".esp") == 0) {
|
||||
if (espProcessRequest(ep, path, buf, &emsg) != 0) {
|
||||
espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (ejsEvalScript(espGetScriptHandle(ep), buf, 0, &emsg) < 0) {
|
||||
espError(ep, "Cant evaluate script - %s", emsg?emsg:"");
|
||||
mprFree(buf);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
mprFree(buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* redirect
|
||||
*
|
||||
* This implemements <% redirect(url, code); %> command. The redirection
|
||||
* code is optional.
|
||||
*/
|
||||
|
||||
static int redirectProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
char *url;
|
||||
int code;
|
||||
|
||||
if (argc < 1) {
|
||||
espError(ep, "Bad args");
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
url = argv[0];
|
||||
if (argc == 2) {
|
||||
code = atoi(argv[1]);
|
||||
} else {
|
||||
code = 302;
|
||||
}
|
||||
espRedirect(ep, code, url);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
#if BLD_FEATURE_SESSION
|
||||
/*
|
||||
* useSession
|
||||
*/
|
||||
|
||||
static int useSessionProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
int timeout;
|
||||
|
||||
if (argc > 1) {
|
||||
espError(ep, "Bad args");
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
|
||||
} else if (argc == 1) {
|
||||
timeout = atoi(argv[0]);
|
||||
} else {
|
||||
timeout = 0;
|
||||
}
|
||||
|
||||
ep->esp->createSession(ep->requestHandle, timeout);
|
||||
espSetReturnString(ep, ep->esp->getSessionId(ep->requestHandle));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* BLD_FEATURE_SESSION */
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* setHeader
|
||||
*
|
||||
* This implemements <% setHeader("key: value", allowMultiple); %> command.
|
||||
*/
|
||||
|
||||
static int setHeaderProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
mprAssert(argv);
|
||||
if (argc != 2) {
|
||||
espError(ep, "Bad args");
|
||||
return MPR_ERR_BAD_ARGS;
|
||||
}
|
||||
ep->esp->setHeader(ep->requestHandle, argv[0], atoi(argv[1]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*
|
||||
* write
|
||||
*
|
||||
* This implemements <% write("text"); %> command.
|
||||
*/
|
||||
|
||||
static int writeProc(EspRequest *ep, int argc, char **argv)
|
||||
{
|
||||
char *s;
|
||||
int i, len;
|
||||
|
||||
mprAssert(argv);
|
||||
for (i = 0; i < argc; i++) {
|
||||
s = argv[i];
|
||||
len = strlen(s);
|
||||
if (len > 0) {
|
||||
if (espWrite(ep, s, len) != len) {
|
||||
espError(ep, "Can't write to client");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void espRegisterProcs()
|
||||
{
|
||||
espDefineStringCFunction(0, "write", writeProc, 0);
|
||||
espDefineStringCFunction(0, "setHeader", setHeaderProc, 0);
|
||||
espDefineStringCFunction(0, "redirect", redirectProc, 0);
|
||||
espDefineStringCFunction(0, "include", includeProc, 0);
|
||||
|
||||
#if BLD_FEATURE_SESSION
|
||||
/*
|
||||
* Create and use are synonomous
|
||||
*/
|
||||
espDefineStringCFunction(0, "useSession", useSessionProc, 0);
|
||||
espDefineStringCFunction(0, "createSession", useSessionProc, 0);
|
||||
espDefineStringCFunction(0, "destroySession", destroySessionProc, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#else
|
||||
void mprEspControlsDummy() {}
|
||||
|
||||
#endif /* BLD_FEATURE_ESP_MODULE */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,506 @@
|
||||
/*
|
||||
* @file miniMpr.cpp
|
||||
* @brief Mini Mbedthis Portable Runtime (MPR)
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
#include "miniMpr.h"
|
||||
|
||||
/************************************ Code ************************************/
|
||||
#if !BLD_APPWEB
|
||||
#if !BLD_GOAHEAD_WEBSERVER
|
||||
|
||||
static void *mpr_ctx;
|
||||
|
||||
/* set the memory context to be used for all ejs variables */
|
||||
void mprSetCtx(TALLOC_CTX *ctx)
|
||||
{
|
||||
mpr_ctx = ctx;
|
||||
}
|
||||
|
||||
/* return the memory context being used for all ejs variables */
|
||||
void *mprMemCtx(void)
|
||||
{
|
||||
return mpr_ctx;
|
||||
}
|
||||
|
||||
void mprFree(void *ptr)
|
||||
{
|
||||
talloc_free(ptr);
|
||||
}
|
||||
|
||||
void *mprMalloc(uint size)
|
||||
{
|
||||
return talloc_size(mpr_ctx, size);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void *mprRealloc(void *ptr, uint size)
|
||||
{
|
||||
return talloc_realloc_size(mpr_ctx, ptr, size);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
char *mprStrdup(const char *str)
|
||||
{
|
||||
if (str == 0) {
|
||||
str = "";
|
||||
}
|
||||
return talloc_strdup(mpr_ctx, str);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
int count;
|
||||
|
||||
va_start(args, fmt);
|
||||
buf = mprMalloc(maxSize + 1);
|
||||
count = mtVsprintf(buf, maxSize, fmt, args);
|
||||
*msgbuf = buf;
|
||||
va_end(args);
|
||||
return count;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, va_list args)
|
||||
{
|
||||
char *buf;
|
||||
int count;
|
||||
|
||||
buf = mprMalloc(maxSize + 1);
|
||||
count = mtVsprintf(buf, maxSize, fmt, args);
|
||||
*msgbuf = buf;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Format a number as a string. FUTURE -- reverse args to be standard.
|
||||
* ie. mprItoa(char *userBuf, int bufsize, int value);
|
||||
*/
|
||||
|
||||
char *mprItoa(int value, char *buf, int width)
|
||||
{
|
||||
char numBuf[16];
|
||||
char *cp, *dp, *endp;
|
||||
int negative;
|
||||
|
||||
cp = &numBuf[sizeof(numBuf)];
|
||||
*--cp = '\0';
|
||||
|
||||
if (value < 0) {
|
||||
negative = 1;
|
||||
value = -value;
|
||||
width--;
|
||||
} else {
|
||||
negative = 0;
|
||||
}
|
||||
|
||||
do {
|
||||
*--cp = '0' + (value % 10);
|
||||
value /= 10;
|
||||
} while (value > 0);
|
||||
|
||||
if (negative) {
|
||||
*--cp = '-';
|
||||
}
|
||||
|
||||
dp = buf;
|
||||
endp = &buf[width];
|
||||
while (dp < endp && *cp) {
|
||||
*dp++ = *cp++;
|
||||
}
|
||||
*dp++ = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
void mprLog(int level, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char *buf;
|
||||
|
||||
if (DEBUGLVL(level)) {
|
||||
va_start(args, fmt);
|
||||
mprAllocVsprintf(&buf, MPR_MAX_STRING, fmt, args);
|
||||
va_end(args);
|
||||
DEBUG(level, ("mprLog: %s", buf));
|
||||
mprFree(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
void mprBreakpoint(const char *file, int line, const char *cond)
|
||||
{
|
||||
char *buf;
|
||||
mprAllocSprintf(&buf, MPR_MAX_STRING, "esp exception - ASSERT at %s:%d, %s\n",
|
||||
file, line, cond);
|
||||
ejs_exception(buf);
|
||||
}
|
||||
|
||||
#endif /* !BLD_GOAHEAD_WEBSERVER */
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Create a general growable array structure
|
||||
*/
|
||||
|
||||
MprArray *mprCreateArray()
|
||||
{
|
||||
MprArray *array;
|
||||
int size;
|
||||
|
||||
array = (MprArray*) mprMalloc(sizeof(MprArray));
|
||||
if (array == 0) {
|
||||
return 0;
|
||||
}
|
||||
memset(array, 0, sizeof(MprArray));
|
||||
|
||||
size = MPR_ARRAY_INCR * sizeof(void*);
|
||||
array->handles = (void**) mprMalloc(size);
|
||||
if (array->handles == 0) {
|
||||
mprFree(array);
|
||||
return 0;
|
||||
}
|
||||
memset(array->handles, 0, size);
|
||||
array->max = MPR_ARRAY_INCR;
|
||||
array->used = 0;
|
||||
return array;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Dispose of the array. Callers responsibility to dispose of handle entries.
|
||||
*/
|
||||
|
||||
void mprDestroyArray(MprArray *array)
|
||||
{
|
||||
mprAssert(array);
|
||||
mprAssert(array->max >= 0);
|
||||
mprAssert(array->used >= 0);
|
||||
|
||||
mprFree(array->handles);
|
||||
mprFree(array);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Add an item to the array
|
||||
*/
|
||||
|
||||
int mprAddToArray(MprArray *array, void *item)
|
||||
{
|
||||
int memsize, idx, len;
|
||||
|
||||
mprAssert(array);
|
||||
mprAssert(array->max >= 0);
|
||||
mprAssert(array->used >= 0);
|
||||
|
||||
if (array->used < array->max) {
|
||||
idx = array->used++;
|
||||
mprAssert(idx >= 0 && idx < array->max);
|
||||
mprAssert(array->handles[idx] == 0);
|
||||
array->handles[idx] = item;
|
||||
return idx;
|
||||
}
|
||||
|
||||
for (idx = array->used; idx < array->max; idx++) {
|
||||
if (array->handles[idx] == 0) {
|
||||
array->used++;
|
||||
mprAssert(array->handles[idx] == 0);
|
||||
array->handles[idx] = item;
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
|
||||
len = array->max + MPR_ARRAY_INCR;
|
||||
memsize = len * sizeof(void*);
|
||||
array->handles = (void**) mprRealloc((void*) array->handles, memsize);
|
||||
if (array->handles == NULL) {
|
||||
return -1;
|
||||
}
|
||||
memset(&array->handles[array->max], 0, sizeof(void*) * MPR_ARRAY_INCR);
|
||||
array->max = len;
|
||||
array->used++;
|
||||
|
||||
mprAssert(idx >= 0 && idx < array->max);
|
||||
mprAssert(array->handles[idx] == 0);
|
||||
|
||||
array->handles[idx] = item;
|
||||
return idx;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Remove from the array
|
||||
*/
|
||||
|
||||
int mprRemoveFromArray(MprArray *array, int idx)
|
||||
{
|
||||
mprAssert(array);
|
||||
mprAssert(array->max > 0);
|
||||
mprAssert(idx >= 0 && idx < array->max);
|
||||
mprAssert(array->handles[idx] != 0);
|
||||
mprAssert(array->used > 0);
|
||||
|
||||
array->handles[idx] = 0;
|
||||
return --array->used;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Thread-safe wrapping of strtok. Note "str" is modifed as per strtok()
|
||||
*/
|
||||
|
||||
char *mprStrTok(char *str, const char *delim, char **tok)
|
||||
{
|
||||
char *start, *end;
|
||||
int i;
|
||||
|
||||
start = str ? str : *tok;
|
||||
|
||||
if (start == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
i = strspn(start, delim);
|
||||
start += i;
|
||||
if (*start == '\0') {
|
||||
*tok = 0;
|
||||
return 0;
|
||||
}
|
||||
end = strpbrk(start, delim);
|
||||
if (end) {
|
||||
*end++ = '\0';
|
||||
i = strspn(end, delim);
|
||||
end += i;
|
||||
}
|
||||
*tok = end;
|
||||
return start;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static int mprCoreStrcat(int alloc, char **destp, int destMax, int existingLen,
|
||||
const char *delim, const char *src, va_list args)
|
||||
{
|
||||
va_list ap;
|
||||
char *dest, *dp;
|
||||
const char *str;
|
||||
int sepLen, addBytes, required;
|
||||
|
||||
mprAssert(destp);
|
||||
mprAssert(destMax > 0);
|
||||
mprAssert(src);
|
||||
|
||||
dest = *destp;
|
||||
sepLen = (delim) ? strlen(delim) : 0;
|
||||
|
||||
va_copy(ap, args);
|
||||
addBytes = 0;
|
||||
str = src;
|
||||
while (str) {
|
||||
addBytes += strlen(str) + sepLen;
|
||||
str = va_arg(ap, const char*);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (existingLen > 0) {
|
||||
addBytes += sepLen;
|
||||
}
|
||||
required = existingLen + addBytes + 1;
|
||||
if (required >= destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
|
||||
if (alloc) {
|
||||
if (dest == 0) {
|
||||
dest = (char*) mprMalloc(required);
|
||||
} else {
|
||||
dest = (char*) mprRealloc(dest, required);
|
||||
}
|
||||
} else {
|
||||
dest = (char*) *destp;
|
||||
}
|
||||
|
||||
dp = &dest[existingLen];
|
||||
if (delim) {
|
||||
strcpy(dp, delim);
|
||||
dp += sepLen;
|
||||
}
|
||||
|
||||
if (addBytes > 0) {
|
||||
va_copy(ap, args);
|
||||
str = src;
|
||||
while (str) {
|
||||
strcpy(dp, str);
|
||||
dp += strlen(str);
|
||||
str = va_arg(ap, char*);
|
||||
if (delim && str) {
|
||||
strcpy(dp, delim);
|
||||
dp += sepLen;
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
} else if (dest == 0) {
|
||||
dest = (char*) mprMalloc(1);
|
||||
}
|
||||
*dp = '\0';
|
||||
|
||||
*destp = dest;
|
||||
mprAssert(dp < &dest[required]);
|
||||
return required - 1;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprReallocStrcat(char **destp, int destMax, int existingLen,
|
||||
const char *delim, const char *src,...)
|
||||
{
|
||||
va_list ap;
|
||||
int rc;
|
||||
|
||||
va_start(ap, src);
|
||||
rc = mprCoreStrcat(1, destp, destMax, existingLen, delim, src, ap);
|
||||
va_end(ap);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/*
|
||||
* Return the directory portion of a pathname into the users buffer.
|
||||
*/
|
||||
|
||||
int mprGetDirName(char *buf, int bufsize, char *path)
|
||||
{
|
||||
char *cp;
|
||||
int dlen;
|
||||
|
||||
mprAssert(path);
|
||||
mprAssert(buf);
|
||||
mprAssert(bufsize > 0);
|
||||
|
||||
cp = strrchr(path, '/');
|
||||
if (cp == 0) {
|
||||
#if WIN
|
||||
cp = strrchr(path, '\\');
|
||||
if (cp == 0)
|
||||
#endif
|
||||
{
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (cp == path && cp[1] == '\0') {
|
||||
strcpy(buf, ".");
|
||||
return 0;
|
||||
}
|
||||
|
||||
dlen = cp - path;
|
||||
if (dlen < bufsize) {
|
||||
if (dlen == 0) {
|
||||
dlen++;
|
||||
}
|
||||
mprMemcpy(buf, bufsize, path, dlen);
|
||||
buf[dlen] = '\0';
|
||||
return 0;
|
||||
}
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprStrcpy(char *dest, int destMax, const char *src)
|
||||
{
|
||||
int len;
|
||||
|
||||
mprAssert(dest);
|
||||
mprAssert(destMax > 0);
|
||||
mprAssert(src);
|
||||
|
||||
len = strlen(src);
|
||||
if (len >= destMax && len > 0) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (len > 0) {
|
||||
memcpy(dest, src, len);
|
||||
dest[len] = '\0';
|
||||
} else {
|
||||
*dest = '\0';
|
||||
len = 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
int mprMemcpy(char *dest, int destMax, const char *src, int nbytes)
|
||||
{
|
||||
mprAssert(dest);
|
||||
mprAssert(destMax > nbytes);
|
||||
mprAssert(src);
|
||||
mprAssert(nbytes > 0);
|
||||
|
||||
if (nbytes > destMax) {
|
||||
mprAssert(0);
|
||||
return MPR_ERR_WONT_FIT;
|
||||
}
|
||||
if (nbytes > 0) {
|
||||
memcpy(dest, src, nbytes);
|
||||
return nbytes;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
#else
|
||||
void miniMprDummy() {}
|
||||
#endif // !BLD_APPWEB && !BLD_GOAHEAD_WEBSERVER
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,297 @@
|
||||
/*
|
||||
* @file miniMpr.h
|
||||
* @brief Mini Mbedthis Portable Runtime (MPR) Environment.
|
||||
* @copy default
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
#ifndef _h_MINI_MPR
|
||||
#define _h_MINI_MPR 1
|
||||
|
||||
/********************************** Includes **********************************/
|
||||
/*
|
||||
* Find out about our configuration
|
||||
*/
|
||||
#ifndef _INCLUDES_H
|
||||
#include "includes.h"
|
||||
#endif
|
||||
|
||||
/* allow this library to use strcpy() */
|
||||
#undef strcpy
|
||||
#include "lib/appweb/ejs/config.h"
|
||||
|
||||
#if BLD_APPWEB
|
||||
/*
|
||||
* If building within AppWeb, use the full MPR
|
||||
*/
|
||||
#include "mpr.h"
|
||||
#else
|
||||
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if !WIN
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if CE
|
||||
#include <io.h>
|
||||
#include "CE/wincompat.h"
|
||||
#endif
|
||||
|
||||
#if LYNX
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if QNX4
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
#ifdef HAVE_MATH_H
|
||||
#include <math.h>
|
||||
#endif
|
||||
/********************************** Defines ***********************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
/*
|
||||
* Reasonable length of a file path name to use in most cases where you know
|
||||
* the expected file name and it is certain to be less than this limit.
|
||||
*/
|
||||
#define MPR_MAX_FNAME 128
|
||||
#define MPR_MAX_STRING 512
|
||||
#define MPR_DEFAULT_HASH_SIZE 23 /* Default size of hash table index */
|
||||
#define MPR_MAX_HEAP_SIZE (32 * 1024)
|
||||
#else
|
||||
#define MPR_MAX_FNAME 256
|
||||
#define MPR_MAX_STRING 4096
|
||||
#define MPR_DEFAULT_HASH_SIZE 43 /* Default size of hash table index */
|
||||
#define MPR_MAX_HEAP_SIZE (64 * 1024)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Useful for debugging
|
||||
*/
|
||||
#define MPR_L __FILE__, __LINE__
|
||||
|
||||
#if BLD_FEATURE_ASSERT
|
||||
#define mprAssert(C) \
|
||||
if (C) ; else mprBreakpoint(__FILE__, __LINE__, #C)
|
||||
#else
|
||||
#define mprAssert(C) if (1) ; else
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Standard MPR return and error codes
|
||||
*/
|
||||
#define MPR_ERR_BASE (-200) /* Error code */
|
||||
#define MPR_ERR_GENERAL (MPR_ERR_BASE - 1) /* Error code */
|
||||
#define MPR_ERR_ABORTED (MPR_ERR_BASE - 2) /* Error code */
|
||||
#define MPR_ERR_ALREADY_EXISTS (MPR_ERR_BASE - 3) /* Error code */
|
||||
#define MPR_ERR_BAD_ARGS (MPR_ERR_BASE - 4) /* Error code */
|
||||
#define MPR_ERR_BAD_FORMAT (MPR_ERR_BASE - 5) /* Error code */
|
||||
#define MPR_ERR_BAD_HANDLE (MPR_ERR_BASE - 6) /* Error code */
|
||||
#define MPR_ERR_BAD_STATE (MPR_ERR_BASE - 7) /* Error code */
|
||||
#define MPR_ERR_BAD_SYNTAX (MPR_ERR_BASE - 8) /* Error code */
|
||||
#define MPR_ERR_BAD_TYPE (MPR_ERR_BASE - 9) /* Error code */
|
||||
#define MPR_ERR_BAD_VALUE (MPR_ERR_BASE - 10) /* Error code */
|
||||
#define MPR_ERR_BUSY (MPR_ERR_BASE - 11) /* Error code */
|
||||
#define MPR_ERR_CANT_ACCESS (MPR_ERR_BASE - 12) /* Error code */
|
||||
#define MPR_ERR_CANT_COMPLETE (MPR_ERR_BASE - 13) /* Error code */
|
||||
#define MPR_ERR_CANT_CREATE (MPR_ERR_BASE - 14) /* Error code */
|
||||
#define MPR_ERR_CANT_INITIALIZE (MPR_ERR_BASE - 15) /* Error code */
|
||||
#define MPR_ERR_CANT_OPEN (MPR_ERR_BASE - 16) /* Error code */
|
||||
#define MPR_ERR_CANT_READ (MPR_ERR_BASE - 17) /* Error code */
|
||||
#define MPR_ERR_CANT_WRITE (MPR_ERR_BASE - 18) /* Error code */
|
||||
#define MPR_ERR_DELETED (MPR_ERR_BASE - 19) /* Error code */
|
||||
#define MPR_ERR_NETWORK (MPR_ERR_BASE - 20) /* Error code */
|
||||
#define MPR_ERR_NOT_FOUND (MPR_ERR_BASE - 21) /* Error code */
|
||||
#define MPR_ERR_NOT_INITIALIZED (MPR_ERR_BASE - 22) /* Error code */
|
||||
#define MPR_ERR_NOT_READY (MPR_ERR_BASE - 23) /* Error code */
|
||||
#define MPR_ERR_READ_ONLY (MPR_ERR_BASE - 24) /* Error code */
|
||||
#define MPR_ERR_TIMEOUT (MPR_ERR_BASE - 25) /* Error code */
|
||||
#define MPR_ERR_TOO_MANY (MPR_ERR_BASE - 26) /* Error code */
|
||||
#define MPR_ERR_WONT_FIT (MPR_ERR_BASE - 27) /* Error code */
|
||||
#define MPR_ERR_WOULD_BLOCK (MPR_ERR_BASE - 28) /* Error code */
|
||||
#define MPR_ERR_CANT_ALLOCATE (MPR_ERR_BASE - 29) /* Error code */
|
||||
#define MPR_ERR_MAX (MPR_ERR_BASE - 30) /* Error code */
|
||||
|
||||
/*
|
||||
* Standard error severity and trace levels. These are ored with the error
|
||||
* severities below. The MPR_LOG_MASK is used to extract the trace level
|
||||
* from a flags word. We expect most apps to run with level 2 trace.
|
||||
*/
|
||||
#define MPR_FATAL 0 /* Fatal error. Cant continue. */
|
||||
#define MPR_ERROR 1 /* Hard error */
|
||||
#define MPR_WARN 2 /* Soft warning */
|
||||
#define MPR_CONFIG 2 /* Essential configuration settings */
|
||||
#define MPR_INFO 3 /* Informational only */
|
||||
#define MPR_DEBUG 4 /* Debug information */
|
||||
#define MPR_VERBOSE 9 /* Highest level of trace */
|
||||
#define MPR_LOG_MASK 0xf /* Level mask */
|
||||
|
||||
/*
|
||||
* Error flags. Specify where the error should be sent to. Note that the
|
||||
* product.xml setting "headless" will modify how errors are reported.
|
||||
* Assert errors are trapped when in DEV mode. Otherwise ignored.
|
||||
*/
|
||||
#define MPR_TRAP 0x10 /* Assert error -- trap in debugger */
|
||||
#define MPR_LOG 0x20 /* Log the error in the O/S event log */
|
||||
#define MPR_USER 0x40 /* Display to the user */
|
||||
#define MPR_ALERT 0x80 /* Send a management alert */
|
||||
#define MPR_TRACE 0x100 /* Trace */
|
||||
|
||||
/*
|
||||
* Error format flags
|
||||
*/
|
||||
#define MPR_RAW 0x200 /* Raw trace output */
|
||||
|
||||
/*
|
||||
* Error line number information
|
||||
*/
|
||||
#define MPR_L __FILE__, __LINE__
|
||||
|
||||
typedef char* MprStr;
|
||||
|
||||
#ifndef __cplusplus
|
||||
typedef unsigned char uchar;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Porters: put other operating system type defines here
|
||||
*/
|
||||
#if WIN
|
||||
typedef unsigned int uint;
|
||||
typedef __int64 int64;
|
||||
typedef unsigned __int64 uint64;
|
||||
#else
|
||||
#define O_BINARY 0
|
||||
#ifndef uint
|
||||
#define uint unsigned
|
||||
#endif
|
||||
#define int64 int64_t
|
||||
#define uint64 uint64_t
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Flexible array data type
|
||||
*/
|
||||
typedef struct {
|
||||
int max; /* Size of the handles array */
|
||||
int used; /* Count of used entries in handles */
|
||||
void **handles;
|
||||
} MprArray;
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define MPR_ARRAY_INCR 8
|
||||
#else
|
||||
#define MPR_ARRAY_INCR 16
|
||||
#endif
|
||||
|
||||
#ifndef max
|
||||
#define max(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
/*
|
||||
* If running in the GoAhead WebServer, map some MPR routines to WebServer
|
||||
* equivalents.
|
||||
*/
|
||||
|
||||
#if BLD_GOAHEAD_WEBSERVER
|
||||
#include "uemf.h"
|
||||
#define mprMalloc(size) balloc(B_L, size)
|
||||
#define mprFree(ptr) bfreeSafe(B_L, ptr)
|
||||
#define mprRealloc(ptr, size) brealloc(B_L, ptr, size)
|
||||
#define mprStrdup(ptr) bstrdup(B_L, ptr)
|
||||
#define mprAllocSprintf fmtAlloc
|
||||
#define mprAllocVsprintf fmtValloc
|
||||
#define mprSprintf fmtStatic
|
||||
#define mprItoa stritoa
|
||||
#define mprLog trace
|
||||
#define mprBreakpoint(file, line, cond) \
|
||||
error(file, line, E_BLD_FEATURE_ASSERT, T("%s"), cond)
|
||||
|
||||
#else /* !BLD_GOAHEAD_WEBSERVER */
|
||||
/* #define mprMalloc malloc */
|
||||
#define mprSprintf snprintf
|
||||
#define mtVsprintf vsnprintf
|
||||
extern void *mprMalloc(uint size);
|
||||
extern void *mprRealloc(void *ptr, uint size);
|
||||
extern void mprFree(void *ptr);
|
||||
extern char *mprStrdup(const char *str);
|
||||
extern int mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt,
|
||||
va_list args) PRINTF_ATTRIBUTE(3,0);
|
||||
extern int mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
|
||||
extern char *mprItoa(int num, char *buf, int width);
|
||||
extern void mprLog(int level, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
|
||||
extern void mprBreakpoint(const char *file, int line, const char *msg) NORETURN_ATTRIBUTE;
|
||||
#endif /* BLD_GOAHEAD_WEBSERVER */
|
||||
|
||||
extern MprArray *mprCreateArray(void);
|
||||
extern void mprDestroyArray(MprArray *array);
|
||||
extern int mprAddToArray(MprArray *array, void *item);
|
||||
extern int mprRemoveFromArray(MprArray *array, int idx);
|
||||
extern char *mprStrTok(char *str, const char *delim, char **tok);
|
||||
|
||||
extern int mprGetDirName(char *buf, int bufsize, char *path);
|
||||
extern int mprReallocStrcat(char **dest, int max, int existingLen,
|
||||
const char *delim, const char *src, ...);
|
||||
extern int mprStrcpy(char *dest, int destMax, const char *src);
|
||||
extern int mprMemcpy(char *dest, int destMax, const char *src, int nbytes);
|
||||
|
||||
extern void mprSetCtx(void *ctx);
|
||||
extern void *mprMemCtx(void);
|
||||
|
||||
/* This function needs to be provided by anyone using ejs */
|
||||
void ejs_exception(const char *reason);
|
||||
|
||||
#define mprStrCmpAnyCase(s1, s2) strcasecmp_m(s1, s2)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* !BLD_APPWEB */
|
||||
#endif /* _h_MINI_MPR */
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,487 @@
|
||||
/*
|
||||
* @file var.h
|
||||
* @brief MPR Universal Variable Type
|
||||
* @copy default.m
|
||||
*
|
||||
* Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
|
||||
* Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
|
||||
*
|
||||
* This software is distributed under commercial and open source licenses.
|
||||
* You may use the GPL open source license described below or you may acquire
|
||||
* a commercial license from Mbedthis Software. You agree to be fully bound
|
||||
* by the terms of either license. Consult the LICENSE.TXT distributed with
|
||||
* this software for full details.
|
||||
*
|
||||
* This software is open source; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version. See the GNU General Public License for more
|
||||
* details at: http://www.mbedthis.com/downloads/gplLicense.html
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the
|
||||
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* This GPL license does NOT permit incorporating this software into
|
||||
* proprietary programs. If you are unable to comply with the GPL, you must
|
||||
* acquire a commercial license to use this software. Commercial licenses
|
||||
* for this software and support services are available from Mbedthis
|
||||
* Software at http://www.mbedthis.com
|
||||
*
|
||||
* @end
|
||||
*/
|
||||
|
||||
/******************************* Documentation ********************************/
|
||||
/*
|
||||
* Variables can efficiently store primitive types and can hold references to
|
||||
* objects. Objects can store properties which are themselves variables.
|
||||
* Properties can be primitive data types, other objects or functions.
|
||||
* Properties are indexed by a character name. A variable may store one of
|
||||
* the following types:
|
||||
*
|
||||
* string, integer, integer-64bit, C function, C function with string args,
|
||||
* Javascript function, Floating point number, boolean value, Undefined
|
||||
* value and the Null value.
|
||||
*
|
||||
* Variables have names while objects may be referenced by multiple variables.
|
||||
* Objects use reference counting for garbage collection.
|
||||
*
|
||||
* This module is not thread safe for performance and compactness. It relies
|
||||
* on upper modules to provide thread synchronization as required. The API
|
||||
* provides primitives to get variable/object references or to get copies of
|
||||
* variables which will help minimize required lock times.
|
||||
*/
|
||||
|
||||
#ifndef _h_MPR_VAR
|
||||
#define _h_MPR_VAR 1
|
||||
|
||||
/********************************* Includes ***********************************/
|
||||
|
||||
#include "miniMpr.h"
|
||||
|
||||
/********************************** Defines ***********************************/
|
||||
|
||||
/*
|
||||
* Define VAR_DEBUG if you want to track objects. However, this code is not
|
||||
* thread safe and you need to run the server single threaded.
|
||||
*
|
||||
* #define VAR_DEBUG 1
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Forward declare types
|
||||
*/
|
||||
struct MprProperties;
|
||||
struct MprVar;
|
||||
|
||||
/*
|
||||
* Possible variable types. Don't use enum because we need to be able to
|
||||
* do compile time conditional compilation on BLD_FEATURE_NUM_TYPE_ID.
|
||||
*/
|
||||
typedef int MprType;
|
||||
#define MPR_TYPE_UNDEFINED 0 /* Undefined. No value has been set. */
|
||||
#define MPR_TYPE_NULL 1 /* Value defined to be null. */
|
||||
#define MPR_TYPE_BOOL 2 /* Boolean type. */
|
||||
#define MPR_TYPE_CFUNCTION 3 /* C function or C++ method */
|
||||
#define MPR_TYPE_FLOAT 4 /* Floating point number */
|
||||
#define MPR_TYPE_INT 5 /* Integer number */
|
||||
#define MPR_TYPE_INT64 6 /* 64-bit Integer number */
|
||||
#define MPR_TYPE_OBJECT 7 /* Object reference */
|
||||
#define MPR_TYPE_FUNCTION 8 /* JavaScript function */
|
||||
#define MPR_TYPE_STRING 9 /* String (immutable) */
|
||||
#define MPR_TYPE_STRING_CFUNCTION 10 /* C/C++ function with string args */
|
||||
#define MPR_TYPE_PTR 11 /* Opaque pointer */
|
||||
|
||||
/*
|
||||
* Create a type for the default number type
|
||||
* Config.h will define the default number type. For example:
|
||||
*
|
||||
* BLD_FEATURE_NUM_TYPE=int
|
||||
* BLD_FEATURE_NUM_TYPE_ID=MPR_TYPE_INT
|
||||
*/
|
||||
|
||||
/**
|
||||
* Set to the type used for MPR numeric variables. Will equate to int, int64
|
||||
* or double.
|
||||
*/
|
||||
typedef BLD_FEATURE_NUM_TYPE MprNum;
|
||||
|
||||
/**
|
||||
* Set to the MPR_TYPE used for MPR numeric variables. Will equate to
|
||||
* MPR_TYPE_INT, MPR_TYPE_INT64 or MPR_TYPE_FLOAT.
|
||||
*/
|
||||
#define MPR_NUM_VAR BLD_FEATURE_NUM_TYPE_ID
|
||||
#define MPR_TYPE_NUM BLD_FEATURE_NUM_TYPE_ID
|
||||
|
||||
/*
|
||||
* Return TRUE if a variable is a function type
|
||||
*/
|
||||
#define mprVarIsFunction(type) \
|
||||
(type == MPR_TYPE_FUNCTION || type == MPR_TYPE_STRING_CFUNCTION || \
|
||||
type == MPR_TYPE_CFUNCTION)
|
||||
|
||||
/*
|
||||
* Return TRUE if a variable is a numeric type
|
||||
*/
|
||||
#define mprVarIsNumber(type) \
|
||||
(type == MPR_TYPE_INT || type == MPR_TYPE_INT64 || type == MPR_TYPE_FLOAT)
|
||||
|
||||
/*
|
||||
* Return TRUE if a variable is a boolean
|
||||
*/
|
||||
#define mprVarIsBoolean(type) \
|
||||
(type == MPR_TYPE_BOOL)
|
||||
#define mprVarIsString(type) \
|
||||
(type == MPR_TYPE_STRING)
|
||||
#define mprVarIsObject(type) \
|
||||
(type == MPR_TYPE_OBJECT)
|
||||
#define mprVarIsFloating(type) \
|
||||
(type == MPR_TYPE_FLOAT)
|
||||
#define mprVarIsPtr(type) \
|
||||
(type == MPR_TYPE_PTR)
|
||||
#define mprVarIsUndefined(var) \
|
||||
((var)->type == MPR_TYPE_UNDEFINED)
|
||||
#define mprVarIsNull(var) \
|
||||
((var)->type == MPR_TYPE_NULL)
|
||||
#define mprVarIsValid(var) \
|
||||
(((var)->type != MPR_TYPE_NULL) && ((var)->type != MPR_TYPE_UNDEFINED))
|
||||
|
||||
#define MPR_VAR_MAX_RECURSE 5 /* Max object loops */
|
||||
|
||||
#if BLD_FEATURE_SQUEEZE
|
||||
#define MPR_MAX_VAR 64 /* Max var full name */
|
||||
#else
|
||||
#define MPR_MAX_VAR 512
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Function signatures
|
||||
*/
|
||||
typedef int MprVarHandle;
|
||||
typedef int (*MprCFunction)(MprVarHandle userHandle, int argc,
|
||||
struct MprVar **argv);
|
||||
typedef int (*MprStringCFunction)(MprVarHandle userHandle, int argc,
|
||||
char **argv);
|
||||
|
||||
/*
|
||||
* Triggers
|
||||
*/
|
||||
typedef enum {
|
||||
MPR_VAR_WRITE, /* This property is being updated */
|
||||
MPR_VAR_READ, /* This property is being read */
|
||||
MPR_VAR_CREATE_PROPERTY, /* A property is being created */
|
||||
MPR_VAR_DELETE_PROPERTY, /* A property is being deleted */
|
||||
MPR_VAR_DELETE /* This object is being deleted */
|
||||
} MprVarTriggerOp;
|
||||
|
||||
/*
|
||||
* Trigger function return codes.
|
||||
*/
|
||||
typedef enum {
|
||||
MPR_TRIGGER_ABORT, /* Abort the current operation */
|
||||
MPR_TRIGGER_USE_NEW_VALUE, /* Proceed and use the newValue */
|
||||
MPR_TRIGGER_PROCEED /* Proceed with the operation */
|
||||
} MprVarTriggerStatus;
|
||||
|
||||
/*
|
||||
* The MprVarTrigger arguments have the following meaning:
|
||||
*
|
||||
* op The operation being performed. See MprVarTriggerOp.
|
||||
* parentProperties Pointer to the MprProperties structure.
|
||||
* vp Pointer to the property that registered the trigger.
|
||||
* newValue New value (see below for more details).
|
||||
* copyDepth Specify what data items to copy.
|
||||
*
|
||||
* For VAR_READ, newVar is set to a temporary variable that the trigger
|
||||
* function may assign a value to be returned instead of the actual
|
||||
* property value.
|
||||
* For VAR_WRITE, newValue holds the new value. The old existing value may be
|
||||
* accessed via vp.
|
||||
* For DELETE_PROPERTY, vp is the property being deleted. newValue is null.
|
||||
* For ADD_PROPERTY, vp is set to the property being added and newValue holds
|
||||
* the new value.
|
||||
*/
|
||||
typedef MprVarTriggerStatus (*MprVarTrigger)(MprVarTriggerOp op,
|
||||
struct MprProperties *parentProperties, struct MprVar *vp,
|
||||
struct MprVar *newValue, int copyDepth);
|
||||
|
||||
/*
|
||||
* mprCreateFunctionVar flags
|
||||
*/
|
||||
/** Use the alternate handle on function callbacks */
|
||||
#define MPR_VAR_ALT_HANDLE 0x1
|
||||
|
||||
/** Use the script handle on function callbacks */
|
||||
#define MPR_VAR_SCRIPT_HANDLE 0x2
|
||||
|
||||
/*
|
||||
* Useful define for the copyDepth argument
|
||||
*/
|
||||
/** Don't copy any data. Copy only the variable name */
|
||||
#define MPR_NO_COPY 0
|
||||
|
||||
/** Copy strings. Increment object reference counts. */
|
||||
#define MPR_SHALLOW_COPY 1
|
||||
|
||||
/** Copy strings and do complete object copies. */
|
||||
#define MPR_DEEP_COPY 2
|
||||
|
||||
/*
|
||||
* GetFirst / GetNext flags
|
||||
*/
|
||||
/** Step into data properties. */
|
||||
#define MPR_ENUM_DATA 0x1
|
||||
|
||||
/** Step into functions properties. */
|
||||
#define MPR_ENUM_FUNCTIONS 0x2
|
||||
|
||||
/*
|
||||
* Collection type to hold properties in an object
|
||||
*/
|
||||
typedef struct MprProperties { /* Collection of properties */
|
||||
#if VAR_DEBUG
|
||||
struct MprProperties *next; /* Linked list */
|
||||
struct MprProperties *prev; /* Linked list */
|
||||
char name[32]; /* Debug name */
|
||||
#endif
|
||||
struct MprVar **buckets; /* Hash chains */
|
||||
int numItems; /* Total count of items */
|
||||
/* FUTURE - Better way of doing this */
|
||||
int numDataItems; /* Enumerable data items */
|
||||
uint hashSize : 8; /* Size of the hash table */
|
||||
/* FUTURE -- increase size of refCount */
|
||||
uint refCount : 8; /* References to this property*/
|
||||
/* FUTURE - make these flags */
|
||||
uint deleteProtect : 8; /* Don't recursively delete */
|
||||
uint visited : 8; /* Node has been processed */
|
||||
} MprProperties;
|
||||
|
||||
/*
|
||||
* Universal Variable Type
|
||||
*/
|
||||
typedef struct MprVar {
|
||||
/* FUTURE - remove name to outside reference */
|
||||
MprStr name; /* Property name */
|
||||
/* FUTURE - remove */
|
||||
MprStr fullName; /* Full object name */
|
||||
/* FUTURE - make part of the union */
|
||||
MprProperties *properties; /* Pointer to properties */
|
||||
|
||||
/*
|
||||
* Packed bit field
|
||||
*/
|
||||
MprType type : 8; /* Selector into union */
|
||||
uint bucketIndex : 8; /* Copy of bucket index */
|
||||
|
||||
uint flags : 5; /* Type specific flags */
|
||||
uint allocatedData : 1; /* Data needs freeing */
|
||||
uint readonly : 1; /* Unmodifiable */
|
||||
uint deleteProtect : 1; /* Don't recursively delete */
|
||||
|
||||
uint visited : 1; /* Node has been processed */
|
||||
uint allocatedVar : 1; /* Var needs freeing */
|
||||
uint spare : 6; /* Unused */
|
||||
|
||||
struct MprVar *forw; /* Hash table linkage */
|
||||
MprVarTrigger trigger; /* Trigger function */
|
||||
|
||||
#if UNUSED && KEEP
|
||||
struct MprVar *baseClass; /* Pointer to class object */
|
||||
#endif
|
||||
MprProperties *parentProperties; /* Pointer to parent object */
|
||||
|
||||
/*
|
||||
* Union of primitive types. When debugging on Linux, don't use unions
|
||||
* as the gdb debugger can't display them.
|
||||
*/
|
||||
#if 0 && !BLD_DEBUG && !LINUX && !VXWORKS
|
||||
union {
|
||||
#endif
|
||||
bool boolean;
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
double floating;
|
||||
#endif
|
||||
int integer;
|
||||
#if BLD_FEATURE_INT64
|
||||
int64 integer64;
|
||||
#endif
|
||||
struct { /* Javascript functions */
|
||||
MprArray *args; /* Null terminated */
|
||||
char *body;
|
||||
} function;
|
||||
struct { /* Function with MprVar args */
|
||||
MprCFunction fn;
|
||||
void *thisPtr;
|
||||
} cFunction;
|
||||
struct { /* Function with string args */
|
||||
MprStringCFunction fn;
|
||||
void *thisPtr;
|
||||
} cFunctionWithStrings;
|
||||
MprStr string; /* Allocated string */
|
||||
void *ptr; /* Opaque pointer */
|
||||
#if 0 && !BLD_DEBUG && !LINUX && !VXWORKS
|
||||
};
|
||||
#endif
|
||||
} MprVar;
|
||||
|
||||
/*
|
||||
* Define a field macro so code an use numbers in a "generic" fashion.
|
||||
*/
|
||||
#if MPR_NUM_VAR == MPR_TYPE_INT || DOXYGEN
|
||||
/* Default numeric type */
|
||||
#define mprNumber integer
|
||||
#endif
|
||||
#if MPR_NUM_VAR == MPR_TYPE_INT64
|
||||
/* Default numeric type */
|
||||
#define mprNumber integer64
|
||||
#endif
|
||||
#if MPR_NUM_VAR == MPR_TYPE_FLOAT
|
||||
/* Default numeric type */
|
||||
#define mprNumber floating
|
||||
#endif
|
||||
|
||||
typedef BLD_FEATURE_NUM_TYPE MprNumber;
|
||||
|
||||
/********************************* Prototypes *********************************/
|
||||
/*
|
||||
* Variable constructors and destructors
|
||||
*/
|
||||
extern MprVar mprCreateObjVar(const char *name, int hashSize);
|
||||
extern MprVar mprCreateBoolVar(bool value);
|
||||
extern MprVar mprCreateCFunctionVar(MprCFunction fn, void *thisPtr,
|
||||
int flags);
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
extern MprVar mprCreateFloatVar(double value);
|
||||
#endif
|
||||
extern MprVar mprCreateIntegerVar(int value);
|
||||
#if BLD_FEATURE_INT64
|
||||
extern MprVar mprCreateInteger64Var(int64 value);
|
||||
#endif
|
||||
extern MprVar mprCreateFunctionVar(char *args, char *body, int flags);
|
||||
extern MprVar mprCreateNullVar(void);
|
||||
extern MprVar mprCreateNumberVar(MprNumber value);
|
||||
extern MprVar mprCreateStringCFunctionVar(MprStringCFunction fn,
|
||||
void *thisPtr, int flags);
|
||||
extern MprVar mprCreateStringVar(const char *value, bool allocate);
|
||||
extern MprVar mprCreateUndefinedVar(void);
|
||||
extern MprVar mprCreatePtrVar(void *ptr);
|
||||
extern bool mprDestroyVar(MprVar *vp);
|
||||
extern bool mprDestroyAllVars(MprVar* vp);
|
||||
extern MprType mprGetVarType(MprVar *vp);
|
||||
|
||||
/*
|
||||
* Copy
|
||||
*/
|
||||
extern void mprCopyVar(MprVar *dest, MprVar *src, int copyDepth);
|
||||
extern void mprCopyVarValue(MprVar *dest, MprVar src, int copyDepth);
|
||||
extern MprVar *mprDupVar(MprVar *src, int copyDepth);
|
||||
|
||||
/*
|
||||
* Manage vars
|
||||
*/
|
||||
extern MprVarTrigger
|
||||
mprAddVarTrigger(MprVar *vp, MprVarTrigger fn);
|
||||
extern int mprGetVarRefCount(MprVar *vp);
|
||||
extern void mprSetVarDeleteProtect(MprVar *vp, int deleteProtect);
|
||||
extern void mprSetVarFullName(MprVar *vp, char *name);
|
||||
extern void mprSetVarReadonly(MprVar *vp, int readonly);
|
||||
extern void mprSetVarName(MprVar *vp, char *name);
|
||||
|
||||
/*
|
||||
* Create properties and return a reference to the property.
|
||||
*/
|
||||
extern MprVar *mprCreateProperty(MprVar *obj, const char *property,
|
||||
MprVar *newValue);
|
||||
extern MprVar *mprCreatePropertyValue(MprVar *obj, const char *property,
|
||||
MprVar newValue);
|
||||
extern int mprDeleteProperty(MprVar *obj, const char *property);
|
||||
|
||||
/*
|
||||
* Get/Set properties. Set will update/create.
|
||||
*/
|
||||
extern MprVar *mprGetProperty(MprVar *obj, const char *property,
|
||||
MprVar *value);
|
||||
extern MprVar *mprSetProperty(MprVar *obj, const char *property,
|
||||
MprVar *value);
|
||||
extern MprVar *mprSetPropertyValue(MprVar *obj, const char *property,
|
||||
MprVar value);
|
||||
|
||||
/*
|
||||
* Directly read/write property values (the property must already exist)
|
||||
* For mprCopyProperty, mprDestroyVar must always called on the var.
|
||||
*/
|
||||
extern int mprReadProperty(MprVar *prop, MprVar *value);
|
||||
extern int mprWriteProperty(MprVar *prop, MprVar *newValue);
|
||||
extern int mprWritePropertyValue(MprVar *prop, MprVar newValue);
|
||||
|
||||
/*
|
||||
* Copy a property. NOTE: reverse of most other args: (dest, src)
|
||||
*/
|
||||
extern int mprCopyProperty(MprVar *dest, MprVar *prop, int copyDepth);
|
||||
|
||||
/*
|
||||
* Enumerate properties
|
||||
*/
|
||||
extern MprVar *mprGetFirstProperty(MprVar *obj, int includeFlags);
|
||||
extern MprVar *mprGetNextProperty(MprVar *obj, MprVar *currentProperty,
|
||||
int includeFlags);
|
||||
|
||||
/*
|
||||
* Query properties characteristics
|
||||
*/
|
||||
extern int mprGetPropertyCount(MprVar *obj, int includeFlags);
|
||||
|
||||
/*
|
||||
* Conversion routines
|
||||
*/
|
||||
extern MprVar mprParseVar(char *str, MprType prefType);
|
||||
extern MprNum mprVarToNumber(const MprVar *vp);
|
||||
extern int mprVarToInteger(const MprVar *vp);
|
||||
#if BLD_FEATURE_INT64
|
||||
extern int64 mprVarToInteger64(const MprVar *vp);
|
||||
#endif
|
||||
extern bool mprVarToBool(const MprVar *vp);
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
extern double mprVarToFloat(const MprVar *vp);
|
||||
#endif
|
||||
extern void mprVarToString(char** buf, int size, char *fmt, MprVar *vp);
|
||||
|
||||
/*
|
||||
* Parsing and utility routines
|
||||
*/
|
||||
extern MprNum mprParseNumber(char *str);
|
||||
extern int mprParseInteger(char *str);
|
||||
|
||||
#if BLD_FEATURE_INT64
|
||||
extern int64 mprParseInteger64(char *str);
|
||||
#endif
|
||||
|
||||
#if BLD_FEATURE_FLOATING_POINT
|
||||
extern double mprParseFloat(char *str);
|
||||
extern bool mprIsInfinite(double f);
|
||||
extern bool mprIsNan(double f);
|
||||
#endif
|
||||
|
||||
#if VAR_DEBUG
|
||||
extern void mprPrintObjects(char *msg);
|
||||
extern void mprPrintObjRefCount(MprVar *vp);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/*****************************************************************************/
|
||||
#endif /* _h_MPR_VAR */
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
* vim:tw=78
|
||||
* vim600: sw=4 ts=4 fdm=marker
|
||||
* vim<600: sw=4 ts=4
|
||||
*/
|
||||
@@ -0,0 +1,49 @@
|
||||
# LIB BASIC subsystem
|
||||
include samba3/config.mk
|
||||
include socket/config.mk
|
||||
include charset/config.mk
|
||||
include ldb/config.mk
|
||||
include talloc/config.mk
|
||||
include tdb/config.mk
|
||||
include tls/config.mk
|
||||
include registry/config.mk
|
||||
include messaging/config.mk
|
||||
include events/config.mk
|
||||
include cmdline/config.mk
|
||||
include socket_wrapper/config.mk
|
||||
include appweb/config.mk
|
||||
include stream/config.mk
|
||||
include util/config.mk
|
||||
include tdr/config.mk
|
||||
|
||||
##############################
|
||||
# Start SUBSYSTEM LIBCRYPTO
|
||||
[SUBSYSTEM::LIBCRYPTO]
|
||||
OBJ_FILES = \
|
||||
crypto/crc32.o \
|
||||
crypto/md5.o \
|
||||
crypto/hmacmd5.o \
|
||||
crypto/md4.o \
|
||||
crypto/arcfour.o \
|
||||
crypto/sha1.o \
|
||||
crypto/hmacsha1.o
|
||||
# End SUBSYSTEM LIBCRYPTO
|
||||
##############################
|
||||
|
||||
################################################
|
||||
# Start SUBSYSTEM LIBCOMPRESSION
|
||||
[SUBSYSTEM::LIBCOMPRESSION]
|
||||
OBJ_FILES = compression/mszip.o
|
||||
# End SUBSYSTEM LIBCOMPRESION
|
||||
################################################
|
||||
|
||||
[SUBSYSTEM::GENCACHE]
|
||||
PRIVATE_PROTO_HEADER = gencache/gencache.h
|
||||
OBJ_FILES = gencache/gencache.o \
|
||||
|
||||
[SUBSYSTEM::DB_WRAP]
|
||||
PUBLIC_PROTO_HEADER = db_wrap_proto.h
|
||||
PUBLIC_HEADERS = db_wrap.h
|
||||
OBJ_FILES = db_wrap.o gendb.o
|
||||
PUBLIC_DEPENDENCIES = LIBTDB ldb
|
||||
PRIVATE_DEPENDENCIES = LDBSAMBA
|
||||
@@ -0,0 +1,720 @@
|
||||
/*
|
||||
Unix SMB/CIFS implementation.
|
||||
Character set conversion Extensions
|
||||
Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
|
||||
Copyright (C) Andrew Tridgell 2001
|
||||
Copyright (C) Simo Sorce 2001
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
#include "includes.h"
|
||||
#include "system/iconv.h"
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief Character-set conversion routines built on our iconv.
|
||||
*
|
||||
* @note Samba's internal character set (at least in the 3.0 series)
|
||||
* is always the same as the one for the Unix filesystem. It is
|
||||
* <b>not</b> necessarily UTF-8 and may be different on machines that
|
||||
* need i18n filenames to be compatible with Unix software. It does
|
||||
* have to be a superset of ASCII. All multibyte sequences must start
|
||||
* with a byte with the high bit set.
|
||||
*
|
||||
* @sa lib/iconv.c
|
||||
*/
|
||||
|
||||
/**
|
||||
* Return the name of a charset to give to iconv().
|
||||
**/
|
||||
static const char *charset_name(charset_t ch)
|
||||
{
|
||||
switch (ch) {
|
||||
case CH_UTF16: return "UTF-16LE";
|
||||
case CH_UNIX: return lp_unix_charset();
|
||||
case CH_DOS: return lp_dos_charset();
|
||||
case CH_DISPLAY: return lp_display_charset();
|
||||
case CH_UTF8: return "UTF8";
|
||||
case CH_UTF16BE: return "UTF-16BE";
|
||||
default:
|
||||
return "ASCII";
|
||||
}
|
||||
}
|
||||
|
||||
static smb_iconv_t conv_handles[NUM_CHARSETS][NUM_CHARSETS];
|
||||
|
||||
/**
|
||||
re-initialize iconv conversion descriptors
|
||||
**/
|
||||
_PUBLIC_ void init_iconv(void)
|
||||
{
|
||||
unsigned c1, c2;
|
||||
for (c1=0;c1<NUM_CHARSETS;c1++) {
|
||||
for (c2=0;c2<NUM_CHARSETS;c2++) {
|
||||
if (conv_handles[c1][c2] != NULL) {
|
||||
if (conv_handles[c1][c2] != (smb_iconv_t)-1) {
|
||||
smb_iconv_close(conv_handles[c1][c2]);
|
||||
}
|
||||
conv_handles[c1][c2] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
on-demand initialisation of conversion handles
|
||||
*/
|
||||
static smb_iconv_t get_conv_handle(charset_t from, charset_t to)
|
||||
{
|
||||
const char *n1, *n2;
|
||||
static int initialised;
|
||||
/* auto-free iconv memory on exit so valgrind reports are easier
|
||||
to look at */
|
||||
if (initialised == 0) {
|
||||
initialised = 1;
|
||||
|
||||
#ifdef LC_ALL
|
||||
/* we set back the locale to C to get ASCII-compatible
|
||||
toupper/lower functions. For now we do not need
|
||||
any other POSIX localisations anyway. When we
|
||||
should really need localized string functions one
|
||||
day we need to write our own ascii_tolower etc.
|
||||
*/
|
||||
setlocale(LC_ALL, "C");
|
||||
#endif
|
||||
|
||||
atexit(init_iconv);
|
||||
}
|
||||
|
||||
if (conv_handles[from][to]) {
|
||||
return conv_handles[from][to];
|
||||
}
|
||||
|
||||
n1 = charset_name(from);
|
||||
n2 = charset_name(to);
|
||||
|
||||
conv_handles[from][to] = smb_iconv_open(n2,n1);
|
||||
|
||||
if (conv_handles[from][to] == (smb_iconv_t)-1) {
|
||||
if ((from == CH_DOS || to == CH_DOS) &&
|
||||
strcasecmp(charset_name(CH_DOS), "ASCII") != 0) {
|
||||
DEBUG(0,("dos charset '%s' unavailable - using ASCII\n",
|
||||
charset_name(CH_DOS)));
|
||||
lp_set_cmdline("dos charset", "ASCII");
|
||||
|
||||
n1 = charset_name(from);
|
||||
n2 = charset_name(to);
|
||||
|
||||
conv_handles[from][to] = smb_iconv_open(n2,n1);
|
||||
}
|
||||
}
|
||||
|
||||
return conv_handles[from][to];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert string from one encoding to another, making error checking etc
|
||||
*
|
||||
* @param src pointer to source string (multibyte or singlebyte)
|
||||
* @param srclen length of the source string in bytes
|
||||
* @param dest pointer to destination string (multibyte or singlebyte)
|
||||
* @param destlen maximal length allowed for string
|
||||
* @returns the number of bytes occupied in the destination
|
||||
**/
|
||||
_PUBLIC_ ssize_t convert_string(charset_t from, charset_t to,
|
||||
void const *src, size_t srclen,
|
||||
void *dest, size_t destlen)
|
||||
{
|
||||
size_t i_len, o_len;
|
||||
size_t retval;
|
||||
const char* inbuf = (const char*)src;
|
||||
char* outbuf = (char*)dest;
|
||||
smb_iconv_t descriptor;
|
||||
|
||||
if (srclen == (size_t)-1)
|
||||
srclen = strlen(src)+1;
|
||||
|
||||
descriptor = get_conv_handle(from, to);
|
||||
|
||||
if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
|
||||
/* conversion not supported, use as is */
|
||||
size_t len = MIN(srclen,destlen);
|
||||
memcpy(dest,src,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
i_len=srclen;
|
||||
o_len=destlen;
|
||||
retval = smb_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len);
|
||||
if(retval==(size_t)-1) {
|
||||
const char *reason;
|
||||
switch(errno) {
|
||||
case EINVAL:
|
||||
reason="Incomplete multibyte sequence";
|
||||
return -1;
|
||||
case E2BIG:
|
||||
reason="No more room";
|
||||
if (from == CH_UNIX) {
|
||||
DEBUG(0,("E2BIG: convert_string(%s,%s): srclen=%d destlen=%d - '%s'\n",
|
||||
charset_name(from), charset_name(to),
|
||||
(int)srclen, (int)destlen,
|
||||
(const char *)src));
|
||||
} else {
|
||||
DEBUG(0,("E2BIG: convert_string(%s,%s): srclen=%d destlen=%d\n",
|
||||
charset_name(from), charset_name(to),
|
||||
(int)srclen, (int)destlen));
|
||||
}
|
||||
return -1;
|
||||
case EILSEQ:
|
||||
reason="Illegal multibyte sequence";
|
||||
return -1;
|
||||
}
|
||||
/* smb_panic(reason); */
|
||||
}
|
||||
return destlen-o_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert between character sets, allocating a new buffer using talloc for the result.
|
||||
*
|
||||
* @param srclen length of source buffer.
|
||||
* @param dest always set at least to NULL
|
||||
* @note -1 is not accepted for srclen.
|
||||
*
|
||||
* @returns Size in bytes of the converted string; or -1 in case of error.
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to,
|
||||
void const *src, size_t srclen, void **dest)
|
||||
{
|
||||
size_t i_len, o_len, destlen;
|
||||
size_t retval;
|
||||
const char *inbuf = (const char *)src;
|
||||
char *outbuf, *ob;
|
||||
smb_iconv_t descriptor;
|
||||
|
||||
*dest = NULL;
|
||||
|
||||
if (src == NULL || srclen == (size_t)-1 || srclen == 0)
|
||||
return (size_t)-1;
|
||||
|
||||
descriptor = get_conv_handle(from, to);
|
||||
|
||||
if (descriptor == (smb_iconv_t)-1 || descriptor == (smb_iconv_t)0) {
|
||||
/* conversion not supported, return -1*/
|
||||
DEBUG(3, ("convert_string_talloc: conversion from %s to %s not supported!\n",
|
||||
charset_name(from), charset_name(to)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* it is _very_ rare that a conversion increases the size by
|
||||
more than 3x */
|
||||
destlen = srclen;
|
||||
outbuf = NULL;
|
||||
convert:
|
||||
destlen = 2 + (destlen*3);
|
||||
ob = talloc_realloc(ctx, outbuf, char, destlen);
|
||||
if (!ob) {
|
||||
DEBUG(0, ("convert_string_talloc: realloc failed!\n"));
|
||||
talloc_free(outbuf);
|
||||
return (size_t)-1;
|
||||
} else {
|
||||
outbuf = ob;
|
||||
}
|
||||
|
||||
/* we give iconv 2 less bytes to allow us to terminate at the
|
||||
end */
|
||||
i_len = srclen;
|
||||
o_len = destlen-2;
|
||||
retval = smb_iconv(descriptor,
|
||||
&inbuf, &i_len,
|
||||
&outbuf, &o_len);
|
||||
if(retval == (size_t)-1) {
|
||||
const char *reason="unknown error";
|
||||
switch(errno) {
|
||||
case EINVAL:
|
||||
reason="Incomplete multibyte sequence";
|
||||
break;
|
||||
case E2BIG:
|
||||
goto convert;
|
||||
case EILSEQ:
|
||||
reason="Illegal multibyte sequence";
|
||||
break;
|
||||
}
|
||||
DEBUG(0,("Conversion error: %s(%s)\n",reason,inbuf));
|
||||
talloc_free(ob);
|
||||
return (size_t)-1;
|
||||
}
|
||||
|
||||
destlen = (destlen-2) - o_len;
|
||||
|
||||
/* guarantee null termination in all charsets */
|
||||
SSVAL(ob, destlen, 0);
|
||||
|
||||
*dest = ob;
|
||||
|
||||
return destlen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a char* unix src to a dos codepage string destination.
|
||||
*
|
||||
* @return the number of bytes occupied by the string in the destination.
|
||||
*
|
||||
* @param flags can include
|
||||
* <dl>
|
||||
* <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
|
||||
* <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
|
||||
* </dl>
|
||||
*
|
||||
* @param dest_len the maximum length in bytes allowed in the
|
||||
* destination. If @p dest_len is -1 then no maximum is used.
|
||||
**/
|
||||
static ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
|
||||
{
|
||||
size_t src_len;
|
||||
ssize_t ret;
|
||||
|
||||
if (flags & STR_UPPER) {
|
||||
char *tmpbuf = strupper_talloc(NULL, src);
|
||||
if (tmpbuf == NULL) {
|
||||
return -1;
|
||||
}
|
||||
ret = push_ascii(dest, tmpbuf, dest_len, flags & ~STR_UPPER);
|
||||
talloc_free(tmpbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
src_len = strlen(src);
|
||||
|
||||
if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
|
||||
src_len++;
|
||||
|
||||
return convert_string(CH_UNIX, CH_DOS, src, src_len, dest, dest_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a unix char* src to an ASCII destination,
|
||||
* allocating a buffer using talloc().
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
* or -1 in case of error.
|
||||
**/
|
||||
_PUBLIC_ ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
|
||||
{
|
||||
size_t src_len = strlen(src)+1;
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_UNIX, CH_DOS, src, src_len, (void **)dest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copy a string from a dos codepage source to a unix char* destination.
|
||||
*
|
||||
* The resulting string in "dest" is always null terminated.
|
||||
*
|
||||
* @param flags can have:
|
||||
* <dl>
|
||||
* <dt>STR_TERMINATE</dt>
|
||||
* <dd>STR_TERMINATE means the string in @p src
|
||||
* is null terminated, and src_len is ignored.</dd>
|
||||
* </dl>
|
||||
*
|
||||
* @param src_len is the length of the source area in bytes.
|
||||
* @returns the number of bytes occupied by the string in @p src.
|
||||
**/
|
||||
static ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
|
||||
if (src_len == (size_t)-1) {
|
||||
src_len = strlen(src) + 1;
|
||||
} else {
|
||||
size_t len = strnlen(src, src_len);
|
||||
if (len < src_len)
|
||||
len++;
|
||||
src_len = len;
|
||||
}
|
||||
}
|
||||
|
||||
ret = convert_string(CH_DOS, CH_UNIX, src, src_len, dest, dest_len);
|
||||
|
||||
if (dest_len)
|
||||
dest[MIN(ret, dest_len-1)] = 0;
|
||||
|
||||
return src_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a char* src to a unicode destination.
|
||||
*
|
||||
* @returns the number of bytes occupied by the string in the destination.
|
||||
*
|
||||
* @param flags can have:
|
||||
*
|
||||
* <dl>
|
||||
* <dt>STR_TERMINATE <dd>means include the null termination.
|
||||
* <dt>STR_UPPER <dd>means uppercase in the destination.
|
||||
* <dt>STR_NOALIGN <dd>means don't do alignment.
|
||||
* </dl>
|
||||
*
|
||||
* @param dest_len is the maximum length allowed in the
|
||||
* destination. If dest_len is -1 then no maxiumum is used.
|
||||
**/
|
||||
static ssize_t push_ucs2(void *dest, const char *src, size_t dest_len, int flags)
|
||||
{
|
||||
size_t len=0;
|
||||
size_t src_len = strlen(src);
|
||||
size_t ret;
|
||||
|
||||
if (flags & STR_UPPER) {
|
||||
char *tmpbuf = strupper_talloc(NULL, src);
|
||||
if (tmpbuf == NULL) {
|
||||
return -1;
|
||||
}
|
||||
ret = push_ucs2(dest, tmpbuf, dest_len, flags & ~STR_UPPER);
|
||||
talloc_free(tmpbuf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (flags & STR_TERMINATE)
|
||||
src_len++;
|
||||
|
||||
if (ucs2_align(NULL, dest, flags)) {
|
||||
*(char *)dest = 0;
|
||||
dest = (void *)((char *)dest + 1);
|
||||
if (dest_len) dest_len--;
|
||||
len++;
|
||||
}
|
||||
|
||||
/* ucs2 is always a multiple of 2 bytes */
|
||||
dest_len &= ~1;
|
||||
|
||||
ret = convert_string(CH_UNIX, CH_UTF16, src, src_len, dest, dest_len);
|
||||
if (ret == (size_t)-1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
len += ret;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copy a string from a unix char* src to a UCS2 destination,
|
||||
* allocating a buffer using talloc().
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
* or -1 in case of error.
|
||||
**/
|
||||
_PUBLIC_ ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src)
|
||||
{
|
||||
size_t src_len = strlen(src)+1;
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_UNIX, CH_UTF16, src, src_len, dest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
|
||||
{
|
||||
size_t src_len = strlen(src)+1;
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_UNIX, CH_UTF8, src, src_len, (void **)dest);
|
||||
}
|
||||
|
||||
/**
|
||||
Copy a string from a ucs2 source to a unix char* destination.
|
||||
Flags can have:
|
||||
STR_TERMINATE means the string in src is null terminated.
|
||||
STR_NOALIGN means don't try to align.
|
||||
if STR_TERMINATE is set then src_len is ignored if it is -1.
|
||||
src_len is the length of the source area in bytes
|
||||
Return the number of bytes occupied by the string in src.
|
||||
The resulting string in "dest" is always null terminated.
|
||||
**/
|
||||
|
||||
static size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
if (ucs2_align(NULL, src, flags)) {
|
||||
src = (const void *)((const char *)src + 1);
|
||||
if (src_len > 0)
|
||||
src_len--;
|
||||
}
|
||||
|
||||
if (flags & STR_TERMINATE) {
|
||||
if (src_len == (size_t)-1) {
|
||||
src_len = utf16_len(src);
|
||||
} else {
|
||||
src_len = utf16_len_n(src, src_len);
|
||||
}
|
||||
}
|
||||
|
||||
/* ucs2 is always a multiple of 2 bytes */
|
||||
if (src_len != (size_t)-1)
|
||||
src_len &= ~1;
|
||||
|
||||
ret = convert_string(CH_UTF16, CH_UNIX, src, src_len, dest, dest_len);
|
||||
if (dest_len)
|
||||
dest[MIN(ret, dest_len-1)] = 0;
|
||||
|
||||
return src_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a ASCII src to a unix char * destination, allocating a buffer using talloc
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
|
||||
{
|
||||
size_t src_len = strlen(src)+1;
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_DOS, CH_UNIX, src, src_len, (void **)dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const void *src)
|
||||
{
|
||||
size_t src_len = utf16_len(src);
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_UTF16, CH_UNIX, src, src_len, (void **)dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
|
||||
*
|
||||
* @param dest always set at least to NULL
|
||||
*
|
||||
* @returns The number of bytes occupied by the string in the destination
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
|
||||
{
|
||||
size_t src_len = strlen(src)+1;
|
||||
*dest = NULL;
|
||||
return convert_string_talloc(ctx, CH_UTF8, CH_UNIX, src, src_len, (void **)dest);
|
||||
}
|
||||
|
||||
/**
|
||||
Copy a string from a char* src to a unicode or ascii
|
||||
dos codepage destination choosing unicode or ascii based on the
|
||||
flags in the SMB buffer starting at base_ptr.
|
||||
Return the number of bytes occupied by the string in the destination.
|
||||
flags can have:
|
||||
STR_TERMINATE means include the null termination.
|
||||
STR_UPPER means uppercase in the destination.
|
||||
STR_ASCII use ascii even with unicode packet.
|
||||
STR_NOALIGN means don't do alignment.
|
||||
dest_len is the maximum length allowed in the destination. If dest_len
|
||||
is -1 then no maxiumum is used.
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags)
|
||||
{
|
||||
if (flags & STR_ASCII) {
|
||||
return push_ascii(dest, src, dest_len, flags);
|
||||
} else if (flags & STR_UNICODE) {
|
||||
return push_ucs2(dest, src, dest_len, flags);
|
||||
} else {
|
||||
smb_panic("push_string requires either STR_ASCII or STR_UNICODE flag to be set");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Copy a string from a unicode or ascii source (depending on
|
||||
the packet flags) to a char* destination.
|
||||
Flags can have:
|
||||
STR_TERMINATE means the string in src is null terminated.
|
||||
STR_UNICODE means to force as unicode.
|
||||
STR_ASCII use ascii even with unicode packet.
|
||||
STR_NOALIGN means don't do alignment.
|
||||
if STR_TERMINATE is set then src_len is ignored is it is -1
|
||||
src_len is the length of the source area in bytes.
|
||||
Return the number of bytes occupied by the string in src.
|
||||
The resulting string in "dest" is always null terminated.
|
||||
**/
|
||||
|
||||
_PUBLIC_ ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
|
||||
{
|
||||
if (flags & STR_ASCII) {
|
||||
return pull_ascii(dest, src, dest_len, src_len, flags);
|
||||
} else if (flags & STR_UNICODE) {
|
||||
return pull_ucs2(dest, src, dest_len, src_len, flags);
|
||||
} else {
|
||||
smb_panic("pull_string requires either STR_ASCII or STR_UNICODE flag to be set");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
return the unicode codepoint for the next multi-byte CH_UNIX character
|
||||
in the string
|
||||
|
||||
also return the number of bytes consumed (which tells the caller
|
||||
how many bytes to skip to get to the next CH_UNIX character)
|
||||
|
||||
return INVALID_CODEPOINT if the next character cannot be converted
|
||||
*/
|
||||
_PUBLIC_ codepoint_t next_codepoint(const char *str, size_t *size)
|
||||
{
|
||||
/* it cannot occupy more than 4 bytes in UTF16 format */
|
||||
uint8_t buf[4];
|
||||
smb_iconv_t descriptor;
|
||||
size_t ilen_orig;
|
||||
size_t ilen;
|
||||
size_t olen;
|
||||
char *outbuf;
|
||||
|
||||
if ((str[0] & 0x80) == 0) {
|
||||
*size = 1;
|
||||
return (codepoint_t)str[0];
|
||||
}
|
||||
|
||||
/* we assume that no multi-byte character can take
|
||||
more than 5 bytes. This is OK as we only
|
||||
support codepoints up to 1M */
|
||||
ilen_orig = strnlen(str, 5);
|
||||
ilen = ilen_orig;
|
||||
|
||||
descriptor = get_conv_handle(CH_UNIX, CH_UTF16);
|
||||
if (descriptor == (smb_iconv_t)-1) {
|
||||
*size = 1;
|
||||
return INVALID_CODEPOINT;
|
||||
}
|
||||
|
||||
/* this looks a little strange, but it is needed to cope
|
||||
with codepoints above 64k */
|
||||
olen = 2;
|
||||
outbuf = (char *)buf;
|
||||
smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
|
||||
if (olen == 2) {
|
||||
olen = 4;
|
||||
outbuf = (char *)buf;
|
||||
smb_iconv(descriptor, &str, &ilen, &outbuf, &olen);
|
||||
if (olen == 4) {
|
||||
/* we didn't convert any bytes */
|
||||
*size = 1;
|
||||
return INVALID_CODEPOINT;
|
||||
}
|
||||
olen = 4 - olen;
|
||||
} else {
|
||||
olen = 2 - olen;
|
||||
}
|
||||
|
||||
*size = ilen_orig - ilen;
|
||||
|
||||
if (olen == 2) {
|
||||
return (codepoint_t)SVAL(buf, 0);
|
||||
}
|
||||
if (olen == 4) {
|
||||
/* decode a 4 byte UTF16 character manually */
|
||||
return (codepoint_t)0x10000 +
|
||||
(buf[2] | ((buf[3] & 0x3)<<8) |
|
||||
(buf[0]<<10) | ((buf[1] & 0x3)<<18));
|
||||
}
|
||||
|
||||
/* no other length is valid */
|
||||
return INVALID_CODEPOINT;
|
||||
}
|
||||
|
||||
/*
|
||||
push a single codepoint into a CH_UNIX string the target string must
|
||||
be able to hold the full character, which is guaranteed if it is at
|
||||
least 5 bytes in size. The caller may pass less than 5 bytes if they
|
||||
are sure the character will fit (for example, you can assume that
|
||||
uppercase/lowercase of a character will not add more than 1 byte)
|
||||
|
||||
return the number of bytes occupied by the CH_UNIX character, or
|
||||
-1 on failure
|
||||
*/
|
||||
_PUBLIC_ ssize_t push_codepoint(char *str, codepoint_t c)
|
||||
{
|
||||
smb_iconv_t descriptor;
|
||||
uint8_t buf[4];
|
||||
size_t ilen, olen;
|
||||
const char *inbuf;
|
||||
|
||||
if (c < 128) {
|
||||
*str = c;
|
||||
return 1;
|
||||
}
|
||||
|
||||
descriptor = get_conv_handle(CH_UTF16, CH_UNIX);
|
||||
if (descriptor == (smb_iconv_t)-1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (c < 0x10000) {
|
||||
ilen = 2;
|
||||
olen = 5;
|
||||
inbuf = (char *)buf;
|
||||
SSVAL(buf, 0, c);
|
||||
smb_iconv(descriptor, &inbuf, &ilen, &str, &olen);
|
||||
if (ilen != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 5 - olen;
|
||||
}
|
||||
|
||||
c -= 0x10000;
|
||||
|
||||
buf[0] = (c>>10) & 0xFF;
|
||||
buf[1] = (c>>18) | 0xd8;
|
||||
buf[2] = c & 0xFF;
|
||||
buf[3] = ((c>>8) & 0x3) | 0xdc;
|
||||
|
||||
ilen = 4;
|
||||
olen = 5;
|
||||
inbuf = (char *)buf;
|
||||
|
||||
smb_iconv(descriptor, &inbuf, &ilen, &str, &olen);
|
||||
if (ilen != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 5 - olen;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user