XBPS Library API 20250705
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 char pkgname[XBPS_NAME_SIZE];
104 bool found = false;
105
106 assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
107 assert(str != NULL);
108
109 for (unsigned int i = 0; i < xbps_array_count(array); i++) {
110 xbps_object_t obj = xbps_array_get(array, i);
111 if (mode == 0) {
112 /* match by string */
113 if (xbps_string_equals_cstring(obj, str)) {
114 found = true;
115 break;
116 }
117 } else if (mode == 1) {
118 /* match by pkgname against pkgver */
119 const char *pkgdep = xbps_string_cstring_nocopy(obj);
120 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgdep))
121 break;
122 if (strcmp(pkgname, str) == 0) {
123 found = true;
124 break;
125 }
126 } else if (mode == 2) {
127 /* match by pkgver against pkgname */
128 const char *pkgdep = xbps_string_cstring_nocopy(obj);
129 if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, str))
130 break;
131 if (strcmp(pkgname, pkgdep) == 0) {
132 found = true;
133 break;
134 }
135 } else if (mode == 3) {
136 /* match pkgpattern against pkgdep */
137 const char *pkgdep = xbps_string_cstring_nocopy(obj);
138 if (xbps_pkgpattern_match(pkgdep, str)) {
139 found = true;
140 break;
141 }
142 } else if (mode == 4) {
143 /* match pkgdep against pkgpattern */
144 const char *pkgdep = xbps_string_cstring_nocopy(obj);
145 if (xbps_pkgpattern_match(str, pkgdep)) {
146 found = true;
147 break;
148 }
149 }
150 }
151
152 return found;
153}
154
155bool
156xbps_match_string_in_array(xbps_array_t array, const char *str)
157{
158 return match_string_in_array(array, str, 0);
159}
160
161bool
162xbps_match_pkgname_in_array(xbps_array_t array, const char *pkgname)
163{
164 return match_string_in_array(array, pkgname, 1);
165}
166
167bool
168xbps_match_pkgver_in_array(xbps_array_t array, const char *pkgver)
169{
170 return match_string_in_array(array, pkgver, 2);
171}
172
173bool
174xbps_match_pkgpattern_in_array(xbps_array_t array, const char *pattern)
175{
176 return match_string_in_array(array, pattern, 3);
177}
178
179bool
180xbps_match_pkgdep_in_array(xbps_array_t array, const char *pkgver)
181{
182 return match_string_in_array(array, pkgver, 4);
183}
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