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 *ssp_result;
39 #define SPRAW_TIME_OFFSET 978307200
40 extern int map_spotlight_to_rdf_query(slq_t *slq, gchar **sparql_result);
41 extern slq_t *srp_slq;
54 %type <sval> match expr line function
61 %token OBRACE CBRACE EQUAL UNEQUAL GT LT COMMA QUOTE
73 ssp_result = talloc_asprintf(srp_slq,
77 " <rdfq:Property name=\"File:Path\" />"
78 " <rdf:String>%s</rdf:String>"
83 srp_slq->slq_vol->v_path, $1);
96 if (strcmp($1, $3) != 0)
97 $$ = talloc_asprintf(srp_slq, "{ %s } UNION { %s }", $1, $3);
99 $$ = talloc_asprintf(srp_slq, "%s", $1);
101 | match {$$ = $1; if ($$ == NULL) YYABORT;}
102 | function {$$ = $1;}
103 | OBRACE expr CBRACE {$$ = talloc_asprintf(srp_slq, "%s", $2);}
104 | expr AND expr {$$ = talloc_asprintf(srp_slq, "%s . %s", $1, $3);}
106 if (strcmp($1, $3) != 0)
107 $$ = talloc_asprintf(srp_slq, "{ %s } UNION { %s }", $1, $3);
109 $$ = talloc_asprintf(srp_slq, "%s", $1);
114 WORD EQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '=', $4);}
115 | WORD UNEQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '!', $4);}
116 | WORD LT QUOTE WORD QUOTE {$$ = map_expr($1, '<', $4);}
117 | WORD GT QUOTE WORD QUOTE {$$ = map_expr($1, '>', $4);}
118 | WORD EQUAL QUOTE WORD QUOTE WORD {$$ = map_expr($1, '=', $4);}
119 | WORD UNEQUAL QUOTE WORD QUOTE WORD {$$ = map_expr($1, '!', $4);}
120 | WORD LT QUOTE WORD QUOTE WORD {$$ = map_expr($1, '<', $4);}
121 | WORD GT QUOTE WORD QUOTE WORD {$$ = map_expr($1, '>', $4);}
125 FUNC_INRANGE OBRACE WORD COMMA date COMMA date CBRACE {$$ = map_daterange($3, $5, $7);}
129 DATE_ISO OBRACE WORD CBRACE {$$ = isodate2unix($3);}
130 | WORD {$$ = atoi($1) + SPRAW_TIME_OFFSET;}
135 static time_t isodate2unix(const char *s)
139 if (strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm) == NULL)
144 const char *map_daterange(const char *dateattr, time_t date1, time_t date2)
148 struct spotlight_rdf_map *p;
150 char buf1[64], buf2[64];
152 EC_NULL_LOG( tmp = localtime(&date1) );
153 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
154 EC_NULL_LOG( tmp = localtime(&date2) );
155 strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
157 for (p = spotlight_rdf_map; p->srm_spotlight_attr; p++) {
158 if (strcmp(dateattr, p->srm_spotlight_attr) == 0) {
170 const char *map_expr(const char *attr, char op, const char *val)
174 struct spotlight_rdf_map *p;
178 bstring q = NULL, search = NULL, replace = NULL;
180 for (p = spotlight_rdf_map; p->srm_spotlight_attr; p++) {
181 if (p->srm_rdf_attr && strcmp(p->srm_spotlight_attr, attr) == 0) {
182 switch (p->srm_type) {
190 q = bformat("^%s$", val);
191 search = bfromcstr("*");
192 replace = bfromcstr(".*");
193 bfindreplace(q, search, replace, 0);
200 t = atoi(val) + SPRAW_TIME_OFFSET;
201 EC_NULL( tmp = localtime(&t) );
202 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
206 yyerror("unknown Spotlight attribute type");
223 void yyerror(const char *str)
226 printf("yyerror: %s\n", str);
228 LOG(log_error, logtype_sl, "yyerror: %s", str);
238 * Map a Spotlight RAW query string to a SPARQL query string
240 * @param[in] slq Spotlight query handle
241 * @param[out] sparql_result Mapped SPARQL query, string is allocated in
242 * talloc context of slq
243 * @return 0 on success, -1 on error
245 int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result)
248 YY_BUFFER_STATE s = NULL;
252 s = yy_scan_string(slq->slq_qstring);
254 EC_ZERO( yyparse() );
260 *sparql_result = ssp_result;
262 *sparql_result = NULL;
267 int main(int argc, char **argv)
273 printf("usage: %s QUERY\n", argv[0]);
277 srp_slq = talloc_zero(NULL, slq_t);
278 struct vol *vol = talloc_zero(srp_slq, struct vol);
279 vol->v_path = "/Volumes/test";
280 srp_slq->slq_vol = vol;
282 s = yy_scan_string(argv[1]);
289 printf("SPARQL: %s\n", ssp_result ? ssp_result : "(empty)");