1 //
2 // aegis - project change supervisor
3 // Copyright (C) 1999, 2002-2008, 2012 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see
17 // <http://www.gnu.org/licenses/>.
18 //
19
20 #include <common/nstring/list.h>
21 #include <common/trace.h>
22 #include <common/wstring/list.h>
23 #include <libaegis/os.h>
24 #include <libaegis/sub.h>
25 #include <libaegis/sub/dirname.h>
26
27
28 //
29 // NAME
30 // sub_dirname - the dirname substitution
31 //
32 // SYNOPSIS
33 // wstring_ty *sub_dirname(wstring_list_ty *arg);
34 //
35 // DESCRIPTION
36 // The sub_dirname function implements the dirname substitution.
37 // The dirname substitution is replaced by the dirname of
38 // the argument path, similar to the dirname(1) command.
39 //
40 // ARGUMENTS
41 // arg - list of arguments, including the function name as [0]
42 //
43 // RETURNS
44 // a pointer to a string in dynamic memory;
45 // or NULL on error, setting suberr appropriately.
46 //
47
48 wstring
sub_dirname(sub_context_ty * scp,const wstring_list & arg)49 sub_dirname(sub_context_ty *scp, const wstring_list &arg)
50 {
51 trace(("sub_dirname()\n{\n"));
52 wstring result;
53 if (arg.size() < 2)
54 {
55 scp->error_set(i18n("requires one argument"));
56 }
57 else
58 {
59 os_become_orig();
60 nstring_list results;
61 for (size_t j = 1; j < arg.size(); ++j)
62 {
63 nstring s1 = arg[j].to_nstring();
64 nstring s2 = os_dirname(s1);
65 results.push_back(s2);
66 }
67 os_become_undo();
68 result = wstring(results.unsplit());
69 }
70 trace(("return %p;\n", result.get_ref()));
71 trace(("}\n"));
72 return result;
73 }
74
75
76 // vim: set ts=8 sw=4 et :
77