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
        /// <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 != 2)
            {
                throw new TclNumArgsException(interp, 1, argv, "list");
            }
            interp.setResult(TclInteger.newInstance(TclList.getLength(interp, argv[1])));
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 3
0
 public static void Tcl_ListObjLength(Interp interp, TclObject to, out int nArg)
 {
     try
     {
         nArg = TclList.getLength(interp, to);
     }
     catch
     {
         nArg = 0;
     }
 }
Exemplo n.º 4
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.º 5
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.º 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
        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.º 8
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);
        }
Exemplo n.º 9
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.º 10
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.º 11
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            int arg;                      // Index of next argument to consume.

            char[] format = null;         // User specified format string.
            char   cmd;                   // Current format character.
            int    cursor;                // Current position within result buffer.
            int    maxPos;                // Greatest position within result buffer that
            // cursor has visited.
            int value = 0;                // Current integer value to be packed.
            // Initialized to avoid compiler warning.
            int offset, size = 0, length; //, index;

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

            switch (cmdIndex)
            {
            case CMD_FORMAT:
            {
                if (argv.Length < 3)
                {
                    throw new TclNumArgsException(interp, 2, argv, "formatString ?arg arg ...?");
                }

                // To avoid copying the data, we format the string in two passes.
                // The first pass computes the size of the output buffer.  The
                // second pass places the formatted data into the buffer.

                format = argv[2].ToString().ToCharArray();
                arg    = 3;
                length = 0;
                offset = 0;
                System.Int32 parsePos = 0;

                while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END)
                {
                    int count = GetFormatCount(format, ref parsePos);

                    switch (cmd)
                    {
                    case 'a':
                    case 'A':
                    case 'b':
                    case 'B':
                    case 'h':
                    case 'H':
                    {
                        // For string-type specifiers, the count corresponds
                        // to the number of bytes in a single argument.

                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        if (count == BINARY_ALL)
                        {
                            count = TclByteArray.getLength(interp, argv[arg]);
                        }
                        else if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        arg++;
                        switch (cmd)
                        {
                        case 'a':
                        case 'A': offset += count; break;

                        case 'b':
                        case 'B': offset += (count + 7) / 8; break;

                        case 'h':
                        case 'H': offset += (count + 1) / 2; break;
                        }
                        break;
                    }

                    case 'c':
                    case 's':
                    case 'S':
                    case 'i':
                    case 'I':
                    case 'f':
                    case 'd':
                    {
                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        switch (cmd)
                        {
                        case 'c': size = 1; break;

                        case 's':
                        case 'S': size = 2; break;

                        case 'i':
                        case 'I': size = 4; break;

                        case 'f': size = 4; break;

                        case 'd': size = 8; break;
                        }

                        // For number-type specifiers, the count corresponds
                        // to the number of elements in the list stored in
                        // a single argument.  If no count is specified, then
                        // the argument is taken as a single non-list value.

                        if (count == BINARY_NOCOUNT)
                        {
                            arg++;
                            count = 1;
                        }
                        else
                        {
                            int listc = TclList.getLength(interp, argv[arg++]);
                            if (count == BINARY_ALL)
                            {
                                count = listc;
                            }
                            else if (count > listc)
                            {
                                throw new TclException(interp, "number of elements in list" + " does not match count");
                            }
                        }
                        offset += count * size;
                        break;
                    }

                    case 'x':
                    {
                        if (count == BINARY_ALL)
                        {
                            throw new TclException(interp, "cannot use \"*\"" + " in format string with \"x\"");
                        }
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        offset += count;
                        break;
                    }

                    case 'X':
                    {
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        if ((count > offset) || (count == BINARY_ALL))
                        {
                            count = offset;
                        }
                        if (offset > length)
                        {
                            length = offset;
                        }
                        offset -= count;
                        break;
                    }

                    case '@':
                    {
                        if (offset > length)
                        {
                            length = offset;
                        }
                        if (count == BINARY_ALL)
                        {
                            offset = length;
                        }
                        else if (count == BINARY_NOCOUNT)
                        {
                            alephWithoutCount(interp);
                        }
                        else
                        {
                            offset = count;
                        }
                        break;
                    }

                    default:
                    {
                        badField(interp, cmd);
                    }
                    break;
                    }
                }
                if (offset > length)
                {
                    length = offset;
                }
                if (length == 0)
                {
                    return(TCL.CompletionCode.RETURN);
                }

                // Prepare the result object by preallocating the calculated
                // number of bytes and filling with nulls.

                TclObject resultObj = TclByteArray.newInstance();
                resultObj._typePtr = "bytearray";
                byte[] resultBytes = TclByteArray.setLength(interp, resultObj, length);
                interp.setResult(resultObj);

                // Pack the data into the result object.  Note that we can skip
                // the error checking during this pass, since we have already
                // parsed the string once.

                arg      = 3;
                cursor   = 0;
                maxPos   = cursor;
                parsePos = 0;

                while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END)
                {
                    int count = GetFormatCount(format, ref parsePos);

                    if ((count == 0) && (cmd != '@'))
                    {
                        arg++;
                        continue;
                    }

                    switch (cmd)
                    {
                    case 'a':
                    case 'A':
                    {
                        byte   pad   = (cmd == 'a') ? (byte)0 : (byte)SupportClass.Identity(' ');
                        byte[] bytes = TclByteArray.getBytes(interp, argv[arg++]);
                        length = bytes.Length;

                        if (count == BINARY_ALL)
                        {
                            count = length;
                        }
                        else if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        if (length >= count)
                        {
                            Array.Copy(bytes, 0, resultBytes, cursor, count);
                        }
                        else
                        {
                            Array.Copy(bytes, 0, resultBytes, cursor, length);
                            for (int ix = 0; ix < count - length; ix++)
                            {
                                resultBytes[cursor + length + ix] = pad;
                            }
                        }
                        cursor += count;
                        break;
                    }

                    case 'b':
                    case 'B':
                    {
                        char[] str = argv[arg++].ToString().ToCharArray();
                        if (count == BINARY_ALL)
                        {
                            count = str.Length;
                        }
                        else if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        int last = cursor + ((count + 7) / 8);
                        if (count > str.Length)
                        {
                            count = str.Length;
                        }
                        if (cmd == 'B')
                        {
                            for (offset = 0; offset < count; offset++)
                            {
                                value <<= 1;
                                if (str[offset] == '1')
                                {
                                    value |= 1;
                                }
                                else if (str[offset] != '0')
                                {
                                    expectedButGot(interp, "binary", new string( str ));
                                }
                                if (((offset + 1) % 8) == 0)
                                {
                                    resultBytes[cursor++] = (byte)value;
                                    value = 0;
                                }
                            }
                        }
                        else
                        {
                            for (offset = 0; offset < count; offset++)
                            {
                                value >>= 1;
                                if (str[offset] == '1')
                                {
                                    value |= 128;
                                }
                                else if (str[offset] != '0')
                                {
                                    expectedButGot(interp, "binary", new string( str ));
                                }
                                if (((offset + 1) % 8) == 0)
                                {
                                    resultBytes[cursor++] = (byte)value;
                                    value = 0;
                                }
                            }
                        }
                        if ((offset % 8) != 0)
                        {
                            if (cmd == 'B')
                            {
                                value <<= 8 - (offset % 8);
                            }
                            else
                            {
                                value >>= 8 - (offset % 8);
                            }
                            resultBytes[cursor++] = (byte)value;
                        }
                        while (cursor < last)
                        {
                            resultBytes[cursor++] = 0;
                        }
                        break;
                    }

                    case 'h':
                    case 'H':
                    {
                        char[] str = argv[arg++].ToString().ToCharArray();
                        if (count == BINARY_ALL)
                        {
                            count = str.Length;
                        }
                        else if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        int last = cursor + ((count + 1) / 2);
                        if (count > str.Length)
                        {
                            count = str.Length;
                        }
                        if (cmd == 'H')
                        {
                            for (offset = 0; offset < count; offset++)
                            {
                                value <<= 4;
                                int c = HEXDIGITS.IndexOf(Char.ToLower(str[offset]));
                                if (c < 0)
                                {
                                    expectedButGot(interp, "hexadecimal", new string( str ));
                                }
                                value |= (c & 0xf);
                                if ((offset % 2) != 0)
                                {
                                    resultBytes[cursor++] = (byte)value;
                                    value = 0;
                                }
                            }
                        }
                        else
                        {
                            for (offset = 0; offset < count; offset++)
                            {
                                value >>= 4;
                                int c = HEXDIGITS.IndexOf(Char.ToLower(str[offset]));
                                if (c < 0)
                                {
                                    expectedButGot(interp, "hexadecimal", new string( str ));
                                }
                                value |= ((c << 4) & 0xf0);
                                if ((offset % 2) != 0)
                                {
                                    resultBytes[cursor++] = (byte)value;
                                    value = 0;
                                }
                            }
                        }
                        if ((offset % 2) != 0)
                        {
                            if (cmd == 'H')
                            {
                                value <<= 4;
                            }
                            else
                            {
                                value >>= 4;
                            }
                            resultBytes[cursor++] = (byte)value;
                        }
                        while (cursor < last)
                        {
                            resultBytes[cursor++] = 0;
                        }
                        break;
                    }

                    case 'c':
                    case 's':
                    case 'S':
                    case 'i':
                    case 'I':
                    case 'f':
                    case 'd':
                    {
                        TclObject[] listv;

                        if (count == BINARY_NOCOUNT)
                        {
                            listv    = new TclObject[1];
                            listv[0] = argv[arg++];
                            count    = 1;
                        }
                        else
                        {
                            listv = TclList.getElements(interp, argv[arg++]);
                            if (count == BINARY_ALL)
                            {
                                count = listv.Length;
                            }
                        }
                        for (int ix = 0; ix < count; ix++)
                        {
                            cursor = FormatNumber(interp, cmd, listv[ix], resultBytes, cursor);
                        }
                        break;
                    }

                    case 'x':
                    {
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        for (int ix = 0; ix < count; ix++)
                        {
                            resultBytes[cursor++] = 0;
                        }
                        break;
                    }

                    case 'X':
                    {
                        if (cursor > maxPos)
                        {
                            maxPos = cursor;
                        }
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        if (count == BINARY_ALL || count > cursor)
                        {
                            cursor = 0;
                        }
                        else
                        {
                            cursor -= count;
                        }
                        break;
                    }

                    case '@':
                    {
                        if (cursor > maxPos)
                        {
                            maxPos = cursor;
                        }
                        if (count == BINARY_ALL)
                        {
                            cursor = maxPos;
                        }
                        else
                        {
                            cursor = count;
                        }
                        break;
                    }
                    }
                }
                break;
            }

            case CMD_SCAN:
            {
                if (argv.Length < 4)
                {
                    throw new TclNumArgsException(interp, 2, argv, "value formatString ?varName varName ...?");
                }
                byte[] src = TclByteArray.getBytes(interp, argv[2]);
                length = src.Length;
                format = argv[3].ToString().ToCharArray();
                arg    = 4;
                cursor = 0;
                offset = 0;
                System.Int32 parsePos = 0;

                while ((cmd = GetFormatSpec(format, ref parsePos)) != FORMAT_END)
                {
                    int count = GetFormatCount(format, ref parsePos);

                    switch (cmd)
                    {
                    case 'a':
                    case 'A':
                    {
                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        if (count == BINARY_ALL)
                        {
                            count = length - offset;
                        }
                        else
                        {
                            if (count == BINARY_NOCOUNT)
                            {
                                count = 1;
                            }
                            if (count > length - offset)
                            {
                                break;
                            }
                        }

                        size = count;

                        // Trim trailing nulls and spaces, if necessary.

                        if (cmd == 'A')
                        {
                            while (size > 0)
                            {
                                if (src[offset + size - 1] != '\x0000' && src[offset + size - 1] != ' ')
                                {
                                    break;
                                }
                                size--;
                            }
                        }

                        interp.setVar(argv[arg++], TclByteArray.newInstance(src, offset, size), 0);

                        offset += count;
                        break;
                    }

                    case 'b':
                    case 'B':
                    {
                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        if (count == BINARY_ALL)
                        {
                            count = (length - offset) * 8;
                        }
                        else
                        {
                            if (count == BINARY_NOCOUNT)
                            {
                                count = 1;
                            }
                            if (count > (length - offset) * 8)
                            {
                                break;
                            }
                        }
                        System.Text.StringBuilder s = new System.Text.StringBuilder(count);
                        int thisOffset = offset;

                        if (cmd == 'b')
                        {
                            for (int ix = 0; ix < count; ix++)
                            {
                                if ((ix % 8) != 0)
                                {
                                    value >>= 1;
                                }
                                else
                                {
                                    value = src[thisOffset++];
                                }
                                s.Append((value & 1) != 0 ? '1' : '0');
                            }
                        }
                        else
                        {
                            for (int ix = 0; ix < count; ix++)
                            {
                                if ((ix % 8) != 0)
                                {
                                    value <<= 1;
                                }
                                else
                                {
                                    value = src[thisOffset++];
                                }
                                s.Append((value & 0x80) != 0 ? '1' : '0');
                            }
                        }

                        interp.setVar(argv[arg++], TclString.newInstance(s.ToString()), 0);

                        offset += (count + 7) / 8;
                        break;
                    }

                    case 'h':
                    case 'H':
                    {
                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        if (count == BINARY_ALL)
                        {
                            count = (length - offset) * 2;
                        }
                        else
                        {
                            if (count == BINARY_NOCOUNT)
                            {
                                count = 1;
                            }
                            if (count > (length - offset) * 2)
                            {
                                break;
                            }
                        }
                        System.Text.StringBuilder s = new System.Text.StringBuilder(count);
                        int thisOffset = offset;

                        if (cmd == 'h')
                        {
                            for (int ix = 0; ix < count; ix++)
                            {
                                if ((ix % 2) != 0)
                                {
                                    value >>= 4;
                                }
                                else
                                {
                                    value = src[thisOffset++];
                                }
                                s.Append(HEXDIGITS[value & 0xf]);
                            }
                        }
                        else
                        {
                            for (int ix = 0; ix < count; ix++)
                            {
                                if ((ix % 2) != 0)
                                {
                                    value <<= 4;
                                }
                                else
                                {
                                    value = src[thisOffset++];
                                }
                                s.Append(HEXDIGITS[value >> 4 & 0xf]);
                            }
                        }

                        interp.setVar(argv[arg++], TclString.newInstance(s.ToString()), 0);

                        offset += (count + 1) / 2;
                        break;
                    }

                    case 'c':
                    case 's':
                    case 'S':
                    case 'i':
                    case 'I':
                    case 'f':
                    case 'd':
                    {
                        if (arg >= argv.Length)
                        {
                            missingArg(interp);
                        }
                        switch (cmd)
                        {
                        case 'c': size = 1; break;

                        case 's':
                        case 'S': size = 2; break;

                        case 'i':
                        case 'I': size = 4; break;

                        case 'f': size = 4; break;

                        case 'd': size = 8; break;
                        }
                        TclObject valueObj;
                        if (count == BINARY_NOCOUNT)
                        {
                            if (length - offset < size)
                            {
                                break;
                            }
                            valueObj = ScanNumber(src, offset, cmd);
                            offset  += size;
                        }
                        else
                        {
                            if (count == BINARY_ALL)
                            {
                                count = (length - offset) / size;
                            }
                            if (length - offset < count * size)
                            {
                                break;
                            }
                            valueObj = TclList.newInstance();
                            int thisOffset = offset;
                            for (int ix = 0; ix < count; ix++)
                            {
                                TclList.append(null, valueObj, ScanNumber(src, thisOffset, cmd));
                                thisOffset += size;
                            }
                            offset += count * size;
                        }

                        interp.setVar(argv[arg++], valueObj, 0);

                        break;
                    }

                    case 'x':
                    {
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        if (count == BINARY_ALL || count > length - offset)
                        {
                            offset = length;
                        }
                        else
                        {
                            offset += count;
                        }
                        break;
                    }

                    case 'X':
                    {
                        if (count == BINARY_NOCOUNT)
                        {
                            count = 1;
                        }
                        if (count == BINARY_ALL || count > offset)
                        {
                            offset = 0;
                        }
                        else
                        {
                            offset -= count;
                        }
                        break;
                    }

                    case '@':
                    {
                        if (count == BINARY_NOCOUNT)
                        {
                            alephWithoutCount(interp);
                        }
                        if (count == BINARY_ALL || count > length)
                        {
                            offset = length;
                        }
                        else
                        {
                            offset = count;
                        }
                        break;
                    }

                    default:
                    {
                        badField(interp, cmd);
                    }
                    break;
                    }
                }

                // Set the result to the last position of the cursor.

                interp.setResult(arg - 4);
            }
            break;
            }
            return(TCL.CompletionCode.RETURN);
        }
Exemplo n.º 12
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.º 13
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.º 14
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);
            }
        }
Exemplo n.º 15
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.º 16
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);
        }
Exemplo n.º 17
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.º 18
0
        public TCL.CompletionCode cmdProc(Interp interp, TclObject[] argv)
        {
            bool noComplain = false; // If false, error msg will be returned
            int  index;              // index of the char just after the end
            //   of the user name
            int       firstArg = 1;  // index of the first non-switch arg
            int       i;             // generic index
            string    arg;           // generic arg string
            string    head = "";     // abs path of user name if provided
            string    tail = "";     // the remaining file path and pattern
            TclObject resultList;    // list of files that match the pattern

            for (bool last = false; (firstArg < argv.Length) && (!last); firstArg++)
            {
                if (!argv[firstArg].ToString().StartsWith("-"))
                {
                    break;
                }
                int opt = TclIndex.get(interp, argv[firstArg], validOptions, "switch", 1);
                switch (opt)
                {
                case OPT_NOCOMPLAIN:
                    noComplain = true;
                    break;

                case OPT_LAST:
                    last = true;
                    break;

                default:
                    throw new TclException(interp, "GlobCmd.cmdProc: bad option " + opt + " index to validOptions");
                }
            }

            if (firstArg >= argv.Length)
            {
                throw new TclNumArgsException(interp, 1, argv, "?switches? name ?name ...?");
            }

            resultList = TclList.newInstance();
            resultList.preserve();

            for (i = firstArg; i < argv.Length; i++)
            {
                arg = argv[i].ToString();

                string separators; // The system-specific file separators
                switch (JACL.PLATFORM)
                {
                case JACL.PLATFORM_WINDOWS:
                    separators = "/\\:";
                    break;

                case JACL.PLATFORM_MAC:
                    if (arg.IndexOf((System.Char) ':') == -1)
                    {
                        separators = "/";
                    }
                    else
                    {
                        separators = ":";
                    }
                    break;

                default:
                    separators = "/";
                    break;
                }

                // Perform tilde substitution, if needed.

                index = 0;
                if (arg.StartsWith("~"))
                {
                    // Find the first path separator after the tilde.

                    for ( ; index < arg.Length; index++)
                    {
                        char c = arg[index];
                        if (c == '\\')
                        {
                            if (separators.IndexOf((System.Char)arg[index + 1]) != -1)
                            {
                                break;
                            }
                        }
                        else if (separators.IndexOf((System.Char)c) != -1)
                        {
                            break;
                        }
                    }

                    // Determine the home directory for the specified user.  Note
                    // that we don't allow special characters in the user name.

                    if (strpbrk(arg.Substring(1, (index) - (1)).ToCharArray(), specCharArr) < 0)
                    {
                        try
                        {
                            head = FileUtil.doTildeSubst(interp, arg.Substring(1, (index) - (1)));
                        }
                        catch (TclException e)
                        {
                            if (noComplain)
                            {
                                head = null;
                            }
                            else
                            {
                                throw new TclException(interp, e.Message);
                            }
                        }
                    }
                    else
                    {
                        if (!noComplain)
                        {
                            throw new TclException(interp, "globbing characters not supported in user names");
                        }
                        head = null;
                    }

                    if ((System.Object)head == null)
                    {
                        if (noComplain)
                        {
                            interp.setResult("");
                            return(TCL.CompletionCode.RETURN);
                        }
                        else
                        {
                            return(TCL.CompletionCode.RETURN);
                        }
                    }
                    if (index != arg.Length)
                    {
                        index++;
                    }
                }

                tail = arg.Substring(index);

                try
                {
                    doGlob(interp, separators, new StringBuilder(head), tail, resultList);
                }
                catch (TclException e)
                {
                    if (noComplain)
                    {
                        continue;
                    }
                    else
                    {
                        throw new TclException(interp, e.Message);
                    }
                }
            }

            // If the list is empty and the nocomplain switch was not set then
            // generate and throw an exception.  Always release the TclList upon
            // completion.

            try
            {
                if ((TclList.getLength(interp, resultList) == 0) && !noComplain)
                {
                    string        sep = "";
                    StringBuilder ret = new StringBuilder();

                    ret.Append("no files matched glob pattern");
                    ret.Append((argv.Length == 2) ? " \"" : "s \"");

                    for (i = firstArg; i < argv.Length; i++)
                    {
                        ret.Append(sep + argv[i].ToString());
                        if (i == firstArg)
                        {
                            sep = " ";
                        }
                    }
                    ret.Append("\"");
                    throw new TclException(interp, ret.ToString());
                }
                else if (TclList.getLength(interp, resultList) > 0)
                {
                    interp.setResult(resultList);
                }
            }
            finally
            {
                resultList.release();
            }
            return(TCL.CompletionCode.RETURN);
        }