c++ - 如何在 C 或 C++ 中包装参数并将它们传递给 system 或 exec*
问题描述
我想编写一个包装器,用 argv 做一些简单的事情并调用一些脚本。我有以下要求:
- 包装器必须是
.exe
文件 - 包装器必须能够正确处理空格和引号
- 包装器将在用户端生成
- 生成过程必须很小(例如使用https://bellard.org/tcc)
我最初的做法:
编写 ac 程序首先清理参数,然后将它们用引号括起来,然后调用system
. 不幸的是,我无法从system
orexec*
函数中获得结构良好的行为。我希望以下所有示例都输出类似的内容arg1=1; arg2=2; arg3=3; arg4=
(引号换行有一些差异),但在某些示例中会出错,并在 execl 上暂停:
输入文件:
@:: test.bat
@echo off
echo arg1=%1; arg2=%2; arg3=%3; arg4=%4
//minimal-example.c
#include <Windows.h>
#include <stdio.h>
int main( int argc, char ** argv ) {
puts("\nExample 1:");
system("\"test.bat\" \"1\" \"2\" \"3\" ");
puts("\nExample 2:");
system("test.bat \"1\" \"2\" \"3\" ");
puts("\nExample 3:");
system("test.bat 1 2 \"3\" ");
puts("\nExample 4:");
system("\"test.bat\" 1 \"2\" 3 ");
puts("\nExample 5:");
system("\"test.bat\" 1 2 3 ");
puts("\nExample 6:");
execl(argv[0], "test.bat", "1", "2", "3", NULL);
return 0;
}
输出运行:
Example 1:
'test.bat" "1" "2" "3' is not recognized as an internal or external command,
operable program or batch file.
Example 2:
arg1="1"; arg2="2"; arg3="3"; arg4=
Example 3:
arg1=1; arg2=2; arg3="3"; arg4=
Example 4:
'test.bat" 1 "2' is not recognized as an internal or external command,
operable program or batch file.
Example 5:
arg1=1; arg2=2; arg3=3; arg4=
Example 6:
arg1=1; arg2=2; arg3=3; arg4=
(示例 6 暂停,直到我按下Enter
)
问题:
- 有没有办法以允许空格的方式正确包装路径/参数
system
? - 我可以在参数中转义引号
system
吗? - 是否有非阻塞方式运行
exec*
? - 一种
exec*
方法是否可以确保包装程序的标准输入标准输出和标准错误行为正确(没有奇怪的溢出或奇怪的阻塞事件?)
解决方案
像这样的东西应该工作:
string cmd = "test.bat";
for(int i = 1; i < argc; i++) {
cmd += " ";
cmd += argv[i]
}
system(cmd.c_str());
当然,其中包含空格的 args 需要通过添加引号来进一步处理,并且带有引号的参数可能需要转义,并且在 args 包含无法直接处理的内容的情况下还有许多其他复杂情况)
作为替代方案,您可以查看使用 CreateProcess 运行批处理文件
推荐阅读
- django - 动态 USERNAME_FIELD djangorestframework 和 simplejwt
- javascript - MultiValueDictKeyError - Django
- php - codeigniter-4 中的分页自定义 url
- python - 在 Python 中使用 Selenium 搜索不区分大小写文本的最佳方法是什么?
- c# - 如果我向 AddControllersWithViews() 添加扩展名,它是否也适用于 AddRazorPages()
- java - 如何在 JavaPoet 中创建具有给定名称的类型?
- bash - Python在退出前运行bash脚本
- flutter - 在 Flutter 中完成 CircularProgressIndicator 后如何导航到另一个页面?
- html - 为什么h1和按钮没有对齐
- javascript - 使用 jQuery 或 Javascript 在动态生成的复选框值上显示和隐藏 Div