parser should be done

This commit is contained in:
Clément Le Bihan
2020-05-21 20:22:33 +02:00
parent 940f1cbc57
commit 8e2504bb92
3 changed files with 28 additions and 39 deletions
+10 -30
View File
@@ -8,23 +8,6 @@
#include <malloc.h>
#include "parser.h"
char *get_var_value(char *var, env_t *env)
{
char *value;
if (!var)
return (NULL);
value = my_getenv(env->env, var);
if (value)
return (value);
value = my_getenv(env->vars, var);
if (value)
return (value);
printf("%s: Undefined variable.\n", var);
env->vars = my_setenv(env->vars, "?", "1");
return (NULL);
}
char *strcat_realloc(char *dest, char *src)
{
if (dest) {
@@ -44,7 +27,6 @@ char *strcat_realloc(char *dest, char *src)
char *add_to_buffer(char *buffer, char *ptr, int nb, env_t *env)
{
char *new;
char *tmp;
if (nb <= 0)
return (buffer);
@@ -54,9 +36,9 @@ char *add_to_buffer(char *buffer, char *ptr, int nb, env_t *env)
new[nb] = '\0';
if (env) {
remove_inhibitors_symbols_n_limit(new, nb);
tmp = process_vars(new, env);
if (tmp)
new = tmp;
new = process_vars(new, env);
if (!new)
return (NULL);
}
buffer = strcat_realloc(buffer, new);
free(new);
@@ -80,11 +62,11 @@ char *substring(char *string, int position, int length)
char *pointer = malloc(length + 1);
int i;
if(pointer == NULL)
if (pointer == NULL)
return (NULL);
for(i = 0; i < length; i++)
*(pointer + i) = *((string + position - 1) + i);
*(pointer + i) = '\0';
for (i = 0; i < length; i++)
*(pointer + i) = *((string + position - 1) + i);
*(pointer + i) = '\0';
return (pointer);
}
@@ -93,12 +75,10 @@ void insert_substring(char *dest, char *src, int position)
char *f;
char *e;
int length;
length = strlen(dest);
f = substring(dest, 1, position - 1);
e = substring(dest, position, length - position + 1);
length = strlen(dest);
f = substring(dest, 1, position - 1);
e = substring(dest, position, length - position + 1);
strcpy(dest, "");
strcat(dest, f);
free(f);
+17
View File
@@ -9,6 +9,23 @@
#include <stdio.h>
#include <malloc.h>
char *get_var_value(char *var, env_t *env)
{
char *value;
if (!var)
return (NULL);
value = my_getenv(env->env, var);
if (value)
return (value);
value = my_getenv(env->vars, var);
if (value)
return (value);
printf("%s: Undefined variable.\n", var);
env->vars = my_setenv(env->vars, "?", "1");
return (NULL);
}
int get_var_name(char *ptr, char **name)
{
int length = 0;
+1 -9
View File
@@ -33,19 +33,11 @@ const builtin builtins[] = {
int prompt_run(char *cmd, redirection *inout[2], env_t *env)
{
//char **argv = get_argv(cmd);
char **argv = parse_input(cmd, env);
if (!argv) {
//perror(SHELL_NAME);
if (!argv) {
return (0);
}
for (int i = 0; argv[i]; i++) {
printf("'%s'\n", argv[i]);
}
return (0);
for (int i = 0; argv[i]; i++)
printf("argv[%i] : %s\n", i , argv[i]);
if (!argv[0])
return (0);
argv = globbing(argv);