【发布时间】:2015-01-02 01:47:00
【问题描述】:
我试图为 C 程序模拟一个简单的 GUI,方法是分叉 C 程序并在子程序中执行 wish,然后将一堆 tcl/tk 命令从父程序传递到它。创建表单后,我会让 C 程序继续读取 tcl 程序的输出并响应它。
我大部分时间都在工作,但在这个例子中,我不断收到来自 tcl 的消息:
invalid command name ""
它没有破坏任何东西,但我真的不明白是什么原因造成的。
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define DIE do { perror(__FUNCTION__); exit(EXIT_FAILURE); } while (0)
#define LEN(a) (sizeof(a) / sizeof(*a))
int child(int p2c[2], int c2p[2]) {
if (close(p2c[1])) DIE;
if (close(c2p[0])) DIE;
if (dup2(p2c[0], fileno(stdin)) < 0) DIE;
if (dup2(c2p[1], fileno(stdout)) < 0) DIE;
char * cmds[] = { "wish", NULL };
execvp(cmds[0], cmds);
DIE;
}
int parent(int p2c[2], int c2p[2]) {
if (close(p2c[0])) DIE;
if (close(c2p[1])) DIE;
char init[] = "button .b -text {Print} -command {puts hi}\n"
"button .x -text {Exit} -command {exit}\n"
"grid .b -column 0 -row 0\n"
"grid .x -column 0 -row 1\n";
if (write(p2c[1], init, LEN(init)) < LEN(init)) DIE;
if (close(p2c[1])) DIE;
char buf[1<<10];
ssize_t s;
while ((s = read(c2p[0], buf, LEN(buf))) > 0) {
buf[s-1] = '\0';
printf("i read '%s'\n", buf);
}
return 0;
}
int main(int argc, char ** argv) {
int p2c[2]; // parent to child pipe
int c2p[2];
if (pipe(p2c)) DIE;
if (pipe(c2p)) DIE;
switch (fork()) {
case -1: DIE; break;
case 0: child(p2c, c2p); break;
default: parent(p2c, c2p); break;
}
return EXIT_SUCCESS;
}
知道为什么它声称空字符串是无效命令吗?
【问题讨论】: