ccanlint: rename structures to match keys
[ccan] / tools / ccanlint / compulsory_tests / has_info.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <limits.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <err.h>
11 #include <string.h>
12 #include <ccan/noerr/noerr.h>
13 #include <ccan/talloc/talloc.h>
14
15 static void check_has_info(struct manifest *m,
16                            bool keep,
17                            unsigned int *timeleft,
18                            struct score *score)
19 {
20         if (m->info_file) {
21                 score->pass = true;
22                 score->score = score->total;
23         } else {
24                 score->error = "You have no _info file.\n\n"
25         "The file _info contains the metadata for a ccan package: things\n"
26         "like the dependencies, the documentation for the package as a whole\n"
27         "and license information.\n";
28         }
29 }
30
31 static const char template[] = 
32         "#include <string.h>\n"
33         "#include \"config.h\"\n"
34         "\n"
35         "/**\n"
36         " * %s - YOUR-ONE-LINE-DESCRIPTION-HERE\n"
37         " *\n"
38         " * This code ... YOUR-BRIEF-SUMMARY-HERE\n"
39         " *\n"
40         " * Example:\n"
41         " *     FULLY-COMPILABLE-INDENTED-TRIVIAL-BUT-USEFUL-EXAMPLE-HERE\n"
42         " */\n"
43         "int main(int argc, char *argv[])\n"
44         "{\n"
45         "       /* Expect exactly one argument */\n"
46         "       if (argc != 2)\n"
47         "               return 1;\n"
48         "\n"
49         "       if (strcmp(argv[1], \"depends\") == 0) {\n"
50         "               PRINTF-CCAN-PACKAGES-YOU-NEED-ONE-PER-LINE-IF-ANY\n"
51         "               return 0;\n"
52         "       }\n"
53         "\n"
54         "       return 1;\n"
55         "}\n";
56
57 static void create_info_template(struct manifest *m, struct score *score)
58 {
59         FILE *info;
60         const char *filename;
61
62         if (!ask("Should I create a template _info file for you?"))
63                 return;
64
65         filename = talloc_asprintf(m, "%s/%s", m->dir, "_info");
66         info = fopen(filename, "w");
67         if (!info)
68                 err(1, "Trying to create a template _info in %s", filename);
69
70         if (fprintf(info, template, m->basename) < 0) {
71                 unlink_noerr(filename);
72                 err(1, "Writing template into %s", filename);
73         }
74         fclose(info);
75 }
76
77 struct ccanlint info_exists = {
78         .key = "info_exists",
79         .name = "Module has _info file",
80         .check = check_has_info,
81         .handle = create_info_template,
82         .needs = ""
83 };
84
85 REGISTER_TEST(info_exists);