From nobody@FreeBSD.org  Wed Mar 19 09:49:41 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 0550B59F
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Mar 2014 09:49:41 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id CC580DA5
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Mar 2014 09:49:40 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s2J9nenL091803
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 19 Mar 2014 09:49:40 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s2J9neLQ091802;
	Wed, 19 Mar 2014 09:49:40 GMT
	(envelope-from nobody)
Message-Id: <201403190949.s2J9neLQ091802@cgiserv.freebsd.org>
Date: Wed, 19 Mar 2014 09:49:40 GMT
From: Makoto Kishimoto <ksmakoto@dd.iij4u.or.jp>
To: freebsd-gnats-submit@FreeBSD.org
Subject: bc(1) should print error message to stderr
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         187728
>Category:       bin
>Synopsis:       [patch] bc(1) should print error message to stderr
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar 19 09:50:00 UTC 2014
>Closed-Date:    
>Last-Modified:  Sun Mar 23 18:30:00 UTC 2014
>Originator:     Makoto Kishimoto
>Release:        9.2-STABLE
>Organization:
N/A
>Environment:
FreeBSD norikura.localdomain 9.2-STABLE FreeBSD 9.2-STABLE #1 r257235: Mon Oct 28 19:56:00 JST 2013     ksmakoto@norikura.localdomain:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
bc(1) should prints error message to stderr. For example,

UNIX (Heirloom) bc prints to stderr
$ echo ':' | /usr/local/heirloom/usr/5bin/bc > /dev/null
syntax error on line 1, teletype
$ echo ':' | /usr/local/heirloom/usr/5bin/bc 2> /dev/null
$

GNU bc prints to stderr
$ echo ':' | /usr/local/bin/bc > /dev/null
(standard_in) 1: illegal character: :
$ echo ':' | /usr/local/bin/bc 2> /dev/null
$

FreeBSD (OpenBSD) bc prints to stdout
$ echo ':' | /usr/bin/bc > /dev/null
$ echo ':' | /usr/bin/bc 2> /dev/null
bc: stdin:1: illegal character: : unexpected
$

>How-To-Repeat:
see Full Description
>Fix:
see attached patch

Patch attached with submission follows:

Index: bc.y
===================================================================
--- bc.y	(revision 263333)
+++ bc.y	(working copy)
@@ -969,25 +969,19 @@
 	int n;
 
 	if (yyin != NULL && feof(yyin))
-		n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF",
+		n = asprintf(&str, "%s: %s:%d: %s: unexpected EOF\n",
 		    __progname, filename, lineno, s);
 	else if (isspace(yytext[0]) || !isprint(yytext[0]))
 		n = asprintf(&str,
-		    "%s: %s:%d: %s: ascii char 0x%02x unexpected",
+		    "%s: %s:%d: %s: ascii char 0x%02x unexpected\n",
 		    __progname, filename, lineno, s, yytext[0]);
 	else
-		n = asprintf(&str, "%s: %s:%d: %s: %s unexpected",
+		n = asprintf(&str, "%s: %s:%d: %s: %s unexpected\n",
 		    __progname, filename, lineno, s, yytext);
 	if (n == -1)
 		err(1, NULL);
 
-	fputs("c[", stdout);
-	for (p = str; *p != '\0'; p++) {
-		if (*p == '[' || *p == ']' || *p =='\\')
-			putchar('\\');
-		putchar(*p);
-	}
-	fputs("]pc\n", stdout);
+	fputs(str, stderr);
 	free(str);
 }
 


>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, ksmakoto@dd.iij4u.or.jp
Cc:  
Subject: Re: bin/187728: [patch] bc(1) should print error message to stderr
Date: Sun, 23 Mar 2014 19:20:25 +0100

 In PR bin/187728, you wrote:
 > bc(1) should prints error message to stderr.
 
 POSIX seems to require this in interactive mode; in non-interactive
 mode, invalid input causes undefined behaviour.
 
 Note that the patch changes more than just the destination of the error
 message. The code in the repository prints the error message if and when
 dc reaches the point of the error, rather than immediately when the
 error is detected in the patched version.
 
 Since the patched version is simpler, it seems that the original code
 may be deliberately written that way.
 
 -- 
 Jilles Tjoelker
>Unformatted:
