From c0fba758e60e2ff4c1bebd566d9a0e56276d07ec Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 2 Apr 2022 16:20:47 +0100 Subject: [PATCH] Standalone screenshot utility. I used this for testing the new windows/utils/screenshot.c, and who knows, it might come in useful again. --- windows/CMakeLists.txt | 4 ++++ windows/test_screenshot.c | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 windows/test_screenshot.c diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt index b988792a..79a09eda 100644 --- a/windows/CMakeLists.txt +++ b/windows/CMakeLists.txt @@ -176,3 +176,7 @@ add_executable(test_split_into_argv utils/split_into_argv.c) target_compile_definitions(test_split_into_argv PRIVATE TEST) target_link_libraries(test_split_into_argv utils ${platform_libraries}) + +add_executable(test_screenshot + test_screenshot.c) +target_link_libraries(test_screenshot utils ${platform_libraries}) diff --git a/windows/test_screenshot.c b/windows/test_screenshot.c new file mode 100644 index 00000000..1e3a20d7 --- /dev/null +++ b/windows/test_screenshot.c @@ -0,0 +1,45 @@ +#include "putty.h" + +static NORETURN PRINTF_LIKE(1, 2) void fatal_error(const char *p, ...) +{ + va_list ap; + fprintf(stderr, "screenshot: "); + va_start(ap, p); + vfprintf(stderr, p, ap); + va_end(ap); + fputc('\n', stderr); + exit(1); +} + +void out_of_memory(void) { fatal_error("out of memory"); } + +int main(int argc, char **argv) +{ + const char *outfile = NULL; + + AuxMatchOpt amo = aux_match_opt_init(argc-1, argv+1, 0, fatal_error); + while (!aux_match_done(&amo)) { + char *val; + #define match_opt(...) aux_match_opt( \ + &amo, NULL, __VA_ARGS__, (const char *)NULL) + #define match_optval(...) aux_match_opt( \ + &amo, &val, __VA_ARGS__, (const char *)NULL) + + if (aux_match_arg(&amo, &val)) { + fatal_error("unexpected argument '%s'", val); + } else if (match_optval("-o", "--output")) { + outfile = val; + } else { + fatal_error("unrecognised option '%s'\n", amo.argv[amo.index]); + } + } + + if (!outfile) + fatal_error("expected an output file name"); + + char *err = save_screenshot(NULL, outfile); + if (err) + fatal_error("%s", err); + + return 0; +}