WMInterpreterShell.Mod 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 = "SHELL>";
  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. text : Texts.Text;
  37. shell : Shell.Shell;
  38. editor : WMDocumentEditor.Editor;
  39. running, dead : BOOLEAN;
  40. timer : Kernel.Timer;
  41. PROCEDURE Clear;
  42. BEGIN
  43. editor.Clear;
  44. Invalidate;
  45. END Clear;
  46. PROCEDURE ExtPointerUp(x, y : LONGINT; keys : SET; VAR handled : BOOLEAN);
  47. BEGIN
  48. text.AcquireRead;
  49. editor.editor.tv.cursor.SetPosition(text.GetLength());
  50. text.ReleaseRead;
  51. END ExtPointerUp;
  52. PROCEDURE ExtKeyPressed(ucs : LONGINT; flags : SET; VAR keySym : LONGINT; VAR handled : BOOLEAN);
  53. BEGIN
  54. handled := FALSE;
  55. IF editor.HandleShortcut(ucs, flags, keySym) THEN handled := TRUE; END;
  56. IF ~handled & ~(Inputs.Release IN flags) THEN
  57. handled := TRUE;
  58. IF keySym = 01H THEN (* Ctrl-A *)
  59. editor.editor.tv.SelectAll
  60. ELSIF keySym = 03H THEN (* Ctrl-C *)
  61. editor.editor.tv.CopySelection
  62. ELSIF keySym = 16H THEN (* Ctrl-V *)
  63. CopyFromClipboard;
  64. ELSIF (keySym = 0FF63H) & (flags * Inputs.Ctrl # {}) THEN (*Ctrl Insert *)
  65. editor.editor.tv.CopySelection
  66. ELSIF keySym = 0FF56H THEN (* Page Down *)
  67. editor.editor.tv.PageDown(flags * Inputs.Shift # {})
  68. ELSIF keySym = 0FF55H THEN (* Page Up *)
  69. editor.editor.tv.PageUp(flags * Inputs.Shift # {})
  70. ELSIF keySym = 0FF50H THEN (* Cursor Home *)
  71. editor.editor.tv.Home(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  72. ELSIF keySym = 0FF57H THEN (* Cursor End *)
  73. editor.editor.tv.End(flags * Inputs.Ctrl # {}, flags * Inputs.Shift # {})
  74. ELSIF keySym = 0FF1BH THEN (* Esc *)
  75. IF ~(Inputs.Release IN flags) THEN
  76. out.Char(ESC); out.Char(ESC); out.Update;
  77. END;
  78. ELSE
  79. handled := FALSE;
  80. END
  81. END;
  82. IF ~handled & (ucs > 0) & (ucs < 256) THEN
  83. IF ~(Inputs.Release IN flags) THEN
  84. IF ucs > 127 THEN (* Escape non-ascii characters *)
  85. out.Char(ESC); out.Char("["); ucs := ucs - 128;
  86. END;
  87. out.Char(CHR(ucs)); out.Update;
  88. END;
  89. handled := TRUE;
  90. END;
  91. END ExtKeyPressed;
  92. PROCEDURE Wait(ms : LONGINT);
  93. BEGIN
  94. timer.Sleep(ms);
  95. END Wait;
  96. PROCEDURE InitShell;
  97. VAR shellIn : Streams.Reader; shellOut : Streams.Writer;
  98. BEGIN
  99. NEW(pipeOut, 256); NEW(pipeIn, 256);
  100. (* wire pipes *)
  101. NEW(shellIn, pipeOut.Receive, 256);
  102. NEW(shellOut, pipeIn.Send, 256);
  103. NEW(out, pipeOut.Send, 256);
  104. NEW(in, pipeIn.Receive, 256);
  105. NEW(shell, shellIn,shellOut, shellOut, TRUE, Prompt);
  106. END InitShell;
  107. PROCEDURE CopyFromClipboard;
  108. VAR string : POINTER TO ARRAY OF CHAR;
  109. BEGIN
  110. Texts.clipboard.AcquireRead;
  111. IF Texts.clipboard.GetLength() > 0 THEN
  112. NEW(string, Texts.clipboard.GetLength()+1);
  113. TextUtilities.TextToStr(Texts.clipboard, string^);
  114. END;
  115. Texts.clipboard.ReleaseRead;
  116. out.String(string^); out.Update;
  117. END CopyFromClipboard;
  118. PROCEDURE Finalize;
  119. BEGIN
  120. (* pipeIn.Close; pipeOut.Close; *)
  121. shell.Exit;
  122. BEGIN {EXCLUSIVE}
  123. running := FALSE;
  124. AWAIT(dead);
  125. END;
  126. END Finalize;
  127. PROCEDURE DeleteNCharacters(nbrOfCharacters : LONGINT);
  128. VAR pos : LONGINT;
  129. BEGIN
  130. text.AcquireWrite;
  131. pos := editor.editor.tv.cursor.GetPosition();
  132. text.Delete(pos - nbrOfCharacters, nbrOfCharacters);
  133. text.ReleaseWrite;
  134. END DeleteNCharacters;
  135. PROCEDURE ReceiveCharacters;
  136. VAR ch : CHAR; buffer : ARRAY ReceiveBufferSize OF CHAR; backspaces, i, size, len : LONGINT;
  137. BEGIN
  138. (* Receive at least one character *)
  139. size := in.Available();
  140. IF size > ReceiveBufferSize THEN size := ReceiveBufferSize; END;
  141. in.Bytes(buffer, 0, size, len);
  142. IF in.res = Streams.Ok THEN
  143. FOR i := 0 TO len-1 DO
  144. ch := buffer[i];
  145. IF (ch = DEL) OR (ch = Backspace) THEN
  146. INC(backspaces);
  147. ELSE
  148. IF (backspaces > 0) THEN
  149. w.Update;
  150. DeleteNCharacters(backspaces);
  151. backspaces := 0;
  152. END;
  153. w.Char(ch);
  154. END;
  155. END;
  156. w.Update;
  157. END;
  158. DeleteNCharacters(backspaces);
  159. END ReceiveCharacters;
  160. PROCEDURE &Init*;
  161. BEGIN
  162. Init^;
  163. running := TRUE;
  164. NEW(timer);
  165. NEW(editor); editor.alignment.Set(WMComponents.AlignClient);
  166. editor.SetToolbar(WMDocumentEditor.StoreButton + WMDocumentEditor.WrapButton + WMDocumentEditor.SearchButton + WMDocumentEditor.ClearButton);
  167. editor.editor.tv.SetExtKeyEventHandler(ExtKeyPressed);
  168. editor.editor.tv.SetExtPointerUpHandler(ExtPointerUp);
  169. AddContent(editor);
  170. NEW(text);
  171. NEW(w, text); w.SetFontName("Courier");
  172. editor.SetText(text);
  173. InitShell;
  174. SetNameAsString(StrShellComponent);
  175. END Init;
  176. BEGIN {ACTIVE}
  177. WHILE running DO
  178. IF running & (in.Available() > 0) THEN ReceiveCharacters; END;
  179. Wait(2);
  180. END;
  181. BEGIN {EXCLUSIVE} dead := TRUE; END;
  182. END ShellComponent;
  183. TYPE
  184. KillerMsg = OBJECT
  185. END KillerMsg;
  186. Window* = OBJECT (WMComponents.FormWindow)
  187. VAR
  188. shell : ShellComponent;
  189. PROCEDURE HandleUpcall(command : LONGINT);
  190. BEGIN
  191. IF command = Shell.Clear THEN
  192. shell.Clear;
  193. ELSIF command = Shell.ExitShell THEN
  194. Close;
  195. END;
  196. END HandleUpcall;
  197. PROCEDURE &New*(c : WMRestorable.Context);
  198. BEGIN
  199. IncCount;
  200. NEW(shell); shell.alignment.Set(WMComponents.AlignClient);
  201. shell.shell.SetUpcall(HandleUpcall);
  202. Init(DefaultWidth, DefaultHeight, FALSE);
  203. SetContent(shell);
  204. SetTitle(Strings.NewString("BlueShell"));
  205. SetIcon(WMGraphics.LoadImage("WMIcons.tar://WMShell.png", TRUE));
  206. IF c # NIL THEN
  207. WMRestorable.AddByContext(SELF, c);
  208. Resized(GetWidth(), GetHeight());
  209. ELSE
  210. WMWindowManager.DefaultAddWindow(SELF);
  211. END;
  212. shell.editor.editor.SetFocus();
  213. END New;
  214. PROCEDURE Close;
  215. BEGIN
  216. Close^;
  217. DecCount
  218. END Close;
  219. PROCEDURE Handle(VAR x : WMMessages.Message);
  220. BEGIN
  221. IF (x.msgType = WMMessages.MsgExt) & (x.ext # NIL) THEN
  222. IF (x.ext IS KillerMsg) THEN
  223. Close;
  224. ELSIF (x.ext IS WMRestorable.Storage) THEN
  225. x.ext(WMRestorable.Storage).Add("Shell", "WMShell.Restore", SELF, NIL);
  226. ELSE
  227. Handle^(x);
  228. END;
  229. ELSE Handle^(x)
  230. END
  231. END Handle;
  232. END Window;
  233. VAR
  234. nofWindows : LONGINT;
  235. StrShellComponent : Strings.String;
  236. PROCEDURE InitStrings;
  237. BEGIN
  238. StrShellComponent := Strings.NewString("ShellComponent");
  239. END InitStrings;
  240. PROCEDURE Restore*(context : WMRestorable.Context);
  241. VAR window : Window;
  242. BEGIN
  243. ASSERT(context # NIL);
  244. NEW(window, context);
  245. END Restore;
  246. PROCEDURE Open*;
  247. VAR window : Window;
  248. BEGIN
  249. NEW(window, NIL);
  250. END Open;
  251. PROCEDURE IncCount;
  252. BEGIN {EXCLUSIVE}
  253. INC(nofWindows)
  254. END IncCount;
  255. PROCEDURE DecCount;
  256. BEGIN {EXCLUSIVE}
  257. DEC(nofWindows)
  258. END DecCount;
  259. PROCEDURE Cleanup;
  260. VAR die : KillerMsg;
  261. msg : WMMessages.Message;
  262. m : WMWindowManager.WindowManager;
  263. BEGIN {EXCLUSIVE}
  264. NEW(die);
  265. msg.ext := die;
  266. msg.msgType := WMMessages.MsgExt;
  267. m := WMWindowManager.GetDefaultManager();
  268. m.Broadcast(msg);
  269. AWAIT(nofWindows = 0)
  270. END Cleanup;
  271. BEGIN
  272. Modules.InstallTermHandler(Cleanup);
  273. InitStrings;
  274. END WMInterpreterShell.
  275. WMShell.Open ~
  276. SystemTools.Free WMShell ~
  277. SystemTools.Free WMShell Shell ~