WMInterpreterShell.Mod 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. MODULE WMInterpreterShell; (** AUTHOR "staubesv"; PURPOSE "GUI for shell"; *)
  2. (**
  3. * Usage:
  4. *
  5. * WMShell.Open ~ opens new shell
  6. * SystemTools.Free WMShell ~ closes all open shells
  7. *
  8. * History:
  9. *
  10. * 25.06.2007 First release (staubesv)
  11. *
  12. * TODO:
  13. * - nice shutdown when freeing module
  14. *)
  15. IMPORT
  16. Shell := InterpreterShell, Streams, Pipes, Texts, TextUtilities, Strings,
  17. Modules, Kernel, Inputs,
  18. WMGraphics, WMWindowManager, WMMessages, WMRestorable,
  19. WMComponents, WMDocumentEditor;
  20. CONST
  21. (* Default size of window at start up *)
  22. DefaultWidth = 640; DefaultHeight = 300;
  23. ReceiveBufferSize = 256;
  24. Prompt = "";
  25. Backspace = 08X;
  26. ESC = 1BX;
  27. DEL = 7FX;
  28. TYPE
  29. ShellComponent = OBJECT(WMComponents.VisualComponent)
  30. VAR
  31. out : Streams.Writer;
  32. in : Streams.Reader;
  33. pipeOut, pipeIn : Pipes.Pipe;
  34. (* Terminal window text writer *)
  35. w: TextUtilities.TextWriter;
  36. r: Texts.TextReader;
  37. text : Texts.Text;
  38. shell : Shell.Shell;
  39. editor : WMDocumentEditor.Editor;
  40. running, dead : BOOLEAN;
  41. timer : Kernel.Timer;
  42. begPos: LONGINT;
  43. selectedAll: BOOLEAN;
  44. buf: POINTER TO ARRAY OF CHAR;
  45. PROCEDURE Clear;
  46. BEGIN
  47. editor.Clear;
  48. NewLine(Prompt);
  49. Invalidate;
  50. END Clear;
  51. PROCEDURE ExtPointerUp(x, y : LONGINT; keys : SET; VAR handled : BOOLEAN);
  52. BEGIN
  53. text.AcquireRead;
  54. editor.editor.tv.cursor.SetPosition(text.GetLength());
  55. text.ReleaseRead;
  56. END ExtPointerUp;
  57. PROCEDURE ExtKeyPressed(ucs : LONGINT; flags : SET; VAR keySym : LONGINT; VAR handled : BOOLEAN);
  58. VAR
  59. i, len, n, u: LONGINT;
  60. BEGIN
  61. handled := FALSE;
  62. IF editor.HandleShortcut(ucs, flags, keySym) THEN handled := TRUE; END;
  63. selectedAll := FALSE;
  64. IF ~handled & ~(Inputs.Release IN flags) THEN
  65. handled := TRUE;
  66. IF keySym = 01H THEN (* Ctrl-A *)
  67. text.AcquireRead;
  68. IF editor.editor.tv.cursor.GetPosition() > begPos THEN
  69. editor.editor.tv.selection.SetFromTo(begPos,text.GetLength());
  70. Texts.SetLastSelection(text,editor.editor.tv.selection.from,editor.editor.tv.selection.to);
  71. END;
  72. text.ReleaseRead;
  73. selectedAll := TRUE;
  74. (*ELSIF keySym = 03H THEN (* Ctrl-C *)
  75. editor.editor.tv.CopySelection
  76. ELSIF keySym = 16H THEN (* Ctrl-V *)
  77. CopyFromClipboard;
  78. ELSIF (keySym = 0FF63H) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Insert *)
  79. editor.editor.tv.CopySelection*)
  80. ELSIF keySym = 0FF56H THEN (* Page Down *)
  81. editor.editor.tv.PageDown(flags * Inputs.Shift # {})
  82. ELSIF keySym = 0FF55H THEN (* Page Up *)
  83. editor.editor.tv.PageUp(flags * Inputs.Shift # {})
  84. ELSIF keySym = 0FF50H THEN (* Cursor Home *)
  85. editor.editor.tv.Home(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  86. ELSIF keySym = 0FF57H THEN (* Cursor End *)
  87. editor.editor.tv.End(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  88. ELSIF (keySym = Inputs.KsBackSpace) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Backspace *)
  89. Clear;
  90. ELSIF (keySym = Inputs.KsReturn) & (flags*Inputs.Shift = {}) THEN (* ENTER *)
  91. text.AcquireRead;
  92. len := text.GetLength()-begPos;
  93. IF len > 0 THEN
  94. IF len >= LEN(buf) THEN NEW(buf,len+(len DIV 4)); END;
  95. NEW(r,text);
  96. r.SetPosition(begPos);
  97. n := 0;
  98. FOR i := 0 TO len-1 DO
  99. r.ReadCh(u);
  100. IF (u >= 32) & (u <= 126) THEN (* take only characters *)
  101. buf[n] := CHR(u); INC(n);
  102. END;
  103. buf[n] := 0X;
  104. END;
  105. END;
  106. text.ReleaseRead;
  107. IF len > 0 THEN
  108. out.String(buf^);
  109. out.Char(ESC); out.Char(0DX); out.Update;
  110. begPos := begPos + len;
  111. END;
  112. editor.editor.KeyPressed(ucs,flags,keySym,handled);
  113. ELSIF ((keySym = Inputs.KsLeft) OR (keySym = Inputs.KsUp) OR (keySym = Inputs.KsDown)) & (editor.editor.tv.cursor.GetPosition() = begPos) THEN
  114. ELSIF (keySym = Inputs.KsBackSpace) & (flags = {}) THEN
  115. IF editor.editor.tv.cursor.GetPosition() # begPos THEN
  116. handled := FALSE;
  117. END;
  118. ELSE
  119. handled := FALSE;
  120. END
  121. END;
  122. IF ~handled & (ucs > 0) & (ucs < 256) THEN
  123. editor.editor.KeyPressed(ucs,flags,keySym,handled);
  124. END;
  125. END ExtKeyPressed;
  126. PROCEDURE Wait(ms : LONGINT);
  127. BEGIN
  128. timer.Sleep(ms);
  129. END Wait;
  130. PROCEDURE InitShell;
  131. VAR shellIn : Streams.Reader; shellOut : Streams.Writer;
  132. BEGIN
  133. NEW(pipeOut, 256); NEW(pipeIn, 256);
  134. (* wire pipes *)
  135. NEW(shellIn, pipeOut.Receive, 256);
  136. NEW(shellOut, pipeIn.Send, 256);
  137. NEW(out, pipeOut.Send, 256);
  138. NEW(in, pipeIn.Receive, 256);
  139. NEW(shell, shellIn,shellOut, shellOut, FALSE, "");
  140. END InitShell;
  141. PROCEDURE CopyFromClipboard;
  142. VAR string : POINTER TO ARRAY OF CHAR;
  143. BEGIN
  144. Texts.clipboard.AcquireRead;
  145. IF Texts.clipboard.GetLength() > 0 THEN
  146. NEW(string, Texts.clipboard.GetLength()+1);
  147. TextUtilities.TextToStr(Texts.clipboard, string^);
  148. END;
  149. Texts.clipboard.ReleaseRead;
  150. out.String(string^); out.Update;
  151. TRACE(string^);
  152. END CopyFromClipboard;
  153. PROCEDURE Finalize;
  154. BEGIN
  155. (* pipeIn.Close; pipeOut.Close; *)
  156. shell.Exit;
  157. BEGIN {EXCLUSIVE}
  158. running := FALSE;
  159. AWAIT(dead);
  160. END;
  161. END Finalize;
  162. PROCEDURE DeleteNCharacters(nbrOfCharacters : LONGINT);
  163. VAR pos : LONGINT;
  164. BEGIN
  165. text.AcquireWrite;
  166. pos := editor.editor.tv.cursor.GetPosition();
  167. text.Delete(pos - nbrOfCharacters, nbrOfCharacters);
  168. text.ReleaseWrite;
  169. END DeleteNCharacters;
  170. PROCEDURE NewLine(CONST prompt: ARRAY OF CHAR);
  171. BEGIN
  172. w.Ln; w.String(prompt); w.Update;
  173. text.AcquireRead;
  174. editor.editor.tv.cursor.SetPosition(text.GetLength());
  175. begPos := editor.editor.tv.cursor.GetPosition();
  176. text.ReleaseRead;
  177. END NewLine;
  178. PROCEDURE ReceiveCharacters;
  179. VAR ch : CHAR; buffer : ARRAY ReceiveBufferSize OF CHAR; backspaces, i, size, len : LONGINT;
  180. BEGIN
  181. (* Receive at least one character *)
  182. size := in.Available();
  183. IF size > ReceiveBufferSize THEN size := ReceiveBufferSize; END;
  184. in.Bytes(buffer, 0, size, len);
  185. IF in.res = Streams.Ok THEN
  186. FOR i := 0 TO len-1 DO
  187. ch := buffer[i];
  188. IF (ch = DEL) OR (ch = Backspace) THEN
  189. INC(backspaces);
  190. ELSE
  191. IF (backspaces > 0) THEN
  192. w.Update;
  193. DeleteNCharacters(backspaces);
  194. backspaces := 0;
  195. END;
  196. w.Char(ch);
  197. END;
  198. END;
  199. w.Update;
  200. NewLine(Prompt);
  201. END;
  202. DeleteNCharacters(backspaces);
  203. END ReceiveCharacters;
  204. PROCEDURE &Init*;
  205. BEGIN
  206. Init^;
  207. running := TRUE;
  208. NEW(timer);
  209. NEW(editor); editor.alignment.Set(WMComponents.AlignClient);
  210. editor.SetToolbar(WMDocumentEditor.StoreButton + WMDocumentEditor.WrapButton + WMDocumentEditor.SearchButton + WMDocumentEditor.ClearButton);
  211. editor.editor.tv.SetExtKeyEventHandler(ExtKeyPressed);
  212. editor.editor.tv.SetExtPointerUpHandler(ExtPointerUp);
  213. AddContent(editor);
  214. NEW(text);
  215. NEW(w, text); w.SetFontName("Courier");
  216. editor.SetText(text);
  217. InitShell;
  218. SetNameAsString(StrShellComponent);
  219. NEW(buf,65536);
  220. END Init;
  221. BEGIN {ACTIVE}
  222. WHILE running DO
  223. IF running & (in.Available() > 0) THEN ReceiveCharacters; END;
  224. Wait(2);
  225. END;
  226. BEGIN {EXCLUSIVE} dead := TRUE; END;
  227. END ShellComponent;
  228. TYPE
  229. KillerMsg = OBJECT
  230. END KillerMsg;
  231. Window* = OBJECT (WMComponents.FormWindow)
  232. VAR
  233. shell : ShellComponent;
  234. PROCEDURE HandleUpcall(command : LONGINT);
  235. BEGIN
  236. IF command = Shell.Clear THEN
  237. shell.Clear;
  238. ELSIF command = Shell.ExitShell THEN
  239. Close;
  240. END;
  241. END HandleUpcall;
  242. PROCEDURE &New*(c : WMRestorable.Context);
  243. BEGIN
  244. IncCount;
  245. NEW(shell); shell.alignment.Set(WMComponents.AlignClient);
  246. shell.shell.SetUpcall(HandleUpcall);
  247. Init(DefaultWidth, DefaultHeight, FALSE);
  248. SetContent(shell);
  249. SetTitle(Strings.NewString("BlueShell"));
  250. SetIcon(WMGraphics.LoadImage("WMIcons.tar://WMShell.png", TRUE));
  251. IF c # NIL THEN
  252. WMRestorable.AddByContext(SELF, c);
  253. Resized(GetWidth(), GetHeight());
  254. ELSE
  255. WMWindowManager.DefaultAddWindow(SELF);
  256. END;
  257. shell.editor.editor.SetFocus();
  258. END New;
  259. PROCEDURE Close;
  260. BEGIN
  261. Close^;
  262. DecCount
  263. END Close;
  264. PROCEDURE Handle(VAR x : WMMessages.Message);
  265. BEGIN
  266. IF (x.msgType = WMMessages.MsgExt) & (x.ext # NIL) THEN
  267. IF (x.ext IS KillerMsg) THEN
  268. Close;
  269. ELSIF (x.ext IS WMRestorable.Storage) THEN
  270. x.ext(WMRestorable.Storage).Add("Shell", "WMShell.Restore", SELF, NIL);
  271. ELSE
  272. Handle^(x);
  273. END;
  274. ELSE Handle^(x)
  275. END
  276. END Handle;
  277. END Window;
  278. VAR
  279. nofWindows : LONGINT;
  280. StrShellComponent : Strings.String;
  281. PROCEDURE InitStrings;
  282. BEGIN
  283. StrShellComponent := Strings.NewString("ShellComponent");
  284. END InitStrings;
  285. PROCEDURE Restore*(context : WMRestorable.Context);
  286. VAR window : Window;
  287. BEGIN
  288. ASSERT(context # NIL);
  289. NEW(window, context);
  290. END Restore;
  291. PROCEDURE Open*;
  292. VAR window : Window;
  293. BEGIN
  294. NEW(window, NIL);
  295. END Open;
  296. PROCEDURE IncCount;
  297. BEGIN {EXCLUSIVE}
  298. INC(nofWindows)
  299. END IncCount;
  300. PROCEDURE DecCount;
  301. BEGIN {EXCLUSIVE}
  302. DEC(nofWindows)
  303. END DecCount;
  304. PROCEDURE Cleanup;
  305. VAR die : KillerMsg;
  306. msg : WMMessages.Message;
  307. m : WMWindowManager.WindowManager;
  308. BEGIN {EXCLUSIVE}
  309. NEW(die);
  310. msg.ext := die;
  311. msg.msgType := WMMessages.MsgExt;
  312. m := WMWindowManager.GetDefaultManager();
  313. m.Broadcast(msg);
  314. AWAIT(nofWindows = 0)
  315. END Cleanup;
  316. BEGIN
  317. Modules.InstallTermHandler(Cleanup);
  318. InitStrings;
  319. END WMInterpreterShell.
  320. WMInterpreterShell.Open ~
  321. SystemTools.Free WMInterpreterShell ~
  322. SystemTools.Free WMInterpreterShell InterpreterShell ~
  323. FOR i := 0 TO 100 DO
  324. CMD "SystemTools.Show ?{i}?"
  325. END;