4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
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.
43 typedef struct _Eval Eval
;
45 /* Static prototypes */
46 static FindCondition
*parse_expression(const gchar
**expression
);
47 static FindCondition
*parse_case(const gchar
**expression
);
48 static FindCondition
*parse_system(const gchar
**expression
);
49 static FindCondition
*parse_condition(const gchar
**expression
);
50 static FindCondition
*parse_match(const gchar
**expression
);
51 static FindCondition
*parse_comparison(const gchar
**expression
);
52 static FindCondition
*parse_dash(const gchar
**expression
);
53 static FindCondition
*parse_is(const gchar
**expression
);
54 static Eval
*parse_eval(const gchar
**expression
);
55 static Eval
*parse_variable(const gchar
**expression
);
57 static gboolean
match(const gchar
**expression
, const gchar
*word
);
105 typedef double (*EvalCalc
)(Eval
*eval
, FindInfo
*info
);
106 typedef void (*EvalFree
)(Eval
*eval
);
108 struct _FindCondition
112 /* These next three depend on the first two... */
126 #define EAT ((*expression)++)
127 #define NEXT (**expression)
128 #define SKIP while (NEXT == ' ' || NEXT == '\t') EAT
129 #define MATCH(word) (match(expression, word))
132 # define S_ISVTX 0x0001000
135 /****************************************************************
136 * EXTERNAL INTERFACE *
137 ****************************************************************/
139 /* Take a string and parse it, returning a condition object which
140 * can be passed to find_test_condition() later. NULL if the string
141 * is not a valid expression.
143 FindCondition
*find_compile(const gchar
*string
)
146 const gchar
**expression
= &string
;
148 g_return_val_if_fail(string
!= NULL
, NULL
);
150 cond
= parse_expression(expression
);
164 gboolean
find_test_condition(FindCondition
*condition
, FindInfo
*info
)
166 g_return_val_if_fail(condition
!= NULL
, FALSE
);
167 g_return_val_if_fail(info
!= NULL
, FALSE
);
169 return condition
->test(condition
, info
);
172 void find_condition_free(FindCondition
*condition
)
174 g_return_if_fail(condition
!= NULL
);
176 condition
->free(condition
);
179 /****************************************************************
180 * INTERNAL FUNCTIONS *
181 ****************************************************************/
183 /* Call this when you've just eaten '('. Returns the string upto the
184 * matching ')' (and eats that bracket), or NULL on failure.
185 * Brackets within the string may be quoted or escaped.
187 static gchar
*get_bracketed_string(const gchar
**expression
)
193 str
= g_string_new(NULL
);
203 if (c
== '\'' || c
== '"')
204 quote
= c
; /* Start quoted section */
205 else if (c
== '\\' && (NEXT
== '(' || NEXT
== ')'))
215 gchar
*retval
= str
->str
;
217 g_string_free(str
, FALSE
);
225 quote
= '\0'; /* End quoted section */
226 else if (c
== '\\' && NEXT
== quote
)
228 g_string_append_c(str
, c
);
233 g_string_append_c(str
, c
);
236 g_string_free(str
, TRUE
);
244 static gboolean
test_prune(FindCondition
*condition
, FindInfo
*info
)
250 static gboolean
test_leaf(FindCondition
*condition
, FindInfo
*info
)
252 return fnmatch(condition
->data1
, info
->leaf
, 0) == 0;
255 static gboolean
test_path(FindCondition
*condition
, FindInfo
*info
)
257 return fnmatch(condition
->data1
, info
->fullpath
, FNM_PATHNAME
) == 0;
260 static gboolean
test_system(FindCondition
*condition
, FindInfo
*info
)
262 gchar
*command
= (gchar
*) condition
->data1
;
263 gchar
*start
= command
;
264 GString
*to_sys
= NULL
;
268 to_sys
= g_string_new(NULL
);
270 while ((perc
= strchr(command
, '%')))
272 if (perc
> start
&& perc
[-1] == '\\')
275 while (command
< perc
)
276 g_string_append_c(to_sys
, *(command
++));
279 g_string_append(to_sys
, info
->fullpath
);
282 g_string_append_c(to_sys
, '%');
289 g_string_append(to_sys
, command
);
291 retcode
= system(to_sys
->str
);
293 g_string_free(to_sys
, TRUE
);
298 static gboolean
test_OR(FindCondition
*condition
, FindInfo
*info
)
300 FindCondition
*first
= (FindCondition
*) condition
->data1
;
301 FindCondition
*second
= (FindCondition
*) condition
->data2
;
303 return first
->test(first
, info
) || second
->test(second
, info
);
306 static gboolean
test_AND(FindCondition
*condition
, FindInfo
*info
)
308 FindCondition
*first
= (FindCondition
*) condition
->data1
;
309 FindCondition
*second
= (FindCondition
*) condition
->data2
;
311 return first
->test(first
, info
) && second
->test(second
, info
);
314 static gboolean
test_neg(FindCondition
*condition
, FindInfo
*info
)
316 FindCondition
*first
= (FindCondition
*) condition
->data1
;
318 return !first
->test(first
, info
);
321 static gboolean
test_is(FindCondition
*condition
, FindInfo
*info
)
323 mode_t mode
= info
->stats
.st_mode
;
325 switch ((IsTest
) condition
->value
)
328 return S_ISDIR(mode
);
330 return S_ISREG(mode
);
332 return S_ISLNK(mode
);
334 return S_ISFIFO(mode
);
336 return S_ISSOCK(mode
);
338 return S_ISCHR(mode
);
340 return S_ISBLK(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
] && !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 'u': test
= IS_SUID
; break;;
696 case 'g': test
= IS_SGID
; break;;
697 case 'k': test
= IS_STICKY
; break;;
698 case 'r': test
= IS_READABLE
; break;;
699 case 'w': test
= IS_WRITEABLE
; break;;
700 case 'x': test
= IS_EXEC
; break;;
701 case 'o': test
= IS_MINE
; break;;
702 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(_("IsSUID")))
762 else if (MATCH(_("IsSGID")))
764 else if (MATCH(_("IsSticky")))
766 else if (MATCH(_("IsReadable")))
768 else if (MATCH(_("IsWriteable")))
770 else if (MATCH(_("IsExecutable")))
772 else if (MATCH(_("IsEmpty")))
774 else if (MATCH(_("IsMine")))
779 cond
= g_new(FindCondition
, 1);
780 cond
->test
= &test_is
;
781 cond
->free
= (FindFree
) &g_free
;
789 /* Call this just after reading a ' */
790 static FindCondition
*parse_match(const gchar
**expression
)
792 FindCondition
*cond
= NULL
;
794 FindTest test
= &test_leaf
;
795 str
= g_string_new(NULL
);
805 if (c
== '\\' && NEXT
== '\'')
814 g_string_append_c(str
, c
);
818 cond
= g_new(FindCondition
, 1);
820 cond
->free
= &free_simple
;
821 cond
->data1
= str
->str
;
825 g_string_free(str
, cond
? FALSE
: TRUE
);
830 /* NUMERIC EXPRESSIONS */
834 static double get_constant(Eval
*eval
, FindInfo
*info
)
836 double value
= *((double *) (eval
->data1
));
837 gint flags
= GPOINTER_TO_INT(eval
->data2
);
839 if (flags
& FLAG_AGO
)
840 value
= info
->now
- value
;
841 else if (flags
& FLAG_HENCE
)
842 value
= info
->now
+ value
;
847 static double get_var(Eval
*eval
, FindInfo
*info
)
849 switch ((VarType
) eval
->data1
)
852 return info
->stats
.st_atime
;
854 return info
->stats
.st_ctime
;
856 return info
->stats
.st_mtime
;
858 return info
->stats
.st_size
;
860 return info
->stats
.st_ino
;
862 return info
->stats
.st_nlink
;
864 return info
->stats
.st_uid
;
866 return info
->stats
.st_gid
;
868 return info
->stats
.st_blocks
;
876 static void free_constant(Eval
*eval
)
884 /* Parse something that evaluates to a number.
885 * This function tries to get a constant - if it fails then it tries
886 * interpreting the next token as a variable.
888 static Eval
*parse_eval(const gchar
**expression
)
898 value
= strtol(start
, &end
, 0);
908 return parse_variable(expression
);
915 if (MATCH(_("Byte")) || MATCH(_("Bytes")))
917 else if (MATCH("Kb"))
919 else if (MATCH("Mb"))
921 else if (MATCH("Gb"))
923 else if (MATCH(_("Sec")) || MATCH(_("Secs")))
925 else if (MATCH(_("Min")) || MATCH(_("Mins")))
927 else if (MATCH(_("Hour")) || MATCH(_("Hours")))
929 else if (MATCH(_("Day")) || MATCH(_("Days")))
930 value
*= 60 * 60 * 24;
931 else if (MATCH(_("Week")) || MATCH(_("Weeks")))
932 value
*= 60 * 60 * 24 * 7;
933 else if (MATCH(_("Year")) || MATCH(_("Years")))
934 value
*= 60 * 60 * 24 * 7 * 365.25;
936 eval
= g_new(Eval
, 1);
937 eval
->calc
= &get_constant
;
938 eval
->free
= &free_constant
;
939 eval
->data1
= g_memdup(&value
, sizeof(value
));
944 else if (MATCH(_("Hence")))
947 eval
->data2
= GINT_TO_POINTER(flags
);
952 static Eval
*parse_variable(const gchar
**expression
)
959 if (MATCH(_("atime")))
961 else if (MATCH(_("ctime")))
963 else if (MATCH(_("mtime")))
965 else if (MATCH(_("size")))
967 else if (MATCH(_("inode")))
969 else if (MATCH(_("nlinks")))
971 else if (MATCH(_("uid")))
973 else if (MATCH(_("gid")))
975 else if (MATCH(_("blocks")))
980 eval
= g_new(Eval
, 1);
981 eval
->calc
= &get_var
;
982 eval
->free
= (EvalFree
) &g_free
;
983 eval
->data1
= (gpointer
) var
;
989 static gboolean
match(const gchar
**expression
, const gchar
*word
)
994 if (g_strncasecmp(*expression
, word
, len
))
997 if (isalpha(*(*expression
+ len
)))
1000 (*expression
) += len
;