Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a command line shell with C; trying to use ncurses/C for the first time
    primarykey
    data
    text
    <p>I'm working on a class project in which I must write a command line shell with the following requirements:</p> <ul> <li>The shell must able to read buffered input</li> <li>Buffer should be 64 characters</li> <li>Error conditions should be handled <ul> <li>Exceeded buffer size</li> <li>Interruptions (when a signal arrives) – see the man page for read()</li> <li>Invalid input (unparsable characters, blank lines, etc)</li> <li>Any other error that may be encountered.</li> </ul></li> <li>Shell must have a history of at least 20 items, and the history must not be of a static size. When the history buffer is full, the oldest item is removed and the newest item added.</li> <li>Programs should be able to run in the foreground or background. (using &amp;)</li> <li>Ctrl-D will exit the shell</li> <li>Ctrl-C will print the complete history</li> <li>The Command ‘history’ will also print the complete history. Newest items will be at the bottom of the list.</li> <li>All other signals will be trapped and displayed to the user in the shell</li> <li>Program will use the read() command to read in input, unless the arrow keys are supported</li> </ul> <p>I have opted to implement arrow keys for history cycling, so I'm using ncurses for input, rather than read(). I think I'm doing all right using strtok() to parse input, and fork() and execvp() to run the processes, but I'm not doing all right implementing ncurses correctly. All I've gotten it to do so far is init a new screen, display the prompt, then segfault upon any key press. Not good.</p> <p>I reckon the problem must be in my design. I'm not wrapping my head around ncurses too well. What sort of data structures should I be using for this project? How should I handle the ncurses setup, teardown, and everything in between? What's the deal with windows and screens, and should I have a single globally accessible window/screen that I work with? Also, I've been trying to use a char* for the input buffer, and a char** for the command history, but I have no experience in C, so despite reading up on malloc, calloc, and realloc, I'm not sure of the best way to store commands in the buffer and the history. Any tips on managing these char arrays?</p> <p><strong>tl;dr: How do I use ncurses correctly to make a command line shell, and how do I handle the command memory management with C?</strong></p> <p>I realize this is a pretty hefty question. :(</p> <p><em>edit: I have already seen <a href="http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html" rel="nofollow">http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html</a> and <a href="http://www.linuxinfor.com/english/NCURSES-Programming/" rel="nofollow">http://www.linuxinfor.com/english/NCURSES-Programming/</a> but the ncurses documentation has actually too much overhead. I just want to use its ability to recognize arrow keys.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload