summaryrefslogtreecommitdiffstats
path: root/src/s_lpipes_split.c
blob: 3dde313ce2bc3d11b69ab1954ff05e9fad7b1a55 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_lpipes_split.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdlib.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "d_define.h"
#include "d_enum.h"
#include "u_parse.h"
#include "u_utils.h"

void	s_set_tmp(char tmp[], size_t pos[], short i, const char word[])
{
	if (pos[i] != 0)
		ft_strlcpy(tmp,
				word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)),
				pos[i] - ((pos[i - 1] == 0) ? (0) : (pos[i - 1]))
				+ ((pos[i - 1] == 0) ? (1) : (0)));
	else
		ft_strlcpy(tmp,
				word + ((pos[i - 1] == 0) ? (0) : (pos[i - 1] + 1)),
				ARG_MAX - pos[i] + 1);
}

void	s_get_split_pos(size_t pos[], const char word[])
{
	char			*ptr;
	t_quote_mode	mode;
	unsigned short		i;

	ptr = (char*)word;
	mode = Q_NONE;
	i = 1;
	while (*ptr != C_NUL && (unsigned short)i < 256)
	{
		if (*ptr == C_PIPE)
		{
			if (mode == Q_NONE && u_is_not_escaped(word, ptr) == TRUE
					&& *(ptr + 1) != C_PIPE && *(ptr - 1) != C_PIPE)
			{
				pos[i] = (ptr - word);
				i++;
			}
		}
		if (*ptr == C_DQUOTE)
			mode = u_meet_dquote(word, ptr, mode);
		else if (*ptr == C_SQUOTE)
			mode = u_meet_squote(word, ptr, mode);
		ptr++;
	}
}