Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SharpExpectProcess.cs « SharpExpect « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 240339cf0274fe0faacbd0d4ddc1cf73603a287d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;

namespace Duplicati.Library.SharpExpect
{
    public class SharpExpectProcess
    {
        private System.Diagnostics.Process m_process;
        private object m_lock = new object();
        private System.Threading.Thread m_stdOutReader;
        private System.Threading.Thread m_stdErrReader;
        private Queue<string> m_stdOut = new Queue<string>();
        private Queue<string> m_stdErr = new Queue<string>();
        private System.Threading.AutoResetEvent m_event = new System.Threading.AutoResetEvent(false);
        
        public SharpExpectProcess(System.Diagnostics.Process process)
        {
            m_process = process;

            if (m_process.StartInfo.RedirectStandardError)
            {
                m_stdErrReader = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StreamReader));
                m_stdErrReader.Start(m_process.StandardError);
            }

            if (m_process.StartInfo.RedirectStandardOutput)
            {
                m_stdOutReader = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StreamReader));
                m_stdOutReader.Start(m_process.StandardOutput);
            }
        }

        public string GetNextOutputLine(int millisecondsTimeout)
        {
            //Is output buffered?
            lock (m_lock)
            {
                if (m_stdOut.Count > 0)
                    return m_stdOut.Dequeue();

                if (m_stdErr.Count > 0)
                    return m_stdErr.Dequeue();
            }

            if (millisecondsTimeout > 0)
            {
                m_event.WaitOne(millisecondsTimeout);
                return GetNextOutputLine(0);
            }
            else
                return null;
        }

        public int Expect(int millisecondsTimeout, params string[] possibilities)
        {
            List<KeyValuePair<System.Text.RegularExpressions.Regex, int>> lst = new List<KeyValuePair<System.Text.RegularExpressions.Regex, int>>();
            for (int i = 0; i < possibilities.Length; i++)
                if (!string.IsNullOrEmpty(possibilities[i]))
                    lst.Add(new KeyValuePair<System.Text.RegularExpressions.Regex, int>(new System.Text.RegularExpressions.Regex(possibilities[i]), i));

            return Expect<int>(millisecondsTimeout, lst);
        }

        public T Expect<T>(int millisecondsTimeout, List<KeyValuePair<string, T>> possibilities)
        {
            List<KeyValuePair<System.Text.RegularExpressions.Regex, T>> lst = new List<KeyValuePair<System.Text.RegularExpressions.Regex, T>>();
            for (int i = 0; i < possibilities.Count; i++)
                if (!string.IsNullOrEmpty(possibilities[i].Key))
                    lst.Add(new KeyValuePair<System.Text.RegularExpressions.Regex, T>(new System.Text.RegularExpressions.Regex(possibilities[i].Key), possibilities[i].Value));

            return Expect<T>(millisecondsTimeout, possibilities);
        }

        public T Expect<T>(int millisecondsTimeout, List<KeyValuePair<System.Text.RegularExpressions.Regex ,T>> possibilities)
        {
            if (possibilities == null || possibilities.Count == 0)
                return default(T);

            string line = GetNextOutputLine(millisecondsTimeout);
            if (line == null)
                return default(T);

            foreach (KeyValuePair<System.Text.RegularExpressions.Regex, T> expr in possibilities)
                if (expr.Key.Match(line).Success)
                    return expr.Value;

            return default(T);
        }

        public void Sendline(string line)
        {
            m_process.StandardInput.WriteLine(line);
        }

        private void StreamReader(object input)
        {
            char[] buf = new char[1024];
            System.IO.StreamReader sr = (System.IO.StreamReader)input;
            Queue<string> queue = sr == m_process.StandardError ? m_stdErr : m_stdOut;

            while (!m_process.HasExited)
            {
                int r = sr.Read(buf, 0, buf.Length);
                if (r > 0)
                {
                    lock (m_lock)
                    {
                        queue.Enqueue(new string(buf, 0, r));
                        m_event.Set();
                    }
                }
            }
        }

        public System.Diagnostics.Process Process { get { return m_process; } }


        public static SharpExpectProcess Spawn(string FileName, string Arguments)
        {
            System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo(FileName, Arguments);
            pi.RedirectStandardError = true;
            pi.RedirectStandardOutput = true;
            pi.RedirectStandardInput = true;
            return Spawn(pi);
        }

        public static SharpExpectProcess Spawn(System.Diagnostics.ProcessStartInfo startInfo)
        {
            return new SharpExpectProcess(System.Diagnostics.Process.Start(startInfo));
        }
    }
}