【发布时间】:2020-09-02 02:56:28
【问题描述】:
我正在用 C 语言中的libreadline Linux 库编写自己的 shell。在readline() 中,它从 shell 中获取行并用空格字符解析并将其存储到指针变量中。现在,我想解析这些行以获取包含命令和执行命令的文件名的命令行参数。
我遇到了这个问题,如果文件名有空格名称并且解析如下所示
myshell$ ls 'file name 1' 'file name 2' file\ name\ 3
上面的cli解析如下
ls
'file
name
1'
'file
name
2'
file\
name\
3
所以,请帮我解决这个问题,从 readline 中获取间隔文件名。
这里是我调用 readline 的代码 sn-p。
while (!done)
{
temp = readline (prompt);
/* Test for EOF. */
if (!temp)
exit (1);
/* If there is anything on the line, print it and remember it. */
if (*temp)
{
add_history (temp);
}
<Other stuffs to execute the command>
}
用标记器更新了帖子。
ptr = strtok(temp, " ");
while(ptr != NULL) {
printf("%s\n", ptr);
ptr = strtok(NULL, " ");
}
【问题讨论】:
-
如果这是 C 不要标记为 C++。
-
你的解析码在哪里?没有它,这只是一篇关于你很酷的副项目的日记。
-
感谢您的 cmets。添加代码sn-p。
-
认为我们也需要
add_history。顺便说一句:你有没有测试过你从中得到了什么:printf("%s\n", temp); -
OT:不知道
<Other stuffs to execute the command>是否可以处理空行...