| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- MODULE editor;
- (* based on the kilo editor building course*)
- (* Step 20 *)
- (*** IMPORTS ***)
- IMPORT IO, termios, FIO, libc, CharClass, NumberIO, ASCII ;
- IMPORT STextIO, Strings;
- (*** data ***)
- VAR
- c : CHAR;
- theTermios : termios.TERMIOS;
- TCSAFLUSH : INTEGER;
- i : CARDINAL;
- (*** Terminal ***)
- PROCEDURE CtrlKey(c: CHAR) : CARDINAL;
- VAR
- n : BITSET;
- BEGIN
- n := BITSET(ORD(c));
- n := n * BITSET(1FH);
- RETURN CARDINAL(n)
- END CtrlKey;
- PROCEDURE die (s : ARRAY OF CHAR);
- BEGIN
- libc.perror(s);
- FOR i := 0 TO Strings.Length(s) DO
- IO.Write(s[i])
- END;
- HALT
- END die;
- PROCEDURE disablerawMode() : INTEGER;
- VAR
- result : INTEGER;
- BEGIN
- IO.BufferedMode(0,TRUE);
- IO.BufferedMode(1,TRUE);
- result := termios.tcsetattr(FIO.StdIn, TCSAFLUSH, theTermios);
- IF result = -1 THEN
- theTermios := termios.KillTermios(theTermios);
- die("tcsetattr")
- ELSE
- RETURN result
- END;
- END disablerawMode;
- PROCEDURE enablerawMode;
- BEGIN
- libc.atexit(disablerawMode);
- theTermios := termios.InitTermios();
- IF termios.tcgetattr(FIO.StdIn, theTermios) = -1 THEN
- die("tcgetattr")
- END;
- IO.UnBufferedMode(0,TRUE);
- IO.UnBufferedMode(1,TRUE);
- END enablerawMode;
- (*** Init ***)
- BEGIN
- TCSAFLUSH := termios.tcsflush ();
- enablerawMode;
- LOOP
- IO.Read(c);
- IF ORD(c) = 17 THEN
- EXIT
- ELSIF CharClass.IsControl(c) THEN
- NumberIO.WriteCard(ORD(c),5);
- IO.Write(CHR(10));
- IO.Write(CHR(13));
- ELSE
- NumberIO.WriteCard(ORD(c),5);
- IO.Write(" ");
- IO.Write("(");
- IO.Write("'");
- IO.Write(c);
- IO.Write(" ");
- IO.Write("'");
- IO.Write(")");
- IO.Write(CHR(10));
- IO.Write(CHR(13));
-
- END;
- END;
- END editor.
|