diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2021-08-26 01:02:20 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2021-08-26 01:02:20 -0700 |
commit | 97960b1f4c080d1d26d44061a76e62102b30bea4 (patch) | |
tree | 83bece94736e0488f206a89f18777fdf25a548f9 /src | |
parent | 44753a9e39bb081a76c5b5cf53a25460d1f0e533 (diff) |
vmon: implement --output-dir
Preparatory commit; saved PNGs need a place to go, and the user
needs a way to control that. Defaults to CWD (".")
Diffstat (limited to 'src')
-rw-r--r-- | src/vmon.c | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -47,6 +47,7 @@ typedef struct vmon_t { int done; int linger; time_t start_time; + char *output_dir; } vmon_t; @@ -115,6 +116,39 @@ static int parse_flag_int(char * const *flag, char * const *end, char * const *o } +/* parse string out of opt, stores parsed opt in newly allocated *res on success */ +static int parse_flag_str(char * const *flag, char * const *end, char * const *opt, int min_len, char **res) +{ + char *tmp; + + assert(flag); + assert(end); + assert(opt); + assert(res); + + if (flag == end || (!min_len || **opt == '\0')) { + VWM_ERROR("flag \"%s\" expects an argument", *flag); + return 0; + } + + if (strlen(*opt) < min_len) { + VWM_ERROR("flag \"%s\" argument must be longer than %i, got \"%s\"", *flag, min_len, *opt); + return 0; + } + + tmp = strdup(*opt); + if (!tmp) { + VWM_ERROR("unable to duplicate argument \"%s\"", *opt); + return 0; + } + + free(*res); + *res = tmp; + + return 1; +} + + /* set vmon->{width,height} to fullscreen dimensions */ static int set_fullscreen(vmon_t *vmon) { @@ -151,6 +185,7 @@ static void print_help(void) " -x --width Window width\n" " -y --height Window height\n" " -z --hertz Sample rate in hertz\n" + " -o --output-dir Directory to store saved output to (\".\" if unspecified)\n" "-----------------------------------------------------------------------------" ); } @@ -208,6 +243,11 @@ static int vmon_handle_argv(vmon_t *vmon, int argc, char * const argv[]) return 0; last = ++argv; + } else if (is_flag(*argv, "-o", "--output-dir")) { + if (!parse_flag_str(argv, end, argv + 1, 1, &vmon->output_dir)) + return 0; + + last = ++argv; } else if (is_flag(*argv, "-f", "--fullscreen")) { if (!set_fullscreen(vmon)) { VWM_ERROR("unable to set fullscreen dimensions"); @@ -307,6 +347,11 @@ static vmon_t * vmon_startup(int argc, char * const argv[]) vmon->start_time = time(NULL); vmon->width = WIDTH_DEFAULT; vmon->height = HEIGHT_DEFAULT; + vmon->output_dir = strdup("."); + if (!vmon->output_dir) { + VWM_ERROR("unable to alloc output dir"); + goto _err_free; + } vmon->xserver = vwm_xserver_open(); if (!vmon->xserver) { |