editor.mod 1.6 KB

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