INI_FIELD_PATH,
INI_FIELD_ARGS,
INI_FIELD_ENV,
+ INI_FIELD_FLAG,
COUNT_INI_FIELDS,
};
[INI_FIELD_PATH] = "path",
[INI_FIELD_ARGS] = "args",
[INI_FIELD_ENV] = "env",
+ [INI_FIELD_FLAG] = "cmdflag",
};
-static const size_t MAX_INI_KEY_LEN = 5;
+static const size_t MAX_INI_KEY_LEN = 7;
struct extra_dump_item {
// not separate named fields, for convenient iteration
const char *fields[COUNT_INI_FIELDS];
};
-int handle_extra_program(int out_fd, struct extra_dump_item *item)
+bool check_cmdflag(const char *const flag, int argc, char **argv)
+{
+ if (!flag)
+ return true;
+
+ for (int i = 1; i < argc; i++) {
+ char *p = argv[i];
+ if (*p != '-')
+ continue;
+ for (++p; *p; p++) {
+ if (*p == '-')
+ break;
+ if (*p == flag[0])
+ return true;
+ }
+ }
+
+ return false;
+}
+
+int handle_extra_program(int out_fd, struct extra_dump_item *item, int argc, char **argv)
{
assert(out_fd >= 0);
assert(item);
const char *const path = item->fields[INI_FIELD_PATH];
const char *const args = item->fields[INI_FIELD_ARGS] ?: "";
const char *const env = item->fields[INI_FIELD_ENV] ?: "";
+ const char *const flag = item->fields[INI_FIELD_FLAG];
+
+ if (!check_cmdflag(flag, argc, argv))
+ return 0;
if (!title || !path) {
fprintf_fd(out_fd, "\nNo title or path in extra program config");
* an array of char pointers. Splitting isn't trivial (consider a brutal set
* of arguments using " or `) and I don't want to reinvent the wheel so I'm
* delegating the splitting to the shell. */
- char *argv[] = {"/bin/sh", "-c", command_line, NULL};
+ char *av[] = {"/bin/sh", "-c", command_line, NULL};
int err;
spawn_param_s param = { .fn = spawn_setstdout, .u.int_val = out_fd };
- bool failed = !spawn_wait(argv, NULL, ¶m, DEFAULT_COMMAND_TIMEOUT_MS, &err) || err != 0;
+ bool failed = !spawn_wait(av, NULL, ¶m, DEFAULT_COMMAND_TIMEOUT_MS, &err) || err != 0;
free(command_line);
return failed ? EXIT_CMDERR : 0;
}
-int handle_extra_file(int out_fd, struct extra_dump_item *item)
+int handle_extra_file(int out_fd, struct extra_dump_item *item, int argc, char **argv)
{
assert(out_fd >= 0);
assert(item);
return 0;
}
-typedef int (*handle_ini_section_t)(int out_fd, struct extra_dump_item *);
+typedef int (*handle_ini_section_t)(int out_fd, struct extra_dump_item *, int argc, char **argv);
-static int handle_ini_Nth_section(int out_fd, const dictionary *ini, int n, handle_ini_section_t handle_ini_section)
+static int handle_ini_Nth_section(int out_fd, const dictionary *ini, int n, handle_ini_section_t handle_ini_section, int argc, char **argv)
{
assert(out_fd >= 0);
assert(ini);
item.fields[i] = iniparser_getstring(ini, key_buf, NULL);
}
- return handle_ini_section(out_fd, &item);
+ return handle_ini_section(out_fd, &item, argc, argv);
}
-static int handle_extra_ini(int out_fd, const char *ini_path, handle_ini_section_t handle_ini_section)
+static int handle_extra_ini(int out_fd, const char *ini_path, handle_ini_section_t handle_ini_section, int argc, char **argv)
{
assert(out_fd >= 0);
assert(ini_path);
int ret = 0;
for (int i = 0; i < nsec; ++i)
- ret |= handle_ini_Nth_section(out_fd, ini, i, handle_ini_section);
+ ret |= handle_ini_Nth_section(out_fd, ini, i, handle_ini_section, argc, argv);
return ret;
}
return de->d_type == DT_REG && string_ends_with(de->d_name, ".conf");
}
-int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section)
+int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section, int argc, char **argv)
{
assert(out_fd >= 0);
assert(dir_path);
snprintf(ini_path, sizeof ini_path, "%s/%s", dir_path, de->d_name);
free(de);
- ret |= handle_extra_ini(out_fd, ini_path, handle_ini_section);
+ ret |= handle_extra_ini(out_fd, ini_path, handle_ini_section, argc, argv);
}
free(entries);
close(dir_fd);
struct extra_dump_item;
-typedef int (*handle_ini_section_t)(int out_fd, struct extra_dump_item *);
+typedef int (*handle_ini_section_t)(int out_fd, struct extra_dump_item *, int argc, char **argv);
-int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section);
-int handle_extra_file(int out_fd, struct extra_dump_item *item);
-int handle_extra_program(int out_fd, struct extra_dump_item *item);
+int handle_extra_dir(int out_fd, char *dir_path, handle_ini_section_t handle_ini_section, int argc, char **argv);
+int handle_extra_file(int out_fd, struct extra_dump_item *item, int argc, char **argv);
+int handle_extra_program(int out_fd, struct extra_dump_item *item, int argc, char **argv);