Best way of making 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.
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

Best way of making a shell

Post 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
1100110100010011
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Best way of making a shell

Post by DavidCooper »

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
User avatar
Combuster
Member
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

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

Re: Best way of making a shell

Post 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?
1100110100010011
Prochamber
Member
Member
Posts: 100
Joined: Wed Mar 13, 2013 2:27 am

Re: Best way of making a shell

Post 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.
TachyonOS - Violates causality on 95% of attempts. Runs at approximately 1.5c.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Best way of making a shell

Post by Love4Boobies »

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 ]
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: Best way of making a shell

Post 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?
Those who understand Unix are doomed to copy it, poorly.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Best way of making a shell

Post by Love4Boobies »

You just won the Internet. :lol:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Best way of making a shell

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Best way of making a shell

Post by Love4Boobies »

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 ]
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Best way of making a shell

Post 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. :)
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Best way of making a shell

Post 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.
Learn to read.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Best way of making a shell

Post by iansjack »

The Art of Computer Programming also makes good reading (Section 2.2.1 of Vol. 1 is relevant to this thread).
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Best way of making a shell

Post 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!
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Best way of making a shell

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Locked