-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandparser.c
More file actions
67 lines (60 loc) · 1.47 KB
/
commandparser.c
File metadata and controls
67 lines (60 loc) · 1.47 KB
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
#include "shellhead.h"
/**
* initialize - Initializes values for shell, prevents ctrl-c and prompts
* @sh: Pointer to structure contaiing important data types for shell
* @ac: Args count
* @av: Args variable array
*/
void initialize(shellstruct *sh, int ac, char **av)
{
sh->ac = ac;
sh->av = av;
sh->cmd = malloc(sizeof(char *) * 128);
if (sh->cmd == NULL)
{
freehelper(sh);
write(STDERR_FILENO, "Malloc failed.\n", 15);
exit(1);
}
sh->pathhead = pathparser(_getenv("PATH"), sh->pathhead);
signal(SIGINT, siginthandler);/** ignore ctrl-c */
prompt(sh);
}
/**
* siginthandler - Helps signal ignore sigint (ctrl-c)
* @sig_num: And unused variable but required for signal second arg
*/
void siginthandler(int sig_num)
{
(void)sig_num;
signal(SIGINT, siginthandler);
write(STDOUT_FILENO, "\n", 1);
write(STDOUT_FILENO, "s_hell$ ", 8);
fflush(stdin);
}
/**
* commandparser_malloc - Tokenizes commands in buffer
* @sh: Buffer to tokenize in sh->buf
* Return: Pointer to tokenized array in sh->buf
*/
shellstruct *commandparser(shellstruct *sh)
{
char *buf2;
int ci = 0;
buf2 = _strtok(sh->buf, " ");
if (buf2 == NULL)/** no tokens because all spaces become NULL */
{
sh->cmd[0] = "";/** set to \0 so strcmp doesn't segfault */
sh->cmd[1] = NULL;
return (sh);
}
while (buf2)/** tokenize like normal */
{
sh->cmd[ci] = _strdup(buf2);
buf2 = _strtok(NULL, " ");
ci++;
}
sh->cmd[ci] = NULL;
sh->execcpy = _strdup(sh->cmd[0]);
return (sh);
}