| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
static char packageName[] = "procbodytest"; |
| 23 |
static char packageVersion[] = "1.0"; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static char procCommand[] = "proc"; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
typedef struct CmdTable |
| 37 |
{ |
| 38 |
char *cmdName; |
| 39 |
Tcl_ObjCmdProc *proc; |
| 40 |
int exportIt; |
| 41 |
} CmdTable; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static int ProcBodyTestProcObjCmd _ANSI_ARGS_((ClientData dummy, |
| 48 |
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])); |
| 49 |
static int ProcBodyTestInitInternal _ANSI_ARGS_((Tcl_Interp *interp, |
| 50 |
int isSafe)); |
| 51 |
static int RegisterCommand _ANSI_ARGS_((Tcl_Interp* interp, |
| 52 |
char *namespace, CONST CmdTable *cmdTablePtr)); |
| 53 |
int Procbodytest_Init _ANSI_ARGS_((Tcl_Interp * interp)); |
| 54 |
int Procbodytest_SafeInit _ANSI_ARGS_((Tcl_Interp * interp)); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static CONST CmdTable commands[] = |
| 62 |
{ |
| 63 |
{ procCommand, ProcBodyTestProcObjCmd, 1 }, |
| 64 |
|
| 65 |
{ 0, 0, 0 } |
| 66 |
}; |
| 67 |
|
| 68 |
static CONST CmdTable safeCommands[] = |
| 69 |
{ |
| 70 |
{ procCommand, ProcBodyTestProcObjCmd, 1 }, |
| 71 |
|
| 72 |
{ 0, 0, 0 } |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
int |
| 92 |
Procbodytest_Init(interp) |
| 93 |
Tcl_Interp *interp; |
| 94 |
|
| 95 |
{ |
| 96 |
return ProcBodyTestInitInternal(interp, 0); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int |
| 116 |
Procbodytest_SafeInit(interp) |
| 117 |
Tcl_Interp *interp; |
| 118 |
|
| 119 |
{ |
| 120 |
return ProcBodyTestInitInternal(interp, 1); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static int RegisterCommand(interp, namespace, cmdTablePtr) |
| 140 |
Tcl_Interp* interp; |
| 141 |
|
| 142 |
char *namespace; |
| 143 |
|
| 144 |
CONST CmdTable *cmdTablePtr; |
| 145 |
{ |
| 146 |
char buf[128]; |
| 147 |
|
| 148 |
if (cmdTablePtr->exportIt) { |
| 149 |
sprintf(buf, "namespace eval %s { namespace export %s }", |
| 150 |
namespace, cmdTablePtr->cmdName); |
| 151 |
if (Tcl_Eval(interp, buf) != TCL_OK) |
| 152 |
return TCL_ERROR; |
| 153 |
} |
| 154 |
|
| 155 |
sprintf(buf, "%s::%s", namespace, cmdTablePtr->cmdName); |
| 156 |
Tcl_CreateObjCommand(interp, buf, cmdTablePtr->proc, 0, 0); |
| 157 |
|
| 158 |
return TCL_OK; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static int |
| 179 |
ProcBodyTestInitInternal(interp, isSafe) |
| 180 |
Tcl_Interp *interp; |
| 181 |
|
| 182 |
int isSafe; |
| 183 |
{ |
| 184 |
CONST CmdTable *cmdTablePtr; |
| 185 |
|
| 186 |
cmdTablePtr = (isSafe) ? &safeCommands[0] : &commands[0]; |
| 187 |
for ( ; cmdTablePtr->cmdName ; cmdTablePtr++) { |
| 188 |
if (RegisterCommand(interp, packageName, cmdTablePtr) != TCL_OK) { |
| 189 |
return TCL_ERROR; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return Tcl_PkgProvide(interp, packageName, packageVersion); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
static int |
| 230 |
ProcBodyTestProcObjCmd (dummy, interp, objc, objv) |
| 231 |
ClientData dummy; |
| 232 |
Tcl_Interp *interp; |
| 233 |
int objc; |
| 234 |
Tcl_Obj *CONST objv[]; |
| 235 |
{ |
| 236 |
char *fullName; |
| 237 |
Tcl_Command procCmd; |
| 238 |
Command *cmdPtr; |
| 239 |
Proc *procPtr = (Proc *) NULL; |
| 240 |
Tcl_Obj *bodyObjPtr; |
| 241 |
Tcl_Obj *myobjv[5]; |
| 242 |
int result; |
| 243 |
|
| 244 |
if (objc != 4) { |
| 245 |
Tcl_WrongNumArgs(interp, 1, objv, "newName argsList bodyName"); |
| 246 |
return TCL_ERROR; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
fullName = Tcl_GetStringFromObj(objv[3], (int *) NULL); |
| 254 |
procCmd = Tcl_FindCommand(interp, fullName, (Tcl_Namespace *) NULL, |
| 255 |
TCL_LEAVE_ERR_MSG); |
| 256 |
if (procCmd == NULL) { |
| 257 |
return TCL_ERROR; |
| 258 |
} |
| 259 |
|
| 260 |
cmdPtr = (Command *) procCmd; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
if (((cmdPtr->objProc != NULL) |
| 270 |
&& (cmdPtr->objProc != TclGetObjInterpProc())) |
| 271 |
|| ((cmdPtr->proc != NULL) |
| 272 |
&& (cmdPtr->proc != TclGetInterpProc()))) { |
| 273 |
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 274 |
"command \"", fullName, |
| 275 |
"\" is not a Tcl procedure", (char *) NULL); |
| 276 |
return TCL_ERROR; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
if (cmdPtr->objProc != NULL) { |
| 284 |
procPtr = (Proc *) cmdPtr->objClientData; |
| 285 |
} else if (cmdPtr->proc != NULL) { |
| 286 |
procPtr = (Proc *) cmdPtr->clientData; |
| 287 |
} |
| 288 |
|
| 289 |
if (procPtr == NULL) { |
| 290 |
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 291 |
"procedure \"", fullName, |
| 292 |
"\" does not have a Proc struct!", (char *) NULL); |
| 293 |
return TCL_ERROR; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
bodyObjPtr = TclNewProcBodyObj(procPtr); |
| 301 |
if (bodyObjPtr == NULL) { |
| 302 |
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 303 |
"failed to create a procbody object for procedure \"", |
| 304 |
fullName, "\"", (char *) NULL); |
| 305 |
return TCL_ERROR; |
| 306 |
} |
| 307 |
Tcl_IncrRefCount(bodyObjPtr); |
| 308 |
|
| 309 |
myobjv[0] = objv[0]; |
| 310 |
myobjv[1] = objv[1]; |
| 311 |
myobjv[2] = objv[2]; |
| 312 |
myobjv[3] = bodyObjPtr; |
| 313 |
myobjv[4] = (Tcl_Obj *) NULL; |
| 314 |
|
| 315 |
result = Tcl_ProcObjCmd((ClientData) NULL, interp, objc, myobjv); |
| 316 |
Tcl_DecrRefCount(bodyObjPtr); |
| 317 |
|
| 318 |
return result; |
| 319 |
} |