9bfa1d2b8d57b3bb005fccc653ba4d209cebf5c8
[ccan] / tools / ccanlint / licenses.c
1 #include "licenses.h"
2 #include "ccanlint.h"
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5
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
75 const char *get_ccan_simplified(struct ccan_file *f)
76 {
77         if (!f->simplified) {
78                 unsigned int i, j;
79
80                 /* Simplify for easy matching: only alnum and single spaces. */
81                 f->simplified = talloc_strdup(f, get_ccan_file_contents(f));
82                 for (i = 0, j = 0; f->simplified[i]; i++) {
83                         if (cisupper(f->simplified[i]))
84                                 f->simplified[j++] = tolower(f->simplified[i]);
85                         else if (cislower(f->simplified[i]))
86                                 f->simplified[j++] = f->simplified[i];
87                         else if (cisdigit(f->simplified[i]))
88                                 f->simplified[j++] = f->simplified[i];
89                         else if (cisspace(f->simplified[i])) {
90                                 if (j != 0 && f->simplified[j-1] != ' ')
91                                         f->simplified[j++] = ' ';
92                         }
93                 }
94                 f->simplified[j] = '\0';
95         }
96         return f->simplified;
97 }
98
99 bool find_boilerplate(struct ccan_file *f, enum license license)
100 {
101         unsigned int i;
102
103         for (i = 0; i < NUM_CLAUSES; i++) {
104                 if (!licenses[license].clause[i])
105                         break;
106
107                 if (!strstr(get_ccan_simplified(f),
108                             licenses[license].clause[i])) {
109                         return false;
110                 }
111         }
112         return true;
113 }