ran gendynapi.pl after newly added SDL_string.c functions.
1 /* See COPYING.txt for the full license governing this code. */
3 * \file harness_argparser.c
5 * Source file for functions to parse arguments to the test harness.
12 #include "SDL_visualtest_harness_argparser.h"
13 #include "SDL_visualtest_rwhelper.h"
15 /** Maximum length of one line in the config file */
16 #define MAX_CONFIG_LINE_LEN 400
17 /** Default value for the timeout after which the SUT is forcefully killed */
18 #define DEFAULT_SUT_TIMEOUT (60 * 1000)
20 /* String compare s1 and s2 ignoring leading hyphens */
22 StrCaseCmpIgnoreHyphen(char* s1, char* s2)
24 /* treat NULL pointer as empty strings */
35 return SDL_strcasecmp(s1, s2);
38 /* parser an argument, updates the state object and returns the number of
39 arguments processed; returns -1 on failure */
41 ParseArg(char** argv, int index, SDLVisualTest_HarnessState* state)
43 if(!argv || !argv[index] || !state)
46 if(StrCaseCmpIgnoreHyphen("sutapp", argv[index]) == 0)
51 SDLTest_LogError("Arguments parsing error: Invalid argument for sutapp.");
54 SDL_strlcpy(state->sutapp, argv[index], MAX_PATH_LEN);
55 SDLTest_Log("SUT Application: %s", state->sutapp);
58 else if(StrCaseCmpIgnoreHyphen("output-dir", argv[index]) == 0)
63 SDLTest_LogError("Arguments parsing error: Invalid argument for output-dir.");
66 SDL_strlcpy(state->output_dir, argv[index], MAX_PATH_LEN);
67 SDLTest_Log("Screenshot Output Directory: %s", state->output_dir);
70 else if(StrCaseCmpIgnoreHyphen("verify-dir", argv[index]) == 0)
75 SDLTest_LogError("Arguments parsing error: Invalid argument for verify-dir.");
78 SDL_strlcpy(state->verify_dir, argv[index], MAX_PATH_LEN);
79 SDLTest_Log("Screenshot Verification Directory: %s", state->verify_dir);
82 else if(StrCaseCmpIgnoreHyphen("sutargs", argv[index]) == 0)
87 SDLTest_LogError("Arguments parsing error: Invalid argument for sutargs.");
90 SDL_strlcpy(state->sutargs, argv[index], MAX_SUT_ARGS_LEN);
91 SDLTest_Log("SUT Arguments: %s", state->sutargs);
94 else if(StrCaseCmpIgnoreHyphen("timeout", argv[index]) == 0)
98 if(!argv[index] || SDL_sscanf(argv[index], "%d:%d:%d", &hr, &min, &sec) != 3)
100 SDLTest_LogError("Arguments parsing error: Invalid argument for timeout.");
103 state->timeout = (((hr * 60) + min) * 60 + sec) * 1000;
104 SDLTest_Log("Maximum Timeout for each SUT run: %d milliseconds",
108 else if(StrCaseCmpIgnoreHyphen("parameter-config", argv[index]) == 0)
113 SDLTest_LogError("Arguments parsing error: Invalid argument for parameter-config.");
116 SDLTest_Log("SUT Parameters file: %s", argv[index]);
117 SDLVisualTest_FreeSUTConfig(&state->sut_config);
118 if(!SDLVisualTest_ParseSUTConfig(argv[index], &state->sut_config))
120 SDLTest_LogError("Failed to parse SUT parameters file");
125 else if(StrCaseCmpIgnoreHyphen("variator", argv[index]) == 0)
130 SDLTest_LogError("Arguments parsing error: Invalid argument for variator.");
133 SDLTest_Log("Variator: %s", argv[index]);
134 if(SDL_strcasecmp("exhaustive", argv[index]) == 0)
135 state->variator_type = SDL_VARIATOR_EXHAUSTIVE;
136 else if(SDL_strcasecmp("random", argv[index]) == 0)
137 state->variator_type = SDL_VARIATOR_RANDOM;
140 SDLTest_LogError("Arguments parsing error: Invalid variator name.");
145 else if(StrCaseCmpIgnoreHyphen("num-variations", argv[index]) == 0)
150 SDLTest_LogError("Arguments parsing error: Invalid argument for num-variations.");
153 state->num_variations = SDL_atoi(argv[index]);
154 SDLTest_Log("Number of variations to run: %d", state->num_variations);
155 if(state->num_variations <= 0)
157 SDLTest_LogError("Arguments parsing error: num-variations must be positive.");
162 else if(StrCaseCmpIgnoreHyphen("no-launch", argv[index]) == 0)
164 state->no_launch = SDL_TRUE;
165 SDLTest_Log("SUT will not be launched.");
168 else if(StrCaseCmpIgnoreHyphen("action-config", argv[index]) == 0)
173 SDLTest_LogError("Arguments parsing error: invalid argument for action-config");
176 SDLTest_Log("Action Config file: %s", argv[index]);
177 SDLVisualTest_EmptyActionQueue(&state->action_queue);
178 if(!SDLVisualTest_ParseActionConfig(argv[index], &state->action_queue))
180 SDLTest_LogError("SDLVisualTest_ParseActionConfig() failed");
185 else if(StrCaseCmpIgnoreHyphen("config", argv[index]) == 0)
190 SDLTest_LogError("Arguments parsing error: invalid argument for config");
194 /* do nothing, this option has already been handled */
200 /* TODO: Trailing/leading spaces and spaces between equals sign not supported. */
202 ParseConfig(char* file, SDLVisualTest_HarnessState* state)
205 SDLVisualTest_RWHelperBuffer buffer;
206 char line[MAX_CONFIG_LINE_LEN];
208 rw = SDL_RWFromFile(file, "r");
211 SDLTest_LogError("SDL_RWFromFile() failed");
215 SDLVisualTest_RWHelperResetBuffer(&buffer);
216 while(SDLVisualTest_RWHelperReadLine(rw, line, MAX_CONFIG_LINE_LEN,
222 /* count number of parameters and replace the trailing newline with 0 */
224 for(i = 0; line[i]; i++)
234 argv = (char**)SDL_malloc((num_params + 1) * sizeof(char*));
237 SDLTest_LogError("malloc() failed.");
242 argv[num_params] = NULL;
243 for(i = 0; i < num_params; i++)
245 argv[i] = strtok(i == 0 ? line : NULL, "=");
248 if(ParseArg(argv, 0, state) == -1)
250 SDLTest_LogError("ParseArg() failed");
259 if(!state->sutapp[0])
265 SDLVisualTest_ParseHarnessArgs(char** argv, SDLVisualTest_HarnessState* state)
269 SDLTest_Log("Parsing commandline arguments..");
273 SDLTest_LogError("argv is NULL");
278 SDLTest_LogError("state is NULL");
282 /* initialize the state object */
283 state->sutargs[0] = '\0';
284 state->sutapp[0] = '\0';
285 state->output_dir[0] = '\0';
286 state->verify_dir[0] = '\0';
287 state->timeout = DEFAULT_SUT_TIMEOUT;
288 SDL_memset(&state->sut_config, 0, sizeof(SDLVisualTest_SUTConfig));
289 SDL_memset(&state->action_queue, 0, sizeof(SDLVisualTest_ActionQueue));
290 state->variator_type = SDL_VARIATOR_RANDOM;
291 state->num_variations = -1;
292 state->no_launch = SDL_FALSE;
294 /* parse config file if passed */
295 for(i = 0; argv[i]; i++)
297 if(StrCaseCmpIgnoreHyphen("config", argv[i]) == 0)
301 SDLTest_Log("Arguments parsing error: invalid argument for config.");
304 if(!ParseConfig(argv[i + 1], state))
306 SDLTest_LogError("ParseConfig() failed");
312 /* parse the arguments */
315 int consumed = ParseArg(argv, i, state);
316 if(consumed == -1 || consumed == 0)
318 SDLTest_LogError("ParseArg() failed");
324 if(state->variator_type == SDL_VARIATOR_RANDOM && state->num_variations == -1)
325 state->num_variations = 1;
327 /* check to see if required options have been passed */
328 if(!state->sutapp[0])
330 SDLTest_LogError("sutapp must be passed.");
333 if(!state->sutargs[0] && !state->sut_config.options)
335 SDLTest_LogError("Either sutargs or parameter-config must be passed.");
338 if(!state->output_dir[0])
340 SDL_strlcpy(state->output_dir, "./output", MAX_PATH_LEN);
342 if(!state->verify_dir[0])
344 SDL_strlcpy(state->verify_dir, "./verify", MAX_PATH_LEN);
351 SDLVisualTest_FreeHarnessState(SDLVisualTest_HarnessState* state)
355 SDLVisualTest_EmptyActionQueue(&state->action_queue);
356 SDLVisualTest_FreeSUTConfig(&state->sut_config);