-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommandline.c
111 lines (91 loc) · 2.39 KB
/
commandline.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
#include "commands.h"
#include "commandline.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
commandline *CreateLine()
{
commandline *cmd = malloc(sizeof(command *));
cmd->current = CreateStack();
cmd->next = NULL;
cmd->prev = NULL;
cmd->nextOperator = '\0';
return cmd;
}
commandline *GetLastCommand(commandline *cmd)
{
if (NULL == cmd->next || NULL == cmd->current)
return cmd;
return GetLastCommand(cmd->next);
}
void CreateCommand(commandline *cmd, char key)
{
commandline *last = GetLastCommand(cmd);
last->nextOperator = key;
commandline *aux = CreateLine();
aux->prev = last;
last->next = aux;
aux->current = CreateStack();
}
void GetCommand(commandline *root)
{
commandline *commandNode = GetLastCommand(root);
command *cmd = commandNode->current;
char string[ARG_MAX_SIZE] = "\0";
char key;
int flagSpace = 0;
int flagPipe = 0;
int *argSize = &(cmd->argSize);
int *cmdSize = &(cmd->cmdSize);
setbuf(stdin, NULL);
while (cmd->cmdSize < CMD_MAX_SIZE && cmd->argSize < ARG_MAX_QUANTITY)
{
key = getchar();
if (key == '\n')
{
strcpy(cmd->args[cmd->argSize], string);
if (strlen(cmd->args[cmd->argSize]) == 0)
break;
cmd->argSize++;
break;
}
else if (key == ' ')
{
if (flagPipe)
{
flagPipe = 0;
continue;
}
strcpy(cmd->args[cmd->argSize], string);
string[0] = '\0';
cmd->argSize++;
cmd->cmdSize++;
flagSpace = 1;
continue;
}
else if ('|' == key || '>' == key || '<' == key)
{
flagPipe = 1;
if (flagSpace)
{
cmd->argSize--;
flagSpace = 0;
}
else
{
strcpy(cmd->args[cmd->argSize], string);
string[0] = '\0';
cmd->argSize++;
cmd->cmdSize++;
}
CreateCommand(root, key);
cmd = commandNode->next->current;
argSize = &(cmd->argSize);
cmdSize = &(cmd->cmdSize);
continue;
}
sprintf(string, "%s%c", string, key);
cmd->cmdSize++;
}
cmd->args[cmd->argSize] = NULL;
}