1 //
2 // aegis - project change supervisor
3 // Copyright (C) 1997, 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 (at
8 // 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 GNU
13 // 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 <http://www.gnu.org/licenses/>.
17 //
18 
19 #include <common/arglex.h>
20 #include <common/sizeof.h>
21 #include <common/str.h>
22 #include <libaegis/aer/value/integer.h>
23 #include <libaegis/aer/value/string.h>
24 #include <libaegis/sub.h>
25 
26 #include <aefind/function/stat.h>
27 #include <aefind/shorthand/stat.h>
28 #include <aefind/tree/arithmetic.h>
29 #include <aefind/tree/constant.h>
30 #include <aefind/tree.h>
31 #include <aefind/tree/list.h>
32 #include <aefind/tree/now.h>
33 #include <aefind/tree/relative.h>
34 #include <aefind/tree/this.h>
35 
36 
37 tree::pointer
shorthand_atime(diadic_t cmp,int n,int units)38 shorthand_atime(diadic_t cmp, int n, int units)
39 {
40     // now
41     tree::pointer tp1 = tree_now_new();
42 
43     // {}
44     tree::pointer tp2 = tree_this::create();
45 
46     // atime({})
47     tree::pointer tp3 = tree_atime::create(tp2);
48 
49     // now - atime({})
50     tree::pointer tp4 = tree_subtract::create(tp1, tp3);
51 
52     // units
53     rpt_value::pointer vp5 = rpt_value_integer::create(units);
54     tree::pointer tp5 = tree_constant::create(vp5);
55 
56     // (now - atime({})) / units
57     tree::pointer tp6 = tree_divide::create(tp4, tp5);
58 
59     rpt_value::pointer vp7 = rpt_value_integer::create(n);
60     tree::pointer tp7 = tree_constant::create(vp7);
61 
62     // (now - atime({})) / units == n
63     return cmp(tp6, tp7);
64 }
65 
66 
67 tree::pointer
shorthand_ctime(diadic_t cmp,int n,int units)68 shorthand_ctime( diadic_t cmp, int n, int units)
69 {
70     // now
71     tree::pointer tp1 = tree_now_new();
72 
73     // {}
74     tree::pointer tp2 = tree_this::create();
75 
76     // ctime({})
77     tree::pointer tp3 = tree_ctime::create(tp2);
78 
79     // now - ctime({})
80     tree::pointer tp4 = tree_subtract::create(tp1, tp3);
81 
82     // units
83     rpt_value::pointer vp5 = rpt_value_integer::create(units);
84     tree::pointer tp5 = tree_constant::create(vp5);
85 
86     // (now - ctime({})) / units
87     tree::pointer tp6 = tree_divide::create(tp4, tp5);
88 
89     rpt_value::pointer vp7 = rpt_value_integer::create(n);
90     tree::pointer tp7 = tree_constant::create(vp7);
91 
92     // (now - ctime({})) / units == n
93     return cmp(tp6, tp7);
94 }
95 
96 
97 tree::pointer
shorthand_gid(diadic_t cmp,int n)98 shorthand_gid(diadic_t cmp, int n)
99 {
100     // {}
101     tree::pointer tp1 = tree_this::create();
102 
103     // gid({})
104     tree::pointer tp2 = tree_gid::create(tp1);
105 
106     // n
107     rpt_value::pointer vp3 = rpt_value_integer::create(n);
108     tree::pointer tp3 = tree_constant::create(vp3);
109 
110     // gid({}) == n
111     return cmp(tp2, tp3);
112 }
113 
114 
115 tree::pointer
shorthand_ino(diadic_t cmp,int n)116 shorthand_ino(diadic_t cmp, int n)
117 {
118     // {}
119     tree::pointer tp1 = tree_this::create();
120 
121     // ino({})
122     tree::pointer tp2 = tree_ino::create(tp1);
123 
124     // n
125     rpt_value::pointer vp3 = rpt_value_integer::create(n);
126     tree::pointer tp3 = tree_constant::create(vp3);
127 
128     // ino({}) == n
129     return cmp(tp2, tp3);
130 }
131 
132 
133 tree::pointer
shorthand_mode(int n)134 shorthand_mode(int n)
135 {
136     // {}
137     tree::pointer tp1 = tree_this::create();
138 
139     // mode({})
140     tree::pointer tp2 = tree_mode::create(tp1);
141 
142     // n
143     rpt_value::pointer vp3 = rpt_value_integer::create(n);
144     tree::pointer tp3 = tree_constant::create(vp3);
145 
146     // mode({}) == n
147     return tree_eq::create(tp2, tp3);
148 }
149 
150 
151 tree::pointer
shorthand_mtime(diadic_t cmp,int n,int units)152 shorthand_mtime(diadic_t cmp, int n, int units)
153 {
154     // now
155     tree::pointer tp1 = tree_now_new();
156 
157     // {}
158     tree::pointer tp2 = tree_this::create();
159 
160     // mtime({})
161     tree::pointer tp3 = tree_mtime::create(tp2);
162 
163     // now - mtime({})
164     tree::pointer tp4 = tree_subtract::create(tp1, tp3);
165 
166     // units
167     rpt_value::pointer vp5 = rpt_value_integer::create(units);
168     tree::pointer tp5 = tree_constant::create(vp5);
169 
170     // (now - mtime({})) / units
171     tree::pointer tp6 = tree_divide::create(tp4, tp5);
172 
173     // n
174     rpt_value::pointer vp7 = rpt_value_integer::create(n);
175     tree::pointer tp7 = tree_constant::create(vp7);
176 
177     // (now - mtime({})) / units == n
178     return cmp(tp6, tp7);
179 }
180 
181 
182 tree::pointer
shorthand_newer(const nstring & filename)183 shorthand_newer(const nstring &filename)
184 {
185     // {}
186     tree::pointer tp1 = tree_this::create();
187 
188     // mtime({})
189     tree::pointer tp2 = tree_mtime::create(tp1);
190 
191     // filename
192     rpt_value::pointer vp3 = rpt_value_string::create(filename);
193     tree::pointer tp3 = tree_constant::create(vp3);
194 
195     // mtime(filename)
196     tree::pointer tp4 = tree_mtime::create(tp3);
197 
198     // mtime({}) > mtime(filename)
199     return tree_gt::create(tp2, tp4);
200 }
201 
202 
203 tree::pointer
shorthand_nlink(diadic_t cmp,int n)204 shorthand_nlink(diadic_t cmp, int n)
205 {
206     // {}
207     tree::pointer tp1 = tree_this::create();
208 
209     // nlink({})
210     tree::pointer tp2 = tree_nlink::create(tp1);
211 
212     // n
213     rpt_value::pointer vp3 = rpt_value_integer::create(n);
214     tree::pointer tp3 = tree_constant::create(vp3);
215 
216     // nlink({}) == n
217     return cmp(tp2, tp3);
218 }
219 
220 
221 tree::pointer
shorthand_size(diadic_t cmp,int n)222 shorthand_size(diadic_t cmp, int n)
223 {
224     // {}
225     tree::pointer tp1 = tree_this::create();
226 
227     // size({})
228     tree::pointer tp2 = tree_size::create(tp1);
229 
230     // n
231     rpt_value::pointer vp3 = rpt_value_integer::create(n);
232     tree::pointer tp3 = tree_constant::create(vp3);
233 
234     // size({}) == n
235     return cmp(tp2, tp3);
236 }
237 
238 
239 tree::pointer
shorthand_uid(diadic_t cmp,int n)240 shorthand_uid(diadic_t cmp, int n)
241 {
242     // {}
243     tree::pointer tp1 = tree_this::create();
244 
245     // uid({})
246     tree::pointer tp2 = tree_uid::create(tp1);
247 
248     // n
249     rpt_value::pointer vp3 = rpt_value_integer::create(n);
250     tree::pointer tp3 = tree_constant::create(vp3);
251 
252     // uid({}) == n
253     return cmp(tp2, tp3);
254 }
255 
256 
257 static nstring
type_name_by_pattern(const nstring & abbrev)258 type_name_by_pattern(const nstring &abbrev)
259 {
260     struct table_ty
261     {
262         const char      *pattern;
263         const char      *name;
264     };
265 
266     static table_ty table[] =
267     {
268         { "Block",              "block_special",        },
269         { "Block_Special",      "block_special",        },
270         { "Character",          "character_special",    },
271         { "Character_Special",  "character_special",    },
272         { "Directory",          "directory",            },
273         { "File",               "file",                 },
274         { "Link",               "symbolic_link",        },
275         { "Named_Pipe",         "named_pipe",           },
276         { "First_In_First_Out", "named_pipe",           },
277         { "Normal",             "file",                 },
278         { "Pipe",               "named_pipe",           },
279         { "Plain",              "file",                 },
280         { "Socket",             "socket",               },
281         { "Symbolic_Link",      "symbolic_link",        },
282     };
283 
284     for (table_ty *tp = table; tp < ENDOF(table); ++tp)
285         if (arglex_compare(tp->pattern, abbrev.c_str(), 0))
286             return tp->name;
287     sub_context_ty sc;
288     sc.var_set_string("Name", abbrev);
289     sc.fatal_intl(i18n("file type $name unknown"));
290     // NOTREACHED
291     return "unknown";
292 }
293 
294 
295 tree::pointer
shorthand_type(const nstring & abbrev)296 shorthand_type(const nstring &abbrev)
297 {
298     nstring name = type_name_by_pattern(abbrev);
299 
300     // {}
301     tree::pointer tp1 = tree_this::create();
302 
303     // type({})
304     tree::pointer tp2 = tree_type::create(tp1);
305 
306     // name
307     rpt_value::pointer vp3 = rpt_value_string::create(name);
308     tree::pointer tp3 = tree_constant::create(vp3);
309 
310     // type({}) == name
311     return tree_eq::create(tp2, tp3);
312 }
313 
314 
315 // vim: set ts=8 sw=4 et :
316