editor.mod 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. MODULE editor;
  2. (* based on the kilo editor building course*)
  3. (* Step 11 *)
  4. IMPORT FIO, SYSTEM, termios, STextIO, NumberIO;
  5. IMPORT CharClass, STextIO, libc;
  6. VAR
  7. c : CHAR;
  8. p : POINTER TO CHAR;
  9. thetermios : termios.TERMIOS;
  10. raw : termios.TERMIOS;
  11. result : INTEGER;
  12. PROCEDURE disableRawMode(): INTEGER;
  13. VAR
  14. TCSAFLUSH : INTEGER;
  15. result : INTEGER;
  16. BEGIN
  17. TCSAFLUSH := termios.tcsflush ();
  18. result := termios.tcsetattr(FIO.StdIn, TCSAFLUSH, thetermios);
  19. RETURN result;
  20. END disableRawMode;
  21. PROCEDURE enableRawMode;
  22. VAR
  23. TCSAFLUSH : INTEGER;
  24. result : INTEGER;
  25. bresult : BOOLEAN;
  26. BEGIN
  27. TCSAFLUSH := termios.tcsflush ();
  28. libc.atexit(disableRawMode);
  29. bresult := termios.SetFlag(raw,termios.ixon, FALSE);
  30. bresult := termios.SetFlag(raw,termios.lecho, FALSE);
  31. bresult := termios.SetFlag(raw,termios.licanon, FALSE);
  32. bresult := termios.SetFlag(raw,termios.liexten, FALSE);
  33. bresult := termios.SetFlag(raw,termios.lisig, FALSE);
  34. result := termios.tcsetattr(FIO.StdIn, TCSAFLUSH, raw);
  35. END enableRawMode;
  36. BEGIN
  37. thetermios := termios.InitTermios();
  38. raw := termios.InitTermios();
  39. result := termios.tcgetattr(FIO.StdIn, thetermios);
  40. result := termios.tcgetattr(FIO.StdIn, raw);
  41. enableRawMode;
  42. p := SYSTEM.ADR(c);
  43. WHILE (FIO.ReadNBytes(FIO.StdIn,1,p) = 1) AND (c <> "q") DO
  44. IF CharClass.IsControl(c) THEN
  45. NumberIO.WriteCard(ORD(c),5);
  46. STextIO.WriteLn;
  47. ELSE
  48. NumberIO.WriteCard(ORD(c),5);
  49. STextIO.WriteString(" ('");
  50. STextIO.WriteChar(c);
  51. STextIO.WriteString(" ')");
  52. STextIO.WriteLn;
  53. END;
  54. END;
  55. END editor.