wasCSharpSQLite – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * InterpCmd.java -- |
||
3 | * |
||
4 | * Implements the built-in "interp" Tcl command. |
||
5 | * |
||
6 | * Copyright (c) 2000 Christian Krone. |
||
7 | * |
||
8 | * See the file "license.terms" for information on usage and |
||
9 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
10 | * WARRANTIES. |
||
11 | * |
||
12 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
13 | * |
||
14 | * RCS @(#) $Id: InterpCmd.java,v 1.1 2000/08/20 06:08:43 mo Exp $ |
||
15 | * |
||
16 | */ |
||
17 | using System; |
||
18 | using System.Collections; |
||
19 | |||
20 | namespace tcl.lang |
||
21 | { |
||
22 | |||
23 | /// <summary> This class implements the built-in "interp" command in Tcl.</summary> |
||
24 | |||
25 | class InterpCmd : Command |
||
26 | { |
||
27 | |||
28 | private static readonly string[] options = new string[] { "alias", "aliases", "create", "delete", "eval", "exists", "expose", "hide", "hidden", "issafe", "invokehidden", "marktrusted", "slaves", "share", "target", "transfer" }; |
||
29 | private const int OPT_ALIAS = 0; |
||
30 | private const int OPT_ALIASES = 1; |
||
31 | private const int OPT_CREATE = 2; |
||
32 | private const int OPT_DELETE = 3; |
||
33 | private const int OPT_EVAL = 4; |
||
34 | private const int OPT_EXISTS = 5; |
||
35 | private const int OPT_EXPOSE = 6; |
||
36 | private const int OPT_HIDE = 7; |
||
37 | private const int OPT_HIDDEN = 8; |
||
38 | private const int OPT_ISSAFE = 9; |
||
39 | private const int OPT_INVOKEHIDDEN = 10; |
||
40 | private const int OPT_MARKTRUSTED = 11; |
||
41 | private const int OPT_SLAVES = 12; |
||
42 | private const int OPT_SHARE = 13; |
||
43 | private const int OPT_TARGET = 14; |
||
44 | private const int OPT_TRANSFER = 15; |
||
45 | |||
46 | private static readonly string[] createOptions = new string[] { "-safe", "--" }; |
||
47 | private const int OPT_CREATE_SAFE = 0; |
||
48 | private const int OPT_CREATE_LAST = 1; |
||
49 | |||
50 | private static readonly string[] hiddenOptions = new string[] { "-global", "--" }; |
||
51 | private const int OPT_HIDDEN_GLOBAL = 0; |
||
52 | private const int OPT_HIDDEN_LAST = 1; |
||
53 | |||
54 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] objv ) |
||
55 | { |
||
56 | if ( objv.Length < 2 ) |
||
57 | { |
||
58 | throw new TclNumArgsException( interp, 1, objv, "cmd ?arg ...?" ); |
||
59 | } |
||
60 | int cmd = TclIndex.get( interp, objv[1], options, "option", 0 ); |
||
61 | |||
62 | switch ( cmd ) |
||
63 | { |
||
64 | |||
65 | case OPT_ALIAS: |
||
66 | { |
||
67 | if ( objv.Length >= 4 ) |
||
68 | { |
||
69 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
70 | |||
71 | if ( objv.Length == 4 ) |
||
72 | { |
||
73 | InterpAliasCmd.describe( interp, slaveInterp, objv[3] ); |
||
74 | return TCL.CompletionCode.RETURN; |
||
75 | } |
||
76 | |||
77 | if ( ( objv.Length == 5 ) && ( "".Equals( objv[4].ToString() ) ) ) |
||
78 | { |
||
79 | InterpAliasCmd.delete( interp, slaveInterp, objv[3] ); |
||
80 | return TCL.CompletionCode.RETURN; |
||
81 | } |
||
82 | if ( objv.Length > 5 ) |
||
83 | { |
||
84 | Interp masterInterp = getInterp( interp, objv[4] ); |
||
85 | |||
86 | if ( "".Equals( objv[5].ToString() ) ) |
||
87 | { |
||
88 | if ( objv.Length == 6 ) |
||
89 | { |
||
90 | InterpAliasCmd.delete( interp, slaveInterp, objv[3] ); |
||
91 | return TCL.CompletionCode.RETURN; |
||
92 | } |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | InterpAliasCmd.create( interp, slaveInterp, masterInterp, objv[3], objv[5], 6, objv ); |
||
97 | return TCL.CompletionCode.RETURN; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | throw new TclNumArgsException( interp, 2, objv, "slavePath slaveCmd ?masterPath masterCmd? ?args ..?" ); |
||
102 | } |
||
103 | |||
104 | case OPT_ALIASES: |
||
105 | { |
||
106 | Interp slaveInterp = getInterp( interp, objv ); |
||
107 | InterpAliasCmd.list( interp, slaveInterp ); |
||
108 | break; |
||
109 | } |
||
110 | |||
111 | case OPT_CREATE: |
||
112 | { |
||
113 | |||
114 | // Weird historical rules: "-safe" is accepted at the end, too. |
||
115 | |||
116 | bool safe = interp.isSafe; |
||
117 | |||
118 | TclObject slaveNameObj = null; |
||
119 | bool last = false; |
||
120 | for ( int i = 2; i < objv.Length; i++ ) |
||
121 | { |
||
122 | |||
123 | if ( ( !last ) && ( objv[i].ToString()[0] == '-' ) ) |
||
124 | { |
||
125 | int index = TclIndex.get( interp, objv[i], createOptions, "option", 0 ); |
||
126 | if ( index == OPT_CREATE_SAFE ) |
||
127 | { |
||
128 | safe = true; |
||
129 | continue; |
||
130 | } |
||
131 | i++; |
||
132 | last = true; |
||
133 | } |
||
134 | if ( slaveNameObj != null ) |
||
135 | { |
||
136 | throw new TclNumArgsException( interp, 2, objv, "?-safe? ?--? ?path?" ); |
||
137 | } |
||
138 | slaveNameObj = objv[i]; |
||
139 | } |
||
140 | if ( slaveNameObj == null ) |
||
141 | { |
||
142 | |||
143 | // Create an anonymous interpreter -- we choose its name and |
||
144 | // the name of the command. We check that the command name |
||
145 | // that we use for the interpreter does not collide with an |
||
146 | // existing command in the master interpreter. |
||
147 | |||
148 | int i = 0; |
||
149 | while ( interp.getCommand( "interp" + i ) != null ) |
||
150 | { |
||
151 | i++; |
||
152 | } |
||
153 | slaveNameObj = TclString.newInstance( "interp" + i ); |
||
154 | } |
||
155 | InterpSlaveCmd.create( interp, slaveNameObj, safe ); |
||
156 | interp.setResult( slaveNameObj ); |
||
157 | break; |
||
158 | } |
||
159 | |||
160 | case OPT_DELETE: |
||
161 | { |
||
162 | for ( int i = 2; i < objv.Length; i++ ) |
||
163 | { |
||
164 | Interp slaveInterp = getInterp( interp, objv[i] ); |
||
165 | |||
166 | if ( slaveInterp == interp ) |
||
167 | { |
||
168 | throw new TclException( interp, "cannot delete the current interpreter" ); |
||
169 | } |
||
170 | InterpSlaveCmd slave = slaveInterp.slave; |
||
171 | slave.masterInterp.deleteCommandFromToken( slave.interpCmd ); |
||
172 | } |
||
173 | break; |
||
174 | } |
||
175 | |||
176 | case OPT_EVAL: |
||
177 | { |
||
178 | if ( objv.Length < 4 ) |
||
179 | { |
||
180 | throw new TclNumArgsException( interp, 2, objv, "path arg ?arg ...?" ); |
||
181 | } |
||
182 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
183 | InterpSlaveCmd.eval( interp, slaveInterp, 3, objv ); |
||
184 | break; |
||
185 | } |
||
186 | |||
187 | case OPT_EXISTS: |
||
188 | { |
||
189 | bool exists = true; |
||
190 | |||
191 | try |
||
192 | { |
||
193 | getInterp( interp, objv ); |
||
194 | } |
||
195 | catch ( TclException e ) |
||
196 | { |
||
197 | if ( objv.Length > 3 ) |
||
198 | { |
||
199 | throw; |
||
200 | } |
||
201 | exists = false; |
||
202 | } |
||
203 | interp.setResult( exists ); |
||
204 | break; |
||
205 | } |
||
206 | |||
207 | case OPT_EXPOSE: |
||
208 | { |
||
209 | if ( objv.Length < 4 || objv.Length > 5 ) |
||
210 | { |
||
211 | throw new TclNumArgsException( interp, 2, objv, "path hiddenCmdName ?cmdName?" ); |
||
212 | } |
||
213 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
214 | InterpSlaveCmd.expose( interp, slaveInterp, 3, objv ); |
||
215 | break; |
||
216 | } |
||
217 | |||
218 | case OPT_HIDE: |
||
219 | { |
||
220 | if ( objv.Length < 4 || objv.Length > 5 ) |
||
221 | { |
||
222 | throw new TclNumArgsException( interp, 2, objv, "path cmdName ?hiddenCmdName?" ); |
||
223 | } |
||
224 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
225 | InterpSlaveCmd.hide( interp, slaveInterp, 3, objv ); |
||
226 | break; |
||
227 | } |
||
228 | |||
229 | case OPT_HIDDEN: |
||
230 | { |
||
231 | Interp slaveInterp = getInterp( interp, objv ); |
||
232 | InterpSlaveCmd.hidden( interp, slaveInterp ); |
||
233 | break; |
||
234 | } |
||
235 | |||
236 | case OPT_ISSAFE: |
||
237 | { |
||
238 | Interp slaveInterp = getInterp( interp, objv ); |
||
239 | interp.setResult( slaveInterp.isSafe ); |
||
240 | break; |
||
241 | } |
||
242 | |||
243 | case OPT_INVOKEHIDDEN: |
||
244 | { |
||
245 | bool global = false; |
||
246 | int i; |
||
247 | for ( i = 3; i < objv.Length; i++ ) |
||
248 | { |
||
249 | |||
250 | if ( objv[i].ToString()[0] != '-' ) |
||
251 | { |
||
252 | break; |
||
253 | } |
||
254 | int index = TclIndex.get( interp, objv[i], hiddenOptions, "option", 0 ); |
||
255 | if ( index == OPT_HIDDEN_GLOBAL ) |
||
256 | { |
||
257 | global = true; |
||
258 | } |
||
259 | else |
||
260 | { |
||
261 | i++; |
||
262 | break; |
||
263 | } |
||
264 | } |
||
265 | if ( objv.Length - i < 1 ) |
||
266 | { |
||
267 | throw new TclNumArgsException( interp, 2, objv, "path ?-global? ?--? cmd ?arg ..?" ); |
||
268 | } |
||
269 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
270 | InterpSlaveCmd.invokeHidden( interp, slaveInterp, global, i, objv ); |
||
271 | break; |
||
272 | } |
||
273 | |||
274 | case OPT_MARKTRUSTED: |
||
275 | { |
||
276 | if ( objv.Length != 3 ) |
||
277 | { |
||
278 | throw new TclNumArgsException( interp, 2, objv, "path" ); |
||
279 | } |
||
280 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
281 | InterpSlaveCmd.markTrusted( interp, slaveInterp ); |
||
282 | break; |
||
283 | } |
||
284 | |||
285 | case OPT_SLAVES: |
||
286 | { |
||
287 | Interp slaveInterp = getInterp( interp, objv ); |
||
288 | |||
289 | TclObject result = TclList.newInstance(); |
||
290 | interp.setResult( result ); |
||
291 | |||
292 | IEnumerator keys = slaveInterp.slaveTable.Keys.GetEnumerator(); |
||
293 | while ( keys.MoveNext() ) |
||
294 | { |
||
295 | string inString = (string)keys.Current; |
||
296 | TclList.append( interp, result, TclString.newInstance( inString ) ); |
||
297 | } |
||
298 | |||
299 | break; |
||
300 | } |
||
301 | |||
302 | case OPT_SHARE: |
||
303 | { |
||
304 | if ( objv.Length != 5 ) |
||
305 | { |
||
306 | throw new TclNumArgsException( interp, 2, objv, "srcPath channelId destPath" ); |
||
307 | } |
||
308 | Interp masterInterp = getInterp( interp, objv[2] ); |
||
309 | |||
310 | |||
311 | Channel chan = TclIO.getChannel( masterInterp, objv[3].ToString() ); |
||
312 | if ( chan == null ) |
||
313 | { |
||
314 | |||
315 | throw new TclException( interp, "can not find channel named \"" + objv[3].ToString() + "\"" ); |
||
316 | } |
||
317 | |||
318 | Interp slaveInterp = getInterp( interp, objv[4] ); |
||
319 | TclIO.registerChannel( slaveInterp, chan ); |
||
320 | break; |
||
321 | } |
||
322 | |||
323 | case OPT_TARGET: |
||
324 | { |
||
325 | if ( objv.Length != 4 ) |
||
326 | { |
||
327 | throw new TclNumArgsException( interp, 2, objv, "path alias" ); |
||
328 | } |
||
329 | |||
330 | Interp slaveInterp = getInterp( interp, objv[2] ); |
||
331 | |||
332 | string aliasName = objv[3].ToString(); |
||
333 | Interp targetInterp = InterpAliasCmd.getTargetInterp( slaveInterp, aliasName ); |
||
334 | if ( targetInterp == null ) |
||
335 | { |
||
336 | |||
337 | throw new TclException( interp, "alias \"" + aliasName + "\" in path \"" + objv[2].ToString() + "\" not found" ); |
||
338 | } |
||
339 | if ( !getInterpPath( interp, targetInterp ) ) |
||
340 | { |
||
341 | |||
342 | throw new TclException( interp, "target interpreter for alias \"" + aliasName + "\" in path \"" + objv[2].ToString() + "\" is not my descendant" ); |
||
343 | } |
||
344 | break; |
||
345 | } |
||
346 | |||
347 | case OPT_TRANSFER: |
||
348 | { |
||
349 | if ( objv.Length != 5 ) |
||
350 | { |
||
351 | throw new TclNumArgsException( interp, 2, objv, "srcPath channelId destPath" ); |
||
352 | } |
||
353 | Interp masterInterp = getInterp( interp, objv[2] ); |
||
354 | |||
355 | |||
356 | Channel chan = TclIO.getChannel( masterInterp, objv[3].ToString() ); |
||
357 | if ( chan == null ) |
||
358 | { |
||
359 | |||
360 | throw new TclException( interp, "can not find channel named \"" + objv[3].ToString() + "\"" ); |
||
361 | } |
||
362 | |||
363 | Interp slaveInterp = getInterp( interp, objv[4] ); |
||
364 | TclIO.registerChannel( slaveInterp, chan ); |
||
365 | TclIO.unregisterChannel( masterInterp, chan ); |
||
366 | break; |
||
367 | } |
||
368 | } |
||
369 | return TCL.CompletionCode.RETURN; |
||
370 | } |
||
371 | private static Interp getInterp( Interp interp, TclObject[] objv ) |
||
372 | { |
||
373 | if ( objv.Length == 2 ) |
||
374 | { |
||
375 | return interp; |
||
376 | } |
||
377 | else if ( objv.Length == 3 ) |
||
378 | { |
||
379 | return getInterp( interp, objv[2] ); |
||
380 | } |
||
381 | else |
||
382 | { |
||
383 | throw new TclNumArgsException( interp, 2, objv, "?path?" ); |
||
384 | } |
||
385 | } |
||
386 | private static bool getInterpPath( Interp askingInterp, Interp targetInterp ) |
||
387 | { |
||
388 | if ( targetInterp == askingInterp ) |
||
389 | { |
||
390 | return true; |
||
391 | } |
||
392 | if ( targetInterp == null || targetInterp.slave == null ) |
||
393 | { |
||
394 | return false; |
||
395 | } |
||
396 | |||
397 | if ( !getInterpPath( askingInterp, targetInterp.slave.masterInterp ) ) |
||
398 | { |
||
399 | return false; |
||
400 | } |
||
401 | askingInterp.appendElement( targetInterp.slave.path ); |
||
402 | return true; |
||
403 | } |
||
404 | internal static Interp getInterp( Interp interp, TclObject path ) |
||
405 | { |
||
406 | TclObject[] objv = TclList.getElements( interp, path ); |
||
407 | Interp searchInterp = interp; //Interim storage for interp. to find. |
||
408 | |||
409 | for ( int i = 0; i < objv.Length; i++ ) |
||
410 | { |
||
411 | |||
412 | string name = objv[i].ToString(); |
||
413 | if ( !searchInterp.slaveTable.ContainsKey( name ) ) |
||
414 | { |
||
415 | searchInterp = null; |
||
416 | break; |
||
417 | } |
||
418 | InterpSlaveCmd slave = (InterpSlaveCmd)searchInterp.slaveTable[name]; |
||
419 | searchInterp = slave.slaveInterp; |
||
420 | if ( searchInterp == null ) |
||
421 | { |
||
422 | break; |
||
423 | } |
||
424 | } |
||
425 | |||
426 | if ( searchInterp == null ) |
||
427 | { |
||
428 | |||
429 | throw new TclException( interp, "could not find interpreter \"" + path.ToString() + "\"" ); |
||
430 | } |
||
431 | |||
432 | return searchInterp; |
||
433 | } |
||
434 | } // end InterpCmd |
||
435 | } |