#include "nsd.h" #include #include char *ns_strcopy (const char *x) { if (x == NULL) return NULL; return strdup (x); } void Ns_MakePath (Ns_DString *ds, ...) { va_list args; int seen_one = 0; char *item; va_start (args, ds); Ns_DStringTrunc (ds, 0); while ((item = va_arg (args, char*))) { if (seen_one) Ns_DStringNAppend (ds, "/", 1); else ++seen_one; Ns_DStringAppend (ds, item); } va_end (args); } char *Ns_NormalizePath (Ns_DString *s, char *name) { /* Well, it does the right thing on Unix... */ Ns_DStringTrunc (s, 0); Ns_DStringAppend (s, name); ap_getparents (s->string); Ns_DStringSetLength (s, strlen (s->string)); return s->string; } /* *---------------------------------------------------------------------- * * Ns_TclGetOpenFd -- * * Return an open Unix file descriptor for the given channel. * This routine is used by the AOLserver ns_sock* routines * to provide access to the underlying socket. * * Results: * TCL_OK or TCL_ERROR. * * Side effects: * The value at fdPtr is updated with a valid Unix file descriptor. * *---------------------------------------------------------------------- */ int Ns_TclGetOpenFd(Tcl_Interp *interp, char *chanId, int write, int *fdPtr) { Tcl_Channel chan; #if TCL_MAJOR_VERSION >= 8 ClientData data; #else Tcl_File file; #endif if (Ns_TclGetOpenChannel(interp, chanId, write, 1, &chan) != TCL_OK) { return TCL_ERROR; } #if TCL_MAJOR_VERSION >= 8 if (Tcl_GetChannelHandle(chan, write ? TCL_WRITABLE : TCL_READABLE, (ClientData*) &data) != TCL_OK) { Tcl_AppendResult(interp, "could not get handle for channel: ", chanId, NULL); return TCL_ERROR; } *fdPtr = (int) data; #else file = Tcl_GetChannelFile(chan, write ? TCL_WRITABLE : TCL_READABLE); if (file == NULL) { Tcl_AppendResult(interp, "could not get file for channel: ", chanId, NULL); return TCL_ERROR; } *fdPtr = (int) Tcl_GetFileInfo(file, NULL); #endif return TCL_OK; } /* *---------------------------------------------------------------------- * * Ns_TclLogError -- * * Log the global errorInfo variable to the server log. * * Results: * Returns a pointer to the errorInfo. * * Side effects: * None. * *---------------------------------------------------------------------- */ char * Ns_TclLogError(Tcl_Interp *interp) { char *errorInfo; errorInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY); if (errorInfo == NULL) { errorInfo = ""; } Ns_ModLog(Error, "tcl", "%s\n%s", interp->result, errorInfo); return errorInfo; } /* *---------------------------------------------------------------------- * * NsTclEval -- * * Wraps Tcl_Eval() of Tcl_EvalEx() to avoid byte code compiling. * * Results: * See Tcl_Eval * * Side effects: * See Tcl_Eval * *---------------------------------------------------------------------- */ int NsTclEval(Tcl_Interp *interp, char *script) { int status; #if TCL_MAJOR_VERSION >= 8 /* * Eval without the byte code compiler, and ensure that we * have a string result so old code can reference interp->result. */ #ifdef NOTDEF status = Tcl_EvalEx(interp, script, strlen(script), TCL_EVAL_DIRECT); Tcl_SetResult(interp, Tcl_GetString(Tcl_GetObjResult(interp)), TCL_VOLATILE); #endif status = Tcl_Eval (interp, script); Tcl_GetStringResult (interp); /* ensure interp->result is valid */ #else status = Tcl_Eval(interp, script); #endif return status; }