/************************************************************************* * FUNCTION : RPMPRoblem_Obj::Get_Obj * * ARGUMENTS : none * * RETURNS : Object with refcount of 0 * * EXCEPTIONS : none * * PURPOSE : Create a Tcl_Obj from a problem * *************************************************************************/ Tcl_Obj *RPMPRoblem_Obj::Get_obj(void) { Tcl_Obj *obj = Tcl_NewObj(); obj->typePtr = &mytype; obj->internalRep.otherValuePtr = Dup(); Tcl_InvalidateStringRep(obj); return obj; }
/************************************************************************* * FUNCTION : RPMPRoblem_Obj::Create_from_problem * * ARGUMENTS : ref to problem * * RETURNS : none * * EXCEPTIONS : none * * PURPOSE : Create a problem Tcl_Obj * *************************************************************************/ Tcl_Obj *RPMPRoblem_Obj::Create_from_problem(const rpmProblem_s &x) { Tcl_Obj *y = Tcl_NewObj(); assert(y); Tcl_IncrRefCount(y); Tcl_InvalidateStringRep(y); y->typePtr = &mytype; y->internalRep.otherValuePtr = new RPMPRoblem_Obj(x); return y; }
Tcl_Obj *Ttk_NewStateSpecObj(unsigned int onbits, unsigned int offbits) { Tcl_Obj *objPtr = Tcl_NewObj(); Tcl_InvalidateStringRep(objPtr); objPtr->typePtr = &StateSpecObjType; objPtr->internalRep.longValue = (onbits << 16) | offbits; return objPtr; }
int Tcljson_JsonObjToTclObj(struct json_object *joPtr, Tcl_Obj **objPtr) { TclJsonObject *tjPtr; *objPtr = NULL; tjPtr = (TclJsonObject *) Tcl_Alloc(sizeof(TclJsonObject)); tjPtr->joPtr = joPtr; *objPtr = Tcl_NewObj(); (*objPtr)->internalRep.otherValuePtr = (VOID *) tjPtr; (*objPtr)->typePtr = &tclJsonObjectType; Tcl_InvalidateStringRep(*objPtr); return TCL_OK; }
TWAPI_EXTERN Tcl_Obj *ObjFromWinCharsN(const WCHAR *wsP, int nchars) { Tcl_Obj *objP; WinChars *rep; if (wsP == NULL) return ObjFromEmptyString(); if (! gBaseSettings.use_unicode_obj) return TwapiUtf8ObjFromWinChars(wsP, -1); rep = WinCharsNew(wsP, nchars); objP = Tcl_NewObj(); Tcl_InvalidateStringRep(objP); WinCharsSet(objP, rep); return objP; }
/************************************************************************* * FUNCTION : RPMPRoblem_ObjL::Problem * * ARGUMENTS : interp, tcl args, * * Problem object * * index in objv of first tag we need * * RETURNS : 0 if OK, else error * * EXCEPTIONS : none * * PURPOSE : Manipulate a problem entry * * NOTES : format for this is * * RPM [<prob>] [part [value]]* * * where <prob> is an existing problem object - if not given, then the * * command will create a new problem object. * * [part] is one of the defined problem tags for a problem * * [value] if given will set the defined part * * * * * *************************************************************************/ int RPMPRoblem_Obj::Problem(Tcl_Interp *interp,int objc, Tcl_Obj *const objv[], Tcl_Obj *prob,int first_tag ) { // Now, we have one of 2 possibilities here - they gave us a single tag // to query, or a list of (tag,value) pairs if (objc == (first_tag+1)) { // Single tag - return the value of the tag. int which = 0; if (Tcl_GetIndexFromObj(interp,objv[first_tag],prob_parts,"tag",0,&which) != TCL_OK) return TCL_ERROR; Tcl_SetObjResult(interp,Get_part((PARTS)which)); // Return value to TCL return TCL_OK; } Tcl_InvalidateStringRep(prob); // OK, so this should be a set of (tag,value) pairs - parse them for (int i = first_tag; i < objc; i += 2) { // Make sure we actually HAVE a value if ((i+1)>objc) return Cmd_base::Error(interp,"Need a value"); // what tag is it? int which = 0; if (Tcl_GetIndexFromObj(interp,objv[i],prob_parts,"tag",0,&which) != TCL_OK) return TCL_ERROR; switch ((PARTS)which) { case PACKAGE: { int len = 0; char *x = Tcl_GetStringFromObj(objv[i+1],&len); char *p = new char[len+1]; strncpy(p,x,len); p[len] = 0; if (problem.pkgNEVR) delete [] problem.pkgNEVR; problem.pkgNEVR = p; } break; case ALT: { int len = 0; char *x = Tcl_GetStringFromObj(objv[i+1],&len); char *p = new char[len+1]; strncpy(p,x,len); p[len] = 0; if (problem.altNEVR) delete [] problem.altNEVR; problem.altNEVR = p; } break; case KEY: { int value = 0; if (Tcl_GetIntFromObj(interp,objv[i+1],&value) != TCL_OK) return TCL_ERROR; problem.key = (fnpyKey)value; } break; case TYPE: { int which = 0; if (Tcl_GetIndexFromObjStruct(interp,objv[i+1],(char **)&prob_strings[0].name,sizeof(prob_strings[0]), "type",0,&which ) != TCL_OK) return TCL_ERROR; problem.type = (rpmProblemType)prob_strings[which].code; } break; case IGNORE: { int value = 0; if (Tcl_GetIntFromObj(interp,objv[i+1],&value) != TCL_OK) return TCL_ERROR; problem.ignoreProblem = value; } break; case STRING: { int len = 0; char *x = Tcl_GetStringFromObj(objv[i+1],&len); char *p = new char[len+1]; strncpy(p,x,len); p[len] = 0; if (problem.str1) delete [] problem.str1; problem.str1 = p; } break; case INT: { int value = 0; if (Tcl_GetIntFromObj(interp,objv[i+1],&value) != TCL_OK) return TCL_ERROR; problem.ulong1 = value; } break; } } Tcl_SetObjResult(interp,prob); // Return value to TCL return TCL_OK; }
static int TestobjCmd( ClientData clientData, /* Not used. */ Tcl_Interp *interp, /* Current interpreter. */ int objc, /* Number of arguments. */ Tcl_Obj *const objv[]) /* Argument objects. */ { int varIndex, destIndex, i; const char *index, *subCmd, *string; const Tcl_ObjType *targetType; if (objc < 2) { wrongNumArgs: Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?"); return TCL_ERROR; } subCmd = Tcl_GetString(objv[1]); if (strcmp(subCmd, "assign") == 0) { if (objc != 4) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } string = Tcl_GetString(objv[3]); if (GetVariableIndex(interp, string, &destIndex) != TCL_OK) { return TCL_ERROR; } SetVarToObj(destIndex, varPtr[varIndex]); Tcl_SetObjResult(interp, varPtr[destIndex]); } else if (strcmp(subCmd, "convert") == 0) { const char *typeName; if (objc != 4) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } typeName = Tcl_GetString(objv[3]); if ((targetType = Tcl_GetObjType(typeName)) == NULL) { Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "no type ", typeName, " found", NULL); return TCL_ERROR; } if (Tcl_ConvertToType(interp, varPtr[varIndex], targetType) != TCL_OK) { return TCL_ERROR; } Tcl_SetObjResult(interp, varPtr[varIndex]); } else if (strcmp(subCmd, "duplicate") == 0) { if (objc != 4) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } string = Tcl_GetString(objv[3]); if (GetVariableIndex(interp, string, &destIndex) != TCL_OK) { return TCL_ERROR; } SetVarToObj(destIndex, Tcl_DuplicateObj(varPtr[varIndex])); Tcl_SetObjResult(interp, varPtr[destIndex]); } else if (strcmp(subCmd, "freeallvars") == 0) { if (objc != 2) { goto wrongNumArgs; } for (i = 0; i < NUMBER_OF_OBJECT_VARS; i++) { if (varPtr[i] != NULL) { Tcl_DecrRefCount(varPtr[i]); varPtr[i] = NULL; } } } else if (strcmp(subCmd, "invalidateStringRep") == 0) { if (objc != 3) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } Tcl_InvalidateStringRep(varPtr[varIndex]); Tcl_SetObjResult(interp, varPtr[varIndex]); } else if (strcmp(subCmd, "newobj") == 0) { if (objc != 3) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } SetVarToObj(varIndex, Tcl_NewObj()); Tcl_SetObjResult(interp, varPtr[varIndex]); } else if (strcmp(subCmd, "objtype") == 0) { const char *typeName; /* * Return an object containing the name of the argument's type of * internal rep. If none exists, return "none". */ if (objc != 3) { goto wrongNumArgs; } if (objv[2]->typePtr == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj("none", -1)); } else { typeName = objv[2]->typePtr->name; Tcl_SetObjResult(interp, Tcl_NewStringObj(typeName, -1)); } } else if (strcmp(subCmd, "refcount") == 0) { if (objc != 3) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } Tcl_SetObjResult(interp, Tcl_NewIntObj(varPtr[varIndex]->refCount)); } else if (strcmp(subCmd, "type") == 0) { if (objc != 3) { goto wrongNumArgs; } index = Tcl_GetString(objv[2]); if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) { return TCL_ERROR; } if (CheckIfVarUnset(interp, varIndex)) { return TCL_ERROR; } if (varPtr[varIndex]->typePtr == NULL) { /* a string! */ Tcl_AppendToObj(Tcl_GetObjResult(interp), "string", -1); } else { Tcl_AppendToObj(Tcl_GetObjResult(interp), varPtr[varIndex]->typePtr->name, -1); } } else if (strcmp(subCmd, "types") == 0) { if (objc != 2) { goto wrongNumArgs; } if (Tcl_AppendAllObjTypes(interp, Tcl_GetObjResult(interp)) != TCL_OK) { return TCL_ERROR; } } else { Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "bad option \"", Tcl_GetString(objv[1]), "\": must be assign, convert, duplicate, freeallvars, " "newobj, objcount, objtype, refcount, type, or types", NULL); return TCL_ERROR; } return TCL_OK; }
static int obj_Cgmap(ClientData /*UNUSED*/, Tcl_Interp *interp, int argc, Tcl_Obj * const objv[]) { Tcl_Obj *atomselect = NULL; Tcl_Obj *object = NULL; Tcl_Obj *bytes = NULL; Tcl_Obj *bytes_append = NULL; Tcl_Obj *sel = NULL; float *coords = NULL; float *coords_append = NULL; const char *blockid_field = "user"; const char *order_field = "user2"; const char *weight_field= "user3"; int nframes, natoms, ncoords, result, length; int first, last, stride; int molid, append_molid; natoms = ncoords = result = 0; molid = append_molid = 0; first = last = 0; stride = 1; nframes = 1; std::vector<float> weight; std::vector<int> bead; std::vector<int> index; // Parse Arguments int n = 1; while (n < argc) { const char *cmd = Tcl_GetString(objv[n]); if (!strncmp(cmd, "-molid", 7)) { if (Tcl_GetIntFromObj(interp,objv[n+1], &molid) != TCL_OK) {return TCL_ERROR;} n += 2; } else if (!strncmp(cmd, "-append", 8)) { if (Tcl_GetIntFromObj(interp,objv[n+1], &append_molid) != TCL_OK) {return TCL_ERROR;} n += 2; } else if (!strncmp(cmd, "-sel", 5)) { sel = objv[n+1]; n += 2; } else if (!strncmp(cmd, "-first", 5)) { if (Tcl_GetIntFromObj(interp,objv[n+1], &first) != TCL_OK) {return TCL_ERROR;} n += 2; } else if (!strncmp(cmd, "-last", 4)) { if (Tcl_GetIntFromObj(interp,objv[n+1], &last) != TCL_OK) {return TCL_ERROR;} n += 2; } else if (!strncmp(cmd, "-stride", 6)) { if (Tcl_GetIntFromObj(interp,objv[n+1], &stride) != TCL_OK) {return TCL_ERROR;} n += 2; } else if (!strncmp(cmd, "-weight", 7)) { weight_field = Tcl_GetString(objv[n+1]); n += 2; } else if (!strncmp(cmd, "-blockid", 7)) { blockid_field = Tcl_GetString(objv[n+1]); n += 2; } else if (!strncmp(cmd, "-order", 6)) { order_field = Tcl_GetString(objv[n+1]); n += 2; } else { Tcl_WrongNumArgs(interp,1,objv, (char *)"molid"); return TCL_ERROR; } } // Create an internal selection that we can manipulate if none was defined // Note that a passed selection overides the passed molid if (!sel) { Tcl_Obj *script = Tcl_ObjPrintf("atomselect %i all", molid); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect", TCL_STATIC); return TCL_ERROR; } atomselect = Tcl_GetObjResult(interp); Tcl_IncrRefCount(atomselect); } else { // Create a internal selection that is a COPY of the passed selection atomselect = Tcl_DuplicateObj(sel); Tcl_IncrRefCount(atomselect); // Get the molid Tcl_Obj *script = Tcl_DuplicateObj(sel); Tcl_AppendToObj(script, " molid", -1); if(Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect", TCL_STATIC); return TCL_ERROR; } Tcl_Obj *molid_result = Tcl_GetObjResult(interp); if (Tcl_GetIntFromObj(interp, molid_result, &molid) != TCL_OK) {return TCL_ERROR;} } // Get the number of frames Tcl_Obj *script = Tcl_ObjPrintf("molinfo %i get numframes", molid); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling molinfo for nframes", TCL_STATIC); return TCL_ERROR; } object = Tcl_GetObjResult(interp); if (Tcl_GetIntFromObj(interp, object, &nframes) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing number of frames", TCL_STATIC); return TCL_ERROR; } if ( first < 0 || first >= nframes ) { Tcl_SetResult(interp, (char *) "Cgmap: illegal value of first_frame", TCL_STATIC); return TCL_ERROR; } if ( last == -1 || last > nframes || last < first ) last = nframes; // Get the number of atoms from selection script = Tcl_DuplicateObj(atomselect); Tcl_AppendToObj(script, " num", -1); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect", TCL_STATIC); return TCL_ERROR; } object = Tcl_GetObjResult(interp); if (Tcl_GetIntFromObj(interp, object, &natoms) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing number of atoms", TCL_STATIC); return TCL_ERROR; } // Make sure we actually have some atoms if (natoms == 0) { Tcl_SetResult(interp, (char *) "Cgmap: Selection or molecule contains no atoms", TCL_STATIC); return TCL_ERROR; } // Get the weights (mass) script = Tcl_DuplicateObj(atomselect); Tcl_AppendPrintfToObj (script, " get %s", weight_field); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect for weights", TCL_STATIC); return TCL_ERROR; } ncoords = parse_vector(Tcl_GetObjResult(interp), weight, interp); if (ncoords == -1) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing atomselect result", TCL_STATIC); return TCL_ERROR; } // Get the bead IDs script = Tcl_DuplicateObj(atomselect); Tcl_AppendPrintfToObj (script, " get %s", blockid_field); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect for blocks", TCL_STATIC); return TCL_ERROR; } ncoords = parse_ivector(Tcl_GetObjResult(interp), bead, interp, true); if (ncoords == -1) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing atomselect result", TCL_STATIC); return TCL_ERROR; } // Get the atom IDs, we use these as a map when accessing the coordinate array // user2 is set via ::CGit::setBeadID script = Tcl_DuplicateObj(atomselect); Tcl_AppendPrintfToObj (script, " get %s", order_field); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error calling atomselect for order", TCL_STATIC); return TCL_ERROR; } ncoords = parse_ivector(Tcl_GetObjResult(interp), index, interp, true); if (ncoords == -1) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing atomselect result", TCL_STATIC); return TCL_ERROR; } // Get current frame of the target mol script = Tcl_ObjPrintf("molinfo %d get frame", append_molid); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error getting append mol's current frame", TCL_STATIC); return TCL_ERROR; } int append_frame = 0; object = Tcl_GetObjResult(interp); if (Tcl_GetIntFromObj(interp, object, &append_frame) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing append mol's current frame", TCL_STATIC); return TCL_ERROR; } //Get number of atoms in target (append) mol script = Tcl_ObjPrintf("molinfo %i get numatoms", append_molid); if (Tcl_EvalObjEx(interp, script, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error getting append mol's number of atoms", TCL_STATIC); return TCL_ERROR; } int append_natoms = 0; object = Tcl_GetObjResult(interp); if (Tcl_GetIntFromObj(interp, object, &append_natoms) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error parsing append mol's number of atoms", TCL_STATIC); return TCL_ERROR; } int print = ((last - first) / 10); if (print < 10) print = 10; if (print > 100) print = 100; //Loop over frames, calculate COMS, set coordinates in target mol for (int frame = first; frame <= last && frame < nframes; frame += stride) { if (frame % print == 0) { //Tcl_Obj *msg = Tcl_ObjPrintf ("puts \"Mapping frame %i\"", frame); Tcl_Obj *msg = Tcl_ObjPrintf ("vmdcon -info \"CGit> Mapping frame %i\"", frame); result = Tcl_EvalObjEx(interp, msg, TCL_EVAL_DIRECT); if (result != TCL_OK) { return TCL_ERROR; } } //Update the frames Tcl_Obj *mol_chgframe = Tcl_ObjPrintf ("molinfo top set frame %i", frame); if (Tcl_EvalObjEx(interp, mol_chgframe, TCL_EVAL_DIRECT) != TCL_OK) return TCL_ERROR; // Get the coordinates of the molecules in the reference mol Tcl_Obj *get_ts = Tcl_ObjPrintf("gettimestep %d %i", molid, frame); if (Tcl_EvalObjEx(interp, get_ts, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error getting coordinates", TCL_STATIC); return TCL_ERROR; } bytes = Tcl_GetObjResult(interp); Tcl_IncrRefCount(bytes); Tcl_InvalidateStringRep (bytes); coords = reinterpret_cast<float *> (Tcl_GetByteArrayFromObj(bytes, &length)); /** Create a new frame for append_mol **/ Tcl_ObjPrintf("animate dup %i", append_molid); if (Tcl_EvalObjEx(interp, get_ts, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error adding frame to append mol", TCL_STATIC); return TCL_ERROR; } append_frame++; Tcl_Obj *setframe = Tcl_ObjPrintf("molinfo %i set frame %i; display update", molid, frame); if (Tcl_EvalObjEx(interp, setframe, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error updating source frame", TCL_STATIC); return TCL_ERROR; } // Copy PBC conditions Tcl_Obj *setpbc = Tcl_ObjPrintf("molinfo %i set {a b c} [molinfo %i get {a b c}]", append_molid, molid); if (Tcl_EvalObjEx(interp, setpbc, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error updating PBC", TCL_STATIC); return TCL_ERROR; } // Get the coordinates of the molecules in the target (append) mol get_ts = Tcl_ObjPrintf("gettimestep %d %i", append_molid, append_frame); if (Tcl_EvalObjEx(interp, get_ts, TCL_EVAL_DIRECT) != TCL_OK) { Tcl_SetResult(interp, (char *) "Cgmap: error getting coordinates", TCL_STATIC); return TCL_ERROR; } bytes_append = Tcl_GetObjResult(interp); Tcl_IncrRefCount(bytes_append); Tcl_InvalidateStringRep(bytes_append); coords_append = reinterpret_cast<float *> (Tcl_GetByteArrayFromObj(bytes_append, &length)); //loop over coordinates and beads, calculate COMs int current_bead, current_atom; current_bead = current_atom = 0; // Nested loop to work on each bead at a time float w,x,y,z; int j = 0; for (int start_atom = 0; start_atom < natoms; ) { current_bead = bead[start_atom]; w = x = y = z = 0; // Calculate COM for each bead for ( current_atom = start_atom; current_atom < natoms && bead[current_atom] == current_bead; current_atom++) { //Lookup the atom index from the selection unsigned int idx = index[current_atom]; float tw = weight[current_atom]; w += tw; x += tw * coords[3*idx]; y += tw * coords[3*idx+1]; z += tw * coords[3*idx+2]; } if (w == 0) { Tcl_SetResult(interp, (char *) "Cgmap: Bad weight can't total zero", TCL_STATIC); return TCL_ERROR; } // Insert calculated COMS into append_mols coordinate array // Need to figure out some kind of bounds checking here... coords_append[3 * j ] = x / w; coords_append[3 * j + 1] = y / w; coords_append[3 * j + 2] = z / w; start_atom = current_atom; j++; } // bead loop // call rawtimestep to set byte array for append_mol Tcl_Obj *set_ts[5]; set_ts[0] = Tcl_NewStringObj("rawtimestep", -1); set_ts[1] = Tcl_ObjPrintf("%d",append_molid); set_ts[2] = bytes_append; set_ts[3] = Tcl_NewStringObj("-frame", -1); set_ts[4] = Tcl_NewIntObj(append_frame); if (Tcl_EvalObjv (interp, 5, set_ts, 0) != TCL_OK) return TCL_ERROR; //Cleanup Tcl_DecrRefCount(bytes); Tcl_DecrRefCount(bytes_append); } // Frame loop //Cleanup Tcl_DecrRefCount(atomselect); Tcl_SetResult(interp, (char *) "", TCL_STATIC); return TCL_OK; }
Tcl_Obj* TnmSnmpNorm(Tcl_Interp *interp, Tcl_Obj *objPtr, int flags) { int i, code, objc; Tcl_Obj **objv; Tcl_Obj *vbListPtr = NULL; /* * The following Tcl_Objs are allocated once and reused whenever * we need to expand a varbind list containing object identifiers * without any value or type elements. */ static Tcl_Obj *nullType = NULL; static Tcl_Obj *zeroValue = NULL; static Tcl_Obj *nullValue = NULL; if (! nullType) { nullType = Tcl_NewStringObj("NULL", 4); Tcl_IncrRefCount(nullType); } if (! zeroValue) { zeroValue = Tcl_NewIntObj(0); Tcl_IncrRefCount(zeroValue); } if (! nullValue) { nullValue = Tcl_NewStringObj(NULL, 0); Tcl_IncrRefCount(nullValue); } /* * Split the varbind list into a list of varbinds. Create a * new Tcl list to hold the expanded varbind list. */ code = Tcl_ListObjGetElements(interp, objPtr, &objc, &objv); if (code != TCL_OK) { goto errorExit; } vbListPtr = Tcl_NewListObj(0, NULL); for (i = 0; i < objc; i++) { int vbc, type; Tcl_Obj **vbv, *vbPtr; TnmOid* oidPtr; Tcl_Obj *oidObjPtr, *typeObjPtr, *valueObjPtr; TnmMibNode *nodePtr = NULL; /* * Create a new varbind element in the expanded result list * for each varbind. */ vbPtr = Tcl_NewListObj(0, NULL); Tcl_ListObjAppendElement(interp, vbListPtr, vbPtr); code = Tcl_ListObjGetElements(interp, objv[i], &vbc, &vbv); if (code != TCL_OK) { goto errorExit; } /* * Get the object identifier value from the first list * element. Check the number of list elements and assign * them to the oid, type and value variables. */ switch (vbc) { case 1: oidObjPtr = vbv[0]; typeObjPtr = nullType; valueObjPtr = nullValue; break; case 2: oidObjPtr = vbv[0]; typeObjPtr = NULL; valueObjPtr = vbv[1]; break; case 3: oidObjPtr = vbv[0]; typeObjPtr = vbv[1]; valueObjPtr = vbv[2]; break; default: { char msg[80]; sprintf(msg, "illegal number of elements in varbind %d", i); Tcl_ResetResult(interp); Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), msg, (char *) NULL); goto errorExit; } } /* * Check/resolve the object identifier and assign it to the * result list. Make sure to make a deep copy if the object * identifier value is shared since the string representation * must be invalidated to ensure that hexadecimal * sub-identifier are converted into decimal sub-identifier. */ oidPtr = TnmGetOidFromObj(interp, oidObjPtr); if (! oidPtr) { goto errorExit; } if (Tcl_IsShared(oidObjPtr)) { oidObjPtr = Tcl_DuplicateObj(oidObjPtr); } TnmOidObjSetRep(oidObjPtr, TNM_OID_AS_OID); Tcl_InvalidateStringRep(oidObjPtr); Tcl_ListObjAppendElement(interp, vbPtr, oidObjPtr); /* * Lookup the type in the MIB if there is no type given in the * varbind element. */ if (! typeObjPtr) { int syntax; nodePtr = TnmMibNodeFromOid(oidPtr, NULL); if (! nodePtr) { char msg[80]; sprintf(msg, "failed to lookup the type for varbind %d", i); Tcl_ResetResult(interp); Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), msg, (char *) NULL); goto errorExit; } syntax = (nodePtr->typePtr && nodePtr->typePtr->name) ? nodePtr->typePtr->syntax : nodePtr->syntax; typeObjPtr = Tcl_NewStringObj( TnmGetTableValue(tnmSnmpTypeTable, (unsigned) syntax), -1); } type = TnmGetTableKeyFromObj(NULL, tnmSnmpTypeTable, typeObjPtr, NULL); if (type == -1) { type = TnmGetTableKeyFromObj(NULL, tnmSnmpExceptionTable, typeObjPtr, NULL); if (type == -1) { char msg[80]; invalidType: sprintf(msg, "illegal type in varbind %d", i); Tcl_ResetResult(interp); Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), msg, (char *) NULL); goto errorExit; } } Tcl_ListObjAppendElement(interp, vbPtr, typeObjPtr); /* * Check the value and perform any conversions needed to * convert the value into the base type representation. */ switch (type) { case ASN1_INTEGER: { long longValue; code = Tcl_GetLongFromObj(interp, valueObjPtr, &longValue); if (code != TCL_OK) { if (! nodePtr) { nodePtr = TnmMibNodeFromOid(oidPtr, NULL); } if (nodePtr) { Tcl_Obj *value; value = TnmMibScanValue(nodePtr->typePtr, nodePtr->syntax, valueObjPtr); if (! value) { goto errorExit; } Tcl_ResetResult(interp); code = Tcl_GetLongFromObj(interp, value, &longValue); } if (code != TCL_OK) { goto errorExit; } valueObjPtr = Tcl_NewLongObj(longValue); } if (flags & TNM_SNMP_NORM_INT) { if (! nodePtr) { nodePtr = TnmMibNodeFromOid(oidPtr, NULL); } if (nodePtr && nodePtr->typePtr) { Tcl_Obj *newPtr; newPtr = TnmMibFormatValue(nodePtr->typePtr, nodePtr->syntax, valueObjPtr); if (newPtr) { valueObjPtr = newPtr; } } } break; } case ASN1_COUNTER32: case ASN1_GAUGE32: case ASN1_TIMETICKS: { TnmUnsigned32 u; code = TnmGetUnsigned32FromObj(interp, valueObjPtr, &u); if (code != TCL_OK) { goto errorExit; } break; } case ASN1_COUNTER64: { TnmUnsigned64 u; code = TnmGetUnsigned64FromObj(interp, valueObjPtr, &u); if (code != TCL_OK) { goto errorExit; } break; } case ASN1_IPADDRESS: { if (TnmGetIpAddressFromObj(interp, valueObjPtr) == NULL) { goto errorExit; } Tcl_InvalidateStringRep(valueObjPtr); break; } case ASN1_OBJECT_IDENTIFIER: if (! TnmGetOidFromObj(interp, valueObjPtr)) { goto errorExit; } if (Tcl_IsShared(valueObjPtr)) { valueObjPtr = Tcl_DuplicateObj(valueObjPtr); } if (flags & TNM_SNMP_NORM_OID) { TnmOidObjSetRep(valueObjPtr, TNM_OID_AS_NAME); } else { TnmOidObjSetRep(valueObjPtr, TNM_OID_AS_OID); } Tcl_InvalidateStringRep(valueObjPtr); break; case ASN1_OCTET_STRING: { int len; if (! nodePtr) { nodePtr = TnmMibNodeFromOid(oidPtr, NULL); } if (nodePtr) { Tcl_Obj *scan; scan = TnmMibScanValue(nodePtr->typePtr, nodePtr->syntax, valueObjPtr); if (scan) { valueObjPtr = scan; } } if (TnmGetOctetStringFromObj(interp, valueObjPtr, &len) == NULL) { goto errorExit; } Tcl_InvalidateStringRep(valueObjPtr); break; } case ASN1_NULL: valueObjPtr = nullValue; break; default: goto invalidType; } Tcl_ListObjAppendElement(interp, vbPtr, valueObjPtr); } return vbListPtr; errorExit: if (vbListPtr) { Tcl_DecrRefCount(vbListPtr); } return NULL; }