editor.mod 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. MODULE editor;
  2. (* based on the kilo editor building course*)
  3. (* Step 18 *)
  4. IMPORT IO, termios, FIO, libc, CharClass, NumberIO, ASCII ;
  5. IMPORT STextIO, Strings;
  6. VAR
  7. c : CHAR;
  8. theTermios : termios.TERMIOS;
  9. TCSAFLUSH : INTEGER;
  10. i : CARDINAL;
  11. PROCEDURE die (s : ARRAY OF CHAR);
  12. BEGIN
  13. libc.perror(s);
  14. FOR i := 0 TO Strings.Length(s) DO
  15. IO.Write(s[i])
  16. END;
  17. HALT
  18. END die;
  19. PROCEDURE disablerawMode() : INTEGER;
  20. VAR
  21. result : INTEGER;
  22. BEGIN
  23. IO.BufferedMode(0,TRUE);
  24. IO.BufferedMode(1,TRUE);
  25. result := termios.tcsetattr(FIO.StdIn, TCSAFLUSH, theTermios);
  26. IF result = -1 THEN
  27. theTermios := termios.KillTermios(theTermios);
  28. die("tcsetattr")
  29. ELSE
  30. RETURN result
  31. END;
  32. END disablerawMode;
  33. PROCEDURE enablerawMode;
  34. BEGIN
  35. libc.atexit(disablerawMode);
  36. theTermios := termios.InitTermios();
  37. IF termios.tcgetattr(FIO.StdIn, theTermios) = -1 THEN
  38. die("tcgetattr")
  39. END;
  40. IO.UnBufferedMode(0,TRUE);
  41. IO.UnBufferedMode(1,TRUE);
  42. END enablerawMode;
  43. BEGIN
  44. TCSAFLUSH := termios.tcsflush ();
  45. enablerawMode;
  46. LOOP
  47. IO.Read(c);
  48. IF c = "q" THEN
  49. EXIT
  50. ELSIF CharClass.IsControl(c) THEN
  51. NumberIO.WriteCard(ORD(c),5);
  52. IO.Write(CHR(10));
  53. IO.Write(CHR(13));
  54. ELSE
  55. NumberIO.WriteCard(ORD(c),5);
  56. IO.Write(" ");
  57. IO.Write("(");
  58. IO.Write("'");
  59. IO.Write(c);
  60. IO.Write(" ");
  61. IO.Write("'");
  62. IO.Write(")");
  63. IO.Write(CHR(10));
  64. IO.Write(CHR(13));
  65. END;
  66. END;
  67. END editor.