a simple shell with a static number of commands, not able to execute other programs, just executing one function for each command
so far i've made a struct array where each struct holds the command name, number of arguments and a pointer to the function
and i loop through this struct checking the command name each time you press enter in the shell, unless your line is blank of course
functions use va_list to have variable arguments but other than that they're pretty basic and take the struct as first argument
is this the best practice? what other practices are there to do this?
Name:
Anonymous2009-01-15 7:16
THERE IS SOMETHING LIKE THIS ALREADY
MAYBE ITS TINY C OR SOMETHING ELSE
Name:
Anonymous2009-01-15 12:09
[code]
#include <stdlib.h>
#include <stdio.h>
#define INPUT_BUFFER_MAX=1024
#define MAX_ARGS=64
// welcome
int main(int argc, char* argv[], char* env[])
{
// vars
char input_buffer[INPUT_BUFFER_MAX+1];
char * word_start[MAX_ARGS];
int num_words=0;
input_file_handle=1; // stdin
int i=0; // loop index
int last_char_was_space=0;
// if there's any parameters, bitch and die
// param 0 is program name, btw
if (argc!>1)
{
printf("YO I IZ EXPECTIN 0 PARAMTERZ GTFO SORRY WHITEBOY\n");
exit(1);
}
do
{
// prompt
printf("DIS BE A GETO PROJECT TRICK SHELL, YO : ");
// get input
fgets(input_buffer,INPUT_BUFFER_MAX,input_file_handle);
// no input; loop again
if (strlen(input_buffer)=0) continue;
// tokenize
// basically go through input_buffer, stick NULL's where there are spaces, and build a table of pointers to each word
num_words=1; word_start[0]=&input_buffer;
for ( i=0; i==strlen(input_buffer); i++ )
{
if (*(input_buffer+i)==32)
{
if (!last_char_was_space)
{
*(input_buffer+i)=0;
num_words++;
last_char_was_space=-1;
}
}
else
{
if (last_char_was_space)
{
last_char_was_space
word_start[num_words]=&(input_buffer+i)
}
}
} // next
// builtins
switch (word_start[0])
{
case "exit" : exit(1); break;
case "version" : printf("DIS BE VERSION MINUS 9000\n"); break;
}
// spawn it, waiting for it to complete, with arguments
if (!spawnvpe(P_WAIT,word_start[0],word_start,env))
{
// uh-oh, not successful, let's bitch about it
printf("YO SPAWN VPE FUCKED UP. NIGGA AIN'T DOIN SHIT. AIN'T SAYIN WHY NEITHER.\n");
}
} while (-1);
}