Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to call tcl procedure from php function

18 views
Skip to first unread message

gk

unread,
Jan 9, 2004, 4:17:18 PM1/9/04
to
Folks,

We do have lot of Tcl/Expect script which has already written, and for
userinterface ( web page), i would like to use php & html . So is it
possible to call tcl proc from the php function, with out using
(exec()).

Can any one show me the right direction to do so..

Thanks...

Cameron Laird

unread,
Jan 9, 2004, 4:30:41 PM1/9/04
to
In article <f9c9dd5a.04010...@posting.google.com>,

gk <gop...@rocketmail.com> wrote:
>Folks,
>
>We do have lot of Tcl/Expect script which has already written, and for
>userinterface ( web page), i would like to use php & html . So is it
>possible to call tcl proc from the php function, with out using
>(exec()).
.
.
.
That strikes me as a PHP question.

In any case, I think the answer is, "No." What do you have
against exec()? Is performance your concern?
--

Cameron Laird <cla...@phaseit.net>
Business: http://www.Phaseit.net

Derk Gwen

unread,
Jan 9, 2004, 8:03:34 PM1/9/04
to
gop...@rocketmail.com (gk) wrote:
# Folks,
#
# We do have lot of Tcl/Expect script which has already written, and for
# userinterface ( web page), i would like to use php & html . So is it
# possible to call tcl proc from the php function, with out using
# (exec()).
#
# Can any one show me the right direction to do so..

/*
PHP=/usr/local/src/php-4.0.4pl1
ZEND=/usr/local/src/php-4.0.4pl1/Zend
tclphp.o: $(GENERIC_DIR)/tclphp.c
$(CC) -w -DCOMPILE_DL=1 -I$(PHP) -I$(PHP)/main -I$(PHP)/TSRM -I$(ZEND) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclphp.c
tclphp.so: ${OBJS} tclphp.o ${STUB_LIB_FILE}
rm -f ${TCL_LIB_FILE}
${SHLIB_LD} -o tclphp.so ${OBJS} tclphp.o ${LIBS}
$(RANLIB) tclphp.so

<?php
//dl("tclphp.so");

$param = "MiXeD cAsE";
$return = tclphp("string tolower \{$param}");

print("We sent \"$param\" and got \"$return\"");
?>
*/
/*
tclphp [type] script

Standard Tcl plus one function:

tclphp.so
This function.
tcl8.3.so
*/

#define IS_EXT_MODULE

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "php.h"
#include "tcl.h"

static int isLong(Tcl_Obj *obj,long *lval) {
char *s = Tcl_GetStringFromObj(obj,0),*t;
*lval = strtol(s,&t,0);
return s!=t && !*t;
}
static Tcl_Interp *interp = 0;

/* declaration of functions to be exported */
ZEND_FUNCTION(tclphp);
ZEND_MINIT_FUNCTION(tclphp);
ZEND_MSHUTDOWN_FUNCTION(tclphp);

/* compiled function list so Zend knows what's in this module */
zend_function_entry tclphp_functions[] = {
ZEND_FE(tclphp,NULL)
{NULL,NULL,NULL}
};

/* compiled module information */
zend_module_entry tclphp_module_entry = {
"tclphp module",
tclphp_functions,
ZEND_MINIT(tclphp),ZEND_MSHUTDOWN(tclphp),NULL,NULL,NULL,
STANDARD_MODULE_PROPERTIES
};

/* implement standard "stub" routine to introduce ourselves to Zend */
#if COMPILE_DL
ZEND_GET_MODULE(tclphp)
#endif

ZEND_MINIT_FUNCTION(tclphp) {
char initialisation[] =
"source $tcl_pkgPath/tcl$tcl_version/init.tcl\n"
;
interp = Tcl_CreateInterp();
#ifdef TCL_MEM_DEBUG
Tcl_InitMemory(interp);
#endif
if (Tcl_Init(interp)==TCL_ERROR) {
Tcl_DString S;
Tcl_DStringInit(&S);
Tcl_DStringAppend(&S,"interpretter Tcl_Init failed: ",-1);
Tcl_DStringAppend(&S,Tcl_GetStringResult(interp),-1);
zend_error(E_WARNING,Tcl_DStringValue(&S));
Tcl_DStringFree(&S);
return FAILURE;
}
Tcl_SetVar(interp,"tcl_rcFileName","~/.tclshrc",TCL_GLOBAL_ONLY);
Tcl_SourceRCFile(interp);
if (Tcl_Eval(interp,initialisation)!=TCL_OK) {
Tcl_DString S;
Tcl_DStringInit(&S);
Tcl_DStringAppend(&S,"interpretter initialisation failed: ",-1);
Tcl_DStringAppend(&S,Tcl_GetStringResult(interp),-1);
Tcl_DStringAppend(&S,": ",-1);
Tcl_DStringAppend(&S,initialisation,-1);
zend_error(E_WARNING,Tcl_DStringValue(&S));
Tcl_DStringFree(&S);
return FAILURE;
}
return SUCCESS;
}

ZEND_MSHUTDOWN_FUNCTION(tclphp) {
Tcl_DeleteInterp(interp);
interp = 0;
return SUCCESS;
}

/* implement function that is meant to be made available to PHP */
ZEND_FUNCTION(tclphp) {
int argc = ZEND_NUM_ARGS();
zval **script;
zval **type;
char Type;
int rc;
Tcl_Obj *Script,*result;

/*
Argument processing.
*/
if (argc==1) {
if (zend_get_parameters_ex(1,&script)!=SUCCESS) WRONG_PARAM_COUNT;
Type = 's';
}else if (argc==2) {
if (zend_get_parameters_ex(2,&type,&script)!=SUCCESS) WRONG_PARAM_COUNT;
if (argc==2 && (*type)->type!=IS_STRING) convert_to_string_ex(type);
Type = tolower(Z_STRVAL_PP(type)[0]);
}else {
WRONG_PARAM_COUNT;
}
if ((*script)->type!=IS_STRING) {
convert_to_string_ex(script);
}

/*
Evaluate the script and coerce to the desired return value.
*/
Script = Tcl_NewStringObj(Z_STRVAL_PP(script),Z_STRLEN_PP(script));
Tcl_IncrRefCount(Script);
rc = Tcl_EvalObjEx(interp,Script,TCL_EVAL_DIRECT|TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(Script);
result = Tcl_GetObjResult(interp);
switch (rc==TCL_OK ? Type : 0) {
case 0: /*error*/ error: {
char *s = Tcl_GetStringResult(interp);
zend_error(E_WARNING,s);
RETVAL_NULL();
} break;
case 'd': /*double*/ {
double d;
if (Tcl_GetDoubleFromObj(interp,result,&d)!=TCL_OK) goto error;
RETVAL_DOUBLE(d);
} break;
case 'i': case 'l': /*long int*/ {
long l;
if (Tcl_GetLongFromObj(interp,result,&l)!=TCL_OK) goto error;
RETVAL_LONG(l);
} break;
case 'b': /*boolean*/ {
int b;
if (Tcl_GetBooleanFromObj(interp,result,&b)!=TCL_OK) goto error;
if (b) {RETVAL_TRUE;}
else {RETVAL_FALSE;}
} break;
case 'a': /*array*/ {
int N; Tcl_Obj **P;
if (Tcl_ListObjGetElements(interp,result,&N,&P)!=TCL_OK) goto error;
else if (N&1) {
Tcl_SetResult(interp,"returned list for array has odd number of elements",TCL_STATIC);
goto error;
}
array_init(return_value);
for (; N>0; N-=2,P+=2) {
long lval; int bval; double dval; char *sval; int slen;
if (isLong(P[0],&lval)) {
long index = lval;
if (isLong(P[1],&lval)) {
add_index_long(return_value,index,lval);
}else if (Tcl_GetDoubleFromObj(0,P[1],&dval)==TCL_OK) {
add_index_double(return_value,index,dval);
}else if (Tcl_GetLongFromObj(0,P[1],&lval)==TCL_OK) {
add_index_long(return_value,index,lval);
}else if (Tcl_GetBooleanFromObj(0,P[1],&bval)==TCL_OK) {
add_index_long(return_value,index,(long)bval);
}else {
sval = Tcl_GetStringFromObj(P[1],&slen);
add_index_stringl(return_value,index,sval,slen,1);
}
}else {
char *key = Tcl_GetStringFromObj(P[0],0);
if (isLong(P[1],&lval)) {
add_assoc_long(return_value,key,lval);
}else if (Tcl_GetDoubleFromObj(0,P[1],&dval)==TCL_OK) {
add_assoc_double(return_value,key,dval);
}else if (Tcl_GetLongFromObj(0,P[1],&lval)==TCL_OK) {
add_assoc_long(return_value,key,lval);
}else if (Tcl_GetBooleanFromObj(0,P[1],&bval)==TCL_OK) {
add_assoc_long(return_value,key,(long)bval);
}else {
sval = Tcl_GetStringFromObj(P[1],&slen);
add_assoc_stringl(return_value,key,sval,slen,1);
}
}
}
} break;
default: /*string*/ {
int n; char *s = Tcl_GetStringFromObj(result,&n);
RETVAL_STRINGL(s,n,1);
} break;
}

/*
Clean up and quit.
*/
Tcl_ResetResult(interp);
return;
}

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.

David N. Welton

unread,
Jan 9, 2004, 8:08:02 PM1/9/04
to
gop...@rocketmail.com (gk) writes:

> We do have lot of Tcl/Expect script which has already written, and
> for userinterface ( web page), i would like to use php & html . So
> is it possible to call tcl proc from the php function, with out
> using (exec()).

What's wrong with creating your web pages in Tcl, with Rivet, Websh,
tclhttpd, AOLserver, or something else?

--
David N. Welton
Consulting: http://www.dedasys.com/
Personal: http://www.dedasys.com/davidw/
Free Software: http://www.dedasys.com/freesoftware/
Apache Tcl: http://tcl.apache.org/

Derk Gwen

unread,
Jan 9, 2004, 9:58:43 PM1/9/04
to
dav...@dedasys.com (David N. Welton) wrote:
# gop...@rocketmail.com (gk) writes:
#
# > We do have lot of Tcl/Expect script which has already written, and
# > for userinterface ( web page), i would like to use php & html . So
# > is it possible to call tcl proc from the php function, with out
# > using (exec()).
#
# What's wrong with creating your web pages in Tcl, with Rivet, Websh,
# tclhttpd, AOLserver, or something else?

What's wrong with writing your general tools in PHP?

I'll use C or Tcl or PHP or ECMAscript or Fortran or awk or sed
or perl or whatever gets the job done. (I will not use VB or C#
or other microsoft abominations.)

OOOOOOOOOO! NAVY SEALS!

Wojciech Kocjan

unread,
Jan 10, 2004, 5:48:04 AM1/10/04
to
Derk Gwen wrote:

> dav...@dedasys.com (David N. Welton) wrote:
> # gop...@rocketmail.com (gk) writes:
> #
> # > We do have lot of Tcl/Expect script which has already written, and
> # > for userinterface ( web page), i would like to use php & html . So
> # > is it possible to call tcl proc from the php function, with out
> # > using (exec()).
> #
> # What's wrong with creating your web pages in Tcl, with Rivet, Websh,
> # tclhttpd, AOLserver, or something else?
>
> What's wrong with writing your general tools in PHP?

In this case is would be a lot wiser to use Tcl, since parts of the code
already use Tcl.

However, in this case I'd consider using client-server solution, since
I'm not sure if the expect script could be run in several instances at
once. Also, if the page works on apache, loading expect in (eventually)
most of the processes would bloat memory.

If it's any kind of production environment, I'd suggest either making
expect run with tclhttpd and use PHP's http functions or writing a small
protocol.

I've done a small protocol for a spam blocking protection in a LAN - the
page already worked on PHP, and the solution required some daemon to
popen() a process and read from it (so I used [open |/path/toprocess] in
the background).

> I'll use C or Tcl or PHP or ECMAscript or Fortran or awk or sed
> or perl or whatever gets the job done. (I will not use VB or C#
> or other microsoft abominations.)

Why perl not VB or C#? ;-)

--
WK

David N. Welton

unread,
Jan 10, 2004, 11:32:24 AM1/10/04
to
Derk Gwen <derk...@HotPOP.com> writes:

> dav...@dedasys.com (David N. Welton) wrote:
> # gop...@rocketmail.com (gk) writes:
> #
> # > We do have lot of Tcl/Expect script which has already written, and
> # > for userinterface ( web page), i would like to use php & html . So
> # > is it possible to call tcl proc from the php function, with out
> # > using (exec()).
> #
> # What's wrong with creating your web pages in Tcl, with Rivet, Websh,
> # tclhttpd, AOLserver, or something else?

> What's wrong with writing your general tools in PHP?

Nothing, but Tcl is faster and more flexible.

> I'll use C or Tcl or PHP or ECMAscript or Fortran or awk or sed or
> perl or whatever gets the job done. (I will not use VB or C# or
> other microsoft abominations.)

Sure, understandable, but Tcl has more available for it if you are not
an expert who is comfortable picking up new languages easily. I have
spent a lot of time working on mod_dtcl and now Rivet to make web
programming as accessible to that group as PHP has, and I'm not above
publicizing it some!

0 new messages