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

Go to the source code of this file.

Functions

t_tokentokenise (char *str)
 
char * handle_special_chars (char *str, t_token **tokens)
 
t_tokennew_token (char *value, t_token_type type)
 
void append_token (t_token **tokens, t_token *new_token)
 
char * handle_phrase (char *str, t_token **tokens)
 

Function Documentation

◆ append_token()

void append_token ( t_token **  tokens,
t_token new_token 
)

Definition at line 85 of file tokeniser.c.

86{
87 t_token *curr;
88
89 if (!*tokens)
90 *tokens = new_token;
91 else
92 {
93 curr = *tokens;
94 while (curr->next)
95 curr = curr->next;
96 curr->next = new_token;
97 }
98}
struct s_token * next
Definition tokens.h:45
t_token * new_token(char *value, t_token_type type)
Definition tokeniser.c:67

References new_token(), and s_token::next.

Referenced by append_word_if_valid(), and handle_special_chars().

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

◆ handle_phrase()

char * handle_phrase ( char *  str,
t_token **  tokens 
)

Definition at line 100 of file tokeniser.c.

101{
102 char *start;
103 char quote_flag;
104 char quote_type;
105
106 start = str;
107 quote_flag = 0;
108 quote_type = 0;
109 while (*str)
110 {
111 if (!quote_flag && (*str == '\'' || *str == '\"'))
112 {
113 quote_flag = 1;
114 quote_type = *str;
115 }
116 else if (quote_flag && *str == quote_type)
117 quote_flag = 0;
118 if (!quote_flag && ft_strchr(" \t\n\r\v\f<|>", *str) != NULL)
119 break ;
120 str++;
121 }
122 append_word_if_valid(start, str, tokens);
123 return (str);
124}
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
void append_word_if_valid(char *start, char *str, t_token **tokens)

References append_word_if_valid(), and ft_strchr().

Referenced by tokenise().

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

◆ handle_special_chars()

char * handle_special_chars ( char *  str,
t_token **  tokens 
)

Definition at line 39 of file tokeniser.c.

40{
41 if (*str == '<')
42 {
43 if (*(str + 1) == '<')
44 {
45 append_token(tokens, new_token("<<", REDIR_HEREDOC));
46 (str)++;
47 }
48 else
49 append_token(tokens, new_token("<", REDIR_IN));
50 }
51 else if (*str == '>')
52 {
53 if (*(str + 1) == '>')
54 {
55 append_token(tokens, new_token(">>", REDIR_APPEND));
56 (str)++;
57 }
58 else
59 append_token(tokens, new_token(">", REDIR_OUT));
60 }
61 else if (*str == '|')
62 append_token(tokens, new_token("|", PIPE));
63 (str)++;
64 return (str);
65}
void append_token(t_token **tokens, t_token *new_token)
Definition tokeniser.c:85
@ REDIR_IN
Definition tokens.h:34
@ REDIR_HEREDOC
Definition tokens.h:37
@ PIPE
Definition tokens.h:32
@ REDIR_OUT
Definition tokens.h:35
@ REDIR_APPEND
Definition tokens.h:36

References append_token(), new_token(), PIPE, REDIR_APPEND, REDIR_HEREDOC, REDIR_IN, and REDIR_OUT.

Referenced by tokenise().

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

◆ new_token()

t_token * new_token ( char *  value,
t_token_type  type 
)

Definition at line 67 of file tokeniser.c.

68{
69 t_token *token;
70
71 token = malloc(sizeof(t_token));
72 if (!token)
73 return (NULL);
74 token->data = ft_strdup(value);
75 if (!token->data)
76 {
77 free(token);
78 return (NULL);
79 }
80 token->type = type;
81 token->next = NULL;
82 return (token);
83}
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
t_token_type type
Definition tokens.h:43
char * data
Definition tokens.h:44

References s_token::data, ft_strdup(), s_token::next, and s_token::type.

Referenced by append_token(), append_word_if_valid(), finalize_token(), handle_special_chars(), and process_single_token().

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

◆ tokenise()

t_token * tokenise ( char *  str)

Definition at line 22 of file tokeniser.c.

23{
24 t_token *tokens;
25
26 tokens = NULL;
27 while (*str)
28 {
29 while (*str && ft_strchr(" \t\n\r\v\f", *str) != NULL)
30 str++;
31 if (ft_strchr("<|>", *str) != NULL)
32 str = handle_special_chars(str, &tokens);
33 else
34 str = handle_phrase(str, &tokens);
35 }
36 return (tokens);
37}
char * handle_phrase(char *str, t_token **tokens)
Definition tokeniser.c:100
char * handle_special_chars(char *str, t_token **tokens)
Definition tokeniser.c:39

References ft_strchr(), handle_phrase(), and handle_special_chars().

Referenced by main_loop().

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