3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/str_talloc/str_talloc.h>
7 const struct license_info licenses[] = {
9 { "gnu lesser general public license",
11 "or at your option any later version"
15 { "gnu lesser general public license",
21 { "gnu lesser general public license",
27 { "gnu lesser general public license",
33 { "gnu general public license",
35 "or at your option any later version"
39 { "gnu general public license",
45 { "gnu general public license",
51 { "gnu general public license",
56 { "BSD-3CLAUSE", "BSD",
57 { "redistributions of source code must retain",
58 "redistributions in binary form must reproduce",
63 { "without restriction",
64 "above copyright notice",
70 "unconditionally waives",
74 { "Public domain", "Public domain",
77 { "Unknown license", "Unknown license",
82 /* License compatibilty chart (simplified: we don't test that licenses between
83 * files are compatible). */
86 bool license_compatible[LICENSE_UNKNOWN+1][LICENSE_UNKNOWN] = {
87 /* LGPL2+ LGPL3 GPL2+ GPL3 BSD CC0
88 LGPL2 LGPL GPL2 GPL MIT PD */
89 /* _info says: LGPL2+ */
90 { O, X, X, O, X, X, X, X, O, O, O, O },
91 /* _info says: LGPL2 only */
92 { O, O, X, O, X, X, X, X, O, O, O, O },
93 /* _info says: LGPL3 (or any later version) */
94 { O, X, O, O, X, X, X, X, O, O, O, O },
95 /* _info says: LGPL (no version specified) */
96 { O, O, O, O, X, X, X, X, O, O, O, O },
97 /* _info says: GPL2+ */
98 { O, O, O, O, O, X, X, O, O, O, O, O },
99 /* _info says: GPL2 only */
100 { O, O, O, O, O, O, X, O, O, O, O, O },
101 /* _info says: GPL3 (or any later version) */
102 { O, O, O, O, O, X, O, O, O, O, O, O },
103 /* _info says: GPL (unknown version) */
104 { O, O, O, O, O, O, O, O, O, O, O, O },
105 /* _info says: BSD (3-clause) */
106 { X, X, X, X, X, X, X, X, O, O, O, O },
107 /* _info says: MIT */
108 { X, X, X, X, X, X, X, X, X, O, O, O },
109 /* _info says: CC0 */
110 { X, X, X, X, X, X, X, X, X, X, O, O },
111 /* _info says: Public domain */
112 { X, X, X, X, X, X, X, X, X, X, O, O },
113 /* _info says something we don't understand */
114 { X, X, X, X, X, X, X, X, X, X, O, O }
119 /* See GPLv2 and v2 (basically same wording) for interpreting versions:
120 * the "any later version" means the recepient can choose. */
121 enum license which_license(struct doc_section *d)
124 return LICENSE_UNKNOWN;
126 /* This means "user chooses what version", including GPLv1! */
127 if (streq(d->lines[0], "GPL"))
129 /* This means "v2 only". */
130 if (streq(d->lines[0], "GPLv2"))
131 return LICENSE_GPLv2;
132 /* This means "v2 or above" at user's choice. */
133 if (streq(d->lines[0], "GPL (v2 or any later version)"))
134 return LICENSE_GPLv2_PLUS;
135 /* This means "v3 or above" at user's choice. */
136 if (streq(d->lines[0], "GPL (v3 or any later version)"))
137 return LICENSE_GPLv3;
139 /* This means "user chooses what version" */
140 if (streq(d->lines[0], "LGPL"))
142 /* This means "v2.1 only". */
143 if (streq(d->lines[0], "LGPLv2.1"))
144 return LICENSE_LGPLv2;
145 /* This means "v2.1 or above" at user's choice. */
146 if (streq(d->lines[0], "LGPL (v2.1 or any later version)"))
147 return LICENSE_LGPLv2_PLUS;
148 /* This means "v3 or above" at user's choice. */
149 if (streq(d->lines[0], "LGPL (v3 or any later version)"))
150 return LICENSE_LGPLv3;
152 if (streq(d->lines[0], "BSD-MIT") || streq(d->lines[0], "MIT"))
154 if (streq(d->lines[0], "BSD (3 clause)"))
156 if (streq(d->lines[0], "CC0"))
158 if (strreg(NULL, d->lines[0], "CC0 \\([Pp]ublic [Dd]omain\\)", NULL))
160 if (strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
161 return LICENSE_PUBLIC_DOMAIN;
163 return LICENSE_UNKNOWN;
166 const char *get_ccan_simplified(struct ccan_file *f)
168 if (!f->simplified) {
171 /* Simplify for easy matching: only alnum and single spaces. */
172 f->simplified = talloc_strdup(f, get_ccan_file_contents(f));
173 for (i = 0, j = 0; f->simplified[i]; i++) {
174 if (cisupper(f->simplified[i]))
175 f->simplified[j++] = tolower(f->simplified[i]);
176 else if (cislower(f->simplified[i]))
177 f->simplified[j++] = f->simplified[i];
178 else if (cisdigit(f->simplified[i]))
179 f->simplified[j++] = f->simplified[i];
180 else if (cisspace(f->simplified[i])) {
181 if (j != 0 && f->simplified[j-1] != ' ')
182 f->simplified[j++] = ' ';
185 f->simplified[j] = '\0';
187 return f->simplified;
190 bool find_boilerplate(struct ccan_file *f, enum license license)
194 for (i = 0; i < NUM_CLAUSES; i++) {
195 if (!licenses[license].clause[i])
198 if (!strstr(get_ccan_simplified(f),
199 licenses[license].clause[i])) {
206 struct doc_section *find_license_tag(const struct manifest *m)
208 struct doc_section *d;
210 list_for_each(get_ccan_file_docs(m->info_file), d, list) {
211 if (!streq(d->function, m->basename))
213 if (streq(d->type, "license"))