2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* find.c - processes the find conditions
22 * A Condition is a tree structure. Each node has a test() fn which
23 * can be used to see whether the current file matches, and a free() fn
24 * which frees it. Both will recurse down the tree as needed.
41 typedef struct _Eval Eval
;
43 /* Static prototypes */
44 static FindCondition
*parse_expression(const gchar
**expression
);
45 static FindCondition
*parse_case(const gchar
**expression
);
46 static FindCondition
*parse_system(const gchar
**expression
);
47 static FindCondition
*parse_condition(const gchar
**expression
);
48 static FindCondition
*parse_match(const gchar
**expression
);
49 static FindCondition
*parse_comparison(const gchar
**expression
);
50 static FindCondition
*parse_dash(const gchar
**expression
);
51 static FindCondition
*parse_is(const gchar
**expression
);
52 static Eval
*parse_eval(const gchar
**expression
);
53 static Eval
*parse_variable(const gchar
**expression
);
55 static gboolean
match(const gchar
**expression
, const gchar
*word
);
104 typedef double (*EvalCalc
)(Eval
*eval
, FindInfo
*info
);
105 typedef void (*EvalFree
)(Eval
*eval
);
107 struct _FindCondition
111 /* These next three depend on the first two... */
125 #define EAT ((*expression)++)
126 #define NEXT (**expression)
127 #define SKIP while (NEXT == ' ' || NEXT == '\t') EAT
128 #define MATCH(word) (match(expression, word))
131 # define S_ISVTX 0x0001000
134 /****************************************************************
135 * EXTERNAL INTERFACE *
136 ****************************************************************/
138 /* Take a string and parse it, returning a condition object which
139 * can be passed to find_test_condition() later. NULL if the string
140 * is not a valid expression.
142 FindCondition
*find_compile(const gchar
*string
)
145 const gchar
**expression
= &string
;
147 g_return_val_if_fail(string
!= NULL
, NULL
);
149 cond
= parse_expression(expression
);
163 gboolean
find_test_condition(FindCondition
*condition
, FindInfo
*info
)
165 g_return_val_if_fail(condition
!= NULL
, FALSE
);
166 g_return_val_if_fail(info
!= NULL
, FALSE
);
168 return condition
->test(condition
, info
);
171 void find_condition_free(FindCondition
*condition
)
174 condition
->free(condition
);
177 /****************************************************************
178 * INTERNAL FUNCTIONS *
179 ****************************************************************/
181 /* Call this when you've just eaten '('. Returns the string upto the
182 * matching ')' (and eats that bracket), or NULL on failure.
183 * Brackets within the string may be quoted or escaped.
185 static gchar
*get_bracketed_string(const gchar
**expression
)
191 str
= g_string_new(NULL
);
201 if (c
== '\'' || c
== '"')
202 quote
= c
; /* Start quoted section */
203 else if (c
== '\\' && (NEXT
== '(' || NEXT
== ')'))
213 gchar
*retval
= str
->str
;
215 g_string_free(str
, FALSE
);
223 quote
= '\0'; /* End quoted section */
224 else if (c
== '\\' && NEXT
== quote
)
226 g_string_append_c(str
, c
);
231 g_string_append_c(str
, c
);
234 g_string_free(str
, TRUE
);
242 static gboolean
test_prune(FindCondition
*condition
, FindInfo
*info
)
248 static gboolean
test_leaf(FindCondition
*condition
, FindInfo
*info
)
250 return fnmatch(condition
->data1
, info
->leaf
, 0) == 0;
253 static gboolean
test_path(FindCondition
*condition
, FindInfo
*info
)
255 return fnmatch(condition
->data1
, info
->fullpath
, FNM_PATHNAME
) == 0;
258 static gboolean
test_system(FindCondition
*condition
, FindInfo
*info
)
260 gchar
*command
= (gchar
*) condition
->data1
;
261 gchar
*start
= command
;
262 GString
*to_sys
= NULL
;
266 to_sys
= g_string_new(NULL
);
268 while ((perc
= strchr(command
, '%')))
270 if (perc
> start
&& perc
[-1] == '\\')
273 while (command
< perc
)
274 g_string_append_c(to_sys
, *(command
++));
277 g_string_append(to_sys
, info
->fullpath
);
280 g_string_append_c(to_sys
, '%');
287 g_string_append(to_sys
, command
);
289 retcode
= system(to_sys
->str
);
291 g_string_free(to_sys
, TRUE
);
296 static gboolean
test_OR(FindCondition
*condition
, FindInfo
*info
)
298 FindCondition
*first
= (FindCondition
*) condition
->data1
;
299 FindCondition
*second
= (FindCondition
*) condition
->data2
;
301 return first
->test(first
, info
) || second
->test(second
, info
);
304 static gboolean
test_AND(FindCondition
*condition
, FindInfo
*info
)
306 FindCondition
*first
= (FindCondition
*) condition
->data1
;
307 FindCondition
*second
= (FindCondition
*) condition
->data2
;
309 return first
->test(first
, info
) && second
->test(second
, info
);
312 static gboolean
test_neg(FindCondition
*condition
, FindInfo
*info
)
314 FindCondition
*first
= (FindCondition
*) condition
->data1
;
316 return !first
->test(first
, info
);
319 static gboolean
test_is(FindCondition
*condition
, FindInfo
*info
)
321 mode_t mode
= info
->stats
.st_mode
;
323 switch ((IsTest
) condition
->value
)
326 return S_ISDIR(mode
);
328 return S_ISREG(mode
);
330 return S_ISLNK(mode
);
332 return S_ISFIFO(mode
);
334 return S_ISSOCK(mode
);
336 return S_ISCHR(mode
);
338 return S_ISBLK(mode
);
343 return S_ISDOOR(mode
);
345 return (mode
& S_ISUID
) != 0;
347 return (mode
& S_ISGID
) != 0;
349 return (mode
& S_ISVTX
) != 0;
350 /* NOTE: access() uses uid, not euid. Shouldn't matter? */
352 return access(info
->fullpath
, R_OK
) == 0;
354 return access(info
->fullpath
, W_OK
) == 0;
356 return access(info
->fullpath
, X_OK
) == 0;
358 return info
->stats
.st_size
== 0;
360 return info
->stats
.st_uid
== euid
;
366 static gboolean
test_comp(FindCondition
*condition
, FindInfo
*info
)
368 Eval
*first
= (Eval
*) condition
->data1
;
369 Eval
*second
= (Eval
*) condition
->data2
;
372 a
= first
->calc(first
, info
);
373 b
= second
->calc(second
, info
);
375 switch ((CompType
) condition
->value
)
396 /* Frees the structure and g_free()s both data items (NULL is OK) */
397 static void free_simple(FindCondition
*condition
)
399 g_return_if_fail(condition
!= NULL
);
401 g_free(condition
->data1
);
402 g_free(condition
->data2
);
406 /* Treats data1 and data2 as conditions (or NULL) and frees recursively */
407 static void free_branch(FindCondition
*condition
)
409 FindCondition
*first
= (FindCondition
*) condition
->data1
;
410 FindCondition
*second
= (FindCondition
*) condition
->data2
;
415 second
->free(second
);
419 /* Treats data1 and data2 as evals (or NULL) and frees recursively */
420 static void free_comp(FindCondition
*condition
)
422 Eval
*first
= (Eval
*) condition
->data1
;
423 Eval
*second
= (Eval
*) condition
->data2
;
428 second
->free(second
);
434 /* These all work in the same way - you give them an expression and the
435 * parse as much as they can, returning a condition for everything that
436 * was parsed and updating the expression pointer to the first unknown
437 * token. NULL indicates an error.
441 /* An expression is a series of comma-separated cases, any of which
444 static FindCondition
*parse_expression(const gchar
**expression
)
446 FindCondition
*first
, *second
, *cond
;
448 first
= parse_case(expression
);
457 second
= parse_expression(expression
);
464 cond
= g_new(FindCondition
, 1);
465 cond
->test
= &test_OR
;
466 cond
->free
= &free_branch
;
468 cond
->data2
= second
;
473 static FindCondition
*parse_case(const gchar
**expression
)
475 FindCondition
*first
, *second
, *cond
;
477 first
= parse_condition(expression
);
482 if (NEXT
== '\0' || NEXT
== ',' || NEXT
== ')')
485 (void) MATCH(_("And"));
487 second
= parse_case(expression
);
494 cond
= g_new(FindCondition
, 1);
495 cond
->test
= &test_AND
;
496 cond
->free
= &free_branch
;
498 cond
->data2
= second
;
503 static FindCondition
*parse_condition(const gchar
**expression
)
505 FindCondition
*cond
= NULL
;
509 if (NEXT
== '!' || MATCH(_("Not")))
511 FindCondition
*operand
;
515 operand
= parse_condition(expression
);
518 cond
= g_new(FindCondition
, 1);
519 cond
->test
= test_neg
;
520 cond
->free
= free_branch
;
521 cond
->data1
= operand
;
528 FindCondition
*subcond
;
532 subcond
= parse_expression(expression
);
538 subcond
->free(subcond
);
549 return parse_match(expression
);
552 if (MATCH(_("system")))
558 return parse_system(expression
);
560 else if (MATCH(_("prune")))
562 cond
= g_new(FindCondition
, 1);
563 cond
->test
= test_prune
;
564 cond
->free
= (FindFree
) g_free
;
571 cond
= parse_dash(expression
);
575 cond
= parse_is(expression
);
579 return parse_comparison(expression
);
582 /* Call this when you've just eaten 'system(' */
583 static FindCondition
*parse_system(const gchar
**expression
)
585 FindCondition
*cond
= NULL
;
586 gchar
*command_string
;
588 command_string
= get_bracketed_string(expression
);
592 cond
= g_new(FindCondition
, 1);
593 cond
->test
= test_system
;
594 cond
->free
= &free_simple
;
595 cond
->data1
= command_string
;
601 static FindCondition
*parse_comparison(const gchar
**expression
)
603 FindCondition
*cond
= NULL
;
610 first
= parse_eval(expression
);
620 else if (NEXT
== '>')
631 else if (NEXT
== '<')
642 else if (NEXT
== '!' && (*expression
)[1] == '=')
648 else if (MATCH(_("After")))
650 else if (MATCH(_("Before")))
656 second
= parse_eval(expression
);
663 cond
= g_new(FindCondition
, 1);
664 cond
->test
= &test_comp
;
665 cond
->free
= (FindFree
) &free_comp
;
667 cond
->data2
= second
;
673 static FindCondition
*parse_dash(const gchar
**expression
)
675 const gchar
*exp
= *expression
;
676 FindCondition
*cond
, *retval
= NULL
;
683 while (exp
[i
] && !g_ascii_isspace(exp
[i
]))
687 case 'f': test
= IS_REG
; break;
688 case 'l': test
= IS_LNK
; break;
689 case 'd': test
= IS_DIR
; break;
690 case 'b': test
= IS_BLK
; break;
691 case 'c': test
= IS_CHR
; break;
692 case 'D': test
= IS_DEV
; break;
693 case 'p': test
= IS_FIFO
; break;
694 case 'S': test
= IS_SOCK
; break;
695 case 'O': test
= IS_DOOR
; break;
696 case 'u': test
= IS_SUID
; break;
697 case 'g': test
= IS_SGID
; break;
698 case 'k': test
= IS_STICKY
; break;
699 case 'r': test
= IS_READABLE
; break;
700 case 'w': test
= IS_WRITEABLE
; break;
701 case 'x': test
= IS_EXEC
; break;
702 case 'o': test
= IS_MINE
; break;
703 case 'z': test
= IS_EMPTY
; break;
705 find_condition_free(retval
);
710 cond
= g_new(FindCondition
, 1);
711 cond
->test
= &test_is
;
712 cond
->free
= (FindFree
) &g_free
;
721 new = g_new(FindCondition
, 1);
722 new->test
= &test_AND
;
723 new->free
= &free_branch
;
738 /* Returns NULL if expression is not an is-expression */
739 static FindCondition
*parse_is(const gchar
**expression
)
744 if (MATCH(_("IsReg")))
746 else if (MATCH(_("IsLink")))
748 else if (MATCH(_("IsDir")))
750 else if (MATCH(_("IsChar")))
752 else if (MATCH(_("IsBlock")))
754 else if (MATCH(_("IsDev")))
756 else if (MATCH(_("IsPipe")))
758 else if (MATCH(_("IsSocket")))
760 else if (MATCH(_("IsDoor")))
762 else if (MATCH(_("IsSUID")))
764 else if (MATCH(_("IsSGID")))
766 else if (MATCH(_("IsSticky")))
768 else if (MATCH(_("IsReadable")))
770 else if (MATCH(_("IsWriteable")))
772 else if (MATCH(_("IsExecutable")))
774 else if (MATCH(_("IsEmpty")))
776 else if (MATCH(_("IsMine")))
781 cond
= g_new(FindCondition
, 1);
782 cond
->test
= &test_is
;
783 cond
->free
= (FindFree
) &g_free
;
791 /* Call this just after reading a ' */
792 static FindCondition
*parse_match(const gchar
**expression
)
794 FindCondition
*cond
= NULL
;
796 FindTest test
= &test_leaf
;
797 str
= g_string_new(NULL
);
807 if (c
== '\\' && NEXT
== '\'')
816 g_string_append_c(str
, c
);
820 cond
= g_new(FindCondition
, 1);
822 cond
->free
= &free_simple
;
823 cond
->data1
= str
->str
;
827 g_string_free(str
, cond
? FALSE
: TRUE
);
832 /* NUMERIC EXPRESSIONS */
836 static double get_constant(Eval
*eval
, FindInfo
*info
)
838 double value
= *((double *) (eval
->data1
));
839 gint flags
= GPOINTER_TO_INT(eval
->data2
);
841 if (flags
& FLAG_AGO
)
842 value
= info
->now
- value
;
843 else if (flags
& FLAG_HENCE
)
844 value
= info
->now
+ value
;
849 static double get_var(Eval
*eval
, FindInfo
*info
)
851 switch ((VarType
) eval
->data1
)
854 return info
->stats
.st_atime
;
856 return info
->stats
.st_ctime
;
858 return info
->stats
.st_mtime
;
860 return info
->stats
.st_size
;
862 return info
->stats
.st_ino
;
864 return info
->stats
.st_nlink
;
866 return info
->stats
.st_uid
;
868 return info
->stats
.st_gid
;
870 return info
->stats
.st_blocks
;
878 static void free_constant(Eval
*eval
)
886 /* Parse something that evaluates to a number.
887 * This function tries to get a constant - if it fails then it tries
888 * interpreting the next token as a variable.
890 static Eval
*parse_eval(const gchar
**expression
)
900 value
= strtol(start
, &end
, 0);
910 return parse_variable(expression
);
917 if (MATCH(_("Byte")) || MATCH(_("Bytes")))
919 else if (MATCH(_("Kb")) || MATCH(_("K")))
921 else if (MATCH(_("Mb")) || MATCH(_("M")))
923 else if (MATCH(_("Gb")) || MATCH(_("G")))
925 else if (MATCH(_("Sec")) || MATCH(_("Secs")))
927 else if (MATCH(_("Min")) || MATCH(_("Mins")))
929 else if (MATCH(_("Hour")) || MATCH(_("Hours")))
931 else if (MATCH(_("Day")) || MATCH(_("Days")))
932 value
*= 60 * 60 * 24;
933 else if (MATCH(_("Week")) || MATCH(_("Weeks")))
934 value
*= 60 * 60 * 24 * 7;
935 else if (MATCH(_("Year")) || MATCH(_("Years")))
936 value
*= 60 * 60 * 24 * 7 * 365.25;
938 eval
= g_new(Eval
, 1);
939 eval
->calc
= &get_constant
;
940 eval
->free
= &free_constant
;
941 eval
->data1
= g_memdup(&value
, sizeof(value
));
946 else if (MATCH(_("Hence")))
949 eval
->data2
= GINT_TO_POINTER(flags
);
954 static Eval
*parse_variable(const gchar
**expression
)
961 if (MATCH(_("atime")))
963 else if (MATCH(_("ctime")))
965 else if (MATCH(_("mtime")))
967 else if (MATCH(_("size")))
969 else if (MATCH(_("inode")))
971 else if (MATCH(_("nlinks")))
973 else if (MATCH(_("uid")))
975 else if (MATCH(_("gid")))
977 else if (MATCH(_("blocks")))
982 eval
= g_new(Eval
, 1);
983 eval
->calc
= &get_var
;
984 eval
->free
= (EvalFree
) &g_free
;
985 eval
->data1
= (gpointer
) var
;
991 static gboolean
match(const gchar
**expression
, const gchar
*word
)
996 if (g_strncasecmp(*expression
, word
, len
))
999 if (g_ascii_isalpha(*(*expression
+ len
)))
1002 (*expression
) += len
;