Exemplo n.º 1
0
        internal Procedure(Interp interp, NamespaceCmd.Namespace ns, string name, TclObject args, TclObject b, string sFileName, int sLineNumber)
        {
            this.ns       = ns;
            srcFileName   = sFileName;
            srcLineNumber = sLineNumber;

            // Break up the argument list into argument specifiers, then process
            // each argument specifier.

            int numArgs = TclList.getLength(interp, args);

            argList = new TclObject[numArgs][];
            for (int i = 0; i < numArgs; i++)
            {
                argList[i] = new TclObject[2];
            }

            for (int i = 0; i < numArgs; i++)
            {
                // Now divide the specifier up into name and default.

                TclObject argSpec = TclList.index(interp, args, i);
                int       specLen = TclList.getLength(interp, argSpec);

                if (specLen == 0)
                {
                    throw new TclException(interp, "procedure \"" + name + "\" has argument with no name");
                }
                if (specLen > 2)
                {
                    throw new TclException(interp, "too many fields in argument " + "specifier \"" + argSpec + "\"");
                }

                argList[i][0] = TclList.index(interp, argSpec, 0);
                argList[i][0].preserve();
                if (specLen == 2)
                {
                    argList[i][1] = TclList.index(interp, argSpec, 1);
                    argList[i][1].preserve();
                }
                else
                {
                    argList[i][1] = null;
                }
            }


            if (numArgs > 0 && (argList[numArgs - 1][0].ToString().Equals("args")))
            {
                isVarArgs = true;
            }
            else
            {
                isVarArgs = false;
            }


            body        = new CharPointer(b.ToString());
            body_length = body.length();
        }
Exemplo n.º 2
0
 public static void Tcl_ListObjIndex(Interp interp, TclObject to, int nItem, out TclObject elmObj)
 {
     try
     {
         elmObj = TclList.index(interp, to, nItem);
     }
     catch
     {
         elmObj = null;
     }
 }
Exemplo n.º 3
0
        internal static TclObject splitAndTranslate(Interp interp, string path)
        {
            TclObject splitResult = splitPath(interp, path);

            int len = TclList.getLength(interp, splitResult);

            if (len == 1)
            {
                string fileName = TclList.index(interp, splitResult, 0).ToString();
                if (fileName[0] == '~')
                {
                    string user = translateFileName(interp, fileName);
                    splitResult = splitPath(interp, user);
                }
            }
            return(splitResult);
        }
Exemplo n.º 4
0
        /// <summary> See Tcl user documentation for details.</summary>
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            string sep = null;

            if (argv.Length == 2)
            {
                sep = null;
            }
            else if (argv.Length == 3)
            {
                sep = argv[2].ToString();
            }
            else
            {
                throw new TclNumArgsException(interp, 1, argv, "list ?joinString?");
            }
            TclObject list = argv[1];
            int       size = TclList.getLength(interp, list);

            if (size == 0)
            {
                interp.resetResult();
                return(TCL.CompletionCode.RETURN);
            }


            System.Text.StringBuilder sbuf = new System.Text.StringBuilder(TclList.index(interp, list, 0).ToString());

            for (int i = 1; i < size; i++)
            {
                if ((System.Object)sep == null)
                {
                    sbuf.Append(' ');
                }
                else
                {
                    sbuf.Append(sep);
                }

                sbuf.Append(TclList.index(interp, list, i).ToString());
            }
            interp.setResult(sbuf.ToString());
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 5
0
        private static string getTail(Interp interp, string path)
        {
            // Split the path and return the string form of the last component,
            // unless there is only one component which is the root or an absolute
            // path.

            TclObject splitResult = FileUtil.splitAndTranslate(interp, path);

            int last = TclList.getLength(interp, splitResult) - 1;

            if (last >= 0)
            {
                if ((last > 0) || (FileUtil.getPathType(path) == FileUtil.PATH_RELATIVE))
                {
                    TclObject tailObj = TclList.index(interp, splitResult, last);

                    return(tailObj.ToString());
                }
            }
            return("");
        }
Exemplo n.º 6
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            if (argv.Length != 3)
            {
                throw new TclNumArgsException(interp, 1, argv, "list index");
            }

            int       size    = TclList.getLength(interp, argv[1]);
            int       index   = Util.getIntForIndex(interp, argv[2], size - 1);
            TclObject element = TclList.index(interp, argv[1], index);

            if (element != null)
            {
                interp.setResult(element);
            }
            else
            {
                interp.resetResult();
            }
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        /// <summary> This procedure is invoked to process the "open" Tcl command.
        /// See the user documentation for details on what it does.
        ///
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="argv">command arguments.
        /// </param>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            bool pipeline  = false;        /* True if opening pipeline chan */
            int  prot      = 438;          /* Final rdwr permissions of file */
            int  modeFlags = TclIO.RDONLY; /* Rdwr mode for the file.  See the
                                            * TclIO class for more info on the
                                            * valid modes */

            if ((argv.Length < 2) || (argv.Length > 4))
            {
                throw new TclNumArgsException(interp, 1, argv, "fileName ?access? ?permissions?");
            }

            if (argv.Length > 2)
            {
                TclObject mode = argv[2];

                string modeStr = mode.ToString();
                int    len     = modeStr.Length;

                // This "r+1" hack is just to get a test case to pass
                if ((len == 0) || (modeStr.StartsWith("r+") && len >= 3))
                {
                    throw new TclException(interp, "illegal access mode \"" + modeStr + "\"");
                }

                if (len < 3)
                {
                    switch (modeStr[0])
                    {
                    case 'r':  {
                        if (len == 1)
                        {
                            modeFlags = TclIO.RDONLY;
                            break;
                        }
                        else if (modeStr[1] == '+')
                        {
                            modeFlags = TclIO.RDWR;
                            break;
                        }
                    }
                        goto case 'w';

                    case 'w':  {
                        FileInfo f = FileUtil.getNewFileObj(interp, argv[1].ToString());
                        bool     tmpBool;
                        if (File.Exists(f.FullName))
                        {
                            tmpBool = true;
                        }
                        else
                        {
                            tmpBool = Directory.Exists(f.FullName);
                        }
                        if (tmpBool)
                        {
                            bool tmpBool2;
                            try {
                                if (File.Exists(f.FullName))
                                {
                                    File.SetAttributes(f.FullName, FileAttributes.Normal);
                                    File.Delete(f.FullName);
                                    tmpBool2 = true;
                                }
                                else if (Directory.Exists(f.FullName))
                                {
                                    Directory.Delete(f.FullName);
                                    tmpBool2 = true;
                                }
                                else
                                {
                                    tmpBool2 = false;
                                }
                            }
                            // ATK added because .NET do not allow often to delete
                            // files used by another process
                            catch (System.IO.IOException e)
                            {
                                throw new TclException(interp, "cannot open file: " + argv[1].ToString());
                            }
                            bool generatedAux = tmpBool2;
                        }
                        if (len == 1)
                        {
                            modeFlags = (TclIO.WRONLY | TclIO.CREAT);
                            break;
                        }
                        else if (modeStr[1] == '+')
                        {
                            modeFlags = (TclIO.RDWR | TclIO.CREAT);
                            break;
                        }
                    }
                        goto case 'a';

                    case 'a':  {
                        if (len == 1)
                        {
                            modeFlags = (TclIO.WRONLY | TclIO.APPEND);
                            break;
                        }
                        else if (modeStr[1] == '+')
                        {
                            modeFlags = (TclIO.RDWR | TclIO.CREAT | TclIO.APPEND);
                            break;
                        }
                    }
                        goto default;

                    default:  {
                        throw new TclException(interp, "illegal access mode \"" + modeStr + "\"");
                    }
                    }
                }
                else
                {
                    modeFlags = 0;
                    bool gotRorWflag = false;
                    int  mlen        = TclList.getLength(interp, mode);
                    for (int i = 0; i < mlen; i++)
                    {
                        TclObject marg = TclList.index(interp, mode, i);

                        if (marg.ToString().Equals("RDONLY"))
                        {
                            modeFlags  |= TclIO.RDONLY;
                            gotRorWflag = true;
                        }
                        else
                        {
                            if (marg.ToString().Equals("WRONLY"))
                            {
                                modeFlags  |= TclIO.WRONLY;
                                gotRorWflag = true;
                            }
                            else
                            {
                                if (marg.ToString().Equals("RDWR"))
                                {
                                    modeFlags  |= TclIO.RDWR;
                                    gotRorWflag = true;
                                }
                                else
                                {
                                    if (marg.ToString().Equals("APPEND"))
                                    {
                                        modeFlags |= TclIO.APPEND;
                                    }
                                    else
                                    {
                                        if (marg.ToString().Equals("CREAT"))
                                        {
                                            modeFlags |= TclIO.CREAT;
                                        }
                                        else
                                        {
                                            if (marg.ToString().Equals("EXCL"))
                                            {
                                                modeFlags |= TclIO.EXCL;
                                            }
                                            else
                                            {
                                                if (marg.ToString().Equals("TRUNC"))
                                                {
                                                    modeFlags |= TclIO.TRUNC;
                                                }
                                                else
                                                {
                                                    throw new TclException(interp, "invalid access mode \"" + marg.ToString() + "\": must be RDONLY, WRONLY, RDWR, APPEND, " + "CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!gotRorWflag)
                    {
                        throw new TclException(interp, "access mode must include either RDONLY, WRONLY, or RDWR");
                    }
                }
            }

            if (argv.Length == 4)
            {
                prot = TclInteger.get(interp, argv[3]);
                throw new TclException(interp, "setting permissions not implemented yet");
            }

            if ((argv[1].ToString().Length > 0) && (argv[1].ToString()[0] == '|'))
            {
                pipeline = true;
                throw new TclException(interp, "pipes not implemented yet");
            }

            /*
             * Open the file or create a process pipeline.
             */

            if (!pipeline)
            {
                try
                {
                    FileChannel file = new FileChannel();

                    file.open(interp, argv[1].ToString(), modeFlags);
                    TclIO.registerChannel(interp, file);
                    interp.setResult(file.ChanName);
                }
                catch (System.IO.IOException e)
                {
                    throw new TclException(interp, "cannot open file: " + argv[1].ToString());
                }
            }
            else
            {
                /*
                 * Pipeline code here...
                 */
            }
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 9
0
        /// <summary> This procedure is invoked to process the "array" Tcl command.
        /// See the user documentation for details on what it does.
        /// </summary>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv)
        {
            Var    var = null, array = null;
            bool   notArray = false;
            string varName, msg;
            int    index;//, result;

            if (objv.Length < 3)
            {
                throw new TclNumArgsException(interp, 1, objv, "option arrayName ?arg ...?");
            }

            index = TclIndex.get(interp, objv[1], validCmds, "option", 0);

            // Locate the array variable (and it better be an array).


            varName = objv[2].ToString();
            Var[] retArray = Var.lookupVar(interp, varName, null, 0, null, false, false);

            // Assign the values returned in the array
            if (retArray != null)
            {
                var   = retArray[0];
                array = retArray[1];
            }

            if ((var == null) || !var.isVarArray() || var.isVarUndefined())
            {
                notArray = true;
            }

            // Special array trace used to keep the env array in sync for
            // array names, array get, etc.

            if (var != null && var.traces != null)
            {
                msg = Var.callTraces(interp, array, var, varName, null, (TCL.VarFlag.LEAVE_ERR_MSG | TCL.VarFlag.NAMESPACE_ONLY | TCL.VarFlag.GLOBAL_ONLY | TCL.VarFlag.TRACE_ARRAY));
                if ((System.Object)msg != null)
                {
                    throw new TclVarException(interp, varName, null, "trace array", msg);
                }
            }

            switch (index)
            {
            case OPT_ANYMORE:
            {
                if (objv.Length != 4)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName searchId");
                }
                if (notArray)
                {
                    errorNotArray(interp, objv[2].ToString());
                }

                if (var.sidVec == null)
                {
                    errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString());
                }


                SearchId e = var.getSearch(objv[3].ToString());
                if (e == null)
                {
                    errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString());
                }

                if (e.HasMore)
                {
                    interp.setResult("1");
                }
                else
                {
                    interp.setResult("0");
                }
                break;
            }

            case OPT_DONESEARCH:
            {
                if (objv.Length != 4)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName searchId");
                }
                if (notArray)
                {
                    errorNotArray(interp, objv[2].ToString());
                }

                bool rmOK = true;
                if (var.sidVec != null)
                {
                    rmOK = (var.removeSearch(objv[3].ToString()));
                }
                if ((var.sidVec == null) || !rmOK)
                {
                    errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString());
                }
                break;
            }

            case OPT_EXISTS:
            {
                if (objv.Length != 3)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName");
                }
                interp.setResult(!notArray);
                break;
            }

            case OPT_GET:
            {
                // Due to the differences in the hashtable implementation
                // from the Tcl core and Java, the output will be rearranged.
                // This is not a negative side effect, however, test results
                // will differ.

                if ((objv.Length != 3) && (objv.Length != 4))
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?");
                }
                if (notArray)
                {
                    return(TCL.CompletionCode.RETURN);
                }

                string pattern = null;
                if (objv.Length == 4)
                {
                    pattern = objv[3].ToString();
                }

                Hashtable table = (Hashtable)var.value;
                TclObject tobj  = TclList.newInstance();

                string arrayName = objv[2].ToString();
                string key, strValue;
                Var    var2;

                // Go through each key in the hash table.  If there is a
                // pattern, test for a match.  Each valid key and its value
                // is written into sbuf, which is returned.

                // FIXME : do we need to port over the 8.1 code for this loop?

                for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();)
                {
                    key  = ((string)e.Key);
                    var2 = (Var)e.Value;
                    if (var2.isVarUndefined())
                    {
                        continue;
                    }

                    if ((System.Object)pattern != null && !Util.stringMatch(key, pattern))
                    {
                        continue;
                    }


                    strValue = interp.getVar(arrayName, key, 0).ToString();

                    TclList.append(interp, tobj, TclString.newInstance(key));
                    TclList.append(interp, tobj, TclString.newInstance(strValue));
                }
                interp.setResult(tobj);
                break;
            }

            case OPT_NAMES:
            {
                if ((objv.Length != 3) && (objv.Length != 4))
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?");
                }
                if (notArray)
                {
                    return(TCL.CompletionCode.RETURN);
                }

                string pattern = null;
                if (objv.Length == 4)
                {
                    pattern = objv[3].ToString();
                }

                Hashtable table = (Hashtable)var.value;
                TclObject tobj  = TclList.newInstance();
                string    key;

                // Go through each key in the hash table.  If there is a
                // pattern, test for a match. Each valid key and its value
                // is written into sbuf, which is returned.

                for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();)
                {
                    key = (string)e.Key;
                    Var elem = (Var)e.Value;
                    if (!elem.isVarUndefined())
                    {
                        if ((System.Object)pattern != null)
                        {
                            if (!Util.stringMatch(key, pattern))
                            {
                                continue;
                            }
                        }
                        TclList.append(interp, tobj, TclString.newInstance(key));
                    }
                }
                interp.setResult(tobj);
                break;
            }

            case OPT_NEXTELEMENT:
            {
                if (objv.Length != 4)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName searchId");
                }
                if (notArray)
                {
                    errorNotArray(interp, objv[2].ToString());
                }

                if (var.sidVec == null)
                {
                    errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString());
                }


                SearchId e = var.getSearch(objv[3].ToString());
                if (e == null)
                {
                    errorIllegalSearchId(interp, objv[2].ToString(), objv[3].ToString());
                }
                if (e.HasMore)
                {
                    Hashtable       table = (Hashtable)var.value;
                    DictionaryEntry entry = e.nextEntry();
                    string          key   = (string)entry.Key;
                    Var             elem  = (Var)entry.Value;
                    if ((elem.flags & VarFlags.UNDEFINED) == 0)
                    {
                        interp.setResult(key);
                    }
                    else
                    {
                        interp.setResult("");
                    }
                }
                break;
            }

            case OPT_SET:
            {
                if (objv.Length != 4)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName list");
                }
                int size = TclList.getLength(interp, objv[3]);
                if (size % 2 != 0)
                {
                    throw new TclException(interp, "list must have an even number of elements");
                }

                int i;

                string name1 = objv[2].ToString();
                string name2, strValue;

                // Set each of the array variable names in the interp

                for (i = 0; i < size; i++)
                {
                    name2 = TclList.index(interp, objv[3], i++).ToString();

                    strValue = TclList.index(interp, objv[3], i).ToString();
                    interp.setVar(name1, name2, TclString.newInstance(strValue), 0);
                }
                break;
            }

            case OPT_SIZE:
            {
                if (objv.Length != 3)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName");
                }
                if (notArray)
                {
                    interp.setResult(0);
                }
                else
                {
                    Hashtable table = (Hashtable)var.value;
                    int       size  = 0;
                    for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();)
                    {
                        Var elem = (Var)e.Value;
                        if ((elem.flags & VarFlags.UNDEFINED) == 0)
                        {
                            size++;
                        }
                    }
                    interp.setResult(size);
                }
                break;
            }

            case OPT_STARTSEARCH:
            {
                if (objv.Length != 3)
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName");
                }
                if (notArray)
                {
                    errorNotArray(interp, objv[2].ToString());
                }

                if (var.sidVec == null)
                {
                    var.sidVec = new ArrayList(10);
                }

                // Create a SearchId Object:
                // To create a new SearchId object, a unique string
                // identifier needs to be composed and we need to
                // create an Enumeration of the array keys.  The
                // unique string identifier is created from three
                // strings:
                //
                //     "s-"   is the default prefix
                //     "i"    is a unique number that is 1+ the greatest
                //	      SearchId index currently on the ArrayVar.
                //     "name" is the name of the array
                //
                // Once the SearchId string is created we construct a
                // new SearchId object using the string and the
                // Enumeration.  From now on the string is used to
                // uniquely identify the SearchId object.

                int i = var.NextIndex;

                string s = "s-" + i + "-" + objv[2].ToString();
                IDictionaryEnumerator e = ((Hashtable)var.value).GetEnumerator();
                var.sidVec.Add(new SearchId(e, s, i));
                interp.setResult(s);
                break;
            }

            case OPT_UNSET:
            {
                string pattern;
                string name;

                if ((objv.Length != 3) && (objv.Length != 4))
                {
                    throw new TclNumArgsException(interp, 2, objv, "arrayName ?pattern?");
                }
                if (notArray)
                {
                    //Ignot this error -- errorNotArray(interp, objv[2].ToString());
                    break;
                }
                if (objv.Length == 3)
                {
                    // When no pattern is given, just unset the whole array

                    interp.unsetVar(objv[2], 0);
                }
                else
                {
                    pattern = objv[3].ToString();
                    Hashtable table = (Hashtable)(((Hashtable)var.value).Clone());
                    for (IDictionaryEnumerator e = table.GetEnumerator(); e.MoveNext();)
                    {
                        name = (string)e.Key;
                        Var elem = (Var)e.Value;
                        if (var.isVarUndefined())
                        {
                            continue;
                        }
                        if (Util.stringMatch(name, pattern))
                        {
                            interp.unsetVar(varName, name, 0);
                        }
                    }
                }
                break;
            }
            }
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 10
0
        /// <summary> This procedure is invoked to process the "fconfigure" Tcl command.
        /// See the user documentation for details on what it does.
        ///
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="argv">command arguments.
        /// </param>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            Channel chan; // The channel being operated on this method

            if ((argv.Length < 2) || (((argv.Length % 2) == 1) && (argv.Length != 3)))
            {
                throw new TclNumArgsException(interp, 1, argv, "channelId ?optionName? ?value? ?optionName value?...");
            }


            chan = TclIO.getChannel(interp, argv[1].ToString());
            if (chan == null)
            {
                throw new TclException(interp, "can not find channel named \"" + argv[1].ToString() + "\"");
            }

            if (argv.Length == 2)
            {
                // return list of all name/value pairs for this channelId
                TclObject list = TclList.newInstance();

                TclList.append(interp, list, TclString.newInstance("-blocking"));
                TclList.append(interp, list, TclBoolean.newInstance(chan.Blocking));

                TclList.append(interp, list, TclString.newInstance("-buffering"));
                TclList.append(interp, list, TclString.newInstance(TclIO.getBufferingString(chan.Buffering)));

                TclList.append(interp, list, TclString.newInstance("-buffersize"));
                TclList.append(interp, list, TclInteger.newInstance(chan.BufferSize));

                // -encoding

                TclList.append(interp, list, TclString.newInstance("-encoding"));

                System.Text.Encoding javaEncoding = chan.Encoding;
                string tclEncoding;
                if ((System.Object)javaEncoding == null)
                {
                    tclEncoding = "binary";
                }
                else
                {
                    tclEncoding = EncodingCmd.getTclName(javaEncoding);
                }
                TclList.append(interp, list, TclString.newInstance(tclEncoding));

                // -eofchar

                TclList.append(interp, list, TclString.newInstance("-eofchar"));
                if (chan.ReadOnly)
                {
                    char eofChar = chan.InputEofChar;
                    TclList.append(interp, list, (eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar));
                }
                else if (chan.WriteOnly)
                {
                    char eofChar = chan.OutputEofChar;
                    TclList.append(interp, list, (eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar));
                }
                else if (chan.ReadWrite)
                {
                    char inEofChar  = chan.InputEofChar;
                    char outEofChar = chan.OutputEofChar;

                    TclObject eofchar_pair = TclList.newInstance();

                    TclList.append(interp, eofchar_pair, (inEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(inEofChar));

                    TclList.append(interp, eofchar_pair, (outEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(outEofChar));

                    TclList.append(interp, list, eofchar_pair);
                }
                else
                {
                    // Not readable or writeable, do nothing
                }

                // -translation

                TclList.append(interp, list, TclString.newInstance("-translation"));

                if (chan.ReadOnly)
                {
                    TclList.append(interp, list, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation)));
                }
                else if (chan.WriteOnly)
                {
                    TclList.append(interp, list, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation)));
                }
                else if (chan.ReadWrite)
                {
                    TclObject translation_pair = TclList.newInstance();

                    TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation)));
                    TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation)));

                    TclList.append(interp, list, translation_pair);
                }
                else
                {
                    // Not readable or writeable, do nothing
                }

                interp.setResult(list);
            }

            if (argv.Length == 3)
            {
                // return value for supplied name

                int index = TclIndex.get(interp, argv[2], validCmds, "option", 0);

                switch (index)
                {
                case OPT_BLOCKING:
                {
                    // -blocking
                    interp.setResult(chan.Blocking);
                    break;
                }

                case OPT_BUFFERING:
                {
                    // -buffering
                    interp.setResult(TclIO.getBufferingString(chan.Buffering));
                    break;
                }

                case OPT_BUFFERSIZE:
                {
                    // -buffersize
                    interp.setResult(chan.BufferSize);
                    break;
                }

                case OPT_ENCODING:
                {
                    // -encoding
                    System.Text.Encoding javaEncoding = chan.Encoding;
                    if ((System.Object)javaEncoding == null)
                    {
                        interp.setResult("binary");
                    }
                    else
                    {
                        interp.setResult(EncodingCmd.getTclName(javaEncoding));
                    }
                    break;
                }

                case OPT_EOFCHAR:
                {
                    // -eofchar
                    if (chan.ReadOnly)
                    {
                        char eofChar = chan.InputEofChar;
                        interp.setResult((eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar));
                    }
                    else if (chan.WriteOnly)
                    {
                        char eofChar = chan.OutputEofChar;
                        interp.setResult((eofChar == 0) ? TclString.newInstance("") : TclString.newInstance(eofChar));
                    }
                    else if (chan.ReadWrite)
                    {
                        char inEofChar  = chan.InputEofChar;
                        char outEofChar = chan.OutputEofChar;

                        TclObject eofchar_pair = TclList.newInstance();

                        TclList.append(interp, eofchar_pair, (inEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(inEofChar));

                        TclList.append(interp, eofchar_pair, (outEofChar == 0) ? TclString.newInstance("") : TclString.newInstance(outEofChar));

                        interp.setResult(eofchar_pair);
                    }
                    else
                    {
                        // Not readable or writeable, do nothing
                    }

                    break;
                }

                case OPT_TRANSLATION:
                {
                    // -translation
                    if (chan.ReadOnly)
                    {
                        interp.setResult(TclIO.getTranslationString(chan.InputTranslation));
                    }
                    else if (chan.WriteOnly)
                    {
                        interp.setResult(TclIO.getTranslationString(chan.OutputTranslation));
                    }
                    else if (chan.ReadWrite)
                    {
                        TclObject translation_pair = TclList.newInstance();

                        TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.InputTranslation)));
                        TclList.append(interp, translation_pair, TclString.newInstance(TclIO.getTranslationString(chan.OutputTranslation)));

                        interp.setResult(translation_pair);
                    }
                    else
                    {
                        // Not readable or writeable, do nothing
                    }

                    break;
                }

                default:
                {
                    throw new TclRuntimeError("Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()");
                }
                }
            }
            for (int i = 3; i < argv.Length; i += 2)
            {
                // Iterate through the list setting the name with the
                // corresponding value.

                int index = TclIndex.get(interp, argv[i - 1], validCmds, "option", 0);

                switch (index)
                {
                case OPT_BLOCKING:
                {
                    // -blocking
                    chan.Blocking = TclBoolean.get(interp, argv[i]);
                    break;
                }

                case OPT_BUFFERING:
                {
                    // -buffering

                    int id = TclIO.getBufferingID(argv[i].ToString());

                    if (id == -1)
                    {
                        throw new TclException(interp, "bad value for -buffering: must be " + "one of full, line, or none");
                    }

                    chan.Buffering = id;
                    break;
                }

                case OPT_BUFFERSIZE:
                {
                    // -buffersize
                    chan.BufferSize = TclInteger.get(interp, argv[i]);
                    break;
                }

                case OPT_ENCODING:
                {
                    // -encoding

                    string tclEncoding = argv[i].ToString();

                    if (tclEncoding.Equals("") || tclEncoding.Equals("binary"))
                    {
                        chan.Encoding = null;
                    }
                    else
                    {
                        System.Text.Encoding javaEncoding = EncodingCmd.getJavaName(tclEncoding);
                        if ((System.Object)javaEncoding == null)
                        {
                            throw new TclException(interp, "unknown encoding \"" + tclEncoding + "\"");
                        }
                        chan.Encoding = javaEncoding;
                    }

                    break;
                }

                case OPT_EOFCHAR:
                {
                    // -eofchar
                    TclList.setListFromAny(interp, argv[i]);
                    int length = TclList.getLength(interp, argv[i]);

                    if (length > 2)
                    {
                        throw new TclException(interp, "bad value for -eofchar: " + "should be a list of zero, one, or two elements");
                    }

                    char   inputEofChar, outputEofChar;
                    string s;

                    if (length == 0)
                    {
                        inputEofChar = outputEofChar = (char)(0);
                    }
                    else if (length == 1)
                    {
                        s            = TclList.index(interp, argv[i], 0).ToString();
                        inputEofChar = outputEofChar = s[0];
                    }
                    else
                    {
                        s            = TclList.index(interp, argv[i], 0).ToString();
                        inputEofChar = s[0];


                        s             = TclList.index(interp, argv[i], 1).ToString();
                        outputEofChar = s[0];
                    }

                    chan.InputEofChar  = inputEofChar;
                    chan.OutputEofChar = outputEofChar;

                    break;
                }

                case OPT_TRANSLATION:
                {
                    // -translation
                    TclList.setListFromAny(interp, argv[i]);
                    int length = TclList.getLength(interp, argv[i]);

                    if (length < 1 || length > 2)
                    {
                        throw new TclException(interp, "bad value for -translation: " + "must be a one or two element list");
                    }

                    string inputTranslationArg, outputTranslationArg;
                    int    inputTranslation, outputTranslation;

                    if (length == 2)
                    {
                        inputTranslationArg = TclList.index(interp, argv[i], 0).ToString();
                        inputTranslation    = TclIO.getTranslationID(inputTranslationArg);

                        outputTranslationArg = TclList.index(interp, argv[i], 1).ToString();
                        outputTranslation    = TclIO.getTranslationID(outputTranslationArg);
                    }
                    else
                    {
                        outputTranslationArg = inputTranslationArg = argv[i].ToString();
                        outputTranslation    = inputTranslation = TclIO.getTranslationID(outputTranslationArg);
                    }

                    if ((inputTranslation == -1) || (outputTranslation == -1))
                    {
                        throw new TclException(interp, "bad value for -translation: " + "must be one of auto, binary, cr, lf, " + "crlf, or platform");
                    }

                    if (outputTranslation == TclIO.TRANS_AUTO)
                    {
                        outputTranslation = TclIO.TRANS_PLATFORM;
                    }

                    if (chan.ReadOnly)
                    {
                        chan.InputTranslation = inputTranslation;
                        if (inputTranslationArg.Equals("binary"))
                        {
                            chan.Encoding = null;
                        }
                    }
                    else if (chan.WriteOnly)
                    {
                        chan.OutputTranslation = outputTranslation;
                        if (outputTranslationArg.Equals("binary"))
                        {
                            chan.Encoding = null;
                        }
                    }
                    else if (chan.ReadWrite)
                    {
                        chan.InputTranslation  = inputTranslation;
                        chan.OutputTranslation = outputTranslation;
                        if (inputTranslationArg.Equals("binary") || outputTranslationArg.Equals("binary"))
                        {
                            chan.Encoding = null;
                        }
                    }
                    else
                    {
                        // Not readable or writeable, do nothing
                    }

                    break;
                }

                default:
                {
                    throw new TclRuntimeError("Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()");
                }
                }
            }
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 11
0
        /// <summary> Tcl_ForeachObjCmd -> ForeachCmd.cmdProc
        ///
        /// This procedure is invoked to process the "foreach" Tcl command.
        /// See the user documentation for details on what it does.
        ///
        /// </summary>
        /// <param name="interp">the current interpreter.
        /// </param>
        /// <param name="objv">command arguments.
        /// </param>
        /// <exception cref=""> TclException if script causes error.
        /// </exception>

        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] objv)
        {
            if (objv.Length < 4 || (objv.Length % 2) != 0)
            {
                throw new TclNumArgsException(interp, 1, objv, "varList list ?varList list ...? command");
            }

            // foreach {n1 n2} {1 2 3 4} {n3} {1 2} {puts $n1-$n2-$n3}
            //	name[0] = {n1 n2}	value[0] = {1 2 3 4}
            //	name[1] = {n3}		value[0] = {1 2}

            TclObject[] name  = new TclObject[(objv.Length - 2) / 2];
            TclObject[] value = new TclObject[(objv.Length - 2) / 2];

            int       c, i, j, base_;
            int       maxIter = 0;
            TclObject command = objv[objv.Length - 1];
            bool      done    = false;

            for (i = 0; i < objv.Length - 2; i += 2)
            {
                int x = i / 2;
                name[x]  = objv[i + 1];
                value[x] = objv[i + 2];

                int nSize = TclList.getLength(interp, name[x]);
                int vSize = TclList.getLength(interp, value[x]);

                if (nSize == 0)
                {
                    throw new TclException(interp, "foreach varlist is empty");
                }

                int iter = (vSize + nSize - 1) / nSize;
                if (maxIter < iter)
                {
                    maxIter = iter;
                }
            }

            for (c = 0; !done && c < maxIter; c++)
            {
                // Set up the variables

                for (i = 0; i < objv.Length - 2; i += 2)
                {
                    int x     = i / 2;
                    int nSize = TclList.getLength(interp, name[x]);
                    base_ = nSize * c;
                    for (j = 0; j < nSize; j++)
                    {
                        // Test and see if the name variable is an array.


                        Var[] result = Var.lookupVar(interp, name[x].ToString(), null, 0, null, false, false);
                        Var   var    = null;

                        if (result != null)
                        {
                            if (result[1] != null)
                            {
                                var = result[1];
                            }
                            else
                            {
                                var = result[0];
                            }
                        }

                        try
                        {
                            if (base_ + j >= TclList.getLength(interp, value[x]))
                            {
                                interp.setVar(TclList.index(interp, name[x], j), TclString.newInstance(""), 0);
                            }
                            else
                            {
                                interp.setVar(TclList.index(interp, name[x], j), TclList.index(interp, value[x], base_ + j), 0);
                            }
                        }
                        catch (TclException e)
                        {
                            throw new TclException(interp, "couldn't set loop variable: \"" + TclList.index(interp, name[x], j) + "\"");
                        }
                    }
                }

                // Execute the script

                try
                {
                    interp.eval(command, 0);
                }
                catch (TclException e)
                {
                    switch (e.getCompletionCode())
                    {
                    case TCL.CompletionCode.BREAK:
                        done = true;
                        break;


                    case TCL.CompletionCode.CONTINUE:
                        continue;


                    case TCL.CompletionCode.ERROR:
                        interp.addErrorInfo("\n    (\"foreach\" body line " + interp.errorLine + ")");
                        throw;


                    default:
                        throw;
                    }
                }
            }

            interp.resetResult();
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        /// <summary> Compares the order of two items in the array.
        ///
        /// </summary>
        /// <param name="obj1">first item.
        /// </param>
        /// <param name="obj2">second item.
        /// </param>
        /// <returns> 0 if they are equal, 1 if obj1 > obj2, -1 otherwise.
        ///
        /// </returns>
        /// <exception cref=""> TclException if an error occurs during sorting.
        /// </exception>
        private int compare(TclObject obj1, TclObject obj2)
        {
            int index;
            int code = 0;

            if (sortIndex != -1)
            {
                // The "-index" option was specified.  Treat each object as a
                // list, extract the requested element from each list, and
                // compare the elements, not the lists.  The special index "end"
                // is signaled here with a negative index (other than -1).

                TclObject obj;
                if (sortIndex < -1)
                {
                    index = TclList.getLength(sortInterp, obj1) - 1;
                }
                else
                {
                    index = sortIndex;
                }

                obj = TclList.index(sortInterp, obj1, index);
                if (obj == null)
                {
                    throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj1 + "\"");
                }
                obj1 = obj;

                if (sortIndex < -1)
                {
                    index = TclList.getLength(sortInterp, obj2) - 1;
                }
                else
                {
                    index = sortIndex;
                }

                obj = TclList.index(sortInterp, obj2, index);
                if (obj == null)
                {
                    throw new TclException(sortInterp, "element " + index + " missing from sublist \"" + obj2 + "\"");
                }
                obj2 = obj;
            }

            switch (sortMode)
            {
            case ASCII:
                // ATK C# CompareTo use option
                // similar to -dictionary but a > A
                code = System.Globalization.CultureInfo.InvariantCulture.CompareInfo.Compare(obj1.ToString(), obj2.ToString(), System.Globalization.CompareOptions.Ordinal);
                // code = obj1.ToString().CompareTo(obj2.ToString());
                break;

            case DICTIONARY:

                code = doDictionary(obj1.ToString(), obj2.ToString());
                break;

            case INTEGER:
                try
                {
                    int int1 = TclInteger.get(sortInterp, obj1);
                    int int2 = TclInteger.get(sortInterp, obj2);

                    if (int1 > int2)
                    {
                        code = 1;
                    }
                    else if (int2 > int1)
                    {
                        code = -1;
                    }
                }
                catch (TclException e1)
                {
                    sortInterp.addErrorInfo("\n    (converting list element from string to integer)");
                    throw e1;
                }
                break;

            case REAL:
                try
                {
                    double f1 = TclDouble.get(sortInterp, obj1);
                    double f2 = TclDouble.get(sortInterp, obj2);

                    if (f1 > f2)
                    {
                        code = 1;
                    }
                    else if (f2 > f1)
                    {
                        code = -1;
                    }
                }
                catch (TclException e2)
                {
                    sortInterp.addErrorInfo("\n    (converting list element from string to real)");
                    throw e2;
                }
                break;

            case COMMAND:
                System.Text.StringBuilder sbuf = new System.Text.StringBuilder(sortCommand);

                Util.appendElement(sortInterp, sbuf, obj1.ToString());

                Util.appendElement(sortInterp, sbuf, obj2.ToString());
                try
                {
                    sortInterp.eval(sbuf.ToString(), 0);
                }
                catch (TclException e3)
                {
                    sortInterp.addErrorInfo("\n    (user-defined comparison command)");
                    throw e3;
                }

                try
                {
                    code = TclInteger.get(sortInterp, sortInterp.getResult());
                }
                catch (TclException e)
                {
                    sortInterp.resetResult();
                    TclException e4 = new TclException(sortInterp, "comparison command returned non-numeric result");
                    throw e4;
                }
                break;


            default:

                throw new TclRuntimeError("Unknown sortMode " + sortMode);
            }

            if (sortIncreasing)
            {
                return(code);
            }
            else
            {
                return(-code);
            }
        }