- readme. todo
- update pwd - update SHLVL - basename pwd in prompt - updating pwd
This commit is contained in:
18
README.md
Normal file
18
README.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# todo
|
||||||
|
|
||||||
|
- [x] environment variables
|
||||||
|
- [x] custom prompts
|
||||||
|
- [x] minimal current directory
|
||||||
|
- [ ] quotes
|
||||||
|
- [-] handle ~
|
||||||
|
- [ ] dynamically allocate cwd in cd builtin command
|
||||||
|
- [-] all of posixs
|
||||||
|
- [ ] aliases
|
||||||
|
- [x] increase the SHLVL var
|
||||||
|
- [x] update pwd
|
||||||
|
- [ ] pipes |
|
||||||
|
- [ ] redirections > <
|
||||||
|
- [ ] append >>
|
||||||
|
- [ ] heredoc
|
||||||
|
- [ ] rcfile
|
||||||
|
- [ ] rcfile in homedir
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef shell_helper_functions
|
#ifndef shell_helper_functions
|
||||||
#define shell_helper_functions
|
#define shell_helper_functions
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -51,4 +53,38 @@ char *str_replace(char *orig, char *rep, char *with) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *basename(char *path_in)
|
||||||
|
{
|
||||||
|
char *token = NULL;
|
||||||
|
char *lastToken = NULL;
|
||||||
|
char *basePath;
|
||||||
|
|
||||||
|
basePath = (char *)malloc(sizeof(char) * (strlen(path_in) + 1));
|
||||||
|
if (!basePath) {
|
||||||
|
fprintf(stderr, "lsh: allocatin error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
strcpy(basePath, path_in);
|
||||||
|
|
||||||
|
token = strtok(basePath, "/");
|
||||||
|
while (token != NULL) {
|
||||||
|
lastToken = token;
|
||||||
|
token = strtok(NULL, "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastToken == NULL){
|
||||||
|
free(basePath);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1);
|
||||||
|
if (!ret_str) {
|
||||||
|
fprintf(stderr, "lsh: allocation error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
strcpy(ret_str,lastToken);
|
||||||
|
free(basePath);
|
||||||
|
return ret_str;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -44,9 +44,18 @@ int lsh_cd(char **args)
|
|||||||
if (args[1] == NULL) {
|
if (args[1] == NULL) {
|
||||||
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
|
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
|
||||||
} else {
|
} else {
|
||||||
if (chdir(args[1]) != 0) {
|
int err = chdir(args[1]);
|
||||||
|
if (err != 0) {
|
||||||
perror("lsh");
|
perror("lsh");
|
||||||
}
|
}
|
||||||
|
char *cwd=(char*)malloc(sizeof(char)*4096);
|
||||||
|
if (!cwd) {
|
||||||
|
fprintf(stderr, "lsh: allocation error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
getcwd(cwd,sizeof(char*)*4096);
|
||||||
|
setenv("PWD",cwd,1);
|
||||||
|
free(cwd);
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -54,7 +63,7 @@ int lsh_cd(char **args)
|
|||||||
int lsh_help(char **args)
|
int lsh_help(char **args)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
printf("Stephen Brennan's LSH\n");
|
printf("FOX SHELL\n");
|
||||||
printf("Type program names and arguments, and hit enter.\n");
|
printf("Type program names and arguments, and hit enter.\n");
|
||||||
printf("The following are built in:\n");
|
printf("The following are built in:\n");
|
||||||
|
|
||||||
|
|||||||
@@ -46,10 +46,12 @@ char *lsh_read_line(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *handle_vars(char *token){
|
char *handle_vars(char *token){
|
||||||
|
token=&token[0];
|
||||||
if (token[0] == '$'){
|
if (token[0] == '$'){
|
||||||
token=&token[1];
|
token=&token[1];
|
||||||
token=getenv(token);
|
token=getenv(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
58
src/main.c
58
src/main.c
@@ -2,43 +2,75 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "helper_functions.c"
|
||||||
#include "lsh_main_func.c"
|
#include "lsh_main_func.c"
|
||||||
#include "lsh_builtins.c"
|
#include "lsh_builtins.c"
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
char *prompt;
|
char *prompt;
|
||||||
|
char *pre_prompt;
|
||||||
} SHELL_OB;
|
} SHELL_OB;
|
||||||
|
|
||||||
void lsh_loop(SHELL_OB *shell_obj)
|
void lsh_loop(SHELL_OB *shell_obj)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
char **args;
|
char **args;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
printf("%s",shell_obj->prompt);
|
//get pwd
|
||||||
line = lsh_read_line();
|
char *cwd= getenv("PWD");
|
||||||
args = lsh_split_line(line);
|
char *pwd=basename(cwd);
|
||||||
status = lsh_execute(args);
|
snprintf(shell_obj->prompt,sizeof(char) * 1024,"%s %s> ",shell_obj->pre_prompt,pwd);
|
||||||
|
free(pwd);
|
||||||
|
printf("%s",shell_obj->prompt);
|
||||||
|
line = lsh_read_line();
|
||||||
|
args = lsh_split_line(line);
|
||||||
|
status = lsh_execute(args);
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
free(args);
|
free(args);
|
||||||
} while (status);
|
} while (status);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
|
//set sh level
|
||||||
|
char *shlvl = getenv("SHLVL");
|
||||||
|
if(shlvl != NULL){
|
||||||
|
int shlvli = atoi(shlvl);
|
||||||
|
shlvli++;
|
||||||
|
sprintf(shlvl,"%d",shlvli);
|
||||||
|
setenv("SHLVL",shlvl,1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Set The prompt
|
||||||
SHELL_OB shell_obj;
|
SHELL_OB shell_obj;
|
||||||
shell_obj.prompt = (char *)malloc(sizeof(char) * 1024);
|
shell_obj.prompt = (char *)malloc(sizeof(char) * 1024);
|
||||||
|
if (!shell_obj.prompt){
|
||||||
|
fprintf(stderr, "lsh: allocation error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
shell_obj.pre_prompt = (char *)malloc(sizeof(char) * 1024);
|
||||||
|
if (!shell_obj.pre_prompt){
|
||||||
|
fprintf(stderr, "lsh: allocation error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
//get the hostname
|
//get the hostname
|
||||||
char *hostname = (char *)malloc(sizeof(char) * 1024);
|
char *hostname = (char *)malloc(sizeof(char) * 1024);
|
||||||
|
if (!hostname) {
|
||||||
|
fprintf(stderr, "lsh: allocation error\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
gethostname(hostname,sizeof(char) * 1024);
|
gethostname(hostname,sizeof(char) * 1024);
|
||||||
|
|
||||||
snprintf(shell_obj.prompt,sizeof(char) * 1024,"%s@%s> ",getenv("USER"),hostname);
|
//set the pre_prompt
|
||||||
|
snprintf(shell_obj.pre_prompt,sizeof(char) * 1024,"%s@%s",getenv("USER"),hostname);
|
||||||
|
free(hostname);
|
||||||
|
|
||||||
lsh_loop(&shell_obj);
|
lsh_loop(&shell_obj);
|
||||||
|
|
||||||
free(hostname);
|
|
||||||
free(shell_obj.prompt);
|
free(shell_obj.prompt);
|
||||||
|
free(shell_obj.pre_prompt);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user