2 #include <atalk/standards.h>
11 #include <atalk/talloc.h>
12 #include <atalk/logger.h>
13 #include <atalk/errchk.h>
15 #include "spotlight_SPARQL_map.h"
16 #include "spotlight.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;
40 #define SPRAW_TIME_OFFSET 978307200
41 extern int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result);
42 extern slq_t *ssp_slq;
55 %type <sval> match expr line function
62 %token OBRACE CBRACE EQUAL UNEQUAL GT LT COMMA QUOTE
74 ssp_result = talloc_asprintf(ssp_slq,
75 "SELECT DISTINCT ?url WHERE "
76 "{ ?x nie:url ?url FILTER(fn:starts-with(?url, 'file://%s/')) . %s}",
77 ssp_slq->slq_vol->v_path, $1);
90 if (strcmp($1, $3) != 0)
91 $$ = talloc_asprintf(ssp_slq, "{ %s } UNION { %s }", $1, $3);
93 $$ = talloc_asprintf(ssp_slq, "%s", $1);
95 | match {$$ = $1; if ($$ == NULL) YYABORT;}
97 | OBRACE expr CBRACE {$$ = talloc_asprintf(ssp_slq, "%s", $2);}
98 | expr AND expr {$$ = talloc_asprintf(ssp_slq, "%s . %s", $1, $3);}
100 if (strcmp($1, $3) != 0)
101 $$ = talloc_asprintf(ssp_slq, "{ %s } UNION { %s }", $1, $3);
103 $$ = talloc_asprintf(ssp_slq, "%s", $1);
108 WORD EQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '=', $4);}
109 | WORD UNEQUAL QUOTE WORD QUOTE {$$ = map_expr($1, '!', $4);}
110 | WORD LT QUOTE WORD QUOTE {$$ = map_expr($1, '<', $4);}
111 | WORD GT QUOTE WORD QUOTE {$$ = map_expr($1, '>', $4);}
115 FUNC_INRANGE OBRACE WORD COMMA date COMMA date CBRACE {$$ = map_daterange($3, $5, $7);}
119 DATE_ISO OBRACE WORD CBRACE {$$ = isodate2unix($3);}
120 | WORD {$$ = atoi($1) + SPRAW_TIME_OFFSET;}
125 static time_t isodate2unix(const char *s)
129 if (strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm) == NULL)
134 const char *map_daterange(const char *dateattr, time_t date1, time_t date2)
138 struct spotlight_sparql_map *p;
140 char buf1[64], buf2[64];
142 EC_NULL( tmp = localtime(&date1) );
143 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
144 EC_NULL( tmp = localtime(&date2) );
145 strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
147 for (p = spotlight_sparql_map; p->ssm_spotlight_attr; p++) {
148 if (strcmp(dateattr, p->ssm_spotlight_attr) == 0) {
149 result = talloc_asprintf(ssp_slq,
150 "?x %s ?d FILTER (?d > '%s' && ?d < '%s')",
163 const char *map_expr(const char *attr, char op, const char *val)
167 struct spotlight_sparql_map *p;
172 for (p = spotlight_sparql_map; p->ssm_spotlight_attr; p++) {
173 if (strcmp(p->ssm_spotlight_attr, attr) == 0) {
174 switch (p->ssm_type) {
176 result = talloc_asprintf(ssp_slq, "?x %s '%s'", p->ssm_sparql_attr, val);
179 result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(?y %c '%s')", p->ssm_sparql_attr, op, val);
182 result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(regex(?y, '%s'))", p->ssm_sparql_attr, val);
185 result = talloc_asprintf(ssp_slq, "?x %s '%s'", p->ssm_sparql_attr, val);
188 t = atoi(val) + SPRAW_TIME_OFFSET;
189 EC_NULL( tmp = localtime(&t) );
190 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
191 result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(?y %c '%s')", p->ssm_sparql_attr, op, buf1);
194 yyerror("unknown Spotlight attribute type");
205 void yyerror(const char *str)
208 printf("yyerror: %s\n", str);
210 LOG(log_error, logtype_sl, "yyerror: %s", str);
220 * Map a Spotlight RAW query string to a SPARQL query string
222 * @param[in] slq Spotlight query handle
223 * @param[out] sparql_result Mapped SPARQL query, string is allocated in
224 * talloc context of slq
225 * @return 0 on success, -1 on error
227 int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result)
230 YY_BUFFER_STATE s = NULL;
234 s = yy_scan_string(slq->slq_qstring);
236 EC_ZERO( yyparse() );
242 *sparql_result = ssp_result;
244 *sparql_result = NULL;
249 int main(int argc, char **argv)
255 printf("usage: %s QUERY\n", argv[0]);
259 ssp_slq = talloc_zero(NULL, slq_t);
260 struct vol *vol = talloc_zero(ssp_slq, struct vol);
261 vol->v_path = "/Volumes/test";
262 ssp_slq->slq_vol = vol;
264 s = yy_scan_string(argv[1]);
271 printf("SPARQL: %s\n", ssp_result ? ssp_result : "(empty)");