4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* find.c - processes the find conditions
24 * A Condition is a tree structure. Each node has a test() fn which
25 * can be used to see whether the current file matches, and a free() fn
26 * which frees it. Both will recurse down the tree as needed.
41 typedef struct _Eval Eval
;
43 /* Static prototypes */
44 static FindCondition
*parse_expression(guchar
**expression
);
45 static FindCondition
*parse_case(guchar
**expression
);
46 static FindCondition
*parse_system(guchar
**expression
);
47 static FindCondition
*parse_condition(guchar
**expression
);
48 static FindCondition
*parse_match(guchar
**expression
);
49 static FindCondition
*parse_comparison(guchar
**expression
);
50 static FindCondition
*parse_is(guchar
**expression
);
51 static Eval
*parse_eval(guchar
**expression
);
52 static Eval
*parse_variable(guchar
**expression
);
54 static gboolean
match(guchar
**expression
, guchar
*word
);
102 typedef long (*EvalCalc
)(Eval
*eval
, FindInfo
*info
);
103 typedef void (*EvalFree
)(Eval
*eval
);
113 #define EAT ((*expression)++)
114 #define NEXT (**expression)
115 #define SKIP while (NEXT == ' ' || NEXT == '\t') EAT
116 #define MATCH(word) (match(expression, word))
119 # define S_ISVTX 0x0001000
122 /****************************************************************
123 * EXTERNAL INTERFACE *
124 ****************************************************************/
126 /* Take a string and parse it, returning a condition object which
127 * can be passed to find_test_condition() later. NULL if the string
128 * is not a valid expression.
130 FindCondition
*find_compile(guchar
*string
)
133 guchar
**expression
= &string
;
135 g_return_val_if_fail(string
!= NULL
, NULL
);
137 cond
= parse_expression(expression
);
151 gboolean
find_test_condition(FindCondition
*condition
, FindInfo
*info
)
153 g_return_val_if_fail(condition
!= NULL
, FALSE
);
154 g_return_val_if_fail(info
!= NULL
, FALSE
);
156 return condition
->test(condition
, info
);
159 void find_condition_free(FindCondition
*condition
)
161 g_return_if_fail(condition
!= NULL
);
163 condition
->free(condition
);
166 /****************************************************************
167 * INTERNAL FUNCTIONS *
168 ****************************************************************/
170 /* Call this when you've just eaten '('. Returns the string upto the
171 * matching ')' (and eats that bracket), or NULL on failure.
172 * Brackets within the string may be quoted or escaped.
174 static guchar
*get_bracketed_string(guchar
**expression
)
180 str
= g_string_new(NULL
);
190 if (c
== '\'' || c
== '"')
191 quote
= c
; /* Start quoted section */
192 else if (c
== '\\' && (NEXT
== '(' || NEXT
== ')'))
202 guchar
*retval
= str
->str
;
204 g_string_free(str
, FALSE
);
212 quote
= '\0'; /* End quoted section */
213 else if (c
== '\\' && NEXT
== quote
)
215 g_string_append_c(str
, c
);
220 g_string_append_c(str
, c
);
223 g_string_free(str
, TRUE
);
231 static gboolean
test_prune(FindCondition
*condition
, FindInfo
*info
)
237 static gboolean
test_leaf(FindCondition
*condition
, FindInfo
*info
)
239 return fnmatch(condition
->data1
, info
->leaf
, 0) == 0;
242 static gboolean
test_path(FindCondition
*condition
, FindInfo
*info
)
244 return fnmatch(condition
->data1
, info
->fullpath
, FNM_PATHNAME
) == 0;
247 static gboolean
test_system(FindCondition
*condition
, FindInfo
*info
)
249 guchar
*command
= (guchar
*) condition
->data1
;
250 guchar
*start
= command
;
251 GString
*to_sys
= NULL
;
255 to_sys
= g_string_new(NULL
);
257 while ((perc
= strchr(command
, '%')))
259 if (perc
> start
&& perc
[-1] == '\\')
262 while (command
< perc
)
263 g_string_append_c(to_sys
, *(command
++));
266 g_string_append(to_sys
, info
->fullpath
);
269 g_string_append_c(to_sys
, '%');
276 g_string_append(to_sys
, command
);
278 retcode
= system(to_sys
->str
);
280 g_string_free(to_sys
, TRUE
);
285 static gboolean
test_OR(FindCondition
*condition
, FindInfo
*info
)
287 FindCondition
*first
= (FindCondition
*) condition
->data1
;
288 FindCondition
*second
= (FindCondition
*) condition
->data2
;
290 return first
->test(first
, info
) || second
->test(second
, info
);
293 static gboolean
test_AND(FindCondition
*condition
, FindInfo
*info
)
295 FindCondition
*first
= (FindCondition
*) condition
->data1
;
296 FindCondition
*second
= (FindCondition
*) condition
->data2
;
298 return first
->test(first
, info
) && second
->test(second
, info
);
301 static gboolean
test_neg(FindCondition
*condition
, FindInfo
*info
)
303 FindCondition
*first
= (FindCondition
*) condition
->data1
;
305 return !first
->test(first
, info
);
308 static gboolean
test_is(FindCondition
*condition
, FindInfo
*info
)
310 mode_t mode
= info
->stats
.st_mode
;
312 switch ((IsTest
) condition
->value
)
315 return S_ISDIR(mode
);
317 return S_ISREG(mode
);
319 return S_ISLNK(mode
);
321 return S_ISFIFO(mode
);
323 return S_ISSOCK(mode
);
325 return S_ISCHR(mode
);
327 return S_ISBLK(mode
);
332 return (mode
& S_ISUID
) != 0;
334 return (mode
& S_ISGID
) != 0;
336 return (mode
& S_ISVTX
) != 0;
337 /* NOTE: access() uses uid, not euid. Shouldn't matter? */
339 return access(info
->fullpath
, R_OK
) == 0;
341 return access(info
->fullpath
, W_OK
) == 0;
343 return access(info
->fullpath
, X_OK
) == 0;
345 return info
->stats
.st_size
== 0;
347 return info
->stats
.st_uid
== euid
;
353 static gboolean
test_comp(FindCondition
*condition
, FindInfo
*info
)
355 Eval
*first
= (Eval
*) condition
->data1
;
356 Eval
*second
= (Eval
*) condition
->data2
;
359 a
= first
->calc(first
, info
);
360 b
= second
->calc(second
, info
);
362 switch ((CompType
) condition
->value
)
383 /* Frees the structure and g_free()s both data items (NULL is OK) */
384 static void free_simple(FindCondition
*condition
)
386 g_return_if_fail(condition
!= NULL
);
388 g_free(condition
->data1
);
389 g_free(condition
->data2
);
393 /* Treats data1 and data2 as conditions (or NULL) and frees recursively */
394 static void free_branch(FindCondition
*condition
)
396 FindCondition
*first
= (FindCondition
*) condition
->data1
;
397 FindCondition
*second
= (FindCondition
*) condition
->data2
;
402 second
->free(second
);
406 /* Treats data1 and data2 as evals (or NULL) and frees recursively */
407 static void free_comp(FindCondition
*condition
)
409 Eval
*first
= (Eval
*) condition
->data1
;
410 Eval
*second
= (Eval
*) condition
->data2
;
415 second
->free(second
);
421 /* These all work in the same way - you give them an expression and the
422 * parse as much as they can, returning a condition for everything that
423 * was parsed and updating the expression pointer to the first unknown
424 * token. NULL indicates an error.
428 /* An expression is a series of comma-separated cases, any of which
431 static FindCondition
*parse_expression(guchar
**expression
)
433 FindCondition
*first
, *second
, *cond
;
435 first
= parse_case(expression
);
444 second
= parse_expression(expression
);
451 cond
= g_new(FindCondition
, 1);
452 cond
->test
= &test_OR
;
453 cond
->free
= &free_branch
;
455 cond
->data2
= second
;
460 static FindCondition
*parse_case(guchar
**expression
)
462 FindCondition
*first
, *second
, *cond
;
464 first
= parse_condition(expression
);
469 if (NEXT
== '\0' || NEXT
== ',' || NEXT
== ')')
472 (void) MATCH(_("And"));
474 second
= parse_case(expression
);
481 cond
= g_new(FindCondition
, 1);
482 cond
->test
= &test_AND
;
483 cond
->free
= &free_branch
;
485 cond
->data2
= second
;
490 static FindCondition
*parse_condition(guchar
**expression
)
492 FindCondition
*cond
= NULL
;
496 if (NEXT
== '!' || MATCH(_("Not")))
498 FindCondition
*operand
;
502 operand
= parse_condition(expression
);
505 cond
= g_new(FindCondition
, 1);
506 cond
->test
= test_neg
;
507 cond
->free
= free_branch
;
508 cond
->data1
= operand
;
515 FindCondition
*subcond
;
519 subcond
= parse_expression(expression
);
525 subcond
->free(subcond
);
536 return parse_match(expression
);
539 if (MATCH(_("system")))
545 return parse_system(expression
);
547 else if (MATCH(_("prune")))
549 cond
= g_new(FindCondition
, 1);
550 cond
->test
= test_prune
;
551 cond
->free
= (FindFree
) g_free
;
558 cond
= parse_is(expression
);
562 return parse_comparison(expression
);
565 /* Call this when you've just eaten 'system(' */
566 static FindCondition
*parse_system(guchar
**expression
)
568 FindCondition
*cond
= NULL
;
569 guchar
*command_string
;
571 command_string
= get_bracketed_string(expression
);
575 cond
= g_new(FindCondition
, 1);
576 cond
->test
= test_system
;
577 cond
->free
= &free_simple
;
578 cond
->data1
= command_string
;
584 static FindCondition
*parse_comparison(guchar
**expression
)
586 FindCondition
*cond
= NULL
;
593 first
= parse_eval(expression
);
603 else if (NEXT
== '>')
614 else if (NEXT
== '<')
625 else if (NEXT
== '!' && (*expression
)[1] == '=')
631 else if (MATCH(_("After")))
633 else if (MATCH(_("Before")))
639 second
= parse_eval(expression
);
646 cond
= g_new(FindCondition
, 1);
647 cond
->test
= &test_comp
;
648 cond
->free
= (FindFree
) &free_comp
;
650 cond
->data2
= second
;
656 /* Returns NULL if expression is not an is-expression */
657 static FindCondition
*parse_is(guchar
**expression
)
662 if (MATCH(_("IsReg")))
664 else if (MATCH(_("IsLink")))
666 else if (MATCH(_("IsDir")))
668 else if (MATCH(_("IsChar")))
670 else if (MATCH(_("IsBlock")))
672 else if (MATCH(_("IsDev")))
674 else if (MATCH(_("IsPipe")))
676 else if (MATCH(_("IsSocket")))
678 else if (MATCH(_("IsSUID")))
680 else if (MATCH(_("IsSGID")))
682 else if (MATCH(_("IsSticky")))
684 else if (MATCH(_("IsReadable")))
686 else if (MATCH(_("IsWriteable")))
688 else if (MATCH(_("IsExecutable")))
690 else if (MATCH(_("IsEmpty")))
692 else if (MATCH(_("IsMine")))
697 cond
= g_new(FindCondition
, 1);
698 cond
->test
= &test_is
;
699 cond
->free
= (FindFree
) &g_free
;
707 /* Call this just after reading a ' */
708 static FindCondition
*parse_match(guchar
**expression
)
710 FindCondition
*cond
= NULL
;
712 FindTest test
= &test_leaf
;
713 str
= g_string_new(NULL
);
723 if (c
== '\\' && NEXT
== '\'')
732 g_string_append_c(str
, c
);
736 cond
= g_new(FindCondition
, 1);
738 cond
->free
= &free_simple
;
739 cond
->data1
= str
->str
;
743 g_string_free(str
, cond
? FALSE
: TRUE
);
748 /* NUMERIC EXPRESSIONS */
752 static long get_constant(Eval
*eval
, FindInfo
*info
)
754 long value
= *((long *) (eval
->data1
));
755 int flags
= (int) (eval
->data2
);
757 if (flags
& FLAG_AGO
)
758 value
= info
->now
- value
;
759 else if (flags
& FLAG_HENCE
)
760 value
= info
->now
+ value
;
765 static long get_var(Eval
*eval
, FindInfo
*info
)
767 switch ((VarType
) eval
->data1
)
770 return info
->stats
.st_atime
;
772 return info
->stats
.st_ctime
;
774 return info
->stats
.st_mtime
;
776 return info
->stats
.st_size
;
778 return info
->stats
.st_ino
;
780 return info
->stats
.st_nlink
;
782 return info
->stats
.st_uid
;
784 return info
->stats
.st_gid
;
786 return info
->stats
.st_blocks
;
794 static void free_constant(Eval
*eval
)
802 /* Parse something that evaluates to a number.
803 * This function tried to get a constant - if it fails then it tries
804 * interpreting the next token as a variable.
806 static Eval
*parse_eval(guchar
**expression
)
815 value
= strtol(start
, &end
, 0);
825 return parse_variable(expression
);
832 if (MATCH(_("Byte")) || MATCH(_("Bytes")))
834 else if (MATCH("Kb"))
836 else if (MATCH("Mb"))
838 else if (MATCH("Gb"))
840 else if (MATCH(_("Sec")) || MATCH(_("Secs")))
842 else if (MATCH(_("Min")) || MATCH(_("Mins")))
844 else if (MATCH(_("Hour")) || MATCH(_("Hours")))
846 else if (MATCH(_("Day")) || MATCH(_("Days")))
847 value
*= 60 * 60 * 24;
848 else if (MATCH(_("Week")) || MATCH(_("Weeks")))
849 value
*= 60 * 60 * 24 * 7;
850 else if (MATCH(_("Year")) || MATCH(_("Years")))
851 value
*= 60 * 60 * 24 * 7 * 365.25;
853 eval
= g_new(Eval
, 1);
854 eval
->calc
= &get_constant
;
855 eval
->free
= &free_constant
;
856 eval
->data1
= g_memdup(&value
, sizeof(value
));
861 else if (MATCH(_("Hence")))
864 eval
->data2
= (gpointer
) flags
;
869 static Eval
*parse_variable(guchar
**expression
)
876 if (MATCH(_("atime")))
878 else if (MATCH(_("ctime")))
880 else if (MATCH(_("mtime")))
882 else if (MATCH(_("size")))
884 else if (MATCH(_("inode")))
886 else if (MATCH(_("nlinks")))
888 else if (MATCH(_("uid")))
890 else if (MATCH(_("gid")))
892 else if (MATCH(_("blocks")))
897 eval
= g_new(Eval
, 1);
898 eval
->calc
= &get_var
;
899 eval
->free
= (EvalFree
) &g_free
;
900 eval
->data1
= (gpointer
) var
;
906 static gboolean
match(guchar
**expression
, guchar
*word
)
911 if (g_strncasecmp(*expression
, word
, len
))
914 if (isalpha(*(*expression
+ len
)))
917 (*expression
) += len
;