2 * base code for various Tcl extensions
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "chiark-tcl-base.h"
21 void cht_scriptinv_init(ScriptToInvoke *si) {
27 void cht_scriptinv_cancel(ScriptToInvoke *si) {
28 if (si->script) { Tcl_DecrRefCount(si->script); si->script= 0; }
29 if (si->xargs) { Tcl_DecrRefCount(si->xargs); si->xargs= 0; }
33 int cht_scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
34 Tcl_Obj *newscript, Tcl_Obj *xargs) {
37 cht_scriptinv_cancel(si);
38 if (!newscript) return 0;
40 rc= Tcl_ListObjLength(ip, newscript, &si->llen); if (rc) return rc;
41 Tcl_IncrRefCount(newscript);
44 rc= Tcl_ListObjLength(ip, xargs, &xlength); if (rc) return rc;
45 Tcl_IncrRefCount(xargs);
46 assert(si->llen < INT_MAX/2 && xlength < INT_MAX/2);
50 si->script= newscript;
56 int cht_scriptinv_invoke_fg(ScriptToInvoke *si, int argc,
57 Tcl_Obj *const *argv) {
61 if (!si->ipq) return TCL_OK;
63 for (i=0; i<argc; i++) Tcl_IncrRefCount(argv[i]);
65 invoke= Tcl_DuplicateObj(si->script);
66 Tcl_IncrRefCount(invoke);
69 rc= Tcl_ListObjAppendList(si->ipq, invoke, si->xargs);
73 rc= Tcl_ListObjReplace(si->ipq, invoke,si->llen,0, argc,argv);
76 rc= Tcl_EvalObjEx(si->ipq, invoke, TCL_EVAL_GLOBAL|TCL_EVAL_DIRECT);
82 for (i=0; i<argc; i++) Tcl_DecrRefCount(argv[i]);
83 if (invoke) Tcl_DecrRefCount(invoke);
87 void cht_scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv) {
89 rc= cht_scriptinv_invoke_fg(si, argc, argv);
90 if (rc) Tcl_BackgroundError(si->ipq);