Page 1 of 2
Best way of making a shell
Posted: Fri May 31, 2013 2:37 pm
by computertrick
I have started writing a basic shell for my operating system. I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out. But a stack is a last in first out approach so anything a user enters comes out backwards.
For example:
hello
olleh
What would you consider be the best approach to get the characters printed out the correct way I did think of pushing them all on the stack again during execution and then popping them off so they would be the correct way around but in order to do that I would need to store the characters in another stack.
Does anybody have any other solutions for this?
Would the SI register be useful to use in this situation?
Thanks
Re: Best way of making a shell
Posted: Fri May 31, 2013 2:46 pm
by DavidCooper
You could ask the user to type everything in backwards. Alternatively, find a way to work with strings.
Re: Best way of making a shell
Posted: Fri May 31, 2013 2:50 pm
by Combuster
I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out
Not sure if trolling or just an incapable programmer?
Re: Best way of making a shell
Posted: Fri May 31, 2013 2:51 pm
by computertrick
Combuster wrote:I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out
Not sure if trolling or just an incapable programmer?
excuse me?
Re: Best way of making a shell
Posted: Fri May 31, 2013 7:28 pm
by Prochamber
Not sure if trolling or just an incapable programmer?
I think the
Required Knowledge is a bit lacking...
I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out.
You need to put the inputted characters into a temporary string
not the stack.
Or you could just pop the characters off the stack, put them in the string and use some kind of string reverse function.
Re: Best way of making a shell
Posted: Fri May 31, 2013 8:56 pm
by Love4Boobies
Wait a minute. I thought all data structures and algorithms were equivalent and could be used interchangeably.
Re: Best way of making a shell
Posted: Fri May 31, 2013 9:01 pm
by Minoto
Love4Boobies wrote:Wait a minute. I thought all data structures and algorithms were equivalent and could be used interchangeably.
Didn't Wirth document that in
Algorithm = Data Structure = Program?
Re: Best way of making a shell
Posted: Fri May 31, 2013 9:04 pm
by Love4Boobies
You just won the Internet.
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 12:26 am
by iansjack
Consider the possibility that you have picked the wrong data structure. For this purpose you need some form of static, semi-dynamic, or dynamic string. You could implement this with an array, a queue, a ring buffer, or even a linked list (unlikely). A stack is, as you have discovered, a poor choice.
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 12:40 am
by Love4Boobies
I'm not sure what is more awesome... Your advice or the fact that you think queues are data structures.
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 2:04 am
by iansjack
I am sure that the least awesome is the fact that you don't think that queues are data structures.
So it goes.
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 2:24 am
by dozniak
Very entertaining thread.
I think Wirth's book is called "Algorithms + Data Structures = Programs" (
proof)
So there's no equality amongst the equals.
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 4:00 am
by iansjack
The Art of Computer Programming also makes good reading (Section 2.2.1 of Vol. 1 is relevant to this thread).
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 4:53 am
by sortie
The best way of making a shell is writing a lexer and a parser that generates an abstract syntax tree representing the commands that user want executed. This is trivial for a shell that only understands 'foo bar baz' commands, but things get more complicated if you add environmental variables, escape characters, quotes, pipes, conditionals, and all the other good shell features. Depending on your shell language, another approach may be better, but lexer+parser is a good and well-understood solution for parsing context-free programming languages.
Why use a stack as a stack? Just allocate 256 characters as a string and fill it up from the start using user-input. When it fills up completely, you reallocate and double its size and then continue. When you encounter a newline, you discard that character, put in a '\0' that terminates the string and call execute_a_shell_command(const char*). Voila!
Re: Best way of making a shell
Posted: Sat Jun 01, 2013 5:07 am
by Love4Boobies
dozniak wrote:Very entertaining thread.
I think Wirth's book is called "Algorithms + Data Structures = Programs" (
proof)
So there's no equality amongst the equals.
It's hardly entertaining if you didn't get the joke.
iansjack wrote:The Art of Computer Programming also makes good reading (Section 2.2.1 of Vol. 1 is relevant to this thread).
Maybe you should go ahead and read it then. I assure you I didn't get my reward checks without doing it. Anyway, a queue is an abstract type, usually implemented using linked lists, which you have mentioned separately.