XBPS Library API 20240111
The X Binary Package System
plist_match.c
1/*-
2 * Copyright (c) 2008-2015 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 */
42bool
43xbps_match_virtual_pkg_in_array(xbps_array_t a, const char *str)
44{
45 if (xbps_pkgpattern_version(str)) {
46 if (xbps_match_pkgdep_in_array(a, str) ||
48 return true;
49 } else if (xbps_pkg_version(str)) {
50 return xbps_match_string_in_array(a, str);
51 } else {
52 return xbps_match_pkgname_in_array(a, str);
53 }
54 return false;
55}
56
57bool
58xbps_match_virtual_pkg_in_dict(xbps_dictionary_t d, const char *str)
59{
60 xbps_array_t provides;
61
62 assert(xbps_object_type(d) == XBPS_TYPE_DICTIONARY);
63
64 if ((provides = xbps_dictionary_get(d, "provides")))
65 return xbps_match_virtual_pkg_in_array(provides, str);
66
67 return false;
68}
69
70bool
72 xbps_array_t provides)
73{
74 xbps_object_t obj, obj2;
75 xbps_object_iterator_t iter, iter2;
76 const char *vpkgver, *pkgpattern;
77
78 iter = xbps_array_iterator(provides);
79 assert(iter);
80
81 while ((obj = xbps_object_iterator_next(iter))) {
82 vpkgver = xbps_string_cstring_nocopy(obj);
83 iter2 = xbps_array_iterator(rundeps);
84 assert(iter2);
85 while ((obj2 = xbps_object_iterator_next(iter2))) {
86 pkgpattern = xbps_string_cstring_nocopy(obj2);
87 if (xbps_pkgpattern_match(vpkgver, pkgpattern)) {
88 xbps_object_iterator_release(iter2);
89 xbps_object_iterator_release(iter);
90 return true;
91 }
92 }
93 xbps_object_iterator_release(iter2);
94 }
95 xbps_object_iterator_release(iter);
96
97 return false;
98}
99
100static bool
101match_string_in_array(xbps_array_t array, const char *str, int mode)
102{
103 xbps_object_iterator_t iter;
104 xbps_object_t obj;
105 const char *pkgdep;
106 char pkgname[XBPS_NAME_SIZE];
107 bool found = false;
108
109 assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
110 assert(str != NULL);
111
112 iter = xbps_array_iterator(array);
113 assert(iter);
114
115 while ((obj = xbps_object_iterator_next(iter))) {
116 if (mode == 0) {
117 /* match by string */
118 if (xbps_string_equals_cstring(obj, str)) {
119 found = true;
120 break;
121 }
122 } else if (mode == 1) {
123 /* match by pkgname against pkgver */
124 pkgdep = xbps_string_cstring_nocopy(obj);
125 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgdep))
126 break;
127 if (strcmp(pkgname, str) == 0) {
128 found = true;
129 break;
130 }
131 } else if (mode == 2) {
132 /* match by pkgver against pkgname */
133 pkgdep = xbps_string_cstring_nocopy(obj);
134 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, str))
135 break;
136 if (strcmp(pkgname, pkgdep) == 0) {
137 found = true;
138 break;
139 }
140 } else if (mode == 3) {
141 /* match pkgpattern against pkgdep */
142 pkgdep = xbps_string_cstring_nocopy(obj);
143 if (xbps_pkgpattern_match(pkgdep, str)) {
144 found = true;
145 break;
146 }
147 } else if (mode == 4) {
148 /* match pkgdep against pkgpattern */
149 pkgdep = xbps_string_cstring_nocopy(obj);
150 if (xbps_pkgpattern_match(str, pkgdep)) {
151 found = true;
152 break;
153 }
154 }
155 }
156 xbps_object_iterator_release(iter);
157
158 return found;
159}
160
161bool
162xbps_match_string_in_array(xbps_array_t array, const char *str)
163{
164 return match_string_in_array(array, str, 0);
165}
166
167bool
168xbps_match_pkgname_in_array(xbps_array_t array, const char *pkgname)
169{
170 return match_string_in_array(array, pkgname, 1);
171}
172
173bool
174xbps_match_pkgver_in_array(xbps_array_t array, const char *pkgver)
175{
176 return match_string_in_array(array, pkgver, 2);
177}
178
179bool
180xbps_match_pkgpattern_in_array(xbps_array_t array, const char *pattern)
181{
182 return match_string_in_array(array, pattern, 3);
183}
184
185bool
186xbps_match_pkgdep_in_array(xbps_array_t array, const char *pkgver)
187{
188 return match_string_in_array(array, pkgver, 4);
189}
bool xbps_match_pkgver_in_array(xbps_array_t array, const char *pkgver)
bool xbps_match_pkgname_in_array(xbps_array_t array, const char *pkgname)
bool xbps_match_pkgdep_in_array(xbps_array_t array, const char *pkgver)
bool xbps_match_string_in_array(xbps_array_t array, const char *str)
bool xbps_match_virtual_pkg_in_dict(xbps_dictionary_t d, const char *str)
Definition plist_match.c:58
bool xbps_match_virtual_pkg_in_array(xbps_array_t a, const char *str)
Definition plist_match.c:43
bool xbps_match_any_virtualpkg_in_rundeps(xbps_array_t rundeps, xbps_array_t provides)
Definition plist_match.c:71
bool xbps_match_pkgpattern_in_array(xbps_array_t array, const char *pattern)
const char * xbps_pkgpattern_version(const char *pattern)
Definition util.c:317
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:530
const char * xbps_pkg_version(const char *pkg)
Definition util.c:124