1 // licenses.c written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
11 // Holds licenses and their associated details and patterns.
12 License license_map[] = {
15 "http://www.opensource.org/licenses/afl-3.0.php",
16 "Academic Free License",
17 "\\bAcademic\\s*Free\\s*License\\b",
25 "http://www.opensource.org/licenses/apl1.0.php",
26 "Adaptive Public License",
27 "\\bAdaptive\\s*Public\\s*License\\b",
35 "http://www.affero.org/oagpl.html",
36 "GNU Affero General Public License",
37 "\\bGNU\\s+Affero\\s+General\\s+Public\\s+License\\b",
45 "http://www.opensource.org/licenses/apachepl.php",
46 "Apache Software License",
47 "(\\bApache\\s*Software\\s*License(?![\\s,]*2))|(\\bapache\\s*license(?![\\s,]*2))",
55 "http://www.opensource.org/licenses/apache2.0.php",
56 "Apache License, 2.0",
57 "\\bapache\\s+(software\\s+)?license,?\\s+(version\\s+)?2",
64 LIC_APPLE_OPEN_SOURCE,
65 "http://www.opensource.org/licenses/apsl-2.0.php",
66 "Apple Public Source License",
67 "\\bApple\\s*Public\\s*Source\\s*License\\b",
75 "http://www.opensource.org/licenses/artistic-license.php",
77 "\\bartistic\\s*license\\b",
84 LIC_ATTRIBUTION_ASSURANCE,
85 "http://www.opensource.org/licenses/attribution.php",
86 "Attribution Assurance Licenses",
87 "\\battribution\\s*assurance\\s*license(s)?\\b",
95 "http://www.boost.org/LICENSE_1_0.txt",
96 "Boost Software License - Version 1.0 - August 17th, 2003",
97 "\\bboost\\s*software\\s*license\\b",
105 "http://www.opensource.org/licenses/bsd-license.php",
107 "(\\bbsd\\s*license\\b)|(The Regents of the University of California)",
115 "http://www.cecill.info/licences/Licence_CeCILL_V2-en.html",
125 "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html",
135 "http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html",
144 LIC_COMPUTER_ASSOCIATES_TRUSTED,
145 "http://www.opensource.org/licenses/ca-tosl1.1.php",
146 "Computer Associates Trusted Open Source License 1.1",
147 "\\bcomputer\\s*associates\\s*trusted\\s*open\\s*source\\s*license\\b",
154 LIC_COMMON_DEVELOPMENT_AND_DISTRIBUTION,
155 "http://www.opensource.org/licenses/cddl1.php",
156 "Common Development and Distribution License",
157 "\\bcommon\\s*development\\s*and\\s*distribution\\s*license\\b",
165 "http://www.opensource.org/licenses/cpl1.0.php",
166 "Common Public License 1.0",
167 "\\bcommon\\s*public\\s*license\\b",
175 "http://www.opensource.org/licenses/cuaoffice.php",
176 "CUA Office Public License Version 1.0",
177 "\\bCUA\\s*office\\s*public\\s*license\\b",
185 "http://www.opensource.org/licenses/eudatagrid.php",
186 "EU DataGrid Software License",
187 "\\beu\\s*datagrid\\s*software\\s*license\\b",
195 "http://www.opensource.org/licenses/eclipse-1.0.php",
196 "Eclipse Public License",
197 "\\beclipse\\s*public\\s*license\\b",
205 "http://www.opensource.org/licenses/ecl1.php",
206 "Educational Community License",
207 "\\beducational\\s*community\\s*license\\b",
215 "http://www.opensource.org/licenses/eiffel.php",
216 "Eiffel Forum License",
217 "\\beiffel\\s*forum\\s*license(?![,V\\s]*2)\\b",
225 "http://www.opensource.org/licenses/ver2_eiffel.php",
226 "Eiffel Forum License V2.0",
227 "\\beiffel\\s*forum\\s*license [,V\\s]*2",
235 "http://www.opensource.org/licenses/entessa.php",
236 "Entessa Public License",
237 "\\bentessa\\s*public\\s*license\\b",
245 "http://www.opensource.org/licenses/fair.php",
247 "\\bfair\\s*license\\b",
255 "http://www.opensource.org/licenses/frameworx.php",
257 "\\bframeworx\\s*license\\b",
265 "http://www.gnu.org/licenses/gpl-3.0.html",
266 "GNU General Public License 3.0",
267 "\\b(GNU GENERAL PUBLIC LICENSE|GPL).{0,100}(Version)? 3.{0,50}later",
268 PCRE_CASELESS | PCRE_MULTILINE,
275 "http://www.gnu.org/licenses/gpl-3.0.html",
276 "GNU General Public License 3.0",
277 "GNU (GENERAL PUBLIC LICENSE|GPL).{0,100}(Version |v)3",
278 PCRE_CASELESS | PCRE_MULTILINE,
279 "((at your option) any later version)|(GENERAL PUBLIC LICENSE.*GENERAL PUBLIC LICENSE)",
285 "http://www.gnu.org/licenses/lgpl-3.0.html",
286 "GNU Lesser General Public License 3.0",
287 "((\\blgpl\\b)|(\\bgnu\\s*(library|lesser)\\s*(general\\s*)?(public\\s*)?license\\b)|(\\b(lesser|library)\\s*gpl\\b)).{0,10}(\\bas published by the free software foundation\\b)?.{0,10}(\\bversion\\b)?.{0,10}\\b3(\\.0)?\\b",
295 "http://www.opensource.org/licenses/gpl-license.php",
296 "GNU General Public License (GPL)",
297 "(\\bgpl\\b)|(\\bgplv2\\b)|(\\bgnu\\s*general\\s*public\\s*license\\b)|(\\bwww\\.gnu\\.org\\/licenses\\/gpl\\.txt\\b)",
305 "http://www.opensource.org/licenses/lgpl-license.php",
306 "GNU Library or \"Lesser\" GPL (LGPL)",
307 "(\\blgpl\\b)|(\\bgnu\\s*(library|lesser)\\s*(general\\s*)?(public\\s*)?license\\b)|(\\b(lesser|library)\\s*gpl\\b)",
315 "http://www.opensource.org/licenses/historical.php",
316 "Historical Permission Notice and Disclaimer",
317 "\\bhistorical\\s*permission\\s*notice\\s*and\\s*disclaimer\\b",
325 "http://i9os.googlecode.com/svn/trunk/Documentation/Licenses/i9_License",
327 "\\bi9\\s*\\s*license\\b",
335 "http://www.opensource.org/licenses/ibmpl.php",
336 "IBM Public License",
337 "\\bibm\\s*public\\s*license\\b",
344 LIC_INTEL_OPEN_SOURCE,
345 "http://www.opensource.org/licenses/intel-open-source-license.php",
346 "Intel Open Source License",
347 "\\bintel\\s*open\\s*source\\s*license\\b",
354 LIC_JABBER_OPEN_SOURCE,
355 "http://www.opensource.org/licenses/jabberpl.php",
356 "Jabber Open Source License",
357 "\\bjabber\\s*open\\s*source\\s*license\\b",
365 "http://www.opensource.org/licenses/plan9.php",
366 "Lucent Public License (Plan9)",
367 "\\blucent\\s*public\\s*license[\\s(]*plan9",
375 "http://www.opensource.org/licenses/lucent1.02.php",
376 "Lucent Public License Version 1.02",
377 "\\blucent\\s*public\\s*license\\s*(version)?\\s+1",
385 "http://www.opensource.org/licenses/mit-license.php",
387 "(\\bmit\\s*license\\b)|(\\bMIT\\/X11\\s*licensed?\\b)",
395 "http://www.opensource.org/licenses/mitrepl.php",
396 "MITRE Collaborative Virtual Workspace License (CVW License)",
397 "\\bmitre\\s*collaborative\\s*virtual\\s*workspace\\s*license\\b",
405 "http://www.opensource.org/licenses/motosoto.php",
407 "\\bmotosoto\\s*license\\b",
415 "http://www.opensource.org/licenses/mozilla1.0.php",
416 "Mozilla Public License 1.0 (MPL)",
417 "\\bmozilla\\s*public\\s*license\\b",
424 LIC_MOZILLA_PUBLIC11,
425 "http://www.opensource.org/licenses/mozilla1.1.php",
426 "Mozilla Public License 1.1 (MPL)",
427 "\\bmozilla\\s*public\\s*license 1\\.1\\b",
435 "http://www.opensource.org/licenses/nasa1.3.php",
436 "NASA Open Source Agreement 1.3",
437 "\\bnasa\\s*open\\s*source\\s*agreement\\b",
445 "http://www.opensource.org/licenses/naumen.php",
446 "Naumen Public License",
447 "\\bnaumen\\s*public\\s*license\\b",
455 "http://www.opensource.org/licenses/nethack.php",
456 "Nethack General Public License",
457 "\\bnethack\\s*general\\s*public\\s*license\\b",
464 LIC_NOKIA_OPEN_SOURCE,
465 "http://www.opensource.org/licenses/nokia.php",
466 "Nokia Open Source License",
467 "\\bnokia\\s*open\\s*source\\s*license\\b",
475 "http://www.opensource.org/licenses/oclc2.php",
476 "OCLC Research Public License 2.0",
477 "\\boclc\\s*research\\s*public\\s*license\\b",
485 "http://www.opensource.org/licenses/opengroup.php",
486 "Open Group Test Suite License",
487 "\\bopen\\s*group\\s*test\\s*suite\\s*license\\b",
495 "http://www.opensource.org/licenses/osl-3.0.php",
496 "Open Software License",
497 "\\bopen\\s*software\\s*license\\b",
505 "http://www.opensource.org/licenses/php.php",
507 "\\bphp\\s*license\\b",
515 "http://www.opensource.org/licenses/pythonpl.php",
517 "\\bpython\\s*license\\b",
524 LIC_PYTHON_SOFTWARE_FOUNDATION,
525 "http://www.opensource.org/licenses/PythonSoftFoundation.php",
526 "Python Software Foundation License",
527 "\\bpython\\s*software\\s*foundation\\s*license\\b",
535 "http://www.opensource.org/licenses/qtpl.php",
536 "Qt Public License (QPL)",
537 "\\bqt\\s*public\\s*license\\b",
544 LIC_REALNETWORKS_PUBLIC_SOURCE,
545 "http://www.opensource.org/licenses/real.php",
546 "RealNetworks Public Source License V1.0",
547 "\\brealnetworks\\s*public\\s*source\\s*license\\b",
554 LIC_RECIPROCAL_PUBLIC,
555 "http://www.opensource.org/licenses/rpl.php",
556 "Reciprocal Public License",
557 "\\breciprocal\\s*public\\s*license\\b",
565 "http://www.opensource.org/licenses/ricohpl.php",
566 "Ricoh Source Code Public License",
567 "\\bricoh\\s*source\\s*code\\s*public\\s*license\\b",
575 "http://www.opensource.org/licenses/sleepycat.php",
577 "\\bsleepycat\\s*license\\b",
585 "http://www.sugarcrm.com/SPL",
586 "SugarCRM Public License 1.1.3",
587 "\\bsugar\\s*public\\s*license\\s*version\\s*1\\.1\\.3\\b",
594 LIC_SUN_INDUSTRY_STANDARDS,
595 "http://www.opensource.org/licenses/sisslpl.php",
596 "Sun Industry Standards Source License (SISSL)",
597 "\\bsun\\s*industry\\s*standards\\s*source\\s*license\\b",
605 "http://www.opensource.org/licenses/sunpublic.php",
606 "Sun Public License",
607 "\\bsun\\s*public\\s*license\\b",
614 LIC_SYBASE_OPEN_WATCOM,
615 "http://www.opensource.org/licenses/sybase.php",
616 "Sybase Open Watcom Public License 1.0",
617 "\\bsybase\\s*open\\s*watcom\\s*public\\s*license\\b",
625 "http://www.opensource.org/licenses/UoI-NCSA.php",
626 "University of Illinois/NCSA Open Source License",
627 "\\buniversity\\s*of\\s*illinois\\/ncsa\\s*open\\s*source\\s*license\\b",
635 "http://www.opensource.org/licenses/vovidapl.php",
636 "Vovida Software License v. 1.0",
637 "\\bvovida\\s*software\\s*license\\b",
645 "http://www.opensource.org/licenses/W3C.php",
647 "\\bw3c\\s*license\\b",
655 "http://www.opensource.org/licenses/wxwindows.php",
656 "wxWindows Library License",
657 "\\bwxwindows\\s*library\\s*license\\b",
665 "http://www.opensource.org/licenses/xnet.php",
667 "\\bx\\.net\\s*license\\b",
675 "http://www.opensource.org/licenses/zpl.php",
676 "Zope Public License",
677 "\\bzope\\s*public\\s*license\\b",
685 "http://www.opensource.org/licenses/zlib-license.php",
686 "zlib/libpng license",
687 "\\bzlib\\/libpng\\s*license\\b",
696 "Apache-ish License",
697 "(\\bapache-style.*license\\b)|(\\bapache-like.*license\\b)",
707 "Copyright\\s.{1,40}All rights reserved.{0,40}Redistribution and use in source and binary forms, with or without.{0,20}modification, are permitted provided that the following conditions.{0,20}\\sare met.{1,40}Redistributions of source code must retain the above copyright\\s.*notice, this list of conditions and the following disclaimer\\.\\s+.*Redistributions in binary form must reproduce the above.*copyright\\s+.{0,10}notice, this list of conditions and the following.*disclaimer in the\\s+.*documentation.*(The (name|names) of the (author|contributors) may not|Neither the name of the).*be used to endorse or promote\\s+.*products\\s+.*derived\\s+.*from this software without specific prior written\\s+.*permission.*HOWEVER\\s+.*CAUSED AND ON ANY.*THEORY OF LIABILITY, WHETHER IN CONTRACT",
709 "The Regents of the University of California",
716 "BSD-ish (2 clause) License",
717 "Copyright\\s.{1,60}All rights reserved.{1,40}Redistribution and use in source and binary forms, with or without.{0,20}modification, are permitted provided that the following conditions.{0,20}\\sare met.{0,20}\\s{1,20}.{0,20}Redistributions of source code must retain the above copyright\\s+.*notice, this list of conditions and the following disclaimer.\\s+.*Redistributions in binary form must reproduce the above copyright\\s+.*notice, this list of conditions and the following disclaimer in the\\s+.*documentation and\\/or other materials provided with the distribution\\.\\s+.*HOWEVER CAUSED AND ON ANY.*THEORY OF LIABILITY, WHETHER IN CONTRACT",
719 "(The Regents of the University of California)|(used to endorse or promote\\s+.*products\\s+.*prior\\s+.*written\\s+.*permission\\.)",
726 "WTF Public License",
727 "(\\bwtfpl\\b)|(\\bwtf\\s*public\\s*license\\b)|(\\b(do\\s*)?what\\s*the\\s*\\fuck\\s*public\\s*license\\b)",
733 { NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL },
735 int license_map_length = 0; // will be set dynamically
737 /** Compiles the regular expressions defined in license_map. */
738 void compile_regexps() {
739 if (license_map_length == 0)
744 for (i = 0; i < license_map_length; i++) {
745 License *l = &license_map[i];
749 if (flags & PCRE_MULTILINE)
750 flags |= PCRE_DOTALL;
751 l->regexp = pcre_compile(l->re, flags, &err, &erroffset, NULL);
754 flags = l->exclude_re_flags;
755 if (flags & PCRE_MULTILINE)
756 flags |= PCRE_DOTALL;
757 l->exclude_regexp = pcre_compile(l->exclude_re, flags, &err, &erroffset,
764 * Overrides a less-specific license l with a more-specific one if the latter
767 #define OVERRIDE_LICENSE(l, with) { \
768 if (strcmp(license_map[i].name, l) == 0) { \
769 for (j = 0; j < license_map_length; j++) \
770 if (potential_licenses_s[j] > -1 && \
771 strcmp(license_map[j].name, with) == 0) { \
778 LicenseList *ohcount_detect_license(SourceFile *sourcefile) {
779 LicenseList *list = ohcount_license_list_new();
781 // Get the size of this map and compile the REs. Only runs once.
782 if (license_map_length == 0) {
783 while (license_map[license_map_length].name) license_map_length++;
787 ohcount_sourcefile_parse(sourcefile);
791 int ovector[30]; // recommended by PCRE
792 ParsedLanguageList *iter_language;
793 iter_language = ohcount_sourcefile_get_parsed_language_list(sourcefile)->head;
795 int potential_licenses_s[license_map_length];
796 int potential_licenses_e[license_map_length];
798 while (iter_language) {
799 // Before looking for licenses, strip whitespace and newlines
800 p = iter_language->pl->comments;
801 int buffer_len = p ? strlen(p) : 0;
802 char *p_max = p + buffer_len;
804 char *buffer = malloc(buffer_len+1);
805 if (buffer == NULL) {
806 fprintf(stderr, "out of memory in ohcount_detect_license");
810 char *q_max = buffer + buffer_len + 1;
812 while (p < p_max && q < q_max) {
813 // Strip leading whitespace and punctuation.
814 while (*p == ' ' || *p == '\t' || ispunct(*p)) p++;
815 // Copy line contents.
816 while (p < p_max && *p != '\r' && *p != '\n' && q < q_max)
818 // Strip newline characters.
819 while (*p == '\r' || *p == '\n') p++;
820 // Add a trailing space.
821 if (q < q_max) *q++ = ' ';
823 if (q < q_max) *q = '\0';
825 for (j = 0; j < license_map_length; j++) {
826 potential_licenses_s[j] = -1;
827 potential_licenses_e[j] = -1;
828 if (pcre_exec(license_map[j].regexp, NULL, buffer, q - buffer, 0, 0,
830 int m0 = ovector[0], m1 = ovector[1];
831 // Exclude terms that may not exist in the license.
832 if (license_map[j].exclude_re &&
833 pcre_exec(license_map[j].exclude_regexp, NULL, buffer + m0, m1 - m0,
834 0, 0, ovector, 30) >= 0)
836 potential_licenses_s[j] = m0;
837 potential_licenses_e[j] = m1;
838 for (k = 0; k < j; k++) {
839 // If this matched license is completely contained inside another one,
840 // do not include it.
841 if ((potential_licenses_s[k] < m0 && potential_licenses_e[k] >= m1) ||
842 (potential_licenses_s[k] <= m0 && potential_licenses_e[k] > m1)) {
843 potential_licenses_s[j] = -1;
844 potential_licenses_e[j] = -1;
846 // If this matched license completely contains another one, do not
847 // include the latter.
848 if ((m0 < potential_licenses_s[k] && m1 >= potential_licenses_e[k]) ||
849 (m0 <= potential_licenses_s[k] && m1 > potential_licenses_e[k])) {
850 potential_licenses_s[k] = -1;
851 potential_licenses_e[k] = -1;
856 iter_language = iter_language->next;
859 // Create the list of licenses from potential licenses.
860 for (i = 0; i < license_map_length; i++) {
861 if (potential_licenses_s[i] > -1) {
863 OVERRIDE_LICENSE(LIC_GPL, LIC_GPL3);
864 OVERRIDE_LICENSE(LIC_GPL, LIC_GPL3_OR_LATER);
865 OVERRIDE_LICENSE(LIC_GPL3, LIC_GPL3_OR_LATER);
866 OVERRIDE_LICENSE(LIC_BSD_2CLAUSE_ISH, LIC_BSD_ISH);
868 if (list->head == NULL) { // empty list
871 list->head->lic = &license_map[i];
874 LicenseList *item = ohcount_license_list_new();
875 item->lic = &license_map[i];
876 list->tail->next = item;
887 LicenseList *ohcount_license_list_new() {
888 LicenseList *list = malloc(sizeof(LicenseList));
896 void ohcount_license_list_free(LicenseList *list) {
898 LicenseList *iter = list->head;
900 LicenseList *next = iter->next;