]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight_rawquery_parser.c
Add lots of mappings and dont ignore case stuff
[netatalk.git] / etc / afpd / spotlight_rawquery_parser.c
index ac9a521af478ba56f071c5fda4513ca142fe812e..7a70abc96fb287b11d86f67814f1fafe3992b707 100644 (file)
 
   /* local vars */
   static gchar *ssp_result;
-
+  static char sparqlvar;
 
 
 /* Line 268 of yacc.c  */
@@ -427,16 +427,16 @@ union yyalloc
 /* YYFINAL -- State number of the termination state.  */
 #define YYFINAL  2
 /* YYLAST -- Last index in YYTABLE.  */
-#define YYLAST   56
+#define YYLAST   52
 
 /* YYNTOKENS -- Number of terminals.  */
 #define YYNTOKENS  17
 /* YYNNTS -- Number of nonterminals.  */
 #define YYNNTS  7
 /* YYNRULES -- Number of rules.  */
-#define YYNRULES  18
+#define YYNRULES  22
 /* YYNRULES -- Number of states.  */
-#define YYNSTATES  47
+#define YYNSTATES  51
 
 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.  */
 #define YYUNDEFTOK  2
@@ -484,7 +484,8 @@ static const yytype_uint8 yytranslate[] =
 static const yytype_uint8 yyprhs[] =
 {
        0,     0,     3,     4,     7,     9,    11,    15,    17,    19,
-      23,    27,    31,    37,    43,    49,    55,    64,    69
+      23,    27,    31,    37,    43,    49,    55,    62,    69,    76,
+      83,    92,    97
 };
 
 /* YYRHS -- A `-1'-separated list of the rules' RHS.  */
@@ -495,16 +496,19 @@ static const yytype_int8 yyrhs[] =
       20,     8,    -1,    20,    15,    20,    -1,    20,    16,    20,
       -1,     3,     9,    14,     3,    14,    -1,     3,    10,    14,
        3,    14,    -1,     3,    12,    14,     3,    14,    -1,     3,
-      11,    14,     3,    14,    -1,     5,     7,     3,    13,    23,
-      13,    23,     8,    -1,     6,     7,     3,     8,    -1,     3,
-      -1
+      11,    14,     3,    14,    -1,     3,     9,    14,     3,    14,
+       3,    -1,     3,    10,    14,     3,    14,     3,    -1,     3,
+      12,    14,     3,    14,     3,    -1,     3,    11,    14,     3,
+      14,     3,    -1,     5,     7,     3,    13,    23,    13,    23,
+       8,    -1,     6,     7,     3,     8,    -1,     3,    -1
 };
 
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
 static const yytype_uint8 yyrline[] =
 {
        0,    67,    67,    69,    73,    83,    89,    95,    96,    97,
-      98,    99,   108,   109,   110,   111,   115,   119,   120
+      98,    99,   108,   109,   110,   111,   112,   113,   114,   115,
+     119,   123,   124
 };
 #endif
 
@@ -534,14 +538,16 @@ static const yytype_uint16 yytoknum[] =
 static const yytype_uint8 yyr1[] =
 {
        0,    17,    18,    18,    19,    20,    20,    20,    20,    20,
-      20,    20,    21,    21,    21,    21,    22,    23,    23
+      20,    20,    21,    21,    21,    21,    21,    21,    21,    21,
+      22,    23,    23
 };
 
 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN.  */
 static const yytype_uint8 yyr2[] =
 {
        0,     2,     0,     2,     1,     1,     3,     1,     1,     3,
-       3,     3,     5,     5,     5,     5,     8,     4,     1
+       3,     3,     5,     5,     5,     5,     6,     6,     6,     6,
+       8,     4,     1
 };
 
 /* YYDEFACT[STATE-NAME] -- Default reduction number in state STATE-NUM.
@@ -552,8 +558,9 @@ static const yytype_uint8 yydefact[] =
        2,     0,     1,     0,     5,     0,     0,     3,     4,     7,
        8,     0,     0,     0,     0,     0,     0,     0,     0,     0,
        0,     0,     0,     0,     0,     9,    10,    11,     6,     0,
-       0,     0,     0,     0,    12,    13,    15,    14,    18,     0,
-       0,     0,     0,     0,     0,    17,    16
+       0,     0,     0,     0,    12,    13,    15,    14,    22,     0,
+       0,    16,    17,    19,    18,     0,     0,     0,     0,    21,
+      20
 };
 
 /* YYDEFGOTO[NTERM-NUM].  */
@@ -570,14 +577,15 @@ static const yytype_int8 yypact[] =
      -10,    10,   -10,     9,   -10,    -2,    -1,   -10,     8,    -9,
      -10,     2,    12,    13,    14,    26,    -7,    -1,    -1,    27,
       28,    29,    30,    31,    22,   -10,    20,   -10,   -10,    23,
-      24,    25,    32,    19,   -10,   -10,   -10,   -10,   -10,    33,
-      34,    38,    19,    35,    36,   -10,   -10
+      24,    25,    32,    19,    37,    38,    39,    40,   -10,    41,
+      34,   -10,   -10,   -10,   -10,    42,    19,    36,    43,   -10,
+     -10
 };
 
 /* YYPGOTO[NTERM-NUM].  */
 static const yytype_int8 yypgoto[] =
 {
-     -10,   -10,   -10,    -6,    37,   -10,     0
+     -10,   -10,   -10,    -6,    33,   -10,     3
 };
 
 /* YYTABLE[YYPACT[STATE-NUM]].  What to do in state STATE-NUM.  If
@@ -590,8 +598,8 @@ static const yytype_uint8 yytable[] =
        2,    26,    27,     3,     4,     5,    20,     6,    11,    12,
       13,    14,    38,    17,    18,    39,    21,    22,    23,    24,
        3,    29,    30,    31,    32,    33,    18,    34,    35,    36,
-      41,    43,    44,    45,    46,     0,    37,    42,     0,     0,
-       0,     0,     0,     0,     0,     0,    28
+      41,    42,    43,    44,    49,    47,    37,    46,    45,    48,
+       0,    50,    28
 };
 
 #define yypact_value_is_default(yystate) \
@@ -606,8 +614,8 @@ static const yytype_int8 yycheck[] =
        0,    17,    18,     3,     4,     5,    14,     7,     9,    10,
       11,    12,     3,    15,    16,     6,    14,    14,    14,     3,
        3,     3,     3,     3,     3,    13,    16,    14,    14,    14,
-       7,     3,    42,     8,     8,    -1,    14,    13,    -1,    -1,
-      -1,    -1,    -1,    -1,    -1,    -1,    19
+       3,     3,     3,     3,     8,     3,    14,    13,     7,    46,
+      -1,     8,    19
 };
 
 /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
@@ -618,7 +626,8 @@ static const yytype_uint8 yystos[] =
       22,     9,    10,    11,    12,     7,    20,    15,    16,    16,
       14,    14,    14,    14,     3,     8,    20,    20,    21,     3,
        3,     3,     3,    13,    14,    14,    14,    14,     3,     6,
-      23,     7,    13,     3,    23,     8,     8
+      23,     3,     3,     3,     3,     7,    13,     3,    23,     8,
+       8
 };
 
 #define yyerrok                (yyerrstatus = 0)
@@ -1459,7 +1468,7 @@ yyreduce:
     {
     ssp_result = talloc_asprintf(ssp_slq,
                                  "SELECT DISTINCT ?url WHERE "
-                                 "{ ?x nie:url ?url FILTER(fn:starts-with(?url, 'file://%s/')) . %s}",
+                                 "{ ?obj nie:url ?url FILTER(fn:starts-with(?url, 'file://%s/')) . %s}",
                                  ssp_slq->slq_vol->v_path, (yyvsp[(1) - (1)].sval));
     (yyval.sval) = ssp_result;
 }
@@ -1560,28 +1569,56 @@ yyreduce:
   case 16:
 
 /* Line 1806 of yacc.c  */
-#line 115 "spotlight_rawquery_parser.y"
-    {(yyval.sval) = map_daterange((yyvsp[(3) - (8)].sval), (yyvsp[(5) - (8)].tval), (yyvsp[(7) - (8)].tval));}
+#line 112 "spotlight_rawquery_parser.y"
+    {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '=', (yyvsp[(4) - (6)].sval));}
     break;
 
   case 17:
 
+/* Line 1806 of yacc.c  */
+#line 113 "spotlight_rawquery_parser.y"
+    {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '!', (yyvsp[(4) - (6)].sval));}
+    break;
+
+  case 18:
+
+/* Line 1806 of yacc.c  */
+#line 114 "spotlight_rawquery_parser.y"
+    {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '<', (yyvsp[(4) - (6)].sval));}
+    break;
+
+  case 19:
+
+/* Line 1806 of yacc.c  */
+#line 115 "spotlight_rawquery_parser.y"
+    {(yyval.sval) = map_expr((yyvsp[(1) - (6)].sval), '>', (yyvsp[(4) - (6)].sval));}
+    break;
+
+  case 20:
+
 /* Line 1806 of yacc.c  */
 #line 119 "spotlight_rawquery_parser.y"
+    {(yyval.sval) = map_daterange((yyvsp[(3) - (8)].sval), (yyvsp[(5) - (8)].tval), (yyvsp[(7) - (8)].tval));}
+    break;
+
+  case 21:
+
+/* Line 1806 of yacc.c  */
+#line 123 "spotlight_rawquery_parser.y"
     {(yyval.tval) = isodate2unix((yyvsp[(3) - (4)].sval));}
     break;
 
-  case 18:
+  case 22:
 
 /* Line 1806 of yacc.c  */
-#line 120 "spotlight_rawquery_parser.y"
+#line 124 "spotlight_rawquery_parser.y"
     {(yyval.tval) = atoi((yyvsp[(1) - (1)].sval)) + SPRAW_TIME_OFFSET;}
     break;
 
 
 
 /* Line 1806 of yacc.c  */
-#line 1585 "spotlight_rawquery_parser.c"
+#line 1622 "spotlight_rawquery_parser.c"
       default: break;
     }
   /* User semantic actions sometimes alter yychar, and that requires
@@ -1812,7 +1849,7 @@ yyreturn:
 
 
 /* Line 2067 of yacc.c  */
-#line 123 "spotlight_rawquery_parser.y"
+#line 127 "spotlight_rawquery_parser.y"
 
 
 static time_t isodate2unix(const char *s)
@@ -1832,18 +1869,23 @@ const char *map_daterange(const char *dateattr, time_t date1, time_t date2)
     struct tm *tmp;
     char buf1[64], buf2[64];
 
-    EC_NULL( tmp = localtime(&date1) );
+    EC_NULL_LOG( tmp = localtime(&date1) );
     strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
-    EC_NULL( tmp = localtime(&date2) );
+    EC_NULL_LOG( tmp = localtime(&date2) );
     strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
 
     for (p = spotlight_sparql_map; p->ssm_spotlight_attr; p++) {
         if (strcmp(dateattr, p->ssm_spotlight_attr) == 0) {
             result = talloc_asprintf(ssp_slq,
-                                     "?x %s ?d FILTER (?d > '%s' && ?d < '%s')",
+                                     "?obj %s ?%c FILTER (?%c > '%s' && ?%c < '%s')",
                                      p->ssm_sparql_attr,
+                                     sparqlvar,
+                                     sparqlvar,
                                      buf1,
+                                     sparqlvar,
                                      buf2);
+            sparqlvar++;
+            break;
         }
     }
 
@@ -1861,27 +1903,50 @@ const char *map_expr(const char *attr, char op, const char *val)
     time_t t;
     struct tm *tmp;
     char buf1[64];
+    bstring q = NULL, search = NULL, replace = NULL;
 
     for (p = spotlight_sparql_map; p->ssm_spotlight_attr; p++) {
         if (strcmp(p->ssm_spotlight_attr, attr) == 0) {
             switch (p->ssm_type) {
             case ssmt_bool:
-                result = talloc_asprintf(ssp_slq, "?x %s '%s'", p->ssm_sparql_attr, val);
+                result = talloc_asprintf(ssp_slq, "?obj %s '%s'", p->ssm_sparql_attr, val);
                 break;
             case ssmt_num:
-                result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(?y %c '%s')", p->ssm_sparql_attr, op, val);
+                result = talloc_asprintf(ssp_slq, "?obj %s ?%c FILTER(?%c %c%c '%s')",
+                                         p->ssm_sparql_attr,
+                                         sparqlvar,
+                                         sparqlvar,
+                                         op,
+                                         op == '!' ? '=' : ' ', /* append '=' to '!' */
+                                         val);
+                sparqlvar++;
                 break;
             case ssmt_str:
-                result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(regex(?y, '%s'))", p->ssm_sparql_attr, val);
+                q = bformat("^%s$", val);
+                search = bfromcstr("*");
+                replace = bfromcstr(".*");
+                bfindreplace(q, search, replace, 0);
+                result = talloc_asprintf(ssp_slq, "?obj %s ?%c FILTER(regex(?%c, '%s'))",
+                                         p->ssm_sparql_attr,
+                                         sparqlvar,
+                                         sparqlvar,
+                                         bdata(q));
+                sparqlvar++;
                 break;
             case ssmt_fts:
-                result = talloc_asprintf(ssp_slq, "?x %s '%s'", p->ssm_sparql_attr, val);
+                result = talloc_asprintf(ssp_slq, "?obj %s '%s'", p->ssm_sparql_attr, val);
                 break;
             case ssmt_date:
                 t = atoi(val) + SPRAW_TIME_OFFSET;
                 EC_NULL( tmp = localtime(&t) );
                 strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
-                result = talloc_asprintf(ssp_slq, "?x %s ?y FILTER(?y %c '%s')", p->ssm_sparql_attr, op, buf1);
+                result = talloc_asprintf(ssp_slq, "?obj %s ?%c FILTER(?%c %c '%s')",
+                                         p->ssm_sparql_attr,
+                                         sparqlvar,
+                                         sparqlvar,
+                                         op,
+                                         buf1);
+                sparqlvar++;
                 break;
             default:
                 yyerror("unknown Spotlight attribute type");
@@ -1892,6 +1957,12 @@ const char *map_expr(const char *attr, char op, const char *val)
     }
 
 EC_CLEANUP:
+    if (q)
+        bdestroy(q);
+    if (search)
+        bdestroy(search);
+    if (replace)
+        bdestroy(replace);
     return result;
 }
 
@@ -1925,6 +1996,7 @@ int map_spotlight_to_sparql_query(slq_t *slq, gchar **sparql_result)
 
     ssp_slq = slq;
     s = yy_scan_string(slq->slq_qstring);
+    sparqlvar = 'a';
 
     EC_ZERO( yyparse() );
 
@@ -1953,6 +2025,7 @@ int main(int argc, char **argv)
     struct vol *vol = talloc_zero(ssp_slq, struct vol);
     vol->v_path = "/Volumes/test";
     ssp_slq->slq_vol = vol;
+    sparqlvar = 'a';
 
     s = yy_scan_string(argv[1]);