3 #include <ccan/str/str.h>
4 #include <ccan/tal/tal.h>
5 #include <ccan/tal/str/str.h>
7 const struct license_info licenses[] = {
9 "GNU LGPL version 2 (or later)",
10 { "gnu lesser general public license",
12 "or at your option any later version"
17 { "gnu lesser general public license",
24 { "gnu lesser general public license",
31 { "gnu lesser general public license",
37 "GNU GPL version 2 (or later)",
38 { "gnu general public license",
40 "or at your option any later version"
45 { "gnu general public license",
51 "GNU GPL version 3 (or later)",
52 { "gnu general public license",
59 { "gnu general public license",
64 { "BSD-3CLAUSE", "BSD",
65 "3-clause BSD license",
66 { "redistributions of source code must retain",
67 "redistributions in binary form must reproduce",
73 { "without restriction",
74 "above copyright notice",
79 "CC0 license (public domain)",
81 "unconditionally waives",
85 { "Public domain", "Public domain",
89 { "Unknown license", "Unknown license",
95 /* License compatibilty chart (simplified: we don't test that licenses between
96 * files are compatible). */
99 bool license_compatible[LICENSE_UNKNOWN+1][LICENSE_UNKNOWN] = {
100 /* LGPL2+ LGPL3 GPL2+ GPL3 BSD CC0
101 LGPL2 LGPL GPL2 GPL MIT PD */
102 /* _info says: LGPL2+ */
103 { O, X, X, O, X, X, X, X, O, O, O, O },
104 /* _info says: LGPL2 only */
105 { O, O, X, O, X, X, X, X, O, O, O, O },
106 /* _info says: LGPL3 (or any later version) */
107 { O, X, O, O, X, X, X, X, O, O, O, O },
108 /* _info says: LGPL (no version specified) */
109 { O, O, O, O, X, X, X, X, O, O, O, O },
110 /* _info says: GPL2+ */
111 { O, O, O, O, O, X, X, O, O, O, O, O },
112 /* _info says: GPL2 only */
113 { O, O, O, O, O, O, X, O, O, O, O, O },
114 /* _info says: GPL3 (or any later version) */
115 { O, O, O, O, O, X, O, O, O, O, O, O },
116 /* _info says: GPL (unknown version) */
117 { O, O, O, O, O, O, O, O, O, O, O, O },
118 /* _info says: BSD (3-clause) */
119 { X, X, X, X, X, X, X, X, O, O, O, O },
120 /* _info says: MIT */
121 { X, X, X, X, X, X, X, X, X, O, O, O },
122 /* _info says: CC0 */
123 { X, X, X, X, X, X, X, X, X, X, O, O },
124 /* _info says: Public domain */
125 { X, X, X, X, X, X, X, X, X, X, O, O },
126 /* _info says something we don't understand */
127 { X, X, X, X, X, X, X, X, X, X, O, O }
132 /* See GPLv2 and v2 (basically same wording) for interpreting versions:
133 * the "any later version" means the recepient can choose. */
134 enum license which_license(struct doc_section *d)
137 return LICENSE_UNKNOWN;
139 /* This means "user chooses what version", including GPLv1! */
140 if (streq(d->lines[0], "GPL"))
142 /* This means "v2 only". */
143 if (streq(d->lines[0], "GPLv2"))
144 return LICENSE_GPLv2;
145 /* This means "v2 or above" at user's choice. */
146 if (streq(d->lines[0], "GPL (v2 or any later version)"))
147 return LICENSE_GPLv2_PLUS;
148 /* This means "v3 or above" at user's choice. */
149 if (streq(d->lines[0], "GPL (v3 or any later version)"))
150 return LICENSE_GPLv3;
152 /* This means "user chooses what version" */
153 if (streq(d->lines[0], "LGPL"))
155 /* This means "v2.1 only". */
156 if (streq(d->lines[0], "LGPLv2.1"))
157 return LICENSE_LGPLv2;
158 /* This means "v2.1 or above" at user's choice. */
159 if (streq(d->lines[0], "LGPL (v2.1 or any later version)"))
160 return LICENSE_LGPLv2_PLUS;
161 /* This means "v3 or above" at user's choice. */
162 if (streq(d->lines[0], "LGPL (v3 or any later version)"))
163 return LICENSE_LGPLv3;
165 if (streq(d->lines[0], "BSD-MIT") || streq(d->lines[0], "MIT"))
167 if (streq(d->lines[0], "BSD (3 clause)"))
169 if (streq(d->lines[0], "CC0"))
171 if (tal_strreg(NULL, d->lines[0], "CC0 \\([Pp]ublic [Dd]omain\\)",
174 if (tal_strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
175 return LICENSE_PUBLIC_DOMAIN;
177 return LICENSE_UNKNOWN;
180 const char *get_ccan_simplified(struct ccan_file *f)
182 if (!f->simplified) {
185 /* Simplify for easy matching: only alnum and single spaces. */
186 f->simplified = tal_strdup(f, get_ccan_file_contents(f));
187 for (i = 0, j = 0; f->simplified[i]; i++) {
188 if (cisupper(f->simplified[i]))
189 f->simplified[j++] = tolower(f->simplified[i]);
190 else if (cislower(f->simplified[i]))
191 f->simplified[j++] = f->simplified[i];
192 else if (cisdigit(f->simplified[i]))
193 f->simplified[j++] = f->simplified[i];
194 else if (cisspace(f->simplified[i])) {
195 if (j != 0 && f->simplified[j-1] != ' ')
196 f->simplified[j++] = ' ';
199 f->simplified[j] = '\0';
201 return f->simplified;
204 bool find_boilerplate(struct ccan_file *f, enum license license)
208 for (i = 0; i < NUM_CLAUSES; i++) {
209 if (!licenses[license].clause[i])
212 if (!strstr(get_ccan_simplified(f),
213 licenses[license].clause[i])) {
220 struct doc_section *find_license_tag(const struct manifest *m)
222 struct doc_section *d;
224 list_for_each(get_ccan_file_docs(m->info_file), d, list) {
225 if (!streq(d->function, m->modname))
227 if (streq(d->type, "license"))
233 const char *get_license_oneliner(const tal_t *ctx, enum license license)
235 return tal_fmt(ctx, "/* %s - see LICENSE file for details */",
236 licenses[license].describe);