licenses.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "licenses.h"
  2. #include "ccanlint.h"
  3. #include <ccan/str/str.h>
  4. #include <ccan/tal/tal.h>
  5. #include <ccan/tal/str/str.h>
  6. const struct license_info licenses[] = {
  7. { "LGPLv2+", "LGPL",
  8. "GNU LGPL version 2 (or later)",
  9. { "gnu lesser general public license",
  10. "version 2",
  11. "or at your option any later version"
  12. }
  13. },
  14. { "LGPLv2", "LGPL",
  15. "GNU LGPL version 2",
  16. { "gnu lesser general public license",
  17. "version 2",
  18. NULL
  19. }
  20. },
  21. { "LGPLv3", "LGPL",
  22. "GNU LGPL version 3",
  23. { "gnu lesser general public license",
  24. "version 3",
  25. NULL
  26. }
  27. },
  28. { "LGPL", "LGPL",
  29. "GNU LGPL",
  30. { "gnu lesser general public license",
  31. NULL,
  32. NULL
  33. }
  34. },
  35. { "GPLv2+", "GPL",
  36. "GNU GPL version 2 (or later)",
  37. { "gnu general public license",
  38. "version 2",
  39. "or at your option any later version"
  40. }
  41. },
  42. { "GPLv2", "GPL",
  43. "GNU GPL version 2",
  44. { "gnu general public license",
  45. "version 2",
  46. NULL
  47. }
  48. },
  49. { "GPLv3", "GPL",
  50. "GNU GPL version 3 (or later)",
  51. { "gnu general public license",
  52. "version 3",
  53. NULL
  54. }
  55. },
  56. { "GPL", "GPL",
  57. "GNU GPL",
  58. { "gnu general public license",
  59. NULL,
  60. NULL
  61. }
  62. },
  63. { "BSD-3CLAUSE", "BSD",
  64. "3-clause BSD license",
  65. { "redistributions of source code must retain",
  66. "redistributions in binary form must reproduce",
  67. "endorse or promote"
  68. }
  69. },
  70. { "BSD-MIT", "MIT",
  71. "MIT (BSD) license",
  72. { "without restriction",
  73. "above copyright notice",
  74. "without warranty"
  75. }
  76. },
  77. { "CC0", "CC0",
  78. "CC0 license (public domain)",
  79. { "Waiver.",
  80. "unconditionally waives",
  81. NULL
  82. }
  83. },
  84. { "Public domain", "Public domain",
  85. NULL,
  86. { NULL, NULL, NULL }
  87. },
  88. { "Unknown license", "Unknown license",
  89. NULL,
  90. { NULL, NULL, NULL }
  91. },
  92. };
  93. /* License compatibilty chart (simplified: we don't test that licenses between
  94. * files are compatible). */
  95. #define O true
  96. #define X false
  97. bool license_compatible[LICENSE_UNKNOWN+1][LICENSE_UNKNOWN] = {
  98. /* LGPL2+ LGPL3 GPL2+ GPL3 BSD CC0
  99. LGPL2 LGPL GPL2 GPL MIT PD */
  100. /* _info says: LGPL2+ */
  101. { O, X, X, O, X, X, X, X, O, O, O, O },
  102. /* _info says: LGPL2 only */
  103. { O, O, X, O, X, X, X, X, O, O, O, O },
  104. /* _info says: LGPL3 (or any later version) */
  105. { O, X, O, O, X, X, X, X, O, O, O, O },
  106. /* _info says: LGPL (no version specified) */
  107. { O, O, O, O, X, X, X, X, O, O, O, O },
  108. /* _info says: GPL2+ */
  109. { O, O, O, O, O, X, X, O, O, O, O, O },
  110. /* _info says: GPL2 only */
  111. { O, O, O, O, O, O, X, O, O, O, O, O },
  112. /* _info says: GPL3 (or any later version) */
  113. { O, O, O, O, O, X, O, O, O, O, O, O },
  114. /* _info says: GPL (unknown version) */
  115. { O, O, O, O, O, O, O, O, O, O, O, O },
  116. /* _info says: BSD (3-clause) */
  117. { X, X, X, X, X, X, X, X, O, O, O, O },
  118. /* _info says: MIT */
  119. { X, X, X, X, X, X, X, X, X, O, O, O },
  120. /* _info says: CC0 */
  121. { X, X, X, X, X, X, X, X, X, X, O, O },
  122. /* _info says: Public domain */
  123. { X, X, X, X, X, X, X, X, X, X, O, O },
  124. /* _info says something we don't understand */
  125. { X, X, X, X, X, X, X, X, X, X, O, O }
  126. };
  127. #undef X
  128. #undef O
  129. /* See GPLv2 and v2 (basically same wording) for interpreting versions:
  130. * the "any later version" means the recepient can choose. */
  131. enum license which_license(struct doc_section *d)
  132. {
  133. if (!d)
  134. return LICENSE_UNKNOWN;
  135. /* This means "user chooses what version", including GPLv1! */
  136. if (streq(d->lines[0], "GPL"))
  137. return LICENSE_GPL;
  138. /* This means "v2 only". */
  139. if (streq(d->lines[0], "GPLv2"))
  140. return LICENSE_GPLv2;
  141. /* This means "v2 or above" at user's choice. */
  142. if (streq(d->lines[0], "GPL (v2 or any later version)"))
  143. return LICENSE_GPLv2_PLUS;
  144. /* This means "v3 or above" at user's choice. */
  145. if (streq(d->lines[0], "GPL (v3 or any later version)"))
  146. return LICENSE_GPLv3;
  147. /* This means "user chooses what version" */
  148. if (streq(d->lines[0], "LGPL"))
  149. return LICENSE_LGPL;
  150. /* This means "v2.1 only". */
  151. if (streq(d->lines[0], "LGPLv2.1"))
  152. return LICENSE_LGPLv2;
  153. /* This means "v2.1 or above" at user's choice. */
  154. if (streq(d->lines[0], "LGPL (v2.1 or any later version)"))
  155. return LICENSE_LGPLv2_PLUS;
  156. /* This means "v3 or above" at user's choice. */
  157. if (streq(d->lines[0], "LGPL (v3 or any later version)"))
  158. return LICENSE_LGPLv3;
  159. if (streq(d->lines[0], "BSD-MIT") || streq(d->lines[0], "MIT"))
  160. return LICENSE_MIT;
  161. if (streq(d->lines[0], "BSD (3 clause)"))
  162. return LICENSE_BSD;
  163. if (streq(d->lines[0], "CC0"))
  164. return LICENSE_CC0;
  165. if (tal_strreg(NULL, d->lines[0], "CC0 \\([Pp]ublic [Dd]omain\\)",
  166. NULL))
  167. return LICENSE_CC0;
  168. if (tal_strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
  169. return LICENSE_PUBLIC_DOMAIN;
  170. return LICENSE_UNKNOWN;
  171. }
  172. const char *get_ccan_simplified(struct ccan_file *f)
  173. {
  174. if (!f->simplified) {
  175. unsigned int i, j;
  176. /* Simplify for easy matching: only alnum and single spaces. */
  177. f->simplified = tal_strdup(f, get_ccan_file_contents(f));
  178. for (i = 0, j = 0; f->simplified[i]; i++) {
  179. if (cisupper(f->simplified[i]))
  180. f->simplified[j++] = tolower(f->simplified[i]);
  181. else if (cislower(f->simplified[i]))
  182. f->simplified[j++] = f->simplified[i];
  183. else if (cisdigit(f->simplified[i]))
  184. f->simplified[j++] = f->simplified[i];
  185. else if (cisspace(f->simplified[i])) {
  186. if (j != 0 && f->simplified[j-1] != ' ')
  187. f->simplified[j++] = ' ';
  188. }
  189. }
  190. f->simplified[j] = '\0';
  191. }
  192. return f->simplified;
  193. }
  194. bool find_boilerplate(struct ccan_file *f, enum license license)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < NUM_CLAUSES; i++) {
  198. if (!licenses[license].clause[i])
  199. break;
  200. if (!strstr(get_ccan_simplified(f),
  201. licenses[license].clause[i])) {
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. struct doc_section *find_license_tag(const struct manifest *m)
  208. {
  209. struct doc_section *d;
  210. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  211. if (!streq(d->function, m->modname))
  212. continue;
  213. if (streq(d->type, "license"))
  214. return d;
  215. }
  216. return NULL;
  217. }
  218. const char *get_license_oneliner(const tal_t *ctx, enum license license)
  219. {
  220. return tal_fmt(ctx, "/* %s - see LICENSE file for details */",
  221. licenses[license].describe);
  222. }