maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
input_checker.c File Reference
#include "tokens.h"
Include dependency graph for input_checker.c:

Go to the source code of this file.

Functions

void free_op_strings (t_loop_data *data, char *tail, char *new)
 
int check_operators (t_loop_data *loop_data)
 
int check_redirections (const char *str)
 
int check_open_quotes (const char *str)
 
int input_error_checks (t_loop_data *loop_data)
 

Function Documentation

◆ check_open_quotes()

int check_open_quotes ( const char *  str)

Definition at line 76 of file input_checker.c.

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}

Referenced by input_error_checks().

Here is the caller graph for this function:

◆ check_operators()

int check_operators ( t_loop_data loop_data)

Definition at line 22 of file input_checker.c.

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}
void free_op_strings(t_loop_data *data, char *tail, char *new)
char * ft_strcat_const(const char *dest, const char *src)
char * trimmed_input
Definition tokens.h:61

References free_op_strings(), ft_strcat_const(), and s_loop_data::trimmed_input.

Referenced by input_error_checks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_redirections()

int check_redirections ( const char *  str)

Definition at line 51 of file input_checker.c.

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}
int valid_operator(const char **str)

References valid_operator().

Referenced by input_error_checks().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_op_strings()

void free_op_strings ( t_loop_data data,
char *  tail,
char *  new 
)

Definition at line 15 of file input_checker.c.

16{
17 free(data->trimmed_input);
18 free(new);
19 free(tail);
20}

References s_loop_data::trimmed_input.

Referenced by check_operators().

Here is the caller graph for this function:

◆ input_error_checks()

int input_error_checks ( t_loop_data loop_data)

Definition at line 96 of file input_checker.c.

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)
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

References check_open_quotes(), check_operators(), check_redirections(), ft_printf(), and s_loop_data::trimmed_input.

Referenced by main_loop().

Here is the call graph for this function:
Here is the caller graph for this function: