-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.c
1093 lines (961 loc) · 27.8 KB
/
execute.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <errno.h>
#include <signal.h>
#include <stddef.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/termios.h>
#include "global.h"
//#define DEBUG
#define S_INPUT 0//标准输入
#define S_OUTPUT 1//标准输出
int goon = 0;
int ingnore = 0; //用于设置signal信号量
char *envPath[10], cmdBuff[40]; //外部命令的存放路径及读取外部命令的缓冲空间
History history; //历史命令
Job *head = NULL; //作业头指针
pid_t fgPid; //当前前台作业的进程号
typedef struct PipeCmd{
int isBack;//是否后台运行
char * input;
char *output;
int simpleCmdNum;//管道所包含的简单命令的数目;
SimpleCmd * temp[101];//管道所包含的简单命令按照输入顺序存放在结构中
}PipeCmd;
/*******************************************************
工具以及辅助方法
********************************************************/
void setGoon(){
goon = 1;
}
/*判断命令是否存在*/
int exists(char *cmdFile){
int i = 0;
if((cmdFile[0] == '/' || cmdFile[0] == '.') && access(cmdFile, F_OK) == 0){ //命令在当前目录
strcpy(cmdBuff, cmdFile);
return 1;
}else{ //查找ysh.conf文件中指定的目录,确定命令是否存在
while(envPath[i] != NULL){ //查找路径已在初始化时设置在envPath[i]中
strcpy(cmdBuff, envPath[i]);
strcat(cmdBuff, cmdFile);
if(access(cmdBuff, F_OK) == 0){ //命令文件被找到
return 1;
}
i++;
}
}
return 0;
}
/*将字符串转换为整型的Pid*/
int str2Pid(char *str, int start, int end){
int i, j;
char chs[20];
for(i = start, j= 0; i < end; i++, j++){
if(str[i] < '0' || str[i] > '9'){
return -1;
}else{
chs[j] = str[i];
}
}
chs[j] = '\0';
return atoi(chs);
}
/*释放环境变量空间*/
void release(){
int i;
for(i = 0; strlen(envPath[i]) > 0; i++){
free(envPath[i]);
}
}
/*******************************************************
信号以及jobs相关
********************************************************/
/*添加新的作业*/
Job* addJob(pid_t pid){
Job *now = NULL, *last = NULL, *job = (Job*)malloc(sizeof(Job));
//初始化新的job
job->pid = pid;
strcpy(job->cmd, inputBuff);
strcpy(job->state, RUNNING);
job->next = NULL;
if(head == NULL){ //若是第一个job,则设置为头指针
head = job;
}else{ //否则,根据pid将新的job插入到链表的合适位置
now = head;
while(now != NULL && now->pid < pid){
last = now;
now = now->next;
}
last->next = job;
job->next = now;
}
return job;
}
/*移除一个作业*/
void rmJob(int sig, siginfo_t *sip, void* noused){
pid_t pid;
Job *now = NULL, *last = NULL;
if(ingnore == 1){
ingnore = 0;
return;
}
pid = sip->si_pid;
now = head;
while(now != NULL && now->pid < pid){
last = now;
now = now->next;
}
if(now == NULL){ //作业不存在,则不进行处理直接返回
return;
}
//开始移除该作业
if(now == head){
head = now->next;
}else{
last->next = now->next;
}
free(now);
}
/*组合键命令ctrl+z*/
void ctrl_Z(){
Job *now = NULL;
if(fgPid == 0){ //前台没有作业则直接返回
return;
}
//SIGCHLD信号产生自ctrl+z
ingnore = 1;
now = head;
while(now != NULL && now->pid != fgPid)
now = now->next;
if(now == NULL){ //未找到前台作业,则根据fgPid添加前台作业
now = addJob(fgPid);
}
//修改前台作业的状态及相应的命令格式,并打印提示信息
strcpy(now->state, STOPPED);
now->cmd[strlen(now->cmd)] = '&';
now->cmd[strlen(now->cmd) + 1] = '\0';
printf("[%d]\t%s\t\t%s\n", now->pid, now->state, now->cmd);
//发送SIGSTOP信号给正在前台运作的工作,将其停止
kill(fgPid, SIGSTOP);
fgPid = 0;
}
/*组合键命令ctrl+c*/
void ctrl_C(){
Job *now = NULL;
//前台没有作业直接返回
if(fgPid == 0){
return;
}
//SIGNCHLD信号产生自此函数
ingnore = 1;
now = head;
while (now != NULL && now -> pid != fgPid)
now = now -> next;
if (now == NULL){
now = addJob(fgPid); //没有前台作业则根据fgPid 添加
}
strcpy(now -> state, DONE);
now -> cmd[strlen(now -> cmd)] = '&';
now -> cmd[strlen(now -> cmd) + 1] = '\0';
printf("[%d]\t%s\t\t%s\n",now -> pid, now -> state, now -> cmd);
kill (fgPid, SIGKILL); //sigstop -> sigkill
fgPid = 0;
//需要删除在作业链表中的当前作业
head = now->next;
free(now);
}
/*fg命令*/
void fg_exec(int pid){
Job *now = NULL;
int i;
//SIGCHLD信号产生自此函数
ingnore = 1;
//根据pid查找作业
now = head;
while(now != NULL && now->pid != pid)
now = now->next;
if(now == NULL){ //未找到作业
printf("pid为7%d 的作业不存在!\n", pid);
return;
}
//记录前台作业的pid,修改对应作业状态
fgPid = now->pid;
strcpy(now->state, RUNNING);
signal(SIGTSTP, ctrl_Z); //设置signal信号,为下一次按下组合键Ctrl+Z做准备
signal(SIGINT,ctrl_C);
i = strlen(now->cmd) - 1;
while(i >= 0 && now->cmd[i] != '&')
i--;
now->cmd[i] = '\0';
printf("%s\n", now->cmd);
kill(now->pid, SIGCONT); //向对象作业发送SIGCONT信号,使其运行
sleep(1);
waitpid(fgPid, NULL, 0); //父进程等待前台进程的运行
}
/*bg命令*/
void bg_exec(int pid){
Job *now = NULL;
//SIGCHLD信号产生自此函数
ingnore = 1;
//根据pid查找作业
now = head;
while(now != NULL && now->pid != pid)
now = now->next;
if(now == NULL){ //未找到作业
printf("pid为%d 的作业不存在!\n", pid);
return;
}
strcpy(now->state, RUNNING); //修改对象作业的状态
printf("[%d]\t%s\t\t%s\n", now->pid, now->state, now->cmd);
kill(now->pid, SIGCONT); //向对象作业发送SIGCONT信号,使其运行
}
/*******************************************************
命令历史记录
********************************************************/
void addHistory(char *cmd){
if(history.end == -1){ //第一次使用history命令
history.end = 0;
strcpy(history.cmds[history.end], cmd);
return;
}
history.end = (history.end + 1)%HISTORY_LEN; //end前移一位
strcpy(history.cmds[history.end], cmd); //将命令拷贝到end指向的数组中
if(history.end == history.start){ //end和start指向同一位置
history.start = (history.start + 1)%HISTORY_LEN; //start前移一位
}
}
/*******************************************************
初始化环境
********************************************************/
/*通过路径文件获取环境路径*/
void getEnvPath(int len, char *buf){
int i, j, last = 0, pathIndex = 0, temp;
char path[40];
for(i = 0, j = 0; i < len; i++){
if(buf[i] == ':'){ //将以冒号(:)分隔的查找路径分别设置到envPath[]中
if(path[j-1] != '/'){
path[j++] = '/';
}
path[j] = '\0';
j = 0;
temp = strlen(path);
envPath[pathIndex] = (char*)malloc(sizeof(char) * (temp + 1));
strcpy(envPath[pathIndex], path);
pathIndex++;
}else{
path[j++] = buf[i];
}
}
envPath[pathIndex] = NULL;
}
/*初始化操作*/
void init(){
int fd, n, len;
char c, buf[80];
//打开查找路径文件ysh.conf
if((fd = open("ysh.conf", O_RDONLY, 660)) == -1){
perror("init environment failed\n");
exit(1);
}
//初始化history链表
history.end = -1;
history.start = 0;
len = 0;
//将路径文件内容依次读入到buf[]中
while(read(fd, &c, 1) != 0){
buf[len++] = c;
}
buf[len] = '\0';
//将环境路径存入envPath[]
getEnvPath(len, buf);
//注册信号
struct sigaction action;
action.sa_sigaction = rmJob;
sigfillset(&action.sa_mask);
action.sa_flags = SA_SIGINFO;
sigaction(SIGCHLD, &action, NULL);
signal(SIGTSTP, ctrl_Z);
signal(SIGINT, ctrl_C);
}
/*******************************************************
命令解析
********************************************************/
/*******************************************************
命令执行
********************************************************/
/*执行外部命令*/
static void
free_simple_cmd(SimpleCmd *cmd)
{
int i;
for(i = 0; cmd->args[i] != NULL; i++)
free(cmd->args[i]);
free(cmd->args);
free(cmd->input);
free(cmd->output);
free(cmd);
}
int checkWildCard(char * str1,char * name){
int i,j;
i=0;
j=0;
while(str1[i]!='\0'){
switch(str1[i]){
case '?':
i++;
j++;
break;
case '*':
while(str1[i]=='*'||str1[i]=='?'){
if(str1[i]=='?')
j++;
i++;
}
while(name[j]!=str1[i]&&name[j]!='\0'){
j++;
}
if(name[j]=='\0'&&str1[i]!='\0')
return 0;
break;
default:
if(str1[i]!=name[j])
return 0;
else{
i++;
j++;
break;
}
}
}
if(name[j]!='\0')
return 0;
return 1;
}
typedef struct string{
char *str;
struct string * link;
}StrList;
StrList * heads,*p;
char * strs[50];
int depth;
void completeArg(char * dire,int dep){
DIR * dir;
struct dirent *dirp;
char * temp;
//char temp[MAX_ARGU_LENGTH];
int i;
dir=opendir(dire);
if(dir==NULL)
return;
while((dirp=readdir(dir))!=NULL){
if(checkWildCard(strs[dep]+1,dirp->d_name)){
//printf("%s %s \n",strs[dep]+1,dirp->d_name);
if(dep!=depth){
temp=(char *)malloc(sizeof(char)*(3+strlen(dire)+strlen(dirp->d_name)));
temp[0]='\0';
strcat(temp,dire);
strcat(temp,dirp->d_name);
strcat(temp,"/");
completeArg(temp,dep+1);
}else{
//dire+d_name :valid!
temp=(char *)malloc(sizeof(char)*(3+strlen(dire)+strlen(dirp->d_name)));
temp[0]='\0';
strcat(temp,dire);
strcat(temp,dirp->d_name);
p->link=(StrList*)malloc(sizeof(StrList));
p=p->link;
p->str=temp;
p->link=NULL;
}
}
}
closedir(dir);
}
void split(char * arg){
char * current;
char * temp;
int i=0,k=0,j=0;
if(arg[0]!='/'){
current=get_current_dir_name();
strcat(current,"/");
strcat(current,arg);
}else{
current=arg;
}
while(current[i]!='\0'){
temp=(char *)malloc(sizeof(char)*1000);
j=1;
temp[0]='/';
i++;
for(;current[i]!='/'&¤t[i]!='\0';i++,j++)
temp[j]=current[i];
temp[j]='\0';
strs[k++]=temp;
//printf("%s %d\n",strs[k-1],k);
}
depth=k-1;
}
int hasWildcard(char * s){
int i=0;
for(;i<strlen(s);i++){
if(s[i]=='*'||s[i]=='?')
return 1;
}
return 0;
}
SimpleCmd * handleSimpleCmd(int begin,int end){
int i, j, k;
int fileFinished; //记录命令是否解析完毕
char c, buff[50][200], inputFile[30], outputFile[30], *temp = NULL;
SimpleCmd *cmd = (SimpleCmd*)malloc(sizeof(SimpleCmd));
#ifdef DEBUG
printf("%s!\n",inputBuff);
#endif
//默认为非后台命令,输入输出重定向为null
cmd->isBack = 0;
cmd->input = cmd->output = NULL;
//初始化相应变量
for(i = begin; i<50; i++){
buff[i][0] = '\0';
}
inputFile[0] = '\0';
outputFile[0] = '\0';
i = begin;
//跳过空格等无用信息
while(i < end && (inputBuff[i] == ' ' || inputBuff[i] == '\t')){
i++;
}
k = 0;
j = 0;
fileFinished = 0;
temp = buff[k]; //以下通过temp指针的移动实现对buff[i]的顺次赋值过程
while(i < end){
/*根据命令字符的不同情况进行不同的处理*/
switch(inputBuff[i]){
case ' ':
case '\t': //命令名及参数的结束标志
temp[j] = '\0';
j = 0;
if(!fileFinished){
k++;
temp = buff[k];
}
break;
case '<': //输入重定向标志
if(j != 0){
//此判断为防止命令直接挨着<符号导致判断为同一个参数,如果ls<sth
temp[j] = '\0';
j = 0;
if(!fileFinished){
k++;
temp = buff[k];
}
}
temp = inputFile;
fileFinished = 1;
i++;
break;
case '>': //输出重定向标志
if(j != 0){
temp[j] = '\0';
j = 0;
if(!fileFinished){
k++;
temp = buff[k];
}
}
temp = outputFile;
fileFinished = 1;
i++;
break;
case '&': //后台运行标志
if(j != 0){
temp[j] = '\0';
j = 0;
//printf("temp:%s\n",temp);
if(!fileFinished){
k++;
temp = buff[k];
}
}
cmd->isBack = 1;
fileFinished = 1;
i++;
break;
default: //默认则读入到temp指定的空间
temp[j++] = inputBuff[i++];
continue;
}
//跳过空格等无用信息
while(i < end && (inputBuff[i] == ' ' || inputBuff[i] == '\t')){
i++;
}
}
if(inputBuff[end-1] != ' ' && inputBuff[end-1] != '\t' ){
temp[j] = '\0';
if(!fileFinished){
k++;
}
}
//printf("k%d\n",k);
//依次为命令名及其各个参数赋值
cmd->args = (char**)malloc(sizeof(char*) * (MAX_ARGU_NUM));
j=0;
for(i = 0; i<k; i++){
if(hasWildcard(buff[i])){
p=(StrList*)malloc(sizeof(StrList));
p->link=NULL;
heads=p;
split(buff[i]);
completeArg("/",0);
p=heads->link;
while(p!=NULL){
cmd->args[j]=(char *)malloc(sizeof(char)*(1+strlen(p->str)));
strcpy(cmd->args[j++], p->str);
p=p->link;
}
}else{
cmd->args[j] = (char*)malloc(sizeof(char) * (strlen(buff[i]) + 1));
strcpy(cmd->args[j++], buff[i]);
}
//printf("%s\n",cmd->args[i]);
}
cmd->args[j]=NULL;
//如果有输入重定向文件,则为命令的输入重定向变量赋值
if(strlen(inputFile) != 0){
j = strlen(inputFile);
cmd->input = (char*)malloc(sizeof(char) * (j + 1));
strcpy(cmd->input, inputFile);
}
//如果有输出重定向文件,则为命令的输出重定向变量赋值
if(strlen(outputFile) != 0){
j = strlen(outputFile);
cmd->output = (char*)malloc(sizeof(char) * (j + 1));
strcpy(cmd->output, outputFile);
}
#ifdef DEBUG
printf("****\n");
printf("isBack: %d\n",cmd->isBack);
for(i = 0; i<j; i++){
printf("args[%d]: %s\n",i,cmd->args[i]);
}
printf("input: %s\n",cmd->input);
printf("output: %s\n",cmd->output);
printf("****\n");
#endif
return cmd;
}
PipeCmd* handlePipeCmdStr(int begin, int end)
{
int i, j, k;
int start,en;
PipeCmd * pcmd=(PipeCmd*)malloc(sizeof(PipeCmd));
start=0;
en=0;
k=0;
for(;inputBuff[en]!='|';en++)
;
pcmd->temp[k++]=handleSimpleCmd(start,en);
start=en+1;
en=en+1;
pcmd->temp[k++]=handleSimpleCmd(start,end);
start=en+1;
en=en+1;
return pcmd;
}
/*******************************************************
命令执行接口
********************************************************/
void execExit(){
exit(0);
}
void execHistory(){
int i;
if(history.end == -1){
printf("尚未执行任何命令\n");
return;
}
i = history.start;
do {
printf("%s\n", history.cmds[i]);
i = (i + 1)%HISTORY_LEN;
} while(i != (history.end + 1)%HISTORY_LEN);
}
void execJobs(){
int i;
Job *now = NULL;
if(head == NULL){
printf("尚无任何作业\n");
} else {
printf("index\tpid\tstate\t\tcommand\n");
for(i = 1, now = head; now != NULL; now = now->next, i++){
printf("%d\t%d\t%s\t\t%s\n", i, now->pid, now->state, now->cmd);
}
}
}
void execCd(){
char temp[MAX_ARGU_LENGTH];
int i=2;
int k=0;
if(inputBuff[2]=='\0'){
chdir("/home");
return ;
}
while(inputBuff[i]==' '||inputBuff[i]=='\t')
i++;
for(;k<MAX_ARGU_LENGTH&&inputBuff[i]!=' '&&inputBuff[i]!='\t'&&inputBuff[i]!='\0';k++,i++)
temp[k]=inputBuff[i];
temp[k]='\0';
if(hasWildcard(temp)){
p=(StrList*)malloc(sizeof(StrList));
p->link=NULL;
heads=p;
split(temp);
completeArg("/",0);
p=heads->link;
if(chdir(p->str)<0)
printf("cd; %s 错误的文件名或文件夹名!\n", p->str);
}
else if(chdir(temp) < 0){
printf("cd; %s 错误的文件名或文件夹名!\n", temp);
}
//printf("%s|\n",temp);
}
void execFgCmd(){
int i=0;
int pid;
int start,end;
for(;i<strlen(inputBuff);i++){
if(inputBuff[i]=='%'){
start=i+1;
break;
}
}
end=strlen(inputBuff);
pid=str2Pid(inputBuff,start,end);
if(pid != -1){
fg_exec(pid);
}
}
void execBgCmd(){
int i=0;
int pid;
int start,end;
for(;i<strlen(inputBuff);i++){
if(inputBuff[i]=='%'){
start=i+1;
break;
}
}
end=strlen(inputBuff);
pid=str2Pid(inputBuff,start,end);
if(pid != -1){
bg_exec(pid);
}
}
void execSimpleCmd(){
SimpleCmd *cmd = handleSimpleCmd(0, strlen(inputBuff));
int i;
pid_t pid;
int pipeIn, pipeOut;
if(exists(cmd->args[0])){ //命令存在
if((pid = fork()) < 0){
perror("fork failed");
return;
}
if(pid == 0){ //子进程
if(cmd->isBack){ //若是后台运行命令,等待父进程增加作业
//printf("& !!\n");
signal(SIGUSR1, setGoon); //收到信号,setGoon函数将goon置1,以跳出下面的循环
while(goon == 0) ; //等待父进程SIGUSR1信号,表示作业已加到链表中
goon = 0; //置0,为下一命令做准备
printf("[%d]\t%s\t\t%s\n", getpid(), RUNNING, inputBuff);
kill(getppid(), SIGUSR1);
}
if(cmd->input != NULL){ //存在输入重定向
if((pipeIn = open(cmd->input, O_RDONLY, S_IRUSR|S_IWUSR)) == -1){
printf("不能打开文件 %s!\n", cmd->input);
return;
}
if(dup2(pipeIn, 0) == -1){
printf("重定向标准输入错误!\n");
return;
}
}
if(cmd->output != NULL){ //存在输出重定向
if((pipeOut = open(cmd->output, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) == -1){
printf("不能打开文件 %s!\n", cmd->output);
return ;
}
if(dup2(pipeOut, 1) == -1){
printf("重定向标准输出错误!\n");
return;
}
}
signal(SIGTSTP, ctrl_Z); //设置signal信号,为下一次按下组合键Ctrl+Z做准备
signal(SIGINT,ctrl_C);
if(execv(cmdBuff, cmd->args) < 0){ //执行命令
printf("execv failed!\n");
return;
}
}
else{ //父进程
if(cmd ->isBack){ //后台命令
fgPid = 0; //pid置0,为下一命令做准备
addJob(pid); //增加新的作业
sleep(2);
kill(pid, SIGUSR1); //子进程发信号,表示作业已加入
//等待子进程输出
signal(SIGUSR1, setGoon);
while(goon == 0) ;//printf("fu jin cheng xun huan ing \n");
goon =0;
}else{ //非后台命令
fgPid = pid;
waitpid(pid, NULL, 0);
}
}
}else{ //命令不存在
printf("找不到命令%s\n", inputBuff);
}
}
void execOuterPipeCmd(SimpleCmd *cmd){
pid_t pid;
int pipeIn, pipeOut;
if(exists(cmd->args[0])){ //命令存在
if(execv(cmdBuff, cmd->args) < 0){ //执行命令
printf("execv failed!\n");
return;
}
}
else
{ //命令不存在
printf("找不到命令 %15s\n", inputBuff);
}
}
/*执行命令*/
void execPPCmd(SimpleCmd *cmd){
int i, pid;
char *temp;
Job *now = NULL;
if(strcmp(cmd->args[0], "exit") == 0) { //exit命令
exit(0);
} else if (strcmp(cmd->args[0], "history") == 0) { //history命令
if(history.end == -1){
printf("尚未执行任何命令\n");
return;
}
i = history.start;
do {
printf("%s\n", history.cmds[i]);
i = (i + 1)%HISTORY_LEN;
} while(i != (history.end + 1)%HISTORY_LEN);
} else if (strcmp(cmd->args[0], "jobs") == 0) { //jobs命令
if(head == NULL){
printf("尚无任何作业\n");
} else {
printf("index\tpid\tstate\t\tcommand\n");
for(i = 1, now = head; now != NULL; now = now->next, i++){
printf("%d\t%d\t%s\t\t%s\n", i, now->pid, now->state, now->cmd);
}
}
} else if (strcmp(cmd->args[0], "cd") == 0) { //cd命令
temp = cmd->args[1];
if(temp != NULL){
if(chdir(temp) < 0){
printf("cd; %s 错误的文件名或文件夹名!\n", temp);
}
}
} else if (strcmp(cmd->args[0], "fg") == 0) { //fg命令
temp = cmd->args[1];
if(temp != NULL && temp[0] == '%'){
pid = str2Pid(temp, 1, strlen(temp));
if(pid != -1){
fg_exec(pid);
}
}else{
printf("fg; 参数不合法,正确格式为:fg %<int>\n");
}
} else if (strcmp(cmd->args[0], "bg") == 0) { //bg命令
temp = cmd->args[1];
if(temp != NULL && temp[0] == '%'){
pid = str2Pid(temp, 1, strlen(temp));
if(pid != -1){
bg_exec(pid);
}
}
else{
printf("bg; 参数不合法,正确格式为:bg %<int>\n");
}
} else{ //外部命令
execOuterPipeCmd(cmd);
}
//释放结构体空间
for(i = 0; cmd->args[i] != NULL; i++){
free(cmd->args[i]);
free(cmd->input);
free(cmd->output);
}
}
/*PipeCmd* handlePipeCmdStr(int begin,int end)崔崔的工作*/
/*******************************************************
命令执行接口
********************************************************/
/*
***********************************************8]
*/
void exePiPCmd(PipeCmd *pipecmd){
int pipeIn,pipeOut,pipenum,i,j,singlecmdnum=pipecmd->simpleCmdNum;
pid_t pid[singlecmdnum],shell_pid,Gpid;
int pipes[singlecmdnum][2];//管道们
shell_pid=getpid();
for(i=0;i<singlecmdnum;i++)
{
if(pipe(pipes[i])<0){
printf("Can not creat a pipe");
}
if(pid[i]=fork()<0)
{
printf("Can not fork()in pipe");
}
else
{
/*创建成功*/
if(!pid[i])//child process
{
if(i==0)//第一个子进程
{
Gpid=getpid();
setpgid(Gpid,Gpid);//新建一个进程,把管道的第一个进程作为组长。
if(!pipecmd->isBack)
{
tcsetpgrp(0,Gpid);
}//把他设置为当前进程组
signal(SIGINT,SIG_DFL);//信号que xing 处理
signal(SIGQUIT,SIG_DFL);
signal(SIGTSTP,SIG_DFL);
signal(SIGTTIN,SIG_DFL);
signal(SIGTTOU,SIG_DFL);
if(pipecmd->input!=NULL)//存在输入重定向
{
if((pipeIn=open(pipecmd->input, O_RDONLY, S_IRUSR|S_IWUSR)) == -1)
{
printf("can not open file%s!\n",pipecmd->input);
}
if(dup2(pipeIn,S_INPUT)==-1)
{
printf("dup2 failed!");
}
}
close(pipes[i][0]);//关闭第一个管道的管道读;
if(dup2(pipes[i][1],S_OUTPUT)<0)
{
printf("dup2 failed!");
}
execPPCmd(pipecmd->temp[i]);
}
else if(i==singlecmdnum-1)//这是最后一个进程
{
setpgid(pid[i],Gpid);
if(pipecmd->output!=NULL)
{
if((pipeOut=open(pipecmd->output, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) == -1)