Beispiel #1
0
        /// <summary> This procedure is invoked to process the "incr" Tcl command.
        /// See the user documentation for details on what it does.
        /// </summary>
        /// <exception cref=""> TclException if wrong # of args or increment is not an
        /// integer.
        /// </exception>

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] objv)
        {
            int incrAmount;
            TclObject newValue;

            if ((objv.Length != 2) && (objv.Length != 3))
            {
                throw new TclNumArgsException(interp, 1, objv, "varName ?increment?");
            }

            // Calculate the amount to increment by.

            if (objv.Length == 2)
            {
                incrAmount = 1;
            }
            else
            {
                try
                {
                    incrAmount = TclInteger.Get(interp, objv[2]);
                }
                catch (TclException e)
                {
                    interp.AddErrorInfo("\n    (reading increment)");
                    throw;
                }
            }

            // Increment the variable's value.

            newValue = Var.incrVar(interp, objv[1], null, incrAmount, TCL.VarFlag.LEAVE_ERR_MSG);

            // FIXME: we need to look at this exception throwing problem again
            /*
            if (newValue == null) {
            return TCL_ERROR;
            }
            */

            // Set the interpreter's object result to refer to the variable's new
            // value object.

            interp.SetResult(newValue);
            return TCL.CompletionCode.RETURN;
        }
Beispiel #2
0
        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            int i, mode, body;
            bool matched;
            string inString;
            TclObject[] switchArgv = null;

            mode = EXACT;
            for (i = 1; i < argv.Length; i++)
            {

                if (!argv[i].ToString().StartsWith("-"))
                {
                    break;
                }
                int opt = TclIndex.Get(interp, argv[i], validCmds, "option", 1);
                if (opt == LAST)
                {
                    i++;
                    break;
                }
                else if (opt > LAST)
                {
                    throw new TclException(interp, "SwitchCmd.cmdProc: bad option " + opt + " index to validCmds");
                }
                else
                {
                    mode = opt;
                }
            }

            if (argv.Length - i < 2)
            {
                throw new TclNumArgsException(interp, 1, argv, "?switches? string pattern body ... ?default body?");
            }

            inString = argv[i].ToString();
            i++;

            // If all of the pattern/command pairs are lumped into a single
            // argument, split them out again.

            if (argv.Length - i == 1)
            {
                switchArgv = TclList.getElements(interp, argv[i]);
                i = 0;
            }
            else
            {
                switchArgv = argv;
            }

            for (; i < switchArgv.Length; i += 2)
            {
                if (i == (switchArgv.Length - 1))
                {
                    throw new TclException(interp, "extra switch pattern with no body");
                }

                // See if the pattern matches the string.

                matched = false;

                string pattern = switchArgv[i].ToString();

                if ((i == switchArgv.Length - 2) && pattern.Equals("default"))
                {
                    matched = true;
                }
                else
                {
                    switch (mode)
                    {

                        case EXACT:
                            matched = inString.Equals(pattern);
                            break;

                        case GLOB:
                            matched = Util.StringMatch(inString, pattern);
                            break;

                        case REGEXP:
                            matched = Util.regExpMatch(interp, inString, switchArgv[i]);
                            break;
                    }
                }
                if (!matched)
                {
                    continue;
                }

                // We've got a match.  Find a body to execute, skipping bodies
                // that are "-".

                for (body = i + 1; ; body += 2)
                {
                    if (body >= switchArgv.Length)
                    {

                        throw new TclException(interp, "no body specified for pattern \"" + switchArgv[i] + "\"");
                    }

                    if (!switchArgv[body].ToString().Equals("-"))
                    {
                        break;
                    }
                }

                try
                {
                    interp.Eval(switchArgv[body], 0);
                    return TCL.CompletionCode.RETURN;
                }
                catch (TclException e)
                {
                    if (e.GetCompletionCode() == TCL.CompletionCode.ERROR)
                    {

                        interp.AddErrorInfo("\n    (\"" + switchArgv[i] + "\" arm line " + interp._errorLine + ")");
                    }
                    throw;
                }
            }

            // Nothing matched:  return nothing.
            return TCL.CompletionCode.RETURN;
        }
Beispiel #3
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)
        {
            int i;
            bool value;

            i = 1;
            while (true)
            {
                /*
                * At this point in the loop, argv and argc refer to an
                * expression to test, either for the main expression or
                * an expression following an "elseif".  The arguments
                * after the expression must be "then" (optional) and a
                * script to execute if the expression is true.
                */

                if (i >= argv.Length)
                {

                    throw new TclException(interp, "wrong # args: no expression after \"" + argv[i - 1] + "\" argument");
                }
                try
                {

                    value = interp._expr.EvalBoolean(interp, argv[i].ToString());
                }
                catch (TclException e)
                {
                    switch (e.GetCompletionCode())
                    {

                        case TCL.CompletionCode.ERROR:
                            interp.AddErrorInfo("\n    (\"if\" test expression)");
                            break;
                    }
                    throw;
                }

                i++;

                if ((i < argv.Length) && (argv[i].ToString().Equals("then")))
                {
                    i++;
                }
                if (i >= argv.Length)
                {

                    throw new TclException(interp, "wrong # args: no script following \"" + argv[i - 1] + "\" argument");
                }
                if (value)
                {
                    try
                    {
                        interp.Eval(argv[i], 0);
                    }
                    catch (TclException e)
                    {
                        switch (e.GetCompletionCode())
                        {

                            case TCL.CompletionCode.ERROR:
                                interp.AddErrorInfo("\n    (\"if\" then script line " + interp._errorLine + ")");
                                break;
                        }
                        throw;
                    }
                    return TCL.CompletionCode.RETURN;
                }

                /*
                * The expression evaluated to false.  Skip the command, then
                * see if there is an "else" or "elseif" clause.
                */

                i++;
                if (i >= argv.Length)
                {
                    interp.ResetResult();
                    return TCL.CompletionCode.RETURN;
                }

                if (argv[i].ToString().Equals("elseif"))
                {
                    i++;
                    continue;
                }
                break;
            }

            /*
            * Couldn't find a "then" or "elseif" clause to execute.
            * Check now for an "else" clause.  We know that there's at
            * least one more argument when we get here.
            */


            if (argv[i].ToString().Equals("else"))
            {
                i++;
                if (i >= argv.Length)
                {
                    throw new TclException(interp, "wrong # args: no script following \"else\" argument");
                }
                else if (i != (argv.Length - 1))
                {
                    throw new TclException(interp, "wrong # args: extra words after \"else\" clause in " + "\"if\" command");
                }
            }
            try
            {
                interp.Eval(argv[i], 0);
            }
            catch (TclException e)
            {
                switch (e.GetCompletionCode())
                {

                    case TCL.CompletionCode.ERROR:
                        interp.AddErrorInfo("\n    (\"if\" else script line " + interp._errorLine + ")");
                        break;
                }
                throw;
            }
            return TCL.CompletionCode.RETURN;
        }
Beispiel #4
0
        /*
        * This procedure is invoked to process the "for" Tcl command.
        * See the user documentation for details on what it does.
        *
        * @param interp the current interpreter.
        * @param argv command arguments.
        * @exception TclException if script causes error.
        */

        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length != 5)
            {
                throw new TclNumArgsException(interp, 1, argv, "start test next command");
            }

            TclObject start = argv[1];

            string test = argv[2].ToString();
            TclObject next = argv[3];
            TclObject command = argv[4];

            bool done = false;
            try
            {
                interp.Eval(start, 0);
            }
            catch (TclException e)
            {
                interp.AddErrorInfo("\n    (\"for\" initial command)");
                throw;
            }

            while (!done)
            {
                if (!interp._expr.EvalBoolean(interp, test))
                {
                    break;
                }

                try
                {
                    interp.Eval(command, 0);
                }
                catch (TclException e)
                {
                    switch (e.GetCompletionCode())
                    {

                        case TCL.CompletionCode.BREAK:
                            done = true;
                            break;


                        case TCL.CompletionCode.CONTINUE:
                            break;


                        case TCL.CompletionCode.ERROR:
                            interp.AddErrorInfo("\n    (\"for\" body line " + interp._errorLine + ")");
                            throw;


                        default:
                            throw;

                    }
                }

                if (!done)
                {
                    try
                    {
                        interp.Eval(next, 0);
                    }
                    catch (TclException e)
                    {
                        switch (e.GetCompletionCode())
                        {

                            case TCL.CompletionCode.BREAK:
                                done = true;
                                break;


                            case TCL.CompletionCode.CONTINUE:
                                break;


                            default:
                                interp.AddErrorInfo("\n    (\"for\" loop-end command)");
                                throw;

                        }
                    }
                }
            }

            interp.ResetResult();
            return TCL.CompletionCode.RETURN;
        }
Beispiel #5
0
        /// <summary> Executes a "case" statement. 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)
        {
            if (argv.Length < 3)
            {
                throw new TclNumArgsException(interp, 1, argv, "string ?in? patList body ... ?default body?");
            }

            int i;
            int body;
            TclObject[] caseArgv;
            string inString;


            inString = argv[1].ToString();
            caseArgv = argv;
            body = -1;


            if (argv[2].ToString().Equals("in"))
            {
                i = 3;
            }
            else
            {
                i = 2;
            }

            /*
            * If all of the pattern/command pairs are lumped into a single
            * argument, split them out again.
            */

            if (argv.Length - i == 1)
            {
                caseArgv = TclList.getElements(interp, argv[i]);
                i = 0;
            }

            {
                for (; i < caseArgv.Length; i += 2)
                {
                    int j;

                    if (i == (caseArgv.Length - 1))
                    {
                        throw new TclException(interp, "extra case pattern with no body");
                    }

                    /*
                    * Check for special case of single pattern (no list) with
                    * no backslash sequences.
                    */


                    string caseString = caseArgv[i].ToString();
                    int len = caseString.Length;
                    for (j = 0; j < len; j++)
                    {
                        char c = caseString[j];
                        if (System.Char.IsWhiteSpace(c) || (c == '\\'))
                        {
                            break;
                        }
                    }
                    if (j == len)
                    {
                        if (caseString.Equals("default"))
                        {
                            body = i + 1;
                        }
                        if (Util.StringMatch(inString, caseString))
                        {
                            body = i + 1;
                            goto match_loop_brk;
                        }
                        continue;
                    }

                    /*
                    * Break up pattern lists, then check each of the patterns
                    * in the list.
                    */

                    int numPats = TclList.getLength(interp, caseArgv[i]);
                    for (j = 0; j < numPats; j++)
                    {

                        if (Util.StringMatch(inString, TclList.index(interp, caseArgv[i], j).ToString()))
                        {
                            body = i + 1;
                            goto match_loop_brk;
                        }
                    }
                }
            }

        match_loop_brk:
            ;


            if (body != -1)
            {
                try
                {
                    interp.Eval(caseArgv[body], 0);
                }
                catch (TclException e)
                {
                    if (e.GetCompletionCode() == TCL.CompletionCode.ERROR)
                    {

                        interp.AddErrorInfo("\n    (\"" + caseArgv[body - 1] + "\" arm line " + interp._errorLine + ")");
                    }
                    throw;
                }
            }
            return TCL.CompletionCode.RETURN;
        }
Beispiel #6
0
        internal void transferResult(Interp sourceInterp, TCL.CompletionCode result)
        {
            if (sourceInterp == this)
            {
                return;
            }

            if (result == TCL.CompletionCode.ERROR)
            {
                TclObject obj;

                // An error occurred, so transfer error information from the source
                // interpreter to the target interpreter.  Setting the flags tells
                // the target interp that it has inherited a partial traceback
                // chain, not just a simple error message.

                if (!sourceInterp._errAlreadyLogged)
                {
                    sourceInterp.AddErrorInfo("");
                }
                sourceInterp._errAlreadyLogged = true;

                ResetResult();

                obj = sourceInterp.GetVar("errorInfo", TCL.VarFlag.GLOBAL_ONLY);
                SetVar("errorInfo", obj, TCL.VarFlag.GLOBAL_ONLY);

                obj = sourceInterp.GetVar("errorCode", TCL.VarFlag.GLOBAL_ONLY);
                SetVar("errorCode", obj, TCL.VarFlag.GLOBAL_ONLY);

                _errInProgress = true;
                _errCodeSet = true;
            }

            _returnCode = result;
            SetResult(sourceInterp.GetResult());
            sourceInterp.ResetResult();

            if (result != TCL.CompletionCode.OK)
            {

                throw new TclException(this, GetResult().ToString(), result);
            }
        }
Beispiel #7
0
        public TCL.CompletionCode CmdProc(Interp interp, TclObject[] argv)
        {
            string fileName = null;
            bool url = false;

            if (argv.Length == 2)
            {

                fileName = argv[1].ToString();
            }
            else if (argv.Length == 3)
            {

                if (argv[1].ToString().Equals("-url"))
                {
                    url = true;

                    fileName = argv[2].ToString();
                }
            }

            if ((System.Object)fileName == null)
            {
                throw new TclNumArgsException(interp, 1, argv, "?-url? fileName");
            }

            try
            {
                if (url)
                {
                    if (fileName.StartsWith("resource:/"))
                    {
                        interp.evalResource(fileName.Substring(9));
                    }
                    else
                    {
                        interp.evalURL(null, fileName);
                    }
                }
                else
                {
                    interp.evalFile(fileName);
                }
            }
            catch (TclException e)
            {
                TCL.CompletionCode code = e.GetCompletionCode();

                if (code == TCL.CompletionCode.RETURN)
                {
                    TCL.CompletionCode realCode = interp.updateReturnInfo();
                    if (realCode != TCL.CompletionCode.OK)
                    {
                        e.setCompletionCode(realCode);
                        throw;
                    }
                }
                else if (code == TCL.CompletionCode.ERROR)
                {
                    /*
                    * Record information telling where the error occurred.
                    */

                    interp.AddErrorInfo("\n    (file line " + interp._errorLine + ")");
                    throw;
                }
                else
                {
                    throw;
                }
            }
            return TCL.CompletionCode.RETURN;
        }