-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_cmd.c
140 lines (129 loc) · 4.21 KB
/
builtin_cmd.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
//
// Created by tbb on 2021/12/6.
//
#include "main.h"
int cd_cmd(node arg){
if(arg->next) {
printf("Command numbers Error\n");
return 0;
}
char* cd_path = NULL;
unsigned path_len = strlen(arg->data) + 1;
if(arg->data[0] == '~'){
path_len += strlen(pwd->pw_dir);
cd_path = malloc(path_len);
if(cd_path == NULL){
printf("cd: malloc failed.\n");
exit(1);
}
strncpy(cd_path, pwd->pw_dir, strlen(pwd->pw_dir)+1);
strncpy(cd_path+strlen(pwd->pw_dir), arg->data+1, strlen(arg->data)+1);
}else{
cd_path = malloc(path_len);
if(cd_path == NULL){
printf("cd: malloc failed.\n");
exit(1);
}
strncpy(cd_path, arg->data, strlen(arg->data)+1);
}
if(chdir(cd_path) != 0)
fprintf(stderr, "T-bb: can't cd %s.\n", cd_path);
free(cd_path);
return 0;
}
void history_cmd(node arg){
int n = atoi(arg->data);
if(arg->data == NULL) n = history_cnt;
if(n >= history_cnt) n = history_cnt;
//(start+n)%50-i
for(int i = n-1; i >= 0; --i)
printf("%d\t%s\n", n-i, history_buff[(history_queue_start+history_cnt-i-1)%50]);
}
void save_history(cmd_t _root){
int cur_line;
if(history_cnt == 50){
cur_line = history_queue_start;
++history_queue_start;
history_queue_start %= 50;
}else{
cur_line = history_cnt % 50;
++history_cnt;
}
memset(history_buff[cur_line], 0, sizeof(history_buff[cur_line]));
write2history(_root, cur_line);
}
void write2history(cmd_t cmd, int cur_line){
switch (cmd->type) {
case CMD_ATOM:{
cmd_atom t = (cmd_atom)cmd;
node _node = t->node;
while(_node){
unsigned cur_len = strlen(history_buff[cur_line]);
unsigned cmd_len = strlen(_node->data);
snprintf(history_buff[cur_line]+cur_len, cmd_len+2, "%s ", _node->data);
_node = _node->next;
}
break;
}
case CMD_LIST:{
cmd_list t = (cmd_list)cmd;
cmd_t left = t->left;
cmd_t right = t->right;
write2history(left, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 3, "; ");
write2history(right, cur_line);
break;
}
case CMD_BACK:{
cmd_back t = (cmd_back)cmd;
cmd_t back = t->back;
write2history(back, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 3, " &");
break;
}
case CMD_PIPE:{
cmd_pipe t = (cmd_pipe)cmd;
cmd_t left = t->left;
cmd_t right = t->right;
write2history(left, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 4, " | ");
write2history(right, cur_line);
break;
}
case CMD_REDI:{
cmd_redi t = (cmd_redi)cmd;
cmd_t left = t->left;
cmd_t right = t->right;
write2history(left, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 4, " < ");
write2history(right, cur_line);
break;
}
case CMD_REDO:{
cmd_redo t = (cmd_redo)cmd;
cmd_t left = t->left;
cmd_t right = t->right;
write2history(left, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 4, " > ");
write2history(right, cur_line);
break;
}
case CMD_REDOR:{
cmd_redor t = (cmd_redor)cmd;
cmd_t left = t->left;
cmd_t right = t->right;
write2history(left, cur_line);
unsigned cur_len = strlen(history_buff[cur_line]);
snprintf(history_buff[cur_line]+cur_len, 5, " >> ");
write2history(right, cur_line);
break;
}
default:
break;
}
}