tools: fix up warnings with -Wwrite-strings.
[ccan] / tools / ccanlint / tests / examples_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/cast/cast.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 /* Creates and adds an example file. */
19 static char *add_example(struct manifest *m, struct ccan_file *source,
20                          bool keep,
21                          struct doc_section *example)
22 {
23         char *name;
24         unsigned int i;
25         int fd;
26         struct ccan_file *f;
27
28         name = talloc_asprintf(m, "%s/example-%s-%s.c",
29                                talloc_dirname(m,
30                                               source->fullname),
31                                source->name,
32                                example->function);
33         /* example->function == 'struct foo' */
34         while (strchr(name, ' '))
35                 *strchr(name, ' ') = '_';
36
37         name = maybe_temp_file(m, ".c", keep, name);
38         f = new_ccan_file(m, talloc_dirname(m, name), talloc_basename(m, name));
39         talloc_steal(f, name);
40         list_add_tail(&m->examples, &f->list);
41
42         fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
43         if (fd < 0)
44                 return talloc_asprintf(m, "Creating temporary file %s: %s",
45                                        f->fullname, strerror(errno));
46
47         for (i = 0; i < example->num_lines; i++) {
48                 if (write(fd, example->lines[i], strlen(example->lines[i]))
49                     != strlen(example->lines[i])
50                     || write(fd, "\n", 1) != 1) {
51                         close(fd);
52                         return cast_const(char *,
53                                           "Failure writing to temporary file");
54                 }
55         }
56         close(fd);
57         return NULL;
58 }
59
60 /* FIXME: We should have one example per function in header. */
61 static void extract_examples(struct manifest *m,
62                              bool keep,
63                              unsigned int *timeleft,
64                              struct score *score)
65 {
66         struct ccan_file *f, *mainh = NULL; /* gcc complains uninitialized */
67         struct doc_section *d;
68         bool have_info_example = false, have_header_example = false;
69
70         score->total = 2;
71         list_for_each(get_ccan_file_docs(m->info_file), d, list) {
72                 if (streq(d->type, "example")) {
73                         score->error = add_example(m, m->info_file, keep, d);
74                         if (score->error)
75                                 return;
76                         have_info_example = true;
77                 }
78         }
79
80         /* Check main header. */
81         list_for_each(&m->h_files, f, list) {
82                 if (!strstarts(f->name, m->basename)
83                     || strlen(f->name) != strlen(m->basename) + 2)
84                         continue;
85
86                 mainh = f;
87                 list_for_each(get_ccan_file_docs(f), d, list) {
88                         if (streq(d->type, "example")) {
89                                 score->error = add_example(m, f, keep, d);
90                                 if (score->error)
91                                         return;
92                                 have_header_example = true;
93                         }
94                 }
95         }
96
97         if (have_info_example && have_header_example) {
98                 score->score = score->total;
99                 score->pass = true;
100                 return;
101         }
102
103         if (!have_info_example)
104                 score_file_error(score, m->info_file, 0, "No Example: section");
105         if (!have_header_example)
106                 score_file_error(score, mainh, 0, "No Example: section");
107
108         score->score = have_info_example + have_header_example;
109         /* We pass if we find any example. */
110         score->pass = score->score != 0;
111 }
112
113 struct ccanlint examples_exist = {
114         .key = "examples_exist",
115         .name = "_info and main header file have Example: sections",
116         .check = extract_examples,
117         .needs = "info_exists"
118 };
119
120 REGISTER_TEST(examples_exist);