internal static void  eval(Interp interp, Interp slaveInterp, int objIx, TclObject[] objv)
        {
            TCL.CompletionCode result;

            slaveInterp.preserve();
            slaveInterp.allowExceptions();

            try
            {
                if (objIx + 1 == objv.Length)
                {
                    slaveInterp.eval(objv[objIx], 0);
                }
                else
                {
                    TclObject obj = TclList.newInstance();
                    for (int ix = objIx; ix < objv.Length; ix++)
                    {
                        TclList.append(interp, obj, objv[ix]);
                    }
                    obj.preserve();
                    slaveInterp.eval(obj, 0);
                    obj.release();
                }
                result = slaveInterp.returnCode;
            }
            catch (TclException e)
            {
                result = e.getCompletionCode();
            }

            slaveInterp.release();
            interp.transferResult(slaveInterp, result);
        }
 public static void Tcl_Free(ref TclObject to)
 {
     while (to.refCount > 0)
     {
         to.release();
     }
 }
Esempio n. 3
0
    /// <summary> Chain this frame into the call frame stack and binds the parameters
    /// values to the formal parameters of the procedure.
    /// 
    /// </summary>
    /// <param name="proc">the procedure.
    /// </param>
    /// <param name="proc">argv the parameter values.
    /// </param>
    /// <exception cref=""> TclException if wrong number of arguments.
    /// </exception>
    internal void chain( Procedure proc, TclObject[] objv )
    {
      // FIXME: double check this ns thing in case where proc is renamed to different ns.
      this.ns = proc.ns;
      this.objv = objv;
      // FIXME : quick level hack : fix later
      level = ( interp.varFrame == null ) ? 1 : ( interp.varFrame.level + 1 );
      caller = interp.frame;
      callerVar = interp.varFrame;
      interp.frame = this;
      interp.varFrame = this;

      // parameter bindings

      int numArgs = proc.argList.Length;

      if ( ( !proc.isVarArgs ) && ( objv.Length - 1 > numArgs ) )
      {
        wrongNumProcArgs( objv[0], proc );
      }

      int i, j;
      for ( i = 0, j = 1; i < numArgs; i++, j++ )
      {
        // Handle the special case of the last formal being
        // "args".  When it occurs, assign it a list consisting of
        // all the remaining actual arguments.

        TclObject varName = proc.argList[i][0];
        TclObject value = null;

        if ( ( i == ( numArgs - 1 ) ) && proc.isVarArgs )
        {
          value = TclList.newInstance();
          value.preserve();
          for ( int k = j; k < objv.Length; k++ )
          {
            TclList.append( interp, value, objv[k] );
          }
          interp.setVar( varName, value, 0 );
          value.release();
        }
        else
        {
          if ( j < objv.Length )
          {
            value = objv[j];
          }
          else if ( proc.argList[i][1] != null )
          {
            value = proc.argList[i][1];
          }
          else
          {
            wrongNumProcArgs( objv[0], proc );
          }
          interp.setVar( varName, value, 0 );
        }
      }
    }
 public static void Tcl_DecrRefCount(ref TclObject to)
 {
     to.release();
     if (to.internalRep == null)
     {
         to = null;
     }
 }
 public static void Tcl_SetLongObj(TclObject to, long result)
 {
     while (to.Shared)
     {
         to.release();
     }
     TclLong.set(to, result);
     to.preserve();
 }
 public static void Tcl_SetIntObj(TclObject to, int result
                                  )
 {
     while (to.Shared)
     {
         to.release();
     }
     TclInteger.set(to, result);
     to.preserve();
 }
Esempio n. 7
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            targetInterp.preserve();
            targetInterp.nestLevel++;

            targetInterp.resetResult();
            targetInterp.allowExceptions();

            // Append the arguments to the command prefix and invoke the command
            // in the target interp's global namespace.

            TclObject[] prefv = TclList.getElements(interp, prefix);
            TclObject   cmd   = TclList.newInstance();

            cmd.preserve();
            TclList.replace(interp, cmd, 0, 0, prefv, 0, prefv.Length - 1);
            TclList.replace(interp, cmd, prefv.Length, 0, argv, 1, argv.Length - 1);
            TclObject[] cmdv = TclList.getElements(interp, cmd);

            TCL.CompletionCode result = targetInterp.invoke(cmdv, Interp.INVOKE_NO_TRACEBACK);

            cmd.release();
            targetInterp.nestLevel--;

            // Check if we are at the bottom of the stack for the target interpreter.
            // If so, check for special return codes.

            if (targetInterp.nestLevel == 0)
            {
                if (result == TCL.CompletionCode.RETURN)
                {
                    result = targetInterp.updateReturnInfo();
                }
                if (result != TCL.CompletionCode.OK && result != TCL.CompletionCode.ERROR)
                {
                    try
                    {
                        targetInterp.processUnexpectedResult(result);
                    }
                    catch (TclException e)
                    {
                        result = e.getCompletionCode();
                    }
                }
            }

            targetInterp.release();
            interp.transferResult(targetInterp, result);
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 8
0
        public void  disposeCmd()
        {
            if ((System.Object)aliasEntry != null)
            {
                SupportClass.HashtableRemove(slaveInterp.aliasTable, aliasEntry);
            }

            if (slaveCmd != null)
            {
                SupportClass.HashtableRemove(targetInterp.targetTable, slaveCmd);
            }

            name.release();
            prefix.release();
        }
Esempio n. 9
0
 public override void  processIdleEvent()
 {
     try
     {
         SupportClass.VectorRemoveElement(Enclosing_Instance.assocData.handlers, this);
         interp.eval(command, TCL.EVAL_GLOBAL);
     }
     catch (TclException e)
     {
         interp.addErrorInfo("\n    (\"after\" script)");
         interp.backgroundError();
     }
     finally
     {
         command.release();
         command = null;
     }
 }
Esempio n. 10
0
        /// <summary> See Tcl user documentation for details.</summary>
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            TclObject list = TclList.newInstance();

            list.preserve();
            try
            {
                for (int i = 1; i < argv.Length; i++)
                {
                    TclList.append(interp, list, argv[i]);
                }
                interp.setResult(list);
            }
            finally
            {
                list.release();
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 11
0
        /// <summary> This procedure replaces zero or more elements of the list
        /// referenced by tobj with the objects from an TclObject array.
        /// If tobj is not a list object, an attempt will be made to
        /// convert it to a list.
        ///
        /// </summary>
        /// <param name="interp">current interpreter.
        /// </param>
        /// <param name="tobj">the TclObject to use as a list.
        /// </param>
        /// <param name="index">the starting index of the replace operation. <=0 means
        /// the beginning of the list. >= TclList.getLength(tobj) means
        /// the end of the list.
        /// </param>
        /// <param name="count">the number of elements to delete from the list. <=0 means
        /// no elements should be deleted and the operation is equivalent to
        /// an insertion operation.
        /// </param>
        /// <param name="elements">the element(s) to insert.
        /// </param>
        /// <param name="from">insert elements starting from elements[from] (inclusive)
        /// </param>
        /// <param name="to">insert elements up to elements[to] (inclusive)
        /// </param>
        /// <exception cref=""> TclException if tobj is not a valid list.
        /// </exception>
        public static void replace(Interp interp, TclObject tobj, int index, int count, TclObject[] elements, int from, int to)
        {
            if (tobj.Shared)
            {
                throw new TclRuntimeError("TclList.replace() called with shared object");
            }
            setListFromAny(interp, tobj);
            tobj.invalidateStringRep();
            TclList tlist = (TclList)tobj.InternalRep;

            int size = tlist.vector.Count;
            int i;

            if (index >= size)
            {
                // Append to the end of the list. There is no need for deleting
                // elements.
                index = size;
            }
            else
            {
                if (index < 0)
                {
                    index = 0;
                }
                if (count > size - index)
                {
                    count = size - index;
                }
                for (i = 0; i < count; i++)
                {
                    TclObject obj = (TclObject)tlist.vector[index];
                    obj.release();
                    tlist.vector.RemoveAt(index);
                }
            }
            for (i = from; i <= to; i++)
            {
                elements[i].preserve();
                tlist.vector.Insert(index++, elements[i]);
            }
        }
Esempio n. 12
0
        /// <summary> See Tcl user documentation for details.</summary>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length < 4)
            {
                throw new TclNumArgsException(interp, 1, argv, "list index element ?element ...?");
            }

            int       size        = TclList.getLength(interp, argv[1]);
            int       index       = Util.getIntForIndex(interp, argv[2], size);
            TclObject list        = argv[1];
            bool      isDuplicate = false;

            // If the list object is unshared we can modify it directly. Otherwise
            // we create a copy to modify: this is "copy on write".

            if (list.Shared)
            {
                list        = list.duplicate();
                isDuplicate = true;
            }

            try
            {
                TclList.insert(interp, list, index, argv, 3, argv.Length - 1);
                interp.setResult(list);
            }
            catch (TclException e)
            {
                if (isDuplicate)
                {
                    list.release();
                }
                throw;
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 13
0
 public void release()
 {
     value.release();
 }
Esempio n. 14
0
        /// <summary> See Tcl user documentation for details.</summary>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length < 4)
            {
                throw new TclNumArgsException(interp, 1, argv, "list first last ?element element ...?");
            }
            int size  = TclList.getLength(interp, argv[1]);
            int first = Util.getIntForIndex(interp, argv[2], size - 1);
            int last  = Util.getIntForIndex(interp, argv[3], size - 1);
            int numToDelete;

            if (first < 0)
            {
                first = 0;
            }

            // Complain if the user asked for a start element that is greater
            // than the list length. This won't ever trigger for the "end*"
            // case as that will be properly constrained by getIntForIndex
            // because we use size-1 (to allow for replacing the last elem).

            if ((first >= size) && (size > 0))
            {
                throw new TclException(interp, "list doesn't contain element " + argv[2]);
            }
            if (last >= size)
            {
                last = size - 1;
            }
            if (first <= last)
            {
                numToDelete = (last - first + 1);
            }
            else
            {
                numToDelete = 0;
            }

            TclObject list        = argv[1];
            bool      isDuplicate = false;

            // If the list object is unshared we can modify it directly. Otherwise
            // we create a copy to modify: this is "copy on write".

            if (list.Shared)
            {
                list        = list.duplicate();
                isDuplicate = true;
            }

            try
            {
                TclList.replace(interp, list, first, numToDelete, argv, 4, argv.Length - 1);
                interp.setResult(list);
            }
            catch (TclException e)
            {
                if (isDuplicate)
                {
                    list.release();
                }
                throw;
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 15
0
        /// <summary> This procedure is invoked to process the "split" Tcl
        /// command. See Tcl user documentation for details.
        ///
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="argv">command arguments.
        /// </param>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            char[] splitChars = null;
            string inString;

            if (argv.Length == 2)
            {
                splitChars = defSplitChars;
            }
            else if (argv.Length == 3)
            {
                splitChars = argv[2].ToString().ToCharArray();
            }
            else
            {
                throw new TclNumArgsException(interp, 1, argv, "string ?splitChars?");
            }


            inString = argv[1].ToString();
            int len = inString.Length;
            int num = splitChars.Length;

            /*
             * Handle the special case of splitting on every character.
             */

            if (num == 0)
            {
                TclObject list = TclList.newInstance();

                list.preserve();
                try
                {
                    for (int i = 0; i < len; i++)
                    {
                        TclList.append(interp, list, TclString.newInstance(inString[i]));
                    }
                    interp.setResult(list);
                }
                finally
                {
                    list.release();
                }
                return(TCL.CompletionCode.RETURN);
            }

            /*
             * Normal case: split on any of a given set of characters.
             * Discard instances of the split characters.
             */
            TclObject list2     = TclList.newInstance();
            int       elemStart = 0;

            list2.preserve();
            try
            {
                int i, j;
                for (i = 0; i < len; i++)
                {
                    char c = inString[i];
                    for (j = 0; j < num; j++)
                    {
                        if (c == splitChars[j])
                        {
                            TclList.append(interp, list2, TclString.newInstance(inString.Substring(elemStart, (i) - (elemStart))));
                            elemStart = i + 1;
                            break;
                        }
                    }
                }
                if (i != 0)
                {
                    TclList.append(interp, list2, TclString.newInstance(inString.Substring(elemStart)));
                }
                interp.setResult(list2);
            }
            finally
            {
                list2.release();
            }
            return(TCL.CompletionCode.RETURN);
        }
 public static void Tcl_DStringFree(ref TclObject str)
 {
     str.release();
 }
Esempio n. 17
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            int      i;
            Notifier notifier = (Notifier)interp.getNotifier();
            Object   info;

            if (assocData == null)
            {
                /*
                 * Create the "after" information associated for this
                 * interpreter, if it doesn't already exist.
                 */

                assocData = (AfterAssocData)interp.getAssocData("tclAfter");
                if (assocData == null)
                {
                    assocData = new AfterAssocData(this);
                    interp.setAssocData("tclAfter", assocData);
                }
            }

            if (argv.Length < 2)
            {
                throw new TclNumArgsException(interp, 1, argv, "option ?arg arg ...?");
            }

            /*
             * First lets see if the command was passed a number as the first argument.
             */

            bool isNumber = false;
            int  ms       = 0;

            if (argv[1].InternalRep is TclInteger)
            {
                ms       = TclInteger.get(interp, argv[1]);
                isNumber = true;
            }
            else
            {
                string s = argv[1].ToString();
                if ((s.Length > 0) && (System.Char.IsDigit(s[0])))
                {
                    ms       = TclInteger.get(interp, argv[1]);
                    isNumber = true;
                }
            }

            if (isNumber)
            {
                if (ms < 0)
                {
                    ms = 0;
                }
                if (argv.Length == 2)
                {
                    /*
                     * Sleep for at least the given milliseconds and return.
                     */

                    long endTime = System.DateTime.Now.Ticks / 10000 + ms;
                    while (true)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(ms);
                            return(TCL.CompletionCode.RETURN);
                        }
                        catch (System.Threading.ThreadInterruptedException e)
                        {
                            /*
                             * We got interrupted. Sleep again if we havn't slept
                             * long enough yet.
                             */

                            long sysTime = System.DateTime.Now.Ticks / 10000;
                            if (sysTime >= endTime)
                            {
                                return(TCL.CompletionCode.RETURN);
                            }
                            ms = (int)(endTime - sysTime);
                            continue;
                        }
                    }
                }

                TclObject cmd = getCmdObject(argv);
                cmd.preserve();

                assocData.lastAfterId++;
                TimerInfo timerInfo = new TimerInfo(this, notifier, ms);
                timerInfo.interp  = interp;
                timerInfo.command = cmd;
                timerInfo.id      = assocData.lastAfterId;

                assocData.handlers.Add(timerInfo);

                interp.setResult("after#" + timerInfo.id);

                return(TCL.CompletionCode.RETURN);
            }

            /*
             * If it's not a number it must be a subcommand.
             */

            int index;

            try
            {
                index = TclIndex.get(interp, argv[1], validOpts, "option", 0);
            }
            catch (TclException e)
            {
                throw new TclException(interp, "bad argument \"" + argv[1] + "\": must be cancel, idle, info, or a number");
            }

            switch (index)
            {
            case OPT_CANCEL:
                if (argv.Length < 3)
                {
                    throw new TclNumArgsException(interp, 2, argv, "id|command");
                }

                TclObject arg = getCmdObject(argv);
                arg.preserve();

                /*
                 * Search the timer/idle handler by id or by command.
                 */

                info = null;
                for (i = 0; i < assocData.handlers.Count; i++)
                {
                    Object obj = assocData.handlers[i];
                    if (obj is TimerInfo)
                    {
                        TclObject cmd = ((TimerInfo)obj).command;

                        if ((cmd == arg) || cmd.ToString().Equals(arg.ToString()))
                        {
                            info = obj;
                            break;
                        }
                    }
                    else
                    {
                        TclObject cmd = ((IdleInfo)obj).command;

                        if ((cmd == arg) || cmd.ToString().Equals(arg.ToString()))
                        {
                            info = obj;
                            break;
                        }
                    }
                }
                if (info == null)
                {
                    info = getAfterEvent(arg.ToString());
                }
                arg.release();

                /*
                 * Cancel the handler.
                 */

                if (info != null)
                {
                    if (info is TimerInfo)
                    {
                        ((TimerInfo)info).cancel();
                        ((TimerInfo)info).command.release();
                    }
                    else
                    {
                        ((IdleInfo)info).cancel();
                        ((IdleInfo)info).command.release();
                    }

                    SupportClass.VectorRemoveElement(assocData.handlers, info);
                }
                break;


            case OPT_IDLE:
                if (argv.Length < 3)
                {
                    throw new TclNumArgsException(interp, 2, argv, "script script ...");
                }

                TclObject cmd2 = getCmdObject(argv);
                cmd2.preserve();
                assocData.lastAfterId++;

                IdleInfo idleInfo = new IdleInfo(this, notifier);
                idleInfo.interp  = interp;
                idleInfo.command = cmd2;
                idleInfo.id      = assocData.lastAfterId;

                assocData.handlers.Add(idleInfo);

                interp.setResult("after#" + idleInfo.id);
                break;


            case OPT_INFO:
                if (argv.Length == 2)
                {
                    /*
                     * No id is given. Return a list of current after id's.
                     */

                    TclObject list = TclList.newInstance();
                    for (i = 0; i < assocData.handlers.Count; i++)
                    {
                        int    id;
                        Object obj = assocData.handlers[i];
                        if (obj is TimerInfo)
                        {
                            id = ((TimerInfo)obj).id;
                        }
                        else
                        {
                            id = ((IdleInfo)obj).id;
                        }
                        TclList.append(interp, list, TclString.newInstance("after#" + id));
                    }
                    interp.resetResult();
                    interp.setResult(list);
                    return(TCL.CompletionCode.RETURN);
                }
                if (argv.Length != 3)
                {
                    throw new TclNumArgsException(interp, 2, argv, "?id?");
                }

                /*
                 * Return command and type of the given after id.
                 */


                info = getAfterEvent(argv[2].ToString());
                if (info == null)
                {
                    throw new TclException(interp, "event \"" + argv[2] + "\" doesn't exist");
                }
                TclObject list2 = TclList.newInstance();
                TclList.append(interp, list2, ((info is TimerInfo)?((TimerInfo)info).command:((IdleInfo)info).command));
                TclList.append(interp, list2, TclString.newInstance((info is TimerInfo)?"timer":"idle"));

                interp.resetResult();
                interp.setResult(list2);
                break;
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 18
0
        /// <summary> See Tcl user documentation for details.</summary>
        /// <exception cref=""> TclException If incorrect number of arguments.
        /// </exception>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length != 4)
            {
                throw new TclNumArgsException(interp, 1, argv, "list first last");
            }

            int size = TclList.getLength(interp, argv[1]);
            int first;
            int last;

            first = Util.getIntForIndex(interp, argv[2], size - 1);
            last  = Util.getIntForIndex(interp, argv[3], size - 1);

            if (last < 0)
            {
                interp.resetResult();
                return(TCL.CompletionCode.RETURN);
            }
            if (first >= size)
            {
                interp.resetResult();
                return(TCL.CompletionCode.RETURN);
            }
            if (first <= 0 && last >= size)
            {
                interp.setResult(argv[1]);
                return(TCL.CompletionCode.RETURN);
            }

            if (first < 0)
            {
                first = 0;
            }
            if (first >= size)
            {
                first = size - 1;
            }
            if (last < 0)
            {
                last = 0;
            }
            if (last >= size)
            {
                last = size - 1;
            }
            if (first > last)
            {
                interp.resetResult();
                return(TCL.CompletionCode.RETURN);
            }

            TclObject list = TclList.newInstance();

            list.preserve();
            try
            {
                for (int i = first; i <= last; i++)
                {
                    TclList.append(interp, list, TclList.index(interp, argv[1], i));
                }
                interp.setResult(list);
            }
            finally
            {
                list.release();
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 19
0
        /*
         *----------------------------------------------------------------------
         *
         * cmdProc --
         *
         *	This procedure is invoked as part of the Command interface to
         *	process the "lsort" Tcl command.  See the user documentation for
         *	details on what it does.
         *
         * Results:
         *	A standard Tcl result.
         *
         * Side effects:
         *	See the user documentation.
         *
         *----------------------------------------------------------------------
         */

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length < 2)
            {
                throw new TclNumArgsException(interp, 1, argv, "?options? list");
            }

            string command        = null;
            int    sortMode       = QSort.ASCII;
            int    sortIndex      = -1;
            bool   sortIncreasing = true;
            bool   unique         = false;

            for (int i = 1; i < argv.Length - 1; i++)
            {
                int index = TclIndex.get(interp, argv[i], validOpts, "option", 0);

                switch (index)
                {
                case 0:
                    sortMode = QSort.ASCII;
                    break;


                case 1:
                    if (i == argv.Length - 2)
                    {
                        throw new TclException(interp, "\"-command\" option must be" + " followed by comparison command");
                    }
                    sortMode = QSort.COMMAND;

                    command = argv[i + 1].ToString();
                    i++;
                    break;


                case 2:
                    sortIncreasing = false;
                    break;


                case 3:
                    sortMode = QSort.DICTIONARY;
                    break;


                case 4:
                    sortIncreasing = true;
                    break;


                case 5:
                    if (i == argv.Length - 2)
                    {
                        throw new TclException(interp, "\"-index\" option must be followed by list index");
                    }
                    sortIndex = Util.getIntForIndex(interp, argv[i + 1], -2);

                    command = argv[i + 1].ToString();
                    i++;
                    break;


                case 6:
                    sortMode = QSort.INTEGER;
                    break;


                case 7:
                    sortMode = QSort.REAL;
                    break;

                case 8:         /* -unique */
                    unique = true;
                    break;
                }
            }

            TclObject list        = argv[argv.Length - 1];
            bool      isDuplicate = false;

            // If the list object is unshared we can modify it directly. Otherwise
            // we create a copy to modify: this is "copy on write".

            if (list.Shared)
            {
                list        = list.duplicate();
                isDuplicate = true;
            }

            try
            {
                TclList.sort(interp, list, sortMode, sortIndex, sortIncreasing, command, unique);
                interp.setResult(list);
            }
            catch (TclException e)
            {
                if (isDuplicate)
                {
                    list.release();
                }
                throw;
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 20
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv)
        {
            int len;

            if (objv.Length < 2)
            {
                throw new TclNumArgsException(interp, 1, objv, "option [arg arg ...]");
            }
            int opt = TclIndex.get(interp, objv[1], validCmds, "option", 0);

            switch (opt)
            {
            case OPT_VARIABLE:
            case OPT_VDELETE:
                if (objv.Length != 5)
                {
                    if (opt == OPT_VARIABLE)
                    {
                        throw new TclNumArgsException(interp, 1, objv, "variable name ops command");
                    }
                    else
                    {
                        throw new TclNumArgsException(interp, 1, objv, "vdelete name ops command");
                    }
                }

                TCL.VarFlag flags = 0;

                string ops = objv[3].ToString();
                len = ops.Length;
                {
                    for (int i = 0; i < len; i++)
                    {
                        switch (ops[i])
                        {
                        case 'r':
                            flags |= TCL.VarFlag.TRACE_READS;
                            break;

                        case 'w':
                            flags |= TCL.VarFlag.TRACE_WRITES;
                            break;

                        case 'u':
                            flags |= TCL.VarFlag.TRACE_UNSETS;
                            break;

                        default:
                            flags = 0;
                            goto check_ops_brk;
                        }
                    }
                }

check_ops_brk:
                ;


                if (flags == 0)
                {
                    throw new TclException(interp, "bad operations \"" + objv[3] + "\": should be one or more of rwu");
                }

                if (opt == OPT_VARIABLE)
                {
                    CmdTraceProc trace = new CmdTraceProc(objv[4].ToString(), flags);
                    Var.traceVar(interp, objv[2], flags, trace);
                }
                else
                {
                    // Search through all of our traces on this variable to
                    // see if there's one with the given command.  If so, then
                    // delete the first one that matches.


                    ArrayList traces = Var.getTraces(interp, objv[2].ToString(), 0);
                    if (traces != null)
                    {
                        len = traces.Count;
                        for (int i = 0; i < len; i++)
                        {
                            TraceRecord rec = (TraceRecord)traces[i];

                            if (rec.trace is CmdTraceProc)
                            {
                                CmdTraceProc proc = (CmdTraceProc)rec.trace;

                                if (proc.flags == flags && proc.command.ToString().Equals(objv[4].ToString()))
                                {
                                    Var.untraceVar(interp, objv[2], flags, proc);
                                    break;
                                }
                            }
                        }
                    }
                }
                break;


            case OPT_VINFO:
                if (objv.Length != 3)
                {
                    throw new TclNumArgsException(interp, 2, objv, "name");
                }

                ArrayList traces2 = Var.getTraces(interp, objv[2].ToString(), 0);
                if (traces2 != null)
                {
                    len = traces2.Count;
                    TclObject list = TclList.newInstance();
                    TclObject cmd  = null;
                    list.preserve();

                    try
                    {
                        for (int i = 0; i < len; i++)
                        {
                            TraceRecord rec = (TraceRecord)traces2[i];

                            if (rec.trace is CmdTraceProc)
                            {
                                CmdTraceProc proc = (CmdTraceProc)rec.trace;
                                TCL.VarFlag  mode = proc.flags;
                                mode &= (TCL.VarFlag.TRACE_READS | TCL.VarFlag.TRACE_WRITES | TCL.VarFlag.TRACE_UNSETS);
                                int modeInt = (int)mode;
                                modeInt /= ((int)TCL.VarFlag.TRACE_READS);

                                cmd = TclList.newInstance();
                                TclList.append(interp, cmd, opStr[modeInt]);
                                TclList.append(interp, cmd, TclString.newInstance(proc.command));
                                TclList.append(interp, list, cmd);
                            }
                        }
                        interp.setResult(list);
                    }
                    finally
                    {
                        list.release();
                    }
                }
                break;
            }
            return(TCL.CompletionCode.RETURN);
        }
Esempio n. 21
0
            public override int processEvent(int flags)
            {
                // See if the command is a complete Tcl command

                if (Interp.commandComplete(command))
                {
                    if (tcl.lang.ConsoleThread.debug)
                    {
                        WriteLine("line was a complete command");
                    }

                    bool      eval_exception = true;
                    TclObject commandObj     = TclString.newInstance(command);

                    try
                    {
                        commandObj.preserve();
                        Enclosing_Instance.interp.recordAndEval(commandObj, 0);
                        eval_exception = false;
                    }
                    catch (TclException e)
                    {
                        if (tcl.lang.ConsoleThread.debug)
                        {
                            WriteLine("eval returned exceptional condition");
                        }

                        TCL.CompletionCode code = e.getCompletionCode();
                        switch (code)
                        {
                        case TCL.CompletionCode.ERROR:

                            Enclosing_Instance.putLine(Enclosing_Instance.err, Enclosing_Instance.interp.getResult().ToString());
                            break;

                        case TCL.CompletionCode.BREAK:
                            Enclosing_Instance.putLine(Enclosing_Instance.err, "invoked \"break\" outside of a loop");
                            break;

                        case TCL.CompletionCode.CONTINUE:
                            Enclosing_Instance.putLine(Enclosing_Instance.err, "invoked \"continue\" outside of a loop");
                            break;

                        default:
                            Enclosing_Instance.putLine(Enclosing_Instance.err, "command returned bad code: " + code);
                            break;
                        }
                    }
                    finally
                    {
                        commandObj.release();
                    }

                    if (!eval_exception)
                    {
                        if (tcl.lang.ConsoleThread.debug)
                        {
                            WriteLine("eval returned normally");
                        }


                        string evalResult = Enclosing_Instance.interp.getResult().ToString();

                        if (tcl.lang.ConsoleThread.debug)
                        {
                            WriteLine("eval result was \"" + evalResult + "\"");
                        }

                        if (evalResult.Length > 0)
                        {
                            Enclosing_Instance.putLine(Enclosing_Instance.out_Renamed, evalResult);
                        }
                    }

                    // Empty out the incoming command buffer
                    Enclosing_Instance.sbuf.Length = 0;

                    // See if the user set a custom shell prompt for the next command

                    TclObject prompt;

                    try
                    {
                        prompt = Enclosing_Instance.interp.getVar("tcl_prompt1", TCL.VarFlag.GLOBAL_ONLY);
                    }
                    catch (TclException e)
                    {
                        prompt = null;
                    }
                    if (prompt != null)
                    {
                        try
                        {
                            Enclosing_Instance.interp.eval(prompt.ToString(), TCL.EVAL_GLOBAL);
                        }
                        catch (TclException e)
                        {
                            Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "% ");
                        }
                    }
                    else
                    {
                        Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "% ");
                    }

                    return(1);
                }
                else
                {
                    // Interp.commandComplete() returned false

                    if (tcl.lang.ConsoleThread.debug)
                    {
                        WriteLine("line was not a complete command");
                    }

                    // We don't have a complete command yet. Print out a level 2
                    // prompt message and wait for further inputs.

                    TclObject prompt;

                    try
                    {
                        prompt = Enclosing_Instance.interp.getVar("tcl_prompt2", TCL.VarFlag.GLOBAL_ONLY);
                    }
                    catch (TclException)
                    {
                        prompt = null;
                    }
                    if (prompt != null)
                    {
                        try
                        {
                            Enclosing_Instance.interp.eval(prompt.ToString(), TCL.EVAL_GLOBAL);
                        }
                        catch (TclException e)
                        {
                            Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "");
                        }
                    }
                    else
                    {
                        Enclosing_Instance.put(Enclosing_Instance.out_Renamed, "");
                    }

                    return(1);
                }
            } // end processEvent method
Esempio n. 22
0
            public override void  processIdleEvent()
            {
                // During the execution of this method, elements may be removed from
                // the errors list (because a TCL.CompletionCode.BREAK was returned by the bgerror
                // command, or because the interp was deleted). We remove this
                // BgError instance from the list first so that this instance won't
                // be deleted twice.

                SupportClass.VectorRemoveElement(Enclosing_Instance.errors, this);

                // Restore important state variables to what they were at
                // the time the error occurred.

                try
                {
                    Enclosing_Instance.interp.setVar("errorInfo", null, errorInfo, TCL.VarFlag.GLOBAL_ONLY);
                }
                catch (TclException e)
                {
                    // Ignore any TclException's, possibly caused by variable traces on
                    // the errorInfo variable. This is compatible with the behavior of
                    // the Tcl C API.
                }

                try
                {
                    Enclosing_Instance.interp.setVar("errorCode", null, errorCode, TCL.VarFlag.GLOBAL_ONLY);
                }
                catch (TclException e)
                {
                    // Ignore any TclException's, possibly caused by variable traces on
                    // the errorCode variable. This is compatible with the behavior of
                    // the Tcl C API.
                }

                // Make sure, that the interpreter will surive the invocation
                // of the bgerror command.

                Enclosing_Instance.interp.preserve();

                try
                {
                    // Invoke the bgerror command.

                    TclObject[] argv = new TclObject[2];
                    argv[0] = Enclosing_Instance.bgerrorCmdObj;
                    argv[1] = errorMsg;

                    Parser.evalObjv(Enclosing_Instance.interp, argv, 0, TCL.EVAL_GLOBAL);
                }
                catch (TclException e)
                {
                    switch (e.getCompletionCode())
                    {
                    case TCL.CompletionCode.ERROR:
                        try
                        {
                            Channel chan = TclIO.getStdChannel(StdChannel.STDERR);

                            if (Enclosing_Instance.interp.getResult().ToString().Equals("\"bgerror\" is an invalid command name or ambiguous abbreviation"))
                            {
                                chan.write(Enclosing_Instance.interp, errorInfo);
                                chan.write(Enclosing_Instance.interp, "\n");
                            }
                            else
                            {
                                chan.write(Enclosing_Instance.interp, "bgerror failed to handle background error.\n");
                                chan.write(Enclosing_Instance.interp, "    Original error: ");
                                chan.write(Enclosing_Instance.interp, errorMsg);
                                chan.write(Enclosing_Instance.interp, "\n");
                                chan.write(Enclosing_Instance.interp, "    Error in bgerror: ");
                                chan.write(Enclosing_Instance.interp, Enclosing_Instance.interp.getResult());
                                chan.write(Enclosing_Instance.interp, "\n");
                            }
                            chan.flush(Enclosing_Instance.interp);
                        }
                        catch (TclException e1)
                        {
                            // Ignore.
                        }
                        catch (System.IO.IOException e2)
                        {
                            // Ignore, too.
                        }
                        break;


                    case TCL.CompletionCode.BREAK:

                        for (int i = Enclosing_Instance.errors.Count - 1; i >= 0; i--)
                        {
                            BgError bgErr = (BgError)Enclosing_Instance.errors[i];
                            Enclosing_Instance.errors.RemoveAt(i);
                            bgErr.cancel();

                            bgErr.errorMsg.release();
                            bgErr.errorMsg = null;
                            bgErr.errorInfo.release();
                            bgErr.errorInfo = null;
                            bgErr.errorCode.release();
                            bgErr.errorCode = null;
                        }
                        break;
                    }
                }

                Enclosing_Instance.interp.release();

                errorMsg.release();
                errorMsg = null;
                errorInfo.release();
                errorInfo = null;
                errorCode.release();
                errorCode = null;
            }