licenses.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. { "Public domain", "Public domain",
  68. { NULL, NULL, NULL }
  69. },
  70. { "Unknown license", "Unknown license",
  71. { NULL, NULL, NULL }
  72. },
  73. };
  74. /* License compatibilty chart (simplified: we don't test that licenses between
  75. * files are compatible). */
  76. bool license_compatible[LICENSE_UNKNOWN+1][LICENSE_UNKNOWN] = {
  77. /* LGPL2+ LGPL2 LGPL3 LGPL GPL2+ GPL2 GPL3 GPL BSD MIT PD */
  78. /* _info says: LGPL2+ */
  79. { true, false,false,true, false,false,false,false,true, true, true },
  80. /* _info says: LGPL2 only */
  81. { true, true, false,true, false,false,false,false,true, true, true },
  82. /* _info says: LGPL3 (or any later version) */
  83. { true, false,true, true, false,false,false,false,true, true, true },
  84. /* _info says: LGPL (no version specified) */
  85. { true, true, true, true, false,false,false,false,true, true, true },
  86. /* _info says: GPL2+ */
  87. { true, true, true, true, true, false,false,true, true, true, true },
  88. /* _info says: GPL2 only */
  89. { true, true, true, true, true, true, false,true, true, true, true },
  90. /* _info says: GPL3 (or any later version) */
  91. { true, true, true, true, true, false,true, true, true, true, true },
  92. /* _info says: GPL (unknown version) */
  93. { true, true, true, true, true, true, true, true, true, true, true },
  94. /* _info says: BSD (3-clause) */
  95. { false,false,false,false,false,false,false,false,true, true, true },
  96. /* _info says: MIT */
  97. { false,false,false,false,false,false,false,false,false,true, true },
  98. /* _info says: Public domain */
  99. { false,false,false,false,false,false,false,false,false,false,true },
  100. /* _info says something we don't understand */
  101. { false,false,false,false,false,false,false,false,false,false,true }
  102. };
  103. /* See GPLv2 and v2 (basically same wording) for interpreting versions:
  104. * the "any later version" means the recepient can choose. */
  105. enum license which_license(struct doc_section *d)
  106. {
  107. if (!d)
  108. return LICENSE_UNKNOWN;
  109. /* This means "user chooses what version", including GPLv1! */
  110. if (streq(d->lines[0], "GPL"))
  111. return LICENSE_GPL;
  112. /* This means "v2 only". */
  113. if (streq(d->lines[0], "GPLv2"))
  114. return LICENSE_GPLv2;
  115. /* This means "v2 or above" at user's choice. */
  116. if (streq(d->lines[0], "GPL (v2 or any later version)"))
  117. return LICENSE_GPLv2_PLUS;
  118. /* This means "v3 or above" at user's choice. */
  119. if (streq(d->lines[0], "GPL (v3 or any later version)"))
  120. return LICENSE_GPLv3;
  121. /* This means "user chooses what version" */
  122. if (streq(d->lines[0], "LGPL"))
  123. return LICENSE_LGPL;
  124. /* This means "v2.1 only". */
  125. if (streq(d->lines[0], "LGPLv2.1"))
  126. return LICENSE_LGPLv2;
  127. /* This means "v2.1 or above" at user's choice. */
  128. if (streq(d->lines[0], "LGPL (v2.1 or any later version)"))
  129. return LICENSE_LGPLv2_PLUS;
  130. /* This means "v3 or above" at user's choice. */
  131. if (streq(d->lines[0], "LGPL (v3 or any later version)"))
  132. return LICENSE_LGPLv3;
  133. if (streq(d->lines[0], "BSD-MIT") || streq(d->lines[0], "MIT"))
  134. return LICENSE_MIT;
  135. if (streq(d->lines[0], "BSD (3 clause)"))
  136. return LICENSE_BSD;
  137. if (strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
  138. return LICENSE_PUBLIC_DOMAIN;
  139. return LICENSE_UNKNOWN;
  140. }
  141. const char *get_ccan_simplified(struct ccan_file *f)
  142. {
  143. if (!f->simplified) {
  144. unsigned int i, j;
  145. /* Simplify for easy matching: only alnum and single spaces. */
  146. f->simplified = talloc_strdup(f, get_ccan_file_contents(f));
  147. for (i = 0, j = 0; f->simplified[i]; i++) {
  148. if (cisupper(f->simplified[i]))
  149. f->simplified[j++] = tolower(f->simplified[i]);
  150. else if (cislower(f->simplified[i]))
  151. f->simplified[j++] = f->simplified[i];
  152. else if (cisdigit(f->simplified[i]))
  153. f->simplified[j++] = f->simplified[i];
  154. else if (cisspace(f->simplified[i])) {
  155. if (j != 0 && f->simplified[j-1] != ' ')
  156. f->simplified[j++] = ' ';
  157. }
  158. }
  159. f->simplified[j] = '\0';
  160. }
  161. return f->simplified;
  162. }
  163. bool find_boilerplate(struct ccan_file *f, enum license license)
  164. {
  165. unsigned int i;
  166. for (i = 0; i < NUM_CLAUSES; i++) {
  167. if (!licenses[license].clause[i])
  168. break;
  169. if (!strstr(get_ccan_simplified(f),
  170. licenses[license].clause[i])) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. struct doc_section *find_license_tag(const struct manifest *m)
  177. {
  178. struct doc_section *d;
  179. list_for_each(get_ccan_file_docs(m->info_file), d, list) {
  180. if (!streq(d->function, m->basename))
  181. continue;
  182. if (streq(d->type, "license"))
  183. return d;
  184. }
  185. return NULL;
  186. }