(feat): ajout d'un prompt affichable

This commit is contained in:
guamss 2023-06-28 19:29:32 +02:00
parent 6490d4de8c
commit 38c27fd583
5 changed files with 48 additions and 33 deletions

2
.gitmodules vendored
View File

@ -1,3 +1,3 @@
[submodule "lib/bozolib"]
url = git@git.chauvet.pro:starnakin/bozolib.git
url = https://git.chauvet.pro/starnakin/bozolib
path = lib/bozolib

View File

@ -36,12 +36,12 @@ int change_directory(char** args)
{
if(len(args)!=2)
{
printf("Mauvais arguments\n");
dprintf(2, "Mauvais arguments\n");
return 1;
}
if(chdir(args[1])!=0)
{
printf("Mauvais chemin : %s\n", args[1]);
dprintf(2, "Mauvais chemin : %s\n", args[1]);
return 1;
}
char cwd[PATH_MAX];
@ -67,20 +67,4 @@ int builtin_execute(cmd** input, char** env)
return exitcode;
}
int main()
{
cmd** tests;
tests = malloc(2*sizeof(cmd*));
cmd test;
test.executable = "cd";
test.args = malloc(3*sizeof(char*));
test.args[0] = "cd";
test.args[1] = "..";
test.args[2] = NULL;
test.fd_in = 0;
test.fd_out = 1;
tests[0] = &test;
tests[1] = NULL;
builtin_execute(tests, NULL);
return 0;
}

View File

@ -1,23 +1,12 @@
#include "prompt/prompt.h"
#include "./env/env.h"
#include <stdio.h>
int main(int ac, char **av, char **env)
{
lst** lst_env;
lst* current;
struct s_env* content;
(void) av;
(void) ac;
lst_env = env_init((const char **) env);
if (lst_env == NULL)
return (1);
current = *lst_env;
while (current != NULL)
{
content = current->content;
if (content == NULL)
printf("nil\n");
printf("%s=%s\n", content->key, content->value);
current = current->next;
}
printf(get_prompt(lst_env));
}

38
src/prompt/prompt.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include "../../lib/bozolib/bozolib.h"
#include "../env/env.h"
char* get_prompt(lst** env)
{
char* out;
int size;
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
dprintf(2, "chemin inconnu");
return NULL;
}
char* computer = get_env_variable(env, "COMPUTER");
char* user = get_env_variable(env, "USER");
char* home = get_env_variable(env, "HOME");
if (computer == NULL)
computer = "";
if (user == NULL)
user = "";
else if (strncmp(home, cwd, strlen(home)) == 0)
{
strcpy(cwd, "~");
strcat(cwd, cwd + strlen(home));
}
size = snprintf(NULL, 0, "%s@%s>%s $",user, computer, cwd);
out = malloc(size*sizeof(char));
if(out == NULL)
{
return NULL;
}
sprintf(out, "%s@%s > %s $",user, computer, cwd);
return out;
}

4
src/prompt/prompt.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include "../../lib/bozolib/bozolib.h"
char* get_prompt(lst** env);