| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: var.c,v 1.402 2020/08/02 18:57:55 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.403 2020/08/02 19:08:54 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1988, 1989, 1990, 1993 | | 4 | * Copyright (c) 1988, 1989, 1990, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Adam de Boor. | | 8 | * Adam de Boor. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -59,34 +59,34 @@ | | | @@ -59,34 +59,34 @@ |
59 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 59 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
64 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 64 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
65 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 65 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
66 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 66 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
67 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 67 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
68 | * SUCH DAMAGE. | | 68 | * SUCH DAMAGE. |
69 | */ | | 69 | */ |
70 | | | 70 | |
71 | #ifndef MAKE_NATIVE | | 71 | #ifndef MAKE_NATIVE |
72 | static char rcsid[] = "$NetBSD: var.c,v 1.402 2020/08/02 18:57:55 rillig Exp $"; | | 72 | static char rcsid[] = "$NetBSD: var.c,v 1.403 2020/08/02 19:08:54 rillig Exp $"; |
73 | #else | | 73 | #else |
74 | #include <sys/cdefs.h> | | 74 | #include <sys/cdefs.h> |
75 | #ifndef lint | | 75 | #ifndef lint |
76 | #if 0 | | 76 | #if 0 |
77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; | | 77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; |
78 | #else | | 78 | #else |
79 | __RCSID("$NetBSD: var.c,v 1.402 2020/08/02 18:57:55 rillig Exp $"); | | 79 | __RCSID("$NetBSD: var.c,v 1.403 2020/08/02 19:08:54 rillig Exp $"); |
80 | #endif | | 80 | #endif |
81 | #endif /* not lint */ | | 81 | #endif /* not lint */ |
82 | #endif | | 82 | #endif |
83 | | | 83 | |
84 | /*- | | 84 | /*- |
85 | * var.c -- | | 85 | * var.c -- |
86 | * Variable-handling functions | | 86 | * Variable-handling functions |
87 | * | | 87 | * |
88 | * Interface: | | 88 | * Interface: |
89 | * Var_Set Set the value of a variable in the given | | 89 | * Var_Set Set the value of a variable in the given |
90 | * context. The variable is created if it doesn't | | 90 | * context. The variable is created if it doesn't |
91 | * yet exist. | | 91 | * yet exist. |
92 | * | | 92 | * |
| @@ -1562,56 +1562,62 @@ ModifyWords(GNode *ctx, Byte sep, Boolea | | | @@ -1562,56 +1562,62 @@ ModifyWords(GNode *ctx, Byte sep, Boolea |
1562 | | | 1562 | |
1563 | static int | | 1563 | static int |
1564 | VarWordCompare(const void *a, const void *b) | | 1564 | VarWordCompare(const void *a, const void *b) |
1565 | { | | 1565 | { |
1566 | return strcmp(*(const char * const *)a, *(const char * const *)b); | | 1566 | return strcmp(*(const char * const *)a, *(const char * const *)b); |
1567 | } | | 1567 | } |
1568 | | | 1568 | |
1569 | static int | | 1569 | static int |
1570 | VarWordCompareReverse(const void *a, const void *b) | | 1570 | VarWordCompareReverse(const void *a, const void *b) |
1571 | { | | 1571 | { |
1572 | return strcmp(*(const char * const *)b, *(const char * const *)a); | | 1572 | return strcmp(*(const char * const *)b, *(const char * const *)a); |
1573 | } | | 1573 | } |
1574 | | | 1574 | |
1575 | /* Remove adjacent duplicate words. */ | | | |
1576 | static char * | | 1575 | static char * |
1577 | VarUniq(const char *str) | | 1576 | WordList_JoinFree(char **av, int ac, char *as) |
1578 | { | | 1577 | { |
1579 | Buffer buf; /* Buffer for new string */ | | 1578 | Buffer buf; |
1580 | Buf_InitZ(&buf, 0); | | 1579 | Buf_InitZ(&buf, 0); |
1581 | | | 1580 | |
| | | 1581 | int i; |
| | | 1582 | for (i = 0; i < ac; i++) { |
| | | 1583 | if (i != 0) |
| | | 1584 | Buf_AddByte(&buf, ' '); |
| | | 1585 | Buf_AddStr(&buf, av[i]); |
| | | 1586 | } |
| | | 1587 | |
| | | 1588 | free(av); |
| | | 1589 | free(as); |
| | | 1590 | |
| | | 1591 | return Buf_Destroy(&buf, FALSE); |
| | | 1592 | } |
| | | 1593 | |
| | | 1594 | /* Remove adjacent duplicate words. */ |
| | | 1595 | static char * |
| | | 1596 | VarUniq(const char *str) |
| | | 1597 | { |
1582 | char *as; /* Word list memory */ | | 1598 | char *as; /* Word list memory */ |
1583 | int ac; | | 1599 | int ac; |
1584 | char **av = brk_string(str, &ac, FALSE, &as); | | 1600 | char **av = brk_string(str, &ac, FALSE, &as); |
1585 | | | 1601 | |
1586 | if (ac > 1) { | | 1602 | if (ac > 1) { |
1587 | int i, j; | | 1603 | int i, j; |
1588 | for (j = 0, i = 1; i < ac; i++) | | 1604 | for (j = 0, i = 1; i < ac; i++) |
1589 | if (strcmp(av[i], av[j]) != 0 && (++j != i)) | | 1605 | if (strcmp(av[i], av[j]) != 0 && (++j != i)) |
1590 | av[j] = av[i]; | | 1606 | av[j] = av[i]; |
1591 | ac = j + 1; | | 1607 | ac = j + 1; |
1592 | } | | 1608 | } |
1593 | | | 1609 | |
1594 | int i; | | 1610 | return WordList_JoinFree(av, ac, as); |
1595 | for (i = 0; i < ac; i++) { | | | |
1596 | if (i != 0) | | | |
1597 | Buf_AddByte(&buf, ' '); | | | |
1598 | Buf_AddStr(&buf, av[i]); | | | |
1599 | } | | | |
1600 | | | | |
1601 | free(as); | | | |
1602 | free(av); | | | |
1603 | | | | |
1604 | return Buf_Destroy(&buf, FALSE); | | | |
1605 | } | | 1611 | } |
1606 | | | 1612 | |
1607 | | | 1613 | |
1608 | /*- | | 1614 | /*- |
1609 | * Parse a text part of a modifier such as the "from" and "to" in :S/from/to/ | | 1615 | * Parse a text part of a modifier such as the "from" and "to" in :S/from/to/ |
1610 | * or the :@ modifier, until the next unescaped delimiter. The delimiter, as | | 1616 | * or the :@ modifier, until the next unescaped delimiter. The delimiter, as |
1611 | * well as the backslash or the dollar, can be escaped with a backslash. | | 1617 | * well as the backslash or the dollar, can be escaped with a backslash. |
1612 | * | | 1618 | * |
1613 | * Return the parsed (and possibly expanded) string, or NULL if no delimiter | | 1619 | * Return the parsed (and possibly expanded) string, or NULL if no delimiter |
1614 | * was found. | | 1620 | * was found. |
1615 | */ | | 1621 | */ |
1616 | static char * | | 1622 | static char * |
1617 | ParseModifierPart( | | 1623 | ParseModifierPart( |
| @@ -2591,40 +2597,27 @@ ApplyModifier_Order(const char *mod, App | | | @@ -2591,40 +2597,27 @@ ApplyModifier_Order(const char *mod, App |
2591 | for (i = ac - 1; i > 0; i--) { | | 2597 | for (i = ac - 1; i > 0; i--) { |
2592 | int rndidx = random() % (i + 1); | | 2598 | int rndidx = random() % (i + 1); |
2593 | char *t = av[i]; | | 2599 | char *t = av[i]; |
2594 | av[i] = av[rndidx]; | | 2600 | av[i] = av[rndidx]; |
2595 | av[rndidx] = t; | | 2601 | av[rndidx] = t; |
2596 | } | | 2602 | } |
2597 | } | | 2603 | } |
2598 | } else { | | 2604 | } else { |
2599 | free(as); | | 2605 | free(as); |
2600 | free(av); | | 2606 | free(av); |
2601 | return AMR_BAD; | | 2607 | return AMR_BAD; |
2602 | } | | 2608 | } |
2603 | | | 2609 | |
2604 | Buffer buf; | | 2610 | st->newVal = WordList_JoinFree(av, ac, as); |
2605 | Buf_InitZ(&buf, 0); | | | |
2606 | | | | |
2607 | int i; | | | |
2608 | for (i = 0; i < ac; i++) { | | | |
2609 | if (i != 0) | | | |
2610 | Buf_AddByte(&buf, ' '); | | | |
2611 | Buf_AddStr(&buf, av[i]); | | | |
2612 | } | | | |
2613 | | | | |
2614 | free(as); | | | |
2615 | free(av); | | | |
2616 | | | | |
2617 | st->newVal = Buf_Destroy(&buf, FALSE); | | | |
2618 | return AMR_OK; | | 2611 | return AMR_OK; |
2619 | } | | 2612 | } |
2620 | | | 2613 | |
2621 | /* :? then : else */ | | 2614 | /* :? then : else */ |
2622 | static ApplyModifierResult | | 2615 | static ApplyModifierResult |
2623 | ApplyModifier_IfElse(const char *mod, ApplyModifiersState *st) | | 2616 | ApplyModifier_IfElse(const char *mod, ApplyModifiersState *st) |
2624 | { | | 2617 | { |
2625 | Boolean value = FALSE; | | 2618 | Boolean value = FALSE; |
2626 | VarEvalFlags then_eflags = st->eflags & ~VARE_WANTRES; | | 2619 | VarEvalFlags then_eflags = st->eflags & ~VARE_WANTRES; |
2627 | VarEvalFlags else_eflags = st->eflags & ~VARE_WANTRES; | | 2620 | VarEvalFlags else_eflags = st->eflags & ~VARE_WANTRES; |
2628 | | | 2621 | |
2629 | int cond_rc = COND_PARSE; /* anything other than COND_INVALID */ | | 2622 | int cond_rc = COND_PARSE; /* anything other than COND_INVALID */ |
2630 | if (st->eflags & VARE_WANTRES) { | | 2623 | if (st->eflags & VARE_WANTRES) { |