util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid
@ 2025-05-28  9:37 Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 2/4] lib/strutils: call gettext() only when argument of --hyperlink " Benno Schulenberg
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Benno Schulenberg @ 2025-05-28  9:37 UTC (permalink / raw)
  To: util-linux

Instead of calling gettext() before starting to interpret the argument
of option -L/--color, call it only when it's needed: when the argument
is not recognized.

Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
---
 disk-utils/cfdisk.c  | 3 +--
 disk-utils/fdisk.c   | 3 +--
 disk-utils/sfdisk.c  | 3 +--
 include/colors.h     | 2 +-
 lib/colors.c         | 7 ++++---
 misc-utils/cal.c     | 3 +--
 sys-utils/dmesg.c    | 3 +--
 text-utils/hexdump.c | 3 +--
 8 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c
index fdd74fbf9..e4e16001e 100644
--- a/disk-utils/cfdisk.c
+++ b/disk-utils/cfdisk.c
@@ -2781,8 +2781,7 @@ int main(int argc, char *argv[])
 		case 'L':
 			colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				colormode = colormode_or_err(optarg);
 			break;
                 case 'r':
                         read_only = 1;
diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c
index 6493e2767..727178da4 100644
--- a/disk-utils/fdisk.c
+++ b/disk-utils/fdisk.c
@@ -1239,8 +1239,7 @@ int main(int argc, char **argv)
 		case 'L':
 			colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				colormode = colormode_or_err(optarg);
 			break;
 		case 'n':
 			noauto_pt = 1;
diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c
index 41b5558ea..7f2f3deea 100644
--- a/disk-utils/sfdisk.c
+++ b/disk-utils/sfdisk.c
@@ -2437,8 +2437,7 @@ int main(int argc, char *argv[])
 		case OPT_COLOR:
 			colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				colormode = colormode_or_err(optarg);
 			break;
 		case OPT_MOVEDATA:
 			sf->movedata = 1;
diff --git a/include/colors.h b/include/colors.h
index 770161ae0..c02f61a57 100644
--- a/include/colors.h
+++ b/include/colors.h
@@ -33,7 +33,7 @@ enum colortmode {
 #endif
 
 extern int colormode_from_string(const char *str);
-extern int colormode_or_err(const char *str, const char *errmsg);
+extern int colormode_or_err(const char *str);
 
 /* Initialize the global variable UL_COLOR_TERM_OK */
 extern int colors_init(int mode, const char *util_name);
diff --git a/lib/colors.c b/lib/colors.c
index 4852a87cc..47d69cabc 100644
--- a/lib/colors.c
+++ b/lib/colors.c
@@ -34,6 +34,7 @@
 #include "c.h"
 #include "cctype.h"
 #include "colors.h"
+#include "nls.h"
 #include "pathnames.h"
 #include "strutils.h"
 
@@ -755,14 +756,14 @@ int colormode_from_string(const char *str)
 /*
  * Parses @str and exit(EXIT_FAILURE) on error
  */
-int colormode_or_err(const char *str, const char *errmsg)
+int colormode_or_err(const char *str)
 {
 	const char *p = str && *str == '=' ? str + 1 : str;
 	int colormode;
 
 	colormode = colormode_from_string(p);
 	if (colormode < 0)
-		errx(EXIT_FAILURE, "%s: '%s'", errmsg, p);
+		errx(EXIT_FAILURE, _("unsupported color mode: %s"), p);
 
 	return colormode;
 }
@@ -791,7 +792,7 @@ int main(int argc, char *argv[])
 			color_scheme = optarg;
 			break;
 		case 'm':
-			mode = colormode_or_err(optarg, "unsupported color mode");
+			mode = colormode_or_err(optarg);
 			break;
 		case 'n':
 			name = optarg;
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 8c0eb3638..09622165a 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -421,8 +421,7 @@ int main(int argc, char **argv)
 		case OPT_COLOR:
 			ctl.colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				ctl.colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				ctl.colormode = colormode_or_err(optarg);
 			break;
 		case OPT_REFORM:
 			ctl.reform_year = parse_reform_year(optarg);
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index 8dcf67c94..6584613c7 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -1756,8 +1756,7 @@ int main(int argc, char *argv[])
 		case 'L':
 			colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				colormode = colormode_or_err(optarg);
 			break;
 		case 'l':
 			ctl.fltr_lev= 1;
diff --git a/text-utils/hexdump.c b/text-utils/hexdump.c
index 36744554e..60bf6f94b 100644
--- a/text-utils/hexdump.c
+++ b/text-utils/hexdump.c
@@ -115,8 +115,7 @@ parse_args(int argc, char **argv, struct hexdump *hex)
 		case 'L':
 			colormode = UL_COLORMODE_AUTO;
 			if (optarg)
-				colormode = colormode_or_err(optarg,
-						_("unsupported color mode"));
+				colormode = colormode_or_err(optarg);
                         break;
 		case 'n':
 			hex->length = strtosize_or_err(optarg, _("failed to parse length"));
-- 
2.48.1


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

* [PATCH 2/4] lib/strutils: call gettext() only when argument of --hyperlink is invalid
  2025-05-28  9:37 [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Benno Schulenberg
@ 2025-05-28  9:37 ` Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 3/4] colrm: make two error messages actually say that something is wrong Benno Schulenberg
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Benno Schulenberg @ 2025-05-28  9:37 UTC (permalink / raw)
  To: util-linux

Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
---
 include/strutils.h   | 2 +-
 lib/strutils.c       | 4 ++--
 lsfd-cmd/lsfd.c      | 3 +--
 misc-utils/findmnt.c | 3 +--
 misc-utils/lsblk.c   | 3 +--
 5 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/include/strutils.h b/include/strutils.h
index a81fdea89..70f97f0c8 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -59,7 +59,7 @@ extern void strtotimespec_or_err(const char *str, struct timespec *ts,
 		const char *errmesg);
 extern time_t strtotime_or_err(const char *str, const char *errmesg);
 
-extern bool hyperlinkwanted_or_err(const char *mode, const char *errmesg);
+extern bool hyperlinkwanted(const char *mode);
 
 extern int isdigit_strend(const char *str, const char **end);
 #define isdigit_string(_s)	isdigit_strend(_s, NULL)
diff --git a/lib/strutils.c b/lib/strutils.c
index af538207a..64fefa878 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -524,7 +524,7 @@ time_t strtotime_or_err(const char *str, const char *errmesg)
 	return (time_t) user_input;
 }
 
-bool hyperlinkwanted_or_err(const char *mode, const char *errmesg)
+bool hyperlinkwanted(const char *mode)
 {
 	if (mode && strcmp(mode, "never") == 0)
 		return false;
@@ -535,7 +535,7 @@ bool hyperlinkwanted_or_err(const char *mode, const char *errmesg)
 	if (!mode || strcmp(mode, "auto") == 0)
 		return isatty(STDOUT_FILENO) ? true : false;
 
-	errx(EXIT_FAILURE, "%s: '%s'", errmesg, mode);
+	errx(EXIT_FAILURE, _("invalid argument of --hyperlink: %s"), mode);
 }
 
 /*
diff --git a/lsfd-cmd/lsfd.c b/lsfd-cmd/lsfd.c
index d05a09396..ffaca8c31 100644
--- a/lsfd-cmd/lsfd.c
+++ b/lsfd-cmd/lsfd.c
@@ -2648,8 +2648,7 @@ int main(int argc, char *argv[])
 				err(EXIT_FAILURE, _("failed to drop privilege"));
 			break;
 		case OPT_HYPERLINK:
-			if (hyperlinkwanted_or_err(optarg,
-					_("invalid hyperlink argument")))
+			if (hyperlinkwanted(optarg))
 				ctl.uri = xgethosturi(NULL);
 			break;
 		case 'V':
diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
index 130ac1b13..1211bfa07 100644
--- a/misc-utils/findmnt.c
+++ b/misc-utils/findmnt.c
@@ -2026,8 +2026,7 @@ int main(int argc, char *argv[])
 			findmnt.flags |= FL_SHADOWED;
 			break;
 		case FINDMNT_OPT_HYPERLINK:
-			if (hyperlinkwanted_or_err(optarg,
-					_("invalid hyperlink argument")))
+			if (hyperlinkwanted(optarg))
 				findmnt.uri = xgethosturi(NULL);
 			break;
 		case FINDMNT_OPT_ID:
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index a65f5e4e6..da44bd631 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -2678,8 +2678,7 @@ int main(int argc, char *argv[])
 				errtryhelp(EXIT_FAILURE);
 			break;
 		case OPT_HYPERLINK:
-			if (hyperlinkwanted_or_err(optarg,
-					_("invalid hyperlink argument")))
+			if (hyperlinkwanted(optarg))
 				lsblk->uri = xgethosturi(NULL);
 			break;
 		case 'H':
-- 
2.48.1


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

* [PATCH 3/4] colrm: make two error messages actually say that something is wrong
  2025-05-28  9:37 [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 2/4] lib/strutils: call gettext() only when argument of --hyperlink " Benno Schulenberg
@ 2025-05-28  9:37 ` Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 4/4] textual: harmonize the wording of the error message for an invalid PID Benno Schulenberg
  2025-06-02 12:05 ` [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Karel Zak
  3 siblings, 0 replies; 5+ messages in thread
From: Benno Schulenberg @ 2025-05-28  9:37 UTC (permalink / raw)
  To: util-linux

Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
---
 text-utils/colrm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/text-utils/colrm.c b/text-utils/colrm.c
index b1370ac81..dd36d1827 100644
--- a/text-utils/colrm.c
+++ b/text-utils/colrm.c
@@ -180,9 +180,9 @@ int main(int argc, char **argv)
 		}
 
 	if (argc > 1)
-		first = strtoul_or_err(*++argv, _("first argument"));
+		first = strtoul_or_err(*++argv, _("invalid first argument"));
 	if (argc > 2)
-		last = strtoul_or_err(*++argv, _("second argument"));
+		last = strtoul_or_err(*++argv, _("invalid second argument"));
 
 	while (process_input(first, last))
 		;
-- 
2.48.1


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

* [PATCH 4/4] textual: harmonize the wording of the error message for an invalid PID
  2025-05-28  9:37 [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 2/4] lib/strutils: call gettext() only when argument of --hyperlink " Benno Schulenberg
  2025-05-28  9:37 ` [PATCH 3/4] colrm: make two error messages actually say that something is wrong Benno Schulenberg
@ 2025-05-28  9:37 ` Benno Schulenberg
  2025-06-02 12:05 ` [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Karel Zak
  3 siblings, 0 replies; 5+ messages in thread
From: Benno Schulenberg @ 2025-05-28  9:37 UTC (permalink / raw)
  To: util-linux

Having four different forms for the same basic message is unneeded.

Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
---
 misc-utils/kill.c      | 4 ++--
 misc-utils/lsclocks.c  | 2 +-
 misc-utils/lslocks.c   | 2 +-
 misc-utils/waitpid.c   | 2 +-
 schedutils/coresched.c | 5 ++---
 schedutils/taskset.c   | 3 +--
 sys-utils/setpriv.c    | 2 +-
 7 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index fefe0d891..81dd5f191 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -396,7 +396,7 @@ static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
 			if (2 < argc)
 				errx(EXIT_FAILURE, _("too many arguments"));
 			arg = argv[1];
-			pid = strtopid_or_err(arg, _("invalid pid argument"));
+			pid = strtopid_or_err(arg, _("invalid PID argument"));
 			print_process_signal_state(pid);
 			exit(EXIT_SUCCESS);
 		}
@@ -404,7 +404,7 @@ static char **parse_arguments(int argc, char **argv, struct kill_control *ctl)
 			pid_t pid;
 			char *p = strchr(arg, '=') + 1;
 
-			pid = strtopid_or_err(p, _("invalid pid argument"));
+			pid = strtopid_or_err(p, _("invalid PID argument"));
 			print_process_signal_state((pid_t)pid);
 			exit(EXIT_SUCCESS);
 		}
diff --git a/misc-utils/lsclocks.c b/misc-utils/lsclocks.c
index 376bc6e51..42a91fb94 100644
--- a/misc-utils/lsclocks.c
+++ b/misc-utils/lsclocks.c
@@ -593,7 +593,7 @@ int main(int argc, char **argv)
 			break;
 		case 'c':
 			cpu_clock = xmalloc(sizeof(*cpu_clock));
-			cpu_clock->pid = strtopid_or_err(optarg, _("failed to parse pid"));
+			cpu_clock->pid = strtopid_or_err(optarg, _("invalid PID argument"));
 			snprintf(cpu_clock->name, sizeof(cpu_clock->name),
 				 "%jd", (intmax_t) cpu_clock->pid);
 			list_add(&cpu_clock->head, &cpu_clocks);
diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c
index e58890207..4ed3a3716 100644
--- a/misc-utils/lslocks.c
+++ b/misc-utils/lslocks.c
@@ -383,7 +383,7 @@ static struct lock *get_lock(char *buf, struct override_info *oinfo, void *fallb
 				l->cmdname = xstrdup(oinfo->cmdname);
 			} else {
 				/* strtopid_or_err() is not suitable here; tok can be -1.*/
-				l->pid = strtos32_or_err(tok, _("failed to parse pid"));
+				l->pid = strtos32_or_err(tok, _("invalid PID argument"));
 				if (l->pid > 0) {
 					l->cmdname = pid_get_cmdname(l->pid);
 					if (!l->cmdname)
diff --git a/misc-utils/waitpid.c b/misc-utils/waitpid.c
index 4d77e1df4..379246b26 100644
--- a/misc-utils/waitpid.c
+++ b/misc-utils/waitpid.c
@@ -51,7 +51,7 @@ static pid_t *parse_pids(size_t n_strings, char * const *strings)
 	pid_t *pids = xcalloc(n_strings, sizeof(*pids));
 
 	for (size_t i = 0; i < n_strings; i++)
-		pids[i] = strtopid_or_err(strings[i], _("failed to parse pid"));
+		pids[i] = strtopid_or_err(strings[i], _("invalid PID argument"));
 
 	return pids;
 }
diff --git a/schedutils/coresched.c b/schedutils/coresched.c
index 7634d988c..419745897 100644
--- a/schedutils/coresched.c
+++ b/schedutils/coresched.c
@@ -232,12 +232,11 @@ static void parse_and_verify_arguments(int argc, char **argv, struct args *args)
 		switch (c) {
 		case 's':
 			args->src = strtopid_or_err(
-				optarg,
-				_("Failed to parse PID for -s/--source"));
+				optarg, _("invalid PID for -s/--source"));
 			break;
 		case 'd':
 			args->dest = strtopid_or_err(
-				optarg, _("Failed to parse PID for -d/--dest"));
+				optarg, _("invalid PID for -d/--dest"));
 			break;
 		case 't':
 			args->type = parse_core_sched_type(optarg);
diff --git a/schedutils/taskset.c b/schedutils/taskset.c
index 46fef5051..dedcdf602 100644
--- a/schedutils/taskset.c
+++ b/schedutils/taskset.c
@@ -187,8 +187,7 @@ int main(int argc, char **argv)
 			all_tasks = 1;
 			break;
 		case 'p':
-			pid = strtopid_or_err(argv[argc - 1],
-					      _("invalid PID argument"));
+			pid = strtopid_or_err(argv[argc - 1], _("invalid PID argument"));
 			break;
 		case 'c':
 			ts.use_list = 1;
diff --git a/sys-utils/setpriv.c b/sys-utils/setpriv.c
index 10274f5f6..d714650e8 100644
--- a/sys-utils/setpriv.c
+++ b/sys-utils/setpriv.c
@@ -473,7 +473,7 @@ static void parse_ptracer(struct privctx *opts, const char *str)
 	} else if (!strcmp(str, "none")) {
 		opts->ptracer = 0;
 	} else {
-		opts->ptracer = strtopid_or_err(str, _("failed to parse ptracer pid"));
+		opts->ptracer = strtopid_or_err(str, _("invalid PID argument"));
 	}
 }
 
-- 
2.48.1


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

* Re: [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid
  2025-05-28  9:37 [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Benno Schulenberg
                   ` (2 preceding siblings ...)
  2025-05-28  9:37 ` [PATCH 4/4] textual: harmonize the wording of the error message for an invalid PID Benno Schulenberg
@ 2025-06-02 12:05 ` Karel Zak
  3 siblings, 0 replies; 5+ messages in thread
From: Karel Zak @ 2025-06-02 12:05 UTC (permalink / raw)
  To: Benno Schulenberg; +Cc: util-linux

On Wed, May 28, 2025 at 11:37:01AM +0200, Benno Schulenberg wrote:
>  disk-utils/cfdisk.c  | 3 +--
>  disk-utils/fdisk.c   | 3 +--
>  disk-utils/sfdisk.c  | 3 +--
>  include/colors.h     | 2 +-
>  lib/colors.c         | 7 ++++---
>  misc-utils/cal.c     | 3 +--
>  sys-utils/dmesg.c    | 3 +--
>  text-utils/hexdump.c | 3 +--
>  8 files changed, 11 insertions(+), 16 deletions(-)

 Applied (all in set), thanks!

    Karel


-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


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

end of thread, other threads:[~2025-06-02 12:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-28  9:37 [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Benno Schulenberg
2025-05-28  9:37 ` [PATCH 2/4] lib/strutils: call gettext() only when argument of --hyperlink " Benno Schulenberg
2025-05-28  9:37 ` [PATCH 3/4] colrm: make two error messages actually say that something is wrong Benno Schulenberg
2025-05-28  9:37 ` [PATCH 4/4] textual: harmonize the wording of the error message for an invalid PID Benno Schulenberg
2025-06-02 12:05 ` [PATCH 1/4] lib/colors: call gettext() only when the argument of --color is invalid Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).