XBPS Library API  0.19
The X Binary Package System
plist_match.c
1 /*-
2  * Copyright (c) 2008-2012 Juan Romero Pardines.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include "xbps_api_impl.h"
33 
34 /**
35  * @file lib/plist_find.c
36  * @brief PropertyList generic routines
37  * @defgroup plist PropertyList generic functions
38  *
39  * These functions manipulate plist files and objects shared by almost
40  * all library functions.
41  */
42 bool
43 xbps_match_virtual_pkg_in_dict(prop_dictionary_t d,
44  const char *str,
45  bool bypattern)
46 {
47  prop_array_t provides;
48  bool found = false;
49 
50  assert(prop_object_type(d) == PROP_TYPE_DICTIONARY);
51 
52  if ((provides = prop_dictionary_get(d, "provides"))) {
53  if (bypattern)
54  found = xbps_match_pkgpattern_in_array(provides, str);
55  else
56  found = xbps_match_pkgname_in_array(provides, str);
57  }
58  return found;
59 }
60 
61 bool
63  prop_array_t provides)
64 {
65  prop_object_t obj, obj2;
66  prop_object_iterator_t iter, iter2;
67  const char *vpkgver, *pkgpattern;
68  char *tmp;
69 
70  iter = prop_array_iterator(provides);
71  assert(iter);
72 
73  while ((obj = prop_object_iterator_next(iter))) {
74  tmp = NULL;
75  vpkgver = prop_string_cstring_nocopy(obj);
76  if (strchr(vpkgver, '_') == NULL) {
77  tmp = xbps_xasprintf("%s_1", vpkgver);
78  vpkgver = tmp;
79  }
80  iter2 = prop_array_iterator(rundeps);
81  assert(iter2);
82  while ((obj2 = prop_object_iterator_next(iter2))) {
83  pkgpattern = prop_string_cstring_nocopy(obj2);
84  if (xbps_pkgpattern_match(vpkgver, pkgpattern)) {
85  if (tmp != NULL)
86  free(tmp);
87 
88  prop_object_iterator_release(iter2);
89  prop_object_iterator_release(iter);
90  return true;
91  }
92  }
93  prop_object_iterator_release(iter2);
94  if (tmp != NULL)
95  free(tmp);
96  }
97  prop_object_iterator_release(iter);
98 
99  return false;
100 }
101 
102 static bool
103 match_string_in_array(prop_array_t array, const char *str, int mode)
104 {
105  prop_object_iterator_t iter;
106  prop_object_t obj;
107  const char *pkgdep;
108  char *curpkgname, *tmp;
109  bool found = false;
110 
111  assert(prop_object_type(array) == PROP_TYPE_ARRAY);
112  assert(str != NULL);
113 
114  iter = prop_array_iterator(array);
115  assert(iter);
116 
117  while ((obj = prop_object_iterator_next(iter))) {
118  tmp = NULL;
119  if (mode == 0) {
120  /* match by string */
121  if (prop_string_equals_cstring(obj, str)) {
122  found = true;
123  break;
124  }
125  } else if (mode == 1) {
126  /* match by pkgname */
127  pkgdep = prop_string_cstring_nocopy(obj);
128  if (strchr(pkgdep, '_') == NULL) {
129  tmp = xbps_xasprintf("%s_1", pkgdep);
130  curpkgname = xbps_pkg_name(tmp);
131  free(tmp);
132  } else {
133  curpkgname = xbps_pkg_name(pkgdep);
134  }
135  if (curpkgname == NULL)
136  break;
137  if (strcmp(curpkgname, str) == 0) {
138  free(curpkgname);
139  found = true;
140  break;
141  }
142  free(curpkgname);
143  } else if (mode == 2) {
144  /* match pkgpattern against pkgdep */
145  pkgdep = prop_string_cstring_nocopy(obj);
146  if (strchr(pkgdep, '_') == NULL) {
147  tmp = xbps_xasprintf("%s_1", pkgdep);
148  pkgdep = tmp;
149  }
150  if (xbps_pkgpattern_match(pkgdep, str)) {
151  if (tmp != NULL)
152  free(tmp);
153  found = true;
154  break;
155  }
156  if (tmp != NULL)
157  free(tmp);
158 
159  } else if (mode == 3) {
160  /* match pkgdep against pkgpattern */
161  pkgdep = prop_string_cstring_nocopy(obj);
162  if (strchr(pkgdep, '_') == NULL) {
163  tmp = xbps_xasprintf("%s_1", pkgdep);
164  pkgdep = tmp;
165  }
166  if (xbps_pkgpattern_match(str, pkgdep)) {
167  if (tmp != NULL)
168  free(tmp);
169  found = true;
170  break;
171  }
172  if (tmp != NULL)
173  free(tmp);
174  }
175  }
176  prop_object_iterator_release(iter);
177 
178  return found;
179 }
180 
181 bool
182 xbps_match_string_in_array(prop_array_t array, const char *str)
183 {
184  return match_string_in_array(array, str, 0);
185 }
186 
187 bool
188 xbps_match_pkgname_in_array(prop_array_t array, const char *pkgname)
189 {
190  return match_string_in_array(array, pkgname, 1);
191 }
192 
193 bool
194 xbps_match_pkgpattern_in_array(prop_array_t array, const char *pattern)
195 {
196  return match_string_in_array(array, pattern, 2);
197 }
198 
199 bool
200 xbps_match_pkgdep_in_array(prop_array_t array, const char *pkgver)
201 {
202  return match_string_in_array(array, pkgver, 3);
203 }