-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
153 lines (130 loc) · 3.18 KB
/
command.c
File metadata and controls
153 lines (130 loc) · 3.18 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datastruct.h"
#include "functions.h"
command* parsed_command = NULL;
char cmd_buff[10][20] = {0}; // command name with his argument
int arg_index = 1; // how many args in a coomand,it start at 1
char input_file[20] = {0}; // input name
char output_file[20] = {0}; // output file name
int is_back = 0;
void make_command(int type,char *str)
{
if(type == 0) // a back ground command
{
is_back = 1;
printf("is_back: %d\n",is_back);
}
else if(type == 1) // command args
{
strcpy(cmd_buff[arg_index ++],str);
}
else if(type == 2) // input file name
{
strcpy(input_file,str);
}
else if(type == 3) // output file name
{
strcpy(output_file,str);
}
else if(type == 4) // command name
{
strcpy(cmd_buff[0],str);
}
//printf("type: %d is_back: %d\n",type,is_back);
}
void push_command()
{
//printf("-------------------------\n");
int i,j;
simple_command *cmd = (simple_command*)malloc(sizeof(simple_command));
cmd->next = NULL;
cmd->args = (char **)malloc(sizeof(char *) * arg_index);
for(i = 0;i < arg_index;i ++)
{
//printf("arg_%d: %s\n",i,cmd_buff[i]);
cmd-> args[i] = (char *)malloc(sizeof(char) * (strlen(cmd_buff[i]) + 1));
strcpy(cmd->args[i],cmd_buff[i]);
}
if(strlen(input_file) != 0)
{
//printf("input: %s\n", input_file);
cmd->input = (char *)malloc(sizeof(char) * (strlen(input_file) + 1));
strcpy(cmd->input,input_file);
}
if(strlen(output_file) != 0)
{
//printf("output: %s\n", output_file);
cmd->output = (char *)malloc(sizeof(char) * (strlen(output_file) + 1));
strcpy(cmd->output,output_file);
}
//printf("---------------------------\n");
// 将命令放入到里面去
if(parsed_command->sim_cmd == NULL)
{
parsed_command->sim_cmd = cmd;
}
else
{
simple_command *temp = parsed_command->sim_cmd;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = cmd;
}
//printf("arg_index: %d\n",arg_index);
arg_index = 1; // 将所有的值清空,提供给下面的命令使用,这里从1开始,0用来放命令名字
input_file[0] = '\0';
output_file[0] = '\0';
}
void print_test()
{
int i,j;
//printf("is_back_final: %d\n",is_back);
simple_command * temp = parsed_command->sim_cmd;
printf("+++++++++++++++++++++\n");
printf("is_back: %d\n",parsed_command->is_back);
while(temp != NULL)
{
printf("cmd_name: %s\n",temp->args[0]);
for(i = 1;temp->args[i];i ++)
{
printf("cmd_args: %s\n",temp->args[i]);
}
printf("input_file: %s\n",temp->input);
printf("output_file: %s\n",temp->output);
temp = temp->next;
printf("===========================\n");
}
printf("+++++++++++++++++++++\n\n\n\n");
}
void before_parse()
{
parsed_command = (command *)malloc(sizeof(command));
parsed_command->sim_cmd = NULL;
}
// 一次命令解析以后,将命令的数恢复为0
void after_parse()
{
parsed_command->is_back = is_back;
is_back = 0;
}
// 解析之后释放分配的空间
void parsed_clean()
{
int i;
simple_command *temp = parsed_command->sim_cmd;
free(parsed_command);
while(temp != NULL)
{
for(i = 0; temp->args[i]; i ++)
{
free(temp->args[i]);
}
free(temp->input);
free(temp->output);
temp = temp->next;
}
}