1 /*
2  *      cook - file construction tool
3  *      Copyright (C) 1991-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  * The builtin function all append their results to the supplied
21  * `result' word list.  The first word of the `args' word list
22  * is the name of the function.
23  *
24  * all of the functions return 0 in success, or -1 on error.
25  */
26 
27 #include <common/error_intl.h>
28 #include <common/trace.h>
29 #include <cook/builtin/pathname.h>
30 #include <cook/expr/position.h>
31 #include <cook/os_interface.h>
32 
33 
34 /*
35  * NAME
36  *      builtin_dir - dir part
37  *
38  * SYNOPSIS
39  *      int builtin_dir(string_list_ty *result, string_list_ty *args);
40  *
41  * DESCRIPTION
42  *      "dir" is a built-in function of cook, described as follows:
43  *      This function requires one or more arguments,
44  *      the name of a files of which to get the dir parts.
45  *
46  * RETURNS
47  *      It returns a string containing the directory parts
48  *      of the named files.
49  *
50  * CAVEAT
51  *      The returned result is in dynamic memory.
52  *      It is the responsibility of the caller to dispose of
53  *      the result when it is finished, with a string_list_destructor() call.
54  */
55 
56 static int
dir_interpret(string_list_ty * result,const string_list_ty * args,const expr_position_ty * pp,const struct opcode_context_ty * ocp)57 dir_interpret(string_list_ty *result, const string_list_ty *args,
58     const expr_position_ty *pp, const struct opcode_context_ty *ocp)
59 {
60     size_t          j;
61 
62     trace(("dir\n"));
63     (void)pp;
64     (void)ocp;
65     assert(result);
66     assert(args);
67     assert(args->nstrings);
68     for (j = 1; j < args->nstrings; j++)
69     {
70         string_ty       *s;
71 
72         s = os_dirname(args->string[j]);
73         if (!s)
74             return -1;
75         string_list_append(result, s);
76         str_free(s);
77     }
78     return 0;
79 }
80 
81 
82 builtin_ty builtin_dir =
83 {
84     "dir",
85     dir_interpret,
86     dir_interpret,              /* script */
87 };
88 
89 
90 builtin_ty builtin_dirname =
91 {
92     "dirname",
93     dir_interpret,
94     dir_interpret,              /* script */
95 };
96 
97 
98 /*
99  * NAME
100  *      builtin_entryname - entryname part
101  *
102  * SYNOPSIS
103  *      int builtin_entryname(string_list_ty *result, string_list_ty *args);
104  *
105  * DESCRIPTION
106  *      Defined is a built-in function of cook, described as follows:
107  *      This function requires one or more arguments,
108  *      the name of a files of which to get the entryname parts.
109  *
110  * RETURNS
111  *      It returns a string containing the entryname parts
112  *      of the named files.
113  *
114  * CAVEAT
115  *      The returned result is in dynamic memory.
116  *      It is the responsibility of the caller to dispose of
117  *      the result when it is finished, with a string_list_destructor() call.
118  */
119 
120 static int
entryname_interpret(string_list_ty * result,const string_list_ty * args,const expr_position_ty * pp,const struct opcode_context_ty * ocp)121 entryname_interpret(string_list_ty *result, const string_list_ty *args,
122     const expr_position_ty *pp, const struct opcode_context_ty *ocp)
123 {
124     size_t          j;
125 
126     trace(("entryname\n"));
127     (void)pp;
128     (void)ocp;
129     assert(result);
130     assert(args);
131     assert(args->nstrings);
132     for (j = 1; j < args->nstrings; j++)
133     {
134         string_ty       *s;
135 
136         s = os_entryname(args->string[j]);
137         if (!s)
138             return -1;
139         string_list_append(result, s);
140         str_free(s);
141     }
142     return 0;
143 }
144 
145 
146 builtin_ty builtin_entryname =
147 {
148     "entryname",
149     entryname_interpret,
150     entryname_interpret,        /* script */
151 };
152 
153 
154 builtin_ty builtin_notdir =
155 {
156     "notdir",
157     entryname_interpret,
158     entryname_interpret,        /* script */
159 };
160 
161 
162 /*
163  * NAME
164  *      builtin_pathname - pathname part
165  *
166  * SYNOPSIS
167  *      int builtin_pathname(string_list_ty *result, string_list_ty *args);
168  *
169  * DESCRIPTION
170  *      Defined is a built-in function of cook, described as follows:
171  *      This function requires one or more arguments,
172  *      the name of a files of which to get the pathname parts.
173  *
174  * RETURNS
175  *      It returns a string containing the pathname parts
176  *      of the named files.
177  *
178  * CAVEAT
179  *      The returned result is in dynamic memory.
180  *      It is the responsibility of the caller to dispose of
181  *      the result when it is finished, with a string_list_destructor() call.
182  */
183 
184 static int
pathname_interpret(string_list_ty * result,const string_list_ty * args,const expr_position_ty * pp,const struct opcode_context_ty * ocp)185 pathname_interpret(string_list_ty *result, const string_list_ty *args,
186     const expr_position_ty *pp, const struct opcode_context_ty *ocp)
187 {
188     size_t          j;
189 
190     trace(("pathname\n"));
191     (void)pp;
192     (void)ocp;
193     assert(result);
194     assert(args);
195     assert(args->nstrings);
196     for (j = 1; j < args->nstrings; j++)
197     {
198         string_ty       *s;
199 
200         s = os_pathname(args->string[j]);
201         if (!s)
202             return -1;
203         string_list_append(result, s);
204         str_free(s);
205     }
206     return 0;
207 }
208 
209 
210 builtin_ty builtin_pathname =
211 {
212     "pathname",
213     pathname_interpret,
214     pathname_interpret,         /* script */
215 };
216