Commit 1fe7f55b authored by Rusty Russell's avatar Rusty Russell

compiler, list, noerr, sparse_bsearch, str, str_talloc, stringmap,...

compiler, list, noerr, sparse_bsearch, str, str_talloc, stringmap, talloc_link, tdb, tdb2, typesafe_cb: fix examples

Phew, now they call compile!
parent ab4a6fd9
...@@ -146,7 +146,7 @@ ...@@ -146,7 +146,7 @@
* *
* Example: * Example:
* // buf param may be freed by this; need return value! * // buf param may be freed by this; need return value!
* static char *WARN_UNUSED_RESULT enlarge(const char *buf, unsigned *size) * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
* { * {
* return realloc(buf, (*size) *= 2); * return realloc(buf, (*size) *= 2);
* } * }
......
...@@ -56,7 +56,7 @@ struct list_head ...@@ -56,7 +56,7 @@ struct list_head
* { * {
* struct child *c; * struct child *c;
* *
* printf("%s (%u children):\n", p->name, parent->num_children); * printf("%s (%u children):\n", p->name, p->num_children);
* list_check(&p->children, "bad child list"); * list_check(&p->children, "bad child list");
* list_for_each(&p->children, c, list) * list_for_each(&p->children, c, list)
* printf(" -> %s\n", c->name); * printf(" -> %s\n", c->name);
...@@ -70,11 +70,27 @@ struct list_head *list_check(const struct list_head *h, const char *abortstr); ...@@ -70,11 +70,27 @@ struct list_head *list_check(const struct list_head *h, const char *abortstr);
#define debug_list(h) (h) #define debug_list(h) (h)
#endif #endif
/**
* LIST_HEAD - define and initalize an empty list_head
* @name: the name of the list.
*
* The LIST_HEAD macro defines a list_head and initializes it to an empty
* list. It can be prepended by "static" to define a static list_head.
*
* Example:
* static LIST_HEAD(my_global_list);
*/
#define LIST_HEAD(name) \
struct list_head name = { { &name.n, &name.n } }
/** /**
* list_head_init - initialize a list_head * list_head_init - initialize a list_head
* @h: the list_head to set to the empty list * @h: the list_head to set to the empty list
* *
* Example: * Example:
* ...
* struct parent *parent = malloc(sizeof(*parent));
*
* list_head_init(&parent->children); * list_head_init(&parent->children);
* parent->num_children = 0; * parent->num_children = 0;
*/ */
...@@ -83,23 +99,6 @@ static inline void list_head_init(struct list_head *h) ...@@ -83,23 +99,6 @@ static inline void list_head_init(struct list_head *h)
h->n.next = h->n.prev = &h->n; h->n.next = h->n.prev = &h->n;
} }
/**
* LIST_HEAD - define and initalized empty list_head
* @name: the name of the list.
*
* The LIST_HEAD macro defines a list_head and initializes it to an empty
* list. It can be prepended by "static" to define a static list_head.
*
* Example:
* // Header:
* extern struct list_head my_list;
*
* // C file:
* LIST_HEAD(my_list);
*/
#define LIST_HEAD(name) \
struct list_head name = { { &name.n, &name.n } }
/** /**
* list_add - add an entry at the start of a linked list. * list_add - add an entry at the start of a linked list.
* @h: the list_head to add the node to * @h: the list_head to add the node to
...@@ -107,6 +106,8 @@ static inline void list_head_init(struct list_head *h) ...@@ -107,6 +106,8 @@ static inline void list_head_init(struct list_head *h)
* *
* The list_node does not need to be initialized; it will be overwritten. * The list_node does not need to be initialized; it will be overwritten.
* Example: * Example:
* struct child *child;
*
* list_add(&parent->children, &child->list); * list_add(&parent->children, &child->list);
* parent->num_children++; * parent->num_children++;
*/ */
...@@ -179,9 +180,11 @@ static inline bool list_empty(const struct list_head *h) ...@@ -179,9 +180,11 @@ static inline bool list_empty(const struct list_head *h)
* @member: the list_node member of the type * @member: the list_node member of the type
* *
* Example: * Example:
* struct child *c;
* // First list entry is children.next; convert back to child. * // First list entry is children.next; convert back to child.
* c = list_entry(parent->children.next, struct child, list); * child = list_entry(parent->children.n.next, struct child, list);
*
* See Also:
* list_top(), list_for_each()
*/ */
#define list_entry(n, type, member) container_of(n, type, member) #define list_entry(n, type, member) container_of(n, type, member)
...@@ -225,9 +228,8 @@ static inline bool list_empty(const struct list_head *h) ...@@ -225,9 +228,8 @@ static inline bool list_empty(const struct list_head *h)
* a for loop, so you can break and continue as normal. * a for loop, so you can break and continue as normal.
* *
* Example: * Example:
* struct child *c; * list_for_each(&parent->children, child, list)
* list_for_each(&parent->children, c, list) * printf("Name: %s\n", child->name);
* printf("Name: %s\n", c->name);
*/ */
#define list_for_each(h, i, member) \ #define list_for_each(h, i, member) \
for (i = container_of_var(debug_list(h)->n.next, i, member); \ for (i = container_of_var(debug_list(h)->n.next, i, member); \
...@@ -246,9 +248,9 @@ static inline bool list_empty(const struct list_head *h) ...@@ -246,9 +248,9 @@ static inline bool list_empty(const struct list_head *h)
* @nxt is used to hold the next element, so you can delete @i from the list. * @nxt is used to hold the next element, so you can delete @i from the list.
* *
* Example: * Example:
* struct child *c, *n; * struct child *next;
* list_for_each_safe(&parent->children, c, n, list) { * list_for_each_safe(&parent->children, child, next, list) {
* list_del(&c->list); * list_del(&child->list);
* parent->num_children--; * parent->num_children--;
* } * }
*/ */
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
* #include <errno.h> * #include <errno.h>
* #include <ccan/noerr/noerr.h> * #include <ccan/noerr/noerr.h>
* *
* bool write_string_to_file(const char *file, const char *string) * static bool write_string_to_file(const char *file, const char *string)
* { * {
* int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600); * int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);
* if (fd < 0) * if (fd < 0)
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
* static unsigned int values[] = { 1, 7, 11, 1235, 99999 }; * static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
* *
* // Return true if this value is in set, and remove it. * // Return true if this value is in set, and remove it.
* bool remove_from_values(unsigned int val) * static bool remove_from_values(unsigned int val)
* { * {
* unsigned int *p; * unsigned int *p;
* // We use 5 here, but ccan/array_size.h is better! * // We use 5 here, but ccan/array_size.h is better!
......
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
* static unsigned int values[] = { 1, 7, 11, 1235, 99999 }; * static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
* *
* // Return true if this value is in set, and remove it. * // Return true if this value is in set, and remove it.
* bool remove_from_values(unsigned int val) * static bool remove_from_values(unsigned int val)
* { * {
* unsigned int *p; * unsigned int *p;
* p = sparse_bsearch(&val, values, 5, val_cmp, val_valid); * p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* This macro is arguably more readable than "!strcmp(a, b)". * This macro is arguably more readable than "!strcmp(a, b)".
* *
* Example: * Example:
* if (streq(str, "")) * if (streq(somestring, ""))
* printf("String is empty!\n"); * printf("String is empty!\n");
*/ */
#define streq(a,b) (strcmp((a),(b)) == 0) #define streq(a,b) (strcmp((a),(b)) == 0)
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
* @prefix: prefix to look for at start of str * @prefix: prefix to look for at start of str
* *
* Example: * Example:
* if (strstarts(str, "foo")) * if (strstarts(somestring, "foo"))
* printf("String %s begins with 'foo'!\n", str); * printf("String %s begins with 'foo'!\n", somestring);
*/ */
#define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0) #define strstarts(str,prefix) (strncmp((str),(prefix),strlen(prefix)) == 0)
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
* @postfix: postfix to look for at end of str * @postfix: postfix to look for at end of str
* *
* Example: * Example:
* if (strends(str, "foo")) * if (strends(somestring, "foo"))
* printf("String %s end with 'foo'!\n", str); * printf("String %s end with 'foo'!\n", somestring);
*/ */
static inline bool strends(const char *str, const char *postfix) static inline bool strends(const char *str, const char *postfix)
{ {
......
...@@ -20,7 +20,10 @@ ...@@ -20,7 +20,10 @@
* @nump to find the array length. * @nump to find the array length.
* *
* Example: * Example:
* unsigned int count_long_lines(const char *text) * #include <ccan/talloc/talloc.h>
* #include <ccan/str_talloc/str_talloc.h>
* ...
* static unsigned int count_long_lines(const char *string)
* { * {
* char **lines; * char **lines;
* unsigned int i, long_lines = 0; * unsigned int i, long_lines = 0;
...@@ -49,10 +52,9 @@ char **strsplit(const void *ctx, const char *string, const char *delims, ...@@ -49,10 +52,9 @@ char **strsplit(const void *ctx, const char *string, const char *delims,
* *
* Example: * Example:
* // Append the string "--EOL" to each line. * // Append the string "--EOL" to each line.
* char *append_to_all_lines(const char *string) * static char *append_to_all_lines(const char *string)
* { * {
* char **lines, *ret; * char **lines, *ret;
* unsigned int i, num, newnum;
* *
* lines = strsplit(NULL, string, "\n", NULL); * lines = strsplit(NULL, string, "\n", NULL);
* ret = strjoin(NULL, lines, "-- EOL\n"); * ret = strjoin(NULL, lines, "-- EOL\n");
......
...@@ -12,22 +12,22 @@ ...@@ -12,22 +12,22 @@
* *
* Example: * Example:
* *
* #include <ccan/stringmap/stringmap.h> * #include <ccan/stringmap/stringmap.h>
* *
* static const char *get_string(void) { * static const char *get_string(void) {
* static char buffer[4096]; * static char buffer[4096];
* char *tail; * char *tail;
* if (!fgets(buffer, sizeof(buffer), stdin)) * if (!fgets(buffer, sizeof(buffer), stdin))
* return NULL; * return NULL;
* tail = strchr(buffer, 0); * tail = strchr(buffer, 0);
* if (tail>buffer && tail[-1]=='\n') * if (tail>buffer && tail[-1]=='\n')
* *--tail = 0; * *--tail = 0;
* if (!*buffer) * if (!*buffer)
* return NULL; * return NULL;
* return buffer; * return buffer;
* } * }
* *
* int main(void) { * int main(void) {
* stringmap(int) map = stringmap_new(NULL); * stringmap(int) map = stringmap_new(NULL);
* const char *string; * const char *string;
* *
...@@ -49,8 +49,8 @@ ...@@ -49,8 +49,8 @@
* return 0; * return 0;
* } * }
* *
* Authors: Joey Adams, Anders Magnusson * Authors: Joey Adams, Anders Magnusson
* License: BSD * License: BSD
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
* static struct upcache *cache; * static struct upcache *cache;
* static unsigned int cache_hits = 0; * static unsigned int cache_hits = 0;
* #define CACHE_SIZE 4 * #define CACHE_SIZE 4
* void init_upcase(void) * static void init_upcase(void)
* { * {
* cache = talloc_zero_array(NULL, struct upcache, CACHE_SIZE); * cache = talloc_zero_array(NULL, struct upcache, CACHE_SIZE);
* } * }
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
* } * }
* *
* // If you want to keep the result, talloc_link it. * // If you want to keep the result, talloc_link it.
* const char *get_upcase(const char *str) * static const char *get_upcase(const char *str)
* { * {
* struct upcache *uc = lookup_upcase(str); * struct upcache *uc = lookup_upcase(str);
* if (!uc) * if (!uc)
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
* return uc->upstr; * return uc->upstr;
* } * }
* *
* void exit_upcase(void) * static void exit_upcase(void)
* { * {
* talloc_free(cache); * talloc_free(cache);
* printf("Cache hits: %u\n", cache_hits); * printf("Cache hits: %u\n", cache_hits);
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
* #include <err.h> * #include <err.h>
* #include <stdio.h> * #include <stdio.h>
* *
* static void usage(void) * static void usage(const char *argv0)
* { * {
* errx(1, "Usage: %s fetch <dbfile> <key>\n" * errx(1, "Usage: %s fetch <dbfile> <key>\n"
* "OR %s store <dbfile> <key> <data>"); * "OR %s store <dbfile> <key> <data>", argv0, argv0);
* } * }
* *
* int main(int argc, char *argv[]) * int main(int argc, char *argv[])
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
* TDB_DATA key, value; * TDB_DATA key, value;
* *
* if (argc < 4) * if (argc < 4)
* usage(); * usage(argv[0]);
* *
* tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR, * tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR,
* 0600); * 0600);
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
* *
* if (streq(argv[1], "fetch")) { * if (streq(argv[1], "fetch")) {
* if (argc != 4) * if (argc != 4)
* usage(); * usage(argv[0]);
* value = tdb_fetch(tdb, key); * value = tdb_fetch(tdb, key);
* if (!value.dptr) * if (!value.dptr)
* errx(1, "fetch %s: %s", * errx(1, "fetch %s: %s",
...@@ -47,14 +47,14 @@ ...@@ -47,14 +47,14 @@
* free(value.dptr); * free(value.dptr);
* } else if (streq(argv[1], "store")) { * } else if (streq(argv[1], "store")) {
* if (argc != 5) * if (argc != 5)
* usage(); * usage(argv[0]);
* value.dptr = (void *)argv[4]; * value.dptr = (void *)argv[4];
* value.dsize = strlen(argv[4]); * value.dsize = strlen(argv[4]);
* if (tdb_store(tdb, key, value, 0) != 0) * if (tdb_store(tdb, key, value, 0) != 0)
* errx(1, "store %s: %s", * errx(1, "store %s: %s",
* argv[3], tdb_errorstr(tdb)); * argv[3], tdb_errorstr(tdb));
* } else * } else
* usage(); * usage(argv[0]);
* *
* return 0; * return 0;
* } * }
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
* #include <err.h> * #include <err.h>
* #include <stdio.h> * #include <stdio.h>
* *
* static void usage(void) * static void usage(const char *argv0)
* { * {
* errx(1, "Usage: %s fetch <dbfile> <key>\n" * errx(1, "Usage: %s fetch <dbfile> <key>\n"
* "OR %s store <dbfile> <key> <data>"); * "OR %s store <dbfile> <key> <data>", argv0, argv0);
* } * }
* *
* int main(int argc, char *argv[]) * int main(int argc, char *argv[])
...@@ -26,10 +26,9 @@ ...@@ -26,10 +26,9 @@
* TDB_DATA key, value; * TDB_DATA key, value;
* *
* if (argc < 4) * if (argc < 4)
* usage(); * usage(argv[0]);
* *
* tdb = tdb_open(argv[2], 1024, TDB_DEFAULT, O_CREAT|O_RDWR, * tdb = tdb_open(argv[2], TDB_DEFAULT, O_CREAT|O_RDWR,0600, NULL);
* 0600);
* if (!tdb) * if (!tdb)
* err(1, "Opening %s", argv[2]); * err(1, "Opening %s", argv[2]);
* *
...@@ -38,7 +37,7 @@ ...@@ -38,7 +37,7 @@
* *
* if (streq(argv[1], "fetch")) { * if (streq(argv[1], "fetch")) {
* if (argc != 4) * if (argc != 4)
* usage(); * usage(argv[0]);
* value = tdb_fetch(tdb, key); * value = tdb_fetch(tdb, key);
* if (!value.dptr) * if (!value.dptr)
* errx(1, "fetch %s: %s", * errx(1, "fetch %s: %s",
...@@ -47,14 +46,14 @@ ...@@ -47,14 +46,14 @@
* free(value.dptr); * free(value.dptr);
* } else if (streq(argv[1], "store")) { * } else if (streq(argv[1], "store")) {
* if (argc != 5) * if (argc != 5)
* usage(); * usage(argv[0]);
* value.dptr = (void *)argv[4]; * value.dptr = (void *)argv[4];
* value.dsize = strlen(argv[4]); * value.dsize = strlen(argv[4]);
* if (tdb_store(tdb, key, value, 0) != 0) * if (tdb_store(tdb, key, value, 0) != 0)
* errx(1, "store %s: %s", * errx(1, "store %s: %s",
* argv[3], tdb_errorstr(tdb)); * argv[3], tdb_errorstr(tdb));
* } else * } else
* usage(); * usage(argv[0]);
* *
* return 0; * return 0;
* } * }
......
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
* *
* // Define several silly callbacks. Note they don't use void *! * // Define several silly callbacks. Note they don't use void *!
* #define DEF_CALLBACK(name, op) \ * #define DEF_CALLBACK(name, op) \
* static int name(int val, const int *arg)\ * static int name(int val, int *arg) \
* { \ * { \
* printf("%s", #op); \ * printf("%s", #op); \
* return val op *arg; \ * return val op *arg; \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment