Added: Pipex as ressources, not most readable code tho...
This commit is contained in:
parent
9482844642
commit
5d425048f2
38 changed files with 2975 additions and 0 deletions
119
other/pipex/src/app/main.c
Normal file
119
other/pipex/src/app/main.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/03 15:35:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 18:52:49 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/main.h"
|
||||
|
||||
void cleanup(t_pipex_args *s, bool error)
|
||||
{
|
||||
if (s->here_doc)
|
||||
close(s->here_doc_fd);
|
||||
close(s->in);
|
||||
close(s->out);
|
||||
vec_str_free(s->env);
|
||||
vec_process_free(s->processes);
|
||||
if (error)
|
||||
exit(1);
|
||||
wait(NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void process_cleanup(t_process elem)
|
||||
{
|
||||
close(elem.stdin.vals.wo.fd);
|
||||
close(elem.stdout.vals.ro.fd);
|
||||
close(elem.stderr.vals.ro.fd);
|
||||
}
|
||||
|
||||
void do_here_doc_thing(t_pipex_args *s)
|
||||
{
|
||||
t_buffer_str line;
|
||||
t_isize f;
|
||||
t_usize len;
|
||||
bool error;
|
||||
|
||||
len = str_len(s->here_doc_limiter);
|
||||
error = false;
|
||||
while (!error)
|
||||
{
|
||||
line = get_next_line(0, &error);
|
||||
if (error)
|
||||
(me_eprintf("Error:\nError when reading stdin!\n"), cleanup(s,
|
||||
true));
|
||||
f = str_n_compare(line.buf, s->here_doc_limiter, len);
|
||||
if (f == 0 && line.len >= len + 1 && (line.buf[len] == '\0'
|
||||
|| line.buf[len] == '\n'))
|
||||
{
|
||||
str_free(line);
|
||||
break ;
|
||||
}
|
||||
write(s->here_doc_fd, line.buf, line.len);
|
||||
str_free(line);
|
||||
}
|
||||
cleanup(s, false);
|
||||
}
|
||||
|
||||
int main2(t_pipex_args *s)
|
||||
{
|
||||
t_usize p_idx;
|
||||
|
||||
if (s->argc < 2 || s->argc < s->min || s->argc > s->max)
|
||||
(me_eprintf("Error:\nWrong number of arguments!\n"), s->here_doc
|
||||
&& close(s->here_doc_fd), close(s->in), vec_str_free(s->env),
|
||||
exit(1));
|
||||
s->processes = vec_process_new(s->argc, process_cleanup);
|
||||
p_idx = s->argc - 2 - (t_usize)(s->here_doc);
|
||||
if (spawn_helper(s, p_idx--, piped(), fd(s->out)))
|
||||
(me_eprintf("Error:\nFailed to spawn process!\n"), cleanup(s, true));
|
||||
while (p_idx > 1 + (t_usize)s->here_doc)
|
||||
{
|
||||
if (spawn_helper(s, p_idx--, piped(),
|
||||
fd(s->processes.buffer[s->processes.len - 1].stdin.vals.wo.fd)))
|
||||
(me_eprintf("Error:\nFailed to spawn process!\n"), cleanup(s,
|
||||
true));
|
||||
}
|
||||
if (spawn_helper(s, p_idx, fd(s->in),
|
||||
fd(s->processes.buffer[s->processes.len - 1].stdin.vals.wo.fd)))
|
||||
(me_eprintf("Error:\nFailed to spawn process!\n"), cleanup(s, true));
|
||||
if (s->here_doc)
|
||||
do_here_doc_thing(s);
|
||||
cleanup(s, false);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, t_str argv[], t_str env[])
|
||||
{
|
||||
t_usize env_idx;
|
||||
t_pipex_args s;
|
||||
|
||||
s.min = 4;
|
||||
s.max = 4;
|
||||
s.argv = argv + 1;
|
||||
s.argc = argc - 1;
|
||||
s.here_doc = false;
|
||||
if (s.argc < 3)
|
||||
(me_eprintf("Error:\nNot enough arguments!"), exit(1));
|
||||
s.env = vec_str_new(128, (t_free_str_item)free);
|
||||
env_idx = 0;
|
||||
while (env[env_idx])
|
||||
vec_str_push(&s.env, str_clone(env[env_idx++]));
|
||||
if (BONUS)
|
||||
s.max = ~0;
|
||||
if (BONUS && str_n_compare(s.argv[0], "here_doc", 9) == 0)
|
||||
set_here_doc(&s);
|
||||
else
|
||||
open_read_file(&s);
|
||||
if ((s.here_doc && me_open_create(s.argv[s.argc - 1], &s.out))
|
||||
|| (!s.here_doc && me_open_truncate(s.argv[s.argc - 1], &s.out)))
|
||||
(me_eprintf("Error:\n Failed to open output file!\n"), s.here_doc
|
||||
&& close(s.here_doc_fd), close(s.in), vec_str_free(s.env), exit(1));
|
||||
return (s.here_doc && s.argv++, main2(&s));
|
||||
}
|
||||
47
other/pipex/src/app/main2.c
Normal file
47
other/pipex/src/app/main2.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/06 18:46:52 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 18:55:12 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/main.h"
|
||||
|
||||
void set_here_doc(t_pipex_args *s)
|
||||
{
|
||||
t_pipe pipe;
|
||||
|
||||
if (create_pipe(&pipe))
|
||||
(vec_str_free(s->env), exit(1));
|
||||
s->min = 5;
|
||||
s->here_doc = true;
|
||||
s->here_doc_limiter = s->argv[1];
|
||||
s->here_doc_fd = pipe.write;
|
||||
s->in = pipe.read;
|
||||
}
|
||||
|
||||
void open_read_file(t_pipex_args *s)
|
||||
{
|
||||
if (me_open(s->argv[0], true, false, &s->in))
|
||||
(me_eprintf("Error:\nInput file not found!\n"), vec_str_free(s->env),
|
||||
exit(1));
|
||||
}
|
||||
|
||||
void clone_vec_str_iter_fn(t_usize idx, t_str *s, void *out)
|
||||
{
|
||||
vec_str_push(out, str_clone(*s));
|
||||
}
|
||||
|
||||
t_vec_str clone_vec_str(t_vec_str *v)
|
||||
{
|
||||
t_vec_str val;
|
||||
|
||||
val = vec_str_new(v->len + 2, (t_free_str_item)free);
|
||||
vec_str_iter(v, &clone_vec_str_iter_fn, &val);
|
||||
return (val);
|
||||
}
|
||||
48
other/pipex/src/app/main3.c
Normal file
48
other/pipex/src/app/main3.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main3.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/06 18:48:21 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 18:55:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/main.h"
|
||||
|
||||
void vec_process_free_ptr(void *s)
|
||||
{
|
||||
close(((t_pipex_args *)s)->in);
|
||||
close(((t_pipex_args *)s)->out);
|
||||
if (((t_pipex_args *)s)->here_doc)
|
||||
close(((t_pipex_args *)s)->here_doc_fd);
|
||||
vec_process_free(((t_pipex_args *)s)->processes);
|
||||
}
|
||||
|
||||
t_error spawn_helper(t_pipex_args *s, t_usize idx, t_redirection in,
|
||||
t_redirection out)
|
||||
{
|
||||
t_process o;
|
||||
t_vec_str args;
|
||||
t_spawn_info info;
|
||||
|
||||
if (split_literals(s->argv[idx], &args))
|
||||
return (ERROR);
|
||||
info = (t_spawn_info){
|
||||
.stdin = in,
|
||||
.stdout = out,
|
||||
.stderr = inherited(),
|
||||
.binary_path = str_clone(args.buffer[0]),
|
||||
.arguments = args,
|
||||
.environement = clone_vec_str(&s->env),
|
||||
.forked_free = vec_process_free_ptr,
|
||||
.forked_free_args = s,
|
||||
};
|
||||
if (spawn_process(
|
||||
info,
|
||||
&o))
|
||||
return (ERROR);
|
||||
return (vec_process_push(&s->processes, o));
|
||||
}
|
||||
24
other/pipex/src/lib/os/pipe.c
Normal file
24
other/pipex/src/lib/os/pipe.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipe.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/04 18:01:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/os/pipe.h"
|
||||
|
||||
t_error create_pipe(t_pipe *out)
|
||||
{
|
||||
t_file fds[2];
|
||||
|
||||
if (pipe(fds))
|
||||
return (ERROR);
|
||||
out->read = fds[0];
|
||||
out->write = fds[1];
|
||||
return (NO_ERROR);
|
||||
}
|
||||
141
other/pipex/src/lib/os/process.c
Normal file
141
other/pipex/src/lib/os/process.c
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* process.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 17:50:47 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/os/pipe.h"
|
||||
#include "me/os/process.h"
|
||||
#include "me/string/str_find_chr.h"
|
||||
#include "me/string/str_n_compare.h"
|
||||
#include "me/string/str_split.h"
|
||||
#include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool find_path(const t_str *s);
|
||||
bool find_null(const t_str *s);
|
||||
bool str_start_with(t_const_str s, t_const_str prefix);
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||
|
||||
t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||
{
|
||||
bool res;
|
||||
|
||||
if (info.forked_free)
|
||||
info.forked_free(info.forked_free_args);
|
||||
dup2(info.stdin.vals.fd.value, 0);
|
||||
dup2(info.stdout.vals.fd.value, 1);
|
||||
dup2(info.stderr.vals.fd.value, 2);
|
||||
close(process->stdin.vals.ro.fd);
|
||||
close(process->stdout.vals.ro.fd);
|
||||
close(process->stderr.vals.ro.fd);
|
||||
close(info.stdin.vals.fd.value);
|
||||
close(info.stdout.vals.fd.value);
|
||||
close(info.stderr.vals.fd.value);
|
||||
if (!vec_str_any(&info.arguments, find_null, &res) && res)
|
||||
vec_str_push(&info.arguments, NULL);
|
||||
res = false;
|
||||
if (!vec_str_any(&info.environement, find_null, &res) && res)
|
||||
vec_str_push(&info.environement, NULL);
|
||||
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
||||
t_buffer_str *s)
|
||||
{
|
||||
t_str *splitted_path;
|
||||
t_usize sp_index;
|
||||
|
||||
splitted_path = str_split(path + 5, ':');
|
||||
if (splitted_path == NULL)
|
||||
return (str_free(*s), ERROR);
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
{
|
||||
((void)(process), str_clear(s));
|
||||
push_str_buffer(s, splitted_path[sp_index]);
|
||||
push_str_buffer(s, "/");
|
||||
push_str_buffer(s, info->binary_path);
|
||||
sp_index++;
|
||||
if (access(s->buf, X_OK | R_OK) == 0)
|
||||
break ;
|
||||
}
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
free(splitted_path[sp_index++]);
|
||||
free(splitted_path);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error find_binary(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
t_usize p_idx;
|
||||
t_str *splitted_path;
|
||||
t_buffer_str s;
|
||||
|
||||
(void)(process);
|
||||
splitted_path = NULL;
|
||||
s = alloc_new_buffer(256);
|
||||
if (str_start_with(info->binary_path, "/")
|
||||
|| str_find_chr(info->binary_path, '/') != NULL)
|
||||
push_str_buffer(&s, info->binary_path);
|
||||
else
|
||||
{
|
||||
if (vec_str_find(&info->environement, find_path, &p_idx))
|
||||
return (str_free(s), ERROR);
|
||||
if (in_path(info, process, info->environement.buffer[p_idx], &s))
|
||||
return (ERROR);
|
||||
}
|
||||
if (access(s.buf, X_OK | R_OK) == 0)
|
||||
{
|
||||
free(info->binary_path);
|
||||
info->binary_path = s.buf;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
return (str_free(s), ERROR);
|
||||
}
|
||||
|
||||
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
|
||||
{
|
||||
if (cleanup_process && process->stdin.tag != INVALID)
|
||||
close(process->stdin.vals.ro.fd);
|
||||
if (cleanup_process && process->stdout.tag != INVALID)
|
||||
close(process->stdout.vals.ro.fd);
|
||||
if (cleanup_process && process->stderr.tag != INVALID)
|
||||
close(process->stderr.vals.ro.fd);
|
||||
close(info.stdin.vals.fd.value);
|
||||
close(info.stdout.vals.fd.value);
|
||||
close(info.stderr.vals.fd.value);
|
||||
vec_str_free(info.arguments);
|
||||
vec_str_free(info.environement);
|
||||
free(info.binary_path);
|
||||
}
|
||||
|
||||
t_error spawn_process(t_spawn_info info, t_process *process)
|
||||
{
|
||||
if (handle_redirections(&info, process))
|
||||
return (cleanup(info, process, true), ERROR);
|
||||
if (find_binary(&info, process))
|
||||
return (cleanup(info, process, true), ERROR);
|
||||
process->pid = fork();
|
||||
if (process->pid == 0)
|
||||
(spawn_process_exec(info, process), exit(1));
|
||||
else
|
||||
{
|
||||
cleanup(info, process, false);
|
||||
if (process->pid == -1)
|
||||
return (printf("pid\n"), ERROR);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
33
other/pipex/src/lib/os/process_inner.c
Normal file
33
other/pipex/src/lib/os/process_inner.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* process_inner.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/04 22:26:10 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
|
||||
bool find_null(const t_str *s)
|
||||
{
|
||||
return (s == NULL);
|
||||
}
|
||||
|
||||
bool str_start_with(t_const_str s, t_const_str prefix)
|
||||
{
|
||||
while (*prefix && *s)
|
||||
{
|
||||
if (*prefix++ != *s++)
|
||||
return (false);
|
||||
}
|
||||
return (*prefix == '\0');
|
||||
}
|
||||
|
||||
bool find_path(const t_str *s)
|
||||
{
|
||||
return (str_start_with(*s, "PATH="));
|
||||
}
|
||||
92
other/pipex/src/lib/os/process_inner2.c
Normal file
92
other/pipex/src/lib/os/process_inner2.c
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* process_inner2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/04 23:01:03 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
#include "me/os/process.h"
|
||||
#include "me/os/pipe.h"
|
||||
|
||||
void handle_redirections_second(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
(void)(process);
|
||||
if (info->stdin.tag == R_INHERITED)
|
||||
{
|
||||
info->stdin = fd(dup(0));
|
||||
process->stdin = ro(dup(0));
|
||||
}
|
||||
if (info->stdout.tag == R_INHERITED)
|
||||
{
|
||||
info->stdout = fd(dup(1));
|
||||
process->stdout = wo(dup(1));
|
||||
}
|
||||
if (info->stderr.tag == R_INHERITED)
|
||||
{
|
||||
info->stderr = fd(dup(2));
|
||||
process->stderr = wo(dup(2));
|
||||
}
|
||||
}
|
||||
|
||||
void handle_redirections_fds(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
if (info->stdin.tag == R_FD)
|
||||
{
|
||||
info->stdin = fd(dup(info->stdin.vals.fd.value));
|
||||
process->stdin = wo(dup(info->stdin.vals.fd.value));
|
||||
}
|
||||
if (info->stdout.tag == R_FD)
|
||||
{
|
||||
info->stdout = fd(dup(info->stdout.vals.fd.value));
|
||||
process->stdout = ro(dup(info->stdout.vals.fd.value));
|
||||
}
|
||||
if (info->stderr.tag == R_FD)
|
||||
{
|
||||
info->stderr = fd(dup(info->stderr.vals.fd.value));
|
||||
process->stderr = ro(dup(info->stderr.vals.fd.value));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void redirection_inner(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
process->stderr.tag = INVALID;
|
||||
process->stdout.tag = INVALID;
|
||||
process->stdin.tag = INVALID;
|
||||
handle_redirections_fds(info, process);
|
||||
handle_redirections_second(info, process);
|
||||
}
|
||||
|
||||
t_error handle_redirections(t_spawn_info *info, t_process *process)
|
||||
{
|
||||
t_pipe pipe_fd;
|
||||
|
||||
redirection_inner(info, process);
|
||||
if (info->stdin.tag == R_PIPED)
|
||||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stdin = wo(pipe_fd.write);
|
||||
info->stdin = fd(pipe_fd.read);
|
||||
}
|
||||
if (info->stdout.tag == R_PIPED)
|
||||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stdout = ro(pipe_fd.read);
|
||||
info->stdout = fd(pipe_fd.write);
|
||||
}
|
||||
if (info->stderr.tag == R_PIPED)
|
||||
{
|
||||
if (create_pipe(&pipe_fd))
|
||||
return (ERROR);
|
||||
process->stderr = ro(pipe_fd.read);
|
||||
info->stderr = fd(pipe_fd.write);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
118
other/pipex/src/lib/string/inner/split_literals_escaped.c
Normal file
118
other/pipex/src/lib/string/inner/split_literals_escaped.c
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* split_literals_escaped.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 14:44:48 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/05 00:06:48 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/char/isspace.h"
|
||||
#include "me/char/tolower.h"
|
||||
#include "me/string/inner/inner_split_literals.h"
|
||||
#include "me/string/split_literals.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline char escape_oct(t_const_str s, t_usize *current_index,
|
||||
bool *did_escape, t_error *error)
|
||||
{
|
||||
char out;
|
||||
char first;
|
||||
|
||||
first = s[*current_index];
|
||||
out = first - '0';
|
||||
(*current_index)++;
|
||||
first = s[(*current_index)];
|
||||
if (first >= '0' && first <= '7')
|
||||
{
|
||||
out = out * 8 + (first - '0');
|
||||
(*current_index)++;
|
||||
first = s[(*current_index)];
|
||||
if (first >= '0' && first <= '7')
|
||||
{
|
||||
out = out * 8 + (first - '0');
|
||||
(*current_index)++;
|
||||
first = s[(*current_index)];
|
||||
}
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
static inline char escape_hex(t_const_str s, t_usize *current_index,
|
||||
bool *did_escape, t_error *error)
|
||||
{
|
||||
char out;
|
||||
char first;
|
||||
|
||||
out = 0;
|
||||
first = me_tolower(s[++(*current_index)]);
|
||||
if (first >= '0' && first <= '9')
|
||||
out = first - '0';
|
||||
else if (first >= 'a' && first <= 'f')
|
||||
out = 10 + first - 'a';
|
||||
else
|
||||
{
|
||||
(*current_index)--;
|
||||
return ('\0');
|
||||
}
|
||||
first = me_tolower(s[++(*current_index)]);
|
||||
if (first >= '0' && first <= '9')
|
||||
out = out * 16 + first - '0';
|
||||
else if (first >= 'a' && first <= 'f')
|
||||
out = out * 16 + 10 + first - 'a';
|
||||
return (out);
|
||||
}
|
||||
|
||||
char unescape_single(char first)
|
||||
{
|
||||
if (first == '\\')
|
||||
return ('\\');
|
||||
if (first == 'a')
|
||||
return ('\a');
|
||||
if (first == 'b')
|
||||
return ('\b');
|
||||
if (first == 't')
|
||||
return ('\t');
|
||||
if (first == 'n')
|
||||
return ('\n');
|
||||
if (first == 'v')
|
||||
return ('\v');
|
||||
if (first == 'f')
|
||||
return ('\f');
|
||||
if (first == 'r')
|
||||
return ('\r');
|
||||
if (first == 'e')
|
||||
return ('\e');
|
||||
if (first == 'E')
|
||||
return ('\e');
|
||||
return ('\0');
|
||||
}
|
||||
|
||||
char unescape(t_const_str s, t_usize *current_index, bool *did_escape,
|
||||
t_error *error)
|
||||
{
|
||||
char first;
|
||||
char out;
|
||||
|
||||
*did_escape = false;
|
||||
if (s[(*current_index)] != '\\')
|
||||
return (s[(*current_index)]);
|
||||
(*current_index)++;
|
||||
first = s[(*current_index)];
|
||||
*did_escape = true;
|
||||
if (unescape_single(first))
|
||||
return (unescape_single(first));
|
||||
if (first >= '0' && first <= '7')
|
||||
return (escape_oct(s, current_index, did_escape, error));
|
||||
if (first == 'x')
|
||||
return (escape_hex(s, current_index, did_escape, error));
|
||||
*did_escape = false;
|
||||
(*current_index)--;
|
||||
return ('\\');
|
||||
}
|
||||
100
other/pipex/src/lib/string/split_literals.c
Normal file
100
other/pipex/src/lib/string/split_literals.c
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* split_literals.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/04 14:28:23 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/01/06 18:43:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/buffered_str/buf_str.h"
|
||||
#include "me/char/isspace.h"
|
||||
#include "me/string/inner/inner_split_literals.h"
|
||||
#include "me/string/split_literals.h"
|
||||
#include "me/string/str_clone.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline void flip_and_set_false(bool *flip, bool *false_)
|
||||
{
|
||||
*flip = !(*flip);
|
||||
*false_ = false;
|
||||
}
|
||||
|
||||
static inline bool is_word_splitter(char c, t_str prefix)
|
||||
{
|
||||
(void)(prefix);
|
||||
return (c == ' ' || c == '\n' || c == '\t');
|
||||
}
|
||||
|
||||
static inline char set_thingy(t_const_str s, t_usize *idx, bool *esc,
|
||||
bool *lit_sq)
|
||||
{
|
||||
char c;
|
||||
bool error;
|
||||
|
||||
if (!lit_sq)
|
||||
{
|
||||
(*idx)++;
|
||||
c = unescape(s, idx, esc, &error);
|
||||
}
|
||||
else
|
||||
{
|
||||
(*idx)++;
|
||||
c = s[*idx];
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
static inline void yes(t_const_str s, t_usize *idx, t_buffer_str *temp,
|
||||
t_usize *len)
|
||||
{
|
||||
t_booleans b;
|
||||
char c;
|
||||
|
||||
b.lit_sq = false;
|
||||
b.lit_dq = false;
|
||||
c = unescape(s, idx, &b.esc, &b.error);
|
||||
while (c != '\0' && !b.esc && is_word_splitter(c, "\t@"))
|
||||
c = unescape(s, ((*idx)++, idx), &b.esc, &b.error);
|
||||
while (c != '\0')
|
||||
{
|
||||
if (((!b.lit_sq && !b.lit_dq && !b.esc) && is_word_splitter(c, ".")))
|
||||
break ;
|
||||
b.append = true;
|
||||
if (c == '\'' && !b.esc && !b.lit_dq)
|
||||
(flip_and_set_false(&b.lit_sq, &b.append), (*len)++);
|
||||
if (c == '"' && !b.esc && !b.lit_sq)
|
||||
(flip_and_set_false(&b.lit_dq, &b.append), (*len)++);
|
||||
if (b.append)
|
||||
(push_str_char(temp, c), (*len)++);
|
||||
c = set_thingy(s, idx, &b.esc, &b.lit_sq);
|
||||
}
|
||||
}
|
||||
|
||||
t_error split_literals(t_const_str s, t_vec_str *out)
|
||||
{
|
||||
t_usize idx;
|
||||
t_usize len;
|
||||
t_buffer_str temp;
|
||||
|
||||
*out = vec_str_new(16, (t_free_str_item)free);
|
||||
temp = alloc_new_buffer(64);
|
||||
idx = 0;
|
||||
while (s[idx])
|
||||
{
|
||||
len = 0;
|
||||
yes(s, &idx, &temp, &len);
|
||||
if (len == 0)
|
||||
continue ;
|
||||
if (vec_str_push(out, temp.buf))
|
||||
return (str_free(temp), vec_str_free(*out), ERROR);
|
||||
temp = alloc_new_buffer(64);
|
||||
}
|
||||
str_free(temp);
|
||||
return (NO_ERROR);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue