maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
input_checker.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* input_checker.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/11 16:51:08 by rocky #+# #+# */
9/* Updated: 2024/06/11 16:53:40 by rocky ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14
15void free_op_strings(t_loop_data *data, char *tail, char *new)
16{
17 free(data->trimmed_input);
18 free(new);
19 free(tail);
20}
21
23{
24 char *input;
25 char *tail;
26 char *new_input;
27 char *final_input;
28
29 input = loop_data->trimmed_input;
30 if (*input == '&' || *input == '|')
31 return (1);
32 while (*input)
33 {
34 if (*input == '|' && *(input + 1) == '\0')
35 {
36 tail = readline("> ");
37 if (tail == NULL)
38 break ;
39 new_input = ft_strcat_const(loop_data->trimmed_input, " ");
40 final_input = ft_strcat_const(new_input, tail);
41 free_op_strings(loop_data, tail, new_input);
42 loop_data->trimmed_input = final_input;
43 free(final_input);
44 break ;
45 }
46 input++;
47 }
48 return (0);
49}
50
51int check_redirections(const char *str)
52{
53 int single_quotes;
54 int double_quotes;
55
56 single_quotes = 0;
57 double_quotes = 0;
58 while (*str)
59 {
60 if (*str == '\'')
61 single_quotes++;
62 if (*str == '\"')
63 double_quotes++;
64 if ((!(single_quotes % 2) && !(double_quotes % 2))
65 && (*str == '>' || *str == '<'))
66 {
67 if (!valid_operator(&str))
68 return (1);
69 }
70 else
71 str++;
72 }
73 return (0);
74}
75
76int check_open_quotes(const char *str)
77{
78 int single_quote_open;
79 int double_quote_open;
80
81 single_quote_open = 0;
82 double_quote_open = 0;
83 while (*str)
84 {
85 if (*str == '\'' && !double_quote_open)
86 {
87 single_quote_open = !single_quote_open;
88 }
89 else if (*str == '"' && !single_quote_open)
90 double_quote_open = !double_quote_open;
91 str++;
92 }
93 return (single_quote_open || double_quote_open);
94}
95
97{
98 const char *str = loop_data->trimmed_input;
99
100 if (check_redirections(str))
101 ft_printf("Input error: invalid redirection.\n");
102 else if (check_operators(loop_data))
103 ft_printf("Input error: invalid operator.\n");
104 else if (check_open_quotes(str))
105 ft_printf("Input error: open quote.\n");
106 else
107 return (0);
108 return (1);
109}
int check_redirections(const char *str)
void free_op_strings(t_loop_data *data, char *tail, char *new)
int input_error_checks(t_loop_data *loop_data)
int check_open_quotes(const char *str)
int check_operators(t_loop_data *loop_data)
int ft_printf(const char *format,...)
Definition ft_printf.c:37
char * ft_strcat_const(const char *dest, const char *src)
char * trimmed_input
Definition tokens.h:61
int valid_operator(const char **str)