writing a shell?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
amirsadig

writing a shell?

Post by amirsadig »

I would like to write a command shell.
I have write an console driver (TTY) which implement some of POSIX standard.

but I have a few question.
1- how BACKSPACE and ARROW KEYS should be treated? in posix is undefine as i know.
2- how the promt work? I mean by doing backspace od delete should not erase the promt.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:writing a shell?

Post by Pype.Clicker »

i will not try to tell how it should work with posix TTY for all i know of linux's /dev/pts is that it's obscurely hard to get what actually occurs.

What you need is some way to control the displayed response for keystroke. No input will be used until the [ENTER] key is pressed, but all other character are used to build a line buffer that you can alter with arrows, backspace, etc.

Usually, that either means that the terminal *itself* can do those edits in a way that pleases the program, or that you turned the terminal in *raw* mode in which every keystroke is delivered but nothing is echoed until *you* display it.

considering the amount of terminal programs that are tedious to use because the shell is actually doing too much line edition (which the other programs don't), i think the 'usual' way is screwed anyway and we should rather have complete line edition, completion request-and-response, lines history etc. as an always-present component that programs could reuse.
amirsadig

Re:writing a shell?

Post by amirsadig »

in POSIX there is tow mode of input.
11.1.6 Canonical Mode Input Processing
In canonical mode input processing, terminal input is processed in units of lines. A line is delimited by a newline character (NL), an end-of-file character (EOF), or an end-of-line (EOL) character. See Special Characters for more information on EOF and EOL. This means that a read request shall not return until an entire line has been typed or a signal has been received. Also, no matter how many bytes are requested in the read() call, at most one line shall be returned. It is not, however, necessary to read a whole line at once; any number of bytes, even one, may be requested in a read() without losing information.

If {MAX_CANON} is defined for this terminal device, it shall be a limit on the number of bytes in a line. The behavior of the system when this limit is exceeded is implementation-defined. If {MAX_CANON} is not defined, there shall be no such limit; see pathconf().

Erase and kill processing occur when either of two special characters, the ERASE and KILL characters (see Special Characters ), is received. This processing shall affect data in the input queue that has not yet been delimited by an NL, EOF, or EOL character. This un-delimited data makes up the current line. The ERASE character shall delete the last character in the current line, if there is one. The KILL character shall delete all data in the current line, if there is any. The ERASE and KILL characters shall have no effect if there is no data in the current line. The ERASE and KILL characters themselves shall not be placed in the input queue.
11.1.7 Non-Canonical Mode Input Processing
In non-canonical mode input processing, input bytes are not assembled into lines, and erase and kill processing shall not occur. The values of the MIN and TIME members of the c_cc array are used to determine how to process the bytes received. IEEE Std 1003.1-2001 does not specify whether the setting of O_NONBLOCK takes precedence over MIN or TIME settings. Therefore, if O_NONBLOCK is set, read() may return immediately, regardless of the setting of MIN or TIME. Also, if no data is available, read() may either return 0, or return -1 with errno set to [EAGAIN].

MIN represents the minimum number of bytes that should be received when the read() function returns successfully. TIME is a timer of 0.1 second granularity that is used to time out bursty and short-term data transmissions. If MIN is greater than {MAX_INPUT}, the response to the request is undefined. The four possible values for MIN and TIME and their interactions are described below.
I coud make that the console response to the backspace and DEL, when it receive those character, as follow:
when backspace character send to console cursor go back and print ' ', when DEL character received it print ' ' at current position.
to use ARROW keys programs must use RAW Mode and with no echo ( as you said) and it should send the correct character sequence to go back or go forward. that mean program receive all keys and it display what it want.
or I can also make the world for programs easear by extending the Canonical Mode so that it give more functionality as they described in POSIX.
I think give programs more functionalty of the tty driver is good idea, so that they do what it need.
they could use POSIX Standard or standard with extentions.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:writing a shell?

Post by Candy »

for shell implementation ideas aside from the obvious (reading a line, storing a set of lines from the past, stuff like that), you might want to check a uni project of a friend & mine, cdsh. www.sf.net/projects/cdsh. It's in c++, and has the command line parse stuff all nice & encapsulated, plus lots of hierarchical operators (the point between a simple shell and a usable shell). They're the stuff that allows things like "for i in a b c d e; do j=`basename $i .c`; cat $i | sed -e 's/\"/\'/g' >$j.cc && rm $i && make $j.o || die; done" work correctly. We didn't do the for though, but it's trivial to implement. The backtick is even implemented (in the most intuitive way, check it out).

GL with your shell :)
Post Reply