Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 20 Dec 2010 23:06:57 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r216606 - in head: bin/sh/bltin tools/regression/bin/sh/builtins usr.bin/printf
Message-ID:  <201012202306.oBKN6v3i096167@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Mon Dec 20 23:06:57 2010
New Revision: 216606
URL: http://svn.freebsd.org/changeset/base/216606

Log:
  sh: Make warnings in the printf builtin non-fatal, like in the program.
  
  The #define for warnx now behaves much like the libc function (except that
  it uses sh command name and output).
  
  Also, it now uses C99 __VA_ARGS__ so there is no need for three different
  macros for 0, 1 or 2 parameters.

Added:
  head/tools/regression/bin/sh/builtins/printf3.0   (contents, props changed)
  head/tools/regression/bin/sh/builtins/printf4.0   (contents, props changed)
Modified:
  head/bin/sh/bltin/bltin.h
  head/usr.bin/printf/printf.c

Modified: head/bin/sh/bltin/bltin.h
==============================================================================
--- head/bin/sh/bltin/bltin.h	Mon Dec 20 22:56:50 2010	(r216605)
+++ head/bin/sh/bltin/bltin.h	Mon Dec 20 23:06:57 2010	(r216606)
@@ -57,21 +57,12 @@
 #define fwrite(ptr, size, nmemb, file) outbin(ptr, (size) * (nmemb), file)
 #define fflush flushout
 #define INITARGS(argv)
-#define warnx1(a, b, c) {				\
-	char buf[64];					\
-	(void)snprintf(buf, sizeof(buf), a);		\
-	error("%s", buf);				\
-}
-#define warnx2(a, b, c) {				\
-	char buf[64];					\
-	(void)snprintf(buf, sizeof(buf), a, b);		\
-	error("%s", buf);				\
-}
-#define warnx3(a, b, c) {				\
-	char buf[64];					\
-	(void)snprintf(buf, sizeof(buf), a, b, c);	\
-	error("%s", buf);				\
-}
+#define warnx(...) do {					\
+	out2fmt_flush("%s: ", commandname);		\
+	out2fmt_flush(__VA_ARGS__);			\
+	out2fmt_flush("\n");				\
+	} while (0)
+#define errx(exitstatus, ...) error(__VA_ARGS__)
 
 #else
 #undef NULL

Added: head/tools/regression/bin/sh/builtins/printf3.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/builtins/printf3.0	Mon Dec 20 23:06:57 2010	(r216606)
@@ -0,0 +1,5 @@
+# $FreeBSD$
+
+set -e
+v=$(! printf "%d" @wrong 2>/dev/null)
+[ "$v" = "0" ]

Added: head/tools/regression/bin/sh/builtins/printf4.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/bin/sh/builtins/printf4.0	Mon Dec 20 23:06:57 2010	(r216606)
@@ -0,0 +1,5 @@
+# $FreeBSD$
+
+set -e
+v=$(! printf "%d" 4wrong 2>/dev/null)
+[ "$v" = "4" ]

Modified: head/usr.bin/printf/printf.c
==============================================================================
--- head/usr.bin/printf/printf.c	Mon Dec 20 22:56:50 2010	(r216605)
+++ head/usr.bin/printf/printf.c	Mon Dec 20 23:06:57 2010	(r216606)
@@ -60,10 +60,6 @@ static const char rcsid[] =
 #include "bltin/bltin.h"
 #include "memalloc.h"
 #include "error.h"
-#else
-#define	warnx1(a, b, c)		warnx(a)
-#define	warnx2(a, b, c)		warnx(a, b)
-#define	warnx3(a, b, c)		warnx(a, b, c)
 #endif
 
 #define PF(f, func) do { \
@@ -165,7 +161,7 @@ main(int argc, char *argv[])
 		}
 
 		if (end == 1) {
-			warnx1("missing format character", NULL, NULL);
+			warnx("missing format character");
 #ifdef SHELL
 			INTON;
 #endif
@@ -226,7 +222,7 @@ printf_doformat(char *start, int *rval)
 	} else
 		haveprec = 0;
 	if (!*fmt) {
-		warnx1("missing format character", NULL, NULL);
+		warnx("missing format character");
 		return (NULL);
 	}
 
@@ -244,7 +240,7 @@ printf_doformat(char *start, int *rval)
 		mod_ldbl = 1;
 		fmt++;
 		if (!strchr("aAeEfFgG", *fmt)) {
-			warnx2("bad modifier L for %%%c", *fmt, NULL);
+			warnx("bad modifier L for %%%c", *fmt);
 			return (NULL);
 		}
 	} else {
@@ -266,7 +262,7 @@ printf_doformat(char *start, int *rval)
 		p = strdup(getstr());
 #endif
 		if (p == NULL) {
-			warnx2("%s", strerror(ENOMEM), NULL);
+			warnx("%s", strerror(ENOMEM));
 			return (NULL);
 		}
 		getout = escape(p, 0, &len);
@@ -328,7 +324,7 @@ printf_doformat(char *start, int *rval)
 		break;
 	}
 	default:
-		warnx2("illegal format character %c", convch, NULL);
+		warnx("illegal format character %c", convch);
 		return (NULL);
 	}
 	*fmt = nextch;
@@ -352,7 +348,7 @@ mknum(char *str, char ch)
 		if ((newcopy = realloc(copy, newlen)) == NULL)
 #endif
 		{
-			warnx2("%s", strerror(ENOMEM), NULL);
+			warnx("%s", strerror(ENOMEM));
 			return (NULL);
 		}
 		copy = newcopy;
@@ -465,7 +461,7 @@ getint(int *ip)
 		return (1);
 	rval = 0;
 	if (val < INT_MIN || val > INT_MAX) {
-		warnx3("%s: %s", *gargv, strerror(ERANGE));
+		warnx("%s: %s", *gargv, strerror(ERANGE));
 		rval = 1;
 	}
 	*ip = (int)val;
@@ -496,15 +492,15 @@ getnum(intmax_t *ip, uintmax_t *uip, int
 	else
 		*uip = strtoumax(*gargv, &ep, 0);
 	if (ep == *gargv) {
-		warnx2("%s: expected numeric value", *gargv, NULL);
+		warnx("%s: expected numeric value", *gargv);
 		rval = 1;
 	}
 	else if (*ep != '\0') {
-		warnx2("%s: not completely converted", *gargv, NULL);
+		warnx("%s: not completely converted", *gargv);
 		rval = 1;
 	}
 	if (errno == ERANGE) {
-		warnx3("%s: %s", *gargv, strerror(ERANGE));
+		warnx("%s: %s", *gargv, strerror(ERANGE));
 		rval = 1;
 	}
 	++gargv;
@@ -532,14 +528,14 @@ getfloating(long double *dp, int mod_ldb
 	else
 		*dp = strtod(*gargv, &ep);
 	if (ep == *gargv) {
-		warnx2("%s: expected numeric value", *gargv, NULL);
+		warnx("%s: expected numeric value", *gargv);
 		rval = 1;
 	} else if (*ep != '\0') {
-		warnx2("%s: not completely converted", *gargv, NULL);
+		warnx("%s: not completely converted", *gargv);
 		rval = 1;
 	}
 	if (errno == ERANGE) {
-		warnx3("%s: %s", *gargv, strerror(ERANGE));
+		warnx("%s: %s", *gargv, strerror(ERANGE));
 		rval = 1;
 	}
 	++gargv;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201012202306.oBKN6v3i096167>