Skip to content

Commit

Permalink
Works, working on norminette
Browse files Browse the repository at this point in the history
  • Loading branch information
¨Roman committed Jun 3, 2024
1 parent 6ba4294 commit 144be92
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 61 deletions.
144 changes: 83 additions & 61 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,76 +23,100 @@ void handle_args(t_token **tokens, char **argv)
i++;
}
}

// to replace ft_split(' ') in main()
char **parse_input(char *input)
void handle_quotes(char **tokens, int *pos, char **input)
{
int pos;
char *token;
char **tokens;
char *delim;
int bufsize;

pos = 0;
bufsize = 64;
delim = " ";
tokens = malloc(bufsize * sizeof(char *));
if (!tokens)
{
ft_printf("@maxishell: allocation error\n");
exit(EXIT_FAILURE);
}
while (*input)
{
while (*input && ft_strchr(delim, *input))
input++;
if (*input == '\0')
break;
if (*input == '\'' || *input == '\"')
char quote = *(*input)++;
char *start = *input;

while (**input && **input != quote)
(*input)++;

if (**input == '\0')
{
char quote;
char *start;

quote = *input++;
start = input;
while (*input && *input != quote)
input++;
if (*input == '\0')
{
ft_printf("@maxishell: syntax error: unmatched %c\n", quote);
printf("@maxishell: syntax error: unmatched %c\n", quote);
free(tokens);
return (NULL);
}
*input = '\0';
token = ft_strndup(start, input - start);
input++;
if (ft_strlen(token) > 0)
tokens[pos++] = token;
tokens = NULL;
return;
}

else if (*input == '|' || *input == '<' || *input == '>')
**input = '\0';
tokens[*pos] = strndup(start, *input - start);
(*input)++;

if (strlen(tokens[*pos]) > 0)
(*pos)++;
}

void handle_special_chars(char **tokens, int *pos, char **input)
{
if ((**input == '<' && *(*input + 1) == '<') || (**input == '>' && *(*input + 1) == '>'))
{
tokens[pos++] = ft_strndup(input, 1);
if ((*input == '<' && *(input + 1) == '<') || (*input == '>' && *(input + 1) == '>'))
{
tokens[pos - 1] = ft_strndup(input, 2);
input++;
}
input++;
tokens[*pos] = strndup(*input, 2);
(*input) += 2;
}
else
{
char *start;
tokens[*pos] = strndup(*input, 1);
(*input)++;
}
(*pos)++;
}

start = input;
while (*input && !ft_strchr(delim, *input) && *input != '|' && *input != '<' && *input != '>' && *input != '\"' && *input != '\'')
input++;
token = ft_strndup(start, input - start);
tokens[pos++] = token;
void handle_regular_chars(char **tokens, int *pos, char **input, char *delim)
{
char *start = *input;

while (**input && !strchr(delim, **input) && **input != '|' && **input != '<' && **input != '>' && **input != '\"' && **input != '\'')
(*input)++;

tokens[*pos] = strndup(start, *input - start);
(*pos)++;
}

char **parse_input(char *input)
{
int pos = 0;
int bufsize = 64;
char *delim = " ";
char **tokens = malloc(bufsize * sizeof(char *));
if (!tokens)
{
printf("@maxishell: allocation error\n");
exit(EXIT_FAILURE);
}
}
tokens[pos] = NULL;
return (tokens);

while (*input)
{
while (*input && strchr(delim, *input))
input++;

if (*input == '\0')
break;

if (*input == '\'' || *input == '\"')
handle_quotes(tokens, &pos, &input);
else if (*input == '|' || *input == '<' || *input == '>')
handle_special_chars(tokens, &pos, &input);
else
handle_regular_chars(tokens, &pos, &input, delim);

if (tokens == NULL)
return NULL; // Return if there's a syntax error

if (pos >= bufsize)
{
bufsize += 64;
tokens = realloc(tokens, bufsize * sizeof(char *));
if (!tokens)
{
printf("@maxishell: allocation error\n");
exit(EXIT_FAILURE);
}
}
}

tokens[pos] = NULL;
return tokens;
}
char *generate_prompt()
{
Expand All @@ -114,7 +138,6 @@ char *generate_prompt()
if (!prompt)
exit(EXIT_FAILURE);
ft_strcpy(prompt, s1);
printf("%s\n", prompt);
ft_strcat(prompt, user);
ft_strcat(prompt, s2);
ft_strcat(prompt, pwd);
Expand Down Expand Up @@ -148,7 +171,6 @@ int main(void)
if (parsed_text != NULL)
{
printf("\033[31m@maxishell: command not found: %s\033[0m\n", parsed_text[0]);
fflush(stdout);
handle_args(&tokens, parsed_text);
arr = list_to_array(tokens);
print_stack(&tokens);
Expand Down
52 changes: 52 additions & 0 deletions utils/.maxishell_history
Original file line number Diff line number Diff line change
Expand Up @@ -2544,3 +2544,55 @@ f
rt
hyth
rtgjlrtn
"test"
test
:test"
:test"
:test"
j
kkk
:test"
hgh<ghg
guhg ghutgh
./gtgi
"fgj"
"gug
"gug
"gug
"gug"
tet"
"test
test
"test
test "
test"
"test"
test
"test
test"
or4ijf<ioergj
"'test'"
"'test'"
'"test"'
"'test
test'"
gh
gh
gj
gj
kjjjjjjjjjjjjjjj
"test"
"test
test"
test
'test
test'
''
||
|fjgj|
make re
./minishell
||
./minishell
make re
./minishell

0 comments on commit 144be92

Please sign in to comment.