Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make Add :O var modifier, that sorts the words in a ...



details:   https://anonhg.NetBSD.org/src/rev/90ec5a50a6b4
branches:  trunk
changeset: 476276:90ec5a50a6b4
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Sep 12 00:17:50 1999 +0000

description:
Add :O var modifier, that sorts the words in a variable.

diffstat:

 usr.bin/make/make.1 |   4 ++-
 usr.bin/make/var.c  |  72 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 72 insertions(+), 4 deletions(-)

diffs (138 lines):

diff -r 3b008ece0afa -r 90ec5a50a6b4 usr.bin/make/make.1
--- a/usr.bin/make/make.1       Sat Sep 11 21:45:28 1999 +0000
+++ b/usr.bin/make/make.1       Sun Sep 12 00:17:50 1999 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: make.1,v 1.35 1999/08/09 21:06:28 aidan Exp $
+.\"    $NetBSD: make.1,v 1.36 1999/09/12 00:17:50 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -509,6 +509,8 @@
 .Ql Cm M ,
 but selects all words which do not match
 the rest of the modifier.
+.It Cm O
+Order every word in variable alphabetically.
 .It Cm Q
 Quotes every shell meta-character in the variable, so that it can be passed
 safely through recursive invocations of
diff -r 3b008ece0afa -r 90ec5a50a6b4 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Sep 11 21:45:28 1999 +0000
+++ b/usr.bin/make/var.c        Sun Sep 12 00:17:50 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.34 1999/06/06 21:16:23 christos Exp $        */
+/*     $NetBSD: var.c,v 1.35 1999/09/12 00:17:50 christos Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -39,14 +39,14 @@
  */
 
 #ifdef MAKE_BOOTSTRAP
-static char rcsid[] = "$NetBSD: var.c,v 1.34 1999/06/06 21:16:23 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.35 1999/09/12 00:17:50 christos 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.34 1999/06/06 21:16:23 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.35 1999/09/12 00:17:50 christos Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -204,6 +204,8 @@
 static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer,
                                                ClientData),
                            ClientData));
+static char *VarSort __P((char *));
+static int VarWordCompare __P((const void *, const void *));
 static int VarPrintVar __P((ClientData, ClientData));
 
 /*-
@@ -1278,6 +1280,62 @@
     return (str);
 }
 
+
+static int
+VarWordCompare(a, b)
+       const void *a;
+       const void *b;
+{
+       int r = strcmp(*(char **)a, *(char **)b);
+       return r;
+}
+
+/*-
+ *-----------------------------------------------------------------------
+ * VarSort --
+ *     Sort the words in the string.
+ *
+ * Results:
+ *     A string containing the words sorted
+ *
+ * Side Effects:
+ *     None.
+ *
+ *-----------------------------------------------------------------------
+ */
+static char *
+VarSort (str)
+    char         *str;             /* String whose words should be sorted */
+                                   /* Function to use to modify them */
+{
+    Buffer       buf;              /* Buffer for the new string */
+    char **av;                     /* word list [first word does not count] */
+    char *as;                      /* word list memory */
+    int ac, i;
+
+    buf = Buf_Init (0);
+
+    av = brk_string(str, &ac, FALSE, &as);
+
+    if (ac > 0)
+       qsort(av, ac, sizeof(char *), VarWordCompare);
+
+    for (i = 0; i < ac; i++) {
+       Buf_AddBytes(buf, strlen(av[i]), (Byte *) av[i]);
+       if (i != ac - 1)
+           Buf_AddByte (buf, ' ');
+    }
+
+    free(as);
+    free(av);
+
+    Buf_AddByte (buf, '\0');
+    str = (char *)Buf_GetAll (buf, (int *)NULL);
+    Buf_Destroy (buf, FALSE);
+    return (str);
+}
+
+
 /*-
  *-----------------------------------------------------------------------
  * VarGetPattern --
@@ -1724,6 +1782,7 @@
      *                         each word
      *           :R            Substitute the root of each word
      *                         (pathname minus the suffix).
+     *           :O            Sort words in variable.
      *           :?<true-value>:<false-value>
      *                         If the variable evaluates to true, return
      *                         true value, else return the second value.
@@ -1988,6 +2047,13 @@
                        break;
                    }
                    /*FALLTHRU*/
+               case 'O':
+                   if (tstr[1] == endc || tstr[1] == ':') {
+                       newStr = VarSort (str);
+                       cp = tstr + 1;
+                       termc = *cp;
+                       break;
+                   }
 #ifdef SUNSHCMD
                case 's':
                    if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {



Home | Main Index | Thread Index | Old Index