aboutsummaryrefslogtreecommitdiff
path: root/include/minishell.h
blob: aebfbf22fd943a38219e83f3b84254aea8cee672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   minishell.h                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/26 15:33:51 by cacharle          #+#    #+#             */
/*   Updated: 2020/07/16 09:17:45 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef MINISHELL_H
# define MINISHELL_H
#include <stdio.h>  // for debugging - dont remove

/*
** \file   minishell.h
** \brief  Common header
*/

# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <fcntl.h>
# include <dirent.h>
# include <sys/wait.h>
# include <signal.h>
# include <sys/stat.h>
# include <stdbool.h>

# include "libft.h"
# include "libft_ht.h"
# include "libft_lst.h"
# include "libft_util.h"
# include "libft_vec.h"
# include "libft_dstr.h"

/*
** \brief  Value of pipe entry if closed
*/

# define PIPE_CLOSED -1

# define BUILTIN_NOT_FOUND -2

typedef t_ftht*			t_path;
typedef t_ftvec*		t_env;

extern int		g_last_status_code;
extern char		*g_basename;

/*
** path.c
*/

t_path					path_update(t_path path, char *path_var);

/*
** env.c
*/

// FIXME refactor env code
t_env					env_from_array(char **envp);
int						env_keycmp(char *var, char *key);
char					*env_search(t_env env, char *key);
int						env_search_index(t_env env, char *key);
char					*env_search_first_match(t_env env, const char *haystack);
char					*env_export(t_env env, char *key, char *value);
char					*env_export_full(t_env env, char *variable);

/*
** builtin*.c - directory with all builtin commands
*/

/*
** \brief       Type of a builtin main function
*/

typedef int	(*t_builtin_func)(char **argv, t_env env);

/*
** \brief       Entry of builtin lookup array
** \param name  Executable name of builtin
** \param func  Associated function
*/

typedef struct
{
	char				*name;
	t_builtin_func		func;
}						t_builtin_entry;

t_builtin_entry			*builtin_search_func(char *name);

int						builtin_echo(char **argv, t_env env);
int						builtin_cd(char **argv, t_env env);
int						builtin_pwd(char **argv, t_env env);
int						builtin_export(char **argv, t_env env);
int						builtin_unset(char **argv, t_env env);
int						builtin_env(char **argv, t_env env);
int						builtin_exit(char **argv, t_env env);

/*
** preprocess.c
*/

char					**preprocess(t_ftlst **tokens, t_env env);
char					*preprocess_filename(t_ftlst **tokens, t_env env);

/*
** error.c
*/

enum					e_error
{
	ERROR_AMBIGUOUS_REDIR,
	ERROR_OPEN,
	ERROR_CMD_NOT_FOUND,
	ERROR_SYNTAX,
	ERROR_CMD_FOUND_ERROR,
};

/*
** \brief         Error specification
** \param id      Error type
** \param status  Status code returned
** \param msg     Error message to print, if is NULL, use strerror as a msg
*/

typedef struct
{
	enum e_error		id;
	int					status;
	char				*msg;
}						t_error;

void					error_eval_put(enum e_error id, char *content);
void					error_put_invalid_identifier(char *prefix, char *identifier);

/*
** signal.c
*/

void signal_sigint(int signum);
void signal_sigquit(int signum);
void signal_sigterm(int signum);

/*
** utils.c
*/

typedef int	(*t_directory_iter_func)(char*, struct dirent*, void*);

int		utils_directory_iter(
	char *dirname,
	void *param,
	t_directory_iter_func f
);

size_t	utils_var_end(char *name);
bool	utils_valid_identifier(char *name);
bool	utils_start_with_valid_identifier(char *name);

void print_prompt(void);

#endif