-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
87 lines (75 loc) · 3.37 KB
/
Copy pathparse.c
File metadata and controls
87 lines (75 loc) · 3.37 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This is the c program to parse any string, both in interactive and batch mode,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
instruction** parseFn(char* inpStr){
// get the input from the user and parse the string to get the command and its arguments.
// define the array of instructions and a counter for the number of instructions
instruction** instructionArr = NULL;
int instructionCount = 0;
// Tokenize the input string using '\n' as the first separator
char* cmdBuffer1;
char* token1 = strtok_r(inpStr, "\n", &cmdBuffer1);
// filername if output redirection.
char* filename;
int redirect = 0;
while(token1){
// Tokenize each line using '&' as the second separator
char* token2;
char* cmdBuffer2;
token2 = strtok_r(token1, "&", &cmdBuffer2);
while(token2){
// Tokenize each command using ' ' as the third separator
char* cmdBuffer3;
char* token3 = strtok_r(token2, " ", &cmdBuffer3);
if (token3){
// Create a new instruction
instruction* inst = (instruction*)malloc(sizeof(instruction));
inst->name = (char*)malloc((strlen(token3) + 1) * sizeof(char));
strcpy(inst->name, token3);
inst->nArguments = 0;
inst->arguments = NULL;
inst->outfile = NULL;
inst->redirection = 0;
while (token3) {
if (inst->outfile && inst->redirection > 1){
errorMessage();
exit(1);
}
// Add command arguments
token3 = strtok_r(NULL, " ", &cmdBuffer3);
// add a condition where token is >
if (token3 && strcmp(token3, ">")==0){
// code for output re-direction
token3 = strtok_r(NULL, " ", &cmdBuffer3);
if (token3 == NULL){
errorMessage();
exit(1);
}
inst->outfile = token3;
inst->redirection++;
}
else{
if (token3) {
inst->arguments = (char**)realloc(inst->arguments, (inst->nArguments + 1) * sizeof(char*));
inst->arguments[inst->nArguments] = (char*)malloc((strlen(token3) + 1) * sizeof(char));
strcpy(inst->arguments[inst->nArguments], token3);
inst->nArguments++;
}
}
}
// Add the instruction to the array
instructionArr = (instruction**)realloc(instructionArr, (instructionCount + 1) * sizeof(instruction*));
instructionArr[instructionCount] = inst;
instructionCount++;
}
token2 = strtok_r(NULL, "&", &cmdBuffer2);
}
token1 = strtok_r(NULL, "\n", &cmdBuffer1);
}
// Add a NULL pointer at the end of the instruction array
instructionArr = (instruction**)realloc(instructionArr, (instructionCount + 1) * sizeof(instruction*));
instructionArr[instructionCount] = NULL;
return instructionArr;
}