From 57bc6e160dcb288aa17ebe09bf2daf95faf0db43 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Fri, 11 Oct 2019 23:37:05 +0300 Subject: [PATCH] keep original argv --- Makefile | 14 +++++++------- src/main.c | 6 ++++-- src/options.c | 34 ++++++++++++++++++++++++++++------ src/options.h | 12 +++++++++++- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index d6c2c49..ef960d9 100644 --- a/Makefile +++ b/Makefile @@ -44,13 +44,13 @@ override CFLAGS += -DWITH_PTHREAD_NP endif -#WITH_SETPROCTITLE ?= 1 -#ifneq ($(call optbool,$(WITH_SETPROCTITLE)),) -#ifeq ($(shell uname -s | tr A-Z a-z),linux) -#_LIBS += -lbsd -#endif -#override CFLAGS += -DWITH_SETPROCTITLE -#endif +WITH_SETPROCTITLE ?= 1 +ifneq ($(call optbool,$(WITH_SETPROCTITLE)),) +ifeq ($(shell uname -s | tr A-Z a-z),linux) +_LIBS += -lbsd +endif +override CFLAGS += -DWITH_SETPROCTITLE +endif # ===== diff --git a/src/main.c b/src/main.c index a337e30..e540092 100644 --- a/src/main.c +++ b/src/main.c @@ -98,6 +98,7 @@ static void _install_signal_handlers(void) { } int main(int argc, char *argv[]) { + struct options_t *options; struct device_t *dev; struct encoder_t *encoder; struct stream_t *stream; @@ -105,8 +106,8 @@ int main(int argc, char *argv[]) { int exit_code = 0; LOGGING_INIT; - A_THREAD_RENAME("main"); + options = options_init(argc, argv); # ifdef WITH_GPIO GPIO_INIT; @@ -117,7 +118,7 @@ int main(int argc, char *argv[]) { stream = stream_init(dev, encoder); server = http_server_init(stream); - if ((exit_code = parse_options(argc, argv, dev, encoder, server)) == 0) { + if ((exit_code = options_parse(options, dev, encoder, server)) == 0) { # ifdef WITH_GPIO GPIO_INIT_PINOUT; # endif @@ -153,6 +154,7 @@ int main(int argc, char *argv[]) { GPIO_SET_LOW(prog_running); # endif + options_destroy(options); if (exit_code == 0) { LOG_INFO("Bye-bye"); } diff --git a/src/options.c b/src/options.c index 403883f..714f3b2 100644 --- a/src/options.c +++ b/src/options.c @@ -25,9 +25,7 @@ #include #include #include -#ifdef WITH_OMX -# include -#endif +#include #include #include #include @@ -212,7 +210,31 @@ static void _features(void); static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server); -int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) { +struct options_t *options_init(int argc, char *argv[]) { + struct options_t *options; + + A_CALLOC(options, 1); + options->argc = argc; + options->argv = argv; + + A_CALLOC(options->argv_copy, argc); + for (int index = 0; index < argc; ++index) { + assert(options->argv_copy[index] = strdup(argv[index])); + } + + return options; +} + +void options_destroy(struct options_t *options) { + for (int index = 0; index < options->argc; ++index) { + free(options->argv_copy[index]); + } + free(options->argv_copy); + free(options); +} + + +int options_parse(struct options_t *options, struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server) { # define OPT_SET(_dest, _value) { \ _dest = _value; \ break; \ @@ -285,7 +307,7 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t } } - while ((ch = getopt_long(argc, argv, short_opts, _LONG_OPTS, NULL)) >= 0) { + while ((ch = getopt_long(options->argc, options->argv_copy, short_opts, _LONG_OPTS, NULL)) >= 0) { switch (ch) { case _O_DEVICE: OPT_SET(dev->path, optarg); case _O_INPUT: OPT_NUMBER("--input", dev->input, 0, 128, 0); @@ -378,7 +400,7 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t # ifdef WITH_SETPROCTITLE if (process_name_prefix != NULL) { - process_set_name_prefix(argc, argv, process_name_prefix); + process_set_name_prefix(options->argc, options->argv, process_name_prefix); } # endif diff --git a/src/options.h b/src/options.h index 01046fc..50a17ab 100644 --- a/src/options.h +++ b/src/options.h @@ -27,4 +27,14 @@ #include "http/server.h" -int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server); +struct options_t { + int argc; + char **argv; + char **argv_copy; +}; + + +struct options_t *options_init(int argc, char *argv[]); +void options_destroy(struct options_t *options); + +int options_parse(struct options_t *options, struct device_t *dev, struct encoder_t *encoder, struct http_server_t *server);