- 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
|
||||
#define shell_helper_functions
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -51,4 +53,38 @@ char *str_replace(char *orig, char *rep, char *with) {
|
||||
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
|
||||
|
||||
@@ -44,9 +44,18 @@ int lsh_cd(char **args)
|
||||
if (args[1] == NULL) {
|
||||
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
|
||||
} else {
|
||||
if (chdir(args[1]) != 0) {
|
||||
int err = chdir(args[1]);
|
||||
if (err != 0) {
|
||||
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;
|
||||
}
|
||||
@@ -54,7 +63,7 @@ int lsh_cd(char **args)
|
||||
int lsh_help(char **args)
|
||||
{
|
||||
int i;
|
||||
printf("Stephen Brennan's LSH\n");
|
||||
printf("FOX SHELL\n");
|
||||
printf("Type program names and arguments, and hit enter.\n");
|
||||
printf("The following are built in:\n");
|
||||
|
||||
|
||||
@@ -46,10 +46,12 @@ char *lsh_read_line(void)
|
||||
}
|
||||
|
||||
char *handle_vars(char *token){
|
||||
token=&token[0];
|
||||
if (token[0] == '$'){
|
||||
token=&token[1];
|
||||
token=getenv(token);
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
36
src/main.c
36
src/main.c
@@ -2,11 +2,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "helper_functions.c"
|
||||
#include "lsh_main_func.c"
|
||||
#include "lsh_builtins.c"
|
||||
|
||||
typedef struct{
|
||||
char *prompt;
|
||||
char *pre_prompt;
|
||||
} SHELL_OB;
|
||||
|
||||
void lsh_loop(SHELL_OB *shell_obj)
|
||||
@@ -16,6 +18,11 @@ void lsh_loop(SHELL_OB *shell_obj)
|
||||
int status;
|
||||
|
||||
do {
|
||||
//get pwd
|
||||
char *cwd= getenv("PWD");
|
||||
char *pwd=basename(cwd);
|
||||
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);
|
||||
@@ -27,18 +34,43 @@ void lsh_loop(SHELL_OB *shell_obj)
|
||||
}
|
||||
|
||||
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_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
|
||||
char *hostname = (char *)malloc(sizeof(char) * 1024);
|
||||
if (!hostname) {
|
||||
fprintf(stderr, "lsh: allocation error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
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);
|
||||
|
||||
free(hostname);
|
||||
free(shell_obj.prompt);
|
||||
free(shell_obj.pre_prompt);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user