OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Jan 21, 2019 4:31 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Porting 'clear'
PostPosted: Sat Jan 19, 2019 8:20 am 
Offline

Joined: Sun Aug 20, 2017 10:59 am
Posts: 11
Hello everyone !
Recently i was porting basic utils software to my kernel, and i compiled 'clear' without errors. I didn't read the code, i was just trying to compile some coreutils i found on the web and then look at compilations errors to see what i needed to implement. As i did not implement any system call to access the 'clear' methods that i have in my kernel, i was sure it would be impossible. Then i found out that 'clear' is actually only printing a sequence of special characters to the standard output.
Now i need to handle these in my kernel, but i was wondering what was the proper way to do it... I could just copy/paste that sequence and check for it but it seems really bad to me.
I have read some articles on the web like the linux manual, but they are not really helping me.
So, how did you do it ? How did you port 'clear' and adapt your kernel to it ? Is there a convention (some parts of POSIX?) that we should follow ?


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sat Jan 19, 2019 8:38 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 516
Most of those escape sequences are documented. In particular, if you ask Google, you'll find lists of escape sequences that xterm implements (e.g. here). Most other modern terminals just imitate xterm.

For example, the \033[2J sequence mentioned by the Linux man page corresponds to CSI P s J ("Erase in Display (ED)") from the xterm list. Here, CSI denotes the start of the control sequence and consists of the characters ESC [.

_________________
managarm: A microkernel-based OS that is capable of running a Wayland desktop


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sat Jan 19, 2019 2:34 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1399
Location: Athens, GA, USA
The solution is going to depend on a few things regarding how your UI works. Porting the Linux code directly isn't going to work unless you have a lower-level implementation which the ported code can use.

Do you have an offsite repo on, say, GitHub, GitLab, or Sourceforge, which would allow us to view your existing code? That would simplify things for us a great deal, because then we'd be able to address what's actually running.

EDIT: I found this repo on GitHub, which I assume is the one in question based your earlier posts.

Speaking in broad terms, a 'clear screen' utility is going to require you to do two things: a) clear all the character cells in the screen buffer, and b) reset the cursor to the first text cell of the first line of text.

For a 'raw' text mode screen, this would usually mean a memset filling the current screen page with a blank space for the character byte and the current background value for the attribute byte. The details of this, and of writing to the text-mode screen in general, is covered in the wiki page Printing To Screen. How large the fill would have to be would depend on the text mode being used; obviously, a 132x60 text mode buffer will be larger than an 80x30 one.

This would be followed by resetting the hardware cursor as described in the wiki pages on Text UI and the Text Mode Cursor.

However, that is a specific set of special cases; you generally want to abstract this in order to make it easier to write code which also works in, say, a text window in a GUI. This is what the Unix Curses and Termcap/Terminfo libraries are meant to do (as are some newer ones which are specific to Linux, which I don't know as much about; and in the case of a desktop environment, still others specific to the GUI being used).

The clear utility which you were trying to port relies on these - it is sending a series of escape codes which those libraries would then interpret (in this case, ones used by the xterm terminal emulator and compatible terminal emulators, as Korona stated earlier). It is probably getting terminal capabilities information from terminfo or something similar before deciding the specific sort of escape codes (or other facilities) to use for the specific text screen type.

In order to port that utility as-is, you would first need to implement the operation for the specific type of screen, and then implement an equivalent set of abstract libraries (either by porting the Unix/Linux libraries, or writing similar ones yourself) which the utility can call upon.

It sounds as if you already have a screen-clearing routine; if so, then what you need is to get the clear utility to talk to it.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sat Jan 19, 2019 4:34 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 252
To properly port clear I recommend reading Dianetics and the works of L Ron Hubbard. To understand better you could go to a local Scientology church.


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sat Jan 19, 2019 6:05 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1399
Location: Athens, GA, USA
MichaelPetch wrote:
To properly port clear I recommend reading Dianetics and the works of L Ron Hubbard. To understand better you could go to a local Scientology church.

#-o :roll: =D>

(I'll admit, I wish I'd thought of this myself fnord. Thank you.)

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sun Jan 20, 2019 6:44 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 157
Schol-R-LEA wrote:
MichaelPetch wrote:
To properly port clear I recommend reading Dianetics and the works of L Ron Hubbard. To understand better you could go to a local Scientology church.

#-o :roll: =D>

(I'll admit, I wish I'd thought of this myself fnord. Thank you.)

I don't think that the victims of that cult would find your posts funny.


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sun Jan 20, 2019 7:04 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 117
Getting back to the actual topic, terminal control sequences are pretty horrid, so I'd suggest creating a halfway usable framebuffer implementation (i.e. methods to read input from keyboard and write output to screen) and then porting an existing terminal emulator to it (like, say, st or uuterm).


Top
 Profile  
 
 Post subject: Re: Porting 'clear'
PostPosted: Sun Jan 20, 2019 9:33 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 280
nullplan wrote:
Getting back to the actual topic, terminal control sequences are pretty horrid, so I'd suggest creating a halfway usable framebuffer implementation (i.e. methods to read input from keyboard and write output to screen) and then porting an existing terminal emulator to it (like, say, st or uuterm).


I disagree. I think implementing a subset of the modern xterm control set - enough to support applications like vim, and to generally run Curses apps - is a valuable and enriching experience, and not nearly as convoluted as it seems.

_________________
toaruos on github | toaruos.org | gitlab | twitter |


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group