-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
41 lines (30 loc) · 785 Bytes
/
parse.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
// Parser's job is to break each command from shell's input
#include "headers/parse.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define TOKEN_DELIM " \t"
#define TOKEN_BUFSIZE 64
parseInfo *parse (char *cmdline) {
parseInfo *Result;
char command[81];
int com_pos = -1;
if (cmdline[0] == '\n' || cmdline[0] == '\0')
return NULL;
Result = malloc(sizeof(parseInfo));
// int bufsize = TOKEN_BUFSIZE, position = 0; required to loop
char *token;
token = strtok(cmdline, TOKEN_DELIM);
if (token!=NULL)
Result->command = token;
token = strtok(0, cmdline);
if (token!=NULL)
Result->arg = token;
com_pos = 0;
command[com_pos]='\0';
return Result;
}
void free_info(parseInfo *info)
{
free(info);
}