176b5071b87fbbaad5ce94b032fd15055aa84d4e
[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, unsigned int *timeleft)
16 {
17         if (m->info_file)
18                 return NULL;
19         return m;
20 }
21
22 static const char *describe_has_info(struct manifest *m, void *check_result)
23 {
24         return "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 static const char template[] = 
31         "#include <string.h>\n"
32         "#include \"config.h\"\n"
33         "\n"
34         "/**\n"
35         " * %s - YOUR-ONE-LINE-DESCRIPTION-HERE\n"
36         " *\n"
37         " * This code ... YOUR-BRIEF-SUMMARY-HERE\n"
38         " *\n"
39         " * Example:\n"
40         " *     FULLY-COMPILABLE-INDENTED-TRIVIAL-BUT-USEFUL-EXAMPLE-HERE\n"
41         " */\n"
42         "int main(int argc, char *argv[])\n"
43         "{\n"
44         "       /* Expect exactly one argument */\n"
45         "       if (argc != 2)\n"
46         "               return 1;\n"
47         "\n"
48         "       if (strcmp(argv[1], \"depends\") == 0) {\n"
49         "               PRINTF-CCAN-PACKAGES-YOU-NEED-ONE-PER-LINE-IF-ANY\n"
50         "               return 0;\n"
51         "       }\n"
52         "\n"
53         "       return 1;\n"
54         "}\n";
55
56 static void create_info_template(struct manifest *m, void *check_result)
57 {
58         FILE *info;
59         const char *filename;
60
61         if (!ask("Should I create a template _info file for you?"))
62                 return;
63
64         filename = talloc_asprintf(m, "%s/%s", m->dir, "_info");
65         info = fopen(filename, "w");
66         if (!info)
67                 err(1, "Trying to create a template _info in %s", filename);
68
69         if (fprintf(info, template, m->basename) < 0) {
70                 unlink_noerr(filename);
71                 err(1, "Writing template into %s", filename);
72         }
73         fclose(info);
74 }
75
76 struct ccanlint has_info = {
77         .key = "info",
78         .name = "Module has _info file",
79         .check = check_has_info,
80         .describe = describe_has_info,
81         .handle = create_info_template,
82 };
83
84 REGISTER_TEST(has_info, NULL);