editor1.mod 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. MODULE editor;
  2. (* based on the kilo editor building course*)
  3. (* Step 18 *)
  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. number : CARDINAL;
  13. PROCEDURE die (s : ARRAY OF CHAR);
  14. BEGIN
  15. libc.perror(s);
  16. HALT
  17. END die;
  18. PROCEDURE disableRawMode(): INTEGER;
  19. VAR
  20. TCSAFLUSH : INTEGER;
  21. result : INTEGER;
  22. BEGIN
  23. TCSAFLUSH := termios.tcsflush ();
  24. IF termios.tcsetattr(FIO.StdIn, TCSAFLUSH, thetermios) = -1 THEN
  25. die("tcsetattr")
  26. END;
  27. RETURN result;
  28. END disableRawMode;
  29. PROCEDURE enableRawMode;
  30. (*
  31. static void setControlLines(int port, int dtr, int rts)
  32. {
  33. struct termios tty;
  34. tcgetattr(port, &tty);
  35. cfmakeraw(&tty); // Sets raw mode, 8N1, etc.
  36. tty.c_cflag &= ~CRTSCTS; // Disable hardware flow control
  37. tty.c_cflag |= CLOCAL; // Ignore modem control lines
  38. tcsetattr(port, TCSANOW, &tty);
  39. int flags = TIOCM_DTR | TIOCM_RTS;
  40. ioctl(port, TIOCMBIS, &flags); // Set bits
  41. } *)
  42. (* termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  43. termios_p->c_oflag &= ~OPOST;
  44. termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  45. termios_p->c_cflag &= ~(CSIZE|PARENB);
  46. termios_p->c_cflag |= CS8; *)
  47. VAR
  48. TCSAFLUSH : INTEGER;
  49. result : INTEGER;
  50. bresult : BOOLEAN;
  51. BEGIN
  52. TCSAFLUSH := termios.tcsflush ();
  53. libc.atexit(disableRawMode);
  54. termios.cfmakeraw(raw);
  55. bresult := termios.SetFlag(raw,termios.crtscts, FALSE);
  56. bresult := termios.SetFlag(raw,termios.clocal, TRUE);
  57. result := termios.tcsnow();
  58. (* STextIO.WriteChar(CHR(13)) *)
  59. bresult := termios.SetChar(raw,termios.vmin,CHR(0));
  60. bresult := termios.SetChar(raw,termios.vtime,CHR(1));
  61. END enableRawMode;
  62. BEGIN
  63. thetermios := termios.InitTermios();
  64. raw := termios.InitTermios();
  65. IF termios.tcgetattr(FIO.StdIn, thetermios) = -1 THEN
  66. die("tcgetattr")
  67. END;
  68. IF termios.tcgetattr(FIO.StdIn, raw) = -1 THEN
  69. die("tcgetattr")
  70. END;
  71. enableRawMode;
  72. p := SYSTEM.ADR(c);
  73. LOOP
  74. c := CHR(0);
  75. number := (FIO.ReadNBytes(FIO.StdIn,1,p));
  76. IF ORD(c) < 32 THEN
  77. NumberIO.WriteCard(ORD(c),5);
  78. STextIO.WriteLn;
  79. STextIO.WriteChar(CHR(13));
  80. ELSE
  81. NumberIO.WriteCard(ORD(c),5);
  82. STextIO.WriteString(" ('");
  83. STextIO.WriteChar(c);
  84. STextIO.WriteString(" ')");
  85. STextIO.WriteLn;
  86. STextIO.WriteChar(CHR(13));
  87. END;
  88. IF c = "q" THEN
  89. EXIT
  90. END;
  91. END;
  92. END editor.