editor.mod 2.1 KB

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