这是一个程序的一部分,它接收一个包含一些 shell 命令的文件。例如)一个 || b, a|b, ... 并以树形结构打印出结果。make_command_stream 函数将 shell 脚本作为输入并创建一个包含命令树结构的结构。
0.command_t和command_stream_t的类型定义
typedef struct command *command_t;
typedef struct command_stream *command_stream_t;
1.结构体的定义:struct command_stream
struct command_stream{
struct command* nodeArray[255];
int count;
int front, rear;
int max;
};
2.结构体的定义:struct command
struct command
{
enum command_type type;
// Exit status, or -1 if not known (e.g., because it has not exited yet).
int status;
// I/O redirections, or null if none.
char *input;
char *output;
union
{
// for AND_COMMAND, SEQUENCE_COMMAND, OR_COMMAND, PIPE_COMMAND:
struct command *command[2];
// for SIMPLE_COMMAND:
char **word;
// for SUBSHELL_COMMAND:
struct command *subshell_command;
} u;
};
3.定义一个函数:postfixToTree
struct command** postfixToTree(char* postfix, struct command **root){
struct nStack X;
struct command *newNode[255] = { 0 };
struct command *op1,*op2;
char *p;
p = &postfix[0];
// 这些是变量的内存分配。我知道它看起来非常混乱和不合理,我无法正确分配内存
struct command stackCmd[255] = { 0 };
char stackBuf[255][255] = { 0 };
char* pStackBuf[255] = { 0 };
int i;
for(i=0;i<256;i++){
pStackBuf[i] = &stackBuf[i][0];
stackCmd[i].u.word = &pStackBuf[i];
X.data[i] = &stackCmd[i];
}
struct command nodeCmd[255] = { 0 };
char nodeBuf[255][255] = { 0 };
char* pNodeBuf[255] = { 0 };
int j;
for(j=0;j<256;j++){
pNodeBuf[j] = &nodeBuf[j][0];
nodeCmd[j].u.word = &pNodeBuf[j];
newNode[j] = &nodeCmd[j];
}
emptyNodeStack(&X);
int k = 0;
while(*p){
char keys[] = ";|&<>";
if(isalpha(*p) || (*p ==':') || (*p == '<') || (*p == '>') || (*p == '-') || (*p == '!')){
int i = strcspn(p, keys);
memcpy(pNodeBuf[k],p,i);
nodeBuf[k][i]='\0';
pNodeBuf[k+1] = NULL;
p = p+i;
newNode[k]->type=SIMPLE_COMMAND;
newNode[k]->status=-1;
pushNode(&X,newNode[k]);
k++;
}
if (*p == '|')
{
op1 = popNode(&X);
op2 = popNode(&X);
newNode[k]->type=PIPE_COMMAND;
newNode[k]->status=-1;
newNode[k]->u.command[0] = op2;
newNode[k]->u.command[1] = op1;
pushNode(&X,newNode[k]);
k++;
p++;
}
}
*root = popNode(&X);
return root;
}
4.函数定义:make_command_stream
command_stream_t
make_command_stream (int (*get_next_byte) (void *),
void *get_next_byte_argument)
{
char c = 0;
char* infix_string= &c;
infix_string=(char*)checked_malloc(sizeof(char)*1000);
char d = 0;
char* postfix_string = &d;
postfix_string = (char*)checked_malloc(sizeof(char)*1000);
command_stream_t csPtr = (command_stream_t)checked_malloc(sizeof(struct command_stream));
if(csPtr == NULL)
return NULL;
csPtr->max = 100;
if(csPtr->nodeArray == NULL){
free(csPtr);
return NULL;
}
csPtr->front = 0;
csPtr->rear = 0;
csPtr->count = 0;
int i = 0;
c = get_next_byte(get_next_byte_argument);
while(c!= EOF){
unsigned int j = 0;
while(c!=EOF && c!='\n'){
infix_string[j] = c;
c = get_next_byte(get_next_byte_argument);
j++;
if(j > sizeof(char)*1000){
infix_string=(char*)realloc(infix_string,sizeof(char)*2000);
}
}
infixToPostfix(infix_string, postfix_string);
struct command** r;
// After executing this line, the value of csPtr->nodeArray[0] is replace to csPtr->nodeArray
r = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
enqueue(csPtr,r);
memset(infix_string,0,strlen(infix_string));
memset(postfix_string,0,strlen(postfix_string));
i++;
while(c == '\n' || c == '\t' || c == ' '){
c = get_next_byte(get_next_byte_argument);
}
}
5. 第一个问题。
我想访问结构的成员。我上面所做的是,声明一个指向结构的指针,称为 csPtr(命令流指针)。我为结构分配了内存。之后,我为这个结构设置了初始值。此时,我是否可以在不创建结构对象的情况下访问结构的成员?编译器没有抱怨,但我不清楚如果我不创建结构对象会发生什么。
6.第二个问题
postfixToTree 函数的目的是修改我上面提到的“命令流结构”。具体来说,我的目标是获取树的一个节点,它是从字符串中指向结构(struct command*)的指针。我的程序当前正在执行make_command_stream 函数中的以下行。
r = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
我使用了 GDB,打破了界限,并检查了值。但是,在我执行上面的行之后,array[0] 的值被覆盖了。这是我从 GDB 得到的:
当前线路:
6: postfix_string = 0x603640 "g++ -c foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "true "
3: *(csPtr->nodeArray[1]->u.word) = <error: Cannot access memory at address 0x18>
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "true "
然后我执行这一行:
(gdb) n
预期的:
6: postfix_string = 0x603640 "g++ -c foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "g++ -c foo.c "
3: *(csPtr->nodeArray[1]->u.word) = <error: Cannot access memory at address 0x18>
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "true "
我得到了什么:
6: postfix_string = 0x603640 "g++ -c foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "g++ -c foo.c "
3: *(csPtr->nodeArray[1]->u.word) = 0x7ffffffd7e90 "g++ -c foo.c "
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "g++ -c foo.c "
为什么我的函数会覆盖数组的值?
编辑:
7.popNode的定义
struct command* popNode(struct nStack* s)
{
struct command *ret=NULL;
if(!isemptyNode(s))
{
ret= s->data[s->top];
--s->top;
}
return ret;
}