4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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
);
106 typedef double (*EvalCalc
)(Eval
*eval
, FindInfo
*info
);
107 typedef void (*EvalFree
)(Eval
*eval
);
109 struct _FindCondition
113 /* These next three depend on the first two... */
127 #define EAT ((*expression)++)
128 #define NEXT (**expression)
129 #define SKIP while (NEXT == ' ' || NEXT == '\t') EAT
130 #define MATCH(word) (match(expression, word))
133 # define S_ISVTX 0x0001000
136 /****************************************************************
137 * EXTERNAL INTERFACE *
138 ****************************************************************/
140 /* Take a string and parse it, returning a condition object which
141 * can be passed to find_test_condition() later. NULL if the string
142 * is not a valid expression.
144 FindCondition
*find_compile(const gchar
*string
)
147 const gchar
**expression
= &string
;
149 g_return_val_if_fail(string
!= NULL
, NULL
);
151 cond
= parse_expression(expression
);
165 gboolean
find_test_condition(FindCondition
*condition
, FindInfo
*info
)
167 g_return_val_if_fail(condition
!= NULL
, FALSE
);
168 g_return_val_if_fail(info
!= NULL
, FALSE
);
170 return condition
->test(condition
, info
);
173 void find_condition_free(FindCondition
*condition
)
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 S_ISDOOR(mode
);
347 return (mode
& S_ISUID
) != 0;
349 return (mode
& S_ISGID
) != 0;
351 return (mode
& S_ISVTX
) != 0;
352 /* NOTE: access() uses uid, not euid. Shouldn't matter? */
354 return access(info
->fullpath
, R_OK
) == 0;
356 return access(info
->fullpath
, W_OK
) == 0;
358 return access(info
->fullpath
, X_OK
) == 0;
360 return info
->stats
.st_size
== 0;
362 return info
->stats
.st_uid
== euid
;
368 static gboolean
test_comp(FindCondition
*condition
, FindInfo
*info
)
370 Eval
*first
= (Eval
*) condition
->data1
;
371 Eval
*second
= (Eval
*) condition
->data2
;
374 a
= first
->calc(first
, info
);
375 b
= second
->calc(second
, info
);
377 switch ((CompType
) condition
->value
)
398 /* Frees the structure and g_free()s both data items (NULL is OK) */
399 static void free_simple(FindCondition
*condition
)
401 g_return_if_fail(condition
!= NULL
);
403 g_free(condition
->data1
);
404 g_free(condition
->data2
);
408 /* Treats data1 and data2 as conditions (or NULL) and frees recursively */
409 static void free_branch(FindCondition
*condition
)
411 FindCondition
*first
= (FindCondition
*) condition
->data1
;
412 FindCondition
*second
= (FindCondition
*) condition
->data2
;
417 second
->free(second
);
421 /* Treats data1 and data2 as evals (or NULL) and frees recursively */
422 static void free_comp(FindCondition
*condition
)
424 Eval
*first
= (Eval
*) condition
->data1
;
425 Eval
*second
= (Eval
*) condition
->data2
;
430 second
->free(second
);
436 /* These all work in the same way - you give them an expression and the
437 * parse as much as they can, returning a condition for everything that
438 * was parsed and updating the expression pointer to the first unknown
439 * token. NULL indicates an error.
443 /* An expression is a series of comma-separated cases, any of which
446 static FindCondition
*parse_expression(const gchar
**expression
)
448 FindCondition
*first
, *second
, *cond
;
450 first
= parse_case(expression
);
459 second
= parse_expression(expression
);
466 cond
= g_new(FindCondition
, 1);
467 cond
->test
= &test_OR
;
468 cond
->free
= &free_branch
;
470 cond
->data2
= second
;
475 static FindCondition
*parse_case(const gchar
**expression
)
477 FindCondition
*first
, *second
, *cond
;
479 first
= parse_condition(expression
);
484 if (NEXT
== '\0' || NEXT
== ',' || NEXT
== ')')
487 (void) MATCH(_("And"));
489 second
= parse_case(expression
);
496 cond
= g_new(FindCondition
, 1);
497 cond
->test
= &test_AND
;
498 cond
->free
= &free_branch
;
500 cond
->data2
= second
;
505 static FindCondition
*parse_condition(const gchar
**expression
)
507 FindCondition
*cond
= NULL
;
511 if (NEXT
== '!' || MATCH(_("Not")))
513 FindCondition
*operand
;
517 operand
= parse_condition(expression
);
520 cond
= g_new(FindCondition
, 1);
521 cond
->test
= test_neg
;
522 cond
->free
= free_branch
;
523 cond
->data1
= operand
;
530 FindCondition
*subcond
;
534 subcond
= parse_expression(expression
);
540 subcond
->free(subcond
);
551 return parse_match(expression
);
554 if (MATCH(_("system")))
560 return parse_system(expression
);
562 else if (MATCH(_("prune")))
564 cond
= g_new(FindCondition
, 1);
565 cond
->test
= test_prune
;
566 cond
->free
= (FindFree
) g_free
;
573 cond
= parse_dash(expression
);
577 cond
= parse_is(expression
);
581 return parse_comparison(expression
);
584 /* Call this when you've just eaten 'system(' */
585 static FindCondition
*parse_system(const gchar
**expression
)
587 FindCondition
*cond
= NULL
;
588 gchar
*command_string
;
590 command_string
= get_bracketed_string(expression
);
594 cond
= g_new(FindCondition
, 1);
595 cond
->test
= test_system
;
596 cond
->free
= &free_simple
;
597 cond
->data1
= command_string
;
603 static FindCondition
*parse_comparison(const gchar
**expression
)
605 FindCondition
*cond
= NULL
;
612 first
= parse_eval(expression
);
622 else if (NEXT
== '>')
633 else if (NEXT
== '<')
644 else if (NEXT
== '!' && (*expression
)[1] == '=')
650 else if (MATCH(_("After")))
652 else if (MATCH(_("Before")))
658 second
= parse_eval(expression
);
665 cond
= g_new(FindCondition
, 1);
666 cond
->test
= &test_comp
;
667 cond
->free
= (FindFree
) &free_comp
;
669 cond
->data2
= second
;
675 static FindCondition
*parse_dash(const gchar
**expression
)
677 const gchar
*exp
= *expression
;
678 FindCondition
*cond
, *retval
= NULL
;
685 while (exp
[i
] && !g_ascii_isspace(exp
[i
]))
689 case 'f': test
= IS_REG
; break;
690 case 'l': test
= IS_LNK
; break;
691 case 'd': test
= IS_DIR
; break;
692 case 'b': test
= IS_BLK
; break;
693 case 'c': test
= IS_CHR
; break;
694 case 'D': test
= IS_DEV
; break;
695 case 'p': test
= IS_FIFO
; break;
696 case 'S': test
= IS_SOCK
; break;
697 case 'O': test
= IS_DOOR
; break;
698 case 'u': test
= IS_SUID
; break;
699 case 'g': test
= IS_SGID
; break;
700 case 'k': test
= IS_STICKY
; break;
701 case 'r': test
= IS_READABLE
; break;
702 case 'w': test
= IS_WRITEABLE
; break;
703 case 'x': test
= IS_EXEC
; break;
704 case 'o': test
= IS_MINE
; break;
705 case 'z': test
= IS_EMPTY
; break;
707 find_condition_free(retval
);
712 cond
= g_new(FindCondition
, 1);
713 cond
->test
= &test_is
;
714 cond
->free
= (FindFree
) &g_free
;
723 new = g_new(FindCondition
, 1);
724 new->test
= &test_AND
;
725 new->free
= &free_branch
;
740 /* Returns NULL if expression is not an is-expression */
741 static FindCondition
*parse_is(const gchar
**expression
)
746 if (MATCH(_("IsReg")))
748 else if (MATCH(_("IsLink")))
750 else if (MATCH(_("IsDir")))
752 else if (MATCH(_("IsChar")))
754 else if (MATCH(_("IsBlock")))
756 else if (MATCH(_("IsDev")))
758 else if (MATCH(_("IsPipe")))
760 else if (MATCH(_("IsSocket")))
762 else if (MATCH(_("IsDoor")))
764 else if (MATCH(_("IsSUID")))
766 else if (MATCH(_("IsSGID")))
768 else if (MATCH(_("IsSticky")))
770 else if (MATCH(_("IsReadable")))
772 else if (MATCH(_("IsWriteable")))
774 else if (MATCH(_("IsExecutable")))
776 else if (MATCH(_("IsEmpty")))
778 else if (MATCH(_("IsMine")))
783 cond
= g_new(FindCondition
, 1);
784 cond
->test
= &test_is
;
785 cond
->free
= (FindFree
) &g_free
;
793 /* Call this just after reading a ' */
794 static FindCondition
*parse_match(const gchar
**expression
)
796 FindCondition
*cond
= NULL
;
798 FindTest test
= &test_leaf
;
799 str
= g_string_new(NULL
);
809 if (c
== '\\' && NEXT
== '\'')
818 g_string_append_c(str
, c
);
822 cond
= g_new(FindCondition
, 1);
824 cond
->free
= &free_simple
;
825 cond
->data1
= str
->str
;
829 g_string_free(str
, cond
? FALSE
: TRUE
);
834 /* NUMERIC EXPRESSIONS */
838 static double get_constant(Eval
*eval
, FindInfo
*info
)
840 double value
= *((double *) (eval
->data1
));
841 gint flags
= GPOINTER_TO_INT(eval
->data2
);
843 if (flags
& FLAG_AGO
)
844 value
= info
->now
- value
;
845 else if (flags
& FLAG_HENCE
)
846 value
= info
->now
+ value
;
851 static double get_var(Eval
*eval
, FindInfo
*info
)
853 switch ((VarType
) eval
->data1
)
856 return info
->stats
.st_atime
;
858 return info
->stats
.st_ctime
;
860 return info
->stats
.st_mtime
;
862 return info
->stats
.st_size
;
864 return info
->stats
.st_ino
;
866 return info
->stats
.st_nlink
;
868 return info
->stats
.st_uid
;
870 return info
->stats
.st_gid
;
872 return info
->stats
.st_blocks
;
880 static void free_constant(Eval
*eval
)
888 /* Parse something that evaluates to a number.
889 * This function tries to get a constant - if it fails then it tries
890 * interpreting the next token as a variable.
892 static Eval
*parse_eval(const gchar
**expression
)
902 value
= strtol(start
, &end
, 0);
912 return parse_variable(expression
);
919 if (MATCH(_("Byte")) || MATCH(_("Bytes")))
921 else if (MATCH(_("Kb")) || MATCH(_("K")))
923 else if (MATCH(_("Mb")) || MATCH(_("M")))
925 else if (MATCH(_("Gb")) || MATCH(_("G")))
927 else if (MATCH(_("Sec")) || MATCH(_("Secs")))
929 else if (MATCH(_("Min")) || MATCH(_("Mins")))
931 else if (MATCH(_("Hour")) || MATCH(_("Hours")))
933 else if (MATCH(_("Day")) || MATCH(_("Days")))
934 value
*= 60 * 60 * 24;
935 else if (MATCH(_("Week")) || MATCH(_("Weeks")))
936 value
*= 60 * 60 * 24 * 7;
937 else if (MATCH(_("Year")) || MATCH(_("Years")))
938 value
*= 60 * 60 * 24 * 7 * 365.25;
940 eval
= g_new(Eval
, 1);
941 eval
->calc
= &get_constant
;
942 eval
->free
= &free_constant
;
943 eval
->data1
= g_memdup(&value
, sizeof(value
));
948 else if (MATCH(_("Hence")))
951 eval
->data2
= GINT_TO_POINTER(flags
);
956 static Eval
*parse_variable(const gchar
**expression
)
963 if (MATCH(_("atime")))
965 else if (MATCH(_("ctime")))
967 else if (MATCH(_("mtime")))
969 else if (MATCH(_("size")))
971 else if (MATCH(_("inode")))
973 else if (MATCH(_("nlinks")))
975 else if (MATCH(_("uid")))
977 else if (MATCH(_("gid")))
979 else if (MATCH(_("blocks")))
984 eval
= g_new(Eval
, 1);
985 eval
->calc
= &get_var
;
986 eval
->free
= (EvalFree
) &g_free
;
987 eval
->data1
= (gpointer
) var
;
993 static gboolean
match(const gchar
**expression
, const gchar
*word
)
998 if (g_strncasecmp(*expression
, word
, len
))
1001 if (g_ascii_isalpha(*(*expression
+ len
)))
1004 (*expression
) += len
;