715b28
From 3ee4cabcfad3a9ccc3c59be21245b57c17e7ae75 Mon Sep 17 00:00:00 2001
715b28
From: Ondrej Holy <oholy@redhat.com>
715b28
Date: Thu, 21 Jan 2021 14:21:09 +0100
715b28
Subject: [PATCH 1/3] client: Fix exit codes for /help and similar option
715b28
715b28
Currently, non-zero exit code is returned for /version, /buildconfig, /help,
715b28
/monitor-list, /kbd-list and /kbd-lang-list command-line options for several
715b28
clients. This is against conventions because 0 is usually returned in
715b28
such cases. Also, there is potentially another problem that the returned
715b28
codes overflow on UNIX systems (where the exit code is a number between 0
715b28
and 255). Let's fix the clients to return 0 in the mentioned cases to honor
715b28
conventions and 1 for the command-line parsing errors (or -1 for clients
715b28
who already use that value).
715b28
715b28
Fixes: https://github.com/FreeRDP/FreeRDP/issues/6686
715b28
---
715b28
 client/Sample/tf_freerdp.c    |  9 +++++----
715b28
 client/Wayland/wlfreerdp.c    | 13 +++++++------
715b28
 client/Windows/cli/wfreerdp.c |  4 ++++
715b28
 client/X11/cli/xfreerdp.c     | 22 +++++++++++++---------
715b28
 winpr/include/winpr/cmdline.h |  1 +
715b28
 5 files changed, 30 insertions(+), 19 deletions(-)
715b28
715b28
diff --git a/client/Sample/tf_freerdp.c b/client/Sample/tf_freerdp.c
715b28
index 3ba82c78338..49412cb417c 100644
715b28
--- a/client/Sample/tf_freerdp.c
715b28
+++ b/client/Sample/tf_freerdp.c
715b28
@@ -338,12 +338,13 @@ int main(int argc, char* argv[])
715b28
 		goto fail;
715b28
 
715b28
 	status = freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
715b28
-	status =
715b28
-	    freerdp_client_settings_command_line_status_print(context->settings, status, argc, argv);
715b28
-
715b28
 	if (status)
715b28
 	{
715b28
-		rc = 0;
715b28
+		freerdp_client_settings_command_line_status_print(context->settings, status, argc, argv);
715b28
+
715b28
+		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
+			rc = 0;
715b28
+
715b28
 		goto fail;
715b28
 	}
715b28
 
715b28
diff --git a/client/Wayland/wlfreerdp.c b/client/Wayland/wlfreerdp.c
715b28
index 329d1200941..d77b47986ea 100644
715b28
--- a/client/Wayland/wlfreerdp.c
715b28
+++ b/client/Wayland/wlfreerdp.c
715b28
@@ -628,18 +628,19 @@ int main(int argc, char* argv[])
715b28
 	settings = context->settings;
715b28
 
715b28
 	status = freerdp_client_settings_parse_command_line(settings, argc, argv, FALSE);
715b28
-	status = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
-
715b28
 	if (status)
715b28
 	{
715b28
 		BOOL list = settings->ListMonitors;
715b28
+
715b28
+		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
+
715b28
 		if (list)
715b28
 			wlf_list_monitors(wlc);
715b28
 
715b28
-		freerdp_client_context_free(context);
715b28
-		if (list)
715b28
-			return 0;
715b28
-		return status;
715b28
+		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
+			rc = 0;
715b28
+
715b28
+		goto fail;
715b28
 	}
715b28
 
715b28
 	if (freerdp_client_start(context) != 0)
715b28
diff --git a/client/Windows/cli/wfreerdp.c b/client/Windows/cli/wfreerdp.c
715b28
index 7a76eeb9b59..b623067e98e 100644
715b28
--- a/client/Windows/cli/wfreerdp.c
715b28
+++ b/client/Windows/cli/wfreerdp.c
715b28
@@ -108,6 +108,10 @@ INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
715b28
 	if (status)
715b28
 	{
715b28
 		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
+
715b28
+		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
+			rc = 0;
715b28
+
715b28
 		goto out;
715b28
 	}
715b28
 
715b28
diff --git a/client/X11/cli/xfreerdp.c b/client/X11/cli/xfreerdp.c
715b28
index c8a77f335f4..a3505b24d3c 100644
715b28
--- a/client/X11/cli/xfreerdp.c
715b28
+++ b/client/X11/cli/xfreerdp.c
715b28
@@ -34,6 +34,7 @@
715b28
 
715b28
 int main(int argc, char* argv[])
715b28
 {
715b28
+	int rc = 1;
715b28
 	int status;
715b28
 	HANDLE thread;
715b28
 	xfContext* xfc;
715b28
@@ -56,31 +57,34 @@ int main(int argc, char* argv[])
715b28
 	xfc = (xfContext*)context;
715b28
 
715b28
 	status = freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
715b28
-
715b28
-	status = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
-
715b28
 	if (status)
715b28
 	{
715b28
 		BOOL list = settings->ListMonitors;
715b28
+
715b28
+		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
+
715b28
 		if (list)
715b28
 			xf_list_monitors(xfc);
715b28
 
715b28
-		freerdp_client_context_free(context);
715b28
-		if (list)
715b28
-			return 0;
715b28
-		return status;
715b28
+		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
+			rc = 0;
715b28
+
715b28
+		goto out;
715b28
 	}
715b28
 
715b28
-	freerdp_client_start(context);
715b28
+	if (freerdp_client_start(context) != 0)
715b28
+		goto out;
715b28
 
715b28
 	thread = freerdp_client_get_thread(context);
715b28
 
715b28
 	WaitForSingleObject(thread, INFINITE);
715b28
 	GetExitCodeThread(thread, &dwExitCode);
715b28
+	rc = xf_exit_code_from_disconnect_reason(dwExitCode);
715b28
 
715b28
 	freerdp_client_stop(context);
715b28
 
715b28
+out:
715b28
 	freerdp_client_context_free(context);
715b28
 
715b28
-	return xf_exit_code_from_disconnect_reason(dwExitCode);
715b28
+	return rc;
715b28
 }
715b28
diff --git a/winpr/include/winpr/cmdline.h b/winpr/include/winpr/cmdline.h
715b28
index 865ee8f25c7..9276cda8eb1 100644
715b28
--- a/winpr/include/winpr/cmdline.h
715b28
+++ b/winpr/include/winpr/cmdline.h
715b28
@@ -81,6 +81,7 @@
715b28
 #define COMMAND_LINE_STATUS_PRINT_HELP -2002
715b28
 #define COMMAND_LINE_STATUS_PRINT_VERSION -2003
715b28
 #define COMMAND_LINE_STATUS_PRINT_BUILDCONFIG -2004
715b28
+#define COMMAND_LINE_STATUS_PRINT_LAST -2999
715b28
 
715b28
 /* Command-Line Macros */
715b28
 
715b28
715b28
From 531dd81836f2c97fcfcfeabdb9671fb76409ce8d Mon Sep 17 00:00:00 2001
715b28
From: akallabeth <akallabeth@posteo.net>
715b28
Date: Fri, 22 Jan 2021 08:40:03 +0100
715b28
Subject: [PATCH 2/3] Refactored
715b28
 freerdp_client_settings_command_line_status_print_ex
715b28
715b28
Now returns 0 if help or version information was requested.
715b28
---
715b28
 client/Sample/tf_freerdp.c    |  7 ++-----
715b28
 client/Wayland/wlfreerdp.c    |  5 +----
715b28
 client/Windows/cli/wfreerdp.c |  6 +-----
715b28
 client/X11/cli/xfreerdp.c     |  5 +----
715b28
 client/common/cmdline.c       | 14 +++++++++-----
715b28
 5 files changed, 14 insertions(+), 23 deletions(-)
715b28
715b28
diff --git a/client/Sample/tf_freerdp.c b/client/Sample/tf_freerdp.c
715b28
index 49412cb417c..e9b9fe8397e 100644
715b28
--- a/client/Sample/tf_freerdp.c
715b28
+++ b/client/Sample/tf_freerdp.c
715b28
@@ -340,11 +340,8 @@ int main(int argc, char* argv[])
715b28
 	status = freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE);
715b28
 	if (status)
715b28
 	{
715b28
-		freerdp_client_settings_command_line_status_print(context->settings, status, argc, argv);
715b28
-
715b28
-		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
-			rc = 0;
715b28
-
715b28
+		rc = freerdp_client_settings_command_line_status_print(context->settings, status, argc,
715b28
+		                                                       argv);
715b28
 		goto fail;
715b28
 	}
715b28
 
715b28
diff --git a/client/Wayland/wlfreerdp.c b/client/Wayland/wlfreerdp.c
715b28
index d77b47986ea..4a583068968 100644
715b28
--- a/client/Wayland/wlfreerdp.c
715b28
+++ b/client/Wayland/wlfreerdp.c
715b28
@@ -632,14 +632,11 @@ int main(int argc, char* argv[])
715b28
 	{
715b28
 		BOOL list = settings->ListMonitors;
715b28
 
715b28
-		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
+		rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
 
715b28
 		if (list)
715b28
 			wlf_list_monitors(wlc);
715b28
 
715b28
-		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
-			rc = 0;
715b28
-
715b28
 		goto fail;
715b28
 	}
715b28
 
715b28
diff --git a/client/Windows/cli/wfreerdp.c b/client/Windows/cli/wfreerdp.c
715b28
index b623067e98e..e325f84771f 100644
715b28
--- a/client/Windows/cli/wfreerdp.c
715b28
+++ b/client/Windows/cli/wfreerdp.c
715b28
@@ -107,11 +107,7 @@ INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
715b28
 
715b28
 	if (status)
715b28
 	{
715b28
-		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
-
715b28
-		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
-			rc = 0;
715b28
-
715b28
+		ret = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
 		goto out;
715b28
 	}
715b28
 
715b28
diff --git a/client/X11/cli/xfreerdp.c b/client/X11/cli/xfreerdp.c
715b28
index a3505b24d3c..5b702194448 100644
715b28
--- a/client/X11/cli/xfreerdp.c
715b28
+++ b/client/X11/cli/xfreerdp.c
715b28
@@ -61,14 +61,11 @@ int main(int argc, char* argv[])
715b28
 	{
715b28
 		BOOL list = settings->ListMonitors;
715b28
 
715b28
-		freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
+		rc = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
715b28
 
715b28
 		if (list)
715b28
 			xf_list_monitors(xfc);
715b28
 
715b28
-		if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
-			rc = 0;
715b28
-
715b28
 		goto out;
715b28
 	}
715b28
 
715b28
diff --git a/client/common/cmdline.c b/client/common/cmdline.c
715b28
index ed467afb7d0..dc8367b7cd2 100644
715b28
--- a/client/common/cmdline.c
715b28
+++ b/client/common/cmdline.c
715b28
@@ -1403,14 +1403,14 @@ int freerdp_client_settings_command_line_status_print_ex(rdpSettings* settings,
715b28
 	if (status == COMMAND_LINE_STATUS_PRINT_VERSION)
715b28
 	{
715b28
 		freerdp_client_print_version();
715b28
-		return COMMAND_LINE_STATUS_PRINT_VERSION;
715b28
+		goto out;
715b28
 	}
715b28
 
715b28
 	if (status == COMMAND_LINE_STATUS_PRINT_BUILDCONFIG)
715b28
 	{
715b28
 		freerdp_client_print_version();
715b28
 		freerdp_client_print_buildconfig();
715b28
-		return COMMAND_LINE_STATUS_PRINT_BUILDCONFIG;
715b28
+		goto out;
715b28
 	}
715b28
 	else if (status == COMMAND_LINE_STATUS_PRINT)
715b28
 	{
715b28
@@ -1465,15 +1465,19 @@ int freerdp_client_settings_command_line_status_print_ex(rdpSettings* settings,
715b28
 			settings->ListMonitors = TRUE;
715b28
 		}
715b28
 
715b28
-		return COMMAND_LINE_STATUS_PRINT;
715b28
+		goto out;
715b28
 	}
715b28
 	else if (status < 0)
715b28
 	{
715b28
 		freerdp_client_print_command_line_help_ex(argc, argv, custom);
715b28
-		return COMMAND_LINE_STATUS_PRINT_HELP;
715b28
+		status = COMMAND_LINE_STATUS_PRINT_HELP;
715b28
+		goto out;
715b28
 	}
715b28
 
715b28
-	return 0;
715b28
+out:
715b28
+	if (status <= COMMAND_LINE_STATUS_PRINT && status >= COMMAND_LINE_STATUS_PRINT_LAST)
715b28
+		return 0;
715b28
+	return status;
715b28
 }
715b28
 
715b28
 static BOOL ends_with(const char* str, const char* ext)
715b28
715b28
From 050a68fec901030c7428852f8f536ace055eb2f7 Mon Sep 17 00:00:00 2001
715b28
From: akallabeth <akallabeth@posteo.net>
715b28
Date: Fri, 22 Jan 2021 09:32:51 +0100
715b28
Subject: [PATCH 3/3] Do not eliminate original error status.
715b28
715b28
---
715b28
 client/common/cmdline.c | 1 -
715b28
 1 file changed, 1 deletion(-)
715b28
715b28
diff --git a/client/common/cmdline.c b/client/common/cmdline.c
715b28
index dc8367b7cd2..66d3c4fffef 100644
715b28
--- a/client/common/cmdline.c
715b28
+++ b/client/common/cmdline.c
715b28
@@ -1470,7 +1470,6 @@ int freerdp_client_settings_command_line_status_print_ex(rdpSettings* settings,
715b28
 	else if (status < 0)
715b28
 	{
715b28
 		freerdp_client_print_command_line_help_ex(argc, argv, custom);
715b28
-		status = COMMAND_LINE_STATUS_PRINT_HELP;
715b28
 		goto out;
715b28
 	}
715b28