From 0d44f16f6738c0776edf60b84f49f0c6f875a8fe Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 17 May 2015 14:40:46 +0200 Subject: style + add timed read example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/daemon-depedencies/announce.c | 3 +- doc/examples/daemon-depedencies/await-ready.c | 9 +++-- doc/examples/daemon-depedencies/await-started.c | 9 +++-- doc/examples/daemon-depedencies/cleanup.c | 3 +- doc/examples/daemon-depedencies/init.c | 9 +++-- doc/examples/daemon-depedencies/require.c | 3 +- doc/examples/daemon-depedencies/start-daemon.c | 3 +- doc/examples/daemon-depedencies/test-daemon.c | 3 +- doc/examples/nonblocking/cleanup.c | 3 +- doc/examples/nonblocking/init.c | 3 +- doc/examples/nonblocking/poll.c | 3 +- doc/examples/nonblocking/write.c | 3 +- doc/examples/telephony-and-music/cleanup.c | 3 +- doc/examples/telephony-and-music/end-call.c | 3 +- doc/examples/telephony-and-music/init.c | 3 +- doc/examples/telephony-and-music/monitor.c | 9 +++-- .../telephony-and-music/receive-or-make-call.c | 3 +- doc/examples/timed/.gitignore | 1 + doc/examples/timed/Makefile | 2 +- doc/examples/timed/README | 5 ++- doc/examples/timed/cleanup.c | 3 +- doc/examples/timed/init.c | 3 +- doc/examples/timed/poll.c | 3 +- doc/examples/timed/read.c | 45 ++++++++++++++++++++++ doc/examples/timed/write.c | 3 +- 25 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 doc/examples/timed/read.c diff --git a/doc/examples/daemon-depedencies/announce.c b/doc/examples/daemon-depedencies/announce.c index 752cc16..d928adb 100644 --- a/doc/examples/daemon-depedencies/announce.c +++ b/doc/examples/daemon-depedencies/announce.c @@ -10,7 +10,8 @@ static char arg[4098]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; if (argc < 3) diff --git a/doc/examples/daemon-depedencies/await-ready.c b/doc/examples/daemon-depedencies/await-ready.c index a787cea..fea2382 100644 --- a/doc/examples/daemon-depedencies/await-ready.c +++ b/doc/examples/daemon-depedencies/await-ready.c @@ -17,7 +17,8 @@ static char *started = NULL; static char msg[BUS_MEMORY_SIZE]; -static void announce_wait(pid_t pid) +static void +announce_wait(pid_t pid) { bus_t bus; int i; @@ -36,7 +37,8 @@ fail: } -static int callback(const char *message, void *user_data) +static int +callback(const char *message, void *user_data) { int i; char *arg2; @@ -80,7 +82,8 @@ static int callback(const char *message, void *user_data) } -int main(int argc_, char *argv_[]) +int +main(int argc_, char *argv_[]) { bus_t bus; int i; diff --git a/doc/examples/daemon-depedencies/await-started.c b/doc/examples/daemon-depedencies/await-started.c index 0408acb..99ba14f 100644 --- a/doc/examples/daemon-depedencies/await-started.c +++ b/doc/examples/daemon-depedencies/await-started.c @@ -17,7 +17,8 @@ static char *started = NULL; static char msg[BUS_MEMORY_SIZE]; -static void announce_wait(pid_t pid) +static void +announce_wait(pid_t pid) { bus_t bus; int i; @@ -36,7 +37,8 @@ fail: } -static int callback(const char *message, void *user_data) +static int +callback(const char *message, void *user_data) { int i; char *arg2; @@ -80,7 +82,8 @@ static int callback(const char *message, void *user_data) } -int main(int argc_, char *argv_[]) +int +main(int argc_, char *argv_[]) { bus_t bus; int i; diff --git a/doc/examples/daemon-depedencies/cleanup.c b/doc/examples/daemon-depedencies/cleanup.c index d3803c3..f9ead6d 100644 --- a/doc/examples/daemon-depedencies/cleanup.c +++ b/doc/examples/daemon-depedencies/cleanup.c @@ -6,7 +6,8 @@ -int main() +int +main() { char *bus_address = getenv("BUS_INIT"); if (!bus_address || !*bus_address) { diff --git a/doc/examples/daemon-depedencies/init.c b/doc/examples/daemon-depedencies/init.c index 09a6abf..b7182c6 100644 --- a/doc/examples/daemon-depedencies/init.c +++ b/doc/examples/daemon-depedencies/init.c @@ -14,7 +14,8 @@ static char **argv; static char arg[4098]; -static void start_daemons() +static void +start_daemons() { int i; for (i = 1; i < argc; i++) @@ -23,7 +24,8 @@ static void start_daemons() } -static int callback(const char *message, void *user_data) +static int +callback(const char *message, void *user_data) { pid_t pid; char *arg2; @@ -91,7 +93,8 @@ fail: } -int main(int argc_, char *argv_[]) +int +main(int argc_, char *argv_[]) { char *bus_address = NULL; bus_t bus; diff --git a/doc/examples/daemon-depedencies/require.c b/doc/examples/daemon-depedencies/require.c index 6970dc2..e378431 100644 --- a/doc/examples/daemon-depedencies/require.c +++ b/doc/examples/daemon-depedencies/require.c @@ -10,7 +10,8 @@ static char arg[4098]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; int i; diff --git a/doc/examples/daemon-depedencies/start-daemon.c b/doc/examples/daemon-depedencies/start-daemon.c index acb85ad..7c224d5 100644 --- a/doc/examples/daemon-depedencies/start-daemon.c +++ b/doc/examples/daemon-depedencies/start-daemon.c @@ -11,7 +11,8 @@ static char arg[4098]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { if (argc != 2) return fprintf(stderr, "This program should be called from ./init\n"), 2; diff --git a/doc/examples/daemon-depedencies/test-daemon.c b/doc/examples/daemon-depedencies/test-daemon.c index 386d107..32ee545 100644 --- a/doc/examples/daemon-depedencies/test-daemon.c +++ b/doc/examples/daemon-depedencies/test-daemon.c @@ -9,7 +9,8 @@ static char arg[4098]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; if (argc != 3) diff --git a/doc/examples/nonblocking/cleanup.c b/doc/examples/nonblocking/cleanup.c index 00f07bc..bf54226 100644 --- a/doc/examples/nonblocking/cleanup.c +++ b/doc/examples/nonblocking/cleanup.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_unlink("/tmp/example-bus") && (perror("cleanup"), 1); } diff --git a/doc/examples/nonblocking/init.c b/doc/examples/nonblocking/init.c index 870e10d..d1325fb 100644 --- a/doc/examples/nonblocking/init.c +++ b/doc/examples/nonblocking/init.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_create("/tmp/example-bus", 0, NULL) && (perror("init"), 1); } diff --git a/doc/examples/nonblocking/poll.c b/doc/examples/nonblocking/poll.c index 2b0c266..5609317 100644 --- a/doc/examples/nonblocking/poll.c +++ b/doc/examples/nonblocking/poll.c @@ -8,7 +8,8 @@ -int main() +int +main() { bus_t bus; const char *message; diff --git a/doc/examples/nonblocking/write.c b/doc/examples/nonblocking/write.c index 7fe9690..afe957b 100644 --- a/doc/examples/nonblocking/write.c +++ b/doc/examples/nonblocking/write.c @@ -11,7 +11,8 @@ static char message[BUS_MEMORY_SIZE]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; if (argc < 2) { diff --git a/doc/examples/telephony-and-music/cleanup.c b/doc/examples/telephony-and-music/cleanup.c index 00f07bc..bf54226 100644 --- a/doc/examples/telephony-and-music/cleanup.c +++ b/doc/examples/telephony-and-music/cleanup.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_unlink("/tmp/example-bus") && (perror("cleanup"), 1); } diff --git a/doc/examples/telephony-and-music/end-call.c b/doc/examples/telephony-and-music/end-call.c index 6198cbb..c03eedf 100644 --- a/doc/examples/telephony-and-music/end-call.c +++ b/doc/examples/telephony-and-music/end-call.c @@ -11,7 +11,8 @@ static char message[BUS_MEMORY_SIZE]; -int main() +int +main() { bus_t bus; sprintf(message, "%ji unforce-pause", (intmax_t)getppid()); diff --git a/doc/examples/telephony-and-music/init.c b/doc/examples/telephony-and-music/init.c index 870e10d..d1325fb 100644 --- a/doc/examples/telephony-and-music/init.c +++ b/doc/examples/telephony-and-music/init.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_create("/tmp/example-bus", 0, NULL) && (perror("init"), 1); } diff --git a/doc/examples/telephony-and-music/monitor.c b/doc/examples/telephony-and-music/monitor.c index bf3e290..b3cc832 100644 --- a/doc/examples/telephony-and-music/monitor.c +++ b/doc/examples/telephony-and-music/monitor.c @@ -13,7 +13,8 @@ static char* pausers = NULL; -static int is_moc_playing(void) +static int +is_moc_playing(void) { return !WEXITSTATUS(system("env LANG=C mocp -i 2>/dev/null | grep 'State: PLAY' >/dev/null")); } @@ -25,7 +26,8 @@ static int is_moc_playing(void) * too much for an example, especially since it would also require * a mutex to make sure two threads do not modify data at the same * time, causing chaos. */ -static int callback(const char *message, void *user_data) +static int +callback(const char *message, void *user_data) { char *msg = NULL; size_t len = 0; @@ -83,7 +85,8 @@ done: -int main() +int +main() { bus_t bus; t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); diff --git a/doc/examples/telephony-and-music/receive-or-make-call.c b/doc/examples/telephony-and-music/receive-or-make-call.c index 92132fa..c61dc30 100644 --- a/doc/examples/telephony-and-music/receive-or-make-call.c +++ b/doc/examples/telephony-and-music/receive-or-make-call.c @@ -11,7 +11,8 @@ static char message[BUS_MEMORY_SIZE]; -int main() +int +main() { bus_t bus; sprintf(message, "%ji force-pause", (intmax_t)getppid()); diff --git a/doc/examples/timed/.gitignore b/doc/examples/timed/.gitignore index e5c1856..b1076f0 100644 --- a/doc/examples/timed/.gitignore +++ b/doc/examples/timed/.gitignore @@ -2,4 +2,5 @@ cleanup init write poll +read diff --git a/doc/examples/timed/Makefile b/doc/examples/timed/Makefile index 1b1cbd3..fe1285c 100644 --- a/doc/examples/timed/Makefile +++ b/doc/examples/timed/Makefile @@ -1,4 +1,4 @@ -COMMANDS = init cleanup write poll +COMMANDS = init cleanup write poll read all: ${COMMANDS} diff --git a/doc/examples/timed/README b/doc/examples/timed/README index b0989bf..db34fa0 100644 --- a/doc/examples/timed/README +++ b/doc/examples/timed/README @@ -12,7 +12,10 @@ When you are done run ./cleanup. Running instances of ./poll will wait for new messages continuously, but with one second timeouts. -./poll, ./init and ./cleanup are run without any +Running instances of ./read will read for ten seconds +then time out. + +./poll, ./read, ./init and ./cleanup are run without any additional arguments. ./write is run with the message as the second argument. diff --git a/doc/examples/timed/cleanup.c b/doc/examples/timed/cleanup.c index 00f07bc..bf54226 100644 --- a/doc/examples/timed/cleanup.c +++ b/doc/examples/timed/cleanup.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_unlink("/tmp/example-bus") && (perror("cleanup"), 1); } diff --git a/doc/examples/timed/init.c b/doc/examples/timed/init.c index 870e10d..d1325fb 100644 --- a/doc/examples/timed/init.c +++ b/doc/examples/timed/init.c @@ -1,7 +1,8 @@ #include #include -int main() +int +main() { return bus_create("/tmp/example-bus", 0, NULL) && (perror("init"), 1); } diff --git a/doc/examples/timed/poll.c b/doc/examples/timed/poll.c index d8c5aac..d57d4ea 100644 --- a/doc/examples/timed/poll.c +++ b/doc/examples/timed/poll.c @@ -8,7 +8,8 @@ -int main() +int +main() { bus_t bus; const char *message; diff --git a/doc/examples/timed/read.c b/doc/examples/timed/read.c new file mode 100644 index 0000000..abe733f --- /dev/null +++ b/doc/examples/timed/read.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +static int +callback(const char *message, void *user_data) +{ + (void) user_data; + + if (message == NULL) + return 1; + + message = strchr(message, ' ') + 1; + if (!strcmp(message, "stop")) + return 0; + printf("%s\n", message); + return 1; +} + + +int +main() +{ + bus_t bus; + struct timespec timeout; + t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); + t(clock_gettime(CLOCK_MONOTONIC, &timeout)); + timeout.tv_sec += 10; + t(bus_read_timed(&bus, callback, NULL, &timeout, CLOCK_MONOTONIC)); + bus_close(&bus); + return 0; + +fail: + perror("poll"); + bus_poll_stop(&bus); + bus_close(&bus); + return 1; +} + diff --git a/doc/examples/timed/write.c b/doc/examples/timed/write.c index b6b4c16..250ba14 100644 --- a/doc/examples/timed/write.c +++ b/doc/examples/timed/write.c @@ -11,7 +11,8 @@ static char message[BUS_MEMORY_SIZE]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; if (argc < 2) { -- cgit v1.2.3-70-g09d2