1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
30 struct test_case_struct
37 const char *wordv
[10];
41 /* Simple field-splitting */
42 { 0, NULL
, "one", 0, 1, { "one", }, IFS
},
43 { 0, NULL
, "one two", 0, 2, { "one", "two", }, IFS
},
44 { 0, NULL
, "one two three", 0, 3, { "one", "two", "three", }, IFS
},
45 { 0, NULL
, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS
},
46 { 0, NULL
, " red , white blue", 0, 3, { "red", "white", "blue", }, ", \n\t" },
48 /* Simple parameter expansion */
49 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS
},
50 { 0, "foo", "$var", 0, 1, { "foo", }, IFS
},
51 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS
},
52 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS
},
53 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS
},
55 /* Simple quote removal */
56 { 0, NULL
, "\"quoted\"", 0, 1, { "quoted", }, IFS
},
57 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS
},
58 { 0, NULL
, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS
},
60 /* Simple command substitution */
61 { 0, NULL
, "$(echo hello)", 0, 1, { "hello", }, IFS
},
62 { 0, NULL
, "$( (echo hello) )", 0, 1, { "hello", }, IFS
},
63 { 0, NULL
, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS
},
64 { 0, NULL
, "`echo one two`", 0, 2, { "one", "two", }, IFS
},
65 { 0, NULL
, "$(echo ')')", 0, 1, { ")" }, IFS
},
66 { 0, NULL
, "$(echo hello; echo)", 0, 1, { "hello", }, IFS
},
68 /* Simple arithmetic expansion */
69 { 0, NULL
, "$((1 + 1))", 0, 1, { "2", }, IFS
},
70 { 0, NULL
, "$((2-3))", 0, 1, { "-1", }, IFS
},
71 { 0, NULL
, "$((-1))", 0, 1, { "-1", }, IFS
},
72 { 0, NULL
, "$[50+20]", 0, 1, { "70", }, IFS
},
73 { 0, NULL
, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS
},
75 /* Advanced parameter expansion */
76 { 0, NULL
, "${var:-bar}", 0, 1, { "bar", }, IFS
},
77 { 0, NULL
, "${var-bar}", 0, 1, { "bar", }, IFS
},
78 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS
},
79 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS
},
80 { 0, "", "${var-bar}", 0, 0, { NULL
, }, IFS
},
81 { 0, NULL
, "${var:=bar}", 0, 1, { "bar", }, IFS
},
82 { 0, NULL
, "${var=bar}", 0, 1, { "bar", }, IFS
},
83 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS
},
84 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS
},
85 { 0, "", "${var=bar}", 0, 0, { NULL
, }, IFS
},
86 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS
},
87 { 0, NULL
, "${var:+bar}", 0, 0, { NULL
, }, IFS
},
88 { 0, NULL
, "${var+bar}", 0, 0, { NULL
, }, IFS
},
89 { 0, "", "${var:+bar}", 0, 0, { NULL
, }, IFS
},
90 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS
},
91 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS
},
92 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS
},
93 { 0, NULL
, "${var:-'}'}", 0, 1, { "}", }, IFS
},
94 { 0, NULL
, "${var-}", 0, 0, { NULL
}, IFS
},
96 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS
},
97 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS
},
98 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS
},
99 { 0, "borabora-island", "${var##*bora}", 0, 1, {"-island", }, IFS
},
100 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS
},
102 /* Pathname expansion */
103 { 0, NULL
, "???", 0, 2, { "one", "two", }, IFS
},
104 { 0, NULL
, "[ot]??", 0, 2, { "one", "two", }, IFS
},
105 { 0, NULL
, "t*", 0, 2, { "three", "two", }, IFS
},
106 { 0, NULL
, "\"t\"*", 0, 2, { "three", "two", }, IFS
},
108 /* Nested constructs */
109 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS
},
110 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS
},
111 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS
},
112 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
113 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
114 { 0, NULL
, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS
},
115 { 0, NULL
, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS
},
116 { 0, NULL
, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS
},
117 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS
},
118 { 0, NULL
, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS
},
119 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS
},
120 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS
},
122 /* Different IFS values */
123 { 0, NULL
, "a b\tc\nd ", 0, 4, { "a", "b", "c", "d" }, NULL
/* unset */ },
124 { 0, NULL
, "a b\tc d ", 0, 1, { "a b\tc d " }, "" /* `null' */ },
125 { 0, NULL
, "a,b c\n, d", 0, 3, { "a", "b c", " d" }, "\t\n," },
127 /* Other things that should succeed */
128 { 0, NULL
, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS
},
129 { 0, "???", "$var", 0, 1, { "???", }, IFS
},
130 { 0, NULL
, "$var", 0, 0, { NULL
, }, IFS
},
131 { 0, NULL
, "\"\\n\"", 0, 1, { "\\n", }, IFS
},
132 { 0, NULL
, "", 0, 0, { NULL
, }, IFS
},
134 /* Things that should fail */
135 { WRDE_BADCHAR
, NULL
, "new\nline", 0, 0, { NULL
, }, "" /* \n not IFS */ },
136 { WRDE_BADCHAR
, NULL
, "pipe|symbol", 0, 0, { NULL
, }, IFS
},
137 { WRDE_BADCHAR
, NULL
, "&ersand", 0, 0, { NULL
, }, IFS
},
138 { WRDE_BADCHAR
, NULL
, "semi;colon", 0, 0, { NULL
, }, IFS
},
139 { WRDE_BADCHAR
, NULL
, "<greater", 0, 0, { NULL
, }, IFS
},
140 { WRDE_BADCHAR
, NULL
, "less>", 0, 0, { NULL
, }, IFS
},
141 { WRDE_BADCHAR
, NULL
, "(open-paren", 0, 0, { NULL
, }, IFS
},
142 { WRDE_BADCHAR
, NULL
, "close-paren)", 0, 0, { NULL
, }, IFS
},
143 { WRDE_BADCHAR
, NULL
, "{open-brace", 0, 0, { NULL
, }, IFS
},
144 { WRDE_BADCHAR
, NULL
, "close-brace}", 0, 0, { NULL
, }, IFS
},
145 { WRDE_CMDSUB
, NULL
, "$(ls)", WRDE_NOCMD
, 0, { NULL
, }, IFS
},
146 { WRDE_BADVAL
, NULL
, "$var", WRDE_UNDEF
, 0, { NULL
, }, IFS
},
147 { WRDE_BADVAL
, NULL
, "$9", WRDE_UNDEF
, 0, { NULL
, }, IFS
},
148 { WRDE_SYNTAX
, NULL
, "$[50+20))", 0, 0, { NULL
, }, IFS
},
149 { WRDE_SYNTAX
, NULL
, "${%%noparam}", 0, 0, { NULL
, }, IFS
},
150 { WRDE_SYNTAX
, NULL
, "${missing-brace", 0, 0, { NULL
, }, IFS
},
151 { WRDE_SYNTAX
, NULL
, "$((2+))", 0, 0, { NULL
, }, IFS
},
152 { WRDE_SYNTAX
, NULL
, "`", 0, 0, { NULL
, }, IFS
},
154 { -1, NULL
, NULL
, 0, 0, { NULL
, }, IFS
},
157 static int testit (struct test_case_struct
*tc
);
161 command_line_test (const char *words
)
165 int retval
= wordexp (words
, &we
, 0);
166 printf ("wordexp returned %d\n", retval
);
167 for (i
= 0; i
< we
.we_wordc
; i
++)
168 printf ("we_wordv[%d] = \"%s\"\n", i
, we
.we_wordv
[i
]);
172 main (int argc
, char *argv
[])
174 const char *globfile
[] = { "one", "two", "three", NULL
};
184 command_line_test (argv
[1]);
188 cwd
= getcwd (NULL
, 0);
190 /* Set up arena for pathname expansion */
192 if (mkdir (tmpdir
, S_IRWXU
) || chdir (tmpdir
))
198 for (i
= 0; globfile
[i
]; ++i
)
199 if ((fd
= creat (globfile
[i
], S_IRUSR
| S_IWUSR
)) == -1
204 for (test
= 0; test_case
[test
].retval
!= -1; test
++)
205 if (testit (&test_case
[test
]))
208 pw
= getpwnam ("root");
211 struct test_case_struct ts
;
218 ts
.wordv
[0] = pw
->pw_dir
;
225 puts ("tests completed, now cleaning up");
228 for (i
= 0; globfile
[i
]; ++i
)
229 remove (globfile
[i
]);
237 printf ("tests failed: %d\n", fail
);
244 testit (struct test_case_struct
*tc
)
252 setenv ("var", tc
->env
, 1);
257 setenv ("IFS", tc
->ifs
, 1);
261 printf ("Test %d (%s): ", ++tests
, tc
->words
);
262 retval
= wordexp (tc
->words
, &we
, tc
->flags
);
264 if (retval
!= tc
->retval
|| (retval
== 0 && we
.we_wordc
!= tc
->wordc
))
267 for (i
= 0; i
< we
.we_wordc
; ++i
)
268 if (strcmp (tc
->wordv
[i
], we
.we_wordv
[i
]) != 0)
277 printf ("Test words: <%s>, need retval %d, wordc %d\n",
278 tc
->words
, tc
->retval
, tc
->wordc
);
279 printf ("Got retval %d, wordc %d: ", retval
, we
.we_wordc
);
280 for (i
= 0; i
< we
.we_wordc
; ++i
)
281 printf ("<%s> ", we
.we_wordv
[i
]);