licenses.c 5.7 KB

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