Another Joey fix:
[ccan] / tools / ccanlint / has_info_documentation.c
1 #include "ccanlint.h"
2 #include "../doc_extract.h"
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <err.h>
12 #include <ccan/str/str.h>
13 #include <ccan/talloc/talloc.h>
14 #include <ccan/noerr/noerr.h>
15 #include <ccan/grab_file/grab_file.h>
16
17 struct info_docs
18 {
19         bool summary;
20         bool description;
21         bool example;
22 };
23
24 static void *check_has_info_documentation(struct manifest *m)
25 {
26         struct list_head *infodocs = get_ccan_file_docs(m->info_file);
27         struct doc_section *d;
28         struct info_docs id = { false, false, false };
29
30         list_for_each(infodocs, d, list) {
31                 if (!streq(d->function, m->basename))
32                         continue;
33                 if (streq(d->type, "summary"))
34                         id.summary = true;
35                 if (streq(d->type, "description"))
36                         id.description = true;
37                 if (streq(d->type, "example"))
38                         id.example = true;
39         }
40
41         if (id.summary && id.description && id.example)
42                 return NULL;
43         return talloc_memdup(m, &id, sizeof(id));
44 }
45
46 /* This is defined below. */
47 extern struct ccanlint has_info_documentation;
48
49 static void create_info_template_doc(struct manifest *m, void *check_result)
50 {
51         int fd = open("_info.c.new", O_WRONLY|O_CREAT|O_EXCL, 0666);
52         FILE *new;
53         char *oldcontents;
54
55         if (fd < 0 || !(new = fdopen(fd, "w")))
56                 err(1, "Creating _info.c.new to insert documentation");
57
58         if (fprintf(new,
59                     "/**\n"
60                     " * %s - [[ONE LINE DESCRIPTION HERE]]\n"
61                     " *\n"
62                     " * Paragraphs why %s exists and where to use it.\n"
63                     " *\n"
64                     " * Followed by an Example: section with a standalone\n"
65                     " * (trivial and usually useless) program\n"
66                     " */\n", m->basename, m->basename) < 0) {
67                 unlink_noerr("_info.c.new");
68                 err(1, "Writing to _info.c.new to insert documentation");
69         }
70
71         oldcontents = grab_file(m, "_info.c", NULL);
72         if (!oldcontents) {
73                 unlink_noerr("_info.c.new");
74                 err(1, "Reading _info.c");
75         }
76         if (fprintf(new, "%s", oldcontents) < 0) {
77                 unlink_noerr("_info.c.new");
78                 err(1, "Appending _info.c to _info.c.new");
79         }
80         if (fclose(new) != 0) {
81                 unlink_noerr("_info.c.new");
82                 err(1, "Closing _info.c.new");
83         }
84         if (rename("_info.c.new", "_info.c") != 0) {
85                 unlink_noerr("_info.c.new");
86                 err(1, "Renaming _info.c.new to _info.c");
87         }
88 }
89
90 static const char *describe_has_info_documentation(struct manifest *m,
91                                                    void *check_result)
92 {
93         struct info_docs *id = check_result;
94         char *reason = talloc_strdup(m, "");
95
96         if (!id->summary) {
97                 has_info_documentation.handle = create_info_template_doc;
98                 reason = talloc_asprintf_append(reason,
99                 "Your _info.c has no module documentation.\n\n"
100                 "CCAN modules use /**-style comments for documentation: the\n"
101                 "overall documentation belongs in the _info.c metafile.\n");
102         }
103         if (!id->description)
104                 reason = talloc_asprintf_append(reason,
105                 "Your _info.c has no module description.\n\n"
106                 "The lines after the first summary line in the _info.c file\n"
107                 "documentation should describe the purpose and use of the\n"
108                 "overall package\n");
109         if (!id->example)
110                 reason = talloc_asprintf_append(reason,
111                 "Your _info.c has no module example.\n\n"
112                 "There should be an Example: section of the _info.c documentation\n"
113                 "which provides a concise toy program which uses your module\n");
114         return reason;
115 }
116
117 static unsigned int has_info_documentation_score(struct manifest *m,
118                                                  void *check_result)
119 {
120         struct info_docs *id = check_result;
121         return id->summary + id->description + id->example;
122 }
123
124 struct ccanlint has_info_documentation = {
125         .name = "Documentation in _info.c",
126         .total_score = 3,
127         .score = has_info_documentation_score,
128         .check = check_has_info_documentation,
129         .describe = describe_has_info_documentation,
130 };