1 /* $NetBSD: license.c,v 1.4 2013/04/20 15:29:23 wiz Exp $ */
4 * Copyright (c) 2009 Joerg Sonnenberger <joerg@NetBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 const char *default_acceptable_licenses
=
49 "apache-1.1 apache-2.0 "
51 "artistic artistic-2.0 "
57 "gnu-fdl-v1.1 gnu-fdl-v1.2 gnu-fdl-v1.3 "
59 "gnu-gpl-v2 gnu-lgpl-v2 gnu-lgpl-v2.1 "
60 "gnu-gpl-v3 gnu-lgpl-v3 "
61 "ibm-public-license-1.0 "
68 "mpl-1.0 mpl-1.1 mpl-2.0 "
71 "original-bsd modified-bsd 2-clause-bsd "
76 "python-software-foundation "
85 static size_t hash_collisions
;
88 static char **license_hash
[HASH_SIZE
];
89 static const char license_spaces
[] = " \t\n";
90 static const char license_chars
[] =
91 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.";
94 hash_license(const char *license
, size_t len
)
98 for (hash
= 0; *license
&& len
; ++license
, --len
)
99 hash
= *license
+ hash
* 32;
100 return hash
% HASH_SIZE
;
104 add_license_internal(const char *license
, size_t len
)
109 slot
= hash_license(license
, len
);
111 new_license
= malloc(len
+ 1);
112 memcpy(new_license
, license
, len
);
113 new_license
[len
] = '\0';
115 if (license_hash
[slot
] == NULL
) {
116 license_hash
[slot
] = calloc(sizeof(char *), 2);
117 license_hash
[slot
][0] = new_license
;
119 for (i
= 0; license_hash
[slot
][i
]; ++i
) {
120 if (!memcmp(license_hash
[slot
][i
], license
, len
) &&
121 license_hash
[slot
][i
][len
] == '\0') {
131 license_hash
[slot
] = realloc(license_hash
[slot
],
132 sizeof(char *) * (i
+ 2));
133 license_hash
[slot
][i
] = new_license
;
134 license_hash
[slot
][i
+ 1] = NULL
;
139 add_licenses(const char *line
)
146 for (line
+= strspn(line
, license_spaces
); line
; ) {
147 next
= line
+ strspn(line
, license_chars
);
149 return *line
? -1 : 0;
150 add_license_internal(line
, next
- line
);
151 line
= next
+ strspn(next
, license_spaces
);
153 return *line
? -1 : 0;
159 acceptable_license_internal(const char *license
, size_t len
)
163 slot
= hash_license(license
, len
);
165 if (license_hash
[slot
] == NULL
)
168 for (i
= 0; license_hash
[slot
][i
]; ++i
) {
169 if (strncmp(license_hash
[slot
][i
], license
, len
) == 0 &&
170 license_hash
[slot
][i
][len
] == '\0')
178 acceptable_license(const char *license
)
182 len
= strlen(license
);
183 if (strspn(license
, license_chars
) != len
) {
184 warnx("Invalid character in license name at position %" PRIzu
, len
);
188 return acceptable_license_internal(license
, len
);
192 acceptable_pkg_license_internal(const char **licensep
, int toplevel
, const char *start
)
194 const char *license
= *licensep
;
195 int need_parenthesis
, is_true
= 0;
196 int expr_type
= 0; /* 0: unset, 1: or, 2: and */
199 license
+= strspn(license
, license_spaces
);
201 if (*license
== '(' && !toplevel
) {
202 need_parenthesis
= 1;
204 license
+= strspn(license
, license_spaces
);
206 need_parenthesis
= 0;
210 if (*license
== '(') {
211 switch (acceptable_pkg_license_internal(&license
, 0, start
)) {
222 license
+= strspn(license
, license_spaces
);
224 len
= strspn(license
, license_chars
);
226 warnx("Invalid character in license name at position %" PRIzu
, license
- start
+ 1);
230 if (acceptable_license_internal(license
, len
)) {
233 } else if (expr_type
== 2) {
239 len
= strspn(license
, license_spaces
);
240 if (len
== 0 && *license
&& *license
!= ')') {
241 warnx("Missing space at position %" PRIzu
, license
- start
+ 1);
247 if (*license
== ')') {
248 if (!need_parenthesis
) {
249 warnx("Missing open parenthesis at position %" PRIzu
, license
- start
+ 1);
252 *licensep
= license
+ 1;
255 if (*license
== '\0') {
256 if (need_parenthesis
) {
257 warnx("Unbalanced parenthesis at position %" PRIzu
, license
- start
+ 1);
264 if (strncmp(license
, "AND", 3) == 0) {
265 if (expr_type
== 1) {
266 warnx("Invalid operator in OR expression at position %" PRIzu
, license
- start
+ 1);
271 } else if (strncmp(license
, "OR", 2) == 0) {
272 if (expr_type
== 2) {
273 warnx("Invalid operator in AND expression at position %" PRIzu
, license
- start
+ 1);
279 warnx("Invalid operator at position %" PRIzu
, license
- start
+ 1);
282 len
= strspn(license
, license_spaces
);
283 if (len
== 0 && *license
!= '(') {
284 warnx("Missing space at position %" PRIzu
, license
- start
+ 1);
292 acceptable_pkg_license(const char *license
)
296 ret
= acceptable_pkg_license_internal(&license
, 1, license
);
299 license
+= strspn(license
, license_spaces
);
301 warnx("Trailing garbage in license specification");
308 load_license_lists(void)
310 if (add_licenses(getenv("PKGSRC_ACCEPTABLE_LICENSES")))
311 errx(EXIT_FAILURE
, "syntax error in PKGSRC_ACCEPTABLE_LICENSES");
312 if (add_licenses(acceptable_licenses
))
313 errx(EXIT_FAILURE
, "syntax error in ACCEPTABLE_LICENSES");
314 if (add_licenses(getenv("PKGSRC_DEFAULT_ACCEPTABLE_LICENSES")))
315 errx(EXIT_FAILURE
, "syntax error in PKGSRC_DEFAULT_ACCEPTABLE_LICENSES");
316 if (add_licenses(default_acceptable_licenses
))
317 errx(EXIT_FAILURE
, "syntax error in DEFAULT_ACCEPTABLE_LICENSES");