add ask_command() and minishell_error()
This commit is contained in:
parent
fa1301d41f
commit
2da8cbabdd
5 changed files with 82 additions and 17 deletions
4
Makefile
4
Makefile
|
@ -3,9 +3,9 @@ NAME = minishell
|
||||||
# src directory
|
# src directory
|
||||||
SRCS = src/
|
SRCS = src/
|
||||||
# include directory
|
# include directory
|
||||||
INCLUDES = include/
|
INCLUDES = include/ libtf/
|
||||||
# .c files in src/ without the extension
|
# .c files in src/ without the extension
|
||||||
CODE = main
|
CODE = main ask_command error
|
||||||
# directories to 'make'
|
# directories to 'make'
|
||||||
LIBRARIES = libtf
|
LIBRARIES = libtf
|
||||||
# .a files to include
|
# .a files to include
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
|
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
|
||||||
/* Updated: 2024/04/23 14:54:39 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/23 16:01:17 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -14,13 +14,26 @@
|
||||||
# define INCLUDE_H
|
# define INCLUDE_H
|
||||||
|
|
||||||
# include <stdbool.h>
|
# include <stdbool.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <errno.h>
|
||||||
|
# include <string.h>
|
||||||
|
# include "libtf.h"
|
||||||
|
|
||||||
|
///// ASK COMMAND /////
|
||||||
|
|
||||||
|
// Show the prompt and ask a command to the user.
|
||||||
|
// Return the command.
|
||||||
|
const char *ask_command(t_memclass mc);
|
||||||
|
|
||||||
|
///// PARSE_COMMAND /////
|
||||||
|
|
||||||
// Represents a call to a program (the program name and its arguments)
|
// Represents a call to a program (the program name and its arguments)
|
||||||
typedef struct s_call
|
typedef struct s_call
|
||||||
{
|
{
|
||||||
char *program; // path of the program to call.
|
char *program; // path of the program to call.
|
||||||
int argc; // number of arguments given (including the program's name)
|
int argc; // number of arguments given
|
||||||
char **argv; // arguments given (including the program's name)
|
const char *const *argv; // arguments given
|
||||||
|
// (argc and argv must include the program name)
|
||||||
} t_call;
|
} t_call;
|
||||||
|
|
||||||
// Represents a command given by the user.
|
// Represents a command given by the user.
|
||||||
|
@ -32,15 +45,19 @@ typedef struct s_command
|
||||||
int output_fd; // fd to use with '>' redirection (1 by default)
|
int output_fd; // fd to use with '>' redirection (1 by default)
|
||||||
} t_command;
|
} t_command;
|
||||||
|
|
||||||
// Show the prompt and ask a command to the user.
|
|
||||||
// Return the command.
|
|
||||||
char *ask_command(void);
|
|
||||||
|
|
||||||
// Return the t_command representing the command given by the user.
|
// Return the t_command representing the command given by the user.
|
||||||
// If error, return a t_command wth the value .error = true.
|
// If error, return a t_command wth the value .error = true.
|
||||||
t_command parse_command(char *command);
|
t_command parse_command(const char *command);
|
||||||
|
|
||||||
|
///// EXECUTE COMMAND /////
|
||||||
|
|
||||||
// Execute the command given. Return the exit status.
|
// Execute the command given. Return the exit status.
|
||||||
int execute_command(t_command command);
|
int execute_command(t_command command);
|
||||||
|
|
||||||
|
///// ERROR /////
|
||||||
|
|
||||||
|
// Call to show an error.
|
||||||
|
// If msg == "errno", use strerror(errno)
|
||||||
|
void minishell_error(const char *msg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
25
src/ask_command.c
Normal file
25
src/ask_command.c
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ask_command.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/23 15:03:23 by mcolonna #+# #+# */
|
||||||
|
/* Updated: 2024/04/23 15:56:21 by mcolonna ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "include.h"
|
||||||
|
|
||||||
|
const char *ask_command(t_memclass mc)
|
||||||
|
{
|
||||||
|
const char *r;
|
||||||
|
|
||||||
|
print_str(minishell_error, 1, "( ^.^)> ");
|
||||||
|
r = read_line(minishell_error, mc, 0);
|
||||||
|
if (r)
|
||||||
|
return (r);
|
||||||
|
else
|
||||||
|
return ("");
|
||||||
|
}
|
22
src/error.c
Normal file
22
src/error.c
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* error.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/23 15:51:56 by mcolonna #+# #+# */
|
||||||
|
/* Updated: 2024/04/23 15:56:49 by mcolonna ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "include.h"
|
||||||
|
|
||||||
|
void minishell_error(const char *msg)
|
||||||
|
{
|
||||||
|
if (str_eq(msg, "errno"))
|
||||||
|
msg = strerror(errno);
|
||||||
|
print_str(err_remember, 2, "minishell: ");
|
||||||
|
print_str(err_remember, 2, msg);
|
||||||
|
err_get();
|
||||||
|
}
|
11
src/main.c
11
src/main.c
|
@ -6,26 +6,27 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
|
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
|
||||||
/* Updated: 2024/04/23 14:55:04 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/23 16:00:53 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "include.h"
|
#include "include.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
t_memclass mc;
|
||||||
t_command command;
|
t_command command;
|
||||||
int errorstatus;
|
int errorstatus;
|
||||||
|
|
||||||
(void)argc;
|
|
||||||
(void)argv;
|
|
||||||
errorstatus = 0;
|
errorstatus = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
command = parse_command(ask_command());
|
mc = mem_newclass(minishell_error);
|
||||||
|
command = parse_command(ask_command(mc));
|
||||||
if (command.error)
|
if (command.error)
|
||||||
continue ;
|
continue ;
|
||||||
errorstatus = execute_command(command);
|
errorstatus = execute_command(command);
|
||||||
|
mem_freeall(mc);
|
||||||
}
|
}
|
||||||
return (errorstatus);
|
return (errorstatus);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue