public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH i2c-tools 1/2] i2cget: printing help is not an error
@ 2024-08-11  9:34 Wolfram Sang
  2024-08-11  9:34 ` [PATCH i2c-tools 2/2] i2cset: " Wolfram Sang
  2024-08-14 18:03 ` [PATCH i2c-tools 1/2] i2cget: " Wolfram Sang
  0 siblings, 2 replies; 4+ messages in thread
From: Wolfram Sang @ 2024-08-11  9:34 UTC (permalink / raw)
  To: linux-i2c; +Cc: Wolfram Sang

The check distinguishing the 'h' and '?' parameters is dead code. Found
by cppcheck:

i2cget.c:193:4: warning: Statements following noreturn function 'help()' will never be executed. [unreachableCode]

Add a parameter to help saying how it should exit. Remove a forward
declaration while here.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2cget.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/tools/i2cget.c b/tools/i2cget.c
index b28ab38..17d78bf 100644
--- a/tools/i2cget.c
+++ b/tools/i2cget.c
@@ -31,9 +31,7 @@
 #include "util.h"
 #include "../version.h"
 
-static void help(void) __attribute__ ((noreturn));
-
-static void help(void)
+static void __attribute__ ((noreturn)) help(int status)
 {
 	fprintf(stderr,
 		"Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE [LENGTH]]]\n"
@@ -48,7 +46,7 @@ static void help(void)
 		"    Append p for SMBus PEC\n"
 		"  LENGTH is the I2C block data length (between 1 and %d, default %d)\n",
 		I2C_SMBUS_BLOCK_MAX, I2C_SMBUS_BLOCK_MAX);
-	exit(1);
+	exit(status);
 }
 
 static int check_funcs(int file, int size, int daddress, int pec)
@@ -189,8 +187,7 @@ int main(int argc, char *argv[])
 		case 'a': all_addrs = 1; break;
 		case 'h':
 		case '?':
-			help();
-			exit(opt == '?');
+			help(opt == '?');
 		}
 	}
 
@@ -200,22 +197,22 @@ int main(int argc, char *argv[])
 	}
 
 	if (argc < optind + 2)
-		help();
+		help(1);
 
 	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0)
-		help();
+		help(1);
 
 	address = parse_i2c_address(argv[optind+1], all_addrs);
 	if (address < 0)
-		help();
+		help(1);
 
 	if (argc > optind + 2) {
 		size = I2C_SMBUS_BYTE_DATA;
 		daddress = strtol(argv[optind+2], &end, 0);
 		if (*end || daddress < 0 || daddress > 0xff) {
 			fprintf(stderr, "Error: Data address invalid!\n");
-			help();
+			help(1);
 		}
 	} else {
 		size = I2C_SMBUS_BYTE;
@@ -231,24 +228,24 @@ int main(int argc, char *argv[])
 		case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
 		default:
 			fprintf(stderr, "Error: Invalid mode!\n");
-			help();
+			help(1);
 		}
 		pec = argv[optind+3][1] == 'p';
 		if (size == I2C_SMBUS_I2C_BLOCK_DATA && pec) {
 			fprintf(stderr, "Error: PEC not supported for I2C block data!\n");
-			help();
+			help(1);
 		}
 	}
 
 	if (argc > optind + 4) {
 		if (size != I2C_SMBUS_I2C_BLOCK_DATA) {
 			fprintf(stderr, "Error: Length only valid for I2C block data!\n");
-			help();
+			help(1);
 		}
 		length = strtol(argv[optind+4], &end, 0);
 		if (*end || length < 1 || length > I2C_SMBUS_BLOCK_MAX) {
 			fprintf(stderr, "Error: Length invalid!\n");
-			help();
+			help(1);
 		}
 	} else {
 		length = I2C_SMBUS_BLOCK_MAX;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH i2c-tools 2/2] i2cset: printing help is not an error
  2024-08-11  9:34 [PATCH i2c-tools 1/2] i2cget: printing help is not an error Wolfram Sang
@ 2024-08-11  9:34 ` Wolfram Sang
  2024-08-14 18:04   ` Wolfram Sang
  2024-08-14 18:03 ` [PATCH i2c-tools 1/2] i2cget: " Wolfram Sang
  1 sibling, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2024-08-11  9:34 UTC (permalink / raw)
  To: linux-i2c; +Cc: Wolfram Sang

The check distinguishing the 'h' and '?' parameters is dead code. Found
by cppcheck:

i2cset.c:177:4: warning: Statements following noreturn function 'help()' will never be executed. [unreachableCode]

Add a parameter to help saying how it should exit. Remove a forward
declaration while here.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 tools/i2cset.c | 41 +++++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 22 deletions(-)

diff --git a/tools/i2cset.c b/tools/i2cset.c
index dd4fdc2..aaf7faf 100644
--- a/tools/i2cset.c
+++ b/tools/i2cset.c
@@ -28,9 +28,7 @@
 #include "util.h"
 #include "../version.h"
 
-static void help(void) __attribute__ ((noreturn));
-
-static void help(void)
+static void __attribute__ ((noreturn)) help(int status)
 {
 	fprintf(stderr,
 		"Usage: i2cset [-f] [-y] [-m MASK] [-r] [-a] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE] ... [MODE]\n"
@@ -43,7 +41,7 @@ static void help(void)
 		"    i (I2C block data)\n"
 		"    s (SMBus block data)\n"
 		"    Append p for SMBus PEC\n");
-	exit(1);
+	exit(status);
 }
 
 static int check_funcs(int file, int size, int pec)
@@ -173,8 +171,7 @@ int main(int argc, char *argv[])
 		case 'a': all_addrs = 1; break;
 		case 'h':
 		case '?':
-			help();
-			exit(opt == '?');
+			help(opt == '?');
 		}
 	}
 
@@ -184,20 +181,20 @@ int main(int argc, char *argv[])
 	}
 
 	if (argc < optind + 3)
-		help();
+		help(1);
 
 	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0)
-		help();
+		help(1);
 
 	address = parse_i2c_address(argv[optind+1], all_addrs);
 	if (address < 0)
-		help();
+		help(1);
 
 	daddress = strtol(argv[optind+2], &end, 0);
 	if (*end || daddress < 0 || daddress > 0xff) {
 		fprintf(stderr, "Error: Data address invalid!\n");
-		help();
+		help(1);
 	}
 
 	/* check for command/mode */
@@ -218,7 +215,7 @@ int main(int argc, char *argv[])
 		if (strlen(argv[argc-1]) > 2
 		    || (strlen(argv[argc-1]) == 2 && argv[argc-1][1] != 'p')) {
 			fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
-			help();
+			help(1);
 		}
 		switch (argv[argc-1][0]) {
 		case 'b': size = I2C_SMBUS_BYTE_DATA; break;
@@ -227,25 +224,25 @@ int main(int argc, char *argv[])
 		case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
 		default:
 			fprintf(stderr, "Error: Invalid mode '%s'!\n", argv[argc-1]);
-			help();
+			help(1);
 		}
 		pec = argv[argc-1][1] == 'p';
 		if (size == I2C_SMBUS_BLOCK_DATA || size == I2C_SMBUS_I2C_BLOCK_DATA) {
 			if (pec && size == I2C_SMBUS_I2C_BLOCK_DATA) {
 				fprintf(stderr, "Error: PEC not supported for I2C block writes!\n");
-				help();
+				help(1);
 			}
 			if (maskp) {
 				fprintf(stderr, "Error: Mask not supported for block writes!\n");
-				help();
+				help(1);
 			}
 			if (argc > (int)sizeof(block) + optind + 4) {
 				fprintf(stderr, "Error: Too many arguments!\n");
-				help();
+				help(1);
 			}
 		} else if (argc != optind + 5) {
 			fprintf(stderr, "Error: Too many arguments!\n");
-			help();
+			help(1);
 		}
 	}
 
@@ -262,12 +259,12 @@ int main(int argc, char *argv[])
 		value = strtol(argv[optind+3], &end, 0);
 		if (*end || value < 0) {
 			fprintf(stderr, "Error: Data value invalid!\n");
-			help();
+			help(1);
 		}
 		if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff)
 		    || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) {
 			fprintf(stderr, "Error: Data value out of range!\n");
-			help();
+			help(1);
 		}
 		break;
 	case I2C_SMBUS_BLOCK_DATA:
@@ -276,11 +273,11 @@ int main(int argc, char *argv[])
 			value = strtol(argv[optind + len + 3], &end, 0);
 			if (*end || value < 0) {
 				fprintf(stderr, "Error: Data value invalid!\n");
-				help();
+				help(1);
 			}
 			if (value > 0xff) {
 				fprintf(stderr, "Error: Data value out of range!\n");
-				help();
+				help(1);
 			}
 			block[len] = value;
 		}
@@ -295,12 +292,12 @@ int main(int argc, char *argv[])
 		vmask = strtol(maskp, &end, 0);
 		if (*end || vmask == 0) {
 			fprintf(stderr, "Error: Data value mask invalid!\n");
-			help();
+			help(1);
 		}
 		if (((size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA)
 		     && vmask > 0xff) || vmask > 0xffff) {
 			fprintf(stderr, "Error: Data value mask out of range!\n");
-			help();
+			help(1);
 		}
 	}
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH i2c-tools 1/2] i2cget: printing help is not an error
  2024-08-11  9:34 [PATCH i2c-tools 1/2] i2cget: printing help is not an error Wolfram Sang
  2024-08-11  9:34 ` [PATCH i2c-tools 2/2] i2cset: " Wolfram Sang
@ 2024-08-14 18:03 ` Wolfram Sang
  1 sibling, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2024-08-14 18:03 UTC (permalink / raw)
  To: linux-i2c

[-- Attachment #1: Type: text/plain, Size: 471 bytes --]

On Sun, Aug 11, 2024 at 11:34:10AM +0200, Wolfram Sang wrote:
> The check distinguishing the 'h' and '?' parameters is dead code. Found
> by cppcheck:
> 
> i2cget.c:193:4: warning: Statements following noreturn function 'help()' will never be executed. [unreachableCode]
> 
> Add a parameter to help saying how it should exit. Remove a forward
> declaration while here.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Pushed to master.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH i2c-tools 2/2] i2cset: printing help is not an error
  2024-08-11  9:34 ` [PATCH i2c-tools 2/2] i2cset: " Wolfram Sang
@ 2024-08-14 18:04   ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2024-08-14 18:04 UTC (permalink / raw)
  To: linux-i2c

[-- Attachment #1: Type: text/plain, Size: 471 bytes --]

On Sun, Aug 11, 2024 at 11:34:11AM +0200, Wolfram Sang wrote:
> The check distinguishing the 'h' and '?' parameters is dead code. Found
> by cppcheck:
> 
> i2cset.c:177:4: warning: Statements following noreturn function 'help()' will never be executed. [unreachableCode]
> 
> Add a parameter to help saying how it should exit. Remove a forward
> declaration while here.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Pushed to master.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-08-14 18:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-11  9:34 [PATCH i2c-tools 1/2] i2cget: printing help is not an error Wolfram Sang
2024-08-11  9:34 ` [PATCH i2c-tools 2/2] i2cset: " Wolfram Sang
2024-08-14 18:04   ` Wolfram Sang
2024-08-14 18:03 ` [PATCH i2c-tools 1/2] i2cget: " Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox