-
Notifications
You must be signed in to change notification settings - Fork 3
/
getargs.c
135 lines (127 loc) · 2.79 KB
/
getargs.c
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
#include "holberton.h"
#include <stdio.h>
/**
* istoken - verfy operators.
* @c: character to verify
* Return: 1 if there is coicidence or 0 if it is not
*/
int istoken(char c)
{
int i = 0;
char tokens[] = {'&', '|', '\0', ';', '\n', -1};
while (tokens[i] != -1)
{
if (tokens[i] == c)
return (1);
i++;
}
return (0);
}
/**
* crear_name - copy argv[0] to display in errors
* @name: pointer to name of command
*
* Return: pointer to command name
*/
char *crear_name(char *name)
{
int l = 0;
char *newname;
l = _strlen(name);
newname = _calloc(l, sizeof(char));
for (l = 0; name[l]; l++)
newname[l] = name[l];
return (newname);
}
/**
* _getargs - create a linked list that contains all arguments.
* @buf: buffer with the string
* @pos: pointer tu the actual buffer position
* @name: pointer to name of commands
* Return: a pointer to the linked list
*/
command_t *_getargs(char *buf, ssize_t *pos, char *name)
{
ssize_t p = 0;
unsigned int flag = 0;
int sp = 0, id = 0, aux = 0;
command_t *head = NULL;
char **args = NULL;
char *n = NULL;
int *paux = &aux;
head = NULL;
while (p < *pos - 1)
{
sp = 0;
flag = 1;
n = crear_name(name);
while (buf[p] == ' ' || buf[p] == '\t')
p++, aux++;
while (p <= *pos)
{
if (p == *pos && buf[p] != ' ' && buf[p] != '\t'
&& !istoken(buf[p]))
sp++;
if (buf[p] == ' ' || istoken(buf[p]) || buf[p] == '\t')
{
if (flag)
sp++;
flag = 0; }
else
flag = 1;
if (istoken(buf[p]))
break;
p++; }
if (p != 0 && sp == 0)
sp++;
p++;
while (buf[p] == ' ' || buf[p] == '\t')
p++;
args = fill_nodes(buf, sp, pos, paux);
id = clfun(&args[0]);
add_node(&head, args, id, 0, n); }
return (head);
}
/**
* fill_nodes - create a linked list that contains all arguments.
* @buf: poiter to pointer to a space in memory
* @sp: number of args of a token
* @pos: size of the all line (all commands)
* @paux: actual position y the all line (all commands)
* Return: a pointer to the linked list
*/
char **fill_nodes(char *buf, int sp, ssize_t *pos, int *paux)
{
char **args = NULL;
int aux2 = 0, s = 0, y = 0, j = 0, i = 0, aux3 = 0;
s = 0;
aux3 = *paux;
args = malloc(sizeof(char *) * (sp + 1));
if (!args)
return (NULL);
*(args + sp) = NULL;
for (i = 0; sp > 0; sp--, i++)
{
aux2 = aux3;
for (s = 0; buf[aux3] != ' ' && buf[aux3] != '\t'
&& !istoken(buf[aux3])
&& aux3 <= *pos; aux3++, s++)
;
args[i] = malloc(sizeof(char) * (s + 1));
if (!args[i])
{
for (y = 0; y < i; y++)
free(args[y]);
free(args);
return (NULL);
}
for (j = 0; j < s; j++, aux2++)
*(*(args + i) + j) = buf[aux2];
*(*(args + i) + s) = 0;
while (buf[aux3] && (buf[aux3] == ' ' || istoken(buf[aux3]) ||
buf[aux3] == '\t'))
aux3++;
}
*paux = aux3;
return (args);
}