2 #include <atalk/standards.h>
11 #include <atalk/talloc.h>
12 #include <atalk/logger.h>
13 #include <atalk/errchk.h>
14 #include <atalk/spotlight.h>
16 #include "slmod_rdf_map.h"
18 struct yy_buffer_state;
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int yylex (void);
21 extern void yyerror (char const *);
22 extern void *yyterminate(void);
23 extern YY_BUFFER_STATE yy_scan_string( const char *str);
24 extern void yy_delete_buffer ( YY_BUFFER_STATE buffer );
26 /* forward declarations */
27 static const char *map_expr(const char *attr, char op, const char *val);
28 static const char *map_daterange(const char *dateattr, time_t date1, time_t date2);
29 static time_t isodate2unix(const char *s);
31 /* global vars, eg needed by the lexer */
35 static gchar *srp_result;
36 static gchar *srp_fts;
40 #define SPRAW_TIME_OFFSET 978307200
41 extern int map_spotlight_to_rdf_query(slq_t *slq, gchar **rdf_result, gchar **fts_result);
42 extern slq_t *srp_slq;
55 %type <sval> match expr line function
62 %token OBRACE CBRACE EQUAL UNEQUAL GT LT COMMA QUOTE
74 srp_result = talloc_asprintf(srp_slq,
77 " <rdfq:startsWith>\n"
78 " <rdfq:Property name=\"File:Path\" />\n"
79 " <rdf:String>%s</rdf:String>\n"
80 " </rdfq:startsWith>\n"
83 "</rdfq:Condition>\n",
84 srp_slq->slq_vol->v_path, $1);
96 | match {$$ = $1; if ($$ == NULL) YYABORT;}
98 | OBRACE expr CBRACE {$$ = talloc_asprintf(srp_slq, "%s\n", $2);}
99 | expr AND expr {$$ = talloc_asprintf(srp_slq, "<rdfq:and>\n%s\n%s\n</rdfq:and>\n", $1, $3);}
101 if (strcmp($1, "") == 0 || strcmp($3, "") == 0) {
103 * The default Spotlight search term issued by the Finder (10.8) is:
104 * '* == "searchterm" || kMDItemTextContent == "searchterm"'
105 * As it isn't mappable to a single Tracker RDF query, we silently
106 * map this to just a filename search
108 if (strcmp($1, "") == 0)
109 $$ = talloc_asprintf(srp_slq, $3);
111 $$ = talloc_asprintf(srp_slq, $1);
112 talloc_free(srp_fts);
115 $$ = talloc_asprintf(srp_slq, "<rdfq:or>\n%s\n%s\n</rdfq:or>\n", $1, $3);
121 WORD EQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '=', $4);}
122 | WORD UNEQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '!', $4);}
123 | WORD LT QUOTE WORD QUOTE {$$ = map_expr($1, '<', $4);}
124 | WORD GT QUOTE WORD QUOTE {$$ = map_expr($1, '>', $4);}
125 | WORD EQUAL QUOTE WORD QUOTE WORD {$$ = map_expr($1, '=', $4);}
126 | WORD UNEQUAL QUOTE WORD QUOTE WORD {$$ = map_expr($1, '!', $4);}
127 | WORD LT QUOTE WORD QUOTE WORD {$$ = map_expr($1, '<', $4);}
128 | WORD GT QUOTE WORD QUOTE WORD {$$ = map_expr($1, '>', $4);}
132 FUNC_INRANGE OBRACE WORD COMMA date COMMA date CBRACE {$$ = map_daterange($3, $5, $7);}
136 DATE_ISO OBRACE WORD CBRACE {$$ = isodate2unix($3);}
137 | WORD {$$ = atoi($1) + SPRAW_TIME_OFFSET;}
142 static time_t isodate2unix(const char *s)
146 if (strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm) == NULL)
151 static const char *map_daterange(const char *dateattr, time_t date1, time_t date2)
155 struct spotlight_rdf_map *p;
157 char buf1[64], buf2[64];
159 EC_NULL_LOG( tmp = localtime(&date1) );
160 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
161 EC_NULL_LOG( tmp = localtime(&date2) );
162 strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
164 for (p = spotlight_rdf_map; p->srm_spotlight_attr; p++) {
165 if (strcmp(dateattr, p->srm_spotlight_attr) == 0) {
177 static const char *map_expr(const char *attr, char op, const char *val)
181 struct spotlight_rdf_map *p;
185 bstring q = NULL, search = NULL, replace = NULL;
188 for (p = spotlight_rdf_map; p->srm_spotlight_attr; p++) {
189 if (p->srm_rdf_attr && strcmp(p->srm_spotlight_attr, attr) == 0) {
190 switch (p->srm_type) {
200 q = bformat("^%s$", val);
201 search = bfromcstr("*");
202 replace = bfromcstr(".*");
203 bfindreplace(q, search, replace, 0);
204 result = talloc_asprintf(srp_slq,
206 " <rdfq:Property name=\"File:Name\" />\n"
207 " <rdf:String>%s</rdf:String>\n"
215 yyerror("only single fts query allowed");
219 search = bfromcstr("*");
220 replace = bfromcstr("");
221 bfindreplace(q, search, replace, 0);
222 srp_fts = talloc_strdup(srp_slq, bdata(q));
228 t = atoi(val) + SPRAW_TIME_OFFSET;
229 EC_NULL( tmp = localtime(&t) );
230 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
235 yyerror("unknown Spotlight attribute type");
252 void yyerror(const char *str)
255 printf("yyerror: %s\n", str);
257 LOG(log_error, logtype_sl, "yyerror: %s", str);
267 * Map a Spotlight RAW query string to a RDF query
269 * @param[in] slq Spotlight query handle
270 * @param[out] sparql_result Mapped RDF query, string is allocated in
271 * talloc context of slq
272 * @return 0 on success, -1 on error
274 int map_spotlight_to_rdf_query(slq_t *slq, gchar **rdf_result, gchar **fts_result)
277 YY_BUFFER_STATE s = NULL;
282 s = yy_scan_string(slq->slq_qstring);
284 EC_ZERO( yyparse() );
290 *rdf_result = srp_result;
291 *fts_result = srp_fts;
300 int main(int argc, char **argv)
306 printf("usage: %s QUERY\n", argv[0]);
310 srp_slq = talloc_zero(NULL, slq_t);
311 struct vol *vol = talloc_zero(srp_slq, struct vol);
312 vol->v_path = "/Volumes/test";
313 srp_slq->slq_vol = vol;
315 s = yy_scan_string(argv[1]);
322 printf("RDF:\n%s\nFTS: %s\n",
323 srp_result ? srp_result : "(empty)",
324 srp_fts ? srp_fts : "(none)");