summaryrefslogtreecommitdiffstats
path: root/src/m_prompt.c
blob: 68126ff7ad4cd6f78cba5f0b451a4fc95fe68795 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   m_prompt.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 <stddef.h>
#include <unistd.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "d_define.h"
#include "m_prompt.h"
#include "m_prompt_seq.h"
#include "s_struct.h"
#include "u_vars.h"

static void		m_subst_prompt_rice(char var[], t_msh *msh)
{
	size_t	i;

	i = 0;
	while (var[i] != C_NUL)
	{
		if (var[i] == C_BACKS)
		{
			m_prompt_sequences(var, i);
			m_prompt_sequences_next(var, i, msh);
			m_prompt_sequences_more(var, i, msh);
			i = m_prompt_sequences_again(var, i, msh);
		}
		i++;
	}
}

static void		m_update_psx(unsigned char x, t_msh *msh)
{
	char	var[ARG_MAX];
	char	psx[5];

	ft_sprintf(psx, "$PS%hu", (unsigned short)x);
	u_get_var_value(var, psx, ARG_MAX, msh);
	if (var[0] == C_NUL)
	{
		msh->ps[x - 1][0] = '\0';
		return ;
	}
	m_subst_prompt_rice(var, msh);
	ft_strlcpy(msh->ps[x - 1], var, 255);
}

size_t			m_plen(char *prompt)
{
	size_t i;
	size_t size;

	i = 0;
	size = 0;
	while (prompt[i] != '\0')
	{
		if (prompt[i] == 27)
		{
			while (prompt[i] != 'm' && prompt[i] != '\0')
				i++;
			i++;
		}
		else
		{
			size++;
			i++;
		}
	}
	return (size);
}

void			m_prompt_psx(unsigned char x, t_msh *msh)
{
	m_update_psx(x, msh);
	ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]);
}