1 /*
2 * cook - file construction tool
3 * Copyright (C) 1994, 1997-1999, 2006, 2007 Peter Miller;
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cook/builtin/split.h>
22 #include <common/error_intl.h>
23 #include <cook/expr/position.h>
24 #include <common/str_list.h>
25 #include <common/trace.h>
26
27
28 static int
interpret(string_list_ty * result,const string_list_ty * arg,const expr_position_ty * pp,const struct opcode_context_ty * ocp)29 interpret(string_list_ty *result, const string_list_ty *arg,
30 const expr_position_ty *pp, const struct opcode_context_ty *ocp)
31 {
32 size_t j;
33
34 trace(("split\n"));
35 (void)ocp;
36 if (arg->nstrings < 2)
37 {
38 sub_context_ty *scp;
39
40 scp = sub_context_new();
41 sub_var_set_string(scp, "Name", arg->string[0]);
42 error_with_position
43 (pp, scp, i18n("$name: requires one or more arguments"));
44 sub_context_delete(scp);
45 return -1;
46 }
47 for (j = 2; j < arg->nstrings; ++j)
48 {
49 string_list_ty wl;
50 size_t k;
51
52 str2wl(&wl, arg->string[j], arg->string[1]->str_text, 0);
53 for (k = 0; k < wl.nstrings; ++k)
54 string_list_append(result, wl.string[k]);
55 string_list_destructor(&wl);
56 }
57 return 0;
58 }
59
60
61 builtin_ty builtin_split =
62 {
63 "split",
64 interpret,
65 interpret, /* script */
66 };
67