editor.mod 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. MODULE editor;
  2. (* based on the kilo editor building course*)
  3. (* Step 13 *)
  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.icrnl, FALSE);
  30. bresult := termios.SetFlag(raw,termios.ixon, FALSE);
  31. bresult := termios.SetFlag(raw,termios.lecho, FALSE);
  32. bresult := termios.SetFlag(raw,termios.opost, FALSE);
  33. bresult := termios.SetFlag(raw,termios.licanon, FALSE);
  34. bresult := termios.SetFlag(raw,termios.liexten, FALSE);
  35. bresult := termios.SetFlag(raw,termios.lisig, FALSE);
  36. result := termios.tcsetattr(FIO.StdIn, TCSAFLUSH, raw);
  37. END enableRawMode;
  38. BEGIN
  39. thetermios := termios.InitTermios();
  40. raw := termios.InitTermios();
  41. result := termios.tcgetattr(FIO.StdIn, thetermios);
  42. result := termios.tcgetattr(FIO.StdIn, raw);
  43. enableRawMode;
  44. p := SYSTEM.ADR(c);
  45. WHILE (FIO.ReadNBytes(FIO.StdIn,1,p) = 1) AND (c <> "q") DO
  46. IF ORD(c) > 32 THEN
  47. NumberIO.WriteCard(ORD(c),5);
  48. STextIO.WriteString(" ('");
  49. STextIO.WriteChar(c);
  50. STextIO.WriteString(" ')");
  51. STextIO.WriteLn;
  52. ELSE
  53. NumberIO.WriteCard(ORD(c),5);
  54. STextIO.WriteLn;
  55. END;
  56. END;
  57. END editor.