Skip to content

Commit 9ed28fb

Browse files
mluggThisAMJ
authored andcommittedMay 2, 2024··
Implement basic CLI usage
1 parent d6cd850 commit 9ed28fb

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed
 

‎src/main.c

+48-28
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,22 @@ void run_demo(const char *path) {
254254
demo_free(demo);
255255
}
256256

257-
int main(void) {
258-
g_errfile = fopen(ERR_FILE, "w");
259-
g_outfile = fopen(OUT_FILE, "w");
257+
int main(int argc, char **argv) {
258+
const char *dem_name = NULL;
259+
if (argc == 1) {
260+
g_errfile = fopen(ERR_FILE, "w");
261+
g_outfile = fopen(OUT_FILE, "w");
262+
} else if (argc == 2) {
263+
g_errfile = stderr;
264+
g_outfile = stdout;
265+
dem_name = argv[1];
266+
} else {
267+
const char *name = argv[0] ? argv[0] : "mdp";
268+
fprintf(stderr, "Usage:\n");
269+
fprintf(stderr, " %s Traverses every demo in ./demos; outputs to " OUT_FILE " and " ERR_FILE "\n", name);
270+
fprintf(stderr, " %s [in.dem] Runs on a specific demo; outputs to stdio\n", name);
271+
return 1;
272+
}
260273

261274
_g_expected_maps = (const char **)config_read_newline_sep(EXPECTED_MAPS_FILE);
262275
g_cmd_whitelist = config_read_newline_sep(CMD_WHITELIST_FILE);
@@ -304,39 +317,46 @@ int main(void) {
304317
config_free_var_whitelist(general_conf);
305318
}
306319

307-
DIR *d = opendir(DEMO_DIR);
308-
if (d) {
309-
size_t demo_dir_len = strlen(DEMO_DIR);
310-
struct dirent *ent;
311-
bool is_first = true;
312-
while ((ent = readdir(d))) {
313-
if (!strcmp(ent->d_name, ".")) continue;
314-
if (!strcmp(ent->d_name, "..")) continue;
315-
316-
if (!is_first) {
317-
fputs("\n", g_outfile);
318-
}
320+
if (dem_name) {
321+
run_demo(dem_name);
322+
if (_g_detected_timescale) {
323+
fputs("\nTIMESCALE DETECTED\n", g_outfile);
324+
}
325+
} else {
326+
DIR *d = opendir(DEMO_DIR);
327+
if (d) {
328+
size_t demo_dir_len = strlen(DEMO_DIR);
329+
struct dirent *ent;
330+
bool is_first = true;
331+
while ((ent = readdir(d))) {
332+
if (!strcmp(ent->d_name, ".")) continue;
333+
if (!strcmp(ent->d_name, "..")) continue;
334+
335+
if (!is_first) {
336+
fputs("\n", g_outfile);
337+
}
319338

320-
is_first = false;
339+
is_first = false;
321340

322-
char *path = malloc(demo_dir_len + strlen(ent->d_name) + 2);
323-
strcpy(path, DEMO_DIR);
324-
strcat(path, "/");
325-
strcat(path, ent->d_name);
341+
char *path = malloc(demo_dir_len + strlen(ent->d_name) + 2);
342+
strcpy(path, DEMO_DIR);
343+
strcat(path, "/");
344+
strcat(path, ent->d_name);
326345

327-
_g_detected_timescale = false;
328-
run_demo(path);
346+
_g_detected_timescale = false;
347+
run_demo(path);
329348

330-
free(path);
349+
free(path);
350+
}
351+
352+
closedir(d);
353+
} else {
354+
fprintf(g_errfile, "failed to open demos folder '%s'\n", DEMO_DIR);
331355
}
332356

333-
closedir(d);
334-
} else {
335-
fprintf(g_errfile, "failed to open demos folder '%s'\n", DEMO_DIR);
357+
fprintf(g_outfile, "\ntimescale detected on %u demos\n", _g_num_timescale);
336358
}
337359

338-
fprintf(g_outfile, "\ntimescale detected on %u demos\n", _g_num_timescale);
339-
340360
if (_g_expected_maps) {
341361
bool did_hdr = false;
342362
for (size_t i = 0; _g_expected_maps[i]; ++i) {

0 commit comments

Comments
 (0)
Please sign in to comment.