-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
70 lines (54 loc) · 1.56 KB
/
parser.c
File metadata and controls
70 lines (54 loc) · 1.56 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
68
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "parser.h"
void trim(char** str) {
char* ptr = *str;
int len;
// Remove leading white-spaces
while(isspace((unsigned char)*ptr)) ptr++;
*str = ptr;
// Remove trailing white-spaces
len = strlen(ptr);
while(len > 0 && isspace((unsigned char)ptr[len-1])) len--;
ptr[len] = '\0';
}
/* Function to parse user command */
Command* parser(char* cmdline) {
// Count spaces for the number of arguments
int spaces = 0;
for (int i = 0; cmdline[i]; i++)
spaces += (cmdline[i] == ' ');
// Allocate memory for arguments
char **argv = malloc((spaces + 2) * sizeof(char*));
// Tokenize cmd_line by spaces and store the tokens in argv
int idx = 0;
char *token = strtok(cmdline, " ");
while (token != NULL) {
argv[idx++] = token;
token = strtok(NULL, " ");
}
argv[idx] = NULL;
// Create and return the Command
Command *cmd = malloc(sizeof(Command));
cmd->argv = argv;
cmd->background = (idx > 0 && argv[idx - 1] && strcmp(argv[idx - 1], "&") == 0);
if (cmd->background) {
argv[idx - 1] = NULL; // Remove '&'
}
cmd->next = NULL;
return cmd;
}
Command* pipe_parser(char *cmdline) {
Command *head = NULL;
Command **node = &head;
char* saveptr;
char* line = strtok_r(cmdline, "|", &saveptr);
while (line != NULL) {
trim(&line);
*node = parser(line);
node = &((*node)->next);
line = strtok_r(NULL, "|", &saveptr);
}
return head;
}