PDA

View Full Version : builtin command



jon21
04-09-2002, 03:37 PM
I am in the progress of creating a bash shell. I have created my own stand alone programs for ls, kill, ps etc etc. How do I incorporate them into my program? I've got two programs - one called <def.h> where all the definitions and globals are stored and the other one the main code, with the main function, getline function, parse function, execute function etc.

The only bit i'm confused about is the builtin command - can anyone explain it to me? This is what I've found on the web site - and I don't understand it :-(
builtin()
{
if (check("exit"))
exit (0);
else if (check("cd"))
return do_cd();
else if (check("history"))
return do_history();
else if (check("!"))
return do_repeat_connand();
else
return 0;
}

The do_cd() function can then extract a directory name from the line[] array and use the chdir() system call to make the change. For the ! and history commands, you need to modify the getline() function so that lines entered at the keyboard are stored as they are entered. The history command can then list all the commands in the store and the command can copy the appropriate stored command back into the line[] array. do_repeat_command() can then return a zero from builtin() so that line[] will be parsed as normal.

Do I need to specify the path of my program in the def.h??? How do I carry out my own script within the builtin command?!!!

Thanks in advanced.
J

pdstatha
04-11-2002, 03:42 AM
In the forkexec function you should see the following code.



execvp(ptr->av[0],ptr->av);
exit(1);

If you use the execv function then u can execute ur own code, for example.



execv("cd.exe",ptr->av);
exit(1);