From: Stephen D. <sd...@us...> - 2005-03-26 16:40:54
|
Update of /cvsroot/naviserver/naviserver/nsd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17754/nsd Modified Files: Makefile nsd.h proc.c tclcmds.c tclsched.c Added Files: tclcallbacks.c Log Message: * include/ns.h: * nsd/nsd.h: * nsd/Makefile: * nsd/tclcallbacks.c: Add routines to support calling Tcl code from C for callback events. * nsd/proc.c: * nsd/tclcmds.c: * nsd/tclsched.c: Update to use new callback and parse proc APIs. Index: nsd.h =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/nsd.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** nsd.h 26 Mar 2005 14:25:25 -0000 1.5 --- nsd.h 26 Mar 2005 16:40:40 -0000 1.6 *************** *** 825,832 **** */ - extern Ns_Callback NsTclCallback; - extern Ns_Callback NsTclSignalProc; extern Ns_SchedProc NsTclSchedProc; - extern Ns_ArgProc NsTclArgProc; extern Ns_ThreadProc NsTclThread; extern Ns_ArgProc NsTclThreadArgProc; --- 825,829 ---- Index: proc.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/proc.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** proc.c 16 Feb 2005 08:40:12 -0000 1.1.1.1 --- proc.c 26 Mar 2005 16:40:40 -0000 1.2 *************** *** 59,65 **** } procs[] = { {(void *) NsTclThread, "ns:tclthread", NsTclThreadArgProc}, ! {(void *) NsTclCallback, "ns:tclcallback", NsTclArgProc}, ! {(void *) NsTclSchedProc, "ns:tclschedproc", NsTclArgProc}, ! {(void *) NsTclSignalProc, "ns:tclsigproc", NsTclArgProc}, {(void *) NsTclSockProc, "ns:tclsockcallback", NsTclSockArgProc}, {(void *) NsCachePurge, "ns:cachepurge", NsCacheArgProc}, --- 59,64 ---- } procs[] = { {(void *) NsTclThread, "ns:tclthread", NsTclThreadArgProc}, ! {(void *) Ns_TclCallbackProc, "ns:tclcallback", Ns_TclCallbackArgProc}, ! {(void *) NsTclSchedProc, "ns:tclschedproc", Ns_TclCallbackArgProc}, {(void *) NsTclSockProc, "ns:tclsockcallback", NsTclSockArgProc}, {(void *) NsCachePurge, "ns:cachepurge", NsCacheArgProc}, --- NEW FILE: tclcallbacks.c --- /* * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://mozilla.org/. * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is AOLserver Code and related documentation * distributed by AOL. * * The Initial Developer of the Original Code is America Online, * Inc. Portions created by AOL are Copyright (C) 1999 America Online, * Inc. All Rights Reserved. * * Alternatively, the contents of this file may be used under the terms * of the GNU General Public License (the "GPL"), in which case the * provisions of GPL are applicable instead of those above. If you wish * to allow use of your version of this file only under the terms of the * GPL and not to allow others to use your version of this file under the * License, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the GPL. * If you do not delete the provisions above, a recipient may use your * version of this file under either the License or the GPL. */ /* * tclcallbacks.c -- * * Support for executing Tcl code in response to a callback event. * */ static const char *RCSID = "@(#) $Header: /cvsroot/naviserver/naviserver/nsd/tclcallbacks.c,v 1.1 2005/03/26 16:40:40 sdeasey Exp $, compiled: " __DATE__ " " __TIME__; #include "ns.h" typedef void *(AtProc)(Ns_Callback *, void *); /* * Local functions defined in this file */ static int AtObjCmd(AtProc *atProc, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]); /* *---------------------------------------------------------------------- * * Ns_TclNewCallback, Ns_TclNewCallbackObj -- * * Create a new script callback. * * Results: * Pointer to Ns_TclCallback. * * Side effects: * Copies are made of script and scriptarg. * *---------------------------------------------------------------------- */ Ns_TclCallback * Ns_TclNewCallback(Tcl_Interp *interp, void *cbProc, char *script, char *scriptarg) { Ns_TclCallback *cbPtr; cbPtr = ns_malloc(sizeof(Ns_TclCallback)); cbPtr->cbProc = cbProc; cbPtr->server = Ns_TclInterpServer(interp); cbPtr->script = ns_strdup(script); cbPtr->scriptarg = ns_strcopy(scriptarg); return cbPtr; } Ns_TclCallback * Ns_TclNewCallbackObj(Tcl_Interp *interp, void *cbProc, Tcl_Obj *objPtr, Tcl_Obj *argObjPtr) { return Ns_TclNewCallback(interp, cbProc, Tcl_GetString(objPtr), (argObjPtr ? Tcl_GetString(argObjPtr) : NULL)); } /* *---------------------------------------------------------------------- * * Ns_TclFreeCallback -- * * Free a callback created with Ns_TclNewCallback. * * Results: * None. * * Side effects: * None. * *---------------------------------------------------------------------- */ void Ns_TclFreeCallback(void *arg) { Ns_TclCallback *cbPtr = arg; ns_free(cbPtr->script); ns_free(cbPtr->scriptarg); ns_free(cbPtr); } /* *---------------------------------------------------------------------- * * Ns_TclEvalCallback -- * * Evaluate a Tcl callback in the given interp. * * Results: * Tcl return code. Result of successful script execution will * be appended to dstring if given. * * Side effects: * An interp may be allocated if none given and none already * cached for current thread. * *---------------------------------------------------------------------- */ int Ns_TclEvalCallback(Tcl_Interp *interp, Ns_TclCallback *cbPtr, Ns_DString *result, ...) { va_list ap; Ns_DString ds; char *arg; int deallocInterp = 0; int status = TCL_ERROR; if (interp == NULL) { interp = Ns_TclAllocateInterp(cbPtr->server); deallocInterp = 1; } if (interp != NULL) { Ns_DStringInit(&ds); Ns_DStringAppend(&ds, cbPtr->script); va_start(ap, result); while ((arg = va_arg(ap, char *)) != NULL) { Ns_DStringAppendElement(&ds, arg); } va_end(ap); if (cbPtr->scriptarg) { Ns_DStringAppendElement(&ds, cbPtr->scriptarg); } status = Tcl_EvalEx(interp, ds.string, ds.length, 0); if (status != TCL_OK) { Ns_DStringTrunc(&ds, 0); Ns_DStringAppend(&ds, "\n while executing callback\n"); Ns_GetProcInfo(&ds, cbPtr->cbProc, cbPtr); Tcl_AddObjErrorInfo(interp, ds.string, ds.length); Ns_TclLogError(interp); } else if (result != NULL) { Ns_DStringAppend(result, Tcl_GetStringResult(interp)); } Ns_DStringFree(&ds); if (deallocInterp) { Ns_TclDeAllocateInterp(interp); } } return status; } /* *---------------------------------------------------------------------- * * Ns_TclCallbackProc -- * * Callback routine which evaluates the registered Tcl script. * * Results: * None. * * Side effects: * Depends on Tcl script. * *---------------------------------------------------------------------- */ void Ns_TclCallbackProc(void *arg) { Ns_TclCallback *cbPtr = arg; (void) Ns_TclEvalCallback(NULL, cbPtr, NULL, NULL); } /* *---------------------------------------------------------------------- * * Ns_TclCallbackArgProc -- * * Proc info routine to copy Tcl callback script. * * Results: * None. * * Side effects: * Will copy script to given dstring. * *---------------------------------------------------------------------- */ void Ns_TclCallbackArgProc(Tcl_DString *dsPtr, void *arg) { Ns_TclCallback *cbPtr = arg; Tcl_DStringAppendElement(dsPtr, cbPtr->script); if (cbPtr->scriptarg != NULL) { Tcl_DStringAppendElement(dsPtr, cbPtr->scriptarg); } } /* *---------------------------------------------------------------------- * * AtObjCmd -- * * Implements ns_atsignal, ns_atshutdown, ns_atstartup, ns_atexit. * * Results: * Tcl result. * * Side effects: * See docs. * *---------------------------------------------------------------------- */ static int AtObjCmd(AtProc *atProc, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Ns_TclCallback *cbPtr; if (objc != 2 && objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, "script ?arg?"); return TCL_ERROR; } cbPtr = Ns_TclNewCallbackObj(interp, Ns_TclCallbackProc, objv[1], (objc == 3) ? objv[2] : NULL); (*atProc)(Ns_TclCallbackProc, cbPtr); return TCL_OK; } int NsTclAtStartupObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { return AtObjCmd(Ns_RegisterAtStartup, interp, objc, objv); } int NsTclAtSignalObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { return AtObjCmd(Ns_RegisterAtSignal, interp, objc, objv); } int NsTclAtShutdownObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { return AtObjCmd(Ns_RegisterShutdown, interp, objc, objv); } int NsTclAtExitObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { return AtObjCmd(Ns_RegisterAtExit, interp, objc, objv); } Index: tclcmds.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/tclcmds.c,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** tclcmds.c 26 Mar 2005 15:53:06 -0000 1.6 --- tclcmds.c 26 Mar 2005 16:40:40 -0000 1.7 *************** *** 63,66 **** --- 63,72 ---- NsTclAdpMimeTypeObjCmd, NsTclAdpCompressObjCmd, + NsTclAfterObjCmd, + NsTclAtExitObjCmd, + NsTclAtStartupObjCmd, + NsTclAtShutdownObjCmd, + NsTclAtSignalObjCmd, + NsTclCancelObjCmd, NsTclChanObjCmd, NsTclChmodObjCmd, *************** *** 114,117 **** --- 120,124 ---- NsTclParseHttpTimeObjCmd, NsTclParseQueryObjCmd, + NsTclPauseObjCmd, NsTclPurgeFilesObjCmd, NsTclRandObjCmd, *************** *** 123,126 **** --- 130,134 ---- NsTclRequestAuthorizeObjCmd, NsTclRespondObjCmd, + NsTclResumeObjCmd, NsTclReturnBadRequestObjCmd, NsTclReturnErrorObjCmd, *************** *** 135,138 **** --- 143,149 ---- NsTclRollFileObjCmd, NsTclRWLockObjCmd, + NsTclSchedObjCmd, + NsTclSchedDailyObjCmd, + NsTclSchedWeeklyObjCmd, NsTclSelectObjCmd, NsTclSemaObjCmd, *************** *** 160,163 **** --- 171,175 ---- NsTclUnRegisterObjCmd, NsTclUnlinkObjCmd, + NsTclUnscheduleObjCmd, NsTclUrl2FileObjCmd, NsTclUrlDecodeObjCmd, *************** *** 175,184 **** NsTclAdpRegisterProcCmd, NsTclAdpStatsCmd, - NsTclAfterCmd, NsTclAtCloseCmd, - NsTclAtExitCmd, - NsTclAtStartupCmd, - NsTclAtShutdownCmd, - NsTclAtSignalCmd, NsTclCacheFlushCmd, NsTclCacheKeysCmd, --- 187,191 ---- *************** *** 186,190 **** NsTclCacheSizeCmd, NsTclCacheStatsCmd, - NsTclCancelCmd, NsTclCharsetsCmd, NsTclConfigCmd, --- 193,196 ---- *************** *** 198,214 **** NsTclMkTempCmd, NsTclParseHeaderCmd, - NsTclPauseCmd, NsTclQuoteHtmlCmd, NsTclRegisterTagCmd, - NsTclResumeCmd, NsTclReturnAdminNoticeCmd, NsTclReturnNoticeCmd, - NsTclSchedCmd, - NsTclSchedDailyCmd, - NsTclSchedWeeklyCmd, NsTclShareCmd, NsTclStripHtmlCmd, NsTclThreadCmd, - NsTclUnscheduleCmd, TclX_KeyldelObjCmd, TclX_KeylgetObjCmd, --- 204,214 ---- *************** *** 274,277 **** --- 274,286 ---- /* + * tclcallbacks.c + */ + + {"ns_atsignal", NULL, NsTclAtSignalObjCmd}, + {"ns_atstartup", NULL, NsTclAtStartupObjCmd}, + {"ns_atshutdown", NULL, NsTclAtShutdownObjCmd}, + {"ns_atexit", NULL, NsTclAtExitObjCmd}, + + /* * tclconf.c */ *************** *** 327,342 **** */ ! {"ns_schedule_proc", NsTclSchedCmd, NULL}, ! {"ns_schedule_daily", NsTclSchedDailyCmd, NULL}, ! {"ns_schedule_weekly", NsTclSchedWeeklyCmd, NULL}, ! {"ns_atsignal", NsTclAtSignalCmd, NULL}, ! {"ns_atstartup", NsTclAtStartupCmd, NULL}, ! {"ns_atshutdown", NsTclAtShutdownCmd, NULL}, ! {"ns_atexit", NsTclAtExitCmd, NULL}, ! {"ns_after", NsTclAfterCmd, NULL}, ! {"ns_cancel", NsTclCancelCmd, NULL}, ! {"ns_pause", NsTclPauseCmd, NULL}, ! {"ns_resume", NsTclResumeCmd, NULL}, ! {"ns_unschedule_proc", NsTclUnscheduleCmd, NULL}, /* --- 336,347 ---- */ ! {"ns_schedule_proc", NULL, NsTclSchedObjCmd}, ! {"ns_schedule_daily", NULL, NsTclSchedDailyObjCmd}, ! {"ns_schedule_weekly", NULL, NsTclSchedWeeklyObjCmd}, ! {"ns_after", NULL, NsTclAfterObjCmd}, ! {"ns_cancel", NULL, NsTclCancelObjCmd}, ! {"ns_pause", NULL, NsTclPauseObjCmd}, ! {"ns_resume", NULL, NsTclResumeObjCmd}, ! {"ns_unschedule_proc", NULL, NsTclUnscheduleObjCmd}, /* Index: Makefile =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/Makefile,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Makefile 26 Mar 2005 15:53:06 -0000 1.3 --- Makefile 26 Mar 2005 16:40:40 -0000 1.4 *************** *** 46,50 **** quotehtml.o random.o request.o return.o rollfile.o sched.o \ server.o set.o sock.o sockcallback.o str.o tclatclose.o \ ! tclcmds.o tclconf.o tclenv.o tclfile.o tclhttp.o tclimg.o \ tclinit.o tcljob.o tclmisc.o tclobj.o tclobjv.o tclrequest.o tclresp.o \ tclsched.o tclset.o tclshare.o tclsock.o tclthread.o tclvar.o \ --- 46,50 ---- quotehtml.o random.o request.o return.o rollfile.o sched.o \ server.o set.o sock.o sockcallback.o str.o tclatclose.o \ ! tclcallbacks.o tclcmds.o tclconf.o tclenv.o tclfile.o tclhttp.o tclimg.o \ tclinit.o tcljob.o tclmisc.o tclobj.o tclobjv.o tclrequest.o tclresp.o \ tclsched.o tclset.o tclshare.o tclsock.o tclthread.o tclvar.o \ Index: tclsched.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/tclsched.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** tclsched.c 22 Feb 2005 20:33:36 -0000 1.2 --- tclsched.c 26 Mar 2005 16:40:40 -0000 1.3 *************** *** 32,36 **** * tclsched.c -- * ! * Implement scheduled procs in Tcl. */ --- 32,36 ---- * tclsched.c -- * ! * Implement scheduled procs in Tcl. */ [...1020 lines suppressed...] ! * Side effects: ! * Will copy script to given dstring. ! * ! *---------------------------------------------------------------------- ! */ ! ! void ! NsTclArgProc(Tcl_DString *dsPtr, void *arg) ! { ! Callback *cbPtr = arg; ! ! Tcl_DStringAppendElement(dsPtr, cbPtr->script); } --- 430,435 ---- static void FreeSched(void *arg, int id) { ! Ns_TclFreeCallback(arg); } |