~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Core/MonoDevelop.Core.Execution/ProcessArgumentBuilder.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields, 1840cc1
  • Date: 2012-02-05 10:49:36 UTC
  • mfrom: (10.2.12)
  • Revision ID: package-import@ubuntu.com-20120205104936-f3dutq6lnseokb6d
Tags: 2.8.6.3+dfsg-1
[1840cc1] Imported Upstream version 2.8.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
25
// THE SOFTWARE.
 
26
 
26
27
using System;
27
28
using System.Text;
 
29
using System.Collections.Generic;
28
30
 
29
31
namespace MonoDevelop.Core.Execution
30
32
{
33
35
        /// </summary>
34
36
        public class ProcessArgumentBuilder
35
37
        {
36
 
                System.Text.StringBuilder sb = new System.Text.StringBuilder ();
 
38
                StringBuilder sb = new StringBuilder ();
37
39
                
38
40
                public string ProcessPath {
39
41
                        get; private set;
132
134
                                sb.Append (c);
133
135
                        }
134
136
                }
 
137
                
 
138
                static string GetArgument (StringBuilder builder, string buf, int startIndex, out int endIndex, out Exception ex)
 
139
                {
 
140
                        bool escaped = false;
 
141
                        char qchar, c = '\0';
 
142
                        int i = startIndex;
 
143
                        
 
144
                        builder.Clear ();
 
145
                        switch (buf[startIndex]) {
 
146
                        case '\'': qchar = '\''; i++; break;
 
147
                        case '"': qchar = '"'; i++; break;
 
148
                        default: qchar = '\0'; break;
 
149
                        }
 
150
                        
 
151
                        while (i < buf.Length) {
 
152
                                c = buf[i];
 
153
                                
 
154
                                if (c == qchar && !escaped) {
 
155
                                        // unescaped qchar means we've reached the end of the argument
 
156
                                        i++;
 
157
                                        break;
 
158
                                }
 
159
                                
 
160
                                if (c == '\\') {
 
161
                                        escaped = true;
 
162
                                } else if (escaped) {
 
163
                                        builder.Append (c);
 
164
                                        escaped = false;
 
165
                                } else if (qchar == '\0' && (c == ' ' || c == '\t')) {
 
166
                                        break;
 
167
                                } else {
 
168
                                        builder.Append (c);
 
169
                                }
 
170
                                
 
171
                                i++;
 
172
                        }
 
173
                        
 
174
                        if (escaped || (qchar != '\0' && c != qchar)) {
 
175
                                ex = new FormatException (escaped ? "Incomplete escape sequence." : "No matching quote found.");
 
176
                                endIndex = -1;
 
177
                                return null;
 
178
                        }
 
179
                        
 
180
                        endIndex = i;
 
181
                        ex = null;
 
182
                        
 
183
                        return builder.ToString ();
 
184
                }
 
185
                
 
186
                static bool TryParse (string commandline, out string[] argv, out Exception ex)
 
187
                {
 
188
                        StringBuilder builder = new StringBuilder ();
 
189
                        List<string> args = new List<string> ();
 
190
                        string argument;
 
191
                        int i = 0, j;
 
192
                        char c;
 
193
                        
 
194
                        while (i < commandline.Length) {
 
195
                                c = commandline[i];
 
196
                                if (c != ' ' && c != '\t') {
 
197
                                        if ((argument = GetArgument (builder, commandline, i, out j, out ex)) == null) {
 
198
                                                argv =  null;
 
199
                                                return false;
 
200
                                        }
 
201
                                        
 
202
                                        args.Add (argument);
 
203
                                        i = j;
 
204
                                }
 
205
                                
 
206
                                i++;
 
207
                        }
 
208
                        
 
209
                        argv = args.ToArray ();
 
210
                        ex = null;
 
211
                        
 
212
                        return true;
 
213
                }
 
214
                
 
215
                public static bool TryParse (string commandline, out string[] argv)
 
216
                {
 
217
                        Exception ex;
 
218
                        
 
219
                        return TryParse (commandline, out argv, out ex);
 
220
                }
 
221
                
 
222
                public static string[] Parse (string commandline)
 
223
                {
 
224
                        string[] argv;
 
225
                        Exception ex;
 
226
                        
 
227
                        if (!TryParse (commandline, out argv, out ex))
 
228
                                throw ex;
 
229
                        
 
230
                        return argv;
 
231
                }
135
232
        }
136
233
}
137
234