1: ================================================================================
  2: 
  3: Smalltalk.KSU defineClass: #ShellInterpreter
  4:     superclass: #{Core.Object}
  5:     indexedType: #none
  6:     private: false
  7:     instanceVariableNames: 'commandString interface '
  8:     classInstanceVariableNames: ''
  9:     imports: ''
 10:     category: ''
 11: 
 12: ================================================================================
 13: 
 14: KSU.ShellInterpreter method for 'interface operation'
 15: 
 16: closeErrorPipe
 17: 
 18:     ^self interface closeErrorPipe
 19: 
 20: ------------------------------------------------------------
 21: 
 22: KSU.ShellInterpreter method for 'interface operation'
 23: 
 24: closeInputPipe
 25: 
 26:     ^self interface closeInputPipe
 27: 
 28: ------------------------------------------------------------
 29: 
 30: KSU.ShellInterpreter method for 'interface operation'
 31: 
 32: closeOutputPipe
 33: 
 34:     ^self interface closeOutputPipe
 35: 
 36: ------------------------------------------------------------
 37: 
 38: KSU.ShellInterpreter method for 'accessing'
 39: 
 40: commandString
 41: 
 42:     commandString ifNil: [commandString := String new].
 43:     ^commandString
 44: 
 45: ------------------------------------------------------------
 46: 
 47: KSU.ShellInterpreter method for 'accessing'
 48: 
 49: commandString: aString
 50: 
 51:     commandString := aString
 52: 
 53: ------------------------------------------------------------
 54: 
 55: KSU.ShellInterpreter method for 'text utilities'
 56: 
 57: convertCR2CRLF: inputString
 58: 
 59:     | inputStream outputStream |
 60:     inputStream := inputString readStream.
 61:     outputStream := String new writeStream.
 62:     [inputStream atEnd] whileFalse: 
 63:             [| aCharacter |
 64:             aCharacter := inputStream next.
 65:             outputStream nextPut: aCharacter.
 66:             aCharacter = Character cr ifTrue: [outputStream nextPut: Character lf]].
 67:     inputStream close.
 68:     outputStream close.
 69:     ^outputStream contents
 70: 
 71: ------------------------------------------------------------
 72: 
 73: KSU.ShellInterpreter method for 'text utilities'
 74: 
 75: convertCR2LF: inputString
 76: 
 77:     | outputString |
 78:     outputString := inputString
 79:                 collect: [:aCharacter | aCharacter = Character cr ifTrue: [Character lf] ifFalse: [aCharacter]].
 80:     ^outputString
 81: 
 82: ------------------------------------------------------------
 83: 
 84: KSU.ShellInterpreter method for 'text utilities'
 85: 
 86: convertCRLF2CR: inputString
 87: 
 88:     | inputStream outputStream |
 89:     inputStream := inputString readStream.
 90:     outputStream := String new writeStream.
 91:     [inputStream atEnd] whileFalse: 
 92:             [| aCharacter |
 93:             aCharacter := inputStream next.
 94:             aCharacter = Character lf ifFalse: [outputStream nextPut: aCharacter]].
 95:     inputStream close.
 96:     outputStream close.
 97:     ^outputStream contents
 98: 
 99: ------------------------------------------------------------
100: 
101: KSU.ShellInterpreter method for 'text utilities'
102: 
103: convertCRLF2LF: inputString
104: 
105:     | inputStream outputStream |
106:     inputStream := inputString readStream.
107:     outputStream := String new writeStream.
108:     [inputStream atEnd] whileFalse: 
109:             [| aCharacter |
110:             aCharacter := inputStream next.
111:             aCharacter = Character cr ifFalse: [outputStream nextPut: aCharacter]].
112:     inputStream close.
113:     outputStream close.
114:     ^outputStream contents
115: 
116: ------------------------------------------------------------
117: 
118: KSU.ShellInterpreter method for 'text utilities'
119: 
120: convertLF2CR: inputString
121: 
122:     | outputString |
123:     outputString := inputString
124:                 collect: [:aCharacter | aCharacter = Character lf ifTrue: [Character cr] ifFalse: [aCharacter]].
125:     ^outputString
126: 
127: ------------------------------------------------------------
128: 
129: KSU.ShellInterpreter method for 'text utilities'
130: 
131: convertLF2CRLF: inputString
132: 
133:     | inputStream outputStream |
134:     inputStream := inputString readStream.
135:     outputStream := String new writeStream.
136:     [inputStream atEnd] whileFalse: 
137:             [| aCharacter |
138:             aCharacter := inputStream next.
139:             aCharacter = Character lf ifTrue: [outputStream nextPut: Character cr].
140:             outputStream nextPut: aCharacter].
141:     inputStream close.
142:     outputStream close.
143:     ^outputStream contents
144: 
145: ------------------------------------------------------------
146: 
147: KSU.ShellInterpreter method for 'defaults'
148: 
149: defaultDelay
150: 
151:     ^Delay forMilliseconds: 10
152: 
153: ------------------------------------------------------------
154: 
155: KSU.ShellInterpreter method for 'evaluating'
156: 
157: evaluate: inputString
158: 
159:     ^self evaluate: inputString withDelay: self defaultDelay
160: 
161: ------------------------------------------------------------
162: 
163: KSU.ShellInterpreter method for 'evaluating'
164: 
165: evaluate: inputString withDelay: aDelay
166: 
167:     | aStream outputString |
168:     aStream := (String new writeStream)
169:                 nextPutAll: inputString;
170:                 nextPut: Character cr;
171:                 yourself.
172:     self input: aStream contents.
173:     aDelay wait.
174:     outputString := self output.
175:     ^outputString
176: 
177: ------------------------------------------------------------
178: 
179: KSU.ShellInterpreter method for 'evaluating'
180: 
181: evaluate: inputString withMilliseconds: milliseconds
182: 
183:     ^self evaluate: inputString withDelay: (Delay forMilliseconds: milliseconds)
184: 
185: ------------------------------------------------------------
186: 
187: KSU.ShellInterpreter method for 'initialize-release'
188: 
189: initialize
190: 
191:     commandString := nil.
192:     interface := nil
193: 
194: ------------------------------------------------------------
195: 
196: KSU.ShellInterpreter method for 'interface operation'
197: 
198: input: inputString
199: 
200:     self interface input: (self convertCR2LF: inputString)
201: 
202: ------------------------------------------------------------
203: 
204: KSU.ShellInterpreter method for 'accessing'
205: 
206: interface
207: 
208:     interface
209:         ifNil: 
210:             [| aString |
211:             aString := self commandString.
212:             aString ifNil: [aString := self class defaultShellCommand].
213:             interface := self class defaultInterfaceClass command: aString].
214:     ^interface
215: 
216: ------------------------------------------------------------
217: 
218: KSU.ShellInterpreter method for 'accessing'
219: 
220: interface: anInterface
221: 
222:     interface := anInterface
223: 
224: ------------------------------------------------------------
225: 
226: KSU.ShellInterpreter method for 'accessing'
227: 
228: isActive
229: 
230:     ^self interface isActive
231: 
232: ------------------------------------------------------------
233: 
234: KSU.ShellInterpreter method for 'interface operation'
235: 
236: output
237: 
238:     ^self convertLF2CR: self interface output
239: 
240: ------------------------------------------------------------
241: 
242: KSU.ShellInterpreter method for 'interface operation'
243: 
244: outputCompletely
245: 
246:     ^self convertLF2CR: self interface outputCompletely
247: 
248: ================================================================================
249: 
250: KSU.ShellInterpreter class
251:     instanceVariableNames: ''
252: 
253: ================================================================================
254: 
255: KSU.ShellInterpreter class method for 'instance creation'
256: 
257: command: commandString
258: 
259:     ^(super new)
260:         initialize;
261:         commandString: commandString;
262:         interface: (self defaultInterfaceClass command: commandString);
263:         yourself
264: 
265: ------------------------------------------------------------
266: 
267: KSU.ShellInterpreter class method for 'utilities'
268: 
269: command: commandString evaluate: inputString
270: 
271:     | anInterpreter outputString |
272:     anInterpreter := self command: commandString.
273:     anInterpreter input: inputString.
274:     anInterpreter closeInputPipe.
275:     outputString := anInterpreter outputCompletely.
276:     ^outputString
277: 
278: ------------------------------------------------------------
279: 
280: KSU.ShellInterpreter class method for 'defaults'
281: 
282: defaultInterfaceClass
283: 
284:     ^KSU.ShellInterface
285: 
286: ------------------------------------------------------------
287: 
288: KSU.ShellInterpreter class method for 'defaults'
289: 
290: defaultShellCommand
291: 
292:     ^'sh'
293: 
294: ------------------------------------------------------------
295: 
296: KSU.ShellInterpreter class method for 'examples'
297: 
298: example1
299:     "KSU.ShellInterpreter example1."
300: 
301:     ^KSU.ShellInterpreter shellScript: 'pwd'
302: 
303: ------------------------------------------------------------
304: 
305: KSU.ShellInterpreter class method for 'examples'
306: 
307: example2
308:     "KSU.ShellInterpreter example2."
309: 
310:     ^KSU.ShellInterpreter command: self defaultShellCommand
311: 
312: ------------------------------------------------------------
313: 
314: KSU.ShellInterpreter class method for 'examples'
315: 
316: example3
317:     "KSU.ShellInterpreter example3."
318: 
319:     ^KSU.ShellInterpreter command: 'bash' evaluate: 'pwd'
320: 
321: ------------------------------------------------------------
322: 
323: KSU.ShellInterpreter class method for 'examples'
324: 
325: example4
326:     "KSU.ShellInterpreter example4."
327: 
328:     ^KSU.ShellInterpreter shellScript: 'pwd;pwd;pwd'
329: 
330: ------------------------------------------------------------
331: 
332: KSU.ShellInterpreter class method for 'examples'
333: 
334: example5
335:     "KSU.ShellInterpreter example5."
336: 
337:     | aStream hundredOfPwd |
338:     aStream := String new writeStream.
339:     100 timesRepeat: [aStream nextPutAll: 'pwd;'].
340:     hundredOfPwd := aStream contents.
341:     ^KSU.ShellInterpreter shellScript: hundredOfPwd
342: 
343: ------------------------------------------------------------
344: 
345: KSU.ShellInterpreter class method for 'examples'
346: 
347: example6
348:     "KSU.ShellInterpreter example6."
349: 
350:     ^KSU.ShellInterpreter shellScript: 'sleep 5'
351: 
352: ------------------------------------------------------------
353: 
354: KSU.ShellInterpreter class method for 'instance creation'
355: 
356: new
357: 
358:     ^self command: self defaultShellCommand
359: 
360: ------------------------------------------------------------
361: 
362: KSU.ShellInterpreter class method for 'utilities'
363: 
364: shellScript: scriptString
365: 
366:     ^self command: self defaultShellCommand evaluate: scriptString
367: 
368: ================================================================================

This document was generated by KSU.TextDoclet on 2013/02/22 at 01:01:06.