Creating A Shell: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
Line 35:
Based on the explanations above, here is the main function for a simple shell:
 
<sourcesyntaxhighlight lang="c">
void main(int argc, char* argv[]) // edit as appropriate for your kernel
{
Line 67:
}
}
</syntaxhighlight>
</source>
 
=Implementation=
The implementation of the functions referred to in the above code snippet depends heavily on the design of your operating system, but here are some typical examples:
 
<sourcesyntaxhighlight lang="c">
static void output_prompt()
{
Line 120:
// TODO: this function will allocate a char buffer containing the line read
}
</syntaxhighlight>
</source>
 
=TODO=
Line 140:
First we need to make a file, shell.c, this file will contain all our functions and shell managers. The first function we need to write is our initialization function then our actual shell function.
 
<sourcesyntaxhighlight lang="c">
/* shell.c */
void init_shell()
Line 149:
{
}
</syntaxhighlight>
</source>
 
Now you need to add "init_shell()" to your setup routine in your kernel.c file or whatever you named it, followed by a while loop running shell until the boolean "exit_status" is true.
Line 155:
Ok, now we need to write a few things. The first is command table, or a way to store our commands. I do this with a structure as follows:
 
<sourcesyntaxhighlight lang="c">
/* shell.h */
#define MAX_COMMANDS 100
Line 165:
void *function;
} command_table_t;
</syntaxhighlight>
</source>
 
Now we have a way to store commands, but how do we use this? Well lets add a few variables to shell.c:
 
<sourcesyntaxhighlight lang="c">
/* shell.c */
command_table_t CommandTable[MAX_COMMANDS];
</syntaxhighlight>
</source>
 
This goes right above the function definitions. The first is a way to keep track of how many commands there are. The second is our command table. Ok, so now we have a "Command Table", but how to we access it? Well in order to add a command we need a function to do it. Add this below the other functions:
Line 194:
Ok, now we are getting somewhere. We can add a command. But what about getting command line input? Thats up to our "shell()" function. But before we code that, lets add another variable: "char* input_string". Now for the shell function:
 
<sourcesyntaxhighlight lang="c">
/* shell.c */
void shell()
Line 203:
void (*command_run)(void);
}
</syntaxhighlight>
</source>
 
Ok, now we can get a string. But what about finding what command the user typed? Well we rely on old trusty "findCommand()" function for that one:
Line 228:
Now we need to add a little something right below "gets(input_string)", but above "void (*command_function)(void)":
 
<sourcesyntaxhighlight lang="c">
/* shell.c */
int i = findCommand(input_string);
Line 243:
return;
</syntaxhighlight>
</source>
 
Now in our "init_shell()" function we need to add a few commands:
 
<sourcesyntaxhighlight lang="c">
/* shell.c */
add_new_command("help", "You code this one.", help_command);
add_new_command("", "", empty_command);
</syntaxhighlight>
</source>
 
Now in our shell.h file:
 
<sourcesyntaxhighlight lang="c">
extern void add_new_command();
extern void help_command();
extern void empty_command();
</syntaxhighlight>
</source>
 
Now you need to code a void help_command function and a empty_command that manages null input, all it needs is a definition:
 
<sourcesyntaxhighlight lang="c">
void empty_command()
{
}
</syntaxhighlight>
</source>
 
An idea is to loop through all the command table and print the command name and description - for the help command.