Best way of making a shell
-
- Member
- Posts: 71
- Joined: Wed May 29, 2013 1:07 pm
Best way of making a shell
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
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
1100110100010011
- DavidCooper
- Member
- Posts: 1150
- Joined: Wed Oct 27, 2010 4:53 pm
- Location: Scotland
Re: Best way of making a shell
You could ask the user to type everything in backwards. Alternatively, find a way to work with strings.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c
MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Best way of making a shell
Not sure if trolling or just an incapable programmer?I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out
-
- Member
- Posts: 71
- Joined: Wed May 29, 2013 1:07 pm
Re: Best way of making a shell
excuse me?Combuster wrote:Not sure if trolling or just an incapable programmer?I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out
1100110100010011
-
- Member
- Posts: 100
- Joined: Wed Mar 13, 2013 2:27 am
Re: Best way of making a shell
I think the Required Knowledge is a bit lacking...Not sure if trolling or just an incapable programmer?
You need to put the inputted characters into a temporary string not the stack.I push the characters the user enters onto the stack and then when the enter key is pressed I pop them all out.
Or you could just pop the characters off the stack, put them in the string and use some kind of string reverse function.
TachyonOS - Violates causality on 95% of attempts. Runs at approximately 1.5c.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Best way of making a shell
Wait a minute. I thought all data structures and algorithms were equivalent and could be used interchangeably.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Best way of making a shell
Didn't Wirth document that in Algorithm = Data Structure = Program?Love4Boobies wrote:Wait a minute. I thought all data structures and algorithms were equivalent and could be used interchangeably.
Those who understand Unix are doomed to copy it, poorly.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Best way of making a shell
You just won the Internet.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Best way of making a shell
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.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Best way of making a shell
I'm not sure what is more awesome... Your advice or the fact that you think queues are data structures.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Best way of making a shell
I am sure that the least awesome is the fact that you don't think that queues are data structures.
So it goes.
So it goes.
Re: Best way of making a shell
Very entertaining thread.
I think Wirth's book is called "Algorithms + Data Structures = Programs" (proof)
So there's no equality amongst the equals.
I think Wirth's book is called "Algorithms + Data Structures = Programs" (proof)
So there's no equality amongst the equals.
Learn to read.
Re: Best way of making a shell
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
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!
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!
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Best way of making a shell
It's hardly entertaining if you didn't get the joke.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.
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.iansjack wrote:The Art of Computer Programming also makes good reading (Section 2.2.1 of Vol. 1 is relevant to this thread).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]