ccanlint: handle nested modules when mentioned in examples.
[ccan] / ccan / tal / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * tal - compact tree allocator routines (inspired by talloc)
7  *
8  * Tal is a hierarchical allocator; any pointer allocated by tal can
9  * become the parent of another allocation.  When you free that parent,
10  * the children (and grandchildren, etc) are automatically freed.
11  *
12  * This allows you to build complex objects based on their lifetimes, eg:
13  *
14  *  struct foo *X = tal(NULL, struct foo);
15  *  X->name = tal_strdup(X, "foo");
16  *
17  * and the pointer X->name would be a "child" of the tal context "X";
18  * tal_free(X->name) would free X->name as expected, by tal_free(X) would
19  * free X and X->name.
20  *
21  * With an overhead of approximately 2.1 pointers per object (vs. talloc's
22  * 12 pointers), it's a little slower in freeing single objects, though
23  * comparable for allocation and freeing whole object trees).  It does not
24  * support talloc's references or failing destructors.
25  *
26  * Example:
27  *      #include <stdio.h>
28  *      #include <stdarg.h>
29  *      #include <err.h>
30  *      #include <ccan/talloc/talloc.h>
31  *
32  *      // A structure containing a popened command.
33  *      struct command {
34  *              FILE *f;
35  *              const char *command;
36  *      };
37  *
38  *      // When struct command is freed, we also want to pclose pipe.
39  *      static void close_cmd(struct command *cmd)
40  *      {
41  *              pclose(cmd->f);
42  *      }
43  *
44  *      // This function opens a writable pipe to the given command.
45  *      static struct command *open_output_cmd(const tal_t *ctx,
46  *                                             const char *fmt, ...)
47  *      {
48  *              va_list ap;
49  *              struct command *cmd = tal(ctx, struct command);
50  *
51  *              if (!cmd)
52  *                      return NULL;
53  *
54  *              va_start(ap, fmt);
55  *              cmd->command = tal_vasprintf(cmd, fmt, ap);
56  *              va_end(ap);
57  *              if (!cmd->command) {
58  *                      tal_free(cmd);
59  *                      return NULL;
60  *              }
61  *
62  *              cmd->f = popen(cmd->command, "w");
63  *              if (!cmd->f) {
64  *                      tal_free(cmd);
65  *                      return NULL;
66  *              }
67  *              tal_add_destructor(cmd, close_cmd);
68  *              return cmd;
69  *      }
70  *
71  *      int main(int argc, char *argv[])
72  *      {
73  *              struct command *cmd;
74  *
75  *              if (argc != 2)
76  *                      errx(1, "Usage: %s <command>\n", argv[0]);
77  *
78  *              cmd = open_output_cmd(NULL, "%s hello", argv[1]);
79  *              if (!cmd)
80  *                      err(1, "Running '%s hello'", argv[1]);
81  *              fprintf(cmd->f, "This is a test\n");
82  *              tal_free(cmd);
83  *              return 0;
84  *      }
85  *
86  * License: BSD-MIT
87  */
88 int main(int argc, char *argv[])
89 {
90         if (argc != 2)
91                 return 1;
92
93         if (strcmp(argv[1], "depends") == 0) {
94                 printf("ccan/compiler\n");
95                 printf("ccan/hash\n");
96                 printf("ccan/likely\n");
97                 printf("ccan/list\n");
98                 printf("ccan/str\n");
99                 printf("ccan/take\n");
100                 printf("ccan/typesafe_cb\n");
101                 return 0;
102         }
103
104         return 1;
105 }