Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-09

Unit crt

18 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Rainer Stratmann

What does SetRawMode do?

var
  OldIO : termio.TermIos;
  inputRaw, outputRaw: boolean;
procedure saveRawSettings(const tio: termio.termios);
Begin
  with tio do
   begin
     inputRaw :=
       ((c_iflag and (IGNBRK or BRKINT or PARMRK or ISTRIP or
                                INLCR or IGNCR or ICRNL or IXON)) = 0) and
       ((c_lflag and (ECHO or ECHONL or ICANON or ISIG or IEXTEN)) = 0);
     outPutRaw :=
       ((c_oflag and OPOST) = 0) and
       ((c_cflag and (CSIZE or PARENB)) = 0) and
       ((c_cflag and CS8)  0);
   end;
end;
procedure restoreRawSettings(tio: termio.termios);
begin
  with tio do
    begin
      if inputRaw then
        begin
          c_iflag := c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
            INLCR or IGNCR or ICRNL or IXON));
          c_lflag := c_lflag and
            (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
       end;
     if outPutRaw then
       begin
         c_oflag := c_oflag and not(OPOST);
         c_cflag := c_cflag and not(CSIZE or PARENB) or CS8;
       end;
   end;
end;
Procedure SetRawMode(b:boolean);
Var
  Tio : Termios;
Begin
  if b then
   begin
     TCGetAttr(1,Tio);
     SaveRawSettings(Tio);
     OldIO:=Tio;
     CFMakeRaw(Tio);
   end
  else
   begin
     RestoreRawSettings(OldIO);
     Tio:=OldIO;
   end;
  TCSetAttr(1,TCSANOW,Tio);
End;
Michael Van Canneyt
to Rainer Stratmann
Rainer Stratmann wrote
What does SetRawMode do?

puts the terminal in RAW mode.
This disables usual terminal tricks such as translating CTRL-C to SIGINT etc.

A terminal does quite some things 'behind your back', which are not desirable for the CRT unit.

Michael.

Show quoted text 66 lines
var OldIO : termio.TermIos; inputRaw, outputRaw: boolean; procedure saveRawSettings(const tio: termio.termios); Begin with tio do begin inputRaw := ((c_iflag and (IGNBRK or BRKINT or PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON)) = 0) and ((c_lflag and (ECHO or ECHONL or ICANON or ISIG or IEXTEN)) = 0); outPutRaw := ((c_oflag and OPOST) = 0) and ((c_cflag and (CSIZE or PARENB)) = 0) and ((c_cflag and CS8) 0); end; end; procedure restoreRawSettings(tio: termio.termios); begin with tio do begin if inputRaw then begin c_iflag := c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON)); c_lflag := c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN)); end; if outPutRaw then begin c_oflag := c_oflag and not(OPOST); c_cflag := c_cflag and not(CSIZE or PARENB) or CS8; end; end; end; Procedure SetRawMode(b:boolean); Var Tio : Termios; Begin if b then begin TCGetAttr(1,Tio); SaveRawSettings(Tio); OldIO:=Tio; CFMakeRaw(Tio); end else begin RestoreRawSettings(OldIO); Tio:=OldIO; end; TCSetAttr(1,TCSANOW,Tio); End; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Michael Van Canneyt: puts the terminal in RAW mode. This disables usual terminal tricks such as translating…

Does it have something to do with the output of Linux commands?

Now the outputs are:

Message 1
Message 2
Message 3

How is it possible to get the normal behaviour?

Message 1
Message 2
Message 3

you wrote 116 lines
On Tue, 22 Sep 2015, Rainer Stratmann wrote: > What does SetRawMode do? puts the terminal in RAW mode. This disables usual terminal tricks such as translating CTRL-C to SIGINT etc. A terminal does quite some things 'behind your back', which are not desirable for the CRT unit. Michael. > var > > OldIO : termio.TermIos; > inputRaw, outputRaw: boolean; > > procedure saveRawSettings(const tio: termio.termios); > Begin > > with tio do > > begin > > inputRaw := > > ((c_iflag and (IGNBRK or BRKINT or PARMRK or ISTRIP or > > INLCR or IGNCR or ICRNL or IXON)) = 0) and > > ((c_lflag and (ECHO or ECHONL or ICANON or ISIG or IEXTEN)) = 0); > > outPutRaw := > > ((c_oflag and OPOST) = 0) and > ((c_cflag and (CSIZE or PARENB)) = 0) and > ((c_cflag and CS8) 0); > > end; > > end; > > procedure restoreRawSettings(tio: termio.termios); > begin > > with tio do > > begin > > if inputRaw then > > begin > > c_iflag := c_iflag and (not (IGNBRK or BRKINT or PARMRK or > ISTRIP or > > INLCR or IGNCR or ICRNL or IXON)); > > c_lflag := c_lflag and > > (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN)); > > end; > > if outPutRaw then > > begin > > c_oflag := c_oflag and not(OPOST); > c_cflag := c_cflag and not(CSIZE or PARENB) or CS8; > > end; > > end; > > end; > > > Procedure SetRawMode(b:boolean); > Var > > Tio : Termios; > > Begin > > if b then > > begin > > TCGetAttr(1,Tio); > SaveRawSettings(Tio); > OldIO:=Tio; > CFMakeRaw(Tio); > > end > > else > > begin > > RestoreRawSettings(OldIO); > Tio:=OldIO; > > end; > > TCSetAttr(1,TCSANOW,Tio); > > End; > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Marco van de Voort
to Rainer Stratmann

In our previous episode, Rainer Stratmann said:

Does it have something to do with the output of Linux commands?

Yes. In raw mode cr and lf are separate. so suddenly your lf only advances to the next line, but doesn't reset the carriage to the first column.

Use TProcess to capture the command output and add #13#10 or turn off rawmode before executing and on again after (like the textmode IDE does)

Rainer Stratmann
to Marco van de Voort
you wrote 9 lines
In our previous episode, Rainer Stratmann said: > Does it have something to do with the output of Linux commands? Yes. In raw mode cr and lf are separate. so suddenly your lf only advances to the next line, but doesn't reset the carriage to the first column. Use TProcess to capture the command output and add #13#10 or turn off rawmode before executing and on again after (like the textmode IDE does)

Does an example exist to turn off (and on) the rawmode?

Mark Morgan Lloyd
to Rainer Stratmann: Does it have something to do with the output of Linux commands? Now the outputs are:…
Rainer Stratmann wrote 9 lines
Does it have something to do with the output of Linux commands? Now the outputs are: Message 1 Message 2 Message 3 How is it possible to get the normal behaviour?

stty sane

Note that is doesn't so much affect the output of the commands as the way your local terminal is going to respond to the text it's being sent.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Rainer Stratmann
to Jonas Maebe
you wrote 5 lines
Rainer Stratmann wrote on Wed, 23 Sep 2015: > Does an example exist to turn off (and on) the rawmode? You posted the routine to do that yourself in the first mail of this thread (the last one).

There is not enough information available at the moment to me to do so.

I need the information what CFMakeRaw(Tio) does. I can not find the source.

Which bit is responsible for surpressing the cr output?

Is this bit in c_iflag, c_oflag, c_lflag, or c_cflag?

Who can help?

Rainer Stratmann
to Jonas Maebe: Rainer Stratmann wrote on Wed, 23 Sep 2015: You posted the routine to do that yourself in…
you wrote 5 lines
Rainer Stratmann wrote on Wed, 23 Sep 2015: > Does an example exist to turn off (and on) the rawmode? You posted the routine to do that yourself in the first mail of this thread (the last one).

Ok, I guess the ONOCR flag in c_oflag bits is responsible for supressing CR.

http://svn.freepascal.org/svn/fpc/tags/release_2_6_4/rtl/linux/termios.inc

Mark Morgan Lloyd
to Rainer Stratmann
Rainer Stratmann wrote 7 lines
On Wednesday 23 September 2015 13:19:28 you wrote: > Rainer Stratmann wrote on Wed, 23 Sep 2015: >> Does an example exist to turn off (and on) the rawmode? > You posted the routine to do that yourself in the first mail of this > thread (the last one). > http://man7.org/linux/man-pages/man3/termios.3.html

Noting in particular that that documents cfmakeraw(), and that there isn't a specific function to "unmake" it which is why the original state is saved.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Jonas Maebe
to Rainer Stratmann: There is not enough information available at the moment to me to do so. I need the…
Rainer Stratmann wrote 9 lines
On Wednesday 23 September 2015 13:19:28 you wrote: > >Rainer Stratmann wrote on Wed, 23 Sep 2015: >> > >Does an example exist to turn off (and on) the rawmode? > > > >You posted the routine to do that yourself in the first mail of this > >thread (the last one). There is not enough information available at the moment to me to do so. I need the information what CFMakeRaw(Tio) does. I can not find the source.

cfmakeraw is a C routine that is implemented in a C library.

However, you can just call SetRawMode(false) before you want the output to be "normal" (and in the process prevent the crt unit from functioning correctly) and call SetRawMode(true) when you want to use the crt unit's functionality again.

Jonas

Rainer Stratmann
to Jonas Maebe
you wrote 19 lines
On 23/09/15 15:06, Rainer Stratmann wrote: > On Wednesday 23 September 2015 13:19:28 you wrote: >> >Rainer Stratmann wrote on Wed, 23 Sep 2015: >>> > >Does an example exist to turn off (and on) the rawmode? >> > >> >You posted the routine to do that yourself in the first mail of this >> >thread (the last one). > > There is not enough information available at the moment to me to do so. > > I need the information what CFMakeRaw(Tio) does. I can not find the > source. cfmakeraw is a C routine that is implemented in a C library. However, you can just call SetRawMode(false) before you want the output to be "normal" (and in the process prevent the crt unit from functioning correctly) and call SetRawMode(true) when you want to use the crt unit's functionality again.

That would be good, but SetRawMode() is not in the interface section.

I am confident to get solution soon. It is already good to know where to work on. Thanks. May be I will post the solution here.

Rainer Stratmann
to Jonas Maebe: cfmakeraw is a C routine that is implemented in a C library. However, you can just call…
you wrote 19 lines
On 23/09/15 15:06, Rainer Stratmann wrote: > On Wednesday 23 September 2015 13:19:28 you wrote: >> >Rainer Stratmann wrote on Wed, 23 Sep 2015: >>> > >Does an example exist to turn off (and on) the rawmode? >> > >> >You posted the routine to do that yourself in the first mail of this >> >thread (the last one). > > There is not enough information available at the moment to me to do so. > > I need the information what CFMakeRaw(Tio) does. I can not find the > source. cfmakeraw is a C routine that is implemented in a C library. However, you can just call SetRawMode(false) before you want the output to be "normal" (and in the process prevent the crt unit from functioning correctly) and call SetRawMode(true) when you want to use the crt unit's functionality again.
var oldio : termio.termios;
procedure crt_output_set_normal;
var tio : termios;
begin
 tcgetattr( 1 , tio );
 oldio := tio;
 // !
 tio.c_oflag := tio.c_oflag or OPOST or ONLCR;
 // !
 tcsetattr( 1 , TCSANOW , tio );
end;
procedure crt_output_restore;
begin
 tcsetattr( 1 , TCSANOW , oldio );
end;
Roger Rivero Jr. (SAMPA WebMaster
to Rainer Stratmann: var oldio : termio.termios; procedure crt_output_set_normal; var tio : termios; begin…

El 23/9/2015 12:25 PM, Rainer Stratmann escribió:

Rather than saving the state in a static way, I prefer implementing a Push/Pop (LIFO queue) approach to allow for nested pairs of setting and restoring, like this:

Type IOsCollection = {Any sort of collection, array, list, or record
with linking pointers};
var oldios : IOsCollection;
procedure PushTio(tio : termios);
begin
//Stores tio in the oldios: IOsCollection, and advances a pointer, index, etc., if necessary
end;
funtion PopTio: termios;
begin
//Retrieves and removes last pushed tio from the oldios: IOsCollection
end;
procedure crt_output_set_normal; var tio : termios; begin tcgetattr( 1 , tio );
   PushTio(tio);
Show quoted text 9 lines
// ! tio.c_oflag := tio.c_oflag or OPOST or ONLCR; // ! tcsetattr( 1 , TCSANOW , tio ); end; procedure crt_output_restore; begin // tcsetattr( 1 , TCSANOW , oldio ); //old suggestion
  tcsetattr( 1 , TCSANOW , PopTio );
Show quoted text 5 lines
end; _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

----------------------------------------------------- I repeat it here for the sake of clarity
----------------------------------------------------- Type IOsCollection = {Any sort of collection, array, list, or record with linking pointers};

var oldios : IOsCollection;
procedure PushTio(tio : termios);
begin
//Stores tio in the IOsCollection, and advances a pointer, index, etc., if necessary
end;
funtion PopTio: termios;
begin
//Retrieves and removes last pushed tio from the IOsCollection
end;
procedure crt_output_set_normal;
var tio : termios;
begin
  tcgetattr( 1 , tio );
  PushTio(tio);
  // !
  tio.c_oflag := tio.c_oflag or OPOST or ONLCR;
  // !
  tcsetattr( 1 , TCSANOW , tio );
end;
procedure crt_output_restore;
begin
  tcsetattr( 1 , TCSANOW , PopTio );
end;

---------------------------------------------
Now you can nest them
---------------------------------------------

uses AnyUnit {Unit that implements crt_output_set_any_other_tio_state and any_other_tio_modifyng_routine};
procedure any_tio_modifyng_routine; forward;
procedure MyNestingExample;
begin
crt_output_set_normal;
// Do some outputs
any_tio_modifyng_routine;
//keep working;
crt_output_restore
end;
procedure any_tio_modifyng_routine;
begin
crt_output_set_any_other_tio_state;
// Do some outputs
any_other_tio_modifyng_routine;
//keep working;
crt_output_restore
end;

Best regards,
Roger Rivero