finished parsing options, read loop done. few options are missing still, also have a break problem : read doesnt stop reading even when i dopnt want anymore input...

This commit is contained in:
Jack Halford 2017-01-22 18:23:21 +01:00
parent ee7ddc5e14
commit 8e3f84cef1
13 changed files with 147 additions and 48 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */
/* Updated: 2017/01/20 15:11:37 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:42:34 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,12 +6,12 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */
/* Updated: 2017/01/20 19:32:45 by jhalford ### ########.fr */
/* Updated: 2017/01/22 18:04:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTIN_H
# define BUILTIN_H
#ifndef BUILTIN_READ_H
# define BUILTIN_READ_H
# include "types.h"
# include "libft.h"
@ -31,14 +31,17 @@
# define READ_OPT_LU (1 << 10)
typedef struct s_read t_read;
typedef struct s_readopt t_readopt;
struct s_read
{
t_flag opts;
char delim;
int nchars;
char *prompt;
int timeout;
int fd;
char **names;
};
struct s_readopt
@ -52,4 +55,10 @@ extern t_readopt g_readtab[];
int builtin_read(const char *path, char *const av[], char *const envp[]);
int bt_read_getdelim(t_read *data, char *arg);
int bt_read_getnchars(t_read *data, char *arg);
int bt_read_getprompt(t_read *data, char *arg);
int bt_read_gettimeout(t_read *data, char *arg);
int bt_read_getfd(t_read *data, char *arg);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/01/11 17:17:16 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:05:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

@ -1 +1 @@
Subproject commit ab92f0e5d817c9d726a8ccf2f11c084ba446bbdf
Subproject commit 4c4977452745481166749b813e8db51dcf0caf44

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bt_read_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */
/* Updated: 2017/01/22 16:24:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtin_read.h"
int bt_read_getdelim(t_read *data, char *arg)
{
data->delim = *arg;
return (0);
}
int bt_read_getnchars(t_read *data, char *arg)
{
data->nchars = ft_atoi(arg);
return (0);
}
int bt_read_getprompt(t_read *data, char *arg)
{
data->prompt = arg;
return (0);
}
int bt_read_gettimeout(t_read *data, char *arg)
{
data->timeout = ft_atoi(arg);
return (0);
}
int bt_read_getfd(t_read *data, char *arg)
{
data->fd = ft_atoi(arg);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
/* Updated: 2017/01/20 19:32:44 by jhalford ### ########.fr */
/* Updated: 2017/01/22 18:21:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,30 +14,31 @@
t_readopt g_readtab[] =
{
{'a', READ_OPT_LA, NULL},
{'d', READ_OPT_LD, NULL},
{'e', READ_OPT_LE, NULL},
{'i', READ_OPT_LI, NULL},
{'n', READ_OPT_LN, NULL},
{'N', READ_OPT_UN, NULL},
{'p', READ_OPT_LP, NULL},
/* {'a', READ_OPT_LA, NULL}, */
{'d', READ_OPT_LD, bt_read_getdelim},
/* {'e', READ_OPT_LE, NULL}, */
/* {'i', READ_OPT_LI, NULL}, */
{'n', READ_OPT_LN, bt_read_getnchars},
/* {'N', READ_OPT_UN, bt_read_getnchars}, */
{'p', READ_OPT_LP, bt_read_getprompt},
{'r', READ_OPT_LR, NULL},
{'s', READ_OPT_LS, NULL},
{'t', READ_OPT_LT, NULL},
{'u', READ_OPT_LU, NULL},
{'t', READ_OPT_LT, bt_read_gettimeout},
{'u', READ_OPT_LU, bt_read_getfd},
{0, 0, 0},
};
void bt_read_init(t_read *data)
{
data.delim = '\n';
data.nchars = -1;
data.prompt = NULL;
data.timeout = -1;
data.fd = 0;
data->opts = 0;
data->delim = '\n';
data->nchars = -1;
data->prompt = NULL;
data->timeout = -1;
data->fd = 0;
}
t_readopt bt_read_getopt(char letter)
t_readopt *bt_read_getopt(char letter)
{
int i;
@ -45,7 +46,8 @@ t_readopt bt_read_getopt(char letter)
while (g_readtab[i].letter)
{
if (g_readtab[i].letter == letter)
return (g_readtab[i]);
return (&g_readtab[i]);
i++;
}
return (NULL);
}
@ -55,38 +57,45 @@ int bt_read_parse(t_read *data, char **av)
int i;
int j;
int k;
t_readopt opt;
t_readopt *opt;
i = 1;
k = 0;
while (av[i])
{
j = 0;
DG("check 1");
if (av[i][j++] == '-')
{
if (av[i][j] == '-')
if (av[i][j] == '-' && av[i][j + 1] == 0)
{
DG("check 2");
i++;
break ;
}
DG("check 3");
while (av[i][j])
{
if (!(opt = bt_read_getopt(av[i][j])))
{
ft_dprintf(2, "%s: bad option: %c", SHELL_NAME, av[i][j]);
ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]);
return (2);
}
data->opts |= opt.flag;
if (data->get)
data->opts |= opt->flag;
if (opt->get)
{
(*data->get)(data, av[i + 1]);
(*opt->get)(data, av[++i]);
break ;
}
j++;
}
DG("check 4");
}
else
break ;
i++;
}
if (av[i])
bt_read_getnames(())
data->names = av + i;
return (0);
}
@ -94,10 +103,46 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
{
t_read data;
int i;
char buf[5];
char *input;
int esc;
(void)path;
(void)envp;
input = NULL;
bt_read_init(&data);
if ((i = bt_read_parse(&data, (char **)av)))
return (i);
if ((bt_read_parse(&data, (char **)av)))
return (2);
DG("read_opts: %b", data.opts);
DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i",
data.delim, data.nchars, data.prompt, data.timeout, data.fd);
ft_sstrprint(data.names, ',');
i = 0;
esc = 0;
if (data.prompt)
ft_printf(data.prompt);
while (42)
{
if (read(0, buf, 1) < 0)
return (1);
buf[1] = 0;
if (!esc && *buf == data.delim)
{
DG("CHECK 1");
break ;
}
if (*buf == '\n' && !(data.opts & READ_OPT_LR))
ft_putstr("> ");
esc = esc ? 0 : !(data.opts & READ_OPT_LR) && (*buf == '\\');
ft_strappend(&input, buf);
i++;
DG("%i:%i", data.opts & READ_OPT_LN, i >= data.nchars);
if ((data.opts & READ_OPT_LN) && i >= data.nchars)
{
DG("CHECK 2");
break ;
}
}
DG("input=%s", input);
return (0);
}

View file

@ -6,13 +6,14 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2017/01/20 15:01:34 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:42:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_stof g_builtin[] = {
t_stof g_builtin[] =
{
{"echo", &builtin_echo},
{"cd", &builtin_cd},
{"setenv", &builtin_setenv},

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */
/* Updated: 2017/01/10 14:29:46 by jhalford ### ########.fr */
/* Updated: 2017/01/22 17:40:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,6 @@ int lexer_number(t_list **alst, char *str)
t_lexstate state;
token = (*alst)->content;
DG("*str=%c", *str);
if ((state = get_lexer_state(str)))
return (ft_tokenize(alst, str, state));
if (*str == '>')

View file

@ -6,19 +6,21 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */
/* Updated: 2016/11/11 17:47:15 by jhalford ### ########.fr */
/* Updated: 2017/01/22 16:44:15 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
#define TOKEN_INCR 10
int token_append(t_token *token, char c)
{
if ((int)ft_strlen(token->data) >= token->size)
{
token->data = (char *)ft_realloc(token->data, token->size + 10);
token->size += 10;
token->data = (char *)ft_realloc(token->data, token->size + TOKEN_INCR);
token->size += TOKEN_INCR - 1;
}
ft_strcat(token->data, (char[2]){c, '\0'});
ft_strcat(token->data, (char[]){c, 0});
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/08 16:36:43 by sbenning #+# #+# */
/* Updated: 2017/01/11 13:55:42 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:39:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/08 16:50:26 by sbenning #+# #+# */
/* Updated: 2017/01/11 13:55:40 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:40:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/08 18:03:48 by sbenning #+# #+# */
/* Updated: 2017/01/11 17:35:14 by jhalford ### ########.fr */
/* Updated: 2017/01/22 18:15:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/01/20 15:23:46 by jhalford ### ########.fr */
/* Updated: 2017/01/21 18:04:06 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */