editor.mod 1.6 KB

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