Module Name:    src
Committed By:   sjg
Date:           Fri Jun  5 19:20:46 UTC 2020

Modified Files:
        src/usr.bin/make: make.1 var.c

Log Message:
make: add :Or for reverse sort

:Or is more efficient than :O:[-1..1]

Reviewed by: christos


To generate a diff of this commit:
cvs rdiff -u -r1.280 -r1.281 src/usr.bin/make/make.1
cvs rdiff -u -r1.223 -r1.224 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/make.1
diff -u src/usr.bin/make/make.1:1.280 src/usr.bin/make/make.1:1.281
--- src/usr.bin/make/make.1:1.280	Mon Apr 27 20:03:08 2020
+++ src/usr.bin/make/make.1	Fri Jun  5 19:20:46 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: make.1,v 1.280 2020/04/27 20:03:08 christos Exp $
+.\"	$NetBSD: make.1,v 1.281 2020/06/05 19:20:46 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
 .\"
-.Dd April 27, 2020
+.Dd June 5, 2020
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -1195,10 +1195,8 @@ but selects all words which do not match
 .Ar pattern .
 .It Cm \&:O
 Order every word in variable alphabetically.
-To sort words in
-reverse order use the
-.Ql Cm \&:O:[-1..1]
-combination of modifiers.
+.It Cm \&:Or
+Order every word in variable in reverse alphabetical order.
 .It Cm \&:Ox
 Randomize words in variable.
 The results will be different each time you are referring to the
@@ -1613,6 +1611,11 @@ then the words are output in reverse ord
 For example,
 .Ql Cm \&:[-1..1]
 selects all the words from last to first.
+If the list is already ordered, then this effectively reverses
+the list, but it is more efficient to use
+.Ql Cm \&:Or
+instead of
+.Ql Cm \&:O:[-1..1] .
 .\" :[*]
 .It Cm \&*
 Causes subsequent modifiers to treat the value as a single word

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.223 src/usr.bin/make/var.c:1.224
--- src/usr.bin/make/var.c:1.223	Sat Apr 25 18:20:57 2020
+++ src/usr.bin/make/var.c	Fri Jun  5 19:20:46 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $	*/
+/*	$NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.223 2020/04/25 18:20:57 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.224 2020/06/05 19:20:46 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2028,6 +2028,13 @@ VarWordCompare(const void *a, const void
 	return r;
 }
 
+static int
+VarWordCompareReverse(const void *a, const void *b)
+{
+	int r = strcmp(*(const char * const *)b, *(const char * const *)a);
+	return r;
+}
+
 /*-
  *-----------------------------------------------------------------------
  * VarOrder --
@@ -2059,6 +2066,9 @@ VarOrder(const char *str, const char oty
 
     if (ac > 0)
 	switch (otype) {
+	case 'r':	/* reverse sort alphabetically */
+	    qsort(av, ac, sizeof(char *), VarWordCompareReverse);
+	    break;
 	case 's':	/* sort alphabetically */
 	    qsort(av, ac, sizeof(char *), VarWordCompare);
 	    break;
@@ -3563,7 +3573,7 @@ ApplyModifiers(char *nstr, const char *t
 		if (tstr[1] == endc || tstr[1] == ':') {
 		    otype = 's';
 		    termc = *cp;
-		} else if ( (tstr[1] == 'x') &&
+		} else if ( (tstr[1] == 'r' || tstr[1] == 'x') &&
 			    (tstr[2] == endc || tstr[2] == ':') ) {
 		    otype = tstr[1];
 		    cp = tstr + 2;

Reply via email to