1 /*
2  *      cook - file construction tool
3  *      Copyright (C) 1998, 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/relati_dirna.h>
22 #include <common/error_intl.h>
23 #include <cook/expr/position.h>
24 #include <cook/os_interface.h>
25 #include <common/str.h>
26 #include <common/str_list.h>
27 #include <common/trace.h>
28 
29 
30 /*
31  * NAME
32  *      builtin_reldir - relative directory part
33  *
34  * SYNOPSIS
35  *      int builtin_reldir(string_list_ty *result, string_list_ty *args);
36  *
37  * DESCRIPTION
38  *      "reldir" is a built-in function of cook, described as follows:
39  *      This function requires one or more arguments, the name of a
40  *      files of which to get the dir parts; files relative to the
41  *      current directory wil return ".".
42  *
43  * RETURNS
44  *      It returns a string containing the directory parts
45  *      of the named files.
46  *
47  * CAVEAT
48  *      The returned result is in dynamic memory.
49  *      It is the responsibility of the caller to dispose of
50  *      the result when it is finished, with a string_list_destructor() call.
51  */
52 
53 static int
interpret(string_list_ty * result,const string_list_ty * args,const expr_position_ty * pp,const struct opcode_context_ty * ocp)54 interpret(string_list_ty *result, const string_list_ty *args,
55     const expr_position_ty *pp, const struct opcode_context_ty *ocp)
56 {
57     size_t          j;
58 
59     trace(("reldir\n"));
60     (void)pp;
61     (void)ocp;
62     assert(result);
63     assert(args);
64     assert(args->nstrings);
65     for (j = 1; j < args->nstrings; j++)
66     {
67         string_ty       *s;
68 
69         s = os_dirname_relative(args->string[j]);
70         if (!s)
71             return -1;
72         string_list_append(result, s);
73         str_free(s);
74     }
75     return 0;
76 }
77 
78 
79 builtin_ty builtin_reldir =
80 {
81     "reldir",
82     interpret,
83     interpret,                  /* script */
84 };
85 
86 
87 builtin_ty builtin_relative_dirname =
88 {
89     "relative_dirname",
90     interpret,
91     interpret,                  /* script */
92 };
93