Skip to content

Commit e8f7118

Browse files
committed
Command: add new option --command-param
Fix #1255
1 parent a052033 commit e8f7118

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

doc/json_schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,10 @@
12361236
"description": "Set the shell program to execute the command text\nDefault: cmd for Windows, /bin/sh for *nix",
12371237
"type": "string"
12381238
},
1239+
"param": {
1240+
"description": "Set the parameter used when starting the shell\nDefault: /c for Windows, -c for *nix",
1241+
"type": "string"
1242+
},
12391243
"text": {
12401244
"description": "Set the command text to be executed",
12411245
"type": "string"

src/data/help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,15 @@
13761376
"default": "\"cmd\" for Windows; \"/bin/sh\" for *nix"
13771377
}
13781378
},
1379+
{
1380+
"long": "command-param",
1381+
"desc": "Set the parameter used when starting the shell",
1382+
"remark": "Due to the difference of how OSes handle command line parameters, Windows will parse whitespaces as param separators, while *nix will not",
1383+
"arg": {
1384+
"type": "str",
1385+
"default": "\"/c\" for Windows; \"-c\" for *nix"
1386+
}
1387+
},
13791388
{
13801389
"long": "command-key",
13811390
"desc": "Set the module key to display",

src/modules/command/command.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
void ffPrintCommand(FFCommandOptions* options)
1010
{
1111
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
12-
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
12+
const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){
13+
options->shell.chars,
14+
options->param.chars,
15+
options->text.chars,
16+
NULL
17+
} : (char* const[]){
1318
options->shell.chars,
14-
#ifdef _WIN32
15-
"/c",
16-
#else
17-
"-c",
18-
#endif
1919
options->text.chars,
2020
NULL
2121
});
@@ -58,6 +58,12 @@ bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, co
5858
return true;
5959
}
6060

61+
if(ffStrEqualsIgnCase(subKey, "param"))
62+
{
63+
ffOptionParseString(key, value, &options->param);
64+
return true;
65+
}
66+
6167
if(ffStrEqualsIgnCase(subKey, "text"))
6268
{
6369
ffOptionParseString(key, value, &options->text);
@@ -86,6 +92,12 @@ void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module)
8692
continue;
8793
}
8894

95+
if (ffStrEqualsIgnCase(key, "param"))
96+
{
97+
ffStrbufSetS(&options->param, yyjson_get_str(val));
98+
continue;
99+
}
100+
89101
if (ffStrEqualsIgnCase(key, "text"))
90102
{
91103
ffStrbufSetS(&options->text, yyjson_get_str(val));
@@ -106,20 +118,23 @@ void ffGenerateCommandJsonConfig(FFCommandOptions* options, yyjson_mut_doc* doc,
106118
if (!ffStrbufEqual(&defaultOptions.shell, &options->shell))
107119
yyjson_mut_obj_add_strbuf(doc, module, "shell", &options->shell);
108120

121+
if (!ffStrbufEqual(&defaultOptions.param, &options->param))
122+
yyjson_mut_obj_add_strbuf(doc, module, "param", &options->param);
123+
109124
if (!ffStrbufEqual(&defaultOptions.text, &options->text))
110125
yyjson_mut_obj_add_strbuf(doc, module, "text", &options->text);
111126
}
112127

113128
void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
114129
{
115130
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
116-
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
131+
const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){
132+
options->shell.chars,
133+
options->param.chars,
134+
options->text.chars,
135+
NULL
136+
} : (char* const[]){
117137
options->shell.chars,
118-
#ifdef _WIN32
119-
"/c",
120-
#else
121-
"-c",
122-
#endif
123138
options->text.chars,
124139
NULL
125140
});
@@ -168,13 +183,20 @@ void ffInitCommandOptions(FFCommandOptions* options)
168183
"/bin/sh"
169184
#endif
170185
);
171-
186+
ffStrbufInitStatic(&options->param,
187+
#ifdef _WIN32
188+
"/c"
189+
#else
190+
"-c"
191+
#endif
192+
);
172193
ffStrbufInit(&options->text);
173194
}
174195

175196
void ffDestroyCommandOptions(FFCommandOptions* options)
176197
{
177198
ffOptionDestroyModuleArg(&options->moduleArgs);
178199
ffStrbufDestroy(&options->shell);
200+
ffStrbufDestroy(&options->param);
179201
ffStrbufDestroy(&options->text);
180202
}

src/modules/command/option.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ typedef struct FFCommandOptions
1010
FFModuleArgs moduleArgs;
1111

1212
FFstrbuf shell;
13+
FFstrbuf param;
1314
FFstrbuf text;
1415
} FFCommandOptions;

0 commit comments

Comments
 (0)