/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 16:05:11 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:52:29 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
char *get_cmd_path(char *cmd, t_env *env);
int execute_cmd(t_cmd *cmd, t_env **env);
char *format_string(char *str, t_env *env);
-
+int set_return_code(int return_code, t_env **env);
#endif
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
-/* Updated: 2025/01/10 14:36:55 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 15:20:42 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
+#include <stdio.h>
int echo(char **av)
{
if (av[1] == NULL || av[1][0] == '\0')
{
write(1, "\n", 1);
- return (1);
+ return (0);
}
if (ft_strncmp(av[1], "-n", 3) == 0)
{
{
while (env != NULL)
{
+ if (strchr(env->name, '?'))
+ {
+ env = env->next;
+ continue;
+ }
printf("%s", env->name);
printf("=%s\n", env->value);
env = env->next;
i = 0;
while (av[++i])
{
+ if (ft_strchr(av[i], '?'))
+ return (1);
current = *env;
prev = NULL;
while (current)
{
tmp = ft_strchr(av[i], '=');
*tmp = '\0';
+ if (ft_strchr(av[i], '?'))
+ return (1);
current = check_existing(*env, av[i]);
if (current)
free(current->value);
}
}
return (0);
-}
\ No newline at end of file
+}
+
+void set_return_code(int return_code, t_env **env)
+{
+ t_env *cur;
+
+ cur = check_existing(*env, "?");
+ if (cur)
+ free(cur->value);
+ else
+ {
+ cur = env_new(ft_strdup("?"));
+ cur->next = *env;
+ *env = cur;
+ }
+ cur->value = ft_itoa(return_code);
+}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:22:36 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:41:39 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
cur = env;
while (cur != NULL)
{
+ if (ft_strchr(cur->name, '?'))
+ {
+ cur = cur->next;
+ continue;
+ }
size++;
cur = cur->next;
}
cur = env;
while (i < size)
{
+ if (ft_strchr(cur->name, '?'))
+ cur = cur->next;
result[i] = get_var_assign(cur);
cur = cur->next;
i++;
result = ft_strjoin(left_side, cur->value);
free(left_side);
return (result);
-}
\ No newline at end of file
+}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2024/12/17 19:31:54 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 18:06:17 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
static void append_slice(char **dst, char *src, int start, int end);
static void append_var(char **dst, char *src, int *pos, t_env *env);
-enum e_format_mode
-{
- LITERAL = 1,
- VARIABLE = 2,
-};
-
char *format_string(char *str, t_env *env)
{
char *result;
int pos;
int start;
- int mode;
+ int is_literal;
pos = 0;
start = 0;
- mode = 0;
+ is_literal = 0;
result = NULL;
if (str == NULL)
return (NULL);
{
append_slice(&result, str, start, pos);
start = pos + 1;
- mode ^= LITERAL;
+ is_literal = !is_literal;
}
- if (str[pos] == '"' && !(mode & LITERAL))
+ if (str[pos] == '"' && !is_literal)
{
append_slice(&result, str, start, pos);
start = pos + 1;
}
- if (str[pos] == '$' && !(mode & LITERAL))
+ if (str[pos] == '$' && !is_literal)
{
append_slice(&result, str, start, pos);
append_var(&result, str, &pos, env);
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */
-/* Updated: 2024/12/17 19:26:42 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 15:22:40 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
if (init())
return (1);
getenvlst(&env, envp);
+ set_return_code(0, &env);
repl("Minishell $ ", &env);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/11 16:01:44 by chuhlig ### ########.fr */
+/* Updated: 2025/01/14 14:53:29 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
if (lines)
{
print_ast(lines->content);
- eval(lines->content, env);
+ set_return_code(eval(lines->content, env), env);
}
free(input);
}