editor.mod 1.9 KB

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