NetBSD Problem Report #44733

From www@NetBSD.org  Thu Mar 17 02:42:22 2011
Return-Path: <www@NetBSD.org>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id C595F63B99B
	for <gnats-bugs@gnats.NetBSD.org>; Thu, 17 Mar 2011 02:42:22 +0000 (UTC)
Message-Id: <20110317024221.CC4CD63B995@www.NetBSD.org>
Date: Thu, 17 Mar 2011 02:42:21 +0000 (UTC)
From: dwheeler@dwheeler.com
Reply-To: dwheeler@dwheeler.com
To: gnats-bugs@NetBSD.org
Subject: [PATCH] Have test/[ accept "==" as a synonym for "="
X-Send-Pr-Version: www-1.0

>Number:         44733
>Category:       bin
>Synopsis:       [PATCH] Have test/[ accept "==" as a synonym for "="
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Mar 17 02:45:00 +0000 2011
>Last-Modified:  Fri Mar 18 15:55:01 +0000 2011
>Originator:     David A. Wheeler
>Release:        -current
>Organization:
>Environment:
NetBSD  5.1 NetBSD 5.1 (GENERIC) #0: Sun Nov  7 14:39:56 UTC 2010  builds@b6.netbsd.org:/home/builds/ab/netbsd-5-1-RELEASE/i386/201011061943Z-obj/home/builds/ab/netbsd-5-1-RELEASE/src/sys/arch/i386/compile/GENERIC i386

But really, the environment doesn't matter.
>Description:
Currently test/[ does not support "==" as a synonym for the
"=" binary operator ("are strings equal").
Although it's not in the POSIX spec, it is widely used and implemented;
"==" is accepted by FreeBSD-current /bin/sh and /bin/test,
OpenBSD's /bin/sh, bash, ksh, and busybox ash.

The included patch adds support for "==" in test/[.

>How-To-Repeat:
Running:
 /bin/test a == a && echo hi
should print "hi".
Currently it prints test: ==: unexpected operator

Running:
 [ a == a ] && echo hi
should print "hi".
Currently it prints [: ==: unexpected operator



>Fix:
--- test.c.orig	2008-09-10 19:00:51.000000000 +0000
+++ test.c	2011-03-15 22:43:50.000000000 +0000
@@ -41,7 +41,7 @@
 	unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
 		"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";

-	binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
+	binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
 			"-nt"|"-ot"|"-ef";
 	operand ::= <any legal UNIX file name>
 */
@@ -113,6 +113,7 @@

 static const struct t_op cop2[] = {
 	{"!=",	STRNE,	BINOP},
+	{"==",	STREQ,	BINOP},
 };

 static const struct t_op mop3[] = {
@@ -623,6 +624,8 @@
 			    compare1);
 		else if (strcmp(s, cop2[0].op_text) == 0)
 			return cop2;
+		else if (strcmp(s, cop2[1].op_text) == 0)
+			return &(cop2[1]);
 		else
 			return NULL;
 	}


--- test.1.orig	2009-11-10 18:19:46.000000000 +0000
+++ test.1	2011-03-15 20:19:17.000000000 +0000
@@ -200,6 +200,12 @@
 and
 .Ar \&s\&2
 are identical.
+.It Ar \&s\&1 Cm \&== Ar \&s\&2
+True if the strings
+.Ar \&s\&1
+and
+.Ar \&s\&2
+are identical (synonym of =).
 .It Ar \&s\&1 Cm \&!= Ar \&s\&2
 True if the strings
 .Ar \&s\&1

>Audit-Trail:
From: David Holland <dholland-bugs@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Thu, 17 Mar 2011 09:37:02 +0000

 On Thu, Mar 17, 2011 at 02:45:00AM +0000, dwheeler@dwheeler.com wrote:
  > Currently test/[ does not support "==" as a synonym for the
  > "=" binary operator ("are strings equal").

 Doesn't bash's == have different semantics?

 -- 
 David A. Holland
 dholland@netbsd.org

From: Robert Elz <kre@munnari.OZ.AU>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Thu, 17 Mar 2011 17:36:08 +0700

     Date:        Thu, 17 Mar 2011 09:40:04 +0000 (UTC)
     From:        David Holland <dholland-bugs@NetBSD.org>
     Message-ID:  <20110317094004.8E01763B995@www.NetBSD.org>

   |  Doesn't bash's == have different semantics?

 No, in [ (or test) the semantics of = and == in bash are the same.

 That said, == is just plain stupid, was never needed, and should
 definitely NOT be added to either /bin/test or /bin/sh in NetBSD.

 Obviously bash is not going to drop == now - I tried to get the doc
 at least fixed to try and persuade its users to use = rather than ==.
 I was partially successful, the bash doc at least now says that == is
 not portable (but doesn't quite go as far as telling people not to use it).

 kre

From: Jukka Ruohonen <jruohonen@iki.fi>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Thu, 17 Mar 2011 13:03:14 +0200

 On Thu, Mar 17, 2011 at 10:40:03AM +0000, Robert Elz wrote:
 >  That said, == is just plain stupid, was never needed, and should
 >  definitely NOT be added to either /bin/test or /bin/sh in NetBSD.

 Why?

 - Jukka.

From: "Valeriy E. Ushakov" <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Thu, 17 Mar 2011 16:08:41 +0300

 On Thu, Mar 17, 2011 at 11:05:03 +0000, Jukka Ruohonen wrote:

 > On Thu, Mar 17, 2011 at 10:40:03AM +0000, Robert Elz wrote:
 >
 > >  That said, == is just plain stupid, was never needed, and should
 > >  definitely NOT be added to either /bin/test or /bin/sh in NetBSD.
 >
 > Why?

 Why would we want to?  Compatibility with bash?  But non-trivial bash
 scripts quite often have other bashisms in them, so anyone who depends
 on running bash scripts is probably better served by just installing
 bash package.

 (And no, I'm not a bash basher.  I've been using it as my shell since
 1992 or so).

 -uwe

From: "David A. Wheeler" <dwheeler@dwheeler.com>
To: gnats-bugs@NetBSD.org
Cc: netbsd-bugs@netbsd.org, gnats-admin@netbsd.org, uwe@stderr.spb.ru,
 jruohonen@iki.fi, kre@munnari.OZ.AU, holland-bugs@netbsd.org
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Thu, 17 Mar 2011 14:41:50 -0400 (EDT)

 Regarding the proposed patch to implement "=3D=3D" as a synonym for "=3D=3D=
 " in test/[:

 Valeriy E. Ushakov:
 >  Why would we want to?  Compatibility with bash?

 Not primarily, though that has some benefit.  There are several reasons to =
 implement "=3D=3D" as a synonym for "=3D".

 The underlying reason is that "=3D" is also used for shell variable assignm=
 ent.  Using the SAME spelling for "is-equal-to" and "assignment" is unfortu=
 nate, and is something that many languages avoid.  Many languages that use =
 "=3D" for assignment use a SEPARATE "=3D=3D" operator for comparison; this =
 includes C, C++, Java, C#, Python, and Perl.  Many languages (like Pascal) =
 that use "=3D" for comparison use another spelling like ":=3D" for assignme=
 nt, again, to keep their spellings separate (these languages can often disa=
 mbiguate from context... but they intentionally don't).  It is odd that the=
  shell, unlike all these languages, conflates their spellings.  It's too la=
 te to get rid of "=3D" for comparison, but it's easy to add "=3D=3D" as a s=
 ynonym, which is what is proposed.  So adding this ability (1) clarifies wh=
 ether assignment or comparison is being used, and (2) improves consistency =
 between shell and other languages that use "=3D" for assignment.

 This is NOT a bash-unique extension, so I argue that it is NOT a bashism.  =
 LOTS of shells ALREADY implement this.  This is implemented in not only bas=
 h, but also ksh, busybox ash, FreeBSD-current's /bin/sh and /bin/test (see =
 http://svn.freebsd.org/base/head/bin/test/test.c), and OpenBSD's /bin/sh. A=
 t least.  *ALL* of these implementations agree that "=3D=3D" is a synonym f=
 or "=3D", so there's no question about "different semantics so which one sh=
 ould we implement?".

 This extension is already in wide use in shell scripts, even ones not writt=
 en for bash.  Several NetBSD bugs have been filed because NetBSD can't hand=
 le "=3D=3D".  Can you change the scripts?  Sure.  But why not add the facil=
 ity to NetBSD instead?  Nothing in the POSIX specification forbids it, it's=
  trivial to add, and it'd make it easier to port programs to NetBSD.

 >  But non-trivial bash
 >  scripts quite often have other bashisms in them, so anyone who depends
 >  on running bash scripts is probably better served by just installing
 >  bash package.

 As noted above, a shell script originally written with a shell OTHER than b=
 ash, such as OpenBSD's /bin/sh or Busybox /bin/sh, can use "=3D=3D", and th=
 ese shells don't support many extensions.  Busybox focuses on being as tiny=
  as possible, and even they found it useful to support "=3D=3D".

 --- David A. Wheeler

From: Jukka Ruohonen <jruohonen@iki.fi>
To: gnats-bugs@NetBSD.org
Cc: dwheeler@dwheeler.com
Subject: Re: bin/44733: [PATCH] Have test/[ accept "==" as a synonym for "="
Date: Fri, 18 Mar 2011 17:50:00 +0200

 On Thu, Mar 17, 2011 at 06:45:02PM +0000, David A. Wheeler wrote:
 >  Not primarily, though that has some benefit.  There are several reasons
 >  to implement "==" as a synonym for "=".
 >
 > [...]
 >

 All rational and well-justified points in my opinion.

 - Jukka.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.