Start of custom prompt

This commit is contained in:
Melefo
2020-05-24 17:50:08 +02:00
parent dc0e381305
commit d4e26d7a95
2 changed files with 61 additions and 2 deletions
+1
View File
@@ -36,6 +36,7 @@ env_t *create_env(char **env)
}
envt->window = NULL;
envt->alias = NULL;
envt->vars = my_setenv(envt->vars, "prompt", "%U%m%u:%B%~%b%# ");
return (envt);
}
+60 -2
View File
@@ -15,6 +15,7 @@
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <limits.h>
const builtin builtins[] = {
{"env", &builtin_env},
@@ -58,12 +59,69 @@ int prompt_run(char *cmd, redirection *inout[2], env_t *env, redirection *cmds)
return (ret);
}
char *get_prompt_value(char c, env_t *env)
{
static char value[PATH_MAX];
switch (c)
{
case '/':
case '~':
return (getcwd(value, sizeof(value)));
case 'n':
case 'N':
return (getlogin());
case 'm':
case 'M':
gethostname(value, sizeof(value));
return (value);
case '%':
return "%";
case 'l':
return (ttyname(STDIN_FILENO));
case '#':
if (!geteuid())
return ("#");
return (">");
case '?':
return (my_getenv(env->vars, "?"));
}
return (NULL);
}
char *parse_prompt(char *prompt, env_t *env)
{
char *value = NULL;
int value_length = 0;
int length = strlen(prompt);
for (int i = 0; prompt[i]; i++) {
if (prompt[i] != '%')
continue;
value = get_prompt_value(prompt[i + 1], env);
if (!value)
continue;
value_length = strlen(value);
prompt = realloc(prompt, sizeof(char) * (value_length + length + 2));
if (!prompt)
return (NULL);
rm_n_char(&prompt[i], 2);
insert_substring(prompt, value, i + 1);
i += value_length - 1;
length += value_length - 2;
}
return (prompt);
}
void prompt_prepare(buffer_t *buffer, env_t *env)
{
char *prompt = my_getenv(env->vars, "PS1");
char *prompt = my_getenv(env->vars, "prompt");
if (!prompt)
prompt = "$ ";
prompt = "";
prompt = strdup(prompt);
prompt = parse_prompt(prompt, env);
printf("%s", prompt);
buffer->startx = strlen(prompt);
free(prompt);
}