licenses.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "licenses.h"
  2. #include "ccanlint.h"
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. const struct license_info licenses[] = {
  6. { "LGPLv2+", "LGPL",
  7. { "gnu lesser general public license",
  8. "version 2",
  9. "or at your option any later version"
  10. }
  11. },
  12. { "LGPLv2", "LGPL",
  13. { "gnu lesser general public license",
  14. "version 2",
  15. NULL
  16. }
  17. },
  18. { "LGPLv3", "LGPL",
  19. { "gnu lesser general public license",
  20. "version 3",
  21. NULL
  22. }
  23. },
  24. { "LGPL", "LGPL",
  25. { "gnu lesser general public license",
  26. NULL,
  27. NULL
  28. }
  29. },
  30. { "GPLv2+", "GPL",
  31. { "gnu general public license",
  32. "version 2",
  33. "or at your option any later version"
  34. }
  35. },
  36. { "GPLv2", "GPL",
  37. { "gnu general public license",
  38. "version 2",
  39. NULL
  40. }
  41. },
  42. { "GPLv3", "GPL",
  43. { "gnu general public license",
  44. "version 3",
  45. NULL
  46. }
  47. },
  48. { "GPL", "GPL",
  49. { "gnu general public license",
  50. NULL,
  51. NULL
  52. }
  53. },
  54. { "BSD-3CLAUSE", "BSD",
  55. { "redistributions of source code must retain",
  56. "redistributions in binary form must reproduce",
  57. "endorse or promote"
  58. }
  59. },
  60. { "BSD-MIT", "MIT",
  61. { "without restriction",
  62. "above copyright notice",
  63. "without warranty"
  64. }
  65. },
  66. { "Public domain", "Public domain",
  67. { NULL, NULL, NULL }
  68. },
  69. { "Unknown license", "Unknown license",
  70. { NULL, NULL, NULL }
  71. },
  72. };
  73. /* License compatibilty chart (simplified: we don't test that licenses between
  74. * files are compatible). */
  75. bool license_compatible[LICENSE_UNKNOWN+1][LICENSE_UNKNOWN] = {
  76. /* LGPL2+ LGPL2 LGPL3 LGPL GPL2+ GPL2 GPL3 GPL BSD MIT PD */
  77. /* _info says: LGPL2+ */
  78. { true, false,false,true, false,false,false,false,true, true, true },
  79. /* _info says: LGPL2 only */
  80. { true, true, false,true, false,false,false,false,true, true, true },
  81. /* _info says: LGPL3 (or any later version) */
  82. { true, false,true, true, false,false,false,false,true, true, true },
  83. /* _info says: LGPL (no version specified) */
  84. { true, true, true, true, false,false,false,false,true, true, true },
  85. /* _info says: GPL2+ */
  86. { true, true, true, true, true, false,false,true, true, true, true },
  87. /* _info says: GPL2 only */
  88. { true, true, true, true, true, true, false,true, true, true, true },
  89. /* _info says: GPL3 (or any later version) */
  90. { true, true, true, true, true, false,true, true, true, true, true },
  91. /* _info says: GPL (unknown version) */
  92. { true, true, true, true, true, true, true, true, true, true, true },
  93. /* _info says: BSD (3-clause) */
  94. { false,false,false,false,false,false,false,false,true, true, true },
  95. /* _info says: MIT */
  96. { false,false,false,false,false,false,false,false,false,true, true },
  97. /* _info says: Public domain */
  98. { false,false,false,false,false,false,false,false,false,false,true },
  99. /* _info says something we don't understand */
  100. { false,false,false,false,false,false,false,false,false,false,true }
  101. };
  102. const char *get_ccan_simplified(struct ccan_file *f)
  103. {
  104. if (!f->simplified) {
  105. unsigned int i, j;
  106. /* Simplify for easy matching: only alnum and single spaces. */
  107. f->simplified = talloc_strdup(f, get_ccan_file_contents(f));
  108. for (i = 0, j = 0; f->simplified[i]; i++) {
  109. if (cisupper(f->simplified[i]))
  110. f->simplified[j++] = tolower(f->simplified[i]);
  111. else if (cislower(f->simplified[i]))
  112. f->simplified[j++] = f->simplified[i];
  113. else if (cisdigit(f->simplified[i]))
  114. f->simplified[j++] = f->simplified[i];
  115. else if (cisspace(f->simplified[i])) {
  116. if (j != 0 && f->simplified[j-1] != ' ')
  117. f->simplified[j++] = ' ';
  118. }
  119. }
  120. f->simplified[j] = '\0';
  121. }
  122. return f->simplified;
  123. }
  124. bool find_boilerplate(struct ccan_file *f, enum license license)
  125. {
  126. unsigned int i;
  127. for (i = 0; i < NUM_CLAUSES; i++) {
  128. if (!licenses[license].clause[i])
  129. break;
  130. if (!strstr(get_ccan_simplified(f),
  131. licenses[license].clause[i])) {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }